8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libsecdb / common / getexecattr.c
blobf4c48f10232e3e74d02f4acfe312987afd38032e
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/types.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <nss_dbdefs.h>
30 #include <deflt.h>
31 #include <exec_attr.h>
32 #include <user_attr.h>
33 #include <auth_attr.h>
34 #include <prof_attr.h>
35 #include <getxby_door.h>
36 #include <sys/mman.h>
39 /* Externs from libnsl */
40 extern execstr_t *_getexecattr(execstr_t *, char *, int, int *);
41 extern void _setexecattr(void);
42 extern void _endexecattr(void);
43 extern execstr_t *_getexecprof(const char *, const char *, const char *, int,
44 execstr_t *, char *, int, int *);
45 extern userstr_t *_getusernam(const char *, userstr_t *, char *, int, int *);
46 extern userstr_t *_getuserattr(userstr_t *, char *, int, int *);
47 extern char *_strtok_escape(char *, char *, char **);
48 extern char *_strdup_null(char *);
50 static execattr_t *userprof(const char *, const char *, const char *, int);
51 static execattr_t *get_tail(execattr_t *);
52 static execattr_t *execstr2attr(execstr_t *);
54 execattr_t *
55 getexecattr()
57 int err = 0;
58 char buf[NSS_BUFLEN_EXECATTR];
59 execstr_t exec;
60 execstr_t *tmp;
62 tmp = _getexecattr(&exec, buf, NSS_BUFLEN_EXECATTR, &err);
64 return (execstr2attr(tmp));
68 execattr_t *
69 getexecprof(const char *name, const char *type, const char *id, int search_flag)
71 int err = 0;
72 char unique[NSS_BUFLEN_EXECATTR];
73 char buf[NSS_BUFLEN_EXECATTR];
74 execattr_t *head = NULL;
75 execattr_t *prev = NULL;
76 execstr_t exec;
77 execstr_t *tmp;
79 (void) memset(unique, 0, NSS_BUFLEN_EXECATTR);
80 (void) memset(&exec, 0, sizeof (execstr_t));
82 if (!IS_GET_ONE(search_flag) && !IS_GET_ALL(search_flag)) {
83 return (NULL);
86 if ((name == NULL) && (type == NULL) && (id == NULL)) {
87 setexecattr();
88 if (IS_GET_ONE(search_flag)) {
89 head = getexecattr();
90 } else if (IS_GET_ALL(search_flag)) {
91 head = getexecattr();
92 prev = head;
93 while (prev != NULL) {
94 prev->next = getexecattr();
95 prev = prev->next;
97 } else {
98 head = NULL;
100 endexecattr();
101 return (head);
104 tmp = _getexecprof(name,
105 type,
107 search_flag,
108 &exec,
109 buf,
110 NSS_BUFLEN_EXECATTR,
111 &err);
113 return (execstr2attr(tmp));
116 execattr_t *
117 getexecuser(const char *username, const char *type, const char *id,
118 int search_flag)
120 int err = 0;
121 char buf[NSS_BUFLEN_USERATTR];
122 userstr_t user;
123 userstr_t *utmp;
124 execattr_t *head = NULL;
125 execattr_t *prev = NULL;
126 execattr_t *new = NULL;
128 if (!IS_GET_ONE(search_flag) && !IS_GET_ALL(search_flag)) {
129 return (NULL);
132 if (username == NULL) {
133 setuserattr();
134 /* avoid userstr2attr mallocs by calling libnsl directly */
135 utmp = _getuserattr(&user, buf, NSS_BUFLEN_USERATTR, &err);
136 if (utmp == NULL) {
137 return (head);
139 if (IS_GET_ONE(search_flag)) {
140 head = userprof((const char *)(utmp->name), type, id,
141 search_flag);
142 } else if (IS_GET_ALL(search_flag)) {
143 head = userprof((const char *)(utmp->name), type, id,
144 search_flag);
145 if (head != NULL) {
146 prev = get_tail(head);
148 while ((utmp = _getuserattr(&user,
149 buf, NSS_BUFLEN_USERATTR, &err)) != NULL) {
150 if ((new =
151 userprof((const char *)(utmp->name),
152 type, id, search_flag)) != NULL) {
153 if (prev != NULL) {
154 prev->next = new;
155 prev = get_tail(prev->next);
156 } else {
157 head = new;
158 prev = get_tail(head);
162 } else {
163 head = NULL;
165 enduserattr();
166 } else {
167 head = userprof(username, type, id, search_flag);
170 return (head);
174 execattr_t *
175 match_execattr(execattr_t *exec, const char *profname, const char *type,
176 const char *id)
178 execattr_t *execp = NULL;
180 for (execp = exec; execp != NULL; execp = execp->next) {
181 if ((profname && execp->name &&
182 (strcmp(profname, execp->name) != 0)) ||
183 (type && execp->type && (strcmp(type, execp->type) != 0)) ||
184 (id && execp->id && (strcmp(id, execp->id) != 0)))
185 continue;
188 return (execp);
192 void
193 setexecattr()
195 _setexecattr();
199 void
200 endexecattr()
202 _endexecattr();
206 void
207 free_execattr(execattr_t *exec)
209 if (exec != NULL) {
210 free(exec->name);
211 free(exec->type);
212 free(exec->policy);
213 free(exec->res1);
214 free(exec->res2);
215 free(exec->id);
216 _kva_free(exec->attr);
217 free_execattr(exec->next);
218 free(exec);
222 typedef struct call {
223 const char *type;
224 const char *id;
225 int sflag;
226 } call;
228 typedef struct result {
229 execattr_t *head;
230 execattr_t *prev;
231 } result;
233 /*ARGSUSED*/
234 static int
235 findexecattr(const char *prof, kva_t *kva, void *ctxt, void *res)
237 execattr_t *exec;
238 call *c = ctxt;
239 result *r = res;
241 if ((exec = getexecprof(prof, c->type, c->id, c->sflag)) != NULL) {
242 if (IS_GET_ONE(c->sflag)) {
243 r->head = exec;
244 return (1);
245 } else if (IS_GET_ALL(c->sflag)) {
246 if (r->head == NULL) {
247 r->head = exec;
248 r->prev = get_tail(r->head);
249 } else {
250 r->prev->next = exec;
251 r->prev = get_tail(exec);
255 return (0);
259 static execattr_t *
260 userprof(const char *username, const char *type, const char *id,
261 int search_flag)
264 char pwdb[NSS_BUFLEN_PASSWD];
265 struct passwd pwd;
266 call call;
267 result result;
270 * Check if specified username is valid user
272 if (getpwnam_r(username, &pwd, pwdb, sizeof (pwdb)) == NULL) {
273 return (NULL);
276 result.head = result.prev = NULL;
277 call.type = type;
278 call.id = id;
279 call.sflag = search_flag;
281 (void) _enum_profs(username, findexecattr, &call, &result);
283 return (result.head);
287 static execattr_t *
288 get_tail(execattr_t *exec)
290 execattr_t *i_exec = NULL;
291 execattr_t *j_exec = NULL;
293 if (exec != NULL) {
294 if (exec->next == NULL) {
295 j_exec = exec;
296 } else {
297 for (i_exec = exec->next; i_exec != NULL;
298 i_exec = i_exec->next) {
299 j_exec = i_exec;
304 return (j_exec);
308 static execattr_t *
309 execstr2attr(execstr_t *es)
311 execattr_t *newexec;
313 if (es == NULL) {
314 return (NULL);
316 if ((newexec = malloc(sizeof (execattr_t))) == NULL) {
317 return (NULL);
320 newexec->name = _do_unescape(es->name);
321 newexec->policy = _do_unescape(es->policy);
322 newexec->type = _do_unescape(es->type);
323 newexec->res1 = _do_unescape(es->res1);
324 newexec->res2 = _do_unescape(es->res2);
325 newexec->id = _do_unescape(es->id);
326 newexec->attr = _str2kva(es->attr, KV_ASSIGN, KV_DELIMITER);
327 if (es->next) {
328 newexec->next = execstr2attr((execstr_t *)(es->next));
329 } else {
330 newexec->next = NULL;
332 return (newexec);
335 #ifdef DEBUG
336 void
337 print_execattr(execattr_t *exec)
339 extern void print_kva(kva_t *);
340 char *empty = "empty";
342 if (exec != NULL) {
343 printf("name=%s\n", exec->name ? exec->name : empty);
344 printf("policy=%s\n", exec->policy ? exec->policy : empty);
345 printf("type=%s\n", exec->type ? exec->type : empty);
346 printf("res1=%s\n", exec->res1 ? exec->res1 : empty);
347 printf("res2=%s\n", exec->res2 ? exec->res2 : empty);
348 printf("id=%s\n", exec->id ? exec->id : empty);
349 printf("attr=\n");
350 print_kva(exec->attr);
351 fflush(stdout);
352 if (exec->next) {
353 print_execattr(exec->next);
355 } else {
356 printf("NULL\n");
359 #endif /* DEBUG */