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
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char sccsid[] = "@(#)netnamer.c 1.13 91/03/11 Copyr 1986 Sun Micro";
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * netname utility routines convert from unix names to network names and
39 * vice-versa This module is operating system dependent! What we define here
40 * will work with any unix system that has adopted the sun NIS domain
43 #include "namespace.h"
44 #include <sys/param.h>
46 #include <rpc/rpc_com.h>
48 #include <rpcsvc/yp_prot.h>
49 #include <rpcsvc/ypclnt.h>
58 #include "un-namespace.h"
60 static char *OPSYS = "unix";
62 static char *NETID = "netid.byname";
64 static char *NETIDFILE = "/etc/netid";
66 static int getnetid( char *, char * );
67 static int _getgroups( char *, gid_t * );
70 * Convert network-name into unix credential
73 netname2user(netname, uidp, gidp, gidlenp, gidlist)
74 char netname[MAXNETNAMELEN + 1];
91 if (getnetid(netname, val)) {
94 p = strsep(&res, ":");
97 *uidp = (uid_t) atol(p);
98 p = strsep(&res, "\n,");
102 *gidp = (gid_t) atol(p);
103 for (gidlen = 0; gidlen < NGRPS; gidlen++) {
104 p = strsep(&res, "\n,");
107 gidlist[gidlen] = (gid_t) atol(p);
113 val1 = strchr(netname, '.');
116 if (strncmp(netname, OPSYS, (val1-netname)))
119 val2 = strchr(val1, '@');
122 vallen = val2 - val1;
123 if (vallen > (1024 - 1))
125 (void) strncpy(val, val1, 1024);
128 err = __rpc_get_default_domain(&domain); /* change to rpc */
132 if (strcmp(val2 + 1, domain))
133 return (0); /* wrong domain */
135 if (sscanf(val, "%ld", &luid) != 1)
139 /* use initgroups method */
145 *gidlenp = _getgroups(pwd->pw_name, gidlist);
154 _getgroups(uname, groups)
165 while ((grp = getgrent())) {
166 for (i = 0; grp->gr_mem[i]; i++)
167 if (!strcmp(grp->gr_mem[i], uname)) {
168 if (ngroups == NGRPS) {
171 "initgroups: %s is in too many groups\n", uname);
175 /* filter out duplicate group entries */
177 for (j = 0; j < ngroups; j++)
178 if (groups[j] == grp->gr_gid) {
183 groups[ngroups++] = grp->gr_gid;
192 * Convert network-name to hostname
195 netname2host(netname, hostname, hostlen)
196 char netname[MAXNETNAMELEN + 1];
207 if (getnetid(netname, valbuf)) {
209 if ((*val == '0') && (val[1] == ':')) {
210 (void) strncpy(hostname, val + 2, hostlen);
214 val = strchr(netname, '.');
217 if (strncmp(netname, OPSYS, (val - netname)))
220 val2 = strchr(val, '@');
224 if (vallen > (hostlen - 1))
225 vallen = hostlen - 1;
226 (void) strncpy(hostname, val, vallen);
227 hostname[vallen] = 0;
229 err = __rpc_get_default_domain(&domain); /* change to rpc */
233 if (strcmp(val2 + 1, domain))
234 return (0); /* wrong domain */
240 * reads the file /etc/netid looking for a + to optionally go to the
241 * network information service.
247 char buf[1024]; /* big enough */
259 fd = fopen(NETIDFILE, "r");
270 return (0); /* getnetidyp brings us here */
271 res = fgets(buf, sizeof(buf), fd);
278 else if (res[0] == '+') {
281 err = yp_get_default_domain(&domain);
286 err = yp_match(domain, NETID, key,
287 strlen(key), &lookup, &len);
290 fprintf(stderr, "match failed error %d\n", err);
303 "Bad record in %s '+' -- NIS not supported in this library copy\n",
309 mkey = strsep(&res, "\t ");
312 "Bad record in %s -- %s", NETIDFILE, buf);
316 mval = strsep(&res, " \t#\n");
317 } while (mval != NULL && !*mval);
320 "Bad record in %s val problem - %s", NETIDFILE, buf);
323 if (strcmp(mkey, key) == 0) {