1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 28*0Sstevel@tonic-gate /* All Rights Reserved */ 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate /* 31*0Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD 32*0Sstevel@tonic-gate * under license from the Regents of the University of California. 33*0Sstevel@tonic-gate */ 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate #include "mt.h" 38*0Sstevel@tonic-gate #include "../rpc/rpc_mt.h" /* for MT declarations only */ 39*0Sstevel@tonic-gate #include <rpc/types.h> 40*0Sstevel@tonic-gate #include <stdio.h> 41*0Sstevel@tonic-gate #include <string.h> 42*0Sstevel@tonic-gate #include <ctype.h> 43*0Sstevel@tonic-gate #include <rpc/trace.h> 44*0Sstevel@tonic-gate #include <netconfig.h> 45*0Sstevel@tonic-gate #include <malloc.h> 46*0Sstevel@tonic-gate #include <syslog.h> 47*0Sstevel@tonic-gate #include "netcspace.h" 48*0Sstevel@tonic-gate #include "nsl_stdio_prv.h" 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate #define FAILURE (unsigned)(-1) 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate /* 53*0Sstevel@tonic-gate * Local routines used by the library procedures 54*0Sstevel@tonic-gate */ 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate static int blank(); 57*0Sstevel@tonic-gate static int comment(); 58*0Sstevel@tonic-gate static struct netconfig *fgetnetconfig(); 59*0Sstevel@tonic-gate static void netconfig_free(); 60*0Sstevel@tonic-gate static unsigned int getflag(); 61*0Sstevel@tonic-gate static char **getlookups(); 62*0Sstevel@tonic-gate static struct netconfig **getnetlist(); 63*0Sstevel@tonic-gate static unsigned int getnlookups(); 64*0Sstevel@tonic-gate static char *gettoken(); 65*0Sstevel@tonic-gate static unsigned int getvalue(); 66*0Sstevel@tonic-gate static void shift1left(); 67*0Sstevel@tonic-gate static void netlist_free(); 68*0Sstevel@tonic-gate static void free_entry(); 69*0Sstevel@tonic-gate static struct netconfig *netconfig_dup(); 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate /* 72*0Sstevel@tonic-gate * System V routines used by the library procedures. 73*0Sstevel@tonic-gate */ 74*0Sstevel@tonic-gate extern char *getenv(); 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate /* messaging stuff. */ 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate extern const char __nsl_dom[]; 79*0Sstevel@tonic-gate extern char *dgettext(const char *, const char *); 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate /* 82*0Sstevel@tonic-gate * Static global variables used by the library procedures: 83*0Sstevel@tonic-gate * 84*0Sstevel@tonic-gate * netpp - points to the beginning of the list of netconfig 85*0Sstevel@tonic-gate * entries used by setnetconfig() and setnetpath(). 86*0Sstevel@tonic-gate * Once netpp is initialized, that memory is *never* 87*0Sstevel@tonic-gate * released. This was necessary to improve performance. 88*0Sstevel@tonic-gate * 89*0Sstevel@tonic-gate * linenum - the current line number of the /etc/netconfig 90*0Sstevel@tonic-gate * file (used for debugging and for nc_perror()). 91*0Sstevel@tonic-gate * 92*0Sstevel@tonic-gate * fieldnum - the current field number of the current line 93*0Sstevel@tonic-gate * of /etc/netconfig (used for debugging and for 94*0Sstevel@tonic-gate * nc_perror()). 95*0Sstevel@tonic-gate * 96*0Sstevel@tonic-gate * nc_error - the error condition encountered. 97*0Sstevel@tonic-gate */ 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gate static struct netconfig **netpp = NULL; 100*0Sstevel@tonic-gate mutex_t netpp_mutex = DEFAULTMUTEX; 101*0Sstevel@tonic-gate /* 102*0Sstevel@tonic-gate * The following two variables are used by the /etc/netconfig parsing 103*0Sstevel@tonic-gate * routines, which will always be executed once, and within the netpp_mutex. 104*0Sstevel@tonic-gate * They are global to allow the nc_sperror routine to provide better 105*0Sstevel@tonic-gate * information to the user about /etc/netconfig file problems. 106*0Sstevel@tonic-gate */ 107*0Sstevel@tonic-gate static int linenum = 0; /* "owned" by getnetlist() */ 108*0Sstevel@tonic-gate static int fieldnum = 0; /* "owned" by fgetnetconfig() */ 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate static int * 112*0Sstevel@tonic-gate __nc_error() 113*0Sstevel@tonic-gate { 114*0Sstevel@tonic-gate static pthread_key_t nc_error_key = 0; 115*0Sstevel@tonic-gate static int nc_error = NC_NOERROR; 116*0Sstevel@tonic-gate int *ret; 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate if (thr_main()) 119*0Sstevel@tonic-gate return (&nc_error); 120*0Sstevel@tonic-gate ret = thr_get_storage(&nc_error_key, sizeof (int), free); 121*0Sstevel@tonic-gate /* if thr_get_storage fails we return the address of nc_error */ 122*0Sstevel@tonic-gate return (ret ? ret : &nc_error); 123*0Sstevel@tonic-gate } 124*0Sstevel@tonic-gate #define nc_error (*(__nc_error())) 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate /* 127*0Sstevel@tonic-gate * setnetconfig() has the effect of "initializing" the 128*0Sstevel@tonic-gate * network configuration database. It reads in the 129*0Sstevel@tonic-gate * netcf entries (if not already read in). 130*0Sstevel@tonic-gate */ 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate void * 133*0Sstevel@tonic-gate setnetconfig() 134*0Sstevel@tonic-gate { 135*0Sstevel@tonic-gate NCONF_HANDLE *retp; 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate trace1(TR_setnetconfig, 0); 138*0Sstevel@tonic-gate mutex_lock(&netpp_mutex); 139*0Sstevel@tonic-gate if ((netpp == NULL) && ((netpp = getnetlist()) == NULL)) { 140*0Sstevel@tonic-gate mutex_unlock(&netpp_mutex); 141*0Sstevel@tonic-gate trace1(TR_setnetconfig, 1); 142*0Sstevel@tonic-gate return (NULL); 143*0Sstevel@tonic-gate } 144*0Sstevel@tonic-gate mutex_unlock(&netpp_mutex); 145*0Sstevel@tonic-gate if ((retp = (NCONF_HANDLE *) malloc(sizeof (NCONF_HANDLE))) == NULL) { 146*0Sstevel@tonic-gate nc_error = NC_NOMEM; 147*0Sstevel@tonic-gate trace1(TR_setnetconfig, 1); 148*0Sstevel@tonic-gate return (NULL); 149*0Sstevel@tonic-gate } 150*0Sstevel@tonic-gate nc_error = NC_NOERROR; 151*0Sstevel@tonic-gate retp->nc_head = retp->nc_curr = netpp; 152*0Sstevel@tonic-gate trace1(TR_setnetconfig, 1); 153*0Sstevel@tonic-gate return ((void *)retp); 154*0Sstevel@tonic-gate } 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate /* 157*0Sstevel@tonic-gate * endnetconfig() frees up all data allocated by setnetconfig() 158*0Sstevel@tonic-gate */ 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate int 161*0Sstevel@tonic-gate endnetconfig(vdata) 162*0Sstevel@tonic-gate void *vdata; 163*0Sstevel@tonic-gate { 164*0Sstevel@tonic-gate NCONF_HANDLE *nconf_handlep = (NCONF_HANDLE *)vdata; 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate trace1(TR_endnetconfig, 0); 167*0Sstevel@tonic-gate mutex_lock(&netpp_mutex); 168*0Sstevel@tonic-gate if (netpp == NULL || nconf_handlep == NULL) { 169*0Sstevel@tonic-gate nc_error = NC_NOSET; 170*0Sstevel@tonic-gate mutex_unlock(&netpp_mutex); 171*0Sstevel@tonic-gate trace1(TR_endnetconfig, 1); 172*0Sstevel@tonic-gate return (-1); 173*0Sstevel@tonic-gate } 174*0Sstevel@tonic-gate mutex_unlock(&netpp_mutex); 175*0Sstevel@tonic-gate 176*0Sstevel@tonic-gate nc_error = NC_NOERROR; 177*0Sstevel@tonic-gate free((void *)nconf_handlep); 178*0Sstevel@tonic-gate trace1(TR_endnetconfig, 1); 179*0Sstevel@tonic-gate return (0); 180*0Sstevel@tonic-gate } 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate /* 183*0Sstevel@tonic-gate * getnetconfig() returns the current entry in the list 184*0Sstevel@tonic-gate * of netconfig structures. It uses the nconf_handlep argument 185*0Sstevel@tonic-gate * to determine the current entry. If setnetconfig() was not 186*0Sstevel@tonic-gate * called previously to set up the list, return failure. 187*0Sstevel@tonic-gate * It also check if ipv6 interface is present(ipv6_present) and 188*0Sstevel@tonic-gate * skips udp6 & tcp6 entries if ipv6 is not supported. 189*0Sstevel@tonic-gate */ 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate struct netconfig * 192*0Sstevel@tonic-gate getnetconfig(vdata) 193*0Sstevel@tonic-gate void *vdata; 194*0Sstevel@tonic-gate { 195*0Sstevel@tonic-gate NCONF_HANDLE *nconf_handlep = (NCONF_HANDLE *)vdata; 196*0Sstevel@tonic-gate struct netconfig *retp; /* holds the return value */ 197*0Sstevel@tonic-gate int ipv6_present = -1; 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate trace1(TR_getnetconfig, 0); 200*0Sstevel@tonic-gate mutex_lock(&netpp_mutex); 201*0Sstevel@tonic-gate if ((netpp == NULL) || (nconf_handlep == NULL)) { 202*0Sstevel@tonic-gate nc_error = NC_NOSET; 203*0Sstevel@tonic-gate mutex_unlock(&netpp_mutex); 204*0Sstevel@tonic-gate trace1(TR_getnetconfig, 1); 205*0Sstevel@tonic-gate return (NULL); 206*0Sstevel@tonic-gate } 207*0Sstevel@tonic-gate mutex_unlock(&netpp_mutex); 208*0Sstevel@tonic-gate for (;;) { 209*0Sstevel@tonic-gate retp = *(nconf_handlep->nc_curr); 210*0Sstevel@tonic-gate if (retp && (strcmp(retp->nc_netid, "udp6") == 0 || 211*0Sstevel@tonic-gate strcmp(retp->nc_netid, "tcp6") == 0)) { 212*0Sstevel@tonic-gate if (ipv6_present == -1) 213*0Sstevel@tonic-gate ipv6_present = __can_use_af(AF_INET6); 214*0Sstevel@tonic-gate if (!ipv6_present) { 215*0Sstevel@tonic-gate ++(nconf_handlep->nc_curr); 216*0Sstevel@tonic-gate continue; 217*0Sstevel@tonic-gate } 218*0Sstevel@tonic-gate } 219*0Sstevel@tonic-gate break; 220*0Sstevel@tonic-gate } 221*0Sstevel@tonic-gate if (retp != NULL) { 222*0Sstevel@tonic-gate ++(nconf_handlep->nc_curr); 223*0Sstevel@tonic-gate nc_error = NC_NOERROR; 224*0Sstevel@tonic-gate } else { 225*0Sstevel@tonic-gate nc_error = NC_NOMOREENTRIES; 226*0Sstevel@tonic-gate } 227*0Sstevel@tonic-gate trace1(TR_getnetconfig, 1); 228*0Sstevel@tonic-gate return (retp); 229*0Sstevel@tonic-gate } 230*0Sstevel@tonic-gate 231*0Sstevel@tonic-gate /* 232*0Sstevel@tonic-gate * getnetconfig() searches the netconfig database for a 233*0Sstevel@tonic-gate * given network id. Returns a pointer to the netconfig 234*0Sstevel@tonic-gate * structure or a NULL if not found. 235*0Sstevel@tonic-gate * It also check if ipv6 interface is present(ipv6_present) and 236*0Sstevel@tonic-gate * skips udp6 & tcp6 entries if ipv6 is not supported. 237*0Sstevel@tonic-gate */ 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate struct netconfig * 240*0Sstevel@tonic-gate getnetconfigent(netid) 241*0Sstevel@tonic-gate const char *netid; 242*0Sstevel@tonic-gate { 243*0Sstevel@tonic-gate struct netconfig **tpp; 244*0Sstevel@tonic-gate int ipv6_present; 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate trace1(TR_getnetconfigent, 0); 247*0Sstevel@tonic-gate mutex_lock(&netpp_mutex); 248*0Sstevel@tonic-gate if ((netpp == NULL) && ((netpp = getnetlist()) == NULL)) { 249*0Sstevel@tonic-gate mutex_unlock(&netpp_mutex); 250*0Sstevel@tonic-gate trace1(TR_getnetconfigent, 1); 251*0Sstevel@tonic-gate return (NULL); 252*0Sstevel@tonic-gate } 253*0Sstevel@tonic-gate mutex_unlock(&netpp_mutex); 254*0Sstevel@tonic-gate for (tpp = netpp; *tpp; tpp++) { 255*0Sstevel@tonic-gate if (strcmp((*tpp)->nc_netid, netid) == 0) { 256*0Sstevel@tonic-gate if (*tpp && (strcmp((*tpp)->nc_netid, "udp6") == 0 || 257*0Sstevel@tonic-gate strcmp((*tpp)->nc_netid, "tcp6") == 0)) { 258*0Sstevel@tonic-gate ipv6_present = __can_use_af(AF_INET6); 259*0Sstevel@tonic-gate if (!ipv6_present) { 260*0Sstevel@tonic-gate nc_error = NC_NOTFOUND; 261*0Sstevel@tonic-gate trace1(TR_getnetconfigent, 1); 262*0Sstevel@tonic-gate return (NULL); 263*0Sstevel@tonic-gate } 264*0Sstevel@tonic-gate } 265*0Sstevel@tonic-gate trace1(TR_getnetconfigent, 1); 266*0Sstevel@tonic-gate return (netconfig_dup(*tpp)); 267*0Sstevel@tonic-gate } 268*0Sstevel@tonic-gate } 269*0Sstevel@tonic-gate nc_error = NC_NOTFOUND; 270*0Sstevel@tonic-gate trace1(TR_getnetconfigent, 1); 271*0Sstevel@tonic-gate return (NULL); 272*0Sstevel@tonic-gate } 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate /* 275*0Sstevel@tonic-gate * freenetconfigent frees the data allocated by getnetconfigent() 276*0Sstevel@tonic-gate */ 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate void 279*0Sstevel@tonic-gate freenetconfigent(netp) 280*0Sstevel@tonic-gate struct netconfig *netp; 281*0Sstevel@tonic-gate { 282*0Sstevel@tonic-gate trace1(TR_freenetconfigent, 0); 283*0Sstevel@tonic-gate netconfig_free(netp); 284*0Sstevel@tonic-gate trace1(TR_freenetconfigent, 1); 285*0Sstevel@tonic-gate } 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate /* 288*0Sstevel@tonic-gate * getnetlist() reads the netconfig file and creates a 289*0Sstevel@tonic-gate * NULL-terminated list of entries. 290*0Sstevel@tonic-gate * Returns the pointer to the head of the list or a NULL 291*0Sstevel@tonic-gate * on failure. 292*0Sstevel@tonic-gate */ 293*0Sstevel@tonic-gate 294*0Sstevel@tonic-gate static struct netconfig ** 295*0Sstevel@tonic-gate getnetlist() 296*0Sstevel@tonic-gate { 297*0Sstevel@tonic-gate char line[BUFSIZ]; /* holds each line of NETCONFIG */ 298*0Sstevel@tonic-gate __NSL_FILE *fp; /* file stream for NETCONFIG */ 299*0Sstevel@tonic-gate struct netconfig **listpp; /* the beginning of the netconfig list */ 300*0Sstevel@tonic-gate struct netconfig **tpp; /* used to traverse the netconfig list */ 301*0Sstevel@tonic-gate int count; /* the number of entries in file */ 302*0Sstevel@tonic-gate 303*0Sstevel@tonic-gate trace1(TR_getnetlist, 0); 304*0Sstevel@tonic-gate if ((fp = __nsl_fopen(NETCONFIG, "r")) == NULL) { 305*0Sstevel@tonic-gate nc_error = NC_OPENFAIL; 306*0Sstevel@tonic-gate trace1(TR_getnetlist, 1); 307*0Sstevel@tonic-gate return (NULL); 308*0Sstevel@tonic-gate } 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate count = 0; 311*0Sstevel@tonic-gate while (__nsl_fgets(line, BUFSIZ, fp)) { 312*0Sstevel@tonic-gate if (!(blank(line) || comment(line))) { 313*0Sstevel@tonic-gate ++count; 314*0Sstevel@tonic-gate } 315*0Sstevel@tonic-gate } 316*0Sstevel@tonic-gate __nsl_rewind(fp); 317*0Sstevel@tonic-gate 318*0Sstevel@tonic-gate if (count == 0) { 319*0Sstevel@tonic-gate nc_error = NC_NOTFOUND; 320*0Sstevel@tonic-gate (void) __nsl_fclose(fp); 321*0Sstevel@tonic-gate trace1(TR_getnetlist, 1); 322*0Sstevel@tonic-gate return (NULL); 323*0Sstevel@tonic-gate } 324*0Sstevel@tonic-gate if ((listpp = (struct netconfig **)malloc((count + 1) * 325*0Sstevel@tonic-gate sizeof (struct netconfig *))) == NULL) { 326*0Sstevel@tonic-gate nc_error = NC_NOMEM; 327*0Sstevel@tonic-gate (void) __nsl_fclose(fp); 328*0Sstevel@tonic-gate trace1(TR_getnetlist, 1); 329*0Sstevel@tonic-gate return (NULL); 330*0Sstevel@tonic-gate } 331*0Sstevel@tonic-gate 332*0Sstevel@tonic-gate /* 333*0Sstevel@tonic-gate * The following loop fills in the list (loops until 334*0Sstevel@tonic-gate * fgetnetconfig() returns a NULL) and counts the 335*0Sstevel@tonic-gate * number of entries placed in the list. Note that 336*0Sstevel@tonic-gate * when the loop is completed, the last entry in the 337*0Sstevel@tonic-gate * list will contain a NULL (signifying the end of 338*0Sstevel@tonic-gate * the list). 339*0Sstevel@tonic-gate */ 340*0Sstevel@tonic-gate linenum = 0; 341*0Sstevel@tonic-gate for (tpp = listpp; *tpp = fgetnetconfig(fp, NULL); tpp++) 342*0Sstevel@tonic-gate ; 343*0Sstevel@tonic-gate (void) __nsl_fclose(fp); 344*0Sstevel@tonic-gate 345*0Sstevel@tonic-gate if (nc_error != NC_NOMOREENTRIES) /* Something is screwed up */ 346*0Sstevel@tonic-gate netlist_free(&listpp); 347*0Sstevel@tonic-gate trace1(TR_getnetlist, 1); 348*0Sstevel@tonic-gate return (listpp); 349*0Sstevel@tonic-gate } 350*0Sstevel@tonic-gate 351*0Sstevel@tonic-gate /* 352*0Sstevel@tonic-gate * fgetnetconfig() parses a line of the netconfig file into 353*0Sstevel@tonic-gate * a netconfig structure. It returns a pointer to the 354*0Sstevel@tonic-gate * structure of success and a NULL on failure or EOF. 355*0Sstevel@tonic-gate */ 356*0Sstevel@tonic-gate 357*0Sstevel@tonic-gate static struct netconfig * 358*0Sstevel@tonic-gate fgetnetconfig(fp, netid) 359*0Sstevel@tonic-gate __NSL_FILE *fp; 360*0Sstevel@tonic-gate char *netid; 361*0Sstevel@tonic-gate { 362*0Sstevel@tonic-gate char linep[BUFSIZ]; /* pointer to a line in the file */ 363*0Sstevel@tonic-gate struct netconfig *netconfigp; /* holds the new netconfig structure */ 364*0Sstevel@tonic-gate char *tok1, *tok2, *tok3; /* holds a token from the line */ 365*0Sstevel@tonic-gate char *retvalp; /* the return value of fgets() */ 366*0Sstevel@tonic-gate char *entnetid; /* netid for the current entry */ 367*0Sstevel@tonic-gate 368*0Sstevel@tonic-gate trace1(TR_fgetnetconfig, 0); 369*0Sstevel@tonic-gate /* skip past blank lines and comments. */ 370*0Sstevel@tonic-gate while (retvalp = __nsl_fgets(linep, BUFSIZ, fp)) { 371*0Sstevel@tonic-gate linenum++; 372*0Sstevel@tonic-gate if (!(blank(linep) || comment(linep))) { 373*0Sstevel@tonic-gate break; 374*0Sstevel@tonic-gate } 375*0Sstevel@tonic-gate retvalp = NULL; 376*0Sstevel@tonic-gate } 377*0Sstevel@tonic-gate if (retvalp == NULL) { 378*0Sstevel@tonic-gate nc_error = NC_NOMOREENTRIES; 379*0Sstevel@tonic-gate trace1(TR_fgetnetconfig, 1); 380*0Sstevel@tonic-gate return (NULL); 381*0Sstevel@tonic-gate } 382*0Sstevel@tonic-gate fieldnum = 0; 383*0Sstevel@tonic-gate if ((entnetid = gettoken(linep, FALSE)) == NULL) { 384*0Sstevel@tonic-gate nc_error = NC_BADLINE; 385*0Sstevel@tonic-gate trace1(TR_fgetnetconfig, 1); 386*0Sstevel@tonic-gate return (NULL); 387*0Sstevel@tonic-gate } 388*0Sstevel@tonic-gate if (netid && (strcmp(netid, entnetid) != 0)) { 389*0Sstevel@tonic-gate free(entnetid); 390*0Sstevel@tonic-gate nc_error = NC_NOTFOUND; 391*0Sstevel@tonic-gate trace1(TR_fgetnetconfig, 1); 392*0Sstevel@tonic-gate return (NULL); 393*0Sstevel@tonic-gate } 394*0Sstevel@tonic-gate if ((netconfigp = (struct netconfig *) 395*0Sstevel@tonic-gate calloc(1, sizeof (struct netconfig))) == NULL) { 396*0Sstevel@tonic-gate free(entnetid); 397*0Sstevel@tonic-gate nc_error = NC_NOMEM; 398*0Sstevel@tonic-gate trace1(TR_fgetnetconfig, 1); 399*0Sstevel@tonic-gate return (NULL); 400*0Sstevel@tonic-gate } 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gate tok1 = tok2 = tok3 = NULL; 403*0Sstevel@tonic-gate netconfigp->nc_netid = entnetid; 404*0Sstevel@tonic-gate if (((tok1 = gettoken(NULL, FALSE)) == NULL) || 405*0Sstevel@tonic-gate ((netconfigp->nc_semantics = 406*0Sstevel@tonic-gate getvalue(tok1, nc_semantics)) == FAILURE) || 407*0Sstevel@tonic-gate ((tok2 = gettoken(NULL, FALSE)) == NULL) || 408*0Sstevel@tonic-gate ((netconfigp->nc_flag = getflag(tok2)) == FAILURE) || 409*0Sstevel@tonic-gate ((netconfigp->nc_protofmly = gettoken(NULL, FALSE)) == NULL) || 410*0Sstevel@tonic-gate ((netconfigp->nc_proto = gettoken(NULL, FALSE)) == NULL) || 411*0Sstevel@tonic-gate ((netconfigp->nc_device = gettoken(NULL, FALSE)) == NULL) || 412*0Sstevel@tonic-gate ((tok3 = gettoken(NULL, TRUE)) == NULL) || 413*0Sstevel@tonic-gate (((netconfigp->nc_nlookups = getnlookups(tok3)) != 0) && 414*0Sstevel@tonic-gate ((netconfigp->nc_lookups = getlookups(tok3)) == NULL))) { 415*0Sstevel@tonic-gate netconfig_free(netconfigp); 416*0Sstevel@tonic-gate nc_error = NC_BADLINE; 417*0Sstevel@tonic-gate netconfigp = NULL; 418*0Sstevel@tonic-gate } 419*0Sstevel@tonic-gate free(tok1); 420*0Sstevel@tonic-gate free(tok2); 421*0Sstevel@tonic-gate free(tok3); 422*0Sstevel@tonic-gate trace1(TR_fgetnetconfig, 1); 423*0Sstevel@tonic-gate return (netconfigp); 424*0Sstevel@tonic-gate } 425*0Sstevel@tonic-gate 426*0Sstevel@tonic-gate /* 427*0Sstevel@tonic-gate * setnetpath() has the effect of "initializing" the 428*0Sstevel@tonic-gate * NETPATH variable. It reads in the netcf entries (if not 429*0Sstevel@tonic-gate * already read in), creates a list corresponding to the entries 430*0Sstevel@tonic-gate * in the NETPATH variable (or the "visible" entries og netconfig 431*0Sstevel@tonic-gate * if NETPATH is not set). 432*0Sstevel@tonic-gate */ 433*0Sstevel@tonic-gate 434*0Sstevel@tonic-gate void * 435*0Sstevel@tonic-gate setnetpath() 436*0Sstevel@tonic-gate { 437*0Sstevel@tonic-gate int count; /* the number of entries in NETPATH */ 438*0Sstevel@tonic-gate char valid_netpath[BUFSIZ]; /* holds the valid entries if NETPATH */ 439*0Sstevel@tonic-gate char templine[BUFSIZ]; /* has value of NETPATH when scanning */ 440*0Sstevel@tonic-gate struct netconfig **curr_pp; /* scans the list from NETPATH */ 441*0Sstevel@tonic-gate struct netconfig **tpp; /* scans the list from netconfig file */ 442*0Sstevel@tonic-gate struct netconfig **rnetpp; /* the list of entries from NETPATH */ 443*0Sstevel@tonic-gate char *netpath; /* value of NETPATH from environment */ 444*0Sstevel@tonic-gate char *netid; /* holds a component of NETPATH */ 445*0Sstevel@tonic-gate char *tp; /* used to scan NETPATH string */ 446*0Sstevel@tonic-gate NCONF_HANDLE *retp; /* the return value */ 447*0Sstevel@tonic-gate 448*0Sstevel@tonic-gate trace1(TR_setnetpath, 0); 449*0Sstevel@tonic-gate /* 450*0Sstevel@tonic-gate * Read in the netconfig database if not already read in 451*0Sstevel@tonic-gate */ 452*0Sstevel@tonic-gate mutex_lock(&netpp_mutex); 453*0Sstevel@tonic-gate if ((netpp == NULL) && ((netpp = getnetlist()) == NULL)) { 454*0Sstevel@tonic-gate mutex_unlock(&netpp_mutex); 455*0Sstevel@tonic-gate trace1(TR_setnetpath, 1); 456*0Sstevel@tonic-gate return (NULL); 457*0Sstevel@tonic-gate } 458*0Sstevel@tonic-gate mutex_unlock(&netpp_mutex); 459*0Sstevel@tonic-gate 460*0Sstevel@tonic-gate if ((retp = (NCONF_HANDLE *) malloc(sizeof (NCONF_HANDLE))) == NULL) { 461*0Sstevel@tonic-gate nc_error = NC_NOMEM; 462*0Sstevel@tonic-gate trace1(TR_setnetpath, 1); 463*0Sstevel@tonic-gate return (NULL); 464*0Sstevel@tonic-gate } 465*0Sstevel@tonic-gate 466*0Sstevel@tonic-gate /* 467*0Sstevel@tonic-gate * Get the valid entries of the NETPATH variable (and 468*0Sstevel@tonic-gate * count the number of entries while doing it). 469*0Sstevel@tonic-gate * 470*0Sstevel@tonic-gate * This is done every time the procedure is called just 471*0Sstevel@tonic-gate * in case NETPATH has changed from call to call. 472*0Sstevel@tonic-gate * 473*0Sstevel@tonic-gate * If NETPATH is too long, we ignore it altogether as 474*0Sstevel@tonic-gate * it can only be a buffer overflow attack. 475*0Sstevel@tonic-gate * Since we add one colon for each entry, but colons only 476*0Sstevel@tonic-gate * need to exist between entries, we have to subtract one. 477*0Sstevel@tonic-gate */ 478*0Sstevel@tonic-gate count = 0; 479*0Sstevel@tonic-gate valid_netpath[0] = '\0'; 480*0Sstevel@tonic-gate if ((netpath = getenv(NETPATH)) == NULL || 481*0Sstevel@tonic-gate strlen(netpath) >= sizeof (templine) - 1) { 482*0Sstevel@tonic-gate /* 483*0Sstevel@tonic-gate * If NETPATH variable is not set or invalid, 484*0Sstevel@tonic-gate * the valid NETPATH consist of all "visible" 485*0Sstevel@tonic-gate * netids from the netconfig database. 486*0Sstevel@tonic-gate */ 487*0Sstevel@tonic-gate 488*0Sstevel@tonic-gate for (tpp = netpp; *tpp; tpp++) { 489*0Sstevel@tonic-gate if ((*tpp)->nc_flag & NC_VISIBLE) { 490*0Sstevel@tonic-gate (void) strcat(valid_netpath, (*tpp)->nc_netid); 491*0Sstevel@tonic-gate (void) strcat(valid_netpath, ":"); 492*0Sstevel@tonic-gate count++; 493*0Sstevel@tonic-gate } 494*0Sstevel@tonic-gate } 495*0Sstevel@tonic-gate } else { 496*0Sstevel@tonic-gate 497*0Sstevel@tonic-gate /* 498*0Sstevel@tonic-gate * Copy the value of NETPATH (since '\0's will be 499*0Sstevel@tonic-gate * put into the string) and create the valid NETPATH 500*0Sstevel@tonic-gate * (by throwing away all netids not in the database). 501*0Sstevel@tonic-gate * If an entry appears more than one, it *will* be 502*0Sstevel@tonic-gate * listed twice in the list of valid netpath entries. 503*0Sstevel@tonic-gate */ 504*0Sstevel@tonic-gate 505*0Sstevel@tonic-gate (void) strcpy(templine, netpath); 506*0Sstevel@tonic-gate tp = templine; 507*0Sstevel@tonic-gate 508*0Sstevel@tonic-gate while (*tp) { 509*0Sstevel@tonic-gate /* Skip all leading ':'s */ 510*0Sstevel@tonic-gate while (*tp && *tp == ':') 511*0Sstevel@tonic-gate tp++; 512*0Sstevel@tonic-gate if (*tp == NULL) 513*0Sstevel@tonic-gate break; /* last one */ 514*0Sstevel@tonic-gate netid = tp; 515*0Sstevel@tonic-gate while (*tp && *tp != ':') 516*0Sstevel@tonic-gate tp++; 517*0Sstevel@tonic-gate if (*tp) 518*0Sstevel@tonic-gate *tp++ = '\0'; /* isolate netid */ 519*0Sstevel@tonic-gate 520*0Sstevel@tonic-gate for (tpp = netpp; *tpp; tpp++) { 521*0Sstevel@tonic-gate if (strcmp(netid, (*tpp)->nc_netid) == 0) { 522*0Sstevel@tonic-gate (void) strcat(valid_netpath, 523*0Sstevel@tonic-gate (*tpp)->nc_netid); 524*0Sstevel@tonic-gate (void) strcat(valid_netpath, ":"); 525*0Sstevel@tonic-gate count++; 526*0Sstevel@tonic-gate break; 527*0Sstevel@tonic-gate } 528*0Sstevel@tonic-gate } 529*0Sstevel@tonic-gate } 530*0Sstevel@tonic-gate } 531*0Sstevel@tonic-gate 532*0Sstevel@tonic-gate /* Get space to hold the valid list (+1 for the NULL) */ 533*0Sstevel@tonic-gate 534*0Sstevel@tonic-gate if ((rnetpp = (struct netconfig **)malloc((count + 1) * 535*0Sstevel@tonic-gate sizeof (struct netconfig *))) == NULL) { 536*0Sstevel@tonic-gate free((void *) retp); 537*0Sstevel@tonic-gate nc_error = NC_NOMEM; 538*0Sstevel@tonic-gate trace1(TR_setnetpath, 1); 539*0Sstevel@tonic-gate return (NULL); 540*0Sstevel@tonic-gate } 541*0Sstevel@tonic-gate 542*0Sstevel@tonic-gate /* 543*0Sstevel@tonic-gate * Populate the NETPATH list, ending it with a NULL. 544*0Sstevel@tonic-gate * Each entry in the list points to the structure in the 545*0Sstevel@tonic-gate * "netpp" list (the entry must exist in the list, otherwise 546*0Sstevel@tonic-gate * it wouldn't appear in valid_netpath[]). 547*0Sstevel@tonic-gate */ 548*0Sstevel@tonic-gate 549*0Sstevel@tonic-gate curr_pp = rnetpp; 550*0Sstevel@tonic-gate netid = tp = valid_netpath; 551*0Sstevel@tonic-gate while (*tp) { 552*0Sstevel@tonic-gate netid = tp; 553*0Sstevel@tonic-gate while (*tp && *tp != ':') 554*0Sstevel@tonic-gate tp++; 555*0Sstevel@tonic-gate if (*tp) 556*0Sstevel@tonic-gate *tp++ = '\0'; 557*0Sstevel@tonic-gate for (tpp = netpp; *tpp; tpp++) { 558*0Sstevel@tonic-gate if (strcmp(netid, (*tpp)->nc_netid) == 0) { 559*0Sstevel@tonic-gate *curr_pp++ = *tpp; 560*0Sstevel@tonic-gate break; 561*0Sstevel@tonic-gate } 562*0Sstevel@tonic-gate } 563*0Sstevel@tonic-gate } 564*0Sstevel@tonic-gate *curr_pp = NULL; 565*0Sstevel@tonic-gate 566*0Sstevel@tonic-gate retp->nc_curr = retp->nc_head = rnetpp; 567*0Sstevel@tonic-gate trace1(TR_setnetpath, 1); 568*0Sstevel@tonic-gate return ((void *)retp); 569*0Sstevel@tonic-gate } 570*0Sstevel@tonic-gate 571*0Sstevel@tonic-gate /* 572*0Sstevel@tonic-gate * endnetpath() frees up all of the memory allocated by setnetpath(). 573*0Sstevel@tonic-gate * It returns -1 (error) if setnetpath was never called. 574*0Sstevel@tonic-gate */ 575*0Sstevel@tonic-gate 576*0Sstevel@tonic-gate int 577*0Sstevel@tonic-gate endnetpath(vdata) 578*0Sstevel@tonic-gate void *vdata; 579*0Sstevel@tonic-gate { 580*0Sstevel@tonic-gate /* The argument is really a NCONF_HANDLE; cast it here */ 581*0Sstevel@tonic-gate NCONF_HANDLE *nconf_handlep = (NCONF_HANDLE *)vdata; 582*0Sstevel@tonic-gate 583*0Sstevel@tonic-gate trace1(TR_endnetpath, 0); 584*0Sstevel@tonic-gate mutex_lock(&netpp_mutex); 585*0Sstevel@tonic-gate if (netpp == NULL || nconf_handlep == NULL) { 586*0Sstevel@tonic-gate nc_error = NC_NOSET; 587*0Sstevel@tonic-gate mutex_unlock(&netpp_mutex); 588*0Sstevel@tonic-gate trace1(TR_endnetpath, 1); 589*0Sstevel@tonic-gate return (-1); 590*0Sstevel@tonic-gate } 591*0Sstevel@tonic-gate mutex_unlock(&netpp_mutex); 592*0Sstevel@tonic-gate 593*0Sstevel@tonic-gate free(nconf_handlep->nc_head); 594*0Sstevel@tonic-gate free(nconf_handlep); 595*0Sstevel@tonic-gate trace1(TR_endnetpath, 1); 596*0Sstevel@tonic-gate return (0); 597*0Sstevel@tonic-gate } 598*0Sstevel@tonic-gate 599*0Sstevel@tonic-gate /* 600*0Sstevel@tonic-gate * getnetpath() returns the current entry in the list 601*0Sstevel@tonic-gate * from the NETPATH variable. If setnetpath() was not called 602*0Sstevel@tonic-gate * previously to set up the list, return NULL. 603*0Sstevel@tonic-gate */ 604*0Sstevel@tonic-gate 605*0Sstevel@tonic-gate struct netconfig * 606*0Sstevel@tonic-gate getnetpath(vdata) 607*0Sstevel@tonic-gate void *vdata; 608*0Sstevel@tonic-gate { 609*0Sstevel@tonic-gate /* The argument is really a NCONF_HANDLE; cast it here */ 610*0Sstevel@tonic-gate NCONF_HANDLE *nconf_handlep = (NCONF_HANDLE *)vdata; 611*0Sstevel@tonic-gate struct netconfig *retp; /* holds the return value */ 612*0Sstevel@tonic-gate int ipv6_present = -1; 613*0Sstevel@tonic-gate 614*0Sstevel@tonic-gate trace1(TR_getnetpath, 0); 615*0Sstevel@tonic-gate mutex_lock(&netpp_mutex); 616*0Sstevel@tonic-gate if (netpp == NULL) { 617*0Sstevel@tonic-gate nc_error = NC_NOSET; 618*0Sstevel@tonic-gate mutex_unlock(&netpp_mutex); 619*0Sstevel@tonic-gate trace1(TR_getnetpath, 1); 620*0Sstevel@tonic-gate return (NULL); 621*0Sstevel@tonic-gate } 622*0Sstevel@tonic-gate mutex_unlock(&netpp_mutex); 623*0Sstevel@tonic-gate for (;;) { 624*0Sstevel@tonic-gate retp = *(nconf_handlep->nc_curr); 625*0Sstevel@tonic-gate if (retp && (strcmp(retp->nc_netid, "udp6") == 0 || 626*0Sstevel@tonic-gate strcmp(retp->nc_netid, "tcp6") == 0)) { 627*0Sstevel@tonic-gate if (ipv6_present == -1) 628*0Sstevel@tonic-gate ipv6_present = __can_use_af(AF_INET6); 629*0Sstevel@tonic-gate if (!ipv6_present) { 630*0Sstevel@tonic-gate ++(nconf_handlep->nc_curr); 631*0Sstevel@tonic-gate continue; 632*0Sstevel@tonic-gate } 633*0Sstevel@tonic-gate } 634*0Sstevel@tonic-gate break; 635*0Sstevel@tonic-gate } 636*0Sstevel@tonic-gate if (retp) { 637*0Sstevel@tonic-gate ++(nconf_handlep->nc_curr); 638*0Sstevel@tonic-gate nc_error = NC_NOERROR; 639*0Sstevel@tonic-gate } else { 640*0Sstevel@tonic-gate nc_error = NC_NOMOREENTRIES; 641*0Sstevel@tonic-gate } 642*0Sstevel@tonic-gate 643*0Sstevel@tonic-gate trace1(TR_getnetpath, 1); 644*0Sstevel@tonic-gate return (retp); 645*0Sstevel@tonic-gate } 646*0Sstevel@tonic-gate 647*0Sstevel@tonic-gate /* 648*0Sstevel@tonic-gate * blank() returns true if the line is a blank line, 0 otherwise 649*0Sstevel@tonic-gate */ 650*0Sstevel@tonic-gate 651*0Sstevel@tonic-gate static int 652*0Sstevel@tonic-gate blank(cp) 653*0Sstevel@tonic-gate char *cp; 654*0Sstevel@tonic-gate { 655*0Sstevel@tonic-gate trace1(TR_blank, 0); 656*0Sstevel@tonic-gate while (*cp && isspace(*cp)) { 657*0Sstevel@tonic-gate cp++; 658*0Sstevel@tonic-gate } 659*0Sstevel@tonic-gate trace1(TR_blank, 1); 660*0Sstevel@tonic-gate return (*cp == '\0'); 661*0Sstevel@tonic-gate } 662*0Sstevel@tonic-gate 663*0Sstevel@tonic-gate /* 664*0Sstevel@tonic-gate * comment() returns true if the line is a comment, 0 otherwise. 665*0Sstevel@tonic-gate */ 666*0Sstevel@tonic-gate 667*0Sstevel@tonic-gate static int 668*0Sstevel@tonic-gate comment(cp) 669*0Sstevel@tonic-gate char *cp; 670*0Sstevel@tonic-gate { 671*0Sstevel@tonic-gate trace1(TR_comment, 0); 672*0Sstevel@tonic-gate while (*cp && isspace(*cp)) { 673*0Sstevel@tonic-gate cp++; 674*0Sstevel@tonic-gate } 675*0Sstevel@tonic-gate trace1(TR_comment, 1); 676*0Sstevel@tonic-gate return (*cp == '#'); 677*0Sstevel@tonic-gate } 678*0Sstevel@tonic-gate 679*0Sstevel@tonic-gate /* 680*0Sstevel@tonic-gate * getvalue() searches for the given string in the given array, 681*0Sstevel@tonic-gate * and return the integer value associated with the string. 682*0Sstevel@tonic-gate */ 683*0Sstevel@tonic-gate 684*0Sstevel@tonic-gate static unsigned int 685*0Sstevel@tonic-gate getvalue(cp, nc_data) 686*0Sstevel@tonic-gate char *cp; 687*0Sstevel@tonic-gate struct nc_data nc_data[]; 688*0Sstevel@tonic-gate { 689*0Sstevel@tonic-gate int i; /* used to index through the given struct nc_data array */ 690*0Sstevel@tonic-gate 691*0Sstevel@tonic-gate trace1(TR_getvalue, 0); 692*0Sstevel@tonic-gate for (i = 0; nc_data[i].string; i++) { 693*0Sstevel@tonic-gate if (strcmp(nc_data[i].string, cp) == 0) { 694*0Sstevel@tonic-gate break; 695*0Sstevel@tonic-gate } 696*0Sstevel@tonic-gate } 697*0Sstevel@tonic-gate trace1(TR_getvalue, 1); 698*0Sstevel@tonic-gate return (nc_data[i].value); 699*0Sstevel@tonic-gate } 700*0Sstevel@tonic-gate 701*0Sstevel@tonic-gate /* 702*0Sstevel@tonic-gate * getflag() creates a bitmap of the one-character flags in 703*0Sstevel@tonic-gate * the given string. It uses nc_flags array to get the values. 704*0Sstevel@tonic-gate */ 705*0Sstevel@tonic-gate 706*0Sstevel@tonic-gate static unsigned int 707*0Sstevel@tonic-gate getflag(cp) 708*0Sstevel@tonic-gate char *cp; 709*0Sstevel@tonic-gate { 710*0Sstevel@tonic-gate int i; /* indexs through the nc_flag array */ 711*0Sstevel@tonic-gate unsigned int mask = 0; /* holds bitmask of flags */ 712*0Sstevel@tonic-gate 713*0Sstevel@tonic-gate trace1(TR_getflag, 0); 714*0Sstevel@tonic-gate while (*cp) { 715*0Sstevel@tonic-gate for (i = 0; nc_flag[i].string; i++) { 716*0Sstevel@tonic-gate if (*nc_flag[i].string == *cp) { 717*0Sstevel@tonic-gate mask |= nc_flag[i].value; 718*0Sstevel@tonic-gate break; 719*0Sstevel@tonic-gate } 720*0Sstevel@tonic-gate } 721*0Sstevel@tonic-gate cp++; 722*0Sstevel@tonic-gate } 723*0Sstevel@tonic-gate trace1(TR_getflag, 1); 724*0Sstevel@tonic-gate return (mask); 725*0Sstevel@tonic-gate } 726*0Sstevel@tonic-gate 727*0Sstevel@tonic-gate /* 728*0Sstevel@tonic-gate * getlookups() creates and returns an array of string representing 729*0Sstevel@tonic-gate * the directory lookup libraries, given as a comma-seperated list 730*0Sstevel@tonic-gate * in the argument "cp". 731*0Sstevel@tonic-gate */ 732*0Sstevel@tonic-gate 733*0Sstevel@tonic-gate static char ** 734*0Sstevel@tonic-gate getlookups(cp) 735*0Sstevel@tonic-gate char *cp; 736*0Sstevel@tonic-gate { 737*0Sstevel@tonic-gate unsigned int num; /* holds the number of entries in the list */ 738*0Sstevel@tonic-gate char **listpp; /* the beginning of the list of dir routines */ 739*0Sstevel@tonic-gate char **tpp; /* traverses the list, populating it */ 740*0Sstevel@tonic-gate char *start; 741*0Sstevel@tonic-gate 742*0Sstevel@tonic-gate trace1(TR_getlookups, 0); 743*0Sstevel@tonic-gate num = getnlookups(cp); 744*0Sstevel@tonic-gate if (num == 0) { 745*0Sstevel@tonic-gate trace1(TR_getlookups, 1); 746*0Sstevel@tonic-gate return (NULL); 747*0Sstevel@tonic-gate } 748*0Sstevel@tonic-gate if ((listpp = (char **)malloc((num + 1) * sizeof (char *))) == NULL) { 749*0Sstevel@tonic-gate trace1(TR_getlookups, 1); 750*0Sstevel@tonic-gate return (NULL); 751*0Sstevel@tonic-gate } 752*0Sstevel@tonic-gate 753*0Sstevel@tonic-gate tpp = listpp; 754*0Sstevel@tonic-gate while (num--) { 755*0Sstevel@tonic-gate start = cp; 756*0Sstevel@tonic-gate 757*0Sstevel@tonic-gate /* 758*0Sstevel@tonic-gate * Traverse the string looking for the next entry 759*0Sstevel@tonic-gate * of the list (i.e, where the ',' or end of the 760*0Sstevel@tonic-gate * string appears). If a "\" is found, shift the 761*0Sstevel@tonic-gate * token over 1 to the left (taking the next char 762*0Sstevel@tonic-gate * literally). 763*0Sstevel@tonic-gate */ 764*0Sstevel@tonic-gate 765*0Sstevel@tonic-gate while (*cp && *cp != ',') { 766*0Sstevel@tonic-gate if (*cp == '\\' && *(cp + 1)) { 767*0Sstevel@tonic-gate shift1left(cp); 768*0Sstevel@tonic-gate } 769*0Sstevel@tonic-gate cp++; 770*0Sstevel@tonic-gate } 771*0Sstevel@tonic-gate if (*cp) 772*0Sstevel@tonic-gate *cp++ = '\0'; 773*0Sstevel@tonic-gate if ((*tpp++ = strdup(start)) == NULL) { 774*0Sstevel@tonic-gate for (tpp = listpp; *tpp; tpp++) 775*0Sstevel@tonic-gate free(*tpp); 776*0Sstevel@tonic-gate free(listpp); 777*0Sstevel@tonic-gate trace1(TR_getlookups, 1); 778*0Sstevel@tonic-gate return (NULL); 779*0Sstevel@tonic-gate } 780*0Sstevel@tonic-gate } 781*0Sstevel@tonic-gate *tpp = NULL; 782*0Sstevel@tonic-gate trace1(TR_getlookups, 1); 783*0Sstevel@tonic-gate return (listpp); 784*0Sstevel@tonic-gate } 785*0Sstevel@tonic-gate 786*0Sstevel@tonic-gate /* 787*0Sstevel@tonic-gate * getnlookups() returns the number of entries in a comma-separated 788*0Sstevel@tonic-gate * string of tokens. A "-" means no strings are present. 789*0Sstevel@tonic-gate */ 790*0Sstevel@tonic-gate 791*0Sstevel@tonic-gate static unsigned int 792*0Sstevel@tonic-gate getnlookups(cp) 793*0Sstevel@tonic-gate char *cp; 794*0Sstevel@tonic-gate { 795*0Sstevel@tonic-gate unsigned int count; /* the number of tokens in the string */ 796*0Sstevel@tonic-gate 797*0Sstevel@tonic-gate trace1(TR_getnlookups, 0); 798*0Sstevel@tonic-gate if (strcmp(cp, "-") == 0) { 799*0Sstevel@tonic-gate trace1(TR_getnlookups, 1); 800*0Sstevel@tonic-gate return (0); 801*0Sstevel@tonic-gate } 802*0Sstevel@tonic-gate 803*0Sstevel@tonic-gate count = 1; 804*0Sstevel@tonic-gate while (*cp) { 805*0Sstevel@tonic-gate if (*cp == ',') { 806*0Sstevel@tonic-gate count++; 807*0Sstevel@tonic-gate } 808*0Sstevel@tonic-gate 809*0Sstevel@tonic-gate /* 810*0Sstevel@tonic-gate * If a "\" is in the string, take the next character 811*0Sstevel@tonic-gate * literally. Onlly skip the character if "\" is 812*0Sstevel@tonic-gate * not the last character of the token. 813*0Sstevel@tonic-gate */ 814*0Sstevel@tonic-gate if (*cp == '\\' && *(cp + 1)) { 815*0Sstevel@tonic-gate cp++; 816*0Sstevel@tonic-gate } 817*0Sstevel@tonic-gate cp++; 818*0Sstevel@tonic-gate } 819*0Sstevel@tonic-gate trace1(TR_getnlookups, 1); 820*0Sstevel@tonic-gate return (count); 821*0Sstevel@tonic-gate } 822*0Sstevel@tonic-gate 823*0Sstevel@tonic-gate /* 824*0Sstevel@tonic-gate * gettoken() behaves much like strtok(), except that 825*0Sstevel@tonic-gate * it knows about escaped space characters (i.e., space characters 826*0Sstevel@tonic-gate * preceeded by a '\' are taken literally). 827*0Sstevel@tonic-gate */ 828*0Sstevel@tonic-gate 829*0Sstevel@tonic-gate static char * 830*0Sstevel@tonic-gate gettoken(cp, skip) 831*0Sstevel@tonic-gate char *cp; 832*0Sstevel@tonic-gate int skip; 833*0Sstevel@tonic-gate { 834*0Sstevel@tonic-gate static char *savep; /* the place where we left off */ 835*0Sstevel@tonic-gate char *p; /* the beginning of the new token */ 836*0Sstevel@tonic-gate char *retp; /* the token to be returned */ 837*0Sstevel@tonic-gate 838*0Sstevel@tonic-gate trace1(TR_gettoken, 0); 839*0Sstevel@tonic-gate fieldnum++; 840*0Sstevel@tonic-gate 841*0Sstevel@tonic-gate /* Determine if first or subsequent call */ 842*0Sstevel@tonic-gate p = (cp == NULL)? savep: cp; 843*0Sstevel@tonic-gate 844*0Sstevel@tonic-gate /* Return if no tokens remain. */ 845*0Sstevel@tonic-gate if (p == 0) { 846*0Sstevel@tonic-gate trace1(TR_gettoken, 1); 847*0Sstevel@tonic-gate return (NULL); 848*0Sstevel@tonic-gate } 849*0Sstevel@tonic-gate 850*0Sstevel@tonic-gate while (isspace(*p)) 851*0Sstevel@tonic-gate p++; 852*0Sstevel@tonic-gate 853*0Sstevel@tonic-gate if (*p == '\0') { 854*0Sstevel@tonic-gate trace1(TR_gettoken, 1); 855*0Sstevel@tonic-gate return (NULL); 856*0Sstevel@tonic-gate } 857*0Sstevel@tonic-gate 858*0Sstevel@tonic-gate /* 859*0Sstevel@tonic-gate * Save the location of the token and then skip past it 860*0Sstevel@tonic-gate */ 861*0Sstevel@tonic-gate 862*0Sstevel@tonic-gate retp = p; 863*0Sstevel@tonic-gate while (*p) { 864*0Sstevel@tonic-gate if (isspace(*p)) 865*0Sstevel@tonic-gate if (skip == TRUE) { 866*0Sstevel@tonic-gate shift1left(p); 867*0Sstevel@tonic-gate continue; 868*0Sstevel@tonic-gate } else 869*0Sstevel@tonic-gate break; 870*0Sstevel@tonic-gate /* 871*0Sstevel@tonic-gate * Only process the escape of the space seperator; 872*0Sstevel@tonic-gate * since the token may contain other separators, 873*0Sstevel@tonic-gate * let the other routines handle the escape of 874*0Sstevel@tonic-gate * specific characters in the token. 875*0Sstevel@tonic-gate */ 876*0Sstevel@tonic-gate 877*0Sstevel@tonic-gate if (*p == '\\' && *(p + 1) != '\n' && isspace(*(p + 1))) { 878*0Sstevel@tonic-gate shift1left(p); 879*0Sstevel@tonic-gate } 880*0Sstevel@tonic-gate p++; 881*0Sstevel@tonic-gate } 882*0Sstevel@tonic-gate if (*p == '\0') { 883*0Sstevel@tonic-gate savep = 0; /* indicate this is last token */ 884*0Sstevel@tonic-gate } else { 885*0Sstevel@tonic-gate *p = '\0'; 886*0Sstevel@tonic-gate savep = ++p; 887*0Sstevel@tonic-gate } 888*0Sstevel@tonic-gate trace1(TR_gettoken, 1); 889*0Sstevel@tonic-gate return (strdup(retp)); 890*0Sstevel@tonic-gate } 891*0Sstevel@tonic-gate 892*0Sstevel@tonic-gate /* 893*0Sstevel@tonic-gate * shift1left() moves all characters in the string over 1 to 894*0Sstevel@tonic-gate * the left. 895*0Sstevel@tonic-gate */ 896*0Sstevel@tonic-gate 897*0Sstevel@tonic-gate static void 898*0Sstevel@tonic-gate shift1left(p) 899*0Sstevel@tonic-gate char *p; 900*0Sstevel@tonic-gate { 901*0Sstevel@tonic-gate trace1(TR_shift1left, 0); 902*0Sstevel@tonic-gate for (; *p; p++) 903*0Sstevel@tonic-gate *p = *(p + 1); 904*0Sstevel@tonic-gate trace1(TR_shift1left, 1); 905*0Sstevel@tonic-gate } 906*0Sstevel@tonic-gate 907*0Sstevel@tonic-gate char * 908*0Sstevel@tonic-gate nc_sperror() 909*0Sstevel@tonic-gate { 910*0Sstevel@tonic-gate static char buf_main[BUFSIZ]; 911*0Sstevel@tonic-gate static pthread_key_t perror_key; 912*0Sstevel@tonic-gate char *retstr = thr_main()? 913*0Sstevel@tonic-gate buf_main : 914*0Sstevel@tonic-gate thr_get_storage(&perror_key, BUFSIZ, free); 915*0Sstevel@tonic-gate 916*0Sstevel@tonic-gate trace1(TR_nc_sperror, 0); 917*0Sstevel@tonic-gate 918*0Sstevel@tonic-gate if (retstr == NULL) { 919*0Sstevel@tonic-gate syslog(LOG_WARNING, 920*0Sstevel@tonic-gate "nc_sperror: malloc failed when trying to create buffer\n"); 921*0Sstevel@tonic-gate return (NULL); 922*0Sstevel@tonic-gate } 923*0Sstevel@tonic-gate 924*0Sstevel@tonic-gate switch (nc_error) { 925*0Sstevel@tonic-gate case NC_NOERROR: 926*0Sstevel@tonic-gate (void) strlcpy(retstr, dgettext(__nsl_dom, "no error"), BUFSIZ); 927*0Sstevel@tonic-gate break; 928*0Sstevel@tonic-gate case NC_NOMEM: 929*0Sstevel@tonic-gate (void) strlcpy(retstr, dgettext(__nsl_dom, "out of memory"), 930*0Sstevel@tonic-gate BUFSIZ); 931*0Sstevel@tonic-gate break; 932*0Sstevel@tonic-gate case NC_NOSET: 933*0Sstevel@tonic-gate (void) strlcpy(retstr, dgettext(__nsl_dom, 934*0Sstevel@tonic-gate "routine called before calling \ 935*0Sstevel@tonic-gate setnetpath() or setnetconfig()"), BUFSIZ); 936*0Sstevel@tonic-gate break; 937*0Sstevel@tonic-gate case NC_OPENFAIL: 938*0Sstevel@tonic-gate (void) strlcpy(retstr, 939*0Sstevel@tonic-gate dgettext(__nsl_dom, "cannot open /etc/netconfig"), 940*0Sstevel@tonic-gate BUFSIZ); 941*0Sstevel@tonic-gate break; 942*0Sstevel@tonic-gate case NC_BADLINE: 943*0Sstevel@tonic-gate (void) snprintf(retstr, BUFSIZ, dgettext(__nsl_dom, 944*0Sstevel@tonic-gate "error in /etc/netconfig: field %d of line %d\n"), 945*0Sstevel@tonic-gate fieldnum, linenum); 946*0Sstevel@tonic-gate break; 947*0Sstevel@tonic-gate case NC_NOTFOUND: 948*0Sstevel@tonic-gate (void) snprintf(retstr, BUFSIZ, 949*0Sstevel@tonic-gate dgettext(__nsl_dom, 950*0Sstevel@tonic-gate "netid not found in /etc/netconfig")); 951*0Sstevel@tonic-gate break; 952*0Sstevel@tonic-gate case NC_NOMOREENTRIES: 953*0Sstevel@tonic-gate (void) snprintf(retstr, BUFSIZ, 954*0Sstevel@tonic-gate dgettext(__nsl_dom, 955*0Sstevel@tonic-gate "no more entries in /etc/netconfig")); 956*0Sstevel@tonic-gate break; 957*0Sstevel@tonic-gate default: 958*0Sstevel@tonic-gate (void) strlcpy(retstr, dgettext(__nsl_dom, "unknown error"), 959*0Sstevel@tonic-gate BUFSIZ); 960*0Sstevel@tonic-gate break; 961*0Sstevel@tonic-gate } 962*0Sstevel@tonic-gate trace1(TR_nc_sperror, 1); 963*0Sstevel@tonic-gate return (retstr); 964*0Sstevel@tonic-gate } 965*0Sstevel@tonic-gate 966*0Sstevel@tonic-gate void 967*0Sstevel@tonic-gate nc_perror(const char *string) 968*0Sstevel@tonic-gate { 969*0Sstevel@tonic-gate trace1(TR_nc_perror, 0); 970*0Sstevel@tonic-gate if (string) 971*0Sstevel@tonic-gate fprintf(stderr, "%s: %s\n", string, nc_sperror()); 972*0Sstevel@tonic-gate else 973*0Sstevel@tonic-gate fprintf(stderr, "%s\n", nc_sperror()); 974*0Sstevel@tonic-gate trace1(TR_nc_perror, 1); 975*0Sstevel@tonic-gate } 976*0Sstevel@tonic-gate 977*0Sstevel@tonic-gate static void 978*0Sstevel@tonic-gate netlist_free(netppp) 979*0Sstevel@tonic-gate struct netconfig ***netppp; 980*0Sstevel@tonic-gate { 981*0Sstevel@tonic-gate struct netconfig **tpp; 982*0Sstevel@tonic-gate 983*0Sstevel@tonic-gate trace1(TR_netlist_free, 0); 984*0Sstevel@tonic-gate for (tpp = *netppp; *tpp; tpp++) { 985*0Sstevel@tonic-gate netconfig_free(*tpp); 986*0Sstevel@tonic-gate } 987*0Sstevel@tonic-gate free(*netppp); 988*0Sstevel@tonic-gate *netppp = NULL; 989*0Sstevel@tonic-gate trace1(TR_netlist_free, 1); 990*0Sstevel@tonic-gate } 991*0Sstevel@tonic-gate 992*0Sstevel@tonic-gate static void 993*0Sstevel@tonic-gate netconfig_free(netconfigp) 994*0Sstevel@tonic-gate struct netconfig *netconfigp; 995*0Sstevel@tonic-gate { 996*0Sstevel@tonic-gate int i; 997*0Sstevel@tonic-gate 998*0Sstevel@tonic-gate trace1(TR_netconfig_free, 0); 999*0Sstevel@tonic-gate if (netconfigp == NULL) { 1000*0Sstevel@tonic-gate trace1(TR_netconfig_free, 1); 1001*0Sstevel@tonic-gate return; 1002*0Sstevel@tonic-gate } 1003*0Sstevel@tonic-gate free_entry(netconfigp->nc_netid); 1004*0Sstevel@tonic-gate free_entry(netconfigp->nc_protofmly); 1005*0Sstevel@tonic-gate free_entry(netconfigp->nc_proto); 1006*0Sstevel@tonic-gate free_entry(netconfigp->nc_device); 1007*0Sstevel@tonic-gate if (netconfigp->nc_lookups) 1008*0Sstevel@tonic-gate for (i = 0; i < netconfigp->nc_nlookups; i++) 1009*0Sstevel@tonic-gate free_entry(netconfigp->nc_lookups[i]); 1010*0Sstevel@tonic-gate free_entry(netconfigp->nc_lookups); 1011*0Sstevel@tonic-gate free(netconfigp); 1012*0Sstevel@tonic-gate trace1(TR_netconfig_free, 1); 1013*0Sstevel@tonic-gate } 1014*0Sstevel@tonic-gate 1015*0Sstevel@tonic-gate static struct netconfig * 1016*0Sstevel@tonic-gate netconfig_dup(netconfigp) 1017*0Sstevel@tonic-gate struct netconfig *netconfigp; 1018*0Sstevel@tonic-gate { 1019*0Sstevel@tonic-gate struct netconfig *nconf; 1020*0Sstevel@tonic-gate int i; 1021*0Sstevel@tonic-gate 1022*0Sstevel@tonic-gate trace1(TR_netconfig_dup, 0); 1023*0Sstevel@tonic-gate nconf = (struct netconfig *)calloc(1, sizeof (struct netconfig)); 1024*0Sstevel@tonic-gate if (nconf == NULL) { 1025*0Sstevel@tonic-gate trace1(TR_netconfig_dup, 1); 1026*0Sstevel@tonic-gate nc_error = NC_NOMEM; 1027*0Sstevel@tonic-gate return (NULL); 1028*0Sstevel@tonic-gate } 1029*0Sstevel@tonic-gate nconf->nc_netid = strdup(netconfigp->nc_netid); 1030*0Sstevel@tonic-gate nconf->nc_protofmly = strdup(netconfigp->nc_protofmly); 1031*0Sstevel@tonic-gate nconf->nc_proto = strdup(netconfigp->nc_proto); 1032*0Sstevel@tonic-gate nconf->nc_device = strdup(netconfigp->nc_device); 1033*0Sstevel@tonic-gate nconf->nc_lookups = (char **)malloc((netconfigp->nc_nlookups + 1) 1034*0Sstevel@tonic-gate * sizeof (char *)); 1035*0Sstevel@tonic-gate if (!(nconf->nc_lookups && nconf->nc_netid && 1036*0Sstevel@tonic-gate nconf->nc_protofmly && nconf->nc_proto && 1037*0Sstevel@tonic-gate nconf->nc_device)) { 1038*0Sstevel@tonic-gate nc_error = NC_NOMEM; 1039*0Sstevel@tonic-gate netconfig_free(nconf); 1040*0Sstevel@tonic-gate trace1(TR_netconfig_dup, 1); 1041*0Sstevel@tonic-gate return (NULL); 1042*0Sstevel@tonic-gate } 1043*0Sstevel@tonic-gate 1044*0Sstevel@tonic-gate for (i = 0; i < netconfigp->nc_nlookups; i++) { 1045*0Sstevel@tonic-gate nconf->nc_lookups[i] = strdup(netconfigp->nc_lookups[i]); 1046*0Sstevel@tonic-gate if (nconf->nc_lookups[i] == NULL) { 1047*0Sstevel@tonic-gate nconf->nc_nlookups = i; 1048*0Sstevel@tonic-gate netconfig_free(nconf); 1049*0Sstevel@tonic-gate nc_error = NC_NOMEM; 1050*0Sstevel@tonic-gate trace1(TR_netconfig_dup, 1); 1051*0Sstevel@tonic-gate return (NULL); 1052*0Sstevel@tonic-gate } 1053*0Sstevel@tonic-gate } 1054*0Sstevel@tonic-gate nconf->nc_lookups[i] = NULL; 1055*0Sstevel@tonic-gate nconf->nc_nlookups = netconfigp->nc_nlookups; 1056*0Sstevel@tonic-gate nconf->nc_flag = netconfigp->nc_flag; 1057*0Sstevel@tonic-gate nconf->nc_semantics = netconfigp->nc_semantics; 1058*0Sstevel@tonic-gate trace1(TR_netconfig_dup, 1); 1059*0Sstevel@tonic-gate return (nconf); 1060*0Sstevel@tonic-gate } 1061*0Sstevel@tonic-gate 1062*0Sstevel@tonic-gate static void 1063*0Sstevel@tonic-gate free_entry(foo) 1064*0Sstevel@tonic-gate void *foo; 1065*0Sstevel@tonic-gate { 1066*0Sstevel@tonic-gate trace1(TR_free_entry, 0); 1067*0Sstevel@tonic-gate if (foo) 1068*0Sstevel@tonic-gate free(foo); 1069*0Sstevel@tonic-gate trace1(TR_free_entry, 1); 1070*0Sstevel@tonic-gate } 1071