/* $NetBSD: getnetpath.c,v 1.3 2000/07/06 03:10:34 christos Exp $ */12/*-3* SPDX-License-Identifier: BSD-3-Clause4*5* Copyright (c) 2009, Sun Microsystems, Inc.6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions are met:10* - Redistributions of source code must retain the above copyright notice,11* this list of conditions and the following disclaimer.12* - Redistributions in binary form must reproduce the above copyright notice,13* this list of conditions and the following disclaimer in the documentation14* and/or other materials provided with the distribution.15* - Neither the name of Sun Microsystems, Inc. nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"20* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE23* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR24* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF25* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS26* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN27* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE29* POSSIBILITY OF SUCH DAMAGE.30*/3132/*33* Copyright (c) 1989 by Sun Microsystems, Inc.34*/3536#include "namespace.h"37#include <stdio.h>38#include <errno.h>39#include <netconfig.h>40#include <stdlib.h>41#include <string.h>42#include <syslog.h>43#include "un-namespace.h"4445/*46* internal structure to keep track of a netpath "session"47*/48struct netpath_chain {49struct netconfig *ncp; /* an nconf entry */50struct netpath_chain *nchain_next; /* next nconf entry allocated */51};525354struct netpath_vars {55int valid; /* token that indicates a valid netpath_vars */56void *nc_handlep; /* handle for current netconfig "session" */57char *netpath; /* pointer to current view-point in NETPATH */58char *netpath_start; /* pointer to start of our copy of NETPATH */59struct netpath_chain *ncp_list; /* list of nconfs allocated this session*/60};6162#define NP_VALID 0xf00d63#define NP_INVALID 06465char *_get_next_token(char *, int);666768/*69* A call to setnetpath() establishes a NETPATH "session". setnetpath()70* must be called before the first call to getnetpath(). A "handle" is71* returned to distinguish the session; this handle should be passed72* subsequently to getnetpath(). (Handles are used to allow for nested calls73* to setnetpath()).74* If setnetpath() is unable to establish a session (due to lack of memory75* resources, or the absence of the /etc/netconfig file), a NULL pointer is76* returned.77*/7879void *80setnetpath(void)81{8283struct netpath_vars *np_sessionp; /* this session's variables */84char *npp; /* NETPATH env variable */8586#ifdef MEM_CHK87malloc_debug(1);88#endif8990if ((np_sessionp =91(struct netpath_vars *)malloc(sizeof (struct netpath_vars))) == NULL) {92return (NULL);93}94if ((np_sessionp->nc_handlep = setnetconfig()) == NULL) {95syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);96goto failed;97}98np_sessionp->valid = NP_VALID;99np_sessionp->ncp_list = NULL;100if ((npp = getenv(NETPATH)) == NULL) {101np_sessionp->netpath = NULL;102} else {103(void) endnetconfig(np_sessionp->nc_handlep);/* won't need nc session*/104np_sessionp->nc_handlep = NULL;105if ((np_sessionp->netpath = malloc(strlen(npp)+1)) == NULL)106goto failed;107else {108(void) strcpy(np_sessionp->netpath, npp);109}110}111np_sessionp->netpath_start = np_sessionp->netpath;112return ((void *)np_sessionp);113114failed:115free(np_sessionp);116return (NULL);117}118119/*120* When first called, getnetpath() returns a pointer to the netconfig121* database entry corresponding to the first valid NETPATH component. The122* netconfig entry is formatted as a struct netconfig.123* On each subsequent call, getnetpath returns a pointer to the netconfig124* entry that corresponds to the next valid NETPATH component. getnetpath125* can thus be used to search the netconfig database for all networks126* included in the NETPATH variable.127* When NETPATH has been exhausted, getnetpath() returns NULL. It returns128* NULL and sets errno in case of an error (e.g., setnetpath was not called129* previously).130* getnetpath() silently ignores invalid NETPATH components. A NETPATH131* component is invalid if there is no corresponding entry in the netconfig132* database.133* If the NETPATH variable is unset, getnetpath() behaves as if NETPATH134* were set to the sequence of default or visible networks in the netconfig135* database, in the order in which they are listed.136*/137138struct netconfig *139getnetpath(void *handlep)140{141struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;142struct netconfig *ncp = NULL; /* temp. holds a netconfig session */143struct netpath_chain *chainp; /* holds chain of ncp's we alloc */144char *npp; /* holds current NETPATH */145146if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {147errno = EINVAL;148return (NULL);149}150if (np_sessionp->netpath_start == NULL) { /* NETPATH was not set */151do { /* select next visible network */152if (np_sessionp->nc_handlep == NULL) {153np_sessionp->nc_handlep = setnetconfig();154if (np_sessionp->nc_handlep == NULL)155syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);156}157if ((ncp = getnetconfig(np_sessionp->nc_handlep)) == NULL) {158return(NULL);159}160} while ((ncp->nc_flag & NC_VISIBLE) == 0);161return (ncp);162}163/*164* Find first valid network ID in netpath.165*/166while ((npp = np_sessionp->netpath) != NULL && strlen(npp) != 0) {167np_sessionp->netpath = _get_next_token(npp, ':');168/*169* npp is a network identifier.170*/171if ((ncp = getnetconfigent(npp)) != NULL) {172chainp = (struct netpath_chain *) /* cobble alloc chain entry */173malloc(sizeof (struct netpath_chain));174chainp->ncp = ncp;175chainp->nchain_next = NULL;176if (np_sessionp->ncp_list == NULL) {177np_sessionp->ncp_list = chainp;178} else {179np_sessionp->ncp_list->nchain_next = chainp;180}181return (ncp);182}183/* couldn't find this token in the database; go to next one. */184}185return (NULL);186}187188/*189* endnetpath() may be called to unbind NETPATH when processing is complete,190* releasing resources for reuse. It returns 0 on success and -1 on failure191* (e.g. if setnetpath() was not called previously.192*/193int194endnetpath(void *handlep)195{196struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;197struct netpath_chain *chainp, *lastp;198199if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {200errno = EINVAL;201return (-1);202}203if (np_sessionp->nc_handlep != NULL)204endnetconfig(np_sessionp->nc_handlep);205if (np_sessionp->netpath_start != NULL)206free(np_sessionp->netpath_start);207for (chainp = np_sessionp->ncp_list; chainp != NULL;208lastp=chainp, chainp=chainp->nchain_next, free(lastp)) {209freenetconfigent(chainp->ncp);210}211free(np_sessionp);212#ifdef MEM_CHK213if (malloc_verify() == 0) {214fprintf(stderr, "memory heap corrupted in endnetpath\n");215exit(1);216}217#endif218return (0);219}220221222223/*224* Returns pointer to the rest-of-the-string after the current token.225* The token itself starts at arg, and we null terminate it. We return NULL226* if either the arg is empty, or if this is the last token.227*228* npp - string229* token - char to parse string for230*/231char *232_get_next_token(char *npp, int token)233{234char *cp; /* char pointer */235char *np; /* netpath pointer */236char *ep; /* escape pointer */237238if ((cp = strchr(npp, token)) == NULL) {239return (NULL);240}241/*242* did find a token, but it might be escaped.243*/244if ((cp > npp) && (cp[-1] == '\\')) {245/* if slash was also escaped, carry on, otherwise find next token */246if ((cp > npp + 1) && (cp[-2] != '\\')) {247/* shift r-o-s onto the escaped token */248strcpy(&cp[-1], cp); /* XXX: overlapping string copy */249/*250* Do a recursive call.251* We don't know how many escaped tokens there might be.252*/253return (_get_next_token(cp, token));254}255}256257*cp++ = '\0'; /* null-terminate token */258/* get rid of any backslash escapes */259ep = npp;260while ((np = strchr(ep, '\\')) != NULL) {261if (np[1] == '\\')262np++;263strcpy(np, (ep = &np[1])); /* XXX: overlapping string copy */264}265return (cp); /* return ptr to r-o-s */266}267268269