xref: /dragonfly/lib/libc/rpc/netnamer.c (revision e98bdfd3)
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  * $FreeBSD: src/lib/libc/rpc/netnamer.c,v 1.12 2005/03/10 00:58:21 stefanf Exp $
29  * $DragonFly: src/lib/libc/rpc/netnamer.c,v 1.5 2005/11/13 12:27:04 swildner Exp $
30  *
31  * @(#)netnamer.c 1.13 91/03/11 Copyr 1986 Sun Micro
32  */
33 
34 /*
35  * netname utility routines convert from unix names to network names and
36  * vice-versa This module is operating system dependent! What we define here
37  * will work with any unix system that has adopted the sun NIS domain
38  * architecture.
39  */
40 #include "namespace.h"
41 #include <sys/param.h>
42 #include <rpc/rpc.h>
43 #include <rpc/rpc_com.h>
44 #ifdef YP
45 #include <rpcsvc/yp_prot.h>
46 #include <rpcsvc/ypclnt.h>
47 #endif
48 #include <ctype.h>
49 #include <stdio.h>
50 #include <grp.h>
51 #include <pwd.h>
52 #include <string.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 #include "un-namespace.h"
56 
57 static char    *OPSYS = "unix";
58 #ifdef YP
59 static char    *NETID = "netid.byname";
60 #endif
61 static char    *NETIDFILE = "/etc/netid";
62 
63 static int	_getgroups(char *, gid_t *);
64 static int	 getnetid(char *, char *);
65 
66 #ifndef NGROUPS
67 #define NGROUPS 16
68 #endif
69 
70 /*
71  * Convert network-name into unix credential
72  */
73 int
74 netname2user(char *netname, uid_t *uidp, gid_t *gidp, int *gidlenp,
75 	     gid_t *gidlist)
76 {
77 	char           *p;
78 	int             gidlen;
79 	uid_t           uid;
80 	long		luid;
81 	struct passwd  *pwd;
82 	char            val[1024];
83 	char           *val1, *val2;
84 	char           *domain;
85 	int             vallen;
86 	int             err;
87 
88 	if (getnetid(netname, val)) {
89 		char *res = val;
90 
91 		p = strsep(&res, ":");
92 		if (p == NULL)
93 			return (0);
94 		*uidp = (uid_t) atol(p);
95 		p = strsep(&res, "\n,");
96 		if (p == NULL) {
97 			return (0);
98 		}
99 		*gidp = (gid_t) atol(p);
100 		for (gidlen = 0; gidlen < NGROUPS; gidlen++) {
101 			p = strsep(&res, "\n,");
102 			if (p == NULL)
103 				break;
104 			gidlist[gidlen] = (gid_t) atol(p);
105 		}
106 		*gidlenp = gidlen;
107 
108 		return (1);
109 	}
110 	val1 = strchr(netname, '.');
111 	if (val1 == NULL)
112 		return (0);
113 	if (strncmp(netname, OPSYS, (val1-netname)))
114 		return (0);
115 	val1++;
116 	val2 = strchr(val1, '@');
117 	if (val2 == NULL)
118 		return (0);
119 	vallen = val2 - val1;
120 	if (vallen > (1024 - 1))
121 		vallen = 1024 - 1;
122 	strncpy(val, val1, 1024);
123 	val[vallen] = 0;
124 
125 	err = __rpc_get_default_domain(&domain);	/* change to rpc */
126 	if (err)
127 		return (0);
128 
129 	if (strcmp(val2 + 1, domain))
130 		return (0);	/* wrong domain */
131 
132 	if (sscanf(val, "%ld", &luid) != 1)
133 		return (0);
134 	uid = luid;
135 
136 	/* use initgroups method */
137 	pwd = getpwuid(uid);
138 	if (pwd == NULL)
139 		return (0);
140 	*uidp = pwd->pw_uid;
141 	*gidp = pwd->pw_gid;
142 	*gidlenp = _getgroups(pwd->pw_name, gidlist);
143 	return (1);
144 }
145 
146 /*
147  * initgroups
148  */
149 
150 static int
151 _getgroups(char *uname, gid_t *groups)
152 {
153 	gid_t           ngroups = 0;
154 	struct group *grp;
155 	int    i;
156 	int    j;
157 	int             filter;
158 
159 	setgrent();
160 	while ((grp = getgrent())) {
161 		for (i = 0; grp->gr_mem[i]; i++)
162 			if (!strcmp(grp->gr_mem[i], uname)) {
163 				if (ngroups == NGROUPS) {
164 #ifdef DEBUG
165 					fprintf(stderr,
166 				"initgroups: %s is in too many groups\n", uname);
167 #endif
168 					goto toomany;
169 				}
170 				/* filter out duplicate group entries */
171 				filter = 0;
172 				for (j = 0; j < ngroups; j++)
173 					if (groups[j] == grp->gr_gid) {
174 						filter++;
175 						break;
176 					}
177 				if (!filter)
178 					groups[ngroups++] = grp->gr_gid;
179 			}
180 	}
181 toomany:
182 	endgrent();
183 	return (ngroups);
184 }
185 
186 /*
187  * Convert network-name to hostname
188  */
189 int
190 netname2host(char *netname, char *hostname, int hostlen)
191 {
192 	int             err;
193 	char            valbuf[1024];
194 	char           *val;
195 	char           *val2;
196 	int             vallen;
197 	char           *domain;
198 
199 	if (getnetid(netname, valbuf)) {
200 		val = valbuf;
201 		if ((*val == '0') && (val[1] == ':')) {
202 			strncpy(hostname, val + 2, hostlen);
203 			return (1);
204 		}
205 	}
206 	val = strchr(netname, '.');
207 	if (val == NULL)
208 		return (0);
209 	if (strncmp(netname, OPSYS, (val - netname)))
210 		return (0);
211 	val++;
212 	val2 = strchr(val, '@');
213 	if (val2 == NULL)
214 		return (0);
215 	vallen = val2 - val;
216 	if (vallen > (hostlen - 1))
217 		vallen = hostlen - 1;
218 	strncpy(hostname, val, vallen);
219 	hostname[vallen] = 0;
220 
221 	err = __rpc_get_default_domain(&domain);	/* change to rpc */
222 	if (err)
223 		return (0);
224 
225 	if (strcmp(val2 + 1, domain))
226 		return (0);	/* wrong domain */
227 	else
228 		return (1);
229 }
230 
231 /*
232  * reads the file /etc/netid looking for a + to optionally go to the
233  * network information service.
234  */
235 static int
236 getnetid(char *key, char *ret)
237 {
238 	char            buf[1024];	/* big enough */
239 	char           *res;
240 	char           *mkey;
241 	char           *mval;
242 	FILE           *fd;
243 #ifdef YP
244 	char           *domain;
245 	int             err;
246 	char           *lookup;
247 	int             len;
248 #endif
249 
250 	fd = fopen(NETIDFILE, "r");
251 	if (fd == NULL) {
252 #ifdef YP
253 		res = "+";
254 		goto getnetidyp;
255 #else
256 		return (0);
257 #endif
258 	}
259 	for (;;) {
260 		if (fd == NULL)
261 			return (0);	/* getnetidyp brings us here */
262 		res = fgets(buf, sizeof(buf), fd);
263 		if (res == NULL) {
264 			fclose(fd);
265 			return (0);
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 			if (fd != NULL)
289 				fclose(fd);
290 			return (2);
291 #else	/* YP */
292 #ifdef DEBUG
293 			fprintf(stderr,
294 "Bad record in %s '+' -- NIS not supported in this library copy\n",
295 				NETIDFILE);
296 #endif
297 			continue;
298 #endif	/* YP */
299 		} else {
300 			mkey = strsep(&res, "\t ");
301 			if (mkey == NULL) {
302 				fprintf(stderr,
303 		"Bad record in %s -- %s", NETIDFILE, buf);
304 				continue;
305 			}
306 			do {
307 				mval = strsep(&res, " \t#\n");
308 			} while (mval != NULL && !*mval);
309 			if (mval == NULL) {
310 				fprintf(stderr,
311 		"Bad record in %s val problem - %s", NETIDFILE, buf);
312 				continue;
313 			}
314 			if (strcmp(mkey, key) == 0) {
315 				strcpy(ret, mval);
316 				fclose(fd);
317 				return (1);
318 
319 			}
320 		}
321 	}
322 }
323