xref: /titanic_51/usr/src/lib/nsswitch/files/common/getgrent.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
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/getgrent.c -- "files" backend for nsswitch "group" database
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <grp.h>
32 #include <unistd.h>		/* for GF_PATH */
33 #include "files_common.h"
34 #include <strings.h>
35 
36 static u_int
37 hash_grname(nss_XbyY_args_t *argp, int keyhash)
38 {
39 	struct group *g = argp->returnval;
40 	const char *name = keyhash ? argp->key.name : g->gr_name;
41 	u_int hash = 0;
42 
43 	while (*name != 0)
44 		hash = hash * 15 + *name++;
45 
46 	return (hash);
47 }
48 
49 static u_int
50 hash_grgid(nss_XbyY_args_t *argp, int keyhash)
51 {
52 	struct group *g = argp->returnval;
53 	return (keyhash ? (u_int)argp->key.gid : (u_int)g->gr_gid);
54 }
55 
56 static files_hash_func hash_gr[2] = { hash_grname, hash_grgid };
57 
58 static files_hash_t hashinfo = {
59 	DEFAULTMUTEX,
60 	sizeof (struct group),
61 	NSS_BUFLEN_GROUP,
62 	2,
63 	hash_gr
64 };
65 
66 static int
67 check_grname(argp)
68 	nss_XbyY_args_t		*argp;
69 {
70 	struct group		*g = (struct group *)argp->returnval;
71 
72 	/* +/- entries only valid in compat source */
73 	if (g->gr_name != 0 && (g->gr_name[0] == '+' || g->gr_name[0] == '-'))
74 		return (0);
75 	return (strcmp(g->gr_name, argp->key.name) == 0);
76 }
77 
78 static nss_status_t
79 getbyname(be, a)
80 	files_backend_ptr_t	be;
81 	void			*a;
82 {
83 	return (_nss_files_XY_hash(be, a, 0, &hashinfo, 0, check_grname));
84 }
85 
86 static int
87 check_grgid(argp)
88 	nss_XbyY_args_t		*argp;
89 {
90 	struct group		*g = (struct group *)argp->returnval;
91 
92 	/* +/- entries only valid in compat source */
93 	if (g->gr_name != 0 && (g->gr_name[0] == '+' || g->gr_name[0] == '-'))
94 		return (0);
95 	return (g->gr_gid == argp->key.gid);
96 }
97 
98 static nss_status_t
99 getbygid(be, a)
100 	files_backend_ptr_t	be;
101 	void			*a;
102 {
103 	return (_nss_files_XY_hash(be, a, 0, &hashinfo, 1, check_grgid));
104 }
105 
106 static nss_status_t
107 getbymember(be, a)
108 	files_backend_ptr_t	be;
109 	void			*a;
110 {
111 	struct nss_groupsbymem	*argp = (struct nss_groupsbymem *) a;
112 
113 	return (_nss_files_do_all(be, argp, argp->username,
114 				(files_do_all_func_t)argp->process_cstr));
115 }
116 
117 static files_backend_op_t group_ops[] = {
118 	_nss_files_destr,
119 	_nss_files_endent,
120 	_nss_files_setent,
121 	_nss_files_getent_rigid,
122 	getbyname,
123 	getbygid,
124 	getbymember
125 };
126 
127 /*ARGSUSED*/
128 nss_backend_t *
129 _nss_files_group_constr(dummy1, dummy2, dummy3)
130 	const char	*dummy1, *dummy2, *dummy3;
131 {
132 	return (_nss_files_constr(group_ops,
133 				sizeof (group_ops) / sizeof (group_ops[0]),
134 				GF_PATH,
135 				NSS_LINELEN_GROUP,
136 				&hashinfo));
137 }
138