From bb08b5bb6ad6cf3e1c409db7d69ace4364e7bc8d Mon Sep 17 00:00:00 2001 From: Shulhan Date: Tue, 17 May 2022 23:39:10 +0700 Subject: cmd/resolver: implement command to delete record on zone Given the following command $ resolver zone.d rr delete <"@" | subdomain> It will delete the record in zone by given subdomain, type, class, and value. --- httpd.go | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) (limited to 'httpd.go') 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 -- cgit v1.3