Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/getrpcent.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2009, Sun Microsystems, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Copyright (c) 1984 by Sun Microsystems, Inc.
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <sys/types.h>
38
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41
42 #include <assert.h>
43 #include <netdb.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47
48 #include <rpc/rpc.h>
49 #ifdef YP
50 #include <rpcsvc/yp_prot.h>
51 #include <rpcsvc/ypclnt.h>
52 #endif
53 #if defined(__FreeBSD__) || defined(__NetBSD__)
54 #include <libc_private.h>
55 #endif
56
57 #if HAVE_CONFIG_H
58 #include "config.h"
59 #endif
60
61 #if !HAVE_GETRPCBYNAME || !HAVE_GETRPCBYNUMBER || \
62 !HAVE_SETRPCENT || !HAVE_ENDRPCENT || !HAVE_GETRPCENT
63
64 /*
65 * Internet version.
66 */
67 static struct rpcdata {
68 FILE *rpcf;
69 int stayopen;
70 #define MAXALIASES 35
71 char *rpc_aliases[MAXALIASES];
72 struct rpcent rpc;
73 char line[BUFSIZ+1];
74 #ifdef YP
75 char *domain;
76 char *current;
77 int currentlen;
78 #endif
79 } *rpcdata;
80
81 static struct rpcent *interpret(char *val, size_t len);
82
83 #ifdef YP
84 static int __yp_nomap = 0;
85 #endif /* YP */
86
87 #define RPCDB "/etc/rpc"
88
89 static struct rpcdata *_rpcdata(void);
90
91 static struct rpcdata *
92 _rpcdata()
93 {
94 struct rpcdata *d = rpcdata;
95
96 if (d == 0) {
97 d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
98 rpcdata = d;
99 }
100 return (d);
101 }
102
103 #if !HAVE_GETRPCBYNUMBER
104 struct rpcent *
105 getrpcbynumber(number)
106 int number;
107 {
108 #ifdef YP
109 int reason;
110 char adrstr[16];
111 #endif
112 struct rpcent *p;
113 struct rpcdata *d = _rpcdata();
114
115 if (d == 0)
116 return (0);
117 #ifdef YP
118 if (!__yp_nomap && __yp_check(&d->domain)) {
119 sprintf(adrstr, "%d", number);
120 reason = yp_match(d->domain, "rpc.bynumber", adrstr, strlen(adrstr),
121 &d->current, &d->currentlen);
122 switch(reason) {
123 case 0:
124 break;
125 case YPERR_MAP:
126 __yp_nomap = 1;
127 goto no_yp;
128 break;
129 default:
130 return(0);
131 break;
132 }
133 d->current[d->currentlen] = '\0';
134 p = interpret(d->current, d->currentlen);
135 (void) free(d->current);
136 return p;
137 }
138 no_yp:
139 #endif /* YP */
140
141 setrpcent(0);
142 while ((p = getrpcent()) != NULL) {
143 if (p->r_number == number)
144 break;
145 }
146 endrpcent();
147 return (p);
148 }
149 #endif /* !HAVE_GETRPCBYNUMBER */
150
151 #if !HAVE_GETRPCBYNAME
152 struct rpcent *
153 getrpcbyname(name)
154 const char *name;
155 {
156 struct rpcent *rpc = NULL;
157 char **rp;
158
159 assert(name != NULL);
160
161 setrpcent(0);
162 while ((rpc = getrpcent()) != NULL) {
163 if (strcmp(rpc->r_name, name) == 0)
164 goto done;
165 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
166 if (strcmp(*rp, name) == 0)
167 goto done;
168 }
169 }
170 done:
171 endrpcent();
172 return (rpc);
173 }
174 #endif /* !HAVE_GETRPCBYNAME */
175
176 #if !HAVE_SETRPCENT
177 void
178 setrpcent(f)
179 int f;
180 {
181 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)
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 == NULL)
196 d->rpcf = fopen(RPCDB, "r");
197 else
198 rewind(d->rpcf);
199 d->stayopen |= f;
200 }
201 #endif
202
203 #if !HAVE_ENDRPCENT
204 void
205 endrpcent()
206 {
207 struct rpcdata *d = _rpcdata();
208
209 if (d == 0)
210 return;
211 #ifdef YP
212 if (!__yp_nomap && __yp_check(NULL)) {
213 if (d->current && !d->stayopen)
214 free(d->current);
215 d->current = NULL;
216 d->currentlen = 0;
217 return;
218 }
219 __yp_nomap = 0;
220 #endif /* YP */
221 if (d->rpcf && !d->stayopen) {
222 fclose(d->rpcf);
223 d->rpcf = NULL;
224 }
225 }
226 #endif
227
228 #if !HAVE_GETRPCENT
229 struct rpcent *
230 getrpcent()
231 {
232 struct rpcdata *d = _rpcdata();
233 #ifdef YP
234 struct rpcent *hp;
235 int reason;
236 char *val = NULL;
237 int vallen;
238 #endif
239
240 if (d == 0)
241 return(NULL);
242 #ifdef YP
243 if (!__yp_nomap && __yp_check(&d->domain)) {
244 if (d->current == NULL && d->currentlen == 0) {
245 reason = yp_first(d->domain, "rpc.bynumber",
246 &d->current, &d->currentlen,
247 &val, &vallen);
248 } else {
249 reason = yp_next(d->domain, "rpc.bynumber",
250 d->current, d->currentlen,
251 &d->current, &d->currentlen,
252 &val, &vallen);
253 }
254 switch(reason) {
255 case 0:
256 break;
257 case YPERR_MAP:
258 __yp_nomap = 1;
259 goto no_yp;
260 break;
261 default:
262 return(0);
263 break;
264 }
265 val[vallen] = '\0';
266 hp = interpret(val, vallen);
267 (void) free(val);
268 return hp;
269 }
270 no_yp:
271 #endif /* YP */
272 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
273 return (NULL);
274 /* -1 so there is room to append a \n below */
275 if (fgets(d->line, BUFSIZ - 1, d->rpcf) == NULL)
276 return (NULL);
277 return (interpret(d->line, strlen(d->line)));
278 }
279 #endif
280
281 static struct rpcent *
282 interpret(val, len)
283 char *val;
284 size_t len;
285 {
286 struct rpcdata *d = _rpcdata();
287 char *p;
288 char *cp, **q;
289
290 assert(val != NULL);
291
292 if (d == 0)
293 return (0);
294 (void) strncpy(d->line, val, BUFSIZ);
295 d->line[BUFSIZ] = '\0';
296 p = d->line;
297 p[len] = '\n';
298 if (*p == '#')
299 return (getrpcent());
300 cp = strpbrk(p, "#\n");
301 if (cp == NULL)
302 return (getrpcent());
303 *cp = '\0';
304 cp = strpbrk(p, " \t");
305 if (cp == NULL)
306 return (getrpcent());
307 *cp++ = '\0';
308 /* THIS STUFF IS INTERNET SPECIFIC */
309 d->rpc.r_name = d->line;
310 while (*cp == ' ' || *cp == '\t')
311 cp++;
312 d->rpc.r_number = atoi(cp);
313 q = d->rpc.r_aliases = d->rpc_aliases;
314 cp = strpbrk(cp, " \t");
315 if (cp != NULL)
316 *cp++ = '\0';
317 while (cp && *cp) {
318 if (*cp == ' ' || *cp == '\t') {
319 cp++;
320 continue;
321 }
322 if (q < &(d->rpc_aliases[MAXALIASES - 1]))
323 *q++ = cp;
324 cp = strpbrk(cp, " \t");
325 if (cp != NULL)
326 *cp++ = '\0';
327 }
328 *q = NULL;
329 return (&d->rpc);
330 }
331
332 #endif