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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*237Sanay * Copyright 2005 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 "synonyms.h" 430Sstevel@tonic-gate 440Sstevel@tonic-gate #include <sys/types.h> 450Sstevel@tonic-gate #include <sys/sockio.h> 460Sstevel@tonic-gate #include <sys/socket.h> 470Sstevel@tonic-gate #include <netinet/in.h> 480Sstevel@tonic-gate #include <stdio.h> 490Sstevel@tonic-gate #include <arpa/nameser.h> 500Sstevel@tonic-gate #include <resolv.h> 510Sstevel@tonic-gate 520Sstevel@tonic-gate #include <netinet/in.h> 530Sstevel@tonic-gate #include <net/if.h> 540Sstevel@tonic-gate #include <netinet/if_ether.h> 550Sstevel@tonic-gate #include <arpa/inet.h> 560Sstevel@tonic-gate 570Sstevel@tonic-gate #define MAXIFS 256 580Sstevel@tonic-gate 590Sstevel@tonic-gate /* 600Sstevel@tonic-gate * Resolver state default settings 610Sstevel@tonic-gate */ 620Sstevel@tonic-gate 630Sstevel@tonic-gate struct state _res = { 640Sstevel@tonic-gate RES_TIMEOUT, /* retransmition time interval */ 650Sstevel@tonic-gate 4, /* number of times to retransmit */ 660Sstevel@tonic-gate RES_DEFAULT, /* options flags */ 670Sstevel@tonic-gate 1, /* number of name servers */ 680Sstevel@tonic-gate }; 690Sstevel@tonic-gate 700Sstevel@tonic-gate /* 710Sstevel@tonic-gate * Set up default settings. If the configuration file exist, the values 720Sstevel@tonic-gate * there will have precedence. Otherwise, the server address is set to 730Sstevel@tonic-gate * INADDR_LOOPBACK and the default domain name comes from the gethostname(). 740Sstevel@tonic-gate * BUT if the NIS/RPC domain name is set, that is used if all else fails. 750Sstevel@tonic-gate * 760Sstevel@tonic-gate * The configuration file should only be used if you want to redefine your 770Sstevel@tonic-gate * domain or run without a server on your machine. 780Sstevel@tonic-gate * 790Sstevel@tonic-gate * Note the user can always override then domain name with the environment 800Sstevel@tonic-gate * variable LOCALDOMAIN which has absolute priority. 810Sstevel@tonic-gate * 820Sstevel@tonic-gate * 830Sstevel@tonic-gate * Return 0 if completes successfully, -1 on error 840Sstevel@tonic-gate */ 85*237Sanay int 86*237Sanay res_init(void) 870Sstevel@tonic-gate { 880Sstevel@tonic-gate register FILE *fp; 890Sstevel@tonic-gate register char *cp, **pp; 900Sstevel@tonic-gate register int n; 910Sstevel@tonic-gate char buf[BUFSIZ]; 920Sstevel@tonic-gate #ifdef SYSV 930Sstevel@tonic-gate extern char *strchr(); 940Sstevel@tonic-gate #else 950Sstevel@tonic-gate extern char *index(); 960Sstevel@tonic-gate #endif 970Sstevel@tonic-gate extern char *strcpy(), *strncpy(); 980Sstevel@tonic-gate extern char *getenv(); 990Sstevel@tonic-gate int nserv = 0; /* number of nameserver records read from file */ 1000Sstevel@tonic-gate int haveenv = 0; 1010Sstevel@tonic-gate int havesearch = 0; 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate _res.nsaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); /* INADDR_ANY */ 1040Sstevel@tonic-gate _res.nsaddr.sin_family = AF_INET; 1050Sstevel@tonic-gate _res.nsaddr.sin_port = htons(NAMESERVER_PORT); 1060Sstevel@tonic-gate _res.nscount = 1; 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate #ifdef SIOCGIFNUM 1090Sstevel@tonic-gate { int numifs, s, n, int_up; 1100Sstevel@tonic-gate struct ifconf ifc; 1110Sstevel@tonic-gate register struct ifreq *ifrp; 1120Sstevel@tonic-gate struct ifreq ifr; 1130Sstevel@tonic-gate unsigned bufsize; 1140Sstevel@tonic-gate unsigned int flags; 1150Sstevel@tonic-gate char *buf; 1160Sstevel@tonic-gate extern void *malloc(); 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 1190Sstevel@tonic-gate perror("socket"); 1200Sstevel@tonic-gate return (-1); 1210Sstevel@tonic-gate } 1220Sstevel@tonic-gate if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) { 1230Sstevel@tonic-gate numifs = MAXIFS; 1240Sstevel@tonic-gate } 1250Sstevel@tonic-gate bufsize = numifs * sizeof (struct ifreq); 1260Sstevel@tonic-gate buf = (char *)malloc(bufsize); 1270Sstevel@tonic-gate if (buf == NULL) { 1280Sstevel@tonic-gate perror("out of memory"); 1290Sstevel@tonic-gate close(s); 1300Sstevel@tonic-gate return (-1); 1310Sstevel@tonic-gate } 1320Sstevel@tonic-gate ifc.ifc_len = bufsize; 1330Sstevel@tonic-gate ifc.ifc_buf = buf; 1340Sstevel@tonic-gate if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { 1350Sstevel@tonic-gate perror("ifconfig: SIOCGIFCONF"); 1360Sstevel@tonic-gate close(s); 1370Sstevel@tonic-gate free(buf); 1380Sstevel@tonic-gate return (-1); 1390Sstevel@tonic-gate } 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate int_up = 0; 1420Sstevel@tonic-gate ifrp = ifc.ifc_req; 1430Sstevel@tonic-gate for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; 1440Sstevel@tonic-gate n--, ifrp++) { 1450Sstevel@tonic-gate memset((void *) &ifr, 0, sizeof (ifr)); 1460Sstevel@tonic-gate strncpy(ifr.ifr_name, ifrp->ifr_name, 1470Sstevel@tonic-gate sizeof (ifr.ifr_name)); 1480Sstevel@tonic-gate if (ioctl(s, SIOCGIFFLAGS, (char *)&ifr) < 0) { 1490Sstevel@tonic-gate perror("SIOCGIFFLAGS"); 1500Sstevel@tonic-gate close(s); 1510Sstevel@tonic-gate free(buf); 1520Sstevel@tonic-gate return (-1); 1530Sstevel@tonic-gate } 1540Sstevel@tonic-gate flags = ifr.ifr_flags; 1550Sstevel@tonic-gate /* we are looking for a non-loopback interface */ 1560Sstevel@tonic-gate if ((flags & IFF_UP) && ((flags & IFF_LOOPBACK) == 0)) 1570Sstevel@tonic-gate int_up = 1; 1580Sstevel@tonic-gate } 1590Sstevel@tonic-gate close(s); 1600Sstevel@tonic-gate free(buf); 1610Sstevel@tonic-gate if (int_up == 0) /* all the non-LOOPBACK interfaces are DOWN */ 1620Sstevel@tonic-gate return (-1); 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate #endif /* SIOCGIFNUM */ 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate /* 1680Sstevel@tonic-gate * for the benefit of hidden NIS domains, we use the same procedure 1690Sstevel@tonic-gate * as sendmail: convert leading + to dot, then drop to first dot 1700Sstevel@tonic-gate */ 1710Sstevel@tonic-gate getdomainname(buf, BUFSIZ); 1720Sstevel@tonic-gate if (buf[0] == '+') 1730Sstevel@tonic-gate buf[0] = '.'; 1740Sstevel@tonic-gate #ifdef SYSV 1750Sstevel@tonic-gate cp = strchr(buf, (int)'.'); 1760Sstevel@tonic-gate #else 1770Sstevel@tonic-gate cp = index(buf, '.'); 1780Sstevel@tonic-gate #endif 1790Sstevel@tonic-gate if (cp == NULL) 1800Sstevel@tonic-gate strcpy(_res.defdname, buf); 1810Sstevel@tonic-gate else 1820Sstevel@tonic-gate strcpy(_res.defdname, cp+1); 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate /* Allow user to override the local domain definition */ 1850Sstevel@tonic-gate if ((cp = getenv("LOCALDOMAIN")) != NULL) { 1860Sstevel@tonic-gate (void) strncpy(_res.defdname, cp, sizeof (_res.defdname)); 1870Sstevel@tonic-gate haveenv++; 1880Sstevel@tonic-gate } 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { 1910Sstevel@tonic-gate /* read the config file */ 1920Sstevel@tonic-gate while (fgets(buf, sizeof (buf), fp) != NULL) { 1930Sstevel@tonic-gate /* read default domain name */ 1940Sstevel@tonic-gate if (!strncmp(buf, "domain", sizeof ("domain") - 1)) { 1950Sstevel@tonic-gate if (haveenv) /* skip if have from environ */ 1960Sstevel@tonic-gate continue; 1970Sstevel@tonic-gate cp = buf + sizeof ("domain") - 1; 1980Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t') 1990Sstevel@tonic-gate cp++; 2000Sstevel@tonic-gate if ((*cp == '\0') || (*cp == '\n')) 2010Sstevel@tonic-gate continue; 2020Sstevel@tonic-gate (void) strncpy(_res.defdname, cp, sizeof (_res.defdname) - 1); 2030Sstevel@tonic-gate #ifdef SYSV 2040Sstevel@tonic-gate if ((cp = strchr(_res.defdname, (int)'\n')) != NULL) 2050Sstevel@tonic-gate #else 2060Sstevel@tonic-gate if ((cp = index(_res.defdname, '\n')) != NULL) 2070Sstevel@tonic-gate #endif 2080Sstevel@tonic-gate *cp = '\0'; 2090Sstevel@tonic-gate havesearch = 0; 2100Sstevel@tonic-gate continue; 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate /* set search list */ 2130Sstevel@tonic-gate if (!strncmp(buf, "search", sizeof ("search") - 1)) { 2140Sstevel@tonic-gate if (haveenv) /* skip if have from environ */ 2150Sstevel@tonic-gate continue; 2160Sstevel@tonic-gate cp = buf + sizeof ("search") - 1; 2170Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t') 2180Sstevel@tonic-gate cp++; 2190Sstevel@tonic-gate if ((*cp == '\0') || (*cp == '\n')) 2200Sstevel@tonic-gate continue; 2210Sstevel@tonic-gate (void) strncpy(_res.defdname, cp, sizeof (_res.defdname) - 1); 2220Sstevel@tonic-gate #ifdef SYSV 2230Sstevel@tonic-gate if ((cp = strchr(_res.defdname, (int)'\n')) != NULL) 2240Sstevel@tonic-gate #else 2250Sstevel@tonic-gate if ((cp = index(_res.defdname, '\n')) != NULL) 2260Sstevel@tonic-gate #endif 2270Sstevel@tonic-gate *cp = '\0'; 2280Sstevel@tonic-gate /* 2290Sstevel@tonic-gate * Set search list to be blank-separated strings 2300Sstevel@tonic-gate * on rest of line. 2310Sstevel@tonic-gate */ 2320Sstevel@tonic-gate cp = _res.defdname; 2330Sstevel@tonic-gate pp = _res.dnsrch; 2340Sstevel@tonic-gate *pp++ = cp; 2350Sstevel@tonic-gate for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) { 2360Sstevel@tonic-gate if (*cp == ' ' || *cp == '\t') { 2370Sstevel@tonic-gate *cp = 0; 2380Sstevel@tonic-gate n = 1; 2390Sstevel@tonic-gate } else if (n) { 2400Sstevel@tonic-gate *pp++ = cp; 2410Sstevel@tonic-gate n = 0; 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate /* null terminate last domain if there are excess */ 2450Sstevel@tonic-gate while (*cp != '\0' && *cp != ' ' && *cp != '\t') 2460Sstevel@tonic-gate cp++; 2470Sstevel@tonic-gate *cp = '\0'; 2480Sstevel@tonic-gate *pp++ = 0; 2490Sstevel@tonic-gate havesearch = 1; 2500Sstevel@tonic-gate continue; 2510Sstevel@tonic-gate } 2520Sstevel@tonic-gate /* read nameservers to query */ 2530Sstevel@tonic-gate if (!strncmp(buf, "nameserver", sizeof ("nameserver") - 1) && 2540Sstevel@tonic-gate (nserv < MAXNS)) { 2550Sstevel@tonic-gate cp = buf + sizeof ("nameserver") - 1; 2560Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t') 2570Sstevel@tonic-gate cp++; 2580Sstevel@tonic-gate if ((*cp == '\0') || (*cp == '\n')) 2590Sstevel@tonic-gate continue; 2600Sstevel@tonic-gate if ((_res.nsaddr_list[nserv].sin_addr.s_addr = 2610Sstevel@tonic-gate inet_addr(cp)) == (unsigned) -1) { 2620Sstevel@tonic-gate _res.nsaddr_list[n].sin_addr.s_addr = INADDR_ANY; 2630Sstevel@tonic-gate continue; 2640Sstevel@tonic-gate } 2650Sstevel@tonic-gate _res.nsaddr_list[nserv].sin_family = AF_INET; 2660Sstevel@tonic-gate _res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT); 2670Sstevel@tonic-gate nserv++; 2680Sstevel@tonic-gate continue; 2690Sstevel@tonic-gate } 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate if (nserv > 1) 2720Sstevel@tonic-gate _res.nscount = nserv; 2730Sstevel@tonic-gate (void) fclose(fp); 2740Sstevel@tonic-gate } 2750Sstevel@tonic-gate if (_res.defdname[0] == 0) { 2760Sstevel@tonic-gate if (gethostname(buf, sizeof (_res.defdname)) == 0 && 2770Sstevel@tonic-gate #ifdef SYSV 2780Sstevel@tonic-gate (cp = strchr(buf, (int)'.'))) 2790Sstevel@tonic-gate #else 2800Sstevel@tonic-gate (cp = index(buf, '.'))) 2810Sstevel@tonic-gate #endif 2820Sstevel@tonic-gate (void) strcpy(_res.defdname, cp + 1); 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate /* find components of local domain that might be searched */ 2860Sstevel@tonic-gate if (havesearch == 0) { 2870Sstevel@tonic-gate pp = _res.dnsrch; 2880Sstevel@tonic-gate *pp++ = _res.defdname; 2890Sstevel@tonic-gate for (cp = _res.defdname, n = 0; *cp; cp++) 2900Sstevel@tonic-gate if (*cp == '.') 2910Sstevel@tonic-gate n++; 2920Sstevel@tonic-gate cp = _res.defdname; 2930Sstevel@tonic-gate for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH; n--) { 2940Sstevel@tonic-gate #ifdef SYSV 2950Sstevel@tonic-gate cp = strchr(cp, (int)'.'); 2960Sstevel@tonic-gate #else 2970Sstevel@tonic-gate cp = index(cp, '.'); 2980Sstevel@tonic-gate #endif 2990Sstevel@tonic-gate *pp++ = ++cp; 3000Sstevel@tonic-gate } 3010Sstevel@tonic-gate *pp++ = 0; 3020Sstevel@tonic-gate } 3030Sstevel@tonic-gate _res.options |= RES_INIT; 3040Sstevel@tonic-gate return (0); 3050Sstevel@tonic-gate } 306