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