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