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