1*1f6edd92Sandvar /* $NetBSD: getnetpath.c,v 1.18 2022/01/04 22:10:08 andvar Exp $ */
27df0ccbaSfvdl
37df0ccbaSfvdl /*
447c0e0c3Stron * Copyright (c) 2010, Oracle America, Inc.
57df0ccbaSfvdl *
647c0e0c3Stron * Redistribution and use in source and binary forms, with or without
747c0e0c3Stron * modification, are permitted provided that the following conditions are
847c0e0c3Stron * met:
97df0ccbaSfvdl *
1047c0e0c3Stron * * Redistributions of source code must retain the above copyright
1147c0e0c3Stron * notice, this list of conditions and the following disclaimer.
1247c0e0c3Stron * * Redistributions in binary form must reproduce the above
1347c0e0c3Stron * copyright notice, this list of conditions and the following
1447c0e0c3Stron * disclaimer in the documentation and/or other materials
1547c0e0c3Stron * provided with the distribution.
1647c0e0c3Stron * * Neither the name of the "Oracle America, Inc." nor the names of its
1747c0e0c3Stron * contributors may be used to endorse or promote products derived
1847c0e0c3Stron * from this software without specific prior written permission.
197df0ccbaSfvdl *
2047c0e0c3Stron * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2147c0e0c3Stron * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2247c0e0c3Stron * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2347c0e0c3Stron * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2447c0e0c3Stron * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
2547c0e0c3Stron * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2647c0e0c3Stron * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2747c0e0c3Stron * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2847c0e0c3Stron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2947c0e0c3Stron * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3047c0e0c3Stron * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3147c0e0c3Stron * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
327df0ccbaSfvdl */
335c945215Sitojun
345c945215Sitojun #include <sys/cdefs.h>
355c945215Sitojun #if defined(LIBC_SCCS) && !defined(lint)
365c945215Sitojun #if 0
377df0ccbaSfvdl static char sccsid[] = "@(#)getnetpath.c 1.11 91/12/19 SMI";
385c945215Sitojun #else
39*1f6edd92Sandvar __RCSID("$NetBSD: getnetpath.c,v 1.18 2022/01/04 22:10:08 andvar Exp $");
407df0ccbaSfvdl #endif
415c945215Sitojun #endif
427df0ccbaSfvdl
437df0ccbaSfvdl /*
447df0ccbaSfvdl * Copyright (c) 1989 by Sun Microsystems, Inc.
457df0ccbaSfvdl */
467df0ccbaSfvdl
477df0ccbaSfvdl #include "namespace.h"
487df0ccbaSfvdl #include <stdio.h>
490e8cfd8fSlukem #include <assert.h>
507df0ccbaSfvdl #include <errno.h>
517df0ccbaSfvdl #include <netconfig.h>
527df0ccbaSfvdl #include <stdlib.h>
537df0ccbaSfvdl #include <string.h>
54598be7b0Sassar #include <syslog.h>
557df0ccbaSfvdl
567df0ccbaSfvdl #ifdef __weak_alias
577df0ccbaSfvdl __weak_alias(getnetpath,_getnetpath)
587df0ccbaSfvdl __weak_alias(setnetpath,_setnetpath)
59fd5cb0acSkleink __weak_alias(endnetpath,_endnetpath)
607df0ccbaSfvdl #endif
617df0ccbaSfvdl
627df0ccbaSfvdl /*
637df0ccbaSfvdl * internal structure to keep track of a netpath "session"
647df0ccbaSfvdl */
657df0ccbaSfvdl struct netpath_chain {
667df0ccbaSfvdl struct netconfig *ncp; /* an nconf entry */
677df0ccbaSfvdl struct netpath_chain *nchain_next; /* next nconf entry allocated */
687df0ccbaSfvdl };
697df0ccbaSfvdl
707df0ccbaSfvdl
717df0ccbaSfvdl struct netpath_vars {
727df0ccbaSfvdl int valid; /* token that indicates a valid netpath_vars */
737df0ccbaSfvdl void *nc_handlep; /* handle for current netconfig "session" */
747df0ccbaSfvdl char *netpath; /* pointer to current view-point in NETPATH */
757df0ccbaSfvdl char *netpath_start; /* pointer to start of our copy of NETPATH */
767df0ccbaSfvdl struct netpath_chain *ncp_list; /* list of nconfs allocated this session*/
777df0ccbaSfvdl };
787df0ccbaSfvdl
797df0ccbaSfvdl #define NP_VALID 0xf00d
807df0ccbaSfvdl #define NP_INVALID 0
817df0ccbaSfvdl
82adb74221Smatt char *_get_next_token(char *, int);
837df0ccbaSfvdl
847df0ccbaSfvdl
857df0ccbaSfvdl /*
867df0ccbaSfvdl * A call to setnetpath() establishes a NETPATH "session". setnetpath()
877df0ccbaSfvdl * must be called before the first call to getnetpath(). A "handle" is
887df0ccbaSfvdl * returned to distinguish the session; this handle should be passed
897df0ccbaSfvdl * subsequently to getnetpath(). (Handles are used to allow for nested calls
907df0ccbaSfvdl * to setnetpath()).
917df0ccbaSfvdl * If setnetpath() is unable to establish a session (due to lack of memory
927df0ccbaSfvdl * resources, or the absence of the /etc/netconfig file), a NULL pointer is
937df0ccbaSfvdl * returned.
947df0ccbaSfvdl */
957df0ccbaSfvdl
967df0ccbaSfvdl void *
setnetpath(void)97adb74221Smatt setnetpath(void)
987df0ccbaSfvdl {
997df0ccbaSfvdl struct netpath_vars *np_sessionp; /* this session's variables */
1007df0ccbaSfvdl char *npp; /* NETPATH env variable */
1017df0ccbaSfvdl
1027df0ccbaSfvdl #ifdef MEM_CHK
1037df0ccbaSfvdl malloc_debug(1);
1047df0ccbaSfvdl #endif
1057df0ccbaSfvdl
106c9cdc302Schristos if ((np_sessionp = malloc(sizeof(*np_sessionp))) == NULL)
1077df0ccbaSfvdl return (NULL);
1087df0ccbaSfvdl if ((np_sessionp->nc_handlep = setnetconfig()) == NULL) {
10966ad4bceSchristos free(np_sessionp);
110598be7b0Sassar syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
1117df0ccbaSfvdl return (NULL);
1127df0ccbaSfvdl }
1137df0ccbaSfvdl np_sessionp->valid = NP_VALID;
1147df0ccbaSfvdl np_sessionp->ncp_list = NULL;
1150d2d9accSlukem if ((npp = getenv(NETPATH)) == NULL)
1167df0ccbaSfvdl np_sessionp->netpath = NULL;
1170d2d9accSlukem else {
1180d2d9accSlukem (void) endnetconfig(np_sessionp->nc_handlep);
1190d2d9accSlukem /* won't need nc session*/
1207df0ccbaSfvdl np_sessionp->nc_handlep = NULL;
1217df0ccbaSfvdl if ((np_sessionp->netpath = malloc(strlen(npp)+1)) == NULL) {
1227df0ccbaSfvdl free(np_sessionp);
1237df0ccbaSfvdl return (NULL);
1240d2d9accSlukem } else
1257df0ccbaSfvdl (void) strcpy(np_sessionp->netpath, npp);
1267df0ccbaSfvdl }
1277df0ccbaSfvdl np_sessionp->netpath_start = np_sessionp->netpath;
1287df0ccbaSfvdl return ((void *)np_sessionp);
1297df0ccbaSfvdl }
1307df0ccbaSfvdl
1317df0ccbaSfvdl /*
1327df0ccbaSfvdl * When first called, getnetpath() returns a pointer to the netconfig
1337df0ccbaSfvdl * database entry corresponding to the first valid NETPATH component. The
1347df0ccbaSfvdl * netconfig entry is formatted as a struct netconfig.
1357df0ccbaSfvdl * On each subsequent call, getnetpath returns a pointer to the netconfig
1367df0ccbaSfvdl * entry that corresponds to the next valid NETPATH component. getnetpath
1377df0ccbaSfvdl * can thus be used to search the netconfig database for all networks
1387df0ccbaSfvdl * included in the NETPATH variable.
1397df0ccbaSfvdl * When NETPATH has been exhausted, getnetpath() returns NULL. It returns
1407df0ccbaSfvdl * NULL and sets errno in case of an error (e.g., setnetpath was not called
1417df0ccbaSfvdl * previously).
1427df0ccbaSfvdl * getnetpath() silently ignores invalid NETPATH components. A NETPATH
143*1f6edd92Sandvar * component is invalid if there is no corresponding entry in the netconfig
1447df0ccbaSfvdl * database.
1457df0ccbaSfvdl * If the NETPATH variable is unset, getnetpath() behaves as if NETPATH
1467df0ccbaSfvdl * were set to the sequence of default or visible networks in the netconfig
1477df0ccbaSfvdl * database, in the order in which they are listed.
1487df0ccbaSfvdl */
1497df0ccbaSfvdl
1507df0ccbaSfvdl struct netconfig *
getnetpath(void * handlep)151adb74221Smatt getnetpath(void *handlep)
1527df0ccbaSfvdl {
1537df0ccbaSfvdl struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
1547df0ccbaSfvdl struct netconfig *ncp = NULL; /* temp. holds a netconfig session */
1557df0ccbaSfvdl struct netpath_chain *chainp; /* holds chain of ncp's we alloc */
1567df0ccbaSfvdl char *npp; /* holds current NETPATH */
1577df0ccbaSfvdl
1587df0ccbaSfvdl if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
1597df0ccbaSfvdl errno = EINVAL;
1607df0ccbaSfvdl return (NULL);
1617df0ccbaSfvdl }
1627df0ccbaSfvdl if (np_sessionp->netpath_start == NULL) { /* NETPATH was not set */
1637df0ccbaSfvdl do { /* select next visible network */
1647df0ccbaSfvdl if (np_sessionp->nc_handlep == NULL) {
1657df0ccbaSfvdl np_sessionp->nc_handlep = setnetconfig();
166598be7b0Sassar if (np_sessionp->nc_handlep == NULL)
1670d2d9accSlukem syslog (LOG_ERR,
1680d2d9accSlukem "rpc: failed to open " NETCONFIG);
1697df0ccbaSfvdl }
1700d2d9accSlukem if ((ncp = getnetconfig(np_sessionp->nc_handlep))
1710d2d9accSlukem == NULL)
1727df0ccbaSfvdl return(NULL);
1737df0ccbaSfvdl } while ((ncp->nc_flag & NC_VISIBLE) == 0);
1747df0ccbaSfvdl return (ncp);
1757df0ccbaSfvdl }
1767df0ccbaSfvdl /*
1777df0ccbaSfvdl * Find first valid network ID in netpath.
1787df0ccbaSfvdl */
1797df0ccbaSfvdl while ((npp = np_sessionp->netpath) != NULL && strlen(npp) != 0) {
1807df0ccbaSfvdl np_sessionp->netpath = _get_next_token(npp, ':');
1817df0ccbaSfvdl /*
1827df0ccbaSfvdl * npp is a network identifier.
1837df0ccbaSfvdl */
1847df0ccbaSfvdl if ((ncp = getnetconfigent(npp)) != NULL) {
1850d2d9accSlukem /* cobble alloc chain entry */
186c9cdc302Schristos chainp = malloc(sizeof (struct netpath_chain));
187be2cd377Schristos if (chainp == NULL) {
188be2cd377Schristos freenetconfigent(ncp);
189c9cdc302Schristos return NULL;
190be2cd377Schristos }
1917df0ccbaSfvdl chainp->ncp = ncp;
1927df0ccbaSfvdl chainp->nchain_next = NULL;
1930d2d9accSlukem if (np_sessionp->ncp_list == NULL)
1947df0ccbaSfvdl np_sessionp->ncp_list = chainp;
1950d2d9accSlukem else
1967df0ccbaSfvdl np_sessionp->ncp_list->nchain_next = chainp;
1977df0ccbaSfvdl return (ncp);
1987df0ccbaSfvdl }
1997df0ccbaSfvdl /* couldn't find this token in the database; go to next one. */
2007df0ccbaSfvdl }
2017df0ccbaSfvdl return (NULL);
2027df0ccbaSfvdl }
2037df0ccbaSfvdl
2047df0ccbaSfvdl /*
2057df0ccbaSfvdl * endnetpath() may be called to unbind NETPATH when processing is complete,
2067df0ccbaSfvdl * releasing resources for reuse. It returns 0 on success and -1 on failure
2077df0ccbaSfvdl * (e.g. if setnetpath() was not called previously.
2087df0ccbaSfvdl */
2097df0ccbaSfvdl int
endnetpath(void * handlep)2109e66e6d7Sabs endnetpath(void *handlep)
2117df0ccbaSfvdl {
2127df0ccbaSfvdl struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
2137df0ccbaSfvdl struct netpath_chain *chainp, *lastp;
2147df0ccbaSfvdl
2157df0ccbaSfvdl if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
2167df0ccbaSfvdl errno = EINVAL;
2177df0ccbaSfvdl return (-1);
2187df0ccbaSfvdl }
2197df0ccbaSfvdl if (np_sessionp->nc_handlep != NULL)
2207df0ccbaSfvdl endnetconfig(np_sessionp->nc_handlep);
2217df0ccbaSfvdl if (np_sessionp->netpath_start != NULL)
2227df0ccbaSfvdl free(np_sessionp->netpath_start);
2237df0ccbaSfvdl for (chainp = np_sessionp->ncp_list; chainp != NULL;
2247df0ccbaSfvdl lastp=chainp, chainp=chainp->nchain_next, free(lastp)) {
2257df0ccbaSfvdl freenetconfigent(chainp->ncp);
2267df0ccbaSfvdl }
2277df0ccbaSfvdl free(np_sessionp);
2287df0ccbaSfvdl #ifdef MEM_CHK
2297df0ccbaSfvdl if (malloc_verify() == 0) {
2307df0ccbaSfvdl fprintf(stderr, "memory heap corrupted in endnetpath\n");
2317df0ccbaSfvdl exit(1);
2327df0ccbaSfvdl }
2337df0ccbaSfvdl #endif
2347df0ccbaSfvdl return (0);
2357df0ccbaSfvdl }
2367df0ccbaSfvdl
2377df0ccbaSfvdl
2387df0ccbaSfvdl /*
2397df0ccbaSfvdl * Returns pointer to the rest-of-the-string after the current token.
2407df0ccbaSfvdl * The token itself starts at arg, and we null terminate it. We return NULL
2417df0ccbaSfvdl * if either the arg is empty, or if this is the last token.
2427df0ccbaSfvdl */
2437df0ccbaSfvdl
2447df0ccbaSfvdl char *
_get_next_token(char * npp,int token)2450e61db23Schristos _get_next_token(
2460e61db23Schristos char *npp, /* string */
2470e61db23Schristos int token /* char to parse string for */
2480e61db23Schristos )
2497df0ccbaSfvdl {
2507df0ccbaSfvdl char *cp; /* char pointer */
2517df0ccbaSfvdl char *np; /* netpath pointer */
2527df0ccbaSfvdl char *ep; /* escape pointer */
2537df0ccbaSfvdl
2540e8cfd8fSlukem _DIAGASSERT(npp != NULL);
2550e8cfd8fSlukem
2560d2d9accSlukem if ((cp = strchr(npp, token)) == NULL)
2577df0ccbaSfvdl return (NULL);
2587df0ccbaSfvdl /*
2597df0ccbaSfvdl * did find a token, but it might be escaped.
2607df0ccbaSfvdl */
2619b7886c6Sgroo if ((cp > npp) && (cp[-1] == '\\')) {
2620d2d9accSlukem /*
2630d2d9accSlukem * if slash was also escaped, carry on, otherwise find
2640d2d9accSlukem * next token
2650d2d9accSlukem */
2669b7886c6Sgroo if ((cp > npp + 1) && (cp[-2] != '\\')) {
2677df0ccbaSfvdl /* shift r-o-s onto the escaped token */
2687df0ccbaSfvdl strcpy(&cp[-1], cp); /* XXX: overlapping string copy */
2697df0ccbaSfvdl /*
2707df0ccbaSfvdl * Do a recursive call.
2717df0ccbaSfvdl * We don't know how many escaped tokens there might be.
2727df0ccbaSfvdl */
2737df0ccbaSfvdl return (_get_next_token(cp, token));
2747df0ccbaSfvdl }
2757df0ccbaSfvdl }
2767df0ccbaSfvdl
2777df0ccbaSfvdl *cp++ = '\0'; /* null-terminate token */
2787df0ccbaSfvdl /* get rid of any backslash escapes */
2797df0ccbaSfvdl ep = npp;
2807df0ccbaSfvdl while ((np = strchr(ep, '\\')) != 0) {
2817df0ccbaSfvdl if (np[1] == '\\')
2827df0ccbaSfvdl np++;
2837df0ccbaSfvdl strcpy(np, (ep = &np[1])); /* XXX: overlapping string copy */
2847df0ccbaSfvdl }
2857df0ccbaSfvdl return (cp); /* return ptr to r-o-s */
2867df0ccbaSfvdl }
287