xref: /onnv-gate/usr/src/lib/libsecdb/common/getexecattr.c (revision 12273:63678502e95e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52830Sdjl  * Common Development and Distribution License (the "License").
62830Sdjl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12273SCasper.Dik@Sun.COM  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <sys/types.h>
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <string.h>
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <nss_dbdefs.h>
300Sstevel@tonic-gate #include <deflt.h>
310Sstevel@tonic-gate #include <exec_attr.h>
320Sstevel@tonic-gate #include <user_attr.h>
330Sstevel@tonic-gate #include <auth_attr.h>
340Sstevel@tonic-gate #include <prof_attr.h>
350Sstevel@tonic-gate #include <getxby_door.h>
360Sstevel@tonic-gate #include <sys/mman.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /* Externs from libnsl */
400Sstevel@tonic-gate extern execstr_t *_getexecattr(execstr_t *, char *, int, int *);
410Sstevel@tonic-gate extern void _setexecattr(void);
420Sstevel@tonic-gate extern void _endexecattr(void);
430Sstevel@tonic-gate extern execstr_t *_getexecprof(const char *, const char *, const char *, int,
440Sstevel@tonic-gate     execstr_t *, char *, int, int *);
450Sstevel@tonic-gate extern userstr_t *_getusernam(const char *, userstr_t *, char *, int, int *);
460Sstevel@tonic-gate extern userstr_t *_getuserattr(userstr_t *, char *, int, int *);
470Sstevel@tonic-gate extern char *_strtok_escape(char *, char *, char **);
480Sstevel@tonic-gate extern char *_strdup_null(char *);
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static execattr_t *userprof(const char *, const char *, const char *, int);
510Sstevel@tonic-gate static execattr_t *get_tail(execattr_t *);
520Sstevel@tonic-gate static execattr_t *execstr2attr(execstr_t *);
530Sstevel@tonic-gate 
540Sstevel@tonic-gate execattr_t *
getexecattr()550Sstevel@tonic-gate getexecattr()
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	int		err = 0;
580Sstevel@tonic-gate 	char		buf[NSS_BUFLEN_EXECATTR];
590Sstevel@tonic-gate 	execstr_t	exec;
600Sstevel@tonic-gate 	execstr_t	*tmp;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	tmp = _getexecattr(&exec, buf, NSS_BUFLEN_EXECATTR, &err);
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	return (execstr2attr(tmp));
650Sstevel@tonic-gate }
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 
680Sstevel@tonic-gate execattr_t *
getexecprof(const char * name,const char * type,const char * id,int search_flag)690Sstevel@tonic-gate getexecprof(const char *name, const char *type, const char *id, int search_flag)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate 	int		err = 0;
720Sstevel@tonic-gate 	char		unique[NSS_BUFLEN_EXECATTR];
730Sstevel@tonic-gate 	char		buf[NSS_BUFLEN_EXECATTR];
746059Sgww 	execattr_t	*head = NULL;
756059Sgww 	execattr_t	*prev = NULL;
760Sstevel@tonic-gate 	execstr_t	exec;
770Sstevel@tonic-gate 	execstr_t	*tmp;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	(void) memset(unique, 0, NSS_BUFLEN_EXECATTR);
800Sstevel@tonic-gate 	(void) memset(&exec, 0, sizeof (execstr_t));
810Sstevel@tonic-gate 
8210020SJoep.Vesseur@Sun.COM 	if (!IS_GET_ONE(search_flag) && !IS_GET_ALL(search_flag)) {
836059Sgww 		return (NULL);
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if ((name == NULL) && (type == NULL) && (id == NULL)) {
870Sstevel@tonic-gate 		setexecattr();
8810020SJoep.Vesseur@Sun.COM 		if (IS_GET_ONE(search_flag)) {
890Sstevel@tonic-gate 			head = getexecattr();
9010020SJoep.Vesseur@Sun.COM 		} else if (IS_GET_ALL(search_flag)) {
910Sstevel@tonic-gate 			head = getexecattr();
920Sstevel@tonic-gate 			prev = head;
930Sstevel@tonic-gate 			while (prev != NULL) {
940Sstevel@tonic-gate 				prev->next = getexecattr();
950Sstevel@tonic-gate 				prev = prev->next;
960Sstevel@tonic-gate 			};
9710020SJoep.Vesseur@Sun.COM 		} else {
986059Sgww 			head = NULL;
990Sstevel@tonic-gate 		}
1000Sstevel@tonic-gate 		endexecattr();
1010Sstevel@tonic-gate 		return (head);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	tmp = _getexecprof(name,
1050Sstevel@tonic-gate 	    type,
1060Sstevel@tonic-gate 	    id,
1070Sstevel@tonic-gate 	    search_flag,
1080Sstevel@tonic-gate 	    &exec,
1090Sstevel@tonic-gate 	    buf,
1100Sstevel@tonic-gate 	    NSS_BUFLEN_EXECATTR,
1110Sstevel@tonic-gate 	    &err);
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	return (execstr2attr(tmp));
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate execattr_t *
getexecuser(const char * username,const char * type,const char * id,int search_flag)1170Sstevel@tonic-gate getexecuser(const char *username, const char *type, const char *id,
1180Sstevel@tonic-gate     int search_flag)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	int		err = 0;
1210Sstevel@tonic-gate 	char		buf[NSS_BUFLEN_USERATTR];
1220Sstevel@tonic-gate 	userstr_t	user;
1230Sstevel@tonic-gate 	userstr_t	*utmp;
1246059Sgww 	execattr_t	*head = NULL;
1256059Sgww 	execattr_t	*prev =  NULL;
1266059Sgww 	execattr_t	*new = NULL;
1270Sstevel@tonic-gate 
12810020SJoep.Vesseur@Sun.COM 	if (!IS_GET_ONE(search_flag) && !IS_GET_ALL(search_flag)) {
1296059Sgww 		return (NULL);
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if (username == NULL) {
1330Sstevel@tonic-gate 		setuserattr();
1340Sstevel@tonic-gate 		/* avoid userstr2attr mallocs by calling libnsl directly */
1350Sstevel@tonic-gate 		utmp = _getuserattr(&user, buf, NSS_BUFLEN_USERATTR, &err);
1360Sstevel@tonic-gate 		if (utmp == NULL) {
1370Sstevel@tonic-gate 			return (head);
1380Sstevel@tonic-gate 		}
13910020SJoep.Vesseur@Sun.COM 		if (IS_GET_ONE(search_flag)) {
1400Sstevel@tonic-gate 			head = userprof((const char *)(utmp->name), type, id,
1410Sstevel@tonic-gate 			    search_flag);
14210020SJoep.Vesseur@Sun.COM 		} else if (IS_GET_ALL(search_flag)) {
1430Sstevel@tonic-gate 			head = userprof((const char *)(utmp->name), type, id,
1440Sstevel@tonic-gate 			    search_flag);
1450Sstevel@tonic-gate 			if (head != NULL) {
1460Sstevel@tonic-gate 				prev = get_tail(head);
1470Sstevel@tonic-gate 			}
1480Sstevel@tonic-gate 			while ((utmp = _getuserattr(&user,
1496059Sgww 			    buf, NSS_BUFLEN_USERATTR, &err)) != NULL) {
1500Sstevel@tonic-gate 				if ((new =
1510Sstevel@tonic-gate 				    userprof((const char *)(utmp->name),
1520Sstevel@tonic-gate 				    type, id, search_flag)) != NULL) {
1530Sstevel@tonic-gate 					if (prev != NULL) {
1540Sstevel@tonic-gate 						prev->next = new;
1550Sstevel@tonic-gate 						prev = get_tail(prev->next);
1560Sstevel@tonic-gate 					} else {
1570Sstevel@tonic-gate 						head = new;
1580Sstevel@tonic-gate 						prev = get_tail(head);
1590Sstevel@tonic-gate 					}
1600Sstevel@tonic-gate 				}
1610Sstevel@tonic-gate 			}
16210020SJoep.Vesseur@Sun.COM 		} else {
1636059Sgww 			head = NULL;
1640Sstevel@tonic-gate 		}
1650Sstevel@tonic-gate 		enduserattr();
1660Sstevel@tonic-gate 	} else {
1670Sstevel@tonic-gate 		head = userprof(username, type, id, search_flag);
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	return (head);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate execattr_t *
match_execattr(execattr_t * exec,const char * profname,const char * type,const char * id)1750Sstevel@tonic-gate match_execattr(execattr_t *exec, const char *profname, const char *type,
1760Sstevel@tonic-gate     const char *id)
1770Sstevel@tonic-gate {
1786059Sgww 	execattr_t	*execp = NULL;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	for (execp = exec; execp != NULL; execp = execp->next) {
1810Sstevel@tonic-gate 		if ((profname && execp->name &&
1820Sstevel@tonic-gate 		    (strcmp(profname, execp->name) != 0)) ||
1830Sstevel@tonic-gate 		    (type && execp->type && (strcmp(type, execp->type) != 0)) ||
1840Sstevel@tonic-gate 		    (id && execp->id && (strcmp(id, execp->id) != 0)))
1850Sstevel@tonic-gate 			continue;
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	return (execp);
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate void
setexecattr()1930Sstevel@tonic-gate setexecattr()
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate 	_setexecattr();
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate void
endexecattr()2000Sstevel@tonic-gate endexecattr()
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate 	_endexecattr();
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate void
free_execattr(execattr_t * exec)2070Sstevel@tonic-gate free_execattr(execattr_t *exec)
2080Sstevel@tonic-gate {
2096059Sgww 	if (exec != NULL) {
2100Sstevel@tonic-gate 		free(exec->name);
2110Sstevel@tonic-gate 		free(exec->type);
2120Sstevel@tonic-gate 		free(exec->policy);
2130Sstevel@tonic-gate 		free(exec->res1);
2140Sstevel@tonic-gate 		free(exec->res2);
2150Sstevel@tonic-gate 		free(exec->id);
2160Sstevel@tonic-gate 		_kva_free(exec->attr);
2170Sstevel@tonic-gate 		free_execattr(exec->next);
2180Sstevel@tonic-gate 		free(exec);
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
222*12273SCasper.Dik@Sun.COM typedef struct call {
223*12273SCasper.Dik@Sun.COM 	const char	*type;
224*12273SCasper.Dik@Sun.COM 	const char	*id;
225*12273SCasper.Dik@Sun.COM 	int		sflag;
226*12273SCasper.Dik@Sun.COM } call;
227*12273SCasper.Dik@Sun.COM 
228*12273SCasper.Dik@Sun.COM typedef struct result {
229*12273SCasper.Dik@Sun.COM 	execattr_t *head;
230*12273SCasper.Dik@Sun.COM 	execattr_t *prev;
231*12273SCasper.Dik@Sun.COM } result;
232*12273SCasper.Dik@Sun.COM 
233*12273SCasper.Dik@Sun.COM /*ARGSUSED*/
234*12273SCasper.Dik@Sun.COM static int
findexecattr(const char * prof,kva_t * kva,void * ctxt,void * res)235*12273SCasper.Dik@Sun.COM findexecattr(const char *prof, kva_t *kva, void *ctxt, void *res)
236*12273SCasper.Dik@Sun.COM {
237*12273SCasper.Dik@Sun.COM 	execattr_t *exec;
238*12273SCasper.Dik@Sun.COM 	call *c = ctxt;
239*12273SCasper.Dik@Sun.COM 	result *r = res;
240*12273SCasper.Dik@Sun.COM 
241*12273SCasper.Dik@Sun.COM 	if ((exec = getexecprof(prof, c->type, c->id, c->sflag)) != NULL) {
242*12273SCasper.Dik@Sun.COM 		if (IS_GET_ONE(c->sflag)) {
243*12273SCasper.Dik@Sun.COM 			r->head = exec;
244*12273SCasper.Dik@Sun.COM 			return (1);
245*12273SCasper.Dik@Sun.COM 		} else if (IS_GET_ALL(c->sflag)) {
246*12273SCasper.Dik@Sun.COM 			if (r->head == NULL) {
247*12273SCasper.Dik@Sun.COM 				r->head = exec;
248*12273SCasper.Dik@Sun.COM 				r->prev = get_tail(r->head);
249*12273SCasper.Dik@Sun.COM 			} else {
250*12273SCasper.Dik@Sun.COM 				r->prev->next = exec;
251*12273SCasper.Dik@Sun.COM 				r->prev = get_tail(exec);
252*12273SCasper.Dik@Sun.COM 			}
253*12273SCasper.Dik@Sun.COM 		}
254*12273SCasper.Dik@Sun.COM 	}
255*12273SCasper.Dik@Sun.COM 	return (0);
256*12273SCasper.Dik@Sun.COM }
257*12273SCasper.Dik@Sun.COM 
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate static execattr_t *
userprof(const char * username,const char * type,const char * id,int search_flag)2600Sstevel@tonic-gate userprof(const char *username, const char *type, const char *id,
2610Sstevel@tonic-gate     int search_flag)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	char		pwdb[NSS_BUFLEN_PASSWD];
2650Sstevel@tonic-gate 	struct passwd	pwd;
266*12273SCasper.Dik@Sun.COM 	call		call;
267*12273SCasper.Dik@Sun.COM 	result		result;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	/*
2700Sstevel@tonic-gate 	 * Check if specified username is valid user
2710Sstevel@tonic-gate 	 */
2720Sstevel@tonic-gate 	if (getpwnam_r(username, &pwd, pwdb, sizeof (pwdb)) == NULL) {
273*12273SCasper.Dik@Sun.COM 		return (NULL);
2740Sstevel@tonic-gate 	}
2750Sstevel@tonic-gate 
276*12273SCasper.Dik@Sun.COM 	result.head = result.prev = NULL;
277*12273SCasper.Dik@Sun.COM 	call.type = type;
278*12273SCasper.Dik@Sun.COM 	call.id = id;
279*12273SCasper.Dik@Sun.COM 	call.sflag = search_flag;
2800Sstevel@tonic-gate 
281*12273SCasper.Dik@Sun.COM 	(void) _enum_profs(username, findexecattr, &call, &result);
282*12273SCasper.Dik@Sun.COM 
283*12273SCasper.Dik@Sun.COM 	return (result.head);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate static execattr_t *
get_tail(execattr_t * exec)2880Sstevel@tonic-gate get_tail(execattr_t *exec)
2890Sstevel@tonic-gate {
2906059Sgww 	execattr_t *i_exec = NULL;
2916059Sgww 	execattr_t *j_exec = NULL;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	if (exec != NULL) {
2940Sstevel@tonic-gate 		if (exec->next == NULL) {
2950Sstevel@tonic-gate 			j_exec = exec;
2960Sstevel@tonic-gate 		} else {
2970Sstevel@tonic-gate 			for (i_exec = exec->next; i_exec != NULL;
2980Sstevel@tonic-gate 			    i_exec = i_exec->next) {
2990Sstevel@tonic-gate 				j_exec = i_exec;
3000Sstevel@tonic-gate 			}
3010Sstevel@tonic-gate 		}
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	return (j_exec);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate static execattr_t *
execstr2attr(execstr_t * es)3090Sstevel@tonic-gate execstr2attr(execstr_t *es)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	execattr_t	*newexec;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	if (es == NULL) {
3146059Sgww 		return (NULL);
3150Sstevel@tonic-gate 	}
3166059Sgww 	if ((newexec = malloc(sizeof (execattr_t))) == NULL) {
3176059Sgww 		return (NULL);
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	newexec->name = _do_unescape(es->name);
3210Sstevel@tonic-gate 	newexec->policy = _do_unescape(es->policy);
3220Sstevel@tonic-gate 	newexec->type = _do_unescape(es->type);
3230Sstevel@tonic-gate 	newexec->res1 =  _do_unescape(es->res1);
3240Sstevel@tonic-gate 	newexec->res2 = _do_unescape(es->res2);
3250Sstevel@tonic-gate 	newexec->id = _do_unescape(es->id);
3260Sstevel@tonic-gate 	newexec->attr = _str2kva(es->attr, KV_ASSIGN, KV_DELIMITER);
3270Sstevel@tonic-gate 	if (es->next) {
3280Sstevel@tonic-gate 		newexec->next = execstr2attr((execstr_t *)(es->next));
3290Sstevel@tonic-gate 	} else {
3306059Sgww 		newexec->next = NULL;
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate 	return (newexec);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate #ifdef DEBUG
3360Sstevel@tonic-gate void
print_execattr(execattr_t * exec)3370Sstevel@tonic-gate print_execattr(execattr_t *exec)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate 	extern void print_kva(kva_t *);
3400Sstevel@tonic-gate 	char *empty = "empty";
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	if (exec != NULL) {
3430Sstevel@tonic-gate 		printf("name=%s\n", exec->name ? exec->name : empty);
3440Sstevel@tonic-gate 		printf("policy=%s\n", exec->policy ? exec->policy : empty);
3450Sstevel@tonic-gate 		printf("type=%s\n", exec->type ? exec->type : empty);
3460Sstevel@tonic-gate 		printf("res1=%s\n", exec->res1 ? exec->res1 : empty);
3470Sstevel@tonic-gate 		printf("res2=%s\n", exec->res2 ? exec->res2 : empty);
3480Sstevel@tonic-gate 		printf("id=%s\n", exec->id ? exec->id : empty);
3490Sstevel@tonic-gate 		printf("attr=\n");
3500Sstevel@tonic-gate 		print_kva(exec->attr);
3510Sstevel@tonic-gate 		fflush(stdout);
3520Sstevel@tonic-gate 		if (exec->next) {
3530Sstevel@tonic-gate 			print_execattr(exec->next);
3540Sstevel@tonic-gate 		}
3550Sstevel@tonic-gate 	} else {
3560Sstevel@tonic-gate 		printf("NULL\n");
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate #endif  /* DEBUG */
360