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 static int regex_matches(char *pat, char *str, char *buf, regmatch_t *matches);
18 static void string_expand(char *newbuf, int bufsiz, char *pattern,
19 char *match, regmatch_t *matches);
23 * access_allowed - check whether op->o_ndn is allowed the requested access
24 * to entry e, attribute attr, value val. if val is null, access to
25 * the whole attribute is assumed (all values). this routine finds
26 * the applicable acl and calls acl_access_allowed() to make the
29 * returns 0 access NOT allowed
48 regmatch_t matches[MAXREMATCHES];
58 Debug( LDAP_DEBUG_ACL, "\n=> access_allowed: entry (%s) attr (%s)\n",
61 /* the lastmod attributes are ignored by ACL checking */
62 if ( oc_check_no_usermod_attr( attr ) ) {
63 Debug( LDAP_DEBUG_ACL, "Operational attribute: %s access allowed\n",
68 memset(matches, 0, sizeof(matches));
70 a = acl_get_applicable( be, op, e, attr, MAXREMATCHES, matches );
73 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
74 Debug( LDAP_DEBUG_ARGS, "=> match[%d]: %d %d ", i,
75 (int)matches[i].rm_so, (int)matches[i].rm_eo );
77 if( matches[i].rm_so <= matches[0].rm_eo ) {
78 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
79 Debug( LDAP_DEBUG_ARGS, "%c", edn[n], 0, 0 );
82 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
86 rc = acl_access_allowed( a, be, conn, e, val, op, access, edn, matches );
88 Debug( LDAP_DEBUG_ACL, "\n=> access_allowed: exit (%s) attr (%s)\n",
95 * acl_get_applicable - return the acl applicable to entry e, attribute
96 * attr. the acl returned is suitable for use in subsequent calls to
97 * acl_access_allowed().
114 Debug( LDAP_DEBUG_ACL, "\n=> acl_get: entry (%s) attr (%s)\n",
117 if ( be_isroot( be, op->o_ndn ) ) {
118 Debug( LDAP_DEBUG_ACL,
119 "<= acl_get: no acl applicable to database root\n", 0, 0,
126 Debug( LDAP_DEBUG_ARGS, "=> acl_get: edn %s\n", edn, 0, 0 );
128 /* check for a backend-specific acl that matches the entry */
129 for ( i = 1, a = be->be_acl; a != NULL; a = a->acl_next, i++ ) {
130 if (a->acl_dn_pat != NULL) {
131 Debug( LDAP_DEBUG_TRACE, "=> dnpat: [%d] %s nsub: %d\n",
132 i, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub);
134 if (regexec(&a->acl_dn_re, edn, nmatch, matches, 0)) {
138 Debug( LDAP_DEBUG_TRACE, "=> acl_get:[%d] backend ACL match\n",
143 if ( a->acl_filter != NULL ) {
144 if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
149 Debug( LDAP_DEBUG_ARGS, "=> acl_get: [%d] check attr %s\n", i, attr, 0);
151 if ( attr == NULL || a->acl_attrs == NULL ||
152 charray_inlist( a->acl_attrs, attr ) )
154 Debug( LDAP_DEBUG_ACL, "<= acl_get: [%d] backend acl %s attr: %s\n",
158 matches[0].rm_so = matches[0].rm_eo = -1;
161 /* check for a global acl that matches the entry */
162 for ( i = 1, a = global_acl; a != NULL; a = a->acl_next, i++ ) {
163 if (a->acl_dn_pat != NULL) {
164 Debug( LDAP_DEBUG_TRACE, "=> dn pat: [%d] %s nsub: %d\n",
165 i, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub);
167 if (regexec(&a->acl_dn_re, edn, nmatch, matches, 0)) {
171 Debug( LDAP_DEBUG_TRACE, "=> acl_get: [%d] global ACL match\n",
176 if ( a->acl_filter != NULL ) {
177 if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
182 Debug( LDAP_DEBUG_ARGS, "=> acl_get: [%d] check attr\n", i, 0, 0);
184 if ( attr == NULL || a->acl_attrs == NULL ||
185 charray_inlist( a->acl_attrs, attr ) )
187 Debug( LDAP_DEBUG_ACL, "<= acl_get: [%d] global acl %s attr: %s\n",
192 matches[0].rm_so = matches[0].rm_eo = -1;
195 Debug( LDAP_DEBUG_ACL, "<= acl_get: no match\n", 0, 0, 0 );
200 * acl_access_allowed - check whether the given acl allows dn the
201 * requested access to entry e, attribute attr, value val. if val
202 * is null, access to the whole attribute is assumed (all values).
204 * returns 0 access NOT allowed
225 Debug( LDAP_DEBUG_ACL,
226 "\n=> acl_access_allowed: %s access to entry \"%s\"\n",
227 access2str( access ), e->e_dn, 0 );
229 Debug( LDAP_DEBUG_ACL,
230 "\n=> acl_access_allowed: %s access to value \"%s\" by \"%s\"\n",
231 access2str( access ),
232 val ? val->bv_val : "any",
233 op->o_ndn ? op->o_ndn : "" );
235 if ( be_isroot( be, op->o_ndn ) ) {
236 Debug( LDAP_DEBUG_ACL,
237 "<= acl_access_allowed: granted to database root\n",
242 default_access = be->be_dfltaccess ? be->be_dfltaccess : global_default_access;
245 Debug( LDAP_DEBUG_ACL,
246 "<= acl_access_allowed: %s by default (no matching to)\n",
247 default_access >= access ? "granted" : "denied", 0, 0 );
248 return( default_access >= access );
251 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
252 /* AND <who> clauses */
253 if ( b->a_dn_pat != NULL ) {
254 Debug( LDAP_DEBUG_TRACE, "<= check a_dn_pat: %s\n",
257 * if access applies to the entry itself, and the
258 * user is bound as somebody in the same namespace as
259 * the entry, OR the given dn matches the dn pattern
261 if ( strcasecmp( b->a_dn_pat, "anonymous" ) == 0 ) {
262 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
266 } else if ( strcasecmp( b->a_dn_pat, "self" ) == 0 ) {
267 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
271 if ( e->e_dn == NULL || strcmp( edn, op->o_ndn ) != 0 ) {
275 } else if ( strcmp( b->a_dn_pat, ".*" ) != 0 &&
276 !regex_matches( b->a_dn_pat, op->o_ndn, edn, matches ) )
282 if ( b->a_sockurl_pat != NULL ) {
283 Debug( LDAP_DEBUG_ARGS, "<= check a_sockurl_pat: %s\n",
284 b->a_sockurl_pat, 0, 0 );
286 if ( strcmp( b->a_sockurl_pat, ".*" ) != 0 &&
287 !regex_matches( b->a_sockurl_pat, conn->c_listener_url,
294 if ( b->a_domain_pat != NULL ) {
295 Debug( LDAP_DEBUG_ARGS, "<= check a_domain_pat: %s\n",
296 b->a_domain_pat, 0, 0 );
298 if ( strcmp( b->a_domain_pat, ".*" ) != 0 &&
299 !regex_matches( b->a_domain_pat, conn->c_peer_domain,
306 if ( b->a_peername_pat != NULL ) {
307 Debug( LDAP_DEBUG_ARGS, "<= check a_peername_path: %s\n",
308 b->a_peername_pat, 0, 0 );
310 if ( strcmp( b->a_peername_pat, ".*" ) != 0 &&
311 !regex_matches( b->a_peername_pat, conn->c_peer_name,
318 if ( b->a_sockname_pat != NULL ) {
319 Debug( LDAP_DEBUG_ARGS, "<= check a_sockname_path: %s\n",
320 b->a_sockname_pat, 0, 0 );
322 if ( strcmp( b->a_sockname_pat, ".*" ) != 0 &&
323 !regex_matches( b->a_sockname_pat, conn->c_sock_name,
330 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
334 Debug( LDAP_DEBUG_ARGS, "<= check a_dn_at: %s\n",
337 bv.bv_val = op->o_ndn;
338 bv.bv_len = strlen( bv.bv_val );
340 /* see if asker is listed in dnattr */
341 if ( (at = attr_find( e->e_attrs, b->a_dn_at )) != NULL &&
342 value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0 )
344 if ( ACL_IS_SELF(b->a_access) &&
345 (val == NULL || value_cmp( &bv, val, at->a_syntax, 2 )) )
350 /* asker not listed in dnattr - check for self access */
351 } else if ( ! ACL_IS_SELF(b->a_access) || val == NULL ||
352 value_cmp( &bv, val, at->a_syntax, 2 ) != 0 )
358 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
361 /* b->a_group is an unexpanded entry name, expanded it should be an
362 * entry with objectclass group* and we test to see if odn is one of
363 * the values in the attribute group
365 /* see if asker is listed in dnattr */
366 string_expand(buf, sizeof(buf), b->a_group_pat, edn, matches);
367 if ( dn_normalize_case(buf) == NULL ) {
368 /* did not expand to a valid dn */
372 if (backend_group(be, e, buf, op->o_ndn,
373 b->a_group_oc, b->a_group_at) != 0)
379 Debug( LDAP_DEBUG_ACL,
380 "<= acl_access_allowed: matched by clause #%d access %s\n",
382 ACL_GRANT(b->a_access, access) ? "granted" : "denied",
385 return ACL_GRANT(b->a_access, access );
388 Debug( LDAP_DEBUG_ACL,
389 "<= acl_access_allowed: %s by default (no matching by)\n",
390 default_access >= access ? "granted" : "denied", 0, 0 );
392 return( default_access >= access );
396 * acl_check_modlist - check access control on the given entry to see if
397 * it allows the given modifications by the user associated with op.
398 * returns LDAP_SUCCESS mods allowed ok
399 * anything else mods not allowed - return is an error
400 * code indicating the problem
414 char *edn = e->e_ndn;
416 for ( ; mlist != NULL; mlist = mlist->ml_next ) {
417 regmatch_t matches[MAXREMATCHES];
419 /* the lastmod attributes are ignored by ACL checking */
420 if ( oc_check_no_usermod_attr( mlist->ml_type ) ) {
421 Debug( LDAP_DEBUG_ACL, "Operational attribute: %s access allowed\n",
422 mlist->ml_type, 0, 0 );
426 a = acl_get_applicable( be, op, e, mlist->ml_type,
427 MAXREMATCHES, matches );
429 switch ( mlist->ml_op & ~LDAP_MOD_BVALUES ) {
430 case LDAP_MOD_REPLACE:
432 if ( mlist->ml_bvalues == NULL ) {
435 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
436 if ( ! acl_access_allowed( a, be, conn, e, mlist->ml_bvalues[i],
437 op, ACL_WRITE, edn, matches) )
439 return( LDAP_INSUFFICIENT_ACCESS );
444 case LDAP_MOD_DELETE:
445 if ( mlist->ml_bvalues == NULL ) {
446 if ( ! acl_access_allowed( a, be, conn, e,
447 NULL, op, ACL_WRITE, edn, matches) )
449 return( LDAP_INSUFFICIENT_ACCESS );
453 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
454 if ( ! acl_access_allowed( a, be, conn, e, mlist->ml_bvalues[i],
455 op, ACL_WRITE, edn, matches) )
457 return( LDAP_INSUFFICIENT_ACCESS );
464 return( LDAP_SUCCESS );
484 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
485 /* did we previously see a $ */
490 } else if (*sp >= '0' && *sp <= '9' ) {
497 i = matches[n].rm_so;
498 l = matches[n].rm_eo;
499 for ( ; size < 512 && i < l; size++, i++ ) {
517 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
518 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
523 char *pat, /* pattern to expand and match against */
524 char *str, /* string to match against pattern */
525 char *buf, /* buffer with $N expansion variables */
526 regmatch_t *matches /* offsets in buffer for $N expansion variables */
533 if(str == NULL) str = "";
535 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
536 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
538 regerror(rc, &re, error, sizeof(error));
540 Debug( LDAP_DEBUG_TRACE,
541 "compile( \"%s\", \"%s\") failed %s\n",
546 rc = regexec(&re, str, 0, NULL, 0);
549 Debug( LDAP_DEBUG_TRACE,
550 "=> regex_matches: string: %s\n", str, 0, 0 );
551 Debug( LDAP_DEBUG_TRACE,
552 "=> regex_matches: rc: %d %s\n",
553 rc, !rc ? "matches" : "no matches", 0 );