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 *
200 assert( count != NULL );
215 for ( ; a != NULL; a = a->acl_next ) {
218 if (a->acl_dn_pat != NULL) {
219 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n",
220 *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub );
222 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0)) {
226 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
231 if ( a->acl_filter != NULL ) {
232 if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
237 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
240 if ( attr == NULL || a->acl_attrs == NULL ||
241 charray_inlist( a->acl_attrs, attr ) )
243 Debug( LDAP_DEBUG_ACL,
244 "<= acl_get: [%d] acl %s attr: %s\n",
245 *count, e->e_dn, attr );
248 matches[0].rm_so = matches[0].rm_eo = -1;
251 Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
257 * acl_mask - modifies mask based upon the given acl and the
258 * requested access to entry e, attribute attr, value val. if val
259 * is null, access to the whole attribute is assumed (all values).
261 * returns 0 access NOT allowed
265 static slap_control_t
268 slap_access_mask_t *mask,
280 char accessmaskbuf[ACCESSMASK_MAXLEN];
283 assert( mask != NULL );
285 Debug( LDAP_DEBUG_ACL,
286 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
289 Debug( LDAP_DEBUG_ACL,
290 "=> acl_mask: to value \"%s\" by \"%s\", (%s) \n",
291 val ? val->bv_val : "*",
292 op->o_ndn ? op->o_ndn : "",
293 accessmask2str( *mask, accessmaskbuf ) );
295 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
296 slap_access_mask_t oldmask, modmask;
298 ACL_INVALIDATE( modmask );
300 /* AND <who> clauses */
301 if ( b->a_dn_pat != NULL ) {
302 Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
305 * if access applies to the entry itself, and the
306 * user is bound as somebody in the same namespace as
307 * the entry, OR the given dn matches the dn pattern
309 if ( strcasecmp( b->a_dn_pat, "anonymous" ) == 0 ) {
310 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
314 } else if ( strcasecmp( b->a_dn_pat, "self" ) == 0 ) {
315 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
319 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn ) != 0 ) {
323 } else if ( strcmp( b->a_dn_pat, ".*" ) != 0 &&
324 !regex_matches( b->a_dn_pat, op->o_ndn, e->e_ndn, matches ) )
330 if ( b->a_sockurl_pat != NULL ) {
331 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
332 b->a_sockurl_pat, 0, 0 );
334 if ( strcmp( b->a_sockurl_pat, ".*" ) != 0 &&
335 !regex_matches( b->a_sockurl_pat, conn->c_listener_url,
336 e->e_ndn, matches ) )
342 if ( b->a_domain_pat != NULL ) {
343 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
344 b->a_domain_pat, 0, 0 );
346 if ( strcmp( b->a_domain_pat, ".*" ) != 0 &&
347 !regex_matches( b->a_domain_pat, conn->c_peer_domain,
348 e->e_ndn, matches ) )
354 if ( b->a_peername_pat != NULL ) {
355 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
356 b->a_peername_pat, 0, 0 );
358 if ( strcmp( b->a_peername_pat, ".*" ) != 0 &&
359 !regex_matches( b->a_peername_pat, conn->c_peer_name,
360 e->e_ndn, matches ) )
366 if ( b->a_sockname_pat != NULL ) {
367 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
368 b->a_sockname_pat, 0, 0 );
370 if ( strcmp( b->a_sockname_pat, ".*" ) != 0 &&
371 !regex_matches( b->a_sockname_pat, conn->c_sock_name,
372 e->e_ndn, matches ) )
378 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
382 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
385 bv.bv_val = op->o_ndn;
386 bv.bv_len = strlen( bv.bv_val );
388 /* see if asker is listed in dnattr */
389 if ( (at = attr_find( e->e_attrs, b->a_dn_at )) != NULL &&
390 value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0 )
393 (val == NULL || value_cmp( &bv, val, at->a_syntax, 2 )) )
398 /* asker not listed in dnattr - check for self access */
399 } else if ( ! b->a_dn_self || val == NULL ||
400 value_cmp( &bv, val, at->a_syntax, 2 ) != 0 )
406 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
409 /* b->a_group is an unexpanded entry name, expanded it should be an
410 * entry with objectclass group* and we test to see if odn is one of
411 * the values in the attribute group
413 /* see if asker is listed in dnattr */
414 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
415 if ( dn_normalize(buf) == NULL ) {
416 /* did not expand to a valid dn */
420 if (backend_group(be, e, buf, op->o_ndn,
421 b->a_group_oc, b->a_group_at) != 0)
427 #ifdef SLAPD_ACI_ENABLED
428 if ( b->a_aci_at != NULL ) {
431 /* this case works different from the others above.
432 * since aci's themselves give permissions, we need
433 * to first check b->a_mask, the ACL's access level.
436 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
440 if ( e->e_dn == NULL ) {
444 /* first check if the right being requested is
445 * higher than allowed by the ACL clause.
447 if ( ! ACL_GRANT( b->a_mask, access ) ) {
451 /* get the aci attribute */
452 at = attr_find( e->e_attrs, b->a_aci_at );
457 /* the aci is an multi-valued attribute. The
458 * rights are determined by OR'ing the individual
459 * rights given by the acis.
461 for ( i = 0; at->a_vals[i] != NULL; i++ ) {
462 if ( aci_access_allowed( be, op,
463 e, attr, at->a_vals[i],
466 Debug( LDAP_DEBUG_ACL,
467 "<= acl_mask: matched by clause #%d access granted\n",
473 if( ACL_IS_INVALID( modmask ) ) {
484 Debug( LDAP_DEBUG_ACL,
485 "<= acl_mask: [%d] applying %s (%s)\n",
486 i, accessmask2str( modmask, accessmaskbuf ),
487 b->a_type == ACL_CONTINUE
489 : b->a_type == ACL_BREAK
496 if( ACL_IS_ADDITIVE(modmask) ) {
498 ACL_PRIV_SET( *mask, modmask );
501 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
503 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
504 /* substract privs */
505 ACL_PRIV_CLR( *mask, modmask );
508 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
515 Debug( LDAP_DEBUG_ACL,
516 "<= acl_mask: [%d] mask: %s\n",
517 i, accessmask2str(*mask, accessmaskbuf), 0 );
519 if( b->a_type == ACL_CONTINUE ) {
522 } else if ( b->a_type == ACL_BREAK ) {
530 Debug( LDAP_DEBUG_ACL,
531 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
532 accessmask2str(*mask, accessmaskbuf), 0, 0 );
537 * acl_check_modlist - check access control on the given entry to see if
538 * it allows the given modifications by the user associated with op.
539 * returns 1 if mods allowed ok
554 assert( be != NULL );
556 /* short circuit root database access */
557 if ( be_isroot( be, op->o_ndn ) ) {
558 Debug( LDAP_DEBUG_ACL,
559 "<= acl_access_allowed: granted to database root\n",
564 /* use backend default access if no backend acls */
565 if( be != NULL && be->be_acl == NULL ) {
566 Debug( LDAP_DEBUG_ACL,
567 "=> access_allowed: backend default %s access %s to \"%s\"\n",
568 access2str( ACL_WRITE ),
569 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
571 return be->be_dfltaccess >= ACL_WRITE;
574 /* be is always non-NULL */
575 /* use global default access if no global acls */
576 } else if ( be == NULL && global_acl == NULL ) {
577 Debug( LDAP_DEBUG_ACL,
578 "=> access_allowed: global default %s access %s to \"%s\"\n",
579 access2str( ACL_WRITE ),
580 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
582 return global_default_access >= ACL_WRITE;
586 for ( ; mlist != NULL; mlist = mlist->ml_next ) {
587 regmatch_t matches[MAXREMATCHES];
589 /* the lastmod attributes are ignored by ACL checking */
590 if ( oc_check_no_usermod_attr( mlist->ml_type ) ) {
591 Debug( LDAP_DEBUG_ACL, "Operational attribute: %s access allowed\n",
592 mlist->ml_type, 0, 0 );
596 switch ( mlist->ml_op & ~LDAP_MOD_BVALUES ) {
597 case LDAP_MOD_REPLACE:
599 if ( mlist->ml_bvalues == NULL ) {
602 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
603 if ( ! access_allowed( be, conn, op, e,
604 mlist->ml_type, mlist->ml_bvalues[i],
612 case LDAP_MOD_DELETE:
613 if ( mlist->ml_bvalues == NULL ) {
614 if ( ! access_allowed( be, conn, op, e,
615 mlist->ml_type, NULL,
622 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
623 if ( ! access_allowed( be, conn, op, e,
624 mlist->ml_type, mlist->ml_bvalues[i],
637 #ifdef SLAPD_ACI_ENABLED
639 aci_bvstrdup (struct berval *bv)
643 s = (char *)ch_malloc(bv->bv_len + 1);
645 memcpy(s, bv->bv_val, bv->bv_len);
652 aci_strbvcmp (char *s, struct berval *bv)
656 res = strncasecmp( s, bv->bv_val, bv->bv_len );
660 if (len > bv->bv_len)
662 if (len < bv->bv_len)
668 aci_get_part (struct berval *list, int ix, char sep, struct berval *bv)
679 while (len >= 0 && --ix >= 0) {
680 while (--len >= 0 && *p++ != sep) ;
682 while (len >= 0 && *p == ' ') {
693 while (--len >= 0 && *p != sep) {
697 while (bv->bv_len > 0 && *--p == ' ')
705 slap_access_t access,
712 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
715 switch (*bv.bv_val) {
720 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
721 * the right 's' to mean "set", but in the examples states
722 * that the right 's' means "search". The latter definition
734 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
735 * define any equivalent to the AUTH right, so I've just used
745 #ifdef SLAPD_ACI_DISCRETE_RIGHTS
746 if (right & access) {
752 if (ACL_GRANT(right, access))
762 #ifdef SLAPD_ACI_DISCRETE_RIGHTS
770 aci_list_has_attr (struct berval *list, char *attr)
775 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
776 if (aci_strbvcmp(attr, &bv) == 0) {
784 aci_list_has_attr_right (struct berval *list, char *attr, int access, int action)
789 /* loop through each rights/attr pair, skip first part (action) */
791 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
792 if (aci_list_has_attr(&bv, attr) == 0)
795 if (aci_get_part(list, i, ';', &bv) < 0)
797 if (aci_list_has_right(&bv, access, action) != 0)
804 aci_list_has_permission(
807 slap_access_t access)
809 struct berval perm, actn;
810 int i, action, specific, general;
812 if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
816 /* loop through each permissions clause */
817 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
818 if (aci_get_part(&perm, 0, ';', &actn) < 0)
820 if (aci_strbvcmp( "grant", &actn ) == 0) {
822 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
828 specific = aci_list_has_attr_right(&perm, attr, access, action);
832 general = aci_list_has_attr_right(&perm, "[all]", access, action);
851 char *subjdn, *grpdn;
854 /* format of string is "group/objectClassValue/groupAttrName" */
855 if (aci_get_part(subj, 0, '/', &bv) < 0)
857 subjdn = aci_bvstrdup(&bv);
861 if (aci_get_part(subj, 1, '/', &bv) < 0)
862 grpoc = ch_strdup(grpoc);
864 grpoc = aci_bvstrdup(&bv);
866 if (aci_get_part(subj, 2, '/', &bv) < 0)
867 grpat = ch_strdup(grpat);
869 grpat = aci_bvstrdup(&bv);
871 grpdn = (char *)ch_malloc(1024);
872 if (grpoc != NULL && grpat != NULL && grpdn != NULL) {
873 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
874 if ( dn_normalize(grpdn) != NULL ) {
875 rc = (backend_group(be, e, grpdn, op->o_ndn, grpoc, grpat) == 0);
894 slap_access_t access,
898 struct berval bv, perms, sdn;
902 Debug( LDAP_DEBUG_ACL,
903 "=> aci_access_allowed: %s access to entry \"%s\"\n",
904 access2str( access ), e->e_dn, 0 );
906 Debug( LDAP_DEBUG_ACL,
907 "=> aci_access_allowed: %s access to attribute \"%s\" by \"%s\"\n",
908 access2str( access ),
910 op->o_ndn ? op->o_ndn : "" );
912 /* parse an aci of the form:
913 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
915 See draft-ietf-ldapext-aci-model-0.3.txt section 9.1 for
916 a full description of the format for this attribute.
918 For now, this routine only supports scope=entry.
921 /* check that the aci has all 5 components */
922 if (aci_get_part(aci, 4, '#', NULL) < 0)
925 /* check that the scope is "entry" */
926 if (aci_get_part(aci, 1, '#', &bv) < 0
927 || aci_strbvcmp( "entry", &bv ) != 0)
932 /* get the list of permissions clauses, bail if empty */
933 if (aci_get_part(aci, 2, '#', &perms) <= 0)
936 /* check if any permissions allow desired access */
937 if (aci_list_has_permission(&perms, attr, access) == 0)
940 /* see if we have a DN match */
941 if (aci_get_part(aci, 3, '#', &bv) < 0)
944 if (aci_get_part(aci, 4, '#', &sdn) < 0)
946 if (aci_strbvcmp( "access-id", &bv ) == 0) {
947 subjdn = aci_bvstrdup(&sdn);
951 if ( dn_normalize(subjdn) != NULL )
952 rc = (strcasecmp(op->o_ndn, subjdn) == 0);
957 if (aci_strbvcmp( "self", &bv ) == 0) {
958 return(strcasecmp(op->o_ndn, e->e_ndn) == 0);
961 if (aci_strbvcmp( "group", &bv ) == 0) {
962 return(aci_group_member(&sdn, "groupOfNames", "member", be, e, op, matches));
965 if (aci_strbvcmp( "role", &bv ) == 0) {
966 return(aci_group_member(&sdn, "organizationalRole", "roleOccupant", be, e, op, matches));
971 #endif /* SLAPD_ACI_ENABLED */
988 bufsiz--; /* leave space for lone $ */
991 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
992 /* did we previously see a $ */
997 } else if (*sp >= '0' && *sp <= '9' ) {
1004 i = matches[n].rm_so;
1005 l = matches[n].rm_eo;
1006 for ( ; size < 512 && i < l; size++, i++ ) {
1024 /* must have ended with a single $ */
1031 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
1032 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1037 char *pat, /* pattern to expand and match against */
1038 char *str, /* string to match against pattern */
1039 char *buf, /* buffer with $N expansion variables */
1040 regmatch_t *matches /* offsets in buffer for $N expansion variables */
1047 if(str == NULL) str = "";
1049 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1050 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1052 regerror(rc, &re, error, sizeof(error));
1054 Debug( LDAP_DEBUG_TRACE,
1055 "compile( \"%s\", \"%s\") failed %s\n",
1060 rc = regexec(&re, str, 0, NULL, 0);
1063 Debug( LDAP_DEBUG_TRACE,
1064 "=> regex_matches: string: %s\n", str, 0, 0 );
1065 Debug( LDAP_DEBUG_TRACE,
1066 "=> regex_matches: rc: %d %s\n",
1067 rc, !rc ? "matches" : "no matches", 0 );