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