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 */ 21e8031f0aSraf 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 */ 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 #include <sys/types.h> 427c478bd9Sstevel@tonic-gate #include <sys/sockio.h> 437c478bd9Sstevel@tonic-gate #include <sys/socket.h> 447c478bd9Sstevel@tonic-gate #include <netinet/in.h> 457c478bd9Sstevel@tonic-gate #include <stdio.h> 46*bd0e95e6SGary Mills #include <string.h> 47*bd0e95e6SGary Mills #include <stdlib.h> 48*bd0e95e6SGary Mills #include <unistd.h> 49*bd0e95e6SGary Mills #include <stropts.h> 507c478bd9Sstevel@tonic-gate #include <arpa/nameser.h> 517c478bd9Sstevel@tonic-gate #include <resolv.h> 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #include <netinet/in.h> 547c478bd9Sstevel@tonic-gate #include <net/if.h> 557c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h> 567c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 577c478bd9Sstevel@tonic-gate 58*bd0e95e6SGary Mills /* 59*bd0e95e6SGary Mills * Undocumented external function in libnsl 60*bd0e95e6SGary Mills */ 61*bd0e95e6SGary Mills extern int 62*bd0e95e6SGary Mills getdomainname(char *, int); 63*bd0e95e6SGary Mills 647c478bd9Sstevel@tonic-gate #define MAXIFS 256 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate /* 677c478bd9Sstevel@tonic-gate * Resolver state default settings 687c478bd9Sstevel@tonic-gate */ 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate struct state _res = { 717c478bd9Sstevel@tonic-gate RES_TIMEOUT, /* retransmition time interval */ 727c478bd9Sstevel@tonic-gate 4, /* number of times to retransmit */ 737c478bd9Sstevel@tonic-gate RES_DEFAULT, /* options flags */ 747c478bd9Sstevel@tonic-gate 1, /* number of name servers */ 757c478bd9Sstevel@tonic-gate }; 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate /* 787c478bd9Sstevel@tonic-gate * Set up default settings. If the configuration file exist, the values 797c478bd9Sstevel@tonic-gate * there will have precedence. Otherwise, the server address is set to 807c478bd9Sstevel@tonic-gate * INADDR_LOOPBACK and the default domain name comes from the gethostname(). 817c478bd9Sstevel@tonic-gate * BUT if the NIS/RPC domain name is set, that is used if all else fails. 827c478bd9Sstevel@tonic-gate * 837c478bd9Sstevel@tonic-gate * The configuration file should only be used if you want to redefine your 847c478bd9Sstevel@tonic-gate * domain or run without a server on your machine. 857c478bd9Sstevel@tonic-gate * 867c478bd9Sstevel@tonic-gate * Note the user can always override then domain name with the environment 877c478bd9Sstevel@tonic-gate * variable LOCALDOMAIN which has absolute priority. 887c478bd9Sstevel@tonic-gate * 897c478bd9Sstevel@tonic-gate * 907c478bd9Sstevel@tonic-gate * Return 0 if completes successfully, -1 on error 917c478bd9Sstevel@tonic-gate */ 926a1c6faaSanay int 936a1c6faaSanay res_init(void) 947c478bd9Sstevel@tonic-gate { 957c478bd9Sstevel@tonic-gate register FILE *fp; 967c478bd9Sstevel@tonic-gate register char *cp, **pp; 977c478bd9Sstevel@tonic-gate register int n; 987c478bd9Sstevel@tonic-gate char buf[BUFSIZ]; 997c478bd9Sstevel@tonic-gate int nserv = 0; /* number of nameserver records read from file */ 1007c478bd9Sstevel@tonic-gate int haveenv = 0; 1017c478bd9Sstevel@tonic-gate int havesearch = 0; 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate _res.nsaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); /* INADDR_ANY */ 1047c478bd9Sstevel@tonic-gate _res.nsaddr.sin_family = AF_INET; 1057c478bd9Sstevel@tonic-gate _res.nsaddr.sin_port = htons(NAMESERVER_PORT); 1067c478bd9Sstevel@tonic-gate _res.nscount = 1; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate #ifdef SIOCGIFNUM 1097c478bd9Sstevel@tonic-gate { int numifs, s, n, int_up; 1107c478bd9Sstevel@tonic-gate struct ifconf ifc; 1117c478bd9Sstevel@tonic-gate register struct ifreq *ifrp; 1127c478bd9Sstevel@tonic-gate struct ifreq ifr; 1137c478bd9Sstevel@tonic-gate unsigned bufsize; 1147c478bd9Sstevel@tonic-gate unsigned int flags; 1157c478bd9Sstevel@tonic-gate char *buf; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 1187c478bd9Sstevel@tonic-gate perror("socket"); 1197c478bd9Sstevel@tonic-gate return (-1); 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) { 1227c478bd9Sstevel@tonic-gate numifs = MAXIFS; 1237c478bd9Sstevel@tonic-gate } 1247c478bd9Sstevel@tonic-gate bufsize = numifs * sizeof (struct ifreq); 1257c478bd9Sstevel@tonic-gate buf = (char *)malloc(bufsize); 1267c478bd9Sstevel@tonic-gate if (buf == NULL) { 1277c478bd9Sstevel@tonic-gate perror("out of memory"); 128*bd0e95e6SGary Mills (void) close(s); 1297c478bd9Sstevel@tonic-gate return (-1); 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate ifc.ifc_len = bufsize; 1327c478bd9Sstevel@tonic-gate ifc.ifc_buf = buf; 1337c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { 1347c478bd9Sstevel@tonic-gate perror("ifconfig: SIOCGIFCONF"); 135*bd0e95e6SGary Mills (void) close(s); 1367c478bd9Sstevel@tonic-gate free(buf); 1377c478bd9Sstevel@tonic-gate return (-1); 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate int_up = 0; 1417c478bd9Sstevel@tonic-gate ifrp = ifc.ifc_req; 1427c478bd9Sstevel@tonic-gate for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; 1437c478bd9Sstevel@tonic-gate n--, ifrp++) { 144*bd0e95e6SGary Mills (void) memset((void *) &ifr, 0, sizeof (ifr)); 1457c478bd9Sstevel@tonic-gate strncpy(ifr.ifr_name, ifrp->ifr_name, 1467c478bd9Sstevel@tonic-gate sizeof (ifr.ifr_name)); 1477c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGIFFLAGS, (char *)&ifr) < 0) { 1487c478bd9Sstevel@tonic-gate perror("SIOCGIFFLAGS"); 149*bd0e95e6SGary Mills (void) close(s); 1507c478bd9Sstevel@tonic-gate free(buf); 1517c478bd9Sstevel@tonic-gate return (-1); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate flags = ifr.ifr_flags; 1547c478bd9Sstevel@tonic-gate /* we are looking for a non-loopback interface */ 1557c478bd9Sstevel@tonic-gate if ((flags & IFF_UP) && ((flags & IFF_LOOPBACK) == 0)) 1567c478bd9Sstevel@tonic-gate int_up = 1; 1577c478bd9Sstevel@tonic-gate } 158*bd0e95e6SGary Mills (void) close(s); 1597c478bd9Sstevel@tonic-gate free(buf); 1607c478bd9Sstevel@tonic-gate if (int_up == 0) /* all the non-LOOPBACK interfaces are DOWN */ 1617c478bd9Sstevel@tonic-gate return (-1); 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate #endif /* SIOCGIFNUM */ 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate /* 1677c478bd9Sstevel@tonic-gate * for the benefit of hidden NIS domains, we use the same procedure 1687c478bd9Sstevel@tonic-gate * as sendmail: convert leading + to dot, then drop to first dot 1697c478bd9Sstevel@tonic-gate */ 170*bd0e95e6SGary Mills (void) getdomainname(buf, BUFSIZ); 1717c478bd9Sstevel@tonic-gate if (buf[0] == '+') 1727c478bd9Sstevel@tonic-gate buf[0] = '.'; 1737c478bd9Sstevel@tonic-gate #ifdef SYSV 1747c478bd9Sstevel@tonic-gate cp = strchr(buf, (int)'.'); 1757c478bd9Sstevel@tonic-gate #else 1767c478bd9Sstevel@tonic-gate cp = index(buf, '.'); 1777c478bd9Sstevel@tonic-gate #endif 1787c478bd9Sstevel@tonic-gate if (cp == NULL) 1797c478bd9Sstevel@tonic-gate strcpy(_res.defdname, buf); 1807c478bd9Sstevel@tonic-gate else 1817c478bd9Sstevel@tonic-gate strcpy(_res.defdname, cp+1); 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate /* Allow user to override the local domain definition */ 1847c478bd9Sstevel@tonic-gate if ((cp = getenv("LOCALDOMAIN")) != NULL) { 1857c478bd9Sstevel@tonic-gate (void) strncpy(_res.defdname, cp, sizeof (_res.defdname)); 1867c478bd9Sstevel@tonic-gate haveenv++; 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { 1907c478bd9Sstevel@tonic-gate /* read the config file */ 1917c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof (buf), fp) != NULL) { 1927c478bd9Sstevel@tonic-gate /* read default domain name */ 1937c478bd9Sstevel@tonic-gate if (!strncmp(buf, "domain", sizeof ("domain") - 1)) { 1947c478bd9Sstevel@tonic-gate if (haveenv) /* skip if have from environ */ 1957c478bd9Sstevel@tonic-gate continue; 1967c478bd9Sstevel@tonic-gate cp = buf + sizeof ("domain") - 1; 1977c478bd9Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t') 1987c478bd9Sstevel@tonic-gate cp++; 1997c478bd9Sstevel@tonic-gate if ((*cp == '\0') || (*cp == '\n')) 2007c478bd9Sstevel@tonic-gate continue; 2017c478bd9Sstevel@tonic-gate (void) strncpy(_res.defdname, cp, sizeof (_res.defdname) - 1); 2027c478bd9Sstevel@tonic-gate #ifdef SYSV 2037c478bd9Sstevel@tonic-gate if ((cp = strchr(_res.defdname, (int)'\n')) != NULL) 2047c478bd9Sstevel@tonic-gate #else 2057c478bd9Sstevel@tonic-gate if ((cp = index(_res.defdname, '\n')) != NULL) 2067c478bd9Sstevel@tonic-gate #endif 2077c478bd9Sstevel@tonic-gate *cp = '\0'; 2087c478bd9Sstevel@tonic-gate havesearch = 0; 2097c478bd9Sstevel@tonic-gate continue; 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate /* set search list */ 2127c478bd9Sstevel@tonic-gate if (!strncmp(buf, "search", sizeof ("search") - 1)) { 2137c478bd9Sstevel@tonic-gate if (haveenv) /* skip if have from environ */ 2147c478bd9Sstevel@tonic-gate continue; 2157c478bd9Sstevel@tonic-gate cp = buf + sizeof ("search") - 1; 2167c478bd9Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t') 2177c478bd9Sstevel@tonic-gate cp++; 2187c478bd9Sstevel@tonic-gate if ((*cp == '\0') || (*cp == '\n')) 2197c478bd9Sstevel@tonic-gate continue; 2207c478bd9Sstevel@tonic-gate (void) strncpy(_res.defdname, cp, sizeof (_res.defdname) - 1); 2217c478bd9Sstevel@tonic-gate #ifdef SYSV 2227c478bd9Sstevel@tonic-gate if ((cp = strchr(_res.defdname, (int)'\n')) != NULL) 2237c478bd9Sstevel@tonic-gate #else 2247c478bd9Sstevel@tonic-gate if ((cp = index(_res.defdname, '\n')) != NULL) 2257c478bd9Sstevel@tonic-gate #endif 2267c478bd9Sstevel@tonic-gate *cp = '\0'; 2277c478bd9Sstevel@tonic-gate /* 2287c478bd9Sstevel@tonic-gate * Set search list to be blank-separated strings 2297c478bd9Sstevel@tonic-gate * on rest of line. 2307c478bd9Sstevel@tonic-gate */ 2317c478bd9Sstevel@tonic-gate cp = _res.defdname; 2327c478bd9Sstevel@tonic-gate pp = _res.dnsrch; 2337c478bd9Sstevel@tonic-gate *pp++ = cp; 2347c478bd9Sstevel@tonic-gate for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) { 2357c478bd9Sstevel@tonic-gate if (*cp == ' ' || *cp == '\t') { 2367c478bd9Sstevel@tonic-gate *cp = 0; 2377c478bd9Sstevel@tonic-gate n = 1; 2387c478bd9Sstevel@tonic-gate } else if (n) { 2397c478bd9Sstevel@tonic-gate *pp++ = cp; 2407c478bd9Sstevel@tonic-gate n = 0; 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate /* null terminate last domain if there are excess */ 2447c478bd9Sstevel@tonic-gate while (*cp != '\0' && *cp != ' ' && *cp != '\t') 2457c478bd9Sstevel@tonic-gate cp++; 2467c478bd9Sstevel@tonic-gate *cp = '\0'; 2477c478bd9Sstevel@tonic-gate *pp++ = 0; 2487c478bd9Sstevel@tonic-gate havesearch = 1; 2497c478bd9Sstevel@tonic-gate continue; 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate /* read nameservers to query */ 2527c478bd9Sstevel@tonic-gate if (!strncmp(buf, "nameserver", sizeof ("nameserver") - 1) && 2537c478bd9Sstevel@tonic-gate (nserv < MAXNS)) { 2547c478bd9Sstevel@tonic-gate cp = buf + sizeof ("nameserver") - 1; 2557c478bd9Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t') 2567c478bd9Sstevel@tonic-gate cp++; 2577c478bd9Sstevel@tonic-gate if ((*cp == '\0') || (*cp == '\n')) 2587c478bd9Sstevel@tonic-gate continue; 2597c478bd9Sstevel@tonic-gate if ((_res.nsaddr_list[nserv].sin_addr.s_addr = 2607c478bd9Sstevel@tonic-gate inet_addr(cp)) == (unsigned) -1) { 2617c478bd9Sstevel@tonic-gate _res.nsaddr_list[n].sin_addr.s_addr = INADDR_ANY; 2627c478bd9Sstevel@tonic-gate continue; 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate _res.nsaddr_list[nserv].sin_family = AF_INET; 2657c478bd9Sstevel@tonic-gate _res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT); 2667c478bd9Sstevel@tonic-gate nserv++; 2677c478bd9Sstevel@tonic-gate continue; 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate } 2707c478bd9Sstevel@tonic-gate if (nserv > 1) 2717c478bd9Sstevel@tonic-gate _res.nscount = nserv; 2727c478bd9Sstevel@tonic-gate (void) fclose(fp); 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate if (_res.defdname[0] == 0) { 2757c478bd9Sstevel@tonic-gate if (gethostname(buf, sizeof (_res.defdname)) == 0 && 2767c478bd9Sstevel@tonic-gate #ifdef SYSV 2777c478bd9Sstevel@tonic-gate (cp = strchr(buf, (int)'.'))) 2787c478bd9Sstevel@tonic-gate #else 2797c478bd9Sstevel@tonic-gate (cp = index(buf, '.'))) 2807c478bd9Sstevel@tonic-gate #endif 2817c478bd9Sstevel@tonic-gate (void) strcpy(_res.defdname, cp + 1); 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate /* find components of local domain that might be searched */ 2857c478bd9Sstevel@tonic-gate if (havesearch == 0) { 2867c478bd9Sstevel@tonic-gate pp = _res.dnsrch; 2877c478bd9Sstevel@tonic-gate *pp++ = _res.defdname; 2887c478bd9Sstevel@tonic-gate for (cp = _res.defdname, n = 0; *cp; cp++) 2897c478bd9Sstevel@tonic-gate if (*cp == '.') 2907c478bd9Sstevel@tonic-gate n++; 2917c478bd9Sstevel@tonic-gate cp = _res.defdname; 2927c478bd9Sstevel@tonic-gate for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH; n--) { 2937c478bd9Sstevel@tonic-gate #ifdef SYSV 2947c478bd9Sstevel@tonic-gate cp = strchr(cp, (int)'.'); 2957c478bd9Sstevel@tonic-gate #else 2967c478bd9Sstevel@tonic-gate cp = index(cp, '.'); 2977c478bd9Sstevel@tonic-gate #endif 2987c478bd9Sstevel@tonic-gate *pp++ = ++cp; 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate *pp++ = 0; 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate _res.options |= RES_INIT; 3037c478bd9Sstevel@tonic-gate return (0); 3047c478bd9Sstevel@tonic-gate } 305