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 ( strcmp( b->a_dn_pat, "anonymous" ) == 0 ) {
310 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
314 } else if ( strcmp( b->a_dn_pat, "users" ) == 0 ) {
315 if (op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
319 } else if ( strcmp( b->a_dn_pat, "self" ) == 0 ) {
320 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
324 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn ) != 0 ) {
328 } else if ( strcmp( b->a_dn_pat, "*" ) != 0 ) {
329 int ret = regex_matches( b->a_dn_pat,
330 op->o_ndn, e->e_ndn, matches );
338 if ( b->a_sockurl_pat != NULL ) {
339 Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
340 b->a_sockurl_pat, 0, 0 );
342 if ( strcmp( b->a_sockurl_pat, "*" ) != 0 &&
343 !regex_matches( b->a_sockurl_pat, conn->c_listener_url,
344 e->e_ndn, matches ) )
350 if ( b->a_domain_pat != NULL ) {
351 Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
352 b->a_domain_pat, 0, 0 );
354 if ( strcmp( b->a_domain_pat, "*" ) != 0 &&
355 !regex_matches( b->a_domain_pat, conn->c_peer_domain,
356 e->e_ndn, matches ) )
362 if ( b->a_peername_pat != NULL ) {
363 Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
364 b->a_peername_pat, 0, 0 );
366 if ( strcmp( b->a_peername_pat, "*" ) != 0 &&
367 !regex_matches( b->a_peername_pat, conn->c_peer_name,
368 e->e_ndn, matches ) )
374 if ( b->a_sockname_pat != NULL ) {
375 Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
376 b->a_sockname_pat, 0, 0 );
378 if ( strcmp( b->a_sockname_pat, "*" ) != 0 &&
379 !regex_matches( b->a_sockname_pat, conn->c_sock_name,
380 e->e_ndn, matches ) )
386 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
390 Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
393 bv.bv_val = op->o_ndn;
394 bv.bv_len = strlen( bv.bv_val );
396 /* see if asker is listed in dnattr */
397 if ( (at = attr_find( e->e_attrs, b->a_dn_at )) != NULL &&
398 value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0 )
401 (val == NULL || value_cmp( &bv, val, at->a_syntax, 2 )) )
406 /* asker not listed in dnattr - check for self access */
407 } else if ( ! b->a_dn_self || val == NULL ||
408 value_cmp( &bv, val, at->a_syntax, 2 ) != 0 )
414 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
417 /* b->a_group is an unexpanded entry name, expanded it should be an
418 * entry with objectclass group* and we test to see if odn is one of
419 * the values in the attribute group
421 /* see if asker is listed in dnattr */
422 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
423 if ( dn_normalize(buf) == NULL ) {
424 /* did not expand to a valid dn */
428 if (backend_group(be, e, buf, op->o_ndn,
429 b->a_group_oc, b->a_group_at) != 0)
435 #ifdef SLAPD_ACI_ENABLED
436 if ( b->a_aci_at != NULL ) {
439 /* this case works different from the others above.
440 * since aci's themselves give permissions, we need
441 * to first check b->a_mask, the ACL's access level.
444 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
448 if ( e->e_dn == NULL ) {
452 /* first check if the right being requested is
453 * higher than allowed by the ACL clause.
455 if ( ! ACL_GRANT( b->a_mask, access ) ) {
459 /* get the aci attribute */
460 at = attr_find( e->e_attrs, b->a_aci_at );
465 /* the aci is an multi-valued attribute. The
466 * rights are determined by OR'ing the individual
467 * rights given by the acis.
469 for ( i = 0; at->a_vals[i] != NULL; i++ ) {
470 if ( aci_access_allowed( be, op,
471 e, attr, at->a_vals[i],
474 Debug( LDAP_DEBUG_ACL,
475 "<= acl_mask: matched by clause #%d access granted\n",
481 if( ACL_IS_INVALID( modmask ) ) {
492 Debug( LDAP_DEBUG_ACL,
493 "<= acl_mask: [%d] applying %s (%s)\n",
494 i, accessmask2str( modmask, accessmaskbuf ),
495 b->a_type == ACL_CONTINUE
497 : b->a_type == ACL_BREAK
504 if( ACL_IS_ADDITIVE(modmask) ) {
506 ACL_PRIV_SET( *mask, modmask );
509 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
511 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
512 /* substract privs */
513 ACL_PRIV_CLR( *mask, modmask );
516 ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
523 Debug( LDAP_DEBUG_ACL,
524 "<= acl_mask: [%d] mask: %s\n",
525 i, accessmask2str(*mask, accessmaskbuf), 0 );
527 if( b->a_type == ACL_CONTINUE ) {
530 } else if ( b->a_type == ACL_BREAK ) {
538 Debug( LDAP_DEBUG_ACL,
539 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
540 accessmask2str(*mask, accessmaskbuf), 0, 0 );
545 * acl_check_modlist - check access control on the given entry to see if
546 * it allows the given modifications by the user associated with op.
547 * returns 1 if mods allowed ok
562 assert( be != NULL );
564 /* short circuit root database access */
565 if ( be_isroot( be, op->o_ndn ) ) {
566 Debug( LDAP_DEBUG_ACL,
567 "<= acl_access_allowed: granted to database root\n",
572 /* use backend default access if no backend acls */
573 if( be != NULL && be->be_acl == NULL ) {
574 Debug( LDAP_DEBUG_ACL,
575 "=> access_allowed: backend default %s access %s to \"%s\"\n",
576 access2str( ACL_WRITE ),
577 be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
579 return be->be_dfltaccess >= ACL_WRITE;
582 /* be is always non-NULL */
583 /* use global default access if no global acls */
584 } else if ( be == NULL && global_acl == NULL ) {
585 Debug( LDAP_DEBUG_ACL,
586 "=> access_allowed: global default %s access %s to \"%s\"\n",
587 access2str( ACL_WRITE ),
588 global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
590 return global_default_access >= ACL_WRITE;
594 for ( ; mlist != NULL; mlist = mlist->ml_next ) {
595 regmatch_t matches[MAXREMATCHES];
597 /* the lastmod attributes are ignored by ACL checking */
598 if ( oc_check_no_usermod_attr( mlist->ml_type ) ) {
599 Debug( LDAP_DEBUG_ACL, "Operational attribute: %s access allowed\n",
600 mlist->ml_type, 0, 0 );
604 switch ( mlist->ml_op & ~LDAP_MOD_BVALUES ) {
605 case LDAP_MOD_REPLACE:
607 if ( mlist->ml_bvalues == NULL ) {
610 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
611 if ( ! access_allowed( be, conn, op, e,
612 mlist->ml_type, mlist->ml_bvalues[i],
620 case LDAP_MOD_DELETE:
621 if ( mlist->ml_bvalues == NULL ) {
622 if ( ! access_allowed( be, conn, op, e,
623 mlist->ml_type, NULL,
630 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
631 if ( ! access_allowed( be, conn, op, e,
632 mlist->ml_type, mlist->ml_bvalues[i],
645 #ifdef SLAPD_ACI_ENABLED
647 aci_bvstrdup (struct berval *bv)
651 s = (char *)ch_malloc(bv->bv_len + 1);
653 memcpy(s, bv->bv_val, bv->bv_len);
660 aci_strbvcmp (char *s, struct berval *bv)
664 res = strncasecmp( s, bv->bv_val, bv->bv_len );
668 if (len > bv->bv_len)
670 if (len < bv->bv_len)
676 aci_get_part (struct berval *list, int ix, char sep, struct berval *bv)
687 while (len >= 0 && --ix >= 0) {
688 while (--len >= 0 && *p++ != sep) ;
690 while (len >= 0 && *p == ' ') {
701 while (--len >= 0 && *p != sep) {
705 while (bv->bv_len > 0 && *--p == ' ')
713 slap_access_t access,
720 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
723 switch (*bv.bv_val) {
728 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
729 * the right 's' to mean "set", but in the examples states
730 * that the right 's' means "search". The latter definition
742 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
743 * define any equivalent to the AUTH right, so I've just used
753 #ifdef SLAPD_ACI_DISCRETE_RIGHTS
754 if (right & access) {
760 if (ACL_GRANT(right, access))
770 #ifdef SLAPD_ACI_DISCRETE_RIGHTS
778 aci_list_has_attr (struct berval *list, char *attr)
783 for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
784 if (aci_strbvcmp(attr, &bv) == 0) {
792 aci_list_has_attr_right (struct berval *list, char *attr, int access, int action)
797 /* loop through each rights/attr pair, skip first part (action) */
799 for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
800 if (aci_list_has_attr(&bv, attr) == 0)
803 if (aci_get_part(list, i, ';', &bv) < 0)
805 if (aci_list_has_right(&bv, access, action) != 0)
812 aci_list_has_permission(
815 slap_access_t access)
817 struct berval perm, actn;
818 int i, action, specific, general;
820 if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
824 /* loop through each permissions clause */
825 for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
826 if (aci_get_part(&perm, 0, ';', &actn) < 0)
828 if (aci_strbvcmp( "grant", &actn ) == 0) {
830 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
836 specific = aci_list_has_attr_right(&perm, attr, access, action);
840 general = aci_list_has_attr_right(&perm, "[all]", access, action);
859 char *subjdn, *grpdn;
862 /* format of string is "group/objectClassValue/groupAttrName" */
863 if (aci_get_part(subj, 0, '/', &bv) < 0)
865 subjdn = aci_bvstrdup(&bv);
869 if (aci_get_part(subj, 1, '/', &bv) < 0)
870 grpoc = ch_strdup(grpoc);
872 grpoc = aci_bvstrdup(&bv);
874 if (aci_get_part(subj, 2, '/', &bv) < 0)
875 grpat = ch_strdup(grpat);
877 grpat = aci_bvstrdup(&bv);
879 grpdn = (char *)ch_malloc(1024);
880 if (grpoc != NULL && grpat != NULL && grpdn != NULL) {
881 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
882 if ( dn_normalize(grpdn) != NULL ) {
883 rc = (backend_group(be, e, grpdn, op->o_ndn, grpoc, grpat) == 0);
902 slap_access_t access,
906 struct berval bv, perms, sdn;
910 Debug( LDAP_DEBUG_ACL,
911 "=> aci_access_allowed: %s access to entry \"%s\"\n",
912 access2str( access ), e->e_dn, 0 );
914 Debug( LDAP_DEBUG_ACL,
915 "=> aci_access_allowed: %s access to attribute \"%s\" by \"%s\"\n",
916 access2str( access ),
918 op->o_ndn ? op->o_ndn : "" );
920 /* parse an aci of the form:
921 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
923 See draft-ietf-ldapext-aci-model-0.3.txt section 9.1 for
924 a full description of the format for this attribute.
926 For now, this routine only supports scope=entry.
929 /* check that the aci has all 5 components */
930 if (aci_get_part(aci, 4, '#', NULL) < 0)
933 /* check that the scope is "entry" */
934 if (aci_get_part(aci, 1, '#', &bv) < 0
935 || aci_strbvcmp( "entry", &bv ) != 0)
940 /* get the list of permissions clauses, bail if empty */
941 if (aci_get_part(aci, 2, '#', &perms) <= 0)
944 /* check if any permissions allow desired access */
945 if (aci_list_has_permission(&perms, attr, access) == 0)
948 /* see if we have a DN match */
949 if (aci_get_part(aci, 3, '#', &bv) < 0)
952 if (aci_get_part(aci, 4, '#', &sdn) < 0)
954 if (aci_strbvcmp( "access-id", &bv ) == 0) {
955 subjdn = aci_bvstrdup(&sdn);
959 if ( dn_normalize(subjdn) != NULL )
960 rc = (strcasecmp(op->o_ndn, subjdn) == 0);
965 if (aci_strbvcmp( "self", &bv ) == 0) {
966 return(strcasecmp(op->o_ndn, e->e_ndn) == 0);
969 if (aci_strbvcmp( "group", &bv ) == 0) {
970 return(aci_group_member(&sdn, "groupOfNames", "member", be, e, op, matches));
973 if (aci_strbvcmp( "role", &bv ) == 0) {
974 return(aci_group_member(&sdn, "organizationalRole", "roleOccupant", be, e, op, matches));
979 #endif /* SLAPD_ACI_ENABLED */
996 bufsiz--; /* leave space for lone $ */
999 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
1000 /* did we previously see a $ */
1005 } else if (*sp >= '0' && *sp <= '9' ) {
1012 i = matches[n].rm_so;
1013 l = matches[n].rm_eo;
1014 for ( ; size < 512 && i < l; size++, i++ ) {
1032 /* must have ended with a single $ */
1039 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
1040 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1045 char *pat, /* pattern to expand and match against */
1046 char *str, /* string to match against pattern */
1047 char *buf, /* buffer with $N expansion variables */
1048 regmatch_t *matches /* offsets in buffer for $N expansion variables */
1055 if(str == NULL) str = "";
1057 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1058 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1060 regerror(rc, &re, error, sizeof(error));
1062 Debug( LDAP_DEBUG_TRACE,
1063 "compile( \"%s\", \"%s\") failed %s\n",
1068 rc = regexec(&re, str, 0, NULL, 0);
1071 Debug( LDAP_DEBUG_TRACE,
1072 "=> regex_matches: string: %s\n", str, 0, 0 );
1073 Debug( LDAP_DEBUG_TRACE,
1074 "=> regex_matches: rc: %d %s\n",
1075 rc, !rc ? "matches" : "no matches", 0 );