xref: /freebsd/lib/libc/rpc/netnamer.c (revision a986ef5788bd3338d2a970eb96104824e4a0e3bb)
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user or with the express written consent of
8  * Sun Microsystems, Inc.
9  *
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  *
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  *
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  *
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
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 static char    *NETID = "netid.byname";
62 static char    *NETIDFILE = "/etc/netid";
63 
64 static int getnetid( char *, char * );
65 static int _getgroups( char *, gid_t * );
66 
67 #ifndef NGROUPS
68 #define NGROUPS 16
69 #endif
70 
71 /*
72  * Convert network-name into unix credential
73  */
74 int
75 netname2user(netname, uidp, gidp, gidlenp, gidlist)
76 	char            netname[MAXNETNAMELEN + 1];
77 	uid_t            *uidp;
78 	gid_t            *gidp;
79 	int            *gidlenp;
80 	gid_t	       *gidlist;
81 {
82 	char           *p;
83 	int             gidlen;
84 	uid_t           uid;
85 	long		luid;
86 	struct passwd  *pwd;
87 	char            val[1024];
88 	char           *val1, *val2;
89 	char           *domain;
90 	int             vallen;
91 	int             err;
92 
93 	if (getnetid(netname, val)) {
94 		char *res = val;
95 
96 		p = strsep(&res, ":");
97 		if (p == NULL)
98 			return (0);
99 		*uidp = (uid_t) atol(p);
100 		p = strsep(&res, "\n,");
101 		if (p == NULL) {
102 			return (0);
103 		}
104 		*gidp = (gid_t) atol(p);
105 		gidlen = 0;
106 		for (gidlen = 0; gidlen < NGROUPS; gidlen++) {
107 			p = strsep(&res, "\n,");
108 			if (p == NULL)
109 				break;
110 			gidlist[gidlen] = (gid_t) atol(p);
111 		}
112 		*gidlenp = gidlen;
113 
114 		return (1);
115 	}
116 	val1 = strchr(netname, '.');
117 	if (val1 == NULL)
118 		return (0);
119 	if (strncmp(netname, OPSYS, (val1-netname)))
120 		return (0);
121 	val1++;
122 	val2 = strchr(val1, '@');
123 	if (val2 == NULL)
124 		return (0);
125 	vallen = val2 - val1;
126 	if (vallen > (1024 - 1))
127 		vallen = 1024 - 1;
128 	(void) strncpy(val, val1, 1024);
129 	val[vallen] = 0;
130 
131 	err = __rpc_get_default_domain(&domain);	/* change to rpc */
132 	if (err)
133 		return (0);
134 
135 	if (strcmp(val2 + 1, domain))
136 		return (0);	/* wrong domain */
137 
138 	if (sscanf(val, "%ld", &luid) != 1)
139 		return (0);
140 	uid = luid;
141 
142 	/* use initgroups method */
143 	pwd = getpwuid(uid);
144 	if (pwd == NULL)
145 		return (0);
146 	*uidp = pwd->pw_uid;
147 	*gidp = pwd->pw_gid;
148 	*gidlenp = _getgroups(pwd->pw_name, gidlist);
149 	return (1);
150 }
151 
152 /*
153  * initgroups
154  */
155 
156 static int
157 _getgroups(uname, groups)
158 	char           *uname;
159 	gid_t          groups[NGROUPS];
160 {
161 	gid_t           ngroups = 0;
162 	struct group *grp;
163 	int    i;
164 	int    j;
165 	int             filter;
166 
167 	setgrent();
168 	while ((grp = getgrent())) {
169 		for (i = 0; grp->gr_mem[i]; i++)
170 			if (!strcmp(grp->gr_mem[i], uname)) {
171 				if (ngroups == NGROUPS) {
172 #ifdef DEBUG
173 					fprintf(stderr,
174 				"initgroups: %s is in too many groups\n", uname);
175 #endif
176 					goto toomany;
177 				}
178 				/* filter out duplicate group entries */
179 				filter = 0;
180 				for (j = 0; j < ngroups; j++)
181 					if (groups[j] == grp->gr_gid) {
182 						filter++;
183 						break;
184 					}
185 				if (!filter)
186 					groups[ngroups++] = grp->gr_gid;
187 			}
188 	}
189 toomany:
190 	endgrent();
191 	return (ngroups);
192 }
193 
194 /*
195  * Convert network-name to hostname
196  */
197 int
198 netname2host(netname, hostname, hostlen)
199 	char            netname[MAXNETNAMELEN + 1];
200 	char           *hostname;
201 	int             hostlen;
202 {
203 	int             err;
204 	char            valbuf[1024];
205 	char           *val;
206 	char           *val2;
207 	int             vallen;
208 	char           *domain;
209 
210 	if (getnetid(netname, valbuf)) {
211 		val = valbuf;
212 		if ((*val == '0') && (val[1] == ':')) {
213 			(void) strncpy(hostname, val + 2, hostlen);
214 			return (1);
215 		}
216 	}
217 	val = strchr(netname, '.');
218 	if (val == NULL)
219 		return (0);
220 	if (strncmp(netname, OPSYS, (val - netname)))
221 		return (0);
222 	val++;
223 	val2 = strchr(val, '@');
224 	if (val2 == NULL)
225 		return (0);
226 	vallen = val2 - val;
227 	if (vallen > (hostlen - 1))
228 		vallen = hostlen - 1;
229 	(void) strncpy(hostname, val, vallen);
230 	hostname[vallen] = 0;
231 
232 	err = __rpc_get_default_domain(&domain);	/* change to rpc */
233 	if (err)
234 		return (0);
235 
236 	if (strcmp(val2 + 1, domain))
237 		return (0);	/* wrong domain */
238 	else
239 		return (1);
240 }
241 
242 /*
243  * reads the file /etc/netid looking for a + to optionally go to the
244  * network information service.
245  */
246 int
247 getnetid(key, ret)
248 	char           *key, *ret;
249 {
250 	char            buf[1024];	/* big enough */
251 	char           *res;
252 	char           *mkey;
253 	char           *mval;
254 	FILE           *fd;
255 #ifdef YP
256 	char           *domain;
257 	int             err;
258 	char           *lookup;
259 	int             len;
260 #endif
261 
262 	fd = fopen(NETIDFILE, "r");
263 	if (fd == NULL) {
264 #ifdef YP
265 		res = "+";
266 		goto getnetidyp;
267 #else
268 		return (0);
269 #endif
270 	}
271 	for (;;) {
272 		if (fd == NULL)
273 			return (0);	/* getnetidyp brings us here */
274 		res = fgets(buf, sizeof(buf), fd);
275 		if (res == NULL) {
276 			fclose(fd);
277 			return (0);
278 		}
279 		if (res[0] == '#')
280 			continue;
281 		else if (res[0] == '+') {
282 #ifdef YP
283 	getnetidyp:
284 			err = yp_get_default_domain(&domain);
285 			if (err) {
286 				continue;
287 			}
288 			lookup = NULL;
289 			err = yp_match(domain, NETID, key,
290 				strlen(key), &lookup, &len);
291 			if (err) {
292 #ifdef DEBUG
293 				fprintf(stderr, "match failed error %d\n", err);
294 #endif
295 				continue;
296 			}
297 			lookup[len] = 0;
298 			strcpy(ret, lookup);
299 			free(lookup);
300 			if (fd != NULL)
301 				fclose(fd);
302 			return (2);
303 #else	/* YP */
304 #ifdef DEBUG
305 			fprintf(stderr,
306 "Bad record in %s '+' -- NIS not supported in this library copy\n",
307 				NETIDFILE);
308 #endif
309 			continue;
310 #endif	/* YP */
311 		} else {
312 			mkey = strsep(&res, "\t ");
313 			if (mkey == NULL) {
314 				fprintf(stderr,
315 		"Bad record in %s -- %s", NETIDFILE, buf);
316 				continue;
317 			}
318 			do {
319 				mval = strsep(&res, " \t#\n");
320 			} while (mval != NULL && !*mval);
321 			if (mval == NULL) {
322 				fprintf(stderr,
323 		"Bad record in %s val problem - %s", NETIDFILE, buf);
324 				continue;
325 			}
326 			if (strcmp(mkey, key) == 0) {
327 				strcpy(ret, mval);
328 				fclose(fd);
329 				return (1);
330 
331 			}
332 		}
333 	}
334 }
335