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]
22 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/types.h>
29 #include <nss_dbdefs.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>
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
*);
58 char buf
[NSS_BUFLEN_EXECATTR
];
62 tmp
= _getexecattr(&exec
, buf
, NSS_BUFLEN_EXECATTR
, &err
);
64 return (execstr2attr(tmp
));
69 getexecprof(const char *name
, const char *type
, const char *id
, int search_flag
)
72 char unique
[NSS_BUFLEN_EXECATTR
];
73 char buf
[NSS_BUFLEN_EXECATTR
];
74 execattr_t
*head
= NULL
;
75 execattr_t
*prev
= NULL
;
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
)) {
86 if ((name
== NULL
) && (type
== NULL
) && (id
== NULL
)) {
88 if (IS_GET_ONE(search_flag
)) {
90 } else if (IS_GET_ALL(search_flag
)) {
93 while (prev
!= NULL
) {
94 prev
->next
= getexecattr();
104 tmp
= _getexecprof(name
,
113 return (execstr2attr(tmp
));
117 getexecuser(const char *username
, const char *type
, const char *id
,
121 char buf
[NSS_BUFLEN_USERATTR
];
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
)) {
132 if (username
== NULL
) {
134 /* avoid userstr2attr mallocs by calling libnsl directly */
135 utmp
= _getuserattr(&user
, buf
, NSS_BUFLEN_USERATTR
, &err
);
139 if (IS_GET_ONE(search_flag
)) {
140 head
= userprof((const char *)(utmp
->name
), type
, id
,
142 } else if (IS_GET_ALL(search_flag
)) {
143 head
= userprof((const char *)(utmp
->name
), type
, id
,
146 prev
= get_tail(head
);
148 while ((utmp
= _getuserattr(&user
,
149 buf
, NSS_BUFLEN_USERATTR
, &err
)) != NULL
) {
151 userprof((const char *)(utmp
->name
),
152 type
, id
, search_flag
)) != NULL
) {
155 prev
= get_tail(prev
->next
);
158 prev
= get_tail(head
);
167 head
= userprof(username
, type
, id
, search_flag
);
175 match_execattr(execattr_t
*exec
, const char *profname
, const char *type
,
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)))
207 free_execattr(execattr_t
*exec
)
216 _kva_free(exec
->attr
);
217 free_execattr(exec
->next
);
222 typedef struct call
{
228 typedef struct result
{
235 findexecattr(const char *prof
, kva_t
*kva
, void *ctxt
, void *res
)
241 if ((exec
= getexecprof(prof
, c
->type
, c
->id
, c
->sflag
)) != NULL
) {
242 if (IS_GET_ONE(c
->sflag
)) {
245 } else if (IS_GET_ALL(c
->sflag
)) {
246 if (r
->head
== NULL
) {
248 r
->prev
= get_tail(r
->head
);
250 r
->prev
->next
= exec
;
251 r
->prev
= get_tail(exec
);
260 userprof(const char *username
, const char *type
, const char *id
,
264 char pwdb
[NSS_BUFLEN_PASSWD
];
270 * Check if specified username is valid user
272 if (getpwnam_r(username
, &pwd
, pwdb
, sizeof (pwdb
)) == NULL
) {
276 result
.head
= result
.prev
= NULL
;
279 call
.sflag
= search_flag
;
281 (void) _enum_profs(username
, findexecattr
, &call
, &result
);
283 return (result
.head
);
288 get_tail(execattr_t
*exec
)
290 execattr_t
*i_exec
= NULL
;
291 execattr_t
*j_exec
= NULL
;
294 if (exec
->next
== NULL
) {
297 for (i_exec
= exec
->next
; i_exec
!= NULL
;
298 i_exec
= i_exec
->next
) {
309 execstr2attr(execstr_t
*es
)
316 if ((newexec
= malloc(sizeof (execattr_t
))) == 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
);
328 newexec
->next
= execstr2attr((execstr_t
*)(es
->next
));
330 newexec
->next
= NULL
;
337 print_execattr(execattr_t
*exec
)
339 extern void print_kva(kva_t
*);
340 char *empty
= "empty";
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
);
350 print_kva(exec
->attr
);
353 print_execattr(exec
->next
);