8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libproject / common / getprojent.c
blob46b373c917ec9a3fbfccaaaed2ae82fc31f7e33e
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright (c) 1999-2001 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/types.h>
30 #include <user_attr.h>
31 #include <pwd.h>
32 #include <grp.h>
33 #include <userdefs.h>
34 #include <project.h>
35 #include <memory.h>
36 #include <nss_dbdefs.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/param.h>
41 #include <sys/mman.h>
43 #pragma weak setprojent = _setprojent
44 #pragma weak endprojent = _endprojent
45 #pragma weak getprojent = _getprojent
46 #pragma weak fgetprojent = _fgetprojent
47 #pragma weak getprojbyid = _getprojbyid
48 #pragma weak getprojbyname = _getprojbyname
49 #pragma weak getdefaultproj = _getdefaultproj
50 #pragma weak inproj = _inproj
51 #pragma weak getprojidbyname = _getprojidbyname
53 #define DEFAULT_PROJECT 1
54 #define NORMAL_PROJECT 0
56 static int ismember(struct project *, const char *, gid_t, int);
57 static int str2project(const char *, int, void *, char *, int);
59 static DEFINE_NSS_DB_ROOT(db_root);
60 static DEFINE_NSS_GETENT(context);
62 void
63 _nss_initf_project(nss_db_params_t *p)
65 p->name = NSS_DBNAM_PROJECT;
66 p->default_config = NSS_DEFCONF_PROJECT;
69 void
70 _setprojent(void)
72 nss_setent(&db_root, _nss_initf_project, &context);
75 void
76 _endprojent(void)
78 nss_endent(&db_root, _nss_initf_project, &context);
79 nss_delete(&db_root);
82 struct project *
83 _getprojent(struct project *result, void *buffer, size_t buflen)
85 nss_XbyY_args_t arg;
87 NSS_XbyY_INIT(&arg, result, buffer, buflen, str2project);
88 (void) nss_getent(&db_root, _nss_initf_project, &context, &arg);
89 return ((struct project *)NSS_XbyY_FINI(&arg));
92 struct project *
93 _fgetprojent(FILE *f, struct project *result, void *buffer, size_t buflen)
95 extern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t *);
96 nss_XbyY_args_t arg;
98 NSS_XbyY_INIT(&arg, result, buffer, buflen, str2project);
99 _nss_XbyY_fgets(f, &arg);
100 return ((struct project *)NSS_XbyY_FINI(&arg));
103 struct project *
104 _getprojbyid(projid_t projid, struct project *result,
105 void *buffer, size_t buflen)
107 nss_XbyY_args_t arg;
109 NSS_XbyY_INIT(&arg, result, buffer, buflen, str2project);
110 arg.key.projid = projid;
111 (void) nss_search(&db_root, _nss_initf_project,
112 NSS_DBOP_PROJECT_BYID, &arg);
113 return ((struct project *)NSS_XbyY_FINI(&arg));
116 struct project *
117 _getprojbyname(const char *name, struct project *result,
118 void *buffer, size_t buflen)
120 nss_XbyY_args_t arg;
121 NSS_XbyY_INIT(&arg, result, buffer, buflen, str2project);
122 arg.key.name = name;
123 (void) nss_search(&db_root, _nss_initf_project,
124 NSS_DBOP_PROJECT_BYNAME, &arg);
125 return ((struct project *)NSS_XbyY_FINI(&arg));
129 * The following routine checks if user specified by the second argument
130 * is allowed to join the project specified as project structure in first
131 * argument. Information about user's default group and whether or not
132 * the project specified in the first argument is user's default project
133 * (i.e., user_attr, "default", "user.username", or "group.groupname"
134 * should also be provided. If is_default is set to DEFAULT_PROJECT,
135 * then this function returns 1 (true), unless specified user explicitly
136 * excluded with "!user", or "!group" wildcards.
138 static int
139 ismember(struct project *proj, const char *user, gid_t gid, int is_default)
141 char grbuf[NSS_BUFLEN_GROUP];
142 char groupname[MAXGLEN + 1];
143 int res = is_default;
144 struct group grp;
145 int group_ok = 0;
146 char **u, **g;
147 char *member;
149 if (getgrgid_r(gid, &grp, grbuf, NSS_BUFLEN_GROUP) != NULL) {
150 group_ok = 1;
151 (void) snprintf(groupname, MAXGLEN, grp.gr_name);
155 * Scan project's user list.
157 for (u = proj->pj_users; *u; u++) {
158 member = *u;
159 if (member[0] == '!' &&
160 (strcmp(member + 1, user) == 0 ||
161 strcmp(member + 1, "*") == 0))
162 return (0);
163 if (strcmp(member, "*") == 0 || strcmp(member, user) == 0)
164 res = 1;
168 * Scan project's group list.
170 for (g = proj->pj_groups; *g; g++) {
171 member = *g;
173 * Check if user's default group is included here.
175 if (group_ok) {
176 if (member[0] == '!' &&
177 (strcmp(member + 1, groupname) == 0 ||
178 strcmp(member + 1, "*") == 0))
179 return (0);
180 if (strcmp(member, "*") == 0 ||
181 strcmp(member, groupname) == 0)
182 res = 1;
185 * Check if user is a member of one of project's groups.
187 if (getgrnam_r(member, &grp, grbuf, NSS_BUFLEN_GROUP) != NULL) {
188 for (u = grp.gr_mem; *u; u++)
189 if (strcmp(*u, user) == 0)
190 res = 1;
193 return (res);
196 struct project *
197 _getdefaultproj(const char *user, struct project *result,
198 void *buffer, size_t buflen)
200 char projname[PROJNAME_MAX + 1];
201 nss_XbyY_args_t arg;
202 userattr_t *uattr;
203 struct passwd p;
204 struct group g;
205 char *attrproj;
207 NSS_XbyY_INIT(&arg, result, buffer, buflen, str2project);
210 * Need user's default group ID for ismember() calls later
212 if (getpwnam_r(user, &p, buffer, buflen) == NULL)
213 return (NULL);
216 * Check user_attr database first
218 if ((uattr = getusernam(user)) != NULL) {
219 if ((attrproj = kva_match(uattr->attr, "project")) != NULL) {
220 arg.key.name = attrproj;
221 (void) nss_search(&db_root, _nss_initf_project,
222 NSS_DBOP_PROJECT_BYNAME, &arg);
223 if ((result = NSS_XbyY_FINI(&arg)) != NULL) {
224 free_userattr(uattr);
225 return (result);
228 free_userattr(uattr);
232 * Check user.{username} and group.{groupname} projects
234 (void) snprintf(projname, PROJNAME_MAX, "user.%s", user);
235 arg.key.name = projname;
236 (void) nss_search(&db_root, _nss_initf_project,
237 NSS_DBOP_PROJECT_BYNAME, &arg);
238 if ((result = NSS_XbyY_FINI(&arg)) != NULL &&
239 ismember(result, user, p.pw_gid, DEFAULT_PROJECT))
240 return (result);
241 if (getgrgid_r(p.pw_gid, &g, buffer, buflen) != NULL) {
242 (void) snprintf(projname, PROJNAME_MAX, "group.%s", g.gr_name);
243 arg.key.name = projname;
244 (void) nss_search(&db_root, _nss_initf_project,
245 NSS_DBOP_PROJECT_BYNAME, &arg);
246 if ((result = NSS_XbyY_FINI(&arg)) != NULL &&
247 ismember(result, user, p.pw_gid, DEFAULT_PROJECT))
248 return (result);
250 arg.key.name = "default";
251 (void) nss_search(&db_root, _nss_initf_project,
252 NSS_DBOP_PROJECT_BYNAME, &arg);
253 if ((result = NSS_XbyY_FINI(&arg)) != NULL &&
254 ismember(result, user, p.pw_gid, DEFAULT_PROJECT))
255 return (result);
256 return (NULL);
260 _inproj(const char *user, const char *name, void *buffer, size_t buflen)
262 char projname[PROJNAME_MAX + 1];
263 char grbuf[NSS_BUFLEN_GROUP];
264 nss_XbyY_args_t arg;
265 struct project proj;
266 struct passwd pwd;
267 userattr_t *uattr;
268 struct group grp;
269 char *attrproj;
270 gid_t gid;
272 NSS_XbyY_INIT(&arg, &proj, buffer, buflen, str2project);
275 * 0. Sanity checks.
277 if (getpwnam_r(user, &pwd, buffer, buflen) == NULL)
278 return (0); /* user does not exist */
279 gid = pwd.pw_gid;
280 if (getprojbyname(name, &proj, buffer, buflen) == NULL)
281 return (0); /* project does not exist */
284 * 1. Check for special "default" project.
286 if (strcmp("default", name) == 0)
287 return (ismember(&proj, user, gid, DEFAULT_PROJECT));
290 * 2. Check user_attr database.
292 if ((uattr = getusernam(user)) != NULL) {
293 if ((attrproj = kva_match(uattr->attr, "project")) != NULL) {
294 if (strcmp(attrproj, name) == 0) {
295 free_userattr(uattr);
296 return (ismember(&proj, user, gid,
297 DEFAULT_PROJECT));
300 free_userattr(uattr);
304 * 3. Check if this is a special "user.username" project.
306 * User "username" is considered to be a member of project
307 * "user.username" even if project's user lists do not
308 * include "username".
310 (void) snprintf(projname, PROJNAME_MAX, "user.%s", user);
311 if (strcmp(projname, name) == 0)
312 return (ismember(&proj, user, gid, DEFAULT_PROJECT));
315 * 4. Check if this is a special "group.groupname" project.
317 * User "username" with default group "groupname" is considered
318 * to be a member of project "group.groupname" even if project's
319 * group list does not include "groupname".
321 if (getgrgid_r(gid, &grp, grbuf, NSS_LINELEN_GROUP) != NULL) {
322 (void) snprintf(projname, PROJNAME_MAX,
323 "group.%s", grp.gr_name);
324 if (strcmp(projname, name) == 0)
325 return (ismember(&proj, user, gid, DEFAULT_PROJECT));
329 * 5. Handle all other (non-default) projects.
331 return (ismember(&proj, user, gid, NORMAL_PROJECT));
335 * Just a quick wrapper around getprojbyname so that the caller does not
336 * need to allocate the buffer.
338 projid_t
339 _getprojidbyname(const char *name)
341 struct project proj;
342 char buf[PROJECT_BUFSZ];
344 if (getprojbyname(name, &proj, &buf, PROJECT_BUFSZ) != NULL)
345 return (proj.pj_projid);
346 else
347 return ((projid_t)-1);
350 static char *
351 gettok(char **nextpp, char sep)
353 char *p = *nextpp;
354 char *q = p;
355 char c;
357 if (p == NULL)
358 return (NULL);
359 while ((c = *q) != '\0' && c != sep)
360 q++;
361 if (c == '\0')
362 *nextpp = 0;
363 else {
364 *q++ = '\0';
365 *nextpp = q;
367 return (p);
372 * Return values: 0 = success, 1 = parse error, 2 = erange ...
373 * The structure pointer passed in is a structure in the caller's space
374 * wherein the field pointers would be set to areas in the buffer if
375 * need be. instring and buffer should be separate areas.
377 static int
378 str2project(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
380 struct project *project = ent;
381 char *p, *next;
382 char *users, *groups;
383 char **uglist;
384 char **limit;
386 if (lenstr + 1 > buflen)
387 return (NSS_STR_PARSE_ERANGE);
389 * We copy the input string into the output buffer and
390 * operate on it in place.
392 (void) memcpy(buffer, instr, lenstr);
393 buffer[lenstr] = '\0';
394 next = buffer;
396 limit = (char **)ROUND_DOWN(buffer + buflen, sizeof (char *));
399 * Parsers for passwd and group have always been pretty rigid;
400 * we wouldn't want to buck a Unix tradition
402 p = gettok(&next, ':');
403 if (p == NULL || *p == '\0' || strlen(p) > PROJNAME_MAX) {
405 * empty or very long project names are not allowed
407 return (NSS_STR_PARSE_ERANGE);
409 project->pj_name = p;
411 p = gettok(&next, ':');
412 if (p == NULL || *p == '\0') {
414 * projid field shouldn't be empty
416 return (NSS_STR_PARSE_PARSE);
418 project->pj_projid = (projid_t)strtol(p, NULL, 10);
419 if (project->pj_projid < 0) {
421 * projids should be positive number
423 project->pj_projid = 0;
424 return (NSS_STR_PARSE_PARSE);
427 p = gettok(&next, ':');
428 if (p == NULL) {
430 * comment field can be empty but should not be last field
432 return (NSS_STR_PARSE_PARSE);
434 project->pj_comment = p;
436 if ((users = gettok(&next, ':')) == NULL) {
438 * users field should not be last field
440 return (NSS_STR_PARSE_PARSE);
443 if ((groups = gettok(&next, ':')) == NULL) {
445 * groups field should not be last field
447 return (NSS_STR_PARSE_PARSE);
450 if (next == NULL) {
452 * attributes field should be last
454 return (NSS_STR_PARSE_PARSE);
457 project->pj_attr = next;
459 uglist = (char **)ROUND_UP(buffer + lenstr + 1, sizeof (char *));
460 *uglist = NULL;
461 project->pj_users = uglist;
462 while (uglist < limit) {
463 p = gettok(&users, ',');
464 if (p == NULL || *p == '\0') {
465 *uglist = 0;
466 break;
468 *uglist++ = p;
470 if (uglist >= limit)
471 return (NSS_STR_PARSE_ERANGE);
473 uglist++;
474 *uglist = NULL;
475 project->pj_groups = uglist;
476 while (uglist < limit) {
477 p = gettok(&groups, ',');
478 if (p == NULL || *p == '\0') {
479 *uglist = 0;
480 break;
482 *uglist++ = p;
484 if (uglist >= limit)
485 return (NSS_STR_PARSE_ERANGE);
487 return (NSS_STR_PARSE_SUCCESS);