1 /* acl.c - routines to parse and check acl's */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
25 aci_bv_entry = { sizeof("entry") - 1, "entry" },
26 aci_bv_br_entry = { sizeof("[entry]") - 1, "[entry]" },
27 aci_bv_br_all = { sizeof("[all]") - 1, "[all]" },
28 aci_bv_access_id = { sizeof("access-id") - 1, "access-id" },
29 aci_bv_anonymous = { sizeof("anonymous") - 1, "anonymous" },
30 aci_bv_users = { sizeof("users") - 1, "users" },
31 aci_bv_self = { sizeof("self") - 1, "self" },
32 aci_bv_dnattr = { sizeof("dnattr") - 1, "dnattr" },
33 aci_bv_group = { sizeof("group") - 1, "group" },
34 aci_bv_role = { sizeof("role") - 1, "role" },
35 aci_bv_set = { sizeof("set") - 1, "set" },
36 aci_bv_set_ref = { sizeof("set-ref") - 1, "set-ref"},
37 aci_bv_grant = { sizeof("grant") - 1, "grant" },
38 aci_bv_deny = { sizeof("deny") - 1, "deny" };
40 static AccessControl * acl_get(
41 AccessControl *ac, int *count,
42 Backend *be, Operation *op,
44 AttributeDescription *desc,
45 int nmatches, regmatch_t *matches );
47 static slap_control_t acl_mask(
48 AccessControl *ac, slap_mask_t *mask,
49 Backend *be, Connection *conn, Operation *op,
51 AttributeDescription *desc,
53 regmatch_t *matches );
55 #ifdef SLAPD_ACI_ENABLED
61 AttributeDescription *desc,
66 slap_access_t *deny );
69 static int regex_matches(
70 char *pat, char *str, char *buf, regmatch_t *matches);
71 static void string_expand(
72 struct berval *newbuf, char *pattern,
73 char *match, regmatch_t *matches);
75 typedef struct AciSetCookie {
82 BerVarray aci_set_gather (void *cookie, char *name, struct berval *attr);
83 static int aci_match_set ( struct berval *subj, Backend *be,
84 Entry *e, Connection *conn, Operation *op, int setref );
87 * access_allowed - check whether op->o_ndn is allowed the requested access
88 * to entry e, attribute attr, value val. if val is null, access to
89 * the whole attribute is assumed (all values).
91 * This routine loops through all access controls and calls
92 * acl_mask() on each applicable access control.
93 * The loop exits when a definitive answer is reached or
94 * or no more controls remain.
107 AttributeDescription *desc,
109 slap_access_t access )
114 char accessmaskbuf[ACCESSMASK_MAXLEN];
117 slap_control_t control;
119 regmatch_t matches[MAXREMATCHES];
122 assert( desc != NULL );
123 assert( access > ACL_NONE );
125 attr = desc->ad_cname.bv_val;
127 assert( attr != NULL );
130 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
131 "access_allowed: conn %d %s access to \"%s\" \"%s\" requested\n",
132 conn ? conn->c_connid : -1, access2str( access ), e->e_dn, attr ));
134 Debug( LDAP_DEBUG_ACL,
135 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
136 access2str( access ), e->e_dn, attr );
144 if ( be == NULL ) be = &backends[0];
145 assert( be != NULL );
147 /* grant database root access */
148 if ( be != NULL && be_isroot( be, &op->o_ndn ) ) {
150 LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
151 "access_allowed: conn %d root access granted\n",
154 Debug( LDAP_DEBUG_ACL,
155 "<= root access granted\n",
162 * no-user-modification operational attributes are ignored
163 * by ACL_WRITE checking as any found here are not provided
166 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
167 && desc != slap_schema.si_ad_entry
168 && desc != slap_schema.si_ad_children )
171 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
172 "access_allowed: conn %d NoUserMod Operational attribute: %s access granted\n",
173 conn->c_connid, attr ));
175 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
176 " %s access granted\n",
182 /* use backend default access if no backend acls */
183 if( be != NULL && be->be_acl == NULL ) {
185 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
186 "access_allowed: conn %d backend default %s access %s to \"%s\"\n",
187 conn->c_connid, access2str( access ),
188 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn.bv_val ));
190 Debug( LDAP_DEBUG_ACL,
191 "=> access_allowed: backend default %s access %s to \"%s\"\n",
192 access2str( access ),
193 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn.bv_val );
195 return be->be_dfltaccess >= access;
198 /* be is always non-NULL */
199 /* use global default access if no global acls */
200 } else if ( be == NULL && global_acl == NULL ) {
202 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
203 "access_allowed: conn %d global default %s access %s to \"%s\"\n",
204 conn->c_connid, access2str( access ),
205 global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val ));
207 Debug( LDAP_DEBUG_ACL,
208 "=> access_allowed: global default %s access %s to \"%s\"\n",
209 access2str( access ),
210 global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val );
212 return global_default_access >= access;
217 memset(matches, '\0', sizeof(matches));
223 while((a = acl_get( a, &count, be, op, e, desc, MAXREMATCHES, matches )) != NULL)
227 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
229 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
230 "access_allowed: conn %d match[%d]: %d %d ",
231 conn->c_connid, i, (int)matches[i].rm_so, (int)matches[i].rm_eo ));
233 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
234 (int)matches[i].rm_so, (int)matches[i].rm_eo );
236 if( matches[i].rm_so <= matches[0].rm_eo ) {
238 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
239 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
243 LDAP_LOG(( "acl", LDAP_LEVEL_ARGS, "\n" ));
245 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
249 control = acl_mask( a, &mask, be, conn, op,
250 e, desc, val, matches );
252 if ( control != ACL_BREAK ) {
256 memset(matches, '\0', sizeof(matches));
259 if ( ACL_IS_INVALID( mask ) ) {
261 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
262 "access_allowed: conn %d \"%s\" (%s) invalid!\n",
263 conn->c_connid, e->e_dn, attr ));
265 Debug( LDAP_DEBUG_ACL,
266 "=> access_allowed: \"%s\" (%s) invalid!\n",
271 } else if ( control == ACL_BREAK ) {
273 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
274 "access_allowed: conn %d no more rules\n", conn->c_connid ));
276 Debug( LDAP_DEBUG_ACL,
277 "=> access_allowed: no more rules\n", 0, 0, 0);
283 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
284 "access_allowed: conn %d %s access %s by %s\n",
286 access2str( access ),
287 ACL_GRANT( mask, access ) ? "granted" : "denied",
288 accessmask2str( mask, accessmaskbuf ) ));
290 Debug( LDAP_DEBUG_ACL,
291 "=> access_allowed: %s access %s by %s\n",
292 access2str( access ),
293 ACL_GRANT(mask, access) ? "granted" : "denied",
294 accessmask2str( mask, accessmaskbuf ) );
296 return ACL_GRANT(mask, access);
300 * acl_get - return the acl applicable to entry e, attribute
301 * attr. the acl returned is suitable for use in subsequent calls to
302 * acl_access_allowed().
305 static AccessControl *
312 AttributeDescription *desc,
314 regmatch_t *matches )
320 assert( count != NULL );
321 assert( desc != NULL );
323 attr = desc->ad_cname.bv_val;
325 assert( attr != NULL );
340 dnlen = e->e_nname.bv_len;
342 for ( ; a != NULL; a = a->acl_next ) {
345 if (a->acl_dn_pat.bv_len != 0) {
346 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
348 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
349 "acl_get: dnpat [%d] %s nsub: %d\n",
350 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub ));
352 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
353 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
355 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
360 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
361 "acl_get: dn [%d] %s\n",
362 *count, a->acl_dn_pat.bv_val ));
364 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
365 *count, a->acl_dn_pat.bv_val, 0 );
367 patlen = a->acl_dn_pat.bv_len;
368 if ( dnlen < patlen )
371 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
372 /* base dn -- entire object DN must match */
373 if ( dnlen != patlen )
376 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
379 if ( dnlen <= patlen )
382 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) || DN_ESCAPE( e->e_ndn[dnlen - patlen - 2] ) )
385 rdnlen = dn_rdnlen( NULL, &e->e_nname );
386 if ( rdnlen != dnlen - patlen - 1 )
389 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
390 if ( dnlen > patlen && ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) || DN_ESCAPE( e->e_ndn[dnlen - patlen - 2] ) ) )
393 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
394 if ( dnlen <= patlen )
396 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) || DN_ESCAPE( e->e_ndn[dnlen - patlen - 2] ) )
400 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
405 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
406 "acl_get: [%d] matched\n",
409 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
414 if ( a->acl_filter != NULL ) {
415 ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
416 if ( rc != LDAP_COMPARE_TRUE ) {
422 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
423 "acl_get: [%d] check attr %s\n",
426 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
429 if ( attr == NULL || a->acl_attrs == NULL ||
430 ad_inlist( desc, a->acl_attrs ) )
433 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
434 "acl_get: [%d] acl %s attr: %s\n",
435 *count, e->e_dn, attr ));
437 Debug( LDAP_DEBUG_ACL,
438 "<= acl_get: [%d] acl %s attr: %s\n",
439 *count, e->e_dn, attr );
443 matches[0].rm_so = matches[0].rm_eo = -1;
447 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
448 "acl_get: done.\n" ));
450 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
457 * acl_mask - modifies mask based upon the given acl and the
458 * requested access to entry e, attribute attr, value val. if val
459 * is null, access to the whole attribute is assumed (all values).
461 * returns 0 access NOT allowed
465 static slap_control_t
473 AttributeDescription *desc,
478 int i, odnlen, patlen;
481 char accessmaskbuf[ACCESSMASK_MAXLEN];
486 assert( mask != NULL );
487 assert( desc != NULL );
489 attr = desc->ad_cname.bv_val;
491 assert( attr != NULL );
494 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
495 "acl_mask: conn %d access to entry \"%s\", attr \"%s\" requested\n",
496 conn->c_connid, e->e_dn, attr ));
498 LDAP_LOG(( "acl", LDAP_LEVEL_ARGS,
499 " to %s by \"%s\", (%s) \n",
500 val ? "value" : "all values",
501 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
502 accessmask2str( *mask, accessmaskbuf ) ));
504 Debug( LDAP_DEBUG_ACL,
505 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
508 Debug( LDAP_DEBUG_ACL,
509 "=> acl_mask: to %s by \"%s\", (%s) \n",
510 val ? "value" : "all values",
511 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
512 accessmask2str( *mask, accessmaskbuf ) );
515 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
516 slap_mask_t oldmask, modmask;
518 ACL_INVALIDATE( modmask );
520 /* AND <who> clauses */
521 if ( b->a_dn_pat.bv_len != 0 ) {
523 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
524 "acl_mask: conn %d check a_dn_pat: %s\n",
525 conn->c_connid, b->a_dn_pat.bv_val ));
527 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
528 b->a_dn_pat.bv_val, 0, 0);
531 * if access applies to the entry itself, and the
532 * user is bound as somebody in the same namespace as
533 * the entry, OR the given dn matches the dn pattern
535 if ( ber_bvcmp( &b->a_dn_pat, &aci_bv_anonymous ) == 0 ) {
536 if ( op->o_ndn.bv_len != 0 ) {
540 } else if ( ber_bvcmp( &b->a_dn_pat, &aci_bv_users ) == 0 ) {
541 if ( op->o_ndn.bv_len == 0 ) {
545 } else if ( ber_bvcmp( &b->a_dn_pat, &aci_bv_self ) == 0 ) {
546 if ( op->o_ndn.bv_len == 0 ) {
550 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
554 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
555 if ( ber_bvccmp( &b->a_dn_pat, '*' ) == 0 ) {
556 int ret = regex_matches( b->a_dn_pat.bv_val,
557 op->o_ndn.bv_val, e->e_ndn, matches );
565 if ( e->e_dn == NULL )
568 patlen = b->a_dn_pat.bv_len;
569 odnlen = op->o_ndn.bv_len;
570 if ( odnlen < patlen )
573 if ( b->a_dn_style == ACL_STYLE_BASE ) {
574 /* base dn -- entire object DN must match */
575 if ( odnlen != patlen )
578 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
581 if ( odnlen <= patlen )
584 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) || DN_ESCAPE( op->o_ndn.bv_val[odnlen - patlen - 2] ) )
587 rdnlen = dn_rdnlen( NULL, &op->o_ndn );
588 if ( rdnlen != odnlen - patlen - 1 )
591 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
592 if ( odnlen > patlen && ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) || DN_ESCAPE( op->o_ndn.bv_val[odnlen - patlen - 2] ) ) )
595 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
596 if ( odnlen <= patlen )
598 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) || DN_ESCAPE( op->o_ndn.bv_val[odnlen - patlen - 2] ) )
602 if ( strcmp( b->a_dn_pat.bv_val, op->o_ndn.bv_val + odnlen - patlen ) != 0 )
607 if ( b->a_sockurl_pat != NULL ) {
609 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
610 "acl_mask: conn %d check a_sockurl_pat: %s\n",
611 conn->c_connid, b->a_sockurl_pat ));
613 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
614 b->a_sockurl_pat, 0, 0 );
617 if ( ber_strccmp( b->a_sockurl_pat, '*' ) != 0) {
618 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
619 if (!regex_matches( b->a_sockurl_pat, conn->c_listener_url,
620 e->e_ndn, matches ) )
625 if ( strcasecmp( b->a_sockurl_pat, conn->c_listener_url ) != 0 )
631 if ( b->a_domain_pat != NULL ) {
633 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
634 "acl_mask: conn %d check a_domain_pat: %s\n",
635 conn->c_connid, b->a_domain_pat ));
637 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
638 b->a_domain_pat, 0, 0 );
640 if ( ber_strccmp( b->a_domain_pat, '*' ) != 0) {
641 if ( b->a_domain_style == ACL_STYLE_REGEX) {
642 if (!regex_matches( b->a_domain_pat, conn->c_peer_domain,
643 e->e_ndn, matches ) )
648 if ( strcasecmp( b->a_domain_pat, conn->c_peer_domain ) != 0 )
654 if ( b->a_peername_pat != NULL ) {
656 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
657 "acl_mask: conn %d check a_perrname_path: %s\n",
658 conn->c_connid, b->a_peername_pat ));
660 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
661 b->a_peername_pat, 0, 0 );
663 if ( ber_strccmp( b->a_peername_pat, '*' ) != 0) {
664 if ( b->a_peername_style == ACL_STYLE_REGEX) {
665 if (!regex_matches( b->a_peername_pat, conn->c_peer_name,
666 e->e_ndn, matches ) )
671 if ( strcasecmp( b->a_peername_pat, conn->c_peer_name ) != 0 )
677 if ( b->a_sockname_pat != NULL ) {
679 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
680 "acl_mask: conn %d check a_sockname_path: %s\n",
681 conn->c_connid, b->a_sockname_pat ));
683 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
684 b->a_sockname_pat, 0, 0 );
686 if ( ber_strccmp( b->a_sockname_pat, '*' ) != 0) {
687 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
688 if (!regex_matches( b->a_sockname_pat, conn->c_sock_name,
689 e->e_ndn, matches ) )
694 if ( strcasecmp( b->a_sockname_pat, conn->c_sock_name ) != 0 )
700 if ( b->a_dn_at != NULL && op->o_ndn.bv_len != 0 ) {
705 const char *attr = b->a_dn_at->ad_cname.bv_val;
707 assert( attr != NULL );
710 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
711 "acl_mask: conn %d check a_dn_pat: %s\n",
712 conn->c_connid, attr ));
714 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
719 /* see if asker is listed in dnattr */
720 for( at = attrs_find( e->e_attrs, b->a_dn_at );
722 at = attrs_find( at->a_next, b->a_dn_at ) )
724 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
732 /* have a dnattr match. if this is a self clause then
733 * the target must also match the op dn.
735 if ( b->a_dn_self ) {
736 /* check if the target is an attribute. */
739 /* target is attribute, check if the attribute value
742 rc = value_match( &match, b->a_dn_at,
743 b->a_dn_at->ad_type->sat_equality, 0,
745 /* on match error or no match, fail the ACL clause */
746 if (rc != LDAP_SUCCESS || match != 0 )
750 /* no dnattr match, check if this is a self clause */
751 if ( ! b->a_dn_self )
753 /* this is a self clause, check if the target is an
758 /* target is attribute, check if the attribute value
761 rc = value_match( &match, b->a_dn_at,
762 b->a_dn_at->ad_type->sat_equality, 0,
765 /* on match error or no match, fail the ACL clause */
766 if (rc != LDAP_SUCCESS || match != 0 )
771 if ( b->a_group_pat.bv_len && op->o_ndn.bv_len ) {
773 struct berval bv = { sizeof(buf) - 1, buf };
774 struct berval ndn = { 0, NULL };
777 /* b->a_group is an unexpanded entry name, expanded it should be an
778 * entry with objectclass group* and we test to see if odn is one of
779 * the values in the attribute group
781 /* see if asker is listed in dnattr */
782 if ( b->a_group_style == ACL_STYLE_REGEX ) {
783 string_expand(&bv, b->a_group_pat.bv_val, e->e_ndn, matches);
784 if ( dnNormalize2(NULL, &bv, &ndn) != LDAP_SUCCESS ) {
785 /* did not expand to a valid dn */
793 rc = backend_group(be, conn, op, e, &bv, &op->o_ndn,
794 b->a_group_oc, b->a_group_at);
802 if ( b->a_set_pat.bv_len != 0 ) {
803 if (aci_match_set( &b->a_set_pat, be, e, conn, op, 0 ) == 0) {
808 if ( b->a_authz.sai_ssf ) {
810 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
811 "acl_mask: conn %d check a_authz.sai_ssf: ACL %u > OP %u\n",
812 conn->c_connid, b->a_authz.sai_ssf, op->o_ssf ));
814 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
815 b->a_authz.sai_ssf, op->o_ssf, 0 );
817 if ( b->a_authz.sai_ssf > op->o_ssf ) {
822 if ( b->a_authz.sai_transport_ssf ) {
824 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
825 "acl_mask: conn %d check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
826 conn->c_connid, b->a_authz.sai_transport_ssf, op->o_transport_ssf ));
828 Debug( LDAP_DEBUG_ACL,
829 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
830 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
832 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
837 if ( b->a_authz.sai_tls_ssf ) {
839 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
840 "acl_mask: conn %d check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
841 conn->c_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf ));
843 Debug( LDAP_DEBUG_ACL,
844 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
845 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
847 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
852 if ( b->a_authz.sai_sasl_ssf ) {
854 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
855 "acl_mask: conn %d check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
856 conn->c_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf ));
858 Debug( LDAP_DEBUG_ACL,
859 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
860 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
862 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
867 #ifdef SLAPD_ACI_ENABLED
868 if ( b->a_aci_at != NULL ) {
870 slap_access_t grant, deny, tgrant, tdeny;
872 /* this case works different from the others above.
873 * since aci's themselves give permissions, we need
874 * to first check b->a_access_mask, the ACL's access level.
877 if ( e->e_nname.bv_len == 0 ) {
878 /* no ACIs in the root DSE */
882 /* first check if the right being requested
883 * is allowed by the ACL clause.
885 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
889 /* get the aci attribute */
890 at = attr_find( e->e_attrs, b->a_aci_at );
895 /* start out with nothing granted, nothing denied */
899 /* the aci is an multi-valued attribute. The
900 * rights are determined by OR'ing the individual
901 * rights given by the acis.
903 for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
904 if (aci_mask( be, conn, op,
905 e, desc, val, &at->a_vals[i],
906 matches, &grant, &deny ) != 0)
913 /* remove anything that the ACL clause does not allow */
914 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
915 tdeny &= ACL_PRIV_MASK;
917 /* see if we have anything to contribute */
918 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
922 /* this could be improved by changing acl_mask so that it can deal with
923 * by clauses that return grant/deny pairs. Right now, it does either
924 * additive or subtractive rights, but not both at the same time. So,
925 * we need to combine the grant/deny pair into a single rights mask in
926 * a smart way: if either grant or deny is "empty", then we use the
927 * opposite as is, otherwise we remove any denied rights from the grant
928 * rights mask and construct an additive mask.
930 if (ACL_IS_INVALID(tdeny)) {
931 modmask = tgrant | ACL_PRIV_ADDITIVE;
933 } else if (ACL_IS_INVALID(tgrant)) {
934 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
937 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
943 modmask = b->a_access_mask;
947 LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
948 "acl_mask: conn %d [%d] applying %s (%s)\n",
949 conn->c_connid, i, accessmask2str( modmask, accessmaskbuf),
950 b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
951 ? "break" : "stop" ));
953 Debug( LDAP_DEBUG_ACL,
954 "<= acl_mask: [%d] applying %s (%s)\n",
955 i, accessmask2str( modmask, accessmaskbuf ),
956 b->a_type == ACL_CONTINUE
958 : b->a_type == ACL_BREAK
965 if( ACL_IS_ADDITIVE(modmask) ) {
967 ACL_PRIV_SET( *mask, modmask );
970 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
972 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
973 /* substract privs */
974 ACL_PRIV_CLR( *mask, modmask );
977 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
985 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
986 "acl_mask: conn %d [%d] mask: %s\n",
987 conn->c_connid, i, accessmask2str( *mask, accessmaskbuf) ));
989 Debug( LDAP_DEBUG_ACL,
990 "<= acl_mask: [%d] mask: %s\n",
991 i, accessmask2str(*mask, accessmaskbuf), 0 );
994 if( b->a_type == ACL_CONTINUE ) {
997 } else if ( b->a_type == ACL_BREAK ) {
1005 /* implicit "by * none" clause */
1009 LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
1010 "acl_mask: conn %d no more <who> clauses, returning %d (stop)\n",
1011 conn->c_connid, accessmask2str( *mask, accessmaskbuf) ));
1013 Debug( LDAP_DEBUG_ACL,
1014 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1015 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1021 * acl_check_modlist - check access control on the given entry to see if
1022 * it allows the given modifications by the user associated with op.
1023 * returns 1 if mods allowed ok
1024 * 0 mods not allowed
1033 Modifications *mlist
1038 assert( be != NULL );
1040 /* short circuit root database access */
1041 if ( be_isroot( be, &op->o_ndn ) ) {
1043 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
1044 "acl_check_modlist: conn %d access granted to root user\n",
1047 Debug( LDAP_DEBUG_ACL,
1048 "<= acl_access_allowed: granted to database root\n",
1054 /* use backend default access if no backend acls */
1055 if( be != NULL && be->be_acl == NULL ) {
1057 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1058 "acl_check_modlist: conn %d backend default %s access %s to \"%s\"\n",
1059 conn->c_connid, access2str( ACL_WRITE ),
1060 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val ));
1062 Debug( LDAP_DEBUG_ACL,
1063 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1064 access2str( ACL_WRITE ),
1065 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1067 return be->be_dfltaccess >= ACL_WRITE;
1070 /* be is always non-NULL */
1071 /* use global default access if no global acls */
1072 } else if ( be == NULL && global_acl == NULL ) {
1074 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1075 "acl_check_modlist: conn %d global default %s access %s to \"%s\"\n",
1076 conn->c_connid, access2str( ACL_WRITE ),
1077 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn ));
1079 Debug( LDAP_DEBUG_ACL,
1080 "=> access_allowed: global default %s access %s to \"%s\"\n",
1081 access2str( ACL_WRITE ),
1082 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1084 return global_default_access >= ACL_WRITE;
1088 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1090 * no-user-modification operational attributes are ignored
1091 * by ACL_WRITE checking as any found here are not provided
1094 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1096 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1097 "acl_check_modlist: conn %d no-user-mod %s: modify access granted\n",
1098 conn->c_connid, mlist->sml_desc->ad_cname.bv_val ));
1100 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1101 " modify access granted\n",
1102 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1107 switch ( mlist->sml_op ) {
1108 case LDAP_MOD_REPLACE:
1110 * We must check both permission to delete the whole
1111 * attribute and permission to add the specific attributes.
1112 * This prevents abuse from selfwriters.
1114 if ( ! access_allowed( be, conn, op, e,
1115 mlist->sml_desc, NULL, ACL_WRITE ) )
1120 if ( mlist->sml_bvalues == NULL ) break;
1122 /* fall thru to check value to add */
1125 assert( mlist->sml_bvalues != NULL );
1127 for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ ) {
1128 if ( ! access_allowed( be, conn, op, e,
1129 mlist->sml_desc, bv, ACL_WRITE ) )
1136 case LDAP_MOD_DELETE:
1137 if ( mlist->sml_bvalues == NULL ) {
1138 if ( ! access_allowed( be, conn, op, e,
1139 mlist->sml_desc, NULL, ACL_WRITE ) )
1145 for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ ) {
1146 if ( ! access_allowed( be, conn, op, e,
1147 mlist->sml_desc, bv, ACL_WRITE ) )
1164 aci_bvstrdup( struct berval *bv )
1168 s = (char *)ch_malloc(bv->bv_len + 1);
1170 AC_MEMCPY(s, bv->bv_val, bv->bv_len);
1176 #ifdef SLAPD_ACI_ENABLED
1184 res = strncasecmp( s, bv->bv_val, bv->bv_len );
1188 if (len > (int)bv->bv_len)
1190 if (len < (int)bv->bv_len)
1198 struct berval *list,
1212 while (len >= 0 && --ix >= 0) {
1213 while (--len >= 0 && *p++ != sep) ;
1215 while (len >= 0 && *p == ' ') {
1226 while (--len >= 0 && *p != sep) {
1230 while (bv->bv_len > 0 && *--p == ' ')
1236 aci_set_gather (void *cookie, char *name, struct berval *attr)
1238 AciSetCookie *cp = cookie;
1239 BerVarray bvals = NULL;
1240 struct berval bv, ndn;
1242 /* this routine needs to return the bervals instead of
1243 * plain strings, since syntax is not known. It should
1244 * also return the syntax or some "comparison cookie".
1248 bv.bv_len = strlen( name );
1249 if (dnNormalize2(NULL, &bv, &ndn) == LDAP_SUCCESS) {
1251 AttributeDescription *desc = NULL;
1252 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1253 backend_attribute(cp->be, NULL, NULL,
1254 cp->e, &ndn, desc, &bvals);
1263 struct berval *subj,
1271 struct berval set = { 0, NULL };
1273 AciSetCookie cookie;
1276 ber_dupbv( &set, subj );
1278 struct berval subjdn, ndn = { 0, NULL };
1279 struct berval setat;
1282 AttributeDescription *desc = NULL;
1284 /* format of string is "entry/setAttrName" */
1285 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1288 /* FIXME: If dnNormalize was based on ldap_bv2dn
1289 * instead of ldap_str2dn and would honor the bv_len
1290 * we could skip this step and not worry about the
1291 * unterminated string.
1293 char *s = ch_malloc(subjdn.bv_len + 1);
1294 AC_MEMCPY(s, subjdn.bv_val, subjdn.bv_len);
1298 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1299 setat.bv_val = SLAPD_ACI_SET_ATTR;
1300 setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1302 if ( setat.bv_val != NULL ) {
1303 if ( dnNormalize2(NULL, &subjdn, &ndn) == LDAP_SUCCESS
1304 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1306 backend_attribute(be, NULL, NULL, e,
1307 &ndn, desc, &bvals);
1308 if ( bvals != NULL ) {
1309 if ( bvals[0].bv_val != NULL ) {
1312 bvals[0].bv_val = NULL;
1313 for (i=1;bvals[i].bv_val;i++);
1314 bvals[0].bv_val = bvals[i-1].bv_val;
1315 bvals[i-1].bv_val = NULL;
1317 ber_bvarray_free(bvals);
1323 ch_free(subjdn.bv_val);
1326 if (set.bv_val != NULL) {
1331 rc = (slap_set_filter(aci_set_gather, &cookie, &set,
1332 op->o_ndn.bv_val, e->e_ndn, NULL) > 0);
1333 ch_free(set.bv_val);
1338 #ifdef SLAPD_ACI_ENABLED
1340 aci_list_map_rights(
1341 struct berval *list )
1348 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1351 switch (*bv.bv_val) {
1353 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1356 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1357 * the right 's' to mean "set", but in the examples states
1358 * that the right 's' means "search". The latter definition
1361 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1364 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1367 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1370 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1371 * define any equivalent to the AUTH right, so I've just used
1374 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1386 struct berval *list,
1387 const struct berval *attr,
1388 struct berval *val )
1390 struct berval bv, left, right;
1393 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1394 if (aci_get_part(&bv, 0, '=', &left) < 0
1395 || aci_get_part(&bv, 1, '=', &right) < 0)
1397 if (ber_bvstrcasecmp(attr, &bv) == 0)
1399 } else if (val == NULL) {
1400 if (ber_bvstrcasecmp(attr, &left) == 0)
1403 if (ber_bvstrcasecmp(attr, &left) == 0) {
1404 /* this is experimental code that implements a
1405 * simple (prefix) match of the attribute value.
1406 * the ACI draft does not provide for aci's that
1407 * apply to specific values, but it would be
1408 * nice to have. If the <attr> part of an aci's
1409 * rights list is of the form <attr>=<value>,
1410 * that means the aci applies only to attrs with
1411 * the given value. Furthermore, if the attr is
1412 * of the form <attr>=<value>*, then <value> is
1413 * treated as a prefix, and the aci applies to
1414 * any value with that prefix.
1416 * Ideally, this would allow r.e. matches.
1418 if (aci_get_part(&right, 0, '*', &left) < 0
1419 || right.bv_len <= left.bv_len)
1421 if (ber_bvstrcasecmp(val, &right) == 0)
1423 } else if (val->bv_len >= left.bv_len) {
1424 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1433 static slap_access_t
1434 aci_list_get_attr_rights(
1435 struct berval *list,
1436 const struct berval *attr,
1437 struct berval *val )
1443 /* loop through each rights/attr pair, skip first part (action) */
1445 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1446 if (aci_list_has_attr(&bv, attr, val) == 0)
1448 if (aci_get_part(list, i, ';', &bv) < 0)
1450 mask |= aci_list_map_rights(&bv);
1456 aci_list_get_rights(
1457 struct berval *list,
1458 const struct berval *attr,
1460 slap_access_t *grant,
1461 slap_access_t *deny )
1463 struct berval perm, actn;
1464 slap_access_t *mask;
1467 if (attr == NULL || attr->bv_len == 0
1468 || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1469 attr = &aci_bv_br_entry;
1475 /* loop through each permissions clause */
1476 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1477 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1479 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1481 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1488 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1489 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1496 struct berval *subj,
1497 struct berval *defgrpoc,
1498 struct berval *defgrpat,
1508 struct berval grpoc;
1509 struct berval grpat;
1510 ObjectClass *grp_oc = NULL;
1511 AttributeDescription *grp_ad = NULL;
1515 /* format of string is "group/objectClassValue/groupAttrName" */
1516 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1520 subjdn = aci_bvstrdup(&bv);
1521 if (subjdn == NULL) {
1525 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1529 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1533 rc = slap_bv2ad( &grpat, &grp_ad, &text );
1534 if( rc != LDAP_SUCCESS ) {
1540 grp_oc = oc_bvfind( &grpoc );
1542 if (grp_oc != NULL && grp_ad != NULL ) {
1544 bv.bv_val = (char *)ch_malloc(1024);
1546 string_expand(&bv, subjdn, e->e_ndn, matches);
1547 if ( dnNormalize2(NULL, &bv, &ndn) == LDAP_SUCCESS ) {
1548 rc = (backend_group(be, conn, op, e, &ndn, &op->o_ndn, grp_oc, grp_ad) == 0);
1559 static struct berval GroupClass = {
1560 sizeof(SLAPD_GROUP_CLASS)-1, SLAPD_GROUP_CLASS };
1561 static struct berval GroupAttr = {
1562 sizeof(SLAPD_GROUP_ATTR)-1, SLAPD_GROUP_ATTR };
1563 static struct berval RoleClass = {
1564 sizeof(SLAPD_ROLE_CLASS)-1, SLAPD_ROLE_CLASS };
1565 static struct berval RoleAttr = {
1566 sizeof(SLAPD_ROLE_ATTR)-1, SLAPD_ROLE_ATTR };
1574 AttributeDescription *desc,
1577 regmatch_t *matches,
1578 slap_access_t *grant,
1582 struct berval bv, perms, sdn;
1586 assert( desc->ad_cname.bv_val != NULL );
1588 /* parse an aci of the form:
1589 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1591 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1592 a full description of the format for this attribute.
1594 For now, this routine only supports scope=entry.
1597 /* check that the aci has all 5 components */
1598 if (aci_get_part(aci, 4, '#', NULL) < 0)
1601 /* check that the aci family is supported */
1602 if (aci_get_part(aci, 0, '#', &bv) < 0)
1605 /* check that the scope is "entry" */
1606 if (aci_get_part(aci, 1, '#', &bv) < 0
1607 || ber_bvstrcasecmp( &aci_bv_entry, &bv ) != 0)
1612 /* get the list of permissions clauses, bail if empty */
1613 if (aci_get_part(aci, 2, '#', &perms) <= 0)
1616 /* check if any permissions allow desired access */
1617 if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
1620 /* see if we have a DN match */
1621 if (aci_get_part(aci, 3, '#', &bv) < 0)
1624 if (aci_get_part(aci, 4, '#', &sdn) < 0)
1627 if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
1630 if ( dnNormalize2(NULL, &sdn, &ndn) == LDAP_SUCCESS ) {
1631 if (!dn_match( &op->o_ndn, &ndn))
1638 if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
1639 if (dn_match(&op->o_ndn, &e->e_nname))
1642 } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
1644 AttributeDescription *ad = NULL;
1647 rc = slap_bv2ad( &sdn, &ad, &text );
1649 if( rc != LDAP_SUCCESS ) {
1657 for(at = attrs_find( e->e_attrs, ad );
1659 at = attrs_find( at->a_next, ad ) )
1661 if (value_find( ad, at->a_vals, &bv) == 0 ) {
1670 } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
1671 if (aci_group_member(&sdn, &GroupClass, &GroupAttr, be, e, conn, op, matches))
1674 } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
1675 if (aci_group_member(&sdn, &RoleClass, &RoleAttr, be, e, conn, op, matches))
1678 } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
1679 if (aci_match_set(&sdn, be, e, conn, op, 0))
1682 } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
1683 if (aci_match_set(&sdn, be, e, conn, op, 1))
1691 #endif /* SLAPD_ACI_ENABLED */
1698 regmatch_t *matches)
1706 bv->bv_val[0] = '\0';
1707 bv->bv_len--; /* leave space for lone $ */
1710 for ( dp = bv->bv_val, sp = pat; size < bv->bv_len && *sp ; sp++) {
1711 /* did we previously see a $ */
1716 } else if (*sp >= '0' && *sp <= '9' ) {
1723 i = matches[n].rm_so;
1724 l = matches[n].rm_eo;
1725 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
1743 /* must have ended with a single $ */
1752 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1753 "string_expand: pattern = %s\n", pat ));
1754 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1755 "string_expand: expanded = %s\n", bv->bv_val ));
1757 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
1758 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
1764 char *pat, /* pattern to expand and match against */
1765 char *str, /* string to match against pattern */
1766 char *buf, /* buffer with $N expansion variables */
1767 regmatch_t *matches /* offsets in buffer for $N expansion variables */
1772 struct berval bv = {sizeof(newbuf), newbuf};
1775 if(str == NULL) str = "";
1777 string_expand(&bv, pat, buf, matches);
1778 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1780 regerror(rc, &re, error, sizeof(error));
1783 LDAP_LOG(( "aci", LDAP_LEVEL_ERR,
1784 "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
1787 Debug( LDAP_DEBUG_TRACE,
1788 "compile( \"%s\", \"%s\") failed %s\n",
1794 rc = regexec(&re, str, 0, NULL, 0);
1798 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1799 "regex_matches: string: %s\n", str ));
1800 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1801 "regex_matches: rc: %d %s\n",
1802 rc, rc ? "matches" : "no matches" ));
1804 Debug( LDAP_DEBUG_TRACE,
1805 "=> regex_matches: string: %s\n", str, 0, 0 );
1806 Debug( LDAP_DEBUG_TRACE,
1807 "=> regex_matches: rc: %d %s\n",
1808 rc, !rc ? "matches" : "no matches", 0 );