xref: /titanic_44/usr/src/lib/nsswitch/files/common/getpwnam.c (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *	Copyright (c) 1988-1995 Sun Microsystems Inc
24  *	All Rights Reserved.
25  *
26  *	files/getpwnam.c -- "files" backend for nsswitch "passwd" database
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <pwd.h>
32 #include <shadow.h>
33 #include <unistd.h>		/* for PF_PATH */
34 #include "files_common.h"
35 #include <strings.h>
36 
37 static u_int
38 hash_pwname(nss_XbyY_args_t *argp, int keyhash)
39 {
40 	struct passwd *p = argp->returnval;
41 	const char *name = keyhash ? argp->key.name : p->pw_name;
42 	u_int hash = 0;
43 
44 	while (*name != 0)
45 		hash = hash * 15 + *name++;
46 
47 	return (hash);
48 }
49 
50 static u_int
51 hash_pwuid(nss_XbyY_args_t *argp, int keyhash)
52 {
53 	struct passwd *p = argp->returnval;
54 	return (keyhash ? (u_int)argp->key.uid : (u_int)p->pw_uid);
55 }
56 
57 static files_hash_func hash_pw[2] = { hash_pwname, hash_pwuid };
58 
59 static files_hash_t hashinfo = {
60 	DEFAULTMUTEX,
61 	sizeof (struct passwd),
62 	NSS_BUFLEN_PASSWD,
63 	2,
64 	hash_pw
65 };
66 
67 static int
68 check_pwname(argp)
69 	nss_XbyY_args_t		*argp;
70 {
71 	struct passwd		*p = (struct passwd *)argp->returnval;
72 
73 	/* +/- entries valid for compat source only */
74 	if (p->pw_name != 0 && (p->pw_name[0] == '+' || p->pw_name[0] == '-'))
75 		return (0);
76 	return (strcmp(p->pw_name, argp->key.name) == 0);
77 }
78 
79 static nss_status_t
80 getbyname(be, a)
81 	files_backend_ptr_t	be;
82 	void			*a;
83 {
84 	return (_nss_files_XY_hash(be, a, 0, &hashinfo, 0, check_pwname));
85 }
86 
87 static int
88 check_pwuid(argp)
89 	nss_XbyY_args_t		*argp;
90 {
91 	struct passwd		*p = (struct passwd *)argp->returnval;
92 
93 	/* +/- entries valid for compat source only */
94 	if (p->pw_name != 0 && (p->pw_name[0] == '+' || p->pw_name[0] == '-'))
95 		return (0);
96 	return (p->pw_uid == argp->key.uid);
97 }
98 
99 static nss_status_t
100 getbyuid(be, a)
101 	files_backend_ptr_t	be;
102 	void			*a;
103 {
104 	return (_nss_files_XY_hash(be, a, 0, &hashinfo, 1, check_pwuid));
105 }
106 
107 static files_backend_op_t passwd_ops[] = {
108 	_nss_files_destr,
109 	_nss_files_endent,
110 	_nss_files_setent,
111 	_nss_files_getent_rigid,
112 	getbyname,
113 	getbyuid
114 };
115 
116 /*ARGSUSED*/
117 nss_backend_t *
118 _nss_files_passwd_constr(dummy1, dummy2, dummy3)
119 	const char	*dummy1, *dummy2, *dummy3;
120 {
121 	return (_nss_files_constr(passwd_ops,
122 				sizeof (passwd_ops) / sizeof (passwd_ops[0]),
123 				PF_PATH,
124 				NSS_LINELEN_PASSWD,
125 				&hashinfo));
126 }
127