xref: /titanic_51/usr/src/lib/nsswitch/compat/common/getpwent.c (revision 36e852a172cba914383d7341c988128b2c667fbd)
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  */
217c478bd9Sstevel@tonic-gate /*
22*36e852a1SRaja Andra  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23cb5caa98Sdjl  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
25cb5caa98Sdjl  *	getpwent.c
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * lib/nsswitch/compat/getpwent.c -- name-service-switch backend for getpwnam()
287c478bd9Sstevel@tonic-gate  *   et al that does 4.x compatibility.  It looks in /etc/passwd; if it finds
297c478bd9Sstevel@tonic-gate  *   passwd entries there that begin with "+" or "-", it consults other
307c478bd9Sstevel@tonic-gate  *   services.  By default it uses NIS (YP), but the user can override this
317c478bd9Sstevel@tonic-gate  *   with a "passwd_compat" entry in /etc/nsswitch.conf, e.g.
32*36e852a1SRaja Andra  *			passwd_compat: ldap
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * This code tries to produce the same results as the 4.x code, even when
357c478bd9Sstevel@tonic-gate  *   the latter seems ill thought-out (mostly in the handling of netgroups,
367c478bd9Sstevel@tonic-gate  *   "-", and the combination thereof).  Bug-compatible, in other words.
377c478bd9Sstevel@tonic-gate  *   Though we do try to be more reasonable about the format of "+" and "-"
387c478bd9Sstevel@tonic-gate  *   entries here, i.e. you don't have to pad them with spurious colons and
397c478bd9Sstevel@tonic-gate  *   bogus uid/gid values.
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  * Caveats:
427c478bd9Sstevel@tonic-gate  *    -	More than one source may be specified, with the usual switch semantics,
437c478bd9Sstevel@tonic-gate  *	but having multiple sources here is definitely odd.
447c478bd9Sstevel@tonic-gate  *    -	People who recursively specify "compat" deserve what they get.
457c478bd9Sstevel@tonic-gate  *    -	Entries that begin with "+@" or "-@" are interpreted using
467c478bd9Sstevel@tonic-gate  *	getnetgrent() and innetgr(), which use the "netgroup" entry in
477c478bd9Sstevel@tonic-gate  *	/etc/nsswitch.conf.  If the sources for "passwd_compat" and "netgroup"
487c478bd9Sstevel@tonic-gate  *	differ, everything should work fine, but the semantics will be pretty
497c478bd9Sstevel@tonic-gate  *	confusing.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include <pwd.h>
537c478bd9Sstevel@tonic-gate #include <shadow.h>		/* For PASSWD (pathname to passwd file) */
547c478bd9Sstevel@tonic-gate #include <stdlib.h>
557c478bd9Sstevel@tonic-gate #include <strings.h>
567c478bd9Sstevel@tonic-gate #include "compat_common.h"
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
597c478bd9Sstevel@tonic-gate 
60cb5caa98Sdjl static void
617c478bd9Sstevel@tonic-gate _nss_initf_passwd_compat(p)
627c478bd9Sstevel@tonic-gate 	nss_db_params_t	*p;
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	p->name		  = NSS_DBNAM_PASSWD;
657c478bd9Sstevel@tonic-gate 	p->config_name	  = NSS_DBNAM_PASSWD_COMPAT;
667c478bd9Sstevel@tonic-gate 	p->default_config = NSS_DEFCONF_PASSWD_COMPAT;
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate 
692b4a7802SBaban Kenkre /*
702b4a7802SBaban Kenkre  * Validates passwd entry replacing uid/gid > MAXUID by ID_NOBODY.
712b4a7802SBaban Kenkre  */
722b4a7802SBaban Kenkre int
732b4a7802SBaban Kenkre validate_passwd_ids(char *line, int *linelenp, int buflen, int extra_chars)
742b4a7802SBaban Kenkre {
752b4a7802SBaban Kenkre 	char	*linep, *limit, *uidp, *gidp;
762b4a7802SBaban Kenkre 	uid_t	uid;
772b4a7802SBaban Kenkre 	gid_t	gid;
782b4a7802SBaban Kenkre 	ulong_t	uidl, gidl;
792b4a7802SBaban Kenkre 	int	olduidlen, oldgidlen, idlen;
802b4a7802SBaban Kenkre 	int	linelen = *linelenp, newlinelen;
812b4a7802SBaban Kenkre 
822b4a7802SBaban Kenkre 	if (linelen == 0 || *line == '+' || *line == '-')
832b4a7802SBaban Kenkre 		return (NSS_STR_PARSE_SUCCESS);
842b4a7802SBaban Kenkre 
852b4a7802SBaban Kenkre 	linep = line;
862b4a7802SBaban Kenkre 	limit = line + linelen;
872b4a7802SBaban Kenkre 
882b4a7802SBaban Kenkre 	while (linep < limit && *linep++ != ':') /* skip username */
892b4a7802SBaban Kenkre 		continue;
902b4a7802SBaban Kenkre 	while (linep < limit && *linep++ != ':') /* skip password */
912b4a7802SBaban Kenkre 		continue;
922b4a7802SBaban Kenkre 	if (linep == limit)
932b4a7802SBaban Kenkre 		return (NSS_STR_PARSE_PARSE);
942b4a7802SBaban Kenkre 
952b4a7802SBaban Kenkre 	uidp = linep;
962b4a7802SBaban Kenkre 	uidl = strtoul(uidp, (char **)&linep, 10); /* grab uid */
972b4a7802SBaban Kenkre 	olduidlen = linep - uidp;
982b4a7802SBaban Kenkre 	if (++linep >= limit || olduidlen == 0)
992b4a7802SBaban Kenkre 		return (NSS_STR_PARSE_PARSE);
1002b4a7802SBaban Kenkre 
1012b4a7802SBaban Kenkre 	gidp = linep;
1022b4a7802SBaban Kenkre 	gidl = strtoul(gidp, (char **)&linep, 10); /* grab gid */
1032b4a7802SBaban Kenkre 	oldgidlen = linep - gidp;
1042b4a7802SBaban Kenkre 	if (linep >= limit || oldgidlen == 0)
1052b4a7802SBaban Kenkre 		return (NSS_STR_PARSE_PARSE);
1062b4a7802SBaban Kenkre 
1072b4a7802SBaban Kenkre 	if (uidl <= MAXUID && gidl <= MAXUID)
1082b4a7802SBaban Kenkre 		return (NSS_STR_PARSE_SUCCESS);
1092b4a7802SBaban Kenkre 	uid = (uidl > MAXUID) ? UID_NOBODY : (uid_t)uidl;
1102b4a7802SBaban Kenkre 	gid = (gidl > MAXUID) ? GID_NOBODY : (gid_t)gidl;
1112b4a7802SBaban Kenkre 
1122b4a7802SBaban Kenkre 	/* Check if we have enough space in the buffer */
1132b4a7802SBaban Kenkre 	idlen = snprintf(NULL, 0, "%u:%u", uid, gid);
1142b4a7802SBaban Kenkre 	newlinelen = linelen + idlen - olduidlen - oldgidlen - 1;
1152b4a7802SBaban Kenkre 	if (newlinelen + extra_chars > buflen)
1162b4a7802SBaban Kenkre 		return (NSS_STR_PARSE_ERANGE);
1172b4a7802SBaban Kenkre 
1182b4a7802SBaban Kenkre 	/* Replace ephemeral ids by ID_NOBODY */
1192b4a7802SBaban Kenkre 	(void) bcopy(linep, uidp + idlen, limit - linep + extra_chars);
1202b4a7802SBaban Kenkre 	(void) snprintf(uidp, idlen + 1, "%u:%u", uid, gid);
1212b4a7802SBaban Kenkre 	*(uidp + idlen) = ':'; /* restore : that was overwritten by snprintf */
1222b4a7802SBaban Kenkre 	*linelenp = newlinelen;
1232b4a7802SBaban Kenkre 	return (NSS_STR_PARSE_SUCCESS);
1242b4a7802SBaban Kenkre }
1252b4a7802SBaban Kenkre 
1267c478bd9Sstevel@tonic-gate static const char *
1277c478bd9Sstevel@tonic-gate get_pwname(argp)
1287c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*argp;
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	struct passwd		*p = (struct passwd *)argp->returnval;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	return (p->pw_name);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static int
1367c478bd9Sstevel@tonic-gate check_pwname(argp)
1377c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*argp;
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	struct passwd		*p = (struct passwd *)argp->returnval;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	return (strcmp(p->pw_name, argp->key.name) == 0);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static nss_status_t
1457c478bd9Sstevel@tonic-gate getbyname(be, a)
1467c478bd9Sstevel@tonic-gate 	compat_backend_ptr_t	be;
1477c478bd9Sstevel@tonic-gate 	void			*a;
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	return (_nss_compat_XY_all(be, argp,
1527c478bd9Sstevel@tonic-gate 				check_pwname, NSS_DBOP_PASSWD_BYNAME));
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate static int
1567c478bd9Sstevel@tonic-gate check_pwuid(argp)
1577c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*argp;
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	struct passwd		*p = (struct passwd *)argp->returnval;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	return (p->pw_uid == argp->key.uid);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate static nss_status_t
1657c478bd9Sstevel@tonic-gate getbyuid(be, a)
1667c478bd9Sstevel@tonic-gate 	compat_backend_ptr_t	be;
1677c478bd9Sstevel@tonic-gate 	void			*a;
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
1707c478bd9Sstevel@tonic-gate 
1712b4a7802SBaban Kenkre 	if (argp->key.uid > MAXUID)
1722b4a7802SBaban Kenkre 		return (NSS_NOTFOUND);
1737c478bd9Sstevel@tonic-gate 	return (_nss_compat_XY_all(be, argp,
1747c478bd9Sstevel@tonic-gate 				check_pwuid, NSS_DBOP_PASSWD_BYUID));
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1787c478bd9Sstevel@tonic-gate static int
1797c478bd9Sstevel@tonic-gate merge_pwents(be, argp, fields)
1807c478bd9Sstevel@tonic-gate 	compat_backend_ptr_t	be;
1817c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*argp;
1827c478bd9Sstevel@tonic-gate 	const char		**fields;
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	struct passwd		*pw	= (struct passwd *)argp->buf.result;
1857c478bd9Sstevel@tonic-gate 	char			*buf	= malloc(NSS_LINELEN_PASSWD);
1867c478bd9Sstevel@tonic-gate 	char			*s;
1877c478bd9Sstevel@tonic-gate 	int			parsestat;
188cb5caa98Sdjl 	int			len;
189cb5caa98Sdjl 	int			buflen;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (buf == 0) {
1927c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
1937c478bd9Sstevel@tonic-gate 		/* Really "out of memory", but PARSE_PARSE will have to do */
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 	/*
1967c478bd9Sstevel@tonic-gate 	 * Don't allow overriding of
1977c478bd9Sstevel@tonic-gate 	 *	- username
1987c478bd9Sstevel@tonic-gate 	 *	- uid
1997c478bd9Sstevel@tonic-gate 	 *	- gid
2007c478bd9Sstevel@tonic-gate 	 * That's what the SunOS 4.x code did;  who are we to question it...
2017c478bd9Sstevel@tonic-gate 	 */
2027c478bd9Sstevel@tonic-gate 	s = buf;
203cb5caa98Sdjl 	buflen = argp->buf.buflen;
204cb5caa98Sdjl 
205cb5caa98Sdjl 	if (fields[1] != 0)
206cb5caa98Sdjl 		len = snprintf(s, buflen, "%s:%s",
207cb5caa98Sdjl 				pw->pw_name, fields[1]);
208cb5caa98Sdjl 	else {
2097c478bd9Sstevel@tonic-gate /* ====> Does this do the right thing? */
210cb5caa98Sdjl 		if (pw->pw_age != 0 && *pw->pw_age != '\0')
211cb5caa98Sdjl 			len = snprintf(s, buflen, "%s:%s,%s",
212cb5caa98Sdjl 				pw->pw_name, pw->pw_passwd, pw->pw_age);
213cb5caa98Sdjl 		else
214cb5caa98Sdjl 			len = snprintf(s, buflen, "%s:%s",
215cb5caa98Sdjl 				pw->pw_name, pw->pw_passwd);
2167c478bd9Sstevel@tonic-gate 	}
217cb5caa98Sdjl 
218cb5caa98Sdjl 	if (len > buflen)
219cb5caa98Sdjl 		return (NSS_STR_PARSE_ERANGE);
220cb5caa98Sdjl 
221cb5caa98Sdjl 	s += len;
222cb5caa98Sdjl 	buflen -= len;
2232b4a7802SBaban Kenkre 	len = snprintf(s, buflen, ":%u:%u:%s:%s:%s",
2247c478bd9Sstevel@tonic-gate 		pw->pw_uid,
2257c478bd9Sstevel@tonic-gate 		pw->pw_gid,
2267c478bd9Sstevel@tonic-gate 		fields[4] != 0 ? fields[4] : pw->pw_gecos,
2277c478bd9Sstevel@tonic-gate 		fields[5] != 0 ? fields[5] : pw->pw_dir,
2287c478bd9Sstevel@tonic-gate 		fields[6] != 0 ? fields[6] : pw->pw_shell);
229cb5caa98Sdjl 
230cb5caa98Sdjl 	if (len > buflen)
231cb5caa98Sdjl 		return (NSS_STR_PARSE_ERANGE);
232cb5caa98Sdjl 
233cb5caa98Sdjl 	s += len;
234cb5caa98Sdjl 	len = s - buf;
235cb5caa98Sdjl 
236cb5caa98Sdjl 	/*
237cb5caa98Sdjl 	 * if asked, return the data in /etc file format
238cb5caa98Sdjl 	 */
239cb5caa98Sdjl 	if (be->return_string_data == 1) {
240cb5caa98Sdjl 		/* reset the result ptr to the original value */
241cb5caa98Sdjl 		argp->buf.result = NULL;
242cb5caa98Sdjl 
243cb5caa98Sdjl 		if (len > argp->buf.buflen) {
244cb5caa98Sdjl 			parsestat = NSS_STR_PARSE_ERANGE;
245cb5caa98Sdjl 		} else {
246cb5caa98Sdjl 			(void) strncpy(argp->buf.buffer, buf, len);
247cb5caa98Sdjl 			argp->returnval = argp->buf.buffer;
248cb5caa98Sdjl 			argp->returnlen = len;
249cb5caa98Sdjl 			parsestat = NSS_SUCCESS;
250cb5caa98Sdjl 		}
251cb5caa98Sdjl 	} else {
252cb5caa98Sdjl 		parsestat = (*argp->str2ent)(buf, len,
2537c478bd9Sstevel@tonic-gate 				    argp->buf.result,
2547c478bd9Sstevel@tonic-gate 				    argp->buf.buffer,
2557c478bd9Sstevel@tonic-gate 				    argp->buf.buflen);
256cb5caa98Sdjl 	}
2577c478bd9Sstevel@tonic-gate 	free(buf);
2587c478bd9Sstevel@tonic-gate 	return (parsestat);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate static compat_backend_op_t passwd_ops[] = {
2627c478bd9Sstevel@tonic-gate 	_nss_compat_destr,
2637c478bd9Sstevel@tonic-gate 	_nss_compat_endent,
2647c478bd9Sstevel@tonic-gate 	_nss_compat_setent,
2657c478bd9Sstevel@tonic-gate 	_nss_compat_getent,
2667c478bd9Sstevel@tonic-gate 	getbyname,
2677c478bd9Sstevel@tonic-gate 	getbyuid
2687c478bd9Sstevel@tonic-gate };
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2717c478bd9Sstevel@tonic-gate nss_backend_t *
2727c478bd9Sstevel@tonic-gate _nss_compat_passwd_constr(dummy1, dummy2, dummy3)
2737c478bd9Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	return (_nss_compat_constr(passwd_ops,
2767c478bd9Sstevel@tonic-gate 				sizeof (passwd_ops) / sizeof (passwd_ops[0]),
2777c478bd9Sstevel@tonic-gate 				PASSWD,
2787c478bd9Sstevel@tonic-gate 				NSS_LINELEN_PASSWD,
2797c478bd9Sstevel@tonic-gate 				&db_root,
2807c478bd9Sstevel@tonic-gate 				_nss_initf_passwd_compat,
2817c478bd9Sstevel@tonic-gate 				1,
2827c478bd9Sstevel@tonic-gate 				get_pwname,
2837c478bd9Sstevel@tonic-gate 				merge_pwents));
2847c478bd9Sstevel@tonic-gate }
285