xref: /titanic_41/usr/src/lib/nsswitch/files/common/getprojent.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 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <project.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
31*cb5caa98Sdjl #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include "files_common.h"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate static uint_t
hash_projname(nss_XbyY_args_t * argp,int keyhash,const char * line,int linelen)35*cb5caa98Sdjl hash_projname(nss_XbyY_args_t *argp, int keyhash, const char *line,
36*cb5caa98Sdjl 		int linelen) {
37*cb5caa98Sdjl 
38*cb5caa98Sdjl 	const char	*name;
39*cb5caa98Sdjl 	int		namelen, i;
407c478bd9Sstevel@tonic-gate 	uint_t		hash = 0;
417c478bd9Sstevel@tonic-gate 
42*cb5caa98Sdjl 	if (keyhash) {
43*cb5caa98Sdjl 		name = argp->key.name;
44*cb5caa98Sdjl 		namelen = strlen(name);
45*cb5caa98Sdjl 	} else {
46*cb5caa98Sdjl 		name = line;
47*cb5caa98Sdjl 		namelen = 0;
48*cb5caa98Sdjl 		while (linelen-- && *line++ != ':')
49*cb5caa98Sdjl 			namelen++;
50*cb5caa98Sdjl 	}
517c478bd9Sstevel@tonic-gate 
52*cb5caa98Sdjl 	for (i = 0; i < namelen; i++)
53*cb5caa98Sdjl 		hash = hash * 15 + name[i];
547c478bd9Sstevel@tonic-gate 	return (hash);
557c478bd9Sstevel@tonic-gate }
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static uint_t
hash_projid(nss_XbyY_args_t * argp,int keyhash,const char * line,int linelen)58*cb5caa98Sdjl hash_projid(nss_XbyY_args_t *argp, int keyhash, const char *line,
59*cb5caa98Sdjl 		int linelen) {
60*cb5caa98Sdjl 
61*cb5caa98Sdjl 	uint_t		id;
62*cb5caa98Sdjl 	const char	*linep, *limit, *end;
63*cb5caa98Sdjl 
64*cb5caa98Sdjl 	linep = line;
65*cb5caa98Sdjl 	limit = line + linelen;
66*cb5caa98Sdjl 
67*cb5caa98Sdjl 	if (keyhash)
68*cb5caa98Sdjl 		return ((uint_t)argp->key.projid);
69*cb5caa98Sdjl 
70*cb5caa98Sdjl 	/* skip projname */
71*cb5caa98Sdjl 	while (linep < limit && *linep++ != ':');
72*cb5caa98Sdjl 	if (linep == limit)
73*cb5caa98Sdjl 		return (0);
74*cb5caa98Sdjl 
75*cb5caa98Sdjl 	/* projid */
76*cb5caa98Sdjl 	end = linep;
77*cb5caa98Sdjl 	id = (uint_t)strtol(linep, (char **)&end, 10);
78*cb5caa98Sdjl 	if (linep == end)
79*cb5caa98Sdjl 		return (0);
80*cb5caa98Sdjl 
81*cb5caa98Sdjl 	return (id);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static files_hash_func hash_proj[2] = {
857c478bd9Sstevel@tonic-gate 	hash_projname,
867c478bd9Sstevel@tonic-gate 	hash_projid
877c478bd9Sstevel@tonic-gate };
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static files_hash_t hashinfo = {
907c478bd9Sstevel@tonic-gate 	DEFAULTMUTEX,
917c478bd9Sstevel@tonic-gate 	sizeof (struct project),
927c478bd9Sstevel@tonic-gate 	NSS_BUFLEN_PROJECT,
937c478bd9Sstevel@tonic-gate 	2,
947c478bd9Sstevel@tonic-gate 	hash_proj
957c478bd9Sstevel@tonic-gate };
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static int
check_projid(nss_XbyY_args_t * argp,const char * line,int linelen)98*cb5caa98Sdjl check_projid(nss_XbyY_args_t *argp, const char *line, int linelen) {
99*cb5caa98Sdjl 	projid_t	projid;
100*cb5caa98Sdjl 	const char	*linep, *limit, *end;
1017c478bd9Sstevel@tonic-gate 
102*cb5caa98Sdjl 	linep = line;
103*cb5caa98Sdjl 	limit = line + linelen;
104*cb5caa98Sdjl 
105*cb5caa98Sdjl 	/* skip projname */
106*cb5caa98Sdjl 	while (linep < limit && *linep++ != ':');
107*cb5caa98Sdjl 
108*cb5caa98Sdjl 	/* empty projname not allowed */
109*cb5caa98Sdjl 	if (linep == limit || linep == line + 1)
1107c478bd9Sstevel@tonic-gate 		return (0);
1117c478bd9Sstevel@tonic-gate 
112*cb5caa98Sdjl 	/* projid */
113*cb5caa98Sdjl 	end = linep;
114*cb5caa98Sdjl 	projid = (projid_t)strtol(linep, (char **)&end, 10);
1157c478bd9Sstevel@tonic-gate 
116*cb5caa98Sdjl 	/* empty projid is not valid */
117*cb5caa98Sdjl 	if (linep == end)
1187c478bd9Sstevel@tonic-gate 		return (0);
119*cb5caa98Sdjl 
120*cb5caa98Sdjl 	return (projid == argp->key.projid);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static nss_status_t
getbyname(files_backend_ptr_t be,void * a)1247c478bd9Sstevel@tonic-gate getbyname(files_backend_ptr_t be, void *a) {
125*cb5caa98Sdjl 	return (_nss_files_XY_hash(be, a, 0, &hashinfo, 0,
126*cb5caa98Sdjl 			_nss_files_check_name_colon));
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate static nss_status_t
getbyprojid(files_backend_ptr_t be,void * a)1307c478bd9Sstevel@tonic-gate getbyprojid(files_backend_ptr_t be, void *a) {
1317c478bd9Sstevel@tonic-gate 	return (_nss_files_XY_hash(be, a, 0, &hashinfo, 1, check_projid));
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate static files_backend_op_t project_ops[] = {
1357c478bd9Sstevel@tonic-gate 	_nss_files_destr,
1367c478bd9Sstevel@tonic-gate 	_nss_files_endent,
1377c478bd9Sstevel@tonic-gate 	_nss_files_setent,
1387c478bd9Sstevel@tonic-gate 	_nss_files_getent_rigid,
1397c478bd9Sstevel@tonic-gate 	getbyname,
1407c478bd9Sstevel@tonic-gate 	getbyprojid
1417c478bd9Sstevel@tonic-gate };
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1447c478bd9Sstevel@tonic-gate nss_backend_t *
_nss_files_project_constr(dummy1,dummy2,dummy3)1457c478bd9Sstevel@tonic-gate _nss_files_project_constr(dummy1, dummy2, dummy3)
1467c478bd9Sstevel@tonic-gate 	const char *dummy1, *dummy2, *dummy3;
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	return (_nss_files_constr(project_ops,
1497c478bd9Sstevel@tonic-gate 		    sizeof (project_ops) / sizeof (project_ops[0]),
1507c478bd9Sstevel@tonic-gate 		    PROJF_PATH,
1517c478bd9Sstevel@tonic-gate 		    NSS_LINELEN_PROJECT,
1527c478bd9Sstevel@tonic-gate 		    &hashinfo));
1537c478bd9Sstevel@tonic-gate }
154