#if !defined(lint) && !defined(SABER)
static char sccsid
[] = "@(#)ns_req.c 4.47 (Berkeley) 7/1/91";
static char rcsid
[] = "$Id: ns_req.c,v 1.1 1993/06/01 02:33:47 vixie Exp vixie $";
* ++Copyright++ 1986, 1988, 1990
* Copyright (c) 1986, 1988, 1990 Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* Portions Copyright (c) 1993 by Digital Equipment Corporation.
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies, and that
* the name of Digital Equipment Corporation not be used in advertising or
* publicity pertaining to distribution of the document or software without
* specific, written prior permission.
* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
#include <arpa/nameser.h>
#include "../conf/portability.h"
#include "../conf/options.h"
char *a_dname
; /* domain name */
u_short a_class
; /* class for address */
static struct addinfo addinfo
[NADDRECS
]; /* additional info records */
int addcount
; /* number of names in addinfo */
int xfr_disabled
= 0; /* set to disable zone xfrs */
int needs_prime_cache
= 0; /* set if we need a priming */
u_char
*dnptrs
[20]; /* ptrs to dnames in message for dn_comp */
extern int writemsg(); /* db_glue.c */
extern int dn_comp(), add_data();
extern void prime_cache();
extern time_t retrytime();
extern struct qinfo
*sysquery();
extern char *localdomain
;
extern struct netinfo
*xfrnets
;
int stale(), make_rr(), doaddinfo(), doaddauth();
void ns_req(), fwritemsg(), getname(), doaxfr(), startxfr();
* Process request using database; assemble and send response.
ns_req(msg
, msglen
, buflen
, qsp
, from
, dfd
)
struct sockaddr_in
*from
;
register struct databuf
*dp
;
char dnbuf
[MAXDNAME
], *dname
;
int n
, class, type
, count
, foundname
, founddata
, omsglen
, cname
= 0;
struct databuf
*nsp
[NSMAX
];
extern struct netinfo
*local();
extern struct fwdinfo
*fwdtab
;
fprintf(ddt
,"ns_req()\n");
fp_query((char *)msg
, ddt
);
/* Now is a safe time for housekeeping */
cp
= msg
+ sizeof(HEADER
);
if (ntohs(hp
->qdcount
) != 1 ||
hp
->ancount
|| hp
->nscount
|| hp
->arcount
) {
fprintf(ddt
,"FORMERR Query header counts wrong\n");
* Get domain name, class, and type.
if ((*cp
& INDIR_MASK
) == 0)
*dpp
++ = cp
; /* remember name for compression */
if ((n
= dn_expand(msg
, eom
, cp
, (u_char
*)dnbuf
,
fprintf(ddt
,"FORMERR Query expand name failed\n");
fprintf(ddt
,"FORMERR Query message length short\n");
fprintf(ddt
,"message length > received message\n");
if ((type
> T_ANY
) || (type
< 0))
typestats
[0]++; /* Bad type */
/* refuse request if not a TCP connection */
if (qsp
== QSTREAM_NULL
|| xfr_disabled
) {
"T_AXFR via UDP refused\n");
/* if xfrnets was specified, peer address
* must be on it. should probably allow
* for negation some day. (vix@decwrl)
int slen
= (sizeof saddr
);
struct sockaddr_in
*iaddr
=
(struct sockaddr_in
*) &saddr
;
if (-1 == getpeername(qsp
->s_rfd
,
syslog(LOG_WARNING
, "getpeername: %m");
if (saddr
.sa_family
!= AF_INET
) {
syslog(LOG_WARNING
, "af%d???",
if (!net_on_netlist(iaddr
->sin_addr
,
"dangerous TCP addr: %s %u",
inet_ntoa(iaddr
->sin_addr
),
dnptrs
[0] = NULL
; /* don't compress names */
hp
->rd
= 0; /* recursion not possible */
syslog(LOG_INFO
, "XX /%s/%s/%s",
inet_ntoa(from
->sin_addr
),
dname
[0] == '\0' ? "." : dname
,
fprintf(ddt
,"req: nlookup(%s) id %d type=%d\n",
htp
= hashtab
; /* lookup relative to root */
if ((np
= nlookup(dname
, &htp
, &fname
, 0)) == NULL
)
fprintf(ddt
,"req: %s '%s' as '%s' (cname=%d)\n",
np
== NULL
? "missed" : "found",
* if nlookup failed to find the name then
* see if there are any '.''s in the name
* if not then add local domain name to the
if (np
== NULL
&& localdomain
&& strchr(dname
, '.') == NULL
) {
(void) strcat(dname
,".");
(void) strcat(dname
, localdomain
);
fprintf(ddt
,"req: nlookup(%s) type=%d\n",
np
= nlookup(dname
, &htp
, &fname
, 0);
/* Some braindamaged resolver software will not
recognize internet addresses in dot notation and
send out address queries for "names" such as
128.93.8.1. This kludge will prevent those
from flooding higher level servers.
We simply claim to be authoritative and that
the domain doesn't exist.
Note that we could return the address but we
don't do that in order to encourage that broken
if (np
==NULL
&& type
==T_A
&& class == C_IN
&& dname
)
if (!inet_aton(dname
, &ina
))
fprintf(ddt
, "ypkludge: hit as '%s'\n",
if (np
== NULL
|| fname
!= dname
)
n
= finddata(np
, class, type
, hp
, &dname
, &buflen
, &count
);
/* NO data available. Refuse AXFR requests, or
* look for better servers for other requests.
"T_AXFR refused: no data\n");
if (fname
!= dname
&& type
!= T_CNAME
&& type
!= T_ANY
) {
if (cname
++ >= MAXCNAMES
) {
"resp: leaving, MAXCNAMES exceeded\n");
fprintf(ddt
, "req: foundname = %d count = %d ",
fprintf(ddt
, "founddata = %d cname = %d\n",
if ((lp
= local(from
)) != NULL
)
sort_response(answers
, count
, lp
, cp
);
hp
->ancount
= htons(hp
->ancount
);
startxfr(qsp
, np
, msg
, cp
- msg
);
* If we found an authoritative answer,
* Look for name servers to refer to and fill in the authority
* section or record the address for forwarding the query
count
= 0; /* del@harris */
switch (findns(&np
, class, nsp
, &count
)) {
fprintf(ddt
,"req: leaving (%s, rcode %d)\n",
* should return SOA if founddata == 0,
* but old named's are confused by an SOA
* in the auth. section if there's no error.
if (foundname
== 0 && np
) {
n
= doaddauth(hp
, cp
, buflen
, np
, nsp
[0]);
if (!founddata
&& !(forward_only
&& fwdtab
)) {
* If we successfully found the answer in the cache
* or this is not a recursive query, then add the
* nameserver references ("authority section") here
if (founddata
|| (!hp
->rd
)) {
n
= add_data(np
, nsp
, cp
, buflen
);
hp
->nscount
= htons((u_short
)count
);
* At this point, we don't have the answer, but we do
* have some NS's to try. If the user would like us
* to recurse, create the initial query. If a cname
* is involved, we need to build a new query and save
* the old one in cmsg/cmsglen.
omsg
= (u_char
*)malloc((unsigned)msglen
);
if (omsg
== (u_char
*)NULL
) {
fprintf(ddt
,"ns_req: malloc fail\n");
syslog(LOG_ERR
, "ns_req: Out Of Memory");
hp
->ancount
= htons(hp
->ancount
);
bcopy(msg
, omsg
, omsglen
= msglen
);
msglen
= res_mkquery(QUERY
, dname
, class, type
,
(char *)NULL
, 0, NULL
, (char *)msg
,
n
= ns_forw(nsp
, msg
, msglen
, from
, qsp
, dfd
, &qp
, dname
);
qp
->q_cmsg
= (char *)omsg
;
break; /* Duplicate request dropped */
** Don't go into an infinite loop if
** the admin gave root NS records in the cache
** file without giving address records
if(np
->n_dname
[0] == '\0'){
"ns_req: no address for root server\n");
"ns_req: no address for root server");
goto fetchns
; /* Try again. */
/* Now is a safe time for housekeeping */
register struct invbuf
*ip
;
char anbuf
[PACKETSZ
], *data
;
hp
->ancount
= htons(hp
->ancount
);
hp
->qdcount
|| hp
->nscount
|| hp
->arcount
) {
fprintf(ddt
,"FORMERR IQuery header counts wrong\n");
* Skip domain name, get class, and type.
if ((n
= dn_skipname(cp
, eom
)) < 0) {
fprintf(ddt
,"FORMERR IQuery packet name problem\n");
fprintf(ddt
,"FORMERR IQuery message length off\n");
/* not all inverse queries are handled. */
fprintf(ddt
,"req: IQuery class %d type %d\n",
fname
= (char *)msg
+ sizeof(HEADER
);
bcopy(fname
, anbuf
, alen
= (char *)cp
- fname
);
data
= anbuf
+ alen
- dlen
;
buflen
-= sizeof(HEADER
);
for (ip
= invtab
[dhash(data
, dlen
)]; ip
!= NULL
;
for (i
= 0; i
< INVBLKSZ
; i
++) {
if ((np
= ip
->i_dname
[i
]) == NULL
)
fprintf(ddt
,"dname = %d\n", np
->n_dname
);
for (dp
= np
->n_data
; dp
!= NULL
; dp
= dp
->d_next
) {
if (!match(dp
, class, type
))
if (dp
->d_size
!= dlen
||
bcmp(dp
->d_data
, data
, dlen
))
getname(np
, dnbuf
, sizeof(dnbuf
));
fprintf(ddt
,"req: IQuery found %s\n",
if ((n
= dn_comp((u_char
*)dnbuf
, cp
, buflen
,
(u_char
**)NULL
, (u_char
**)NULL
)) < 0)
PUTSHORT((u_short
)dp
->d_type
, cp
);
PUTSHORT((u_short
)dp
->d_class
, cp
);
fprintf(ddt
,"req: IQuery %d records\n", count
);
hp
->qdcount
= htons((u_short
)count
);
* In a sense the following constant should be defined in <arpa/nameser.h>,
* since it is returned here in place of a response code if the update was
* forwarded, and the response codes are defined in nameser.h. On the other
* hand, though, this constant is only seen in this file. The assumption
* here is that none of the other return codes equals this one (a good
* assumption, since they only occupy 4 bits over-the-wire)
/* Call InitDynUpdate for all dynamic update requests */
n
= InitDynUpdate(hp
, nsp
, msg
, msglen
, cp
, from
, qsp
, dfd
);
return; /* Return directly because InitDynUpdate
* forwarded the query to the primary, so we
* will send response later
break; /* Either sucessful primary update or failure;
* return response code to client
#endif /* ALLOW_UPDATES */
fprintf(ddt
,"Refresh Zone\n");
fprintf(ddt
,"Opcode %d not implemented\n", hp
->opcode
);
stats
[S_RESPFORMERR
].cnt
++;
hp
->qr
= 1; /* set Response flag */
hp
->ra
= 1; /* Recursion is Available */
hp
->ancount
= htons(hp
->ancount
);
n
= doaddinfo(hp
, cp
, buflen
);
fprintf(ddt
,"req: answer -> %s %d (%d) id=%d %s\n",
inet_ntoa(from
->sin_addr
),
qsp
== QSTREAM_NULL
? dfd
: qsp
->s_rfd
,
ntohs(hp
->id
), local(from
) == NULL
? "Remote" : "Local");
fp_query((char *)msg
, ddt
);
if (qsp
== QSTREAM_NULL
) {
if (sendto(dfd
, msg
, cp
-msg
, 0, (struct sockaddr
*)from
,
fprintf(ddt
,"error returning msg errno=%d\n",errno
);
(void) writemsg(qsp
->s_rfd
, msg
, cp
- msg
);
prime_cache(); /* Now is a safe time */
fwritemsg(rfp
, msg
, msglen
)
u_short len
= htons((u_short
)msglen
);
if (fwrite((char *)&len
, sizeof(len
), 1, rfp
) != 1 ||
fwrite(msg
, msglen
, 1, rfp
) != 1) {
fprintf(ddt
,"fwrite failed %d\n", errno
);
* Test a datum for validity and return non-zero if it is out of date.
register struct databuf
*dp
;
register struct zoneinfo
*zp
= &zones
[dp
->d_zone
];
* Check to see whether a secondary zone
* has expired; if so clear authority flag
* for zone and return true. If lastupdate
* is in the future, assume zone is up-to-date.
if ((int32_t)(tt
.tv_sec
- zp
->z_lastupdate
) > (int32_t)zp
->z_expire
){
"stale: secondary zone %s expired\n",
if (!haveComplained(zp
->z_origin
, (char*)stale
)) {
"secondary zone \"%s\" expired",
fprintf(ddt
,"stale: ttl %d %d (x%x)\n",
dp
->d_ttl
, dp
->d_ttl
- tt
.tv_sec
, dp
->d_flags
);
if (dp
->d_flags
& DB_F_HINT
)
return(dp
->d_ttl
< tt
.tv_sec
);
* Copy databuf into a resource record for replies.
* Return size of RR if OK, -1 if buffer is full.
make_rr(name
, dp
, buf
, buflen
, doadd
)
register struct databuf
*dp
;
u_char
**edp
= dnptrs
+ sizeof(dnptrs
)/sizeof(dnptrs
[0]);
fprintf(ddt
,"make_rr(%s, %x, %x, %d, %d) %d zone %d ttl %d\n",
buflen
, doadd
, dp
->d_size
, dp
->d_zone
, dp
->d_ttl
);
/* check for outdated RR before updating dnptrs by dn_comp() (???) */
if (zp
->z_type
== Z_CACHE
) {
ttl
= dp
->d_ttl
- (u_int32_t
) tt
.tv_sec
;
if ((dp
->d_flags
& DB_F_HINT
) || (ttl
< 0)) {
/*XXX*/if (debug
>= 3) fprintf(ddt
,"make_rr: %d=>0, x%x\n", ttl
, dp
->d_flags
);
ttl
= zp
->z_minimum
; /* really default */
#ifdef notdef /* don't decrease ttl based on time since verification */
if (zp
->z_type
== Z_SECONDARY
) {
* Set ttl to value received from primary,
* less time since we verified it (but never
* less than a small positive value).
ttl
-= tt
.tv_sec
- zp
->z_lastupdate
;
if ((n
= dn_comp((u_char
*)name
, (u_char
*)buf
, buflen
,
(u_char
**)dnptrs
, (u_char
**)edp
)) < 0)
PUTSHORT((u_short
)dp
->d_type
, cp
);
PUTSHORT((u_short
)dp
->d_class
, cp
);
if ((n
= dn_comp((u_char
*)dp
->d_data
, (u_char
*)cp
, buflen
,
(u_char
**)dnptrs
, (u_char
**)edp
)) < 0)
PUTSHORT((u_short
)n
, sp
);
/* Store domain name in answer */
if ((n
= dn_comp((u_char
*)dp
->d_data
, (u_char
*)cp
, buflen
,
(u_char
**)dnptrs
, (u_char
**)edp
)) < 0)
PUTSHORT((u_short
)n
, sp
);
addname((char*)dp
->d_data
, dp
->d_class
);
if ((n
= dn_comp((u_char
*)cp1
, (u_char
*)cp
, buflen
,
(u_char
**)dnptrs
, (u_char
**)edp
)) < 0)
buflen
-= dp
->d_type
== T_SOA
? n
+ 5 * sizeof(u_int32_t
) : n
;
cp1
+= strlen((char *)cp1
) + 1;
if ((n
= dn_comp((u_char
*)cp1
, (u_char
*)cp
, buflen
,
(u_char
**)dnptrs
, (u_char
**)edp
)) < 0)
if (dp
->d_type
== T_SOA
) {
cp1
+= strlen((char *)cp1
) + 1;
(int)(n
= dp
->d_size
- (cp1
- dp
->d_data
)));
n
= (u_short
)((cp
- sp
) - sizeof(u_short
));
PUTSHORT((u_short
)n
, sp
);
/* cp1 == our data/ cp == data of RR */
bcopy(cp1
,cp
,sizeof(u_short
));
buflen
-= sizeof(u_short
);
if ((n
= dn_comp((u_char
*)cp1
, (u_char
*)cp
, buflen
,
(u_char
**)dnptrs
, (u_char
**)edp
)) < 0)
n
= (u_short
)((cp
- sp
) - sizeof(u_short
));
PUTSHORT((u_short
)n
, sp
);
addname((char*)cp1
, dp
->d_class
);
bcopy(dp
->d_data
, cp
, dp
->d_size
);
PUTSHORT((u_short
)dp
->d_size
, sp
);
register struct addinfo
*ap
;
for (ap
= addinfo
, n
= addcount
; --n
>= 0; ap
++)
if (strcasecmp(ap
->a_dname
, name
) == 0)
/* add domain name to additional section */
if (addcount
< NADDRECS
) {
* Lookup addresses for names in addinfo and put into the message's
doaddinfo(hp
, msg
, msglen
)
register struct namebuf
*np
;
register struct databuf
*dp
;
register struct addinfo
*ap
;
fprintf(ddt
, "doaddinfo() addcount = %d\n", addcount
);
fprintf(ddt
, "doaddinfo(): tc already set, bailing\n");
for (ap
= addinfo
; --addcount
>= 0; ap
++) {
fprintf(ddt
,"do additional '%s'\n", ap
->a_dname
);
htp
= hashtab
; /* because "nlookup" stomps on arg. */
np
= nlookup(ap
->a_dname
, &htp
, &fname
, 0);
if (np
== NULL
|| fname
!= ap
->a_dname
)
fprintf(ddt
,"found it\n");
for (dp
= np
->n_data
; dp
!= NULL
; dp
= dp
->d_next
) {
if (!match(dp
, (int)ap
->a_class
, T_A
))
"doaddinfo: stale entry '%s'%s\n",
* Should be smart and eliminate duplicate
if ((n
= make_rr(ap
->a_dname
, dp
, cp
, msglen
, 0)) < 0){
/* truncation in the additional-data section
* is not all that serious. we do not set TC,
* since the answer and authority sections are
* OK; however, since we're not setting TC we
* have to make sure that none of the RR's for
* this name go out (!TC implies that all
* {name,type} appearances are complete -- and
* since we only do A RR's here, the name is
* the key). vixie, 23apr93
"addinfo: adding address data n = %d\n",
next_rr
: if (foundstale
) {
/* Cache invalidate the address RR's */
delete_all(np
, (int)ap
->a_class
, T_A
);
if (foundstale
|| !foundany
) {
/* ask a real server for this info */
(void) sysquery(ap
->a_dname
, (int)ap
->a_class
, T_A
);
hp
->arcount
= htons((u_short
)count
);
doaddauth(hp
, cp
, buflen
, np
, dp
)
getname(np
, dnbuf
, sizeof(dnbuf
));
if (stale(dp
) || (n
= make_rr(dnbuf
, dp
, cp
, buflen
, 1)) <= 0) {
fprintf(ddt
, "doaddauth: can't add '%s' (%d) (n=%d)\n",
* Get the domain name of 'np' and put in 'buf'. Bounds checking is done.
if ((i
= strlen(np
->n_dname
))+1 >= buflen
) {
syslog(LOG_ERR
, "domain name too long: %s...\n", buf
);
strcpy(buf
, "Name_Too_Long");
(void) strcpy(cp
, np
->n_dname
);
* Do a zone transfer (or a recursive part of a zone transfer).
* SOA record already sent.
* top always refers to the domain at the top of the zone being transferred.
* np refers to a domain inside the zone being transferred,
* which will be equal to top if this is the first call,
* or will be a subdomain below top if this is a recursive call,
* rfp is a stdio file to which output is sent.
register struct namebuf
*np
;
register struct databuf
*dp
;
struct databuf
*gdp
; /* glue databuf */
struct namebuf
*gnp
; /* glue namebuf */
struct namebuf
*tnp
; /* top namebuf */
struct databuf
*tdp
; /* top databuf */
struct namebuf
**npp
, **nppend
;
HEADER
*hp
= (HEADER
*) msg
;
if (debug
&& (np
== top
))
fprintf(ddt
,"doaxfr()\n");
hp
->aa
= hp
->tc
= hp
->ra
= hp
->pr
= hp
->rd
= 0;
cp
= (u_char
*) (msg
+ sizeof(HEADER
));
getname(np
, dname
, sizeof(dname
));
/* first do the NS records (del@harris) */
for (dp
= np
->n_data
; dp
!= NULL
; dp
= dp
->d_next
) {
if (dp
->d_type
== T_NS
) {
if ((n
= make_rr(dname
, dp
, cp
,
sizeof(msg
)-sizeof(HEADER
), 0)) < 0)
fwritemsg(rfp
, msg
, n
+ sizeof(HEADER
));
/* Glue the sub domains together by sending
* the address records for the sub domain
* name servers along if necessary.
* Glue is necessary if the server is in any zone
* delegated from the current (top) zone. Such
* a delegated zone might or might not be that
* referred to by the NS record now being handled.
cp
= (u_char
*) (msg
+ sizeof(HEADER
));
gnp
= nlookup((caddr_t
)dp
->d_data
, &htp
, &fname
, 0);
if (gnp
== NULL
|| fname
!= (caddr_t
)dp
->d_data
)
for (tnp
= gnp
; tnp
!= NULL
; tnp
= tnp
->n_parent
)
continue; /* name server is not below top domain */
for (tnp
= gnp
; tnp
!= top
; tnp
= tnp
->n_parent
) {
for (tdp
= tnp
->n_data
; tdp
!= NULL
; tdp
= tdp
->d_next
)
break; /* found a zone cut */
continue; /* name server is not in a delegated zone */
/* now we know glue records are needed. send them. */
for(gdp
=gnp
->n_data
; gdp
!= NULL
; gdp
=gdp
->d_next
) {
if (gdp
->d_type
!= T_A
|| stale(gdp
))
if ((n
= make_rr(fname
, gdp
, cp
,
sizeof(msg
)-sizeof(HEADER
), 0)) < 0)
fwritemsg(rfp
, msg
, n
+ sizeof(HEADER
));
/* no need to send anything else because of delegation */
if ((np
!= top
) && fndns
)
/* do the rest of the data records */
for (dp
= np
->n_data
; dp
!= NULL
; dp
= dp
->d_next
) {
* Skip the top SOA record (marks end of data);
* don't send SOA for subdomains, as we're not sending them;
* skip the NS records because we did them first.
if (dp
->d_type
== T_SOA
|| dp
->d_type
== T_NS
)
if (dp
->d_zone
== 0 || stale(dp
))
if ((n
= make_rr(dname
, dp
, cp
, sizeof(msg
)-sizeof(HEADER
), 0)) < 0)
fwritemsg(rfp
, msg
, n
+ sizeof(HEADER
));
/* Finally do non-delegated subdomains. Delegated subdomains
* have already been handled.
* We find the subdomains by looking in the hash table for this
* domain, but the root domain needs special treatment, because
* of the following wart in the database design:
* The top level hash table (pointed to by the global `hashtab'
* variable) contains pointers to the namebuf's for the root as
* well as for the top-level domains below the root, in contrast
* to the usual situation where a hash table contains entries
* for domains at the same level. The n_hash member of the
* namebuf for the root domain is NULL instead of pointing to a
* hashbuf for the top-level domains. The n_parent members of
* the namebufs for the top-level domains are NULL instead of
* pointing to the namebuf for the root.
* We work around the wart as follows:
* If we are not dealing with the root zone then we just set
* htp = np->n_hash, pointing to the hash table for the current
* domain, and we walk through the hash table as usual,
* processing the namebufs for all the subdomains.
* If we are dealing with the root zone, then we set
* htp = hashtab, pointing to the global hash table (because
* there is no hash table associated with the root domain's
* namebuf. While we walk this hash table, we take care not to
* recursively process the entry for the root namebuf.
htp
= ((dname
[0] == '\0') ? hashtab
: np
->n_hash
);
return; /* no subdomains */
nppend
= npp
+ htp
->h_size
;
for (np
= *npp
++; np
!= NULL
; np
= np
->n_next
) {
if (np
->n_dname
[0] != '\0') { /* don't redo root domain */
if (debug
&& (np
== top
))
fprintf(ddt
,"exit doaxfr()\n");
* Called by UPDATE{A,D,DA,M,MA} to initiate a dynamic update. If this is the
* primary server for the zone being updated, we update the zone's serial
* number and then call doupdate directly. If this is a secondary, we just
* forward the update; this way, if the primary update fails (e.g., if the
* primary is unavailable), we don't update the secondary; if the primary
* update suceeds, ns_resp will get called with the response (when it comes
* in), and then update the secondary's copy.
InitDynUpdate(hp
, nsp
, msg
, msglen
, startcp
, from
, qsp
, dfd
)
struct sockaddr_in
*from
;
struct hashbuf
*htp
= hashtab
; /* lookup relative to root */
struct databuf
*olddp
, *newdp
, *dp
;
register u_char
*cp
= startcp
;
char ZoneName
[MAXDNAME
], *znp
;
if ((n
= dn_expand(msg
, msg
+ msglen
, cp
, dnbuf
, sizeof(dnbuf
))) < 0) {
fprintf(ddt
,"FORMERR InitDynUpdate expand name failed\n");
if (type
== T_SOA
) { /* T_SOA updates not allowed */
fprintf(ddt
, "InitDynUpdate: REFUSED - SOA update\n");
/****XXX - need bounds checking here ****/
if ((zonenum
= findzone(dnbuf
, class)) == 0) { /* zone not found */
/* Disallow updates for which we aren't authoratative. Note: the
following test doesn't work right: If it's for a non-local zone,
we will think it's a primary but be unable to lookup the namebuf,
thus returning 'NXDOMAIN' */
if (zp
->z_type
!= Z_PRIMARY
&& zp
->z_type
!= Z_SECONDARY
) {
fprintf(ddt
, "InitDynUpdate: REFUSED - non-primary, non-sedondary update\n");
if (!(zp
->z_state
& Z_DYNAMIC
)) {
fprintf(ddt
, "InitDynUpdate: REFUSED - dynamic flag not set for zone\n");
* Lookup the zone namebuf. Lookup "xyz" not "xyz.", since
* otherwise the lookup fails, because '.' may have a nil n_hash
strcpy(ZoneName
, zp
->z_origin
);
znp
= &ZoneName
[strlen(ZoneName
) - 1];
np
= nlookup(ZoneName
, &htp
, &fname
, 0);
if ((np
== NULL
) || (fname
!= ZoneName
)) {
fprintf(ddt
, "InitDynUpdate: lookup failed on zone (%s)\n",
syslog(LOG_ERR
, "InitDynUpdate: lookup failed on zone (%s)\n",
* If this is the primary copy increment the serial number. Don't
* increment the serial number if this is a secondary; this way, if 2
* different secondaries both update the primary, they will both have
* lower serial numbers than the primary has, and hence eventually
* refresh and get all updates and become consistent.
* Note that the serial number must be incremented in both the zone
* data structure and the zone's namebuf.
case Z_SECONDARY
: /* forward update to primary */
if (match(dp
, class, T_NS
)) {
if (nspp
< &nsp
[NSMAX
-1])
*nspp
= NULL
; /* Delimiter */
if (ns_forw(nsp
, msg
, msglen
, from
, qsp
, dfd
, NULL
, dnbuf
)
olddp
= np
->n_data
; /* old databuf */
/* Find the SOA record */
for (olddp
= np
->n_data
; olddp
!= NULL
; olddp
= olddp
->d_next
)
if (match(olddp
, class, T_SOA
))
fprintf(ddt
,"InitDynUpdate: Couldn't find SOA record for '%s'\n",
"InitDynUpdate: Couldn't find SOA record for '%s'\n"
newdp
= savedata(olddp
->d_class
, olddp
->d_type
, olddp
->d_ttl
,
olddp
->d_data
, olddp
->d_size
);
newdp
->d_zone
= olddp
->d_zone
;
newdp
->d_cred
= DB_C_AUTH
;
cp
= (u_char
*)newdp
->d_data
;
cp
+= strlen(cp
) + 1; /* skip origin string */
cp
+= strlen(cp
) + 1; /* skip in-charge string */
putlong((u_int32_t
)(zp
->z_serial
), cp
);
fprintf(ddt
, "after stuffing data into newdp:\n");
if ((n
= db_update(ZoneName
, olddp
, newdp
, DB_DELETE
,
fprintf(ddt
,"InitDynUpdate: SOA update failed\n");
free((char*) dp
); /* vix@dec mar92 */
/* Now update the RR itself */
if (doupdate(msg
, msglen
, msg
+ sizeof(HEADER
),
zonenum
, (struct databuf
*)0, DB_NODATA
fprintf(ddt
,"InitDynUpdate: doupdate failed\n");
/* doupdate fills in rcode */
zp
->z_state
|= Z_CHANGED
;
* Print the contents of the data in databuf pointed to by dp for an SOA record
return; /* Otherwise fprintf to ddt will bomb */
cp
= (u_char
*)dp
->d_data
;
fprintf(ddt
, "printSOAdata(%x): origin(%x)='%s'\n", dp
, cp
, cp
);
cp
+= strlen(cp
) + 1; /* skip origin string */
fprintf(ddt
, "printSOAdata: in-charge(%x)='%s'\n", cp
, cp
);
cp
+= strlen(cp
) + 1; /* skip in-charge string */
fprintf(ddt
, "printSOAdata: serial(%x)=%d\n", cp
, _getlong(cp
));
#endif /* ALLOW_UPDATES */
* remove datum 'dp' from name 'np'. pdp is previous data pointer.
* "next" field from removed datum, suitable for relinking
register struct databuf
*pdp
, *dp
;
register struct namebuf
*np
;
register struct databuf
*ndp
= dp
->d_next
;
fprintf(ddt
, "rm_datum(%x, %x, %x) -> %x\n",
dp
, np
->n_data
, pdp
, ndp
);
* remove name 'np' from parent 'pp'. pnp is previous name pointer.
* "next" field from removed name, suitable for relinking
struct namebuf
*np
, **pp
, *pnp
;
struct namebuf
*nnp
= np
->n_next
;
if ( (np
->n_data
&& (msg
= "data"))
|| (np
->n_hash
&& (msg
= "hash"))
"rm_name(%x(%s)): non-nil %s pointer\n",
np
, np
->n_dname
?np
->n_dname
:"Nil", msg
);
"rm_name(%x(%s)): non-nil %s pointer\n",
np
, np
->n_dname
?np
->n_dname
:"Nil", msg
);
startxfr(qsp
, np
, msg
, msglen
)
fprintf(ddt
,"startxfr()\n");
* child does the work while
* XXX this needs to be a vfork/exec since on non-copy-on-write
* systems with huge nameserver images, this is very expensive.
fprintf(ddt
,"startxfr: child pid %d\n", getpid());
rfp
= fdopen(qsp
->s_rfd
, "w");
setproctitle("zone XFR to", qsp
->s_rfd
);
fdstat
= fcntl(qsp
->s_rfd
, F_GETFL
, 0);
(void) fcntl(qsp
->s_rfd
, F_SETFL
,
fdstat
& ~PORT_NONBLOCK
);
fwritemsg(rfp
, msg
, msglen
);
fwritemsg(rfp
, msg
, msglen
);