xref: /titanic_44/usr/src/lib/libnsl/rpc/netnamer.c (revision 5ad42b1b1469908fabc0099764182e9ecbc04dda)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5004388ebScasper  * Common Development and Distribution License (the "License").
6004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
2061961e0fSrobinson  */
2161961e0fSrobinson 
2261961e0fSrobinson /*
2367dbe2beSCasper H.S. Dik  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
307c478bd9Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
317c478bd9Sstevel@tonic-gate  * California.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * ==== hack-attack:  possibly MT-safe but definitely not MT-hot.
357c478bd9Sstevel@tonic-gate  * ==== turn this into a real switch frontend and backends
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * Well, at least the API doesn't involve pointers-to-static.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * netname utility routines convert from netnames to unix names (uid, gid)
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * This module is operating system dependent!
447c478bd9Sstevel@tonic-gate  * What we define here will work with any unix system that has adopted
457c478bd9Sstevel@tonic-gate  * the Sun NIS domain architecture.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #undef NIS
497c478bd9Sstevel@tonic-gate #include "mt.h"
507c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
517c478bd9Sstevel@tonic-gate #include <stdio.h>
527c478bd9Sstevel@tonic-gate #include <stdlib.h>
5367dbe2beSCasper H.S. Dik #include <unistd.h>
5467dbe2beSCasper H.S. Dik #include <alloca.h>
557c478bd9Sstevel@tonic-gate #include <sys/types.h>
567c478bd9Sstevel@tonic-gate #include <ctype.h>
577c478bd9Sstevel@tonic-gate #include <grp.h>
587c478bd9Sstevel@tonic-gate #include <pwd.h>
597c478bd9Sstevel@tonic-gate #include <string.h>
607c478bd9Sstevel@tonic-gate #include <syslog.h>
617c478bd9Sstevel@tonic-gate #include <sys/param.h>
627c478bd9Sstevel@tonic-gate #include <nsswitch.h>
637c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
647c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
657c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
667c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static const char    OPSYS[]	= "unix";
697c478bd9Sstevel@tonic-gate static const char    NETIDFILE[] = "/etc/netid";
707c478bd9Sstevel@tonic-gate static const char    NETID[]	= "netid.byname";
717c478bd9Sstevel@tonic-gate #define	OPSYS_LEN 4
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate extern int _getgroupsbymember(const char *, gid_t[], int, int);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * the value for NOBODY_UID is set by the SVID. The following define also
777c478bd9Sstevel@tonic-gate  * appears in netname.c
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #define	NOBODY_UID 60001
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  *	default publickey policy:
847c478bd9Sstevel@tonic-gate  *		publickey: nis [NOTFOUND = return] files
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*		NSW_NOTSUCCESS  NSW_NOTFOUND   NSW_UNAVAIL    NSW_TRYAGAIN */
897c478bd9Sstevel@tonic-gate #define	DEF_ACTION {__NSW_RETURN, __NSW_RETURN, __NSW_CONTINUE, __NSW_CONTINUE}
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static struct __nsw_lookup lookup_files = {"files", DEF_ACTION, NULL, NULL},
927c478bd9Sstevel@tonic-gate 		lookup_nis = {"nis", DEF_ACTION, NULL, &lookup_files};
937c478bd9Sstevel@tonic-gate static struct __nsw_switchconfig publickey_default =
947c478bd9Sstevel@tonic-gate 			{0, "publickey", 2, &lookup_nis};
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static mutex_t serialize_netname_r = DEFAULTMUTEX;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate struct netid_userdata {
997c478bd9Sstevel@tonic-gate 	uid_t	*uidp;
1007c478bd9Sstevel@tonic-gate 	gid_t	*gidp;
1017c478bd9Sstevel@tonic-gate 	int	*gidlenp;
1027c478bd9Sstevel@tonic-gate 	gid_t	*gidlist;
1037c478bd9Sstevel@tonic-gate };
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static int
10661961e0fSrobinson parse_uid(char *s, struct netid_userdata *argp)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	uid_t	u;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if (!s || !isdigit(*s)) {
1117c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
1127c478bd9Sstevel@tonic-gate 		    "netname2user: expecting uid '%s'", s);
1137c478bd9Sstevel@tonic-gate 		return (__NSW_NOTFOUND); /* xxx need a better error */
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	/* Fetch the uid */
1177c478bd9Sstevel@tonic-gate 	u = (uid_t)(atoi(s));
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (u == 0) {
1207c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "netname2user: should not have uid 0");
1217c478bd9Sstevel@tonic-gate 		return (__NSW_NOTFOUND);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 	*(argp->uidp) = u;
1247c478bd9Sstevel@tonic-gate 	return (__NSW_SUCCESS);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /* parse a comma separated gid list */
1297c478bd9Sstevel@tonic-gate static int
13061961e0fSrobinson parse_gidlist(char *p, struct netid_userdata *argp)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	int len;
1337c478bd9Sstevel@tonic-gate 	gid_t	g;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	if (!p || (!isdigit(*p))) {
1367c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
1377c478bd9Sstevel@tonic-gate 		    "netname2user: missing group id list in '%s'.",
1387c478bd9Sstevel@tonic-gate 		    p);
1397c478bd9Sstevel@tonic-gate 		return (__NSW_NOTFOUND);
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	g = (gid_t)(atoi(p));
1437c478bd9Sstevel@tonic-gate 	*(argp->gidp) = g;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	len = 0;
1467c478bd9Sstevel@tonic-gate 	while (p = strchr(p, ','))
1477c478bd9Sstevel@tonic-gate 		argp->gidlist[len++] = (gid_t)atoi(++p);
1487c478bd9Sstevel@tonic-gate 	*(argp->gidlenp) = len;
1497c478bd9Sstevel@tonic-gate 	return (__NSW_SUCCESS);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * parse_netid_str()
1557c478bd9Sstevel@tonic-gate  *
1567c478bd9Sstevel@tonic-gate  * Parse uid and group information from the passed string.
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  * The format of the string passed is
1597c478bd9Sstevel@tonic-gate  * 	uid:gid,grp,grp, ...
1607c478bd9Sstevel@tonic-gate  *
1617c478bd9Sstevel@tonic-gate  */
1627c478bd9Sstevel@tonic-gate static int
16361961e0fSrobinson parse_netid_str(char *s, struct netid_userdata *argp)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	char	*p;
1667c478bd9Sstevel@tonic-gate 	int	err;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	/* get uid */
1697c478bd9Sstevel@tonic-gate 	err = parse_uid(s, argp);
17061961e0fSrobinson 	if (err != __NSW_SUCCESS)
1717c478bd9Sstevel@tonic-gate 		return (err);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/* Now get the group list */
1747c478bd9Sstevel@tonic-gate 	p = strchr(s, ':');
1757c478bd9Sstevel@tonic-gate 	if (!p) {
1767c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
17761961e0fSrobinson 		    "netname2user: missing group id list in '%s'", s);
1787c478bd9Sstevel@tonic-gate 		return (__NSW_NOTFOUND);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 	++p;			/* skip ':' */
1817c478bd9Sstevel@tonic-gate 	err = parse_gidlist(p, argp);
1827c478bd9Sstevel@tonic-gate 	return (err);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  * netname2user_files()
1877c478bd9Sstevel@tonic-gate  *
1887c478bd9Sstevel@tonic-gate  * This routine fetches the netid information from the "files" nameservice.
1897c478bd9Sstevel@tonic-gate  * ie /etc/netid.
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate static int
19261961e0fSrobinson netname2user_files(int *err, char *netname, struct netid_userdata *argp)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	char 	buf[512];	/* one line from the file */
1957c478bd9Sstevel@tonic-gate 	char	*name;
1967c478bd9Sstevel@tonic-gate 	char	*value;
1977c478bd9Sstevel@tonic-gate 	char 	*res;
198004388ebScasper 	FILE	*fd;
1997c478bd9Sstevel@tonic-gate 
200004388ebScasper 	fd = fopen(NETIDFILE, "rF");
201004388ebScasper 	if (fd == NULL) {
2027c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
2037c478bd9Sstevel@tonic-gate 		return (0);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 	/*
2067c478bd9Sstevel@tonic-gate 	 * for each line in the file parse it appropriately
2077c478bd9Sstevel@tonic-gate 	 * file format is :
2087c478bd9Sstevel@tonic-gate 	 *	netid	uid:grp,grp,grp # for users
2097c478bd9Sstevel@tonic-gate 	 *	netid	0:hostname	# for hosts
2107c478bd9Sstevel@tonic-gate 	 */
211004388ebScasper 	while (!feof(fd)) {
212004388ebScasper 		res = fgets(buf, 512, fd);
2137c478bd9Sstevel@tonic-gate 		if (res == NULL)
2147c478bd9Sstevel@tonic-gate 			break;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		/* Skip comments and blank lines */
2177c478bd9Sstevel@tonic-gate 		if ((*res == '#') || (*res == '\n'))
2187c478bd9Sstevel@tonic-gate 			continue;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		name = &(buf[0]);
2217c478bd9Sstevel@tonic-gate 		while (isspace(*name))
2227c478bd9Sstevel@tonic-gate 			name++;
2237c478bd9Sstevel@tonic-gate 		if (*name == '\0')	/* blank line continue */
2247c478bd9Sstevel@tonic-gate 			continue;
2257c478bd9Sstevel@tonic-gate 		value = name;		/* will contain the value eventually */
2267c478bd9Sstevel@tonic-gate 		while (!isspace(*value))
2277c478bd9Sstevel@tonic-gate 			value++;
2287c478bd9Sstevel@tonic-gate 		if (*value == '\0') {
2297c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING,
2307c478bd9Sstevel@tonic-gate 			    "netname2user: badly formatted line in %s.",
2317c478bd9Sstevel@tonic-gate 			    NETIDFILE);
2327c478bd9Sstevel@tonic-gate 			continue;
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 		*value++ = '\0'; /* nul terminate the name */
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		if (strcasecmp(name, netname) == 0) {
237004388ebScasper 			(void) fclose(fd);
2387c478bd9Sstevel@tonic-gate 			while (isspace(*value))
2397c478bd9Sstevel@tonic-gate 				value++;
2407c478bd9Sstevel@tonic-gate 			*err = parse_netid_str(value, argp);
2417c478bd9Sstevel@tonic-gate 			return (*err == __NSW_SUCCESS);
2427c478bd9Sstevel@tonic-gate 		}
2437c478bd9Sstevel@tonic-gate 	}
244004388ebScasper 	(void) fclose(fd);
2457c478bd9Sstevel@tonic-gate 	*err = __NSW_NOTFOUND;
2467c478bd9Sstevel@tonic-gate 	return (0);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * netname2user_nis()
2517c478bd9Sstevel@tonic-gate  *
2527c478bd9Sstevel@tonic-gate  * This function reads the netid from the NIS (YP) nameservice.
2537c478bd9Sstevel@tonic-gate  */
2547c478bd9Sstevel@tonic-gate static int
25561961e0fSrobinson netname2user_nis(int *err, char *netname, struct netid_userdata *argp)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	char *domain;
2587c478bd9Sstevel@tonic-gate 	int yperr;
2597c478bd9Sstevel@tonic-gate 	char *lookup;
2607c478bd9Sstevel@tonic-gate 	int len;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	domain = strchr(netname, '@');
2637c478bd9Sstevel@tonic-gate 	if (!domain) {
2647c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
2657c478bd9Sstevel@tonic-gate 		return (0);
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	/* Point past the '@' character */
2697c478bd9Sstevel@tonic-gate 	domain++;
2707c478bd9Sstevel@tonic-gate 	lookup = NULL;
2717c478bd9Sstevel@tonic-gate 	yperr = yp_match(domain, (char *)NETID, netname, strlen(netname),
2727c478bd9Sstevel@tonic-gate 	    &lookup, &len);
2737c478bd9Sstevel@tonic-gate 	switch (yperr) {
2747c478bd9Sstevel@tonic-gate 		case 0:
2757c478bd9Sstevel@tonic-gate 			break; /* the successful case */
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		default :
2787c478bd9Sstevel@tonic-gate 			/*
2797c478bd9Sstevel@tonic-gate 			 *  XXX not sure about yp_match semantics.
2807c478bd9Sstevel@tonic-gate 			 * should err be set to NOTFOUND here?
2817c478bd9Sstevel@tonic-gate 			 */
2827c478bd9Sstevel@tonic-gate 			*err = __NSW_UNAVAIL;
2837c478bd9Sstevel@tonic-gate 			return (0);
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 	if (lookup) {
2867c478bd9Sstevel@tonic-gate 		lookup[len] = '\0';
2877c478bd9Sstevel@tonic-gate 		*err = parse_netid_str(lookup, argp);
2887c478bd9Sstevel@tonic-gate 		free(lookup);
2897c478bd9Sstevel@tonic-gate 		return (*err == __NSW_SUCCESS);
29061961e0fSrobinson 	}
2917c478bd9Sstevel@tonic-gate 	*err = __NSW_NOTFOUND;
2927c478bd9Sstevel@tonic-gate 	return (0);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate  * Build the uid and gid from the netname for users in LDAP.
2977c478bd9Sstevel@tonic-gate  * There is no netid container in LDAP. For this we build
2987c478bd9Sstevel@tonic-gate  * the netname to user data dynamically from the passwd and
2997c478bd9Sstevel@tonic-gate  * group data. This works only for users in a single domain.
3007c478bd9Sstevel@tonic-gate  * This function is an interim solution until we support a
3017c478bd9Sstevel@tonic-gate  * netid container in LDAP which enables us to do netname2user
3027c478bd9Sstevel@tonic-gate  * resolution for multiple domains.
3037c478bd9Sstevel@tonic-gate  */
3047c478bd9Sstevel@tonic-gate static int
30561961e0fSrobinson netname2user_ldap(int *err, char *netname, struct netid_userdata *argp)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	char buf[NSS_LINELEN_PASSWD];
30861961e0fSrobinson 	char *p2, *lasts;
3097c478bd9Sstevel@tonic-gate 	struct passwd pw;
3107c478bd9Sstevel@tonic-gate 	uid_t uidnu;
3117c478bd9Sstevel@tonic-gate 	int ngroups = 0;
3127c478bd9Sstevel@tonic-gate 	int count;
3137c478bd9Sstevel@tonic-gate 	char pwbuf[NSS_LINELEN_PASSWD];
31467dbe2beSCasper H.S. Dik 	int maxgrp = sysconf(_SC_NGROUPS_MAX);
31567dbe2beSCasper H.S. Dik 	gid_t *groups = alloca(maxgrp * sizeof (gid_t));
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (strlcpy(buf, netname, NSS_LINELEN_PASSWD) >= NSS_LINELEN_PASSWD) {
3187c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
3197c478bd9Sstevel@tonic-gate 		return (0);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/* get the uid from the netname */
32361961e0fSrobinson 	if (strtok_r(buf, ".", &lasts) == NULL) {
3247c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
3257c478bd9Sstevel@tonic-gate 		return (0);
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 	if ((p2 = strtok_r(NULL, "@", &lasts)) == NULL) {
3287c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
3297c478bd9Sstevel@tonic-gate 		return (0);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 	uidnu = atoi(p2);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	/*
3347c478bd9Sstevel@tonic-gate 	 * check out the primary group and crosscheck the uid
3357c478bd9Sstevel@tonic-gate 	 * with the passwd data
3367c478bd9Sstevel@tonic-gate 	 */
3377c478bd9Sstevel@tonic-gate 	if ((getpwuid_r(uidnu, &pw, pwbuf, sizeof (pwbuf))) == NULL) {
3387c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
3397c478bd9Sstevel@tonic-gate 		return (0);
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	*(argp->uidp) = pw.pw_uid;
3437c478bd9Sstevel@tonic-gate 	*(argp->gidp) = pw.pw_gid;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	/* search through all groups for membership */
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	groups[0] = pw.pw_gid;
3487c478bd9Sstevel@tonic-gate 
34967dbe2beSCasper H.S. Dik 	ngroups = _getgroupsbymember(pw.pw_name, groups, maxgrp,
350f48205beScasper 	    (pw.pw_gid <= MAXUID) ? 1 : 0);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	if (ngroups < 0) {
3537c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
3547c478bd9Sstevel@tonic-gate 		return (0);
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	*(argp->gidlenp) = ngroups;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	for (count = 0; count < ngroups; count++) {
3607c478bd9Sstevel@tonic-gate 		(argp->gidlist[count]) = groups[count];
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	*err = __NSW_SUCCESS;
3647c478bd9Sstevel@tonic-gate 	return (1);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate /*
3697c478bd9Sstevel@tonic-gate  * Convert network-name into unix credential
3707c478bd9Sstevel@tonic-gate  */
3717c478bd9Sstevel@tonic-gate int
37261961e0fSrobinson netname2user(const char netname[MAXNETNAMELEN + 1], uid_t *uidp, gid_t *gidp,
37361961e0fSrobinson 						int *gidlenp, gid_t *gidlist)
3747c478bd9Sstevel@tonic-gate {
3757c478bd9Sstevel@tonic-gate 	struct __nsw_switchconfig *conf;
3767c478bd9Sstevel@tonic-gate 	struct __nsw_lookup *look;
3777c478bd9Sstevel@tonic-gate 	enum __nsw_parse_err perr;
3787c478bd9Sstevel@tonic-gate 	int needfree = 1, res;
3797c478bd9Sstevel@tonic-gate 	struct netid_userdata argp;
3807c478bd9Sstevel@tonic-gate 	int err;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/*
3837c478bd9Sstevel@tonic-gate 	 * Take care of the special case of nobody. Compare the netname
3847c478bd9Sstevel@tonic-gate 	 * to the string "nobody". If they are equal, return the SVID
3857c478bd9Sstevel@tonic-gate 	 * standard value for nobody.
3867c478bd9Sstevel@tonic-gate 	 */
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (strcmp(netname, "nobody") == 0) {
3897c478bd9Sstevel@tonic-gate 		*uidp = NOBODY_UID;
3907c478bd9Sstevel@tonic-gate 		*gidp = NOBODY_UID;
3917c478bd9Sstevel@tonic-gate 		*gidlenp = 0;
3927c478bd9Sstevel@tonic-gate 		return (1);
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	/*
3967c478bd9Sstevel@tonic-gate 	 * First we do some generic sanity checks on the name we were
3977c478bd9Sstevel@tonic-gate 	 * passed. This lets us assume they are correct in the backends.
3987c478bd9Sstevel@tonic-gate 	 *
3997c478bd9Sstevel@tonic-gate 	 * NOTE: this code only recognizes names of the form :
4007c478bd9Sstevel@tonic-gate 	 *		unix.UID@domainname
4017c478bd9Sstevel@tonic-gate 	 */
40261961e0fSrobinson 	if (strncmp(netname, OPSYS, OPSYS_LEN) != 0)
4037c478bd9Sstevel@tonic-gate 		return (0);
40461961e0fSrobinson 	if (!isdigit(netname[OPSYS_LEN+1]))	/* check for uid string */
4057c478bd9Sstevel@tonic-gate 		return (0);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	argp.uidp = uidp;
4087c478bd9Sstevel@tonic-gate 	argp.gidp = gidp;
4097c478bd9Sstevel@tonic-gate 	argp.gidlenp = gidlenp;
4107c478bd9Sstevel@tonic-gate 	argp.gidlist = gidlist;
41161961e0fSrobinson 	(void) mutex_lock(&serialize_netname_r);
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	conf = __nsw_getconfig("publickey", &perr);
4147c478bd9Sstevel@tonic-gate 	if (!conf) {
4157c478bd9Sstevel@tonic-gate 		conf = &publickey_default;
4167c478bd9Sstevel@tonic-gate 		needfree = 0;
4177c478bd9Sstevel@tonic-gate 	} else
4187c478bd9Sstevel@tonic-gate 		needfree = 1; /* free the config structure */
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	for (look = conf->lookups; look; look = look->next) {
42136e852a1SRaja Andra 		if (strcmp(look->service_name, "nis") == 0)
4227c478bd9Sstevel@tonic-gate 			res = netname2user_nis(&err, (char *)netname, &argp);
4237c478bd9Sstevel@tonic-gate 		else if (strcmp(look->service_name, "files") == 0)
4247c478bd9Sstevel@tonic-gate 			res = netname2user_files(&err, (char *)netname, &argp);
4257c478bd9Sstevel@tonic-gate 		else if (strcmp(look->service_name, "ldap") == 0)
4267c478bd9Sstevel@tonic-gate 			res = netname2user_ldap(&err, (char *)netname, &argp);
4277c478bd9Sstevel@tonic-gate 		else {
4287c478bd9Sstevel@tonic-gate 			syslog(LOG_INFO,
42936e852a1SRaja Andra 			    "netname2user: unknown nameservice for publickey"
43036e852a1SRaja Andra 			    "info '%s'\n", look->service_name);
4317c478bd9Sstevel@tonic-gate 			err = __NSW_UNAVAIL;
4327c478bd9Sstevel@tonic-gate 		}
4337c478bd9Sstevel@tonic-gate 		switch (look->actions[err]) {
4347c478bd9Sstevel@tonic-gate 			case __NSW_CONTINUE :
4357c478bd9Sstevel@tonic-gate 				break;
4367c478bd9Sstevel@tonic-gate 			case __NSW_RETURN :
4377c478bd9Sstevel@tonic-gate 				if (needfree)
438*5ad42b1bSSurya Prakki 					(void) __nsw_freeconfig(conf);
43961961e0fSrobinson 				(void) mutex_unlock(&serialize_netname_r);
4407c478bd9Sstevel@tonic-gate 				return (res);
4417c478bd9Sstevel@tonic-gate 			default :
4427c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
44336e852a1SRaja Andra 				    "netname2user: Unknown action for "
44436e852a1SRaja Andra 				    "nameservice '%s'", look->service_name);
4457c478bd9Sstevel@tonic-gate 		}
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 	if (needfree)
448*5ad42b1bSSurya Prakki 		(void) __nsw_freeconfig(conf);
44961961e0fSrobinson 	(void) mutex_unlock(&serialize_netname_r);
4507c478bd9Sstevel@tonic-gate 	return (0);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate /*
4547c478bd9Sstevel@tonic-gate  * Convert network-name to hostname (fully qualified)
4557c478bd9Sstevel@tonic-gate  * NOTE: this code only recognizes names of the form :
4567c478bd9Sstevel@tonic-gate  *		unix.HOST@domainname
4577c478bd9Sstevel@tonic-gate  *
4587c478bd9Sstevel@tonic-gate  * This is very simple.  Since the netname is of the form:
4597c478bd9Sstevel@tonic-gate  *	unix.host@domainname
4607c478bd9Sstevel@tonic-gate  * We just construct the hostname using information from the domainname.
4617c478bd9Sstevel@tonic-gate  */
4627c478bd9Sstevel@tonic-gate int
46361961e0fSrobinson netname2host(const char netname[MAXNETNAMELEN + 1], char *hostname,
46461961e0fSrobinson 							const int hostlen)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	char *p, *domainname;
4677c478bd9Sstevel@tonic-gate 	int len, dlen;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	if (!netname) {
4707c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "netname2host: null netname");
4717c478bd9Sstevel@tonic-gate 		goto bad_exit;
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	if (strncmp(netname, OPSYS, OPSYS_LEN) != 0)
4757c478bd9Sstevel@tonic-gate 		goto bad_netname;
4767c478bd9Sstevel@tonic-gate 	p = (char *)netname + OPSYS_LEN;	/* skip OPSYS part */
4777c478bd9Sstevel@tonic-gate 	if (*p != '.')
4787c478bd9Sstevel@tonic-gate 		goto bad_netname;
4797c478bd9Sstevel@tonic-gate 	++p;				/* skip '.' */
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	domainname = strchr(p, '@');	/* get domain name */
4827c478bd9Sstevel@tonic-gate 	if (domainname == 0)
4837c478bd9Sstevel@tonic-gate 		goto bad_netname;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	len = domainname - p;		/* host sits between '.' and '@' */
4867c478bd9Sstevel@tonic-gate 	domainname++;			/* skip '@' sign */
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	if (len <= 0)
4897c478bd9Sstevel@tonic-gate 		goto bad_netname;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	if (hostlen < len) {
4927c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
4937c478bd9Sstevel@tonic-gate 		    "netname2host: insufficient space for hostname");
4947c478bd9Sstevel@tonic-gate 		goto bad_exit;
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if (isdigit(*p))		/* don't want uid here */
4987c478bd9Sstevel@tonic-gate 		goto bad_netname;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	if (*p == '\0')			/* check for null hostname */
5017c478bd9Sstevel@tonic-gate 		goto bad_netname;
5027c478bd9Sstevel@tonic-gate 
50361961e0fSrobinson 	(void) strncpy(hostname, p, len);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	/* make into fully qualified hostname by concatenating domain part */
5067c478bd9Sstevel@tonic-gate 	dlen = strlen(domainname);
5077c478bd9Sstevel@tonic-gate 	if (hostlen < (len + dlen + 2)) {
5087c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5097c478bd9Sstevel@tonic-gate 		    "netname2host: insufficient space for hostname");
5107c478bd9Sstevel@tonic-gate 		goto bad_exit;
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	hostname[len] = '.';
51461961e0fSrobinson 	(void) strncpy(hostname+len+1, domainname, dlen);
5157c478bd9Sstevel@tonic-gate 	hostname[len+dlen+1] = '\0';
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	return (1);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate bad_netname:
5207c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, "netname2host: invalid host netname %s", netname);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate bad_exit:
5237c478bd9Sstevel@tonic-gate 	hostname[0] = '\0';
5247c478bd9Sstevel@tonic-gate 	return (0);
5257c478bd9Sstevel@tonic-gate }
526