xref: /illumos-gate/usr/src/lib/nsswitch/files/common/getgrent.c (revision cb5caa98562cf06753163f558cbcfe30b8f4673a)
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
5*cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6*cb5caa98Sdjl  * 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*cb5caa98Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*cb5caa98Sdjl  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * files/getgrent.c -- "files" backend for nsswitch "group" database
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <grp.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>		/* for GF_PATH */
32*cb5caa98Sdjl #include <stdlib.h>		/* for GF_PATH */
337c478bd9Sstevel@tonic-gate #include "files_common.h"
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate 
36*cb5caa98Sdjl static uint_t
37*cb5caa98Sdjl hash_grname(nss_XbyY_args_t *argp, int keyhash, const char *line,
38*cb5caa98Sdjl 	int linelen)
397c478bd9Sstevel@tonic-gate {
40*cb5caa98Sdjl 	const char 	*name;
41*cb5caa98Sdjl 	int		namelen, i;
42*cb5caa98Sdjl 	uint_t		hash = 0;
437c478bd9Sstevel@tonic-gate 
44*cb5caa98Sdjl 	if (keyhash) {
45*cb5caa98Sdjl 		name = argp->key.name;
46*cb5caa98Sdjl 		namelen = strlen(name);
47*cb5caa98Sdjl 	} else {
48*cb5caa98Sdjl 		name = line;
49*cb5caa98Sdjl 		namelen = 0;
50*cb5caa98Sdjl 		while (linelen-- && *line++ != ':')
51*cb5caa98Sdjl 			namelen++;
52*cb5caa98Sdjl 	}
537c478bd9Sstevel@tonic-gate 
54*cb5caa98Sdjl 	for (i = 0; i < namelen; i++)
55*cb5caa98Sdjl 		hash = hash * 15 + name[i];
567c478bd9Sstevel@tonic-gate 	return (hash);
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate 
59*cb5caa98Sdjl static uint_t
60*cb5caa98Sdjl hash_grgid(nss_XbyY_args_t *argp, int keyhash, const char *line,
61*cb5caa98Sdjl 	int linelen)
627c478bd9Sstevel@tonic-gate {
63*cb5caa98Sdjl 	uint_t		id;
64*cb5caa98Sdjl 	const char	*linep, *limit, *end;
65*cb5caa98Sdjl 
66*cb5caa98Sdjl 	linep = line;
67*cb5caa98Sdjl 	limit = line + linelen;
68*cb5caa98Sdjl 
69*cb5caa98Sdjl 	if (keyhash)
70*cb5caa98Sdjl 		return ((uint_t)argp->key.gid);
71*cb5caa98Sdjl 
72*cb5caa98Sdjl 	/* skip groupname */
73*cb5caa98Sdjl 	while (linep < limit && *linep++ != ':');
74*cb5caa98Sdjl 	/* skip password */
75*cb5caa98Sdjl 	while (linep < limit && *linep++ != ':');
76*cb5caa98Sdjl 	if (linep == limit)
77*cb5caa98Sdjl 		return (GID_NOBODY);
78*cb5caa98Sdjl 
79*cb5caa98Sdjl 	/* gid */
80*cb5caa98Sdjl 	end = linep;
81*cb5caa98Sdjl 	id = (uint_t)strtol(linep, (char **)&end, 10);
82*cb5caa98Sdjl 	/* empty gid */
83*cb5caa98Sdjl 	if (linep == end)
84*cb5caa98Sdjl 		return (GID_NOBODY);
85*cb5caa98Sdjl 
86*cb5caa98Sdjl 	return (id);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static files_hash_func hash_gr[2] = { hash_grname, hash_grgid };
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static files_hash_t hashinfo = {
927c478bd9Sstevel@tonic-gate 	DEFAULTMUTEX,
937c478bd9Sstevel@tonic-gate 	sizeof (struct group),
947c478bd9Sstevel@tonic-gate 	NSS_BUFLEN_GROUP,
957c478bd9Sstevel@tonic-gate 	2,
967c478bd9Sstevel@tonic-gate 	hash_gr
977c478bd9Sstevel@tonic-gate };
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static int
100*cb5caa98Sdjl check_grname(nss_XbyY_args_t *argp, const char *line, int linelen)
1017c478bd9Sstevel@tonic-gate {
102*cb5caa98Sdjl 	const char *linep, *limit;
103*cb5caa98Sdjl 	const char *keyp = argp->key.name;
1047c478bd9Sstevel@tonic-gate 
105*cb5caa98Sdjl 	linep = line;
106*cb5caa98Sdjl 	limit = line + linelen;
107*cb5caa98Sdjl 
108*cb5caa98Sdjl 	/* +/- entries valid for compat source only */
109*cb5caa98Sdjl 	if (linelen == 0 || *line == '+' || *line == '-')
1107c478bd9Sstevel@tonic-gate 		return (0);
111*cb5caa98Sdjl 	while (*keyp && linep < limit && *keyp == *linep) {
112*cb5caa98Sdjl 		keyp++;
113*cb5caa98Sdjl 		linep++;
114*cb5caa98Sdjl 	}
115*cb5caa98Sdjl 	return (linep < limit && *keyp == '\0' && *linep == ':');
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate static nss_status_t
1197c478bd9Sstevel@tonic-gate getbyname(be, a)
1207c478bd9Sstevel@tonic-gate 	files_backend_ptr_t	be;
1217c478bd9Sstevel@tonic-gate 	void			*a;
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	return (_nss_files_XY_hash(be, a, 0, &hashinfo, 0, check_grname));
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate static int
127*cb5caa98Sdjl check_grgid(nss_XbyY_args_t *argp, const char *line, int linelen)
1287c478bd9Sstevel@tonic-gate {
129*cb5caa98Sdjl 	const char	*linep, *limit, *end;
130*cb5caa98Sdjl 	gid_t		gr_gid;
1317c478bd9Sstevel@tonic-gate 
132*cb5caa98Sdjl 	linep = line;
133*cb5caa98Sdjl 	limit = line + linelen;
134*cb5caa98Sdjl 
135*cb5caa98Sdjl 	/* +/- entries valid for compat source only */
136*cb5caa98Sdjl 	if (linelen == 0 || *line == '+' || *line == '-')
1377c478bd9Sstevel@tonic-gate 		return (0);
138*cb5caa98Sdjl 
139*cb5caa98Sdjl 	/* skip username */
140*cb5caa98Sdjl 	while (linep < limit && *linep++ != ':');
141*cb5caa98Sdjl 	/* skip password */
142*cb5caa98Sdjl 	while (linep < limit && *linep++ != ':');
143*cb5caa98Sdjl 	if (linep == limit)
144*cb5caa98Sdjl 		return (0);
145*cb5caa98Sdjl 
146*cb5caa98Sdjl 	/* uid */
147*cb5caa98Sdjl 	end = linep;
148*cb5caa98Sdjl 	gr_gid = (gid_t)strtol(linep, (char **)&end, 10);
149*cb5caa98Sdjl 
150*cb5caa98Sdjl 	/* empty gid is not valid */
151*cb5caa98Sdjl 	if (linep == end)
152*cb5caa98Sdjl 		return (0);
153*cb5caa98Sdjl 
154*cb5caa98Sdjl 	return (gr_gid == argp->key.gid);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate static nss_status_t
1587c478bd9Sstevel@tonic-gate getbygid(be, a)
1597c478bd9Sstevel@tonic-gate 	files_backend_ptr_t	be;
1607c478bd9Sstevel@tonic-gate 	void			*a;
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	return (_nss_files_XY_hash(be, a, 0, &hashinfo, 1, check_grgid));
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate static nss_status_t
1667c478bd9Sstevel@tonic-gate getbymember(be, a)
1677c478bd9Sstevel@tonic-gate 	files_backend_ptr_t	be;
1687c478bd9Sstevel@tonic-gate 	void			*a;
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	struct nss_groupsbymem	*argp = (struct nss_groupsbymem *)a;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	return (_nss_files_do_all(be, argp, argp->username,
1737c478bd9Sstevel@tonic-gate 				(files_do_all_func_t)argp->process_cstr));
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate static files_backend_op_t group_ops[] = {
1777c478bd9Sstevel@tonic-gate 	_nss_files_destr,
1787c478bd9Sstevel@tonic-gate 	_nss_files_endent,
1797c478bd9Sstevel@tonic-gate 	_nss_files_setent,
1807c478bd9Sstevel@tonic-gate 	_nss_files_getent_rigid,
1817c478bd9Sstevel@tonic-gate 	getbyname,
1827c478bd9Sstevel@tonic-gate 	getbygid,
1837c478bd9Sstevel@tonic-gate 	getbymember
1847c478bd9Sstevel@tonic-gate };
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1877c478bd9Sstevel@tonic-gate nss_backend_t *
1887c478bd9Sstevel@tonic-gate _nss_files_group_constr(dummy1, dummy2, dummy3)
1897c478bd9Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	return (_nss_files_constr(group_ops,
1927c478bd9Sstevel@tonic-gate 				sizeof (group_ops) / sizeof (group_ops[0]),
1937c478bd9Sstevel@tonic-gate 				GF_PATH,
1947c478bd9Sstevel@tonic-gate 				NSS_LINELEN_GROUP,
1957c478bd9Sstevel@tonic-gate 				&hashinfo));
1967c478bd9Sstevel@tonic-gate }
197