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