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