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