xref: /freebsd/lib/libc/rpc/netnamer.c (revision 6b806d21d144c25f4fad714e1c0cf780f5e27d7e)
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 #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 #ifndef NGROUPS
70 #define NGROUPS 16
71 #endif
72 
73 /*
74  * Convert network-name into unix credential
75  */
76 int
77 netname2user(netname, uidp, gidp, gidlenp, gidlist)
78 	char            netname[MAXNETNAMELEN + 1];
79 	uid_t            *uidp;
80 	gid_t            *gidp;
81 	int            *gidlenp;
82 	gid_t	       *gidlist;
83 {
84 	char           *p;
85 	int             gidlen;
86 	uid_t           uid;
87 	long		luid;
88 	struct passwd  *pwd;
89 	char            val[1024];
90 	char           *val1, *val2;
91 	char           *domain;
92 	int             vallen;
93 	int             err;
94 
95 	if (getnetid(netname, val)) {
96 		char *res = val;
97 
98 		p = strsep(&res, ":");
99 		if (p == NULL)
100 			return (0);
101 		*uidp = (uid_t) atol(p);
102 		p = strsep(&res, "\n,");
103 		if (p == NULL) {
104 			return (0);
105 		}
106 		*gidp = (gid_t) atol(p);
107 		gidlen = 0;
108 		for (gidlen = 0; gidlen < NGROUPS; gidlen++) {
109 			p = strsep(&res, "\n,");
110 			if (p == NULL)
111 				break;
112 			gidlist[gidlen] = (gid_t) atol(p);
113 		}
114 		*gidlenp = gidlen;
115 
116 		return (1);
117 	}
118 	val1 = strchr(netname, '.');
119 	if (val1 == NULL)
120 		return (0);
121 	if (strncmp(netname, OPSYS, (val1-netname)))
122 		return (0);
123 	val1++;
124 	val2 = strchr(val1, '@');
125 	if (val2 == NULL)
126 		return (0);
127 	vallen = val2 - val1;
128 	if (vallen > (1024 - 1))
129 		vallen = 1024 - 1;
130 	(void) strncpy(val, val1, 1024);
131 	val[vallen] = 0;
132 
133 	err = __rpc_get_default_domain(&domain);	/* change to rpc */
134 	if (err)
135 		return (0);
136 
137 	if (strcmp(val2 + 1, domain))
138 		return (0);	/* wrong domain */
139 
140 	if (sscanf(val, "%ld", &luid) != 1)
141 		return (0);
142 	uid = luid;
143 
144 	/* use initgroups method */
145 	pwd = getpwuid(uid);
146 	if (pwd == NULL)
147 		return (0);
148 	*uidp = pwd->pw_uid;
149 	*gidp = pwd->pw_gid;
150 	*gidlenp = _getgroups(pwd->pw_name, gidlist);
151 	return (1);
152 }
153 
154 /*
155  * initgroups
156  */
157 
158 static int
159 _getgroups(uname, groups)
160 	char           *uname;
161 	gid_t          groups[NGROUPS];
162 {
163 	gid_t           ngroups = 0;
164 	struct group *grp;
165 	int    i;
166 	int    j;
167 	int             filter;
168 
169 	setgrent();
170 	while ((grp = getgrent())) {
171 		for (i = 0; grp->gr_mem[i]; i++)
172 			if (!strcmp(grp->gr_mem[i], uname)) {
173 				if (ngroups == NGROUPS) {
174 #ifdef DEBUG
175 					fprintf(stderr,
176 				"initgroups: %s is in too many groups\n", uname);
177 #endif
178 					goto toomany;
179 				}
180 				/* filter out duplicate group entries */
181 				filter = 0;
182 				for (j = 0; j < ngroups; j++)
183 					if (groups[j] == grp->gr_gid) {
184 						filter++;
185 						break;
186 					}
187 				if (!filter)
188 					groups[ngroups++] = grp->gr_gid;
189 			}
190 	}
191 toomany:
192 	endgrent();
193 	return (ngroups);
194 }
195 
196 /*
197  * Convert network-name to hostname
198  */
199 int
200 netname2host(netname, hostname, hostlen)
201 	char            netname[MAXNETNAMELEN + 1];
202 	char           *hostname;
203 	int             hostlen;
204 {
205 	int             err;
206 	char            valbuf[1024];
207 	char           *val;
208 	char           *val2;
209 	int             vallen;
210 	char           *domain;
211 
212 	if (getnetid(netname, valbuf)) {
213 		val = valbuf;
214 		if ((*val == '0') && (val[1] == ':')) {
215 			(void) strncpy(hostname, val + 2, hostlen);
216 			return (1);
217 		}
218 	}
219 	val = strchr(netname, '.');
220 	if (val == NULL)
221 		return (0);
222 	if (strncmp(netname, OPSYS, (val - netname)))
223 		return (0);
224 	val++;
225 	val2 = strchr(val, '@');
226 	if (val2 == NULL)
227 		return (0);
228 	vallen = val2 - val;
229 	if (vallen > (hostlen - 1))
230 		vallen = hostlen - 1;
231 	(void) strncpy(hostname, val, vallen);
232 	hostname[vallen] = 0;
233 
234 	err = __rpc_get_default_domain(&domain);	/* change to rpc */
235 	if (err)
236 		return (0);
237 
238 	if (strcmp(val2 + 1, domain))
239 		return (0);	/* wrong domain */
240 	else
241 		return (1);
242 }
243 
244 /*
245  * reads the file /etc/netid looking for a + to optionally go to the
246  * network information service.
247  */
248 int
249 getnetid(key, ret)
250 	char           *key, *ret;
251 {
252 	char            buf[1024];	/* big enough */
253 	char           *res;
254 	char           *mkey;
255 	char           *mval;
256 	FILE           *fd;
257 #ifdef YP
258 	char           *domain;
259 	int             err;
260 	char           *lookup;
261 	int             len;
262 #endif
263 
264 	fd = fopen(NETIDFILE, "r");
265 	if (fd == NULL) {
266 #ifdef YP
267 		res = "+";
268 		goto getnetidyp;
269 #else
270 		return (0);
271 #endif
272 	}
273 	for (;;) {
274 		if (fd == NULL)
275 			return (0);	/* getnetidyp brings us here */
276 		res = fgets(buf, sizeof(buf), fd);
277 		if (res == NULL) {
278 			fclose(fd);
279 			return (0);
280 		}
281 		if (res[0] == '#')
282 			continue;
283 		else if (res[0] == '+') {
284 #ifdef YP
285 	getnetidyp:
286 			err = yp_get_default_domain(&domain);
287 			if (err) {
288 				continue;
289 			}
290 			lookup = NULL;
291 			err = yp_match(domain, NETID, key,
292 				strlen(key), &lookup, &len);
293 			if (err) {
294 #ifdef DEBUG
295 				fprintf(stderr, "match failed error %d\n", err);
296 #endif
297 				continue;
298 			}
299 			lookup[len] = 0;
300 			strcpy(ret, lookup);
301 			free(lookup);
302 			if (fd != NULL)
303 				fclose(fd);
304 			return (2);
305 #else	/* YP */
306 #ifdef DEBUG
307 			fprintf(stderr,
308 "Bad record in %s '+' -- NIS not supported in this library copy\n",
309 				NETIDFILE);
310 #endif
311 			continue;
312 #endif	/* YP */
313 		} else {
314 			mkey = strsep(&res, "\t ");
315 			if (mkey == NULL) {
316 				fprintf(stderr,
317 		"Bad record in %s -- %s", NETIDFILE, buf);
318 				continue;
319 			}
320 			do {
321 				mval = strsep(&res, " \t#\n");
322 			} while (mval != NULL && !*mval);
323 			if (mval == NULL) {
324 				fprintf(stderr,
325 		"Bad record in %s val problem - %s", NETIDFILE, buf);
326 				continue;
327 			}
328 			if (strcmp(mkey, key) == 0) {
329 				strcpy(ret, mval);
330 				fclose(fd);
331 				return (1);
332 
333 			}
334 		}
335 	}
336 }
337