MFC r1.28:
[dragonfly.git] / lib / libc / rpc / netnamer.c
blob4396918480d990cfe2e8bc3a676f3914b0a1ad8a
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.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
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.5 2005/11/13 12:27:04 swildner Exp $
33 * @(#)netnamer.c 1.13 91/03/11 Copyr 1986 Sun Micro
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.
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>
56 static char *OPSYS = "unix";
57 static char *NETID = "netid.byname";
58 static char *NETIDFILE = "/etc/netid";
60 static int getnetid ( char *, char * );
61 static int _getgroups ( char *, gid_t * );
63 #ifndef NGROUPS
64 #define NGROUPS 16
65 #endif
68 * Convert network-name into unix credential
70 int
71 netname2user(char *netname, uid_t *uidp, gid_t *gidp, int *gidlenp,
72 gid_t *gidlist)
74 char *p;
75 int gidlen;
76 uid_t uid;
77 long luid;
78 struct passwd *pwd;
79 char val[1024];
80 char *val1, *val2;
81 char *domain;
82 int vallen;
83 int err;
85 if (getnetid(netname, val)) {
86 char *res = val;
88 p = strsep(&res, ":");
89 if (p == NULL)
90 return (0);
91 *uidp = (uid_t) atol(p);
92 p = strsep(&res, "\n,");
93 if (p == NULL) {
94 return (0);
96 *gidp = (gid_t) atol(p);
97 gidlen = 0;
98 for (gidlen = 0; gidlen < NGROUPS; gidlen++) {
99 p = strsep(&res, "\n,");
100 if (p == NULL)
101 break;
102 gidlist[gidlen] = (gid_t) atol(p);
104 *gidlenp = gidlen;
106 return (1);
108 val1 = strchr(netname, '.');
109 if (val1 == NULL)
110 return (0);
111 if (strncmp(netname, OPSYS, (val1-netname)))
112 return (0);
113 val1++;
114 val2 = strchr(val1, '@');
115 if (val2 == NULL)
116 return (0);
117 vallen = val2 - val1;
118 if (vallen > (1024 - 1))
119 vallen = 1024 - 1;
120 strncpy(val, val1, 1024);
121 val[vallen] = 0;
123 err = _rpc_get_default_domain(&domain); /* change to rpc */
124 if (err)
125 return (0);
127 if (strcmp(val2 + 1, domain))
128 return (0); /* wrong domain */
130 if (sscanf(val, "%ld", &luid) != 1)
131 return (0);
132 uid = luid;
134 /* use initgroups method */
135 pwd = getpwuid(uid);
136 if (pwd == NULL)
137 return (0);
138 *uidp = pwd->pw_uid;
139 *gidp = pwd->pw_gid;
140 *gidlenp = _getgroups(pwd->pw_name, gidlist);
141 return (1);
145 * initgroups
148 static int
149 _getgroups(char *uname, gid_t *groups)
151 gid_t ngroups = 0;
152 struct group *grp;
153 int i;
154 int j;
155 int filter;
157 setgrent();
158 while ((grp = getgrent())) {
159 for (i = 0; grp->gr_mem[i]; i++)
160 if (!strcmp(grp->gr_mem[i], uname)) {
161 if (ngroups == NGROUPS) {
162 #ifdef DEBUG
163 fprintf(stderr,
164 "initgroups: %s is in too many groups\n", uname);
165 #endif
166 goto toomany;
168 /* filter out duplicate group entries */
169 filter = 0;
170 for (j = 0; j < ngroups; j++)
171 if (groups[j] == grp->gr_gid) {
172 filter++;
173 break;
175 if (!filter)
176 groups[ngroups++] = grp->gr_gid;
179 toomany:
180 endgrent();
181 return (ngroups);
185 * Convert network-name to hostname
188 netname2host(char *netname, char *hostname, int hostlen)
190 int err;
191 char valbuf[1024];
192 char *val;
193 char *val2;
194 int vallen;
195 char *domain;
197 if (getnetid(netname, valbuf)) {
198 val = valbuf;
199 if ((*val == '0') && (val[1] == ':')) {
200 strncpy(hostname, val + 2, hostlen);
201 return (1);
204 val = strchr(netname, '.');
205 if (val == NULL)
206 return (0);
207 if (strncmp(netname, OPSYS, (val - netname)))
208 return (0);
209 val++;
210 val2 = strchr(val, '@');
211 if (val2 == NULL)
212 return (0);
213 vallen = val2 - val;
214 if (vallen > (hostlen - 1))
215 vallen = hostlen - 1;
216 strncpy(hostname, val, vallen);
217 hostname[vallen] = 0;
219 err = _rpc_get_default_domain(&domain); /* change to rpc */
220 if (err)
221 return (0);
223 if (strcmp(val2 + 1, domain))
224 return (0); /* wrong domain */
225 else
226 return (1);
230 * reads the file /etc/netid looking for a + to optionally go to the
231 * network information service.
234 getnetid(char *key, char *ret)
236 char buf[1024]; /* big enough */
237 char *res;
238 char *mkey;
239 char *mval;
240 FILE *fd;
241 #ifdef YP
242 char *domain;
243 int err;
244 char *lookup;
245 int len;
246 #endif
248 fd = fopen(NETIDFILE, "r");
249 if (fd == NULL) {
250 #ifdef YP
251 res = "+";
252 goto getnetidyp;
253 #else
254 return (0);
255 #endif
257 for (;;) {
258 if (fd == NULL)
259 return (0); /* getnetidyp brings us here */
260 res = fgets(buf, sizeof(buf), fd);
261 if (res == NULL) {
262 fclose(fd);
263 return (0);
265 if (res[0] == '#')
266 continue;
267 else if (res[0] == '+') {
268 #ifdef YP
269 getnetidyp:
270 err = yp_get_default_domain(&domain);
271 if (err) {
272 continue;
274 lookup = NULL;
275 err = yp_match(domain, NETID, key,
276 strlen(key), &lookup, &len);
277 if (err) {
278 #ifdef DEBUG
279 fprintf(stderr, "match failed error %d\n", err);
280 #endif
281 continue;
283 lookup[len] = 0;
284 strcpy(ret, lookup);
285 free(lookup);
286 if (fd != NULL)
287 fclose(fd);
288 return (2);
289 #else /* YP */
290 #ifdef DEBUG
291 fprintf(stderr,
292 "Bad record in %s '+' -- NIS not supported in this library copy\n",
293 NETIDFILE);
294 #endif
295 continue;
296 #endif /* YP */
297 } else {
298 mkey = strsep(&res, "\t ");
299 if (mkey == NULL) {
300 fprintf(stderr,
301 "Bad record in %s -- %s", NETIDFILE, buf);
302 continue;
304 do {
305 mval = strsep(&res, " \t#\n");
306 } while (mval != NULL && !*mval);
307 if (mval == NULL) {
308 fprintf(stderr,
309 "Bad record in %s val problem - %s", NETIDFILE, buf);
310 continue;
312 if (strcmp(mkey, key) == 0) {
313 strcpy(ret, mval);
314 fclose(fd);
315 return (1);