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.
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.4 2004/10/25 19:38:01 drhodus 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
41 #include <sys/param.h>
43 #include <rpc/rpc_com.h>
45 #include <rpcsvc/yp_prot.h>
46 #include <rpcsvc/ypclnt.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 * );
68 * Convert network-name into unix credential
71 netname2user(netname, uidp, gidp, gidlenp, gidlist)
72 char netname[MAXNETNAMELEN + 1];
89 if (getnetid(netname, val)) {
92 p = strsep(&res, ":");
95 *uidp = (uid_t) atol(p);
96 p = strsep(&res, "\n,");
100 *gidp = (gid_t) atol(p);
102 for (gidlen = 0; gidlen < NGROUPS; gidlen++) {
103 p = strsep(&res, "\n,");
106 gidlist[gidlen] = (gid_t) atol(p);
112 val1 = strchr(netname, '.');
115 if (strncmp(netname, OPSYS, (val1-netname)))
118 val2 = strchr(val1, '@');
121 vallen = val2 - val1;
122 if (vallen > (1024 - 1))
124 (void) strncpy(val, val1, 1024);
127 err = _rpc_get_default_domain(&domain); /* change to rpc */
131 if (strcmp(val2 + 1, domain))
132 return (0); /* wrong domain */
134 if (sscanf(val, "%ld", &luid) != 1)
138 /* use initgroups method */
144 *gidlenp = _getgroups(pwd->pw_name, gidlist);
153 _getgroups(uname, groups)
155 gid_t groups[NGROUPS];
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) {
170 "initgroups: %s is in too many groups\n", uname);
174 /* filter out duplicate group entries */
176 for (j = 0; j < ngroups; j++)
177 if (groups[j] == grp->gr_gid) {
182 groups[ngroups++] = grp->gr_gid;
191 * Convert network-name to hostname
194 netname2host(netname, hostname, hostlen)
195 char netname[MAXNETNAMELEN + 1];
206 if (getnetid(netname, valbuf)) {
208 if ((*val == '0') && (val[1] == ':')) {
209 (void) strncpy(hostname, val + 2, hostlen);
213 val = strchr(netname, '.');
216 if (strncmp(netname, OPSYS, (val - netname)))
219 val2 = strchr(val, '@');
223 if (vallen > (hostlen - 1))
224 vallen = hostlen - 1;
225 (void) strncpy(hostname, val, vallen);
226 hostname[vallen] = 0;
228 err = _rpc_get_default_domain(&domain); /* change to rpc */
232 if (strcmp(val2 + 1, domain))
233 return (0); /* wrong domain */
239 * reads the file /etc/netid looking for a + to optionally go to the
240 * network information service.
246 char buf[1024]; /* big enough */
258 fd = fopen(NETIDFILE, "r");
269 return (0); /* getnetidyp brings us here */
270 res = fgets(buf, sizeof(buf), fd);
277 else if (res[0] == '+') {
280 err = yp_get_default_domain(&domain);
285 err = yp_match(domain, NETID, key,
286 strlen(key), &lookup, &len);
289 fprintf(stderr, "match failed error %d\n", err);
302 "Bad record in %s '+' -- NIS not supported in this library copy\n",
308 mkey = strsep(&res, "\t ");
311 "Bad record in %s -- %s", NETIDFILE, buf);
315 mval = strsep(&res, " \t#\n");
316 } while (mval != NULL && !*mval);
319 "Bad record in %s val problem - %s", NETIDFILE, buf);
322 if (strcmp(mkey, key) == 0) {