503733d5e22efce6f4daf186dae18a5b88e37d89
[shibboleth/cpp-sp.git] / oncrpc / getrpcen.c
1 /*********************************************************************
2  * RPC for the Windows NT Operating System
3  * 1993 by Martin F. Gergeleit
4  * Users may use, copy or modify Sun RPC for the Windows NT Operating 
5  * System according to the Sun copyright below.
6  *
7  * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO 
8  * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE 
9  * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
10  *********************************************************************/
11
12 /* @(#)getrpcent.c      2.2 88/07/29 4.0 RPCSRC */
13 #if !defined(lint) && defined(SCCSIDS)
14 static  char sccsid[] = "@(#)getrpcent.c 1.9 87/08/11  Copyr 1984 Sun Micro";
15 #endif
16
17 /*
18  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
19  * unrestricted use provided that this legend is included on all tape
20  * media and as a part of the software program in whole or part.  Users
21  * may copy or modify Sun RPC without charge, but are not authorized
22  * to license or distribute it to anyone else except as part of a product or
23  * program developed by the user.
24  * 
25  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
26  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
27  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
28  * 
29  * Sun RPC is provided with no support and without any obligation on the
30  * part of Sun Microsystems, Inc. to assist in its use, correction,
31  * modification or enhancement.
32  * 
33  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
34  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
35  * OR ANY PART THEREOF.
36  * 
37  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
38  * or profits or other special, indirect and consequential damages, even if
39  * Sun has been advised of the possibility of such damages.
40  * 
41  * Sun Microsystems, Inc.
42  * 2550 Garcia Avenue
43  * Mountain View, California  94043
44  */
45
46 /*
47  * Copyright (c) 1985 by Sun Microsystems, Inc.
48  */
49
50 #include <stdio.h>
51 #include <sys/types.h>
52 #include <rpc/rpc.h>
53 #ifndef WIN32
54 #include <netdb.h>
55 #include <sys/socket.h>
56 #endif
57
58 // eventually we might be able to support autoconf via cygwin...
59 #if defined (_MSC_VER) || defined(__BORLANDC__)
60 # include "config_win32.h"
61 #else
62 # include "config.h"
63 #endif
64
65 #ifndef HAVE_STRUCT_RPCENT
66 /* routines for parsing /etc/rpc */
67 #include <rpc/netdb.h>          /* structures and routines to parse /etc/rpc */
68 #endif
69
70 /*
71  * Internet version.
72  */
73 struct rpcdata {
74         FILE    *rpcf;
75         char    *current;
76         int     currentlen;
77         int     stayopen;
78 #define MAXALIASES      35
79         char    *rpc_aliases[MAXALIASES];
80         struct  rpcent rpc;
81         char    line[BUFSIZ+1];
82         char    *domain;
83 } *rpcdata, *_rpcdata();
84
85 static  struct rpcent *interpret();
86 struct  hostent *gethostent();
87 #ifdef WIN32
88 #define index(str,ch) strchr(str,ch)
89 #else
90 char    *inet_ntoa();
91 static  char *index();
92 #endif
93
94 #ifdef WIN32
95 static char RPCDB[1024];
96 #else
97 static char RPCDB[] = "/etc/rpc";
98 #endif
99
100 static struct rpcdata *
101 _rpcdata()
102 {
103         register struct rpcdata *d = rpcdata;
104
105 #ifdef WIN32
106         char *str;
107
108         if ((RPCDB[0] == '\0') && (str = getenv("SystemRoot"))) {
109                 strcpy(RPCDB, str);
110                 strcat(RPCDB, "\\system32\\drivers\\etc\\rpc");
111         }
112 #endif
113         
114         if (d == 0) {
115                 d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
116                 rpcdata = d;
117         }
118         return (d);
119 }
120
121 struct rpcent *
122 getrpcbynumber(number)
123         register int number;
124 {
125         register struct rpcdata *d = _rpcdata();
126         register struct rpcent *p;
127         int reason;
128         char adrstr[16], *val = NULL;
129         int vallen;
130
131         if (d == 0)
132                 return (0);
133         setrpcent(0);
134         while (p = getrpcent()) {
135                 if (p->r_number == number)
136                         break;
137         }
138         endrpcent();
139         return (p);
140 }
141
142 struct rpcent *
143 getrpcbyname(name)
144         char *name;
145 {
146         struct rpcent *rpc;
147         char **rp;
148
149         setrpcent(0);
150         while(rpc = getrpcent()) {
151                 if (strcmp(rpc->r_name, name) == 0)
152                         return (rpc);
153                 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
154                         if (strcmp(*rp, name) == 0)
155                                 return (rpc);
156                 }
157         }
158         endrpcent();
159         return (NULL);
160 }
161
162 setrpcent(f)
163         int f;
164 {
165         register struct rpcdata *d = _rpcdata();
166
167         if (d == 0)
168                 return;
169         if (d->rpcf == NULL)
170                 d->rpcf = fopen(RPCDB, "r");
171         else
172                 rewind(d->rpcf);
173         if (d->current)
174                 free(d->current);
175         d->current = NULL;
176         d->stayopen |= f;
177 }
178
179 endrpcent()
180 {
181         register struct rpcdata *d = _rpcdata();
182
183         if (d == 0)
184                 return;
185         if (d->current && !d->stayopen) {
186                 free(d->current);
187                 d->current = NULL;
188         }
189         if (d->rpcf && !d->stayopen) {
190                 fclose(d->rpcf);
191                 d->rpcf = NULL;
192         }
193 }
194
195 struct rpcent *
196 getrpcent()
197 {
198         struct rpcent *hp;
199         int reason;
200         char *key = NULL, *val = NULL;
201         int keylen, vallen;
202         register struct rpcdata *d = _rpcdata();
203
204         if (d == 0)
205                 return(NULL);
206         if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
207                 return (NULL);
208     if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
209                 return (NULL);
210         return interpret(d->line, strlen(d->line));
211 }
212
213 static struct rpcent *
214 interpret(val, len)
215         char *val;
216 {
217         register struct rpcdata *d = _rpcdata();
218         char *p;
219         register char *cp, **q;
220
221         if (d == 0)
222                 return NULL;
223         strncpy(d->line, val, len);
224         p = d->line;
225         d->line[len] = '\n';
226         if (*p == '#')
227                 return (getrpcent());
228         cp = index(p, '#');
229         if (cp == NULL)
230     {
231                 cp = index(p, '\n');
232                 if (cp == NULL)
233                         return (getrpcent());
234         }
235         *cp = '\0';
236         cp = index(p, ' ');
237         if (cp == NULL)
238     {
239                 cp = index(p, '\t');
240                 if (cp == NULL)
241                         return (getrpcent());
242         }
243         *cp++ = '\0';
244         /* THIS STUFF IS INTERNET SPECIFIC */
245         d->rpc.r_name = d->line;
246         while (*cp == ' ' || *cp == '\t')
247                 cp++;
248         d->rpc.r_number = atoi(cp);
249         q = d->rpc.r_aliases = d->rpc_aliases;
250         cp = index(p, ' ');
251         if (cp != NULL)
252                 *cp++ = '\0';
253         else
254     {
255                 cp = index(p, '\t');
256                 if (cp != NULL)
257                         *cp++ = '\0';
258         }
259         while (cp && *cp) {
260                 if (*cp == ' ' || *cp == '\t') {
261                         cp++;
262                         continue;
263                 }
264                 if (q < &(d->rpc_aliases[MAXALIASES - 1]))
265                         *q++ = cp;
266                 cp = index(p, ' ');
267                 if (cp != NULL)
268                         *cp++ = '\0';
269                 else
270             {
271                         cp = index(p, '\t');
272                         if (cp != NULL)
273                                 *cp++ = '\0';
274                 }
275         }
276         *q = NULL;
277         return (&d->rpc);
278 }