Merge from vendor branch OPENSSL:
[dragonfly.git] / contrib / bind-9.3 / bin / named / notify.c
1 /*
2  * Copyright (C) 2004  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.24.2.2.2.7 2004/08/28 06:25:30 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 /*
36  * This module implements notify as in RFC 1996.
37  */
38
39 static void
40 notify_log(ns_client_t *client, int level, const char *fmt, ...) {
41         va_list ap;
42
43         va_start(ap, fmt);
44         ns_client_logv(client, DNS_LOGCATEGORY_NOTIFY, NS_LOGMODULE_NOTIFY,
45                        level, fmt, ap);
46         va_end(ap);
47 }
48
49 static void
50 respond(ns_client_t *client, isc_result_t result) {
51         dns_rcode_t rcode;
52         dns_message_t *message;
53         isc_result_t msg_result;
54
55         message = client->message;
56         rcode = dns_result_torcode(result);
57
58         msg_result = dns_message_reply(message, ISC_TRUE);
59         if (msg_result != ISC_R_SUCCESS)
60                 msg_result = dns_message_reply(message, ISC_FALSE);
61         if (msg_result != ISC_R_SUCCESS) {
62                 ns_client_next(client, msg_result);
63                 return;
64         }
65         message->rcode = rcode;
66         if (rcode == dns_rcode_noerror)
67                 message->flags |= DNS_MESSAGEFLAG_AA;
68         else
69                 message->flags &= ~DNS_MESSAGEFLAG_AA;
70         ns_client_send(client);
71 }
72
73 void
74 ns_notify_start(ns_client_t *client) {
75         dns_message_t *request = client->message;
76         isc_result_t result;
77         dns_name_t *zonename;
78         dns_rdataset_t *zone_rdataset;
79         dns_zone_t *zone = NULL;
80         char namebuf[DNS_NAME_FORMATSIZE];
81         char tsigbuf[DNS_NAME_FORMATSIZE + sizeof(": TSIG ''")];
82         dns_name_t *tsigname;
83
84         /*
85          * Interpret the question section.
86          */
87         result = dns_message_firstname(request, DNS_SECTION_QUESTION);
88         if (result != ISC_R_SUCCESS) {
89                 notify_log(client, ISC_LOG_NOTICE,
90                            "notify question section empty");
91                 goto formerr;
92         }
93
94         /*
95          * The question section must contain exactly one question.
96          */
97         zonename = NULL;
98         dns_message_currentname(request, DNS_SECTION_QUESTION, &zonename);
99         zone_rdataset = ISC_LIST_HEAD(zonename->list);
100         if (ISC_LIST_NEXT(zone_rdataset, link) != NULL) {
101                 notify_log(client, ISC_LOG_NOTICE,
102                            "notify question section contains multiple RRs");
103                 goto formerr;
104         }
105
106         /* The zone section must have exactly one name. */
107         result = dns_message_nextname(request, DNS_SECTION_ZONE);
108         if (result != ISC_R_NOMORE) {
109                 notify_log(client, ISC_LOG_NOTICE,
110                            "notify question section contains multiple RRs");
111                 goto formerr;
112         }
113
114         /* The one rdataset must be an SOA. */
115         if (zone_rdataset->type != dns_rdatatype_soa) {
116                 notify_log(client, ISC_LOG_NOTICE,
117                            "notify question section contains no SOA");
118                 goto formerr;
119         }
120
121         tsigname = NULL;
122         if (dns_message_gettsig(request, &tsigname) != NULL) {
123                 dns_name_format(tsigname, namebuf, sizeof(namebuf));
124                 snprintf(tsigbuf, sizeof(tsigbuf), ": TSIG '%s'", namebuf);
125         } else
126                 tsigbuf[0] = '\0';
127         dns_name_format(zonename, namebuf, sizeof(namebuf));
128         result = dns_zt_find(client->view->zonetable, zonename, 0, NULL,
129                              &zone);
130         if (result != ISC_R_SUCCESS)
131                 goto notauth;
132
133         switch (dns_zone_gettype(zone)) {
134         case dns_zone_master:
135         case dns_zone_slave:
136         case dns_zone_stub:     /* Allow dialup passive to work. */
137                 notify_log(client, ISC_LOG_INFO,
138                            "received notify for zone '%s'%s", namebuf, tsigbuf);
139                 respond(client, dns_zone_notifyreceive(zone,
140                         ns_client_getsockaddr(client), request));
141                 break;
142         default:
143                 goto notauth;
144         }
145         dns_zone_detach(&zone);
146         return;
147
148  notauth:
149         notify_log(client, ISC_LOG_NOTICE,
150                    "received notify for zone '%s'%s: not authoritative",
151                    namebuf, tsigbuf);
152         result = DNS_R_NOTAUTH;
153         goto failure;
154
155  formerr:
156         result = DNS_R_FORMERR;
157
158  failure:
159         if (zone != NULL)
160                 dns_zone_detach(&zone);
161         respond(client, result);
162 }