Home | History | Annotate | Line # | Download | only in rpc
getnetpath.c revision 1.10
      1  1.10  christos /*	$NetBSD: getnetpath.c,v 1.10 2006/03/19 01:44:48 christos Exp $	*/
      2   1.1      fvdl 
      3   1.1      fvdl /*
      4   1.1      fvdl  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5   1.1      fvdl  * unrestricted use provided that this legend is included on all tape
      6   1.1      fvdl  * media and as a part of the software program in whole or part.  Users
      7   1.1      fvdl  * may copy or modify Sun RPC without charge, but are not authorized
      8   1.1      fvdl  * to license or distribute it to anyone else except as part of a product or
      9   1.1      fvdl  * program developed by the user or with the express written consent of
     10   1.1      fvdl  * Sun Microsystems, Inc.
     11   1.1      fvdl  *
     12   1.1      fvdl  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     13   1.1      fvdl  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     14   1.1      fvdl  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     15   1.1      fvdl  *
     16   1.1      fvdl  * Sun RPC is provided with no support and without any obligation on the
     17   1.1      fvdl  * part of Sun Microsystems, Inc. to assist in its use, correction,
     18   1.1      fvdl  * modification or enhancement.
     19   1.1      fvdl  *
     20   1.1      fvdl  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     21   1.1      fvdl  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     22   1.1      fvdl  * OR ANY PART THEREOF.
     23   1.1      fvdl  *
     24   1.1      fvdl  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     25   1.1      fvdl  * or profits or other special, indirect and consequential damages, even if
     26   1.1      fvdl  * Sun has been advised of the possibility of such damages.
     27   1.1      fvdl  *
     28   1.1      fvdl  * Sun Microsystems, Inc.
     29   1.1      fvdl  * 2550 Garcia Avenue
     30   1.1      fvdl  * Mountain View, California  94043
     31   1.1      fvdl  */
     32   1.8    itojun 
     33   1.8    itojun #include <sys/cdefs.h>
     34   1.8    itojun #if defined(LIBC_SCCS) && !defined(lint)
     35   1.8    itojun #if 0
     36   1.1      fvdl static        char sccsid[] = "@(#)getnetpath.c	1.11 91/12/19 SMI";
     37   1.8    itojun #else
     38  1.10  christos __RCSID("$NetBSD: getnetpath.c,v 1.10 2006/03/19 01:44:48 christos Exp $");
     39   1.8    itojun #endif
     40   1.1      fvdl #endif
     41   1.1      fvdl 
     42   1.1      fvdl /*
     43   1.1      fvdl  * Copyright (c) 1989 by Sun Microsystems, Inc.
     44   1.1      fvdl  */
     45   1.1      fvdl 
     46   1.1      fvdl #include "namespace.h"
     47   1.1      fvdl #include <sys/cdefs.h>
     48   1.1      fvdl #include <stdio.h>
     49   1.4     lukem #include <assert.h>
     50   1.1      fvdl #include <errno.h>
     51   1.1      fvdl #include <netconfig.h>
     52   1.1      fvdl #include <stdlib.h>
     53   1.1      fvdl #include <string.h>
     54   1.2     assar #include <syslog.h>
     55   1.1      fvdl 
     56   1.1      fvdl #ifdef __weak_alias
     57   1.1      fvdl __weak_alias(getnetpath,_getnetpath)
     58   1.1      fvdl __weak_alias(setnetpath,_setnetpath)
     59   1.9    kleink __weak_alias(endnetpath,_endnetpath)
     60   1.1      fvdl #endif
     61   1.1      fvdl 
     62   1.1      fvdl /*
     63   1.1      fvdl  * internal structure to keep track of a netpath "session"
     64   1.1      fvdl  */
     65   1.1      fvdl struct netpath_chain {
     66   1.5     lukem 	struct netconfig *ncp;  		/* an nconf entry */
     67   1.5     lukem 	struct netpath_chain *nchain_next;	/* next nconf entry allocated */
     68   1.1      fvdl };
     69   1.1      fvdl 
     70   1.1      fvdl 
     71   1.1      fvdl struct netpath_vars {
     72   1.5     lukem 	int   valid;		/* token that indicates a valid netpath_vars */
     73   1.5     lukem 	void *nc_handlep;	/* handle for current netconfig "session" */
     74   1.5     lukem 	char *netpath;		/* pointer to current view-point in NETPATH */
     75   1.5     lukem 	char *netpath_start;	/* pointer to start of our copy of NETPATH */
     76   1.5     lukem 	struct netpath_chain *ncp_list;  /* list of nconfs allocated this session*/
     77   1.1      fvdl };
     78   1.1      fvdl 
     79   1.1      fvdl #define NP_VALID	0xf00d
     80   1.1      fvdl #define NP_INVALID	0
     81   1.1      fvdl 
     82   1.3  christos char *_get_next_token __P((char *, int));
     83   1.1      fvdl 
     84   1.1      fvdl 
     85   1.1      fvdl /*
     86   1.1      fvdl  * A call to setnetpath() establishes a NETPATH "session".  setnetpath()
     87   1.1      fvdl  * must be called before the first call to getnetpath().  A "handle" is
     88   1.1      fvdl  * returned to distinguish the session; this handle should be passed
     89   1.1      fvdl  * subsequently to getnetpath().  (Handles are used to allow for nested calls
     90   1.1      fvdl  * to setnetpath()).
     91   1.1      fvdl  * If setnetpath() is unable to establish a session (due to lack of memory
     92   1.1      fvdl  * resources, or the absence of the /etc/netconfig file), a NULL pointer is
     93   1.1      fvdl  * returned.
     94   1.1      fvdl  */
     95   1.1      fvdl 
     96   1.1      fvdl void *
     97   1.1      fvdl setnetpath()
     98   1.1      fvdl {
     99   1.5     lukem 	struct netpath_vars *np_sessionp;   /* this session's variables */
    100   1.5     lukem 	char *npp;				/* NETPATH env variable */
    101   1.1      fvdl 
    102   1.1      fvdl #ifdef MEM_CHK
    103   1.5     lukem 	malloc_debug(1);
    104   1.1      fvdl #endif
    105   1.1      fvdl 
    106   1.5     lukem 	if ((np_sessionp = (struct netpath_vars *)
    107   1.5     lukem 	    malloc(sizeof (struct netpath_vars))) == NULL)
    108   1.5     lukem 		return (NULL);
    109   1.5     lukem 	if ((np_sessionp->nc_handlep = setnetconfig()) == NULL) {
    110  1.10  christos 		free(np_sessionp);
    111   1.5     lukem 		syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
    112   1.5     lukem 		return (NULL);
    113   1.5     lukem 	}
    114   1.5     lukem 	np_sessionp->valid = NP_VALID;
    115   1.5     lukem 	np_sessionp->ncp_list = NULL;
    116   1.5     lukem 	if ((npp = getenv(NETPATH)) == NULL)
    117   1.5     lukem 		np_sessionp->netpath = NULL;
    118   1.5     lukem 	else {
    119   1.5     lukem 		(void) endnetconfig(np_sessionp->nc_handlep);
    120   1.5     lukem 					/* won't need nc session*/
    121   1.5     lukem 		np_sessionp->nc_handlep = NULL;
    122   1.5     lukem 		if ((np_sessionp->netpath = malloc(strlen(npp)+1)) == NULL) {
    123   1.5     lukem 			free(np_sessionp);
    124   1.5     lukem 			return (NULL);
    125   1.5     lukem 		} else
    126   1.5     lukem 			(void) strcpy(np_sessionp->netpath, npp);
    127   1.1      fvdl 	}
    128   1.5     lukem 	np_sessionp->netpath_start = np_sessionp->netpath;
    129   1.5     lukem 	return ((void *)np_sessionp);
    130   1.1      fvdl }
    131   1.1      fvdl 
    132   1.1      fvdl /*
    133   1.1      fvdl  * When first called, getnetpath() returns a pointer to the netconfig
    134   1.1      fvdl  * database entry corresponding to the first valid NETPATH component.  The
    135   1.1      fvdl  * netconfig entry is formatted as a struct netconfig.
    136   1.1      fvdl  * On each subsequent call, getnetpath returns a pointer to the netconfig
    137   1.1      fvdl  * entry that corresponds to the next valid NETPATH component.  getnetpath
    138   1.1      fvdl  * can thus be used to search the netconfig database for all networks
    139   1.1      fvdl  * included in the NETPATH variable.
    140   1.1      fvdl  * When NETPATH has been exhausted, getnetpath() returns NULL.  It returns
    141   1.1      fvdl  * NULL and sets errno in case of an error (e.g., setnetpath was not called
    142   1.1      fvdl  * previously).
    143   1.1      fvdl  * getnetpath() silently ignores invalid NETPATH components.  A NETPATH
    144   1.1      fvdl  * compnent is invalid if there is no corresponding entry in the netconfig
    145   1.1      fvdl  * database.
    146   1.1      fvdl  * If the NETPATH variable is unset, getnetpath() behaves as if NETPATH
    147   1.1      fvdl  * were set to the sequence of default or visible networks in the netconfig
    148   1.1      fvdl  * database, in the order in which they are listed.
    149   1.1      fvdl  */
    150   1.1      fvdl 
    151   1.1      fvdl struct netconfig *
    152   1.1      fvdl getnetpath(handlep)
    153   1.5     lukem 	void *handlep;
    154   1.1      fvdl {
    155   1.5     lukem 	struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
    156   1.5     lukem 	struct netconfig *ncp = NULL;   /* temp. holds a netconfig session */
    157   1.5     lukem 	struct netpath_chain *chainp;   /* holds chain of ncp's we alloc */
    158   1.5     lukem 	char  *npp;		/* holds current NETPATH */
    159   1.5     lukem 
    160   1.5     lukem 	if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
    161   1.5     lukem 		errno = EINVAL;
    162   1.5     lukem 		return (NULL);
    163   1.5     lukem 	}
    164   1.5     lukem 	if (np_sessionp->netpath_start == NULL) { /* NETPATH was not set */
    165   1.5     lukem 		do {                /* select next visible network */
    166   1.5     lukem 			if (np_sessionp->nc_handlep == NULL) {
    167   1.5     lukem 				np_sessionp->nc_handlep = setnetconfig();
    168   1.5     lukem 				if (np_sessionp->nc_handlep == NULL)
    169   1.5     lukem 					syslog (LOG_ERR,
    170   1.5     lukem 					    "rpc: failed to open " NETCONFIG);
    171   1.5     lukem 			}
    172   1.5     lukem 			if ((ncp = getnetconfig(np_sessionp->nc_handlep))
    173   1.5     lukem 			    == NULL)
    174   1.5     lukem 				return(NULL);
    175   1.5     lukem 		} while ((ncp->nc_flag & NC_VISIBLE) == 0);
    176   1.5     lukem 		return (ncp);
    177   1.5     lukem 	}
    178   1.5     lukem 	/*
    179   1.5     lukem 	 * Find first valid network ID in netpath.
    180   1.1      fvdl 	 */
    181   1.5     lukem 	while ((npp = np_sessionp->netpath) != NULL && strlen(npp) != 0) {
    182   1.5     lukem 		np_sessionp->netpath = _get_next_token(npp, ':');
    183   1.5     lukem 		/*
    184   1.5     lukem 		 * npp is a network identifier.
    185   1.5     lukem 		 */
    186   1.5     lukem 		if ((ncp = getnetconfigent(npp)) != NULL) {
    187   1.5     lukem 					/* cobble alloc chain entry */
    188   1.5     lukem 			chainp = (struct netpath_chain *)
    189   1.5     lukem 			    malloc(sizeof (struct netpath_chain));
    190   1.5     lukem 			chainp->ncp = ncp;
    191   1.5     lukem 			chainp->nchain_next = NULL;
    192   1.5     lukem 			if (np_sessionp->ncp_list == NULL)
    193   1.5     lukem 				np_sessionp->ncp_list = chainp;
    194   1.5     lukem 			else
    195   1.5     lukem 				np_sessionp->ncp_list->nchain_next = chainp;
    196   1.5     lukem 			return (ncp);
    197   1.5     lukem 		}
    198   1.5     lukem 		/* couldn't find this token in the database; go to next one. */
    199   1.1      fvdl 	}
    200   1.5     lukem 	return (NULL);
    201   1.1      fvdl }
    202   1.1      fvdl 
    203   1.1      fvdl /*
    204   1.1      fvdl  * endnetpath() may be called to unbind NETPATH when processing is complete,
    205   1.1      fvdl  * releasing resources for reuse.  It returns 0 on success and -1 on failure
    206   1.1      fvdl  * (e.g. if setnetpath() was not called previously.
    207   1.1      fvdl  */
    208   1.1      fvdl int
    209   1.1      fvdl endnetpath(handlep)
    210   1.5     lukem 	void *handlep;
    211   1.1      fvdl {
    212   1.5     lukem 	struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
    213   1.5     lukem 	struct netpath_chain *chainp, *lastp;
    214   1.1      fvdl 
    215   1.5     lukem 	if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
    216   1.5     lukem 		errno = EINVAL;
    217   1.5     lukem 		return (-1);
    218   1.5     lukem 	}
    219   1.5     lukem 	if (np_sessionp->nc_handlep != NULL)
    220   1.5     lukem 		endnetconfig(np_sessionp->nc_handlep);
    221   1.5     lukem 	if (np_sessionp->netpath_start != NULL)
    222   1.5     lukem 		free(np_sessionp->netpath_start);
    223   1.5     lukem 	for (chainp = np_sessionp->ncp_list; chainp != NULL;
    224   1.1      fvdl 	    lastp=chainp, chainp=chainp->nchain_next, free(lastp)) {
    225   1.5     lukem 		freenetconfigent(chainp->ncp);
    226   1.5     lukem 	}
    227   1.5     lukem 	free(np_sessionp);
    228   1.1      fvdl #ifdef MEM_CHK
    229   1.5     lukem 	if (malloc_verify() == 0) {
    230   1.5     lukem 		fprintf(stderr, "memory heap corrupted in endnetpath\n");
    231   1.5     lukem 		exit(1);
    232   1.5     lukem 	}
    233   1.1      fvdl #endif
    234   1.5     lukem 	return (0);
    235   1.1      fvdl }
    236   1.1      fvdl 
    237   1.1      fvdl 
    238   1.1      fvdl /*
    239   1.1      fvdl  * Returns pointer to the rest-of-the-string after the current token.
    240   1.1      fvdl  * The token itself starts at arg, and we null terminate it.  We return NULL
    241   1.1      fvdl  * if either the arg is empty, or if this is the last token.
    242   1.1      fvdl  */
    243   1.1      fvdl 
    244   1.1      fvdl char *
    245   1.1      fvdl _get_next_token(npp, token)
    246   1.5     lukem 	char *npp;		/* string */
    247   1.5     lukem 	int token;		/* char to parse string for */
    248   1.1      fvdl {
    249   1.5     lukem 	char  *cp;		/* char pointer */
    250   1.5     lukem 	char  *np;		/* netpath pointer */
    251   1.5     lukem 	char  *ep;		/* escape pointer */
    252   1.5     lukem 
    253   1.5     lukem 	_DIAGASSERT(npp != NULL);
    254   1.5     lukem 	_DIAGASSERT(token != NULL);
    255   1.5     lukem 
    256   1.5     lukem 	if ((cp = strchr(npp, token)) == NULL)
    257   1.5     lukem 		return (NULL);
    258   1.5     lukem 	/*
    259   1.5     lukem 	 * did find a token, but it might be escaped.
    260   1.5     lukem 	 */
    261   1.6      groo 	if ((cp > npp) && (cp[-1] == '\\')) {
    262   1.5     lukem 		/*
    263   1.5     lukem 		 * if slash was also escaped, carry on, otherwise find
    264   1.5     lukem 		 * next token
    265   1.5     lukem 		 */
    266   1.6      groo 		if ((cp > npp + 1) && (cp[-2] != '\\')) {
    267   1.5     lukem 			/* shift r-o-s  onto the escaped token */
    268   1.5     lukem 			strcpy(&cp[-1], cp);  /* XXX: overlapping string copy */
    269   1.5     lukem 			/*
    270   1.5     lukem 			 * Do a recursive call.
    271   1.5     lukem 			 * We don't know how many escaped tokens there might be.
    272   1.5     lukem 			 */
    273   1.5     lukem 			return (_get_next_token(cp, token));
    274   1.5     lukem 		}
    275   1.5     lukem 	}
    276   1.1      fvdl 
    277   1.5     lukem 	*cp++ = '\0';		/* null-terminate token */
    278   1.5     lukem 	/* get rid of any backslash escapes */
    279   1.5     lukem 	ep = npp;
    280   1.5     lukem 	while ((np = strchr(ep, '\\')) != 0) {
    281   1.5     lukem 		if (np[1] == '\\')
    282   1.5     lukem 			np++;
    283   1.5     lukem 		strcpy(np, (ep = &np[1]));  /* XXX: overlapping string copy */
    284   1.1      fvdl 	}
    285   1.5     lukem 	return (cp);		/* return ptr to r-o-s */
    286   1.1      fvdl }
    287