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