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