xref: /titanic_44/usr/src/lib/libnsl/rpc/netnamer.c (revision 67dbe2be0c0f1e2eb428b89088bb5667e8f0b9f6)
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 /*
23*67dbe2beSCasper 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>
53*67dbe2beSCasper H.S. Dik #include <unistd.h>
54*67dbe2beSCasper 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 static const char    PKTABLE[]  = "cred.org_dir";
727c478bd9Sstevel@tonic-gate #define	PKTABLE_LEN 12
737c478bd9Sstevel@tonic-gate #define	OPSYS_LEN 4
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate extern int _getgroupsbymember(const char *, gid_t[], int, int);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * the value for NOBODY_UID is set by the SVID. The following define also
797c478bd9Sstevel@tonic-gate  * appears in netname.c
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	NOBODY_UID 60001
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  *	default publickey policy:
867c478bd9Sstevel@tonic-gate  *		publickey: nis [NOTFOUND = return] files
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*		NSW_NOTSUCCESS  NSW_NOTFOUND   NSW_UNAVAIL    NSW_TRYAGAIN */
917c478bd9Sstevel@tonic-gate #define	DEF_ACTION {__NSW_RETURN, __NSW_RETURN, __NSW_CONTINUE, __NSW_CONTINUE}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static struct __nsw_lookup lookup_files = {"files", DEF_ACTION, NULL, NULL},
947c478bd9Sstevel@tonic-gate 		lookup_nis = {"nis", DEF_ACTION, NULL, &lookup_files};
957c478bd9Sstevel@tonic-gate static struct __nsw_switchconfig publickey_default =
967c478bd9Sstevel@tonic-gate 			{0, "publickey", 2, &lookup_nis};
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static mutex_t serialize_netname_r = DEFAULTMUTEX;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate struct netid_userdata {
1017c478bd9Sstevel@tonic-gate 	uid_t	*uidp;
1027c478bd9Sstevel@tonic-gate 	gid_t	*gidp;
1037c478bd9Sstevel@tonic-gate 	int	*gidlenp;
1047c478bd9Sstevel@tonic-gate 	gid_t	*gidlist;
1057c478bd9Sstevel@tonic-gate };
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static int
10861961e0fSrobinson parse_uid(char *s, struct netid_userdata *argp)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	uid_t	u;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if (!s || !isdigit(*s)) {
1137c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
1147c478bd9Sstevel@tonic-gate 			"netname2user: expecting uid '%s'", s);
1157c478bd9Sstevel@tonic-gate 		return (__NSW_NOTFOUND); /* xxx need a better error */
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	/* Fetch the uid */
1197c478bd9Sstevel@tonic-gate 	u = (uid_t)(atoi(s));
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (u == 0) {
1227c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "netname2user: should not have uid 0");
1237c478bd9Sstevel@tonic-gate 		return (__NSW_NOTFOUND);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	*(argp->uidp) = u;
1267c478bd9Sstevel@tonic-gate 	return (__NSW_SUCCESS);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /* parse a comma separated gid list */
1317c478bd9Sstevel@tonic-gate static int
13261961e0fSrobinson parse_gidlist(char *p, struct netid_userdata *argp)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	int len;
1357c478bd9Sstevel@tonic-gate 	gid_t	g;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if (!p || (!isdigit(*p))) {
1387c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
1397c478bd9Sstevel@tonic-gate 			"netname2user: missing group id list in '%s'.",
1407c478bd9Sstevel@tonic-gate 			p);
1417c478bd9Sstevel@tonic-gate 		return (__NSW_NOTFOUND);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	g = (gid_t)(atoi(p));
1457c478bd9Sstevel@tonic-gate 	*(argp->gidp) = g;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	len = 0;
1487c478bd9Sstevel@tonic-gate 	while (p = strchr(p, ','))
1497c478bd9Sstevel@tonic-gate 		argp->gidlist[len++] = (gid_t)atoi(++p);
1507c478bd9Sstevel@tonic-gate 	*(argp->gidlenp) = len;
1517c478bd9Sstevel@tonic-gate 	return (__NSW_SUCCESS);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * parse_netid_str()
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  * Parse uid and group information from the passed string.
1597c478bd9Sstevel@tonic-gate  *
1607c478bd9Sstevel@tonic-gate  * The format of the string passed is
1617c478bd9Sstevel@tonic-gate  * 	uid:gid,grp,grp, ...
1627c478bd9Sstevel@tonic-gate  *
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate static int
16561961e0fSrobinson parse_netid_str(char *s, struct netid_userdata *argp)
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	char	*p;
1687c478bd9Sstevel@tonic-gate 	int	err;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/* get uid */
1717c478bd9Sstevel@tonic-gate 	err = parse_uid(s, argp);
17261961e0fSrobinson 	if (err != __NSW_SUCCESS)
1737c478bd9Sstevel@tonic-gate 		return (err);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/* Now get the group list */
1767c478bd9Sstevel@tonic-gate 	p = strchr(s, ':');
1777c478bd9Sstevel@tonic-gate 	if (!p) {
1787c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
17961961e0fSrobinson 			"netname2user: missing group id list in '%s'", s);
1807c478bd9Sstevel@tonic-gate 		return (__NSW_NOTFOUND);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 	++p;			/* skip ':' */
1837c478bd9Sstevel@tonic-gate 	err = parse_gidlist(p, argp);
1847c478bd9Sstevel@tonic-gate 	return (err);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate static int
18861961e0fSrobinson parse_uid_gidlist(char *ustr, char *gstr, struct netid_userdata *argp)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	int	err;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/* get uid */
1937c478bd9Sstevel@tonic-gate 	err = parse_uid(ustr, argp);
19461961e0fSrobinson 	if (err != __NSW_SUCCESS)
1957c478bd9Sstevel@tonic-gate 		return (err);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/* Now get the group list */
19861961e0fSrobinson 	return (parse_gidlist(gstr, argp));
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate  * netname2user_files()
2047c478bd9Sstevel@tonic-gate  *
2057c478bd9Sstevel@tonic-gate  * This routine fetches the netid information from the "files" nameservice.
2067c478bd9Sstevel@tonic-gate  * ie /etc/netid.
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate static int
20961961e0fSrobinson netname2user_files(int *err, char *netname, struct netid_userdata *argp)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	char 	buf[512];	/* one line from the file */
2127c478bd9Sstevel@tonic-gate 	char	*name;
2137c478bd9Sstevel@tonic-gate 	char	*value;
2147c478bd9Sstevel@tonic-gate 	char 	*res;
215004388ebScasper 	FILE	*fd;
2167c478bd9Sstevel@tonic-gate 
217004388ebScasper 	fd = fopen(NETIDFILE, "rF");
218004388ebScasper 	if (fd == NULL) {
2197c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
2207c478bd9Sstevel@tonic-gate 		return (0);
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 	/*
2237c478bd9Sstevel@tonic-gate 	 * for each line in the file parse it appropriately
2247c478bd9Sstevel@tonic-gate 	 * file format is :
2257c478bd9Sstevel@tonic-gate 	 *	netid	uid:grp,grp,grp # for users
2267c478bd9Sstevel@tonic-gate 	 *	netid	0:hostname	# for hosts
2277c478bd9Sstevel@tonic-gate 	 */
228004388ebScasper 	while (!feof(fd)) {
229004388ebScasper 		res = fgets(buf, 512, fd);
2307c478bd9Sstevel@tonic-gate 		if (res == NULL)
2317c478bd9Sstevel@tonic-gate 			break;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		/* Skip comments and blank lines */
2347c478bd9Sstevel@tonic-gate 		if ((*res == '#') || (*res == '\n'))
2357c478bd9Sstevel@tonic-gate 			continue;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		name = &(buf[0]);
2387c478bd9Sstevel@tonic-gate 		while (isspace(*name))
2397c478bd9Sstevel@tonic-gate 			name++;
2407c478bd9Sstevel@tonic-gate 		if (*name == '\0')	/* blank line continue */
2417c478bd9Sstevel@tonic-gate 			continue;
2427c478bd9Sstevel@tonic-gate 		value = name;		/* will contain the value eventually */
2437c478bd9Sstevel@tonic-gate 		while (!isspace(*value))
2447c478bd9Sstevel@tonic-gate 			value++;
2457c478bd9Sstevel@tonic-gate 		if (*value == '\0') {
2467c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING,
2477c478bd9Sstevel@tonic-gate 				"netname2user: badly formatted line in %s.",
2487c478bd9Sstevel@tonic-gate 				NETIDFILE);
2497c478bd9Sstevel@tonic-gate 			continue;
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 		*value++ = '\0'; /* nul terminate the name */
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 		if (strcasecmp(name, netname) == 0) {
254004388ebScasper 			(void) fclose(fd);
2557c478bd9Sstevel@tonic-gate 			while (isspace(*value))
2567c478bd9Sstevel@tonic-gate 				value++;
2577c478bd9Sstevel@tonic-gate 			*err = parse_netid_str(value, argp);
2587c478bd9Sstevel@tonic-gate 			return (*err == __NSW_SUCCESS);
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 	}
261004388ebScasper 	(void) fclose(fd);
2627c478bd9Sstevel@tonic-gate 	*err = __NSW_NOTFOUND;
2637c478bd9Sstevel@tonic-gate 	return (0);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * netname2user_nis()
2687c478bd9Sstevel@tonic-gate  *
2697c478bd9Sstevel@tonic-gate  * This function reads the netid from the NIS (YP) nameservice.
2707c478bd9Sstevel@tonic-gate  */
2717c478bd9Sstevel@tonic-gate static int
27261961e0fSrobinson netname2user_nis(int *err, char *netname, struct netid_userdata *argp)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	char *domain;
2757c478bd9Sstevel@tonic-gate 	int yperr;
2767c478bd9Sstevel@tonic-gate 	char *lookup;
2777c478bd9Sstevel@tonic-gate 	int len;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	domain = strchr(netname, '@');
2807c478bd9Sstevel@tonic-gate 	if (!domain) {
2817c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
2827c478bd9Sstevel@tonic-gate 		return (0);
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	/* Point past the '@' character */
2867c478bd9Sstevel@tonic-gate 	domain++;
2877c478bd9Sstevel@tonic-gate 	lookup = NULL;
2887c478bd9Sstevel@tonic-gate 	yperr = yp_match(domain, (char *)NETID, netname, strlen(netname),
2897c478bd9Sstevel@tonic-gate 			&lookup, &len);
2907c478bd9Sstevel@tonic-gate 	switch (yperr) {
2917c478bd9Sstevel@tonic-gate 		case 0:
2927c478bd9Sstevel@tonic-gate 			break; /* the successful case */
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 		default :
2957c478bd9Sstevel@tonic-gate 			/*
2967c478bd9Sstevel@tonic-gate 			 *  XXX not sure about yp_match semantics.
2977c478bd9Sstevel@tonic-gate 			 * should err be set to NOTFOUND here?
2987c478bd9Sstevel@tonic-gate 			 */
2997c478bd9Sstevel@tonic-gate 			*err = __NSW_UNAVAIL;
3007c478bd9Sstevel@tonic-gate 			return (0);
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 	if (lookup) {
3037c478bd9Sstevel@tonic-gate 		lookup[len] = '\0';
3047c478bd9Sstevel@tonic-gate 		*err = parse_netid_str(lookup, argp);
3057c478bd9Sstevel@tonic-gate 		free(lookup);
3067c478bd9Sstevel@tonic-gate 		return (*err == __NSW_SUCCESS);
30761961e0fSrobinson 	}
3087c478bd9Sstevel@tonic-gate 	*err = __NSW_NOTFOUND;
3097c478bd9Sstevel@tonic-gate 	return (0);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate  * Obtain user information (uid, gidlist) from nisplus.
3147c478bd9Sstevel@tonic-gate  * What we're trying to do here is to map a netname into
3157c478bd9Sstevel@tonic-gate  * local unix information (uid, gids), relevant in
3167c478bd9Sstevel@tonic-gate  * the *local* domain.
3177c478bd9Sstevel@tonic-gate  *
3187c478bd9Sstevel@tonic-gate  *	 cname   auth_type auth_name public  private
3197c478bd9Sstevel@tonic-gate  * ----------------------------------------------------------
3207c478bd9Sstevel@tonic-gate  *	nisname   DES     netname   pubkey  prikey
3217c478bd9Sstevel@tonic-gate  *	nisname   LOCAL   uid       gidlist
3227c478bd9Sstevel@tonic-gate  *
3237c478bd9Sstevel@tonic-gate  * 1.  Find out which 'home' domain to look for user's DES entry.
3247c478bd9Sstevel@tonic-gate  *	This is gotten from the domain part of the netname.
3257c478bd9Sstevel@tonic-gate  * 2.  Get the nisplus principal name from the DES entry in the cred
3267c478bd9Sstevel@tonic-gate  *	table of user's home domain.
3277c478bd9Sstevel@tonic-gate  * 3.  Use the nisplus principal name and search in the cred table of
3287c478bd9Sstevel@tonic-gate  *	the *local* directory for the LOCAL entry.
3297c478bd9Sstevel@tonic-gate  *
3307c478bd9Sstevel@tonic-gate  * Note that we need this translation of netname to <uid,gidlist> to be
3317c478bd9Sstevel@tonic-gate  * secure, so we *must* use authenticated connections.
3327c478bd9Sstevel@tonic-gate  */
3337c478bd9Sstevel@tonic-gate static int
33461961e0fSrobinson netname2user_nisplus(int *err, char *netname, struct netid_userdata *argp)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	char *domain;
3377c478bd9Sstevel@tonic-gate 	nis_result *res;
3387c478bd9Sstevel@tonic-gate 	char	sname[NIS_MAXNAMELEN+1]; /*  search criteria + table name */
3397c478bd9Sstevel@tonic-gate 	char	principal[NIS_MAXNAMELEN+1];
3407c478bd9Sstevel@tonic-gate 	int len;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	/* 1.  Get home domain of user. */
3437c478bd9Sstevel@tonic-gate 	domain = strchr(netname, '@');
3447c478bd9Sstevel@tonic-gate 	if (!domain) {
3457c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
3467c478bd9Sstevel@tonic-gate 		return (0);
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 	domain++;  /* skip '@' */
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	/* 2.  Get user's nisplus principal name.  */
3527c478bd9Sstevel@tonic-gate 	if ((strlen(netname)+strlen(domain)+PKTABLE_LEN+32) >
3537c478bd9Sstevel@tonic-gate 		(size_t)NIS_MAXNAMELEN) {
3547c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
3557c478bd9Sstevel@tonic-gate 		return (0);
3567c478bd9Sstevel@tonic-gate 	}
35761961e0fSrobinson 	(void) snprintf(sname, sizeof (sname),
35861961e0fSrobinson 		"[auth_name=\"%s\",auth_type=DES],%s.%s",
3597c478bd9Sstevel@tonic-gate 		netname, PKTABLE, domain);
3607c478bd9Sstevel@tonic-gate 	if (sname[strlen(sname) - 1] != '.')
36161961e0fSrobinson 		(void) strcat(sname, ".");
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	/* must use authenticated call here */
3647c478bd9Sstevel@tonic-gate 	/* XXX but we cant, for now. XXX */
3657c478bd9Sstevel@tonic-gate 	res = nis_list(sname, USE_DGRAM+NO_AUTHINFO+FOLLOW_LINKS+FOLLOW_PATH,
3667c478bd9Sstevel@tonic-gate 	    NULL, NULL);
3677c478bd9Sstevel@tonic-gate 	switch (res->status) {
3687c478bd9Sstevel@tonic-gate 	case NIS_SUCCESS:
3697c478bd9Sstevel@tonic-gate 	case NIS_S_SUCCESS:
3707c478bd9Sstevel@tonic-gate 		break;   /* go and do something useful */
3717c478bd9Sstevel@tonic-gate 	case NIS_NOTFOUND:
3727c478bd9Sstevel@tonic-gate 	case NIS_PARTIAL:
3737c478bd9Sstevel@tonic-gate 	case NIS_NOSUCHNAME:
3747c478bd9Sstevel@tonic-gate 	case NIS_NOSUCHTABLE:
3757c478bd9Sstevel@tonic-gate 		*err = __NSW_NOTFOUND;
3767c478bd9Sstevel@tonic-gate 		nis_freeresult(res);
3777c478bd9Sstevel@tonic-gate 		return (0);
3787c478bd9Sstevel@tonic-gate 	case NIS_S_NOTFOUND:
3797c478bd9Sstevel@tonic-gate 	case NIS_TRYAGAIN:
3807c478bd9Sstevel@tonic-gate 		*err = __NSW_TRYAGAIN;
3817c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
3827c478bd9Sstevel@tonic-gate 			"netname2user: (nis+ lookup): %s\n",
3837c478bd9Sstevel@tonic-gate 			nis_sperrno(res->status));
3847c478bd9Sstevel@tonic-gate 		nis_freeresult(res);
3857c478bd9Sstevel@tonic-gate 		return (0);
3867c478bd9Sstevel@tonic-gate 	default:
3877c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
3887c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "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 	}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	if (res->objects.objects_len > 1) {
3957c478bd9Sstevel@tonic-gate 		/*
3967c478bd9Sstevel@tonic-gate 		 * A netname belonging to more than one principal?
3977c478bd9Sstevel@tonic-gate 		 * Something wrong with cred table. should be unique.
3987c478bd9Sstevel@tonic-gate 		 * Warn user and continue.
3997c478bd9Sstevel@tonic-gate 		 */
4007c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT,
4017c478bd9Sstevel@tonic-gate 			"netname2user: DES entry for %s in \
4027c478bd9Sstevel@tonic-gate 			directory %s not unique",
4037c478bd9Sstevel@tonic-gate 			netname, domain);
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	len = ENTRY_LEN(res->objects.objects_val, 0);
40761961e0fSrobinson 	(void) strncpy(principal, ENTRY_VAL(res->objects.objects_val, 0), len);
4087c478bd9Sstevel@tonic-gate 	principal[len] = '\0';
4097c478bd9Sstevel@tonic-gate 	nis_freeresult(res);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	if (principal[0] == '\0') {
4127c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
4137c478bd9Sstevel@tonic-gate 		return (0);
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	/*
4177c478bd9Sstevel@tonic-gate 	 *	3.  Use principal name to look up uid/gid information in
4187c478bd9Sstevel@tonic-gate 	 *	LOCAL entry in **local** cred table.
4197c478bd9Sstevel@tonic-gate 	 */
4207c478bd9Sstevel@tonic-gate 	domain = nis_local_directory();
4217c478bd9Sstevel@tonic-gate 	if ((strlen(principal)+strlen(domain)+PKTABLE_LEN+30) >
4227c478bd9Sstevel@tonic-gate 		(size_t)NIS_MAXNAMELEN) {
4237c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
4247c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "netname2user: principal name '%s' too long",
4257c478bd9Sstevel@tonic-gate 			principal);
4267c478bd9Sstevel@tonic-gate 		return (0);
4277c478bd9Sstevel@tonic-gate 	}
42861961e0fSrobinson 	(void) snprintf(sname, sizeof (sname),
42961961e0fSrobinson 		"[cname=\"%s\",auth_type=LOCAL],%s.%s",
4307c478bd9Sstevel@tonic-gate 		principal, PKTABLE, domain);
4317c478bd9Sstevel@tonic-gate 	if (sname[strlen(sname) - 1] != '.')
43261961e0fSrobinson 		(void) strcat(sname, ".");
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	/* must use authenticated call here */
4357c478bd9Sstevel@tonic-gate 	/* XXX but we cant, for now. XXX */
4367c478bd9Sstevel@tonic-gate 	res = nis_list(sname, USE_DGRAM+NO_AUTHINFO+FOLLOW_LINKS+FOLLOW_PATH,
4377c478bd9Sstevel@tonic-gate 	    NULL, NULL);
4387c478bd9Sstevel@tonic-gate 	switch (res->status) {
4397c478bd9Sstevel@tonic-gate 	case NIS_NOTFOUND:
4407c478bd9Sstevel@tonic-gate 	case NIS_PARTIAL:
4417c478bd9Sstevel@tonic-gate 	case NIS_NOSUCHNAME:
4427c478bd9Sstevel@tonic-gate 	case NIS_NOSUCHTABLE:
4437c478bd9Sstevel@tonic-gate 		*err = __NSW_NOTFOUND;
4447c478bd9Sstevel@tonic-gate 		nis_freeresult(res);
4457c478bd9Sstevel@tonic-gate 		return (0);
4467c478bd9Sstevel@tonic-gate 	case NIS_S_NOTFOUND:
4477c478bd9Sstevel@tonic-gate 	case NIS_TRYAGAIN:
4487c478bd9Sstevel@tonic-gate 		*err = __NSW_TRYAGAIN;
4497c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
4507c478bd9Sstevel@tonic-gate 			"netname2user: (nis+ lookup): %s\n",
4517c478bd9Sstevel@tonic-gate 			nis_sperrno(res->status));
4527c478bd9Sstevel@tonic-gate 		nis_freeresult(res);
4537c478bd9Sstevel@tonic-gate 		return (0);
4547c478bd9Sstevel@tonic-gate 	case NIS_SUCCESS:
4557c478bd9Sstevel@tonic-gate 	case NIS_S_SUCCESS:
4567c478bd9Sstevel@tonic-gate 		break;   /* go and do something useful */
4577c478bd9Sstevel@tonic-gate 	default:
4587c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
4597c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "netname2user: (nis+ lookup): %s\n",
4607c478bd9Sstevel@tonic-gate 			nis_sperrno(res->status));
4617c478bd9Sstevel@tonic-gate 		nis_freeresult(res);
4627c478bd9Sstevel@tonic-gate 		return (0);
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if (res->objects.objects_len > 1) {
4667c478bd9Sstevel@tonic-gate 		/*
4677c478bd9Sstevel@tonic-gate 		 * A principal can have more than one LOCAL entry?
4687c478bd9Sstevel@tonic-gate 		 * Something wrong with cred table.
4697c478bd9Sstevel@tonic-gate 		 * Warn user and continue.
4707c478bd9Sstevel@tonic-gate 		 */
4717c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT,
4727c478bd9Sstevel@tonic-gate 			"netname2user: LOCAL entry for %s in\
4737c478bd9Sstevel@tonic-gate 				directory %s not unique",
4747c478bd9Sstevel@tonic-gate 			netname, domain);
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 	/* nisname	LOCAL	uid 	grp,grp,grp */
4777c478bd9Sstevel@tonic-gate 	*err = parse_uid_gidlist(ENTRY_VAL(res->objects.objects_val, 2),
4787c478bd9Sstevel@tonic-gate 					/* uid */
4797c478bd9Sstevel@tonic-gate 			ENTRY_VAL(res->objects.objects_val, 3), /* gids */
4807c478bd9Sstevel@tonic-gate 			argp);
4817c478bd9Sstevel@tonic-gate 	nis_freeresult(res);
4827c478bd9Sstevel@tonic-gate 	return (*err == __NSW_SUCCESS);
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate /*
4867c478bd9Sstevel@tonic-gate  * Build the uid and gid from the netname for users in LDAP.
4877c478bd9Sstevel@tonic-gate  * There is no netid container in LDAP. For this we build
4887c478bd9Sstevel@tonic-gate  * the netname to user data dynamically from the passwd and
4897c478bd9Sstevel@tonic-gate  * group data. This works only for users in a single domain.
4907c478bd9Sstevel@tonic-gate  * This function is an interim solution until we support a
4917c478bd9Sstevel@tonic-gate  * netid container in LDAP which enables us to do netname2user
4927c478bd9Sstevel@tonic-gate  * resolution for multiple domains.
4937c478bd9Sstevel@tonic-gate  */
4947c478bd9Sstevel@tonic-gate static int
49561961e0fSrobinson netname2user_ldap(int *err, char *netname, struct netid_userdata *argp)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	char buf[NSS_LINELEN_PASSWD];
49861961e0fSrobinson 	char *p2, *lasts;
4997c478bd9Sstevel@tonic-gate 	struct passwd pw;
5007c478bd9Sstevel@tonic-gate 	uid_t uidnu;
5017c478bd9Sstevel@tonic-gate 	int ngroups = 0;
5027c478bd9Sstevel@tonic-gate 	int count;
5037c478bd9Sstevel@tonic-gate 	char pwbuf[NSS_LINELEN_PASSWD];
504*67dbe2beSCasper H.S. Dik 	int maxgrp = sysconf(_SC_NGROUPS_MAX);
505*67dbe2beSCasper H.S. Dik 	gid_t *groups = alloca(maxgrp * sizeof (gid_t));
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	if (strlcpy(buf, netname, NSS_LINELEN_PASSWD) >= NSS_LINELEN_PASSWD) {
5087c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
5097c478bd9Sstevel@tonic-gate 		return (0);
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	/* get the uid from the netname */
51361961e0fSrobinson 	if (strtok_r(buf, ".", &lasts) == NULL) {
5147c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
5157c478bd9Sstevel@tonic-gate 		return (0);
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 	if ((p2 = strtok_r(NULL, "@", &lasts)) == NULL) {
5187c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
5197c478bd9Sstevel@tonic-gate 		return (0);
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 	uidnu = atoi(p2);
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	/*
5247c478bd9Sstevel@tonic-gate 	 * check out the primary group and crosscheck the uid
5257c478bd9Sstevel@tonic-gate 	 * with the passwd data
5267c478bd9Sstevel@tonic-gate 	 */
5277c478bd9Sstevel@tonic-gate 	if ((getpwuid_r(uidnu, &pw, pwbuf, sizeof (pwbuf))) == NULL) {
5287c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
5297c478bd9Sstevel@tonic-gate 		return (0);
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	*(argp->uidp) = pw.pw_uid;
5337c478bd9Sstevel@tonic-gate 	*(argp->gidp) = pw.pw_gid;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	/* search through all groups for membership */
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	groups[0] = pw.pw_gid;
5387c478bd9Sstevel@tonic-gate 
539*67dbe2beSCasper H.S. Dik 	ngroups = _getgroupsbymember(pw.pw_name, groups, maxgrp,
540f48205beScasper 				(pw.pw_gid <= MAXUID) ? 1 : 0);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	if (ngroups < 0) {
5437c478bd9Sstevel@tonic-gate 		*err = __NSW_UNAVAIL;
5447c478bd9Sstevel@tonic-gate 		return (0);
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	*(argp->gidlenp) = ngroups;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	for (count = 0; count < ngroups; count++) {
5507c478bd9Sstevel@tonic-gate 		(argp->gidlist[count]) = groups[count];
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	*err = __NSW_SUCCESS;
5547c478bd9Sstevel@tonic-gate 	return (1);
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate /*
5597c478bd9Sstevel@tonic-gate  * Convert network-name into unix credential
5607c478bd9Sstevel@tonic-gate  */
5617c478bd9Sstevel@tonic-gate int
56261961e0fSrobinson netname2user(const char netname[MAXNETNAMELEN + 1], uid_t *uidp, gid_t *gidp,
56361961e0fSrobinson 						int *gidlenp, gid_t *gidlist)
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate 	struct __nsw_switchconfig *conf;
5667c478bd9Sstevel@tonic-gate 	struct __nsw_lookup *look;
5677c478bd9Sstevel@tonic-gate 	enum __nsw_parse_err perr;
5687c478bd9Sstevel@tonic-gate 	int needfree = 1, res;
5697c478bd9Sstevel@tonic-gate 	struct netid_userdata argp;
5707c478bd9Sstevel@tonic-gate 	int err;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	/*
5737c478bd9Sstevel@tonic-gate 	 * Take care of the special case of nobody. Compare the netname
5747c478bd9Sstevel@tonic-gate 	 * to the string "nobody". If they are equal, return the SVID
5757c478bd9Sstevel@tonic-gate 	 * standard value for nobody.
5767c478bd9Sstevel@tonic-gate 	 */
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	if (strcmp(netname, "nobody") == 0) {
5797c478bd9Sstevel@tonic-gate 		*uidp = NOBODY_UID;
5807c478bd9Sstevel@tonic-gate 		*gidp = NOBODY_UID;
5817c478bd9Sstevel@tonic-gate 		*gidlenp = 0;
5827c478bd9Sstevel@tonic-gate 		return (1);
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	/*
5867c478bd9Sstevel@tonic-gate 	 * First we do some generic sanity checks on the name we were
5877c478bd9Sstevel@tonic-gate 	 * passed. This lets us assume they are correct in the backends.
5887c478bd9Sstevel@tonic-gate 	 *
5897c478bd9Sstevel@tonic-gate 	 * NOTE: this code only recognizes names of the form :
5907c478bd9Sstevel@tonic-gate 	 *		unix.UID@domainname
5917c478bd9Sstevel@tonic-gate 	 */
59261961e0fSrobinson 	if (strncmp(netname, OPSYS, OPSYS_LEN) != 0)
5937c478bd9Sstevel@tonic-gate 		return (0);
59461961e0fSrobinson 	if (!isdigit(netname[OPSYS_LEN+1]))	/* check for uid string */
5957c478bd9Sstevel@tonic-gate 		return (0);
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	argp.uidp = uidp;
5987c478bd9Sstevel@tonic-gate 	argp.gidp = gidp;
5997c478bd9Sstevel@tonic-gate 	argp.gidlenp = gidlenp;
6007c478bd9Sstevel@tonic-gate 	argp.gidlist = gidlist;
60161961e0fSrobinson 	(void) mutex_lock(&serialize_netname_r);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	conf = __nsw_getconfig("publickey", &perr);
6047c478bd9Sstevel@tonic-gate 	if (!conf) {
6057c478bd9Sstevel@tonic-gate 		conf = &publickey_default;
6067c478bd9Sstevel@tonic-gate 		needfree = 0;
6077c478bd9Sstevel@tonic-gate 	} else
6087c478bd9Sstevel@tonic-gate 		needfree = 1; /* free the config structure */
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	for (look = conf->lookups; look; look = look->next) {
6117c478bd9Sstevel@tonic-gate 		if (strcmp(look->service_name, "nisplus") == 0)
6127c478bd9Sstevel@tonic-gate 			res = netname2user_nisplus(&err,
6137c478bd9Sstevel@tonic-gate 						(char *)netname, &argp);
6147c478bd9Sstevel@tonic-gate 		else if (strcmp(look->service_name, "nis") == 0)
6157c478bd9Sstevel@tonic-gate 			res = netname2user_nis(&err, (char *)netname, &argp);
6167c478bd9Sstevel@tonic-gate 		else if (strcmp(look->service_name, "files") == 0)
6177c478bd9Sstevel@tonic-gate 			res = netname2user_files(&err, (char *)netname, &argp);
6187c478bd9Sstevel@tonic-gate 		else if (strcmp(look->service_name, "ldap") == 0)
6197c478bd9Sstevel@tonic-gate 			res = netname2user_ldap(&err, (char *)netname, &argp);
6207c478bd9Sstevel@tonic-gate 		else {
6217c478bd9Sstevel@tonic-gate 			syslog(LOG_INFO,
6227c478bd9Sstevel@tonic-gate 		"netname2user: unknown nameservice for publickey info '%s'\n",
6237c478bd9Sstevel@tonic-gate 						look->service_name);
6247c478bd9Sstevel@tonic-gate 			err = __NSW_UNAVAIL;
6257c478bd9Sstevel@tonic-gate 		}
6267c478bd9Sstevel@tonic-gate 		switch (look->actions[err]) {
6277c478bd9Sstevel@tonic-gate 			case __NSW_CONTINUE :
6287c478bd9Sstevel@tonic-gate 				break;
6297c478bd9Sstevel@tonic-gate 			case __NSW_RETURN :
6307c478bd9Sstevel@tonic-gate 				if (needfree)
6317c478bd9Sstevel@tonic-gate 					__nsw_freeconfig(conf);
63261961e0fSrobinson 				(void) mutex_unlock(&serialize_netname_r);
6337c478bd9Sstevel@tonic-gate 				return (res);
6347c478bd9Sstevel@tonic-gate 			default :
6357c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
6367c478bd9Sstevel@tonic-gate 			"netname2user: Unknown action for nameservice '%s'",
6377c478bd9Sstevel@tonic-gate 							look->service_name);
6387c478bd9Sstevel@tonic-gate 		}
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate 	if (needfree)
6417c478bd9Sstevel@tonic-gate 		__nsw_freeconfig(conf);
64261961e0fSrobinson 	(void) mutex_unlock(&serialize_netname_r);
6437c478bd9Sstevel@tonic-gate 	return (0);
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate /*
6477c478bd9Sstevel@tonic-gate  * Convert network-name to hostname (fully qualified)
6487c478bd9Sstevel@tonic-gate  * NOTE: this code only recognizes names of the form :
6497c478bd9Sstevel@tonic-gate  *		unix.HOST@domainname
6507c478bd9Sstevel@tonic-gate  *
6517c478bd9Sstevel@tonic-gate  * This is very simple.  Since the netname is of the form:
6527c478bd9Sstevel@tonic-gate  *	unix.host@domainname
6537c478bd9Sstevel@tonic-gate  * We just construct the hostname using information from the domainname.
6547c478bd9Sstevel@tonic-gate  */
6557c478bd9Sstevel@tonic-gate int
65661961e0fSrobinson netname2host(const char netname[MAXNETNAMELEN + 1], char *hostname,
65761961e0fSrobinson 							const int hostlen)
6587c478bd9Sstevel@tonic-gate {
6597c478bd9Sstevel@tonic-gate 	char *p, *domainname;
6607c478bd9Sstevel@tonic-gate 	int len, dlen;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	if (!netname) {
6637c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "netname2host: null netname");
6647c478bd9Sstevel@tonic-gate 		goto bad_exit;
6657c478bd9Sstevel@tonic-gate 	}
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	if (strncmp(netname, OPSYS, OPSYS_LEN) != 0)
6687c478bd9Sstevel@tonic-gate 		goto bad_netname;
6697c478bd9Sstevel@tonic-gate 	p = (char *)netname + OPSYS_LEN;	/* skip OPSYS part */
6707c478bd9Sstevel@tonic-gate 	if (*p != '.')
6717c478bd9Sstevel@tonic-gate 		goto bad_netname;
6727c478bd9Sstevel@tonic-gate 	++p;				/* skip '.' */
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	domainname = strchr(p, '@');	/* get domain name */
6757c478bd9Sstevel@tonic-gate 	if (domainname == 0)
6767c478bd9Sstevel@tonic-gate 		goto bad_netname;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	len = domainname - p;		/* host sits between '.' and '@' */
6797c478bd9Sstevel@tonic-gate 	domainname++;			/* skip '@' sign */
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	if (len <= 0)
6827c478bd9Sstevel@tonic-gate 		goto bad_netname;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	if (hostlen < len) {
6857c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
6867c478bd9Sstevel@tonic-gate 			"netname2host: insufficient space for hostname");
6877c478bd9Sstevel@tonic-gate 		goto bad_exit;
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	if (isdigit(*p))		/* don't want uid here */
6917c478bd9Sstevel@tonic-gate 		goto bad_netname;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	if (*p == '\0')			/* check for null hostname */
6947c478bd9Sstevel@tonic-gate 		goto bad_netname;
6957c478bd9Sstevel@tonic-gate 
69661961e0fSrobinson 	(void) strncpy(hostname, p, len);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	/* make into fully qualified hostname by concatenating domain part */
6997c478bd9Sstevel@tonic-gate 	dlen = strlen(domainname);
7007c478bd9Sstevel@tonic-gate 	if (hostlen < (len + dlen + 2)) {
7017c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
7027c478bd9Sstevel@tonic-gate 			"netname2host: insufficient space for hostname");
7037c478bd9Sstevel@tonic-gate 		goto bad_exit;
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	hostname[len] = '.';
70761961e0fSrobinson 	(void) strncpy(hostname+len+1, domainname, dlen);
7087c478bd9Sstevel@tonic-gate 	hostname[len+dlen+1] = '\0';
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	return (1);
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate bad_netname:
7137c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, "netname2host: invalid host netname %s", netname);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate bad_exit:
7167c478bd9Sstevel@tonic-gate 	hostname[0] = '\0';
7177c478bd9Sstevel@tonic-gate 	return (0);
7187c478bd9Sstevel@tonic-gate }
719