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 #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <project.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
29*cb5caa98Sdjl #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include "files_common.h"
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate static uint_t
hash_projname(nss_XbyY_args_t * argp,int keyhash,const char * line,int linelen)33*cb5caa98Sdjl hash_projname(nss_XbyY_args_t *argp, int keyhash, const char *line,
34*cb5caa98Sdjl int linelen) {
35*cb5caa98Sdjl
36*cb5caa98Sdjl const char *name;
37*cb5caa98Sdjl int namelen, i;
38*cb5caa98Sdjl uint_t hash = 0;
39*cb5caa98Sdjl
40*cb5caa98Sdjl if (keyhash) {
41*cb5caa98Sdjl name = argp->key.name;
42*cb5caa98Sdjl namelen = strlen(name);
43*cb5caa98Sdjl } else {
44*cb5caa98Sdjl name = line;
45*cb5caa98Sdjl namelen = 0;
46*cb5caa98Sdjl while (linelen-- && *line++ != ':')
47*cb5caa98Sdjl namelen++;
48*cb5caa98Sdjl }
49*cb5caa98Sdjl
50*cb5caa98Sdjl for (i = 0; i < namelen; i++)
51*cb5caa98Sdjl hash = hash * 15 + name[i];
527c478bd9Sstevel@tonic-gate return (hash);
537c478bd9Sstevel@tonic-gate }
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate static uint_t
hash_projid(nss_XbyY_args_t * argp,int keyhash,const char * line,int linelen)56*cb5caa98Sdjl hash_projid(nss_XbyY_args_t *argp, int keyhash, const char *line,
57*cb5caa98Sdjl int linelen) {
58*cb5caa98Sdjl
59*cb5caa98Sdjl uint_t id;
60*cb5caa98Sdjl const char *linep, *limit, *end;
61*cb5caa98Sdjl
62*cb5caa98Sdjl linep = line;
63*cb5caa98Sdjl limit = line + linelen;
64*cb5caa98Sdjl
65*cb5caa98Sdjl if (keyhash)
66*cb5caa98Sdjl return ((uint_t)argp->key.projid);
67*cb5caa98Sdjl
68*cb5caa98Sdjl /* skip projname */
69*cb5caa98Sdjl while (linep < limit && *linep++ != ':');
70*cb5caa98Sdjl if (linep == limit)
71*cb5caa98Sdjl return (0);
72*cb5caa98Sdjl
73*cb5caa98Sdjl /* projid */
74*cb5caa98Sdjl end = linep;
75*cb5caa98Sdjl id = (uint_t)strtol(linep, (char **)&end, 10);
76*cb5caa98Sdjl if (linep == end)
77*cb5caa98Sdjl return (0);
78*cb5caa98Sdjl
79*cb5caa98Sdjl return (id);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate static files_hash_func hash_proj[2] = {
837c478bd9Sstevel@tonic-gate hash_projname,
847c478bd9Sstevel@tonic-gate hash_projid
857c478bd9Sstevel@tonic-gate };
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate static files_hash_t hashinfo = {
887c478bd9Sstevel@tonic-gate DEFAULTMUTEX,
897c478bd9Sstevel@tonic-gate sizeof (struct project),
907c478bd9Sstevel@tonic-gate NSS_BUFLEN_PROJECT,
917c478bd9Sstevel@tonic-gate 2,
927c478bd9Sstevel@tonic-gate hash_proj
937c478bd9Sstevel@tonic-gate };
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate static int
check_projid(nss_XbyY_args_t * argp,const char * line,int linelen)96*cb5caa98Sdjl check_projid(nss_XbyY_args_t *argp, const char *line, int linelen) {
97*cb5caa98Sdjl projid_t projid;
98*cb5caa98Sdjl const char *linep, *limit, *end;
99*cb5caa98Sdjl
100*cb5caa98Sdjl linep = line;
101*cb5caa98Sdjl limit = line + linelen;
102*cb5caa98Sdjl
103*cb5caa98Sdjl /* skip projname */
104*cb5caa98Sdjl while (linep < limit && *linep++ != ':');
1057c478bd9Sstevel@tonic-gate
106*cb5caa98Sdjl /* empty projname not allowed */
107*cb5caa98Sdjl if (linep == limit || linep == line + 1)
1087c478bd9Sstevel@tonic-gate return (0);
1097c478bd9Sstevel@tonic-gate
110*cb5caa98Sdjl /* projid */
111*cb5caa98Sdjl end = linep;
112*cb5caa98Sdjl projid = (projid_t)strtol(linep, (char **)&end, 10);
1137c478bd9Sstevel@tonic-gate
114*cb5caa98Sdjl /* empty projid is not valid */
115*cb5caa98Sdjl if (linep == end)
1167c478bd9Sstevel@tonic-gate return (0);
117*cb5caa98Sdjl
118*cb5caa98Sdjl return (projid == argp->key.projid);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate static nss_status_t
getbyname(files_backend_ptr_t be,void * a)1227c478bd9Sstevel@tonic-gate getbyname(files_backend_ptr_t be, void *a) {
123*cb5caa98Sdjl return (_nss_files_XY_hash(be, a, 0, &hashinfo, 0,
124*cb5caa98Sdjl _nss_files_check_name_colon));
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate static nss_status_t
getbyprojid(files_backend_ptr_t be,void * a)1287c478bd9Sstevel@tonic-gate getbyprojid(files_backend_ptr_t be, void *a) {
1297c478bd9Sstevel@tonic-gate return (_nss_files_XY_hash(be, a, 0, &hashinfo, 1, check_projid));
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate static files_backend_op_t project_ops[] = {
1337c478bd9Sstevel@tonic-gate _nss_files_destr,
1347c478bd9Sstevel@tonic-gate _nss_files_endent,
1357c478bd9Sstevel@tonic-gate _nss_files_setent,
1367c478bd9Sstevel@tonic-gate _nss_files_getent_rigid,
1377c478bd9Sstevel@tonic-gate getbyname,
1387c478bd9Sstevel@tonic-gate getbyprojid
1397c478bd9Sstevel@tonic-gate };
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1427c478bd9Sstevel@tonic-gate nss_backend_t *
_nss_files_project_constr(dummy1,dummy2,dummy3)1437c478bd9Sstevel@tonic-gate _nss_files_project_constr(dummy1, dummy2, dummy3)
1447c478bd9Sstevel@tonic-gate const char *dummy1, *dummy2, *dummy3;
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate return (_nss_files_constr(project_ops,
1477c478bd9Sstevel@tonic-gate sizeof (project_ops) / sizeof (project_ops[0]),
1487c478bd9Sstevel@tonic-gate PROJF_PATH,
1497c478bd9Sstevel@tonic-gate NSS_LINELEN_PROJECT,
1507c478bd9Sstevel@tonic-gate &hashinfo));
1517c478bd9Sstevel@tonic-gate }
152