]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
Declare filter_escape_value_x() and make it static.
[openldap] / servers / slapd / acl.c
1 /* acl.c - routines to parse and check acl's */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/regex.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15
16 #include "slap.h"
17 #include "sets.h"
18 #include "lber_pvt.h"
19
20 #define ACL_BUF_SIZE    1024    /* use most appropriate size */
21
22 /*
23  * speed up compares
24  */
25 static struct berval 
26         aci_bv_entry            = BER_BVC("entry"),
27         aci_bv_children         = BER_BVC("children"),
28         aci_bv_br_entry         = BER_BVC("[entry]"),
29         aci_bv_br_all           = BER_BVC("[all]"),
30         aci_bv_access_id        = BER_BVC("access-id"),
31         aci_bv_anonymous        = BER_BVC("anonymous"),
32         aci_bv_public           = BER_BVC("public"),
33         aci_bv_users            = BER_BVC("users"),
34         aci_bv_self             = BER_BVC("self"),
35         aci_bv_dnattr           = BER_BVC("dnattr"),
36         aci_bv_group            = BER_BVC("group"),
37         aci_bv_role             = BER_BVC("role"),
38         aci_bv_set              = BER_BVC("set"),
39         aci_bv_set_ref          = BER_BVC("set-ref"),
40         aci_bv_grant            = BER_BVC("grant"),
41         aci_bv_deny             = BER_BVC("deny"),
42         
43         aci_bv_group_class      = BER_BVC(SLAPD_GROUP_CLASS),
44         aci_bv_group_attr       = BER_BVC(SLAPD_GROUP_ATTR),
45         aci_bv_role_class       = BER_BVC(SLAPD_ROLE_CLASS),
46         aci_bv_role_attr        = BER_BVC(SLAPD_ROLE_ATTR);
47
48
49 static AccessControl * acl_get(
50         AccessControl *ac, int *count,
51         Operation *op, Entry *e,
52         AttributeDescription *desc,
53         struct berval *val,
54         int nmatches, regmatch_t *matches );
55
56 static slap_control_t acl_mask(
57         AccessControl *ac, slap_mask_t *mask,
58         Operation *op, Entry *e,
59         AttributeDescription *desc,
60         struct berval *val,
61         regmatch_t *matches,
62         int count,
63         AccessControlState *state );
64
65 #ifdef SLAPD_ACI_ENABLED
66 static int aci_mask(
67         Operation *op, Entry *e,
68         AttributeDescription *desc,
69         struct berval *val,
70         struct berval *aci,
71         regmatch_t *matches,
72         slap_access_t *grant,
73         slap_access_t *deny,
74         struct berval *scope);
75 #endif
76
77 static int      regex_matches(
78         struct berval *pat, char *str, char *buf, regmatch_t *matches);
79 static void     string_expand(
80         struct berval *newbuf, struct berval *pattern,
81         char *match, regmatch_t *matches);
82
83 typedef struct AciSetCookie {
84         Operation *op;
85         Entry *e;
86 } AciSetCookie;
87
88 SLAP_SET_GATHER aci_set_gather;
89 static int aci_match_set ( struct berval *subj, Operation *op,
90     Entry *e, int setref );
91
92 /*
93  * access_allowed - check whether op->o_ndn is allowed the requested access
94  * to entry e, attribute attr, value val.  if val is null, access to
95  * the whole attribute is assumed (all values).
96  *
97  * This routine loops through all access controls and calls
98  * acl_mask() on each applicable access control.
99  * The loop exits when a definitive answer is reached or
100  * or no more controls remain.
101  *
102  * returns:
103  *              0       access denied
104  *              1       access granted
105  */
106
107 int
108 access_allowed(
109         Operation               *op,
110         Entry           *e,
111         AttributeDescription    *desc,
112         struct berval   *val,
113         slap_access_t   access,
114         AccessControlState *state )
115 {
116         int                             ret = 1;
117         int                             count;
118         AccessControl                   *a = NULL;
119         Backend *be;
120         int     be_null = 0;
121
122 #ifdef LDAP_DEBUG
123         char accessmaskbuf[ACCESSMASK_MAXLEN];
124 #endif
125         slap_mask_t mask;
126         slap_control_t control;
127         const char *attr;
128         regmatch_t matches[MAXREMATCHES];
129         int        st_same_attr = 0;
130         int        st_initialized = 0;
131         static AccessControlState state_init = ACL_STATE_INIT;
132
133         assert( e != NULL );
134         assert( desc != NULL );
135         assert( access > ACL_NONE );
136
137         attr = desc->ad_cname.bv_val;
138
139         assert( attr != NULL );
140
141         if( op && op->o_is_auth_check &&
142                 ( access == ACL_SEARCH || access == ACL_READ ))
143         {
144                 access = ACL_AUTH;
145         }
146
147         if( state && state->as_recorded && state->as_vd_ad==desc) { 
148                 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
149                         val == NULL )
150                 {
151                         return state->as_result;
152
153                 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
154                         val != NULL && state->as_vd_acl == NULL )
155                 {
156                         return state->as_result;
157                 }
158                 st_same_attr = 1;
159         }
160
161         if( state ) {
162                 state->as_vd_ad=desc;
163         }
164
165 #ifdef NEW_LOGGING
166         LDAP_LOG( ACL, ENTRY, 
167                 "access_allowed: %s access to \"%s\" \"%s\" requested\n",
168                 access2str( access ), e->e_dn, attr );
169 #else
170         Debug( LDAP_DEBUG_ACL,
171                 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
172             access2str( access ), e->e_dn, attr );
173 #endif
174
175         if ( op == NULL ) {
176                 /* no-op call */
177                 goto done;
178         }
179
180 #ifdef LDAP_SLAPI
181         ret = slapi_x_access_allowed( op, e, desc, val, access, state );
182         if ( ret == 0 ) {
183                 /* ACL plugin denied access */
184                 goto done;
185         }
186 #endif /* LDAP_SLAPI */
187
188         be = op->o_bd;
189         if ( be == NULL ) {
190                 be = &backends[0];
191                 be_null = 1;
192                 op->o_bd = be;
193         }
194         assert( be != NULL );
195
196         /* grant database root access */
197         if ( be != NULL && be_isroot( be, &op->o_ndn ) ) {
198 #ifdef NEW_LOGGING
199                 LDAP_LOG( ACL, INFO, 
200                         "access_allowed: conn %lu root access granted\n", 
201                         op->o_connid, 0, 0 );
202 #else
203                 Debug( LDAP_DEBUG_ACL,
204                     "<= root access granted\n",
205                         0, 0, 0 );
206 #endif
207                 goto done;
208         }
209
210         /*
211          * no-user-modification operational attributes are ignored
212          * by ACL_WRITE checking as any found here are not provided
213          * by the user
214          */
215         if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
216                 && desc != slap_schema.si_ad_entry
217                 && desc != slap_schema.si_ad_children )
218         {
219 #ifdef NEW_LOGGING
220                 LDAP_LOG( ACL, DETAIL1, 
221                         "access_allowed: conn %lu NoUserMod Operational attribute: %s "
222                         "access granted\n", op->o_connid, attr , 0 );
223 #else
224                 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
225                         " %s access granted\n",
226                         attr, 0, 0 );
227 #endif
228                 goto done;
229         }
230
231         /* use backend default access if no backend acls */
232         if( be != NULL && be->be_acl == NULL ) {
233 #ifdef NEW_LOGGING
234                 LDAP_LOG( ACL, DETAIL1, 
235                         "access_allowed: backend default %s access %s to \"%s\"\n",
236                     access2str( access ),
237                     be->be_dfltaccess >= access ? "granted" : "denied", 
238                         op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
239 #else
240                 Debug( LDAP_DEBUG_ACL,
241                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
242                         access2str( access ),
243                         be->be_dfltaccess >= access ? "granted" : "denied",
244                         op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
245 #endif
246                 ret = be->be_dfltaccess >= access;
247                 goto done;
248
249 #ifdef notdef
250         /* be is always non-NULL */
251         /* use global default access if no global acls */
252         } else if ( be == NULL && global_acl == NULL ) {
253 #ifdef NEW_LOGGING
254                 LDAP_LOG( ACL, DETAIL1, 
255                         "access_allowed: global default %s access %s to \"%s\"\n",
256                     access2str( access ),
257                     global_default_access >= access ? "granted" : "denied", 
258                         op->o_dn.bv_val );
259 #else
260                 Debug( LDAP_DEBUG_ACL,
261                         "=> access_allowed: global default %s access %s to \"%s\"\n",
262                         access2str( access ),
263                         global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val );
264 #endif
265                 ret = global_default_access >= access;
266                 goto done;
267 #endif
268         }
269
270         ret = 0;
271         control = ACL_BREAK;
272
273         if( st_same_attr ) {
274                 assert( state->as_vd_acl != NULL );
275
276                 a = state->as_vd_acl;
277                 mask = state->as_vd_acl_mask;
278                 count = state->as_vd_acl_count;
279                 AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
280                 goto vd_access;
281
282         } else {
283                 if ( state ) state->as_vi_acl = NULL;
284                 a = NULL;
285                 ACL_INIT(mask);
286                 count = 0;
287                 memset(matches, '\0', sizeof(matches));
288         }
289
290         while((a = acl_get( a, &count, op, e, desc, val,
291                 MAXREMATCHES, matches )) != NULL)
292         {
293                 int i;
294
295                 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
296 #ifdef NEW_LOGGING
297                         LDAP_LOG( ACL, DETAIL1, 
298                                 "access_allowed: match[%d]:  %d %d ",
299                             i, (int)matches[i].rm_so, (int)matches[i].rm_eo );
300 #else
301                         Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
302                             (int)matches[i].rm_so, (int)matches[i].rm_eo );
303 #endif
304                         if( matches[i].rm_so <= matches[0].rm_eo ) {
305                                 int n;
306                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
307                                         Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
308                                 }
309                         }
310 #ifdef NEW_LOGGING
311                         LDAP_LOG( ACL, ARGS, "\n" , 0, 0, 0 );
312 #else
313                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
314 #endif
315                 }
316
317                 if (state) {
318                         if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
319                                 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
320                                 ret = state->as_result;
321                                 goto done;
322                         } else if (!st_initialized) {
323                                 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
324                             *state = state_init;
325                                 state->as_vd_ad=desc;
326                                 st_initialized=1;
327                         }
328                 }
329
330 vd_access:
331                 control = acl_mask( a, &mask, op,
332                         e, desc, val, matches, count, state );
333
334                 if ( control != ACL_BREAK ) {
335                         break;
336                 }
337
338                 memset(matches, '\0', sizeof(matches));
339         }
340
341         if ( ACL_IS_INVALID( mask ) ) {
342 #ifdef NEW_LOGGING
343                 LDAP_LOG( ACL, DETAIL1, 
344                         "access_allowed: conn %lu \"%s\" (%s) invalid!\n",
345                     op->o_connid, e->e_dn, attr );
346 #else
347                 Debug( LDAP_DEBUG_ACL,
348                         "=> access_allowed: \"%s\" (%s) invalid!\n",
349                         e->e_dn, attr, 0 );
350 #endif
351                 ACL_INIT(mask);
352
353         } else if ( control == ACL_BREAK ) {
354 #ifdef NEW_LOGGING
355                 LDAP_LOG( ACL, DETAIL1, 
356                         "access_allowed: conn %lu        no more rules\n", op->o_connid, 0,0 );
357 #else
358                 Debug( LDAP_DEBUG_ACL,
359                         "=> access_allowed: no more rules\n", 0, 0, 0);
360 #endif
361
362                 goto done;
363         }
364
365 #ifdef NEW_LOGGING
366         LDAP_LOG( ACL, ENTRY, 
367                 "access_allowed: %s access %s by %s\n", 
368                 access2str( access ), ACL_GRANT( mask, access ) ? "granted" : "denied",
369                 accessmask2str( mask, accessmaskbuf ) );
370 #else
371         Debug( LDAP_DEBUG_ACL,
372                 "=> access_allowed: %s access %s by %s\n",
373                 access2str( access ),
374                 ACL_GRANT(mask, access) ? "granted" : "denied",
375                 accessmask2str( mask, accessmaskbuf ) );
376 #endif
377
378         ret = ACL_GRANT(mask, access);
379
380 done:
381         if( state != NULL ) {
382                 /* If not value-dependent, save ACL in case of more attrs */
383                 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
384                         state->as_vi_acl = a;
385                         state->as_result = ret;
386                 }
387                 state->as_recorded |= ACL_STATE_RECORDED;
388         }
389         if (be_null) op->o_bd = NULL;
390         return ret;
391 }
392
393 /*
394  * acl_get - return the acl applicable to entry e, attribute
395  * attr.  the acl returned is suitable for use in subsequent calls to
396  * acl_access_allowed().
397  */
398
399 static AccessControl *
400 acl_get(
401         AccessControl *a,
402         int                     *count,
403         Operation       *op,
404         Entry           *e,
405         AttributeDescription *desc,
406         struct berval   *val,
407         int                     nmatch,
408         regmatch_t      *matches )
409 {
410         const char *attr;
411         int dnlen, patlen;
412
413         assert( e != NULL );
414         assert( count != NULL );
415         assert( desc != NULL );
416
417         attr = desc->ad_cname.bv_val;
418
419         assert( attr != NULL );
420
421         if( a == NULL ) {
422                 if( op->o_bd == NULL ) {
423                         a = global_acl;
424                 } else {
425                         a = op->o_bd->be_acl;
426                 }
427
428                 assert( a != NULL );
429
430         } else {
431                 a = a->acl_next;
432         }
433
434         dnlen = e->e_nname.bv_len;
435
436         for ( ; a != NULL; a = a->acl_next ) {
437                 (*count) ++;
438
439                 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
440                         if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
441 #ifdef NEW_LOGGING
442                                 LDAP_LOG( ACL, DETAIL1, 
443                                         "acl_get: dnpat [%d] %s nsub: %d\n",
444                                         *count, a->acl_dn_pat.bv_val, 
445                                         (int) a->acl_dn_re.re_nsub );
446 #else
447                                 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n", 
448                                         *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
449 #endif
450                                 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
451                                         continue;
452
453                         } else {
454 #ifdef NEW_LOGGING
455                                 LDAP_LOG( ACL, DETAIL1, "acl_get: dn [%d] %s\n",
456                                            *count, a->acl_dn_pat.bv_val, 0 );
457 #else
458                                 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n", 
459                                         *count, a->acl_dn_pat.bv_val, 0 );
460 #endif
461                                 patlen = a->acl_dn_pat.bv_len;
462                                 if ( dnlen < patlen )
463                                         continue;
464
465                                 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
466                                         /* base dn -- entire object DN must match */
467                                         if ( dnlen != patlen )
468                                                 continue;
469
470                                 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
471                                         int rdnlen = -1;
472
473                                         if ( dnlen <= patlen )
474                                                 continue;
475
476                                         if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
477                                                 continue;
478
479                                         rdnlen = dn_rdnlen( NULL, &e->e_nname );
480                                         if ( rdnlen != dnlen - patlen - 1 )
481                                                 continue;
482
483                                 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
484                                         if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
485                                                 continue;
486
487                                 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
488                                         if ( dnlen <= patlen )
489                                                 continue;
490                                         if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
491                                                 continue;
492                                 }
493
494                                 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
495                                         continue;
496                         }
497
498 #ifdef NEW_LOGGING
499                         LDAP_LOG( ACL, DETAIL1, 
500                                 "acl_get: [%d] matched\n", *count, 0, 0 );
501 #else
502                         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
503                                 *count, 0, 0 );
504 #endif
505                 }
506
507                 if ( a->acl_filter != NULL ) {
508                         ber_int_t rc = test_filter( NULL, e, a->acl_filter );
509                         if ( rc != LDAP_COMPARE_TRUE ) {
510                                 continue;
511                         }
512                 }
513
514 #ifdef NEW_LOGGING
515                 LDAP_LOG( ACL, DETAIL1, 
516                         "acl_get: [%d] check attr %s\n", *count, attr ,0 );
517 #else
518                 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
519                        *count, attr, 0);
520 #endif
521                 if ( a->acl_attrs == NULL ||
522                         ad_inlist( desc, a->acl_attrs ) )
523                 {
524 #ifdef NEW_LOGGING
525                         LDAP_LOG( ACL, DETAIL1, 
526                                 "acl_get:  [%d] acl %s attr: %s\n", *count, e->e_dn, attr );
527 #else
528                         Debug( LDAP_DEBUG_ACL,
529                                 "<= acl_get: [%d] acl %s attr: %s\n",
530                                 *count, e->e_dn, attr );
531 #endif
532                         return a;
533                 }
534                 matches[0].rm_so = matches[0].rm_eo = -1;
535         }
536
537 #ifdef NEW_LOGGING
538         LDAP_LOG( ACL, RESULTS, "acl_get: done.\n", 0, 0, 0 );
539 #else
540         Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
541 #endif
542         return( NULL );
543 }
544
545 /*
546  * Record value-dependent access control state
547  */
548 #define ACL_RECORD_VALUE_STATE do { \
549                 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
550                         state->as_recorded |= ACL_STATE_RECORDED_VD; \
551                         state->as_vd_acl = a; \
552                         AC_MEMCPY( state->as_vd_acl_matches, matches, \
553                                 sizeof( state->as_vd_acl_matches )) ; \
554                         state->as_vd_acl_count = count; \
555                         state->as_vd_access = b; \
556                         state->as_vd_access_count = i; \
557                 } \
558         } while( 0 )
559
560 /*
561  * acl_mask - modifies mask based upon the given acl and the
562  * requested access to entry e, attribute attr, value val.  if val
563  * is null, access to the whole attribute is assumed (all values).
564  *
565  * returns      0       access NOT allowed
566  *              1       access allowed
567  */
568
569 static slap_control_t
570 acl_mask(
571         AccessControl   *a,
572         slap_mask_t *mask,
573         Operation       *op,
574         Entry           *e,
575         AttributeDescription *desc,
576         struct berval   *val,
577         regmatch_t      *matches,
578         int     count,
579         AccessControlState *state )
580 {
581         int             i, odnlen, patlen;
582         Access  *b;
583 #ifdef LDAP_DEBUG
584         char accessmaskbuf[ACCESSMASK_MAXLEN];
585         char accessmaskbuf1[ACCESSMASK_MAXLEN];
586 #endif
587         const char *attr;
588
589         assert( a != NULL );
590         assert( mask != NULL );
591         assert( desc != NULL );
592
593         attr = desc->ad_cname.bv_val;
594
595         assert( attr != NULL );
596
597 #ifdef NEW_LOGGING
598         LDAP_LOG( ACL, ENTRY, 
599                 "acl_mask: conn %lu  access to entry \"%s\", attr \"%s\" requested\n",
600                 op->o_connid, e->e_dn, attr );
601
602         LDAP_LOG( ACL, ARGS, 
603                 " to %s by \"%s\", (%s) \n", val ? "value" : "all values",
604                 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
605                 accessmask2str( *mask, accessmaskbuf ) );
606 #else
607         Debug( LDAP_DEBUG_ACL,
608                 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
609                 e->e_dn, attr, 0 );
610
611         Debug( LDAP_DEBUG_ACL,
612                 "=> acl_mask: to %s by \"%s\", (%s) \n",
613                 val ? "value" : "all values",
614                 op->o_ndn.bv_val ?  op->o_ndn.bv_val : "",
615                 accessmask2str( *mask, accessmaskbuf ) );
616 #endif
617
618         /* Is this ACL only for a specific value? */
619         if ( a->acl_attrval.bv_len ) {
620                 if ( state && !state->as_vd_acl ) {
621                         state->as_vd_acl = a;
622                         state->as_vd_access = a->acl_access;
623                         state->as_vd_access_count = 1;
624                 }
625                 if ( val == NULL ) {
626                         return ACL_BREAK;
627                 }
628                 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
629 #ifdef NEW_LOGGING
630                         LDAP_LOG( ACL, DETAIL1, 
631                                 "acl_get: valpat %s\n",
632                                 a->acl_attrval.bv_val, 0, 0 );
633 #else
634                         Debug( LDAP_DEBUG_ACL,
635                                 "acl_get: valpat %s\n",
636                                 a->acl_attrval.bv_val, 0, 0 );
637 #endif
638                         if (regexec(&a->acl_attrval_re, val->bv_val, 0, NULL, 0))
639                                 return ACL_BREAK;
640                 } else {
641                         int match = 0;
642                         const char *text;
643 #ifdef NEW_LOGGING
644                         LDAP_LOG( ACL, DETAIL1, 
645                                 "acl_get: val %s\n",
646                                 a->acl_attrval.bv_val, 0, 0 );
647 #else
648                         Debug( LDAP_DEBUG_ACL,
649                                 "acl_get: val %s\n",
650                                 a->acl_attrval.bv_val, 0, 0 );
651 #endif
652                         if (value_match( &match, desc,
653                                 desc->ad_type->sat_equality, 0,
654                                 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
655                                         match )
656                                 return ACL_BREAK;
657                 }
658         }
659
660         if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
661                 && state->as_vd_acl == a )
662         {
663                 b = state->as_vd_access;
664                 i = state->as_vd_access_count;
665
666         } else {
667                 b = a->acl_access;
668                 i = 1;
669         }
670
671         for ( ; b != NULL; b = b->a_next, i++ ) {
672                 slap_mask_t oldmask, modmask;
673
674                 ACL_INVALIDATE( modmask );
675
676                 /* AND <who> clauses */
677                 if ( b->a_dn_pat.bv_len != 0 ) {
678 #ifdef NEW_LOGGING
679                         LDAP_LOG( ACL, DETAIL1, 
680                                 "acl_mask: conn %lu  check a_dn_pat: %s\n",
681                                 op->o_connid, b->a_dn_pat.bv_val ,0 );
682 #else
683                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
684                                 b->a_dn_pat.bv_val, 0, 0);
685 #endif
686                         /*
687                          * if access applies to the entry itself, and the
688                          * user is bound as somebody in the same namespace as
689                          * the entry, OR the given dn matches the dn pattern
690                          */
691                         if ( bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) ) {
692                                 if ( op->o_ndn.bv_len != 0 ) {
693                                         continue;
694                                 }
695
696                         } else if ( bvmatch( &b->a_dn_pat, &aci_bv_users ) ) {
697                                 if ( op->o_ndn.bv_len == 0 ) {
698                                         continue;
699                                 }
700
701                         } else if ( bvmatch( &b->a_dn_pat, &aci_bv_self ) ) {
702                                 if ( op->o_ndn.bv_len == 0 ) {
703                                         continue;
704                                 }
705                                 
706                                 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
707                                         continue;
708                                 }
709
710                         } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
711                                 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
712                                         int ret = regex_matches( &b->a_dn_pat,
713                                                 op->o_ndn.bv_val, e->e_ndn, matches );
714
715                                         if( ret == 0 ) {
716                                                 continue;
717                                         }
718                                 }
719
720                         } else {
721                                 struct berval pat;
722                                 int got_match = 0;
723
724                                 if ( e->e_dn == NULL )
725                                         continue;
726
727                                 if ( b->a_dn_expand ) {
728                                         struct berval bv;
729                                         char buf[ACL_BUF_SIZE];
730
731                                         bv.bv_len = sizeof( buf ) - 1;
732                                         bv.bv_val = buf;
733
734                                         string_expand(&bv, &b->a_dn_pat, 
735                                                         e->e_ndn, matches);
736                                         if ( dnNormalize(0, NULL, NULL, &bv, &pat, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
737                                                 /* did not expand to a valid dn */
738                                                 continue;
739                                         }
740                                 } else {
741                                         pat = b->a_dn_pat;
742                                 }
743
744                                 patlen = pat.bv_len;
745                                 odnlen = op->o_ndn.bv_len;
746                                 if ( odnlen < patlen ) {
747                                         goto dn_match_cleanup;
748
749                                 }
750
751                                 if ( b->a_dn_style == ACL_STYLE_BASE ) {
752                                         /* base dn -- entire object DN must match */
753                                         if ( odnlen != patlen ) {
754                                                 goto dn_match_cleanup;
755                                         }
756
757                                 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
758                                         int rdnlen = -1;
759
760                                         if ( odnlen <= patlen ) {
761                                                 goto dn_match_cleanup;
762                                         }
763
764                                         if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
765                                                 goto dn_match_cleanup;
766                                         }
767
768                                         rdnlen = dn_rdnlen( NULL, &op->o_ndn );
769                                         if ( rdnlen != odnlen - patlen - 1 ) {
770                                                 goto dn_match_cleanup;
771                                         }
772
773                                 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
774                                         if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
775                                                 goto dn_match_cleanup;
776                                         }
777
778                                 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
779                                         if ( odnlen <= patlen ) {
780                                                 goto dn_match_cleanup;
781                                         }
782
783                                         if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
784                                                 goto dn_match_cleanup;
785                                         }
786                                 }
787
788                                 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
789
790 dn_match_cleanup:;
791                                 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
792                                         free( pat.bv_val );
793                                 }
794
795                                 if ( !got_match ) {
796                                         continue;
797                                 }
798                         }
799                 }
800
801                 if ( b->a_sockurl_pat.bv_len ) {
802                         if ( ! op->o_conn->c_listener ) {
803                                 continue;
804                         }
805 #ifdef NEW_LOGGING
806                         LDAP_LOG( ACL, DETAIL1, 
807                                    "acl_mask: conn %lu  check a_sockurl_pat: %s\n",
808                                    op->o_connid, b->a_sockurl_pat.bv_val , 0 );
809 #else
810                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
811                                 b->a_sockurl_pat.bv_val, 0, 0 );
812 #endif
813
814                         if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
815                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
816                                         if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
817                                                         e->e_ndn, matches ) ) 
818                                         {
819                                                 continue;
820                                         }
821                                 } else {
822                                         if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
823                                                 continue;
824                                 }
825                         }
826                 }
827
828                 if ( b->a_domain_pat.bv_len ) {
829                         if ( !op->o_conn->c_peer_domain.bv_val ) {
830                                 continue;
831                         }
832 #ifdef NEW_LOGGING
833                         LDAP_LOG( ACL, DETAIL1, 
834                                    "acl_mask: conn %lu  check a_domain_pat: %s\n",
835                                    op->o_connid, b->a_domain_pat.bv_val , 0 );
836 #else
837                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
838                                 b->a_domain_pat.bv_val, 0, 0 );
839 #endif
840                         if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
841                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
842                                         if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
843                                                         e->e_ndn, matches ) ) 
844                                         {
845                                                 continue;
846                                         }
847                                 } else {
848                                         char buf[ACL_BUF_SIZE];
849
850                                         struct berval   cmp = op->o_conn->c_peer_domain;
851                                         struct berval   pat = b->a_domain_pat;
852
853                                         if ( b->a_domain_expand ) {
854                                                 struct berval bv;
855
856                                                 bv.bv_len = sizeof(buf) - 1;
857                                                 bv.bv_val = buf;
858
859                                                 string_expand(&bv, &b->a_domain_pat, e->e_ndn, matches);
860                                                 pat = bv;
861                                         }
862
863                                         if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
864                                                 int offset = cmp.bv_len - pat.bv_len;
865                                                 if ( offset < 0 ) {
866                                                         continue;
867                                                 }
868
869                                                 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
870                                                         continue;
871                                                 }
872
873                                                 /* trim the domain */
874                                                 cmp.bv_val = &cmp.bv_val[ offset ];
875                                                 cmp.bv_len -= offset;
876                                         }
877                                         
878                                         if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
879                                                 continue;
880                                         }
881                                 }
882                         }
883                 }
884
885                 if ( b->a_peername_pat.bv_len ) {
886                         if ( !op->o_conn->c_peer_name.bv_val ) {
887                                 continue;
888                         }
889 #ifdef NEW_LOGGING
890                         LDAP_LOG( ACL, DETAIL1, 
891                                    "acl_mask: conn %lu  check a_peername_path: %s\n",
892                                    op->o_connid, b->a_peername_pat.bv_val , 0 );
893 #else
894                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
895                                 b->a_peername_pat.bv_val, 0, 0 );
896 #endif
897                         if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
898                                 if ( b->a_peername_style == ACL_STYLE_REGEX) {
899                                         if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
900                                                         e->e_ndn, matches ) ) 
901                                         {
902                                                 continue;
903                                         }
904                                 } else {
905                                         if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 )
906                                                 continue;
907                                 }
908                         }
909                 }
910
911                 if ( b->a_sockname_pat.bv_len ) {
912                         if ( !op->o_conn->c_sock_name.bv_val ) {
913                                 continue;
914                         }
915 #ifdef NEW_LOGGING
916                         LDAP_LOG( ACL, DETAIL1, 
917                                    "acl_mask: conn %lu  check a_sockname_path: %s\n",
918                                    op->o_connid, b->a_sockname_pat.bv_val , 0 );
919 #else
920                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
921                                 b->a_sockname_pat.bv_val, 0, 0 );
922 #endif
923                         if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
924                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
925                                         if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
926                                                         e->e_ndn, matches ) ) 
927                                         {
928                                                 continue;
929                                         }
930                                 } else {
931                                         if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 )
932                                                 continue;
933                                 }
934                         }
935                 }
936
937                 if ( b->a_dn_at != NULL ) {
938                         Attribute       *at;
939                         struct berval   bv;
940                         int rc, match = 0;
941                         const char *text;
942                         const char *attr = b->a_dn_at->ad_cname.bv_val;
943
944                         assert( attr != NULL );
945
946                         if ( op->o_ndn.bv_len == 0 ) {
947                                 continue;
948                         }
949
950 #ifdef NEW_LOGGING
951                         LDAP_LOG( ACL, DETAIL1, 
952                                    "acl_mask: conn %lu  check a_dn_pat: %s\n",
953                                    op->o_connid, attr , 0 );
954 #else
955                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
956                                 attr, 0, 0);
957 #endif
958                         bv = op->o_ndn;
959
960                         /* see if asker is listed in dnattr */
961                         for( at = attrs_find( e->e_attrs, b->a_dn_at );
962                                 at != NULL;
963                                 at = attrs_find( at->a_next, b->a_dn_at ) )
964                         {
965                                 if( value_find_ex( b->a_dn_at,
966                                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
967                                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
968                                         at->a_nvals,
969                                         &bv, op->o_tmpmemctx ) == 0 )
970                                 {
971                                         /* found it */
972                                         match = 1;
973                                         break;
974                                 }
975                         }
976
977                         if( match ) {
978                                 /* have a dnattr match. if this is a self clause then
979                                  * the target must also match the op dn.
980                                  */
981                                 if ( b->a_dn_self ) {
982                                         /* check if the target is an attribute. */
983                                         if ( val == NULL ) continue;
984
985                                         /* target is attribute, check if the attribute value
986                                          * is the op dn.
987                                          */
988                                         rc = value_match( &match, b->a_dn_at,
989                                                 b->a_dn_at->ad_type->sat_equality, 0,
990                                                 val, &bv, &text );
991                                         /* on match error or no match, fail the ACL clause */
992                                         if (rc != LDAP_SUCCESS || match != 0 )
993                                                 continue;
994                                 }
995                         } else {
996                                 /* no dnattr match, check if this is a self clause */
997                                 if ( ! b->a_dn_self )
998                                         continue;
999
1000                                 ACL_RECORD_VALUE_STATE;
1001                                 
1002                                 /* this is a self clause, check if the target is an
1003                                  * attribute.
1004                                  */
1005                                 if ( val == NULL )
1006                                         continue;
1007
1008                                 /* target is attribute, check if the attribute value
1009                                  * is the op dn.
1010                                  */
1011                                 rc = value_match( &match, b->a_dn_at,
1012                                         b->a_dn_at->ad_type->sat_equality, 0,
1013                                         val, &bv, &text );
1014
1015                                 /* on match error or no match, fail the ACL clause */
1016                                 if (rc != LDAP_SUCCESS || match != 0 )
1017                                         continue;
1018                         }
1019                 }
1020
1021                 if ( b->a_group_pat.bv_len ) {
1022                         struct berval bv;
1023                         struct berval ndn = { 0, NULL };
1024                         int rc;
1025
1026                         if ( op->o_ndn.bv_len == 0 ) {
1027                                 continue;
1028                         }
1029
1030                         /* b->a_group is an unexpanded entry name, expanded it should be an 
1031                          * entry with objectclass group* and we test to see if odn is one of
1032                          * the values in the attribute group
1033                          */
1034                         /* see if asker is listed in dnattr */
1035                         if ( b->a_group_style == ACL_STYLE_REGEX ) {
1036                                 char buf[ACL_BUF_SIZE];
1037                                 bv.bv_len = sizeof(buf) - 1;
1038                                 bv.bv_val = buf; 
1039
1040                                 string_expand( &bv, &b->a_group_pat, e->e_ndn, matches );
1041                                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
1042                                         /* did not expand to a valid dn */
1043                                         continue;
1044                                 }
1045
1046                                 bv = ndn;
1047
1048                         } else {
1049                                 bv = b->a_group_pat;
1050                         }
1051
1052                         rc = backend_group( op, e, &bv, &op->o_ndn,
1053                                 b->a_group_oc, b->a_group_at );
1054
1055                         if ( ndn.bv_val ) free( ndn.bv_val );
1056
1057                         if ( rc != 0 ) {
1058                                 continue;
1059                         }
1060                 }
1061
1062                 if ( b->a_set_pat.bv_len != 0 ) {
1063                         struct berval bv;
1064                         char buf[ACL_BUF_SIZE];
1065                         if( b->a_set_style == ACL_STYLE_REGEX ){
1066                                 bv.bv_len = sizeof(buf) - 1;
1067                                 bv.bv_val = buf;
1068                                 string_expand( &bv, &b->a_set_pat, e->e_ndn, matches );
1069                         }else{
1070                                 bv = b->a_set_pat;
1071                         }
1072                         if (aci_match_set( &bv, op, e, 0 ) == 0) {
1073                                 continue;
1074                         }
1075                 }
1076
1077                 if ( b->a_authz.sai_ssf ) {
1078 #ifdef NEW_LOGGING
1079                         LDAP_LOG( ACL, DETAIL1, 
1080                                 "acl_mask: conn %lu  check a_authz.sai_ssf: ACL %u > OP %u\n",
1081                                 op->o_connid, b->a_authz.sai_ssf, op->o_ssf  );
1082 #else
1083                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1084                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
1085 #endif
1086                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
1087                                 continue;
1088                         }
1089                 }
1090
1091                 if ( b->a_authz.sai_transport_ssf ) {
1092 #ifdef NEW_LOGGING
1093                         LDAP_LOG( ACL, DETAIL1, 
1094                                 "acl_mask: conn %lu  check a_authz.sai_transport_ssf: "
1095                                 "ACL %u > OP %u\n",
1096                                 op->o_connid, b->a_authz.sai_transport_ssf, 
1097                                 op->o_transport_ssf  );
1098 #else
1099                         Debug( LDAP_DEBUG_ACL,
1100                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1101                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1102 #endif
1103                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
1104                                 continue;
1105                         }
1106                 }
1107
1108                 if ( b->a_authz.sai_tls_ssf ) {
1109 #ifdef NEW_LOGGING
1110                         LDAP_LOG( ACL, DETAIL1, 
1111                                 "acl_mask: conn %lu  check a_authz.sai_tls_ssf: ACL %u > "
1112                                 "OP %u\n",
1113                                 op->o_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf  );
1114 #else
1115                         Debug( LDAP_DEBUG_ACL,
1116                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1117                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1118 #endif
1119                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
1120                                 continue;
1121                         }
1122                 }
1123
1124                 if ( b->a_authz.sai_sasl_ssf ) {
1125 #ifdef NEW_LOGGING
1126                         LDAP_LOG( ACL, DETAIL1, 
1127                            "acl_mask: conn %lu check a_authz.sai_sasl_ssf: " 
1128                            "ACL %u > OP %u\n",
1129                                 op->o_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf );
1130 #else
1131                         Debug( LDAP_DEBUG_ACL,
1132                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1133                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1134 #endif
1135                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
1136                                 continue;
1137                         }
1138                 }
1139
1140 #ifdef SLAPD_ACI_ENABLED
1141                 if ( b->a_aci_at != NULL ) {
1142                         Attribute       *at;
1143                         slap_access_t grant, deny, tgrant, tdeny;
1144                         struct berval parent_ndn, old_parent_ndn;
1145                         BerVarray bvals = NULL;
1146                         int ret,stop;
1147
1148                         /* this case works different from the others above.
1149                          * since aci's themselves give permissions, we need
1150                          * to first check b->a_access_mask, the ACL's access level.
1151                          */
1152
1153                         if ( e->e_nname.bv_len == 0 ) {
1154                                 /* no ACIs in the root DSE */
1155                                 continue;
1156                         }
1157
1158                         /* first check if the right being requested
1159                          * is allowed by the ACL clause.
1160                          */
1161                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1162                                 continue;
1163                         }
1164                         /* start out with nothing granted, nothing denied */
1165                         ACL_INIT(tgrant);
1166                         ACL_INIT(tdeny);
1167
1168                         /* get the aci attribute */
1169                         at = attr_find( e->e_attrs, b->a_aci_at );
1170                         if ( at != NULL ) {
1171                                 ACL_RECORD_VALUE_STATE;
1172                                 /* the aci is an multi-valued attribute.  The
1173                                 * rights are determined by OR'ing the individual
1174                                 * rights given by the acis.
1175                                 */
1176                                 for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
1177                                         if (aci_mask( op,
1178                                                 e, desc, val,
1179                                                 &at->a_nvals[i],
1180                                                 matches, &grant, &deny,  &aci_bv_entry ) != 0)
1181                                         {
1182                                                 tgrant |= grant;
1183                                                 tdeny |= deny;
1184                                         }
1185                                 }
1186                                 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1187                                           accessmask2str(tgrant,accessmaskbuf), 
1188                                           accessmask2str(tdeny, accessmaskbuf1), 0);
1189
1190                         }
1191                         /* If the entry level aci didn't contain anything valid for the 
1192                          * current operation, climb up the tree and evaluate the
1193                          * acis with scope set to subtree
1194                          */
1195                         if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1196                                 dnParent(&(e->e_nname), &parent_ndn);
1197                                 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1198                                         old_parent_ndn = parent_ndn;
1199                                         Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1200                                         ret=backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals);
1201                                         switch(ret){
1202                                                 case LDAP_SUCCESS :
1203                                                         if(bvals){
1204                                                                 for( i = 0; bvals[i].bv_val != NULL; i++){
1205                                                                         ACL_RECORD_VALUE_STATE;
1206                                                                         if (aci_mask(op, e, desc, val, &bvals[i], matches,
1207                                                                                         &grant, &deny, &aci_bv_children) != 0) {
1208                                                                                 tgrant |= grant;
1209                                                                                 tdeny |= deny;
1210                                                                                 /* evaluation stops as soon as either a "deny" or a 
1211                                                                                  * "grant" directive matches.
1212                                                                                  */
1213                                                                                 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1214                                                                                         stop=1;
1215                                                                                 }
1216                                                                         }
1217                                                                         Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n", 
1218                                                                                 accessmask2str(tgrant,accessmaskbuf),
1219                                                                                 accessmask2str(tdeny, accessmaskbuf1), 0);
1220                                                                 }
1221                                                         }
1222                                                         stop=0;
1223                                                         break;
1224                                                 case LDAP_NO_SUCH_ATTRIBUTE:
1225                                                         /* just go on if the aci-Attribute is not present in
1226                                                          * the current entry 
1227                                                          */
1228                                                         Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1229                                                         stop=0;
1230                                                         break;
1231                                                 case LDAP_NO_SUCH_OBJECT:
1232                                                         /* We have reached the base object */
1233                                                         Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1234                                                         stop=1;
1235                                                         break;
1236                                                 default:
1237                                                         stop=1;
1238                                                         break;
1239                                         }
1240                                         if(stop){
1241                                                 break;
1242                                         }
1243                                         dnParent(&old_parent_ndn, &parent_ndn);
1244                                 }
1245                         }
1246
1247
1248                         /* remove anything that the ACL clause does not allow */
1249                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1250                         tdeny &= ACL_PRIV_MASK;
1251
1252                         /* see if we have anything to contribute */
1253                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
1254                                 continue;
1255                         }
1256
1257                         /* this could be improved by changing acl_mask so that it can deal with
1258                          * by clauses that return grant/deny pairs.  Right now, it does either
1259                          * additive or subtractive rights, but not both at the same time.  So,
1260                          * we need to combine the grant/deny pair into a single rights mask in
1261                          * a smart way:  if either grant or deny is "empty", then we use the
1262                          * opposite as is, otherwise we remove any denied rights from the grant
1263                          * rights mask and construct an additive mask.
1264                          */
1265                         if (ACL_IS_INVALID(tdeny)) {
1266                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
1267
1268                         } else if (ACL_IS_INVALID(tgrant)) {
1269                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1270
1271                         } else {
1272                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1273                         }
1274
1275                 } else
1276 #endif
1277                 {
1278                         modmask = b->a_access_mask;
1279                 }
1280
1281 #ifdef NEW_LOGGING
1282                 LDAP_LOG( ACL, RESULTS, 
1283                            "acl_mask: [%d] applying %s (%s)\n",
1284                            i, accessmask2str( modmask, accessmaskbuf),
1285                            b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
1286                            ? "break" : "stop"  );
1287 #else
1288                 Debug( LDAP_DEBUG_ACL,
1289                         "<= acl_mask: [%d] applying %s (%s)\n",
1290                         i, accessmask2str( modmask, accessmaskbuf ), 
1291                         b->a_type == ACL_CONTINUE
1292                                 ? "continue"
1293                                 : b->a_type == ACL_BREAK
1294                                         ? "break"
1295                                         : "stop" );
1296 #endif
1297                 /* save old mask */
1298                 oldmask = *mask;
1299
1300                 if( ACL_IS_ADDITIVE(modmask) ) {
1301                         /* add privs */
1302                         ACL_PRIV_SET( *mask, modmask );
1303
1304                         /* cleanup */
1305                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1306
1307                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1308                         /* substract privs */
1309                         ACL_PRIV_CLR( *mask, modmask );
1310
1311                         /* cleanup */
1312                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1313
1314                 } else {
1315                         /* assign privs */
1316                         *mask = modmask;
1317                 }
1318
1319 #ifdef NEW_LOGGING
1320                 LDAP_LOG( ACL, DETAIL1, 
1321                            "acl_mask: conn %lu  [%d] mask: %s\n",
1322                            op->o_connid, i, accessmask2str( *mask, accessmaskbuf)  );
1323 #else
1324                 Debug( LDAP_DEBUG_ACL,
1325                         "<= acl_mask: [%d] mask: %s\n",
1326                         i, accessmask2str(*mask, accessmaskbuf), 0 );
1327 #endif
1328
1329                 if( b->a_type == ACL_CONTINUE ) {
1330                         continue;
1331
1332                 } else if ( b->a_type == ACL_BREAK ) {
1333                         return ACL_BREAK;
1334
1335                 } else {
1336                         return ACL_STOP;
1337                 }
1338         }
1339
1340         /* implicit "by * none" clause */
1341         ACL_INIT(*mask);
1342
1343 #ifdef NEW_LOGGING
1344         LDAP_LOG( ACL, RESULTS, 
1345                    "acl_mask: conn %lu  no more <who> clauses, returning %d (stop)\n",
1346                    op->o_connid, accessmask2str( *mask, accessmaskbuf) , 0 );
1347 #else
1348         Debug( LDAP_DEBUG_ACL,
1349                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1350                 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1351 #endif
1352         return ACL_STOP;
1353 }
1354
1355 /*
1356  * acl_check_modlist - check access control on the given entry to see if
1357  * it allows the given modifications by the user associated with op.
1358  * returns      1       if mods allowed ok
1359  *                      0       mods not allowed
1360  */
1361
1362 int
1363 acl_check_modlist(
1364         Operation       *op,
1365         Entry   *e,
1366         Modifications   *mlist
1367 )
1368 {
1369         struct berval *bv;
1370         AccessControlState state = ACL_STATE_INIT;
1371
1372         assert( op->o_bd != NULL );
1373
1374         /* short circuit root database access */
1375         if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
1376 #ifdef NEW_LOGGING
1377                 LDAP_LOG( ACL, DETAIL1, 
1378                            "acl_check_modlist: conn %lu  access granted to root user\n",
1379                            op->o_connid, 0, 0 );
1380 #else
1381                 Debug( LDAP_DEBUG_ACL,
1382                         "<= acl_access_allowed: granted to database root\n",
1383                     0, 0, 0 );
1384 #endif
1385                 return 1;
1386         }
1387
1388         /* use backend default access if no backend acls */
1389         if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1390 #ifdef NEW_LOGGING
1391                 LDAP_LOG( ACL, DETAIL1, 
1392                         "acl_check_modlist: backend default %s access %s to \"%s\"\n",
1393                         access2str( ACL_WRITE ),
1394                         op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", 
1395                         op->o_dn.bv_val  );
1396 #else
1397                 Debug( LDAP_DEBUG_ACL,
1398                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
1399                         access2str( ACL_WRITE ),
1400                         op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1401 #endif
1402                 return op->o_bd->be_dfltaccess >= ACL_WRITE;
1403
1404 #ifdef notdef
1405         /* op->o_bd is always non-NULL */
1406         /* use global default access if no global acls */
1407         } else if ( op->o_bd == NULL && global_acl == NULL ) {
1408 #ifdef NEW_LOGGING
1409                 LDAP_LOG( ACL, DETAIL1, 
1410                         "acl_check_modlist: global default %s access %s to \"%s\"\n",
1411                    access2str( ACL_WRITE ),
1412                    global_default_access >= ACL_WRITE ? "granted" : "denied", 
1413                    op->o_dn  );
1414 #else
1415                 Debug( LDAP_DEBUG_ACL,
1416                         "=> access_allowed: global default %s access %s to \"%s\"\n",
1417                         access2str( ACL_WRITE ),
1418                         global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1419 #endif
1420                 return global_default_access >= ACL_WRITE;
1421 #endif
1422         }
1423
1424         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1425                 /*
1426                  * no-user-modification operational attributes are ignored
1427                  * by ACL_WRITE checking as any found here are not provided
1428                  * by the user
1429                  */
1430                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1431 #ifdef NEW_LOGGING
1432                         LDAP_LOG( ACL, DETAIL1, 
1433                                    "acl_check_modlist: conn %lu  no-user-mod %s: modify access granted\n",
1434                                    op->o_connid, mlist->sml_desc->ad_cname.bv_val , 0 );
1435 #else
1436                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1437                                 " modify access granted\n",
1438                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1439 #endif
1440                         continue;
1441                 }
1442
1443                 switch ( mlist->sml_op ) {
1444                 case LDAP_MOD_REPLACE:
1445                         /*
1446                          * We must check both permission to delete the whole
1447                          * attribute and permission to add the specific attributes.
1448                          * This prevents abuse from selfwriters.
1449                          */
1450                         if ( ! access_allowed( op, e,
1451                                 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1452                         {
1453                                 return( 0 );
1454                         }
1455
1456                         if ( mlist->sml_bvalues == NULL ) break;
1457
1458                         /* fall thru to check value to add */
1459
1460                 case LDAP_MOD_ADD:
1461                         assert( mlist->sml_bvalues != NULL );
1462
1463                         for ( bv = mlist->sml_nvalues
1464                                         ? mlist->sml_nvalues : mlist->sml_values;
1465                                 bv->bv_val != NULL; bv++ )
1466                         {
1467                                 if ( ! access_allowed( op, e,
1468                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1469                                 {
1470                                         return( 0 );
1471                                 }
1472                         }
1473                         break;
1474
1475                 case LDAP_MOD_DELETE:
1476                         if ( mlist->sml_bvalues == NULL ) {
1477                                 if ( ! access_allowed( op, e,
1478                                         mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1479                                 {
1480                                         return( 0 );
1481                                 }
1482                                 break;
1483                         }
1484                         for ( bv = mlist->sml_nvalues
1485                                         ? mlist->sml_nvalues : mlist->sml_values;
1486                                 bv->bv_val != NULL; bv++ )
1487                         {
1488                                 if ( ! access_allowed( op, e,
1489                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1490                                 {
1491                                         return( 0 );
1492                                 }
1493                         }
1494                         break;
1495
1496                 case SLAP_MOD_SOFTADD:
1497                         /* allow adding attribute via modrdn thru */
1498                         break;
1499
1500                 default:
1501                         assert( 0 );
1502                         return( 0 );
1503                 }
1504         }
1505
1506         return( 1 );
1507 }
1508
1509 static int
1510 aci_get_part(
1511         struct berval *list,
1512         int ix,
1513         char sep,
1514         struct berval *bv )
1515 {
1516         int len;
1517         char *p;
1518
1519         if (bv) {
1520                 bv->bv_len = 0;
1521                 bv->bv_val = NULL;
1522         }
1523         len = list->bv_len;
1524         p = list->bv_val;
1525         while (len >= 0 && --ix >= 0) {
1526                 while (--len >= 0 && *p++ != sep) ;
1527         }
1528         while (len >= 0 && *p == ' ') {
1529                 len--;
1530                 p++;
1531         }
1532         if (len < 0)
1533                 return(-1);
1534
1535         if (!bv)
1536                 return(0);
1537
1538         bv->bv_val = p;
1539         while (--len >= 0 && *p != sep) {
1540                 bv->bv_len++;
1541                 p++;
1542         }
1543         while (bv->bv_len > 0 && *--p == ' ')
1544                 bv->bv_len--;
1545         return(bv->bv_len);
1546 }
1547
1548 BerVarray
1549 aci_set_gather (SetCookie *cookie, struct berval *name, struct berval *attr)
1550 {
1551         AciSetCookie *cp = (AciSetCookie *)cookie;
1552         BerVarray bvals = NULL;
1553         struct berval ndn;
1554
1555         /* this routine needs to return the bervals instead of
1556          * plain strings, since syntax is not known.  It should
1557          * also return the syntax or some "comparison cookie".
1558          */
1559
1560         if (dnNormalize(0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx) == LDAP_SUCCESS) {
1561                 const char *text;
1562                 AttributeDescription *desc = NULL;
1563                 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1564                         backend_attribute(cp->op,
1565                                 cp->e, &ndn, desc, &bvals);
1566                 }
1567                 sl_free(ndn.bv_val, cp->op->o_tmpmemctx);
1568         }
1569         return(bvals);
1570 }
1571
1572 static int
1573 aci_match_set (
1574         struct berval *subj,
1575         Operation *op,
1576         Entry *e,
1577         int setref
1578 )
1579 {
1580         struct berval set = { 0, NULL };
1581         int rc = 0;
1582         AciSetCookie cookie;
1583
1584         if (setref == 0) {
1585                 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
1586         } else {
1587                 struct berval subjdn, ndn = { 0, NULL };
1588                 struct berval setat;
1589                 BerVarray bvals;
1590                 const char *text;
1591                 AttributeDescription *desc = NULL;
1592
1593                 /* format of string is "entry/setAttrName" */
1594                 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1595                         return(0);
1596                 }
1597
1598                 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1599                         setat.bv_val = SLAPD_ACI_SET_ATTR;
1600                         setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1601                 }
1602
1603                 if ( setat.bv_val != NULL ) {
1604                         /*
1605                          * NOTE: dnNormalize honors the ber_len field
1606                          * as the length of the dn to be normalized
1607                          */
1608                         if ( dnNormalize(0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS
1609                                 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1610                         {
1611                                 backend_attribute(op, e,
1612                                         &ndn, desc, &bvals);
1613                                 if ( bvals != NULL ) {
1614                                         if ( bvals[0].bv_val != NULL ) {
1615                                                 int i;
1616                                                 set = bvals[0];
1617                                                 bvals[0].bv_val = NULL;
1618                                                 for (i=1;bvals[i].bv_val;i++);
1619                                                 bvals[0].bv_val = bvals[i-1].bv_val;
1620                                                 bvals[i-1].bv_val = NULL;
1621                                         }
1622                                         ber_bvarray_free_x(bvals, op->o_tmpmemctx);
1623                                 }
1624                         }
1625                         if (ndn.bv_val)
1626                                 free(ndn.bv_val);
1627                 }
1628         }
1629
1630         if (set.bv_val != NULL) {
1631                 cookie.op = op;
1632                 cookie.e = e;
1633                 rc = (slap_set_filter(aci_set_gather, (SetCookie *)&cookie, &set,
1634                         &op->o_ndn, &e->e_nname, NULL) > 0);
1635                 sl_free(set.bv_val, op->o_tmpmemctx);
1636         }
1637         return(rc);
1638 }
1639
1640 #ifdef SLAPD_ACI_ENABLED
1641 static int
1642 aci_list_map_rights(
1643         struct berval *list )
1644 {
1645         struct berval bv;
1646         slap_access_t mask;
1647         int i;
1648
1649         ACL_INIT(mask);
1650         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1651                 if (bv.bv_len <= 0)
1652                         continue;
1653                 switch (*bv.bv_val) {
1654                 case 'c':
1655                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1656                         break;
1657                 case 's':
1658                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1659                          * the right 's' to mean "set", but in the examples states
1660                          * that the right 's' means "search".  The latter definition
1661                          * is used here.
1662                          */
1663                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1664                         break;
1665                 case 'r':
1666                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
1667                         break;
1668                 case 'w':
1669                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1670                         break;
1671                 case 'x':
1672                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
1673                          * define any equivalent to the AUTH right, so I've just used
1674                          * 'x' for now.
1675                          */
1676                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1677                         break;
1678                 default:
1679                         break;
1680                 }
1681
1682         }
1683         return(mask);
1684 }
1685
1686 static int
1687 aci_list_has_attr(
1688         struct berval *list,
1689         const struct berval *attr,
1690         struct berval *val )
1691 {
1692         struct berval bv, left, right;
1693         int i;
1694
1695         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1696                 if (aci_get_part(&bv, 0, '=', &left) < 0
1697                         || aci_get_part(&bv, 1, '=', &right) < 0)
1698                 {
1699                         if (ber_bvstrcasecmp(attr, &bv) == 0)
1700                                 return(1);
1701                 } else if (val == NULL) {
1702                         if (ber_bvstrcasecmp(attr, &left) == 0)
1703                                 return(1);
1704                 } else {
1705                         if (ber_bvstrcasecmp(attr, &left) == 0) {
1706                                 /* this is experimental code that implements a
1707                                  * simple (prefix) match of the attribute value.
1708                                  * the ACI draft does not provide for aci's that
1709                                  * apply to specific values, but it would be
1710                                  * nice to have.  If the <attr> part of an aci's
1711                                  * rights list is of the form <attr>=<value>,
1712                                  * that means the aci applies only to attrs with
1713                                  * the given value.  Furthermore, if the attr is
1714                                  * of the form <attr>=<value>*, then <value> is
1715                                  * treated as a prefix, and the aci applies to 
1716                                  * any value with that prefix.
1717                                  *
1718                                  * Ideally, this would allow r.e. matches.
1719                                  */
1720                                 if (aci_get_part(&right, 0, '*', &left) < 0
1721                                         || right.bv_len <= left.bv_len)
1722                                 {
1723                                         if (ber_bvstrcasecmp(val, &right) == 0)
1724                                                 return(1);
1725                                 } else if (val->bv_len >= left.bv_len) {
1726                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1727                                                 return(1);
1728                                 }
1729                         }
1730                 }
1731         }
1732         return(0);
1733 }
1734
1735 static slap_access_t
1736 aci_list_get_attr_rights(
1737         struct berval *list,
1738         const struct berval *attr,
1739         struct berval *val )
1740 {
1741     struct berval bv;
1742     slap_access_t mask;
1743     int i;
1744
1745         /* loop through each rights/attr pair, skip first part (action) */
1746         ACL_INIT(mask);
1747         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1748                 if (aci_list_has_attr(&bv, attr, val) == 0)
1749                         continue;
1750                 if (aci_get_part(list, i, ';', &bv) < 0)
1751                         continue;
1752                 mask |= aci_list_map_rights(&bv);
1753         }
1754         return(mask);
1755 }
1756
1757 static int
1758 aci_list_get_rights(
1759         struct berval *list,
1760         const struct berval *attr,
1761         struct berval *val,
1762         slap_access_t *grant,
1763         slap_access_t *deny )
1764 {
1765     struct berval perm, actn;
1766     slap_access_t *mask;
1767     int i, found;
1768
1769         if (attr == NULL || attr->bv_len == 0 
1770                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1771                 attr = &aci_bv_br_entry;
1772         }
1773
1774         found = 0;
1775         ACL_INIT(*grant);
1776         ACL_INIT(*deny);
1777         /* loop through each permissions clause */
1778         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1779                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1780                         continue;
1781                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1782                         mask = grant;
1783                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1784                         mask = deny;
1785                 } else {
1786                         continue;
1787                 }
1788
1789                 found = 1;
1790                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1791                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1792         }
1793         return(found);
1794 }
1795
1796 static int
1797 aci_group_member (
1798         struct berval *subj,
1799         struct berval *defgrpoc,
1800         struct berval *defgrpat,
1801         Operation               *op,
1802         Entry           *e,
1803         regmatch_t      *matches
1804 )
1805 {
1806         struct berval subjdn;
1807         struct berval grpoc;
1808         struct berval grpat;
1809         ObjectClass *grp_oc = NULL;
1810         AttributeDescription *grp_ad = NULL;
1811         const char *text;
1812         int rc;
1813
1814         /* format of string is "group/objectClassValue/groupAttrName" */
1815         if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1816                 return(0);
1817         }
1818
1819         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1820                 grpoc = *defgrpoc;
1821         }
1822
1823         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1824                 grpat = *defgrpat;
1825         }
1826
1827         rc = slap_bv2ad( &grpat, &grp_ad, &text );
1828         if( rc != LDAP_SUCCESS ) {
1829                 rc = 0;
1830                 goto done;
1831         }
1832         rc = 0;
1833
1834         grp_oc = oc_bvfind( &grpoc );
1835
1836         if (grp_oc != NULL && grp_ad != NULL ) {
1837                 char buf[ACL_BUF_SIZE];
1838                 struct berval bv, ndn;
1839                 bv.bv_len = sizeof( buf ) - 1;
1840                 bv.bv_val = (char *)&buf;
1841                 string_expand(&bv, &subjdn, e->e_ndn, matches);
1842                 if ( dnNormalize(0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
1843                         rc = (backend_group(op, e, &ndn, &op->o_ndn,
1844                                 grp_oc, grp_ad) == 0);
1845                         free( ndn.bv_val );
1846                 }
1847         }
1848
1849 done:
1850         return(rc);
1851 }
1852
1853 static int
1854 aci_mask(
1855     Operation           *op,
1856     Entry                       *e,
1857         AttributeDescription *desc,
1858     struct berval       *val,
1859     struct berval       *aci,
1860         regmatch_t              *matches,
1861         slap_access_t   *grant,
1862         slap_access_t   *deny,
1863         struct berval   *scope
1864 )
1865 {
1866     struct berval bv, perms, sdn;
1867         int rc;
1868                 
1869
1870         assert( desc->ad_cname.bv_val != NULL );
1871
1872         /* parse an aci of the form:
1873                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1874
1875            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1876            a full description of the format for this attribute.
1877            Differences: "this" in the draft is "self" here, and
1878            "self" and "public" is in the position of dnType.
1879
1880            For now, this routine only supports scope=entry.
1881          */
1882         /* check that the aci has all 5 components */
1883         if (aci_get_part(aci, 4, '#', NULL) < 0)
1884                 return(0);
1885
1886         /* check that the aci family is supported */
1887         if (aci_get_part(aci, 0, '#', &bv) < 0)
1888                 return(0);
1889
1890         /* check that the scope matches */
1891         if (aci_get_part(aci, 1, '#', &bv) < 0
1892                 || ber_bvstrcasecmp( scope, &bv ) != 0)
1893         {
1894                 return(0);
1895         }
1896
1897         /* get the list of permissions clauses, bail if empty */
1898         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1899                 return(0);
1900
1901         /* check if any permissions allow desired access */
1902         if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
1903                 return(0);
1904
1905         /* see if we have a DN match */
1906         if (aci_get_part(aci, 3, '#', &bv) < 0)
1907                 return(0);
1908
1909         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1910                 return(0);
1911
1912         if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
1913                 struct berval ndn;
1914                 rc = 0;
1915                 if ( dnNormalize(0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
1916                         if (dn_match( &op->o_ndn, &ndn))
1917                                 rc = 1;
1918                         free(ndn.bv_val);
1919                 }
1920                 return (rc);
1921
1922         } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
1923                 return(1);
1924
1925         } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
1926                 if (dn_match(&op->o_ndn, &e->e_nname))
1927                         return(1);
1928
1929         } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
1930                 Attribute *at;
1931                 AttributeDescription *ad = NULL;
1932                 const char *text;
1933
1934                 rc = slap_bv2ad( &sdn, &ad, &text );
1935
1936                 if( rc != LDAP_SUCCESS ) {
1937                         return 0;
1938                 }
1939
1940                 rc = 0;
1941
1942                 bv = op->o_ndn;
1943
1944                 for(at = attrs_find( e->e_attrs, ad );
1945                         at != NULL;
1946                         at = attrs_find( at->a_next, ad ) )
1947                 {
1948                         if (value_find_ex( ad,
1949                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1950                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1951                                 at->a_nvals,
1952                                 &bv, op->o_tmpmemctx) == 0 )
1953                         {
1954                                 rc = 1;
1955                                 break;
1956                         }
1957                 }
1958
1959                 return rc;
1960
1961
1962         } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
1963                 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, op, e, matches))
1964                         return(1);
1965
1966         } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
1967                 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, op, e, matches))
1968                         return(1);
1969
1970         } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
1971                 if (aci_match_set(&sdn, op, e, 0))
1972                         return(1);
1973
1974         } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
1975                 if (aci_match_set(&sdn, op, e, 1))
1976                         return(1);
1977
1978         }
1979
1980         return(0);
1981 }
1982
1983 #endif  /* SLAPD_ACI_ENABLED */
1984
1985 static void
1986 string_expand(
1987         struct berval *bv,
1988         struct berval *pat,
1989         char *match,
1990         regmatch_t *matches)
1991 {
1992         ber_len_t       size;
1993         char   *sp;
1994         char   *dp;
1995         int     flag;
1996
1997         size = 0;
1998         bv->bv_val[0] = '\0';
1999         bv->bv_len--; /* leave space for lone $ */
2000
2001         flag = 0;
2002         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2003                 sp < pat->bv_val + pat->bv_len ; sp++ )
2004         {
2005                 /* did we previously see a $ */
2006                 if ( flag ) {
2007                         if ( flag == 1 && *sp == '$' ) {
2008                                 *dp++ = '$';
2009                                 size++;
2010                                 flag = 0;
2011
2012                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2013                                 flag = 2;
2014
2015                         } else if ( *sp >= '0' && *sp <= '9' ) {
2016                                 int     n;
2017                                 int     i;
2018                                 int     l;
2019
2020                                 n = *sp - '0';
2021
2022                                 if ( flag == 2 ) {
2023                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2024                                                 if ( *sp >= '0' && *sp <= '9' ) {
2025                                                         n = 10*n + ( *sp - '0' );
2026                                                 }
2027                                         }
2028
2029                                         if ( *sp != /*'{'*/ '}' ) {
2030                                                 /* error */
2031                                         }
2032                                 }
2033
2034                                 if ( n >= MAXREMATCHES ) {
2035                                 
2036                                 }
2037                                 
2038                                 *dp = '\0';
2039                                 i = matches[n].rm_so;
2040                                 l = matches[n].rm_eo; 
2041                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2042                                         *dp++ = match[i];
2043                                 }
2044                                 *dp = '\0';
2045
2046                                 flag = 0;
2047                         }
2048                 } else {
2049                         if (*sp == '$') {
2050                                 flag = 1;
2051                         } else {
2052                                 *dp++ = *sp;
2053                                 size++;
2054                         }
2055                 }
2056         }
2057
2058         if ( flag ) {
2059                 /* must have ended with a single $ */
2060                 *dp++ = '$';
2061                 size++;
2062         }
2063
2064         *dp = '\0';
2065         bv->bv_len = size;
2066
2067 #ifdef NEW_LOGGING
2068         LDAP_LOG( ACL, DETAIL1, 
2069            "string_expand:  pattern = %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2070         LDAP_LOG( ACL, DETAIL1, "string_expand:  expanded = %s\n", bv->bv_val, 0, 0 );
2071 #else
2072         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2073         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2074 #endif
2075 }
2076
2077 static int
2078 regex_matches(
2079         struct berval *pat,                     /* pattern to expand and match against */
2080         char *str,                              /* string to match against pattern */
2081         char *buf,                              /* buffer with $N expansion variables */
2082         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
2083 )
2084 {
2085         regex_t re;
2086         char newbuf[ACL_BUF_SIZE];
2087         struct berval bv;
2088         int     rc;
2089
2090         bv.bv_len = sizeof(newbuf) - 1;
2091         bv.bv_val = newbuf;
2092
2093         if(str == NULL) str = "";
2094
2095         string_expand(&bv, pat, buf, matches);
2096         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
2097                 char error[ACL_BUF_SIZE];
2098                 regerror(rc, &re, error, sizeof(error));
2099
2100 #ifdef NEW_LOGGING
2101                 LDAP_LOG( ACL, ERR, 
2102                            "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
2103                            pat->bv_val, str, error  );
2104 #else
2105                 Debug( LDAP_DEBUG_TRACE,
2106                     "compile( \"%s\", \"%s\") failed %s\n",
2107                         pat->bv_val, str, error );
2108 #endif
2109                 return( 0 );
2110         }
2111
2112         rc = regexec(&re, str, 0, NULL, 0);
2113         regfree( &re );
2114
2115 #ifdef NEW_LOGGING
2116         LDAP_LOG( ACL, DETAIL2, "regex_matches: string:   %s\n", str, 0, 0 );
2117         LDAP_LOG( ACL, DETAIL2, "regex_matches: rc:     %d  %s\n",
2118                    rc, rc ? "matches" : "no matches", 0  );
2119 #else
2120         Debug( LDAP_DEBUG_TRACE,
2121             "=> regex_matches: string:   %s\n", str, 0, 0 );
2122         Debug( LDAP_DEBUG_TRACE,
2123             "=> regex_matches: rc: %d %s\n",
2124                 rc, !rc ? "matches" : "no matches", 0 );
2125 #endif
2126         return( !rc );
2127 }
2128