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>
24 aci_bv_entry = { sizeof("entry") - 1, "entry" },
25 aci_bv_br_entry = { sizeof("[entry]") - 1, "[entry]" },
26 aci_bv_br_all = { sizeof("[all]") - 1, "[all]" },
27 aci_bv_access_id = { sizeof("access-id") - 1, "access-id" },
28 aci_bv_anonymous = { sizeof("anonymous") - 1, "anonymous" },
29 aci_bv_users = { sizeof("users") - 1, "users" },
30 aci_bv_self = { sizeof("self") - 1, "self" },
31 aci_bv_dnattr = { sizeof("dnattr") - 1, "dnattr" },
32 aci_bv_group = { sizeof("group") - 1, "group" },
33 aci_bv_role = { sizeof("role") - 1, "role" },
34 aci_bv_set = { sizeof("set") - 1, "set" },
35 aci_bv_set_ref = { sizeof("set-ref") - 1, "set-ref"},
36 aci_bv_grant = { sizeof("grant") - 1, "grant" },
37 aci_bv_deny = { sizeof("deny") - 1, "deny" };
39 static AccessControl * acl_get(
40 AccessControl *ac, int *count,
41 Backend *be, Operation *op,
43 AttributeDescription *desc,
44 int nmatches, regmatch_t *matches );
46 static slap_control_t acl_mask(
47 AccessControl *ac, slap_mask_t *mask,
48 Backend *be, Connection *conn, Operation *op,
50 AttributeDescription *desc,
52 regmatch_t *matches );
54 #ifdef SLAPD_ACI_ENABLED
60 AttributeDescription *desc,
65 slap_access_t *deny );
68 static int regex_matches(
69 char *pat, char *str, char *buf, regmatch_t *matches);
70 static void string_expand(
71 struct berval *newbuf, char *pattern,
72 char *match, regmatch_t *matches);
74 typedef struct AciSetCookie {
81 BerVarray aci_set_gather (void *cookie, char *name, struct berval *attr);
82 static int aci_match_set ( struct berval *subj, Backend *be,
83 Entry *e, Connection *conn, Operation *op, int setref );
86 * access_allowed - check whether op->o_ndn is allowed the requested access
87 * to entry e, attribute attr, value val. if val is null, access to
88 * the whole attribute is assumed (all values).
90 * This routine loops through all access controls and calls
91 * acl_mask() on each applicable access control.
92 * The loop exits when a definitive answer is reached or
93 * or no more controls remain.
106 AttributeDescription *desc,
108 slap_access_t access )
113 char accessmaskbuf[ACCESSMASK_MAXLEN];
116 slap_control_t control;
118 regmatch_t matches[MAXREMATCHES];
121 assert( desc != NULL );
122 assert( access > ACL_NONE );
124 attr = desc->ad_cname.bv_val;
126 assert( attr != NULL );
129 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
130 "access_allowed: conn %d %s access to \"%s\" \"%s\" requested\n",
131 conn ? conn->c_connid : -1, access2str( access ), e->e_dn, attr ));
133 Debug( LDAP_DEBUG_ACL,
134 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
135 access2str( access ), e->e_dn, attr );
143 if ( be == NULL ) be = &backends[0];
144 assert( be != NULL );
146 /* grant database root access */
147 if ( be != NULL && be_isroot( be, &op->o_ndn ) ) {
149 LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
150 "access_allowed: conn %d root access granted\n",
153 Debug( LDAP_DEBUG_ACL,
154 "<= root access granted\n",
161 * no-user-modification operational attributes are ignored
162 * by ACL_WRITE checking as any found here are not provided
165 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
166 && desc != slap_schema.si_ad_entry
167 && desc != slap_schema.si_ad_children )
170 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
171 "access_allowed: conn %d NoUserMod Operational attribute: %s access granted\n",
172 conn->c_connid, attr ));
174 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
175 " %s access granted\n",
181 /* use backend default access if no backend acls */
182 if( be != NULL && be->be_acl == NULL ) {
184 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
185 "access_allowed: conn %d backend default %s access %s to \"%s\"\n",
186 conn->c_connid, access2str( access ),
187 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn.bv_val ));
189 Debug( LDAP_DEBUG_ACL,
190 "=> access_allowed: backend default %s access %s to \"%s\"\n",
191 access2str( access ),
192 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn.bv_val );
194 return be->be_dfltaccess >= access;
197 /* be is always non-NULL */
198 /* use global default access if no global acls */
199 } else if ( be == NULL && global_acl == NULL ) {
201 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
202 "access_allowed: conn %d global default %s access %s to \"%s\"\n",
203 conn->c_connid, access2str( access ),
204 global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val ));
206 Debug( LDAP_DEBUG_ACL,
207 "=> access_allowed: global default %s access %s to \"%s\"\n",
208 access2str( access ),
209 global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val );
211 return global_default_access >= access;
216 memset(matches, '\0', sizeof(matches));
222 while((a = acl_get( a, &count, be, op, e, desc, MAXREMATCHES, matches )) != NULL)
226 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
228 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
229 "access_allowed: conn %d match[%d]: %d %d ",
230 conn->c_connid, i, (int)matches[i].rm_so, (int)matches[i].rm_eo ));
232 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
233 (int)matches[i].rm_so, (int)matches[i].rm_eo );
235 if( matches[i].rm_so <= matches[0].rm_eo ) {
237 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
238 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
242 LDAP_LOG(( "acl", LDAP_LEVEL_ARGS, "\n" ));
244 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
248 control = acl_mask( a, &mask, be, conn, op,
249 e, desc, val, matches );
251 if ( control != ACL_BREAK ) {
255 memset(matches, '\0', sizeof(matches));
258 if ( ACL_IS_INVALID( mask ) ) {
260 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
261 "access_allowed: conn %d \"%s\" (%s) invalid!\n",
262 conn->c_connid, e->e_dn, attr ));
264 Debug( LDAP_DEBUG_ACL,
265 "=> access_allowed: \"%s\" (%s) invalid!\n",
270 } else if ( control == ACL_BREAK ) {
272 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
273 "access_allowed: conn %d no more rules\n", conn->c_connid ));
275 Debug( LDAP_DEBUG_ACL,
276 "=> access_allowed: no more rules\n", 0, 0, 0);
282 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
283 "access_allowed: conn %d %s access %s by %s\n",
285 access2str( access ),
286 ACL_GRANT( mask, access ) ? "granted" : "denied",
287 accessmask2str( mask, accessmaskbuf ) ));
289 Debug( LDAP_DEBUG_ACL,
290 "=> access_allowed: %s access %s by %s\n",
291 access2str( access ),
292 ACL_GRANT(mask, access) ? "granted" : "denied",
293 accessmask2str( mask, accessmaskbuf ) );
295 return ACL_GRANT(mask, access);
299 * acl_get - return the acl applicable to entry e, attribute
300 * attr. the acl returned is suitable for use in subsequent calls to
301 * acl_access_allowed().
304 static AccessControl *
311 AttributeDescription *desc,
313 regmatch_t *matches )
319 assert( count != NULL );
320 assert( desc != NULL );
322 attr = desc->ad_cname.bv_val;
324 assert( attr != NULL );
339 dnlen = e->e_nname.bv_len;
341 for ( ; a != NULL; a = a->acl_next ) {
344 if (a->acl_dn_pat.bv_len != 0) {
345 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
347 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
348 "acl_get: dnpat [%d] %s nsub: %d\n",
349 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub ));
351 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
352 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
354 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
359 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
360 "acl_get: dn [%d] %s\n",
361 *count, a->acl_dn_pat.bv_val ));
363 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
364 *count, a->acl_dn_pat.bv_val, 0 );
366 patlen = a->acl_dn_pat.bv_len;
367 if ( dnlen < patlen )
370 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
371 /* base dn -- entire object DN must match */
372 if ( dnlen != patlen )
375 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
378 if ( dnlen <= patlen )
381 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) || DN_ESCAPE( e->e_ndn[dnlen - patlen - 2] ) )
384 rdnlen = dn_rdnlen( NULL, &e->e_nname );
385 if ( rdnlen != dnlen - patlen - 1 )
388 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
389 if ( dnlen > patlen && ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) || DN_ESCAPE( e->e_ndn[dnlen - patlen - 2] ) ) )
392 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
393 if ( dnlen <= patlen )
395 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) || DN_ESCAPE( e->e_ndn[dnlen - patlen - 2] ) )
399 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
404 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
405 "acl_get: [%d] matched\n",
408 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
413 if ( a->acl_filter != NULL ) {
414 ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
415 if ( rc != LDAP_COMPARE_TRUE ) {
421 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
422 "acl_get: [%d] check attr %s\n",
425 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
428 if ( attr == NULL || a->acl_attrs == NULL ||
429 ad_inlist( desc, a->acl_attrs ) )
432 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
433 "acl_get: [%d] acl %s attr: %s\n",
434 *count, e->e_dn, attr ));
436 Debug( LDAP_DEBUG_ACL,
437 "<= acl_get: [%d] acl %s attr: %s\n",
438 *count, e->e_dn, attr );
442 matches[0].rm_so = matches[0].rm_eo = -1;
446 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
447 "acl_get: done.\n" ));
449 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
456 * acl_mask - modifies mask based upon the given acl and the
457 * requested access to entry e, attribute attr, value val. if val
458 * is null, access to the whole attribute is assumed (all values).
460 * returns 0 access NOT allowed
464 static slap_control_t
472 AttributeDescription *desc,
477 int i, odnlen, patlen;
480 char accessmaskbuf[ACCESSMASK_MAXLEN];
485 assert( mask != NULL );
486 assert( desc != NULL );
488 attr = desc->ad_cname.bv_val;
490 assert( attr != NULL );
493 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
494 "acl_mask: conn %d access to entry \"%s\", attr \"%s\" requested\n",
495 conn->c_connid, e->e_dn, attr ));
497 LDAP_LOG(( "acl", LDAP_LEVEL_ARGS,
498 " to %s by \"%s\", (%s) \n",
499 val ? "value" : "all values",
500 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
501 accessmask2str( *mask, accessmaskbuf ) ));
503 Debug( LDAP_DEBUG_ACL,
504 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
507 Debug( LDAP_DEBUG_ACL,
508 "=> acl_mask: to %s by \"%s\", (%s) \n",
509 val ? "value" : "all values",
510 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
511 accessmask2str( *mask, accessmaskbuf ) );
514 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
515 slap_mask_t oldmask, modmask;
517 ACL_INVALIDATE( modmask );
519 /* AND <who> clauses */
520 if ( b->a_dn_pat.bv_len != 0 ) {
522 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
523 "acl_mask: conn %d check a_dn_pat: %s\n",
524 conn->c_connid, b->a_dn_pat.bv_val ));
526 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
527 b->a_dn_pat.bv_val, 0, 0);
530 * if access applies to the entry itself, and the
531 * user is bound as somebody in the same namespace as
532 * the entry, OR the given dn matches the dn pattern
534 if ( ber_cmp( &b->a_dn_pat, &aci_bv_anonymous ) == 0 ) {
535 if ( op->o_ndn.bv_len != 0 ) {
539 } else if ( ber_cmp( &b->a_dn_pat, &aci_bv_users ) == 0 ) {
540 if ( op->o_ndn.bv_len == 0 ) {
544 } else if ( ber_cmp( &b->a_dn_pat, &aci_bv_self ) == 0 ) {
545 if ( op->o_ndn.bv_len == 0 ) {
549 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
553 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
554 if ( b->a_dn_pat.bv_len != 1 ||
555 ber_charcmp( &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 )
608 if ( b->a_sockurl_pat != NULL ) {
610 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
611 "acl_mask: conn %d check a_sockurl_pat: %s\n",
612 conn->c_connid, b->a_sockurl_pat ));
614 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
615 b->a_sockurl_pat, 0, 0 );
618 if ( strcmp( b->a_sockurl_pat, "*" ) != 0) {
619 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
620 if (!regex_matches( b->a_sockurl_pat, conn->c_listener_url,
621 e->e_ndn, matches ) )
626 if ( strcasecmp( b->a_sockurl_pat, conn->c_listener_url ) != 0 )
632 if ( b->a_domain_pat != NULL ) {
634 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
635 "acl_mask: conn %d check a_domain_pat: %s\n",
636 conn->c_connid, b->a_domain_pat ));
638 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
639 b->a_domain_pat, 0, 0 );
641 if ( strcmp( b->a_domain_pat, "*" ) != 0) {
642 if ( b->a_domain_style == ACL_STYLE_REGEX) {
643 if (!regex_matches( b->a_domain_pat, conn->c_peer_domain,
644 e->e_ndn, matches ) )
649 if ( strcasecmp( b->a_domain_pat, conn->c_peer_domain ) != 0 )
655 if ( b->a_peername_pat != NULL ) {
657 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
658 "acl_mask: conn %d check a_perrname_path: %s\n",
659 conn->c_connid, b->a_peername_pat ));
661 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
662 b->a_peername_pat, 0, 0 );
664 if ( strcmp( b->a_peername_pat, "*" ) != 0) {
665 if ( b->a_peername_style == ACL_STYLE_REGEX) {
666 if (!regex_matches( b->a_peername_pat, conn->c_peer_name,
667 e->e_ndn, matches ) )
672 if ( strcasecmp( b->a_peername_pat, conn->c_peer_name ) != 0 )
678 if ( b->a_sockname_pat != NULL ) {
680 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
681 "acl_mask: conn %d check a_sockname_path: %s\n",
682 conn->c_connid, b->a_sockname_pat ));
684 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
685 b->a_sockname_pat, 0, 0 );
687 if ( strcmp( b->a_sockname_pat, "*" ) != 0) {
688 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
689 if (!regex_matches( b->a_sockname_pat, conn->c_sock_name,
690 e->e_ndn, matches ) )
695 if ( strcasecmp( b->a_sockname_pat, conn->c_sock_name ) != 0 )
701 if ( b->a_dn_at != NULL && op->o_ndn.bv_len != 0 ) {
706 const char *attr = b->a_dn_at->ad_cname.bv_val;
708 assert( attr != NULL );
711 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
712 "acl_mask: conn %d check a_dn_pat: %s\n",
713 conn->c_connid, attr ));
715 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
720 /* see if asker is listed in dnattr */
721 for( at = attrs_find( e->e_attrs, b->a_dn_at );
723 at = attrs_find( at->a_next, b->a_dn_at ) )
725 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
733 /* have a dnattr match. if this is a self clause then
734 * the target must also match the op dn.
736 if ( b->a_dn_self ) {
737 /* check if the target is an attribute. */
740 /* target is attribute, check if the attribute value
743 rc = value_match( &match, b->a_dn_at,
744 b->a_dn_at->ad_type->sat_equality, 0,
746 /* on match error or no match, fail the ACL clause */
747 if (rc != LDAP_SUCCESS || match != 0 )
751 /* no dnattr match, check if this is a self clause */
752 if ( ! b->a_dn_self )
754 /* this is a self clause, check if the target is an
759 /* target is attribute, check if the attribute value
762 rc = value_match( &match, b->a_dn_at,
763 b->a_dn_at->ad_type->sat_equality, 0,
766 /* on match error or no match, fail the ACL clause */
767 if (rc != LDAP_SUCCESS || match != 0 )
772 if ( b->a_group_pat.bv_len && op->o_ndn.bv_len ) {
774 struct berval bv = { sizeof(buf) - 1, buf };
775 struct berval ndn = { 0, NULL };
778 /* b->a_group is an unexpanded entry name, expanded it should be an
779 * entry with objectclass group* and we test to see if odn is one of
780 * the values in the attribute group
782 /* see if asker is listed in dnattr */
783 if ( b->a_group_style == ACL_STYLE_REGEX ) {
784 string_expand(&bv, b->a_group_pat.bv_val, e->e_ndn, matches);
785 if ( dnNormalize2(NULL, &bv, &ndn) != LDAP_SUCCESS ) {
786 /* did not expand to a valid dn */
794 rc = backend_group(be, conn, op, e, &bv, &op->o_ndn,
795 b->a_group_oc, b->a_group_at);
804 if ( b->a_set_pat.bv_len != 0 ) {
805 if (aci_match_set( &b->a_set_pat, be, e, conn, op, 0 ) == 0) {
810 if ( b->a_authz.sai_ssf ) {
812 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
813 "acl_mask: conn %d check a_authz.sai_ssf: ACL %u > OP %u\n",
814 conn->c_connid, b->a_authz.sai_ssf, op->o_ssf ));
816 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
817 b->a_authz.sai_ssf, op->o_ssf, 0 );
819 if ( b->a_authz.sai_ssf > op->o_ssf ) {
824 if ( b->a_authz.sai_transport_ssf ) {
826 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
827 "acl_mask: conn %d check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
828 conn->c_connid, b->a_authz.sai_transport_ssf, op->o_transport_ssf ));
830 Debug( LDAP_DEBUG_ACL,
831 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
832 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
834 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
839 if ( b->a_authz.sai_tls_ssf ) {
841 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
842 "acl_mask: conn %d check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
843 conn->c_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf ));
845 Debug( LDAP_DEBUG_ACL,
846 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
847 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
849 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
854 if ( b->a_authz.sai_sasl_ssf ) {
856 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
857 "acl_mask: conn %d check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
858 conn->c_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf ));
860 Debug( LDAP_DEBUG_ACL,
861 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
862 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
864 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
869 #ifdef SLAPD_ACI_ENABLED
870 if ( b->a_aci_at != NULL ) {
872 slap_access_t grant, deny, tgrant, tdeny;
874 /* this case works different from the others above.
875 * since aci's themselves give permissions, we need
876 * to first check b->a_access_mask, the ACL's access level.
879 if ( e->e_nname.bv_len == 0 ) {
880 /* no ACIs in the root DSE */
884 /* first check if the right being requested
885 * is allowed by the ACL clause.
887 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
891 /* get the aci attribute */
892 at = attr_find( e->e_attrs, b->a_aci_at );
897 /* start out with nothing granted, nothing denied */
901 /* the aci is an multi-valued attribute. The
902 * rights are determined by OR'ing the individual
903 * rights given by the acis.
905 for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
906 if (aci_mask( be, conn, op,
907 e, desc, val, &at->a_vals[i],
908 matches, &grant, &deny ) != 0)
915 /* remove anything that the ACL clause does not allow */
916 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
917 tdeny &= ACL_PRIV_MASK;
919 /* see if we have anything to contribute */
920 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
924 /* this could be improved by changing acl_mask so that it can deal with
925 * by clauses that return grant/deny pairs. Right now, it does either
926 * additive or subtractive rights, but not both at the same time. So,
927 * we need to combine the grant/deny pair into a single rights mask in
928 * a smart way: if either grant or deny is "empty", then we use the
929 * opposite as is, otherwise we remove any denied rights from the grant
930 * rights mask and construct an additive mask.
932 if (ACL_IS_INVALID(tdeny)) {
933 modmask = tgrant | ACL_PRIV_ADDITIVE;
935 } else if (ACL_IS_INVALID(tgrant)) {
936 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
939 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
945 modmask = b->a_access_mask;
949 LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
950 "acl_mask: conn %d [%d] applying %s (%s)\n",
951 conn->c_connid, i, accessmask2str( modmask, accessmaskbuf),
952 b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
953 ? "break" : "stop" ));
955 Debug( LDAP_DEBUG_ACL,
956 "<= acl_mask: [%d] applying %s (%s)\n",
957 i, accessmask2str( modmask, accessmaskbuf ),
958 b->a_type == ACL_CONTINUE
960 : b->a_type == ACL_BREAK
967 if( ACL_IS_ADDITIVE(modmask) ) {
969 ACL_PRIV_SET( *mask, modmask );
972 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
974 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
975 /* substract privs */
976 ACL_PRIV_CLR( *mask, modmask );
979 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
987 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
988 "acl_mask: conn %d [%d] mask: %s\n",
989 conn->c_connid, i, accessmask2str( *mask, accessmaskbuf) ));
991 Debug( LDAP_DEBUG_ACL,
992 "<= acl_mask: [%d] mask: %s\n",
993 i, accessmask2str(*mask, accessmaskbuf), 0 );
996 if( b->a_type == ACL_CONTINUE ) {
999 } else if ( b->a_type == ACL_BREAK ) {
1007 /* implicit "by * none" clause */
1011 LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
1012 "acl_mask: conn %d no more <who> clauses, returning %d (stop)\n",
1013 conn->c_connid, accessmask2str( *mask, accessmaskbuf) ));
1015 Debug( LDAP_DEBUG_ACL,
1016 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1017 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1023 * acl_check_modlist - check access control on the given entry to see if
1024 * it allows the given modifications by the user associated with op.
1025 * returns 1 if mods allowed ok
1026 * 0 mods not allowed
1035 Modifications *mlist
1040 assert( be != NULL );
1042 /* short circuit root database access */
1043 if ( be_isroot( be, &op->o_ndn ) ) {
1045 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
1046 "acl_check_modlist: conn %d access granted to root user\n",
1049 Debug( LDAP_DEBUG_ACL,
1050 "<= acl_access_allowed: granted to database root\n",
1056 /* use backend default access if no backend acls */
1057 if( be != NULL && be->be_acl == NULL ) {
1059 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1060 "acl_check_modlist: conn %d backend default %s access %s to \"%s\"\n",
1061 conn->c_connid, access2str( ACL_WRITE ),
1062 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val ));
1064 Debug( LDAP_DEBUG_ACL,
1065 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1066 access2str( ACL_WRITE ),
1067 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1069 return be->be_dfltaccess >= ACL_WRITE;
1072 /* be is always non-NULL */
1073 /* use global default access if no global acls */
1074 } else if ( be == NULL && global_acl == NULL ) {
1076 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1077 "acl_check_modlist: conn %d global default %s access %s to \"%s\"\n",
1078 conn->c_connid, access2str( ACL_WRITE ),
1079 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn ));
1081 Debug( LDAP_DEBUG_ACL,
1082 "=> access_allowed: global default %s access %s to \"%s\"\n",
1083 access2str( ACL_WRITE ),
1084 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1086 return global_default_access >= ACL_WRITE;
1090 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1092 * no-user-modification operational attributes are ignored
1093 * by ACL_WRITE checking as any found here are not provided
1096 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1098 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1099 "acl_check_modlist: conn %d no-user-mod %s: modify access granted\n",
1100 conn->c_connid, mlist->sml_desc->ad_cname.bv_val ));
1102 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1103 " modify access granted\n",
1104 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1109 switch ( mlist->sml_op ) {
1110 case LDAP_MOD_REPLACE:
1112 * We must check both permission to delete the whole
1113 * attribute and permission to add the specific attributes.
1114 * This prevents abuse from selfwriters.
1116 if ( ! access_allowed( be, conn, op, e,
1117 mlist->sml_desc, NULL, ACL_WRITE ) )
1122 if ( mlist->sml_bvalues == NULL ) break;
1124 /* fall thru to check value to add */
1127 assert( mlist->sml_bvalues != NULL );
1129 for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ ) {
1130 if ( ! access_allowed( be, conn, op, e,
1131 mlist->sml_desc, bv, ACL_WRITE ) )
1138 case LDAP_MOD_DELETE:
1139 if ( mlist->sml_bvalues == NULL ) {
1140 if ( ! access_allowed( be, conn, op, e,
1141 mlist->sml_desc, NULL, ACL_WRITE ) )
1147 for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ ) {
1148 if ( ! access_allowed( be, conn, op, e,
1149 mlist->sml_desc, bv, ACL_WRITE ) )
1166 aci_bvstrdup( struct berval *bv )
1170 s = (char *)ch_malloc(bv->bv_len + 1);
1172 AC_MEMCPY(s, bv->bv_val, bv->bv_len);
1178 #ifdef SLAPD_ACI_ENABLED
1186 res = strncasecmp( s, bv->bv_val, bv->bv_len );
1190 if (len > (int)bv->bv_len)
1192 if (len < (int)bv->bv_len)
1200 struct berval *list,
1214 while (len >= 0 && --ix >= 0) {
1215 while (--len >= 0 && *p++ != sep) ;
1217 while (len >= 0 && *p == ' ') {
1228 while (--len >= 0 && *p != sep) {
1232 while (bv->bv_len > 0 && *--p == ' ')
1238 aci_set_gather (void *cookie, char *name, struct berval *attr)
1240 AciSetCookie *cp = cookie;
1241 BerVarray bvals = NULL;
1242 struct berval bv, ndn;
1244 /* this routine needs to return the bervals instead of
1245 * plain strings, since syntax is not known. It should
1246 * also return the syntax or some "comparison cookie".
1250 bv.bv_len = strlen( name );
1251 if (dnNormalize2(NULL, &bv, &ndn) == LDAP_SUCCESS) {
1253 AttributeDescription *desc = NULL;
1254 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1255 backend_attribute(cp->be, NULL, NULL,
1256 cp->e, &ndn, desc, &bvals);
1265 struct berval *subj,
1273 struct berval set = { 0, NULL };
1275 AciSetCookie cookie;
1278 ber_dupbv( &set, subj );
1280 struct berval subjdn, ndn = { 0, NULL };
1281 struct berval setat;
1284 AttributeDescription *desc = NULL;
1286 /* format of string is "entry/setAttrName" */
1287 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1290 /* FIXME: If dnNormalize was based on ldap_bv2dn
1291 * instead of ldap_str2dn and would honor the bv_len
1292 * we could skip this step and not worry about the
1293 * unterminated string.
1295 char *s = ch_malloc(subjdn.bv_len + 1);
1296 AC_MEMCPY(s, subjdn.bv_val, subjdn.bv_len);
1300 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1301 setat.bv_val = SLAPD_ACI_SET_ATTR;
1302 setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1304 if ( setat.bv_val != NULL ) {
1305 if ( dnNormalize2(NULL, &subjdn, &ndn) == LDAP_SUCCESS
1306 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1308 backend_attribute(be, NULL, NULL, e,
1309 &ndn, desc, &bvals);
1310 if ( bvals != NULL ) {
1311 if ( bvals[0].bv_val != NULL ) {
1314 bvals[0].bv_val = NULL;
1315 for (i=1;bvals[i].bv_val;i++);
1316 bvals[0].bv_val = bvals[i-1].bv_val;
1317 bvals[i-1].bv_val = NULL;
1319 ber_bvarray_free(bvals);
1325 ch_free(subjdn.bv_val);
1328 if (set.bv_val != NULL) {
1333 rc = (slap_set_filter(aci_set_gather, &cookie, &set,
1334 op->o_ndn.bv_val, e->e_ndn, NULL) > 0);
1335 ch_free(set.bv_val);
1340 #ifdef SLAPD_ACI_ENABLED
1342 aci_list_map_rights(
1343 struct berval *list )
1350 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1353 switch (*bv.bv_val) {
1355 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1358 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1359 * the right 's' to mean "set", but in the examples states
1360 * that the right 's' means "search". The latter definition
1363 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1366 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1369 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1372 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1373 * define any equivalent to the AUTH right, so I've just used
1376 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1388 struct berval *list,
1389 const struct berval *attr,
1390 struct berval *val )
1392 struct berval bv, left, right;
1395 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1396 if (aci_get_part(&bv, 0, '=', &left) < 0
1397 || aci_get_part(&bv, 1, '=', &right) < 0)
1399 if (ber_casecmp(attr, &bv) == 0)
1401 } else if (val == NULL) {
1402 if (ber_casecmp(attr, &left) == 0)
1405 if (ber_casecmp(attr, &left) == 0) {
1406 /* this is experimental code that implements a
1407 * simple (prefix) match of the attribute value.
1408 * the ACI draft does not provide for aci's that
1409 * apply to specific values, but it would be
1410 * nice to have. If the <attr> part of an aci's
1411 * rights list is of the form <attr>=<value>,
1412 * that means the aci applies only to attrs with
1413 * the given value. Furthermore, if the attr is
1414 * of the form <attr>=<value>*, then <value> is
1415 * treated as a prefix, and the aci applies to
1416 * any value with that prefix.
1418 * Ideally, this would allow r.e. matches.
1420 if (aci_get_part(&right, 0, '*', &left) < 0
1421 || right.bv_len <= left.bv_len)
1423 if (ber_casecmp(val, &right) == 0)
1425 } else if (val->bv_len >= left.bv_len) {
1426 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1435 static slap_access_t
1436 aci_list_get_attr_rights(
1437 struct berval *list,
1438 const struct berval *attr,
1439 struct berval *val )
1445 /* loop through each rights/attr pair, skip first part (action) */
1447 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1448 if (aci_list_has_attr(&bv, attr, val) == 0)
1450 if (aci_get_part(list, i, ';', &bv) < 0)
1452 mask |= aci_list_map_rights(&bv);
1458 aci_list_get_rights(
1459 struct berval *list,
1460 const struct berval *attr,
1462 slap_access_t *grant,
1463 slap_access_t *deny )
1465 struct berval perm, actn;
1466 slap_access_t *mask;
1469 if (attr == NULL || attr->bv_len == 0
1470 || ber_casecmp( attr, &aci_bv_entry ) == 0) {
1471 attr = &aci_bv_br_entry;
1477 /* loop through each permissions clause */
1478 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1479 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1481 if (ber_casecmp( &aci_bv_grant, &actn ) == 0) {
1483 } else if (ber_casecmp( &aci_bv_deny, &actn ) == 0) {
1490 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1491 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1498 struct berval *subj,
1499 struct berval *defgrpoc,
1500 struct berval *defgrpat,
1510 struct berval grpoc;
1511 struct berval grpat;
1512 ObjectClass *grp_oc = NULL;
1513 AttributeDescription *grp_ad = NULL;
1517 /* format of string is "group/objectClassValue/groupAttrName" */
1518 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1522 subjdn = aci_bvstrdup(&bv);
1523 if (subjdn == NULL) {
1527 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1531 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1535 rc = slap_bv2ad( &grpat, &grp_ad, &text );
1536 if( rc != LDAP_SUCCESS ) {
1542 grp_oc = oc_bvfind( &grpoc );
1544 if (grp_oc != NULL && grp_ad != NULL ) {
1546 bv.bv_val = (char *)ch_malloc(1024);
1548 string_expand(&bv, subjdn, e->e_ndn, matches);
1549 if ( dnNormalize2(NULL, &bv, &ndn) == LDAP_SUCCESS ) {
1550 rc = (backend_group(be, conn, op, e, &ndn, &op->o_ndn, grp_oc, grp_ad) == 0);
1561 static struct berval GroupClass = {
1562 sizeof(SLAPD_GROUP_CLASS)-1, SLAPD_GROUP_CLASS };
1563 static struct berval GroupAttr = {
1564 sizeof(SLAPD_GROUP_ATTR)-1, SLAPD_GROUP_ATTR };
1565 static struct berval RoleClass = {
1566 sizeof(SLAPD_ROLE_CLASS)-1, SLAPD_ROLE_CLASS };
1567 static struct berval RoleAttr = {
1568 sizeof(SLAPD_ROLE_ATTR)-1, SLAPD_ROLE_ATTR };
1576 AttributeDescription *desc,
1579 regmatch_t *matches,
1580 slap_access_t *grant,
1584 struct berval bv, perms, sdn;
1588 assert( desc->ad_cname.bv_val != NULL );
1590 /* parse an aci of the form:
1591 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1593 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1594 a full description of the format for this attribute.
1596 For now, this routine only supports scope=entry.
1599 /* check that the aci has all 5 components */
1600 if (aci_get_part(aci, 4, '#', NULL) < 0)
1603 /* check that the aci family is supported */
1604 if (aci_get_part(aci, 0, '#', &bv) < 0)
1607 /* check that the scope is "entry" */
1608 if (aci_get_part(aci, 1, '#', &bv) < 0
1609 || ber_casecmp( &aci_bv_entry, &bv ) != 0)
1614 /* get the list of permissions clauses, bail if empty */
1615 if (aci_get_part(aci, 2, '#', &perms) <= 0)
1618 /* check if any permissions allow desired access */
1619 if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
1622 /* see if we have a DN match */
1623 if (aci_get_part(aci, 3, '#', &bv) < 0)
1626 if (aci_get_part(aci, 4, '#', &sdn) < 0)
1629 if (ber_casecmp( &aci_bv_access_id, &bv ) == 0) {
1632 if ( dnNormalize2(NULL, &sdn, &ndn) == LDAP_SUCCESS ) {
1633 if (!dn_match( &op->o_ndn, &ndn))
1640 if (ber_casecmp( &aci_bv_self, &bv ) == 0) {
1641 if (dn_match(&op->o_ndn, &e->e_nname))
1644 } else if (ber_casecmp( &aci_bv_dnattr, &bv ) == 0) {
1646 AttributeDescription *ad = NULL;
1649 rc = slap_bv2ad( &sdn, &ad, &text );
1651 if( rc != LDAP_SUCCESS ) {
1659 for(at = attrs_find( e->e_attrs, ad );
1661 at = attrs_find( at->a_next, ad ) )
1663 if (value_find( ad, at->a_vals, &bv) == 0 ) {
1672 } else if (ber_casecmp( &aci_bv_group, &bv ) == 0) {
1673 if (aci_group_member(&sdn, &GroupClass, &GroupAttr, be, e, conn, op, matches))
1676 } else if (ber_casecmp( &aci_bv_role, &bv ) == 0) {
1677 if (aci_group_member(&sdn, &RoleClass, &RoleAttr, be, e, conn, op, matches))
1680 } else if (ber_casecmp( &aci_bv_set, &bv ) == 0) {
1681 if (aci_match_set(&sdn, be, e, conn, op, 0))
1684 } else if (ber_casecmp( &aci_bv_set_ref, &bv ) == 0) {
1685 if (aci_match_set(&sdn, be, e, conn, op, 1))
1693 #endif /* SLAPD_ACI_ENABLED */
1700 regmatch_t *matches)
1708 bv->bv_val[0] = '\0';
1709 bv->bv_len--; /* leave space for lone $ */
1712 for ( dp = bv->bv_val, sp = pat; size < bv->bv_len && *sp ; sp++) {
1713 /* did we previously see a $ */
1718 } else if (*sp >= '0' && *sp <= '9' ) {
1725 i = matches[n].rm_so;
1726 l = matches[n].rm_eo;
1727 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
1745 /* must have ended with a single $ */
1754 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1755 "string_expand: pattern = %s\n", pat ));
1756 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1757 "string_expand: expanded = %s\n", bv->bv_val ));
1759 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
1760 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
1766 char *pat, /* pattern to expand and match against */
1767 char *str, /* string to match against pattern */
1768 char *buf, /* buffer with $N expansion variables */
1769 regmatch_t *matches /* offsets in buffer for $N expansion variables */
1774 struct berval bv = {sizeof(newbuf), newbuf};
1777 if(str == NULL) str = "";
1779 string_expand(&bv, pat, buf, matches);
1780 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1782 regerror(rc, &re, error, sizeof(error));
1785 LDAP_LOG(( "aci", LDAP_LEVEL_ERR,
1786 "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
1789 Debug( LDAP_DEBUG_TRACE,
1790 "compile( \"%s\", \"%s\") failed %s\n",
1796 rc = regexec(&re, str, 0, NULL, 0);
1800 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1801 "regex_matches: string: %s\n", str ));
1802 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1803 "regex_matches: rc: %d %s\n",
1804 rc, rc ? "matches" : "no matches" ));
1806 Debug( LDAP_DEBUG_TRACE,
1807 "=> regex_matches: string: %s\n", str, 0, 0 );
1808 Debug( LDAP_DEBUG_TRACE,
1809 "=> regex_matches: rc: %d %s\n",
1810 rc, !rc ? "matches" : "no matches", 0 );