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>
19 static AccessControl * acl_get(
20 AccessControl *ac, int *count,
21 Backend *be, Operation *op,
23 AttributeDescription *desc,
24 int nmatches, regmatch_t *matches );
26 static slap_control_t acl_mask(
27 AccessControl *ac, slap_mask_t *mask,
28 Backend *be, Connection *conn, Operation *op,
30 AttributeDescription *desc,
32 regmatch_t *matches );
34 #ifdef SLAPD_ACI_ENABLED
40 AttributeDescription *desc,
45 slap_access_t *deny );
48 static int regex_matches(
49 char *pat, char *str, char *buf, regmatch_t *matches);
50 static void string_expand(
51 struct berval *newbuf, char *pattern,
52 char *match, regmatch_t *matches);
54 typedef struct AciSetCookie {
61 BVarray aci_set_gather (void *cookie, char *name, struct berval *attr);
62 static int aci_match_set ( struct berval *subj, Backend *be,
63 Entry *e, Connection *conn, Operation *op, int setref );
66 * access_allowed - check whether op->o_ndn is allowed the requested access
67 * to entry e, attribute attr, value val. if val is null, access to
68 * the whole attribute is assumed (all values).
70 * This routine loops through all access controls and calls
71 * acl_mask() on each applicable access control.
72 * The loop exits when a definitive answer is reached or
73 * or no more controls remain.
86 AttributeDescription *desc,
88 slap_access_t access )
93 char accessmaskbuf[ACCESSMASK_MAXLEN];
96 slap_control_t control;
98 regmatch_t matches[MAXREMATCHES];
101 assert( desc != NULL );
102 assert( access > ACL_NONE );
104 attr = desc->ad_cname.bv_val;
106 assert( attr != NULL );
109 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
110 "access_allowed: conn %d %s access to \"%s\" \"%s\" requested\n",
111 conn ? conn->c_connid : -1, access2str( access ), e->e_dn, attr ));
113 Debug( LDAP_DEBUG_ACL,
114 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
115 access2str( access ), e->e_dn, attr );
123 if ( be == NULL ) be = &backends[0];
124 assert( be != NULL );
126 /* grant database root access */
127 if ( be != NULL && be_isroot( be, &op->o_ndn ) ) {
129 LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
130 "access_allowed: conn %d root access granted\n",
133 Debug( LDAP_DEBUG_ACL,
134 "<= root access granted\n",
141 * no-user-modification operational attributes are ignored
142 * by ACL_WRITE checking as any found here are not provided
145 if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
146 && desc != slap_schema.si_ad_entry
147 && desc != slap_schema.si_ad_children )
150 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
151 "access_allowed: conn %d NoUserMod Operational attribute: %s access granted\n",
152 conn->c_connid, attr ));
154 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
155 " %s access granted\n",
161 /* use backend default access if no backend acls */
162 if( be != NULL && be->be_acl == NULL ) {
164 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
165 "access_allowed: conn %d backend default %s access %s to \"%s\"\n",
166 conn->c_connid, access2str( access ),
167 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn.bv_val ));
169 Debug( LDAP_DEBUG_ACL,
170 "=> access_allowed: backend default %s access %s to \"%s\"\n",
171 access2str( access ),
172 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn.bv_val );
174 return be->be_dfltaccess >= access;
177 /* be is always non-NULL */
178 /* use global default access if no global acls */
179 } else if ( be == NULL && global_acl == NULL ) {
181 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
182 "access_allowed: conn %d global default %s access %s to \"%s\"\n",
183 conn->c_connid, access2str( access ),
184 global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val ));
186 Debug( LDAP_DEBUG_ACL,
187 "=> access_allowed: global default %s access %s to \"%s\"\n",
188 access2str( access ),
189 global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val );
191 return global_default_access >= access;
196 memset(matches, '\0', sizeof(matches));
202 while((a = acl_get( a, &count, be, op, e, desc, MAXREMATCHES, matches )) != NULL)
206 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
208 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
209 "access_allowed: conn %d match[%d]: %d %d ",
210 conn->c_connid, i, (int)matches[i].rm_so, (int)matches[i].rm_eo ));
212 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
213 (int)matches[i].rm_so, (int)matches[i].rm_eo );
215 if( matches[i].rm_so <= matches[0].rm_eo ) {
217 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
218 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
222 LDAP_LOG(( "acl", LDAP_LEVEL_ARGS, "\n" ));
224 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
228 control = acl_mask( a, &mask, be, conn, op,
229 e, desc, val, matches );
231 if ( control != ACL_BREAK ) {
235 memset(matches, '\0', sizeof(matches));
238 if ( ACL_IS_INVALID( mask ) ) {
240 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
241 "access_allowed: conn %d \"%s\" (%s) invalid!\n",
242 conn->c_connid, e->e_dn, attr ));
244 Debug( LDAP_DEBUG_ACL,
245 "=> access_allowed: \"%s\" (%s) invalid!\n",
250 } else if ( control == ACL_BREAK ) {
252 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
253 "access_allowed: conn %d no more rules\n", conn->c_connid ));
255 Debug( LDAP_DEBUG_ACL,
256 "=> access_allowed: no more rules\n", 0, 0, 0);
262 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
263 "access_allowed: conn %d %s access %s by %s\n",
265 access2str( access ),
266 ACL_GRANT( mask, access ) ? "granted" : "denied",
267 accessmask2str( mask, accessmaskbuf ) ));
269 Debug( LDAP_DEBUG_ACL,
270 "=> access_allowed: %s access %s by %s\n",
271 access2str( access ),
272 ACL_GRANT(mask, access) ? "granted" : "denied",
273 accessmask2str( mask, accessmaskbuf ) );
275 return ACL_GRANT(mask, access);
279 * acl_get - return the acl applicable to entry e, attribute
280 * attr. the acl returned is suitable for use in subsequent calls to
281 * acl_access_allowed().
284 static AccessControl *
291 AttributeDescription *desc,
293 regmatch_t *matches )
299 assert( count != NULL );
300 assert( desc != NULL );
302 attr = desc->ad_cname.bv_val;
304 assert( attr != NULL );
319 dnlen = e->e_nname.bv_len;
321 for ( ; a != NULL; a = a->acl_next ) {
324 if (a->acl_dn_pat.bv_len != 0) {
325 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
327 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
328 "acl_get: dnpat [%d] %s nsub: %d\n",
329 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub ));
331 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
332 *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
334 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
339 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
340 "acl_get: dn [%d] %s\n",
341 *count, a->acl_dn_pat.bv_val ));
343 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n",
344 *count, a->acl_dn_pat.bv_val, 0 );
346 patlen = a->acl_dn_pat.bv_len;
347 if ( dnlen < patlen )
350 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
351 /* base dn -- entire object DN must match */
352 if ( dnlen != patlen )
355 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
358 if ( dnlen <= patlen )
361 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) || DN_ESCAPE( e->e_ndn[dnlen - patlen - 2] ) )
364 rdnlen = dn_rdnlen( NULL, &e->e_nname );
365 if ( rdnlen != dnlen - patlen - 1 )
368 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
369 if ( dnlen > patlen && ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) || DN_ESCAPE( e->e_ndn[dnlen - patlen - 2] ) ) )
372 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
373 if ( dnlen <= patlen )
375 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) || DN_ESCAPE( e->e_ndn[dnlen - patlen - 2] ) )
379 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
384 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
385 "acl_get: [%d] matched\n",
388 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
393 if ( a->acl_filter != NULL ) {
394 ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
395 if ( rc != LDAP_COMPARE_TRUE ) {
401 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
402 "acl_get: [%d] check attr %s\n",
405 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
408 if ( attr == NULL || a->acl_attrs == NULL ||
409 ad_inlist( desc, a->acl_attrs ) )
412 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
413 "acl_get: [%d] acl %s attr: %s\n",
414 *count, e->e_dn, attr ));
416 Debug( LDAP_DEBUG_ACL,
417 "<= acl_get: [%d] acl %s attr: %s\n",
418 *count, e->e_dn, attr );
422 matches[0].rm_so = matches[0].rm_eo = -1;
426 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
427 "acl_get: done.\n" ));
429 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
436 * acl_mask - modifies mask based upon the given acl and the
437 * requested access to entry e, attribute attr, value val. if val
438 * is null, access to the whole attribute is assumed (all values).
440 * returns 0 access NOT allowed
444 static slap_control_t
452 AttributeDescription *desc,
457 int i, odnlen, patlen;
460 char accessmaskbuf[ACCESSMASK_MAXLEN];
465 assert( mask != NULL );
466 assert( desc != NULL );
468 attr = desc->ad_cname.bv_val;
470 assert( attr != NULL );
473 LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
474 "acl_mask: conn %d access to entry \"%s\", attr \"%s\" requested\n",
475 conn->c_connid, e->e_dn, attr ));
477 LDAP_LOG(( "acl", LDAP_LEVEL_ARGS,
478 " to %s by \"%s\", (%s) \n",
479 val ? "value" : "all values",
480 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
481 accessmask2str( *mask, accessmaskbuf ) ));
483 Debug( LDAP_DEBUG_ACL,
484 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
487 Debug( LDAP_DEBUG_ACL,
488 "=> acl_mask: to %s by \"%s\", (%s) \n",
489 val ? "value" : "all values",
490 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
491 accessmask2str( *mask, accessmaskbuf ) );
494 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
495 slap_mask_t oldmask, modmask;
497 ACL_INVALIDATE( modmask );
499 /* AND <who> clauses */
500 if ( b->a_dn_pat.bv_len != 0 ) {
502 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
503 "acl_mask: conn %d check a_dn_pat: %s\n",
504 conn->c_connid, b->a_dn_pat.bv_val ));
506 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
507 b->a_dn_pat.bv_val, 0, 0);
510 * if access applies to the entry itself, and the
511 * user is bound as somebody in the same namespace as
512 * the entry, OR the given dn matches the dn pattern
514 if ( b->a_dn_pat.bv_len == sizeof("anonymous") -1 &&
515 strcmp( b->a_dn_pat.bv_val, "anonymous" ) == 0 ) {
516 if (op->o_ndn.bv_len != 0 ) {
520 } else if ( b->a_dn_pat.bv_len == sizeof("users") - 1 &&
521 strcmp( b->a_dn_pat.bv_val, "users" ) == 0 ) {
522 if (op->o_ndn.bv_len == 0 ) {
526 } else if ( b->a_dn_pat.bv_len == sizeof("self") - 1 &&
527 strcmp( b->a_dn_pat.bv_val, "self" ) == 0 ) {
528 if( op->o_ndn.bv_len == 0 ) {
532 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn.bv_val ) != 0 ) {
536 } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
537 if ( b->a_dn_pat.bv_len != 1 ||
538 strcmp( b->a_dn_pat.bv_val, "*" ) != 0 ) {
539 int ret = regex_matches( b->a_dn_pat.bv_val,
540 op->o_ndn.bv_val, e->e_ndn, matches );
548 if ( e->e_dn == NULL )
551 patlen = b->a_dn_pat.bv_len;
552 odnlen = op->o_ndn.bv_len;
553 if ( odnlen < patlen )
556 if ( b->a_dn_style == ACL_STYLE_BASE ) {
557 /* base dn -- entire object DN must match */
558 if ( odnlen != patlen )
561 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
564 if ( odnlen <= patlen )
567 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) || DN_ESCAPE( op->o_ndn.bv_val[odnlen - patlen - 2] ) )
570 rdnlen = dn_rdnlen( NULL, &op->o_ndn );
571 if ( rdnlen != odnlen - patlen - 1 )
574 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
575 if ( odnlen > patlen && ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) || DN_ESCAPE( op->o_ndn.bv_val[odnlen - patlen - 2] ) ) )
578 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
579 if ( odnlen <= patlen )
581 if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) || DN_ESCAPE( op->o_ndn.bv_val[odnlen - patlen - 2] ) )
585 if ( strcmp( b->a_dn_pat.bv_val, op->o_ndn.bv_val + odnlen - patlen ) != 0 )
591 if ( b->a_sockurl_pat != NULL ) {
593 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
594 "acl_mask: conn %d check a_sockurl_pat: %s\n",
595 conn->c_connid, b->a_sockurl_pat ));
597 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
598 b->a_sockurl_pat, 0, 0 );
601 if ( strcmp( b->a_sockurl_pat, "*" ) != 0) {
602 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
603 if (!regex_matches( b->a_sockurl_pat, conn->c_listener_url,
604 e->e_ndn, matches ) )
609 if ( strcasecmp( b->a_sockurl_pat, conn->c_listener_url ) == 0 )
615 if ( b->a_domain_pat != NULL ) {
617 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
618 "acl_mask: conn %d check a_domain_pat: %s\n",
619 conn->c_connid, b->a_domain_pat ));
621 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
622 b->a_domain_pat, 0, 0 );
624 if ( strcmp( b->a_domain_pat, "*" ) != 0) {
625 if ( b->a_domain_style == ACL_STYLE_REGEX) {
626 if (!regex_matches( b->a_domain_pat, conn->c_peer_domain,
627 e->e_ndn, matches ) )
632 if ( strcasecmp( b->a_domain_pat, conn->c_peer_domain ) == 0 )
638 if ( b->a_peername_pat != NULL ) {
640 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
641 "acl_mask: conn %d check a_perrname_path: %s\n",
642 conn->c_connid, b->a_peername_pat ));
644 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
645 b->a_peername_pat, 0, 0 );
647 if ( strcmp( b->a_peername_pat, "*" ) != 0) {
648 if ( b->a_peername_style == ACL_STYLE_REGEX) {
649 if (!regex_matches( b->a_peername_pat, conn->c_peer_name,
650 e->e_ndn, matches ) )
655 if ( strcasecmp( b->a_peername_pat, conn->c_peer_name ) == 0 )
661 if ( b->a_sockname_pat != NULL ) {
663 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
664 "acl_mask: conn %d check a_sockname_path: %s\n",
665 conn->c_connid, b->a_sockname_pat ));
667 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
668 b->a_sockname_pat, 0, 0 );
670 if ( strcmp( b->a_sockname_pat, "*" ) != 0) {
671 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
672 if (!regex_matches( b->a_sockname_pat, conn->c_sock_name,
673 e->e_ndn, matches ) )
678 if ( strcasecmp( b->a_sockname_pat, conn->c_sock_name ) == 0 )
684 if ( b->a_dn_at != NULL && op->o_ndn.bv_len != 0 ) {
689 const char *attr = b->a_dn_at->ad_cname.bv_val;
691 assert( attr != NULL );
694 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
695 "acl_mask: conn %d check a_dn_pat: %s\n",
696 conn->c_connid, attr ));
698 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
703 /* see if asker is listed in dnattr */
704 for( at = attrs_find( e->e_attrs, b->a_dn_at );
706 at = attrs_find( at->a_next, b->a_dn_at ) )
708 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
716 /* have a dnattr match. if this is a self clause then
717 * the target must also match the op dn.
719 if ( b->a_dn_self ) {
720 /* check if the target is an attribute. */
723 /* target is attribute, check if the attribute value
726 rc = value_match( &match, b->a_dn_at,
727 b->a_dn_at->ad_type->sat_equality, 0,
729 /* on match error or no match, fail the ACL clause */
730 if (rc != LDAP_SUCCESS || match != 0 )
734 /* no dnattr match, check if this is a self clause */
735 if ( ! b->a_dn_self )
737 /* this is a self clause, check if the target is an
742 /* target is attribute, check if the attribute value
745 rc = value_match( &match, b->a_dn_at,
746 b->a_dn_at->ad_type->sat_equality, 0,
749 /* on match error or no match, fail the ACL clause */
750 if (rc != LDAP_SUCCESS || match != 0 )
755 if ( b->a_group_pat.bv_len && op->o_ndn.bv_len ) {
757 struct berval bv = { sizeof(buf) - 1, buf };
758 struct berval ndn = { 0, NULL };
761 /* b->a_group is an unexpanded entry name, expanded it should be an
762 * entry with objectclass group* and we test to see if odn is one of
763 * the values in the attribute group
765 /* see if asker is listed in dnattr */
766 if ( b->a_group_style == ACL_STYLE_REGEX ) {
767 string_expand(&bv, b->a_group_pat.bv_val, e->e_ndn, matches);
768 if ( dnNormalize2(NULL, &bv, &ndn) != LDAP_SUCCESS ) {
769 /* did not expand to a valid dn */
777 rc = backend_group(be, conn, op, e, &bv, &op->o_ndn,
778 b->a_group_oc, b->a_group_at);
787 if ( b->a_set_pat.bv_len != 0 ) {
788 if (aci_match_set( &b->a_set_pat, be, e, conn, op, 0 ) == 0) {
793 if ( b->a_authz.sai_ssf ) {
795 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
796 "acl_mask: conn %d check a_authz.sai_ssf: ACL %u > OP %u\n",
797 conn->c_connid, b->a_authz.sai_ssf, op->o_ssf ));
799 Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
800 b->a_authz.sai_ssf, op->o_ssf, 0 );
802 if ( b->a_authz.sai_ssf > op->o_ssf ) {
807 if ( b->a_authz.sai_transport_ssf ) {
809 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
810 "acl_mask: conn %d check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
811 conn->c_connid, b->a_authz.sai_transport_ssf, op->o_transport_ssf ));
813 Debug( LDAP_DEBUG_ACL,
814 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
815 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
817 if ( b->a_authz.sai_transport_ssf > op->o_transport_ssf ) {
822 if ( b->a_authz.sai_tls_ssf ) {
824 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
825 "acl_mask: conn %d check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
826 conn->c_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf ));
828 Debug( LDAP_DEBUG_ACL,
829 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
830 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
832 if ( b->a_authz.sai_tls_ssf > op->o_tls_ssf ) {
837 if ( b->a_authz.sai_sasl_ssf ) {
839 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
840 "acl_mask: conn %d check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
841 conn->c_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf ));
843 Debug( LDAP_DEBUG_ACL,
844 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
845 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
847 if ( b->a_authz.sai_sasl_ssf > op->o_sasl_ssf ) {
852 #ifdef SLAPD_ACI_ENABLED
853 if ( b->a_aci_at != NULL ) {
855 slap_access_t grant, deny, tgrant, tdeny;
857 /* this case works different from the others above.
858 * since aci's themselves give permissions, we need
859 * to first check b->a_access_mask, the ACL's access level.
862 if( op->o_ndn.bv_len == 0 ) {
866 if ( e->e_dn == NULL ) {
870 /* first check if the right being requested
871 * is allowed by the ACL clause.
873 if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
877 /* get the aci attribute */
878 at = attr_find( e->e_attrs, b->a_aci_at );
883 /* start out with nothing granted, nothing denied */
887 /* the aci is an multi-valued attribute. The
888 * rights are determined by OR'ing the individual
889 * rights given by the acis.
891 for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
892 if (aci_mask( be, conn, op,
893 e, desc, val, &at->a_vals[i],
894 matches, &grant, &deny ) != 0)
901 /* remove anything that the ACL clause does not allow */
902 tgrant &= b->a_access_mask & ACL_PRIV_MASK;
903 tdeny &= ACL_PRIV_MASK;
905 /* see if we have anything to contribute */
906 if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) {
910 /* this could be improved by changing acl_mask so that it can deal with
911 * by clauses that return grant/deny pairs. Right now, it does either
912 * additive or subtractive rights, but not both at the same time. So,
913 * we need to combine the grant/deny pair into a single rights mask in
914 * a smart way: if either grant or deny is "empty", then we use the
915 * opposite as is, otherwise we remove any denied rights from the grant
916 * rights mask and construct an additive mask.
918 if (ACL_IS_INVALID(tdeny)) {
919 modmask = tgrant | ACL_PRIV_ADDITIVE;
921 } else if (ACL_IS_INVALID(tgrant)) {
922 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
925 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
931 modmask = b->a_access_mask;
935 LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
936 "acl_mask: conn %d [%d] applying %s (%s)\n",
937 conn->c_connid, i, accessmask2str( modmask, accessmaskbuf),
938 b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
939 ? "break" : "stop" ));
941 Debug( LDAP_DEBUG_ACL,
942 "<= acl_mask: [%d] applying %s (%s)\n",
943 i, accessmask2str( modmask, accessmaskbuf ),
944 b->a_type == ACL_CONTINUE
946 : b->a_type == ACL_BREAK
953 if( ACL_IS_ADDITIVE(modmask) ) {
955 ACL_PRIV_SET( *mask, modmask );
958 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
960 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
961 /* substract privs */
962 ACL_PRIV_CLR( *mask, modmask );
965 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
973 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
974 "acl_mask: conn %d [%d] mask: %s\n",
975 conn->c_connid, i, accessmask2str( *mask, accessmaskbuf) ));
977 Debug( LDAP_DEBUG_ACL,
978 "<= acl_mask: [%d] mask: %s\n",
979 i, accessmask2str(*mask, accessmaskbuf), 0 );
982 if( b->a_type == ACL_CONTINUE ) {
985 } else if ( b->a_type == ACL_BREAK ) {
993 /* implicit "by * none" clause */
997 LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
998 "acl_mask: conn %d no more <who> clauses, returning %d (stop)\n",
999 conn->c_connid, accessmask2str( *mask, accessmaskbuf) ));
1001 Debug( LDAP_DEBUG_ACL,
1002 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1003 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1009 * acl_check_modlist - check access control on the given entry to see if
1010 * it allows the given modifications by the user associated with op.
1011 * returns 1 if mods allowed ok
1012 * 0 mods not allowed
1021 Modifications *mlist
1026 assert( be != NULL );
1028 /* short circuit root database access */
1029 if ( be_isroot( be, &op->o_ndn ) ) {
1031 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
1032 "acl_check_modlist: conn %d access granted to root user\n",
1035 Debug( LDAP_DEBUG_ACL,
1036 "<= acl_access_allowed: granted to database root\n",
1042 /* use backend default access if no backend acls */
1043 if( be != NULL && be->be_acl == NULL ) {
1045 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1046 "acl_check_modlist: conn %d backend default %s access %s to \"%s\"\n",
1047 conn->c_connid, access2str( ACL_WRITE ),
1048 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val ));
1050 Debug( LDAP_DEBUG_ACL,
1051 "=> access_allowed: backend default %s access %s to \"%s\"\n",
1052 access2str( ACL_WRITE ),
1053 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1055 return be->be_dfltaccess >= ACL_WRITE;
1058 /* be is always non-NULL */
1059 /* use global default access if no global acls */
1060 } else if ( be == NULL && global_acl == NULL ) {
1062 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1063 "acl_check_modlist: conn %d global default %s access %s to \"%s\"\n",
1064 conn->c_connid, access2str( ACL_WRITE ),
1065 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn ));
1067 Debug( LDAP_DEBUG_ACL,
1068 "=> access_allowed: global default %s access %s to \"%s\"\n",
1069 access2str( ACL_WRITE ),
1070 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1072 return global_default_access >= ACL_WRITE;
1076 for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1078 * no-user-modification operational attributes are ignored
1079 * by ACL_WRITE checking as any found here are not provided
1082 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1084 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1085 "acl_check_modlist: conn %d no-user-mod %s: modify access granted\n",
1086 conn->c_connid, mlist->sml_desc->ad_cname.bv_val ));
1088 Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1089 " modify access granted\n",
1090 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1095 switch ( mlist->sml_op ) {
1096 case LDAP_MOD_REPLACE:
1098 if ( mlist->sml_bvalues == NULL ) {
1101 for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ ) {
1102 if ( ! access_allowed( be, conn, op, e,
1103 mlist->sml_desc, bv, ACL_WRITE ) )
1110 case LDAP_MOD_DELETE:
1111 if ( mlist->sml_bvalues == NULL ) {
1112 if ( ! access_allowed( be, conn, op, e,
1113 mlist->sml_desc, NULL, ACL_WRITE ) )
1119 for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ ) {
1120 if ( ! access_allowed( be, conn, op, e,
1121 mlist->sml_desc, bv, ACL_WRITE ) )
1134 aci_bvstrdup( struct berval *bv )
1138 s = (char *)ch_malloc(bv->bv_len + 1);
1140 AC_MEMCPY(s, bv->bv_val, bv->bv_len);
1146 #ifdef SLAPD_ACI_ENABLED
1154 res = strncasecmp( s, bv->bv_val, bv->bv_len );
1158 if (len > (int)bv->bv_len)
1160 if (len < (int)bv->bv_len)
1168 struct berval *list,
1182 while (len >= 0 && --ix >= 0) {
1183 while (--len >= 0 && *p++ != sep) ;
1185 while (len >= 0 && *p == ' ') {
1196 while (--len >= 0 && *p != sep) {
1200 while (bv->bv_len > 0 && *--p == ' ')
1206 aci_set_gather (void *cookie, char *name, struct berval *attr)
1208 AciSetCookie *cp = cookie;
1209 BVarray bvals = NULL;
1210 struct berval bv, ndn;
1212 /* this routine needs to return the bervals instead of
1213 * plain strings, since syntax is not known. It should
1214 * also return the syntax or some "comparison cookie".
1218 bv.bv_len = strlen( name );
1219 if (dnNormalize2(NULL, &bv, &ndn) == LDAP_SUCCESS) {
1221 AttributeDescription *desc = NULL;
1222 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1223 backend_attribute(cp->be, NULL, NULL,
1224 cp->e, &ndn, desc, &bvals);
1233 struct berval *subj,
1241 struct berval set = { 0, NULL };
1243 AciSetCookie cookie;
1246 ber_dupbv( &set, subj );
1248 struct berval subjdn, ndn = { 0, NULL };
1249 struct berval setat;
1252 AttributeDescription *desc = NULL;
1254 /* format of string is "entry/setAttrName" */
1255 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1258 /* FIXME: If dnNormalize was based on ldap_bv2dn
1259 * instead of ldap_str2dn and would honor the bv_len
1260 * we could skip this step and not worry about the
1261 * unterminated string.
1263 char *s = ch_malloc(subjdn.bv_len + 1);
1264 AC_MEMCPY(s, subjdn.bv_val, subjdn.bv_len);
1268 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1269 setat.bv_val = SLAPD_ACI_SET_ATTR;
1270 setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1272 if ( setat.bv_val != NULL ) {
1273 if ( dnNormalize2(NULL, &subjdn, &ndn) == LDAP_SUCCESS
1274 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1276 backend_attribute(be, NULL, NULL, e,
1277 &ndn, desc, &bvals);
1278 if ( bvals != NULL ) {
1279 if ( bvals[0].bv_val != NULL ) {
1282 bvals[0].bv_val = NULL;
1283 for (i=1;bvals[i].bv_val;i++);
1284 bvals[0].bv_val = bvals[i-1].bv_val;
1285 bvals[i-1].bv_val = NULL;
1287 bvarray_free(bvals);
1293 ch_free(subjdn.bv_val);
1296 if (set.bv_val != NULL) {
1301 rc = (slap_set_filter(aci_set_gather, &cookie, &set,
1302 op->o_ndn.bv_val, e->e_ndn, NULL) > 0);
1303 ch_free(set.bv_val);
1308 #ifdef SLAPD_ACI_ENABLED
1310 aci_list_map_rights(
1311 struct berval *list )
1318 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1321 switch (*bv.bv_val) {
1323 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1326 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1327 * the right 's' to mean "set", but in the examples states
1328 * that the right 's' means "search". The latter definition
1331 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1334 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1337 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1340 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
1341 * define any equivalent to the AUTH right, so I've just used
1344 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1356 struct berval *list,
1358 struct berval *val )
1360 struct berval bv, left, right;
1363 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1364 if (aci_get_part(&bv, 0, '=', &left) < 0
1365 || aci_get_part(&bv, 1, '=', &right) < 0)
1367 if (aci_strbvcmp(attr, &bv) == 0)
1369 } else if (val == NULL) {
1370 if (aci_strbvcmp(attr, &left) == 0)
1373 if (aci_strbvcmp(attr, &left) == 0) {
1374 /* this is experimental code that implements a
1375 * simple (prefix) match of the attribute value.
1376 * the ACI draft does not provide for aci's that
1377 * apply to specific values, but it would be
1378 * nice to have. If the <attr> part of an aci's
1379 * rights list is of the form <attr>=<value>,
1380 * that means the aci applies only to attrs with
1381 * the given value. Furthermore, if the attr is
1382 * of the form <attr>=<value>*, then <value> is
1383 * treated as a prefix, and the aci applies to
1384 * any value with that prefix.
1386 * Ideally, this would allow r.e. matches.
1388 if (aci_get_part(&right, 0, '*', &left) < 0
1389 || right.bv_len <= left.bv_len)
1391 if (aci_strbvcmp(val->bv_val, &right) == 0)
1393 } else if (val->bv_len >= left.bv_len) {
1394 if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1403 static slap_access_t
1404 aci_list_get_attr_rights(
1405 struct berval *list,
1407 struct berval *val )
1413 /* loop through each rights/attr pair, skip first part (action) */
1415 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1416 if (aci_list_has_attr(&bv, attr, val) == 0)
1418 if (aci_get_part(list, i, ';', &bv) < 0)
1420 mask |= aci_list_map_rights(&bv);
1426 aci_list_get_rights(
1427 struct berval *list,
1430 slap_access_t *grant,
1431 slap_access_t *deny )
1433 struct berval perm, actn;
1434 slap_access_t *mask;
1437 if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
1444 /* loop through each permissions clause */
1445 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1446 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1448 if (aci_strbvcmp( "grant", &actn ) == 0) {
1450 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
1457 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1458 *mask |= aci_list_get_attr_rights(&perm, "[all]", NULL);
1465 struct berval *subj,
1466 struct berval *defgrpoc,
1467 struct berval *defgrpat,
1477 struct berval grpoc;
1478 struct berval grpat;
1479 ObjectClass *grp_oc = NULL;
1480 AttributeDescription *grp_ad = NULL;
1484 /* format of string is "group/objectClassValue/groupAttrName" */
1485 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1489 subjdn = aci_bvstrdup(&bv);
1490 if (subjdn == NULL) {
1494 if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1498 if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1502 rc = slap_bv2ad( &grpat, &grp_ad, &text );
1503 if( rc != LDAP_SUCCESS ) {
1509 grp_oc = oc_bvfind( &grpoc );
1511 if (grp_oc != NULL && grp_ad != NULL ) {
1513 bv.bv_val = (char *)ch_malloc(1024);
1515 string_expand(&bv, subjdn, e->e_ndn, matches);
1516 if ( dnNormalize2(NULL, &bv, &ndn) == LDAP_SUCCESS ) {
1517 rc = (backend_group(be, conn, op, e, &ndn, &op->o_ndn, grp_oc, grp_ad) == 0);
1528 static struct berval GroupClass = {
1529 sizeof(SLAPD_GROUP_CLASS)-1, SLAPD_GROUP_CLASS };
1530 static struct berval GroupAttr = {
1531 sizeof(SLAPD_GROUP_ATTR)-1, SLAPD_GROUP_ATTR };
1532 static struct berval RoleClass = {
1533 sizeof(SLAPD_ROLE_CLASS)-1, SLAPD_ROLE_CLASS };
1534 static struct berval RoleAttr = {
1535 sizeof(SLAPD_ROLE_ATTR)-1, SLAPD_ROLE_ATTR };
1543 AttributeDescription *desc,
1546 regmatch_t *matches,
1547 slap_access_t *grant,
1551 struct berval bv, perms, sdn;
1553 char *attr = desc->ad_cname.bv_val;
1555 assert( attr != NULL );
1557 /* parse an aci of the form:
1558 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1560 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1561 a full description of the format for this attribute.
1563 For now, this routine only supports scope=entry.
1566 /* check that the aci has all 5 components */
1567 if (aci_get_part(aci, 4, '#', NULL) < 0)
1570 /* check that the aci family is supported */
1571 if (aci_get_part(aci, 0, '#', &bv) < 0)
1574 /* check that the scope is "entry" */
1575 if (aci_get_part(aci, 1, '#', &bv) < 0
1576 || aci_strbvcmp( "entry", &bv ) != 0)
1581 /* get the list of permissions clauses, bail if empty */
1582 if (aci_get_part(aci, 2, '#', &perms) <= 0)
1585 /* check if any permissions allow desired access */
1586 if (aci_list_get_rights(&perms, attr, val, grant, deny) == 0)
1589 /* see if we have a DN match */
1590 if (aci_get_part(aci, 3, '#', &bv) < 0)
1593 if (aci_get_part(aci, 4, '#', &sdn) < 0)
1596 if (aci_strbvcmp( "access-id", &bv ) == 0) {
1599 if ( dnNormalize2(NULL, &sdn, &ndn) == LDAP_SUCCESS ) {
1600 if (strcasecmp(op->o_ndn.bv_val, ndn.bv_val) != 0)
1607 if (aci_strbvcmp( "self", &bv ) == 0) {
1608 if (strcmp(op->o_ndn.bv_val, e->e_ndn) == 0)
1611 } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
1612 char *dnattr = aci_bvstrdup(&sdn);
1614 AttributeDescription *ad = NULL;
1617 rc = slap_str2ad( dnattr, &ad, &text );
1620 if( rc != LDAP_SUCCESS ) {
1628 for(at = attrs_find( e->e_attrs, ad );
1630 at = attrs_find( at->a_next, ad ) )
1632 if (value_find( ad, at->a_vals, &bv) == 0 ) {
1641 } else if (aci_strbvcmp( "group", &bv ) == 0) {
1642 if (aci_group_member(&sdn, &GroupClass, &GroupAttr, be, e, conn, op, matches))
1645 } else if (aci_strbvcmp( "role", &bv ) == 0) {
1646 if (aci_group_member(&sdn, &RoleClass, &RoleAttr, be, e, conn, op, matches))
1649 } else if (aci_strbvcmp( "set", &bv ) == 0) {
1650 if (aci_match_set(&sdn, be, e, conn, op, 0))
1653 } else if (aci_strbvcmp( "set-ref", &bv ) == 0) {
1654 if (aci_match_set(&sdn, be, e, conn, op, 1))
1662 #endif /* SLAPD_ACI_ENABLED */
1669 regmatch_t *matches)
1677 bv->bv_val[0] = '\0';
1678 bv->bv_len--; /* leave space for lone $ */
1681 for ( dp = bv->bv_val, sp = pat; size < bv->bv_len && *sp ; sp++) {
1682 /* did we previously see a $ */
1687 } else if (*sp >= '0' && *sp <= '9' ) {
1694 i = matches[n].rm_so;
1695 l = matches[n].rm_eo;
1696 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
1714 /* must have ended with a single $ */
1723 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1724 "string_expand: pattern = %s\n", pat ));
1725 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1726 "string_expand: expanded = %s\n", bv->bv_val ));
1728 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
1729 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
1735 char *pat, /* pattern to expand and match against */
1736 char *str, /* string to match against pattern */
1737 char *buf, /* buffer with $N expansion variables */
1738 regmatch_t *matches /* offsets in buffer for $N expansion variables */
1743 struct berval bv = {sizeof(newbuf), newbuf};
1746 if(str == NULL) str = "";
1748 string_expand(&bv, pat, buf, matches);
1749 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1751 regerror(rc, &re, error, sizeof(error));
1754 LDAP_LOG(( "aci", LDAP_LEVEL_ERR,
1755 "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
1758 Debug( LDAP_DEBUG_TRACE,
1759 "compile( \"%s\", \"%s\") failed %s\n",
1765 rc = regexec(&re, str, 0, NULL, 0);
1769 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1770 "regex_matches: string: %s\n", str ));
1771 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1772 "regex_matches: rc: %d %s\n",
1773 rc, rc ? "matches" : "no matches" ));
1775 Debug( LDAP_DEBUG_TRACE,
1776 "=> regex_matches: string: %s\n", str, 0, 0 );
1777 Debug( LDAP_DEBUG_TRACE,
1778 "=> regex_matches: rc: %d %s\n",
1779 rc, !rc ? "matches" : "no matches", 0 );