1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * SPDX-License-Identifier: BSD-3-Clause |
---|
5 | * |
---|
6 | * Copyright (c) 2009, Sun Microsystems, Inc. |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions are met: |
---|
11 | * - Redistributions of source code must retain the above copyright notice, |
---|
12 | * this list of conditions and the following disclaimer. |
---|
13 | * - Redistributions in binary form must reproduce the above copyright notice, |
---|
14 | * this list of conditions and the following disclaimer in the documentation |
---|
15 | * and/or other materials provided with the distribution. |
---|
16 | * - Neither the name of Sun Microsystems, Inc. nor the names of its |
---|
17 | * contributors may be used to endorse or promote products derived |
---|
18 | * from this software without specific prior written permission. |
---|
19 | * |
---|
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
30 | * POSSIBILITY OF SUCH DAMAGE. |
---|
31 | */ |
---|
32 | |
---|
33 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
34 | static char sccsid[] = "@(#)netnamer.c 1.13 91/03/11 Copyr 1986 Sun Micro"; |
---|
35 | #endif |
---|
36 | #include <sys/cdefs.h> |
---|
37 | __FBSDID("$FreeBSD$"); |
---|
38 | |
---|
39 | /* |
---|
40 | * netname utility routines convert from unix names to network names and |
---|
41 | * vice-versa This module is operating system dependent! What we define here |
---|
42 | * will work with any unix system that has adopted the sun NIS domain |
---|
43 | * architecture. |
---|
44 | */ |
---|
45 | #include "namespace.h" |
---|
46 | #include <sys/param.h> |
---|
47 | #include <rpc/rpc.h> |
---|
48 | #include <rpc/rpc_com.h> |
---|
49 | #ifdef YP |
---|
50 | #include <rpcsvc/yp_prot.h> |
---|
51 | #include <rpcsvc/ypclnt.h> |
---|
52 | #endif |
---|
53 | #include <ctype.h> |
---|
54 | #include <stdio.h> |
---|
55 | #include <grp.h> |
---|
56 | #include <pwd.h> |
---|
57 | #include <string.h> |
---|
58 | #include <stdlib.h> |
---|
59 | #include <unistd.h> |
---|
60 | #include "un-namespace.h" |
---|
61 | |
---|
62 | static char *OPSYS = "unix"; |
---|
63 | #ifdef YP |
---|
64 | static char *NETID = "netid.byname"; |
---|
65 | #endif |
---|
66 | static char *NETIDFILE = "/etc/netid"; |
---|
67 | |
---|
68 | static int getnetid( char *, char * ); |
---|
69 | static int _getgroups( char *, gid_t * ); |
---|
70 | |
---|
71 | /* |
---|
72 | * Convert network-name into unix credential |
---|
73 | */ |
---|
74 | int |
---|
75 | netname2user(char netname[MAXNETNAMELEN + 1], uid_t *uidp, gid_t *gidp, |
---|
76 | int *gidlenp, gid_t *gidlist) |
---|
77 | { |
---|
78 | char *p; |
---|
79 | int gidlen; |
---|
80 | uid_t uid; |
---|
81 | long luid; |
---|
82 | struct passwd *pwd; |
---|
83 | char val[1024]; |
---|
84 | char *val1, *val2; |
---|
85 | char *domain; |
---|
86 | int vallen; |
---|
87 | int err; |
---|
88 | |
---|
89 | if (getnetid(netname, val)) { |
---|
90 | char *res = val; |
---|
91 | |
---|
92 | p = strsep(&res, ":"); |
---|
93 | if (p == NULL) |
---|
94 | return (0); |
---|
95 | *uidp = (uid_t) atol(p); |
---|
96 | p = strsep(&res, "\n,"); |
---|
97 | if (p == NULL) { |
---|
98 | return (0); |
---|
99 | } |
---|
100 | *gidp = (gid_t) atol(p); |
---|
101 | for (gidlen = 0; gidlen < NGRPS; gidlen++) { |
---|
102 | p = strsep(&res, "\n,"); |
---|
103 | if (p == NULL) |
---|
104 | break; |
---|
105 | gidlist[gidlen] = (gid_t) atol(p); |
---|
106 | } |
---|
107 | *gidlenp = gidlen; |
---|
108 | |
---|
109 | return (1); |
---|
110 | } |
---|
111 | val1 = strchr(netname, '.'); |
---|
112 | if (val1 == NULL) |
---|
113 | return (0); |
---|
114 | if (strncmp(netname, OPSYS, (val1-netname))) |
---|
115 | return (0); |
---|
116 | val1++; |
---|
117 | val2 = strchr(val1, '@'); |
---|
118 | if (val2 == NULL) |
---|
119 | return (0); |
---|
120 | vallen = val2 - val1; |
---|
121 | if (vallen > (1024 - 1)) |
---|
122 | vallen = 1024 - 1; |
---|
123 | (void) strncpy(val, val1, 1024); |
---|
124 | val[vallen] = 0; |
---|
125 | |
---|
126 | err = __rpc_get_default_domain(&domain); /* change to rpc */ |
---|
127 | if (err) |
---|
128 | return (0); |
---|
129 | |
---|
130 | if (strcmp(val2 + 1, domain)) |
---|
131 | return (0); /* wrong domain */ |
---|
132 | |
---|
133 | if (sscanf(val, "%ld", &luid) != 1) |
---|
134 | return (0); |
---|
135 | uid = luid; |
---|
136 | |
---|
137 | /* use initgroups method */ |
---|
138 | pwd = getpwuid(uid); |
---|
139 | if (pwd == NULL) |
---|
140 | return (0); |
---|
141 | *uidp = pwd->pw_uid; |
---|
142 | *gidp = pwd->pw_gid; |
---|
143 | *gidlenp = _getgroups(pwd->pw_name, gidlist); |
---|
144 | return (1); |
---|
145 | } |
---|
146 | |
---|
147 | /* |
---|
148 | * initgroups |
---|
149 | */ |
---|
150 | |
---|
151 | static int |
---|
152 | _getgroups(char *uname, gid_t groups[NGRPS]) |
---|
153 | { |
---|
154 | gid_t ngroups = 0; |
---|
155 | struct group *grp; |
---|
156 | int i; |
---|
157 | int j; |
---|
158 | int filter; |
---|
159 | |
---|
160 | setgrent(); |
---|
161 | while ((grp = getgrent())) { |
---|
162 | for (i = 0; grp->gr_mem[i]; i++) |
---|
163 | if (!strcmp(grp->gr_mem[i], uname)) { |
---|
164 | if (ngroups == NGRPS) { |
---|
165 | #ifdef DEBUG |
---|
166 | fprintf(stderr, |
---|
167 | "initgroups: %s is in too many groups\n", uname); |
---|
168 | #endif |
---|
169 | goto toomany; |
---|
170 | } |
---|
171 | /* filter out duplicate group entries */ |
---|
172 | filter = 0; |
---|
173 | for (j = 0; j < ngroups; j++) |
---|
174 | if (groups[j] == grp->gr_gid) { |
---|
175 | filter++; |
---|
176 | break; |
---|
177 | } |
---|
178 | if (!filter) |
---|
179 | groups[ngroups++] = grp->gr_gid; |
---|
180 | } |
---|
181 | } |
---|
182 | toomany: |
---|
183 | endgrent(); |
---|
184 | return (ngroups); |
---|
185 | } |
---|
186 | |
---|
187 | /* |
---|
188 | * Convert network-name to hostname |
---|
189 | */ |
---|
190 | int |
---|
191 | netname2host(char netname[MAXNETNAMELEN + 1], char *hostname, int hostlen) |
---|
192 | { |
---|
193 | int err; |
---|
194 | char valbuf[1024]; |
---|
195 | char *val; |
---|
196 | char *val2; |
---|
197 | int vallen; |
---|
198 | char *domain; |
---|
199 | |
---|
200 | if (getnetid(netname, valbuf)) { |
---|
201 | val = valbuf; |
---|
202 | if ((*val == '0') && (val[1] == ':')) { |
---|
203 | (void) strncpy(hostname, val + 2, hostlen); |
---|
204 | return (1); |
---|
205 | } |
---|
206 | } |
---|
207 | val = strchr(netname, '.'); |
---|
208 | if (val == NULL) |
---|
209 | return (0); |
---|
210 | if (strncmp(netname, OPSYS, (val - netname))) |
---|
211 | return (0); |
---|
212 | val++; |
---|
213 | val2 = strchr(val, '@'); |
---|
214 | if (val2 == NULL) |
---|
215 | return (0); |
---|
216 | vallen = val2 - val; |
---|
217 | if (vallen > (hostlen - 1)) |
---|
218 | vallen = hostlen - 1; |
---|
219 | (void) strncpy(hostname, val, vallen); |
---|
220 | hostname[vallen] = 0; |
---|
221 | |
---|
222 | err = __rpc_get_default_domain(&domain); /* change to rpc */ |
---|
223 | if (err) |
---|
224 | return (0); |
---|
225 | |
---|
226 | if (strcmp(val2 + 1, domain)) |
---|
227 | return (0); /* wrong domain */ |
---|
228 | else |
---|
229 | return (1); |
---|
230 | } |
---|
231 | |
---|
232 | /* |
---|
233 | * reads the file /etc/netid looking for a + to optionally go to the |
---|
234 | * network information service. |
---|
235 | */ |
---|
236 | int |
---|
237 | getnetid(char *key, char *ret) |
---|
238 | { |
---|
239 | char buf[1024]; /* big enough */ |
---|
240 | char *res; |
---|
241 | char *mkey; |
---|
242 | char *mval; |
---|
243 | FILE *fd; |
---|
244 | #ifdef YP |
---|
245 | char *domain; |
---|
246 | int err; |
---|
247 | char *lookup; |
---|
248 | int len; |
---|
249 | #endif |
---|
250 | int rv; |
---|
251 | |
---|
252 | rv = 0; |
---|
253 | |
---|
254 | fd = fopen(NETIDFILE, "r"); |
---|
255 | if (fd == NULL) { |
---|
256 | #ifdef YP |
---|
257 | res = "+"; |
---|
258 | goto getnetidyp; |
---|
259 | #else |
---|
260 | return (0); |
---|
261 | #endif |
---|
262 | } |
---|
263 | while (fd != NULL) { |
---|
264 | res = fgets(buf, sizeof(buf), fd); |
---|
265 | if (res == NULL) { |
---|
266 | rv = 0; |
---|
267 | goto done; |
---|
268 | } |
---|
269 | if (res[0] == '#') |
---|
270 | continue; |
---|
271 | else if (res[0] == '+') { |
---|
272 | #ifdef YP |
---|
273 | getnetidyp: |
---|
274 | err = yp_get_default_domain(&domain); |
---|
275 | if (err) { |
---|
276 | continue; |
---|
277 | } |
---|
278 | lookup = NULL; |
---|
279 | err = yp_match(domain, NETID, key, |
---|
280 | strlen(key), &lookup, &len); |
---|
281 | if (err) { |
---|
282 | #ifdef DEBUG |
---|
283 | fprintf(stderr, "match failed error %d\n", err); |
---|
284 | #endif |
---|
285 | continue; |
---|
286 | } |
---|
287 | lookup[len] = 0; |
---|
288 | strcpy(ret, lookup); |
---|
289 | free(lookup); |
---|
290 | rv = 2; |
---|
291 | goto done; |
---|
292 | #else /* YP */ |
---|
293 | #ifdef DEBUG |
---|
294 | fprintf(stderr, |
---|
295 | "Bad record in %s '+' -- NIS not supported in this library copy\n", |
---|
296 | NETIDFILE); |
---|
297 | #endif |
---|
298 | continue; |
---|
299 | #endif /* YP */ |
---|
300 | } else { |
---|
301 | mkey = strsep(&res, "\t "); |
---|
302 | if (mkey == NULL) { |
---|
303 | fprintf(stderr, |
---|
304 | "Bad record in %s -- %s", NETIDFILE, buf); |
---|
305 | continue; |
---|
306 | } |
---|
307 | do { |
---|
308 | mval = strsep(&res, " \t#\n"); |
---|
309 | } while (mval != NULL && !*mval); |
---|
310 | if (mval == NULL) { |
---|
311 | fprintf(stderr, |
---|
312 | "Bad record in %s val problem - %s", NETIDFILE, buf); |
---|
313 | continue; |
---|
314 | } |
---|
315 | if (strcmp(mkey, key) == 0) { |
---|
316 | strcpy(ret, mval); |
---|
317 | rv = 1; |
---|
318 | goto done; |
---|
319 | } |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | done: |
---|
324 | if (fd != NULL) |
---|
325 | fclose(fd); |
---|
326 | return (rv); |
---|
327 | } |
---|