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.
30 * netname utility routines convert from unix names to network names and
31 * vice-versa This module is operating system dependent! What we define here
32 * will work with any unix system that has adopted the sun NIS domain
35 #include <sys/param.h>
39 #include <rpcsvc/yp_prot.h>
40 #include <rpcsvc/ypclnt.h>
52 static char *OPSYS = "unix";
53 static char *NETID = "netid.byname";
54 static char *NETIDFILE = "/etc/netid";
56 static int getnetid( char *, char * );
57 static int _getgroups( char *, gid_t * );
64 * Convert network-name into unix credential
67 netname2user(netname, uidp, gidp, gidlenp, gidlist)
68 char netname[MAXNETNAMELEN + 1];
85 if (getnetid(netname, val)) {
88 p = strsep(&res, ":");
91 *uidp = (uid_t) atol(p);
92 p = strsep(&res, "\n,");
96 *gidp = (gid_t) atol(p);
98 for (gidlen = 0; gidlen < NGROUPS; gidlen++) {
99 p = strsep(&res, "\n,");
102 gidlist[gidlen] = (gid_t) atol(p);
108 val1 = strchr(netname, '.');
111 if (strncmp(netname, OPSYS, (val1-netname)))
114 val2 = strchr(val1, '@');
117 vallen = val2 - val1;
118 if (vallen > (1024 - 1))
120 (void) strncpy(val, val1, 1024);
123 err = __rpc_get_default_domain(&domain); /* change to rpc */
127 if (strcmp(val2 + 1, domain))
128 return (0); /* wrong domain */
130 if (sscanf(val, "%ld", &luid) != 1)
134 /* use initgroups method */
140 *gidlenp = _getgroups(pwd->pw_name, gidlist);
149 _getgroups(uname, groups)
151 gid_t groups[NGROUPS];
160 while ((grp = getgrent())) {
161 for (i = 0; grp->gr_mem[i]; i++)
162 if (!strcmp(grp->gr_mem[i], uname)) {
163 if (ngroups == NGROUPS) {
165 ("_getgroups: %s is in too many groups\n", uname));
168 /* filter out duplicate group entries */
170 for (j = 0; j < ngroups; j++)
171 if (groups[j] == grp->gr_gid) {
176 groups[ngroups++] = grp->gr_gid;
185 * Convert network-name to hostname
188 netname2host(netname, hostname, hostlen)
189 char netname[MAXNETNAMELEN + 1];
200 if (getnetid(netname, valbuf)) {
202 if ((*val == '0') && (val[1] == ':')) {
203 (void) strncpy(hostname, val + 2, hostlen);
207 val = strchr(netname, '.');
210 if (strncmp(netname, OPSYS, (val - netname)))
213 val2 = strchr(val, '@');
217 if (vallen > (hostlen - 1))
218 vallen = hostlen - 1;
219 (void) strncpy(hostname, val, vallen);
220 hostname[vallen] = 0;
222 err = __rpc_get_default_domain(&domain); /* change to rpc */
226 if (strcmp(val2 + 1, domain))
227 return (0); /* wrong domain */
233 * reads the file /etc/netid looking for a + to optionally go to the
234 * network information service.
240 char buf[1024]; /* big enough */
252 fd = fopen(NETIDFILE, "r");
263 return (0); /* getnetidyp brings us here */
264 res = fgets(buf, sizeof(buf), fd);
271 else if (res[0] == '+') {
274 err = yp_get_default_domain(&domain);
279 err = yp_match(domain, NETID, key,
280 strlen(key), &lookup, &len);
282 LIBTIRPC_DEBUG(1, ("getnetid: match failed error %d", err));
293 ("Bad record in %s '+' -- NIS not supported in this library copy\n",
298 mkey = strsep(&res, "\t ");
301 "Bad record in %s -- %s", NETIDFILE, buf);
305 mval = strsep(&res, " \t#\n");
306 } while (mval != NULL && !*mval);
309 "Bad record in %s val problem - %s", NETIDFILE, buf);
312 if (strcmp(mkey, key) == 0) {