xref: /titanic_51/usr/src/lib/libsecdb/common/getexecattr.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <string.h>
32*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
33*7c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
34*7c478bd9Sstevel@tonic-gate #include <deflt.h>
35*7c478bd9Sstevel@tonic-gate #include <exec_attr.h>
36*7c478bd9Sstevel@tonic-gate #include <user_attr.h>
37*7c478bd9Sstevel@tonic-gate #include <auth_attr.h>
38*7c478bd9Sstevel@tonic-gate #include <prof_attr.h>
39*7c478bd9Sstevel@tonic-gate #include <getxby_door.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/mman.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /* Externs from libnsl */
44*7c478bd9Sstevel@tonic-gate extern execstr_t *_getexecattr(execstr_t *, char *, int, int *);
45*7c478bd9Sstevel@tonic-gate extern void _setexecattr(void);
46*7c478bd9Sstevel@tonic-gate extern void _endexecattr(void);
47*7c478bd9Sstevel@tonic-gate extern execstr_t *_getexecprof(const char *, const char *, const char *, int,
48*7c478bd9Sstevel@tonic-gate     execstr_t *, char *, int, int *);
49*7c478bd9Sstevel@tonic-gate extern userstr_t *_getusernam(const char *, userstr_t *, char *, int, int *);
50*7c478bd9Sstevel@tonic-gate extern userstr_t *_getuserattr(userstr_t *, char *, int, int *);
51*7c478bd9Sstevel@tonic-gate extern char *_strtok_escape(char *, char *, char **);
52*7c478bd9Sstevel@tonic-gate extern char *_strdup_null(char *);
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate static execattr_t *userprof(const char *, const char *, const char *, int);
55*7c478bd9Sstevel@tonic-gate static execattr_t *get_tail(execattr_t *);
56*7c478bd9Sstevel@tonic-gate static execattr_t *execstr2attr(execstr_t *);
57*7c478bd9Sstevel@tonic-gate static execstr_t *process_getexec(execstr_t *, char *, int, nsc_data_t *);
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate execattr_t *
60*7c478bd9Sstevel@tonic-gate getexecattr()
61*7c478bd9Sstevel@tonic-gate {
62*7c478bd9Sstevel@tonic-gate 	int		err = 0;
63*7c478bd9Sstevel@tonic-gate 	char		buf[NSS_BUFLEN_EXECATTR];
64*7c478bd9Sstevel@tonic-gate 	execstr_t	exec;
65*7c478bd9Sstevel@tonic-gate 	execstr_t	*tmp;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	tmp = _getexecattr(&exec, buf, NSS_BUFLEN_EXECATTR, &err);
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	return (execstr2attr(tmp));
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate execattr_t *
74*7c478bd9Sstevel@tonic-gate getexecprof(const char *name, const char *type, const char *id, int search_flag)
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate 	int		len_unique;
77*7c478bd9Sstevel@tonic-gate 	int		err = 0;
78*7c478bd9Sstevel@tonic-gate 	int		ndata = 0;
79*7c478bd9Sstevel@tonic-gate 	int		adata = 0;
80*7c478bd9Sstevel@tonic-gate 	char		unique[NSS_BUFLEN_EXECATTR];
81*7c478bd9Sstevel@tonic-gate 	char		buf[NSS_BUFLEN_EXECATTR];
82*7c478bd9Sstevel@tonic-gate 	execattr_t	*head = (execattr_t *)NULL;
83*7c478bd9Sstevel@tonic-gate 	execattr_t	*prev = (execattr_t *)NULL;
84*7c478bd9Sstevel@tonic-gate 	execstr_t	exec;
85*7c478bd9Sstevel@tonic-gate 	execstr_t	*tmp;
86*7c478bd9Sstevel@tonic-gate 	execstr_t	*resptr = (execstr_t *)NULL;
87*7c478bd9Sstevel@tonic-gate 	nsc_data_t	*sptr = (nsc_data_t *)NULL;
88*7c478bd9Sstevel@tonic-gate 	union {
89*7c478bd9Sstevel@tonic-gate 		nsc_data_t 	s_d;
90*7c478bd9Sstevel@tonic-gate 		char		s_b[NSS_BUFLEN_EXECATTR];
91*7c478bd9Sstevel@tonic-gate 	} space;
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	(void) memset(unique, 0, NSS_BUFLEN_EXECATTR);
94*7c478bd9Sstevel@tonic-gate 	(void) memset(&exec, 0, sizeof (execstr_t));
95*7c478bd9Sstevel@tonic-gate 	(void) memset(&space, 0, sizeof (space));
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	if ((search_flag != GET_ONE) && (search_flag != GET_ALL)) {
98*7c478bd9Sstevel@tonic-gate 		return ((execattr_t *)NULL);
99*7c478bd9Sstevel@tonic-gate 	}
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	if ((name == NULL) && (type == NULL) && (id == NULL)) {
102*7c478bd9Sstevel@tonic-gate 		setexecattr();
103*7c478bd9Sstevel@tonic-gate 		switch (search_flag) {
104*7c478bd9Sstevel@tonic-gate 		case GET_ONE:
105*7c478bd9Sstevel@tonic-gate 			head = getexecattr();
106*7c478bd9Sstevel@tonic-gate 			break;
107*7c478bd9Sstevel@tonic-gate 		case GET_ALL:
108*7c478bd9Sstevel@tonic-gate 			head = getexecattr();
109*7c478bd9Sstevel@tonic-gate 			prev = head;
110*7c478bd9Sstevel@tonic-gate 			while (prev != NULL) {
111*7c478bd9Sstevel@tonic-gate 				prev->next = getexecattr();
112*7c478bd9Sstevel@tonic-gate 				prev = prev->next;
113*7c478bd9Sstevel@tonic-gate 			};
114*7c478bd9Sstevel@tonic-gate 			break;
115*7c478bd9Sstevel@tonic-gate 		default:
116*7c478bd9Sstevel@tonic-gate 			head = (execattr_t *)NULL;
117*7c478bd9Sstevel@tonic-gate 			break;
118*7c478bd9Sstevel@tonic-gate 		}
119*7c478bd9Sstevel@tonic-gate 		endexecattr();
120*7c478bd9Sstevel@tonic-gate 		return (head);
121*7c478bd9Sstevel@tonic-gate 	}
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate #ifdef PIC
124*7c478bd9Sstevel@tonic-gate 	/*
125*7c478bd9Sstevel@tonic-gate 	 * If the search criteria is completely specified
126*7c478bd9Sstevel@tonic-gate 	 * and we only want a single entry,
127*7c478bd9Sstevel@tonic-gate 	 * then attempt to look up the entry using the nscd.
128*7c478bd9Sstevel@tonic-gate 	 * Only commands are cached.
129*7c478bd9Sstevel@tonic-gate 	 */
130*7c478bd9Sstevel@tonic-gate 	if (name && type && (strcmp(type, KV_COMMAND) == 0) && id &&
131*7c478bd9Sstevel@tonic-gate 	    (search_flag == GET_ONE)) {
132*7c478bd9Sstevel@tonic-gate 		if (snprintf(unique, NSS_BUFLEN_EXECATTR, "%s:%s:%s",
133*7c478bd9Sstevel@tonic-gate 		    name, type, id) >= NSS_BUFLEN_EXECATTR) {
134*7c478bd9Sstevel@tonic-gate 			errno = ERANGE;
135*7c478bd9Sstevel@tonic-gate 			return ((execattr_t *)NULL);
136*7c478bd9Sstevel@tonic-gate 		}
137*7c478bd9Sstevel@tonic-gate 		len_unique = strlen(unique);
138*7c478bd9Sstevel@tonic-gate 		if ((len_unique >= (sizeof (space) - sizeof (nsc_data_t)))) {
139*7c478bd9Sstevel@tonic-gate 			errno = ERANGE;
140*7c478bd9Sstevel@tonic-gate 			return ((execattr_t *)NULL);
141*7c478bd9Sstevel@tonic-gate 		}
142*7c478bd9Sstevel@tonic-gate 		ndata = sizeof (space);
143*7c478bd9Sstevel@tonic-gate 		adata = len_unique + sizeof (nsc_call_t) + 1;
144*7c478bd9Sstevel@tonic-gate 		space.s_d.nsc_call.nsc_callnumber = GETEXECID;
145*7c478bd9Sstevel@tonic-gate 		(void) strcpy(space.s_d.nsc_call.nsc_u.name, unique);
146*7c478bd9Sstevel@tonic-gate 		sptr = &space.s_d;
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 		switch (_nsc_trydoorcall(&sptr, &ndata, &adata)) {
149*7c478bd9Sstevel@tonic-gate 		case SUCCESS:	/* positive cache hit */
150*7c478bd9Sstevel@tonic-gate 			break;
151*7c478bd9Sstevel@tonic-gate 		case NOTFOUND:	/* negative cache hit */
152*7c478bd9Sstevel@tonic-gate 			return ((execattr_t *)NULL);
153*7c478bd9Sstevel@tonic-gate 		default:
154*7c478bd9Sstevel@tonic-gate 			resptr = _getexecprof(name, type, id, search_flag,
155*7c478bd9Sstevel@tonic-gate 			    &exec, buf, NSS_BUFLEN_EXECATTR, &err);
156*7c478bd9Sstevel@tonic-gate 			return (execstr2attr(resptr));
157*7c478bd9Sstevel@tonic-gate 		}
158*7c478bd9Sstevel@tonic-gate 		resptr = process_getexec(&exec, buf, NSS_BUFLEN_EXECATTR,
159*7c478bd9Sstevel@tonic-gate 		    sptr);
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 		/*
162*7c478bd9Sstevel@tonic-gate 		 * check if doors reallocated the memory underneath us
163*7c478bd9Sstevel@tonic-gate 		 * if they did munmap it or suffer a memory leak
164*7c478bd9Sstevel@tonic-gate 		 */
165*7c478bd9Sstevel@tonic-gate 		if (sptr != &space.s_d)
166*7c478bd9Sstevel@tonic-gate 			(void) munmap((void *)sptr, ndata);
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 		return (execstr2attr(resptr));
169*7c478bd9Sstevel@tonic-gate 	} /* end if (name && type && id && search_flag == GET_ONE) */
170*7c478bd9Sstevel@tonic-gate #endif	/* PIC */
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	tmp = _getexecprof(name,
173*7c478bd9Sstevel@tonic-gate 	    type,
174*7c478bd9Sstevel@tonic-gate 	    id,
175*7c478bd9Sstevel@tonic-gate 	    search_flag,
176*7c478bd9Sstevel@tonic-gate 	    &exec,
177*7c478bd9Sstevel@tonic-gate 	    buf,
178*7c478bd9Sstevel@tonic-gate 	    NSS_BUFLEN_EXECATTR,
179*7c478bd9Sstevel@tonic-gate 	    &err);
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	return (execstr2attr(tmp));
182*7c478bd9Sstevel@tonic-gate }
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate execattr_t *
186*7c478bd9Sstevel@tonic-gate getexecuser(const char *username, const char *type, const char *id,
187*7c478bd9Sstevel@tonic-gate     int search_flag)
188*7c478bd9Sstevel@tonic-gate {
189*7c478bd9Sstevel@tonic-gate 	int		err = 0;
190*7c478bd9Sstevel@tonic-gate 	char		buf[NSS_BUFLEN_USERATTR];
191*7c478bd9Sstevel@tonic-gate 	userstr_t	user;
192*7c478bd9Sstevel@tonic-gate 	userstr_t	*utmp;
193*7c478bd9Sstevel@tonic-gate 	execattr_t	*head = (execattr_t *)NULL;
194*7c478bd9Sstevel@tonic-gate 	execattr_t	*prev = (execattr_t *)NULL;
195*7c478bd9Sstevel@tonic-gate 	execattr_t	*new = (execattr_t *)NULL;
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	if ((search_flag != GET_ONE) && (search_flag != GET_ALL)) {
198*7c478bd9Sstevel@tonic-gate 		return ((execattr_t *)NULL);
199*7c478bd9Sstevel@tonic-gate 	}
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	if (username == NULL) {
202*7c478bd9Sstevel@tonic-gate 		setuserattr();
203*7c478bd9Sstevel@tonic-gate 		/* avoid userstr2attr mallocs by calling libnsl directly */
204*7c478bd9Sstevel@tonic-gate 		utmp = _getuserattr(&user, buf, NSS_BUFLEN_USERATTR, &err);
205*7c478bd9Sstevel@tonic-gate 		if (utmp == NULL) {
206*7c478bd9Sstevel@tonic-gate 			return (head);
207*7c478bd9Sstevel@tonic-gate 		}
208*7c478bd9Sstevel@tonic-gate 		switch (search_flag) {
209*7c478bd9Sstevel@tonic-gate 		case GET_ONE:
210*7c478bd9Sstevel@tonic-gate 			head = userprof((const char *)(utmp->name), type, id,
211*7c478bd9Sstevel@tonic-gate 			    search_flag);
212*7c478bd9Sstevel@tonic-gate 			break;
213*7c478bd9Sstevel@tonic-gate 		case GET_ALL:
214*7c478bd9Sstevel@tonic-gate 			head = userprof((const char *)(utmp->name), type, id,
215*7c478bd9Sstevel@tonic-gate 			    search_flag);
216*7c478bd9Sstevel@tonic-gate 			if (head != NULL) {
217*7c478bd9Sstevel@tonic-gate 				prev = get_tail(head);
218*7c478bd9Sstevel@tonic-gate 			}
219*7c478bd9Sstevel@tonic-gate 			while ((utmp = _getuserattr(&user,
220*7c478bd9Sstevel@tonic-gate 				    buf, NSS_BUFLEN_USERATTR, &err)) != NULL) {
221*7c478bd9Sstevel@tonic-gate 				if ((new =
222*7c478bd9Sstevel@tonic-gate 				    userprof((const char *)(utmp->name),
223*7c478bd9Sstevel@tonic-gate 				    type, id, search_flag)) != NULL) {
224*7c478bd9Sstevel@tonic-gate 					if (prev != NULL) {
225*7c478bd9Sstevel@tonic-gate 						prev->next = new;
226*7c478bd9Sstevel@tonic-gate 						prev = get_tail(prev->next);
227*7c478bd9Sstevel@tonic-gate 					} else {
228*7c478bd9Sstevel@tonic-gate 						head = new;
229*7c478bd9Sstevel@tonic-gate 						prev = get_tail(head);
230*7c478bd9Sstevel@tonic-gate 					}
231*7c478bd9Sstevel@tonic-gate 				}
232*7c478bd9Sstevel@tonic-gate 			}
233*7c478bd9Sstevel@tonic-gate 			break;
234*7c478bd9Sstevel@tonic-gate 		default:
235*7c478bd9Sstevel@tonic-gate 			head = (execattr_t *)NULL;
236*7c478bd9Sstevel@tonic-gate 			break;
237*7c478bd9Sstevel@tonic-gate 		}
238*7c478bd9Sstevel@tonic-gate 		enduserattr();
239*7c478bd9Sstevel@tonic-gate 	} else {
240*7c478bd9Sstevel@tonic-gate 		head = userprof(username, type, id, search_flag);
241*7c478bd9Sstevel@tonic-gate 	}
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	return (head);
244*7c478bd9Sstevel@tonic-gate }
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate execattr_t *
248*7c478bd9Sstevel@tonic-gate match_execattr(execattr_t *exec, const char *profname, const char *type,
249*7c478bd9Sstevel@tonic-gate     const char *id)
250*7c478bd9Sstevel@tonic-gate {
251*7c478bd9Sstevel@tonic-gate 	execattr_t	*execp = (execattr_t *)NULL;
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 	for (execp = exec; execp != NULL; execp = execp->next) {
254*7c478bd9Sstevel@tonic-gate 		if ((profname && execp->name &&
255*7c478bd9Sstevel@tonic-gate 		    (strcmp(profname, execp->name) != 0)) ||
256*7c478bd9Sstevel@tonic-gate 		    (type && execp->type && (strcmp(type, execp->type) != 0)) ||
257*7c478bd9Sstevel@tonic-gate 		    (id && execp->id && (strcmp(id, execp->id) != 0)))
258*7c478bd9Sstevel@tonic-gate 			continue;
259*7c478bd9Sstevel@tonic-gate 	}
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate 	return (execp);
262*7c478bd9Sstevel@tonic-gate }
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate void
266*7c478bd9Sstevel@tonic-gate setexecattr()
267*7c478bd9Sstevel@tonic-gate {
268*7c478bd9Sstevel@tonic-gate 	_setexecattr();
269*7c478bd9Sstevel@tonic-gate }
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate void
273*7c478bd9Sstevel@tonic-gate endexecattr()
274*7c478bd9Sstevel@tonic-gate {
275*7c478bd9Sstevel@tonic-gate 	_endexecattr();
276*7c478bd9Sstevel@tonic-gate }
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate void
280*7c478bd9Sstevel@tonic-gate free_execattr(execattr_t *exec)
281*7c478bd9Sstevel@tonic-gate {
282*7c478bd9Sstevel@tonic-gate 	if (exec != (execattr_t *)NULL) {
283*7c478bd9Sstevel@tonic-gate 		free(exec->name);
284*7c478bd9Sstevel@tonic-gate 		free(exec->type);
285*7c478bd9Sstevel@tonic-gate 		free(exec->policy);
286*7c478bd9Sstevel@tonic-gate 		free(exec->res1);
287*7c478bd9Sstevel@tonic-gate 		free(exec->res2);
288*7c478bd9Sstevel@tonic-gate 		free(exec->id);
289*7c478bd9Sstevel@tonic-gate 		_kva_free(exec->attr);
290*7c478bd9Sstevel@tonic-gate 		free_execattr(exec->next);
291*7c478bd9Sstevel@tonic-gate 		free(exec);
292*7c478bd9Sstevel@tonic-gate 	}
293*7c478bd9Sstevel@tonic-gate }
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate static execattr_t *
297*7c478bd9Sstevel@tonic-gate userprof(const char *username, const char *type, const char *id,
298*7c478bd9Sstevel@tonic-gate     int search_flag)
299*7c478bd9Sstevel@tonic-gate {
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 	int		err = 0;
302*7c478bd9Sstevel@tonic-gate 	char		*last;
303*7c478bd9Sstevel@tonic-gate 	char		*sep = ",";
304*7c478bd9Sstevel@tonic-gate 	char		*proflist = (char *)NULL;
305*7c478bd9Sstevel@tonic-gate 	char		*profname = (char *)NULL;
306*7c478bd9Sstevel@tonic-gate 	char		buf[NSS_BUFLEN_USERATTR];
307*7c478bd9Sstevel@tonic-gate 	char		pwdb[NSS_BUFLEN_PASSWD];
308*7c478bd9Sstevel@tonic-gate 	kva_t		*user_attr;
309*7c478bd9Sstevel@tonic-gate 	userstr_t	user;
310*7c478bd9Sstevel@tonic-gate 	userstr_t	*utmp;
311*7c478bd9Sstevel@tonic-gate 	execattr_t	*exec;
312*7c478bd9Sstevel@tonic-gate 	execattr_t	*head = (execattr_t *)NULL;
313*7c478bd9Sstevel@tonic-gate 	execattr_t	*prev = (execattr_t *)NULL;
314*7c478bd9Sstevel@tonic-gate 	struct passwd	pwd;
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	char		*profArray[MAXPROFS];
317*7c478bd9Sstevel@tonic-gate 	int		profcnt = 0;
318*7c478bd9Sstevel@tonic-gate 	int		i;
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 	/*
321*7c478bd9Sstevel@tonic-gate 	 * Check if specified username is valid user
322*7c478bd9Sstevel@tonic-gate 	 */
323*7c478bd9Sstevel@tonic-gate 	if (getpwnam_r(username, &pwd, pwdb, sizeof (pwdb)) == NULL) {
324*7c478bd9Sstevel@tonic-gate 		return (head);
325*7c478bd9Sstevel@tonic-gate 	}
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 	utmp = _getusernam(username, &user, buf, NSS_BUFLEN_USERATTR, &err);
328*7c478bd9Sstevel@tonic-gate 	if (utmp != NULL) {
329*7c478bd9Sstevel@tonic-gate 		proflist = NULL;
330*7c478bd9Sstevel@tonic-gate 		user_attr = _str2kva(user.attr, KV_ASSIGN, KV_DELIMITER);
331*7c478bd9Sstevel@tonic-gate 		if ((proflist = kva_match(user_attr, "profiles")) != NULL) {
332*7c478bd9Sstevel@tonic-gate 			/* Get the list of profiles for this user */
333*7c478bd9Sstevel@tonic-gate 			for (profname = _strtok_escape(proflist, sep, &last);
334*7c478bd9Sstevel@tonic-gate 			    profname != NULL;
335*7c478bd9Sstevel@tonic-gate 			    profname = _strtok_escape(NULL, sep, &last)) {
336*7c478bd9Sstevel@tonic-gate 				getproflist(profname, profArray, &profcnt);
337*7c478bd9Sstevel@tonic-gate 			}
338*7c478bd9Sstevel@tonic-gate 		}
339*7c478bd9Sstevel@tonic-gate 	}
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	/* Get the list of default profiles */
342*7c478bd9Sstevel@tonic-gate 	if (defopen(AUTH_POLICY) == NULL) {
343*7c478bd9Sstevel@tonic-gate 		proflist = defread(DEF_PROF);
344*7c478bd9Sstevel@tonic-gate 		(void) defopen(NULL);
345*7c478bd9Sstevel@tonic-gate 	}
346*7c478bd9Sstevel@tonic-gate 	if (proflist != NULL) {
347*7c478bd9Sstevel@tonic-gate 		for (profname = _strtok_escape(proflist, sep, &last);
348*7c478bd9Sstevel@tonic-gate 		    profname != NULL;
349*7c478bd9Sstevel@tonic-gate 		    profname = _strtok_escape(NULL, sep, &last)) {
350*7c478bd9Sstevel@tonic-gate 			getproflist(profname, profArray, &profcnt);
351*7c478bd9Sstevel@tonic-gate 		}
352*7c478bd9Sstevel@tonic-gate 	}
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 	if (profcnt == 0) {
355*7c478bd9Sstevel@tonic-gate 		return (head);
356*7c478bd9Sstevel@tonic-gate 	}
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 	/* Get execs from the list of profiles */
359*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < profcnt; i++) {
360*7c478bd9Sstevel@tonic-gate 		profname = profArray[i];
361*7c478bd9Sstevel@tonic-gate 		if ((exec = getexecprof(profname, type, id, search_flag)) !=
362*7c478bd9Sstevel@tonic-gate 		    NULL) {
363*7c478bd9Sstevel@tonic-gate 			if (search_flag == GET_ONE) {
364*7c478bd9Sstevel@tonic-gate 				head = exec;
365*7c478bd9Sstevel@tonic-gate 				break;
366*7c478bd9Sstevel@tonic-gate 			} else if (search_flag == GET_ALL) {
367*7c478bd9Sstevel@tonic-gate 				if (head == NULL) {
368*7c478bd9Sstevel@tonic-gate 					head = exec;
369*7c478bd9Sstevel@tonic-gate 					prev = get_tail(head);
370*7c478bd9Sstevel@tonic-gate 				} else {
371*7c478bd9Sstevel@tonic-gate 					prev->next = exec;
372*7c478bd9Sstevel@tonic-gate 					prev = get_tail(exec);
373*7c478bd9Sstevel@tonic-gate 				}
374*7c478bd9Sstevel@tonic-gate 			}
375*7c478bd9Sstevel@tonic-gate 		}
376*7c478bd9Sstevel@tonic-gate 	}
377*7c478bd9Sstevel@tonic-gate 	free_proflist(profArray, profcnt);
378*7c478bd9Sstevel@tonic-gate 	return (head);
379*7c478bd9Sstevel@tonic-gate }
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate static execattr_t *
383*7c478bd9Sstevel@tonic-gate get_tail(execattr_t *exec)
384*7c478bd9Sstevel@tonic-gate {
385*7c478bd9Sstevel@tonic-gate 	execattr_t *i_exec = (execattr_t *)NULL;
386*7c478bd9Sstevel@tonic-gate 	execattr_t *j_exec = (execattr_t *)NULL;
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 	if (exec != NULL) {
389*7c478bd9Sstevel@tonic-gate 		if (exec->next == NULL) {
390*7c478bd9Sstevel@tonic-gate 			j_exec = exec;
391*7c478bd9Sstevel@tonic-gate 		} else {
392*7c478bd9Sstevel@tonic-gate 			for (i_exec = exec->next; i_exec != NULL;
393*7c478bd9Sstevel@tonic-gate 			    i_exec = i_exec->next) {
394*7c478bd9Sstevel@tonic-gate 				j_exec = i_exec;
395*7c478bd9Sstevel@tonic-gate 			}
396*7c478bd9Sstevel@tonic-gate 		}
397*7c478bd9Sstevel@tonic-gate 	}
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 	return (j_exec);
400*7c478bd9Sstevel@tonic-gate }
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate static execattr_t *
404*7c478bd9Sstevel@tonic-gate execstr2attr(execstr_t *es)
405*7c478bd9Sstevel@tonic-gate {
406*7c478bd9Sstevel@tonic-gate 	execattr_t	*newexec;
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 	if (es == NULL) {
409*7c478bd9Sstevel@tonic-gate 		return ((execattr_t *)NULL);
410*7c478bd9Sstevel@tonic-gate 	}
411*7c478bd9Sstevel@tonic-gate 	if ((newexec = (execattr_t *)malloc(sizeof (execattr_t))) == NULL) {
412*7c478bd9Sstevel@tonic-gate 		return ((execattr_t *)NULL);
413*7c478bd9Sstevel@tonic-gate 	}
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	newexec->name = _do_unescape(es->name);
416*7c478bd9Sstevel@tonic-gate 	newexec->policy = _do_unescape(es->policy);
417*7c478bd9Sstevel@tonic-gate 	newexec->type = _do_unescape(es->type);
418*7c478bd9Sstevel@tonic-gate 	newexec->res1 =  _do_unescape(es->res1);
419*7c478bd9Sstevel@tonic-gate 	newexec->res2 = _do_unescape(es->res2);
420*7c478bd9Sstevel@tonic-gate 	newexec->id = _do_unescape(es->id);
421*7c478bd9Sstevel@tonic-gate 	newexec->attr = _str2kva(es->attr, KV_ASSIGN, KV_DELIMITER);
422*7c478bd9Sstevel@tonic-gate 	if (es->next) {
423*7c478bd9Sstevel@tonic-gate 		newexec->next = execstr2attr((execstr_t *)(es->next));
424*7c478bd9Sstevel@tonic-gate 	} else {
425*7c478bd9Sstevel@tonic-gate 		newexec->next = (execattr_t *)NULL;
426*7c478bd9Sstevel@tonic-gate 	}
427*7c478bd9Sstevel@tonic-gate 	return (newexec);
428*7c478bd9Sstevel@tonic-gate }
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate 
431*7c478bd9Sstevel@tonic-gate static execstr_t *
432*7c478bd9Sstevel@tonic-gate process_getexec(
433*7c478bd9Sstevel@tonic-gate 	execstr_t *result,
434*7c478bd9Sstevel@tonic-gate 	char *buffer,
435*7c478bd9Sstevel@tonic-gate 	int buflen,
436*7c478bd9Sstevel@tonic-gate 	nsc_data_t *sptr)
437*7c478bd9Sstevel@tonic-gate {
438*7c478bd9Sstevel@tonic-gate 	char *fixed;
439*7c478bd9Sstevel@tonic-gate #ifdef	_LP64
440*7c478bd9Sstevel@tonic-gate 	execstr_t exec64;
441*7c478bd9Sstevel@tonic-gate 
442*7c478bd9Sstevel@tonic-gate 	fixed = (char *)(((uintptr_t)buffer + 7) & ~7);
443*7c478bd9Sstevel@tonic-gate #else
444*7c478bd9Sstevel@tonic-gate 	fixed = (char *)(((uintptr_t)buffer + 3) & ~3);
445*7c478bd9Sstevel@tonic-gate #endif
446*7c478bd9Sstevel@tonic-gate 	buflen -= fixed - buffer;
447*7c478bd9Sstevel@tonic-gate 	buffer = fixed;
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate 	if (sptr->nsc_ret.nsc_return_code != SUCCESS)
450*7c478bd9Sstevel@tonic-gate 		return ((execstr_t *)NULL);
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate #ifdef	_LP64
453*7c478bd9Sstevel@tonic-gate 	if (sptr->nsc_ret.nsc_bufferbytesused - (int)sizeof (execstr32_t)
454*7c478bd9Sstevel@tonic-gate 	    > buflen)
455*7c478bd9Sstevel@tonic-gate #else
456*7c478bd9Sstevel@tonic-gate 	if (sptr->nsc_ret.nsc_bufferbytesused - (int)sizeof (execstr_t)
457*7c478bd9Sstevel@tonic-gate 	    > buflen)
458*7c478bd9Sstevel@tonic-gate #endif
459*7c478bd9Sstevel@tonic-gate 	{
460*7c478bd9Sstevel@tonic-gate 		errno = ERANGE;
461*7c478bd9Sstevel@tonic-gate 		return ((execstr_t *)NULL);
462*7c478bd9Sstevel@tonic-gate 	}
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate #ifdef	_LP64
465*7c478bd9Sstevel@tonic-gate 	(void) memcpy(buffer, (sptr->nsc_ret.nsc_u.buff + sizeof (execstr32_t)),
466*7c478bd9Sstevel@tonic-gate 	    (sptr->nsc_ret.nsc_bufferbytesused - sizeof (execstr32_t)));
467*7c478bd9Sstevel@tonic-gate 	exec64.name = (char *)(sptr->nsc_ret.nsc_u.exec.name +
468*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)buffer);
469*7c478bd9Sstevel@tonic-gate 	exec64.type = (char *)(sptr->nsc_ret.nsc_u.exec.type +
470*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)buffer);
471*7c478bd9Sstevel@tonic-gate 	exec64.policy = (char *)(sptr->nsc_ret.nsc_u.exec.policy +
472*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)buffer);
473*7c478bd9Sstevel@tonic-gate 	exec64.res1 = (char *)(sptr->nsc_ret.nsc_u.exec.res1 +
474*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)buffer);
475*7c478bd9Sstevel@tonic-gate 	exec64.res2 = (char *)(sptr->nsc_ret.nsc_u.exec.res2 +
476*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)buffer);
477*7c478bd9Sstevel@tonic-gate 	exec64.id = (char *)(sptr->nsc_ret.nsc_u.exec.id +
478*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)buffer);
479*7c478bd9Sstevel@tonic-gate 	exec64.attr = (char *)(sptr->nsc_ret.nsc_u.exec.attr +
480*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)buffer);
481*7c478bd9Sstevel@tonic-gate 	exec64.next = (execstr_t *)NULL;
482*7c478bd9Sstevel@tonic-gate 	*result = exec64;
483*7c478bd9Sstevel@tonic-gate #else
484*7c478bd9Sstevel@tonic-gate 	sptr->nsc_ret.nsc_u.exec.name += (uintptr_t)buffer;
485*7c478bd9Sstevel@tonic-gate 	sptr->nsc_ret.nsc_u.exec.type += (uintptr_t)buffer;
486*7c478bd9Sstevel@tonic-gate 	sptr->nsc_ret.nsc_u.exec.policy += (uintptr_t)buffer;
487*7c478bd9Sstevel@tonic-gate 	sptr->nsc_ret.nsc_u.exec.res1 += (uintptr_t)buffer;
488*7c478bd9Sstevel@tonic-gate 	sptr->nsc_ret.nsc_u.exec.res2 += (uintptr_t)buffer;
489*7c478bd9Sstevel@tonic-gate 	sptr->nsc_ret.nsc_u.exec.id += (uintptr_t)buffer;
490*7c478bd9Sstevel@tonic-gate 	sptr->nsc_ret.nsc_u.exec.attr += (uintptr_t)buffer;
491*7c478bd9Sstevel@tonic-gate 	sptr->nsc_ret.nsc_u.exec.next = (execstr_t *)NULL;
492*7c478bd9Sstevel@tonic-gate 	*result = sptr->nsc_ret.nsc_u.exec;
493*7c478bd9Sstevel@tonic-gate 	(void) memcpy(buffer, (sptr->nsc_ret.nsc_u.buff + sizeof (execstr_t)),
494*7c478bd9Sstevel@tonic-gate 	    (sptr->nsc_ret.nsc_bufferbytesused - sizeof (execstr_t)));
495*7c478bd9Sstevel@tonic-gate #endif
496*7c478bd9Sstevel@tonic-gate 	return (result);
497*7c478bd9Sstevel@tonic-gate }
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
501*7c478bd9Sstevel@tonic-gate void
502*7c478bd9Sstevel@tonic-gate print_execattr(execattr_t *exec)
503*7c478bd9Sstevel@tonic-gate {
504*7c478bd9Sstevel@tonic-gate 	extern void print_kva(kva_t *);
505*7c478bd9Sstevel@tonic-gate 	char *empty = "empty";
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate 	if (exec != NULL) {
508*7c478bd9Sstevel@tonic-gate 		printf("name=%s\n", exec->name ? exec->name : empty);
509*7c478bd9Sstevel@tonic-gate 		printf("policy=%s\n", exec->policy ? exec->policy : empty);
510*7c478bd9Sstevel@tonic-gate 		printf("type=%s\n", exec->type ? exec->type : empty);
511*7c478bd9Sstevel@tonic-gate 		printf("res1=%s\n", exec->res1 ? exec->res1 : empty);
512*7c478bd9Sstevel@tonic-gate 		printf("res2=%s\n", exec->res2 ? exec->res2 : empty);
513*7c478bd9Sstevel@tonic-gate 		printf("id=%s\n", exec->id ? exec->id : empty);
514*7c478bd9Sstevel@tonic-gate 		printf("attr=\n");
515*7c478bd9Sstevel@tonic-gate 		print_kva(exec->attr);
516*7c478bd9Sstevel@tonic-gate 		fflush(stdout);
517*7c478bd9Sstevel@tonic-gate 		if (exec->next) {
518*7c478bd9Sstevel@tonic-gate 			print_execattr(exec->next);
519*7c478bd9Sstevel@tonic-gate 		}
520*7c478bd9Sstevel@tonic-gate 	} else {
521*7c478bd9Sstevel@tonic-gate 		printf("NULL\n");
522*7c478bd9Sstevel@tonic-gate 	}
523*7c478bd9Sstevel@tonic-gate }
524*7c478bd9Sstevel@tonic-gate #endif  /* DEBUG */
525