1 /* acl.c - routines to parse and check acl's */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
13 #include <ac/string.h>
17 #ifdef SLAPD_ACI_ENABLED
18 int aci_access_allowed (struct berval *aci, char *attr, Backend *be, Entry *e,
19 Operation *op, int access, char *edn, regmatch_t *matches);
22 static int regex_matches(char *pat, char *str, char *buf, regmatch_t *matches);
23 static void string_expand(char *newbuf, int bufsiz, char *pattern,
24 char *match, regmatch_t *matches);
28 * access_allowed - check whether op->o_ndn is allowed the requested access
29 * to entry e, attribute attr, value val. if val is null, access to
30 * the whole attribute is assumed (all values). this routine finds
31 * the applicable acl and calls acl_access_allowed() to make the
34 * returns 0 access NOT allowed
53 regmatch_t matches[MAXREMATCHES];
63 Debug( LDAP_DEBUG_ACL, "\n=> access_allowed: entry (%s) attr (%s)\n",
66 /* the lastmod attributes are ignored by ACL checking */
67 if ( oc_check_no_usermod_attr( attr ) ) {
68 Debug( LDAP_DEBUG_ACL, "Operational attribute: %s access allowed\n",
73 memset(matches, 0, sizeof(matches));
75 a = acl_get_applicable( be, op, e, attr, MAXREMATCHES, matches );
78 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
79 Debug( LDAP_DEBUG_ARGS, "=> match[%d]: %d %d ", i,
80 (int)matches[i].rm_so, (int)matches[i].rm_eo );
82 if( matches[i].rm_so <= matches[0].rm_eo ) {
83 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
84 Debug( LDAP_DEBUG_ARGS, "%c", edn[n], 0, 0 );
87 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
91 rc = acl_access_allowed( a, attr, be, conn, e, val, op, access, edn, matches );
93 Debug( LDAP_DEBUG_ACL, "\n=> access_allowed: exit (%s) attr (%s)\n",
100 * acl_get_applicable - return the acl applicable to entry e, attribute
101 * attr. the acl returned is suitable for use in subsequent calls to
102 * acl_access_allowed().
119 Debug( LDAP_DEBUG_ACL, "\n=> acl_get: entry (%s) attr (%s)\n",
122 if ( be_isroot( be, op->o_ndn ) ) {
123 Debug( LDAP_DEBUG_ACL,
124 "<= acl_get: no acl applicable to database root\n", 0, 0,
131 Debug( LDAP_DEBUG_ARGS, "=> acl_get: edn %s\n", edn, 0, 0 );
133 /* check for a backend-specific acl that matches the entry */
134 for ( i = 1, a = be->be_acl; a != NULL; a = a->acl_next, i++ ) {
135 if (a->acl_dn_pat != NULL) {
136 Debug( LDAP_DEBUG_TRACE, "=> dnpat: [%d] %s nsub: %d\n",
137 i, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub);
139 if (regexec(&a->acl_dn_re, edn, nmatch, matches, 0)) {
143 Debug( LDAP_DEBUG_TRACE, "=> acl_get:[%d] backend ACL match\n",
148 if ( a->acl_filter != NULL ) {
149 if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
154 Debug( LDAP_DEBUG_ARGS, "=> acl_get: [%d] check attr %s\n", i, attr, 0);
156 if ( attr == NULL || a->acl_attrs == NULL ||
157 charray_inlist( a->acl_attrs, attr ) )
159 Debug( LDAP_DEBUG_ACL, "<= acl_get: [%d] backend acl %s attr: %s\n",
163 matches[0].rm_so = matches[0].rm_eo = -1;
166 /* check for a global acl that matches the entry */
167 for ( i = 1, a = global_acl; a != NULL; a = a->acl_next, i++ ) {
168 if (a->acl_dn_pat != NULL) {
169 Debug( LDAP_DEBUG_TRACE, "=> dn pat: [%d] %s nsub: %d\n",
170 i, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub);
172 if (regexec(&a->acl_dn_re, edn, nmatch, matches, 0)) {
176 Debug( LDAP_DEBUG_TRACE, "=> acl_get: [%d] global ACL match\n",
181 if ( a->acl_filter != NULL ) {
182 if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
187 Debug( LDAP_DEBUG_ARGS, "=> acl_get: [%d] check attr\n", i, 0, 0);
189 if ( attr == NULL || a->acl_attrs == NULL ||
190 charray_inlist( a->acl_attrs, attr ) )
192 Debug( LDAP_DEBUG_ACL, "<= acl_get: [%d] global acl %s attr: %s\n",
197 matches[0].rm_so = matches[0].rm_eo = -1;
200 Debug( LDAP_DEBUG_ACL, "<= acl_get: no match\n", 0, 0, 0 );
205 * acl_access_allowed - check whether the given acl allows dn the
206 * requested access to entry e, attribute attr, value val. if val
207 * is null, access to the whole attribute is assumed (all values).
209 * returns 0 access NOT allowed
231 Debug( LDAP_DEBUG_ACL,
232 "\n=> acl_access_allowed: %s access to entry \"%s\"\n",
233 access2str( access ), e->e_dn, 0 );
235 Debug( LDAP_DEBUG_ACL,
236 "\n=> acl_access_allowed: %s access to value \"%s\" by \"%s\"\n",
237 access2str( access ),
238 val ? val->bv_val : "any",
239 op->o_ndn ? op->o_ndn : "" );
241 if ( be_isroot( be, op->o_ndn ) ) {
242 Debug( LDAP_DEBUG_ACL,
243 "<= acl_access_allowed: granted to database root\n",
248 default_access = be->be_dfltaccess ? be->be_dfltaccess : global_default_access;
251 Debug( LDAP_DEBUG_ACL,
252 "<= acl_access_allowed: %s by default (no matching to)\n",
253 default_access >= access ? "granted" : "denied", 0, 0 );
254 return( default_access >= access );
257 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
258 /* AND <who> clauses */
259 if ( b->a_dn_pat != NULL ) {
260 Debug( LDAP_DEBUG_TRACE, "<= check a_dn_pat: %s\n",
263 * if access applies to the entry itself, and the
264 * user is bound as somebody in the same namespace as
265 * the entry, OR the given dn matches the dn pattern
267 if ( strcasecmp( b->a_dn_pat, "anonymous" ) == 0 ) {
268 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
272 } else if ( strcasecmp( b->a_dn_pat, "self" ) == 0 ) {
273 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
277 if ( e->e_dn == NULL || strcmp( edn, op->o_ndn ) != 0 ) {
281 } else if ( strcmp( b->a_dn_pat, ".*" ) != 0 &&
282 !regex_matches( b->a_dn_pat, op->o_ndn, edn, matches ) )
288 if ( b->a_sockurl_pat != NULL ) {
289 Debug( LDAP_DEBUG_ARGS, "<= check a_sockurl_pat: %s\n",
290 b->a_sockurl_pat, 0, 0 );
292 if ( strcmp( b->a_sockurl_pat, ".*" ) != 0 &&
293 !regex_matches( b->a_sockurl_pat, conn->c_listener_url,
300 if ( b->a_domain_pat != NULL ) {
301 Debug( LDAP_DEBUG_ARGS, "<= check a_domain_pat: %s\n",
302 b->a_domain_pat, 0, 0 );
304 if ( strcmp( b->a_domain_pat, ".*" ) != 0 &&
305 !regex_matches( b->a_domain_pat, conn->c_peer_domain,
312 if ( b->a_peername_pat != NULL ) {
313 Debug( LDAP_DEBUG_ARGS, "<= check a_peername_path: %s\n",
314 b->a_peername_pat, 0, 0 );
316 if ( strcmp( b->a_peername_pat, ".*" ) != 0 &&
317 !regex_matches( b->a_peername_pat, conn->c_peer_name,
324 if ( b->a_sockname_pat != NULL ) {
325 Debug( LDAP_DEBUG_ARGS, "<= check a_sockname_path: %s\n",
326 b->a_sockname_pat, 0, 0 );
328 if ( strcmp( b->a_sockname_pat, ".*" ) != 0 &&
329 !regex_matches( b->a_sockname_pat, conn->c_sock_name,
336 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
340 Debug( LDAP_DEBUG_ARGS, "<= check a_dn_at: %s\n",
343 bv.bv_val = op->o_ndn;
344 bv.bv_len = strlen( bv.bv_val );
346 /* see if asker is listed in dnattr */
347 if ( (at = attr_find( e->e_attrs, b->a_dn_at )) != NULL &&
348 value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0 )
350 if ( ACL_IS_SELF(b->a_access) &&
351 (val == NULL || value_cmp( &bv, val, at->a_syntax, 2 )) )
356 /* asker not listed in dnattr - check for self access */
357 } else if ( ! ACL_IS_SELF(b->a_access) || val == NULL ||
358 value_cmp( &bv, val, at->a_syntax, 2 ) != 0 )
364 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
367 /* b->a_group is an unexpanded entry name, expanded it should be an
368 * entry with objectclass group* and we test to see if odn is one of
369 * the values in the attribute group
371 /* see if asker is listed in dnattr */
372 string_expand(buf, sizeof(buf), b->a_group_pat, edn, matches);
373 if ( dn_normalize_case(buf) == NULL ) {
374 /* did not expand to a valid dn */
378 if (backend_group(be, e, buf, op->o_ndn,
379 b->a_group_oc, b->a_group_at) != 0)
385 #ifdef SLAPD_ACI_ENABLED
386 if ( b->a_aci_at != NULL ) {
389 /* this case works different from the others above.
390 * since aci's themselves give permissions, we need
391 * to first check b->a_access, the ACL's access level.
394 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
398 if ( e->e_dn == NULL ) {
402 /* first check if the right being requested is
403 * higher than allowed by the ACL clause.
405 if ( ! ACL_GRANT( b->a_access, access ) ) {
409 /* get the aci attribute */
410 at = attr_find( e->e_attrs, b->a_aci_at );
415 /* the aci is an multi-valued attribute. The
416 * rights are determined by OR'ing the individual
417 * rights given by the acis.
419 for ( i = 0; at->a_vals[i] != NULL; i++ ) {
420 if ( aci_access_allowed( at->a_vals[i], attr, be, e, op, access, edn, matches ) ) {
421 Debug( LDAP_DEBUG_ACL,
422 "<= acl_access_allowed: matched by clause #%d access granted\n",
431 Debug( LDAP_DEBUG_ACL,
432 "<= acl_access_allowed: matched by clause #%d access %s\n",
434 ACL_GRANT(b->a_access, access) ? "granted" : "denied",
437 return ACL_GRANT(b->a_access, access );
440 Debug( LDAP_DEBUG_ACL,
441 "<= acl_access_allowed: %s by default (no matching by)\n",
442 default_access >= access ? "granted" : "denied", 0, 0 );
444 return( default_access >= access );
448 * acl_check_modlist - check access control on the given entry to see if
449 * it allows the given modifications by the user associated with op.
450 * returns LDAP_SUCCESS mods allowed ok
451 * anything else mods not allowed - return is an error
452 * code indicating the problem
466 char *edn = e->e_ndn;
468 for ( ; mlist != NULL; mlist = mlist->ml_next ) {
469 regmatch_t matches[MAXREMATCHES];
471 /* the lastmod attributes are ignored by ACL checking */
472 if ( oc_check_no_usermod_attr( mlist->ml_type ) ) {
473 Debug( LDAP_DEBUG_ACL, "Operational attribute: %s access allowed\n",
474 mlist->ml_type, 0, 0 );
478 a = acl_get_applicable( be, op, e, mlist->ml_type,
479 MAXREMATCHES, matches );
481 switch ( mlist->ml_op & ~LDAP_MOD_BVALUES ) {
482 case LDAP_MOD_REPLACE:
484 if ( mlist->ml_bvalues == NULL ) {
487 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
488 if ( ! acl_access_allowed( a, mlist->ml_type, be, conn, e, mlist->ml_bvalues[i],
489 op, ACL_WRITE, edn, matches) )
491 return( LDAP_INSUFFICIENT_ACCESS );
496 case LDAP_MOD_DELETE:
497 if ( mlist->ml_bvalues == NULL ) {
498 if ( ! acl_access_allowed( a, mlist->ml_type, be, conn, e,
499 NULL, op, ACL_WRITE, edn, matches) )
501 return( LDAP_INSUFFICIENT_ACCESS );
505 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
506 if ( ! acl_access_allowed( a, mlist->ml_type, be, conn, e, mlist->ml_bvalues[i],
507 op, ACL_WRITE, edn, matches) )
509 return( LDAP_INSUFFICIENT_ACCESS );
516 return( LDAP_SUCCESS );
519 #ifdef SLAPD_ACI_ENABLED
521 aci_bvstrdup (struct berval *bv)
525 s = (char *)ch_malloc(bv->bv_len + 1);
527 memcpy(s, bv->bv_val, bv->bv_len);
534 aci_strbvcmp (char *s, struct berval *bv)
538 res = strncasecmp( s, bv->bv_val, bv->bv_len );
542 if (len > bv->bv_len)
544 if (len < bv->bv_len)
550 aci_get_part (struct berval *list, int ix, char sep, struct berval *bv)
561 while (len >= 0 && --ix >= 0) {
562 while (--len >= 0 && *p++ != sep) ;
564 while (len >= 0 && *p == ' ') {
575 while (--len >= 0 && *p != sep) {
579 while (bv->bv_len > 0 && *--p == ' ')
585 aci_list_has_right (struct berval *list, int access, int action)
590 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
593 switch (*bv.bv_val) {
598 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
599 * the right 's' to mean "set", but in the examples states
600 * that the right 's' means "search". The latter definition
612 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
613 * define any equivalent to the AUTH right, so I've just used
622 #ifdef SLAPD_ACI_DISCRETE_RIGHTS
623 if (right & access) {
631 if (ACL_GRANT(right, access))
644 aci_list_has_attr (struct berval *list, char *attr)
649 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
650 if (aci_strbvcmp(attr, &bv) == 0) {
658 aci_list_has_attr_right (struct berval *list, char *attr, int access, int action)
663 /* loop through each rights/attr pair, skip first part (action) */
665 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
666 if (aci_list_has_attr(&bv, attr) == 0)
669 if (aci_get_part(list, i, ';', &bv) < 0)
671 if (aci_list_has_right(&bv, access, action) != 0)
678 aci_list_has_permission (struct berval *list, char *attr, int access)
680 struct berval perm, actn;
681 int i, action, specific, general;
683 if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
687 /* loop through each permissions clause */
688 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
689 if (aci_get_part(&perm, 0, ';', &actn) < 0)
691 if (aci_strbvcmp( "grant", &actn ) == 0) {
693 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
699 specific = aci_list_has_attr_right(&perm, attr, access, action);
703 general = aci_list_has_attr_right(&perm, "[all]", access, action);
723 char *subjdn, *grpdn;
726 /* format of string is "group/objectClassValue/groupAttrName" */
727 if (aci_get_part(subj, 0, '/', &bv) < 0)
729 subjdn = aci_bvstrdup(&bv);
733 if (aci_get_part(subj, 1, '/', &bv) < 0)
734 grpoc = ch_strdup(grpoc);
736 grpoc = aci_bvstrdup(&bv);
738 if (aci_get_part(subj, 2, '/', &bv) < 0)
739 grpat = ch_strdup(grpat);
741 grpat = aci_bvstrdup(&bv);
743 grpdn = (char *)ch_malloc(1024);
744 if (grpoc != NULL && grpat != NULL && grpdn != NULL) {
745 string_expand(grpdn, 1024, subjdn, edn, matches);
746 if ( dn_normalize_case(grpdn) != NULL ) {
747 rc = (backend_group(be, e, grpdn, op->o_ndn, grpoc, grpat) == 0);
771 struct berval bv, perms, sdn;
775 Debug( LDAP_DEBUG_ACL,
776 "\n=> aci_access_allowed: %s access to entry \"%s\"\n",
777 access2str( access ), e->e_dn, 0 );
779 Debug( LDAP_DEBUG_ACL,
780 "\n=> aci_access_allowed: %s access to attribute \"%s\" by \"%s\"\n",
781 access2str( access ),
783 op->o_ndn ? op->o_ndn : "" );
785 /* parse an aci of the form:
786 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
788 See draft-ietf-ldapext-aci-model-0.3.txt section 9.1 for
789 a full description of the format for this attribute.
791 For now, this routine only supports scope=entry.
794 /* check that the aci has all 5 components */
795 if (aci_get_part(aci, 4, '#', NULL) < 0)
798 /* check that the scope is "entry" */
799 if (aci_get_part(aci, 1, '#', &bv) < 0
800 || aci_strbvcmp( "entry", &bv ) != 0)
805 /* get the list of permissions clauses, bail if empty */
806 if (aci_get_part(aci, 2, '#', &perms) <= 0)
809 /* check if any permissions allow desired access */
810 if (aci_list_has_permission(&perms, attr, access) == 0)
813 /* see if we have a DN match */
814 if (aci_get_part(aci, 3, '#', &bv) < 0)
817 if (aci_get_part(aci, 4, '#', &sdn) < 0)
819 if (aci_strbvcmp( "access-id", &bv ) == 0) {
820 subjdn = aci_bvstrdup(&sdn);
824 if (dn_normalize_case(subjdn) != NULL)
825 rc = (strcasecmp(op->o_ndn, subjdn) == 0);
830 if (aci_strbvcmp( "self", &bv ) == 0) {
831 return(strcasecmp(op->o_ndn, edn) == 0);
834 if (aci_strbvcmp( "group", &bv ) == 0) {
835 return(aci_group_member(&sdn, "groupOfNames", "member", be, e, op, edn, matches));
838 if (aci_strbvcmp( "role", &bv ) == 0) {
839 return(aci_group_member(&sdn, "organizationalRole", "roleOccupant", be, e, op, edn, matches));
844 #endif /* SLAPD_ACI_ENABLED */
863 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
864 /* did we previously see a $ */
869 } else if (*sp >= '0' && *sp <= '9' ) {
876 i = matches[n].rm_so;
877 l = matches[n].rm_eo;
878 for ( ; size < 512 && i < l; size++, i++ ) {
896 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
897 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
902 char *pat, /* pattern to expand and match against */
903 char *str, /* string to match against pattern */
904 char *buf, /* buffer with $N expansion variables */
905 regmatch_t *matches /* offsets in buffer for $N expansion variables */
912 if(str == NULL) str = "";
914 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
915 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
917 regerror(rc, &re, error, sizeof(error));
919 Debug( LDAP_DEBUG_TRACE,
920 "compile( \"%s\", \"%s\") failed %s\n",
925 rc = regexec(&re, str, 0, NULL, 0);
928 Debug( LDAP_DEBUG_TRACE,
929 "=> regex_matches: string: %s\n", str, 0, 0 );
930 Debug( LDAP_DEBUG_TRACE,
931 "=> regex_matches: rc: %d %s\n",
932 rc, !rc ? "matches" : "no matches", 0 );