xref: /illumos-gate/usr/src/lib/libc/port/gen/getpwnam_r.c (revision 48bbca81)
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
5cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6cb5caa98Sdjl  * 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  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
2362272d53SPradhap Devarajan  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287257d1b4Sraf #include "lint.h"
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <pwd.h>
317c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <synch.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <sys/mman.h>
3862272d53SPradhap Devarajan #include <errno.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate int str2passwd(const char *, int, void *,
417c478bd9Sstevel@tonic-gate 	char *, int);
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
447c478bd9Sstevel@tonic-gate static DEFINE_NSS_GETENT(context);
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate void
_nss_initf_passwd(nss_db_params_t * p)477c478bd9Sstevel@tonic-gate _nss_initf_passwd(nss_db_params_t *p)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	p->name	= NSS_DBNAM_PASSWD;
507c478bd9Sstevel@tonic-gate 	p->default_config = NSS_DEFCONF_PASSWD;
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <getxby_door.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate struct passwd *
567c478bd9Sstevel@tonic-gate _uncached_getpwuid_r(uid_t uid, struct passwd *result, char *buffer,
577c478bd9Sstevel@tonic-gate 	int buflen);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate struct passwd *
607c478bd9Sstevel@tonic-gate _uncached_getpwnam_r(const char *name, struct passwd *result, char *buffer,
617c478bd9Sstevel@tonic-gate     int buflen);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * POSIX.1c Draft-6 version of the function getpwnam_r.
657c478bd9Sstevel@tonic-gate  * It was implemented by Solaris 2.3.
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate struct passwd *
getpwnam_r(const char * name,struct passwd * result,char * buffer,int buflen)687257d1b4Sraf getpwnam_r(const char *name, struct passwd *result, char *buffer, int buflen)
697c478bd9Sstevel@tonic-gate {
70cb5caa98Sdjl 	nss_XbyY_args_t arg;
71cb5caa98Sdjl 
72cb5caa98Sdjl 	if (name == (const char *)NULL) {
737c478bd9Sstevel@tonic-gate 		errno = ERANGE;
747c478bd9Sstevel@tonic-gate 		return (NULL);
757c478bd9Sstevel@tonic-gate 	}
76cb5caa98Sdjl 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2passwd);
77cb5caa98Sdjl 	arg.key.name = name;
78cb5caa98Sdjl 	(void) nss_search(&db_root, _nss_initf_passwd, NSS_DBOP_PASSWD_BYNAME,
797257d1b4Sraf 	    &arg);
80cb5caa98Sdjl 	return ((struct passwd *)NSS_XbyY_FINI(&arg));
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * POSIX.1c Draft-6 version of the function getpwuid_r.
857c478bd9Sstevel@tonic-gate  * It was implemented by Solaris 2.3.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate struct passwd *
getpwuid_r(uid_t uid,struct passwd * result,char * buffer,int buflen)887257d1b4Sraf getpwuid_r(uid_t uid, struct passwd *result, char *buffer, int buflen)
897c478bd9Sstevel@tonic-gate {
90cb5caa98Sdjl 	nss_XbyY_args_t arg;
917c478bd9Sstevel@tonic-gate 
92cb5caa98Sdjl 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2passwd);
93cb5caa98Sdjl 	arg.key.uid = uid;
94cb5caa98Sdjl 	(void) nss_search(&db_root, _nss_initf_passwd, NSS_DBOP_PASSWD_BYUID,
957257d1b4Sraf 	    &arg);
96cb5caa98Sdjl 	return ((struct passwd *)NSS_XbyY_FINI(&arg));
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate struct passwd *
_uncached_getpwuid_r(uid_t uid,struct passwd * result,char * buffer,int buflen)1017c478bd9Sstevel@tonic-gate _uncached_getpwuid_r(uid_t uid, struct passwd *result, char *buffer,
1027c478bd9Sstevel@tonic-gate 	int buflen)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2passwd);
1077c478bd9Sstevel@tonic-gate 	arg.key.uid = uid;
1087c478bd9Sstevel@tonic-gate 	(void) nss_search(&db_root, _nss_initf_passwd, NSS_DBOP_PASSWD_BYUID,
1097257d1b4Sraf 	    &arg);
1107c478bd9Sstevel@tonic-gate 	return ((struct passwd *)NSS_XbyY_FINI(&arg));
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * POSIX.1c standard version of the function getpwuid_r.
1167c478bd9Sstevel@tonic-gate  * User gets it via static getpwuid_r from the header file.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate int
__posix_getpwuid_r(uid_t uid,struct passwd * pwd,char * buffer,size_t bufsize,struct passwd ** result)1197c478bd9Sstevel@tonic-gate __posix_getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer,
1207c478bd9Sstevel@tonic-gate     size_t bufsize, struct passwd **result)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	int nerrno = 0;
1237c478bd9Sstevel@tonic-gate 	int oerrno = errno;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	errno = 0;
1267257d1b4Sraf 	if ((*result = getpwuid_r(uid, pwd, buffer, (uintptr_t)bufsize))
1277257d1b4Sraf 	    == NULL) {
1287c478bd9Sstevel@tonic-gate 			nerrno = errno;
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 	errno = oerrno;
1317c478bd9Sstevel@tonic-gate 	return (nerrno);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate struct passwd *
_uncached_getpwnam_r(const char * name,struct passwd * result,char * buffer,int buflen)1357c478bd9Sstevel@tonic-gate _uncached_getpwnam_r(const char *name, struct passwd *result, char *buffer,
1367c478bd9Sstevel@tonic-gate 	int buflen)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2passwd);
1417c478bd9Sstevel@tonic-gate 	arg.key.name = name;
1427c478bd9Sstevel@tonic-gate 	(void) nss_search(&db_root, _nss_initf_passwd, NSS_DBOP_PASSWD_BYNAME,
1437257d1b4Sraf 	    &arg);
1447c478bd9Sstevel@tonic-gate 	return ((struct passwd *)NSS_XbyY_FINI(&arg));
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * POSIX.1c standard version of the function getpwnam_r.
1497c478bd9Sstevel@tonic-gate  * User gets it via static getpwnam_r from the header file.
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate int
__posix_getpwnam_r(const char * name,struct passwd * pwd,char * buffer,size_t bufsize,struct passwd ** result)1527c478bd9Sstevel@tonic-gate __posix_getpwnam_r(const char *name, struct passwd *pwd, char *buffer,
1537c478bd9Sstevel@tonic-gate     size_t bufsize, struct passwd **result)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	int nerrno = 0;
1567c478bd9Sstevel@tonic-gate 	int oerrno = errno;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	errno = 0;
1597257d1b4Sraf 	if ((*result = getpwnam_r(name, pwd, buffer, (uintptr_t)bufsize))
1607257d1b4Sraf 	    == NULL) {
1617c478bd9Sstevel@tonic-gate 			nerrno = errno;
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 	errno = oerrno;
1647c478bd9Sstevel@tonic-gate 	return (nerrno);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate void
setpwent(void)1687c478bd9Sstevel@tonic-gate setpwent(void)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	nss_setent(&db_root, _nss_initf_passwd, &context);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate void
endpwent(void)1747c478bd9Sstevel@tonic-gate endpwent(void)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	nss_endent(&db_root, _nss_initf_passwd, &context);
1777c478bd9Sstevel@tonic-gate 	nss_delete(&db_root);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate struct passwd *
getpwent_r(struct passwd * result,char * buffer,int buflen)1817c478bd9Sstevel@tonic-gate getpwent_r(struct passwd *result, char *buffer, int buflen)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
1847c478bd9Sstevel@tonic-gate 	char		*nam;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	/* In getXXent_r(), protect the unsuspecting caller from +/- entries */
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	do {
1897c478bd9Sstevel@tonic-gate 		NSS_XbyY_INIT(&arg, result, buffer, buflen, str2passwd);
1907c478bd9Sstevel@tonic-gate 		/* No key to fill in */
1917c478bd9Sstevel@tonic-gate 		(void) nss_getent(&db_root, _nss_initf_passwd, &context, &arg);
1927c478bd9Sstevel@tonic-gate 	} while (arg.returnval != 0 &&
1937c478bd9Sstevel@tonic-gate 	    (nam = ((struct passwd *)arg.returnval)->pw_name) != 0 &&
1947257d1b4Sraf 	    (*nam == '+' || *nam == '-'));
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	return ((struct passwd *)NSS_XbyY_FINI(&arg));
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate struct passwd *
fgetpwent_r(FILE * f,struct passwd * result,char * buffer,int buflen)2007c478bd9Sstevel@tonic-gate fgetpwent_r(FILE *f, struct passwd *result, char *buffer, int buflen)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	extern void	_nss_XbyY_fgets(FILE *, nss_XbyY_args_t *);
2037c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	arg;
2047c478bd9Sstevel@tonic-gate 
205*48bbca81SDaniel Hoffman 	/* ... but in fgetXXent_r, the caller deserves any +/- entry it gets */
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	/* No key to fill in */
2087c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2passwd);
2097c478bd9Sstevel@tonic-gate 	_nss_XbyY_fgets(f, &arg);
2107c478bd9Sstevel@tonic-gate 	return ((struct passwd *)NSS_XbyY_FINI(&arg));
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate static char *
gettok(char ** nextpp)2147c478bd9Sstevel@tonic-gate gettok(char **nextpp)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate 	char	*p = *nextpp;
2177c478bd9Sstevel@tonic-gate 	char	*q = p;
2187c478bd9Sstevel@tonic-gate 	char	c;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (p == 0)
2217c478bd9Sstevel@tonic-gate 		return (0);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	while ((c = *q) != '\0' && c != ':')
2247c478bd9Sstevel@tonic-gate 		q++;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if (c == '\0')
2277c478bd9Sstevel@tonic-gate 		*nextpp = 0;
2287c478bd9Sstevel@tonic-gate 	else {
2297c478bd9Sstevel@tonic-gate 		*q++ = '\0';
2307c478bd9Sstevel@tonic-gate 		*nextpp = q;
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 	return (p);
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate /*
2367c478bd9Sstevel@tonic-gate  * Return values: 0 = success, 1 = parse error, 2 = erange ...
2377c478bd9Sstevel@tonic-gate  * The structure pointer passed in is a structure in the caller's space
2387c478bd9Sstevel@tonic-gate  * wherein the field pointers would be set to areas in the buffer if
2397c478bd9Sstevel@tonic-gate  * need be. instring and buffer should be separate areas.
2407c478bd9Sstevel@tonic-gate  */
2417c478bd9Sstevel@tonic-gate int
str2passwd(const char * instr,int lenstr,void * ent,char * buffer,int buflen)2427c478bd9Sstevel@tonic-gate str2passwd(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	struct passwd	*passwd	= (struct passwd *)ent;
2457c478bd9Sstevel@tonic-gate 	char		*p, *next;
2467c478bd9Sstevel@tonic-gate 	int		black_magic;	/* "+" or "-" entry */
2472b4a7802SBaban Kenkre 	ulong_t		tmp;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	if (lenstr + 1 > buflen)
2507c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_ERANGE);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * We copy the input string into the output buffer and
2547c478bd9Sstevel@tonic-gate 	 * operate on it in place.
2557c478bd9Sstevel@tonic-gate 	 */
256cb5caa98Sdjl 	if (instr != buffer) {
257cb5caa98Sdjl 		/* Overlapping buffer copies are OK */
258cb5caa98Sdjl 		(void) memmove(buffer, instr, lenstr);
259cb5caa98Sdjl 		buffer[lenstr] = '\0';
260cb5caa98Sdjl 	}
261cb5caa98Sdjl 
262cb5caa98Sdjl 	/* quick exit do not entry fill if not needed */
263cb5caa98Sdjl 	if (ent == (void *)NULL)
264cb5caa98Sdjl 		return (NSS_STR_PARSE_SUCCESS);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	next = buffer;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	passwd->pw_name = p = gettok(&next);		/* username */
2697c478bd9Sstevel@tonic-gate 	if (*p == '\0') {
2707c478bd9Sstevel@tonic-gate 		/* Empty username;  not allowed */
2717c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 	black_magic = (*p == '+' || *p == '-');
2747c478bd9Sstevel@tonic-gate 	if (black_magic) {
2757c478bd9Sstevel@tonic-gate 		passwd->pw_uid = UID_NOBODY;
2767c478bd9Sstevel@tonic-gate 		passwd->pw_gid = GID_NOBODY;
2777c478bd9Sstevel@tonic-gate 		/*
2787c478bd9Sstevel@tonic-gate 		 *  pwconv tests pw_passwd and pw_age == NULL
2797c478bd9Sstevel@tonic-gate 		 */
2807c478bd9Sstevel@tonic-gate 		passwd->pw_passwd  = "";
2817c478bd9Sstevel@tonic-gate 		passwd->pw_age	= "";
2827c478bd9Sstevel@tonic-gate 		/*
2837c478bd9Sstevel@tonic-gate 		 * the rest of the passwd entry is "optional"
2847c478bd9Sstevel@tonic-gate 		 */
2857c478bd9Sstevel@tonic-gate 		passwd->pw_comment = "";
2867c478bd9Sstevel@tonic-gate 		passwd->pw_gecos = "";
2877c478bd9Sstevel@tonic-gate 		passwd->pw_dir	= "";
2887c478bd9Sstevel@tonic-gate 		passwd->pw_shell = "";
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	passwd->pw_passwd = p = gettok(&next);		/* password */
2927c478bd9Sstevel@tonic-gate 	if (p == 0) {
2937c478bd9Sstevel@tonic-gate 		if (black_magic)
2947c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
2957c478bd9Sstevel@tonic-gate 		else
2967c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 	for (; *p != '\0';  p++) {			/* age */
2997c478bd9Sstevel@tonic-gate 		if (*p == ',') {
3007c478bd9Sstevel@tonic-gate 			*p++ = '\0';
3017c478bd9Sstevel@tonic-gate 			break;
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 	passwd->pw_age = p;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	p = next;					/* uid */
3077c478bd9Sstevel@tonic-gate 	if (p == 0 || *p == '\0') {
3087c478bd9Sstevel@tonic-gate 		if (black_magic)
3097c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
3107c478bd9Sstevel@tonic-gate 		else
3117c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 	if (!black_magic) {
3142b4a7802SBaban Kenkre 		/*
3152b4a7802SBaban Kenkre 		 * strtoul returns unsigned long which is
3162b4a7802SBaban Kenkre 		 * 8 bytes on a 64-bit system. We don't want
3172b4a7802SBaban Kenkre 		 * to assign it directly to passwd->pw_uid
3182b4a7802SBaban Kenkre 		 * which is 4 bytes or else we will end up
3192b4a7802SBaban Kenkre 		 * truncating the value.
3202b4a7802SBaban Kenkre 		 */
32162272d53SPradhap Devarajan 		errno = 0;
3222b4a7802SBaban Kenkre 		tmp = strtoul(p, &next, 10);
32362272d53SPradhap Devarajan 		if (next == p || errno != 0) {
3247c478bd9Sstevel@tonic-gate 			/* uid field should be nonempty */
32562272d53SPradhap Devarajan 			/* also check errno from strtoul */
3267c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 		/*
3297c478bd9Sstevel@tonic-gate 		 * The old code (in 2.0 through 2.5) would check
3307c478bd9Sstevel@tonic-gate 		 * for the uid being negative, or being greater
3317c478bd9Sstevel@tonic-gate 		 * than 60001 (the rfs limit).  If it met either of
3327c478bd9Sstevel@tonic-gate 		 * these conditions, the uid was translated to 60001.
3337c478bd9Sstevel@tonic-gate 		 *
3342b4a7802SBaban Kenkre 		 * Now we just check for -1 (UINT32_MAX); anything else
3357c478bd9Sstevel@tonic-gate 		 * is administrative policy
3367c478bd9Sstevel@tonic-gate 		 */
3372b4a7802SBaban Kenkre 		if (tmp >= UINT32_MAX)
3387c478bd9Sstevel@tonic-gate 			passwd->pw_uid = UID_NOBODY;
3392b4a7802SBaban Kenkre 		else
3402b4a7802SBaban Kenkre 			passwd->pw_uid = (uid_t)tmp;
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 	if (*next++ != ':') {
3437c478bd9Sstevel@tonic-gate 		if (black_magic)
3447c478bd9Sstevel@tonic-gate 			(void) gettok(&next);
3457c478bd9Sstevel@tonic-gate 		else
3467c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 	p = next;					/* gid */
3497c478bd9Sstevel@tonic-gate 	if (p == 0 || *p == '\0') {
3507c478bd9Sstevel@tonic-gate 		if (black_magic)
3517c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
3527c478bd9Sstevel@tonic-gate 		else
3537c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 	if (!black_magic) {
35662272d53SPradhap Devarajan 		errno = 0;
3572b4a7802SBaban Kenkre 		tmp = strtoul(p, &next, 10);
35862272d53SPradhap Devarajan 		if (next == p || errno != 0) {
3597c478bd9Sstevel@tonic-gate 			/* gid field should be nonempty */
36062272d53SPradhap Devarajan 			/* also check errno from strtoul */
3617c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 		/*
3642b4a7802SBaban Kenkre 		 * gid should not be -1; anything else
3657c478bd9Sstevel@tonic-gate 		 * is administrative policy.
3667c478bd9Sstevel@tonic-gate 		 */
36762272d53SPradhap Devarajan 		if (tmp >= UINT32_MAX)
3687c478bd9Sstevel@tonic-gate 			passwd->pw_gid = GID_NOBODY;
3692b4a7802SBaban Kenkre 		else
3702b4a7802SBaban Kenkre 			passwd->pw_gid = (gid_t)tmp;
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	if (*next++ != ':') {
3737c478bd9Sstevel@tonic-gate 		if (black_magic)
3747c478bd9Sstevel@tonic-gate 			(void) gettok(&next);
3757c478bd9Sstevel@tonic-gate 		else
3767c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	passwd->pw_gecos = passwd->pw_comment = p = gettok(&next);
3807c478bd9Sstevel@tonic-gate 	if (p == 0) {
3817c478bd9Sstevel@tonic-gate 		if (black_magic)
3827c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
3837c478bd9Sstevel@tonic-gate 		else
3847c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	passwd->pw_dir = p = gettok(&next);
3887c478bd9Sstevel@tonic-gate 	if (p == 0) {
3897c478bd9Sstevel@tonic-gate 		if (black_magic)
3907c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
3917c478bd9Sstevel@tonic-gate 		else
3927c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	passwd->pw_shell = p = gettok(&next);
3967c478bd9Sstevel@tonic-gate 	if (p == 0) {
3977c478bd9Sstevel@tonic-gate 		if (black_magic)
3987c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
3997c478bd9Sstevel@tonic-gate 		else
4007c478bd9Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	/* Better not be any more fields... */
4047c478bd9Sstevel@tonic-gate 	if (next == 0) {
4057c478bd9Sstevel@tonic-gate 		/* Successfully parsed and stored */
4067c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_SUCCESS);
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 	return (NSS_STR_PARSE_PARSE);
4097c478bd9Sstevel@tonic-gate }
410