xref: /titanic_51/usr/src/cmd/nscd/getexec.c (revision cb5caa98562cf06753163f558cbcfe30b8f4673a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6*cb5caa98Sdjl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*cb5caa98Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Routines to handle getexec* calls in nscd
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <exec_attr.h>
34*cb5caa98Sdjl #include "cache.h"
357c478bd9Sstevel@tonic-gate 
36*cb5caa98Sdjl static int execattr_compar(const void *, const void *);
37*cb5caa98Sdjl static uint_t execattr_gethash(nss_XbyY_key_t *, int);
38*cb5caa98Sdjl static void execattr_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
397c478bd9Sstevel@tonic-gate 
40*cb5caa98Sdjl #define	nam_db		ctx->nsc_db[0]
41*cb5caa98Sdjl #define	id_db		ctx->nsc_db[1]
42*cb5caa98Sdjl #define	nam_id_db	ctx->nsc_db[2]
43*cb5caa98Sdjl #define	NSC_NAME_EXECATTR_BYNAME	"execattr_byname"
44*cb5caa98Sdjl #define	NSC_NAME_EXECATTR_BYID		"execattr_byid"
45*cb5caa98Sdjl #define	NSC_NAME_EXECATTR_BYNAMEID	"execattr_bynameid"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate void
48*cb5caa98Sdjl exec_init_ctx(nsc_ctx_t *ctx) {
49*cb5caa98Sdjl 	ctx->dbname = NSS_DBNAM_EXECATTR;
50*cb5caa98Sdjl 	ctx->file_name = "/etc/security/exec_attr";
51*cb5caa98Sdjl 	ctx->db_count = 3;
52*cb5caa98Sdjl 	nam_db = make_cache(nsc_key_other,
53*cb5caa98Sdjl 			NSS_DBOP_EXECATTR_BYNAME,
54*cb5caa98Sdjl 			NSC_NAME_EXECATTR_BYNAME,
55*cb5caa98Sdjl 			execattr_compar,
56*cb5caa98Sdjl 			execattr_getlogstr,
57*cb5caa98Sdjl 			execattr_gethash, nsc_ht_default, -1);
58*cb5caa98Sdjl 	id_db = make_cache(nsc_key_other,
59*cb5caa98Sdjl 			NSS_DBOP_EXECATTR_BYID,
60*cb5caa98Sdjl 			NSC_NAME_EXECATTR_BYID,
61*cb5caa98Sdjl 			execattr_compar,
62*cb5caa98Sdjl 			execattr_getlogstr,
63*cb5caa98Sdjl 			execattr_gethash, nsc_ht_default, -1);
64*cb5caa98Sdjl 	nam_id_db = make_cache(nsc_key_other,
65*cb5caa98Sdjl 			NSS_DBOP_EXECATTR_BYNAMEID,
66*cb5caa98Sdjl 			NSC_NAME_EXECATTR_BYNAMEID,
67*cb5caa98Sdjl 			execattr_compar,
68*cb5caa98Sdjl 			execattr_getlogstr,
69*cb5caa98Sdjl 			execattr_gethash, nsc_ht_default, -1);
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
72*cb5caa98Sdjl #define	EXEC_STR_CMP(s1, s2) \
73*cb5caa98Sdjl 	if ((a = s1) == NULL) \
74*cb5caa98Sdjl 		a = z; \
75*cb5caa98Sdjl 	if ((b = s2) == NULL) \
76*cb5caa98Sdjl 		b = z; \
77*cb5caa98Sdjl 	res = strcmp(a, b); \
78*cb5caa98Sdjl 	if (res != 0) \
79*cb5caa98Sdjl 		return (res > 0 ? 1 : -1);
807c478bd9Sstevel@tonic-gate 
81f166393fSesolom static int
82*cb5caa98Sdjl execattr_compar(const void *n1, const void *n2) {
83*cb5caa98Sdjl 	nsc_entry_t	*e1 = (nsc_entry_t *)n1;
84*cb5caa98Sdjl 	nsc_entry_t	*e2 = (nsc_entry_t *)n2;
85*cb5caa98Sdjl 	_priv_execattr	*ep1 = (_priv_execattr *)e1->key.attrp;
86*cb5caa98Sdjl 	_priv_execattr	*ep2 = (_priv_execattr *)e2->key.attrp;
87*cb5caa98Sdjl 	int		res;
88*cb5caa98Sdjl 	const char	*a, *b, *z = "";
897c478bd9Sstevel@tonic-gate 
90*cb5caa98Sdjl 	/* compare name */
91*cb5caa98Sdjl 	EXEC_STR_CMP(ep1->name, ep2->name);
927c478bd9Sstevel@tonic-gate 
93*cb5caa98Sdjl 	/* compare policy */
94*cb5caa98Sdjl 	EXEC_STR_CMP(ep1->policy, ep2->policy);
957c478bd9Sstevel@tonic-gate 
96*cb5caa98Sdjl 	/* compare type */
97*cb5caa98Sdjl 	EXEC_STR_CMP(ep1->type, ep2->type);
987c478bd9Sstevel@tonic-gate 
99*cb5caa98Sdjl 	/* compare id */
100*cb5caa98Sdjl 	EXEC_STR_CMP(ep1->id, ep2->id);
1017c478bd9Sstevel@tonic-gate 
102*cb5caa98Sdjl 	/* compare search flag */
103*cb5caa98Sdjl 	return (_NSC_INT_KEY_CMP(ep1->search_flag, ep2->search_flag));
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
106*cb5caa98Sdjl static uint_t
107*cb5caa98Sdjl execattr_gethash(nss_XbyY_key_t *key, int htsize) {
108*cb5caa98Sdjl 	_priv_execattr	*ep = key->attrp;
109*cb5caa98Sdjl 	char		keys[1024];
110*cb5caa98Sdjl 	int		len;
1117c478bd9Sstevel@tonic-gate 
112*cb5caa98Sdjl 	len = snprintf(keys, sizeof (keys), "%s:%s:%s:%s:%d",
113*cb5caa98Sdjl 		ep->name ? ep->name : "", ep->type ? ep->type : "",
114*cb5caa98Sdjl 		ep->id ? ep->id : "", ep->policy ? ep->policy : "",
115*cb5caa98Sdjl 		ep->search_flag);
116*cb5caa98Sdjl 	return (db_gethash(keys, len, htsize));
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate static void
120*cb5caa98Sdjl execattr_getlogstr(char *name, char *whoami, size_t len,
121*cb5caa98Sdjl 	nss_XbyY_args_t *argp) {
122*cb5caa98Sdjl 	_priv_execattr	*ep = argp->key.attrp;
1237c478bd9Sstevel@tonic-gate 
124*cb5caa98Sdjl 	(void) snprintf(whoami, len,
125*cb5caa98Sdjl 		"%s [name=%s:type=%s:id=%s:policy=%s:flags=%d]",
126*cb5caa98Sdjl 		name, check_null(ep->name), check_null(ep->type),
127*cb5caa98Sdjl 		check_null(ep->id), check_null(ep->policy),
128*cb5caa98Sdjl 		ep->search_flag);
1297c478bd9Sstevel@tonic-gate }
130