17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*bd0e95e6SGary Mills  * Copyright 2015 Gary Mills
247257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
337c478bd9Sstevel@tonic-gate  * The Regents of the University of California
347c478bd9Sstevel@tonic-gate  * All Rights Reserved
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
377c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
387c478bd9Sstevel@tonic-gate  * contributors.
397c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/socket.h>
447c478bd9Sstevel@tonic-gate #include <sys/stat.h>
457c478bd9Sstevel@tonic-gate #include <netinet/in.h>
467c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
477c478bd9Sstevel@tonic-gate #include <resolv.h>
48*bd0e95e6SGary Mills #include <string.h>
49*bd0e95e6SGary Mills #include <stdlib.h>
50*bd0e95e6SGary Mills #include <unistd.h>
517c478bd9Sstevel@tonic-gate #include <errno.h>
527c478bd9Sstevel@tonic-gate #include <netdb.h>
53*bd0e95e6SGary Mills #include "crossl.h"
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * Kludge to time out quickly if there is no /etc/resolv.conf
577c478bd9Sstevel@tonic-gate  * and a TCP connection to the local DNS server fails.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * Moved function from res_send.c to res_mkquery.c.  This
607c478bd9Sstevel@tonic-gate  * solves a long timeout problem with nslookup.
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * __areweinnamed is needed because there is a possibility that the
637c478bd9Sstevel@tonic-gate  * user might do bad things to resolv.conf and cause in.named to call
647c478bd9Sstevel@tonic-gate  * _confcheck and deadlock the server.
657c478bd9Sstevel@tonic-gate  */
__areweinnamed()677c478bd9Sstevel@tonic-gate int __areweinnamed()
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	return (0);
707c478bd9Sstevel@tonic-gate }
_confcheck()727c478bd9Sstevel@tonic-gate static int _confcheck()
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	int ns;
757c478bd9Sstevel@tonic-gate 	struct stat rc_stat;
767c478bd9Sstevel@tonic-gate 	struct sockaddr_in ns_sin;
797c478bd9Sstevel@tonic-gate 	/* First, we check to see if /etc/resolv.conf exists.
807c478bd9Sstevel@tonic-gate 	 * If it doesn't, then localhost is mostlikely to be
817c478bd9Sstevel@tonic-gate 	 * the nameserver.
827c478bd9Sstevel@tonic-gate 	 */
837c478bd9Sstevel@tonic-gate 	if (stat(_PATH_RESCONF, &rc_stat) == -1 && errno == ENOENT) {
857c478bd9Sstevel@tonic-gate 		/* Next, we check to see if _res.nsaddr is set to loopback.
867c478bd9Sstevel@tonic-gate 		 * If it isn't, it has been altered by the application
877c478bd9Sstevel@tonic-gate 		 * explicitly and we then want to bail with success.
887c478bd9Sstevel@tonic-gate 		 */
897c478bd9Sstevel@tonic-gate 		if (__areweinnamed())
907c478bd9Sstevel@tonic-gate 			return (0);
927c478bd9Sstevel@tonic-gate 		if (_res.nsaddr.sin_addr.S_un.S_addr == htonl(INADDR_LOOPBACK)) {
947c478bd9Sstevel@tonic-gate 			/* Lastly, we try to connect to the TCP port of the
957c478bd9Sstevel@tonic-gate 			 * nameserver.  If this fails, then we know that
967c478bd9Sstevel@tonic-gate 			 * DNS is misconfigured and we can quickly exit.
977c478bd9Sstevel@tonic-gate 			 */
987c478bd9Sstevel@tonic-gate 			ns = socket(AF_INET, SOCK_STREAM, 0);
997c478bd9Sstevel@tonic-gate 			IN_SET_LOOPBACK_ADDR(&ns_sin);
1007c478bd9Sstevel@tonic-gate 			ns_sin.sin_port = htons(NAMESERVER_PORT);
1017c478bd9Sstevel@tonic-gate 			if (connect(ns, (struct sockaddr *) &ns_sin,
1027c478bd9Sstevel@tonic-gate 				    sizeof ns_sin) == -1) {
103*bd0e95e6SGary Mills 				(void) close(ns);
1047c478bd9Sstevel@tonic-gate 				return(-1);
1057c478bd9Sstevel@tonic-gate 			}
1067c478bd9Sstevel@tonic-gate 			else {
107*bd0e95e6SGary Mills 				(void) close(ns);
1087c478bd9Sstevel@tonic-gate 				return(0);
1097c478bd9Sstevel@tonic-gate 			}
1107c478bd9Sstevel@tonic-gate 		}
1127c478bd9Sstevel@tonic-gate 		return(0);
1137c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 	return (0);
1167c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate  * Form all types of queries.
1207c478bd9Sstevel@tonic-gate  * Returns the size of the result or -1.
1217c478bd9Sstevel@tonic-gate  */
1226a1c6faaSanay int
res_mkquery(op,dname,class,type,data,datalen,newrr,buf,buflen)1237c478bd9Sstevel@tonic-gate res_mkquery(op, dname, class, type, data, datalen, newrr, buf, buflen)
1247c478bd9Sstevel@tonic-gate 	int op;			/* opcode of query */
1257c478bd9Sstevel@tonic-gate 	char *dname;		/* domain name */
1267c478bd9Sstevel@tonic-gate 	int class, type;	/* class and type of query */
1277c478bd9Sstevel@tonic-gate 	char *data;		/* resource record data */
1287c478bd9Sstevel@tonic-gate 	int datalen;		/* length of data */
1297c478bd9Sstevel@tonic-gate 	struct rrec *newrr;	/* new rr for modify or append */
1307c478bd9Sstevel@tonic-gate 	char *buf;		/* buffer to put query */
1317c478bd9Sstevel@tonic-gate 	int buflen;		/* size of buffer */
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	register HEADER *hp;
134*bd0e95e6SGary Mills 	register u_char *cp;
1357c478bd9Sstevel@tonic-gate 	register int n;
136*bd0e95e6SGary Mills 	u_char *dnptrs[10], **dpp, **lastdnptr;
1387c478bd9Sstevel@tonic-gate #ifdef DEBUG
1397c478bd9Sstevel@tonic-gate 	if (_res.options & RES_DEBUG)
1407c478bd9Sstevel@tonic-gate 		printf("res_mkquery(%d, %s, %d, %d)\n", op, dname, class, type);
1416a1c6faaSanay #endif /* DEBUG */
1437c478bd9Sstevel@tonic-gate 	/*
1447c478bd9Sstevel@tonic-gate 	 * Check to see if we can bailout quickly.
1457c478bd9Sstevel@tonic-gate 	 * Also rerun res_init if we failed in the past.
1467c478bd9Sstevel@tonic-gate 	 */
1487c478bd9Sstevel@tonic-gate 	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
1497c478bd9Sstevel@tonic-gate 		h_errno = NO_RECOVERY;
1507c478bd9Sstevel@tonic-gate 		return(-1);
1517c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 	if (_confcheck() == -1) {
1547c478bd9Sstevel@tonic-gate 		_res.options &= ~RES_INIT;
1557c478bd9Sstevel@tonic-gate 		h_errno = NO_RECOVERY;
1567c478bd9Sstevel@tonic-gate 		return(-1);
1577c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 	/*
1607c478bd9Sstevel@tonic-gate 	 * Initialize header fields.
1617c478bd9Sstevel@tonic-gate 	 */
1627c478bd9Sstevel@tonic-gate 	if ((buf == NULL) || (buflen < sizeof (HEADER)))
1637c478bd9Sstevel@tonic-gate 		return (-1);
1647c478bd9Sstevel@tonic-gate #ifdef SYSV
165*bd0e95e6SGary Mills 	(void) memset(buf, 0, sizeof (HEADER));
1667c478bd9Sstevel@tonic-gate #else
1677c478bd9Sstevel@tonic-gate 	bzero(buf, sizeof (HEADER));
1687c478bd9Sstevel@tonic-gate #endif
1697c478bd9Sstevel@tonic-gate 	hp = (HEADER *) buf;
1707c478bd9Sstevel@tonic-gate 	hp->id = htons(++_res.id);
1717c478bd9Sstevel@tonic-gate 	hp->opcode = op;
1727c478bd9Sstevel@tonic-gate 	hp->pr = (_res.options & RES_PRIMARY) != 0;
1737c478bd9Sstevel@tonic-gate 	hp->rd = (_res.options & RES_RECURSE) != 0;
1747c478bd9Sstevel@tonic-gate 	hp->rcode = NOERROR;
175*bd0e95e6SGary Mills 	cp = (u_char *)(buf + sizeof (HEADER));
1767c478bd9Sstevel@tonic-gate 	buflen -= sizeof (HEADER);
1777c478bd9Sstevel@tonic-gate 	dpp = dnptrs;
178*bd0e95e6SGary Mills 	*dpp++ = (u_char *)buf;
1797c478bd9Sstevel@tonic-gate 	*dpp++ = NULL;
1807c478bd9Sstevel@tonic-gate 	lastdnptr = dnptrs + sizeof (dnptrs) / sizeof (dnptrs[0]);
1817c478bd9Sstevel@tonic-gate 	/*
1827c478bd9Sstevel@tonic-gate 	 * perform opcode specific processing
1837c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	switch (op) {
1857c478bd9Sstevel@tonic-gate 	case QUERY:
1867c478bd9Sstevel@tonic-gate 		if ((buflen -= QFIXEDSZ) < 0)
1877c478bd9Sstevel@tonic-gate 			return (-1);
188*bd0e95e6SGary Mills 		if ((n = dn_comp((u_char *)dname, cp, buflen,
189*bd0e95e6SGary Mills 		    dnptrs, lastdnptr)) < 0)
1907c478bd9Sstevel@tonic-gate 			return (-1);
1917c478bd9Sstevel@tonic-gate 		cp += n;
1927c478bd9Sstevel@tonic-gate 		buflen -= n;
1937c478bd9Sstevel@tonic-gate 		putshort(type, cp);
1947c478bd9Sstevel@tonic-gate 		cp += sizeof (u_short);
1957c478bd9Sstevel@tonic-gate 		putshort(class, cp);
1967c478bd9Sstevel@tonic-gate 		cp += sizeof (u_short);
1977c478bd9Sstevel@tonic-gate 		hp->qdcount = htons(1);
1987c478bd9Sstevel@tonic-gate 		if (op == QUERY || data == NULL)
1997c478bd9Sstevel@tonic-gate 			break;
2007c478bd9Sstevel@tonic-gate 		/*
2017c478bd9Sstevel@tonic-gate 		 * Make an additional record for completion domain.
2027c478bd9Sstevel@tonic-gate 		 */
2037c478bd9Sstevel@tonic-gate 		buflen -= RRFIXEDSZ;
204*bd0e95e6SGary Mills 		if ((n = dn_comp((u_char *)data, cp, buflen,
205*bd0e95e6SGary Mills 		    dnptrs, lastdnptr)) < 0)
2067c478bd9Sstevel@tonic-gate 			return (-1);
2077c478bd9Sstevel@tonic-gate 		cp += n;
2087c478bd9Sstevel@tonic-gate 		buflen -= n;
2097c478bd9Sstevel@tonic-gate 		putshort(T_NULL, cp);
2107c478bd9Sstevel@tonic-gate 		cp += sizeof (u_short);
2117c478bd9Sstevel@tonic-gate 		putshort(class, cp);
2127c478bd9Sstevel@tonic-gate 		cp += sizeof (u_short);
2137c478bd9Sstevel@tonic-gate 		putlong(0, cp);
2147c478bd9Sstevel@tonic-gate 		cp += sizeof (u_long);
2157c478bd9Sstevel@tonic-gate 		putshort(0, cp);
2167c478bd9Sstevel@tonic-gate 		cp += sizeof (u_short);
2177c478bd9Sstevel@tonic-gate 		hp->arcount = htons(1);
2187c478bd9Sstevel@tonic-gate 		break;
2207c478bd9Sstevel@tonic-gate 	case IQUERY:
2217c478bd9Sstevel@tonic-gate 		/*
2227c478bd9Sstevel@tonic-gate 		 * Initialize answer section
2237c478bd9Sstevel@tonic-gate 		 */
2247c478bd9Sstevel@tonic-gate 		if (buflen < 1 + RRFIXEDSZ + datalen)
2257c478bd9Sstevel@tonic-gate 			return (-1);
2267c478bd9Sstevel@tonic-gate 		*cp++ = '\0';	/* no domain name */
2277c478bd9Sstevel@tonic-gate 		putshort(type, cp);
2287c478bd9Sstevel@tonic-gate 		cp += sizeof (u_short);
2297c478bd9Sstevel@tonic-gate 		putshort(class, cp);
2307c478bd9Sstevel@tonic-gate 		cp += sizeof (u_short);
2317c478bd9Sstevel@tonic-gate 		putlong(0, cp);
2327c478bd9Sstevel@tonic-gate 		cp += sizeof (u_long);
2337c478bd9Sstevel@tonic-gate 		putshort(datalen, cp);
2347c478bd9Sstevel@tonic-gate 		cp += sizeof (u_short);
2357c478bd9Sstevel@tonic-gate 		if (datalen) {
2367c478bd9Sstevel@tonic-gate #ifdef SYSV
237*bd0e95e6SGary Mills 			(void) memcpy((void *)cp, (void *)data, datalen);
2387c478bd9Sstevel@tonic-gate #else
2397c478bd9Sstevel@tonic-gate 			bcopy(data, cp, datalen);
2407c478bd9Sstevel@tonic-gate #endif
2417c478bd9Sstevel@tonic-gate 			cp += datalen;
2427c478bd9Sstevel@tonic-gate 		}
2437c478bd9Sstevel@tonic-gate 		hp->ancount = htons(1);
2447c478bd9Sstevel@tonic-gate 		break;
2467c478bd9Sstevel@tonic-gate #ifdef ALLOW_UPDATES
2477c478bd9Sstevel@tonic-gate 	/*
2487c478bd9Sstevel@tonic-gate 	 * For UPDATEM/UPDATEMA, do UPDATED/UPDATEDA followed by UPDATEA
2497c478bd9Sstevel@tonic-gate 	 * (Record to be modified is followed by its replacement in msg.)
2507c478bd9Sstevel@tonic-gate 	 */
2517c478bd9Sstevel@tonic-gate 	case UPDATEM:
2527c478bd9Sstevel@tonic-gate 	case UPDATEMA:
2547c478bd9Sstevel@tonic-gate 	case UPDATED:
2557c478bd9Sstevel@tonic-gate 		/*
2567c478bd9Sstevel@tonic-gate 		 * The res code for UPDATED and UPDATEDA is the same; user
2577c478bd9Sstevel@tonic-gate 		 * calls them differently: specifies data for UPDATED; server
2587c478bd9Sstevel@tonic-gate 		 * ignores data if specified for UPDATEDA.
2597c478bd9Sstevel@tonic-gate 		 */
2607c478bd9Sstevel@tonic-gate 	case UPDATEDA:
2617c478bd9Sstevel@tonic-gate 		buflen -= RRFIXEDSZ + datalen;
262*bd0e95e6SGary Mills 		if ((n = dn_comp((u_char *)dname, cp, buflen,
263*bd0e95e6SGary Mills 		    dnptrs, lastdnptr)) < 0)
2647c478bd9Sstevel@tonic-gate 			return (-1);
2657c478bd9Sstevel@tonic-gate 		cp += n;
2667c478bd9Sstevel@tonic-gate 		putshort(type, cp);
2677c478bd9Sstevel@tonic-gate 		cp += sizeof (u_short);
2687c478bd9Sstevel@tonic-gate 		putshort(class, cp);
2697c478bd9Sstevel@tonic-gate 		cp += sizeof (u_short);
2707c478bd9Sstevel@tonic-gate 		putlong(0, cp);
2717c478bd9Sstevel@tonic-gate 		cp += sizeof (u_long);
2727c478bd9Sstevel@tonic-gate 		putshort(datalen, cp);
2737c478bd9Sstevel@tonic-gate 		cp += sizeof (u_short);
2747c478bd9Sstevel@tonic-gate 		if (datalen) {
2757c478bd9Sstevel@tonic-gate #ifdef SYSV
2767c478bd9Sstevel@tonic-gate 			memcpy((void *)cp, (void *)data, datalen);
2777c478bd9Sstevel@tonic-gate #else
2787c478bd9Sstevel@tonic-gate 			bcopy(data, cp, datalen);
2797c478bd9Sstevel@tonic-gate #endif
2807c478bd9Sstevel@tonic-gate 			cp += datalen;
2817c478bd9Sstevel@tonic-gate 		}
2827c478bd9Sstevel@tonic-gate 		if ((op == UPDATED) || (op == UPDATEDA)) {
2837c478bd9Sstevel@tonic-gate 			hp->ancount = htons(0);
2847c478bd9Sstevel@tonic-gate 			break;
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 		/* Else UPDATEM/UPDATEMA, so drop into code for UPDATEA */
2887c478bd9Sstevel@tonic-gate 	case UPDATEA:	/* Add new resource record */
2897c478bd9Sstevel@tonic-gate 		buflen -= RRFIXEDSZ + datalen;
290*bd0e95e6SGary Mills 		if ((n = dn_comp((u_char *)dname, cp, buflen,
291*bd0e95e6SGary Mills 		    dnptrs, lastdnptr)) < 0)
2927c478bd9Sstevel@tonic-gate 			return (-1);
2937c478bd9Sstevel@tonic-gate 		cp += n;
2947c478bd9Sstevel@tonic-gate 		putshort(newrr->r_type, cp);
2957c478bd9Sstevel@tonic-gate 		cp += sizeof (u_short);
2967c478bd9Sstevel@tonic-gate 		putshort(newrr->r_class, cp);
2977c478bd9Sstevel@tonic-gate 		cp += sizeof (u_short);
2987c478bd9Sstevel@tonic-gate 		putlong(0, cp);
2997c478bd9Sstevel@tonic-gate 		cp += sizeof (u_long);
3007c478bd9Sstevel@tonic-gate 		putshort(newrr->r_size, cp);
3017c478bd9Sstevel@tonic-gate 		cp += sizeof (u_short);
3027c478bd9Sstevel@tonic-gate 		if (newrr->r_size) {
3037c478bd9Sstevel@tonic-gate #ifdef SYSV
3047c478bd9Sstevel@tonic-gate 			memcpy((void *)cp, newrr->r_data, newrr->r_size);
3057c478bd9Sstevel@tonic-gate #else
3067c478bd9Sstevel@tonic-gate 			bcopy(newrr->r_data, cp, newrr->r_size);
3077c478bd9Sstevel@tonic-gate #endif
3087c478bd9Sstevel@tonic-gate 			cp += newrr->r_size;
3097c478bd9Sstevel@tonic-gate 		}
3107c478bd9Sstevel@tonic-gate 		hp->ancount = htons(0);
3117c478bd9Sstevel@tonic-gate 		break;
3136a1c6faaSanay #endif /* ALLOW_UPDATES */
3147c478bd9Sstevel@tonic-gate 	}
315*bd0e95e6SGary Mills 	return ((char *)cp - buf);
3167c478bd9Sstevel@tonic-gate }