xref: /freebsd/lib/libc/rpc/getrpcent.c (revision 8360efbd6c932013ffdb2f83d2f2de4278febb5e)
18360efbdSAlfred Perlstein /*	$NetBSD: getrpcent.c,v 1.17 2000/01/22 22:19:17 mycroft Exp $	*/
28360efbdSAlfred Perlstein 
399064799SGarrett Wollman /*
499064799SGarrett Wollman  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
599064799SGarrett Wollman  * unrestricted use provided that this legend is included on all tape
699064799SGarrett Wollman  * media and as a part of the software program in whole or part.  Users
799064799SGarrett Wollman  * may copy or modify Sun RPC without charge, but are not authorized
899064799SGarrett Wollman  * to license or distribute it to anyone else except as part of a product or
999064799SGarrett Wollman  * program developed by the user or with the express written consent of
1099064799SGarrett Wollman  * Sun Microsystems, Inc.
1199064799SGarrett Wollman  *
1299064799SGarrett Wollman  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1399064799SGarrett Wollman  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1499064799SGarrett Wollman  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1599064799SGarrett Wollman  *
1699064799SGarrett Wollman  * Sun RPC is provided with no support and without any obligation on the
1799064799SGarrett Wollman  * part of Sun Microsystems, Inc. to assist in its use, correction,
1899064799SGarrett Wollman  * modification or enhancement.
1999064799SGarrett Wollman  *
2099064799SGarrett Wollman  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
2199064799SGarrett Wollman  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
2299064799SGarrett Wollman  * OR ANY PART THEREOF.
2399064799SGarrett Wollman  *
2499064799SGarrett Wollman  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2599064799SGarrett Wollman  * or profits or other special, indirect and consequential damages, even if
2699064799SGarrett Wollman  * Sun has been advised of the possibility of such damages.
2799064799SGarrett Wollman  *
2899064799SGarrett Wollman  * Sun Microsystems, Inc.
2999064799SGarrett Wollman  * 2550 Garcia Avenue
3099064799SGarrett Wollman  * Mountain View, California  94043
3199064799SGarrett Wollman  */
3299064799SGarrett Wollman 
338360efbdSAlfred Perlstein #include <sys/cdefs.h>
3499064799SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint)
358360efbdSAlfred Perlstein static char *sccsid = "@(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro";
367f3dea24SPeter Wemm static char *rcsid = "$FreeBSD$";
3799064799SGarrett Wollman #endif
3899064799SGarrett Wollman 
3999064799SGarrett Wollman /*
4099064799SGarrett Wollman  * Copyright (c) 1984 by Sun Microsystems, Inc.
4199064799SGarrett Wollman  */
4299064799SGarrett Wollman 
438360efbdSAlfred Perlstein #include "namespace.h"
448360efbdSAlfred Perlstein #include <sys/types.h>
458360efbdSAlfred Perlstein 
468360efbdSAlfred Perlstein #include <netinet/in.h>
478360efbdSAlfred Perlstein #include <arpa/inet.h>
488360efbdSAlfred Perlstein 
498360efbdSAlfred Perlstein #include <assert.h>
508360efbdSAlfred Perlstein #include <netdb.h>
5199064799SGarrett Wollman #include <stdio.h>
524c3af266SPoul-Henning Kamp #include <stdlib.h>
5399064799SGarrett Wollman #include <string.h>
548360efbdSAlfred Perlstein 
5599064799SGarrett Wollman #include <rpc/rpc.h>
5699064799SGarrett Wollman #ifdef YP
5799064799SGarrett Wollman #include <rpcsvc/yp_prot.h>
5899064799SGarrett Wollman #include <rpcsvc/ypclnt.h>
5999064799SGarrett Wollman #endif
608360efbdSAlfred Perlstein #include "un-namespace.h"
6199064799SGarrett Wollman 
6299064799SGarrett Wollman /*
6399064799SGarrett Wollman  * Internet version.
6499064799SGarrett Wollman  */
658360efbdSAlfred Perlstein static struct rpcdata {
6699064799SGarrett Wollman 	FILE	*rpcf;
6799064799SGarrett Wollman 	int	stayopen;
6899064799SGarrett Wollman #define	MAXALIASES	35
6999064799SGarrett Wollman 	char	*rpc_aliases[MAXALIASES];
7099064799SGarrett Wollman 	struct	rpcent rpc;
7199064799SGarrett Wollman 	char	line[BUFSIZ+1];
7299064799SGarrett Wollman #ifdef	YP
7399064799SGarrett Wollman 	char	*domain;
7499064799SGarrett Wollman 	char	*current;
7599064799SGarrett Wollman 	int	currentlen;
7699064799SGarrett Wollman #endif
7799064799SGarrett Wollman } *rpcdata;
7899064799SGarrett Wollman 
798360efbdSAlfred Perlstein static	struct rpcent *interpret __P((char *val, size_t len));
808360efbdSAlfred Perlstein 
8199064799SGarrett Wollman #ifdef	YP
8299064799SGarrett Wollman static int	__yp_nomap = 0;
83f12d1a5dSJames Raynard extern int _yp_check(char **);
8499064799SGarrett Wollman #endif	/* YP */
8599064799SGarrett Wollman 
868360efbdSAlfred Perlstein #define	RPCDB	"/etc/rpc"
8799064799SGarrett Wollman 
888360efbdSAlfred Perlstein static struct rpcdata *_rpcdata __P((void));
8999064799SGarrett Wollman 
9099064799SGarrett Wollman static struct rpcdata *
9199064799SGarrett Wollman _rpcdata()
9299064799SGarrett Wollman {
938360efbdSAlfred Perlstein 	struct rpcdata *d = rpcdata;
9499064799SGarrett Wollman 
9599064799SGarrett Wollman 	if (d == 0) {
9699064799SGarrett Wollman 		d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
9799064799SGarrett Wollman 		rpcdata = d;
9899064799SGarrett Wollman 	}
9999064799SGarrett Wollman 	return (d);
10099064799SGarrett Wollman }
10199064799SGarrett Wollman 
10299064799SGarrett Wollman struct rpcent *
10399064799SGarrett Wollman getrpcbynumber(number)
1048360efbdSAlfred Perlstein 	int number;
10599064799SGarrett Wollman {
10699064799SGarrett Wollman #ifdef	YP
10799064799SGarrett Wollman 	int reason;
10899064799SGarrett Wollman 	char adrstr[16];
10999064799SGarrett Wollman #endif
1108360efbdSAlfred Perlstein 	struct rpcent *p;
1118360efbdSAlfred Perlstein 	struct rpcdata *d = _rpcdata();
11299064799SGarrett Wollman 
11399064799SGarrett Wollman 	if (d == 0)
11499064799SGarrett Wollman 		return (0);
11599064799SGarrett Wollman #ifdef	YP
11699064799SGarrett Wollman         if (!__yp_nomap && _yp_check(&d->domain)) {
11799064799SGarrett Wollman                 sprintf(adrstr, "%d", number);
11899064799SGarrett Wollman                 reason = yp_match(d->domain, "rpc.bynumber", adrstr, strlen(adrstr),
11999064799SGarrett Wollman                                   &d->current, &d->currentlen);
12099064799SGarrett Wollman                 switch(reason) {
12199064799SGarrett Wollman                 case 0:
12299064799SGarrett Wollman                         break;
12399064799SGarrett Wollman                 case YPERR_MAP:
12499064799SGarrett Wollman                         __yp_nomap = 1;
12599064799SGarrett Wollman                         goto no_yp;
12699064799SGarrett Wollman                         break;
12799064799SGarrett Wollman                 default:
12899064799SGarrett Wollman                         return(0);
12999064799SGarrett Wollman                         break;
13099064799SGarrett Wollman                 }
13199064799SGarrett Wollman                 d->current[d->currentlen] = '\0';
13299064799SGarrett Wollman                 p = interpret(d->current, d->currentlen);
13399064799SGarrett Wollman                 (void) free(d->current);
13499064799SGarrett Wollman                 return p;
13599064799SGarrett Wollman         }
13699064799SGarrett Wollman no_yp:
13799064799SGarrett Wollman #endif	/* YP */
1388360efbdSAlfred Perlstein 
13999064799SGarrett Wollman 	setrpcent(0);
1408360efbdSAlfred Perlstein 	while ((p = getrpcent()) != NULL) {
14199064799SGarrett Wollman 		if (p->r_number == number)
14299064799SGarrett Wollman 			break;
14399064799SGarrett Wollman 	}
14499064799SGarrett Wollman 	endrpcent();
14599064799SGarrett Wollman 	return (p);
14699064799SGarrett Wollman }
14799064799SGarrett Wollman 
14899064799SGarrett Wollman struct rpcent *
14999064799SGarrett Wollman getrpcbyname(name)
15099064799SGarrett Wollman 	char *name;
15199064799SGarrett Wollman {
152e7485a47SPeter Wemm 	struct rpcent *rpc = NULL;
15399064799SGarrett Wollman 	char **rp;
15499064799SGarrett Wollman 
1558360efbdSAlfred Perlstein 	assert(name != NULL);
1568360efbdSAlfred Perlstein 
15799064799SGarrett Wollman 	setrpcent(0);
1588360efbdSAlfred Perlstein 	while ((rpc = getrpcent()) != NULL) {
15999064799SGarrett Wollman 		if (strcmp(rpc->r_name, name) == 0)
160e7485a47SPeter Wemm 			goto done;
16199064799SGarrett Wollman 		for (rp = rpc->r_aliases; *rp != NULL; rp++) {
16299064799SGarrett Wollman 			if (strcmp(*rp, name) == 0)
163e7485a47SPeter Wemm 				goto done;
16499064799SGarrett Wollman 		}
16599064799SGarrett Wollman 	}
166e7485a47SPeter Wemm done:
16799064799SGarrett Wollman 	endrpcent();
168e7485a47SPeter Wemm 	return (rpc);
16999064799SGarrett Wollman }
17099064799SGarrett Wollman 
17199064799SGarrett Wollman void
17299064799SGarrett Wollman setrpcent(f)
17399064799SGarrett Wollman 	int f;
17499064799SGarrett Wollman {
1758360efbdSAlfred Perlstein 	struct rpcdata *d = _rpcdata();
17699064799SGarrett Wollman 
17799064799SGarrett Wollman 	if (d == 0)
17899064799SGarrett Wollman 		return;
17999064799SGarrett Wollman #ifdef	YP
18099064799SGarrett Wollman         if (!__yp_nomap && _yp_check(NULL)) {
18199064799SGarrett Wollman                 if (d->current)
18299064799SGarrett Wollman                         free(d->current);
18399064799SGarrett Wollman                 d->current = NULL;
18499064799SGarrett Wollman                 d->currentlen = 0;
18599064799SGarrett Wollman                 return;
18699064799SGarrett Wollman         }
18799064799SGarrett Wollman         __yp_nomap = 0;
18899064799SGarrett Wollman #endif	/* YP */
18999064799SGarrett Wollman 	if (d->rpcf == NULL)
19099064799SGarrett Wollman 		d->rpcf = fopen(RPCDB, "r");
19199064799SGarrett Wollman 	else
19299064799SGarrett Wollman 		rewind(d->rpcf);
19399064799SGarrett Wollman 	d->stayopen |= f;
19499064799SGarrett Wollman }
19599064799SGarrett Wollman 
19699064799SGarrett Wollman void
19799064799SGarrett Wollman endrpcent()
19899064799SGarrett Wollman {
1998360efbdSAlfred Perlstein 	struct rpcdata *d = _rpcdata();
20099064799SGarrett Wollman 
20199064799SGarrett Wollman 	if (d == 0)
20299064799SGarrett Wollman 		return;
20399064799SGarrett Wollman #ifdef	YP
20499064799SGarrett Wollman         if (!__yp_nomap && _yp_check(NULL)) {
20599064799SGarrett Wollman         	if (d->current && !d->stayopen)
20699064799SGarrett Wollman                         free(d->current);
20799064799SGarrett Wollman                 d->current = NULL;
20899064799SGarrett Wollman                 d->currentlen = 0;
20999064799SGarrett Wollman                 return;
21099064799SGarrett Wollman         }
21199064799SGarrett Wollman         __yp_nomap = 0;
21299064799SGarrett Wollman #endif	/* YP */
21399064799SGarrett Wollman 	if (d->rpcf && !d->stayopen) {
21499064799SGarrett Wollman 		fclose(d->rpcf);
21599064799SGarrett Wollman 		d->rpcf = NULL;
21699064799SGarrett Wollman 	}
21799064799SGarrett Wollman }
21899064799SGarrett Wollman 
21999064799SGarrett Wollman struct rpcent *
22099064799SGarrett Wollman getrpcent()
22199064799SGarrett Wollman {
2228360efbdSAlfred Perlstein 	struct rpcdata *d = _rpcdata();
22399064799SGarrett Wollman #ifdef	YP
224b2843ce2SJames Raynard 	struct rpcent *hp;
225b2843ce2SJames Raynard 	int reason;
2264c3af266SPoul-Henning Kamp 	char *val = NULL;
2274c3af266SPoul-Henning Kamp 	int vallen;
22899064799SGarrett Wollman #endif
22999064799SGarrett Wollman 
23099064799SGarrett Wollman 	if (d == 0)
23199064799SGarrett Wollman 		return(NULL);
23299064799SGarrett Wollman #ifdef	YP
23399064799SGarrett Wollman         if (!__yp_nomap && _yp_check(&d->domain)) {
23499064799SGarrett Wollman                 if (d->current == NULL && d->currentlen == 0) {
23599064799SGarrett Wollman                         reason = yp_first(d->domain, "rpc.bynumber",
23699064799SGarrett Wollman                                           &d->current, &d->currentlen,
23799064799SGarrett Wollman                                           &val, &vallen);
23899064799SGarrett Wollman                 } else {
23999064799SGarrett Wollman                         reason = yp_next(d->domain, "rpc.bynumber",
24099064799SGarrett Wollman                                          d->current, d->currentlen,
24199064799SGarrett Wollman                                          &d->current, &d->currentlen,
24299064799SGarrett Wollman                                          &val, &vallen);
24399064799SGarrett Wollman                 }
24499064799SGarrett Wollman                 switch(reason) {
24599064799SGarrett Wollman                 case 0:
24699064799SGarrett Wollman                         break;
24799064799SGarrett Wollman                 case YPERR_MAP:
24899064799SGarrett Wollman                         __yp_nomap = 1;
24999064799SGarrett Wollman                         goto no_yp;
25099064799SGarrett Wollman                         break;
25199064799SGarrett Wollman                 default:
25299064799SGarrett Wollman                         return(0);
25399064799SGarrett Wollman                         break;
25499064799SGarrett Wollman                 }
25599064799SGarrett Wollman                 val[vallen] = '\0';
25699064799SGarrett Wollman                 hp = interpret(val, vallen);
25799064799SGarrett Wollman                 (void) free(val);
25899064799SGarrett Wollman                 return hp;
25999064799SGarrett Wollman         }
26099064799SGarrett Wollman no_yp:
26199064799SGarrett Wollman #endif	/* YP */
26299064799SGarrett Wollman 	if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
26399064799SGarrett Wollman 		return (NULL);
264e7485a47SPeter Wemm 	/* -1 so there is room to append a \n below */
265e7485a47SPeter Wemm         if (fgets(d->line, BUFSIZ - 1, d->rpcf) == NULL)
26699064799SGarrett Wollman 		return (NULL);
26799064799SGarrett Wollman 	return (interpret(d->line, strlen(d->line)));
26899064799SGarrett Wollman }
26999064799SGarrett Wollman 
27099064799SGarrett Wollman static struct rpcent *
27199064799SGarrett Wollman interpret(val, len)
27299064799SGarrett Wollman 	char *val;
2738360efbdSAlfred Perlstein 	size_t len;
27499064799SGarrett Wollman {
2758360efbdSAlfred Perlstein 	struct rpcdata *d = _rpcdata();
27699064799SGarrett Wollman 	char *p;
2778360efbdSAlfred Perlstein 	char *cp, **q;
2788360efbdSAlfred Perlstein 
2798360efbdSAlfred Perlstein 	assert(val != NULL);
28099064799SGarrett Wollman 
28199064799SGarrett Wollman 	if (d == 0)
28299064799SGarrett Wollman 		return (0);
283e7485a47SPeter Wemm 	(void) strncpy(d->line, val, BUFSIZ);
284e7485a47SPeter Wemm 	d->line[BUFSIZ] = '\0';
28599064799SGarrett Wollman 	p = d->line;
286e7485a47SPeter Wemm 	p[len] = '\n';
28799064799SGarrett Wollman 	if (*p == '#')
28899064799SGarrett Wollman 		return (getrpcent());
28999064799SGarrett Wollman 	cp = strpbrk(p, "#\n");
29099064799SGarrett Wollman 	if (cp == NULL)
29199064799SGarrett Wollman 		return (getrpcent());
29299064799SGarrett Wollman 	*cp = '\0';
29399064799SGarrett Wollman 	cp = strpbrk(p, " \t");
29499064799SGarrett Wollman 	if (cp == NULL)
29599064799SGarrett Wollman 		return (getrpcent());
29699064799SGarrett Wollman 	*cp++ = '\0';
29799064799SGarrett Wollman 	/* THIS STUFF IS INTERNET SPECIFIC */
29899064799SGarrett Wollman 	d->rpc.r_name = d->line;
29999064799SGarrett Wollman 	while (*cp == ' ' || *cp == '\t')
30099064799SGarrett Wollman 		cp++;
30199064799SGarrett Wollman 	d->rpc.r_number = atoi(cp);
30299064799SGarrett Wollman 	q = d->rpc.r_aliases = d->rpc_aliases;
30399064799SGarrett Wollman 	cp = strpbrk(cp, " \t");
30499064799SGarrett Wollman 	if (cp != NULL)
30599064799SGarrett Wollman 		*cp++ = '\0';
30699064799SGarrett Wollman 	while (cp && *cp) {
30799064799SGarrett Wollman 		if (*cp == ' ' || *cp == '\t') {
30899064799SGarrett Wollman 			cp++;
30999064799SGarrett Wollman 			continue;
31099064799SGarrett Wollman 		}
31199064799SGarrett Wollman 		if (q < &(d->rpc_aliases[MAXALIASES - 1]))
31299064799SGarrett Wollman 			*q++ = cp;
31399064799SGarrett Wollman 		cp = strpbrk(cp, " \t");
31499064799SGarrett Wollman 		if (cp != NULL)
31599064799SGarrett Wollman 			*cp++ = '\0';
31699064799SGarrett Wollman 	}
31799064799SGarrett Wollman 	*q = NULL;
31899064799SGarrett Wollman 	return (&d->rpc);
31999064799SGarrett Wollman }
32099064799SGarrett Wollman 
321