xref: /titanic_51/usr/src/lib/nsswitch/files/common/getprojent.c (revision 9512fe850e98fdd448c638ca63fdd92a8a510255)
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) 1999-2000 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <project.h>
31 #include <string.h>
32 #include "files_common.h"
33 
34 static uint_t
35 hash_projname(nss_XbyY_args_t *argp, int keyhash) {
36 	struct project *p = argp->returnval;
37 	const char *name = keyhash ? argp->key.name : p->pj_name;
38 	uint_t hash = 0;
39 
40 	while (*name != 0)
41 		hash = hash * 15 + *name++;
42 
43 	return (hash);
44 }
45 
46 static uint_t
47 hash_projid(nss_XbyY_args_t *argp, int keyhash) {
48 	struct project *p = argp->returnval;
49 	return (keyhash ? (uint_t)argp->key.projid : (uint_t)p->pj_projid);
50 }
51 
52 static files_hash_func hash_proj[2] = {
53 	hash_projname,
54 	hash_projid
55 };
56 
57 static files_hash_t hashinfo = {
58 	DEFAULTMUTEX,
59 	sizeof (struct project),
60 	NSS_BUFLEN_PROJECT,
61 	2,
62 	hash_proj
63 };
64 
65 static int
66 check_projname(nss_XbyY_args_t *argp) {
67 	struct project *p = argp->returnval;
68 
69 	if (p->pj_name == 0)
70 		return (0);
71 	return (strcmp(p->pj_name, argp->key.name) == 0);
72 }
73 
74 static int
75 check_projid(nss_XbyY_args_t *argp) {
76 	struct project *p = argp->returnval;
77 
78 	if (p->pj_name == 0)
79 		return (0);
80 	return (p->pj_projid == argp->key.projid);
81 }
82 
83 static nss_status_t
84 getbyname(files_backend_ptr_t be, void *a) {
85 	return (_nss_files_XY_hash(be, a, 0, &hashinfo, 0, check_projname));
86 }
87 
88 static nss_status_t
89 getbyprojid(files_backend_ptr_t be, void *a) {
90 	return (_nss_files_XY_hash(be, a, 0, &hashinfo, 1, check_projid));
91 }
92 
93 static files_backend_op_t project_ops[] = {
94 	_nss_files_destr,
95 	_nss_files_endent,
96 	_nss_files_setent,
97 	_nss_files_getent_rigid,
98 	getbyname,
99 	getbyprojid
100 };
101 
102 /*ARGSUSED*/
103 nss_backend_t *
104 _nss_files_project_constr(dummy1, dummy2, dummy3)
105 	const char *dummy1, *dummy2, *dummy3;
106 {
107 	return (_nss_files_constr(project_ops,
108 		    sizeof (project_ops) / sizeof (project_ops[0]),
109 		    PROJF_PATH,
110 		    NSS_LINELEN_PROJECT,
111 		    &hashinfo));
112 }
113