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 5004388ebScasper * Common Development and Distribution License (the "License"). 6004388ebScasper * 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 */ 2161961e0fSrobinson 227c478bd9Sstevel@tonic-gate /* 23*cb620785Sraf * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD 327c478bd9Sstevel@tonic-gate * under license from the Regents of the University of California. 337c478bd9Sstevel@tonic-gate */ 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate #include "mt.h" 387c478bd9Sstevel@tonic-gate #include "../rpc/rpc_mt.h" /* for MT declarations only */ 397c478bd9Sstevel@tonic-gate #include <rpc/types.h> 407c478bd9Sstevel@tonic-gate #include <stdio.h> 4161961e0fSrobinson #include <stdlib.h> 427c478bd9Sstevel@tonic-gate #include <string.h> 437c478bd9Sstevel@tonic-gate #include <ctype.h> 447c478bd9Sstevel@tonic-gate #include <netconfig.h> 457c478bd9Sstevel@tonic-gate #include <malloc.h> 4661961e0fSrobinson #include <libintl.h> 477c478bd9Sstevel@tonic-gate #include <syslog.h> 487c478bd9Sstevel@tonic-gate #include "netcspace.h" 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate #define FAILURE (unsigned)(-1) 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate /* 537c478bd9Sstevel@tonic-gate * Local routines used by the library procedures 547c478bd9Sstevel@tonic-gate */ 557c478bd9Sstevel@tonic-gate 5661961e0fSrobinson static int blank(char *); 5761961e0fSrobinson static int comment(char *); 58004388ebScasper static struct netconfig *fgetnetconfig(FILE *, char *); 5961961e0fSrobinson static void netconfig_free(struct netconfig *); 6061961e0fSrobinson static unsigned int getflag(char *); 6161961e0fSrobinson static char **getlookups(char *); 6261961e0fSrobinson static struct netconfig **getnetlist(void); 6361961e0fSrobinson static unsigned int getnlookups(char *); 6461961e0fSrobinson static char *gettoken(char *, int); 6561961e0fSrobinson static unsigned int getvalue(char *, struct nc_data nc_data[]); 6661961e0fSrobinson static void shift1left(char *); 6761961e0fSrobinson static void netlist_free(struct netconfig ***); 6861961e0fSrobinson static void free_entry(void *); 6961961e0fSrobinson static struct netconfig *netconfig_dup(struct netconfig *); 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate extern const char __nsl_dom[]; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate /* 747c478bd9Sstevel@tonic-gate * Static global variables used by the library procedures: 757c478bd9Sstevel@tonic-gate * 767c478bd9Sstevel@tonic-gate * netpp - points to the beginning of the list of netconfig 777c478bd9Sstevel@tonic-gate * entries used by setnetconfig() and setnetpath(). 787c478bd9Sstevel@tonic-gate * Once netpp is initialized, that memory is *never* 797c478bd9Sstevel@tonic-gate * released. This was necessary to improve performance. 807c478bd9Sstevel@tonic-gate * 817c478bd9Sstevel@tonic-gate * linenum - the current line number of the /etc/netconfig 827c478bd9Sstevel@tonic-gate * file (used for debugging and for nc_perror()). 837c478bd9Sstevel@tonic-gate * 847c478bd9Sstevel@tonic-gate * fieldnum - the current field number of the current line 857c478bd9Sstevel@tonic-gate * of /etc/netconfig (used for debugging and for 867c478bd9Sstevel@tonic-gate * nc_perror()). 877c478bd9Sstevel@tonic-gate * 887c478bd9Sstevel@tonic-gate * nc_error - the error condition encountered. 897c478bd9Sstevel@tonic-gate */ 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate static struct netconfig **netpp = NULL; 927c478bd9Sstevel@tonic-gate mutex_t netpp_mutex = DEFAULTMUTEX; 937c478bd9Sstevel@tonic-gate /* 947c478bd9Sstevel@tonic-gate * The following two variables are used by the /etc/netconfig parsing 957c478bd9Sstevel@tonic-gate * routines, which will always be executed once, and within the netpp_mutex. 967c478bd9Sstevel@tonic-gate * They are global to allow the nc_sperror routine to provide better 977c478bd9Sstevel@tonic-gate * information to the user about /etc/netconfig file problems. 987c478bd9Sstevel@tonic-gate */ 997c478bd9Sstevel@tonic-gate static int linenum = 0; /* "owned" by getnetlist() */ 1007c478bd9Sstevel@tonic-gate static int fieldnum = 0; /* "owned" by fgetnetconfig() */ 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate static int * 10461961e0fSrobinson __nc_error(void) 1057c478bd9Sstevel@tonic-gate { 106*cb620785Sraf static pthread_key_t nc_error_key = PTHREAD_ONCE_KEY_NP; 1077c478bd9Sstevel@tonic-gate static int nc_error = NC_NOERROR; 1087c478bd9Sstevel@tonic-gate int *ret; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate if (thr_main()) 1117c478bd9Sstevel@tonic-gate return (&nc_error); 1127c478bd9Sstevel@tonic-gate ret = thr_get_storage(&nc_error_key, sizeof (int), free); 1137c478bd9Sstevel@tonic-gate /* if thr_get_storage fails we return the address of nc_error */ 1147c478bd9Sstevel@tonic-gate return (ret ? ret : &nc_error); 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate #define nc_error (*(__nc_error())) 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate /* 1197c478bd9Sstevel@tonic-gate * setnetconfig() has the effect of "initializing" the 1207c478bd9Sstevel@tonic-gate * network configuration database. It reads in the 1217c478bd9Sstevel@tonic-gate * netcf entries (if not already read in). 1227c478bd9Sstevel@tonic-gate */ 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate void * 12561961e0fSrobinson setnetconfig(void) 1267c478bd9Sstevel@tonic-gate { 1277c478bd9Sstevel@tonic-gate NCONF_HANDLE *retp; 1287c478bd9Sstevel@tonic-gate 12961961e0fSrobinson (void) mutex_lock(&netpp_mutex); 1307c478bd9Sstevel@tonic-gate if ((netpp == NULL) && ((netpp = getnetlist()) == NULL)) { 13161961e0fSrobinson (void) mutex_unlock(&netpp_mutex); 1327c478bd9Sstevel@tonic-gate return (NULL); 1337c478bd9Sstevel@tonic-gate } 13461961e0fSrobinson (void) mutex_unlock(&netpp_mutex); 13561961e0fSrobinson if ((retp = malloc(sizeof (NCONF_HANDLE))) == NULL) { 1367c478bd9Sstevel@tonic-gate nc_error = NC_NOMEM; 1377c478bd9Sstevel@tonic-gate return (NULL); 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate nc_error = NC_NOERROR; 1407c478bd9Sstevel@tonic-gate retp->nc_head = retp->nc_curr = netpp; 1417c478bd9Sstevel@tonic-gate return ((void *)retp); 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate /* 1457c478bd9Sstevel@tonic-gate * endnetconfig() frees up all data allocated by setnetconfig() 1467c478bd9Sstevel@tonic-gate */ 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate int 14961961e0fSrobinson endnetconfig(void *vdata) 1507c478bd9Sstevel@tonic-gate { 1517c478bd9Sstevel@tonic-gate NCONF_HANDLE *nconf_handlep = (NCONF_HANDLE *)vdata; 1527c478bd9Sstevel@tonic-gate 15361961e0fSrobinson (void) mutex_lock(&netpp_mutex); 1547c478bd9Sstevel@tonic-gate if (netpp == NULL || nconf_handlep == NULL) { 1557c478bd9Sstevel@tonic-gate nc_error = NC_NOSET; 15661961e0fSrobinson (void) mutex_unlock(&netpp_mutex); 1577c478bd9Sstevel@tonic-gate return (-1); 1587c478bd9Sstevel@tonic-gate } 15961961e0fSrobinson (void) mutex_unlock(&netpp_mutex); 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate nc_error = NC_NOERROR; 16261961e0fSrobinson free(nconf_handlep); 1637c478bd9Sstevel@tonic-gate return (0); 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate /* 1677c478bd9Sstevel@tonic-gate * getnetconfig() returns the current entry in the list 1687c478bd9Sstevel@tonic-gate * of netconfig structures. It uses the nconf_handlep argument 1697c478bd9Sstevel@tonic-gate * to determine the current entry. If setnetconfig() was not 1707c478bd9Sstevel@tonic-gate * called previously to set up the list, return failure. 1717c478bd9Sstevel@tonic-gate * It also check if ipv6 interface is present(ipv6_present) and 1727c478bd9Sstevel@tonic-gate * skips udp6 & tcp6 entries if ipv6 is not supported. 1737c478bd9Sstevel@tonic-gate */ 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate struct netconfig * 17661961e0fSrobinson getnetconfig(void *vdata) 1777c478bd9Sstevel@tonic-gate { 1787c478bd9Sstevel@tonic-gate NCONF_HANDLE *nconf_handlep = (NCONF_HANDLE *)vdata; 1797c478bd9Sstevel@tonic-gate struct netconfig *retp; /* holds the return value */ 1807c478bd9Sstevel@tonic-gate int ipv6_present = -1; 1817c478bd9Sstevel@tonic-gate 18261961e0fSrobinson (void) mutex_lock(&netpp_mutex); 1837c478bd9Sstevel@tonic-gate if ((netpp == NULL) || (nconf_handlep == NULL)) { 1847c478bd9Sstevel@tonic-gate nc_error = NC_NOSET; 18561961e0fSrobinson (void) mutex_unlock(&netpp_mutex); 1867c478bd9Sstevel@tonic-gate return (NULL); 1877c478bd9Sstevel@tonic-gate } 18861961e0fSrobinson (void) mutex_unlock(&netpp_mutex); 1897c478bd9Sstevel@tonic-gate for (;;) { 1907c478bd9Sstevel@tonic-gate retp = *(nconf_handlep->nc_curr); 1917c478bd9Sstevel@tonic-gate if (retp && (strcmp(retp->nc_netid, "udp6") == 0 || 1927c478bd9Sstevel@tonic-gate strcmp(retp->nc_netid, "tcp6") == 0)) { 1937c478bd9Sstevel@tonic-gate if (ipv6_present == -1) 1947c478bd9Sstevel@tonic-gate ipv6_present = __can_use_af(AF_INET6); 1957c478bd9Sstevel@tonic-gate if (!ipv6_present) { 1967c478bd9Sstevel@tonic-gate ++(nconf_handlep->nc_curr); 1977c478bd9Sstevel@tonic-gate continue; 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate break; 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate if (retp != NULL) { 2037c478bd9Sstevel@tonic-gate ++(nconf_handlep->nc_curr); 2047c478bd9Sstevel@tonic-gate nc_error = NC_NOERROR; 2057c478bd9Sstevel@tonic-gate } else { 2067c478bd9Sstevel@tonic-gate nc_error = NC_NOMOREENTRIES; 2077c478bd9Sstevel@tonic-gate } 2087c478bd9Sstevel@tonic-gate return (retp); 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate /* 2127c478bd9Sstevel@tonic-gate * getnetconfig() searches the netconfig database for a 2137c478bd9Sstevel@tonic-gate * given network id. Returns a pointer to the netconfig 2147c478bd9Sstevel@tonic-gate * structure or a NULL if not found. 2157c478bd9Sstevel@tonic-gate * It also check if ipv6 interface is present(ipv6_present) and 2167c478bd9Sstevel@tonic-gate * skips udp6 & tcp6 entries if ipv6 is not supported. 2177c478bd9Sstevel@tonic-gate */ 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate struct netconfig * 22061961e0fSrobinson getnetconfigent(const char *netid) 2217c478bd9Sstevel@tonic-gate { 2227c478bd9Sstevel@tonic-gate struct netconfig **tpp; 2237c478bd9Sstevel@tonic-gate int ipv6_present; 2247c478bd9Sstevel@tonic-gate 22561961e0fSrobinson (void) mutex_lock(&netpp_mutex); 2267c478bd9Sstevel@tonic-gate if ((netpp == NULL) && ((netpp = getnetlist()) == NULL)) { 22761961e0fSrobinson (void) mutex_unlock(&netpp_mutex); 2287c478bd9Sstevel@tonic-gate return (NULL); 2297c478bd9Sstevel@tonic-gate } 23061961e0fSrobinson (void) mutex_unlock(&netpp_mutex); 2317c478bd9Sstevel@tonic-gate for (tpp = netpp; *tpp; tpp++) { 2327c478bd9Sstevel@tonic-gate if (strcmp((*tpp)->nc_netid, netid) == 0) { 2337c478bd9Sstevel@tonic-gate if (*tpp && (strcmp((*tpp)->nc_netid, "udp6") == 0 || 2347c478bd9Sstevel@tonic-gate strcmp((*tpp)->nc_netid, "tcp6") == 0)) { 2357c478bd9Sstevel@tonic-gate ipv6_present = __can_use_af(AF_INET6); 2367c478bd9Sstevel@tonic-gate if (!ipv6_present) { 2377c478bd9Sstevel@tonic-gate nc_error = NC_NOTFOUND; 2387c478bd9Sstevel@tonic-gate return (NULL); 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate return (netconfig_dup(*tpp)); 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate nc_error = NC_NOTFOUND; 2457c478bd9Sstevel@tonic-gate return (NULL); 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate /* 2497c478bd9Sstevel@tonic-gate * freenetconfigent frees the data allocated by getnetconfigent() 2507c478bd9Sstevel@tonic-gate */ 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate void 25361961e0fSrobinson freenetconfigent(struct netconfig *netp) 2547c478bd9Sstevel@tonic-gate { 2557c478bd9Sstevel@tonic-gate netconfig_free(netp); 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate /* 2597c478bd9Sstevel@tonic-gate * getnetlist() reads the netconfig file and creates a 2607c478bd9Sstevel@tonic-gate * NULL-terminated list of entries. 2617c478bd9Sstevel@tonic-gate * Returns the pointer to the head of the list or a NULL 2627c478bd9Sstevel@tonic-gate * on failure. 2637c478bd9Sstevel@tonic-gate */ 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate static struct netconfig ** 26661961e0fSrobinson getnetlist(void) 2677c478bd9Sstevel@tonic-gate { 2687c478bd9Sstevel@tonic-gate char line[BUFSIZ]; /* holds each line of NETCONFIG */ 269004388ebScasper FILE *fp; /* file stream for NETCONFIG */ 2707c478bd9Sstevel@tonic-gate struct netconfig **listpp; /* the beginning of the netconfig list */ 2717c478bd9Sstevel@tonic-gate struct netconfig **tpp; /* used to traverse the netconfig list */ 2727c478bd9Sstevel@tonic-gate int count; /* the number of entries in file */ 2737c478bd9Sstevel@tonic-gate 274004388ebScasper if ((fp = fopen(NETCONFIG, "rF")) == NULL) { 2757c478bd9Sstevel@tonic-gate nc_error = NC_OPENFAIL; 2767c478bd9Sstevel@tonic-gate return (NULL); 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate count = 0; 280004388ebScasper while (fgets(line, BUFSIZ, fp)) { 2817c478bd9Sstevel@tonic-gate if (!(blank(line) || comment(line))) { 2827c478bd9Sstevel@tonic-gate ++count; 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate } 285004388ebScasper rewind(fp); 2867c478bd9Sstevel@tonic-gate 2877c478bd9Sstevel@tonic-gate if (count == 0) { 2887c478bd9Sstevel@tonic-gate nc_error = NC_NOTFOUND; 289004388ebScasper (void) fclose(fp); 2907c478bd9Sstevel@tonic-gate return (NULL); 2917c478bd9Sstevel@tonic-gate } 29261961e0fSrobinson if ((listpp = malloc((count + 1) * 2937c478bd9Sstevel@tonic-gate sizeof (struct netconfig *))) == NULL) { 2947c478bd9Sstevel@tonic-gate nc_error = NC_NOMEM; 295004388ebScasper (void) fclose(fp); 2967c478bd9Sstevel@tonic-gate return (NULL); 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate /* 3007c478bd9Sstevel@tonic-gate * The following loop fills in the list (loops until 3017c478bd9Sstevel@tonic-gate * fgetnetconfig() returns a NULL) and counts the 3027c478bd9Sstevel@tonic-gate * number of entries placed in the list. Note that 3037c478bd9Sstevel@tonic-gate * when the loop is completed, the last entry in the 3047c478bd9Sstevel@tonic-gate * list will contain a NULL (signifying the end of 3057c478bd9Sstevel@tonic-gate * the list). 3067c478bd9Sstevel@tonic-gate */ 3077c478bd9Sstevel@tonic-gate linenum = 0; 3087c478bd9Sstevel@tonic-gate for (tpp = listpp; *tpp = fgetnetconfig(fp, NULL); tpp++) 3097c478bd9Sstevel@tonic-gate ; 310004388ebScasper (void) fclose(fp); 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate if (nc_error != NC_NOMOREENTRIES) /* Something is screwed up */ 3137c478bd9Sstevel@tonic-gate netlist_free(&listpp); 3147c478bd9Sstevel@tonic-gate return (listpp); 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate /* 3187c478bd9Sstevel@tonic-gate * fgetnetconfig() parses a line of the netconfig file into 3197c478bd9Sstevel@tonic-gate * a netconfig structure. It returns a pointer to the 3207c478bd9Sstevel@tonic-gate * structure of success and a NULL on failure or EOF. 3217c478bd9Sstevel@tonic-gate */ 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate static struct netconfig * 324004388ebScasper fgetnetconfig(FILE *fp, char *netid) 3257c478bd9Sstevel@tonic-gate { 3267c478bd9Sstevel@tonic-gate char linep[BUFSIZ]; /* pointer to a line in the file */ 3277c478bd9Sstevel@tonic-gate struct netconfig *netconfigp; /* holds the new netconfig structure */ 3287c478bd9Sstevel@tonic-gate char *tok1, *tok2, *tok3; /* holds a token from the line */ 3297c478bd9Sstevel@tonic-gate char *retvalp; /* the return value of fgets() */ 3307c478bd9Sstevel@tonic-gate char *entnetid; /* netid for the current entry */ 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate /* skip past blank lines and comments. */ 333004388ebScasper while (retvalp = fgets(linep, BUFSIZ, fp)) { 3347c478bd9Sstevel@tonic-gate linenum++; 3357c478bd9Sstevel@tonic-gate if (!(blank(linep) || comment(linep))) { 3367c478bd9Sstevel@tonic-gate break; 3377c478bd9Sstevel@tonic-gate } 3387c478bd9Sstevel@tonic-gate retvalp = NULL; 3397c478bd9Sstevel@tonic-gate } 3407c478bd9Sstevel@tonic-gate if (retvalp == NULL) { 3417c478bd9Sstevel@tonic-gate nc_error = NC_NOMOREENTRIES; 3427c478bd9Sstevel@tonic-gate return (NULL); 3437c478bd9Sstevel@tonic-gate } 3447c478bd9Sstevel@tonic-gate fieldnum = 0; 3457c478bd9Sstevel@tonic-gate if ((entnetid = gettoken(linep, FALSE)) == NULL) { 3467c478bd9Sstevel@tonic-gate nc_error = NC_BADLINE; 3477c478bd9Sstevel@tonic-gate return (NULL); 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate if (netid && (strcmp(netid, entnetid) != 0)) { 3507c478bd9Sstevel@tonic-gate free(entnetid); 3517c478bd9Sstevel@tonic-gate nc_error = NC_NOTFOUND; 3527c478bd9Sstevel@tonic-gate return (NULL); 3537c478bd9Sstevel@tonic-gate } 35461961e0fSrobinson if ((netconfigp = calloc(1, sizeof (struct netconfig))) == NULL) { 3557c478bd9Sstevel@tonic-gate free(entnetid); 3567c478bd9Sstevel@tonic-gate nc_error = NC_NOMEM; 3577c478bd9Sstevel@tonic-gate return (NULL); 3587c478bd9Sstevel@tonic-gate } 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate tok1 = tok2 = tok3 = NULL; 3617c478bd9Sstevel@tonic-gate netconfigp->nc_netid = entnetid; 3627c478bd9Sstevel@tonic-gate if (((tok1 = gettoken(NULL, FALSE)) == NULL) || 3637c478bd9Sstevel@tonic-gate ((netconfigp->nc_semantics = 3647c478bd9Sstevel@tonic-gate getvalue(tok1, nc_semantics)) == FAILURE) || 3657c478bd9Sstevel@tonic-gate ((tok2 = gettoken(NULL, FALSE)) == NULL) || 3667c478bd9Sstevel@tonic-gate ((netconfigp->nc_flag = getflag(tok2)) == FAILURE) || 3677c478bd9Sstevel@tonic-gate ((netconfigp->nc_protofmly = gettoken(NULL, FALSE)) == NULL) || 3687c478bd9Sstevel@tonic-gate ((netconfigp->nc_proto = gettoken(NULL, FALSE)) == NULL) || 3697c478bd9Sstevel@tonic-gate ((netconfigp->nc_device = gettoken(NULL, FALSE)) == NULL) || 3707c478bd9Sstevel@tonic-gate ((tok3 = gettoken(NULL, TRUE)) == NULL) || 3717c478bd9Sstevel@tonic-gate (((netconfigp->nc_nlookups = getnlookups(tok3)) != 0) && 3727c478bd9Sstevel@tonic-gate ((netconfigp->nc_lookups = getlookups(tok3)) == NULL))) { 3737c478bd9Sstevel@tonic-gate netconfig_free(netconfigp); 3747c478bd9Sstevel@tonic-gate nc_error = NC_BADLINE; 3757c478bd9Sstevel@tonic-gate netconfigp = NULL; 3767c478bd9Sstevel@tonic-gate } 3777c478bd9Sstevel@tonic-gate free(tok1); 3787c478bd9Sstevel@tonic-gate free(tok2); 3797c478bd9Sstevel@tonic-gate free(tok3); 3807c478bd9Sstevel@tonic-gate return (netconfigp); 3817c478bd9Sstevel@tonic-gate } 3827c478bd9Sstevel@tonic-gate 3837c478bd9Sstevel@tonic-gate /* 3847c478bd9Sstevel@tonic-gate * setnetpath() has the effect of "initializing" the 3857c478bd9Sstevel@tonic-gate * NETPATH variable. It reads in the netcf entries (if not 3867c478bd9Sstevel@tonic-gate * already read in), creates a list corresponding to the entries 3877c478bd9Sstevel@tonic-gate * in the NETPATH variable (or the "visible" entries og netconfig 3887c478bd9Sstevel@tonic-gate * if NETPATH is not set). 3897c478bd9Sstevel@tonic-gate */ 3907c478bd9Sstevel@tonic-gate 3917c478bd9Sstevel@tonic-gate void * 39261961e0fSrobinson setnetpath(void) 3937c478bd9Sstevel@tonic-gate { 3947c478bd9Sstevel@tonic-gate int count; /* the number of entries in NETPATH */ 3957c478bd9Sstevel@tonic-gate char valid_netpath[BUFSIZ]; /* holds the valid entries if NETPATH */ 3967c478bd9Sstevel@tonic-gate char templine[BUFSIZ]; /* has value of NETPATH when scanning */ 3977c478bd9Sstevel@tonic-gate struct netconfig **curr_pp; /* scans the list from NETPATH */ 3987c478bd9Sstevel@tonic-gate struct netconfig **tpp; /* scans the list from netconfig file */ 3997c478bd9Sstevel@tonic-gate struct netconfig **rnetpp; /* the list of entries from NETPATH */ 4007c478bd9Sstevel@tonic-gate char *netpath; /* value of NETPATH from environment */ 4017c478bd9Sstevel@tonic-gate char *netid; /* holds a component of NETPATH */ 4027c478bd9Sstevel@tonic-gate char *tp; /* used to scan NETPATH string */ 4037c478bd9Sstevel@tonic-gate NCONF_HANDLE *retp; /* the return value */ 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate /* 4067c478bd9Sstevel@tonic-gate * Read in the netconfig database if not already read in 4077c478bd9Sstevel@tonic-gate */ 40861961e0fSrobinson (void) mutex_lock(&netpp_mutex); 4097c478bd9Sstevel@tonic-gate if ((netpp == NULL) && ((netpp = getnetlist()) == NULL)) { 41061961e0fSrobinson (void) mutex_unlock(&netpp_mutex); 4117c478bd9Sstevel@tonic-gate return (NULL); 4127c478bd9Sstevel@tonic-gate } 41361961e0fSrobinson (void) mutex_unlock(&netpp_mutex); 4147c478bd9Sstevel@tonic-gate 41561961e0fSrobinson if ((retp = malloc(sizeof (NCONF_HANDLE))) == NULL) { 4167c478bd9Sstevel@tonic-gate nc_error = NC_NOMEM; 4177c478bd9Sstevel@tonic-gate return (NULL); 4187c478bd9Sstevel@tonic-gate } 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate /* 4217c478bd9Sstevel@tonic-gate * Get the valid entries of the NETPATH variable (and 4227c478bd9Sstevel@tonic-gate * count the number of entries while doing it). 4237c478bd9Sstevel@tonic-gate * 4247c478bd9Sstevel@tonic-gate * This is done every time the procedure is called just 4257c478bd9Sstevel@tonic-gate * in case NETPATH has changed from call to call. 4267c478bd9Sstevel@tonic-gate * 4277c478bd9Sstevel@tonic-gate * If NETPATH is too long, we ignore it altogether as 4287c478bd9Sstevel@tonic-gate * it can only be a buffer overflow attack. 4297c478bd9Sstevel@tonic-gate * Since we add one colon for each entry, but colons only 4307c478bd9Sstevel@tonic-gate * need to exist between entries, we have to subtract one. 4317c478bd9Sstevel@tonic-gate */ 4327c478bd9Sstevel@tonic-gate count = 0; 4337c478bd9Sstevel@tonic-gate valid_netpath[0] = '\0'; 4347c478bd9Sstevel@tonic-gate if ((netpath = getenv(NETPATH)) == NULL || 4357c478bd9Sstevel@tonic-gate strlen(netpath) >= sizeof (templine) - 1) { 4367c478bd9Sstevel@tonic-gate /* 4377c478bd9Sstevel@tonic-gate * If NETPATH variable is not set or invalid, 4387c478bd9Sstevel@tonic-gate * the valid NETPATH consist of all "visible" 4397c478bd9Sstevel@tonic-gate * netids from the netconfig database. 4407c478bd9Sstevel@tonic-gate */ 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate for (tpp = netpp; *tpp; tpp++) { 4437c478bd9Sstevel@tonic-gate if ((*tpp)->nc_flag & NC_VISIBLE) { 4447c478bd9Sstevel@tonic-gate (void) strcat(valid_netpath, (*tpp)->nc_netid); 4457c478bd9Sstevel@tonic-gate (void) strcat(valid_netpath, ":"); 4467c478bd9Sstevel@tonic-gate count++; 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate } 4497c478bd9Sstevel@tonic-gate } else { 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate /* 4527c478bd9Sstevel@tonic-gate * Copy the value of NETPATH (since '\0's will be 4537c478bd9Sstevel@tonic-gate * put into the string) and create the valid NETPATH 4547c478bd9Sstevel@tonic-gate * (by throwing away all netids not in the database). 4557c478bd9Sstevel@tonic-gate * If an entry appears more than one, it *will* be 4567c478bd9Sstevel@tonic-gate * listed twice in the list of valid netpath entries. 4577c478bd9Sstevel@tonic-gate */ 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate (void) strcpy(templine, netpath); 4607c478bd9Sstevel@tonic-gate tp = templine; 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate while (*tp) { 4637c478bd9Sstevel@tonic-gate /* Skip all leading ':'s */ 4647c478bd9Sstevel@tonic-gate while (*tp && *tp == ':') 4657c478bd9Sstevel@tonic-gate tp++; 4667c478bd9Sstevel@tonic-gate if (*tp == NULL) 4677c478bd9Sstevel@tonic-gate break; /* last one */ 4687c478bd9Sstevel@tonic-gate netid = tp; 4697c478bd9Sstevel@tonic-gate while (*tp && *tp != ':') 4707c478bd9Sstevel@tonic-gate tp++; 4717c478bd9Sstevel@tonic-gate if (*tp) 4727c478bd9Sstevel@tonic-gate *tp++ = '\0'; /* isolate netid */ 4737c478bd9Sstevel@tonic-gate 4747c478bd9Sstevel@tonic-gate for (tpp = netpp; *tpp; tpp++) { 4757c478bd9Sstevel@tonic-gate if (strcmp(netid, (*tpp)->nc_netid) == 0) { 4767c478bd9Sstevel@tonic-gate (void) strcat(valid_netpath, 4777c478bd9Sstevel@tonic-gate (*tpp)->nc_netid); 4787c478bd9Sstevel@tonic-gate (void) strcat(valid_netpath, ":"); 4797c478bd9Sstevel@tonic-gate count++; 4807c478bd9Sstevel@tonic-gate break; 4817c478bd9Sstevel@tonic-gate } 4827c478bd9Sstevel@tonic-gate } 4837c478bd9Sstevel@tonic-gate } 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate /* Get space to hold the valid list (+1 for the NULL) */ 4877c478bd9Sstevel@tonic-gate 48861961e0fSrobinson if ((rnetpp = malloc((count + 1) * 4897c478bd9Sstevel@tonic-gate sizeof (struct netconfig *))) == NULL) { 49061961e0fSrobinson free(retp); 4917c478bd9Sstevel@tonic-gate nc_error = NC_NOMEM; 4927c478bd9Sstevel@tonic-gate return (NULL); 4937c478bd9Sstevel@tonic-gate } 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate /* 4967c478bd9Sstevel@tonic-gate * Populate the NETPATH list, ending it with a NULL. 4977c478bd9Sstevel@tonic-gate * Each entry in the list points to the structure in the 4987c478bd9Sstevel@tonic-gate * "netpp" list (the entry must exist in the list, otherwise 4997c478bd9Sstevel@tonic-gate * it wouldn't appear in valid_netpath[]). 5007c478bd9Sstevel@tonic-gate */ 5017c478bd9Sstevel@tonic-gate 5027c478bd9Sstevel@tonic-gate curr_pp = rnetpp; 5037c478bd9Sstevel@tonic-gate netid = tp = valid_netpath; 5047c478bd9Sstevel@tonic-gate while (*tp) { 5057c478bd9Sstevel@tonic-gate netid = tp; 5067c478bd9Sstevel@tonic-gate while (*tp && *tp != ':') 5077c478bd9Sstevel@tonic-gate tp++; 5087c478bd9Sstevel@tonic-gate if (*tp) 5097c478bd9Sstevel@tonic-gate *tp++ = '\0'; 5107c478bd9Sstevel@tonic-gate for (tpp = netpp; *tpp; tpp++) { 5117c478bd9Sstevel@tonic-gate if (strcmp(netid, (*tpp)->nc_netid) == 0) { 5127c478bd9Sstevel@tonic-gate *curr_pp++ = *tpp; 5137c478bd9Sstevel@tonic-gate break; 5147c478bd9Sstevel@tonic-gate } 5157c478bd9Sstevel@tonic-gate } 5167c478bd9Sstevel@tonic-gate } 5177c478bd9Sstevel@tonic-gate *curr_pp = NULL; 5187c478bd9Sstevel@tonic-gate 5197c478bd9Sstevel@tonic-gate retp->nc_curr = retp->nc_head = rnetpp; 5207c478bd9Sstevel@tonic-gate return ((void *)retp); 5217c478bd9Sstevel@tonic-gate } 5227c478bd9Sstevel@tonic-gate 5237c478bd9Sstevel@tonic-gate /* 5247c478bd9Sstevel@tonic-gate * endnetpath() frees up all of the memory allocated by setnetpath(). 5257c478bd9Sstevel@tonic-gate * It returns -1 (error) if setnetpath was never called. 5267c478bd9Sstevel@tonic-gate */ 5277c478bd9Sstevel@tonic-gate 5287c478bd9Sstevel@tonic-gate int 52961961e0fSrobinson endnetpath(void *vdata) 5307c478bd9Sstevel@tonic-gate { 5317c478bd9Sstevel@tonic-gate /* The argument is really a NCONF_HANDLE; cast it here */ 5327c478bd9Sstevel@tonic-gate NCONF_HANDLE *nconf_handlep = (NCONF_HANDLE *)vdata; 5337c478bd9Sstevel@tonic-gate 53461961e0fSrobinson (void) mutex_lock(&netpp_mutex); 5357c478bd9Sstevel@tonic-gate if (netpp == NULL || nconf_handlep == NULL) { 5367c478bd9Sstevel@tonic-gate nc_error = NC_NOSET; 53761961e0fSrobinson (void) mutex_unlock(&netpp_mutex); 5387c478bd9Sstevel@tonic-gate return (-1); 5397c478bd9Sstevel@tonic-gate } 54061961e0fSrobinson (void) mutex_unlock(&netpp_mutex); 5417c478bd9Sstevel@tonic-gate 5427c478bd9Sstevel@tonic-gate free(nconf_handlep->nc_head); 5437c478bd9Sstevel@tonic-gate free(nconf_handlep); 5447c478bd9Sstevel@tonic-gate return (0); 5457c478bd9Sstevel@tonic-gate } 5467c478bd9Sstevel@tonic-gate 5477c478bd9Sstevel@tonic-gate /* 5487c478bd9Sstevel@tonic-gate * getnetpath() returns the current entry in the list 5497c478bd9Sstevel@tonic-gate * from the NETPATH variable. If setnetpath() was not called 5507c478bd9Sstevel@tonic-gate * previously to set up the list, return NULL. 5517c478bd9Sstevel@tonic-gate */ 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate struct netconfig * 55461961e0fSrobinson getnetpath(void *vdata) 5557c478bd9Sstevel@tonic-gate { 5567c478bd9Sstevel@tonic-gate /* The argument is really a NCONF_HANDLE; cast it here */ 5577c478bd9Sstevel@tonic-gate NCONF_HANDLE *nconf_handlep = (NCONF_HANDLE *)vdata; 5587c478bd9Sstevel@tonic-gate struct netconfig *retp; /* holds the return value */ 5597c478bd9Sstevel@tonic-gate int ipv6_present = -1; 5607c478bd9Sstevel@tonic-gate 56161961e0fSrobinson (void) mutex_lock(&netpp_mutex); 5627c478bd9Sstevel@tonic-gate if (netpp == NULL) { 5637c478bd9Sstevel@tonic-gate nc_error = NC_NOSET; 56461961e0fSrobinson (void) mutex_unlock(&netpp_mutex); 5657c478bd9Sstevel@tonic-gate return (NULL); 5667c478bd9Sstevel@tonic-gate } 56761961e0fSrobinson (void) mutex_unlock(&netpp_mutex); 5687c478bd9Sstevel@tonic-gate for (;;) { 5697c478bd9Sstevel@tonic-gate retp = *(nconf_handlep->nc_curr); 5707c478bd9Sstevel@tonic-gate if (retp && (strcmp(retp->nc_netid, "udp6") == 0 || 5717c478bd9Sstevel@tonic-gate strcmp(retp->nc_netid, "tcp6") == 0)) { 5727c478bd9Sstevel@tonic-gate if (ipv6_present == -1) 5737c478bd9Sstevel@tonic-gate ipv6_present = __can_use_af(AF_INET6); 5747c478bd9Sstevel@tonic-gate if (!ipv6_present) { 5757c478bd9Sstevel@tonic-gate ++(nconf_handlep->nc_curr); 5767c478bd9Sstevel@tonic-gate continue; 5777c478bd9Sstevel@tonic-gate } 5787c478bd9Sstevel@tonic-gate } 5797c478bd9Sstevel@tonic-gate break; 5807c478bd9Sstevel@tonic-gate } 5817c478bd9Sstevel@tonic-gate if (retp) { 5827c478bd9Sstevel@tonic-gate ++(nconf_handlep->nc_curr); 5837c478bd9Sstevel@tonic-gate nc_error = NC_NOERROR; 5847c478bd9Sstevel@tonic-gate } else { 5857c478bd9Sstevel@tonic-gate nc_error = NC_NOMOREENTRIES; 5867c478bd9Sstevel@tonic-gate } 5877c478bd9Sstevel@tonic-gate 5887c478bd9Sstevel@tonic-gate return (retp); 5897c478bd9Sstevel@tonic-gate } 5907c478bd9Sstevel@tonic-gate 5917c478bd9Sstevel@tonic-gate /* 5927c478bd9Sstevel@tonic-gate * blank() returns true if the line is a blank line, 0 otherwise 5937c478bd9Sstevel@tonic-gate */ 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate static int 59661961e0fSrobinson blank(char *cp) 5977c478bd9Sstevel@tonic-gate { 5987c478bd9Sstevel@tonic-gate while (*cp && isspace(*cp)) { 5997c478bd9Sstevel@tonic-gate cp++; 6007c478bd9Sstevel@tonic-gate } 6017c478bd9Sstevel@tonic-gate return (*cp == '\0'); 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate 6047c478bd9Sstevel@tonic-gate /* 6057c478bd9Sstevel@tonic-gate * comment() returns true if the line is a comment, 0 otherwise. 6067c478bd9Sstevel@tonic-gate */ 6077c478bd9Sstevel@tonic-gate 6087c478bd9Sstevel@tonic-gate static int 60961961e0fSrobinson comment(char *cp) 6107c478bd9Sstevel@tonic-gate { 6117c478bd9Sstevel@tonic-gate while (*cp && isspace(*cp)) { 6127c478bd9Sstevel@tonic-gate cp++; 6137c478bd9Sstevel@tonic-gate } 6147c478bd9Sstevel@tonic-gate return (*cp == '#'); 6157c478bd9Sstevel@tonic-gate } 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate /* 6187c478bd9Sstevel@tonic-gate * getvalue() searches for the given string in the given array, 6197c478bd9Sstevel@tonic-gate * and return the integer value associated with the string. 6207c478bd9Sstevel@tonic-gate */ 6217c478bd9Sstevel@tonic-gate 6227c478bd9Sstevel@tonic-gate static unsigned int 62361961e0fSrobinson getvalue(char *cp, struct nc_data nc_data[]) 6247c478bd9Sstevel@tonic-gate { 6257c478bd9Sstevel@tonic-gate int i; /* used to index through the given struct nc_data array */ 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate for (i = 0; nc_data[i].string; i++) { 6287c478bd9Sstevel@tonic-gate if (strcmp(nc_data[i].string, cp) == 0) { 6297c478bd9Sstevel@tonic-gate break; 6307c478bd9Sstevel@tonic-gate } 6317c478bd9Sstevel@tonic-gate } 6327c478bd9Sstevel@tonic-gate return (nc_data[i].value); 6337c478bd9Sstevel@tonic-gate } 6347c478bd9Sstevel@tonic-gate 6357c478bd9Sstevel@tonic-gate /* 6367c478bd9Sstevel@tonic-gate * getflag() creates a bitmap of the one-character flags in 6377c478bd9Sstevel@tonic-gate * the given string. It uses nc_flags array to get the values. 6387c478bd9Sstevel@tonic-gate */ 6397c478bd9Sstevel@tonic-gate 6407c478bd9Sstevel@tonic-gate static unsigned int 64161961e0fSrobinson getflag(char *cp) 6427c478bd9Sstevel@tonic-gate { 6437c478bd9Sstevel@tonic-gate int i; /* indexs through the nc_flag array */ 6447c478bd9Sstevel@tonic-gate unsigned int mask = 0; /* holds bitmask of flags */ 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate while (*cp) { 6477c478bd9Sstevel@tonic-gate for (i = 0; nc_flag[i].string; i++) { 6487c478bd9Sstevel@tonic-gate if (*nc_flag[i].string == *cp) { 6497c478bd9Sstevel@tonic-gate mask |= nc_flag[i].value; 6507c478bd9Sstevel@tonic-gate break; 6517c478bd9Sstevel@tonic-gate } 6527c478bd9Sstevel@tonic-gate } 6537c478bd9Sstevel@tonic-gate cp++; 6547c478bd9Sstevel@tonic-gate } 6557c478bd9Sstevel@tonic-gate return (mask); 6567c478bd9Sstevel@tonic-gate } 6577c478bd9Sstevel@tonic-gate 6587c478bd9Sstevel@tonic-gate /* 6597c478bd9Sstevel@tonic-gate * getlookups() creates and returns an array of string representing 6607c478bd9Sstevel@tonic-gate * the directory lookup libraries, given as a comma-seperated list 6617c478bd9Sstevel@tonic-gate * in the argument "cp". 6627c478bd9Sstevel@tonic-gate */ 6637c478bd9Sstevel@tonic-gate 6647c478bd9Sstevel@tonic-gate static char ** 66561961e0fSrobinson getlookups(char *cp) 6667c478bd9Sstevel@tonic-gate { 6677c478bd9Sstevel@tonic-gate unsigned int num; /* holds the number of entries in the list */ 6687c478bd9Sstevel@tonic-gate char **listpp; /* the beginning of the list of dir routines */ 6697c478bd9Sstevel@tonic-gate char **tpp; /* traverses the list, populating it */ 6707c478bd9Sstevel@tonic-gate char *start; 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate num = getnlookups(cp); 67361961e0fSrobinson if (num == 0) 6747c478bd9Sstevel@tonic-gate return (NULL); 67561961e0fSrobinson if ((listpp = malloc((num + 1) * sizeof (char *))) == NULL) 6767c478bd9Sstevel@tonic-gate return (NULL); 6777c478bd9Sstevel@tonic-gate 6787c478bd9Sstevel@tonic-gate tpp = listpp; 6797c478bd9Sstevel@tonic-gate while (num--) { 6807c478bd9Sstevel@tonic-gate start = cp; 6817c478bd9Sstevel@tonic-gate 6827c478bd9Sstevel@tonic-gate /* 6837c478bd9Sstevel@tonic-gate * Traverse the string looking for the next entry 6847c478bd9Sstevel@tonic-gate * of the list (i.e, where the ',' or end of the 6857c478bd9Sstevel@tonic-gate * string appears). If a "\" is found, shift the 6867c478bd9Sstevel@tonic-gate * token over 1 to the left (taking the next char 6877c478bd9Sstevel@tonic-gate * literally). 6887c478bd9Sstevel@tonic-gate */ 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate while (*cp && *cp != ',') { 6917c478bd9Sstevel@tonic-gate if (*cp == '\\' && *(cp + 1)) { 6927c478bd9Sstevel@tonic-gate shift1left(cp); 6937c478bd9Sstevel@tonic-gate } 6947c478bd9Sstevel@tonic-gate cp++; 6957c478bd9Sstevel@tonic-gate } 6967c478bd9Sstevel@tonic-gate if (*cp) 6977c478bd9Sstevel@tonic-gate *cp++ = '\0'; 6987c478bd9Sstevel@tonic-gate if ((*tpp++ = strdup(start)) == NULL) { 6997c478bd9Sstevel@tonic-gate for (tpp = listpp; *tpp; tpp++) 7007c478bd9Sstevel@tonic-gate free(*tpp); 7017c478bd9Sstevel@tonic-gate free(listpp); 7027c478bd9Sstevel@tonic-gate return (NULL); 7037c478bd9Sstevel@tonic-gate } 7047c478bd9Sstevel@tonic-gate } 7057c478bd9Sstevel@tonic-gate *tpp = NULL; 7067c478bd9Sstevel@tonic-gate return (listpp); 7077c478bd9Sstevel@tonic-gate } 7087c478bd9Sstevel@tonic-gate 7097c478bd9Sstevel@tonic-gate /* 7107c478bd9Sstevel@tonic-gate * getnlookups() returns the number of entries in a comma-separated 7117c478bd9Sstevel@tonic-gate * string of tokens. A "-" means no strings are present. 7127c478bd9Sstevel@tonic-gate */ 7137c478bd9Sstevel@tonic-gate 7147c478bd9Sstevel@tonic-gate static unsigned int 71561961e0fSrobinson getnlookups(char *cp) 7167c478bd9Sstevel@tonic-gate { 7177c478bd9Sstevel@tonic-gate unsigned int count; /* the number of tokens in the string */ 7187c478bd9Sstevel@tonic-gate 71961961e0fSrobinson if (strcmp(cp, "-") == 0) 7207c478bd9Sstevel@tonic-gate return (0); 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate count = 1; 7237c478bd9Sstevel@tonic-gate while (*cp) { 7247c478bd9Sstevel@tonic-gate if (*cp == ',') { 7257c478bd9Sstevel@tonic-gate count++; 7267c478bd9Sstevel@tonic-gate } 7277c478bd9Sstevel@tonic-gate 7287c478bd9Sstevel@tonic-gate /* 7297c478bd9Sstevel@tonic-gate * If a "\" is in the string, take the next character 7307c478bd9Sstevel@tonic-gate * literally. Onlly skip the character if "\" is 7317c478bd9Sstevel@tonic-gate * not the last character of the token. 7327c478bd9Sstevel@tonic-gate */ 7337c478bd9Sstevel@tonic-gate if (*cp == '\\' && *(cp + 1)) { 7347c478bd9Sstevel@tonic-gate cp++; 7357c478bd9Sstevel@tonic-gate } 7367c478bd9Sstevel@tonic-gate cp++; 7377c478bd9Sstevel@tonic-gate } 7387c478bd9Sstevel@tonic-gate return (count); 7397c478bd9Sstevel@tonic-gate } 7407c478bd9Sstevel@tonic-gate 7417c478bd9Sstevel@tonic-gate /* 7427c478bd9Sstevel@tonic-gate * gettoken() behaves much like strtok(), except that 7437c478bd9Sstevel@tonic-gate * it knows about escaped space characters (i.e., space characters 7447c478bd9Sstevel@tonic-gate * preceeded by a '\' are taken literally). 7457c478bd9Sstevel@tonic-gate */ 7467c478bd9Sstevel@tonic-gate 7477c478bd9Sstevel@tonic-gate static char * 74861961e0fSrobinson gettoken(char *cp, int skip) 7497c478bd9Sstevel@tonic-gate { 7507c478bd9Sstevel@tonic-gate static char *savep; /* the place where we left off */ 7517c478bd9Sstevel@tonic-gate char *p; /* the beginning of the new token */ 7527c478bd9Sstevel@tonic-gate char *retp; /* the token to be returned */ 7537c478bd9Sstevel@tonic-gate 7547c478bd9Sstevel@tonic-gate fieldnum++; 7557c478bd9Sstevel@tonic-gate 7567c478bd9Sstevel@tonic-gate /* Determine if first or subsequent call */ 7577c478bd9Sstevel@tonic-gate p = (cp == NULL)? savep: cp; 7587c478bd9Sstevel@tonic-gate 7597c478bd9Sstevel@tonic-gate /* Return if no tokens remain. */ 76061961e0fSrobinson if (p == 0) 7617c478bd9Sstevel@tonic-gate return (NULL); 7627c478bd9Sstevel@tonic-gate 7637c478bd9Sstevel@tonic-gate while (isspace(*p)) 7647c478bd9Sstevel@tonic-gate p++; 7657c478bd9Sstevel@tonic-gate 76661961e0fSrobinson if (*p == '\0') 7677c478bd9Sstevel@tonic-gate return (NULL); 7687c478bd9Sstevel@tonic-gate 7697c478bd9Sstevel@tonic-gate /* 7707c478bd9Sstevel@tonic-gate * Save the location of the token and then skip past it 7717c478bd9Sstevel@tonic-gate */ 7727c478bd9Sstevel@tonic-gate 7737c478bd9Sstevel@tonic-gate retp = p; 7747c478bd9Sstevel@tonic-gate while (*p) { 7757c478bd9Sstevel@tonic-gate if (isspace(*p)) 7767c478bd9Sstevel@tonic-gate if (skip == TRUE) { 7777c478bd9Sstevel@tonic-gate shift1left(p); 7787c478bd9Sstevel@tonic-gate continue; 7797c478bd9Sstevel@tonic-gate } else 7807c478bd9Sstevel@tonic-gate break; 7817c478bd9Sstevel@tonic-gate /* 7827c478bd9Sstevel@tonic-gate * Only process the escape of the space seperator; 7837c478bd9Sstevel@tonic-gate * since the token may contain other separators, 7847c478bd9Sstevel@tonic-gate * let the other routines handle the escape of 7857c478bd9Sstevel@tonic-gate * specific characters in the token. 7867c478bd9Sstevel@tonic-gate */ 7877c478bd9Sstevel@tonic-gate 7887c478bd9Sstevel@tonic-gate if (*p == '\\' && *(p + 1) != '\n' && isspace(*(p + 1))) { 7897c478bd9Sstevel@tonic-gate shift1left(p); 7907c478bd9Sstevel@tonic-gate } 7917c478bd9Sstevel@tonic-gate p++; 7927c478bd9Sstevel@tonic-gate } 7937c478bd9Sstevel@tonic-gate if (*p == '\0') { 7947c478bd9Sstevel@tonic-gate savep = 0; /* indicate this is last token */ 7957c478bd9Sstevel@tonic-gate } else { 7967c478bd9Sstevel@tonic-gate *p = '\0'; 7977c478bd9Sstevel@tonic-gate savep = ++p; 7987c478bd9Sstevel@tonic-gate } 7997c478bd9Sstevel@tonic-gate return (strdup(retp)); 8007c478bd9Sstevel@tonic-gate } 8017c478bd9Sstevel@tonic-gate 8027c478bd9Sstevel@tonic-gate /* 8037c478bd9Sstevel@tonic-gate * shift1left() moves all characters in the string over 1 to 8047c478bd9Sstevel@tonic-gate * the left. 8057c478bd9Sstevel@tonic-gate */ 8067c478bd9Sstevel@tonic-gate 8077c478bd9Sstevel@tonic-gate static void 80861961e0fSrobinson shift1left(char *p) 8097c478bd9Sstevel@tonic-gate { 8107c478bd9Sstevel@tonic-gate for (; *p; p++) 8117c478bd9Sstevel@tonic-gate *p = *(p + 1); 8127c478bd9Sstevel@tonic-gate } 8137c478bd9Sstevel@tonic-gate 8147c478bd9Sstevel@tonic-gate char * 81561961e0fSrobinson nc_sperror(void) 8167c478bd9Sstevel@tonic-gate { 8177c478bd9Sstevel@tonic-gate static char buf_main[BUFSIZ]; 818*cb620785Sraf static pthread_key_t perror_key = PTHREAD_ONCE_KEY_NP; 8197c478bd9Sstevel@tonic-gate char *retstr = thr_main()? 8207c478bd9Sstevel@tonic-gate buf_main : 8217c478bd9Sstevel@tonic-gate thr_get_storage(&perror_key, BUFSIZ, free); 8227c478bd9Sstevel@tonic-gate 8237c478bd9Sstevel@tonic-gate if (retstr == NULL) { 8247c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, 8257c478bd9Sstevel@tonic-gate "nc_sperror: malloc failed when trying to create buffer\n"); 8267c478bd9Sstevel@tonic-gate return (NULL); 8277c478bd9Sstevel@tonic-gate } 8287c478bd9Sstevel@tonic-gate 8297c478bd9Sstevel@tonic-gate switch (nc_error) { 8307c478bd9Sstevel@tonic-gate case NC_NOERROR: 8317c478bd9Sstevel@tonic-gate (void) strlcpy(retstr, dgettext(__nsl_dom, "no error"), BUFSIZ); 8327c478bd9Sstevel@tonic-gate break; 8337c478bd9Sstevel@tonic-gate case NC_NOMEM: 8347c478bd9Sstevel@tonic-gate (void) strlcpy(retstr, dgettext(__nsl_dom, "out of memory"), 8357c478bd9Sstevel@tonic-gate BUFSIZ); 8367c478bd9Sstevel@tonic-gate break; 8377c478bd9Sstevel@tonic-gate case NC_NOSET: 8387c478bd9Sstevel@tonic-gate (void) strlcpy(retstr, dgettext(__nsl_dom, 8397c478bd9Sstevel@tonic-gate "routine called before calling \ 8407c478bd9Sstevel@tonic-gate setnetpath() or setnetconfig()"), BUFSIZ); 8417c478bd9Sstevel@tonic-gate break; 8427c478bd9Sstevel@tonic-gate case NC_OPENFAIL: 8437c478bd9Sstevel@tonic-gate (void) strlcpy(retstr, 8447c478bd9Sstevel@tonic-gate dgettext(__nsl_dom, "cannot open /etc/netconfig"), 8457c478bd9Sstevel@tonic-gate BUFSIZ); 8467c478bd9Sstevel@tonic-gate break; 8477c478bd9Sstevel@tonic-gate case NC_BADLINE: 8487c478bd9Sstevel@tonic-gate (void) snprintf(retstr, BUFSIZ, dgettext(__nsl_dom, 8497c478bd9Sstevel@tonic-gate "error in /etc/netconfig: field %d of line %d\n"), 8507c478bd9Sstevel@tonic-gate fieldnum, linenum); 8517c478bd9Sstevel@tonic-gate break; 8527c478bd9Sstevel@tonic-gate case NC_NOTFOUND: 8537c478bd9Sstevel@tonic-gate (void) snprintf(retstr, BUFSIZ, 8547c478bd9Sstevel@tonic-gate dgettext(__nsl_dom, 8557c478bd9Sstevel@tonic-gate "netid not found in /etc/netconfig")); 8567c478bd9Sstevel@tonic-gate break; 8577c478bd9Sstevel@tonic-gate case NC_NOMOREENTRIES: 8587c478bd9Sstevel@tonic-gate (void) snprintf(retstr, BUFSIZ, 8597c478bd9Sstevel@tonic-gate dgettext(__nsl_dom, 8607c478bd9Sstevel@tonic-gate "no more entries in /etc/netconfig")); 8617c478bd9Sstevel@tonic-gate break; 8627c478bd9Sstevel@tonic-gate default: 8637c478bd9Sstevel@tonic-gate (void) strlcpy(retstr, dgettext(__nsl_dom, "unknown error"), 8647c478bd9Sstevel@tonic-gate BUFSIZ); 8657c478bd9Sstevel@tonic-gate break; 8667c478bd9Sstevel@tonic-gate } 8677c478bd9Sstevel@tonic-gate return (retstr); 8687c478bd9Sstevel@tonic-gate } 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate void 8717c478bd9Sstevel@tonic-gate nc_perror(const char *string) 8727c478bd9Sstevel@tonic-gate { 8737c478bd9Sstevel@tonic-gate if (string) 87461961e0fSrobinson (void) fprintf(stderr, "%s: %s\n", string, nc_sperror()); 8757c478bd9Sstevel@tonic-gate else 87661961e0fSrobinson (void) fprintf(stderr, "%s\n", nc_sperror()); 8777c478bd9Sstevel@tonic-gate } 8787c478bd9Sstevel@tonic-gate 8797c478bd9Sstevel@tonic-gate static void 88061961e0fSrobinson netlist_free(struct netconfig ***netppp) 8817c478bd9Sstevel@tonic-gate { 8827c478bd9Sstevel@tonic-gate struct netconfig **tpp; 8837c478bd9Sstevel@tonic-gate 8847c478bd9Sstevel@tonic-gate for (tpp = *netppp; *tpp; tpp++) { 8857c478bd9Sstevel@tonic-gate netconfig_free(*tpp); 8867c478bd9Sstevel@tonic-gate } 8877c478bd9Sstevel@tonic-gate free(*netppp); 8887c478bd9Sstevel@tonic-gate *netppp = NULL; 8897c478bd9Sstevel@tonic-gate } 8907c478bd9Sstevel@tonic-gate 8917c478bd9Sstevel@tonic-gate static void 89261961e0fSrobinson netconfig_free(struct netconfig *netconfigp) 8937c478bd9Sstevel@tonic-gate { 8947c478bd9Sstevel@tonic-gate int i; 8957c478bd9Sstevel@tonic-gate 89661961e0fSrobinson if (netconfigp == NULL) 8977c478bd9Sstevel@tonic-gate return; 8987c478bd9Sstevel@tonic-gate free_entry(netconfigp->nc_netid); 8997c478bd9Sstevel@tonic-gate free_entry(netconfigp->nc_protofmly); 9007c478bd9Sstevel@tonic-gate free_entry(netconfigp->nc_proto); 9017c478bd9Sstevel@tonic-gate free_entry(netconfigp->nc_device); 9027c478bd9Sstevel@tonic-gate if (netconfigp->nc_lookups) 9037c478bd9Sstevel@tonic-gate for (i = 0; i < netconfigp->nc_nlookups; i++) 9047c478bd9Sstevel@tonic-gate free_entry(netconfigp->nc_lookups[i]); 9057c478bd9Sstevel@tonic-gate free_entry(netconfigp->nc_lookups); 9067c478bd9Sstevel@tonic-gate free(netconfigp); 9077c478bd9Sstevel@tonic-gate } 9087c478bd9Sstevel@tonic-gate 9097c478bd9Sstevel@tonic-gate static struct netconfig * 91061961e0fSrobinson netconfig_dup(struct netconfig *netconfigp) 9117c478bd9Sstevel@tonic-gate { 9127c478bd9Sstevel@tonic-gate struct netconfig *nconf; 9137c478bd9Sstevel@tonic-gate int i; 9147c478bd9Sstevel@tonic-gate 91561961e0fSrobinson nconf = calloc(1, sizeof (struct netconfig)); 9167c478bd9Sstevel@tonic-gate if (nconf == NULL) { 9177c478bd9Sstevel@tonic-gate nc_error = NC_NOMEM; 9187c478bd9Sstevel@tonic-gate return (NULL); 9197c478bd9Sstevel@tonic-gate } 9207c478bd9Sstevel@tonic-gate nconf->nc_netid = strdup(netconfigp->nc_netid); 9217c478bd9Sstevel@tonic-gate nconf->nc_protofmly = strdup(netconfigp->nc_protofmly); 9227c478bd9Sstevel@tonic-gate nconf->nc_proto = strdup(netconfigp->nc_proto); 9237c478bd9Sstevel@tonic-gate nconf->nc_device = strdup(netconfigp->nc_device); 92461961e0fSrobinson nconf->nc_lookups = malloc((netconfigp->nc_nlookups + 1) 9257c478bd9Sstevel@tonic-gate * sizeof (char *)); 9267c478bd9Sstevel@tonic-gate if (!(nconf->nc_lookups && nconf->nc_netid && 9277c478bd9Sstevel@tonic-gate nconf->nc_protofmly && nconf->nc_proto && 9287c478bd9Sstevel@tonic-gate nconf->nc_device)) { 9297c478bd9Sstevel@tonic-gate nc_error = NC_NOMEM; 9307c478bd9Sstevel@tonic-gate netconfig_free(nconf); 9317c478bd9Sstevel@tonic-gate return (NULL); 9327c478bd9Sstevel@tonic-gate } 9337c478bd9Sstevel@tonic-gate 9347c478bd9Sstevel@tonic-gate for (i = 0; i < netconfigp->nc_nlookups; i++) { 9357c478bd9Sstevel@tonic-gate nconf->nc_lookups[i] = strdup(netconfigp->nc_lookups[i]); 9367c478bd9Sstevel@tonic-gate if (nconf->nc_lookups[i] == NULL) { 9377c478bd9Sstevel@tonic-gate nconf->nc_nlookups = i; 9387c478bd9Sstevel@tonic-gate netconfig_free(nconf); 9397c478bd9Sstevel@tonic-gate nc_error = NC_NOMEM; 9407c478bd9Sstevel@tonic-gate return (NULL); 9417c478bd9Sstevel@tonic-gate } 9427c478bd9Sstevel@tonic-gate } 9437c478bd9Sstevel@tonic-gate nconf->nc_lookups[i] = NULL; 9447c478bd9Sstevel@tonic-gate nconf->nc_nlookups = netconfigp->nc_nlookups; 9457c478bd9Sstevel@tonic-gate nconf->nc_flag = netconfigp->nc_flag; 9467c478bd9Sstevel@tonic-gate nconf->nc_semantics = netconfigp->nc_semantics; 9477c478bd9Sstevel@tonic-gate return (nconf); 9487c478bd9Sstevel@tonic-gate } 9497c478bd9Sstevel@tonic-gate 9507c478bd9Sstevel@tonic-gate static void 95161961e0fSrobinson free_entry(void *foo) 9527c478bd9Sstevel@tonic-gate { 9537c478bd9Sstevel@tonic-gate if (foo) 9547c478bd9Sstevel@tonic-gate free(foo); 9557c478bd9Sstevel@tonic-gate } 956