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