getrpcent.c revision 1.24 1 1.24 mrg /* $NetBSD: getrpcent.c,v 1.24 2021/04/13 00:29:22 mrg Exp $ */
2 1.4 cgd
3 1.1 cgd /*
4 1.23 tron * Copyright (c) 2010, Oracle America, Inc.
5 1.1 cgd *
6 1.23 tron * Redistribution and use in source and binary forms, with or without
7 1.23 tron * modification, are permitted provided that the following conditions are
8 1.23 tron * met:
9 1.1 cgd *
10 1.23 tron * * Redistributions of source code must retain the above copyright
11 1.23 tron * notice, this list of conditions and the following disclaimer.
12 1.23 tron * * Redistributions in binary form must reproduce the above
13 1.23 tron * copyright notice, this list of conditions and the following
14 1.23 tron * disclaimer in the documentation and/or other materials
15 1.23 tron * provided with the distribution.
16 1.23 tron * * Neither the name of the "Oracle America, Inc." nor the names of its
17 1.23 tron * contributors may be used to endorse or promote products derived
18 1.23 tron * from this software without specific prior written permission.
19 1.1 cgd *
20 1.23 tron * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 1.23 tron * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 1.23 tron * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 1.23 tron * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 1.23 tron * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 1.23 tron * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.23 tron * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 1.23 tron * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.23 tron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.23 tron * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 1.23 tron * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 1.23 tron * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.7 christos #include <sys/cdefs.h>
35 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
36 1.7 christos #if 0
37 1.7 christos static char *sccsid = "@(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro";
38 1.7 christos #else
39 1.24 mrg __RCSID("$NetBSD: getrpcent.c,v 1.24 2021/04/13 00:29:22 mrg Exp $");
40 1.7 christos #endif
41 1.1 cgd #endif
42 1.1 cgd
43 1.1 cgd /*
44 1.1 cgd * Copyright (c) 1984 by Sun Microsystems, Inc.
45 1.1 cgd */
46 1.1 cgd
47 1.8 jtc #include "namespace.h"
48 1.11 lukem
49 1.11 lukem #include <sys/types.h>
50 1.11 lukem
51 1.11 lukem #include <netinet/in.h>
52 1.11 lukem #include <arpa/inet.h>
53 1.11 lukem
54 1.15 lukem #include <assert.h>
55 1.11 lukem #include <netdb.h>
56 1.1 cgd #include <stdio.h>
57 1.3 cgd #include <stdlib.h>
58 1.1 cgd #include <string.h>
59 1.11 lukem
60 1.1 cgd #include <rpc/rpc.h>
61 1.1 cgd
62 1.8 jtc #ifdef __weak_alias
63 1.17 mycroft __weak_alias(endrpcent,_endrpcent)
64 1.17 mycroft __weak_alias(getrpcbyname,_getrpcbyname)
65 1.17 mycroft __weak_alias(getrpcbynumber,_getrpcbynumber)
66 1.17 mycroft __weak_alias(getrpcent,_getrpcent)
67 1.17 mycroft __weak_alias(setrpcent,_setrpcent)
68 1.8 jtc #endif
69 1.8 jtc
70 1.1 cgd /*
71 1.1 cgd * Internet version.
72 1.1 cgd */
73 1.14 kleink static struct rpcdata {
74 1.1 cgd FILE *rpcf;
75 1.1 cgd int stayopen;
76 1.1 cgd #define MAXALIASES 35
77 1.1 cgd char *rpc_aliases[MAXALIASES];
78 1.1 cgd struct rpcent rpc;
79 1.1 cgd char line[BUFSIZ+1];
80 1.1 cgd } *rpcdata;
81 1.1 cgd
82 1.20 ginsbach static struct rpcent *interpret(char *val, size_t len);
83 1.1 cgd
84 1.12 mycroft #define RPCDB "/etc/rpc"
85 1.1 cgd
86 1.20 ginsbach static struct rpcdata *_rpcdata(void);
87 1.7 christos
88 1.1 cgd static struct rpcdata *
89 1.20 ginsbach _rpcdata(void)
90 1.1 cgd {
91 1.11 lukem struct rpcdata *d = rpcdata;
92 1.1 cgd
93 1.10 lukem if (d == 0) {
94 1.1 cgd d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
95 1.1 cgd rpcdata = d;
96 1.1 cgd }
97 1.1 cgd return (d);
98 1.1 cgd }
99 1.1 cgd
100 1.1 cgd struct rpcent *
101 1.20 ginsbach getrpcbynumber(int number)
102 1.1 cgd {
103 1.6 mycroft struct rpcent *rpc;
104 1.1 cgd
105 1.1 cgd setrpcent(0);
106 1.7 christos while ((rpc = getrpcent()) != NULL) {
107 1.6 mycroft if (rpc->r_number == number)
108 1.1 cgd break;
109 1.1 cgd }
110 1.1 cgd endrpcent();
111 1.6 mycroft return (rpc);
112 1.1 cgd }
113 1.1 cgd
114 1.1 cgd struct rpcent *
115 1.21 ginsbach getrpcbyname(const char *name)
116 1.1 cgd {
117 1.1 cgd struct rpcent *rpc;
118 1.1 cgd char **rp;
119 1.1 cgd
120 1.15 lukem _DIAGASSERT(name != NULL);
121 1.15 lukem
122 1.1 cgd setrpcent(0);
123 1.7 christos while ((rpc = getrpcent()) != NULL) {
124 1.1 cgd if (strcmp(rpc->r_name, name) == 0)
125 1.6 mycroft break;
126 1.1 cgd for (rp = rpc->r_aliases; *rp != NULL; rp++) {
127 1.1 cgd if (strcmp(*rp, name) == 0)
128 1.19 ginsbach goto found;
129 1.1 cgd }
130 1.1 cgd }
131 1.19 ginsbach found:
132 1.1 cgd endrpcent();
133 1.6 mycroft return (rpc);
134 1.1 cgd }
135 1.1 cgd
136 1.1 cgd void
137 1.20 ginsbach setrpcent(int f)
138 1.1 cgd {
139 1.11 lukem struct rpcdata *d = _rpcdata();
140 1.1 cgd
141 1.10 lukem if (d == 0)
142 1.1 cgd return;
143 1.1 cgd if (d->rpcf == NULL)
144 1.22 christos d->rpcf = fopen(RPCDB, "re");
145 1.1 cgd else
146 1.1 cgd rewind(d->rpcf);
147 1.1 cgd d->stayopen |= f;
148 1.1 cgd }
149 1.1 cgd
150 1.1 cgd void
151 1.20 ginsbach endrpcent(void)
152 1.1 cgd {
153 1.11 lukem struct rpcdata *d = _rpcdata();
154 1.1 cgd
155 1.10 lukem if (d == 0)
156 1.1 cgd return;
157 1.1 cgd if (d->rpcf && !d->stayopen) {
158 1.1 cgd fclose(d->rpcf);
159 1.1 cgd d->rpcf = NULL;
160 1.1 cgd }
161 1.1 cgd }
162 1.1 cgd
163 1.1 cgd struct rpcent *
164 1.20 ginsbach getrpcent(void)
165 1.1 cgd {
166 1.11 lukem struct rpcdata *d = _rpcdata();
167 1.1 cgd
168 1.10 lukem if (d == 0)
169 1.1 cgd return(NULL);
170 1.22 christos if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "re")) == NULL)
171 1.1 cgd return (NULL);
172 1.18 lukem if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
173 1.1 cgd return (NULL);
174 1.1 cgd return (interpret(d->line, strlen(d->line)));
175 1.1 cgd }
176 1.1 cgd
177 1.1 cgd static struct rpcent *
178 1.20 ginsbach interpret(char *val, size_t len)
179 1.1 cgd {
180 1.11 lukem struct rpcdata *d = _rpcdata();
181 1.1 cgd char *p;
182 1.11 lukem char *cp, **q;
183 1.15 lukem
184 1.15 lukem _DIAGASSERT(val != NULL);
185 1.1 cgd
186 1.10 lukem if (d == 0)
187 1.1 cgd return (0);
188 1.24 mrg strncpy(d->line, val, sizeof(d->line) - 1);
189 1.24 mrg d->line[sizeof(d->line) - 1] = '\0';
190 1.1 cgd p = d->line;
191 1.1 cgd d->line[len] = '\n';
192 1.1 cgd if (*p == '#')
193 1.1 cgd return (getrpcent());
194 1.1 cgd cp = strpbrk(p, "#\n");
195 1.1 cgd if (cp == NULL)
196 1.1 cgd return (getrpcent());
197 1.1 cgd *cp = '\0';
198 1.1 cgd cp = strpbrk(p, " \t");
199 1.1 cgd if (cp == NULL)
200 1.1 cgd return (getrpcent());
201 1.1 cgd *cp++ = '\0';
202 1.1 cgd /* THIS STUFF IS INTERNET SPECIFIC */
203 1.1 cgd d->rpc.r_name = d->line;
204 1.1 cgd while (*cp == ' ' || *cp == '\t')
205 1.1 cgd cp++;
206 1.1 cgd d->rpc.r_number = atoi(cp);
207 1.1 cgd q = d->rpc.r_aliases = d->rpc_aliases;
208 1.1 cgd cp = strpbrk(cp, " \t");
209 1.1 cgd if (cp != NULL)
210 1.1 cgd *cp++ = '\0';
211 1.1 cgd while (cp && *cp) {
212 1.1 cgd if (*cp == ' ' || *cp == '\t') {
213 1.1 cgd cp++;
214 1.1 cgd continue;
215 1.1 cgd }
216 1.1 cgd if (q < &(d->rpc_aliases[MAXALIASES - 1]))
217 1.1 cgd *q++ = cp;
218 1.1 cgd cp = strpbrk(cp, " \t");
219 1.1 cgd if (cp != NULL)
220 1.1 cgd *cp++ = '\0';
221 1.1 cgd }
222 1.1 cgd *q = NULL;
223 1.1 cgd return (&d->rpc);
224 1.1 cgd }
225