10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*6812Sraf * Common Development and Distribution License (the "License").
6*6812Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211219Sraf
220Sstevel@tonic-gate /*
23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate * The Regents of the University of California
330Sstevel@tonic-gate * All Rights Reserved
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate * contributors.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include <sys/types.h>
440Sstevel@tonic-gate #include <sys/socket.h>
450Sstevel@tonic-gate #include <sys/stat.h>
460Sstevel@tonic-gate #include <netinet/in.h>
470Sstevel@tonic-gate #include <arpa/nameser.h>
480Sstevel@tonic-gate #include <resolv.h>
490Sstevel@tonic-gate #include <errno.h>
500Sstevel@tonic-gate #include <netdb.h>
510Sstevel@tonic-gate
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate * Kludge to time out quickly if there is no /etc/resolv.conf
540Sstevel@tonic-gate * and a TCP connection to the local DNS server fails.
550Sstevel@tonic-gate *
560Sstevel@tonic-gate * Moved function from res_send.c to res_mkquery.c. This
570Sstevel@tonic-gate * solves a long timeout problem with nslookup.
580Sstevel@tonic-gate *
590Sstevel@tonic-gate * __areweinnamed is needed because there is a possibility that the
600Sstevel@tonic-gate * user might do bad things to resolv.conf and cause in.named to call
610Sstevel@tonic-gate * _confcheck and deadlock the server.
620Sstevel@tonic-gate */
630Sstevel@tonic-gate
__areweinnamed()640Sstevel@tonic-gate int __areweinnamed()
650Sstevel@tonic-gate {
660Sstevel@tonic-gate return (0);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate
_confcheck()690Sstevel@tonic-gate static int _confcheck()
700Sstevel@tonic-gate {
710Sstevel@tonic-gate int ns;
720Sstevel@tonic-gate struct stat rc_stat;
730Sstevel@tonic-gate struct sockaddr_in ns_sin;
740Sstevel@tonic-gate
750Sstevel@tonic-gate
760Sstevel@tonic-gate /* First, we check to see if /etc/resolv.conf exists.
770Sstevel@tonic-gate * If it doesn't, then localhost is mostlikely to be
780Sstevel@tonic-gate * the nameserver.
790Sstevel@tonic-gate */
800Sstevel@tonic-gate if (stat(_PATH_RESCONF, &rc_stat) == -1 && errno == ENOENT) {
810Sstevel@tonic-gate
820Sstevel@tonic-gate /* Next, we check to see if _res.nsaddr is set to loopback.
830Sstevel@tonic-gate * If it isn't, it has been altered by the application
840Sstevel@tonic-gate * explicitly and we then want to bail with success.
850Sstevel@tonic-gate */
860Sstevel@tonic-gate if (__areweinnamed())
870Sstevel@tonic-gate return (0);
880Sstevel@tonic-gate
890Sstevel@tonic-gate if (_res.nsaddr.sin_addr.S_un.S_addr == htonl(INADDR_LOOPBACK)) {
900Sstevel@tonic-gate
910Sstevel@tonic-gate /* Lastly, we try to connect to the TCP port of the
920Sstevel@tonic-gate * nameserver. If this fails, then we know that
930Sstevel@tonic-gate * DNS is misconfigured and we can quickly exit.
940Sstevel@tonic-gate */
950Sstevel@tonic-gate ns = socket(AF_INET, SOCK_STREAM, 0);
960Sstevel@tonic-gate IN_SET_LOOPBACK_ADDR(&ns_sin);
970Sstevel@tonic-gate ns_sin.sin_port = htons(NAMESERVER_PORT);
980Sstevel@tonic-gate if (connect(ns, (struct sockaddr *) &ns_sin,
990Sstevel@tonic-gate sizeof ns_sin) == -1) {
1000Sstevel@tonic-gate close(ns);
1010Sstevel@tonic-gate return(-1);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate else {
1040Sstevel@tonic-gate close(ns);
1050Sstevel@tonic-gate return(0);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate return(0);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate return (0);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate * Form all types of queries.
1170Sstevel@tonic-gate * Returns the size of the result or -1.
1180Sstevel@tonic-gate */
119237Sanay int
res_mkquery(op,dname,class,type,data,datalen,newrr,buf,buflen)1200Sstevel@tonic-gate res_mkquery(op, dname, class, type, data, datalen, newrr, buf, buflen)
1210Sstevel@tonic-gate int op; /* opcode of query */
1220Sstevel@tonic-gate char *dname; /* domain name */
1230Sstevel@tonic-gate int class, type; /* class and type of query */
1240Sstevel@tonic-gate char *data; /* resource record data */
1250Sstevel@tonic-gate int datalen; /* length of data */
1260Sstevel@tonic-gate struct rrec *newrr; /* new rr for modify or append */
1270Sstevel@tonic-gate char *buf; /* buffer to put query */
1280Sstevel@tonic-gate int buflen; /* size of buffer */
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate register HEADER *hp;
1310Sstevel@tonic-gate register char *cp;
1320Sstevel@tonic-gate register int n;
1330Sstevel@tonic-gate char *dnptrs[10], **dpp, **lastdnptr;
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate #ifdef DEBUG
1360Sstevel@tonic-gate if (_res.options & RES_DEBUG)
1370Sstevel@tonic-gate printf("res_mkquery(%d, %s, %d, %d)\n", op, dname, class, type);
138237Sanay #endif /* DEBUG */
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate * Check to see if we can bailout quickly.
1420Sstevel@tonic-gate * Also rerun res_init if we failed in the past.
1430Sstevel@tonic-gate */
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
1460Sstevel@tonic-gate h_errno = NO_RECOVERY;
1470Sstevel@tonic-gate return(-1);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate if (_confcheck() == -1) {
1510Sstevel@tonic-gate _res.options &= ~RES_INIT;
1520Sstevel@tonic-gate h_errno = NO_RECOVERY;
1530Sstevel@tonic-gate return(-1);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate /*
1570Sstevel@tonic-gate * Initialize header fields.
1580Sstevel@tonic-gate */
1590Sstevel@tonic-gate if ((buf == NULL) || (buflen < sizeof (HEADER)))
1600Sstevel@tonic-gate return (-1);
1610Sstevel@tonic-gate #ifdef SYSV
1620Sstevel@tonic-gate memset(buf, 0, sizeof (HEADER));
1630Sstevel@tonic-gate #else
1640Sstevel@tonic-gate bzero(buf, sizeof (HEADER));
1650Sstevel@tonic-gate #endif
1660Sstevel@tonic-gate hp = (HEADER *) buf;
1670Sstevel@tonic-gate hp->id = htons(++_res.id);
1680Sstevel@tonic-gate hp->opcode = op;
1690Sstevel@tonic-gate hp->pr = (_res.options & RES_PRIMARY) != 0;
1700Sstevel@tonic-gate hp->rd = (_res.options & RES_RECURSE) != 0;
1710Sstevel@tonic-gate hp->rcode = NOERROR;
1720Sstevel@tonic-gate cp = buf + sizeof (HEADER);
1730Sstevel@tonic-gate buflen -= sizeof (HEADER);
1740Sstevel@tonic-gate dpp = dnptrs;
1750Sstevel@tonic-gate *dpp++ = buf;
1760Sstevel@tonic-gate *dpp++ = NULL;
1770Sstevel@tonic-gate lastdnptr = dnptrs + sizeof (dnptrs) / sizeof (dnptrs[0]);
1780Sstevel@tonic-gate /*
1790Sstevel@tonic-gate * perform opcode specific processing
1800Sstevel@tonic-gate */
1810Sstevel@tonic-gate switch (op) {
1820Sstevel@tonic-gate case QUERY:
1830Sstevel@tonic-gate if ((buflen -= QFIXEDSZ) < 0)
1840Sstevel@tonic-gate return (-1);
1850Sstevel@tonic-gate if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
1860Sstevel@tonic-gate return (-1);
1870Sstevel@tonic-gate cp += n;
1880Sstevel@tonic-gate buflen -= n;
1890Sstevel@tonic-gate putshort(type, cp);
1900Sstevel@tonic-gate cp += sizeof (u_short);
1910Sstevel@tonic-gate putshort(class, cp);
1920Sstevel@tonic-gate cp += sizeof (u_short);
1930Sstevel@tonic-gate hp->qdcount = htons(1);
1940Sstevel@tonic-gate if (op == QUERY || data == NULL)
1950Sstevel@tonic-gate break;
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate * Make an additional record for completion domain.
1980Sstevel@tonic-gate */
1990Sstevel@tonic-gate buflen -= RRFIXEDSZ;
2000Sstevel@tonic-gate if ((n = dn_comp(data, cp, buflen, dnptrs, lastdnptr)) < 0)
2010Sstevel@tonic-gate return (-1);
2020Sstevel@tonic-gate cp += n;
2030Sstevel@tonic-gate buflen -= n;
2040Sstevel@tonic-gate putshort(T_NULL, cp);
2050Sstevel@tonic-gate cp += sizeof (u_short);
2060Sstevel@tonic-gate putshort(class, cp);
2070Sstevel@tonic-gate cp += sizeof (u_short);
2080Sstevel@tonic-gate putlong(0, cp);
2090Sstevel@tonic-gate cp += sizeof (u_long);
2100Sstevel@tonic-gate putshort(0, cp);
2110Sstevel@tonic-gate cp += sizeof (u_short);
2120Sstevel@tonic-gate hp->arcount = htons(1);
2130Sstevel@tonic-gate break;
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate case IQUERY:
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate * Initialize answer section
2180Sstevel@tonic-gate */
2190Sstevel@tonic-gate if (buflen < 1 + RRFIXEDSZ + datalen)
2200Sstevel@tonic-gate return (-1);
2210Sstevel@tonic-gate *cp++ = '\0'; /* no domain name */
2220Sstevel@tonic-gate putshort(type, cp);
2230Sstevel@tonic-gate cp += sizeof (u_short);
2240Sstevel@tonic-gate putshort(class, cp);
2250Sstevel@tonic-gate cp += sizeof (u_short);
2260Sstevel@tonic-gate putlong(0, cp);
2270Sstevel@tonic-gate cp += sizeof (u_long);
2280Sstevel@tonic-gate putshort(datalen, cp);
2290Sstevel@tonic-gate cp += sizeof (u_short);
2300Sstevel@tonic-gate if (datalen) {
2310Sstevel@tonic-gate #ifdef SYSV
2320Sstevel@tonic-gate memcpy((void *)cp, (void *)data, datalen);
2330Sstevel@tonic-gate #else
2340Sstevel@tonic-gate bcopy(data, cp, datalen);
2350Sstevel@tonic-gate #endif
2360Sstevel@tonic-gate cp += datalen;
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate hp->ancount = htons(1);
2390Sstevel@tonic-gate break;
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate #ifdef ALLOW_UPDATES
2420Sstevel@tonic-gate /*
2430Sstevel@tonic-gate * For UPDATEM/UPDATEMA, do UPDATED/UPDATEDA followed by UPDATEA
2440Sstevel@tonic-gate * (Record to be modified is followed by its replacement in msg.)
2450Sstevel@tonic-gate */
2460Sstevel@tonic-gate case UPDATEM:
2470Sstevel@tonic-gate case UPDATEMA:
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate case UPDATED:
2500Sstevel@tonic-gate /*
2510Sstevel@tonic-gate * The res code for UPDATED and UPDATEDA is the same; user
2520Sstevel@tonic-gate * calls them differently: specifies data for UPDATED; server
2530Sstevel@tonic-gate * ignores data if specified for UPDATEDA.
2540Sstevel@tonic-gate */
2550Sstevel@tonic-gate case UPDATEDA:
2560Sstevel@tonic-gate buflen -= RRFIXEDSZ + datalen;
2570Sstevel@tonic-gate if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
2580Sstevel@tonic-gate return (-1);
2590Sstevel@tonic-gate cp += n;
2600Sstevel@tonic-gate putshort(type, cp);
2610Sstevel@tonic-gate cp += sizeof (u_short);
2620Sstevel@tonic-gate putshort(class, cp);
2630Sstevel@tonic-gate cp += sizeof (u_short);
2640Sstevel@tonic-gate putlong(0, cp);
2650Sstevel@tonic-gate cp += sizeof (u_long);
2660Sstevel@tonic-gate putshort(datalen, cp);
2670Sstevel@tonic-gate cp += sizeof (u_short);
2680Sstevel@tonic-gate if (datalen) {
2690Sstevel@tonic-gate #ifdef SYSV
2700Sstevel@tonic-gate memcpy((void *)cp, (void *)data, datalen);
2710Sstevel@tonic-gate #else
2720Sstevel@tonic-gate bcopy(data, cp, datalen);
2730Sstevel@tonic-gate #endif
2740Sstevel@tonic-gate cp += datalen;
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate if ((op == UPDATED) || (op == UPDATEDA)) {
2770Sstevel@tonic-gate hp->ancount = htons(0);
2780Sstevel@tonic-gate break;
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate /* Else UPDATEM/UPDATEMA, so drop into code for UPDATEA */
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate case UPDATEA: /* Add new resource record */
2830Sstevel@tonic-gate buflen -= RRFIXEDSZ + datalen;
2840Sstevel@tonic-gate if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
2850Sstevel@tonic-gate return (-1);
2860Sstevel@tonic-gate cp += n;
2870Sstevel@tonic-gate putshort(newrr->r_type, cp);
2880Sstevel@tonic-gate cp += sizeof (u_short);
2890Sstevel@tonic-gate putshort(newrr->r_class, cp);
2900Sstevel@tonic-gate cp += sizeof (u_short);
2910Sstevel@tonic-gate putlong(0, cp);
2920Sstevel@tonic-gate cp += sizeof (u_long);
2930Sstevel@tonic-gate putshort(newrr->r_size, cp);
2940Sstevel@tonic-gate cp += sizeof (u_short);
2950Sstevel@tonic-gate if (newrr->r_size) {
2960Sstevel@tonic-gate #ifdef SYSV
2970Sstevel@tonic-gate memcpy((void *)cp, newrr->r_data, newrr->r_size);
2980Sstevel@tonic-gate #else
2990Sstevel@tonic-gate bcopy(newrr->r_data, cp, newrr->r_size);
3000Sstevel@tonic-gate #endif
3010Sstevel@tonic-gate cp += newrr->r_size;
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate hp->ancount = htons(0);
3040Sstevel@tonic-gate break;
3050Sstevel@tonic-gate
306237Sanay #endif /* ALLOW_UPDATES */
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate return (cp - buf);
3090Sstevel@tonic-gate }
310