2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
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(char netname[MAXNETNAMELEN + 1], uid_t *uidp, gid_t *gidp,
74 int *gidlenp, gid_t *gidlist)
87 if (getnetid(netname, val)) {
90 p = strsep(&res, ":");
93 *uidp = (uid_t) atol(p);
94 p = strsep(&res, "\n,");
98 *gidp = (gid_t) atol(p);
99 for (gidlen = 0; gidlen < NGRPS; gidlen++) {
100 p = strsep(&res, "\n,");
103 gidlist[gidlen] = (gid_t) atol(p);
109 val1 = strchr(netname, '.');
112 if (strncmp(netname, OPSYS, (val1-netname)))
115 val2 = strchr(val1, '@');
118 vallen = val2 - val1;
119 if (vallen > (1024 - 1))
121 (void) strncpy(val, val1, 1024);
124 err = __rpc_get_default_domain(&domain); /* change to rpc */
128 if (strcmp(val2 + 1, domain))
129 return (0); /* wrong domain */
131 if (sscanf(val, "%ld", &luid) != 1)
135 /* use initgroups method */
141 *gidlenp = _getgroups(pwd->pw_name, gidlist);
150 _getgroups(char *uname, gid_t groups[NGRPS])
159 while ((grp = getgrent())) {
160 for (i = 0; grp->gr_mem[i]; i++)
161 if (!strcmp(grp->gr_mem[i], uname)) {
162 if (ngroups == NGRPS) {
165 "initgroups: %s is in too many groups\n", uname);
169 /* filter out duplicate group entries */
171 for (j = 0; j < ngroups; j++)
172 if (groups[j] == grp->gr_gid) {
177 groups[ngroups++] = grp->gr_gid;
186 * Convert network-name to hostname
189 netname2host(char netname[MAXNETNAMELEN + 1], char *hostname, int hostlen)
198 if (getnetid(netname, valbuf)) {
200 if ((*val == '0') && (val[1] == ':')) {
201 (void) strncpy(hostname, val + 2, hostlen);
205 val = strchr(netname, '.');
208 if (strncmp(netname, OPSYS, (val - netname)))
211 val2 = strchr(val, '@');
215 if (vallen > (hostlen - 1))
216 vallen = hostlen - 1;
217 (void) strncpy(hostname, val, vallen);
218 hostname[vallen] = 0;
220 err = __rpc_get_default_domain(&domain); /* change to rpc */
224 if (strcmp(val2 + 1, domain))
225 return (0); /* wrong domain */
231 * reads the file /etc/netid looking for a + to optionally go to the
232 * network information service.
235 getnetid(char *key, char *ret)
237 char buf[1024]; /* big enough */
252 fd = fopen(NETIDFILE, "r");
262 res = fgets(buf, sizeof(buf), fd);
269 else if (res[0] == '+') {
272 err = yp_get_default_domain(&domain);
277 err = yp_match(domain, NETID, key,
278 strlen(key), &lookup, &len);
281 fprintf(stderr, "match failed error %d\n", err);
293 "Bad record in %s '+' -- NIS not supported in this library copy\n",
299 mkey = strsep(&res, "\t ");
302 "Bad record in %s -- %s", NETIDFILE, buf);
306 mval = strsep(&res, " \t#\n");
307 } while (mval != NULL && !*mval);
310 "Bad record in %s val problem - %s", NETIDFILE, buf);
313 if (strcmp(mkey, key) == 0) {