Bring in a transport-independent RPC (TI-RPC).
[dragonfly.git] / lib / libc / rpc / netnamer.c
blob90d1c168f79ec351f7459e4db5fecf29da881f11
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.12 2005/03/10 00:58:21 stefanf 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
37 * netname utility routines convert from unix names to network names and
38 * vice-versa This module is operating system dependent! What we define here
39 * will work with any unix system that has adopted the sun NIS domain
40 * architecture.
42 #include "namespace.h"
43 #include <sys/param.h>
44 #include <rpc/rpc.h>
45 #include <rpc/rpc_com.h>
46 #ifdef YP
47 #include <rpcsvc/yp_prot.h>
48 #include <rpcsvc/ypclnt.h>
49 #endif
50 #include <ctype.h>
51 #include <stdio.h>
52 #include <grp.h>
53 #include <pwd.h>
54 #include <string.h>
55 #include <stdlib.h>
56 #include <unistd.h>
57 #include "un-namespace.h"
59 static char *OPSYS = "unix";
60 #ifdef YP
61 static char *NETID = "netid.byname";
62 #endif
63 static char *NETIDFILE = "/etc/netid";
65 static int _getgroups(char *, gid_t *);
66 static int getnetid(char *, char *);
68 #ifndef NGROUPS
69 #define NGROUPS 16
70 #endif
73 * Convert network-name into unix credential
75 int
76 netname2user(char *netname, uid_t *uidp, gid_t *gidp, int *gidlenp,
77 gid_t *gidlist)
79 char *p;
80 int gidlen;
81 uid_t uid;
82 long luid;
83 struct passwd *pwd;
84 char val[1024];
85 char *val1, *val2;
86 char *domain;
87 int vallen;
88 int err;
90 if (getnetid(netname, val)) {
91 char *res = val;
93 p = strsep(&res, ":");
94 if (p == NULL)
95 return (0);
96 *uidp = (uid_t) atol(p);
97 p = strsep(&res, "\n,");
98 if (p == NULL) {
99 return (0);
101 *gidp = (gid_t) atol(p);
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);
108 *gidlenp = gidlen;
110 return (1);
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 strncpy(val, val1, 1024);
125 val[vallen] = 0;
127 err = __rpc_get_default_domain(&domain); /* change to rpc */
128 if (err)
129 return (0);
131 if (strcmp(val2 + 1, domain))
132 return (0); /* wrong domain */
134 if (sscanf(val, "%ld", &luid) != 1)
135 return (0);
136 uid = luid;
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);
149 * initgroups
152 static int
153 _getgroups(char *uname, gid_t *groups)
155 gid_t ngroups = 0;
156 struct group *grp;
157 int i;
158 int j;
159 int filter;
161 setgrent();
162 while ((grp = getgrent())) {
163 for (i = 0; grp->gr_mem[i]; i++)
164 if (!strcmp(grp->gr_mem[i], uname)) {
165 if (ngroups == NGROUPS) {
166 #ifdef DEBUG
167 fprintf(stderr,
168 "initgroups: %s is in too many groups\n", uname);
169 #endif
170 goto toomany;
172 /* filter out duplicate group entries */
173 filter = 0;
174 for (j = 0; j < ngroups; j++)
175 if (groups[j] == grp->gr_gid) {
176 filter++;
177 break;
179 if (!filter)
180 groups[ngroups++] = grp->gr_gid;
183 toomany:
184 endgrent();
185 return (ngroups);
189 * Convert network-name to hostname
192 netname2host(char *netname, char *hostname, int hostlen)
194 int err;
195 char valbuf[1024];
196 char *val;
197 char *val2;
198 int vallen;
199 char *domain;
201 if (getnetid(netname, valbuf)) {
202 val = valbuf;
203 if ((*val == '0') && (val[1] == ':')) {
204 strncpy(hostname, val + 2, hostlen);
205 return (1);
208 val = strchr(netname, '.');
209 if (val == NULL)
210 return (0);
211 if (strncmp(netname, OPSYS, (val - netname)))
212 return (0);
213 val++;
214 val2 = strchr(val, '@');
215 if (val2 == NULL)
216 return (0);
217 vallen = val2 - val;
218 if (vallen > (hostlen - 1))
219 vallen = hostlen - 1;
220 strncpy(hostname, val, vallen);
221 hostname[vallen] = 0;
223 err = __rpc_get_default_domain(&domain); /* change to rpc */
224 if (err)
225 return (0);
227 if (strcmp(val2 + 1, domain))
228 return (0); /* wrong domain */
229 else
230 return (1);
234 * reads the file /etc/netid looking for a + to optionally go to the
235 * network information service.
238 getnetid(char *key, char *ret)
240 char buf[1024]; /* big enough */
241 char *res;
242 char *mkey;
243 char *mval;
244 FILE *fd;
245 #ifdef YP
246 char *domain;
247 int err;
248 char *lookup;
249 int len;
250 #endif
252 fd = fopen(NETIDFILE, "r");
253 if (fd == NULL) {
254 #ifdef YP
255 res = "+";
256 goto getnetidyp;
257 #else
258 return (0);
259 #endif
261 for (;;) {
262 if (fd == NULL)
263 return (0); /* getnetidyp brings us here */
264 res = fgets(buf, sizeof(buf), fd);
265 if (res == NULL) {
266 fclose(fd);
267 return (0);
269 if (res[0] == '#')
270 continue;
271 else if (res[0] == '+') {
272 #ifdef YP
273 getnetidyp:
274 err = yp_get_default_domain(&domain);
275 if (err) {
276 continue;
278 lookup = NULL;
279 err = yp_match(domain, NETID, key,
280 strlen(key), &lookup, &len);
281 if (err) {
282 #ifdef DEBUG
283 fprintf(stderr, "match failed error %d\n", err);
284 #endif
285 continue;
287 lookup[len] = 0;
288 strcpy(ret, lookup);
289 free(lookup);
290 if (fd != NULL)
291 fclose(fd);
292 return (2);
293 #else /* YP */
294 #ifdef DEBUG
295 fprintf(stderr,
296 "Bad record in %s '+' -- NIS not supported in this library copy\n",
297 NETIDFILE);
298 #endif
299 continue;
300 #endif /* YP */
301 } else {
302 mkey = strsep(&res, "\t ");
303 if (mkey == NULL) {
304 fprintf(stderr,
305 "Bad record in %s -- %s", NETIDFILE, buf);
306 continue;
308 do {
309 mval = strsep(&res, " \t#\n");
310 } while (mval != NULL && !*mval);
311 if (mval == NULL) {
312 fprintf(stderr,
313 "Bad record in %s val problem - %s", NETIDFILE, buf);
314 continue;
316 if (strcmp(mkey, key) == 0) {
317 strcpy(ret, mval);
318 fclose(fd);
319 return (1);