xref: /titanic_51/usr/src/lib/libnsl/rpc/netnamer.c (revision 61961e0f20c7637a3846bb39786bb9dffa91dfb9)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*61961e0fSrobinson  */
22*61961e0fSrobinson 
23*61961e0fSrobinson /*
247c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
317c478bd9Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
327c478bd9Sstevel@tonic-gate  * California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * ==== hack-attack:  possibly MT-safe but definitely not MT-hot.
367c478bd9Sstevel@tonic-gate  * ==== turn this into a real switch frontend and backends
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * Well, at least the API doesn't involve pointers-to-static.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
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>
567c478bd9Sstevel@tonic-gate #include <sys/types.h>
577c478bd9Sstevel@tonic-gate #include <ctype.h>
587c478bd9Sstevel@tonic-gate #include <grp.h>
597c478bd9Sstevel@tonic-gate #include <pwd.h>
607c478bd9Sstevel@tonic-gate #include <string.h>
617c478bd9Sstevel@tonic-gate #include <syslog.h>
627c478bd9Sstevel@tonic-gate #include <sys/param.h>
637c478bd9Sstevel@tonic-gate #include <nsswitch.h>
647c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
657c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
667c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
677c478bd9Sstevel@tonic-gate #include "nsl_stdio_prv.h"
687c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static const char    OPSYS[]	= "unix";
717c478bd9Sstevel@tonic-gate static const char    NETIDFILE[] = "/etc/netid";
727c478bd9Sstevel@tonic-gate static const char    NETID[]	= "netid.byname";
737c478bd9Sstevel@tonic-gate static const char    PKTABLE[]  = "cred.org_dir";
747c478bd9Sstevel@tonic-gate #define	PKTABLE_LEN 12
757c478bd9Sstevel@tonic-gate #define	OPSYS_LEN 4
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #ifndef NGROUPS
787c478bd9Sstevel@tonic-gate #define	NGROUPS 16
797c478bd9Sstevel@tonic-gate #endif
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate extern int _getgroupsbymember(const char *, gid_t[], int, int);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * the value for NOBODY_UID is set by the SVID. The following define also
857c478bd9Sstevel@tonic-gate  * appears in netname.c
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #define	NOBODY_UID 60001
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  *	default publickey policy:
927c478bd9Sstevel@tonic-gate  *		publickey: nis [NOTFOUND = return] files
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*		NSW_NOTSUCCESS  NSW_NOTFOUND   NSW_UNAVAIL    NSW_TRYAGAIN */
977c478bd9Sstevel@tonic-gate #define	DEF_ACTION {__NSW_RETURN, __NSW_RETURN, __NSW_CONTINUE, __NSW_CONTINUE}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static struct __nsw_lookup lookup_files = {"files", DEF_ACTION, NULL, NULL},
1007c478bd9Sstevel@tonic-gate 		lookup_nis = {"nis", DEF_ACTION, NULL, &lookup_files};
1017c478bd9Sstevel@tonic-gate static struct __nsw_switchconfig publickey_default =
1027c478bd9Sstevel@tonic-gate 			{0, "publickey", 2, &lookup_nis};
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static mutex_t serialize_netname_r = DEFAULTMUTEX;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate struct netid_userdata {
1077c478bd9Sstevel@tonic-gate 	uid_t	*uidp;
1087c478bd9Sstevel@tonic-gate 	gid_t	*gidp;
1097c478bd9Sstevel@tonic-gate 	int	*gidlenp;
1107c478bd9Sstevel@tonic-gate 	gid_t	*gidlist;
1117c478bd9Sstevel@tonic-gate };
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static int
114*61961e0fSrobinson parse_uid(char *s, struct netid_userdata *argp)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	uid_t	u;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (!s || !isdigit(*s)) {
1197c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
1207c478bd9Sstevel@tonic-gate 			"netname2user: expecting uid '%s'", s);
1217c478bd9Sstevel@tonic-gate 		return (__NSW_NOTFOUND); /* xxx need a better error */
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	/* Fetch the uid */
1257c478bd9Sstevel@tonic-gate 	u = (uid_t)(atoi(s));
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (u == 0) {
1287c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "netname2user: should not have uid 0");
1297c478bd9Sstevel@tonic-gate 		return (__NSW_NOTFOUND);
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 	*(argp->uidp) = u;
1327c478bd9Sstevel@tonic-gate 	return (__NSW_SUCCESS);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /* parse a comma separated gid list */
1377c478bd9Sstevel@tonic-gate static int
138*61961e0fSrobinson parse_gidlist(char *p, struct netid_userdata *argp)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	int len;
1417c478bd9Sstevel@tonic-gate 	gid_t	g;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (!p || (!isdigit(*p))) {
1447c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
1457c478bd9Sstevel@tonic-gate 			"netname2user: missing group id list in '%s'.",
1467c478bd9Sstevel@tonic-gate 			p);
1477c478bd9Sstevel@tonic-gate 		return (__NSW_NOTFOUND);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	g = (gid_t)(atoi(p));
1517c478bd9Sstevel@tonic-gate 	*(argp->gidp) = g;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	len = 0;
1547c478bd9Sstevel@tonic-gate 	while (p = strchr(p, ','))
1557c478bd9Sstevel@tonic-gate 		argp->gidlist[len++] = (gid_t)atoi(++p);
1567c478bd9Sstevel@tonic-gate 	*(argp->gidlenp) = len;
1577c478bd9Sstevel@tonic-gate 	return (__NSW_SUCCESS);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate  * parse_netid_str()
1637c478bd9Sstevel@tonic-gate  *
1647c478bd9Sstevel@tonic-gate  * Parse uid and group information from the passed string.
1657c478bd9Sstevel@tonic-gate  *
1667c478bd9Sstevel@tonic-gate  * The format of the string passed is
1677c478bd9Sstevel@tonic-gate  * 	uid:gid,grp,grp, ...
1687c478bd9Sstevel@tonic-gate  *
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate static int
171*61961e0fSrobinson parse_netid_str(char *s, struct netid_userdata *argp)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	char	*p;
1747c478bd9Sstevel@tonic-gate 	int	err;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/* get uid */
1777c478bd9Sstevel@tonic-gate 	err = parse_uid(s, argp);
178*61961e0fSrobinson 	if (err != __NSW_SUCCESS)
1797c478bd9Sstevel@tonic-gate 		return (err);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/* Now get the group list */
1827c478bd9Sstevel@tonic-gate 	p = strchr(s, ':');
1837c478bd9Sstevel@tonic-gate 	if (!p) {
1847c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
185*61961e0fSrobinson 			"netname2user: missing group id list in '%s'", s);
1867c478bd9Sstevel@tonic-gate 		return (__NSW_NOTFOUND);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 	++p;			/* skip ':' */
1897c478bd9Sstevel@tonic-gate 	err = parse_gidlist(p, argp);
1907c478bd9Sstevel@tonic-gate 	return (err);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate static int
194*61961e0fSrobinson parse_uid_gidlist(char *ustr, char *gstr, struct netid_userdata *argp)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	int	err;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	/* get uid */
1997c478bd9Sstevel@tonic-gate 	err = parse_uid(ustr, argp);
200*61961e0fSrobinson 	if (err != __NSW_SUCCESS)
2017c478bd9Sstevel@tonic-gate 		return (err);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/* Now get the group list */
204*61961e0fSrobinson 	return (parse_gidlist(gstr, argp));
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * netname2user_files()
2107c478bd9Sstevel@tonic-gate  *
2117c478bd9Sstevel@tonic-gate  * This routine fetches the netid information from the "files" nameservice.
2127c478bd9Sstevel@tonic-gate  * ie /etc/netid.
2137c478bd9Sstevel@tonic-gate  */
2147c478bd9Sstevel@tonic-gate static int
215*61961e0fSrobinson netname2user_files(int *err, char *netname, struct netid_userdata *argp)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	char 	buf[512];	/* one line from the file */
2187c478bd9Sstevel@tonic-gate 	char	*name;
2197c478bd9Sstevel@tonic-gate 	char	*value;
2207c478bd9Sstevel@tonic-gate 	char 	*res;
2217c478bd9Sstevel@tonic-gate 	__NSL_FILE *fd;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	fd = __nsl_fopen(NETIDFILE, "r");
2247c478bd9Sstevel@tonic-gate 	if (fd == (__NSL_FILE *)0) {
2257c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
2267c478bd9Sstevel@tonic-gate 		return (0);
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 	/*
2297c478bd9Sstevel@tonic-gate 	 * for each line in the file parse it appropriately
2307c478bd9Sstevel@tonic-gate 	 * file format is :
2317c478bd9Sstevel@tonic-gate 	 *	netid	uid:grp,grp,grp # for users
2327c478bd9Sstevel@tonic-gate 	 *	netid	0:hostname	# for hosts
2337c478bd9Sstevel@tonic-gate 	 */
2347c478bd9Sstevel@tonic-gate 	while (!__nsl_feof(fd)) {
2357c478bd9Sstevel@tonic-gate 		res = __nsl_fgets(buf, 512, fd);
2367c478bd9Sstevel@tonic-gate 		if (res == NULL)
2377c478bd9Sstevel@tonic-gate 			break;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		/* Skip comments and blank lines */
2407c478bd9Sstevel@tonic-gate 		if ((*res == '#') || (*res == '\n'))
2417c478bd9Sstevel@tonic-gate 			continue;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		name = &(buf[0]);
2447c478bd9Sstevel@tonic-gate 		while (isspace(*name))
2457c478bd9Sstevel@tonic-gate 			name++;
2467c478bd9Sstevel@tonic-gate 		if (*name == '\0')	/* blank line continue */
2477c478bd9Sstevel@tonic-gate 			continue;
2487c478bd9Sstevel@tonic-gate 		value = name;		/* will contain the value eventually */
2497c478bd9Sstevel@tonic-gate 		while (!isspace(*value))
2507c478bd9Sstevel@tonic-gate 			value++;
2517c478bd9Sstevel@tonic-gate 		if (*value == '\0') {
2527c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING,
2537c478bd9Sstevel@tonic-gate 				"netname2user: badly formatted line in %s.",
2547c478bd9Sstevel@tonic-gate 				NETIDFILE);
2557c478bd9Sstevel@tonic-gate 			continue;
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 		*value++ = '\0'; /* nul terminate the name */
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 		if (strcasecmp(name, netname) == 0) {
260*61961e0fSrobinson 			(void) __nsl_fclose(fd);
2617c478bd9Sstevel@tonic-gate 			while (isspace(*value))
2627c478bd9Sstevel@tonic-gate 				value++;
2637c478bd9Sstevel@tonic-gate 			*err = parse_netid_str(value, argp);
2647c478bd9Sstevel@tonic-gate 			return (*err == __NSW_SUCCESS);
2657c478bd9Sstevel@tonic-gate 		}
2667c478bd9Sstevel@tonic-gate 	}
267*61961e0fSrobinson 	(void) __nsl_fclose(fd);
2687c478bd9Sstevel@tonic-gate 	*err = __NSW_NOTFOUND;
2697c478bd9Sstevel@tonic-gate 	return (0);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  * netname2user_nis()
2747c478bd9Sstevel@tonic-gate  *
2757c478bd9Sstevel@tonic-gate  * This function reads the netid from the NIS (YP) nameservice.
2767c478bd9Sstevel@tonic-gate  */
2777c478bd9Sstevel@tonic-gate static int
278*61961e0fSrobinson netname2user_nis(int *err, char *netname, struct netid_userdata *argp)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	char *domain;
2817c478bd9Sstevel@tonic-gate 	int yperr;
2827c478bd9Sstevel@tonic-gate 	char *lookup;
2837c478bd9Sstevel@tonic-gate 	int len;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	domain = strchr(netname, '@');
2867c478bd9Sstevel@tonic-gate 	if (!domain) {
2877c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
2887c478bd9Sstevel@tonic-gate 		return (0);
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	/* Point past the '@' character */
2927c478bd9Sstevel@tonic-gate 	domain++;
2937c478bd9Sstevel@tonic-gate 	lookup = NULL;
2947c478bd9Sstevel@tonic-gate 	yperr = yp_match(domain, (char *)NETID, netname, strlen(netname),
2957c478bd9Sstevel@tonic-gate 			&lookup, &len);
2967c478bd9Sstevel@tonic-gate 	switch (yperr) {
2977c478bd9Sstevel@tonic-gate 		case 0:
2987c478bd9Sstevel@tonic-gate 			break; /* the successful case */
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 		default :
3017c478bd9Sstevel@tonic-gate 			/*
3027c478bd9Sstevel@tonic-gate 			 *  XXX not sure about yp_match semantics.
3037c478bd9Sstevel@tonic-gate 			 * should err be set to NOTFOUND here?
3047c478bd9Sstevel@tonic-gate 			 */
3057c478bd9Sstevel@tonic-gate 			*err = __NSW_UNAVAIL;
3067c478bd9Sstevel@tonic-gate 			return (0);
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 	if (lookup) {
3097c478bd9Sstevel@tonic-gate 		lookup[len] = '\0';
3107c478bd9Sstevel@tonic-gate 		*err = parse_netid_str(lookup, argp);
3117c478bd9Sstevel@tonic-gate 		free(lookup);
3127c478bd9Sstevel@tonic-gate 		return (*err == __NSW_SUCCESS);
313*61961e0fSrobinson 	}
3147c478bd9Sstevel@tonic-gate 	*err = __NSW_NOTFOUND;
3157c478bd9Sstevel@tonic-gate 	return (0);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate /*
3197c478bd9Sstevel@tonic-gate  * Obtain user information (uid, gidlist) from nisplus.
3207c478bd9Sstevel@tonic-gate  * What we're trying to do here is to map a netname into
3217c478bd9Sstevel@tonic-gate  * local unix information (uid, gids), relevant in
3227c478bd9Sstevel@tonic-gate  * the *local* domain.
3237c478bd9Sstevel@tonic-gate  *
3247c478bd9Sstevel@tonic-gate  *	 cname   auth_type auth_name public  private
3257c478bd9Sstevel@tonic-gate  * ----------------------------------------------------------
3267c478bd9Sstevel@tonic-gate  *	nisname   DES     netname   pubkey  prikey
3277c478bd9Sstevel@tonic-gate  *	nisname   LOCAL   uid       gidlist
3287c478bd9Sstevel@tonic-gate  *
3297c478bd9Sstevel@tonic-gate  * 1.  Find out which 'home' domain to look for user's DES entry.
3307c478bd9Sstevel@tonic-gate  *	This is gotten from the domain part of the netname.
3317c478bd9Sstevel@tonic-gate  * 2.  Get the nisplus principal name from the DES entry in the cred
3327c478bd9Sstevel@tonic-gate  *	table of user's home domain.
3337c478bd9Sstevel@tonic-gate  * 3.  Use the nisplus principal name and search in the cred table of
3347c478bd9Sstevel@tonic-gate  *	the *local* directory for the LOCAL entry.
3357c478bd9Sstevel@tonic-gate  *
3367c478bd9Sstevel@tonic-gate  * Note that we need this translation of netname to <uid,gidlist> to be
3377c478bd9Sstevel@tonic-gate  * secure, so we *must* use authenticated connections.
3387c478bd9Sstevel@tonic-gate  */
3397c478bd9Sstevel@tonic-gate static int
340*61961e0fSrobinson netname2user_nisplus(int *err, char *netname, struct netid_userdata *argp)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	char *domain;
3437c478bd9Sstevel@tonic-gate 	nis_result *res;
3447c478bd9Sstevel@tonic-gate 	char	sname[NIS_MAXNAMELEN+1]; /*  search criteria + table name */
3457c478bd9Sstevel@tonic-gate 	char	principal[NIS_MAXNAMELEN+1];
3467c478bd9Sstevel@tonic-gate 	int len;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/* 1.  Get home domain of user. */
3497c478bd9Sstevel@tonic-gate 	domain = strchr(netname, '@');
3507c478bd9Sstevel@tonic-gate 	if (!domain) {
3517c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
3527c478bd9Sstevel@tonic-gate 		return (0);
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 	domain++;  /* skip '@' */
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	/* 2.  Get user's nisplus principal name.  */
3587c478bd9Sstevel@tonic-gate 	if ((strlen(netname)+strlen(domain)+PKTABLE_LEN+32) >
3597c478bd9Sstevel@tonic-gate 		(size_t)NIS_MAXNAMELEN) {
3607c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
3617c478bd9Sstevel@tonic-gate 		return (0);
3627c478bd9Sstevel@tonic-gate 	}
363*61961e0fSrobinson 	(void) snprintf(sname, sizeof (sname),
364*61961e0fSrobinson 		"[auth_name=\"%s\",auth_type=DES],%s.%s",
3657c478bd9Sstevel@tonic-gate 		netname, PKTABLE, domain);
3667c478bd9Sstevel@tonic-gate 	if (sname[strlen(sname) - 1] != '.')
367*61961e0fSrobinson 		(void) strcat(sname, ".");
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	/* must use authenticated call here */
3707c478bd9Sstevel@tonic-gate 	/* XXX but we cant, for now. XXX */
3717c478bd9Sstevel@tonic-gate 	res = nis_list(sname, USE_DGRAM+NO_AUTHINFO+FOLLOW_LINKS+FOLLOW_PATH,
3727c478bd9Sstevel@tonic-gate 	    NULL, NULL);
3737c478bd9Sstevel@tonic-gate 	switch (res->status) {
3747c478bd9Sstevel@tonic-gate 	case NIS_SUCCESS:
3757c478bd9Sstevel@tonic-gate 	case NIS_S_SUCCESS:
3767c478bd9Sstevel@tonic-gate 		break;   /* go and do something useful */
3777c478bd9Sstevel@tonic-gate 	case NIS_NOTFOUND:
3787c478bd9Sstevel@tonic-gate 	case NIS_PARTIAL:
3797c478bd9Sstevel@tonic-gate 	case NIS_NOSUCHNAME:
3807c478bd9Sstevel@tonic-gate 	case NIS_NOSUCHTABLE:
3817c478bd9Sstevel@tonic-gate 		*err = __NSW_NOTFOUND;
3827c478bd9Sstevel@tonic-gate 		nis_freeresult(res);
3837c478bd9Sstevel@tonic-gate 		return (0);
3847c478bd9Sstevel@tonic-gate 	case NIS_S_NOTFOUND:
3857c478bd9Sstevel@tonic-gate 	case NIS_TRYAGAIN:
3867c478bd9Sstevel@tonic-gate 		*err = __NSW_TRYAGAIN;
3877c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
3887c478bd9Sstevel@tonic-gate 			"netname2user: (nis+ lookup): %s\n",
3897c478bd9Sstevel@tonic-gate 			nis_sperrno(res->status));
3907c478bd9Sstevel@tonic-gate 		nis_freeresult(res);
3917c478bd9Sstevel@tonic-gate 		return (0);
3927c478bd9Sstevel@tonic-gate 	default:
3937c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
3947c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "netname2user: (nis+ lookup): %s\n",
3957c478bd9Sstevel@tonic-gate 			nis_sperrno(res->status));
3967c478bd9Sstevel@tonic-gate 		nis_freeresult(res);
3977c478bd9Sstevel@tonic-gate 		return (0);
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	if (res->objects.objects_len > 1) {
4017c478bd9Sstevel@tonic-gate 		/*
4027c478bd9Sstevel@tonic-gate 		 * A netname belonging to more than one principal?
4037c478bd9Sstevel@tonic-gate 		 * Something wrong with cred table. should be unique.
4047c478bd9Sstevel@tonic-gate 		 * Warn user and continue.
4057c478bd9Sstevel@tonic-gate 		 */
4067c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT,
4077c478bd9Sstevel@tonic-gate 			"netname2user: DES entry for %s in \
4087c478bd9Sstevel@tonic-gate 			directory %s not unique",
4097c478bd9Sstevel@tonic-gate 			netname, domain);
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	len = ENTRY_LEN(res->objects.objects_val, 0);
413*61961e0fSrobinson 	(void) strncpy(principal, ENTRY_VAL(res->objects.objects_val, 0), len);
4147c478bd9Sstevel@tonic-gate 	principal[len] = '\0';
4157c478bd9Sstevel@tonic-gate 	nis_freeresult(res);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if (principal[0] == '\0') {
4187c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
4197c478bd9Sstevel@tonic-gate 		return (0);
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	/*
4237c478bd9Sstevel@tonic-gate 	 *	3.  Use principal name to look up uid/gid information in
4247c478bd9Sstevel@tonic-gate 	 *	LOCAL entry in **local** cred table.
4257c478bd9Sstevel@tonic-gate 	 */
4267c478bd9Sstevel@tonic-gate 	domain = nis_local_directory();
4277c478bd9Sstevel@tonic-gate 	if ((strlen(principal)+strlen(domain)+PKTABLE_LEN+30) >
4287c478bd9Sstevel@tonic-gate 		(size_t)NIS_MAXNAMELEN) {
4297c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
4307c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "netname2user: principal name '%s' too long",
4317c478bd9Sstevel@tonic-gate 			principal);
4327c478bd9Sstevel@tonic-gate 		return (0);
4337c478bd9Sstevel@tonic-gate 	}
434*61961e0fSrobinson 	(void) snprintf(sname, sizeof (sname),
435*61961e0fSrobinson 		"[cname=\"%s\",auth_type=LOCAL],%s.%s",
4367c478bd9Sstevel@tonic-gate 		principal, PKTABLE, domain);
4377c478bd9Sstevel@tonic-gate 	if (sname[strlen(sname) - 1] != '.')
438*61961e0fSrobinson 		(void) strcat(sname, ".");
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	/* must use authenticated call here */
4417c478bd9Sstevel@tonic-gate 	/* XXX but we cant, for now. XXX */
4427c478bd9Sstevel@tonic-gate 	res = nis_list(sname, USE_DGRAM+NO_AUTHINFO+FOLLOW_LINKS+FOLLOW_PATH,
4437c478bd9Sstevel@tonic-gate 	    NULL, NULL);
4447c478bd9Sstevel@tonic-gate 	switch (res->status) {
4457c478bd9Sstevel@tonic-gate 	case NIS_NOTFOUND:
4467c478bd9Sstevel@tonic-gate 	case NIS_PARTIAL:
4477c478bd9Sstevel@tonic-gate 	case NIS_NOSUCHNAME:
4487c478bd9Sstevel@tonic-gate 	case NIS_NOSUCHTABLE:
4497c478bd9Sstevel@tonic-gate 		*err = __NSW_NOTFOUND;
4507c478bd9Sstevel@tonic-gate 		nis_freeresult(res);
4517c478bd9Sstevel@tonic-gate 		return (0);
4527c478bd9Sstevel@tonic-gate 	case NIS_S_NOTFOUND:
4537c478bd9Sstevel@tonic-gate 	case NIS_TRYAGAIN:
4547c478bd9Sstevel@tonic-gate 		*err = __NSW_TRYAGAIN;
4557c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
4567c478bd9Sstevel@tonic-gate 			"netname2user: (nis+ lookup): %s\n",
4577c478bd9Sstevel@tonic-gate 			nis_sperrno(res->status));
4587c478bd9Sstevel@tonic-gate 		nis_freeresult(res);
4597c478bd9Sstevel@tonic-gate 		return (0);
4607c478bd9Sstevel@tonic-gate 	case NIS_SUCCESS:
4617c478bd9Sstevel@tonic-gate 	case NIS_S_SUCCESS:
4627c478bd9Sstevel@tonic-gate 		break;   /* go and do something useful */
4637c478bd9Sstevel@tonic-gate 	default:
4647c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
4657c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "netname2user: (nis+ lookup): %s\n",
4667c478bd9Sstevel@tonic-gate 			nis_sperrno(res->status));
4677c478bd9Sstevel@tonic-gate 		nis_freeresult(res);
4687c478bd9Sstevel@tonic-gate 		return (0);
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	if (res->objects.objects_len > 1) {
4727c478bd9Sstevel@tonic-gate 		/*
4737c478bd9Sstevel@tonic-gate 		 * A principal can have more than one LOCAL entry?
4747c478bd9Sstevel@tonic-gate 		 * Something wrong with cred table.
4757c478bd9Sstevel@tonic-gate 		 * Warn user and continue.
4767c478bd9Sstevel@tonic-gate 		 */
4777c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT,
4787c478bd9Sstevel@tonic-gate 			"netname2user: LOCAL entry for %s in\
4797c478bd9Sstevel@tonic-gate 				directory %s not unique",
4807c478bd9Sstevel@tonic-gate 			netname, domain);
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 	/* nisname	LOCAL	uid 	grp,grp,grp */
4837c478bd9Sstevel@tonic-gate 	*err = parse_uid_gidlist(ENTRY_VAL(res->objects.objects_val, 2),
4847c478bd9Sstevel@tonic-gate 					/* uid */
4857c478bd9Sstevel@tonic-gate 			ENTRY_VAL(res->objects.objects_val, 3), /* gids */
4867c478bd9Sstevel@tonic-gate 			argp);
4877c478bd9Sstevel@tonic-gate 	nis_freeresult(res);
4887c478bd9Sstevel@tonic-gate 	return (*err == __NSW_SUCCESS);
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate /*
4927c478bd9Sstevel@tonic-gate  * Build the uid and gid from the netname for users in LDAP.
4937c478bd9Sstevel@tonic-gate  * There is no netid container in LDAP. For this we build
4947c478bd9Sstevel@tonic-gate  * the netname to user data dynamically from the passwd and
4957c478bd9Sstevel@tonic-gate  * group data. This works only for users in a single domain.
4967c478bd9Sstevel@tonic-gate  * This function is an interim solution until we support a
4977c478bd9Sstevel@tonic-gate  * netid container in LDAP which enables us to do netname2user
4987c478bd9Sstevel@tonic-gate  * resolution for multiple domains.
4997c478bd9Sstevel@tonic-gate  */
5007c478bd9Sstevel@tonic-gate static int
501*61961e0fSrobinson netname2user_ldap(int *err, char *netname, struct netid_userdata *argp)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	char buf[NSS_LINELEN_PASSWD];
504*61961e0fSrobinson 	char *p2, *lasts;
5057c478bd9Sstevel@tonic-gate 	struct passwd pw;
5067c478bd9Sstevel@tonic-gate 	uid_t uidnu;
5077c478bd9Sstevel@tonic-gate 	int ngroups = 0;
5087c478bd9Sstevel@tonic-gate 	int count;
5097c478bd9Sstevel@tonic-gate 	char pwbuf[NSS_LINELEN_PASSWD];
5107c478bd9Sstevel@tonic-gate 	gid_t groups[NGROUPS_MAX];
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	if (strlcpy(buf, netname, NSS_LINELEN_PASSWD) >= NSS_LINELEN_PASSWD) {
5137c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
5147c478bd9Sstevel@tonic-gate 		return (0);
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	/* get the uid from the netname */
518*61961e0fSrobinson 	if (strtok_r(buf, ".", &lasts) == NULL) {
5197c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
5207c478bd9Sstevel@tonic-gate 		return (0);
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 	if ((p2 = strtok_r(NULL, "@", &lasts)) == NULL) {
5237c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
5247c478bd9Sstevel@tonic-gate 		return (0);
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 	uidnu = atoi(p2);
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	/*
5297c478bd9Sstevel@tonic-gate 	 * check out the primary group and crosscheck the uid
5307c478bd9Sstevel@tonic-gate 	 * with the passwd data
5317c478bd9Sstevel@tonic-gate 	 */
5327c478bd9Sstevel@tonic-gate 	if ((getpwuid_r(uidnu, &pw, pwbuf, sizeof (pwbuf))) == NULL) {
5337c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
5347c478bd9Sstevel@tonic-gate 		return (0);
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	*(argp->uidp) = pw.pw_uid;
5387c478bd9Sstevel@tonic-gate 	*(argp->gidp) = pw.pw_gid;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	/* search through all groups for membership */
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	groups[0] = pw.pw_gid;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	ngroups = _getgroupsbymember(pw.pw_name, groups, NGROUPS_MAX,
5457c478bd9Sstevel@tonic-gate 				(pw.pw_gid >= 0) ? 1 : 0);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	if (ngroups < 0) {
5487c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
5497c478bd9Sstevel@tonic-gate 		return (0);
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	*(argp->gidlenp) = ngroups;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	for (count = 0; count < ngroups; count++) {
5557c478bd9Sstevel@tonic-gate 		(argp->gidlist[count]) = groups[count];
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	*err = __NSW_SUCCESS;
5597c478bd9Sstevel@tonic-gate 	return (1);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate /*
5647c478bd9Sstevel@tonic-gate  * Convert network-name into unix credential
5657c478bd9Sstevel@tonic-gate  */
5667c478bd9Sstevel@tonic-gate int
567*61961e0fSrobinson netname2user(const char netname[MAXNETNAMELEN + 1], uid_t *uidp, gid_t *gidp,
568*61961e0fSrobinson 						int *gidlenp, gid_t *gidlist)
5697c478bd9Sstevel@tonic-gate {
5707c478bd9Sstevel@tonic-gate 	struct __nsw_switchconfig *conf;
5717c478bd9Sstevel@tonic-gate 	struct __nsw_lookup *look;
5727c478bd9Sstevel@tonic-gate 	enum __nsw_parse_err perr;
5737c478bd9Sstevel@tonic-gate 	int needfree = 1, res;
5747c478bd9Sstevel@tonic-gate 	struct netid_userdata argp;
5757c478bd9Sstevel@tonic-gate 	int err;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	/*
5787c478bd9Sstevel@tonic-gate 	 * Take care of the special case of nobody. Compare the netname
5797c478bd9Sstevel@tonic-gate 	 * to the string "nobody". If they are equal, return the SVID
5807c478bd9Sstevel@tonic-gate 	 * standard value for nobody.
5817c478bd9Sstevel@tonic-gate 	 */
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	if (strcmp(netname, "nobody") == 0) {
5847c478bd9Sstevel@tonic-gate 		*uidp = NOBODY_UID;
5857c478bd9Sstevel@tonic-gate 		*gidp = NOBODY_UID;
5867c478bd9Sstevel@tonic-gate 		*gidlenp = 0;
5877c478bd9Sstevel@tonic-gate 		return (1);
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	/*
5917c478bd9Sstevel@tonic-gate 	 * First we do some generic sanity checks on the name we were
5927c478bd9Sstevel@tonic-gate 	 * passed. This lets us assume they are correct in the backends.
5937c478bd9Sstevel@tonic-gate 	 *
5947c478bd9Sstevel@tonic-gate 	 * NOTE: this code only recognizes names of the form :
5957c478bd9Sstevel@tonic-gate 	 *		unix.UID@domainname
5967c478bd9Sstevel@tonic-gate 	 */
597*61961e0fSrobinson 	if (strncmp(netname, OPSYS, OPSYS_LEN) != 0)
5987c478bd9Sstevel@tonic-gate 		return (0);
599*61961e0fSrobinson 	if (!isdigit(netname[OPSYS_LEN+1]))	/* check for uid string */
6007c478bd9Sstevel@tonic-gate 		return (0);
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	argp.uidp = uidp;
6037c478bd9Sstevel@tonic-gate 	argp.gidp = gidp;
6047c478bd9Sstevel@tonic-gate 	argp.gidlenp = gidlenp;
6057c478bd9Sstevel@tonic-gate 	argp.gidlist = gidlist;
606*61961e0fSrobinson 	(void) mutex_lock(&serialize_netname_r);
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	conf = __nsw_getconfig("publickey", &perr);
6097c478bd9Sstevel@tonic-gate 	if (!conf) {
6107c478bd9Sstevel@tonic-gate 		conf = &publickey_default;
6117c478bd9Sstevel@tonic-gate 		needfree = 0;
6127c478bd9Sstevel@tonic-gate 	} else
6137c478bd9Sstevel@tonic-gate 		needfree = 1; /* free the config structure */
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	for (look = conf->lookups; look; look = look->next) {
6167c478bd9Sstevel@tonic-gate 		if (strcmp(look->service_name, "nisplus") == 0)
6177c478bd9Sstevel@tonic-gate 			res = netname2user_nisplus(&err,
6187c478bd9Sstevel@tonic-gate 						(char *)netname, &argp);
6197c478bd9Sstevel@tonic-gate 		else if (strcmp(look->service_name, "nis") == 0)
6207c478bd9Sstevel@tonic-gate 			res = netname2user_nis(&err, (char *)netname, &argp);
6217c478bd9Sstevel@tonic-gate 		else if (strcmp(look->service_name, "files") == 0)
6227c478bd9Sstevel@tonic-gate 			res = netname2user_files(&err, (char *)netname, &argp);
6237c478bd9Sstevel@tonic-gate 		else if (strcmp(look->service_name, "ldap") == 0)
6247c478bd9Sstevel@tonic-gate 			res = netname2user_ldap(&err, (char *)netname, &argp);
6257c478bd9Sstevel@tonic-gate 		else {
6267c478bd9Sstevel@tonic-gate 			syslog(LOG_INFO,
6277c478bd9Sstevel@tonic-gate 		"netname2user: unknown nameservice for publickey info '%s'\n",
6287c478bd9Sstevel@tonic-gate 						look->service_name);
6297c478bd9Sstevel@tonic-gate 			err = __NSW_UNAVAIL;
6307c478bd9Sstevel@tonic-gate 		}
6317c478bd9Sstevel@tonic-gate 		switch (look->actions[err]) {
6327c478bd9Sstevel@tonic-gate 			case __NSW_CONTINUE :
6337c478bd9Sstevel@tonic-gate 				break;
6347c478bd9Sstevel@tonic-gate 			case __NSW_RETURN :
6357c478bd9Sstevel@tonic-gate 				if (needfree)
6367c478bd9Sstevel@tonic-gate 					__nsw_freeconfig(conf);
637*61961e0fSrobinson 				(void) mutex_unlock(&serialize_netname_r);
6387c478bd9Sstevel@tonic-gate 				return (res);
6397c478bd9Sstevel@tonic-gate 			default :
6407c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
6417c478bd9Sstevel@tonic-gate 			"netname2user: Unknown action for nameservice '%s'",
6427c478bd9Sstevel@tonic-gate 							look->service_name);
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 	if (needfree)
6467c478bd9Sstevel@tonic-gate 		__nsw_freeconfig(conf);
647*61961e0fSrobinson 	(void) mutex_unlock(&serialize_netname_r);
6487c478bd9Sstevel@tonic-gate 	return (0);
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate /*
6527c478bd9Sstevel@tonic-gate  * Convert network-name to hostname (fully qualified)
6537c478bd9Sstevel@tonic-gate  * NOTE: this code only recognizes names of the form :
6547c478bd9Sstevel@tonic-gate  *		unix.HOST@domainname
6557c478bd9Sstevel@tonic-gate  *
6567c478bd9Sstevel@tonic-gate  * This is very simple.  Since the netname is of the form:
6577c478bd9Sstevel@tonic-gate  *	unix.host@domainname
6587c478bd9Sstevel@tonic-gate  * We just construct the hostname using information from the domainname.
6597c478bd9Sstevel@tonic-gate  */
6607c478bd9Sstevel@tonic-gate int
661*61961e0fSrobinson netname2host(const char netname[MAXNETNAMELEN + 1], char *hostname,
662*61961e0fSrobinson 							const int hostlen)
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate 	char *p, *domainname;
6657c478bd9Sstevel@tonic-gate 	int len, dlen;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	if (!netname) {
6687c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "netname2host: null netname");
6697c478bd9Sstevel@tonic-gate 		goto bad_exit;
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	if (strncmp(netname, OPSYS, OPSYS_LEN) != 0)
6737c478bd9Sstevel@tonic-gate 		goto bad_netname;
6747c478bd9Sstevel@tonic-gate 	p = (char *)netname + OPSYS_LEN;	/* skip OPSYS part */
6757c478bd9Sstevel@tonic-gate 	if (*p != '.')
6767c478bd9Sstevel@tonic-gate 		goto bad_netname;
6777c478bd9Sstevel@tonic-gate 	++p;				/* skip '.' */
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	domainname = strchr(p, '@');	/* get domain name */
6807c478bd9Sstevel@tonic-gate 	if (domainname == 0)
6817c478bd9Sstevel@tonic-gate 		goto bad_netname;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	len = domainname - p;		/* host sits between '.' and '@' */
6847c478bd9Sstevel@tonic-gate 	domainname++;			/* skip '@' sign */
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	if (len <= 0)
6877c478bd9Sstevel@tonic-gate 		goto bad_netname;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	if (hostlen < len) {
6907c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
6917c478bd9Sstevel@tonic-gate 			"netname2host: insufficient space for hostname");
6927c478bd9Sstevel@tonic-gate 		goto bad_exit;
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	if (isdigit(*p))		/* don't want uid here */
6967c478bd9Sstevel@tonic-gate 		goto bad_netname;
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	if (*p == '\0')			/* check for null hostname */
6997c478bd9Sstevel@tonic-gate 		goto bad_netname;
7007c478bd9Sstevel@tonic-gate 
701*61961e0fSrobinson 	(void) strncpy(hostname, p, len);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	/* make into fully qualified hostname by concatenating domain part */
7047c478bd9Sstevel@tonic-gate 	dlen = strlen(domainname);
7057c478bd9Sstevel@tonic-gate 	if (hostlen < (len + dlen + 2)) {
7067c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
7077c478bd9Sstevel@tonic-gate 			"netname2host: insufficient space for hostname");
7087c478bd9Sstevel@tonic-gate 		goto bad_exit;
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	hostname[len] = '.';
712*61961e0fSrobinson 	(void) strncpy(hostname+len+1, domainname, dlen);
7137c478bd9Sstevel@tonic-gate 	hostname[len+dlen+1] = '\0';
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	return (1);
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate bad_netname:
7187c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, "netname2host: invalid host netname %s", netname);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate bad_exit:
7217c478bd9Sstevel@tonic-gate 	hostname[0] = '\0';
7227c478bd9Sstevel@tonic-gate 	return (0);
7237c478bd9Sstevel@tonic-gate }
724