getrpcent.c revision 1.1 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.1 1993/10/07 07:29:52 cgd Exp $";
34 #endif
35
36 /*
37 * Copyright (c) 1984 by Sun Microsystems, Inc.
38 */
39
40 #include <stdio.h>
41 #include <sys/types.h>
42 #include <string.h>
43 #include <rpc/rpc.h>
44 #ifdef YP
45 #include <rpcsvc/yp_prot.h>
46 #include <rpcsvc/ypclnt.h>
47 #endif
48
49 /*
50 * Internet version.
51 */
52 struct rpcdata {
53 FILE *rpcf;
54 int stayopen;
55 #define MAXALIASES 35
56 char *rpc_aliases[MAXALIASES];
57 struct rpcent rpc;
58 char line[BUFSIZ+1];
59 #ifdef YP
60 char *domain;
61 char *current;
62 int currentlen;
63 #endif
64 } *rpcdata;
65
66 #ifdef YP
67 static int __yp_nomap = 0;
68 #endif /* YP */
69
70 static struct rpcent *interpret();
71 struct hostent *gethostent();
72 char *inet_ntoa();
73
74 static char RPCDB[] = "/etc/rpc";
75
76 static struct rpcdata *
77 _rpcdata()
78 {
79 register struct rpcdata *d = rpcdata;
80
81 if (d == 0) {
82 d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
83 rpcdata = d;
84 }
85 return (d);
86 }
87
88 struct rpcent *
89 getrpcbynumber(number)
90 register int number;
91 {
92 register struct rpcdata *d = _rpcdata();
93 register struct rpcent *p;
94 #ifdef YP
95 int reason;
96 char adrstr[16];
97 #endif
98
99 if (d == 0)
100 return (0);
101 #ifdef YP
102 if (!__yp_nomap && _yp_check(&d->domain)) {
103 sprintf(adrstr, "%d", number);
104 reason = yp_match(d->domain, "rpc.bynumber", adrstr, strlen(adrstr),
105 &d->current, &d->currentlen);
106 switch(reason) {
107 case 0:
108 break;
109 case YPERR_MAP:
110 __yp_nomap = 1;
111 goto no_yp;
112 break;
113 default:
114 return(0);
115 break;
116 }
117 d->current[d->currentlen] = '\0';
118 p = interpret(d->current, d->currentlen);
119 (void) free(d->current);
120 return p;
121 }
122 no_yp:
123 #endif /* YP */
124 setrpcent(0);
125 while (p = getrpcent()) {
126 if (p->r_number == number)
127 break;
128 }
129 endrpcent();
130 return (p);
131 }
132
133 struct rpcent *
134 getrpcbyname(name)
135 char *name;
136 {
137 struct rpcent *rpc;
138 char **rp;
139
140 setrpcent(0);
141 while (rpc = getrpcent()) {
142 if (strcmp(rpc->r_name, name) == 0)
143 return (rpc);
144 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
145 if (strcmp(*rp, name) == 0)
146 return (rpc);
147 }
148 }
149 endrpcent();
150 return (NULL);
151 }
152
153 void
154 setrpcent(f)
155 int f;
156 {
157 register struct rpcdata *d = _rpcdata();
158
159 if (d == 0)
160 return;
161 #ifdef YP
162 if (!__yp_nomap && _yp_check(NULL)) {
163 if (d->current)
164 free(d->current);
165 d->current = NULL;
166 d->currentlen = 0;
167 return;
168 }
169 __yp_nomap = 0;
170 #endif /* YP */
171 if (d->rpcf == NULL)
172 d->rpcf = fopen(RPCDB, "r");
173 else
174 rewind(d->rpcf);
175 d->stayopen |= f;
176 }
177
178 void
179 endrpcent()
180 {
181 register struct rpcdata *d = _rpcdata();
182
183 if (d == 0)
184 return;
185 #ifdef YP
186 if (!__yp_nomap && _yp_check(NULL)) {
187 if (d->current && !d->stayopen)
188 free(d->current);
189 d->current = NULL;
190 d->currentlen = 0;
191 return;
192 }
193 __yp_nomap = 0;
194 #endif /* YP */
195 if (d->rpcf && !d->stayopen) {
196 fclose(d->rpcf);
197 d->rpcf = NULL;
198 }
199 }
200
201 struct rpcent *
202 getrpcent()
203 {
204 struct rpcent *hp;
205 int reason;
206 register struct rpcdata *d = _rpcdata();
207 #ifdef YP
208 char *key = NULL, *val = NULL;
209 int keylen, vallen;
210 #endif
211
212 if (d == 0)
213 return(NULL);
214 #ifdef YP
215 if (!__yp_nomap && _yp_check(&d->domain)) {
216 if (d->current == NULL && d->currentlen == 0) {
217 reason = yp_first(d->domain, "rpc.bynumber",
218 &d->current, &d->currentlen,
219 &val, &vallen);
220 } else {
221 reason = yp_next(d->domain, "rpc.bynumber",
222 d->current, d->currentlen,
223 &d->current, &d->currentlen,
224 &val, &vallen);
225 }
226 switch(reason) {
227 case 0:
228 break;
229 case YPERR_MAP:
230 __yp_nomap = 1;
231 goto no_yp;
232 break;
233 default:
234 return(0);
235 break;
236 }
237 val[vallen] = '\0';
238 hp = interpret(val, vallen);
239 (void) free(val);
240 return hp;
241 }
242 no_yp:
243 #endif /* YP */
244 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
245 return (NULL);
246 if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
247 return (NULL);
248 return (interpret(d->line, strlen(d->line)));
249 }
250
251 static struct rpcent *
252 interpret(val, len)
253 char *val;
254 int len;
255 {
256 register struct rpcdata *d = _rpcdata();
257 char *p;
258 register char *cp, **q;
259
260 if (d == 0)
261 return (0);
262 (void) strncpy(d->line, val, len);
263 p = d->line;
264 d->line[len] = '\n';
265 if (*p == '#')
266 return (getrpcent());
267 cp = strpbrk(p, "#\n");
268 if (cp == NULL)
269 return (getrpcent());
270 *cp = '\0';
271 cp = strpbrk(p, " \t");
272 if (cp == NULL)
273 return (getrpcent());
274 *cp++ = '\0';
275 /* THIS STUFF IS INTERNET SPECIFIC */
276 d->rpc.r_name = d->line;
277 while (*cp == ' ' || *cp == '\t')
278 cp++;
279 d->rpc.r_number = atoi(cp);
280 q = d->rpc.r_aliases = d->rpc_aliases;
281 cp = strpbrk(cp, " \t");
282 if (cp != NULL)
283 *cp++ = '\0';
284 while (cp && *cp) {
285 if (*cp == ' ' || *cp == '\t') {
286 cp++;
287 continue;
288 }
289 if (q < &(d->rpc_aliases[MAXALIASES - 1]))
290 *q++ = cp;
291 cp = strpbrk(cp, " \t");
292 if (cp != NULL)
293 *cp++ = '\0';
294 }
295 *q = NULL;
296 return (&d->rpc);
297 }
298
299