10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * Copyright (c) 1989, 1993, 1995 30Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 60Sstevel@tonic-gate * modification, are permitted provided that the following conditions 70Sstevel@tonic-gate * are met: 80Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 90Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 100Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 110Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 120Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 130Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 140Sstevel@tonic-gate * must display the following acknowledgement: 150Sstevel@tonic-gate * This product includes software developed by the University of 160Sstevel@tonic-gate * California, Berkeley and its contributors. 170Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 180Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 190Sstevel@tonic-gate * without specific prior written permission. 200Sstevel@tonic-gate * 210Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 220Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 230Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 240Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 250Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 260Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 270Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 280Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 290Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 300Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 310Sstevel@tonic-gate * SUCH DAMAGE. 320Sstevel@tonic-gate */ 330Sstevel@tonic-gate 340Sstevel@tonic-gate /* 35*11038SRao.Shoaib@Sun.COM * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 360Sstevel@tonic-gate * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 370Sstevel@tonic-gate * 380Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 390Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 400Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 410Sstevel@tonic-gate * 42*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 43*11038SRao.Shoaib@Sun.COM * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 44*11038SRao.Shoaib@Sun.COM * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 45*11038SRao.Shoaib@Sun.COM * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 46*11038SRao.Shoaib@Sun.COM * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 47*11038SRao.Shoaib@Sun.COM * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 48*11038SRao.Shoaib@Sun.COM * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 490Sstevel@tonic-gate */ 500Sstevel@tonic-gate 510Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint) 52*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: lcl_nw.c,v 1.4 2005/04/27 04:56:31 sra Exp $"; 530Sstevel@tonic-gate /* from getgrent.c 8.2 (Berkeley) 3/21/94"; */ 540Sstevel@tonic-gate /* from BSDI Id: getgrent.c,v 2.8 1996/05/28 18:15:14 bostic Exp $ */ 550Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */ 560Sstevel@tonic-gate 570Sstevel@tonic-gate /* Imports */ 580Sstevel@tonic-gate 590Sstevel@tonic-gate #include "port_before.h" 600Sstevel@tonic-gate 610Sstevel@tonic-gate #include <sys/types.h> 620Sstevel@tonic-gate #include <sys/socket.h> 630Sstevel@tonic-gate 640Sstevel@tonic-gate #include <netinet/in.h> 650Sstevel@tonic-gate #include <arpa/inet.h> 660Sstevel@tonic-gate #include <arpa/nameser.h> 670Sstevel@tonic-gate 680Sstevel@tonic-gate #include <errno.h> 690Sstevel@tonic-gate #include <fcntl.h> 700Sstevel@tonic-gate #include <resolv.h> 710Sstevel@tonic-gate #include <stdio.h> 720Sstevel@tonic-gate #include <stdlib.h> 730Sstevel@tonic-gate #include <string.h> 740Sstevel@tonic-gate 750Sstevel@tonic-gate #include <irs.h> 760Sstevel@tonic-gate #include <isc/memcluster.h> 770Sstevel@tonic-gate 780Sstevel@tonic-gate #include "port_after.h" 790Sstevel@tonic-gate 800Sstevel@tonic-gate #include <isc/misc.h> 810Sstevel@tonic-gate #include "irs_p.h" 820Sstevel@tonic-gate #include "lcl_p.h" 830Sstevel@tonic-gate 840Sstevel@tonic-gate #define MAXALIASES 35 850Sstevel@tonic-gate #define MAXADDRSIZE 4 860Sstevel@tonic-gate 870Sstevel@tonic-gate struct pvt { 880Sstevel@tonic-gate FILE * fp; 890Sstevel@tonic-gate char line[BUFSIZ+1]; 900Sstevel@tonic-gate struct nwent net; 910Sstevel@tonic-gate char * aliases[MAXALIASES]; 920Sstevel@tonic-gate char addr[MAXADDRSIZE]; 930Sstevel@tonic-gate struct __res_state * res; 940Sstevel@tonic-gate void (*free_res)(void *); 950Sstevel@tonic-gate }; 960Sstevel@tonic-gate 970Sstevel@tonic-gate /* Forward */ 980Sstevel@tonic-gate 990Sstevel@tonic-gate static void nw_close(struct irs_nw *); 1000Sstevel@tonic-gate static struct nwent * nw_byname(struct irs_nw *, const char *, int); 1010Sstevel@tonic-gate static struct nwent * nw_byaddr(struct irs_nw *, void *, int, int); 1020Sstevel@tonic-gate static struct nwent * nw_next(struct irs_nw *); 1030Sstevel@tonic-gate static void nw_rewind(struct irs_nw *); 1040Sstevel@tonic-gate static void nw_minimize(struct irs_nw *); 1050Sstevel@tonic-gate static struct __res_state * nw_res_get(struct irs_nw *this); 1060Sstevel@tonic-gate static void nw_res_set(struct irs_nw *this, 1070Sstevel@tonic-gate struct __res_state *res, 1080Sstevel@tonic-gate void (*free_res)(void *)); 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate static int init(struct irs_nw *this); 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate /* Portability. */ 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate #ifndef SEEK_SET 1150Sstevel@tonic-gate # define SEEK_SET 0 1160Sstevel@tonic-gate #endif 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate /* Public */ 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate struct irs_nw * 1210Sstevel@tonic-gate irs_lcl_nw(struct irs_acc *this) { 1220Sstevel@tonic-gate struct irs_nw *nw; 1230Sstevel@tonic-gate struct pvt *pvt; 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate UNUSED(this); 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) { 1280Sstevel@tonic-gate errno = ENOMEM; 1290Sstevel@tonic-gate return (NULL); 1300Sstevel@tonic-gate } 1310Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt); 1320Sstevel@tonic-gate if (!(nw = memget(sizeof *nw))) { 1330Sstevel@tonic-gate memput(pvt, sizeof *pvt); 1340Sstevel@tonic-gate errno = ENOMEM; 1350Sstevel@tonic-gate return (NULL); 1360Sstevel@tonic-gate } 1370Sstevel@tonic-gate memset(nw, 0x5e, sizeof *nw); 1380Sstevel@tonic-gate nw->private = pvt; 1390Sstevel@tonic-gate nw->close = nw_close; 1400Sstevel@tonic-gate nw->byname = nw_byname; 1410Sstevel@tonic-gate nw->byaddr = nw_byaddr; 1420Sstevel@tonic-gate nw->next = nw_next; 1430Sstevel@tonic-gate nw->rewind = nw_rewind; 1440Sstevel@tonic-gate nw->minimize = nw_minimize; 1450Sstevel@tonic-gate nw->res_get = nw_res_get; 1460Sstevel@tonic-gate nw->res_set = nw_res_set; 1470Sstevel@tonic-gate return (nw); 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate /* Methods */ 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate static void 1530Sstevel@tonic-gate nw_close(struct irs_nw *this) { 1540Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate nw_minimize(this); 1570Sstevel@tonic-gate if (pvt->res && pvt->free_res) 1580Sstevel@tonic-gate (*pvt->free_res)(pvt->res); 1590Sstevel@tonic-gate if (pvt->fp) 1600Sstevel@tonic-gate (void)fclose(pvt->fp); 1610Sstevel@tonic-gate memput(pvt, sizeof *pvt); 1620Sstevel@tonic-gate memput(this, sizeof *this); 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate static struct nwent * 1660Sstevel@tonic-gate nw_byaddr(struct irs_nw *this, void *net, int length, int type) { 1670Sstevel@tonic-gate struct nwent *p; 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate if (init(this) == -1) 1700Sstevel@tonic-gate return(NULL); 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate nw_rewind(this); 1730Sstevel@tonic-gate while ((p = nw_next(this)) != NULL) 1740Sstevel@tonic-gate if (p->n_addrtype == type && p->n_length == length) 1750Sstevel@tonic-gate if (bitncmp(p->n_addr, net, length) == 0) 1760Sstevel@tonic-gate break; 1770Sstevel@tonic-gate return (p); 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate static struct nwent * 1810Sstevel@tonic-gate nw_byname(struct irs_nw *this, const char *name, int type) { 1820Sstevel@tonic-gate struct nwent *p; 1830Sstevel@tonic-gate char **ap; 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate if (init(this) == -1) 1860Sstevel@tonic-gate return(NULL); 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate nw_rewind(this); 1890Sstevel@tonic-gate while ((p = nw_next(this)) != NULL) { 1900Sstevel@tonic-gate if (ns_samename(p->n_name, name) == 1 && 1910Sstevel@tonic-gate p->n_addrtype == type) 1920Sstevel@tonic-gate break; 1930Sstevel@tonic-gate for (ap = p->n_aliases; *ap; ap++) 1940Sstevel@tonic-gate if ((ns_samename(*ap, name) == 1) && 1950Sstevel@tonic-gate (p->n_addrtype == type)) 1960Sstevel@tonic-gate goto found; 1970Sstevel@tonic-gate } 1980Sstevel@tonic-gate found: 1990Sstevel@tonic-gate return (p); 2000Sstevel@tonic-gate } 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate static void 2030Sstevel@tonic-gate nw_rewind(struct irs_nw *this) { 2040Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate if (pvt->fp) { 2070Sstevel@tonic-gate if (fseek(pvt->fp, 0L, SEEK_SET) == 0) 2080Sstevel@tonic-gate return; 2090Sstevel@tonic-gate (void)fclose(pvt->fp); 2100Sstevel@tonic-gate } 2110Sstevel@tonic-gate if (!(pvt->fp = fopen(_PATH_NETWORKS, "r"))) 2120Sstevel@tonic-gate return; 2130Sstevel@tonic-gate if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) { 2140Sstevel@tonic-gate (void)fclose(pvt->fp); 2150Sstevel@tonic-gate pvt->fp = NULL; 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate } 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate static struct nwent * 2200Sstevel@tonic-gate nw_next(struct irs_nw *this) { 2210Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 2220Sstevel@tonic-gate struct nwent *ret = NULL; 2230Sstevel@tonic-gate char *p, *cp, **q; 2240Sstevel@tonic-gate char *bufp, *ndbuf, *dbuf = NULL; 2250Sstevel@tonic-gate int c, bufsiz, offset = 0; 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate if (init(this) == -1) 2280Sstevel@tonic-gate return(NULL); 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate if (pvt->fp == NULL) 2310Sstevel@tonic-gate nw_rewind(this); 2320Sstevel@tonic-gate if (pvt->fp == NULL) { 2330Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); 2340Sstevel@tonic-gate return (NULL); 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate bufp = pvt->line; 2370Sstevel@tonic-gate bufsiz = sizeof(pvt->line); 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate again: 2400Sstevel@tonic-gate p = fgets(bufp + offset, bufsiz - offset, pvt->fp); 2410Sstevel@tonic-gate if (p == NULL) 2420Sstevel@tonic-gate goto cleanup; 2430Sstevel@tonic-gate if (!strchr(p, '\n') && !feof(pvt->fp)) { 2440Sstevel@tonic-gate #define GROWBUF 1024 2450Sstevel@tonic-gate /* allocate space for longer line */ 2460Sstevel@tonic-gate if (dbuf == NULL) { 2470Sstevel@tonic-gate if ((ndbuf = malloc(bufsiz + GROWBUF)) != NULL) 2480Sstevel@tonic-gate strcpy(ndbuf, bufp); 2490Sstevel@tonic-gate } else 2500Sstevel@tonic-gate ndbuf = realloc(dbuf, bufsiz + GROWBUF); 2510Sstevel@tonic-gate if (ndbuf) { 2520Sstevel@tonic-gate dbuf = ndbuf; 2530Sstevel@tonic-gate bufp = dbuf; 2540Sstevel@tonic-gate bufsiz += GROWBUF; 2550Sstevel@tonic-gate offset = strlen(dbuf); 2560Sstevel@tonic-gate } else { 2570Sstevel@tonic-gate /* allocation failed; skip this long line */ 2580Sstevel@tonic-gate while ((c = getc(pvt->fp)) != EOF) 2590Sstevel@tonic-gate if (c == '\n') 2600Sstevel@tonic-gate break; 2610Sstevel@tonic-gate if (c != EOF) 2620Sstevel@tonic-gate ungetc(c, pvt->fp); 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate goto again; 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate p -= offset; 2680Sstevel@tonic-gate offset = 0; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate if (*p == '#') 2710Sstevel@tonic-gate goto again; 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate cp = strpbrk(p, "#\n"); 2740Sstevel@tonic-gate if (cp != NULL) 2750Sstevel@tonic-gate *cp = '\0'; 2760Sstevel@tonic-gate pvt->net.n_name = p; 2770Sstevel@tonic-gate cp = strpbrk(p, " \t"); 2780Sstevel@tonic-gate if (cp == NULL) 2790Sstevel@tonic-gate goto again; 2800Sstevel@tonic-gate *cp++ = '\0'; 2810Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t') 2820Sstevel@tonic-gate cp++; 2830Sstevel@tonic-gate p = strpbrk(cp, " \t"); 2840Sstevel@tonic-gate if (p != NULL) 2850Sstevel@tonic-gate *p++ = '\0'; 2860Sstevel@tonic-gate pvt->net.n_length = inet_net_pton(AF_INET, cp, pvt->addr, 2870Sstevel@tonic-gate sizeof pvt->addr); 2880Sstevel@tonic-gate if (pvt->net.n_length < 0) 2890Sstevel@tonic-gate goto again; 2900Sstevel@tonic-gate pvt->net.n_addrtype = AF_INET; 2910Sstevel@tonic-gate pvt->net.n_addr = pvt->addr; 2920Sstevel@tonic-gate q = pvt->net.n_aliases = pvt->aliases; 2930Sstevel@tonic-gate if (p != NULL) { 2940Sstevel@tonic-gate cp = p; 2950Sstevel@tonic-gate while (cp && *cp) { 2960Sstevel@tonic-gate if (*cp == ' ' || *cp == '\t') { 2970Sstevel@tonic-gate cp++; 2980Sstevel@tonic-gate continue; 2990Sstevel@tonic-gate } 3000Sstevel@tonic-gate if (q < &pvt->aliases[MAXALIASES - 1]) 3010Sstevel@tonic-gate *q++ = cp; 3020Sstevel@tonic-gate cp = strpbrk(cp, " \t"); 3030Sstevel@tonic-gate if (cp != NULL) 3040Sstevel@tonic-gate *cp++ = '\0'; 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate } 3070Sstevel@tonic-gate *q = NULL; 3080Sstevel@tonic-gate ret = &pvt->net; 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate cleanup: 3110Sstevel@tonic-gate if (dbuf) 3120Sstevel@tonic-gate free(dbuf); 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate return (ret); 3150Sstevel@tonic-gate } 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate static void 3180Sstevel@tonic-gate nw_minimize(struct irs_nw *this) { 3190Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate if (pvt->res) 3220Sstevel@tonic-gate res_nclose(pvt->res); 3230Sstevel@tonic-gate if (pvt->fp != NULL) { 3240Sstevel@tonic-gate (void)fclose(pvt->fp); 3250Sstevel@tonic-gate pvt->fp = NULL; 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate static struct __res_state * 3300Sstevel@tonic-gate nw_res_get(struct irs_nw *this) { 3310Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate if (!pvt->res) { 3340Sstevel@tonic-gate struct __res_state *res; 3350Sstevel@tonic-gate res = (struct __res_state *)malloc(sizeof *res); 3360Sstevel@tonic-gate if (!res) { 3370Sstevel@tonic-gate errno = ENOMEM; 3380Sstevel@tonic-gate return (NULL); 3390Sstevel@tonic-gate } 3400Sstevel@tonic-gate memset(res, 0, sizeof *res); 3410Sstevel@tonic-gate nw_res_set(this, res, free); 3420Sstevel@tonic-gate } 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate return (pvt->res); 3450Sstevel@tonic-gate } 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate static void 3480Sstevel@tonic-gate nw_res_set(struct irs_nw *this, struct __res_state *res, 3490Sstevel@tonic-gate void (*free_res)(void *)) { 3500Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate if (pvt->res && pvt->free_res) { 3530Sstevel@tonic-gate res_nclose(pvt->res); 3540Sstevel@tonic-gate (*pvt->free_res)(pvt->res); 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate pvt->res = res; 3580Sstevel@tonic-gate pvt->free_res = free_res; 3590Sstevel@tonic-gate } 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate static int 3620Sstevel@tonic-gate init(struct irs_nw *this) { 3630Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate if (!pvt->res && !nw_res_get(this)) 3660Sstevel@tonic-gate return (-1); 367*11038SRao.Shoaib@Sun.COM if (((pvt->res->options & RES_INIT) == 0U) && 3680Sstevel@tonic-gate res_ninit(pvt->res) == -1) 3690Sstevel@tonic-gate return (-1); 3700Sstevel@tonic-gate return (0); 3710Sstevel@tonic-gate } 372*11038SRao.Shoaib@Sun.COM 373*11038SRao.Shoaib@Sun.COM /*! \file */ 374