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