xref: /illumos-gate/usr/src/cmd/nscd/getexec.c (revision ba2be53024c0b999e74ba9adcd7d80fec5df8c57)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Routines to handle getexec* calls in nscd
30  */
31 
32 #include <string.h>
33 #include <exec_attr.h>
34 #include "cache.h"
35 
36 static int execattr_compar(const void *, const void *);
37 static uint_t execattr_gethash(nss_XbyY_key_t *, int);
38 static void execattr_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
39 
40 #define	nam_db		ctx->nsc_db[0]
41 #define	id_db		ctx->nsc_db[1]
42 #define	nam_id_db	ctx->nsc_db[2]
43 #define	NSC_NAME_EXECATTR_BYNAME	"execattr_byname"
44 #define	NSC_NAME_EXECATTR_BYID		"execattr_byid"
45 #define	NSC_NAME_EXECATTR_BYNAMEID	"execattr_bynameid"
46 
47 void
48 exec_init_ctx(nsc_ctx_t *ctx) {
49 	ctx->dbname = NSS_DBNAM_EXECATTR;
50 	ctx->file_name = "/etc/security/exec_attr";
51 	ctx->db_count = 3;
52 	nam_db = make_cache(nsc_key_other,
53 			NSS_DBOP_EXECATTR_BYNAME,
54 			NSC_NAME_EXECATTR_BYNAME,
55 			execattr_compar,
56 			execattr_getlogstr,
57 			execattr_gethash, nsc_ht_default, -1);
58 	id_db = make_cache(nsc_key_other,
59 			NSS_DBOP_EXECATTR_BYID,
60 			NSC_NAME_EXECATTR_BYID,
61 			execattr_compar,
62 			execattr_getlogstr,
63 			execattr_gethash, nsc_ht_default, -1);
64 	nam_id_db = make_cache(nsc_key_other,
65 			NSS_DBOP_EXECATTR_BYNAMEID,
66 			NSC_NAME_EXECATTR_BYNAMEID,
67 			execattr_compar,
68 			execattr_getlogstr,
69 			execattr_gethash, nsc_ht_default, -1);
70 }
71 
72 #define	EXEC_STR_CMP(s1, s2) \
73 	if ((a = s1) == NULL) \
74 		a = z; \
75 	if ((b = s2) == NULL) \
76 		b = z; \
77 	res = strcmp(a, b); \
78 	if (res != 0) \
79 		return (res > 0 ? 1 : -1);
80 
81 static int
82 execattr_compar(const void *n1, const void *n2) {
83 	nsc_entry_t	*e1 = (nsc_entry_t *)n1;
84 	nsc_entry_t	*e2 = (nsc_entry_t *)n2;
85 	_priv_execattr	*ep1 = (_priv_execattr *)e1->key.attrp;
86 	_priv_execattr	*ep2 = (_priv_execattr *)e2->key.attrp;
87 	int		res;
88 	const char	*a, *b, *z = "";
89 
90 	/* compare name */
91 	EXEC_STR_CMP(ep1->name, ep2->name);
92 
93 	/* compare policy */
94 	EXEC_STR_CMP(ep1->policy, ep2->policy);
95 
96 	/* compare type */
97 	EXEC_STR_CMP(ep1->type, ep2->type);
98 
99 	/* compare id */
100 	EXEC_STR_CMP(ep1->id, ep2->id);
101 
102 	/* compare search flag */
103 	return (_NSC_INT_KEY_CMP(ep1->search_flag, ep2->search_flag));
104 }
105 
106 static uint_t
107 execattr_gethash(nss_XbyY_key_t *key, int htsize) {
108 	_priv_execattr	*ep = key->attrp;
109 	char		keys[1024];
110 	int		len;
111 
112 	len = snprintf(keys, sizeof (keys), "%s:%s:%s:%s:%d",
113 		ep->name ? ep->name : "", ep->type ? ep->type : "",
114 		ep->id ? ep->id : "", ep->policy ? ep->policy : "",
115 		ep->search_flag);
116 	return (db_gethash(keys, len, htsize));
117 }
118 
119 static void
120 execattr_getlogstr(char *name, char *whoami, size_t len,
121 	nss_XbyY_args_t *argp) {
122 	_priv_execattr	*ep = argp->key.attrp;
123 
124 	(void) snprintf(whoami, len,
125 		"%s [name=%s:type=%s:id=%s:policy=%s:flags=%d]",
126 		name, check_null(ep->name), check_null(ep->type),
127 		check_null(ep->id), check_null(ep->policy),
128 		ep->search_flag);
129 }
130