1 /* acl.c - routines to parse and check acl's */
4 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
18 static AccessControl * acl_get(
19 AccessControl *ac, int *count,
20 Backend *be, Operation *op,
22 int nmatches, regmatch_t *matches );
24 static slap_control_t acl_mask(
25 AccessControl *ac, slap_access_mask_t *mask,
26 Backend *be, Connection *conn, Operation *op,
27 Entry *e, char *attr, struct berval *val,
28 regmatch_t *matches );
30 #ifdef SLAPD_ACI_ENABLED
31 static int aci_access_allowed(
34 Entry *e, char *attr, struct berval *aci,
35 regmatch_t *matches );
38 static int regex_matches(char *pat, char *str, char *buf, regmatch_t *matches);
39 static void string_expand(char *newbuf, int bufsiz, char *pattern,
40 char *match, regmatch_t *matches);
44 * access_allowed - check whether op->o_ndn is allowed the requested access
45 * to entry e, attribute attr, value val. if val is null, access to
46 * the whole attribute is assumed (all values).
48 * This routine loops through all access controls and calls
49 * acl_mask() on each applicable access control.
50 * The loop exits when a definitive answer is reached or
51 * or no more controls remain.
71 char accessmaskbuf[ACCESSMASK_MAXLEN];
72 slap_access_mask_t mask;
73 slap_control_t control;
75 regmatch_t matches[MAXREMATCHES];
77 Debug( LDAP_DEBUG_ACL,
78 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
84 assert( attr != NULL );
85 assert( access > ACL_NONE );
87 /* grant database root access */
88 if ( be != NULL && be_isroot( be, op->o_ndn ) ) {
89 Debug( LDAP_DEBUG_ACL,
90 "<= root access granted\n",
95 /* no user modify operational attributes are ignored by ACL checking */
96 if ( oc_check_no_usermod_attr( attr ) ) {
97 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
98 " %s access granted\n",
103 /* use backend default access if no backend acls */
104 if( be != NULL && be->be_acl == NULL ) {
105 Debug( LDAP_DEBUG_ACL,
106 "=> access_allowed: backend default %s access %s to \"%s\"\n",
107 access2str( access ),
108 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn );
110 return be->be_dfltaccess >= access;
113 /* be is always non-NULL */
114 /* use global default access if no global acls */
115 } else if ( be == NULL && global_acl == NULL ) {
116 Debug( LDAP_DEBUG_ACL,
117 "=> access_allowed: global default %s access %s to \"%s\"\n",
118 access2str( access ),
119 global_default_access >= access ? "granted" : "denied", op->o_dn );
121 return global_default_access >= access;
126 memset(matches, 0, sizeof(matches));
132 while( a = acl_get( a, &count, be, op, e, attr, MAXREMATCHES, matches ) )
136 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
137 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
138 (int)matches[i].rm_so, (int)matches[i].rm_eo );
140 if( matches[i].rm_so <= matches[0].rm_eo ) {
142 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
143 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
146 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
149 control = acl_mask( a, &mask, be, conn, op,
150 e, attr, val, matches );
152 if ( control != ACL_BREAK ) {
156 memset(matches, 0, sizeof(matches));
159 if ( ACL_IS_INVALID( mask ) ) {
160 Debug( LDAP_DEBUG_ACL,
161 "=> access_allowed: \"%s\" (%s) invalid!\n",
165 } else if ( control == ACL_BREAK ) {
166 Debug( LDAP_DEBUG_ACL,
167 "=> access_allowed: no more rules\n", 0, 0, 0);
171 Debug( LDAP_DEBUG_ACL,
172 "=> access_allowed: %s access %s by %s\n",
173 access2str( access ),
174 ACL_GRANT(mask, access) ? "granted" : "denied",
175 accessmask2str( mask, accessmaskbuf ) );
177 return ACL_GRANT(mask, access);
181 * acl_get - return the acl applicable to entry e, attribute
182 * attr. the acl returned is suitable for use in subsequent calls to
183 * acl_access_allowed().
186 static AccessControl *
199 assert( count != NULL );
214 for ( ; a != NULL; a = a->acl_next ) {
217 if (a->acl_dn_pat != NULL) {
218 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
219 *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub );
221 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0)) {
225 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
230 if ( a->acl_filter != NULL ) {
231 if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
236 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
239 if ( attr == NULL || a->acl_attrs == NULL ||
240 charray_inlist( a->acl_attrs, attr ) )
242 Debug( LDAP_DEBUG_ACL,
243 "<= acl_get: [%d] acl %s attr: %s\n",
244 *count, e->e_dn, attr );
247 matches[0].rm_so = matches[0].rm_eo = -1;
250 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
256 * acl_mask - modifies mask based upon the given acl and the
257 * requested access to entry e, attribute attr, value val. if val
258 * is null, access to the whole attribute is assumed (all values).
260 * returns 0 access NOT allowed
264 static slap_control_t
267 slap_access_mask_t *mask,
279 char accessmaskbuf[ACCESSMASK_MAXLEN];
282 assert( mask != NULL );
284 Debug( LDAP_DEBUG_ACL,
285 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
288 Debug( LDAP_DEBUG_ACL,
289 "=> acl_mask: to value \"%s\" by \"%s\", (%s) \n",
290 val ? val->bv_val : "*",
291 op->o_ndn ? op->o_ndn : "",
292 accessmask2str( *mask, accessmaskbuf ) );
294 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
295 slap_access_mask_t oldmask, modmask;
297 ACL_INVALIDATE( modmask );
299 /* AND <who> clauses */
300 if ( b->a_dn_pat != NULL ) {
301 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
304 * if access applies to the entry itself, and the
305 * user is bound as somebody in the same namespace as
306 * the entry, OR the given dn matches the dn pattern
308 if ( strcmp( b->a_dn_pat, "anonymous" ) == 0 ) {
309 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
313 } else if ( strcmp( b->a_dn_pat, "users" ) == 0 ) {
314 if (op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
318 } else if ( strcmp( b->a_dn_pat, "self" ) == 0 ) {
319 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
323 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn ) != 0 ) {
327 } else if ( strcmp( b->a_dn_pat, "*" ) != 0 ) {
328 int ret = regex_matches( b->a_dn_pat,
329 op->o_ndn, e->e_ndn, matches );
337 if ( b->a_sockurl_pat != NULL ) {
338 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
339 b->a_sockurl_pat, 0, 0 );
341 if ( strcmp( b->a_sockurl_pat, "*" ) != 0 &&
342 !regex_matches( b->a_sockurl_pat, conn->c_listener_url,
343 e->e_ndn, matches ) )
349 if ( b->a_domain_pat != NULL ) {
350 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
351 b->a_domain_pat, 0, 0 );
353 if ( strcmp( b->a_domain_pat, "*" ) != 0 &&
354 !regex_matches( b->a_domain_pat, conn->c_peer_domain,
355 e->e_ndn, matches ) )
361 if ( b->a_peername_pat != NULL ) {
362 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
363 b->a_peername_pat, 0, 0 );
365 if ( strcmp( b->a_peername_pat, "*" ) != 0 &&
366 !regex_matches( b->a_peername_pat, conn->c_peer_name,
367 e->e_ndn, matches ) )
373 if ( b->a_sockname_pat != NULL ) {
374 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
375 b->a_sockname_pat, 0, 0 );
377 if ( strcmp( b->a_sockname_pat, "*" ) != 0 &&
378 !regex_matches( b->a_sockname_pat, conn->c_sock_name,
379 e->e_ndn, matches ) )
385 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
389 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
392 bv.bv_val = op->o_ndn;
393 bv.bv_len = strlen( bv.bv_val );
395 /* see if asker is listed in dnattr */
396 if ( (at = attr_find( e->e_attrs, b->a_dn_at )) != NULL &&
397 value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0 )
400 (val == NULL || value_cmp( &bv, val, at->a_syntax, 2 )) )
405 /* asker not listed in dnattr - check for self access */
406 } else if ( ! b->a_dn_self || val == NULL ||
407 value_cmp( &bv, val, at->a_syntax, 2 ) != 0 )
413 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
416 /* b->a_group is an unexpanded entry name, expanded it should be an
417 * entry with objectclass group* and we test to see if odn is one of
418 * the values in the attribute group
420 /* see if asker is listed in dnattr */
421 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
422 if ( dn_normalize(buf) == NULL ) {
423 /* did not expand to a valid dn */
427 if (backend_group(be, e, buf, op->o_ndn,
428 b->a_group_oc, b->a_group_at) != 0)
434 #ifdef SLAPD_ACI_ENABLED
435 if ( b->a_aci_at != NULL ) {
438 /* this case works different from the others above.
439 * since aci's themselves give permissions, we need
440 * to first check b->a_mask, the ACL's access level.
443 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
447 if ( e->e_dn == NULL ) {
451 /* first check if the right being requested is
452 * higher than allowed by the ACL clause.
454 if ( ! ACL_GRANT( b->a_mask, access ) ) {
458 /* get the aci attribute */
459 at = attr_find( e->e_attrs, b->a_aci_at );
464 /* the aci is an multi-valued attribute. The
465 * rights are determined by OR'ing the individual
466 * rights given by the acis.
468 for ( i = 0; at->a_vals[i] != NULL; i++ ) {
469 if ( aci_access_allowed( be, op,
470 e, attr, at->a_vals[i],
473 Debug( LDAP_DEBUG_ACL,
474 "<= acl_mask: matched by clause #%d access granted\n",
480 if( ACL_IS_INVALID( modmask ) ) {
491 Debug( LDAP_DEBUG_ACL,
492 "<= acl_mask: [%d] applying %s (%s)\n",
493 i, accessmask2str( modmask, accessmaskbuf ),
494 b->a_type == ACL_CONTINUE
496 : b->a_type == ACL_BREAK
503 if( ACL_IS_ADDITIVE(modmask) ) {
505 ACL_PRIV_SET( *mask, modmask );
508 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
510 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
511 /* substract privs */
512 ACL_PRIV_CLR( *mask, modmask );
515 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
522 Debug( LDAP_DEBUG_ACL,
523 "<= acl_mask: [%d] mask: %s\n",
524 i, accessmask2str(*mask, accessmaskbuf), 0 );
526 if( b->a_type == ACL_CONTINUE ) {
529 } else if ( b->a_type == ACL_BREAK ) {
537 Debug( LDAP_DEBUG_ACL,
538 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
539 accessmask2str(*mask, accessmaskbuf), 0, 0 );
544 * acl_check_modlist - check access control on the given entry to see if
545 * it allows the given modifications by the user associated with op.
546 * returns 1 if mods allowed ok
561 assert( be != NULL );
563 /* short circuit root database access */
564 if ( be_isroot( be, op->o_ndn ) ) {
565 Debug( LDAP_DEBUG_ACL,
566 "<= acl_access_allowed: granted to database root\n",
571 /* use backend default access if no backend acls */
572 if( be != NULL && be->be_acl == NULL ) {
573 Debug( LDAP_DEBUG_ACL,
574 "=> access_allowed: backend default %s access %s to \"%s\"\n",
575 access2str( ACL_WRITE ),
576 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
578 return be->be_dfltaccess >= ACL_WRITE;
581 /* be is always non-NULL */
582 /* use global default access if no global acls */
583 } else if ( be == NULL && global_acl == NULL ) {
584 Debug( LDAP_DEBUG_ACL,
585 "=> access_allowed: global default %s access %s to \"%s\"\n",
586 access2str( ACL_WRITE ),
587 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
589 return global_default_access >= ACL_WRITE;
593 for ( ; mlist != NULL; mlist = mlist->ml_next ) {
594 /* the lastmod attributes are ignored by ACL checking */
595 if ( oc_check_no_usermod_attr( mlist->ml_type ) ) {
596 Debug( LDAP_DEBUG_ACL, "Operational attribute: %s access allowed\n",
597 mlist->ml_type, 0, 0 );
601 switch ( mlist->ml_op & ~LDAP_MOD_BVALUES ) {
602 case LDAP_MOD_REPLACE:
604 if ( mlist->ml_bvalues == NULL ) {
607 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
608 if ( ! access_allowed( be, conn, op, e,
609 mlist->ml_type, mlist->ml_bvalues[i],
617 case LDAP_MOD_DELETE:
618 if ( mlist->ml_bvalues == NULL ) {
619 if ( ! access_allowed( be, conn, op, e,
620 mlist->ml_type, NULL,
627 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
628 if ( ! access_allowed( be, conn, op, e,
629 mlist->ml_type, mlist->ml_bvalues[i],
642 #ifdef SLAPD_ACI_ENABLED
644 aci_bvstrdup (struct berval *bv)
648 s = (char *)ch_malloc(bv->bv_len + 1);
650 memcpy(s, bv->bv_val, bv->bv_len);
657 aci_strbvcmp (char *s, struct berval *bv)
661 res = strncasecmp( s, bv->bv_val, bv->bv_len );
665 if (len > bv->bv_len)
667 if (len < bv->bv_len)
673 aci_get_part (struct berval *list, int ix, char sep, struct berval *bv)
684 while (len >= 0 && --ix >= 0) {
685 while (--len >= 0 && *p++ != sep) ;
687 while (len >= 0 && *p == ' ') {
698 while (--len >= 0 && *p != sep) {
702 while (bv->bv_len > 0 && *--p == ' ')
710 slap_access_t access,
717 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
720 switch (*bv.bv_val) {
725 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
726 * the right 's' to mean "set", but in the examples states
727 * that the right 's' means "search". The latter definition
739 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
740 * define any equivalent to the AUTH right, so I've just used
750 #ifdef SLAPD_ACI_DISCRETE_RIGHTS
751 if (right & access) {
757 if (ACL_GRANT(right, access))
767 #ifdef SLAPD_ACI_DISCRETE_RIGHTS
775 aci_list_has_attr (struct berval *list, char *attr)
780 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
781 if (aci_strbvcmp(attr, &bv) == 0) {
789 aci_list_has_attr_right (struct berval *list, char *attr, int access, int action)
794 /* loop through each rights/attr pair, skip first part (action) */
796 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
797 if (aci_list_has_attr(&bv, attr) == 0)
800 if (aci_get_part(list, i, ';', &bv) < 0)
802 if (aci_list_has_right(&bv, access, action) != 0)
809 aci_list_has_permission(
812 slap_access_t access)
814 struct berval perm, actn;
815 int i, action, specific, general;
817 if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
821 /* loop through each permissions clause */
822 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
823 if (aci_get_part(&perm, 0, ';', &actn) < 0)
825 if (aci_strbvcmp( "grant", &actn ) == 0) {
827 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
833 specific = aci_list_has_attr_right(&perm, attr, access, action);
837 general = aci_list_has_attr_right(&perm, "[all]", access, action);
856 char *subjdn, *grpdn;
859 /* format of string is "group/objectClassValue/groupAttrName" */
860 if (aci_get_part(subj, 0, '/', &bv) < 0)
862 subjdn = aci_bvstrdup(&bv);
866 if (aci_get_part(subj, 1, '/', &bv) < 0)
867 grpoc = ch_strdup(grpoc);
869 grpoc = aci_bvstrdup(&bv);
871 if (aci_get_part(subj, 2, '/', &bv) < 0)
872 grpat = ch_strdup(grpat);
874 grpat = aci_bvstrdup(&bv);
876 grpdn = (char *)ch_malloc(1024);
877 if (grpoc != NULL && grpat != NULL && grpdn != NULL) {
878 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
879 if ( dn_normalize(grpdn) != NULL ) {
880 rc = (backend_group(be, e, grpdn, op->o_ndn, grpoc, grpat) == 0);
899 slap_access_t access,
903 struct berval bv, perms, sdn;
907 Debug( LDAP_DEBUG_ACL,
908 "=> aci_access_allowed: %s access to entry \"%s\"\n",
909 access2str( access ), e->e_dn, 0 );
911 Debug( LDAP_DEBUG_ACL,
912 "=> aci_access_allowed: %s access to attribute \"%s\" by \"%s\"\n",
913 access2str( access ),
915 op->o_ndn ? op->o_ndn : "" );
917 /* parse an aci of the form:
918 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
920 See draft-ietf-ldapext-aci-model-0.3.txt section 9.1 for
921 a full description of the format for this attribute.
923 For now, this routine only supports scope=entry.
926 /* check that the aci has all 5 components */
927 if (aci_get_part(aci, 4, '#', NULL) < 0)
930 /* check that the scope is "entry" */
931 if (aci_get_part(aci, 1, '#', &bv) < 0
932 || aci_strbvcmp( "entry", &bv ) != 0)
937 /* get the list of permissions clauses, bail if empty */
938 if (aci_get_part(aci, 2, '#', &perms) <= 0)
941 /* check if any permissions allow desired access */
942 if (aci_list_has_permission(&perms, attr, access) == 0)
945 /* see if we have a DN match */
946 if (aci_get_part(aci, 3, '#', &bv) < 0)
949 if (aci_get_part(aci, 4, '#', &sdn) < 0)
951 if (aci_strbvcmp( "access-id", &bv ) == 0) {
952 subjdn = aci_bvstrdup(&sdn);
956 if ( dn_normalize(subjdn) != NULL )
957 rc = (strcasecmp(op->o_ndn, subjdn) == 0);
962 if (aci_strbvcmp( "self", &bv ) == 0) {
963 return(strcasecmp(op->o_ndn, e->e_ndn) == 0);
966 if (aci_strbvcmp( "group", &bv ) == 0) {
967 return(aci_group_member(&sdn, "groupOfNames", "member", be, e, op, matches));
970 if (aci_strbvcmp( "role", &bv ) == 0) {
971 return(aci_group_member(&sdn, "organizationalRole", "roleOccupant", be, e, op, matches));
976 #endif /* SLAPD_ACI_ENABLED */
993 bufsiz--; /* leave space for lone $ */
996 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
997 /* did we previously see a $ */
1002 } else if (*sp >= '0' && *sp <= '9' ) {
1009 i = matches[n].rm_so;
1010 l = matches[n].rm_eo;
1011 for ( ; size < 512 && i < l; size++, i++ ) {
1029 /* must have ended with a single $ */
1036 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
1037 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1042 char *pat, /* pattern to expand and match against */
1043 char *str, /* string to match against pattern */
1044 char *buf, /* buffer with $N expansion variables */
1045 regmatch_t *matches /* offsets in buffer for $N expansion variables */
1052 if(str == NULL) str = "";
1054 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1055 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1057 regerror(rc, &re, error, sizeof(error));
1059 Debug( LDAP_DEBUG_TRACE,
1060 "compile( \"%s\", \"%s\") failed %s\n",
1065 rc = regexec(&re, str, 0, NULL, 0);
1068 Debug( LDAP_DEBUG_TRACE,
1069 "=> regex_matches: string: %s\n", str, 0, 0 );
1070 Debug( LDAP_DEBUG_TRACE,
1071 "=> regex_matches: rc: %d %s\n",
1072 rc, !rc ? "matches" : "no matches", 0 );