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