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  */
21*5ad42b1bSSurya Prakki 
227c478bd9Sstevel@tonic-gate /*
2336e852a1SRaja Andra  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <nsswitch.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <syslog.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include "ns_sldap.h"
377c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
387c478bd9Sstevel@tonic-gate #include <nsswitch.h>
397c478bd9Sstevel@tonic-gate #include <pwd.h>
407c478bd9Sstevel@tonic-gate #include <shadow.h>
417c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include "passwdutil.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * name_to_int(rep)
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * Translate the repository to a bitmask.
497c478bd9Sstevel@tonic-gate  * if we don't recognise the repository name, we return REP_ERANGE
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate int
name_to_int(char * rep_name)527c478bd9Sstevel@tonic-gate name_to_int(char *rep_name)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	int result = REP_ERANGE;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	if (strcmp(rep_name, "files") == 0)
577c478bd9Sstevel@tonic-gate 		result = REP_FILES;
587c478bd9Sstevel@tonic-gate 	else if (strcmp(rep_name, "nis") == 0)
597c478bd9Sstevel@tonic-gate 		result = REP_NIS;
607c478bd9Sstevel@tonic-gate 	else if (strcmp(rep_name, "ldap") == 0)
617c478bd9Sstevel@tonic-gate 		result = REP_LDAP;
627c478bd9Sstevel@tonic-gate 	else if (strcmp(rep_name, "compat") == 0) {
637c478bd9Sstevel@tonic-gate 		struct __nsw_switchconfig *cfg;
647c478bd9Sstevel@tonic-gate 		enum   __nsw_parse_err pserr;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 		cfg = __nsw_getconfig("passwd_compat", &pserr);
677c478bd9Sstevel@tonic-gate 		if (cfg == NULL) {
687c478bd9Sstevel@tonic-gate 			result = REP_FILES | REP_NIS;
697c478bd9Sstevel@tonic-gate 		} else {
7036e852a1SRaja Andra 			if (strcmp(cfg->lookups->service_name, "ldap") == 0)
717c478bd9Sstevel@tonic-gate 				result = REP_FILES | REP_LDAP;
727c478bd9Sstevel@tonic-gate 			else
737c478bd9Sstevel@tonic-gate 				result = REP_ERANGE;
74*5ad42b1bSSurya Prakki 			(void) __nsw_freeconfig(cfg);
757c478bd9Sstevel@tonic-gate 		}
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	return (result);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * Figure out which repository we use in compat mode.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate int
get_compat_mode(void)857c478bd9Sstevel@tonic-gate get_compat_mode(void)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	struct __nsw_switchconfig *cfg;
887c478bd9Sstevel@tonic-gate 	enum   __nsw_parse_err pserr;
897c478bd9Sstevel@tonic-gate 	int result = REP_COMPAT_NIS;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if ((cfg = __nsw_getconfig("passwd_compat", &pserr)) != NULL) {
9236e852a1SRaja Andra 		if (strcmp(cfg->lookups->service_name, "ldap") == 0)
937c478bd9Sstevel@tonic-gate 			result = REP_COMPAT_LDAP;
947c478bd9Sstevel@tonic-gate 	}
95*5ad42b1bSSurya Prakki 	(void) __nsw_freeconfig(cfg);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	return (result);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * get_ns(rep, accesstype)
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  * returns a bitmask of repositories to use based on either
1047c478bd9Sstevel@tonic-gate  *   1. the repository that is given as argument
1057c478bd9Sstevel@tonic-gate  *   2. the nsswitch.conf file
1067c478bd9Sstevel@tonic-gate  *   3. the type of access requested
1077c478bd9Sstevel@tonic-gate  *
1087c478bd9Sstevel@tonic-gate  * "accesstype" indicates whether we are reading from or writing to the
1097c478bd9Sstevel@tonic-gate  * repository. We need to know this since "compat" will translate into
1107c478bd9Sstevel@tonic-gate  * REP_NSS (the nss-switch) for READ access (needed to decode
1117c478bd9Sstevel@tonic-gate  * the black-magic '+' entries) but it translates into a bitmask
1127c478bd9Sstevel@tonic-gate  * on WRITE access.
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  * If we detect read-access in compat mode, we augment the result
11536e852a1SRaja Andra  * with one of REP_COMPAT_{NIS,LDAP}. We need this in order to
1167c478bd9Sstevel@tonic-gate  * implement ATTR_REP_NAME in nss_getpwnam.
1177c478bd9Sstevel@tonic-gate  *
1187c478bd9Sstevel@tonic-gate  * A return value of REP_NOREP indicates an error.
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate int
get_ns(pwu_repository_t * rep,int accesstype)1217c478bd9Sstevel@tonic-gate get_ns(pwu_repository_t *rep, int accesstype)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	struct __nsw_switchconfig *conf = NULL;
1247c478bd9Sstevel@tonic-gate 	enum __nsw_parse_err pserr;
1257c478bd9Sstevel@tonic-gate 	struct __nsw_lookup *lkp;
1267c478bd9Sstevel@tonic-gate 	struct __nsw_lookup *lkp2;
1272b4a7802SBaban Kenkre 	struct __nsw_lookup *lkp3;
1282b4a7802SBaban Kenkre 	struct __nsw_lookup *lkpn;
1297c478bd9Sstevel@tonic-gate 	int result = REP_NOREP;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (rep != PWU_DEFAULT_REP) {
1327c478bd9Sstevel@tonic-gate 		result = name_to_int(rep->type);
1337c478bd9Sstevel@tonic-gate 		return (result);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	conf = __nsw_getconfig("passwd", &pserr);
1377c478bd9Sstevel@tonic-gate 	if (conf == NULL) {
1387c478bd9Sstevel@tonic-gate 		/*
1397c478bd9Sstevel@tonic-gate 		 * No config found. The user didn't supply a repository,
1407c478bd9Sstevel@tonic-gate 		 * so we try to change the password in the default
1417c478bd9Sstevel@tonic-gate 		 * repositories (files and nis) even though we cannot
1427c478bd9Sstevel@tonic-gate 		 * find the name service switch entry. (Backward compat)
1437c478bd9Sstevel@tonic-gate 		 */
1447c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "passwdutil.so: nameservice switch entry for "
1452b4a7802SBaban Kenkre 		    "passwd not found.");
1467c478bd9Sstevel@tonic-gate 		result = REP_FILES | REP_NIS;
1477c478bd9Sstevel@tonic-gate 		return (result);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	lkp = conf->lookups;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/*
1532b4a7802SBaban Kenkre 	 * Supported nsswitch.conf can have a maximum of 3 repositories.
1547c478bd9Sstevel@tonic-gate 	 * If we encounter an unsupported nsswitch.conf, we return REP_NSS
1557c478bd9Sstevel@tonic-gate 	 * to fall back to the nsswitch backend.
1562b4a7802SBaban Kenkre 	 *
1572b4a7802SBaban Kenkre 	 * Note that specifying 'ad' in the configuration is acceptable
1582b4a7802SBaban Kenkre 	 * though changing AD users' passwords through passwd(1) is not.
1592b4a7802SBaban Kenkre 	 * Therefore "ad" will be silently ignored.
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	if (conf->num_lookups == 1) {
1627c478bd9Sstevel@tonic-gate 		/* files or compat */
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		if (strcmp(lkp->service_name, "files") == 0) {
1657c478bd9Sstevel@tonic-gate 			result = name_to_int(lkp->service_name);
1667c478bd9Sstevel@tonic-gate 		} else if (strcmp(lkp->service_name, "compat") == 0) {
1677c478bd9Sstevel@tonic-gate 			if (accesstype == PWU_READ)
1687c478bd9Sstevel@tonic-gate 				result = REP_NSS | get_compat_mode();
1697c478bd9Sstevel@tonic-gate 			else
1707c478bd9Sstevel@tonic-gate 				result = name_to_int(lkp->service_name);
1717c478bd9Sstevel@tonic-gate 		} else
1727c478bd9Sstevel@tonic-gate 			result = REP_NSS;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	} else if (conf->num_lookups == 2) {
1757c478bd9Sstevel@tonic-gate 		lkp2 = lkp->next;
1767c478bd9Sstevel@tonic-gate 		if (strcmp(lkp->service_name, "files") == 0) {
1777c478bd9Sstevel@tonic-gate 			result = REP_FILES;
1787c478bd9Sstevel@tonic-gate 			if (strcmp(lkp2->service_name, "ldap") == 0)
1797c478bd9Sstevel@tonic-gate 				result |= REP_LDAP;
1807c478bd9Sstevel@tonic-gate 			else if (strcmp(lkp2->service_name, "nis") == 0)
1817c478bd9Sstevel@tonic-gate 				result |= REP_NIS;
1822b4a7802SBaban Kenkre 			else if (strcmp(lkp2->service_name, "ad") != 0)
1832b4a7802SBaban Kenkre 				result = REP_NSS;
1842b4a7802SBaban Kenkre 			/* AD is ignored */
1852b4a7802SBaban Kenkre 		} else {
1862b4a7802SBaban Kenkre 			result = REP_NSS;
1872b4a7802SBaban Kenkre 		}
1882b4a7802SBaban Kenkre 	} else if (conf->num_lookups == 3) {
1892b4a7802SBaban Kenkre 		/*
1902b4a7802SBaban Kenkre 		 * Valid configurations with 3 repositories are:
19136e852a1SRaja Andra 		 *   files ad [nis | ldap ] OR
19236e852a1SRaja Andra 		 *   files [nis | ldap ] ad
1932b4a7802SBaban Kenkre 		 */
1942b4a7802SBaban Kenkre 		lkp2 = lkp->next;
1952b4a7802SBaban Kenkre 		lkp3 = lkp2->next;
1962b4a7802SBaban Kenkre 		if (strcmp(lkp2->service_name, "ad") == 0)
1972b4a7802SBaban Kenkre 			lkpn = lkp3;
1982b4a7802SBaban Kenkre 		else if (strcmp(lkp3->service_name, "ad") == 0)
1992b4a7802SBaban Kenkre 			lkpn = lkp2;
2002b4a7802SBaban Kenkre 		else
2012b4a7802SBaban Kenkre 			lkpn = NULL;
2022b4a7802SBaban Kenkre 		if (strcmp(lkp->service_name, "files") == 0 &&
2032b4a7802SBaban Kenkre 		    lkpn != NULL) {
2042b4a7802SBaban Kenkre 			result = REP_FILES;
2052b4a7802SBaban Kenkre 			if (strcmp(lkpn->service_name, "ldap") == 0)
2062b4a7802SBaban Kenkre 				result |= REP_LDAP;
2072b4a7802SBaban Kenkre 			else if (strcmp(lkpn->service_name, "nis") == 0)
2082b4a7802SBaban Kenkre 				result |= REP_NIS;
2097c478bd9Sstevel@tonic-gate 			else
2107c478bd9Sstevel@tonic-gate 				result = REP_NSS;
2117c478bd9Sstevel@tonic-gate 		} else {
2127c478bd9Sstevel@tonic-gate 			result = REP_NSS;
2137c478bd9Sstevel@tonic-gate 		}
2147c478bd9Sstevel@tonic-gate 	} else {
2157c478bd9Sstevel@tonic-gate 		result = REP_NSS;
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
218*5ad42b1bSSurya Prakki 	(void) __nsw_freeconfig(conf);
2197c478bd9Sstevel@tonic-gate 	return (result);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate static void
nss_ldap_passwd(p)2237c478bd9Sstevel@tonic-gate nss_ldap_passwd(p)
2247c478bd9Sstevel@tonic-gate 	nss_db_params_t	*p;
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	p->name = NSS_DBNAM_PASSWD;
2277c478bd9Sstevel@tonic-gate 	p->flags |= NSS_USE_DEFAULT_CONFIG;
2287c478bd9Sstevel@tonic-gate 	p->default_config = "ldap";
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate static void
nss_ldap_shadow(p)2327c478bd9Sstevel@tonic-gate nss_ldap_shadow(p)
2337c478bd9Sstevel@tonic-gate 	nss_db_params_t	*p;
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	p->name = NSS_DBNAM_SHADOW;
2367c478bd9Sstevel@tonic-gate 	p->config_name    = NSS_DBNAM_PASSWD;	/* Use config for "passwd" */
2377c478bd9Sstevel@tonic-gate 	p->flags |= NSS_USE_DEFAULT_CONFIG;
2387c478bd9Sstevel@tonic-gate 	p->default_config = "ldap";
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate #ifdef PAM_NIS
2437c478bd9Sstevel@tonic-gate static void
nss_nis_passwd(p)2447c478bd9Sstevel@tonic-gate nss_nis_passwd(p)
2457c478bd9Sstevel@tonic-gate 	nss_db_params_t	*p;
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 	p->name = NSS_DBNAM_PASSWD;
2487c478bd9Sstevel@tonic-gate 	p->flags |= NSS_USE_DEFAULT_CONFIG;
2497c478bd9Sstevel@tonic-gate 	p->default_config = "nis";
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate static void
nss_nis_shadow(p)2537c478bd9Sstevel@tonic-gate nss_nis_shadow(p)
2547c478bd9Sstevel@tonic-gate 	nss_db_params_t	*p;
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	p->name = NSS_DBNAM_SHADOW;
2577c478bd9Sstevel@tonic-gate 	p->config_name    = NSS_DBNAM_PASSWD;	/* Use config for "passwd" */
2587c478bd9Sstevel@tonic-gate 	p->flags |= NSS_USE_DEFAULT_CONFIG;
2597c478bd9Sstevel@tonic-gate 	p->default_config = "nis";
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate #endif /* PAM_NIS */
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate static char *
gettok(nextpp)2647c478bd9Sstevel@tonic-gate gettok(nextpp)
2657c478bd9Sstevel@tonic-gate 	char	**nextpp;
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	char	*p = *nextpp;
2687c478bd9Sstevel@tonic-gate 	char	*q = p;
2697c478bd9Sstevel@tonic-gate 	char	c;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	if (p == 0) {
2727c478bd9Sstevel@tonic-gate 		return (0);
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 	while ((c = *q) != '\0' && c != ':') {
2757c478bd9Sstevel@tonic-gate 		q++;
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 	if (c == '\0') {
2787c478bd9Sstevel@tonic-gate 		*nextpp = 0;
2797c478bd9Sstevel@tonic-gate 	} else {
2807c478bd9Sstevel@tonic-gate 		*q++ = '\0';
2817c478bd9Sstevel@tonic-gate 		*nextpp = q;
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 	return (p);
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate /*
2877c478bd9Sstevel@tonic-gate  * Return values: 0 = success, 1 = parse error, 2 = erange ...
2887c478bd9Sstevel@tonic-gate  * The structure pointer passed in is a structure in the caller's space
2897c478bd9Sstevel@tonic-gate  * wherein the field pointers would be set to areas in the buffer if
2907c478bd9Sstevel@tonic-gate  * need be. instring and buffer should be separate areas.
2917c478bd9Sstevel@tonic-gate  */
2927c478bd9Sstevel@tonic-gate static int
str2passwd(const char * instr,int lenstr,void * ent,char * buffer,int buflen)2937c478bd9Sstevel@tonic-gate str2passwd(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	struct passwd	*passwd	= (struct passwd *)ent;
2967c478bd9Sstevel@tonic-gate 	char		*p, *next;
2977c478bd9Sstevel@tonic-gate 	int		black_magic;	/* "+" or "-" entry */
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (lenstr + 1 > buflen) {
3007c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_ERANGE);
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 	/*
3037c478bd9Sstevel@tonic-gate 	 * We copy the input string into the output buffer and
3047c478bd9Sstevel@tonic-gate 	 * operate on it in place.
3057c478bd9Sstevel@tonic-gate 	 */
3067c478bd9Sstevel@tonic-gate 	(void) memcpy(buffer, instr, lenstr);
3077c478bd9Sstevel@tonic-gate 	buffer[lenstr] = '\0';
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	next = buffer;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	passwd->pw_name = p = gettok(&next);		/* username */
3127c478bd9Sstevel@tonic-gate 	if (*p == '\0') {
3137c478bd9Sstevel@tonic-gate 		/* Empty username;  not allowed */
3147c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 	black_magic = (*p == '+' || *p == '-');
3177c478bd9Sstevel@tonic-gate 	if (black_magic) {
3187c478bd9Sstevel@tonic-gate 		passwd->pw_uid	= UID_NOBODY;
3197c478bd9Sstevel@tonic-gate 		passwd->pw_gid	= GID_NOBODY;
3207c478bd9Sstevel@tonic-gate 		/*
3217c478bd9Sstevel@tonic-gate 		 * pwconv tests pw_passwd and pw_age == NULL
3227c478bd9Sstevel@tonic-gate 		 */
3237c478bd9Sstevel@tonic-gate 		passwd->pw_passwd = "";
3247c478bd9Sstevel@tonic-gate 		passwd->pw_age	= "";
3257c478bd9Sstevel@tonic-gate 		/*
3267c478bd9Sstevel@tonic-gate 		 * the rest of the passwd entry is "optional"
3277c478bd9Sstevel@tonic-gate 		 */
3287c478bd9Sstevel@tonic-gate 		passwd->pw_comment = "";
3297c478bd9Sstevel@tonic-gate 		passwd->pw_gecos = "";
3307c478bd9Sstevel@tonic-gate 		passwd->pw_dir	= "";
3317c478bd9Sstevel@tonic-gate 		passwd->pw_shell = "";
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	passwd->pw_passwd = p = gettok(&next);		/* password */
3357c478bd9Sstevel@tonic-gate 	if (p == 0) {
3367c478bd9Sstevel@tonic-gate 		if (black_magic)
3377c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
3387c478bd9Sstevel@tonic-gate 		else
3397c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 	for (; *p != '\0'; p++) {			/* age */
3427c478bd9Sstevel@tonic-gate 		if (*p == ',') {
3437c478bd9Sstevel@tonic-gate 			*p++ = '\0';
3447c478bd9Sstevel@tonic-gate 			break;
3457c478bd9Sstevel@tonic-gate 		}
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 	passwd->pw_age = p;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	p = next;					/* uid */
3507c478bd9Sstevel@tonic-gate 	if (p == 0 || *p == '\0') {
3517c478bd9Sstevel@tonic-gate 		if (black_magic)
3527c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
3537c478bd9Sstevel@tonic-gate 		else
3547c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 	if (!black_magic) {
3577c478bd9Sstevel@tonic-gate 		passwd->pw_uid = strtol(p, &next, 10);
3587c478bd9Sstevel@tonic-gate 		if (next == p) {
3597c478bd9Sstevel@tonic-gate 			/* uid field should be nonempty */
3607c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 		/*
3637c478bd9Sstevel@tonic-gate 		 * The old code (in 2.0 thru 2.5) would check
3647c478bd9Sstevel@tonic-gate 		 * for the uid being negative, or being greater
3657c478bd9Sstevel@tonic-gate 		 * than 60001 (the rfs limit).  If it met either of
3667c478bd9Sstevel@tonic-gate 		 * these conditions, the uid was translated to 60001.
3677c478bd9Sstevel@tonic-gate 		 *
368f48205beScasper 		 * Now we just check for ephemeral uids; anything else
3697c478bd9Sstevel@tonic-gate 		 * is administrative policy
3707c478bd9Sstevel@tonic-gate 		 */
371f48205beScasper 		if (passwd->pw_uid > MAXUID)
3727c478bd9Sstevel@tonic-gate 			passwd->pw_uid = UID_NOBODY;
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 	if (*next++ != ':') {
3757c478bd9Sstevel@tonic-gate 		if (black_magic)
3767c478bd9Sstevel@tonic-gate 			p = gettok(&next);
3777c478bd9Sstevel@tonic-gate 		else
3787c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 	p = next;					/* gid */
3817c478bd9Sstevel@tonic-gate 	if (p == 0 || *p == '\0') {
3827c478bd9Sstevel@tonic-gate 		if (black_magic)
3837c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
3847c478bd9Sstevel@tonic-gate 		else
3857c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 	if (!black_magic) {
3887c478bd9Sstevel@tonic-gate 		passwd->pw_gid = strtol(p, &next, 10);
3897c478bd9Sstevel@tonic-gate 		if (next == p) {
3907c478bd9Sstevel@tonic-gate 			/* gid field should be nonempty */
3917c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3927c478bd9Sstevel@tonic-gate 		}
3937c478bd9Sstevel@tonic-gate 		/*
3947c478bd9Sstevel@tonic-gate 		 * gid should be non-negative; anything else
3957c478bd9Sstevel@tonic-gate 		 * is administrative policy.
3967c478bd9Sstevel@tonic-gate 		 */
397f48205beScasper 		if (passwd->pw_gid > MAXUID)
3987c478bd9Sstevel@tonic-gate 			passwd->pw_gid = GID_NOBODY;
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 	if (*next++ != ':') {
4017c478bd9Sstevel@tonic-gate 		if (black_magic)
4027c478bd9Sstevel@tonic-gate 			p = gettok(&next);
4037c478bd9Sstevel@tonic-gate 		else
4047c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	passwd->pw_gecos = passwd->pw_comment = p = gettok(&next);
4087c478bd9Sstevel@tonic-gate 	if (p == 0) {
4097c478bd9Sstevel@tonic-gate 		if (black_magic)
4107c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
4117c478bd9Sstevel@tonic-gate 		else
4127c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	passwd->pw_dir = p = gettok(&next);
4167c478bd9Sstevel@tonic-gate 	if (p == 0) {
4177c478bd9Sstevel@tonic-gate 		if (black_magic)
4187c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
4197c478bd9Sstevel@tonic-gate 		else
4207c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	passwd->pw_shell = p = gettok(&next);
4247c478bd9Sstevel@tonic-gate 	if (p == 0) {
4257c478bd9Sstevel@tonic-gate 		if (black_magic)
4267c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
4277c478bd9Sstevel@tonic-gate 		else
4287c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	/* Better not be any more fields... */
4327c478bd9Sstevel@tonic-gate 	if (next == 0) {
4337c478bd9Sstevel@tonic-gate 		/* Successfully parsed and stored */
4347c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_SUCCESS);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 	return (NSS_STR_PARSE_PARSE);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate typedef const char *constp;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate /*
4427c478bd9Sstevel@tonic-gate  * Return value 1 means success and more input, 0 means error or no more
4437c478bd9Sstevel@tonic-gate  */
4447c478bd9Sstevel@tonic-gate static int
getfield(nextp,limit,uns,valp)4457c478bd9Sstevel@tonic-gate getfield(nextp, limit, uns, valp)
4467c478bd9Sstevel@tonic-gate 	constp		*nextp;
4477c478bd9Sstevel@tonic-gate 	constp		limit;
4487c478bd9Sstevel@tonic-gate 	int		uns;
4497c478bd9Sstevel@tonic-gate 	void		*valp;
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	constp		p = *nextp;
4527c478bd9Sstevel@tonic-gate 	char		*endfield;
4537c478bd9Sstevel@tonic-gate 	char		numbuf[12];  /* Holds -2^31 and trailing ':' */
4547c478bd9Sstevel@tonic-gate 	int		len;
4557c478bd9Sstevel@tonic-gate 	long		x;
4567c478bd9Sstevel@tonic-gate 	unsigned long	ux;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	if (p == 0 || p >= limit) {
4597c478bd9Sstevel@tonic-gate 		return (0);
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 	if (*p == ':') {
4627c478bd9Sstevel@tonic-gate 		p++;
4637c478bd9Sstevel@tonic-gate 		*nextp = p;
4647c478bd9Sstevel@tonic-gate 		return (p < limit);
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 	if ((len = limit - p) > sizeof (numbuf) - 1) {
4677c478bd9Sstevel@tonic-gate 		len = sizeof (numbuf) - 1;
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 	/*
4707c478bd9Sstevel@tonic-gate 	 * We want to use strtol() and we have a readonly non-zero-terminated
4717c478bd9Sstevel@tonic-gate 	 *   string, so first we copy and terminate the interesting bit.
4727c478bd9Sstevel@tonic-gate 	 *   Ugh.  (It's convenient to terminate with a colon rather than \0).
4737c478bd9Sstevel@tonic-gate 	 */
4747c478bd9Sstevel@tonic-gate 	if ((endfield = memccpy(numbuf, p, ':', len)) == 0) {
4757c478bd9Sstevel@tonic-gate 		if (len != limit - p) {
4767c478bd9Sstevel@tonic-gate 			/* Error -- field is too big to be a legit number */
4777c478bd9Sstevel@tonic-gate 			return (0);
4787c478bd9Sstevel@tonic-gate 		}
4797c478bd9Sstevel@tonic-gate 		numbuf[len] = ':';
4807c478bd9Sstevel@tonic-gate 		p = limit;
4817c478bd9Sstevel@tonic-gate 	} else {
4827c478bd9Sstevel@tonic-gate 		p += (endfield - numbuf);
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 	if (uns) {
4857c478bd9Sstevel@tonic-gate 		ux = strtoul(numbuf, &endfield, 10);
4867c478bd9Sstevel@tonic-gate 		if (*endfield != ':') {
4877c478bd9Sstevel@tonic-gate 			/* Error -- expected <integer><colon> */
4887c478bd9Sstevel@tonic-gate 			return (0);
4897c478bd9Sstevel@tonic-gate 		}
4907c478bd9Sstevel@tonic-gate 		*((unsigned int *)valp) = (unsigned int)ux;
4917c478bd9Sstevel@tonic-gate 	} else {
4927c478bd9Sstevel@tonic-gate 		x = strtol(numbuf, &endfield, 10);
4937c478bd9Sstevel@tonic-gate 		if (*endfield != ':') {
4947c478bd9Sstevel@tonic-gate 			/* Error -- expected <integer><colon> */
4957c478bd9Sstevel@tonic-gate 			return (0);
4967c478bd9Sstevel@tonic-gate 		}
4977c478bd9Sstevel@tonic-gate 		*((int *)valp) = (int)x;
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate 	*nextp = p;
5007c478bd9Sstevel@tonic-gate 	return (p < limit);
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate /*
5047c478bd9Sstevel@tonic-gate  *  str2spwd() -- convert a string to a shadow passwd entry.  The parser is
5057c478bd9Sstevel@tonic-gate  *	more liberal than the passwd or group parsers;  since it's legitimate
5067c478bd9Sstevel@tonic-gate  *	for almost all the fields here to be blank, the parser lets one omit
5077c478bd9Sstevel@tonic-gate  *	any number of blank fields at the end of the entry.  The acceptable
5087c478bd9Sstevel@tonic-gate  *	forms for '+' and '-' entries are the same as those for normal entries.
5097c478bd9Sstevel@tonic-gate  *  === Is this likely to do more harm than good?
5107c478bd9Sstevel@tonic-gate  *
5117c478bd9Sstevel@tonic-gate  * Return values: 0 = success, 1 = parse error, 2 = erange ...
5127c478bd9Sstevel@tonic-gate  * The structure pointer passed in is a structure in the caller's space
5137c478bd9Sstevel@tonic-gate  * wherein the field pointers would be set to areas in the buffer if
5147c478bd9Sstevel@tonic-gate  * need be. instring and buffer should be separate areas.
5157c478bd9Sstevel@tonic-gate  */
5167c478bd9Sstevel@tonic-gate int
str2spwd(instr,lenstr,ent,buffer,buflen)5177c478bd9Sstevel@tonic-gate str2spwd(instr, lenstr, ent, buffer, buflen)
5187c478bd9Sstevel@tonic-gate 	const char	*instr;
5197c478bd9Sstevel@tonic-gate 	int		lenstr;
5207c478bd9Sstevel@tonic-gate 	void	*ent; /* really (struct spwd *) */
5217c478bd9Sstevel@tonic-gate 	char	*buffer;
5227c478bd9Sstevel@tonic-gate 	int	buflen;
5237c478bd9Sstevel@tonic-gate {
5247c478bd9Sstevel@tonic-gate 	struct spwd	*shadow	= (struct spwd *)ent;
5257c478bd9Sstevel@tonic-gate 	const char	*p = instr, *limit;
5267c478bd9Sstevel@tonic-gate 	char		*bufp;
5277c478bd9Sstevel@tonic-gate 	int	lencopy, black_magic;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	limit = p + lenstr;
5307c478bd9Sstevel@tonic-gate 	if ((p = memchr(instr, ':', lenstr)) == 0 ||
5317c478bd9Sstevel@tonic-gate 		++p >= limit ||
5327c478bd9Sstevel@tonic-gate 		(p = memchr(p, ':', limit - p)) == 0) {
5337c478bd9Sstevel@tonic-gate 		lencopy = lenstr;
5347c478bd9Sstevel@tonic-gate 		p = 0;
5357c478bd9Sstevel@tonic-gate 	} else {
5367c478bd9Sstevel@tonic-gate 		lencopy = p - instr;
5377c478bd9Sstevel@tonic-gate 		p++;
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate 	if (lencopy + 1 > buflen) {
5407c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_ERANGE);
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 	(void) memcpy(buffer, instr, lencopy);
5437c478bd9Sstevel@tonic-gate 	buffer[lencopy] = 0;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	black_magic = (*instr == '+' || *instr == '-');
5467c478bd9Sstevel@tonic-gate 	shadow->sp_namp = bufp = buffer;
5477c478bd9Sstevel@tonic-gate 	shadow->sp_pwdp	= 0;
5487c478bd9Sstevel@tonic-gate 	shadow->sp_lstchg = -1;
5497c478bd9Sstevel@tonic-gate 	shadow->sp_min	= -1;
5507c478bd9Sstevel@tonic-gate 	shadow->sp_max	= -1;
5517c478bd9Sstevel@tonic-gate 	shadow->sp_warn	= -1;
5527c478bd9Sstevel@tonic-gate 	shadow->sp_inact = -1;
5537c478bd9Sstevel@tonic-gate 	shadow->sp_expire = -1;
5547c478bd9Sstevel@tonic-gate 	shadow->sp_flag	= 0;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	if ((bufp = strchr(bufp, ':')) == 0) {
5577c478bd9Sstevel@tonic-gate 		if (black_magic)
5587c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
5597c478bd9Sstevel@tonic-gate 		else
5607c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate 	*bufp++ = '\0';
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	shadow->sp_pwdp = bufp;
5657c478bd9Sstevel@tonic-gate 	if (instr == 0) {
5667c478bd9Sstevel@tonic-gate 		if ((bufp = strchr(bufp, ':')) == 0) {
5677c478bd9Sstevel@tonic-gate 			if (black_magic)
5687c478bd9Sstevel@tonic-gate 				return (NSS_STR_PARSE_SUCCESS);
5697c478bd9Sstevel@tonic-gate 			else
5707c478bd9Sstevel@tonic-gate 				return (NSS_STR_PARSE_PARSE);
5717c478bd9Sstevel@tonic-gate 		}
5727c478bd9Sstevel@tonic-gate 		*bufp++ = '\0';
5737c478bd9Sstevel@tonic-gate 		p = bufp;
5747c478bd9Sstevel@tonic-gate 	} /* else p was set when we copied name and passwd into the buffer */
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_lstchg))
5777c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
5787c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_min))
5797c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
5807c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_max))
5817c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
5827c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_warn))
5837c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
5847c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_inact))
5857c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
5867c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_expire))
5877c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
5887c478bd9Sstevel@tonic-gate 	if (!getfield(&p, limit, 1, &shadow->sp_flag))
5897c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
5907c478bd9Sstevel@tonic-gate 	if (p != limit) {
5917c478bd9Sstevel@tonic-gate 		/* Syntax error -- garbage at end of line */
5927c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 	return (NSS_STR_PARSE_SUCCESS);
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate static nss_XbyY_buf_t *buffer;
5987c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate #define	GETBUF()	\
6017c478bd9Sstevel@tonic-gate 	NSS_XbyY_ALLOC(&buffer, sizeof (struct passwd), NSS_BUFLEN_PASSWD)
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate #pragma fini(endutilpwent)
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate static void
endutilpwent(void)6067c478bd9Sstevel@tonic-gate endutilpwent(void)
6077c478bd9Sstevel@tonic-gate {
6087c478bd9Sstevel@tonic-gate 	NSS_XbyY_FREE(&buffer);
6097c478bd9Sstevel@tonic-gate 	nss_delete(&db_root);
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate 
61236e852a1SRaja Andra /*ARGSUSED*/
6137c478bd9Sstevel@tonic-gate struct passwd *
getpwnam_from(const char * name,pwu_repository_t * rep,int reptype)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 #ifdef PAM_NIS
6317c478bd9Sstevel@tonic-gate 	case REP_NIS:
6327c478bd9Sstevel@tonic-gate 		(void) nss_search(&db_root, nss_nis_passwd,
6337c478bd9Sstevel@tonic-gate 		    NSS_DBOP_PASSWD_BYNAME, &arg);
6347c478bd9Sstevel@tonic-gate 		break;
6357c478bd9Sstevel@tonic-gate #endif
6367c478bd9Sstevel@tonic-gate 	default:
6377c478bd9Sstevel@tonic-gate 		return (NULL);
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	return (struct passwd *)NSS_XbyY_FINI(&arg);
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6447c478bd9Sstevel@tonic-gate struct passwd *
getpwuid_from(uid_t uid,pwu_repository_t * rep,int reptype)6457c478bd9Sstevel@tonic-gate getpwuid_from(uid_t uid, pwu_repository_t *rep, int reptype)
6467c478bd9Sstevel@tonic-gate {
6477c478bd9Sstevel@tonic-gate 	nss_XbyY_buf_t  *b = GETBUF();
6487c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	if (b == 0)
6517c478bd9Sstevel@tonic-gate 		return (0);
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2passwd);
6547c478bd9Sstevel@tonic-gate 	arg.key.uid = uid;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	switch (reptype) {
6577c478bd9Sstevel@tonic-gate 	case REP_LDAP:
6587c478bd9Sstevel@tonic-gate 		(void) nss_search(&db_root, nss_ldap_passwd,
6597c478bd9Sstevel@tonic-gate 		    NSS_DBOP_PASSWD_BYUID, &arg);
6607c478bd9Sstevel@tonic-gate 		break;
6617c478bd9Sstevel@tonic-gate #ifdef PAM_NIS
6627c478bd9Sstevel@tonic-gate 	case REP_NIS:
6637c478bd9Sstevel@tonic-gate 		(void) nss_search(&db_root, nss_nis_passwd,
6647c478bd9Sstevel@tonic-gate 		    NSS_DBOP_PASSWD_BYUID, &arg);
6657c478bd9Sstevel@tonic-gate 		break;
6667c478bd9Sstevel@tonic-gate #endif
6677c478bd9Sstevel@tonic-gate 	default:
6687c478bd9Sstevel@tonic-gate 		return (NULL);
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	return (struct passwd *)NSS_XbyY_FINI(&arg);
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate static nss_XbyY_buf_t *spbuf;
6757c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(spdb_root);
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate #define	GETSPBUF()	\
6787c478bd9Sstevel@tonic-gate 	NSS_XbyY_ALLOC(&spbuf, sizeof (struct spwd), NSS_BUFLEN_SHADOW)
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate #pragma fini(endutilspent)
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate static void
endutilspent(void)6837c478bd9Sstevel@tonic-gate endutilspent(void)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	NSS_XbyY_FREE(&spbuf);
6867c478bd9Sstevel@tonic-gate 	nss_delete(&spdb_root);
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate 
68936e852a1SRaja Andra /*ARGSUSED*/
6907c478bd9Sstevel@tonic-gate struct spwd *
getspnam_from(const char * name,pwu_repository_t * rep,int reptype)6917c478bd9Sstevel@tonic-gate getspnam_from(const char *name, pwu_repository_t *rep, int reptype)
6927c478bd9Sstevel@tonic-gate {
6937c478bd9Sstevel@tonic-gate 	nss_XbyY_buf_t  *b = GETSPBUF();
6947c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	if (b == 0)
6977c478bd9Sstevel@tonic-gate 		return (0);
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2spwd);
7007c478bd9Sstevel@tonic-gate 	arg.key.name = name;
7017c478bd9Sstevel@tonic-gate 	switch (reptype) {
7027c478bd9Sstevel@tonic-gate 	case REP_LDAP:
7037c478bd9Sstevel@tonic-gate 		(void) nss_search(&spdb_root, nss_ldap_shadow,
7047c478bd9Sstevel@tonic-gate 		    NSS_DBOP_SHADOW_BYNAME, &arg);
7057c478bd9Sstevel@tonic-gate 		break;
7067c478bd9Sstevel@tonic-gate #ifdef PAM_NIS
7077c478bd9Sstevel@tonic-gate 	case REP_NIS:
7087c478bd9Sstevel@tonic-gate 		(void) nss_search(&spdb_root, nss_nis_shadow,
7097c478bd9Sstevel@tonic-gate 		    NSS_DBOP_SHADOW_BYNAME, &arg);
7107c478bd9Sstevel@tonic-gate 		break;
7117c478bd9Sstevel@tonic-gate #endif
7127c478bd9Sstevel@tonic-gate 	default:
7137c478bd9Sstevel@tonic-gate 		return (NULL);
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 	return (struct spwd *)NSS_XbyY_FINI(&arg);
7167c478bd9Sstevel@tonic-gate }
717