Home | History | Annotate | Line # | Download | only in rpc
getrpcent.c revision 1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      3  1.1  cgd  * unrestricted use provided that this legend is included on all tape
      4  1.1  cgd  * media and as a part of the software program in whole or part.  Users
      5  1.1  cgd  * may copy or modify Sun RPC without charge, but are not authorized
      6  1.1  cgd  * to license or distribute it to anyone else except as part of a product or
      7  1.1  cgd  * program developed by the user or with the express written consent of
      8  1.1  cgd  * Sun Microsystems, Inc.
      9  1.1  cgd  *
     10  1.1  cgd  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     11  1.1  cgd  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     12  1.1  cgd  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     13  1.1  cgd  *
     14  1.1  cgd  * Sun RPC is provided with no support and without any obligation on the
     15  1.1  cgd  * part of Sun Microsystems, Inc. to assist in its use, correction,
     16  1.1  cgd  * modification or enhancement.
     17  1.1  cgd  *
     18  1.1  cgd  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     19  1.1  cgd  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     20  1.1  cgd  * OR ANY PART THEREOF.
     21  1.1  cgd  *
     22  1.1  cgd  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     23  1.1  cgd  * or profits or other special, indirect and consequential damages, even if
     24  1.1  cgd  * Sun has been advised of the possibility of such damages.
     25  1.1  cgd  *
     26  1.1  cgd  * Sun Microsystems, Inc.
     27  1.1  cgd  * 2550 Garcia Avenue
     28  1.1  cgd  * Mountain View, California  94043
     29  1.1  cgd  */
     30  1.1  cgd 
     31  1.1  cgd #if defined(LIBC_SCCS) && !defined(lint)
     32  1.1  cgd /*static char *sccsid = "from: @(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro";*/
     33  1.1  cgd static char *rcsid = "$Id: getrpcent.c,v 1.1 1993/10/07 07:29:52 cgd Exp $";
     34  1.1  cgd #endif
     35  1.1  cgd 
     36  1.1  cgd /*
     37  1.1  cgd  * Copyright (c) 1984 by Sun Microsystems, Inc.
     38  1.1  cgd  */
     39  1.1  cgd 
     40  1.1  cgd #include <stdio.h>
     41  1.1  cgd #include <sys/types.h>
     42  1.1  cgd #include <string.h>
     43  1.1  cgd #include <rpc/rpc.h>
     44  1.1  cgd #ifdef YP
     45  1.1  cgd #include <rpcsvc/yp_prot.h>
     46  1.1  cgd #include <rpcsvc/ypclnt.h>
     47  1.1  cgd #endif
     48  1.1  cgd 
     49  1.1  cgd /*
     50  1.1  cgd  * Internet version.
     51  1.1  cgd  */
     52  1.1  cgd struct rpcdata {
     53  1.1  cgd 	FILE	*rpcf;
     54  1.1  cgd 	int	stayopen;
     55  1.1  cgd #define	MAXALIASES	35
     56  1.1  cgd 	char	*rpc_aliases[MAXALIASES];
     57  1.1  cgd 	struct	rpcent rpc;
     58  1.1  cgd 	char	line[BUFSIZ+1];
     59  1.1  cgd #ifdef	YP
     60  1.1  cgd 	char	*domain;
     61  1.1  cgd 	char	*current;
     62  1.1  cgd 	int	currentlen;
     63  1.1  cgd #endif
     64  1.1  cgd } *rpcdata;
     65  1.1  cgd 
     66  1.1  cgd #ifdef	YP
     67  1.1  cgd static int	__yp_nomap = 0;
     68  1.1  cgd #endif	/* YP */
     69  1.1  cgd 
     70  1.1  cgd static	struct rpcent *interpret();
     71  1.1  cgd struct	hostent *gethostent();
     72  1.1  cgd char	*inet_ntoa();
     73  1.1  cgd 
     74  1.1  cgd static char RPCDB[] = "/etc/rpc";
     75  1.1  cgd 
     76  1.1  cgd static struct rpcdata *
     77  1.1  cgd _rpcdata()
     78  1.1  cgd {
     79  1.1  cgd 	register struct rpcdata *d = rpcdata;
     80  1.1  cgd 
     81  1.1  cgd 	if (d == 0) {
     82  1.1  cgd 		d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
     83  1.1  cgd 		rpcdata = d;
     84  1.1  cgd 	}
     85  1.1  cgd 	return (d);
     86  1.1  cgd }
     87  1.1  cgd 
     88  1.1  cgd struct rpcent *
     89  1.1  cgd getrpcbynumber(number)
     90  1.1  cgd 	register int number;
     91  1.1  cgd {
     92  1.1  cgd 	register struct rpcdata *d = _rpcdata();
     93  1.1  cgd 	register struct rpcent *p;
     94  1.1  cgd #ifdef	YP
     95  1.1  cgd 	int reason;
     96  1.1  cgd 	char adrstr[16];
     97  1.1  cgd #endif
     98  1.1  cgd 
     99  1.1  cgd 	if (d == 0)
    100  1.1  cgd 		return (0);
    101  1.1  cgd #ifdef	YP
    102  1.1  cgd         if (!__yp_nomap && _yp_check(&d->domain)) {
    103  1.1  cgd                 sprintf(adrstr, "%d", number);
    104  1.1  cgd                 reason = yp_match(d->domain, "rpc.bynumber", adrstr, strlen(adrstr),
    105  1.1  cgd                                   &d->current, &d->currentlen);
    106  1.1  cgd                 switch(reason) {
    107  1.1  cgd                 case 0:
    108  1.1  cgd                         break;
    109  1.1  cgd                 case YPERR_MAP:
    110  1.1  cgd                         __yp_nomap = 1;
    111  1.1  cgd                         goto no_yp;
    112  1.1  cgd                         break;
    113  1.1  cgd                 default:
    114  1.1  cgd                         return(0);
    115  1.1  cgd                         break;
    116  1.1  cgd                 }
    117  1.1  cgd                 d->current[d->currentlen] = '\0';
    118  1.1  cgd                 p = interpret(d->current, d->currentlen);
    119  1.1  cgd                 (void) free(d->current);
    120  1.1  cgd                 return p;
    121  1.1  cgd         }
    122  1.1  cgd no_yp:
    123  1.1  cgd #endif	/* YP */
    124  1.1  cgd 	setrpcent(0);
    125  1.1  cgd 	while (p = getrpcent()) {
    126  1.1  cgd 		if (p->r_number == number)
    127  1.1  cgd 			break;
    128  1.1  cgd 	}
    129  1.1  cgd 	endrpcent();
    130  1.1  cgd 	return (p);
    131  1.1  cgd }
    132  1.1  cgd 
    133  1.1  cgd struct rpcent *
    134  1.1  cgd getrpcbyname(name)
    135  1.1  cgd 	char *name;
    136  1.1  cgd {
    137  1.1  cgd 	struct rpcent *rpc;
    138  1.1  cgd 	char **rp;
    139  1.1  cgd 
    140  1.1  cgd 	setrpcent(0);
    141  1.1  cgd 	while (rpc = getrpcent()) {
    142  1.1  cgd 		if (strcmp(rpc->r_name, name) == 0)
    143  1.1  cgd 			return (rpc);
    144  1.1  cgd 		for (rp = rpc->r_aliases; *rp != NULL; rp++) {
    145  1.1  cgd 			if (strcmp(*rp, name) == 0)
    146  1.1  cgd 				return (rpc);
    147  1.1  cgd 		}
    148  1.1  cgd 	}
    149  1.1  cgd 	endrpcent();
    150  1.1  cgd 	return (NULL);
    151  1.1  cgd }
    152  1.1  cgd 
    153  1.1  cgd void
    154  1.1  cgd setrpcent(f)
    155  1.1  cgd 	int f;
    156  1.1  cgd {
    157  1.1  cgd 	register struct rpcdata *d = _rpcdata();
    158  1.1  cgd 
    159  1.1  cgd 	if (d == 0)
    160  1.1  cgd 		return;
    161  1.1  cgd #ifdef	YP
    162  1.1  cgd         if (!__yp_nomap && _yp_check(NULL)) {
    163  1.1  cgd                 if (d->current)
    164  1.1  cgd                         free(d->current);
    165  1.1  cgd                 d->current = NULL;
    166  1.1  cgd                 d->currentlen = 0;
    167  1.1  cgd                 return;
    168  1.1  cgd         }
    169  1.1  cgd         __yp_nomap = 0;
    170  1.1  cgd #endif	/* YP */
    171  1.1  cgd 	if (d->rpcf == NULL)
    172  1.1  cgd 		d->rpcf = fopen(RPCDB, "r");
    173  1.1  cgd 	else
    174  1.1  cgd 		rewind(d->rpcf);
    175  1.1  cgd 	d->stayopen |= f;
    176  1.1  cgd }
    177  1.1  cgd 
    178  1.1  cgd void
    179  1.1  cgd endrpcent()
    180  1.1  cgd {
    181  1.1  cgd 	register struct rpcdata *d = _rpcdata();
    182  1.1  cgd 
    183  1.1  cgd 	if (d == 0)
    184  1.1  cgd 		return;
    185  1.1  cgd #ifdef	YP
    186  1.1  cgd         if (!__yp_nomap && _yp_check(NULL)) {
    187  1.1  cgd         	if (d->current && !d->stayopen)
    188  1.1  cgd                         free(d->current);
    189  1.1  cgd                 d->current = NULL;
    190  1.1  cgd                 d->currentlen = 0;
    191  1.1  cgd                 return;
    192  1.1  cgd         }
    193  1.1  cgd         __yp_nomap = 0;
    194  1.1  cgd #endif	/* YP */
    195  1.1  cgd 	if (d->rpcf && !d->stayopen) {
    196  1.1  cgd 		fclose(d->rpcf);
    197  1.1  cgd 		d->rpcf = NULL;
    198  1.1  cgd 	}
    199  1.1  cgd }
    200  1.1  cgd 
    201  1.1  cgd struct rpcent *
    202  1.1  cgd getrpcent()
    203  1.1  cgd {
    204  1.1  cgd 	struct rpcent *hp;
    205  1.1  cgd 	int reason;
    206  1.1  cgd 	register struct rpcdata *d = _rpcdata();
    207  1.1  cgd #ifdef	YP
    208  1.1  cgd 	char *key = NULL, *val = NULL;
    209  1.1  cgd 	int keylen, vallen;
    210  1.1  cgd #endif
    211  1.1  cgd 
    212  1.1  cgd 	if (d == 0)
    213  1.1  cgd 		return(NULL);
    214  1.1  cgd #ifdef	YP
    215  1.1  cgd         if (!__yp_nomap && _yp_check(&d->domain)) {
    216  1.1  cgd                 if (d->current == NULL && d->currentlen == 0) {
    217  1.1  cgd                         reason = yp_first(d->domain, "rpc.bynumber",
    218  1.1  cgd                                           &d->current, &d->currentlen,
    219  1.1  cgd                                           &val, &vallen);
    220  1.1  cgd                 } else {
    221  1.1  cgd                         reason = yp_next(d->domain, "rpc.bynumber",
    222  1.1  cgd                                          d->current, d->currentlen,
    223  1.1  cgd                                          &d->current, &d->currentlen,
    224  1.1  cgd                                          &val, &vallen);
    225  1.1  cgd                 }
    226  1.1  cgd                 switch(reason) {
    227  1.1  cgd                 case 0:
    228  1.1  cgd                         break;
    229  1.1  cgd                 case YPERR_MAP:
    230  1.1  cgd                         __yp_nomap = 1;
    231  1.1  cgd                         goto no_yp;
    232  1.1  cgd                         break;
    233  1.1  cgd                 default:
    234  1.1  cgd                         return(0);
    235  1.1  cgd                         break;
    236  1.1  cgd                 }
    237  1.1  cgd                 val[vallen] = '\0';
    238  1.1  cgd                 hp = interpret(val, vallen);
    239  1.1  cgd                 (void) free(val);
    240  1.1  cgd                 return hp;
    241  1.1  cgd         }
    242  1.1  cgd no_yp:
    243  1.1  cgd #endif	/* YP */
    244  1.1  cgd 	if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
    245  1.1  cgd 		return (NULL);
    246  1.1  cgd         if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
    247  1.1  cgd 		return (NULL);
    248  1.1  cgd 	return (interpret(d->line, strlen(d->line)));
    249  1.1  cgd }
    250  1.1  cgd 
    251  1.1  cgd static struct rpcent *
    252  1.1  cgd interpret(val, len)
    253  1.1  cgd 	char *val;
    254  1.1  cgd 	int len;
    255  1.1  cgd {
    256  1.1  cgd 	register struct rpcdata *d = _rpcdata();
    257  1.1  cgd 	char *p;
    258  1.1  cgd 	register char *cp, **q;
    259  1.1  cgd 
    260  1.1  cgd 	if (d == 0)
    261  1.1  cgd 		return (0);
    262  1.1  cgd 	(void) strncpy(d->line, val, len);
    263  1.1  cgd 	p = d->line;
    264  1.1  cgd 	d->line[len] = '\n';
    265  1.1  cgd 	if (*p == '#')
    266  1.1  cgd 		return (getrpcent());
    267  1.1  cgd 	cp = strpbrk(p, "#\n");
    268  1.1  cgd 	if (cp == NULL)
    269  1.1  cgd 		return (getrpcent());
    270  1.1  cgd 	*cp = '\0';
    271  1.1  cgd 	cp = strpbrk(p, " \t");
    272  1.1  cgd 	if (cp == NULL)
    273  1.1  cgd 		return (getrpcent());
    274  1.1  cgd 	*cp++ = '\0';
    275  1.1  cgd 	/* THIS STUFF IS INTERNET SPECIFIC */
    276  1.1  cgd 	d->rpc.r_name = d->line;
    277  1.1  cgd 	while (*cp == ' ' || *cp == '\t')
    278  1.1  cgd 		cp++;
    279  1.1  cgd 	d->rpc.r_number = atoi(cp);
    280  1.1  cgd 	q = d->rpc.r_aliases = d->rpc_aliases;
    281  1.1  cgd 	cp = strpbrk(cp, " \t");
    282  1.1  cgd 	if (cp != NULL)
    283  1.1  cgd 		*cp++ = '\0';
    284  1.1  cgd 	while (cp && *cp) {
    285  1.1  cgd 		if (*cp == ' ' || *cp == '\t') {
    286  1.1  cgd 			cp++;
    287  1.1  cgd 			continue;
    288  1.1  cgd 		}
    289  1.1  cgd 		if (q < &(d->rpc_aliases[MAXALIASES - 1]))
    290  1.1  cgd 			*q++ = cp;
    291  1.1  cgd 		cp = strpbrk(cp, " \t");
    292  1.1  cgd 		if (cp != NULL)
    293  1.1  cgd 			*cp++ = '\0';
    294  1.1  cgd 	}
    295  1.1  cgd 	*q = NULL;
    296  1.1  cgd 	return (&d->rpc);
    297  1.1  cgd }
    298  1.1  cgd 
    299