]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/bind9/bin/named/notify.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / bind9 / bin / named / notify.c
1 /*
2  * Copyright (C) 2004, 2005  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: notify.c,v 1.30.18.3 2005/04/29 00:15:26 marka Exp $ */
19
20 #include <config.h>
21
22 #include <isc/log.h>
23 #include <isc/print.h>
24
25 #include <dns/message.h>
26 #include <dns/rdataset.h>
27 #include <dns/result.h>
28 #include <dns/view.h>
29 #include <dns/zone.h>
30 #include <dns/zt.h>
31
32 #include <named/log.h>
33 #include <named/notify.h>
34
35 /*! \file
36  * \brief
37  * This module implements notify as in RFC1996.
38  */
39
40 static void
41 notify_log(ns_client_t *client, int level, const char *fmt, ...) {
42         va_list ap;
43
44         va_start(ap, fmt);
45         ns_client_logv(client, DNS_LOGCATEGORY_NOTIFY, NS_LOGMODULE_NOTIFY,
46                        level, fmt, ap);
47         va_end(ap);
48 }
49
50 static void
51 respond(ns_client_t *client, isc_result_t result) {
52         dns_rcode_t rcode;
53         dns_message_t *message;
54         isc_result_t msg_result;
55
56         message = client->message;
57         rcode = dns_result_torcode(result);
58
59         msg_result = dns_message_reply(message, ISC_TRUE);
60         if (msg_result != ISC_R_SUCCESS)
61                 msg_result = dns_message_reply(message, ISC_FALSE);
62         if (msg_result != ISC_R_SUCCESS) {
63                 ns_client_next(client, msg_result);
64                 return;
65         }
66         message->rcode = rcode;
67         if (rcode == dns_rcode_noerror)
68                 message->flags |= DNS_MESSAGEFLAG_AA;
69         else
70                 message->flags &= ~DNS_MESSAGEFLAG_AA;
71         ns_client_send(client);
72 }
73
74 void
75 ns_notify_start(ns_client_t *client) {
76         dns_message_t *request = client->message;
77         isc_result_t result;
78         dns_name_t *zonename;
79         dns_rdataset_t *zone_rdataset;
80         dns_zone_t *zone = NULL;
81         char namebuf[DNS_NAME_FORMATSIZE];
82         char tsigbuf[DNS_NAME_FORMATSIZE + sizeof(": TSIG ''")];
83         dns_name_t *tsigname;
84
85         /*
86          * Interpret the question section.
87          */
88         result = dns_message_firstname(request, DNS_SECTION_QUESTION);
89         if (result != ISC_R_SUCCESS) {
90                 notify_log(client, ISC_LOG_NOTICE,
91                            "notify question section empty");
92                 goto formerr;
93         }
94
95         /*
96          * The question section must contain exactly one question.
97          */
98         zonename = NULL;
99         dns_message_currentname(request, DNS_SECTION_QUESTION, &zonename);
100         zone_rdataset = ISC_LIST_HEAD(zonename->list);
101         if (ISC_LIST_NEXT(zone_rdataset, link) != NULL) {
102                 notify_log(client, ISC_LOG_NOTICE,
103                            "notify question section contains multiple RRs");
104                 goto formerr;
105         }
106
107         /* The zone section must have exactly one name. */
108         result = dns_message_nextname(request, DNS_SECTION_ZONE);
109         if (result != ISC_R_NOMORE) {
110                 notify_log(client, ISC_LOG_NOTICE,
111                            "notify question section contains multiple RRs");
112                 goto formerr;
113         }
114
115         /* The one rdataset must be an SOA. */
116         if (zone_rdataset->type != dns_rdatatype_soa) {
117                 notify_log(client, ISC_LOG_NOTICE,
118                            "notify question section contains no SOA");
119                 goto formerr;
120         }
121
122         tsigname = NULL;
123         if (dns_message_gettsig(request, &tsigname) != NULL) {
124                 dns_name_format(tsigname, namebuf, sizeof(namebuf));
125                 snprintf(tsigbuf, sizeof(tsigbuf), ": TSIG '%s'", namebuf);
126         } else
127                 tsigbuf[0] = '\0';
128         dns_name_format(zonename, namebuf, sizeof(namebuf));
129         result = dns_zt_find(client->view->zonetable, zonename, 0, NULL,
130                              &zone);
131         if (result != ISC_R_SUCCESS)
132                 goto notauth;
133
134         switch (dns_zone_gettype(zone)) {
135         case dns_zone_master:
136         case dns_zone_slave:
137         case dns_zone_stub:     /* Allow dialup passive to work. */
138                 notify_log(client, ISC_LOG_INFO,
139                            "received notify for zone '%s'%s", namebuf, tsigbuf);
140                 respond(client, dns_zone_notifyreceive(zone,
141                         ns_client_getsockaddr(client), request));
142                 break;
143         default:
144                 goto notauth;
145         }
146         dns_zone_detach(&zone);
147         return;
148
149  notauth:
150         notify_log(client, ISC_LOG_NOTICE,
151                    "received notify for zone '%s'%s: not authoritative",
152                    namebuf, tsigbuf);
153         result = DNS_R_NOTAUTH;
154         goto failure;
155
156  formerr:
157         result = DNS_R_FORMERR;
158
159  failure:
160         if (zone != NULL)
161                 dns_zone_detach(&zone);
162         respond(client, result);
163 }