aboutsummaryrefslogtreecommitdiff
path: root/httpd.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-05-17 23:39:10 +0700
committerShulhan <ms@kilabit.info>2022-05-17 23:51:00 +0700
commitbb08b5bb6ad6cf3e1c409db7d69ace4364e7bc8d (patch)
tree5fed4b58027ada9acafb5af0795e2acc1868325d /httpd.go
parentbe96c68a070474364690f0655b26dc8823eea293 (diff)
downloadrescached-bb08b5bb6ad6cf3e1c409db7d69ace4364e7bc8d.tar.xz
cmd/resolver: implement command to delete record on zone
Given the following command $ resolver zone.d rr delete <zone> <"@" | subdomain> <type> <class> <value> It will delete the record in zone by given subdomain, type, class, and value.
Diffstat (limited to 'httpd.go')
-rw-r--r--httpd.go45
1 files changed, 36 insertions, 9 deletions
diff --git a/httpd.go b/httpd.go
index c74717b..3c819bf 100644
--- a/httpd.go
+++ b/httpd.go
@@ -1301,14 +1301,19 @@ func (srv *Server) apiZonedRRAdd(epr *libhttp.EndpointRequest) (resb []byte, err
// # Response
//
// On success it will return all the records in the zone.
+//
+// On fail it will return,
+// - 400: if one of the parameter is invalid or empty.
+// - 404: if the record to be deleted not found.
func (srv *Server) apiZonedRRDelete(epr *libhttp.EndpointRequest) (resbody []byte, err error) {
var (
res = libhttp.EndpointResponse{}
req = zoneRecordRequest{}
- rr = dns.ResourceRecord{}
+ rr = &dns.ResourceRecord{}
- zone *dns.Zone
- ok bool
+ zone *dns.Zone
+ rrValue string
+ ok bool
)
res.Code = http.StatusBadRequest
@@ -1344,27 +1349,49 @@ func (srv *Server) apiZonedRRDelete(epr *libhttp.EndpointRequest) (resbody []byt
rr.Value = &dns.RDataSOA{}
case dns.RecordTypeMX:
rr.Value = &dns.RDataMX{}
+ default:
+ rr.Value = rrValue
}
- err = json.Unmarshal(req.recordRaw, &rr)
+ err = json.Unmarshal(req.recordRaw, rr)
if err != nil {
res.Message = "json.Unmarshal: " + err.Error()
return nil, &res
}
- if len(rr.Name) == 0 {
- res.Message = "invalid or empty ResourceRecord.Name"
- return nil, &res
+ rr.Name = strings.TrimRight(rr.Name, ".")
+
+ if rr.Type == dns.RecordTypePTR {
+ if len(rr.Name) == 0 {
+ res.Message = "empty PTR name"
+ return nil, &res
+ }
+ if len(rrValue) == 0 {
+ rr.Value = req.Name
+ } else {
+ rr.Value = rrValue + "." + req.Name
+ }
+ } else {
+ if len(rr.Name) == 0 {
+ rr.Name = req.Name
+ } else {
+ rr.Name += "." + req.Name
+ }
}
// Remove the RR from caches.
- err = srv.dns.RemoveCachesByRR(&rr)
+ rr, err = srv.dns.RemoveCachesByRR(rr)
if err != nil {
res.Message = err.Error()
return nil, &res
}
+ if rr == nil {
+ res.Code = http.StatusNotFound
+ res.Message = "record not found"
+ return nil, &res
+ }
// Remove the RR from zone file.
- err = zone.Remove(&rr)
+ err = zone.Remove(rr)
if err != nil {
res.Message = err.Error()
return nil, &res