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