notify.c revision 70e5a7403f0e0a3bd292b8287c5fed5772c15270
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Copyright (C) 1999-2003 Internet Software Consortium.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Permission to use, copy, modify, and/or distribute this software for any
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * purpose with or without fee is hereby granted, provided that the above
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * copyright notice and this permission notice appear in all copies.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * PERFORMANCE OF THIS SOFTWARE.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/* $Id: notify.c,v 1.37 2007/06/19 23:46:59 tbox Exp $ */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <config.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <isc/log.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <isc/print.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <dns/message.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <dns/rdataset.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <dns/result.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <dns/tsig.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <dns/view.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <dns/zone.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <dns/zt.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <named/log.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <named/notify.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*! \file
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * \brief
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * This module implements notify as in RFC1996.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonstatic void
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonnotify_log(ns_client_t *client, int level, const char *fmt, ...) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson va_list ap;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson va_start(ap, fmt);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_client_logv(client, DNS_LOGCATEGORY_NOTIFY, NS_LOGMODULE_NOTIFY,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson level, fmt, ap);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson va_end(ap);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonstatic void
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonrespond(ns_client_t *client, isc_result_t result) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson dns_rcode_t rcode;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson dns_message_t *message;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_result_t msg_result;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson message = client->message;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson rcode = dns_result_torcode(result);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson msg_result = dns_message_reply(message, ISC_TRUE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (msg_result != ISC_R_SUCCESS)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson msg_result = dns_message_reply(message, ISC_FALSE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (msg_result != ISC_R_SUCCESS) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_client_next(client, msg_result);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson message->rcode = rcode;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (rcode == dns_rcode_noerror)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson message->flags |= DNS_MESSAGEFLAG_AA;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson else
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson message->flags &= ~DNS_MESSAGEFLAG_AA;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_client_send(client);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonvoid
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonns_notify_start(ns_client_t *client) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson dns_message_t *request = client->message;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_result_t result;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson dns_name_t *zonename;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson dns_rdataset_t *zone_rdataset;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson dns_zone_t *zone = NULL;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson char namebuf[DNS_NAME_FORMATSIZE];
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson char tsigbuf[DNS_NAME_FORMATSIZE + sizeof(": TSIG ''")];
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson dns_tsigkey_t *tsigkey;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Interpret the question section.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson result = dns_message_firstname(request, DNS_SECTION_QUESTION);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (result != ISC_R_SUCCESS) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson notify_log(client, ISC_LOG_NOTICE,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "notify question section empty");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson goto formerr;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * The question section must contain exactly one question.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson zonename = NULL;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson dns_message_currentname(request, DNS_SECTION_QUESTION, &zonename);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson zone_rdataset = ISC_LIST_HEAD(zonename->list);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (ISC_LIST_NEXT(zone_rdataset, link) != NULL) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson notify_log(client, ISC_LOG_NOTICE,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "notify question section contains multiple RRs");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson goto formerr;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /* The zone section must have exactly one name. */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson result = dns_message_nextname(request, DNS_SECTION_ZONE);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (result != ISC_R_NOMORE) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson notify_log(client, ISC_LOG_NOTICE,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "notify question section contains multiple RRs");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson goto formerr;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /* The one rdataset must be an SOA. */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (zone_rdataset->type != dns_rdatatype_soa) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson notify_log(client, ISC_LOG_NOTICE,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "notify question section contains no SOA");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson goto formerr;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson tsigkey = dns_message_gettsigkey(request);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (tsigkey != NULL) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson dns_name_format(&tsigkey->name, namebuf, sizeof(namebuf));
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (tsigkey->generated) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson char cnamebuf[DNS_NAME_FORMATSIZE];
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson dns_name_format(tsigkey->creator, cnamebuf,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson sizeof(cnamebuf));
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson snprintf(tsigbuf, sizeof(tsigbuf), ": TSIG '%s' (%s)",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson namebuf, cnamebuf);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson snprintf(tsigbuf, sizeof(tsigbuf), ": TSIG '%s'",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson namebuf);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson } else
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson tsigbuf[0] = '\0';
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson dns_name_format(zonename, namebuf, sizeof(namebuf));
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson result = dns_zt_find(client->view->zonetable, zonename, 0, NULL,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson &zone);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (result != ISC_R_SUCCESS)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson goto notauth;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson switch (dns_zone_gettype(zone)) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson case dns_zone_master:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson case dns_zone_slave:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson case dns_zone_stub: /* Allow dialup passive to work. */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson notify_log(client, ISC_LOG_INFO,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "received notify for zone '%s'%s", namebuf, tsigbuf);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson respond(client, dns_zone_notifyreceive(zone,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_client_getsockaddr(client), request));
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson break;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson default:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson goto notauth;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson dns_zone_detach(&zone);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson notauth:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson notify_log(client, ISC_LOG_NOTICE,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "received notify for zone '%s'%s: not authoritative",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson namebuf, tsigbuf);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson result = DNS_R_NOTAUTH;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson goto failure;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson formerr:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson result = DNS_R_FORMERR;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson failure:
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (zone != NULL)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson dns_zone_detach(&zone);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson respond(client, result);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson