2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #if defined(LIBC_SCCS) && !defined(lint)
30 static char sccsid[] = "@(#)netnamer.c 1.13 91/03/11 Copyr 1986 Sun Micro";
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
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 "namespace.h"
42 #include <sys/param.h>
44 #include <rpc/rpc_com.h>
46 #include <rpcsvc/yp_prot.h>
47 #include <rpcsvc/ypclnt.h>
56 #include "un-namespace.h"
58 static char *OPSYS = "unix";
60 static char *NETID = "netid.byname";
62 static char *NETIDFILE = "/etc/netid";
64 static int getnetid( char *, char * );
65 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);
101 for (gidlen = 0; gidlen < NGRPS; gidlen++) {
102 p = strsep(&res, "\n,");
105 gidlist[gidlen] = (gid_t) atol(p);
111 val1 = strchr(netname, '.');
114 if (strncmp(netname, OPSYS, (val1-netname)))
117 val2 = strchr(val1, '@');
120 vallen = val2 - val1;
121 if (vallen > (1024 - 1))
123 (void) strncpy(val, val1, 1024);
126 err = __rpc_get_default_domain(&domain); /* change to rpc */
130 if (strcmp(val2 + 1, domain))
131 return (0); /* wrong domain */
133 if (sscanf(val, "%ld", &luid) != 1)
137 /* use initgroups method */
143 *gidlenp = _getgroups(pwd->pw_name, gidlist);
152 _getgroups(uname, groups)
163 while ((grp = getgrent())) {
164 for (i = 0; grp->gr_mem[i]; i++)
165 if (!strcmp(grp->gr_mem[i], uname)) {
166 if (ngroups == NGRPS) {
169 "initgroups: %s is in too many groups\n", uname);
173 /* filter out duplicate group entries */
175 for (j = 0; j < ngroups; j++)
176 if (groups[j] == grp->gr_gid) {
181 groups[ngroups++] = grp->gr_gid;
190 * Convert network-name to hostname
193 netname2host(netname, hostname, hostlen)
194 char netname[MAXNETNAMELEN + 1];
205 if (getnetid(netname, valbuf)) {
207 if ((*val == '0') && (val[1] == ':')) {
208 (void) strncpy(hostname, val + 2, hostlen);
212 val = strchr(netname, '.');
215 if (strncmp(netname, OPSYS, (val - netname)))
218 val2 = strchr(val, '@');
222 if (vallen > (hostlen - 1))
223 vallen = hostlen - 1;
224 (void) strncpy(hostname, val, vallen);
225 hostname[vallen] = 0;
227 err = __rpc_get_default_domain(&domain); /* change to rpc */
231 if (strcmp(val2 + 1, domain))
232 return (0); /* wrong domain */
238 * reads the file /etc/netid looking for a + to optionally go to the
239 * network information service.
245 char buf[1024]; /* big enough */
257 fd = fopen(NETIDFILE, "r");
268 return (0); /* getnetidyp brings us here */
269 res = fgets(buf, sizeof(buf), fd);
276 else if (res[0] == '+') {
279 err = yp_get_default_domain(&domain);
284 err = yp_match(domain, NETID, key,
285 strlen(key), &lookup, &len);
288 fprintf(stderr, "match failed error %d\n", err);
301 "Bad record in %s '+' -- NIS not supported in this library copy\n",
307 mkey = strsep(&res, "\t ");
310 "Bad record in %s -- %s", NETIDFILE, buf);
314 mval = strsep(&res, " \t#\n");
315 } while (mval != NULL && !*mval);
318 "Bad record in %s val problem - %s", NETIDFILE, buf);
321 if (strcmp(mkey, key) == 0) {