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
5*2830Sdjl * Common Development and Distribution License (the "License").
6*2830Sdjl * 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*2830Sdjl * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * Routines to handle getexec* calls in nscd
300Sstevel@tonic-gate */
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <exec_attr.h>
34*2830Sdjl #include "cache.h"
350Sstevel@tonic-gate
36*2830Sdjl static int execattr_compar(const void *, const void *);
37*2830Sdjl static uint_t execattr_gethash(nss_XbyY_key_t *, int);
38*2830Sdjl static void execattr_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
390Sstevel@tonic-gate
40*2830Sdjl #define nam_db ctx->nsc_db[0]
41*2830Sdjl #define id_db ctx->nsc_db[1]
42*2830Sdjl #define nam_id_db ctx->nsc_db[2]
43*2830Sdjl #define NSC_NAME_EXECATTR_BYNAME "execattr_byname"
44*2830Sdjl #define NSC_NAME_EXECATTR_BYID "execattr_byid"
45*2830Sdjl #define NSC_NAME_EXECATTR_BYNAMEID "execattr_bynameid"
460Sstevel@tonic-gate
470Sstevel@tonic-gate void
exec_init_ctx(nsc_ctx_t * ctx)48*2830Sdjl exec_init_ctx(nsc_ctx_t *ctx) {
49*2830Sdjl ctx->dbname = NSS_DBNAM_EXECATTR;
50*2830Sdjl ctx->file_name = "/etc/security/exec_attr";
51*2830Sdjl ctx->db_count = 3;
52*2830Sdjl nam_db = make_cache(nsc_key_other,
53*2830Sdjl NSS_DBOP_EXECATTR_BYNAME,
54*2830Sdjl NSC_NAME_EXECATTR_BYNAME,
55*2830Sdjl execattr_compar,
56*2830Sdjl execattr_getlogstr,
57*2830Sdjl execattr_gethash, nsc_ht_default, -1);
58*2830Sdjl id_db = make_cache(nsc_key_other,
59*2830Sdjl NSS_DBOP_EXECATTR_BYID,
60*2830Sdjl NSC_NAME_EXECATTR_BYID,
61*2830Sdjl execattr_compar,
62*2830Sdjl execattr_getlogstr,
63*2830Sdjl execattr_gethash, nsc_ht_default, -1);
64*2830Sdjl nam_id_db = make_cache(nsc_key_other,
65*2830Sdjl NSS_DBOP_EXECATTR_BYNAMEID,
66*2830Sdjl NSC_NAME_EXECATTR_BYNAMEID,
67*2830Sdjl execattr_compar,
68*2830Sdjl execattr_getlogstr,
69*2830Sdjl execattr_gethash, nsc_ht_default, -1);
70*2830Sdjl }
710Sstevel@tonic-gate
72*2830Sdjl #define EXEC_STR_CMP(s1, s2) \
73*2830Sdjl if ((a = s1) == NULL) \
74*2830Sdjl a = z; \
75*2830Sdjl if ((b = s2) == NULL) \
76*2830Sdjl b = z; \
77*2830Sdjl res = strcmp(a, b); \
78*2830Sdjl if (res != 0) \
79*2830Sdjl return (res > 0 ? 1 : -1);
800Sstevel@tonic-gate
81501Sesolom static int
execattr_compar(const void * n1,const void * n2)82*2830Sdjl execattr_compar(const void *n1, const void *n2) {
83*2830Sdjl nsc_entry_t *e1 = (nsc_entry_t *)n1;
84*2830Sdjl nsc_entry_t *e2 = (nsc_entry_t *)n2;
85*2830Sdjl _priv_execattr *ep1 = (_priv_execattr *)e1->key.attrp;
86*2830Sdjl _priv_execattr *ep2 = (_priv_execattr *)e2->key.attrp;
87*2830Sdjl int res;
88*2830Sdjl const char *a, *b, *z = "";
890Sstevel@tonic-gate
90*2830Sdjl /* compare name */
91*2830Sdjl EXEC_STR_CMP(ep1->name, ep2->name);
920Sstevel@tonic-gate
93*2830Sdjl /* compare policy */
94*2830Sdjl EXEC_STR_CMP(ep1->policy, ep2->policy);
95*2830Sdjl
96*2830Sdjl /* compare type */
97*2830Sdjl EXEC_STR_CMP(ep1->type, ep2->type);
980Sstevel@tonic-gate
99*2830Sdjl /* compare id */
100*2830Sdjl EXEC_STR_CMP(ep1->id, ep2->id);
1010Sstevel@tonic-gate
102*2830Sdjl /* compare search flag */
103*2830Sdjl return (_NSC_INT_KEY_CMP(ep1->search_flag, ep2->search_flag));
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate
106*2830Sdjl static uint_t
execattr_gethash(nss_XbyY_key_t * key,int htsize)107*2830Sdjl execattr_gethash(nss_XbyY_key_t *key, int htsize) {
108*2830Sdjl _priv_execattr *ep = key->attrp;
109*2830Sdjl char keys[1024];
110*2830Sdjl int len;
1110Sstevel@tonic-gate
112*2830Sdjl len = snprintf(keys, sizeof (keys), "%s:%s:%s:%s:%d",
113*2830Sdjl ep->name ? ep->name : "", ep->type ? ep->type : "",
114*2830Sdjl ep->id ? ep->id : "", ep->policy ? ep->policy : "",
115*2830Sdjl ep->search_flag);
116*2830Sdjl return (db_gethash(keys, len, htsize));
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate static void
execattr_getlogstr(char * name,char * whoami,size_t len,nss_XbyY_args_t * argp)120*2830Sdjl execattr_getlogstr(char *name, char *whoami, size_t len,
121*2830Sdjl nss_XbyY_args_t *argp) {
122*2830Sdjl _priv_execattr *ep = argp->key.attrp;
1230Sstevel@tonic-gate
124*2830Sdjl (void) snprintf(whoami, len,
125*2830Sdjl "%s [name=%s:type=%s:id=%s:policy=%s:flags=%d]",
126*2830Sdjl name, check_null(ep->name), check_null(ep->type),
127*2830Sdjl check_null(ep->id), check_null(ep->policy),
128*2830Sdjl ep->search_flag);
1290Sstevel@tonic-gate }
130