xref: /titanic_51/usr/src/lib/libnsl/rpc/netnamer.c (revision d00075c72fe6e0468054c64c53d70554c1d1cb02)
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  */
26*d00075c7SMarcel Telka /*
27*d00075c7SMarcel Telka  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
28*d00075c7SMarcel Telka  */
297c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
307c478bd9Sstevel@tonic-gate /* All Rights Reserved */
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
337c478bd9Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
347c478bd9Sstevel@tonic-gate  * California.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * ==== hack-attack:  possibly MT-safe but definitely not MT-hot.
387c478bd9Sstevel@tonic-gate  * ==== turn this into a real switch frontend and backends
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * Well, at least the API doesn't involve pointers-to-static.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * netname utility routines convert from netnames to unix names (uid, gid)
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * This module is operating system dependent!
477c478bd9Sstevel@tonic-gate  * What we define here will work with any unix system that has adopted
487c478bd9Sstevel@tonic-gate  * the Sun NIS domain architecture.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #undef NIS
527c478bd9Sstevel@tonic-gate #include "mt.h"
537c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
547c478bd9Sstevel@tonic-gate #include <stdio.h>
557c478bd9Sstevel@tonic-gate #include <stdlib.h>
5667dbe2beSCasper H.S. Dik #include <unistd.h>
5767dbe2beSCasper H.S. Dik #include <alloca.h>
587c478bd9Sstevel@tonic-gate #include <sys/types.h>
597c478bd9Sstevel@tonic-gate #include <ctype.h>
607c478bd9Sstevel@tonic-gate #include <grp.h>
617c478bd9Sstevel@tonic-gate #include <pwd.h>
627c478bd9Sstevel@tonic-gate #include <string.h>
637c478bd9Sstevel@tonic-gate #include <syslog.h>
647c478bd9Sstevel@tonic-gate #include <sys/param.h>
657c478bd9Sstevel@tonic-gate #include <nsswitch.h>
667c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
677c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
687c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
697c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static const char    OPSYS[]	= "unix";
727c478bd9Sstevel@tonic-gate static const char    NETIDFILE[] = "/etc/netid";
737c478bd9Sstevel@tonic-gate static const char    NETID[]	= "netid.byname";
747c478bd9Sstevel@tonic-gate #define	OPSYS_LEN 4
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate extern int _getgroupsbymember(const char *, gid_t[], int, int);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * the value for NOBODY_UID is set by the SVID. The following define also
807c478bd9Sstevel@tonic-gate  * appears in netname.c
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #define	NOBODY_UID 60001
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  *	default publickey policy:
877c478bd9Sstevel@tonic-gate  *		publickey: nis [NOTFOUND = return] files
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*		NSW_NOTSUCCESS  NSW_NOTFOUND   NSW_UNAVAIL    NSW_TRYAGAIN */
927c478bd9Sstevel@tonic-gate #define	DEF_ACTION {__NSW_RETURN, __NSW_RETURN, __NSW_CONTINUE, __NSW_CONTINUE}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static struct __nsw_lookup lookup_files = {"files", DEF_ACTION, NULL, NULL},
957c478bd9Sstevel@tonic-gate 		lookup_nis = {"nis", DEF_ACTION, NULL, &lookup_files};
967c478bd9Sstevel@tonic-gate static struct __nsw_switchconfig publickey_default =
977c478bd9Sstevel@tonic-gate 			{0, "publickey", 2, &lookup_nis};
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static mutex_t serialize_netname_r = DEFAULTMUTEX;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate struct netid_userdata {
1027c478bd9Sstevel@tonic-gate 	uid_t	*uidp;
1037c478bd9Sstevel@tonic-gate 	gid_t	*gidp;
1047c478bd9Sstevel@tonic-gate 	int	*gidlenp;
1057c478bd9Sstevel@tonic-gate 	gid_t	*gidlist;
1067c478bd9Sstevel@tonic-gate };
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate static int
10961961e0fSrobinson parse_uid(char *s, struct netid_userdata *argp)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	uid_t	u;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if (!s || !isdigit(*s)) {
1147c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
1157c478bd9Sstevel@tonic-gate 		    "netname2user: expecting uid '%s'", s);
1167c478bd9Sstevel@tonic-gate 		return (__NSW_NOTFOUND); /* xxx need a better error */
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	/* Fetch the uid */
1207c478bd9Sstevel@tonic-gate 	u = (uid_t)(atoi(s));
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (u == 0) {
1237c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "netname2user: should not have uid 0");
1247c478bd9Sstevel@tonic-gate 		return (__NSW_NOTFOUND);
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 	*(argp->uidp) = u;
1277c478bd9Sstevel@tonic-gate 	return (__NSW_SUCCESS);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /* parse a comma separated gid list */
1327c478bd9Sstevel@tonic-gate static int
13361961e0fSrobinson parse_gidlist(char *p, struct netid_userdata *argp)
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate 	int len;
1367c478bd9Sstevel@tonic-gate 	gid_t	g;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (!p || (!isdigit(*p))) {
1397c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
1407c478bd9Sstevel@tonic-gate 		    "netname2user: missing group id list in '%s'.",
1417c478bd9Sstevel@tonic-gate 		    p);
1427c478bd9Sstevel@tonic-gate 		return (__NSW_NOTFOUND);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	g = (gid_t)(atoi(p));
1467c478bd9Sstevel@tonic-gate 	*(argp->gidp) = g;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	len = 0;
1497c478bd9Sstevel@tonic-gate 	while (p = strchr(p, ','))
1507c478bd9Sstevel@tonic-gate 		argp->gidlist[len++] = (gid_t)atoi(++p);
1517c478bd9Sstevel@tonic-gate 	*(argp->gidlenp) = len;
1527c478bd9Sstevel@tonic-gate 	return (__NSW_SUCCESS);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate  * parse_netid_str()
1587c478bd9Sstevel@tonic-gate  *
1597c478bd9Sstevel@tonic-gate  * Parse uid and group information from the passed string.
1607c478bd9Sstevel@tonic-gate  *
1617c478bd9Sstevel@tonic-gate  * The format of the string passed is
1627c478bd9Sstevel@tonic-gate  * 	uid:gid,grp,grp, ...
1637c478bd9Sstevel@tonic-gate  *
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate static int
16661961e0fSrobinson parse_netid_str(char *s, struct netid_userdata *argp)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	char	*p;
1697c478bd9Sstevel@tonic-gate 	int	err;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/* get uid */
1727c478bd9Sstevel@tonic-gate 	err = parse_uid(s, argp);
17361961e0fSrobinson 	if (err != __NSW_SUCCESS)
1747c478bd9Sstevel@tonic-gate 		return (err);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/* Now get the group list */
1777c478bd9Sstevel@tonic-gate 	p = strchr(s, ':');
1787c478bd9Sstevel@tonic-gate 	if (!p) {
1797c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
18061961e0fSrobinson 		    "netname2user: missing group id list in '%s'", s);
1817c478bd9Sstevel@tonic-gate 		return (__NSW_NOTFOUND);
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 	++p;			/* skip ':' */
1847c478bd9Sstevel@tonic-gate 	err = parse_gidlist(p, argp);
1857c478bd9Sstevel@tonic-gate 	return (err);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * netname2user_files()
1907c478bd9Sstevel@tonic-gate  *
1917c478bd9Sstevel@tonic-gate  * This routine fetches the netid information from the "files" nameservice.
1927c478bd9Sstevel@tonic-gate  * ie /etc/netid.
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate static int
19561961e0fSrobinson netname2user_files(int *err, char *netname, struct netid_userdata *argp)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	char 	buf[512];	/* one line from the file */
1987c478bd9Sstevel@tonic-gate 	char	*name;
1997c478bd9Sstevel@tonic-gate 	char	*value;
2007c478bd9Sstevel@tonic-gate 	char 	*res;
201004388ebScasper 	FILE	*fd;
2027c478bd9Sstevel@tonic-gate 
203004388ebScasper 	fd = fopen(NETIDFILE, "rF");
204004388ebScasper 	if (fd == NULL) {
2057c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
2067c478bd9Sstevel@tonic-gate 		return (0);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * for each line in the file parse it appropriately
2107c478bd9Sstevel@tonic-gate 	 * file format is :
2117c478bd9Sstevel@tonic-gate 	 *	netid	uid:grp,grp,grp # for users
2127c478bd9Sstevel@tonic-gate 	 *	netid	0:hostname	# for hosts
2137c478bd9Sstevel@tonic-gate 	 */
214004388ebScasper 	while (!feof(fd)) {
215004388ebScasper 		res = fgets(buf, 512, fd);
2167c478bd9Sstevel@tonic-gate 		if (res == NULL)
2177c478bd9Sstevel@tonic-gate 			break;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 		/* Skip comments and blank lines */
2207c478bd9Sstevel@tonic-gate 		if ((*res == '#') || (*res == '\n'))
2217c478bd9Sstevel@tonic-gate 			continue;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		name = &(buf[0]);
2247c478bd9Sstevel@tonic-gate 		while (isspace(*name))
2257c478bd9Sstevel@tonic-gate 			name++;
2267c478bd9Sstevel@tonic-gate 		if (*name == '\0')	/* blank line continue */
2277c478bd9Sstevel@tonic-gate 			continue;
2287c478bd9Sstevel@tonic-gate 		value = name;		/* will contain the value eventually */
2297c478bd9Sstevel@tonic-gate 		while (!isspace(*value))
2307c478bd9Sstevel@tonic-gate 			value++;
2317c478bd9Sstevel@tonic-gate 		if (*value == '\0') {
2327c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING,
2337c478bd9Sstevel@tonic-gate 			    "netname2user: badly formatted line in %s.",
2347c478bd9Sstevel@tonic-gate 			    NETIDFILE);
2357c478bd9Sstevel@tonic-gate 			continue;
2367c478bd9Sstevel@tonic-gate 		}
2377c478bd9Sstevel@tonic-gate 		*value++ = '\0'; /* nul terminate the name */
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		if (strcasecmp(name, netname) == 0) {
240004388ebScasper 			(void) fclose(fd);
2417c478bd9Sstevel@tonic-gate 			while (isspace(*value))
2427c478bd9Sstevel@tonic-gate 				value++;
2437c478bd9Sstevel@tonic-gate 			*err = parse_netid_str(value, argp);
2447c478bd9Sstevel@tonic-gate 			return (*err == __NSW_SUCCESS);
2457c478bd9Sstevel@tonic-gate 		}
2467c478bd9Sstevel@tonic-gate 	}
247004388ebScasper 	(void) fclose(fd);
2487c478bd9Sstevel@tonic-gate 	*err = __NSW_NOTFOUND;
2497c478bd9Sstevel@tonic-gate 	return (0);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  * netname2user_nis()
2547c478bd9Sstevel@tonic-gate  *
2557c478bd9Sstevel@tonic-gate  * This function reads the netid from the NIS (YP) nameservice.
2567c478bd9Sstevel@tonic-gate  */
2577c478bd9Sstevel@tonic-gate static int
25861961e0fSrobinson netname2user_nis(int *err, char *netname, struct netid_userdata *argp)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate 	char *domain;
2617c478bd9Sstevel@tonic-gate 	int yperr;
2627c478bd9Sstevel@tonic-gate 	char *lookup;
2637c478bd9Sstevel@tonic-gate 	int len;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	domain = strchr(netname, '@');
2667c478bd9Sstevel@tonic-gate 	if (!domain) {
2677c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
2687c478bd9Sstevel@tonic-gate 		return (0);
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/* Point past the '@' character */
2727c478bd9Sstevel@tonic-gate 	domain++;
2737c478bd9Sstevel@tonic-gate 	lookup = NULL;
2747c478bd9Sstevel@tonic-gate 	yperr = yp_match(domain, (char *)NETID, netname, strlen(netname),
2757c478bd9Sstevel@tonic-gate 	    &lookup, &len);
2767c478bd9Sstevel@tonic-gate 	switch (yperr) {
2777c478bd9Sstevel@tonic-gate 		case 0:
2787c478bd9Sstevel@tonic-gate 			break; /* the successful case */
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 		default :
2817c478bd9Sstevel@tonic-gate 			/*
2827c478bd9Sstevel@tonic-gate 			 *  XXX not sure about yp_match semantics.
2837c478bd9Sstevel@tonic-gate 			 * should err be set to NOTFOUND here?
2847c478bd9Sstevel@tonic-gate 			 */
2857c478bd9Sstevel@tonic-gate 			*err = __NSW_UNAVAIL;
2867c478bd9Sstevel@tonic-gate 			return (0);
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 	if (lookup) {
2897c478bd9Sstevel@tonic-gate 		lookup[len] = '\0';
2907c478bd9Sstevel@tonic-gate 		*err = parse_netid_str(lookup, argp);
2917c478bd9Sstevel@tonic-gate 		free(lookup);
2927c478bd9Sstevel@tonic-gate 		return (*err == __NSW_SUCCESS);
29361961e0fSrobinson 	}
2947c478bd9Sstevel@tonic-gate 	*err = __NSW_NOTFOUND;
2957c478bd9Sstevel@tonic-gate 	return (0);
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate /*
2997c478bd9Sstevel@tonic-gate  * Build the uid and gid from the netname for users in LDAP.
3007c478bd9Sstevel@tonic-gate  * There is no netid container in LDAP. For this we build
3017c478bd9Sstevel@tonic-gate  * the netname to user data dynamically from the passwd and
3027c478bd9Sstevel@tonic-gate  * group data. This works only for users in a single domain.
3037c478bd9Sstevel@tonic-gate  * This function is an interim solution until we support a
3047c478bd9Sstevel@tonic-gate  * netid container in LDAP which enables us to do netname2user
3057c478bd9Sstevel@tonic-gate  * resolution for multiple domains.
3067c478bd9Sstevel@tonic-gate  */
3077c478bd9Sstevel@tonic-gate static int
30861961e0fSrobinson netname2user_ldap(int *err, char *netname, struct netid_userdata *argp)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	char buf[NSS_LINELEN_PASSWD];
31161961e0fSrobinson 	char *p2, *lasts;
3127c478bd9Sstevel@tonic-gate 	struct passwd pw;
3137c478bd9Sstevel@tonic-gate 	uid_t uidnu;
3147c478bd9Sstevel@tonic-gate 	int ngroups = 0;
3157c478bd9Sstevel@tonic-gate 	int count;
3167c478bd9Sstevel@tonic-gate 	char pwbuf[NSS_LINELEN_PASSWD];
31767dbe2beSCasper H.S. Dik 	int maxgrp = sysconf(_SC_NGROUPS_MAX);
31867dbe2beSCasper H.S. Dik 	gid_t *groups = alloca(maxgrp * sizeof (gid_t));
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	if (strlcpy(buf, netname, NSS_LINELEN_PASSWD) >= NSS_LINELEN_PASSWD) {
3217c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
3227c478bd9Sstevel@tonic-gate 		return (0);
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	/* get the uid from the netname */
32661961e0fSrobinson 	if (strtok_r(buf, ".", &lasts) == NULL) {
3277c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
3287c478bd9Sstevel@tonic-gate 		return (0);
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 	if ((p2 = strtok_r(NULL, "@", &lasts)) == NULL) {
3317c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
3327c478bd9Sstevel@tonic-gate 		return (0);
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 	uidnu = atoi(p2);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	/*
3377c478bd9Sstevel@tonic-gate 	 * check out the primary group and crosscheck the uid
3387c478bd9Sstevel@tonic-gate 	 * with the passwd data
3397c478bd9Sstevel@tonic-gate 	 */
3407c478bd9Sstevel@tonic-gate 	if ((getpwuid_r(uidnu, &pw, pwbuf, sizeof (pwbuf))) == NULL) {
3417c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
3427c478bd9Sstevel@tonic-gate 		return (0);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	*(argp->uidp) = pw.pw_uid;
3467c478bd9Sstevel@tonic-gate 	*(argp->gidp) = pw.pw_gid;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/* search through all groups for membership */
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	groups[0] = pw.pw_gid;
3517c478bd9Sstevel@tonic-gate 
35267dbe2beSCasper H.S. Dik 	ngroups = _getgroupsbymember(pw.pw_name, groups, maxgrp,
353f48205beScasper 	    (pw.pw_gid <= MAXUID) ? 1 : 0);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	if (ngroups < 0) {
3567c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
3577c478bd9Sstevel@tonic-gate 		return (0);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	*(argp->gidlenp) = ngroups;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	for (count = 0; count < ngroups; count++) {
3637c478bd9Sstevel@tonic-gate 		(argp->gidlist[count]) = groups[count];
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	*err = __NSW_SUCCESS;
3677c478bd9Sstevel@tonic-gate 	return (1);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate /*
3727c478bd9Sstevel@tonic-gate  * Convert network-name into unix credential
3737c478bd9Sstevel@tonic-gate  */
3747c478bd9Sstevel@tonic-gate int
37561961e0fSrobinson netname2user(const char netname[MAXNETNAMELEN + 1], uid_t *uidp, gid_t *gidp,
37661961e0fSrobinson 						int *gidlenp, gid_t *gidlist)
3777c478bd9Sstevel@tonic-gate {
3787c478bd9Sstevel@tonic-gate 	struct __nsw_switchconfig *conf;
3797c478bd9Sstevel@tonic-gate 	struct __nsw_lookup *look;
3807c478bd9Sstevel@tonic-gate 	enum __nsw_parse_err perr;
3817c478bd9Sstevel@tonic-gate 	int needfree = 1, res;
3827c478bd9Sstevel@tonic-gate 	struct netid_userdata argp;
3837c478bd9Sstevel@tonic-gate 	int err;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	/*
3867c478bd9Sstevel@tonic-gate 	 * Take care of the special case of nobody. Compare the netname
3877c478bd9Sstevel@tonic-gate 	 * to the string "nobody". If they are equal, return the SVID
3887c478bd9Sstevel@tonic-gate 	 * standard value for nobody.
3897c478bd9Sstevel@tonic-gate 	 */
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if (strcmp(netname, "nobody") == 0) {
3927c478bd9Sstevel@tonic-gate 		*uidp = NOBODY_UID;
3937c478bd9Sstevel@tonic-gate 		*gidp = NOBODY_UID;
3947c478bd9Sstevel@tonic-gate 		*gidlenp = 0;
3957c478bd9Sstevel@tonic-gate 		return (1);
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	/*
3997c478bd9Sstevel@tonic-gate 	 * First we do some generic sanity checks on the name we were
4007c478bd9Sstevel@tonic-gate 	 * passed. This lets us assume they are correct in the backends.
4017c478bd9Sstevel@tonic-gate 	 *
4027c478bd9Sstevel@tonic-gate 	 * NOTE: this code only recognizes names of the form :
4037c478bd9Sstevel@tonic-gate 	 *		unix.UID@domainname
4047c478bd9Sstevel@tonic-gate 	 */
40561961e0fSrobinson 	if (strncmp(netname, OPSYS, OPSYS_LEN) != 0)
4067c478bd9Sstevel@tonic-gate 		return (0);
40761961e0fSrobinson 	if (!isdigit(netname[OPSYS_LEN+1]))	/* check for uid string */
4087c478bd9Sstevel@tonic-gate 		return (0);
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	argp.uidp = uidp;
4117c478bd9Sstevel@tonic-gate 	argp.gidp = gidp;
4127c478bd9Sstevel@tonic-gate 	argp.gidlenp = gidlenp;
4137c478bd9Sstevel@tonic-gate 	argp.gidlist = gidlist;
41461961e0fSrobinson 	(void) mutex_lock(&serialize_netname_r);
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	conf = __nsw_getconfig("publickey", &perr);
4177c478bd9Sstevel@tonic-gate 	if (!conf) {
4187c478bd9Sstevel@tonic-gate 		conf = &publickey_default;
4197c478bd9Sstevel@tonic-gate 		needfree = 0;
4207c478bd9Sstevel@tonic-gate 	} else
4217c478bd9Sstevel@tonic-gate 		needfree = 1; /* free the config structure */
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	for (look = conf->lookups; look; look = look->next) {
42436e852a1SRaja Andra 		if (strcmp(look->service_name, "nis") == 0)
4257c478bd9Sstevel@tonic-gate 			res = netname2user_nis(&err, (char *)netname, &argp);
4267c478bd9Sstevel@tonic-gate 		else if (strcmp(look->service_name, "files") == 0)
4277c478bd9Sstevel@tonic-gate 			res = netname2user_files(&err, (char *)netname, &argp);
4287c478bd9Sstevel@tonic-gate 		else if (strcmp(look->service_name, "ldap") == 0)
4297c478bd9Sstevel@tonic-gate 			res = netname2user_ldap(&err, (char *)netname, &argp);
4307c478bd9Sstevel@tonic-gate 		else {
4317c478bd9Sstevel@tonic-gate 			syslog(LOG_INFO,
43236e852a1SRaja Andra 			    "netname2user: unknown nameservice for publickey"
43336e852a1SRaja Andra 			    "info '%s'\n", look->service_name);
4347c478bd9Sstevel@tonic-gate 			err = __NSW_UNAVAIL;
435*d00075c7SMarcel Telka 			res = 0;
4367c478bd9Sstevel@tonic-gate 		}
4377c478bd9Sstevel@tonic-gate 		switch (look->actions[err]) {
4387c478bd9Sstevel@tonic-gate 			case __NSW_CONTINUE :
4397c478bd9Sstevel@tonic-gate 				break;
4407c478bd9Sstevel@tonic-gate 			case __NSW_RETURN :
4417c478bd9Sstevel@tonic-gate 				if (needfree)
4425ad42b1bSSurya Prakki 					(void) __nsw_freeconfig(conf);
44361961e0fSrobinson 				(void) mutex_unlock(&serialize_netname_r);
4447c478bd9Sstevel@tonic-gate 				return (res);
4457c478bd9Sstevel@tonic-gate 			default :
4467c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
44736e852a1SRaja Andra 				    "netname2user: Unknown action for "
44836e852a1SRaja Andra 				    "nameservice '%s'", look->service_name);
4497c478bd9Sstevel@tonic-gate 		}
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 	if (needfree)
4525ad42b1bSSurya Prakki 		(void) __nsw_freeconfig(conf);
45361961e0fSrobinson 	(void) mutex_unlock(&serialize_netname_r);
4547c478bd9Sstevel@tonic-gate 	return (0);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate /*
4587c478bd9Sstevel@tonic-gate  * Convert network-name to hostname (fully qualified)
4597c478bd9Sstevel@tonic-gate  * NOTE: this code only recognizes names of the form :
4607c478bd9Sstevel@tonic-gate  *		unix.HOST@domainname
4617c478bd9Sstevel@tonic-gate  *
4627c478bd9Sstevel@tonic-gate  * This is very simple.  Since the netname is of the form:
4637c478bd9Sstevel@tonic-gate  *	unix.host@domainname
4647c478bd9Sstevel@tonic-gate  * We just construct the hostname using information from the domainname.
4657c478bd9Sstevel@tonic-gate  */
4667c478bd9Sstevel@tonic-gate int
46761961e0fSrobinson netname2host(const char netname[MAXNETNAMELEN + 1], char *hostname,
46861961e0fSrobinson 							const int hostlen)
4697c478bd9Sstevel@tonic-gate {
4707c478bd9Sstevel@tonic-gate 	char *p, *domainname;
4717c478bd9Sstevel@tonic-gate 	int len, dlen;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	if (!netname) {
4747c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "netname2host: null netname");
4757c478bd9Sstevel@tonic-gate 		goto bad_exit;
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	if (strncmp(netname, OPSYS, OPSYS_LEN) != 0)
4797c478bd9Sstevel@tonic-gate 		goto bad_netname;
4807c478bd9Sstevel@tonic-gate 	p = (char *)netname + OPSYS_LEN;	/* skip OPSYS part */
4817c478bd9Sstevel@tonic-gate 	if (*p != '.')
4827c478bd9Sstevel@tonic-gate 		goto bad_netname;
4837c478bd9Sstevel@tonic-gate 	++p;				/* skip '.' */
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	domainname = strchr(p, '@');	/* get domain name */
4867c478bd9Sstevel@tonic-gate 	if (domainname == 0)
4877c478bd9Sstevel@tonic-gate 		goto bad_netname;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	len = domainname - p;		/* host sits between '.' and '@' */
4907c478bd9Sstevel@tonic-gate 	domainname++;			/* skip '@' sign */
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	if (len <= 0)
4937c478bd9Sstevel@tonic-gate 		goto bad_netname;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	if (hostlen < len) {
4967c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
4977c478bd9Sstevel@tonic-gate 		    "netname2host: insufficient space for hostname");
4987c478bd9Sstevel@tonic-gate 		goto bad_exit;
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if (isdigit(*p))		/* don't want uid here */
5027c478bd9Sstevel@tonic-gate 		goto bad_netname;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	if (*p == '\0')			/* check for null hostname */
5057c478bd9Sstevel@tonic-gate 		goto bad_netname;
5067c478bd9Sstevel@tonic-gate 
50761961e0fSrobinson 	(void) strncpy(hostname, p, len);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	/* make into fully qualified hostname by concatenating domain part */
5107c478bd9Sstevel@tonic-gate 	dlen = strlen(domainname);
5117c478bd9Sstevel@tonic-gate 	if (hostlen < (len + dlen + 2)) {
5127c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5137c478bd9Sstevel@tonic-gate 		    "netname2host: insufficient space for hostname");
5147c478bd9Sstevel@tonic-gate 		goto bad_exit;
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	hostname[len] = '.';
51861961e0fSrobinson 	(void) strncpy(hostname+len+1, domainname, dlen);
5197c478bd9Sstevel@tonic-gate 	hostname[len+dlen+1] = '\0';
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	return (1);
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate bad_netname:
5247c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, "netname2host: invalid host netname %s", netname);
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate bad_exit:
5277c478bd9Sstevel@tonic-gate 	hostname[0] = '\0';
5287c478bd9Sstevel@tonic-gate 	return (0);
5297c478bd9Sstevel@tonic-gate }
530