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 slap_access_mask_t mask;
72 slap_control_t control;
74 regmatch_t matches[MAXREMATCHES];
76 Debug( LDAP_DEBUG_ACL,
77 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
83 assert( attr != NULL );
84 assert( access > ACL_NONE );
86 /* grant database root access */
87 if ( be != NULL && be_isroot( be, op->o_ndn ) ) {
88 Debug( LDAP_DEBUG_ACL,
89 "<= root access granted\n",
94 /* no user modify operational attributes are ignored by ACL checking */
95 if ( oc_check_no_usermod_attr( attr ) ) {
96 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
97 " %s access granted\n",
102 /* use backend default access if no backend acls */
103 if( be != NULL && be->be_acl == NULL ) {
104 Debug( LDAP_DEBUG_ACL,
105 "=> access_allowed: backend default %s access %s to \"%s\"\n",
106 access2str( access ),
107 be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn );
109 return be->be_dfltaccess >= access;
112 /* be is always non-NULL */
113 /* use global default access if no global acls */
114 } else if ( be == NULL && global_acl == NULL ) {
115 Debug( LDAP_DEBUG_ACL,
116 "=> access_allowed: global default %s access %s to \"%s\"\n",
117 access2str( access ),
118 global_default_access >= access ? "granted" : "denied", op->o_dn );
120 return global_default_access >= access;
125 memset(matches, 0, sizeof(matches));
131 while( a = acl_get( a, &count, be, op, e, attr, MAXREMATCHES, matches ) )
135 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
136 Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
137 (int)matches[i].rm_so, (int)matches[i].rm_eo );
139 if( matches[i].rm_so <= matches[0].rm_eo ) {
141 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
142 Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
145 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
148 control = acl_mask( a, &mask, be, conn, op,
149 e, attr, val, matches );
151 if ( control != ACL_BREAK ) {
155 memset(matches, 0, sizeof(matches));
158 if ( ACL_IS_INVALID( mask ) ) {
159 Debug( LDAP_DEBUG_ACL,
160 "=> access_allowed: \"%s\" (%s) invalid!\n",
164 } else if ( control == ACL_BREAK ) {
165 Debug( LDAP_DEBUG_ACL,
166 "=> access_allowed: no more rules\n", 0, 0, 0);
170 Debug( LDAP_DEBUG_ACL,
171 "=> access_allowed: %s access %s by %s\n",
172 access2str( access ),
173 ACL_GRANT(mask, access) ? "granted" : "denied",
174 accessmask2str( mask ) );
176 return ACL_GRANT(mask, access);
180 * acl_get - return the acl applicable to entry e, attribute
181 * attr. the acl returned is suitable for use in subsequent calls to
182 * acl_access_allowed().
185 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,
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 ) );
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 ( strcasecmp( b->a_dn_pat, "anonymous" ) == 0 ) {
309 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
313 } else if ( strcasecmp( b->a_dn_pat, "self" ) == 0 ) {
314 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
318 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn ) != 0 ) {
322 } else if ( strcmp( b->a_dn_pat, ".*" ) != 0 &&
323 !regex_matches( b->a_dn_pat, op->o_ndn, e->e_ndn, matches ) )
329 if ( b->a_sockurl_pat != NULL ) {
330 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
331 b->a_sockurl_pat, 0, 0 );
333 if ( strcmp( b->a_sockurl_pat, ".*" ) != 0 &&
334 !regex_matches( b->a_sockurl_pat, conn->c_listener_url,
335 e->e_ndn, matches ) )
341 if ( b->a_domain_pat != NULL ) {
342 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
343 b->a_domain_pat, 0, 0 );
345 if ( strcmp( b->a_domain_pat, ".*" ) != 0 &&
346 !regex_matches( b->a_domain_pat, conn->c_peer_domain,
347 e->e_ndn, matches ) )
353 if ( b->a_peername_pat != NULL ) {
354 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
355 b->a_peername_pat, 0, 0 );
357 if ( strcmp( b->a_peername_pat, ".*" ) != 0 &&
358 !regex_matches( b->a_peername_pat, conn->c_peer_name,
359 e->e_ndn, matches ) )
365 if ( b->a_sockname_pat != NULL ) {
366 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
367 b->a_sockname_pat, 0, 0 );
369 if ( strcmp( b->a_sockname_pat, ".*" ) != 0 &&
370 !regex_matches( b->a_sockname_pat, conn->c_sock_name,
371 e->e_ndn, matches ) )
377 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
381 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
384 bv.bv_val = op->o_ndn;
385 bv.bv_len = strlen( bv.bv_val );
387 /* see if asker is listed in dnattr */
388 if ( (at = attr_find( e->e_attrs, b->a_dn_at )) != NULL &&
389 value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0 )
392 (val == NULL || value_cmp( &bv, val, at->a_syntax, 2 )) )
397 /* asker not listed in dnattr - check for self access */
398 } else if ( ! b->a_dn_self || val == NULL ||
399 value_cmp( &bv, val, at->a_syntax, 2 ) != 0 )
405 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
408 /* b->a_group is an unexpanded entry name, expanded it should be an
409 * entry with objectclass group* and we test to see if odn is one of
410 * the values in the attribute group
412 /* see if asker is listed in dnattr */
413 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
414 if ( dn_normalize(buf) == NULL ) {
415 /* did not expand to a valid dn */
419 if (backend_group(be, e, buf, op->o_ndn,
420 b->a_group_oc, b->a_group_at) != 0)
426 #ifdef SLAPD_ACI_ENABLED
427 if ( b->a_aci_at != NULL ) {
430 /* this case works different from the others above.
431 * since aci's themselves give permissions, we need
432 * to first check b->a_mask, the ACL's access level.
435 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
439 if ( e->e_dn == NULL ) {
443 /* first check if the right being requested is
444 * higher than allowed by the ACL clause.
446 if ( ! ACL_GRANT( b->a_mask, access ) ) {
450 /* get the aci attribute */
451 at = attr_find( e->e_attrs, b->a_aci_at );
456 /* the aci is an multi-valued attribute. The
457 * rights are determined by OR'ing the individual
458 * rights given by the acis.
460 for ( i = 0; at->a_vals[i] != NULL; i++ ) {
461 if ( aci_access_allowed( be, op,
462 e, attr, at->a_vals[i],
465 Debug( LDAP_DEBUG_ACL,
466 "<= acl_mask: matched by clause #%d access granted\n",
472 if( ACL_IS_INVALID( modmask ) ) {
483 Debug( LDAP_DEBUG_ACL,
484 "<= acl_mask: [%d] applying %s (%s)\n",
485 i, accessmask2str( modmask ),
486 b->a_type == ACL_CONTINUE
488 : b->a_type == ACL_BREAK
495 if( ACL_IS_ADDITIVE(modmask) ) {
497 ACL_PRIV_SET( *mask, modmask );
500 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
502 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
503 /* substract privs */
504 ACL_PRIV_CLR( *mask, modmask );
507 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
514 Debug( LDAP_DEBUG_ACL,
515 "<= acl_mask: [%d] old: %s new: %s\n",
516 i, accessmask2str(oldmask), accessmask2str(*mask));
518 if( b->a_type == ACL_CONTINUE ) {
521 } else if ( b->a_type == ACL_BREAK ) {
529 Debug( LDAP_DEBUG_ACL,
530 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
531 accessmask2str(*mask), 0, 0 );
536 * acl_check_modlist - check access control on the given entry to see if
537 * it allows the given modifications by the user associated with op.
538 * returns 1 if mods allowed ok
553 assert( be != NULL );
555 /* short circuit root database access */
556 if ( be_isroot( be, op->o_ndn ) ) {
557 Debug( LDAP_DEBUG_ACL,
558 "<= acl_access_allowed: granted to database root\n",
563 /* use backend default access if no backend acls */
564 if( be != NULL && be->be_acl == NULL ) {
565 Debug( LDAP_DEBUG_ACL,
566 "=> access_allowed: backend default %s access %s to \"%s\"\n",
567 access2str( ACL_WRITE ),
568 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
570 return be->be_dfltaccess >= ACL_WRITE;
573 /* be is always non-NULL */
574 /* use global default access if no global acls */
575 } else if ( be == NULL && global_acl == NULL ) {
576 Debug( LDAP_DEBUG_ACL,
577 "=> access_allowed: global default %s access %s to \"%s\"\n",
578 access2str( ACL_WRITE ),
579 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
581 return global_default_access >= ACL_WRITE;
585 for ( ; mlist != NULL; mlist = mlist->ml_next ) {
586 regmatch_t matches[MAXREMATCHES];
588 /* the lastmod attributes are ignored by ACL checking */
589 if ( oc_check_no_usermod_attr( mlist->ml_type ) ) {
590 Debug( LDAP_DEBUG_ACL, "Operational attribute: %s access allowed\n",
591 mlist->ml_type, 0, 0 );
595 switch ( mlist->ml_op & ~LDAP_MOD_BVALUES ) {
596 case LDAP_MOD_REPLACE:
598 if ( mlist->ml_bvalues == NULL ) {
601 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
602 if ( ! access_allowed( be, conn, op, e,
603 mlist->ml_type, mlist->ml_bvalues[i],
611 case LDAP_MOD_DELETE:
612 if ( mlist->ml_bvalues == NULL ) {
613 if ( ! access_allowed( be, conn, op, e,
614 mlist->ml_type, NULL,
621 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
622 if ( ! access_allowed( be, conn, op, e,
623 mlist->ml_type, mlist->ml_bvalues[i],
636 #ifdef SLAPD_ACI_ENABLED
638 aci_bvstrdup (struct berval *bv)
642 s = (char *)ch_malloc(bv->bv_len + 1);
644 memcpy(s, bv->bv_val, bv->bv_len);
651 aci_strbvcmp (char *s, struct berval *bv)
655 res = strncasecmp( s, bv->bv_val, bv->bv_len );
659 if (len > bv->bv_len)
661 if (len < bv->bv_len)
667 aci_get_part (struct berval *list, int ix, char sep, struct berval *bv)
678 while (len >= 0 && --ix >= 0) {
679 while (--len >= 0 && *p++ != sep) ;
681 while (len >= 0 && *p == ' ') {
692 while (--len >= 0 && *p != sep) {
696 while (bv->bv_len > 0 && *--p == ' ')
704 slap_access_t access,
711 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
714 switch (*bv.bv_val) {
719 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
720 * the right 's' to mean "set", but in the examples states
721 * that the right 's' means "search". The latter definition
733 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
734 * define any equivalent to the AUTH right, so I've just used
744 #ifdef SLAPD_ACI_DISCRETE_RIGHTS
745 if (right & access) {
751 if (ACL_GRANT(right, access))
761 #ifdef SLAPD_ACI_DISCRETE_RIGHTS
769 aci_list_has_attr (struct berval *list, char *attr)
774 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
775 if (aci_strbvcmp(attr, &bv) == 0) {
783 aci_list_has_attr_right (struct berval *list, char *attr, int access, int action)
788 /* loop through each rights/attr pair, skip first part (action) */
790 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
791 if (aci_list_has_attr(&bv, attr) == 0)
794 if (aci_get_part(list, i, ';', &bv) < 0)
796 if (aci_list_has_right(&bv, access, action) != 0)
803 aci_list_has_permission(
806 slap_access_t access)
808 struct berval perm, actn;
809 int i, action, specific, general;
811 if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
815 /* loop through each permissions clause */
816 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
817 if (aci_get_part(&perm, 0, ';', &actn) < 0)
819 if (aci_strbvcmp( "grant", &actn ) == 0) {
821 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
827 specific = aci_list_has_attr_right(&perm, attr, access, action);
831 general = aci_list_has_attr_right(&perm, "[all]", access, action);
850 char *subjdn, *grpdn;
853 /* format of string is "group/objectClassValue/groupAttrName" */
854 if (aci_get_part(subj, 0, '/', &bv) < 0)
856 subjdn = aci_bvstrdup(&bv);
860 if (aci_get_part(subj, 1, '/', &bv) < 0)
861 grpoc = ch_strdup(grpoc);
863 grpoc = aci_bvstrdup(&bv);
865 if (aci_get_part(subj, 2, '/', &bv) < 0)
866 grpat = ch_strdup(grpat);
868 grpat = aci_bvstrdup(&bv);
870 grpdn = (char *)ch_malloc(1024);
871 if (grpoc != NULL && grpat != NULL && grpdn != NULL) {
872 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
873 if ( dn_normalize(grpdn) != NULL ) {
874 rc = (backend_group(be, e, grpdn, op->o_ndn, grpoc, grpat) == 0);
893 slap_access_t access,
897 struct berval bv, perms, sdn;
901 Debug( LDAP_DEBUG_ACL,
902 "=> aci_access_allowed: %s access to entry \"%s\"\n",
903 access2str( access ), e->e_dn, 0 );
905 Debug( LDAP_DEBUG_ACL,
906 "=> aci_access_allowed: %s access to attribute \"%s\" by \"%s\"\n",
907 access2str( access ),
909 op->o_ndn ? op->o_ndn : "" );
911 /* parse an aci of the form:
912 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
914 See draft-ietf-ldapext-aci-model-0.3.txt section 9.1 for
915 a full description of the format for this attribute.
917 For now, this routine only supports scope=entry.
920 /* check that the aci has all 5 components */
921 if (aci_get_part(aci, 4, '#', NULL) < 0)
924 /* check that the scope is "entry" */
925 if (aci_get_part(aci, 1, '#', &bv) < 0
926 || aci_strbvcmp( "entry", &bv ) != 0)
931 /* get the list of permissions clauses, bail if empty */
932 if (aci_get_part(aci, 2, '#', &perms) <= 0)
935 /* check if any permissions allow desired access */
936 if (aci_list_has_permission(&perms, attr, access) == 0)
939 /* see if we have a DN match */
940 if (aci_get_part(aci, 3, '#', &bv) < 0)
943 if (aci_get_part(aci, 4, '#', &sdn) < 0)
945 if (aci_strbvcmp( "access-id", &bv ) == 0) {
946 subjdn = aci_bvstrdup(&sdn);
950 if ( dn_normalize(subjdn) != NULL )
951 rc = (strcasecmp(op->o_ndn, subjdn) == 0);
956 if (aci_strbvcmp( "self", &bv ) == 0) {
957 return(strcasecmp(op->o_ndn, e->e_ndn) == 0);
960 if (aci_strbvcmp( "group", &bv ) == 0) {
961 return(aci_group_member(&sdn, "groupOfNames", "member", be, e, op, matches));
964 if (aci_strbvcmp( "role", &bv ) == 0) {
965 return(aci_group_member(&sdn, "organizationalRole", "roleOccupant", be, e, op, matches));
970 #endif /* SLAPD_ACI_ENABLED */
989 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
990 /* did we previously see a $ */
995 } else if (*sp >= '0' && *sp <= '9' ) {
1002 i = matches[n].rm_so;
1003 l = matches[n].rm_eo;
1004 for ( ; size < 512 && i < l; size++, i++ ) {
1022 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
1023 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1028 char *pat, /* pattern to expand and match against */
1029 char *str, /* string to match against pattern */
1030 char *buf, /* buffer with $N expansion variables */
1031 regmatch_t *matches /* offsets in buffer for $N expansion variables */
1038 if(str == NULL) str = "";
1040 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1041 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1043 regerror(rc, &re, error, sizeof(error));
1045 Debug( LDAP_DEBUG_TRACE,
1046 "compile( \"%s\", \"%s\") failed %s\n",
1051 rc = regexec(&re, str, 0, NULL, 0);
1054 Debug( LDAP_DEBUG_TRACE,
1055 "=> regex_matches: string: %s\n", str, 0, 0 );
1056 Debug( LDAP_DEBUG_TRACE,
1057 "=> regex_matches: rc: %d %s\n",
1058 rc, !rc ? "matches" : "no matches", 0 );