xref: /titanic_50/usr/src/lib/passwdutil/switch_utils.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
5*f48205beScasper  * Common Development and Distribution License (the "License").
6*f48205beScasper  * 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
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*f48205beScasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <nsswitch.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <syslog.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "ns_sldap.h"
387c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
397c478bd9Sstevel@tonic-gate #include <nsswitch.h>
407c478bd9Sstevel@tonic-gate #include <pwd.h>
417c478bd9Sstevel@tonic-gate #include <shadow.h>
427c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include "passwdutil.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static struct passwd *nisplus_getpw_from_master(const char *, char *);
477c478bd9Sstevel@tonic-gate static struct spwd *nisplus_getsp_from_master(const char *, char *);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * name_to_int(rep)
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * Translate the repository to a bitmask.
537c478bd9Sstevel@tonic-gate  * if we don't recognise the repository name, we return REP_ERANGE
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate int
567c478bd9Sstevel@tonic-gate name_to_int(char *rep_name)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	int result = REP_ERANGE;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	if (strcmp(rep_name, "files") == 0)
617c478bd9Sstevel@tonic-gate 		result = REP_FILES;
627c478bd9Sstevel@tonic-gate 	else if (strcmp(rep_name, "nis") == 0)
637c478bd9Sstevel@tonic-gate 		result = REP_NIS;
647c478bd9Sstevel@tonic-gate 	else if (strcmp(rep_name, "nisplus") == 0)
657c478bd9Sstevel@tonic-gate 		result = REP_NISPLUS;
667c478bd9Sstevel@tonic-gate 	else if (strcmp(rep_name, "ldap") == 0)
677c478bd9Sstevel@tonic-gate 		result = REP_LDAP;
687c478bd9Sstevel@tonic-gate 	else if (strcmp(rep_name, "compat") == 0) {
697c478bd9Sstevel@tonic-gate 		struct __nsw_switchconfig *cfg;
707c478bd9Sstevel@tonic-gate 		enum   __nsw_parse_err pserr;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 		cfg = __nsw_getconfig("passwd_compat", &pserr);
737c478bd9Sstevel@tonic-gate 		if (cfg == NULL) {
747c478bd9Sstevel@tonic-gate 			result = REP_FILES | REP_NIS;
757c478bd9Sstevel@tonic-gate 		} else {
767c478bd9Sstevel@tonic-gate 			if (strcmp(cfg->lookups->service_name, "nisplus") == 0)
777c478bd9Sstevel@tonic-gate 				result = REP_FILES | REP_NISPLUS;
787c478bd9Sstevel@tonic-gate 			else if (strcmp(cfg->lookups->service_name, "ldap") ==
797c478bd9Sstevel@tonic-gate 			    0)
807c478bd9Sstevel@tonic-gate 				result = REP_FILES | REP_LDAP;
817c478bd9Sstevel@tonic-gate 			else
827c478bd9Sstevel@tonic-gate 				result = REP_ERANGE;
837c478bd9Sstevel@tonic-gate 			__nsw_freeconfig(cfg);
847c478bd9Sstevel@tonic-gate 		}
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	return (result);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * Figure out which repository we use in compat mode.
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate int
947c478bd9Sstevel@tonic-gate get_compat_mode(void)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	struct __nsw_switchconfig *cfg;
977c478bd9Sstevel@tonic-gate 	enum   __nsw_parse_err pserr;
987c478bd9Sstevel@tonic-gate 	int result = REP_COMPAT_NIS;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if ((cfg = __nsw_getconfig("passwd_compat", &pserr)) != NULL) {
1017c478bd9Sstevel@tonic-gate 		if (strcmp(cfg->lookups->service_name, "nisplus") == 0)
1027c478bd9Sstevel@tonic-gate 			result = REP_COMPAT_NISPLUS;
1037c478bd9Sstevel@tonic-gate 		else if (strcmp(cfg->lookups->service_name, "ldap") == 0)
1047c478bd9Sstevel@tonic-gate 			result = REP_COMPAT_LDAP;
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 	__nsw_freeconfig(cfg);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	return (result);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * get_ns(rep, accesstype)
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  * returns a bitmask of repositories to use based on either
1157c478bd9Sstevel@tonic-gate  *   1. the repository that is given as argument
1167c478bd9Sstevel@tonic-gate  *   2. the nsswitch.conf file
1177c478bd9Sstevel@tonic-gate  *   3. the type of access requested
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  * "accesstype" indicates whether we are reading from or writing to the
1207c478bd9Sstevel@tonic-gate  * repository. We need to know this since "compat" will translate into
1217c478bd9Sstevel@tonic-gate  * REP_NSS (the nss-switch) for READ access (needed to decode
1227c478bd9Sstevel@tonic-gate  * the black-magic '+' entries) but it translates into a bitmask
1237c478bd9Sstevel@tonic-gate  * on WRITE access.
1247c478bd9Sstevel@tonic-gate  *
1257c478bd9Sstevel@tonic-gate  * If we detect read-access in compat mode, we augment the result
1267c478bd9Sstevel@tonic-gate  * with one of REP_COMPAT_{NIS,NISPLUS,LDAP}. We need this in order to
1277c478bd9Sstevel@tonic-gate  * implement ATTR_REP_NAME in nss_getpwnam.
1287c478bd9Sstevel@tonic-gate  *
1297c478bd9Sstevel@tonic-gate  * A return value of REP_NOREP indicates an error.
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate int
1327c478bd9Sstevel@tonic-gate get_ns(pwu_repository_t *rep, int accesstype)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	struct __nsw_switchconfig *conf = NULL;
1357c478bd9Sstevel@tonic-gate 	enum __nsw_parse_err pserr;
1367c478bd9Sstevel@tonic-gate 	struct __nsw_lookup *lkp;
1377c478bd9Sstevel@tonic-gate 	struct __nsw_lookup *lkp2;
1387c478bd9Sstevel@tonic-gate 	int result = REP_NOREP;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (rep != PWU_DEFAULT_REP) {
1417c478bd9Sstevel@tonic-gate 		result = name_to_int(rep->type);
1427c478bd9Sstevel@tonic-gate 		return (result);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	conf = __nsw_getconfig("passwd", &pserr);
1467c478bd9Sstevel@tonic-gate 	if (conf == NULL) {
1477c478bd9Sstevel@tonic-gate 		/*
1487c478bd9Sstevel@tonic-gate 		 * No config found. The user didn't supply a repository,
1497c478bd9Sstevel@tonic-gate 		 * so we try to change the password in the default
1507c478bd9Sstevel@tonic-gate 		 * repositories (files and nis) even though we cannot
1517c478bd9Sstevel@tonic-gate 		 * find the name service switch entry. (Backward compat)
1527c478bd9Sstevel@tonic-gate 		 */
1537c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "passwdutil.so: nameservice switch entry for "
1547c478bd9Sstevel@tonic-gate 				"passwd not found.");
1557c478bd9Sstevel@tonic-gate 		result = REP_FILES | REP_NIS;
1567c478bd9Sstevel@tonic-gate 		return (result);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	lkp = conf->lookups;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	/*
1627c478bd9Sstevel@tonic-gate 	 * Supported nsswitch.conf can have a maximum of 2 repositories.
1637c478bd9Sstevel@tonic-gate 	 * If we encounter an unsupported nsswitch.conf, we return REP_NSS
1647c478bd9Sstevel@tonic-gate 	 * to fall back to the nsswitch backend.
1657c478bd9Sstevel@tonic-gate 	 */
1667c478bd9Sstevel@tonic-gate 	if (conf->num_lookups == 1) {
1677c478bd9Sstevel@tonic-gate 		/* files or compat */
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		if (strcmp(lkp->service_name, "files") == 0) {
1707c478bd9Sstevel@tonic-gate 			result = name_to_int(lkp->service_name);
1717c478bd9Sstevel@tonic-gate 		} else if (strcmp(lkp->service_name, "compat") == 0) {
1727c478bd9Sstevel@tonic-gate 			if (accesstype == PWU_READ)
1737c478bd9Sstevel@tonic-gate 				result = REP_NSS | get_compat_mode();
1747c478bd9Sstevel@tonic-gate 			else
1757c478bd9Sstevel@tonic-gate 				result = name_to_int(lkp->service_name);
1767c478bd9Sstevel@tonic-gate 		} else
1777c478bd9Sstevel@tonic-gate 			result = REP_NSS;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	} else if (conf->num_lookups == 2) {
1807c478bd9Sstevel@tonic-gate 		lkp2 = lkp->next;
1817c478bd9Sstevel@tonic-gate 		if (strcmp(lkp->service_name, "files") == 0) {
1827c478bd9Sstevel@tonic-gate 			result = REP_FILES;
1837c478bd9Sstevel@tonic-gate 			if (strcmp(lkp2->service_name, "ldap") == 0)
1847c478bd9Sstevel@tonic-gate 				result |= REP_LDAP;
1857c478bd9Sstevel@tonic-gate 			else if (strcmp(lkp2->service_name, "nis") == 0)
1867c478bd9Sstevel@tonic-gate 				result |= REP_NIS;
1877c478bd9Sstevel@tonic-gate 			else if (strcmp(lkp2->service_name, "nisplus") == 0)
1887c478bd9Sstevel@tonic-gate 				result |= REP_NISPLUS;
1897c478bd9Sstevel@tonic-gate 			else
1907c478bd9Sstevel@tonic-gate 				result = REP_NSS;
1917c478bd9Sstevel@tonic-gate 		} else {
1927c478bd9Sstevel@tonic-gate 			result = REP_NSS;
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 	} else {
1957c478bd9Sstevel@tonic-gate 		result = REP_NSS;
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	__nsw_freeconfig(conf);
1997c478bd9Sstevel@tonic-gate 	return (result);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate static void
2037c478bd9Sstevel@tonic-gate nss_ldap_passwd(p)
2047c478bd9Sstevel@tonic-gate 	nss_db_params_t	*p;
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	p->name = NSS_DBNAM_PASSWD;
2077c478bd9Sstevel@tonic-gate 	p->flags |= NSS_USE_DEFAULT_CONFIG;
2087c478bd9Sstevel@tonic-gate 	p->default_config = "ldap";
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate static void
2127c478bd9Sstevel@tonic-gate nss_ldap_shadow(p)
2137c478bd9Sstevel@tonic-gate 	nss_db_params_t	*p;
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	p->name = NSS_DBNAM_SHADOW;
2167c478bd9Sstevel@tonic-gate 	p->config_name    = NSS_DBNAM_PASSWD;	/* Use config for "passwd" */
2177c478bd9Sstevel@tonic-gate 	p->flags |= NSS_USE_DEFAULT_CONFIG;
2187c478bd9Sstevel@tonic-gate 	p->default_config = "ldap";
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate #ifdef PAM_NIS
2237c478bd9Sstevel@tonic-gate static void
2247c478bd9Sstevel@tonic-gate nss_nis_passwd(p)
2257c478bd9Sstevel@tonic-gate 	nss_db_params_t	*p;
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	p->name = NSS_DBNAM_PASSWD;
2287c478bd9Sstevel@tonic-gate 	p->flags |= NSS_USE_DEFAULT_CONFIG;
2297c478bd9Sstevel@tonic-gate 	p->default_config = "nis";
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate static void
2337c478bd9Sstevel@tonic-gate nss_nis_shadow(p)
2347c478bd9Sstevel@tonic-gate 	nss_db_params_t	*p;
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	p->name = NSS_DBNAM_SHADOW;
2377c478bd9Sstevel@tonic-gate 	p->config_name    = NSS_DBNAM_PASSWD;	/* Use config for "passwd" */
2387c478bd9Sstevel@tonic-gate 	p->flags |= NSS_USE_DEFAULT_CONFIG;
2397c478bd9Sstevel@tonic-gate 	p->default_config = "nis";
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate #endif /* PAM_NIS */
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate static void
2457c478bd9Sstevel@tonic-gate nss_nisplus_passwd(p)
2467c478bd9Sstevel@tonic-gate 	nss_db_params_t	*p;
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate 	p->name = NSS_DBNAM_PASSWD;
2497c478bd9Sstevel@tonic-gate 	p->flags |= NSS_USE_DEFAULT_CONFIG;
2507c478bd9Sstevel@tonic-gate 	p->default_config = "nisplus";
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate static void
2547c478bd9Sstevel@tonic-gate nss_nisplus_shadow(p)
2557c478bd9Sstevel@tonic-gate 	nss_db_params_t	*p;
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	p->name = NSS_DBNAM_SHADOW;
2587c478bd9Sstevel@tonic-gate 	p->config_name    = NSS_DBNAM_PASSWD;	/* Use config for "passwd" */
2597c478bd9Sstevel@tonic-gate 	p->flags |= NSS_USE_DEFAULT_CONFIG;
2607c478bd9Sstevel@tonic-gate 	p->default_config = "nisplus";
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate static char *
2657c478bd9Sstevel@tonic-gate gettok(nextpp)
2667c478bd9Sstevel@tonic-gate 	char	**nextpp;
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	char	*p = *nextpp;
2697c478bd9Sstevel@tonic-gate 	char	*q = p;
2707c478bd9Sstevel@tonic-gate 	char	c;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if (p == 0) {
2737c478bd9Sstevel@tonic-gate 		return (0);
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 	while ((c = *q) != '\0' && c != ':') {
2767c478bd9Sstevel@tonic-gate 		q++;
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 	if (c == '\0') {
2797c478bd9Sstevel@tonic-gate 		*nextpp = 0;
2807c478bd9Sstevel@tonic-gate 	} else {
2817c478bd9Sstevel@tonic-gate 		*q++ = '\0';
2827c478bd9Sstevel@tonic-gate 		*nextpp = q;
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 	return (p);
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate /*
2887c478bd9Sstevel@tonic-gate  * Return values: 0 = success, 1 = parse error, 2 = erange ...
2897c478bd9Sstevel@tonic-gate  * The structure pointer passed in is a structure in the caller's space
2907c478bd9Sstevel@tonic-gate  * wherein the field pointers would be set to areas in the buffer if
2917c478bd9Sstevel@tonic-gate  * need be. instring and buffer should be separate areas.
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate static int
2947c478bd9Sstevel@tonic-gate str2passwd(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate 	struct passwd	*passwd	= (struct passwd *)ent;
2977c478bd9Sstevel@tonic-gate 	char		*p, *next;
2987c478bd9Sstevel@tonic-gate 	int		black_magic;	/* "+" or "-" entry */
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if (lenstr + 1 > buflen) {
3017c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_ERANGE);
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 	/*
3047c478bd9Sstevel@tonic-gate 	 * We copy the input string into the output buffer and
3057c478bd9Sstevel@tonic-gate 	 * operate on it in place.
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 	(void) memcpy(buffer, instr, lenstr);
3087c478bd9Sstevel@tonic-gate 	buffer[lenstr] = '\0';
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	next = buffer;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	passwd->pw_name = p = gettok(&next);		/* username */
3137c478bd9Sstevel@tonic-gate 	if (*p == '\0') {
3147c478bd9Sstevel@tonic-gate 		/* Empty username;  not allowed */
3157c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 	black_magic = (*p == '+' || *p == '-');
3187c478bd9Sstevel@tonic-gate 	if (black_magic) {
3197c478bd9Sstevel@tonic-gate 		passwd->pw_uid	= UID_NOBODY;
3207c478bd9Sstevel@tonic-gate 		passwd->pw_gid	= GID_NOBODY;
3217c478bd9Sstevel@tonic-gate 		/*
3227c478bd9Sstevel@tonic-gate 		 * pwconv tests pw_passwd and pw_age == NULL
3237c478bd9Sstevel@tonic-gate 		 */
3247c478bd9Sstevel@tonic-gate 		passwd->pw_passwd = "";
3257c478bd9Sstevel@tonic-gate 		passwd->pw_age	= "";
3267c478bd9Sstevel@tonic-gate 		/*
3277c478bd9Sstevel@tonic-gate 		 * the rest of the passwd entry is "optional"
3287c478bd9Sstevel@tonic-gate 		 */
3297c478bd9Sstevel@tonic-gate 		passwd->pw_comment = "";
3307c478bd9Sstevel@tonic-gate 		passwd->pw_gecos = "";
3317c478bd9Sstevel@tonic-gate 		passwd->pw_dir	= "";
3327c478bd9Sstevel@tonic-gate 		passwd->pw_shell = "";
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	passwd->pw_passwd = p = gettok(&next);		/* password */
3367c478bd9Sstevel@tonic-gate 	if (p == 0) {
3377c478bd9Sstevel@tonic-gate 		if (black_magic)
3387c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
3397c478bd9Sstevel@tonic-gate 		else
3407c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 	for (; *p != '\0'; p++) {			/* age */
3437c478bd9Sstevel@tonic-gate 		if (*p == ',') {
3447c478bd9Sstevel@tonic-gate 			*p++ = '\0';
3457c478bd9Sstevel@tonic-gate 			break;
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 	passwd->pw_age = p;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	p = next;					/* uid */
3517c478bd9Sstevel@tonic-gate 	if (p == 0 || *p == '\0') {
3527c478bd9Sstevel@tonic-gate 		if (black_magic)
3537c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
3547c478bd9Sstevel@tonic-gate 		else
3557c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	if (!black_magic) {
3587c478bd9Sstevel@tonic-gate 		passwd->pw_uid = strtol(p, &next, 10);
3597c478bd9Sstevel@tonic-gate 		if (next == p) {
3607c478bd9Sstevel@tonic-gate 			/* uid field should be nonempty */
3617c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 		/*
3647c478bd9Sstevel@tonic-gate 		 * The old code (in 2.0 thru 2.5) would check
3657c478bd9Sstevel@tonic-gate 		 * for the uid being negative, or being greater
3667c478bd9Sstevel@tonic-gate 		 * than 60001 (the rfs limit).  If it met either of
3677c478bd9Sstevel@tonic-gate 		 * these conditions, the uid was translated to 60001.
3687c478bd9Sstevel@tonic-gate 		 *
369*f48205beScasper 		 * Now we just check for ephemeral uids; anything else
3707c478bd9Sstevel@tonic-gate 		 * is administrative policy
3717c478bd9Sstevel@tonic-gate 		 */
372*f48205beScasper 		if (passwd->pw_uid > MAXUID)
3737c478bd9Sstevel@tonic-gate 			passwd->pw_uid = UID_NOBODY;
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 	if (*next++ != ':') {
3767c478bd9Sstevel@tonic-gate 		if (black_magic)
3777c478bd9Sstevel@tonic-gate 			p = gettok(&next);
3787c478bd9Sstevel@tonic-gate 		else
3797c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 	p = next;					/* gid */
3827c478bd9Sstevel@tonic-gate 	if (p == 0 || *p == '\0') {
3837c478bd9Sstevel@tonic-gate 		if (black_magic)
3847c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
3857c478bd9Sstevel@tonic-gate 		else
3867c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 	if (!black_magic) {
3897c478bd9Sstevel@tonic-gate 		passwd->pw_gid = strtol(p, &next, 10);
3907c478bd9Sstevel@tonic-gate 		if (next == p) {
3917c478bd9Sstevel@tonic-gate 			/* gid field should be nonempty */
3927c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 		/*
3957c478bd9Sstevel@tonic-gate 		 * gid should be non-negative; anything else
3967c478bd9Sstevel@tonic-gate 		 * is administrative policy.
3977c478bd9Sstevel@tonic-gate 		 */
398*f48205beScasper 		if (passwd->pw_gid > MAXUID)
3997c478bd9Sstevel@tonic-gate 			passwd->pw_gid = GID_NOBODY;
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 	if (*next++ != ':') {
4027c478bd9Sstevel@tonic-gate 		if (black_magic)
4037c478bd9Sstevel@tonic-gate 			p = gettok(&next);
4047c478bd9Sstevel@tonic-gate 		else
4057c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	passwd->pw_gecos = passwd->pw_comment = p = gettok(&next);
4097c478bd9Sstevel@tonic-gate 	if (p == 0) {
4107c478bd9Sstevel@tonic-gate 		if (black_magic)
4117c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
4127c478bd9Sstevel@tonic-gate 		else
4137c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	passwd->pw_dir = p = gettok(&next);
4177c478bd9Sstevel@tonic-gate 	if (p == 0) {
4187c478bd9Sstevel@tonic-gate 		if (black_magic)
4197c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
4207c478bd9Sstevel@tonic-gate 		else
4217c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	passwd->pw_shell = p = gettok(&next);
4257c478bd9Sstevel@tonic-gate 	if (p == 0) {
4267c478bd9Sstevel@tonic-gate 		if (black_magic)
4277c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
4287c478bd9Sstevel@tonic-gate 		else
4297c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	/* Better not be any more fields... */
4337c478bd9Sstevel@tonic-gate 	if (next == 0) {
4347c478bd9Sstevel@tonic-gate 		/* Successfully parsed and stored */
4357c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_SUCCESS);
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 	return (NSS_STR_PARSE_PARSE);
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate typedef const char *constp;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate /*
4437c478bd9Sstevel@tonic-gate  * Return value 1 means success and more input, 0 means error or no more
4447c478bd9Sstevel@tonic-gate  */
4457c478bd9Sstevel@tonic-gate static int
4467c478bd9Sstevel@tonic-gate getfield(nextp, limit, uns, valp)
4477c478bd9Sstevel@tonic-gate 	constp		*nextp;
4487c478bd9Sstevel@tonic-gate 	constp		limit;
4497c478bd9Sstevel@tonic-gate 	int		uns;
4507c478bd9Sstevel@tonic-gate 	void		*valp;
4517c478bd9Sstevel@tonic-gate {
4527c478bd9Sstevel@tonic-gate 	constp		p = *nextp;
4537c478bd9Sstevel@tonic-gate 	char		*endfield;
4547c478bd9Sstevel@tonic-gate 	char		numbuf[12];  /* Holds -2^31 and trailing ':' */
4557c478bd9Sstevel@tonic-gate 	int		len;
4567c478bd9Sstevel@tonic-gate 	long		x;
4577c478bd9Sstevel@tonic-gate 	unsigned long	ux;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if (p == 0 || p >= limit) {
4607c478bd9Sstevel@tonic-gate 		return (0);
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 	if (*p == ':') {
4637c478bd9Sstevel@tonic-gate 		p++;
4647c478bd9Sstevel@tonic-gate 		*nextp = p;
4657c478bd9Sstevel@tonic-gate 		return (p < limit);
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 	if ((len = limit - p) > sizeof (numbuf) - 1) {
4687c478bd9Sstevel@tonic-gate 		len = sizeof (numbuf) - 1;
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * We want to use strtol() and we have a readonly non-zero-terminated
4727c478bd9Sstevel@tonic-gate 	 *   string, so first we copy and terminate the interesting bit.
4737c478bd9Sstevel@tonic-gate 	 *   Ugh.  (It's convenient to terminate with a colon rather than \0).
4747c478bd9Sstevel@tonic-gate 	 */
4757c478bd9Sstevel@tonic-gate 	if ((endfield = memccpy(numbuf, p, ':', len)) == 0) {
4767c478bd9Sstevel@tonic-gate 		if (len != limit - p) {
4777c478bd9Sstevel@tonic-gate 			/* Error -- field is too big to be a legit number */
4787c478bd9Sstevel@tonic-gate 			return (0);
4797c478bd9Sstevel@tonic-gate 		}
4807c478bd9Sstevel@tonic-gate 		numbuf[len] = ':';
4817c478bd9Sstevel@tonic-gate 		p = limit;
4827c478bd9Sstevel@tonic-gate 	} else {
4837c478bd9Sstevel@tonic-gate 		p += (endfield - numbuf);
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 	if (uns) {
4867c478bd9Sstevel@tonic-gate 		ux = strtoul(numbuf, &endfield, 10);
4877c478bd9Sstevel@tonic-gate 		if (*endfield != ':') {
4887c478bd9Sstevel@tonic-gate 			/* Error -- expected <integer><colon> */
4897c478bd9Sstevel@tonic-gate 			return (0);
4907c478bd9Sstevel@tonic-gate 		}
4917c478bd9Sstevel@tonic-gate 		*((unsigned int *)valp) = (unsigned int)ux;
4927c478bd9Sstevel@tonic-gate 	} else {
4937c478bd9Sstevel@tonic-gate 		x = strtol(numbuf, &endfield, 10);
4947c478bd9Sstevel@tonic-gate 		if (*endfield != ':') {
4957c478bd9Sstevel@tonic-gate 			/* Error -- expected <integer><colon> */
4967c478bd9Sstevel@tonic-gate 			return (0);
4977c478bd9Sstevel@tonic-gate 		}
4987c478bd9Sstevel@tonic-gate 		*((int *)valp) = (int)x;
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 	*nextp = p;
5017c478bd9Sstevel@tonic-gate 	return (p < limit);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate /*
5057c478bd9Sstevel@tonic-gate  *  str2spwd() -- convert a string to a shadow passwd entry.  The parser is
5067c478bd9Sstevel@tonic-gate  *	more liberal than the passwd or group parsers;  since it's legitimate
5077c478bd9Sstevel@tonic-gate  *	for almost all the fields here to be blank, the parser lets one omit
5087c478bd9Sstevel@tonic-gate  *	any number of blank fields at the end of the entry.  The acceptable
5097c478bd9Sstevel@tonic-gate  *	forms for '+' and '-' entries are the same as those for normal entries.
5107c478bd9Sstevel@tonic-gate  *  === Is this likely to do more harm than good?
5117c478bd9Sstevel@tonic-gate  *
5127c478bd9Sstevel@tonic-gate  * Return values: 0 = success, 1 = parse error, 2 = erange ...
5137c478bd9Sstevel@tonic-gate  * The structure pointer passed in is a structure in the caller's space
5147c478bd9Sstevel@tonic-gate  * wherein the field pointers would be set to areas in the buffer if
5157c478bd9Sstevel@tonic-gate  * need be. instring and buffer should be separate areas.
5167c478bd9Sstevel@tonic-gate  */
5177c478bd9Sstevel@tonic-gate int
5187c478bd9Sstevel@tonic-gate str2spwd(instr, lenstr, ent, buffer, buflen)
5197c478bd9Sstevel@tonic-gate 	const char	*instr;
5207c478bd9Sstevel@tonic-gate 	int		lenstr;
5217c478bd9Sstevel@tonic-gate 	void	*ent; /* really (struct spwd *) */
5227c478bd9Sstevel@tonic-gate 	char	*buffer;
5237c478bd9Sstevel@tonic-gate 	int	buflen;
5247c478bd9Sstevel@tonic-gate {
5257c478bd9Sstevel@tonic-gate 	struct spwd	*shadow	= (struct spwd *)ent;
5267c478bd9Sstevel@tonic-gate 	const char	*p = instr, *limit;
5277c478bd9Sstevel@tonic-gate 	char		*bufp;
5287c478bd9Sstevel@tonic-gate 	int	lencopy, black_magic;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	limit = p + lenstr;
5317c478bd9Sstevel@tonic-gate 	if ((p = memchr(instr, ':', lenstr)) == 0 ||
5327c478bd9Sstevel@tonic-gate 		++p >= limit ||
5337c478bd9Sstevel@tonic-gate 		(p = memchr(p, ':', limit - p)) == 0) {
5347c478bd9Sstevel@tonic-gate 		lencopy = lenstr;
5357c478bd9Sstevel@tonic-gate 		p = 0;
5367c478bd9Sstevel@tonic-gate 	} else {
5377c478bd9Sstevel@tonic-gate 		lencopy = p - instr;
5387c478bd9Sstevel@tonic-gate 		p++;
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 	if (lencopy + 1 > buflen) {
5417c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_ERANGE);
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 	(void) memcpy(buffer, instr, lencopy);
5447c478bd9Sstevel@tonic-gate 	buffer[lencopy] = 0;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	black_magic = (*instr == '+' || *instr == '-');
5477c478bd9Sstevel@tonic-gate 	shadow->sp_namp = bufp = buffer;
5487c478bd9Sstevel@tonic-gate 	shadow->sp_pwdp	= 0;
5497c478bd9Sstevel@tonic-gate 	shadow->sp_lstchg = -1;
5507c478bd9Sstevel@tonic-gate 	shadow->sp_min	= -1;
5517c478bd9Sstevel@tonic-gate 	shadow->sp_max	= -1;
5527c478bd9Sstevel@tonic-gate 	shadow->sp_warn	= -1;
5537c478bd9Sstevel@tonic-gate 	shadow->sp_inact = -1;
5547c478bd9Sstevel@tonic-gate 	shadow->sp_expire = -1;
5557c478bd9Sstevel@tonic-gate 	shadow->sp_flag	= 0;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	if ((bufp = strchr(bufp, ':')) == 0) {
5587c478bd9Sstevel@tonic-gate 		if (black_magic)
5597c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
5607c478bd9Sstevel@tonic-gate 		else
5617c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 	*bufp++ = '\0';
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	shadow->sp_pwdp = bufp;
5667c478bd9Sstevel@tonic-gate 	if (instr == 0) {
5677c478bd9Sstevel@tonic-gate 		if ((bufp = strchr(bufp, ':')) == 0) {
5687c478bd9Sstevel@tonic-gate 			if (black_magic)
5697c478bd9Sstevel@tonic-gate 				return (NSS_STR_PARSE_SUCCESS);
5707c478bd9Sstevel@tonic-gate 			else
5717c478bd9Sstevel@tonic-gate 				return (NSS_STR_PARSE_PARSE);
5727c478bd9Sstevel@tonic-gate 		}
5737c478bd9Sstevel@tonic-gate 		*bufp++ = '\0';
5747c478bd9Sstevel@tonic-gate 		p = bufp;
5757c478bd9Sstevel@tonic-gate 	} /* else p was set when we copied name and passwd into the buffer */
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_lstchg))
5787c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
5797c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_min))
5807c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
5817c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_max))
5827c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
5837c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_warn))
5847c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
5857c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_inact))
5867c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
5877c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_expire))
5887c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
5897c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 1, &shadow->sp_flag))
5907c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
5917c478bd9Sstevel@tonic-gate 	if (p != limit) {
5927c478bd9Sstevel@tonic-gate 		/* Syntax error -- garbage at end of line */
5937c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
5947c478bd9Sstevel@tonic-gate 	}
5957c478bd9Sstevel@tonic-gate 	return (NSS_STR_PARSE_SUCCESS);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate static nss_XbyY_buf_t *buffer;
5997c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate #define	GETBUF()	\
6027c478bd9Sstevel@tonic-gate 	NSS_XbyY_ALLOC(&buffer, sizeof (struct passwd), NSS_BUFLEN_PASSWD)
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate #pragma fini(endutilpwent)
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate static void
6077c478bd9Sstevel@tonic-gate endutilpwent(void)
6087c478bd9Sstevel@tonic-gate {
6097c478bd9Sstevel@tonic-gate 	NSS_XbyY_FREE(&buffer);
6107c478bd9Sstevel@tonic-gate 	nss_delete(&db_root);
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate struct passwd *
6147c478bd9Sstevel@tonic-gate getpwnam_from(const char *name, pwu_repository_t *rep, int reptype)
6157c478bd9Sstevel@tonic-gate {
6167c478bd9Sstevel@tonic-gate 	nss_XbyY_buf_t  *b = GETBUF();
6177c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	if (b == 0)
6207c478bd9Sstevel@tonic-gate 		return (0);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2passwd);
6237c478bd9Sstevel@tonic-gate 	arg.key.name = name;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	switch (reptype) {
6267c478bd9Sstevel@tonic-gate 	case REP_LDAP:
6277c478bd9Sstevel@tonic-gate 		(void) nss_search(&db_root, nss_ldap_passwd,
6287c478bd9Sstevel@tonic-gate 		    NSS_DBOP_PASSWD_BYNAME, &arg);
6297c478bd9Sstevel@tonic-gate 		break;
6307c478bd9Sstevel@tonic-gate 	case REP_NISPLUS:
6317c478bd9Sstevel@tonic-gate 		if (rep && rep->scope)
6327c478bd9Sstevel@tonic-gate 			return (nisplus_getpw_from_master(name, rep->scope));
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 		(void) nss_search(&db_root, nss_nisplus_passwd,
6357c478bd9Sstevel@tonic-gate 		    NSS_DBOP_PASSWD_BYNAME, &arg);
6367c478bd9Sstevel@tonic-gate 		break;
6377c478bd9Sstevel@tonic-gate #ifdef PAM_NIS
6387c478bd9Sstevel@tonic-gate 	case REP_NIS:
6397c478bd9Sstevel@tonic-gate 		(void) nss_search(&db_root, nss_nis_passwd,
6407c478bd9Sstevel@tonic-gate 		    NSS_DBOP_PASSWD_BYNAME, &arg);
6417c478bd9Sstevel@tonic-gate 		break;
6427c478bd9Sstevel@tonic-gate #endif
6437c478bd9Sstevel@tonic-gate 	default:
6447c478bd9Sstevel@tonic-gate 		return (NULL);
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	return (struct passwd *)NSS_XbyY_FINI(&arg);
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6517c478bd9Sstevel@tonic-gate struct passwd *
6527c478bd9Sstevel@tonic-gate getpwuid_from(uid_t uid, pwu_repository_t *rep, int reptype)
6537c478bd9Sstevel@tonic-gate {
6547c478bd9Sstevel@tonic-gate 	nss_XbyY_buf_t  *b = GETBUF();
6557c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	if (b == 0)
6587c478bd9Sstevel@tonic-gate 		return (0);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2passwd);
6617c478bd9Sstevel@tonic-gate 	arg.key.uid = uid;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	switch (reptype) {
6647c478bd9Sstevel@tonic-gate 	case REP_LDAP:
6657c478bd9Sstevel@tonic-gate 		(void) nss_search(&db_root, nss_ldap_passwd,
6667c478bd9Sstevel@tonic-gate 		    NSS_DBOP_PASSWD_BYUID, &arg);
6677c478bd9Sstevel@tonic-gate 		break;
6687c478bd9Sstevel@tonic-gate 	case REP_NISPLUS:
6697c478bd9Sstevel@tonic-gate 		(void) nss_search(&db_root, nss_nisplus_passwd,
6707c478bd9Sstevel@tonic-gate 		    NSS_DBOP_PASSWD_BYUID, &arg);
6717c478bd9Sstevel@tonic-gate 		break;
6727c478bd9Sstevel@tonic-gate #ifdef PAM_NIS
6737c478bd9Sstevel@tonic-gate 	case REP_NIS:
6747c478bd9Sstevel@tonic-gate 		(void) nss_search(&db_root, nss_nis_passwd,
6757c478bd9Sstevel@tonic-gate 		    NSS_DBOP_PASSWD_BYUID, &arg);
6767c478bd9Sstevel@tonic-gate 		break;
6777c478bd9Sstevel@tonic-gate #endif
6787c478bd9Sstevel@tonic-gate 	default:
6797c478bd9Sstevel@tonic-gate 		return (NULL);
6807c478bd9Sstevel@tonic-gate 	}
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	return (struct passwd *)NSS_XbyY_FINI(&arg);
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate static nss_XbyY_buf_t *spbuf;
6867c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(spdb_root);
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate #define	GETSPBUF()	\
6897c478bd9Sstevel@tonic-gate 	NSS_XbyY_ALLOC(&spbuf, sizeof (struct spwd), NSS_BUFLEN_SHADOW)
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate #pragma fini(endutilspent)
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate static void
6947c478bd9Sstevel@tonic-gate endutilspent(void)
6957c478bd9Sstevel@tonic-gate {
6967c478bd9Sstevel@tonic-gate 	NSS_XbyY_FREE(&spbuf);
6977c478bd9Sstevel@tonic-gate 	nss_delete(&spdb_root);
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate struct spwd *
7017c478bd9Sstevel@tonic-gate getspnam_from(const char *name, pwu_repository_t *rep, int reptype)
7027c478bd9Sstevel@tonic-gate {
7037c478bd9Sstevel@tonic-gate 	nss_XbyY_buf_t  *b = GETSPBUF();
7047c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	if (b == 0)
7077c478bd9Sstevel@tonic-gate 		return (0);
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2spwd);
7107c478bd9Sstevel@tonic-gate 	arg.key.name = name;
7117c478bd9Sstevel@tonic-gate 	switch (reptype) {
7127c478bd9Sstevel@tonic-gate 	case REP_LDAP:
7137c478bd9Sstevel@tonic-gate 		(void) nss_search(&spdb_root, nss_ldap_shadow,
7147c478bd9Sstevel@tonic-gate 		    NSS_DBOP_SHADOW_BYNAME, &arg);
7157c478bd9Sstevel@tonic-gate 		break;
7167c478bd9Sstevel@tonic-gate 	case REP_NISPLUS:
7177c478bd9Sstevel@tonic-gate 		if (rep && rep->scope)
7187c478bd9Sstevel@tonic-gate 			return (nisplus_getsp_from_master(name, rep->scope));
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 		(void) nss_search(&spdb_root, nss_nisplus_shadow,
7217c478bd9Sstevel@tonic-gate 		    NSS_DBOP_SHADOW_BYNAME, &arg);
7227c478bd9Sstevel@tonic-gate 		break;
7237c478bd9Sstevel@tonic-gate #ifdef PAM_NIS
7247c478bd9Sstevel@tonic-gate 	case REP_NIS:
7257c478bd9Sstevel@tonic-gate 		(void) nss_search(&spdb_root, nss_nis_shadow,
7267c478bd9Sstevel@tonic-gate 		    NSS_DBOP_SHADOW_BYNAME, &arg);
7277c478bd9Sstevel@tonic-gate 		break;
7287c478bd9Sstevel@tonic-gate #endif
7297c478bd9Sstevel@tonic-gate 	default:
7307c478bd9Sstevel@tonic-gate 		return (NULL);
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 	return (struct spwd *)NSS_XbyY_FINI(&arg);
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate static nis_result *
7377c478bd9Sstevel@tonic-gate nisplus_match(const char *name, char *domain, char *buf, int len)
7387c478bd9Sstevel@tonic-gate {
7397c478bd9Sstevel@tonic-gate 	int n;
7407c478bd9Sstevel@tonic-gate 	int flags;
7417c478bd9Sstevel@tonic-gate 	nis_result *res;
7427c478bd9Sstevel@tonic-gate 	nis_object *object;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	n = snprintf(buf, len, "[name=%s],passwd.org_dir.%s", name, domain);
7457c478bd9Sstevel@tonic-gate 	if (n >= len) {
7467c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "nisplus_match: name too long");
7477c478bd9Sstevel@tonic-gate 		return (NULL);
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 	if (buf[n-1] != '.') {
7507c478bd9Sstevel@tonic-gate 		if (n == len-1) {
7517c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "nisplus_match: name too long");
7527c478bd9Sstevel@tonic-gate 			return (NULL);
7537c478bd9Sstevel@tonic-gate 		}
7547c478bd9Sstevel@tonic-gate 		buf[n++] = '.';
7557c478bd9Sstevel@tonic-gate 		buf[n] = '\0';
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	flags = USE_DGRAM | FOLLOW_LINKS | FOLLOW_PATH | MASTER_ONLY;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	res = nis_list(buf, flags, NULL, NULL);
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	if (res == NULL) {
7637c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "nisplus_match: nis_list returned NULL");
7647c478bd9Sstevel@tonic-gate 		return (NULL);
7657c478bd9Sstevel@tonic-gate 	}
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	if (NIS_RES_STATUS(res) != NIS_SUCCESS || NIS_RES_NUMOBJ(res) != 1) {
7687c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "nisplus_match: match failed: %s",
7697c478bd9Sstevel@tonic-gate 		    nis_sperrno(NIS_RES_STATUS(res)));
7707c478bd9Sstevel@tonic-gate 		nis_freeresult(res);
7717c478bd9Sstevel@tonic-gate 		return (NULL);
7727c478bd9Sstevel@tonic-gate 	}
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	object = NIS_RES_OBJECT(res);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	if (object->EN_data.en_cols.en_cols_len < 8) {
7777c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "nisplus_match: "
7787c478bd9Sstevel@tonic-gate 		    "not a valid passwd table entry for user %s", name);
7797c478bd9Sstevel@tonic-gate 		nis_freeresult(res);
7807c478bd9Sstevel@tonic-gate 		return (NULL);
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	return (res);
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate #define	SAFE_STRDUP(dst, idx) \
7877c478bd9Sstevel@tonic-gate 	if ((idx) <= 3 && ENTRY_VAL(nret, (idx)) == NULL) { \
7887c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, \
7897c478bd9Sstevel@tonic-gate 		    "passwdutil: missing field from password entry"); \
7907c478bd9Sstevel@tonic-gate 		goto error; \
7917c478bd9Sstevel@tonic-gate 	} \
7927c478bd9Sstevel@tonic-gate 	len = ENTRY_LEN(nret, (idx)); \
7937c478bd9Sstevel@tonic-gate 	(dst) = malloc(len+1); \
7947c478bd9Sstevel@tonic-gate 	if ((dst) == NULL) { \
7957c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "passwdutil: out of memory"); \
7967c478bd9Sstevel@tonic-gate 		goto error; \
7977c478bd9Sstevel@tonic-gate 	} \
7987c478bd9Sstevel@tonic-gate 	(dst)[len] = '\0'; \
7997c478bd9Sstevel@tonic-gate 	(void) strncpy((dst), \
8007c478bd9Sstevel@tonic-gate 	    ENTRY_VAL(nret, (idx)) ? ENTRY_VAL(nret, (idx)) : "", \
8017c478bd9Sstevel@tonic-gate 	    len);
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate static struct passwd *
8067c478bd9Sstevel@tonic-gate nisplus_getpw_from_master(const char *name, char *domain)
8077c478bd9Sstevel@tonic-gate {
8087c478bd9Sstevel@tonic-gate 	char lookup[NIS_MAXNAMELEN+1];
8097c478bd9Sstevel@tonic-gate 	nis_result *res;
8107c478bd9Sstevel@tonic-gate 	nis_object *nret;
8117c478bd9Sstevel@tonic-gate 	int len;
8127c478bd9Sstevel@tonic-gate 	char *p;
8137c478bd9Sstevel@tonic-gate 	struct passwd *pw;
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	if ((pw = calloc(1, sizeof (*pw))) == NULL)
8167c478bd9Sstevel@tonic-gate 		return (NULL);
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	res = nisplus_match(name, domain, lookup, sizeof (lookup));
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	if (res == NULL)
8217c478bd9Sstevel@tonic-gate 		return (NULL);
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	nret = NIS_RES_OBJECT(res);
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	SAFE_STRDUP(pw->pw_name, 0);
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	if ((pw->pw_passwd = strdup("x")) == NULL) {
8287c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "passwdutil: out of memory");
8297c478bd9Sstevel@tonic-gate 		goto error;
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	SAFE_STRDUP(p, 2);
8337c478bd9Sstevel@tonic-gate 	pw->pw_uid = atoi(p);
8347c478bd9Sstevel@tonic-gate 	free(p);
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	SAFE_STRDUP(p, 3);
8377c478bd9Sstevel@tonic-gate 	pw->pw_gid = atoi(p);
8387c478bd9Sstevel@tonic-gate 	free(p);
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	pw->pw_age = NULL;
8417c478bd9Sstevel@tonic-gate 	pw->pw_comment = NULL;
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/
8447c478bd9Sstevel@tonic-gate 	SAFE_STRDUP(pw->pw_gecos, 4);
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/
8477c478bd9Sstevel@tonic-gate 	SAFE_STRDUP(pw->pw_dir, 5);
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/
8507c478bd9Sstevel@tonic-gate 	SAFE_STRDUP(pw->pw_shell, 6);
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	nis_freeresult(res);
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	return (pw);
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate error:
8577c478bd9Sstevel@tonic-gate 	nis_freeresult(res);
8587c478bd9Sstevel@tonic-gate 	if (pw->pw_name)
8597c478bd9Sstevel@tonic-gate 		free(pw->pw_name);
8607c478bd9Sstevel@tonic-gate 	if (pw->pw_passwd)
8617c478bd9Sstevel@tonic-gate 		free(pw->pw_passwd);
8627c478bd9Sstevel@tonic-gate 	if (pw->pw_gecos)
8637c478bd9Sstevel@tonic-gate 		free(pw->pw_gecos);
8647c478bd9Sstevel@tonic-gate 	if (pw->pw_dir)
8657c478bd9Sstevel@tonic-gate 		free(pw->pw_dir);
8667c478bd9Sstevel@tonic-gate 	if (pw->pw_shell)
8677c478bd9Sstevel@tonic-gate 		free(pw->pw_shell);
8687c478bd9Sstevel@tonic-gate 	free(pw);
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	return (NULL);
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate /*
8747c478bd9Sstevel@tonic-gate  * struct spwd * nisplus_getsp_from_master()
8757c478bd9Sstevel@tonic-gate  *
8767c478bd9Sstevel@tonic-gate  * Get the shadow structure from a NIS+ master.
8777c478bd9Sstevel@tonic-gate  * This routine normally runs with EUID==0. This can cause trouble
8787c478bd9Sstevel@tonic-gate  * if the NIS+ tables are locked down so that only the owner can
8797c478bd9Sstevel@tonic-gate  * access the encrypted password. If we detect that scenario, we switch
8807c478bd9Sstevel@tonic-gate  * EUID to the owner of the record and refetch it.
8817c478bd9Sstevel@tonic-gate  */
8827c478bd9Sstevel@tonic-gate static struct spwd *
8837c478bd9Sstevel@tonic-gate nisplus_getsp_from_master(const char *name, char *domain)
8847c478bd9Sstevel@tonic-gate {
8857c478bd9Sstevel@tonic-gate 	char lookup[NIS_MAXNAMELEN+1];
8867c478bd9Sstevel@tonic-gate 	nis_result *res = NULL;
8877c478bd9Sstevel@tonic-gate 	nis_object *nret = NULL;
8887c478bd9Sstevel@tonic-gate 	int len;
8897c478bd9Sstevel@tonic-gate 	struct spwd *spw;
8907c478bd9Sstevel@tonic-gate 	char *shadow = NULL;
8917c478bd9Sstevel@tonic-gate 	const char *p = NULL;
8927c478bd9Sstevel@tonic-gate 	const char *limit;
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	res = nisplus_match(name, domain, lookup, sizeof (lookup));
8957c478bd9Sstevel@tonic-gate 	if (res == NULL)
8967c478bd9Sstevel@tonic-gate 		return (NULL);
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	nret = NIS_RES_OBJECT(res);
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	/*CONSTANTCONDITION*/
9017c478bd9Sstevel@tonic-gate 	SAFE_STRDUP(shadow, 7);
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	/*
9047c478bd9Sstevel@tonic-gate 	 * If we got "*NP*" as password, try again with EUID set to
9057c478bd9Sstevel@tonic-gate 	 * the UID of the record-owner.
9067c478bd9Sstevel@tonic-gate 	 */
9077c478bd9Sstevel@tonic-gate 	if (strncmp(shadow, "*NP*", 4) == 0) {
9087c478bd9Sstevel@tonic-gate 		char *p;
9097c478bd9Sstevel@tonic-gate 		uid_t owner_uid;
9107c478bd9Sstevel@tonic-gate 		uid_t euid = geteuid();
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 		SAFE_STRDUP(p, 2);	/* record-owner field */
9137c478bd9Sstevel@tonic-gate 		owner_uid = atoi(p);
9147c478bd9Sstevel@tonic-gate 		free(p);
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 		if (owner_uid != euid) {
9177c478bd9Sstevel@tonic-gate 			/* re-obtain entry using owners EUID */
9187c478bd9Sstevel@tonic-gate 			free(shadow);
9197c478bd9Sstevel@tonic-gate 			nis_freeresult(res);
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 			(void) seteuid(owner_uid);
9227c478bd9Sstevel@tonic-gate 			res = nisplus_match(name, domain, lookup,
9237c478bd9Sstevel@tonic-gate 			    sizeof (lookup));
9247c478bd9Sstevel@tonic-gate 			(void) seteuid(euid);
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 			if (res == NULL)
9277c478bd9Sstevel@tonic-gate 				return (NULL);
9287c478bd9Sstevel@tonic-gate 			nret = NIS_RES_OBJECT(res);
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 			/*CONSTANTCONDITION*/
9317c478bd9Sstevel@tonic-gate 			SAFE_STRDUP(shadow, 7);
9327c478bd9Sstevel@tonic-gate 		}
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	if ((spw = calloc(1, sizeof (*spw))) == NULL) {
9367c478bd9Sstevel@tonic-gate 		nis_freeresult(res);
9377c478bd9Sstevel@tonic-gate 		return (NULL);
9387c478bd9Sstevel@tonic-gate 	}
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	SAFE_STRDUP(spw->sp_namp, 0);
9417c478bd9Sstevel@tonic-gate 	SAFE_STRDUP(spw->sp_pwdp, 1);
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	nis_freeresult(res);
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	limit = shadow + strlen(shadow) + 1;
9467c478bd9Sstevel@tonic-gate 	p = shadow;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	spw->sp_lstchg = -1;
9497c478bd9Sstevel@tonic-gate 	spw->sp_min	= -1;
9507c478bd9Sstevel@tonic-gate 	spw->sp_max	= -1;
9517c478bd9Sstevel@tonic-gate 	spw->sp_warn	= -1;
9527c478bd9Sstevel@tonic-gate 	spw->sp_inact = -1;
9537c478bd9Sstevel@tonic-gate 	spw->sp_expire = -1;
9547c478bd9Sstevel@tonic-gate 	spw->sp_flag	= 0;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &spw->sp_lstchg))
9577c478bd9Sstevel@tonic-gate 		goto out;
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &spw->sp_min))
9607c478bd9Sstevel@tonic-gate 		goto out;
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &spw->sp_max))
9637c478bd9Sstevel@tonic-gate 		goto out;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &spw->sp_warn))
9667c478bd9Sstevel@tonic-gate 		goto out;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &spw->sp_inact))
9697c478bd9Sstevel@tonic-gate 		goto out;
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &spw->sp_expire))
9727c478bd9Sstevel@tonic-gate 		goto out;
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 1, &spw->sp_flag))
9757c478bd9Sstevel@tonic-gate 		goto out;
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	if (p != limit) {
9787c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "passwdutil: garbage at end of record");
9797c478bd9Sstevel@tonic-gate 		goto error;
9807c478bd9Sstevel@tonic-gate 	}
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate out:
9837c478bd9Sstevel@tonic-gate 	free(shadow);
9847c478bd9Sstevel@tonic-gate 	return (spw);
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate error:
9877c478bd9Sstevel@tonic-gate 	if (spw->sp_namp)
9887c478bd9Sstevel@tonic-gate 		free(spw->sp_namp);
9897c478bd9Sstevel@tonic-gate 	if (spw->sp_pwdp)
9907c478bd9Sstevel@tonic-gate 		free(spw->sp_pwdp);
9917c478bd9Sstevel@tonic-gate 	free(spw);
9927c478bd9Sstevel@tonic-gate 	if (shadow)
9937c478bd9Sstevel@tonic-gate 		free(shadow);
9947c478bd9Sstevel@tonic-gate 	return (NULL);
9957c478bd9Sstevel@tonic-gate }
996