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 #ifdef SLAPD_ACI_ENABLED
19 int aci_access_allowed (struct berval *aci, char *attr, Backend *be, Entry *e,
20 Operation *op, int access, char *edn, regmatch_t *matches);
23 static int regex_matches(char *pat, char *str, char *buf, regmatch_t *matches);
24 static void string_expand(char *newbuf, int bufsiz, char *pattern,
25 char *match, regmatch_t *matches);
29 * access_allowed - check whether op->o_ndn is allowed the requested access
30 * to entry e, attribute attr, value val. if val is null, access to
31 * the whole attribute is assumed (all values). this routine finds
32 * the applicable acl and calls acl_access_allowed() to make the
35 * returns 0 access NOT allowed
54 regmatch_t matches[MAXREMATCHES];
64 Debug( LDAP_DEBUG_ACL, "\n=> access_allowed: entry (%s) attr (%s)\n",
67 /* the lastmod attributes are ignored by ACL checking */
68 if ( oc_check_no_usermod_attr( attr ) ) {
69 Debug( LDAP_DEBUG_ACL, "Operational attribute: %s access allowed\n",
74 memset(matches, 0, sizeof(matches));
76 a = acl_get_applicable( be, op, e, attr, MAXREMATCHES, matches );
79 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
80 Debug( LDAP_DEBUG_ARGS, "=> match[%d]: %d %d ", i,
81 (int)matches[i].rm_so, (int)matches[i].rm_eo );
83 if( matches[i].rm_so <= matches[0].rm_eo ) {
84 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
85 Debug( LDAP_DEBUG_ARGS, "%c", edn[n], 0, 0 );
88 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
92 rc = acl_access_allowed( a, attr, be, conn, e, val, op, access, edn, matches );
94 Debug( LDAP_DEBUG_ACL, "\n=> access_allowed: exit (%s) attr (%s)\n",
101 * acl_get_applicable - return the acl applicable to entry e, attribute
102 * attr. the acl returned is suitable for use in subsequent calls to
103 * acl_access_allowed().
120 Debug( LDAP_DEBUG_ACL, "\n=> acl_get: entry (%s) attr (%s)\n",
123 if ( be_isroot( be, op->o_ndn ) ) {
124 Debug( LDAP_DEBUG_ACL,
125 "<= acl_get: no acl applicable to database root\n", 0, 0,
132 Debug( LDAP_DEBUG_ARGS, "=> acl_get: edn %s\n", edn, 0, 0 );
134 /* check for a backend-specific acl that matches the entry */
135 for ( i = 1, a = be->be_acl; a != NULL; a = a->acl_next, i++ ) {
136 if (a->acl_dn_pat != NULL) {
137 Debug( LDAP_DEBUG_TRACE, "=> dnpat: [%d] %s nsub: %d\n",
138 i, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub);
140 if (regexec(&a->acl_dn_re, edn, nmatch, matches, 0)) {
144 Debug( LDAP_DEBUG_TRACE, "=> acl_get:[%d] backend ACL match\n",
149 if ( a->acl_filter != NULL ) {
150 if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
155 Debug( LDAP_DEBUG_ARGS, "=> acl_get: [%d] check attr %s\n", i, attr, 0);
157 if ( attr == NULL || a->acl_attrs == NULL ||
158 charray_inlist( a->acl_attrs, attr ) )
160 Debug( LDAP_DEBUG_ACL, "<= acl_get: [%d] backend acl %s attr: %s\n",
164 matches[0].rm_so = matches[0].rm_eo = -1;
167 /* check for a global acl that matches the entry */
168 for ( i = 1, a = global_acl; a != NULL; a = a->acl_next, i++ ) {
169 if (a->acl_dn_pat != NULL) {
170 Debug( LDAP_DEBUG_TRACE, "=> dn pat: [%d] %s nsub: %d\n",
171 i, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub);
173 if (regexec(&a->acl_dn_re, edn, nmatch, matches, 0)) {
177 Debug( LDAP_DEBUG_TRACE, "=> acl_get: [%d] global ACL match\n",
182 if ( a->acl_filter != NULL ) {
183 if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
188 Debug( LDAP_DEBUG_ARGS, "=> acl_get: [%d] check attr\n", i, 0, 0);
190 if ( attr == NULL || a->acl_attrs == NULL ||
191 charray_inlist( a->acl_attrs, attr ) )
193 Debug( LDAP_DEBUG_ACL, "<= acl_get: [%d] global acl %s attr: %s\n",
198 matches[0].rm_so = matches[0].rm_eo = -1;
201 Debug( LDAP_DEBUG_ACL, "<= acl_get: no match\n", 0, 0, 0 );
206 * acl_access_allowed - check whether the given acl allows dn the
207 * requested access to entry e, attribute attr, value val. if val
208 * is null, access to the whole attribute is assumed (all values).
210 * returns 0 access NOT allowed
232 Debug( LDAP_DEBUG_ACL,
233 "\n=> acl_access_allowed: %s access to entry \"%s\"\n",
234 access2str( access ), e->e_dn, 0 );
236 Debug( LDAP_DEBUG_ACL,
237 "\n=> acl_access_allowed: %s access to value \"%s\" by \"%s\"\n",
238 access2str( access ),
239 val ? val->bv_val : "any",
240 op->o_ndn ? op->o_ndn : "" );
242 if ( be_isroot( be, op->o_ndn ) ) {
243 Debug( LDAP_DEBUG_ACL,
244 "<= acl_access_allowed: granted to database root\n",
249 default_access = be->be_dfltaccess ? be->be_dfltaccess : global_default_access;
252 Debug( LDAP_DEBUG_ACL,
253 "<= acl_access_allowed: %s by default (no matching to)\n",
254 default_access >= access ? "granted" : "denied", 0, 0 );
255 return( default_access >= access );
258 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
259 /* AND <who> clauses */
260 if ( b->a_dn_pat != NULL ) {
261 Debug( LDAP_DEBUG_TRACE, "<= check a_dn_pat: %s\n",
264 * if access applies to the entry itself, and the
265 * user is bound as somebody in the same namespace as
266 * the entry, OR the given dn matches the dn pattern
268 if ( strcasecmp( b->a_dn_pat, "anonymous" ) == 0 ) {
269 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
273 } else if ( strcasecmp( b->a_dn_pat, "self" ) == 0 ) {
274 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
278 if ( e->e_dn == NULL || strcmp( edn, op->o_ndn ) != 0 ) {
282 } else if ( strcmp( b->a_dn_pat, ".*" ) != 0 &&
283 !regex_matches( b->a_dn_pat, op->o_ndn, edn, matches ) )
289 if ( b->a_sockurl_pat != NULL ) {
290 Debug( LDAP_DEBUG_ARGS, "<= check a_sockurl_pat: %s\n",
291 b->a_sockurl_pat, 0, 0 );
293 if ( strcmp( b->a_sockurl_pat, ".*" ) != 0 &&
294 !regex_matches( b->a_sockurl_pat, conn->c_listener_url,
301 if ( b->a_domain_pat != NULL ) {
302 Debug( LDAP_DEBUG_ARGS, "<= check a_domain_pat: %s\n",
303 b->a_domain_pat, 0, 0 );
305 if ( strcmp( b->a_domain_pat, ".*" ) != 0 &&
306 !regex_matches( b->a_domain_pat, conn->c_peer_domain,
313 if ( b->a_peername_pat != NULL ) {
314 Debug( LDAP_DEBUG_ARGS, "<= check a_peername_path: %s\n",
315 b->a_peername_pat, 0, 0 );
317 if ( strcmp( b->a_peername_pat, ".*" ) != 0 &&
318 !regex_matches( b->a_peername_pat, conn->c_peer_name,
325 if ( b->a_sockname_pat != NULL ) {
326 Debug( LDAP_DEBUG_ARGS, "<= check a_sockname_path: %s\n",
327 b->a_sockname_pat, 0, 0 );
329 if ( strcmp( b->a_sockname_pat, ".*" ) != 0 &&
330 !regex_matches( b->a_sockname_pat, conn->c_sock_name,
337 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
341 Debug( LDAP_DEBUG_ARGS, "<= check a_dn_at: %s\n",
344 bv.bv_val = op->o_ndn;
345 bv.bv_len = strlen( bv.bv_val );
347 /* see if asker is listed in dnattr */
348 if ( (at = attr_find( e->e_attrs, b->a_dn_at )) != NULL &&
349 value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0 )
351 if ( ACL_IS_SELF(b->a_access) &&
352 (val == NULL || value_cmp( &bv, val, at->a_syntax, 2 )) )
357 /* asker not listed in dnattr - check for self access */
358 } else if ( ! ACL_IS_SELF(b->a_access) || val == NULL ||
359 value_cmp( &bv, val, at->a_syntax, 2 ) != 0 )
365 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
368 /* b->a_group is an unexpanded entry name, expanded it should be an
369 * entry with objectclass group* and we test to see if odn is one of
370 * the values in the attribute group
372 /* see if asker is listed in dnattr */
373 string_expand(buf, sizeof(buf), b->a_group_pat, edn, matches);
374 if ( dn_normalize_case(buf) == NULL ) {
375 /* did not expand to a valid dn */
379 if (backend_group(be, e, buf, op->o_ndn,
380 b->a_group_oc, b->a_group_at) != 0)
386 #ifdef SLAPD_ACI_ENABLED
387 if ( b->a_aci_at != NULL ) {
390 /* this case works different from the others above.
391 * since aci's themselves give permissions, we need
392 * to first check b->a_access, the ACL's access level.
395 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
399 if ( e->e_dn == NULL ) {
403 /* first check if the right being requested is
404 * higher than allowed by the ACL clause.
406 if ( ! ACL_GRANT( b->a_access, access ) ) {
410 /* get the aci attribute */
411 at = attr_find( e->e_attrs, b->a_aci_at );
416 /* the aci is an multi-valued attribute. The
417 * rights are determined by OR'ing the individual
418 * rights given by the acis.
420 for ( i = 0; at->a_vals[i] != NULL; i++ ) {
421 if ( aci_access_allowed( at->a_vals[i], attr, be, e, op, access, edn, matches ) ) {
422 Debug( LDAP_DEBUG_ACL,
423 "<= acl_access_allowed: matched by clause #%d access granted\n",
432 Debug( LDAP_DEBUG_ACL,
433 "<= acl_access_allowed: matched by clause #%d access %s\n",
435 ACL_GRANT(b->a_access, access) ? "granted" : "denied",
438 return ACL_GRANT(b->a_access, access );
441 Debug( LDAP_DEBUG_ACL,
442 "<= acl_access_allowed: %s by default (no matching by)\n",
443 default_access >= access ? "granted" : "denied", 0, 0 );
445 return( default_access >= access );
449 * acl_check_modlist - check access control on the given entry to see if
450 * it allows the given modifications by the user associated with op.
451 * returns LDAP_SUCCESS mods allowed ok
452 * anything else mods not allowed - return is an error
453 * code indicating the problem
467 char *edn = e->e_ndn;
469 for ( ; mlist != NULL; mlist = mlist->ml_next ) {
470 regmatch_t matches[MAXREMATCHES];
472 /* the lastmod attributes are ignored by ACL checking */
473 if ( oc_check_no_usermod_attr( mlist->ml_type ) ) {
474 Debug( LDAP_DEBUG_ACL, "Operational attribute: %s access allowed\n",
475 mlist->ml_type, 0, 0 );
479 a = acl_get_applicable( be, op, e, mlist->ml_type,
480 MAXREMATCHES, matches );
482 switch ( mlist->ml_op & ~LDAP_MOD_BVALUES ) {
483 case LDAP_MOD_REPLACE:
485 if ( mlist->ml_bvalues == NULL ) {
488 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
489 if ( ! acl_access_allowed( a, mlist->ml_type, be, conn, e, mlist->ml_bvalues[i],
490 op, ACL_WRITE, edn, matches) )
492 return( LDAP_INSUFFICIENT_ACCESS );
497 case LDAP_MOD_DELETE:
498 if ( mlist->ml_bvalues == NULL ) {
499 if ( ! acl_access_allowed( a, mlist->ml_type, be, conn, e,
500 NULL, op, ACL_WRITE, edn, matches) )
502 return( LDAP_INSUFFICIENT_ACCESS );
506 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
507 if ( ! acl_access_allowed( a, mlist->ml_type, be, conn, e, mlist->ml_bvalues[i],
508 op, ACL_WRITE, edn, matches) )
510 return( LDAP_INSUFFICIENT_ACCESS );
517 return( LDAP_SUCCESS );
520 #ifdef SLAPD_ACI_ENABLED
522 aci_bvstrdup (struct berval *bv)
526 s = (char *)ch_malloc(bv->bv_len + 1);
528 memcpy(s, bv->bv_val, bv->bv_len);
535 aci_strbvcmp (char *s, struct berval *bv)
539 res = strncasecmp( s, bv->bv_val, bv->bv_len );
543 if (len > bv->bv_len)
545 if (len < bv->bv_len)
551 aci_get_part (struct berval *list, int ix, char sep, struct berval *bv)
562 while (len >= 0 && --ix >= 0) {
563 while (--len >= 0 && *p++ != sep) ;
565 while (len >= 0 && *p == ' ') {
576 while (--len >= 0 && *p != sep) {
580 while (bv->bv_len > 0 && *--p == ' ')
586 aci_list_has_right (struct berval *list, int access, int action)
591 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
594 switch (*bv.bv_val) {
599 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
600 * the right 's' to mean "set", but in the examples states
601 * that the right 's' means "search". The latter definition
613 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
614 * define any equivalent to the AUTH right, so I've just used
624 #ifdef SLAPD_ACI_DISCRETE_RIGHTS
625 if (right & access) {
631 if (ACL_GRANT(right, access))
641 #ifdef SLAPD_ACI_DISCRETE_RIGHTS
649 aci_list_has_attr (struct berval *list, char *attr)
654 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
655 if (aci_strbvcmp(attr, &bv) == 0) {
663 aci_list_has_attr_right (struct berval *list, char *attr, int access, int action)
668 /* loop through each rights/attr pair, skip first part (action) */
670 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
671 if (aci_list_has_attr(&bv, attr) == 0)
674 if (aci_get_part(list, i, ';', &bv) < 0)
676 if (aci_list_has_right(&bv, access, action) != 0)
683 aci_list_has_permission (struct berval *list, char *attr, int access)
685 struct berval perm, actn;
686 int i, action, specific, general;
688 if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
692 /* loop through each permissions clause */
693 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
694 if (aci_get_part(&perm, 0, ';', &actn) < 0)
696 if (aci_strbvcmp( "grant", &actn ) == 0) {
698 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
704 specific = aci_list_has_attr_right(&perm, attr, access, action);
708 general = aci_list_has_attr_right(&perm, "[all]", access, action);
728 char *subjdn, *grpdn;
731 /* format of string is "group/objectClassValue/groupAttrName" */
732 if (aci_get_part(subj, 0, '/', &bv) < 0)
734 subjdn = aci_bvstrdup(&bv);
738 if (aci_get_part(subj, 1, '/', &bv) < 0)
739 grpoc = ch_strdup(grpoc);
741 grpoc = aci_bvstrdup(&bv);
743 if (aci_get_part(subj, 2, '/', &bv) < 0)
744 grpat = ch_strdup(grpat);
746 grpat = aci_bvstrdup(&bv);
748 grpdn = (char *)ch_malloc(1024);
749 if (grpoc != NULL && grpat != NULL && grpdn != NULL) {
750 string_expand(grpdn, 1024, subjdn, edn, matches);
751 if ( dn_normalize_case(grpdn) != NULL ) {
752 rc = (backend_group(be, e, grpdn, op->o_ndn, grpoc, grpat) == 0);
776 struct berval bv, perms, sdn;
780 Debug( LDAP_DEBUG_ACL,
781 "\n=> aci_access_allowed: %s access to entry \"%s\"\n",
782 access2str( access ), e->e_dn, 0 );
784 Debug( LDAP_DEBUG_ACL,
785 "\n=> aci_access_allowed: %s access to attribute \"%s\" by \"%s\"\n",
786 access2str( access ),
788 op->o_ndn ? op->o_ndn : "" );
790 /* parse an aci of the form:
791 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
793 See draft-ietf-ldapext-aci-model-0.3.txt section 9.1 for
794 a full description of the format for this attribute.
796 For now, this routine only supports scope=entry.
799 /* check that the aci has all 5 components */
800 if (aci_get_part(aci, 4, '#', NULL) < 0)
803 /* check that the scope is "entry" */
804 if (aci_get_part(aci, 1, '#', &bv) < 0
805 || aci_strbvcmp( "entry", &bv ) != 0)
810 /* get the list of permissions clauses, bail if empty */
811 if (aci_get_part(aci, 2, '#', &perms) <= 0)
814 /* check if any permissions allow desired access */
815 if (aci_list_has_permission(&perms, attr, access) == 0)
818 /* see if we have a DN match */
819 if (aci_get_part(aci, 3, '#', &bv) < 0)
822 if (aci_get_part(aci, 4, '#', &sdn) < 0)
824 if (aci_strbvcmp( "access-id", &bv ) == 0) {
825 subjdn = aci_bvstrdup(&sdn);
829 if (dn_normalize_case(subjdn) != NULL)
830 rc = (strcasecmp(op->o_ndn, subjdn) == 0);
835 if (aci_strbvcmp( "self", &bv ) == 0) {
836 return(strcasecmp(op->o_ndn, edn) == 0);
839 if (aci_strbvcmp( "group", &bv ) == 0) {
840 return(aci_group_member(&sdn, "groupOfNames", "member", be, e, op, edn, matches));
843 if (aci_strbvcmp( "role", &bv ) == 0) {
844 return(aci_group_member(&sdn, "organizationalRole", "roleOccupant", be, e, op, edn, matches));
849 #endif /* SLAPD_ACI_ENABLED */
868 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
869 /* did we previously see a $ */
874 } else if (*sp >= '0' && *sp <= '9' ) {
881 i = matches[n].rm_so;
882 l = matches[n].rm_eo;
883 for ( ; size < 512 && i < l; size++, i++ ) {
901 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
902 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
907 char *pat, /* pattern to expand and match against */
908 char *str, /* string to match against pattern */
909 char *buf, /* buffer with $N expansion variables */
910 regmatch_t *matches /* offsets in buffer for $N expansion variables */
917 if(str == NULL) str = "";
919 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
920 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
922 regerror(rc, &re, error, sizeof(error));
924 Debug( LDAP_DEBUG_TRACE,
925 "compile( \"%s\", \"%s\") failed %s\n",
930 rc = regexec(&re, str, 0, NULL, 0);
933 Debug( LDAP_DEBUG_TRACE,
934 "=> regex_matches: string: %s\n", str, 0, 0 );
935 Debug( LDAP_DEBUG_TRACE,
936 "=> regex_matches: rc: %d %s\n",
937 rc, !rc ? "matches" : "no matches", 0 );