xref: /netbsd-src/lib/libc/rpc/getrpcent.c (revision 02402fe1109cfd16f303a0d24555f5fd199d8e62)
1*02402fe1Smrg /*	$NetBSD: getrpcent.c,v 1.24 2021/04/13 00:29:22 mrg Exp $	*/
29e15c989Scgd 
363d7b677Scgd /*
447c0e0c3Stron  * Copyright (c) 2010, Oracle America, Inc.
563d7b677Scgd  *
647c0e0c3Stron  * Redistribution and use in source and binary forms, with or without
747c0e0c3Stron  * modification, are permitted provided that the following conditions are
847c0e0c3Stron  * met:
963d7b677Scgd  *
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.
1963d7b677Scgd  *
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.
3263d7b677Scgd  */
3363d7b677Scgd 
34c63c52b2Schristos #include <sys/cdefs.h>
3563d7b677Scgd #if defined(LIBC_SCCS) && !defined(lint)
36c63c52b2Schristos #if 0
37c63c52b2Schristos static char *sccsid = "@(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro";
38c63c52b2Schristos #else
39*02402fe1Smrg __RCSID("$NetBSD: getrpcent.c,v 1.24 2021/04/13 00:29:22 mrg Exp $");
40c63c52b2Schristos #endif
4163d7b677Scgd #endif
4263d7b677Scgd 
4363d7b677Scgd /*
4463d7b677Scgd  * Copyright (c) 1984 by Sun Microsystems, Inc.
4563d7b677Scgd  */
4663d7b677Scgd 
4743fa6fe3Sjtc #include "namespace.h"
4846e6c5e8Slukem 
4946e6c5e8Slukem #include <sys/types.h>
5046e6c5e8Slukem 
5146e6c5e8Slukem #include <netinet/in.h>
5246e6c5e8Slukem #include <arpa/inet.h>
5346e6c5e8Slukem 
54b48252f3Slukem #include <assert.h>
5546e6c5e8Slukem #include <netdb.h>
5663d7b677Scgd #include <stdio.h>
572e2a3a25Scgd #include <stdlib.h>
5863d7b677Scgd #include <string.h>
5946e6c5e8Slukem 
6063d7b677Scgd #include <rpc/rpc.h>
6163d7b677Scgd 
6243fa6fe3Sjtc #ifdef __weak_alias
6360549036Smycroft __weak_alias(endrpcent,_endrpcent)
6460549036Smycroft __weak_alias(getrpcbyname,_getrpcbyname)
6560549036Smycroft __weak_alias(getrpcbynumber,_getrpcbynumber)
6660549036Smycroft __weak_alias(getrpcent,_getrpcent)
6760549036Smycroft __weak_alias(setrpcent,_setrpcent)
6843fa6fe3Sjtc #endif
6943fa6fe3Sjtc 
7063d7b677Scgd /*
7163d7b677Scgd  * Internet version.
7263d7b677Scgd  */
7394281e53Skleink static struct rpcdata {
7463d7b677Scgd 	FILE	*rpcf;
7563d7b677Scgd 	int	stayopen;
7663d7b677Scgd #define	MAXALIASES	35
7763d7b677Scgd 	char	*rpc_aliases[MAXALIASES];
7863d7b677Scgd 	struct	rpcent rpc;
7963d7b677Scgd 	char	line[BUFSIZ+1];
8063d7b677Scgd } *rpcdata;
8163d7b677Scgd 
82ca4f974aSginsbach static	struct rpcent *interpret(char *val, size_t len);
8363d7b677Scgd 
8400eb97ddSmycroft #define	RPCDB	"/etc/rpc"
8563d7b677Scgd 
86ca4f974aSginsbach static struct rpcdata *_rpcdata(void);
87c63c52b2Schristos 
8863d7b677Scgd static struct rpcdata *
_rpcdata(void)89ca4f974aSginsbach _rpcdata(void)
9063d7b677Scgd {
9146e6c5e8Slukem 	struct rpcdata *d = rpcdata;
9263d7b677Scgd 
93ce147c1cSlukem 	if (d == 0) {
9463d7b677Scgd 		d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
9563d7b677Scgd 		rpcdata = d;
9663d7b677Scgd 	}
9763d7b677Scgd 	return (d);
9863d7b677Scgd }
9963d7b677Scgd 
10063d7b677Scgd struct rpcent *
getrpcbynumber(int number)101ca4f974aSginsbach getrpcbynumber(int number)
10263d7b677Scgd {
10313038ab3Smycroft 	struct rpcent *rpc;
10463d7b677Scgd 
10563d7b677Scgd 	setrpcent(0);
106c63c52b2Schristos 	while ((rpc = getrpcent()) != NULL) {
10713038ab3Smycroft 		if (rpc->r_number == number)
10863d7b677Scgd 			break;
10963d7b677Scgd 	}
11063d7b677Scgd 	endrpcent();
11113038ab3Smycroft 	return (rpc);
11263d7b677Scgd }
11363d7b677Scgd 
11463d7b677Scgd struct rpcent *
getrpcbyname(const char * name)115ebcc92c1Sginsbach getrpcbyname(const char *name)
11663d7b677Scgd {
11763d7b677Scgd 	struct rpcent *rpc;
11863d7b677Scgd 	char **rp;
11963d7b677Scgd 
120b48252f3Slukem 	_DIAGASSERT(name != NULL);
121b48252f3Slukem 
12263d7b677Scgd 	setrpcent(0);
123c63c52b2Schristos 	while ((rpc = getrpcent()) != NULL) {
12463d7b677Scgd 		if (strcmp(rpc->r_name, name) == 0)
12513038ab3Smycroft 			break;
12663d7b677Scgd 		for (rp = rpc->r_aliases; *rp != NULL; rp++) {
12763d7b677Scgd 			if (strcmp(*rp, name) == 0)
1289af61ddeSginsbach 				goto found;
12963d7b677Scgd 		}
13063d7b677Scgd 	}
1319af61ddeSginsbach found:
13263d7b677Scgd 	endrpcent();
13313038ab3Smycroft 	return (rpc);
13463d7b677Scgd }
13563d7b677Scgd 
13663d7b677Scgd void
setrpcent(int f)137ca4f974aSginsbach setrpcent(int f)
13863d7b677Scgd {
13946e6c5e8Slukem 	struct rpcdata *d = _rpcdata();
14063d7b677Scgd 
141ce147c1cSlukem 	if (d == 0)
14263d7b677Scgd 		return;
14363d7b677Scgd 	if (d->rpcf == NULL)
1449292cfb2Schristos 		d->rpcf = fopen(RPCDB, "re");
14563d7b677Scgd 	else
14663d7b677Scgd 		rewind(d->rpcf);
14763d7b677Scgd 	d->stayopen |= f;
14863d7b677Scgd }
14963d7b677Scgd 
15063d7b677Scgd void
endrpcent(void)151ca4f974aSginsbach endrpcent(void)
15263d7b677Scgd {
15346e6c5e8Slukem 	struct rpcdata *d = _rpcdata();
15463d7b677Scgd 
155ce147c1cSlukem 	if (d == 0)
15663d7b677Scgd 		return;
15763d7b677Scgd 	if (d->rpcf && !d->stayopen) {
15863d7b677Scgd 		fclose(d->rpcf);
15963d7b677Scgd 		d->rpcf = NULL;
16063d7b677Scgd 	}
16163d7b677Scgd }
16263d7b677Scgd 
16363d7b677Scgd struct rpcent *
getrpcent(void)164ca4f974aSginsbach getrpcent(void)
16563d7b677Scgd {
16646e6c5e8Slukem 	struct rpcdata *d = _rpcdata();
16763d7b677Scgd 
168ce147c1cSlukem 	if (d == 0)
16963d7b677Scgd 		return(NULL);
1709292cfb2Schristos 	if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "re")) == NULL)
17163d7b677Scgd 		return (NULL);
17263d7b677Scgd 	if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
17363d7b677Scgd 		return (NULL);
17463d7b677Scgd 	return (interpret(d->line, strlen(d->line)));
17563d7b677Scgd }
17663d7b677Scgd 
17763d7b677Scgd static struct rpcent *
interpret(char * val,size_t len)178ca4f974aSginsbach interpret(char *val, size_t len)
17963d7b677Scgd {
18046e6c5e8Slukem 	struct rpcdata *d = _rpcdata();
18163d7b677Scgd 	char *p;
18246e6c5e8Slukem 	char *cp, **q;
18363d7b677Scgd 
184b48252f3Slukem 	_DIAGASSERT(val != NULL);
185b48252f3Slukem 
186ce147c1cSlukem 	if (d == 0)
18763d7b677Scgd 		return (0);
188*02402fe1Smrg 	strncpy(d->line, val, sizeof(d->line) - 1);
189*02402fe1Smrg 	d->line[sizeof(d->line) - 1] = '\0';
19063d7b677Scgd 	p = d->line;
19163d7b677Scgd 	d->line[len] = '\n';
19263d7b677Scgd 	if (*p == '#')
19363d7b677Scgd 		return (getrpcent());
19463d7b677Scgd 	cp = strpbrk(p, "#\n");
19563d7b677Scgd 	if (cp == NULL)
19663d7b677Scgd 		return (getrpcent());
19763d7b677Scgd 	*cp = '\0';
19863d7b677Scgd 	cp = strpbrk(p, " \t");
19963d7b677Scgd 	if (cp == NULL)
20063d7b677Scgd 		return (getrpcent());
20163d7b677Scgd 	*cp++ = '\0';
20263d7b677Scgd 	/* THIS STUFF IS INTERNET SPECIFIC */
20363d7b677Scgd 	d->rpc.r_name = d->line;
20463d7b677Scgd 	while (*cp == ' ' || *cp == '\t')
20563d7b677Scgd 		cp++;
20663d7b677Scgd 	d->rpc.r_number = atoi(cp);
20763d7b677Scgd 	q = d->rpc.r_aliases = d->rpc_aliases;
20863d7b677Scgd 	cp = strpbrk(cp, " \t");
20963d7b677Scgd 	if (cp != NULL)
21063d7b677Scgd 		*cp++ = '\0';
21163d7b677Scgd 	while (cp && *cp) {
21263d7b677Scgd 		if (*cp == ' ' || *cp == '\t') {
21363d7b677Scgd 			cp++;
21463d7b677Scgd 			continue;
21563d7b677Scgd 		}
21663d7b677Scgd 		if (q < &(d->rpc_aliases[MAXALIASES - 1]))
21763d7b677Scgd 			*q++ = cp;
21863d7b677Scgd 		cp = strpbrk(cp, " \t");
21963d7b677Scgd 		if (cp != NULL)
22063d7b677Scgd 			*cp++ = '\0';
22163d7b677Scgd 	}
22263d7b677Scgd 	*q = NULL;
22363d7b677Scgd 	return (&d->rpc);
22463d7b677Scgd }
225