]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
Sync with HEAD
[openldap] / servers / slapd / acl.c
1 /* acl.c - routines to parse and check acl's */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2003 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/regex.h>
32 #include <ac/socket.h>
33 #include <ac/string.h>
34
35 #include "slap.h"
36 #include "sets.h"
37 #include "lber_pvt.h"
38
39 #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                         if (value_match( &match, desc,
672                                 desc->ad_type->sat_equality, 0,
673                                 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
674                                         match )
675                                 return ACL_BREAK;
676                 }
677         }
678
679         if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
680                 && state->as_vd_acl == a )
681         {
682                 b = state->as_vd_access;
683                 i = state->as_vd_access_count;
684
685         } else {
686                 b = a->acl_access;
687                 i = 1;
688         }
689
690         for ( ; b != NULL; b = b->a_next, i++ ) {
691                 slap_mask_t oldmask, modmask;
692
693                 ACL_INVALIDATE( modmask );
694
695                 /* AND <who> clauses */
696                 if ( b->a_dn_pat.bv_len != 0 ) {
697 #ifdef NEW_LOGGING
698                         LDAP_LOG( ACL, DETAIL1, 
699                                 "acl_mask: conn %lu  check a_dn_pat: %s\n",
700                                 op->o_connid, b->a_dn_pat.bv_val ,0 );
701 #else
702                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
703                                 b->a_dn_pat.bv_val, 0, 0);
704 #endif
705                         /*
706                          * if access applies to the entry itself, and the
707                          * user is bound as somebody in the same namespace as
708                          * the entry, OR the given dn matches the dn pattern
709                          */
710                         if ( bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) ) {
711                                 if ( op->o_ndn.bv_len != 0 ) {
712                                         continue;
713                                 }
714
715                         } else if ( bvmatch( &b->a_dn_pat, &aci_bv_users ) ) {
716                                 if ( op->o_ndn.bv_len == 0 ) {
717                                         continue;
718                                 }
719
720                         } else if ( bvmatch( &b->a_dn_pat, &aci_bv_self ) ) {
721                                 if ( op->o_ndn.bv_len == 0 ) {
722                                         continue;
723                                 }
724                                 
725                                 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
726                                         continue;
727                                 }
728
729                         } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
730                                 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
731                                         int ret = regex_matches( &b->a_dn_pat,
732                                                 op->o_ndn.bv_val, e->e_ndn, matches );
733
734                                         if( ret == 0 ) {
735                                                 continue;
736                                         }
737                                 }
738
739                         } else {
740                                 struct berval pat;
741                                 int got_match = 0;
742
743                                 if ( e->e_dn == NULL )
744                                         continue;
745
746                                 if ( b->a_dn_expand ) {
747                                         struct berval bv;
748                                         char buf[ACL_BUF_SIZE];
749
750                                         bv.bv_len = sizeof( buf ) - 1;
751                                         bv.bv_val = buf;
752
753                                         string_expand(&bv, &b->a_dn_pat, 
754                                                         e->e_ndn, matches);
755                                         if ( dnNormalize(0, NULL, NULL, &bv, &pat, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
756                                                 /* did not expand to a valid dn */
757                                                 continue;
758                                         }
759                                 } else {
760                                         pat = b->a_dn_pat;
761                                 }
762
763                                 patlen = pat.bv_len;
764                                 odnlen = op->o_ndn.bv_len;
765                                 if ( odnlen < patlen ) {
766                                         goto dn_match_cleanup;
767
768                                 }
769
770                                 if ( b->a_dn_style == ACL_STYLE_BASE ) {
771                                         /* base dn -- entire object DN must match */
772                                         if ( odnlen != patlen ) {
773                                                 goto dn_match_cleanup;
774                                         }
775
776                                 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
777                                         int rdnlen = -1;
778
779                                         if ( odnlen <= patlen ) {
780                                                 goto dn_match_cleanup;
781                                         }
782
783                                         if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
784                                                 goto dn_match_cleanup;
785                                         }
786
787                                         rdnlen = dn_rdnlen( NULL, &op->o_ndn );
788                                         if ( rdnlen != odnlen - patlen - 1 ) {
789                                                 goto dn_match_cleanup;
790                                         }
791
792                                 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
793                                         if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
794                                                 goto dn_match_cleanup;
795                                         }
796
797                                 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
798                                         if ( odnlen <= patlen ) {
799                                                 goto dn_match_cleanup;
800                                         }
801
802                                         if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
803                                                 goto dn_match_cleanup;
804                                         }
805                                 }
806
807                                 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
808
809 dn_match_cleanup:;
810                                 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
811                                         free( pat.bv_val );
812                                 }
813
814                                 if ( !got_match ) {
815                                         continue;
816                                 }
817                         }
818                 }
819
820                 if ( b->a_sockurl_pat.bv_len ) {
821                         if ( ! op->o_conn->c_listener ) {
822                                 continue;
823                         }
824 #ifdef NEW_LOGGING
825                         LDAP_LOG( ACL, DETAIL1, 
826                                    "acl_mask: conn %lu  check a_sockurl_pat: %s\n",
827                                    op->o_connid, b->a_sockurl_pat.bv_val , 0 );
828 #else
829                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
830                                 b->a_sockurl_pat.bv_val, 0, 0 );
831 #endif
832
833                         if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
834                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
835                                         if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
836                                                         e->e_ndn, matches ) ) 
837                                         {
838                                                 continue;
839                                         }
840                                 } else {
841                                         if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
842                                                 continue;
843                                 }
844                         }
845                 }
846
847                 if ( b->a_domain_pat.bv_len ) {
848                         if ( !op->o_conn->c_peer_domain.bv_val ) {
849                                 continue;
850                         }
851 #ifdef NEW_LOGGING
852                         LDAP_LOG( ACL, DETAIL1, 
853                                    "acl_mask: conn %lu  check a_domain_pat: %s\n",
854                                    op->o_connid, b->a_domain_pat.bv_val , 0 );
855 #else
856                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
857                                 b->a_domain_pat.bv_val, 0, 0 );
858 #endif
859                         if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
860                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
861                                         if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
862                                                         e->e_ndn, matches ) ) 
863                                         {
864                                                 continue;
865                                         }
866                                 } else {
867                                         char buf[ACL_BUF_SIZE];
868
869                                         struct berval   cmp = op->o_conn->c_peer_domain;
870                                         struct berval   pat = b->a_domain_pat;
871
872                                         if ( b->a_domain_expand ) {
873                                                 struct berval bv;
874
875                                                 bv.bv_len = sizeof(buf) - 1;
876                                                 bv.bv_val = buf;
877
878                                                 string_expand(&bv, &b->a_domain_pat, e->e_ndn, matches);
879                                                 pat = bv;
880                                         }
881
882                                         if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
883                                                 int offset = cmp.bv_len - pat.bv_len;
884                                                 if ( offset < 0 ) {
885                                                         continue;
886                                                 }
887
888                                                 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
889                                                         continue;
890                                                 }
891
892                                                 /* trim the domain */
893                                                 cmp.bv_val = &cmp.bv_val[ offset ];
894                                                 cmp.bv_len -= offset;
895                                         }
896                                         
897                                         if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
898                                                 continue;
899                                         }
900                                 }
901                         }
902                 }
903
904                 if ( b->a_peername_pat.bv_len ) {
905                         if ( !op->o_conn->c_peer_name.bv_val ) {
906                                 continue;
907                         }
908 #ifdef NEW_LOGGING
909                         LDAP_LOG( ACL, DETAIL1, 
910                                    "acl_mask: conn %lu  check a_peername_path: %s\n",
911                                    op->o_connid, b->a_peername_pat.bv_val , 0 );
912 #else
913                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
914                                 b->a_peername_pat.bv_val, 0, 0 );
915 #endif
916                         if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
917                                 if ( b->a_peername_style == ACL_STYLE_REGEX) {
918                                         if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
919                                                         e->e_ndn, matches ) ) 
920                                         {
921                                                 continue;
922                                         }
923                                 } else {
924                                         if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 )
925                                                 continue;
926                                 }
927                         }
928                 }
929
930                 if ( b->a_sockname_pat.bv_len ) {
931                         if ( !op->o_conn->c_sock_name.bv_val ) {
932                                 continue;
933                         }
934 #ifdef NEW_LOGGING
935                         LDAP_LOG( ACL, DETAIL1, 
936                                    "acl_mask: conn %lu  check a_sockname_path: %s\n",
937                                    op->o_connid, b->a_sockname_pat.bv_val , 0 );
938 #else
939                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
940                                 b->a_sockname_pat.bv_val, 0, 0 );
941 #endif
942                         if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
943                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
944                                         if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
945                                                         e->e_ndn, matches ) ) 
946                                         {
947                                                 continue;
948                                         }
949                                 } else {
950                                         if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 )
951                                                 continue;
952                                 }
953                         }
954                 }
955
956                 if ( b->a_dn_at != NULL ) {
957                         Attribute       *at;
958                         struct berval   bv;
959                         int rc, match = 0;
960                         const char *text;
961                         const char *attr = b->a_dn_at->ad_cname.bv_val;
962
963                         assert( attr != NULL );
964
965                         if ( op->o_ndn.bv_len == 0 ) {
966                                 continue;
967                         }
968
969 #ifdef NEW_LOGGING
970                         LDAP_LOG( ACL, DETAIL1, 
971                                    "acl_mask: conn %lu  check a_dn_pat: %s\n",
972                                    op->o_connid, attr , 0 );
973 #else
974                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
975                                 attr, 0, 0);
976 #endif
977                         bv = op->o_ndn;
978
979                         /* see if asker is listed in dnattr */
980                         for( at = attrs_find( e->e_attrs, b->a_dn_at );
981                                 at != NULL;
982                                 at = attrs_find( at->a_next, b->a_dn_at ) )
983                         {
984                                 if( value_find_ex( b->a_dn_at,
985                                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
986                                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
987                                         at->a_nvals,
988                                         &bv, op->o_tmpmemctx ) == 0 )
989                                 {
990                                         /* found it */
991                                         match = 1;
992                                         break;
993                                 }
994                         }
995
996                         if( match ) {
997                                 /* have a dnattr match. if this is a self clause then
998                                  * the target must also match the op dn.
999                                  */
1000                                 if ( b->a_dn_self ) {
1001                                         /* check if the target is an attribute. */
1002                                         if ( val == NULL ) continue;
1003
1004                                         /* target is attribute, check if the attribute value
1005                                          * is the op dn.
1006                                          */
1007                                         rc = value_match( &match, b->a_dn_at,
1008                                                 b->a_dn_at->ad_type->sat_equality, 0,
1009                                                 val, &bv, &text );
1010                                         /* on match error or no match, fail the ACL clause */
1011                                         if (rc != LDAP_SUCCESS || match != 0 )
1012                                                 continue;
1013                                 }
1014                         } else {
1015                                 /* no dnattr match, check if this is a self clause */
1016                                 if ( ! b->a_dn_self )
1017                                         continue;
1018
1019                                 ACL_RECORD_VALUE_STATE;
1020                                 
1021                                 /* this is a self clause, check if the target is an
1022                                  * attribute.
1023                                  */
1024                                 if ( val == NULL )
1025                                         continue;
1026
1027                                 /* target is attribute, check if the attribute value
1028                                  * is the op dn.
1029                                  */
1030                                 rc = value_match( &match, b->a_dn_at,
1031                                         b->a_dn_at->ad_type->sat_equality, 0,
1032                                         val, &bv, &text );
1033
1034                                 /* on match error or no match, fail the ACL clause */
1035                                 if (rc != LDAP_SUCCESS || match != 0 )
1036                                         continue;
1037                         }
1038                 }
1039
1040                 if ( b->a_group_pat.bv_len ) {
1041                         struct berval bv;
1042                         struct berval ndn = { 0, NULL };
1043                         int rc;
1044
1045                         if ( op->o_ndn.bv_len == 0 ) {
1046                                 continue;
1047                         }
1048
1049                         /* b->a_group is an unexpanded entry name, expanded it should be an 
1050                          * entry with objectclass group* and we test to see if odn is one of
1051                          * the values in the attribute group
1052                          */
1053                         /* see if asker is listed in dnattr */
1054                         if ( b->a_group_style == ACL_STYLE_REGEX ) {
1055                                 char buf[ACL_BUF_SIZE];
1056                                 bv.bv_len = sizeof(buf) - 1;
1057                                 bv.bv_val = buf; 
1058
1059                                 string_expand( &bv, &b->a_group_pat, e->e_ndn, matches );
1060                                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
1061                                         /* did not expand to a valid dn */
1062                                         continue;
1063                                 }
1064
1065                                 bv = ndn;
1066
1067                         } else {
1068                                 bv = b->a_group_pat;
1069                         }
1070
1071                         rc = backend_group( op, e, &bv, &op->o_ndn,
1072                                 b->a_group_oc, b->a_group_at );
1073
1074                         if ( ndn.bv_val ) free( ndn.bv_val );
1075
1076                         if ( rc != 0 ) {
1077                                 continue;
1078                         }
1079                 }
1080
1081                 if ( b->a_set_pat.bv_len != 0 ) {
1082                         struct berval bv;
1083                         char buf[ACL_BUF_SIZE];
1084                         if( b->a_set_style == ACL_STYLE_REGEX ){
1085                                 bv.bv_len = sizeof(buf) - 1;
1086                                 bv.bv_val = buf;
1087                                 string_expand( &bv, &b->a_set_pat, e->e_ndn, matches );
1088                         }else{
1089                                 bv = b->a_set_pat;
1090                         }
1091                         if (aci_match_set( &bv, op, e, 0 ) == 0) {
1092                                 continue;
1093                         }
1094                 }
1095
1096                 if ( b->a_authz.sai_ssf ) {
1097 #ifdef NEW_LOGGING
1098                         LDAP_LOG( ACL, DETAIL1, 
1099                                 "acl_mask: conn %lu  check a_authz.sai_ssf: ACL %u > OP %u\n",
1100                                 op->o_connid, b->a_authz.sai_ssf, op->o_ssf  );
1101 #else
1102                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1103                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
1104 #endif
1105                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
1106                                 continue;
1107                         }
1108                 }
1109
1110                 if ( b->a_authz.sai_transport_ssf ) {
1111 #ifdef NEW_LOGGING
1112                         LDAP_LOG( ACL, DETAIL1, 
1113                                 "acl_mask: conn %lu  check a_authz.sai_transport_ssf: "
1114                                 "ACL %u > OP %u\n",
1115                                 op->o_connid, b->a_authz.sai_transport_ssf, 
1116                                 op->o_transport_ssf  );
1117 #else
1118                         Debug( LDAP_DEBUG_ACL,
1119                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1120                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1121 #endif
1122                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
1123                                 continue;
1124                         }
1125                 }
1126
1127                 if ( b->a_authz.sai_tls_ssf ) {
1128 #ifdef NEW_LOGGING
1129                         LDAP_LOG( ACL, DETAIL1, 
1130                                 "acl_mask: conn %lu  check a_authz.sai_tls_ssf: ACL %u > "
1131                                 "OP %u\n",
1132                                 op->o_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf  );
1133 #else
1134                         Debug( LDAP_DEBUG_ACL,
1135                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1136                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1137 #endif
1138                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
1139                                 continue;
1140                         }
1141                 }
1142
1143                 if ( b->a_authz.sai_sasl_ssf ) {
1144 #ifdef NEW_LOGGING
1145                         LDAP_LOG( ACL, DETAIL1, 
1146                            "acl_mask: conn %lu check a_authz.sai_sasl_ssf: " 
1147                            "ACL %u > OP %u\n",
1148                                 op->o_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf );
1149 #else
1150                         Debug( LDAP_DEBUG_ACL,
1151                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1152                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1153 #endif
1154                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
1155                                 continue;
1156                         }
1157                 }
1158
1159 #ifdef SLAPD_ACI_ENABLED
1160                 if ( b->a_aci_at != NULL ) {
1161                         Attribute       *at;
1162                         slap_access_t grant, deny, tgrant, tdeny;
1163                         struct berval parent_ndn, old_parent_ndn;
1164                         BerVarray bvals = NULL;
1165                         int ret,stop;
1166
1167                         /* this case works different from the others above.
1168                          * since aci's themselves give permissions, we need
1169                          * to first check b->a_access_mask, the ACL's access level.
1170                          */
1171
1172                         if ( e->e_nname.bv_len == 0 ) {
1173                                 /* no ACIs in the root DSE */
1174                                 continue;
1175                         }
1176
1177                         /* first check if the right being requested
1178                          * is allowed by the ACL clause.
1179                          */
1180                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1181                                 continue;
1182                         }
1183                         /* start out with nothing granted, nothing denied */
1184                         ACL_INIT(tgrant);
1185                         ACL_INIT(tdeny);
1186
1187                         /* get the aci attribute */
1188                         at = attr_find( e->e_attrs, b->a_aci_at );
1189                         if ( at != NULL ) {
1190                                 ACL_RECORD_VALUE_STATE;
1191                                 /* the aci is an multi-valued attribute.  The
1192                                 * rights are determined by OR'ing the individual
1193                                 * rights given by the acis.
1194                                 */
1195                                 for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
1196                                         if (aci_mask( op,
1197                                                 e, desc, val,
1198                                                 &at->a_nvals[i],
1199                                                 matches, &grant, &deny,  &aci_bv_entry ) != 0)
1200                                         {
1201                                                 tgrant |= grant;
1202                                                 tdeny |= deny;
1203                                         }
1204                                 }
1205                                 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1206                                           accessmask2str(tgrant,accessmaskbuf), 
1207                                           accessmask2str(tdeny, accessmaskbuf1), 0);
1208
1209                         }
1210                         /* If the entry level aci didn't contain anything valid for the 
1211                          * current operation, climb up the tree and evaluate the
1212                          * acis with scope set to subtree
1213                          */
1214                         if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1215                                 dnParent(&(e->e_nname), &parent_ndn);
1216                                 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1217                                         old_parent_ndn = parent_ndn;
1218                                         Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1219                                         ret=backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals);
1220                                         switch(ret){
1221                                                 case LDAP_SUCCESS :
1222                                                         if(bvals){
1223                                                                 for( i = 0; bvals[i].bv_val != NULL; i++){
1224                                                                         ACL_RECORD_VALUE_STATE;
1225                                                                         if (aci_mask(op, e, desc, val, &bvals[i], matches,
1226                                                                                         &grant, &deny, &aci_bv_children) != 0) {
1227                                                                                 tgrant |= grant;
1228                                                                                 tdeny |= deny;
1229                                                                                 /* evaluation stops as soon as either a "deny" or a 
1230                                                                                  * "grant" directive matches.
1231                                                                                  */
1232                                                                                 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1233                                                                                         stop=1;
1234                                                                                 }
1235                                                                         }
1236                                                                         Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n", 
1237                                                                                 accessmask2str(tgrant,accessmaskbuf),
1238                                                                                 accessmask2str(tdeny, accessmaskbuf1), 0);
1239                                                                 }
1240                                                         }
1241                                                         stop=0;
1242                                                         break;
1243                                                 case LDAP_NO_SUCH_ATTRIBUTE:
1244                                                         /* just go on if the aci-Attribute is not present in
1245                                                          * the current entry 
1246                                                          */
1247                                                         Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1248                                                         stop=0;
1249                                                         break;
1250                                                 case LDAP_NO_SUCH_OBJECT:
1251                                                         /* We have reached the base object */
1252                                                         Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1253                                                         stop=1;
1254                                                         break;
1255                                                 default:
1256                                                         stop=1;
1257                                                         break;
1258                                         }
1259                                         if(stop){
1260                                                 break;
1261                                         }
1262                                         dnParent(&old_parent_ndn, &parent_ndn);
1263                                 }
1264                         }
1265
1266
1267                         /* remove anything that the ACL clause does not allow */
1268                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1269                         tdeny &= ACL_PRIV_MASK;
1270
1271                         /* see if we have anything to contribute */
1272                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
1273                                 continue;
1274                         }
1275
1276                         /* this could be improved by changing acl_mask so that it can deal with
1277                          * by clauses that return grant/deny pairs.  Right now, it does either
1278                          * additive or subtractive rights, but not both at the same time.  So,
1279                          * we need to combine the grant/deny pair into a single rights mask in
1280                          * a smart way:  if either grant or deny is "empty", then we use the
1281                          * opposite as is, otherwise we remove any denied rights from the grant
1282                          * rights mask and construct an additive mask.
1283                          */
1284                         if (ACL_IS_INVALID(tdeny)) {
1285                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
1286
1287                         } else if (ACL_IS_INVALID(tgrant)) {
1288                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1289
1290                         } else {
1291                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1292                         }
1293
1294                 } else
1295 #endif
1296                 {
1297                         modmask = b->a_access_mask;
1298                 }
1299
1300 #ifdef NEW_LOGGING
1301                 LDAP_LOG( ACL, RESULTS, 
1302                            "acl_mask: [%d] applying %s (%s)\n",
1303                            i, accessmask2str( modmask, accessmaskbuf),
1304                            b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
1305                            ? "break" : "stop"  );
1306 #else
1307                 Debug( LDAP_DEBUG_ACL,
1308                         "<= acl_mask: [%d] applying %s (%s)\n",
1309                         i, accessmask2str( modmask, accessmaskbuf ), 
1310                         b->a_type == ACL_CONTINUE
1311                                 ? "continue"
1312                                 : b->a_type == ACL_BREAK
1313                                         ? "break"
1314                                         : "stop" );
1315 #endif
1316                 /* save old mask */
1317                 oldmask = *mask;
1318
1319                 if( ACL_IS_ADDITIVE(modmask) ) {
1320                         /* add privs */
1321                         ACL_PRIV_SET( *mask, modmask );
1322
1323                         /* cleanup */
1324                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1325
1326                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1327                         /* substract privs */
1328                         ACL_PRIV_CLR( *mask, modmask );
1329
1330                         /* cleanup */
1331                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1332
1333                 } else {
1334                         /* assign privs */
1335                         *mask = modmask;
1336                 }
1337
1338 #ifdef NEW_LOGGING
1339                 LDAP_LOG( ACL, DETAIL1, 
1340                            "acl_mask: conn %lu  [%d] mask: %s\n",
1341                            op->o_connid, i, accessmask2str( *mask, accessmaskbuf)  );
1342 #else
1343                 Debug( LDAP_DEBUG_ACL,
1344                         "<= acl_mask: [%d] mask: %s\n",
1345                         i, accessmask2str(*mask, accessmaskbuf), 0 );
1346 #endif
1347
1348                 if( b->a_type == ACL_CONTINUE ) {
1349                         continue;
1350
1351                 } else if ( b->a_type == ACL_BREAK ) {
1352                         return ACL_BREAK;
1353
1354                 } else {
1355                         return ACL_STOP;
1356                 }
1357         }
1358
1359         /* implicit "by * none" clause */
1360         ACL_INIT(*mask);
1361
1362 #ifdef NEW_LOGGING
1363         LDAP_LOG( ACL, RESULTS, 
1364                    "acl_mask: conn %lu  no more <who> clauses, returning %d (stop)\n",
1365                    op->o_connid, accessmask2str( *mask, accessmaskbuf) , 0 );
1366 #else
1367         Debug( LDAP_DEBUG_ACL,
1368                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1369                 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1370 #endif
1371         return ACL_STOP;
1372 }
1373
1374 /*
1375  * acl_check_modlist - check access control on the given entry to see if
1376  * it allows the given modifications by the user associated with op.
1377  * returns      1       if mods allowed ok
1378  *                      0       mods not allowed
1379  */
1380
1381 int
1382 acl_check_modlist(
1383         Operation       *op,
1384         Entry   *e,
1385         Modifications   *mlist
1386 )
1387 {
1388         struct berval *bv;
1389         AccessControlState state = ACL_STATE_INIT;
1390
1391         assert( op->o_bd != NULL );
1392
1393         /* short circuit root database access */
1394         if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
1395 #ifdef NEW_LOGGING
1396                 LDAP_LOG( ACL, DETAIL1, 
1397                            "acl_check_modlist: conn %lu  access granted to root user\n",
1398                            op->o_connid, 0, 0 );
1399 #else
1400                 Debug( LDAP_DEBUG_ACL,
1401                         "<= acl_access_allowed: granted to database root\n",
1402                     0, 0, 0 );
1403 #endif
1404                 return 1;
1405         }
1406
1407         /* use backend default access if no backend acls */
1408         if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1409 #ifdef NEW_LOGGING
1410                 LDAP_LOG( ACL, DETAIL1, 
1411                         "acl_check_modlist: backend default %s access %s to \"%s\"\n",
1412                         access2str( ACL_WRITE ),
1413                         op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", 
1414                         op->o_dn.bv_val  );
1415 #else
1416                 Debug( LDAP_DEBUG_ACL,
1417                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
1418                         access2str( ACL_WRITE ),
1419                         op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1420 #endif
1421                 return op->o_bd->be_dfltaccess >= ACL_WRITE;
1422
1423 #ifdef notdef
1424         /* op->o_bd is always non-NULL */
1425         /* use global default access if no global acls */
1426         } else if ( op->o_bd == NULL && global_acl == NULL ) {
1427 #ifdef NEW_LOGGING
1428                 LDAP_LOG( ACL, DETAIL1, 
1429                         "acl_check_modlist: global default %s access %s to \"%s\"\n",
1430                    access2str( ACL_WRITE ),
1431                    global_default_access >= ACL_WRITE ? "granted" : "denied", 
1432                    op->o_dn  );
1433 #else
1434                 Debug( LDAP_DEBUG_ACL,
1435                         "=> access_allowed: global default %s access %s to \"%s\"\n",
1436                         access2str( ACL_WRITE ),
1437                         global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1438 #endif
1439                 return global_default_access >= ACL_WRITE;
1440 #endif
1441         }
1442
1443         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1444                 /*
1445                  * no-user-modification operational attributes are ignored
1446                  * by ACL_WRITE checking as any found here are not provided
1447                  * by the user
1448                  */
1449                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1450 #ifdef NEW_LOGGING
1451                         LDAP_LOG( ACL, DETAIL1, 
1452                                    "acl_check_modlist: conn %lu  no-user-mod %s: modify access granted\n",
1453                                    op->o_connid, mlist->sml_desc->ad_cname.bv_val , 0 );
1454 #else
1455                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1456                                 " modify access granted\n",
1457                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1458 #endif
1459                         continue;
1460                 }
1461
1462                 switch ( mlist->sml_op ) {
1463                 case LDAP_MOD_REPLACE:
1464                         /*
1465                          * We must check both permission to delete the whole
1466                          * attribute and permission to add the specific attributes.
1467                          * This prevents abuse from selfwriters.
1468                          */
1469                         if ( ! access_allowed( op, e,
1470                                 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1471                         {
1472                                 return( 0 );
1473                         }
1474
1475                         if ( mlist->sml_bvalues == NULL ) break;
1476
1477                         /* fall thru to check value to add */
1478
1479                 case LDAP_MOD_ADD:
1480                         assert( mlist->sml_bvalues != NULL );
1481
1482                         for ( bv = mlist->sml_nvalues
1483                                         ? mlist->sml_nvalues : mlist->sml_values;
1484                                 bv->bv_val != NULL; bv++ )
1485                         {
1486                                 if ( ! access_allowed( op, e,
1487                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1488                                 {
1489                                         return( 0 );
1490                                 }
1491                         }
1492                         break;
1493
1494                 case LDAP_MOD_DELETE:
1495                         if ( mlist->sml_bvalues == NULL ) {
1496                                 if ( ! access_allowed( op, e,
1497                                         mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1498                                 {
1499                                         return( 0 );
1500                                 }
1501                                 break;
1502                         }
1503                         for ( bv = mlist->sml_nvalues
1504                                         ? mlist->sml_nvalues : mlist->sml_values;
1505                                 bv->bv_val != NULL; bv++ )
1506                         {
1507                                 if ( ! access_allowed( op, e,
1508                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1509                                 {
1510                                         return( 0 );
1511                                 }
1512                         }
1513                         break;
1514
1515                 case SLAP_MOD_SOFTADD:
1516                         /* allow adding attribute via modrdn thru */
1517                         break;
1518
1519                 default:
1520                         assert( 0 );
1521                         return( 0 );
1522                 }
1523         }
1524
1525         return( 1 );
1526 }
1527
1528 static int
1529 aci_get_part(
1530         struct berval *list,
1531         int ix,
1532         char sep,
1533         struct berval *bv )
1534 {
1535         int len;
1536         char *p;
1537
1538         if (bv) {
1539                 bv->bv_len = 0;
1540                 bv->bv_val = NULL;
1541         }
1542         len = list->bv_len;
1543         p = list->bv_val;
1544         while (len >= 0 && --ix >= 0) {
1545                 while (--len >= 0 && *p++ != sep) ;
1546         }
1547         while (len >= 0 && *p == ' ') {
1548                 len--;
1549                 p++;
1550         }
1551         if (len < 0)
1552                 return(-1);
1553
1554         if (!bv)
1555                 return(0);
1556
1557         bv->bv_val = p;
1558         while (--len >= 0 && *p != sep) {
1559                 bv->bv_len++;
1560                 p++;
1561         }
1562         while (bv->bv_len > 0 && *--p == ' ')
1563                 bv->bv_len--;
1564         return(bv->bv_len);
1565 }
1566
1567 BerVarray
1568 aci_set_gather (SetCookie *cookie, struct berval *name, struct berval *attr)
1569 {
1570         AciSetCookie *cp = (AciSetCookie *)cookie;
1571         BerVarray bvals = NULL;
1572         struct berval ndn;
1573
1574         /* this routine needs to return the bervals instead of
1575          * plain strings, since syntax is not known.  It should
1576          * also return the syntax or some "comparison cookie".
1577          */
1578
1579         if (dnNormalize(0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx) == LDAP_SUCCESS) {
1580                 const char *text;
1581                 AttributeDescription *desc = NULL;
1582                 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1583                         backend_attribute(cp->op,
1584                                 cp->e, &ndn, desc, &bvals);
1585                 }
1586                 sl_free(ndn.bv_val, cp->op->o_tmpmemctx);
1587         }
1588         return(bvals);
1589 }
1590
1591 static int
1592 aci_match_set (
1593         struct berval *subj,
1594         Operation *op,
1595         Entry *e,
1596         int setref
1597 )
1598 {
1599         struct berval set = { 0, NULL };
1600         int rc = 0;
1601         AciSetCookie cookie;
1602
1603         if (setref == 0) {
1604                 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
1605         } else {
1606                 struct berval subjdn, ndn = { 0, NULL };
1607                 struct berval setat;
1608                 BerVarray bvals;
1609                 const char *text;
1610                 AttributeDescription *desc = NULL;
1611
1612                 /* format of string is "entry/setAttrName" */
1613                 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1614                         return(0);
1615                 }
1616
1617                 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1618                         setat.bv_val = SLAPD_ACI_SET_ATTR;
1619                         setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1620                 }
1621
1622                 if ( setat.bv_val != NULL ) {
1623                         /*
1624                          * NOTE: dnNormalize honors the ber_len field
1625                          * as the length of the dn to be normalized
1626                          */
1627                         if ( dnNormalize(0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS
1628                                 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1629                         {
1630                                 backend_attribute(op, e,
1631                                         &ndn, desc, &bvals);
1632                                 if ( bvals != NULL ) {
1633                                         if ( bvals[0].bv_val != NULL ) {
1634                                                 int i;
1635                                                 set = bvals[0];
1636                                                 bvals[0].bv_val = NULL;
1637                                                 for (i=1;bvals[i].bv_val;i++);
1638                                                 bvals[0].bv_val = bvals[i-1].bv_val;
1639                                                 bvals[i-1].bv_val = NULL;
1640                                         }
1641                                         ber_bvarray_free_x(bvals, op->o_tmpmemctx);
1642                                 }
1643                         }
1644                         if (ndn.bv_val)
1645                                 free(ndn.bv_val);
1646                 }
1647         }
1648
1649         if (set.bv_val != NULL) {
1650                 cookie.op = op;
1651                 cookie.e = e;
1652                 rc = (slap_set_filter(aci_set_gather, (SetCookie *)&cookie, &set,
1653                         &op->o_ndn, &e->e_nname, NULL) > 0);
1654                 sl_free(set.bv_val, op->o_tmpmemctx);
1655         }
1656         return(rc);
1657 }
1658
1659 #ifdef SLAPD_ACI_ENABLED
1660 static int
1661 aci_list_map_rights(
1662         struct berval *list )
1663 {
1664         struct berval bv;
1665         slap_access_t mask;
1666         int i;
1667
1668         ACL_INIT(mask);
1669         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1670                 if (bv.bv_len <= 0)
1671                         continue;
1672                 switch (*bv.bv_val) {
1673                 case 'c':
1674                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1675                         break;
1676                 case 's':
1677                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1678                          * the right 's' to mean "set", but in the examples states
1679                          * that the right 's' means "search".  The latter definition
1680                          * is used here.
1681                          */
1682                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1683                         break;
1684                 case 'r':
1685                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
1686                         break;
1687                 case 'w':
1688                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1689                         break;
1690                 case 'x':
1691                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
1692                          * define any equivalent to the AUTH right, so I've just used
1693                          * 'x' for now.
1694                          */
1695                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1696                         break;
1697                 default:
1698                         break;
1699                 }
1700
1701         }
1702         return(mask);
1703 }
1704
1705 static int
1706 aci_list_has_attr(
1707         struct berval *list,
1708         const struct berval *attr,
1709         struct berval *val )
1710 {
1711         struct berval bv, left, right;
1712         int i;
1713
1714         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1715                 if (aci_get_part(&bv, 0, '=', &left) < 0
1716                         || aci_get_part(&bv, 1, '=', &right) < 0)
1717                 {
1718                         if (ber_bvstrcasecmp(attr, &bv) == 0)
1719                                 return(1);
1720                 } else if (val == NULL) {
1721                         if (ber_bvstrcasecmp(attr, &left) == 0)
1722                                 return(1);
1723                 } else {
1724                         if (ber_bvstrcasecmp(attr, &left) == 0) {
1725                                 /* this is experimental code that implements a
1726                                  * simple (prefix) match of the attribute value.
1727                                  * the ACI draft does not provide for aci's that
1728                                  * apply to specific values, but it would be
1729                                  * nice to have.  If the <attr> part of an aci's
1730                                  * rights list is of the form <attr>=<value>,
1731                                  * that means the aci applies only to attrs with
1732                                  * the given value.  Furthermore, if the attr is
1733                                  * of the form <attr>=<value>*, then <value> is
1734                                  * treated as a prefix, and the aci applies to 
1735                                  * any value with that prefix.
1736                                  *
1737                                  * Ideally, this would allow r.e. matches.
1738                                  */
1739                                 if (aci_get_part(&right, 0, '*', &left) < 0
1740                                         || right.bv_len <= left.bv_len)
1741                                 {
1742                                         if (ber_bvstrcasecmp(val, &right) == 0)
1743                                                 return(1);
1744                                 } else if (val->bv_len >= left.bv_len) {
1745                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1746                                                 return(1);
1747                                 }
1748                         }
1749                 }
1750         }
1751         return(0);
1752 }
1753
1754 static slap_access_t
1755 aci_list_get_attr_rights(
1756         struct berval *list,
1757         const struct berval *attr,
1758         struct berval *val )
1759 {
1760     struct berval bv;
1761     slap_access_t mask;
1762     int i;
1763
1764         /* loop through each rights/attr pair, skip first part (action) */
1765         ACL_INIT(mask);
1766         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1767                 if (aci_list_has_attr(&bv, attr, val) == 0)
1768                         continue;
1769                 if (aci_get_part(list, i, ';', &bv) < 0)
1770                         continue;
1771                 mask |= aci_list_map_rights(&bv);
1772         }
1773         return(mask);
1774 }
1775
1776 static int
1777 aci_list_get_rights(
1778         struct berval *list,
1779         const struct berval *attr,
1780         struct berval *val,
1781         slap_access_t *grant,
1782         slap_access_t *deny )
1783 {
1784     struct berval perm, actn;
1785     slap_access_t *mask;
1786     int i, found;
1787
1788         if (attr == NULL || attr->bv_len == 0 
1789                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1790                 attr = &aci_bv_br_entry;
1791         }
1792
1793         found = 0;
1794         ACL_INIT(*grant);
1795         ACL_INIT(*deny);
1796         /* loop through each permissions clause */
1797         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1798                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1799                         continue;
1800                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1801                         mask = grant;
1802                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1803                         mask = deny;
1804                 } else {
1805                         continue;
1806                 }
1807
1808                 found = 1;
1809                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1810                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1811         }
1812         return(found);
1813 }
1814
1815 static int
1816 aci_group_member (
1817         struct berval *subj,
1818         struct berval *defgrpoc,
1819         struct berval *defgrpat,
1820         Operation               *op,
1821         Entry           *e,
1822         regmatch_t      *matches
1823 )
1824 {
1825         struct berval subjdn;
1826         struct berval grpoc;
1827         struct berval grpat;
1828         ObjectClass *grp_oc = NULL;
1829         AttributeDescription *grp_ad = NULL;
1830         const char *text;
1831         int rc;
1832
1833         /* format of string is "group/objectClassValue/groupAttrName" */
1834         if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1835                 return(0);
1836         }
1837
1838         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1839                 grpoc = *defgrpoc;
1840         }
1841
1842         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1843                 grpat = *defgrpat;
1844         }
1845
1846         rc = slap_bv2ad( &grpat, &grp_ad, &text );
1847         if( rc != LDAP_SUCCESS ) {
1848                 rc = 0;
1849                 goto done;
1850         }
1851         rc = 0;
1852
1853         grp_oc = oc_bvfind( &grpoc );
1854
1855         if (grp_oc != NULL && grp_ad != NULL ) {
1856                 char buf[ACL_BUF_SIZE];
1857                 struct berval bv, ndn;
1858                 bv.bv_len = sizeof( buf ) - 1;
1859                 bv.bv_val = (char *)&buf;
1860                 string_expand(&bv, &subjdn, e->e_ndn, matches);
1861                 if ( dnNormalize(0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
1862                         rc = (backend_group(op, e, &ndn, &op->o_ndn,
1863                                 grp_oc, grp_ad) == 0);
1864                         free( ndn.bv_val );
1865                 }
1866         }
1867
1868 done:
1869         return(rc);
1870 }
1871
1872 static int
1873 aci_mask(
1874     Operation           *op,
1875     Entry                       *e,
1876         AttributeDescription *desc,
1877     struct berval       *val,
1878     struct berval       *aci,
1879         regmatch_t              *matches,
1880         slap_access_t   *grant,
1881         slap_access_t   *deny,
1882         struct berval   *scope
1883 )
1884 {
1885     struct berval bv, perms, sdn;
1886         int rc;
1887                 
1888
1889         assert( desc->ad_cname.bv_val != NULL );
1890
1891         /* parse an aci of the form:
1892                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1893
1894            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1895            a full description of the format for this attribute.
1896            Differences: "this" in the draft is "self" here, and
1897            "self" and "public" is in the position of dnType.
1898
1899            For now, this routine only supports scope=entry.
1900          */
1901         /* check that the aci has all 5 components */
1902         if (aci_get_part(aci, 4, '#', NULL) < 0)
1903                 return(0);
1904
1905         /* check that the aci family is supported */
1906         if (aci_get_part(aci, 0, '#', &bv) < 0)
1907                 return(0);
1908
1909         /* check that the scope matches */
1910         if (aci_get_part(aci, 1, '#', &bv) < 0
1911                 || ber_bvstrcasecmp( scope, &bv ) != 0)
1912         {
1913                 return(0);
1914         }
1915
1916         /* get the list of permissions clauses, bail if empty */
1917         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1918                 return(0);
1919
1920         /* check if any permissions allow desired access */
1921         if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
1922                 return(0);
1923
1924         /* see if we have a DN match */
1925         if (aci_get_part(aci, 3, '#', &bv) < 0)
1926                 return(0);
1927
1928         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1929                 return(0);
1930
1931         if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
1932                 struct berval ndn;
1933                 rc = 0;
1934                 if ( dnNormalize(0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
1935                         if (dn_match( &op->o_ndn, &ndn))
1936                                 rc = 1;
1937                         free(ndn.bv_val);
1938                 }
1939                 return (rc);
1940
1941         } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
1942                 return(1);
1943
1944         } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
1945                 if (dn_match(&op->o_ndn, &e->e_nname))
1946                         return(1);
1947
1948         } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
1949                 Attribute *at;
1950                 AttributeDescription *ad = NULL;
1951                 const char *text;
1952
1953                 rc = slap_bv2ad( &sdn, &ad, &text );
1954
1955                 if( rc != LDAP_SUCCESS ) {
1956                         return 0;
1957                 }
1958
1959                 rc = 0;
1960
1961                 bv = op->o_ndn;
1962
1963                 for(at = attrs_find( e->e_attrs, ad );
1964                         at != NULL;
1965                         at = attrs_find( at->a_next, ad ) )
1966                 {
1967                         if (value_find_ex( ad,
1968                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1969                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1970                                 at->a_nvals,
1971                                 &bv, op->o_tmpmemctx) == 0 )
1972                         {
1973                                 rc = 1;
1974                                 break;
1975                         }
1976                 }
1977
1978                 return rc;
1979
1980
1981         } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
1982                 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, op, e, matches))
1983                         return(1);
1984
1985         } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
1986                 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, op, e, matches))
1987                         return(1);
1988
1989         } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
1990                 if (aci_match_set(&sdn, op, e, 0))
1991                         return(1);
1992
1993         } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
1994                 if (aci_match_set(&sdn, op, e, 1))
1995                         return(1);
1996
1997         }
1998
1999         return(0);
2000 }
2001
2002 #endif  /* SLAPD_ACI_ENABLED */
2003
2004 static void
2005 string_expand(
2006         struct berval *bv,
2007         struct berval *pat,
2008         char *match,
2009         regmatch_t *matches)
2010 {
2011         ber_len_t       size;
2012         char   *sp;
2013         char   *dp;
2014         int     flag;
2015
2016         size = 0;
2017         bv->bv_val[0] = '\0';
2018         bv->bv_len--; /* leave space for lone $ */
2019
2020         flag = 0;
2021         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2022                 sp < pat->bv_val + pat->bv_len ; sp++ )
2023         {
2024                 /* did we previously see a $ */
2025                 if ( flag ) {
2026                         if ( flag == 1 && *sp == '$' ) {
2027                                 *dp++ = '$';
2028                                 size++;
2029                                 flag = 0;
2030
2031                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2032                                 flag = 2;
2033
2034                         } else if ( *sp >= '0' && *sp <= '9' ) {
2035                                 int     n;
2036                                 int     i;
2037                                 int     l;
2038
2039                                 n = *sp - '0';
2040
2041                                 if ( flag == 2 ) {
2042                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2043                                                 if ( *sp >= '0' && *sp <= '9' ) {
2044                                                         n = 10*n + ( *sp - '0' );
2045                                                 }
2046                                         }
2047
2048                                         if ( *sp != /*'{'*/ '}' ) {
2049                                                 /* error */
2050                                         }
2051                                 }
2052
2053                                 if ( n >= MAXREMATCHES ) {
2054                                 
2055                                 }
2056                                 
2057                                 *dp = '\0';
2058                                 i = matches[n].rm_so;
2059                                 l = matches[n].rm_eo; 
2060                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2061                                         *dp++ = match[i];
2062                                 }
2063                                 *dp = '\0';
2064
2065                                 flag = 0;
2066                         }
2067                 } else {
2068                         if (*sp == '$') {
2069                                 flag = 1;
2070                         } else {
2071                                 *dp++ = *sp;
2072                                 size++;
2073                         }
2074                 }
2075         }
2076
2077         if ( flag ) {
2078                 /* must have ended with a single $ */
2079                 *dp++ = '$';
2080                 size++;
2081         }
2082
2083         *dp = '\0';
2084         bv->bv_len = size;
2085
2086 #ifdef NEW_LOGGING
2087         LDAP_LOG( ACL, DETAIL1, 
2088            "string_expand:  pattern = %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2089         LDAP_LOG( ACL, DETAIL1, "string_expand:  expanded = %s\n", bv->bv_val, 0, 0 );
2090 #else
2091         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2092         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2093 #endif
2094 }
2095
2096 static int
2097 regex_matches(
2098         struct berval *pat,                     /* pattern to expand and match against */
2099         char *str,                              /* string to match against pattern */
2100         char *buf,                              /* buffer with $N expansion variables */
2101         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
2102 )
2103 {
2104         regex_t re;
2105         char newbuf[ACL_BUF_SIZE];
2106         struct berval bv;
2107         int     rc;
2108
2109         bv.bv_len = sizeof(newbuf) - 1;
2110         bv.bv_val = newbuf;
2111
2112         if(str == NULL) str = "";
2113
2114         string_expand(&bv, pat, buf, matches);
2115         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
2116                 char error[ACL_BUF_SIZE];
2117                 regerror(rc, &re, error, sizeof(error));
2118
2119 #ifdef NEW_LOGGING
2120                 LDAP_LOG( ACL, ERR, 
2121                            "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
2122                            pat->bv_val, str, error  );
2123 #else
2124                 Debug( LDAP_DEBUG_TRACE,
2125                     "compile( \"%s\", \"%s\") failed %s\n",
2126                         pat->bv_val, str, error );
2127 #endif
2128                 return( 0 );
2129         }
2130
2131         rc = regexec(&re, str, 0, NULL, 0);
2132         regfree( &re );
2133
2134 #ifdef NEW_LOGGING
2135         LDAP_LOG( ACL, DETAIL2, "regex_matches: string:   %s\n", str, 0, 0 );
2136         LDAP_LOG( ACL, DETAIL2, "regex_matches: rc:     %d  %s\n",
2137                    rc, rc ? "matches" : "no matches", 0  );
2138 #else
2139         Debug( LDAP_DEBUG_TRACE,
2140             "=> regex_matches: string:   %s\n", str, 0, 0 );
2141         Debug( LDAP_DEBUG_TRACE,
2142             "=> regex_matches: rc: %d %s\n",
2143                 rc, !rc ? "matches" : "no matches", 0 );
2144 #endif
2145         return( !rc );
2146 }
2147