10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2830Sdjl  * Common Development and Distribution License (the "License").
6*2830Sdjl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2830Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*2830Sdjl  * Use is subject to license terms.
240Sstevel@tonic-gate  *
25*2830Sdjl  * files/getpwnam.c -- "files" backend for nsswitch "passwd" database
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <pwd.h>
310Sstevel@tonic-gate #include <shadow.h>
320Sstevel@tonic-gate #include <unistd.h>		/* for PF_PATH */
330Sstevel@tonic-gate #include "files_common.h"
340Sstevel@tonic-gate #include <strings.h>
35*2830Sdjl #include <stdlib.h>
360Sstevel@tonic-gate 
37*2830Sdjl static uint_t
38*2830Sdjl hash_pwname(nss_XbyY_args_t *argp, int keyhash, const char *line,
39*2830Sdjl 	int linelen)
400Sstevel@tonic-gate {
41*2830Sdjl 	const char	*name;
42*2830Sdjl 	int		namelen, i;
43*2830Sdjl 	uint_t 		hash = 0;
440Sstevel@tonic-gate 
45*2830Sdjl 	if (keyhash) {
46*2830Sdjl 		name = argp->key.name;
47*2830Sdjl 		namelen = strlen(name);
48*2830Sdjl 	} else {
49*2830Sdjl 		name = line;
50*2830Sdjl 		namelen = 0;
51*2830Sdjl 		while (linelen-- && *line++ != ':')
52*2830Sdjl 			namelen++;
53*2830Sdjl 	}
540Sstevel@tonic-gate 
55*2830Sdjl 	for (i = 0; i < namelen; i++)
56*2830Sdjl 		hash = hash * 15 + name[i];
570Sstevel@tonic-gate 	return (hash);
580Sstevel@tonic-gate }
590Sstevel@tonic-gate 
60*2830Sdjl static uint_t
61*2830Sdjl hash_pwuid(nss_XbyY_args_t *argp, int keyhash, const char *line,
62*2830Sdjl 	int linelen)
630Sstevel@tonic-gate {
64*2830Sdjl 	uint_t		id;
65*2830Sdjl 	const char	*linep, *limit, *end;
66*2830Sdjl 
67*2830Sdjl 	linep = line;
68*2830Sdjl 	limit = line + linelen;
69*2830Sdjl 
70*2830Sdjl 	if (keyhash)
71*2830Sdjl 		return ((uint_t)argp->key.uid);
72*2830Sdjl 
73*2830Sdjl 	/* skip username */
74*2830Sdjl 	while (linep < limit && *linep++ != ':');
75*2830Sdjl 	/* skip password */
76*2830Sdjl 	while (linep < limit && *linep++ != ':');
77*2830Sdjl 	if (linep == limit)
78*2830Sdjl 		return (UID_NOBODY);
79*2830Sdjl 
80*2830Sdjl 	/* uid */
81*2830Sdjl 	end = linep;
82*2830Sdjl 	id = (uint_t)strtol(linep, (char **)&end, 10);
83*2830Sdjl 
84*2830Sdjl 	/* empty uid */
85*2830Sdjl 	if (linep == end)
86*2830Sdjl 		return (UID_NOBODY);
87*2830Sdjl 
88*2830Sdjl 	return (id);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate static files_hash_func hash_pw[2] = { hash_pwname, hash_pwuid };
920Sstevel@tonic-gate 
930Sstevel@tonic-gate static files_hash_t hashinfo = {
940Sstevel@tonic-gate 	DEFAULTMUTEX,
950Sstevel@tonic-gate 	sizeof (struct passwd),
960Sstevel@tonic-gate 	NSS_BUFLEN_PASSWD,
970Sstevel@tonic-gate 	2,
980Sstevel@tonic-gate 	hash_pw
990Sstevel@tonic-gate };
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static int
102*2830Sdjl check_pwname(nss_XbyY_args_t *argp, const char *line, int linelen)
1030Sstevel@tonic-gate {
104*2830Sdjl 	const char	*linep, *limit;
105*2830Sdjl 	const char *keyp = argp->key.name;
106*2830Sdjl 
107*2830Sdjl 	linep = line;
108*2830Sdjl 	limit = line + linelen;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	/* +/- entries valid for compat source only */
111*2830Sdjl 	if (linelen == 0 || *line == '+' || *line == '-')
1120Sstevel@tonic-gate 		return (0);
113*2830Sdjl 	while (*keyp && linep < limit && *keyp == *linep) {
114*2830Sdjl 		keyp++;
115*2830Sdjl 		linep++;
116*2830Sdjl 	}
117*2830Sdjl 	return (linep < limit && *keyp == '\0' && *linep == ':');
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate static nss_status_t
1210Sstevel@tonic-gate getbyname(be, a)
1220Sstevel@tonic-gate 	files_backend_ptr_t	be;
1230Sstevel@tonic-gate 	void			*a;
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	return (_nss_files_XY_hash(be, a, 0, &hashinfo, 0, check_pwname));
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate static int
129*2830Sdjl check_pwuid(nss_XbyY_args_t *argp, const char *line, int linelen)
1300Sstevel@tonic-gate {
131*2830Sdjl 	const char	*linep, *limit, *end;
132*2830Sdjl 	uid_t		pw_uid;
133*2830Sdjl 
134*2830Sdjl 	linep = line;
135*2830Sdjl 	limit = line + linelen;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	/* +/- entries valid for compat source only */
138*2830Sdjl 	if (linelen == 0 || *line == '+' || *line == '-')
139*2830Sdjl 		return (0);
140*2830Sdjl 
141*2830Sdjl 	/* skip username */
142*2830Sdjl 	while (linep < limit && *linep++ != ':');
143*2830Sdjl 	/* skip password */
144*2830Sdjl 	while (linep < limit && *linep++ != ':');
145*2830Sdjl 	if (linep == limit)
1460Sstevel@tonic-gate 		return (0);
147*2830Sdjl 
148*2830Sdjl 	/* uid */
149*2830Sdjl 	end = linep;
150*2830Sdjl 	pw_uid = (uid_t)strtol(linep, (char **)&end, 10);
151*2830Sdjl 
152*2830Sdjl 	/* empty uid is not valid */
153*2830Sdjl 	if (linep == end)
154*2830Sdjl 		return (0);
155*2830Sdjl 
156*2830Sdjl 	return (pw_uid == argp->key.uid);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate static nss_status_t
1600Sstevel@tonic-gate getbyuid(be, a)
1610Sstevel@tonic-gate 	files_backend_ptr_t	be;
1620Sstevel@tonic-gate 	void			*a;
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	return (_nss_files_XY_hash(be, a, 0, &hashinfo, 1, check_pwuid));
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate static files_backend_op_t passwd_ops[] = {
1680Sstevel@tonic-gate 	_nss_files_destr,
1690Sstevel@tonic-gate 	_nss_files_endent,
1700Sstevel@tonic-gate 	_nss_files_setent,
1710Sstevel@tonic-gate 	_nss_files_getent_rigid,
1720Sstevel@tonic-gate 	getbyname,
1730Sstevel@tonic-gate 	getbyuid
1740Sstevel@tonic-gate };
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate /*ARGSUSED*/
1770Sstevel@tonic-gate nss_backend_t *
1780Sstevel@tonic-gate _nss_files_passwd_constr(dummy1, dummy2, dummy3)
1790Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate 	return (_nss_files_constr(passwd_ops,
1820Sstevel@tonic-gate 				sizeof (passwd_ops) / sizeof (passwd_ops[0]),
1830Sstevel@tonic-gate 				PF_PATH,
1840Sstevel@tonic-gate 				NSS_LINELEN_PASSWD,
1850Sstevel@tonic-gate 				&hashinfo));
1860Sstevel@tonic-gate }
187