]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
4af1cfdb3cd2c8e3c4afa8095d6adffc702bd248
[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-2005 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 #include "lutil.h"
39
40 #define ACL_BUF_SIZE    1024    /* use most appropriate size */
41
42 /*
43  * speed up compares
44  */
45 static struct berval 
46         aci_bv_entry            = BER_BVC("entry"),
47         aci_bv_children         = BER_BVC("children"),
48         aci_bv_onelevel         = BER_BVC("onelevel"),
49         aci_bv_subtree          = BER_BVC("subtree"),
50         aci_bv_br_entry         = BER_BVC("[entry]"),
51         aci_bv_br_all           = BER_BVC("[all]"),
52         aci_bv_access_id        = BER_BVC("access-id"),
53 #if 0
54         aci_bv_anonymous        = BER_BVC("anonymous"),
55 #endif
56         aci_bv_public           = BER_BVC("public"),
57         aci_bv_users            = BER_BVC("users"),
58         aci_bv_self             = BER_BVC("self"),
59         aci_bv_dnattr           = BER_BVC("dnattr"),
60         aci_bv_group            = BER_BVC("group"),
61         aci_bv_role             = BER_BVC("role"),
62         aci_bv_set              = BER_BVC("set"),
63         aci_bv_set_ref          = BER_BVC("set-ref"),
64         aci_bv_grant            = BER_BVC("grant"),
65         aci_bv_deny             = BER_BVC("deny"),
66
67         aci_bv_ip_eq            = BER_BVC("IP="),
68 #ifdef LDAP_PF_LOCAL
69         aci_bv_path_eq          = BER_BVC("PATH="),
70 #if 0
71         aci_bv_dirsep           = BER_BVC(LDAP_DIRSEP),
72 #endif
73 #endif /* LDAP_PF_LOCAL */
74         
75         aci_bv_group_class      = BER_BVC(SLAPD_GROUP_CLASS),
76         aci_bv_group_attr       = BER_BVC(SLAPD_GROUP_ATTR),
77         aci_bv_role_class       = BER_BVC(SLAPD_ROLE_CLASS),
78         aci_bv_role_attr        = BER_BVC(SLAPD_ROLE_ATTR),
79         aci_bv_set_attr         = BER_BVC(SLAPD_ACI_SET_ATTR);
80
81 typedef enum slap_aci_scope_t {
82         SLAP_ACI_SCOPE_ENTRY            = 0x1,
83         SLAP_ACI_SCOPE_CHILDREN         = 0x2,
84         SLAP_ACI_SCOPE_SUBTREE          = ( SLAP_ACI_SCOPE_ENTRY | SLAP_ACI_SCOPE_CHILDREN )
85 } slap_aci_scope_t;
86
87 static AccessControl * slap_acl_get(
88         AccessControl *ac, int *count,
89         Operation *op, Entry *e,
90         AttributeDescription *desc,
91         struct berval *val,
92         int nmatch, regmatch_t *matches,
93         AccessControlState *state );
94
95 static slap_control_t slap_acl_mask(
96         AccessControl *ac, slap_mask_t *mask,
97         Operation *op, Entry *e,
98         AttributeDescription *desc,
99         struct berval *val,
100         int nmatch,
101         regmatch_t *matches,
102         int count,
103         AccessControlState *state );
104
105 #ifdef SLAPD_ACI_ENABLED
106 static int aci_mask(
107         Operation *op, Entry *e,
108         AttributeDescription *desc,
109         struct berval *val,
110         struct berval *aci,
111         int nmatch,
112         regmatch_t *matches,
113         slap_access_t *grant,
114         slap_access_t *deny,
115         slap_aci_scope_t scope);
116 #endif /* SLAPD_ACI_ENABLED */
117
118 static int      regex_matches(
119         struct berval *pat, char *str, char *buf,
120         int nmatch, regmatch_t *matches);
121 static int      string_expand(
122         struct berval *newbuf, struct berval *pattern,
123         char *match, int nmatch, regmatch_t *matches);
124
125 typedef struct AciSetCookie {
126         Operation *op;
127         Entry *e;
128 } AciSetCookie;
129
130 SLAP_SET_GATHER aci_set_gather;
131 SLAP_SET_GATHER aci_set_gather2;
132 static int aci_match_set ( struct berval *subj, Operation *op,
133     Entry *e, int setref );
134
135 /*
136  * access_allowed - check whether op->o_ndn is allowed the requested access
137  * to entry e, attribute attr, value val.  if val is null, access to
138  * the whole attribute is assumed (all values).
139  *
140  * This routine loops through all access controls and calls
141  * slap_acl_mask() on each applicable access control.
142  * The loop exits when a definitive answer is reached or
143  * or no more controls remain.
144  *
145  * returns:
146  *              0       access denied
147  *              1       access granted
148  *
149  * Notes:
150  * - can be legally called with op == NULL
151  * - can be legally called with op->o_bd == NULL
152  */
153
154 #ifdef SLAP_OVERLAY_ACCESS
155 int
156 slap_access_always_allowed(
157         Operation               *op,
158         Entry                   *e,
159         AttributeDescription    *desc,
160         struct berval           *val,
161         slap_access_t           access,
162         AccessControlState      *state,
163         slap_mask_t             *maskp )
164 {
165         assert( maskp != NULL );
166
167         ACL_PRIV_SET( *maskp, ACL_ACCESS2PRIV( access ) );
168
169         return 1;
170 }
171
172 int
173 slap_access_allowed(
174         Operation               *op,
175         Entry                   *e,
176         AttributeDescription    *desc,
177         struct berval           *val,
178         slap_access_t           access,
179         AccessControlState      *state,
180         slap_mask_t             *maskp )
181 {
182         int                             ret = 1;
183         int                             count;
184         AccessControl                   *a = NULL;
185
186 #ifdef LDAP_DEBUG
187         char                            accessmaskbuf[ACCESSMASK_MAXLEN];
188 #endif
189         slap_mask_t                     mask;
190         slap_control_t                  control;
191         slap_access_t                   access_level;
192         const char                      *attr;
193         regmatch_t                      matches[MAXREMATCHES];
194         int                             st_same_attr = 0;
195
196         assert( op != NULL );
197         assert( e != NULL );
198         assert( desc != NULL );
199         assert( maskp != NULL );
200
201         access_level = ACL_LEVEL( access );
202         attr = desc->ad_cname.bv_val;
203
204         assert( attr != NULL );
205
206         /* grant database root access */
207         if ( be_isroot( op ) ) {
208                 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
209                 mask = ACL_LVL_MANAGE;
210                 goto done;
211         }
212
213         /*
214          * no-user-modification operational attributes are ignored
215          * by ACL_WRITE checking as any found here are not provided
216          * by the user
217          */
218         if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
219                 && desc != slap_schema.si_ad_entry
220                 && desc != slap_schema.si_ad_children )
221         {
222                 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
223                         " %s access granted\n",
224                         attr, 0, 0 );
225                 goto done;
226         }
227
228         /* use backend default access if no backend acls */
229         if ( op->o_bd->be_acl == NULL ) {
230                 int     i;
231
232                 Debug( LDAP_DEBUG_ACL,
233                         "=> slap_access_allowed: backend default %s "
234                         "access %s to \"%s\"\n",
235                         access2str( access ),
236                         op->o_bd->be_dfltaccess >= access_level ? "granted" : "denied",
237                         op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
238                 ret = op->o_bd->be_dfltaccess >= access_level;
239
240                 mask = ACL_PRIV_LEVEL;
241                 for ( i = ACL_NONE; i <= op->o_bd->be_dfltaccess; i++ ) {
242                         ACL_PRIV_SET( mask, ACL_ACCESS2PRIV( i ) );
243                 }
244
245                 goto done;
246         }
247
248         ret = 0;
249         control = ACL_BREAK;
250
251         if ( st_same_attr ) {
252                 assert( state->as_vd_acl != NULL );
253
254                 a = state->as_vd_acl;
255                 count = state->as_vd_acl_count;
256                 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
257                         mask = state->as_vd_acl_mask;
258                         AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
259                         goto vd_access;
260                 }
261
262         } else {
263                 if ( state ) state->as_vi_acl = NULL;
264                 a = NULL;
265                 ACL_PRIV_ASSIGN( mask, *maskp );
266                 count = 0;
267                 memset( matches, '\0', sizeof( matches ) );
268         }
269
270         while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
271                 MAXREMATCHES, matches, state ) ) != NULL )
272         {
273                 int i;
274
275                 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
276                         Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
277                                 (int)matches[i].rm_so, (int)matches[i].rm_eo );
278                         if ( matches[i].rm_so <= matches[0].rm_eo ) {
279                                 int n;
280                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
281                                         Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
282                                 }
283                         }
284                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
285                 }
286
287                 if ( state ) {
288                         if ( state->as_vi_acl == a &&
289                                 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
290                         {
291                                 Debug( LDAP_DEBUG_ACL,
292                                         "=> slap_access_allowed: result from state (%s)\n",
293                                         attr, 0, 0 );
294                                 ret = state->as_result;
295                                 goto done;
296                         } else {
297                                 Debug( LDAP_DEBUG_ACL,
298                                         "=> slap_access_allowed: no res from state (%s)\n",
299                                         attr, 0, 0 );
300                         }
301                 }
302
303 vd_access:
304                 control = slap_acl_mask( a, &mask, op,
305                         e, desc, val, MAXREMATCHES, matches, count, state );
306
307                 if ( control != ACL_BREAK ) {
308                         break;
309                 }
310
311                 memset( matches, '\0', sizeof( matches ) );
312         }
313
314         if ( ACL_IS_INVALID( mask ) ) {
315                 Debug( LDAP_DEBUG_ACL,
316                         "=> slap_access_allowed: \"%s\" (%s) invalid!\n",
317                         e->e_dn, attr, 0 );
318                 ACL_PRIV_ASSIGN( mask, *maskp );
319
320         } else if ( control == ACL_BREAK ) {
321                 Debug( LDAP_DEBUG_ACL,
322                         "=> slap_access_allowed: no more rules\n", 0, 0, 0 );
323
324                 goto done;
325         }
326
327         ret = ACL_GRANT( mask, access );
328
329         Debug( LDAP_DEBUG_ACL,
330                 "=> slap_access_allowed: %s access %s by %s\n",
331                 access2str( access ), ret ? "granted" : "denied",
332                 accessmask2str( mask, accessmaskbuf, 1 ) );
333
334 done:
335         ACL_PRIV_ASSIGN( *maskp, mask );
336         return ret;
337 }
338
339 int
340 fe_access_allowed(
341         Operation               *op,
342         Entry                   *e,
343         AttributeDescription    *desc,
344         struct berval           *val,
345         slap_access_t           access,
346         AccessControlState      *state,
347         slap_mask_t             *maskp )
348 {
349         BackendDB               *be_orig;
350         int                     rc;
351
352         be_orig = op->o_bd;
353
354         op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
355         rc = slap_access_allowed( op, e, desc, val, access, state, maskp );
356         op->o_bd = be_orig;
357
358         return rc;
359 }
360
361 int
362 access_allowed_mask(
363         Operation               *op,
364         Entry                   *e,
365         AttributeDescription    *desc,
366         struct berval           *val,
367         slap_access_t           access,
368         AccessControlState      *state,
369         slap_mask_t             *maskp )
370 {
371         int                             ret = 1;
372         AccessControl                   *a = NULL;
373         int                             be_null = 0;
374
375 #ifdef LDAP_DEBUG
376         char                            accessmaskbuf[ACCESSMASK_MAXLEN];
377 #endif
378         slap_mask_t                     mask;
379         slap_control_t                  control;
380         slap_access_t                   access_level;
381         const char                      *attr;
382         int                             st_same_attr = 0;
383         static AccessControlState       state_init = ACL_STATE_INIT;
384
385         assert( e != NULL );
386         assert( desc != NULL );
387
388         access_level = ACL_LEVEL( access );
389
390         assert( access_level > ACL_NONE );
391
392         ACL_INIT( mask );
393         if ( maskp ) ACL_INVALIDATE( *maskp );
394
395         attr = desc->ad_cname.bv_val;
396
397         assert( attr != NULL );
398
399         if ( op && op->o_is_auth_check &&
400                 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
401         {
402                 access = ACL_AUTH;
403         }
404
405         if ( state ) {
406                 if ( state->as_vd_ad == desc ) {
407                         if ( state->as_recorded ) {
408                                 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
409                                         val == NULL )
410                                 {
411                                         return state->as_result;
412
413                                 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
414                                         val != NULL && state->as_vd_acl == NULL )
415                                 {
416                                         return state->as_result;
417                                 }
418                         }
419                         st_same_attr = 1;
420                 } else {
421                         *state = state_init;
422                 }
423
424                 state->as_vd_ad = desc;
425         }
426
427         Debug( LDAP_DEBUG_ACL,
428                 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
429                 access2str( access ), e->e_dn, attr );
430
431         if ( op == NULL ) {
432                 /* no-op call */
433                 goto done;
434         }
435
436         if ( op->o_bd == NULL ) {
437                 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
438                 be_null = 1;
439
440 #ifdef LDAP_DEVEL
441                 /*
442                  * FIXME: experimental; use first backend rules
443                  * iff there is no global_acl (ITS#3100) */
444                 if ( frontendDB->be_acl != NULL ) {
445                         op->o_bd = frontendDB;
446                 }
447 #endif /* LDAP_DEVEL */
448         }
449         assert( op->o_bd != NULL );
450
451         /* this is enforced in backend_add() */
452         if ( op->o_bd->bd_info->bi_access_allowed ) {
453                 /* delegate to backend */
454                 ret = op->o_bd->bd_info->bi_access_allowed( op, e,
455                                 desc, val, access, state, &mask );
456
457         } else {
458                 BackendDB       *be_orig = op->o_bd;
459
460                 /* use default (but pass through frontend
461                  * for global ACL overlays) */
462                 op->o_bd = frontendDB;
463                 ret = frontendDB->bd_info->bi_access_allowed( op, e,
464                                 desc, val, access, state, &mask );
465                 op->o_bd = be_orig;
466         }
467
468         if ( !ret ) {
469                 if ( ACL_IS_INVALID( mask ) ) {
470                         Debug( LDAP_DEBUG_ACL,
471                                 "=> access_allowed: \"%s\" (%s) invalid!\n",
472                                 e->e_dn, attr, 0 );
473                         ACL_INIT( mask );
474
475                 } else if ( control == ACL_BREAK ) {
476                         Debug( LDAP_DEBUG_ACL,
477                                 "=> access_allowed: no more rules\n", 0, 0, 0 );
478
479                         goto done;
480                 }
481
482                 ret = ACL_GRANT( mask, access );
483         }
484
485         Debug( LDAP_DEBUG_ACL,
486                 "=> access_allowed: %s access %s by %s\n",
487                 access2str( access ), ret ? "granted" : "denied",
488                 accessmask2str( mask, accessmaskbuf, 1 ) );
489
490 done:
491         if ( state != NULL ) {
492                 /* If not value-dependent, save ACL in case of more attrs */
493                 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
494                         state->as_vi_acl = a;
495                         state->as_result = ret;
496                 }
497                 state->as_recorded |= ACL_STATE_RECORDED;
498         }
499         if ( be_null ) op->o_bd = NULL;
500         if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
501         return ret;
502 }
503
504 #else /* !SLAP_OVERLAY_ACCESS */
505
506 int
507 access_allowed_mask(
508         Operation               *op,
509         Entry                   *e,
510         AttributeDescription    *desc,
511         struct berval           *val,
512         slap_access_t           access,
513         AccessControlState      *state,
514         slap_mask_t             *maskp )
515 {
516         int                             ret = 1;
517         int                             count;
518         AccessControl                   *a = NULL;
519         Backend                         *be;
520         int                             be_null = 0;
521
522 #ifdef LDAP_DEBUG
523         char                            accessmaskbuf[ACCESSMASK_MAXLEN];
524 #endif
525         slap_mask_t                     mask;
526         slap_control_t                  control;
527         slap_access_t                   access_level;
528         const char                      *attr;
529         regmatch_t                      matches[MAXREMATCHES];
530         int                             st_same_attr = 0;
531         static AccessControlState       state_init = ACL_STATE_INIT;
532
533         assert( e != NULL );
534         assert( desc != NULL );
535
536         access_level = ACL_LEVEL( access );
537
538         assert( access_level > ACL_NONE );
539         if ( maskp ) ACL_INVALIDATE( *maskp );
540
541         attr = desc->ad_cname.bv_val;
542
543         assert( attr != NULL );
544
545         if ( op && op->o_is_auth_check &&
546                 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
547         {
548                 access = ACL_AUTH;
549         }
550
551         if ( state ) {
552                 if ( state->as_vd_ad == desc ) {
553                         if ( state->as_recorded ) {
554                                 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
555                                         val == NULL )
556                                 {
557                                         return state->as_result;
558
559                                 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
560                                         val != NULL && state->as_vd_acl == NULL )
561                                 {
562                                         return state->as_result;
563                                 }
564                         }
565                         st_same_attr = 1;
566                 } else {
567                         *state = state_init;
568                 }
569
570                 state->as_vd_ad=desc;
571         }
572
573         Debug( LDAP_DEBUG_ACL,
574                 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
575                 access2str( access ), e->e_dn, attr );
576
577         if ( op == NULL ) {
578                 /* no-op call */
579                 goto done;
580         }
581
582         be = op->o_bd;
583         if ( be == NULL ) {
584                 be = LDAP_STAILQ_FIRST(&backendDB);
585                 be_null = 1;
586 #ifdef LDAP_DEVEL
587                 /*
588                  * FIXME: experimental; use first backend rules
589                  * iff there is no global_acl (ITS#3100) */
590                 if ( frontendDB->be_acl == NULL ) 
591 #endif
592                 {
593                         op->o_bd = be;
594                 }
595         }
596         assert( be != NULL );
597
598         /* grant database root access */
599         if ( be_isroot( op ) ) {
600                 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
601                 if ( maskp ) {
602                         mask = ACL_LVL_MANAGE;
603                 }
604
605                 goto done;
606         }
607
608         /*
609          * no-user-modification operational attributes are ignored
610          * by ACL_WRITE checking as any found here are not provided
611          * by the user
612          */
613         if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
614                 && desc != slap_schema.si_ad_entry
615                 && desc != slap_schema.si_ad_children )
616         {
617                 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
618                         " %s access granted\n",
619                         attr, 0, 0 );
620                 goto done;
621         }
622
623         /* use backend default access if no backend acls */
624         if ( be->be_acl == NULL ) {
625                 Debug( LDAP_DEBUG_ACL,
626                         "=> access_allowed: backend default %s "
627                         "access %s to \"%s\"\n",
628                         access2str( access ),
629                         be->be_dfltaccess >= access_level ? "granted" : "denied",
630                         op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
631                 ret = be->be_dfltaccess >= access_level;
632
633                 if ( maskp ) {
634                         int     i;
635
636                         mask = ACL_PRIV_LEVEL;
637                         for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
638                                 mask |= ACL_ACCESS2PRIV( i );
639                         }
640                 }
641
642                 goto done;
643
644 #ifdef notdef
645         /* be is always non-NULL */
646         /* use global default access if no global acls */
647         } else if ( be == NULL && frontendDB->be_acl == NULL ) {
648                 Debug( LDAP_DEBUG_ACL,
649                         "=> access_allowed: global default %s access %s to \"%s\"\n",
650                         access2str( access ),
651                         frontendDB->be_dfltaccess >= access_level ?
652                                 "granted" : "denied", op->o_dn.bv_val );
653                 ret = frontendDB->be_dfltaccess >= access_level;
654
655                 if ( maskp ) {
656                         int     i;
657
658                         mask = ACL_PRIV_LEVEL;
659                         for ( i = ACL_NONE; i <= global_default_access; i++ ) {
660                                 mask |= ACL_ACCESS2PRIV( i );
661                         }
662                 }
663
664                 goto done;
665 #endif
666         }
667
668         ret = 0;
669         control = ACL_BREAK;
670
671         if ( st_same_attr ) {
672                 assert( state->as_vd_acl != NULL );
673
674                 a = state->as_vd_acl;
675                 count = state->as_vd_acl_count;
676                 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
677                         mask = state->as_vd_acl_mask;
678                         AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
679                         goto vd_access;
680                 }
681
682         } else {
683                 if ( state ) state->as_vi_acl = NULL;
684                 a = NULL;
685                 ACL_INIT(mask);
686                 count = 0;
687                 memset( matches, '\0', sizeof(matches) );
688         }
689
690         while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
691                 MAXREMATCHES, matches, state ) ) != NULL )
692         {
693                 int i;
694
695                 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
696                         Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
697                                 (int)matches[i].rm_so, (int)matches[i].rm_eo );
698                         if ( matches[i].rm_so <= matches[0].rm_eo ) {
699                                 int n;
700                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
701                                         Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
702                                 }
703                         }
704                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
705                 }
706
707                 if ( state ) {
708                         if ( state->as_vi_acl == a &&
709                                 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
710                         {
711                                 Debug( LDAP_DEBUG_ACL,
712                                         "access_allowed: result from state (%s)\n",
713                                         attr, 0, 0 );
714                                 ret = state->as_result;
715                                 goto done;
716                         } else {
717                                 Debug( LDAP_DEBUG_ACL,
718                                         "access_allowed: no res from state (%s)\n",
719                                         attr, 0, 0 );
720                         }
721                 }
722
723 vd_access:
724                 control = slap_acl_mask( a, &mask, op,
725                         e, desc, val, MAXREMATCHES, matches, count, state );
726
727                 if ( control != ACL_BREAK ) {
728                         break;
729                 }
730
731                 memset( matches, '\0', sizeof(matches) );
732         }
733
734         if ( ACL_IS_INVALID( mask ) ) {
735                 Debug( LDAP_DEBUG_ACL,
736                         "=> access_allowed: \"%s\" (%s) invalid!\n",
737                         e->e_dn, attr, 0 );
738                 ACL_INIT(mask);
739
740         } else if ( control == ACL_BREAK ) {
741                 Debug( LDAP_DEBUG_ACL,
742                         "=> access_allowed: no more rules\n", 0, 0, 0 );
743
744                 goto done;
745         }
746
747         Debug( LDAP_DEBUG_ACL,
748                 "=> access_allowed: %s access %s by %s\n",
749                 access2str( access ),
750                 ACL_GRANT(mask, access) ? "granted" : "denied",
751                 accessmask2str( mask, accessmaskbuf, 1 ) );
752
753         ret = ACL_GRANT(mask, access);
754
755 done:
756         if ( state != NULL ) {
757                 /* If not value-dependent, save ACL in case of more attrs */
758                 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
759                         state->as_vi_acl = a;
760                         state->as_result = ret;
761                 }
762                 state->as_recorded |= ACL_STATE_RECORDED;
763         }
764         if ( be_null ) op->o_bd = NULL;
765         if ( maskp ) *maskp = mask;
766         return ret;
767 }
768
769 #endif /* SLAP_OVERLAY_ACCESS */
770
771 /*
772  * slap_acl_get - return the acl applicable to entry e, attribute
773  * attr.  the acl returned is suitable for use in subsequent calls to
774  * acl_access_allowed().
775  */
776
777 static AccessControl *
778 slap_acl_get(
779         AccessControl *a,
780         int                     *count,
781         Operation       *op,
782         Entry           *e,
783         AttributeDescription *desc,
784         struct berval   *val,
785         int                     nmatch,
786         regmatch_t      *matches,
787         AccessControlState *state )
788 {
789         const char *attr;
790         int dnlen, patlen;
791         AccessControl *prev;
792
793         assert( e != NULL );
794         assert( count != NULL );
795         assert( desc != NULL );
796
797         attr = desc->ad_cname.bv_val;
798
799         assert( attr != NULL );
800
801         if( a == NULL ) {
802                 if( op->o_bd == NULL ) {
803                         a = frontendDB->be_acl;
804                 } else {
805                         a = op->o_bd->be_acl;
806                 }
807                 prev = NULL;
808
809                 assert( a != NULL );
810
811         } else {
812                 prev = a;
813                 a = a->acl_next;
814         }
815
816         dnlen = e->e_nname.bv_len;
817
818         for ( ; a != NULL; a = a->acl_next ) {
819                 (*count) ++;
820
821                 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
822                         if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
823                                 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n", 
824                                         *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
825                                 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
826                                         continue;
827
828                         } else {
829                                 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n", 
830                                         *count, a->acl_dn_pat.bv_val, 0 );
831                                 patlen = a->acl_dn_pat.bv_len;
832                                 if ( dnlen < patlen )
833                                         continue;
834
835                                 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
836                                         /* base dn -- entire object DN must match */
837                                         if ( dnlen != patlen )
838                                                 continue;
839
840                                 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
841                                         int     rdnlen = -1, sep = 0;
842
843                                         if ( dnlen <= patlen )
844                                                 continue;
845
846                                         if ( patlen > 0 ) {
847                                                 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
848                                                         continue;
849                                                 sep = 1;
850                                         }
851
852                                         rdnlen = dn_rdnlen( NULL, &e->e_nname );
853                                         if ( rdnlen != dnlen - patlen - sep )
854                                                 continue;
855
856                                 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
857                                         if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
858                                                 continue;
859
860                                 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
861                                         if ( dnlen <= patlen )
862                                                 continue;
863                                         if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
864                                                 continue;
865                                 }
866
867                                 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
868                                         continue;
869                         }
870
871                         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
872                                 *count, 0, 0 );
873                 }
874
875                 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
876                         matches[0].rm_so = matches[0].rm_eo = -1;
877                         continue;
878                 }
879
880                 /* Is this ACL only for a specific value? */
881                 if ( a->acl_attrval.bv_len ) {
882                         if ( val == NULL ) {
883                                 continue;
884                         }
885
886                         if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
887                                 state->as_recorded |= ACL_STATE_RECORDED_VD;
888                                 state->as_vd_acl = a;
889                                 state->as_vd_acl_count = *count;
890                                 state->as_vd_access = a->acl_access;
891                                 state->as_vd_access_count = 1;
892                                 ACL_INVALIDATE( state->as_vd_acl_mask );
893                         }
894
895                         if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
896                                 Debug( LDAP_DEBUG_ACL,
897                                         "acl_get: valpat %s\n",
898                                         a->acl_attrval.bv_val, 0, 0 );
899                                 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
900                                 {
901                                         continue;
902                                 }
903
904                         } else {
905                                 int match = 0;
906                                 const char *text;
907                                 Debug( LDAP_DEBUG_ACL,
908                                         "acl_get: val %s\n",
909                                         a->acl_attrval.bv_val, 0, 0 );
910         
911                                 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
912                                         if (value_match( &match, desc,
913                                                 /* desc->ad_type->sat_equality */ a->acl_attrval_mr, 0,
914                                                 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
915                                                         match )
916                                                 continue;
917                                         
918                                 } else {
919                                         int             patlen, vdnlen;
920         
921                                         patlen = a->acl_attrval.bv_len;
922                                         vdnlen = val->bv_len;
923         
924                                         if ( vdnlen < patlen )
925                                                 continue;
926         
927                                         if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
928                                                 if ( vdnlen > patlen )
929                                                         continue;
930         
931                                         } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
932                                                 int rdnlen = -1;
933         
934                                                 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
935                                                         continue;
936         
937                                                 rdnlen = dn_rdnlen( NULL, val );
938                                                 if ( rdnlen != vdnlen - patlen - 1 )
939                                                         continue;
940         
941                                         } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
942                                                 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
943                                                         continue;
944         
945                                         } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
946                                                 if ( vdnlen <= patlen )
947                                                         continue;
948         
949                                                 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
950                                                         continue;
951                                         }
952         
953                                         if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
954                                                 continue;
955                                 }
956                         }
957                 }
958
959                 if ( a->acl_filter != NULL ) {
960                         ber_int_t rc = test_filter( NULL, e, a->acl_filter );
961                         if ( rc != LDAP_COMPARE_TRUE ) {
962                                 continue;
963                         }
964                 }
965
966                 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
967                        *count, attr, 0);
968                 return a;
969         }
970
971         Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
972         return( NULL );
973 }
974
975 static int
976 acl_mask_dn(
977         Operation               *op,
978         Entry                   *e,
979         AccessControl           *a,
980         int                     nmatch,
981         regmatch_t              *matches,
982         slap_dn_access          *b,
983         struct berval           *opndn )
984 {
985         /*
986          * if access applies to the entry itself, and the
987          * user is bound as somebody in the same namespace as
988          * the entry, OR the given dn matches the dn pattern
989          */
990         /*
991          * NOTE: styles "anonymous", "users" and "self" 
992          * have been moved to enum slap_style_t, whose 
993          * value is set in a_dn_style; however, the string
994          * is maintaned in a_dn_pat.
995          */
996         if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
997                 if ( !BER_BVISEMPTY( opndn ) ) {
998                         return 1;
999                 }
1000
1001         } else if ( b->a_style == ACL_STYLE_USERS ) {
1002                 if ( BER_BVISEMPTY( opndn ) ) {
1003                         return 1;
1004                 }
1005
1006         } else if ( b->a_style == ACL_STYLE_SELF ) {
1007                 struct berval   ndn, selfndn;
1008                 int             level;
1009
1010                 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
1011                         return 1;
1012                 }
1013
1014                 level = b->a_self_level;
1015                 if ( level < 0 ) {
1016                         selfndn = *opndn;
1017                         ndn = e->e_nname;
1018                         level = -level;
1019
1020                 } else {
1021                         ndn = *opndn;
1022                         selfndn = e->e_nname;
1023                 }
1024
1025                 for ( ; level > 0; level-- ) {
1026                         if ( BER_BVISEMPTY( &ndn ) ) {
1027                                 break;
1028                         }
1029                         dnParent( &ndn, &ndn );
1030                 }
1031                         
1032                 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
1033                 {
1034                         return 1;
1035                 }
1036
1037         } else if ( b->a_style == ACL_STYLE_REGEX ) {
1038                 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
1039                         int             tmp_nmatch;
1040                         regmatch_t      tmp_matches[2],
1041                                         *tmp_matchesp = tmp_matches;
1042
1043                         int             rc = 0;
1044
1045                         switch ( a->acl_dn_style ) {
1046                         case ACL_STYLE_REGEX:
1047                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1048                                         tmp_matchesp = matches;
1049                                         tmp_nmatch = nmatch;
1050                                         break;
1051                                 }
1052                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1053
1054                         case ACL_STYLE_BASE:
1055                                 tmp_matches[0].rm_so = 0;
1056                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1057                                 tmp_nmatch = 1;
1058                                 break;
1059
1060                         case ACL_STYLE_ONE:
1061                         case ACL_STYLE_SUBTREE:
1062                         case ACL_STYLE_CHILDREN:
1063                                 tmp_matches[0].rm_so = 0;
1064                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1065                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1066                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1067                                 tmp_nmatch = 2;
1068                                 break;
1069
1070                         default:
1071                                 /* error */
1072                                 rc = 1;
1073                                 break;
1074                         }
1075
1076                         if ( rc ) {
1077                                 return 1;
1078                         }
1079
1080                         if ( !regex_matches( &b->a_pat, opndn->bv_val,
1081                                 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
1082                         {
1083                                 return 1;
1084                         }
1085                 }
1086
1087         } else {
1088                 struct berval   pat;
1089                 ber_len_t       patlen, odnlen;
1090                 int             got_match = 0;
1091
1092                 if ( e->e_dn == NULL )
1093                         return 1;
1094
1095                 if ( b->a_expand ) {
1096                         struct berval   bv;
1097                         char            buf[ACL_BUF_SIZE];
1098                         
1099                         int             tmp_nmatch;
1100                         regmatch_t      tmp_matches[2],
1101                                         *tmp_matchesp = tmp_matches;
1102
1103                         int             rc = 0;
1104
1105                         bv.bv_len = sizeof( buf ) - 1;
1106                         bv.bv_val = buf;
1107
1108                         switch ( a->acl_dn_style ) {
1109                         case ACL_STYLE_REGEX:
1110                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1111                                         tmp_matchesp = matches;
1112                                         tmp_nmatch = nmatch;
1113                                         break;
1114                                 }
1115                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1116
1117                         case ACL_STYLE_BASE:
1118                                 tmp_matches[0].rm_so = 0;
1119                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1120                                 tmp_nmatch = 1;
1121                                 break;
1122
1123                         case ACL_STYLE_ONE:
1124                         case ACL_STYLE_SUBTREE:
1125                         case ACL_STYLE_CHILDREN:
1126                                 tmp_matches[0].rm_so = 0;
1127                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1128                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1129                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1130                                 tmp_nmatch = 2;
1131                                 break;
1132
1133                         default:
1134                                 /* error */
1135                                 rc = 1;
1136                                 break;
1137                         }
1138
1139                         if ( rc ) {
1140                                 return 1;
1141                         }
1142
1143                         if ( string_expand( &bv, &b->a_pat, 
1144                                         e->e_nname.bv_val,
1145                                         tmp_nmatch, tmp_matchesp ) )
1146                         {
1147                                 return 1;
1148                         }
1149                         
1150                         if ( dnNormalize(0, NULL, NULL, &bv,
1151                                         &pat, op->o_tmpmemctx )
1152                                         != LDAP_SUCCESS )
1153                         {
1154                                 /* did not expand to a valid dn */
1155                                 return 1;
1156                         }
1157
1158                 } else {
1159                         pat = b->a_pat;
1160                 }
1161
1162                 patlen = pat.bv_len;
1163                 odnlen = opndn->bv_len;
1164                 if ( odnlen < patlen ) {
1165                         goto dn_match_cleanup;
1166
1167                 }
1168
1169                 if ( b->a_style == ACL_STYLE_BASE ) {
1170                         /* base dn -- entire object DN must match */
1171                         if ( odnlen != patlen ) {
1172                                 goto dn_match_cleanup;
1173                         }
1174
1175                 } else if ( b->a_style == ACL_STYLE_ONE ) {
1176                         int rdnlen = -1;
1177
1178                         if ( odnlen <= patlen ) {
1179                                 goto dn_match_cleanup;
1180                         }
1181
1182                         if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1183                                 goto dn_match_cleanup;
1184                         }
1185
1186                         rdnlen = dn_rdnlen( NULL, opndn );
1187                         if ( rdnlen != odnlen - patlen - 1 ) {
1188                                 goto dn_match_cleanup;
1189                         }
1190
1191                 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
1192                         if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1193                                 goto dn_match_cleanup;
1194                         }
1195
1196                 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
1197                         if ( odnlen <= patlen ) {
1198                                 goto dn_match_cleanup;
1199                         }
1200
1201                         if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1202                                 goto dn_match_cleanup;
1203                         }
1204
1205                 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
1206                         int level;
1207                         struct berval ndn;
1208
1209                         if ( odnlen <= patlen ) {
1210                                 goto dn_match_cleanup;
1211                         }
1212
1213                         if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
1214                         {
1215                                 goto dn_match_cleanup;
1216                         }
1217                         
1218                         level = b->a_level;
1219                         ndn = *opndn;
1220                         for ( ; level > 0; level-- ) {
1221                                 if ( BER_BVISEMPTY( &ndn ) ) {
1222                                         goto dn_match_cleanup;
1223                                 }
1224                                 dnParent( &ndn, &ndn );
1225                                 if ( ndn.bv_len < patlen ) {
1226                                         goto dn_match_cleanup;
1227                                 }
1228                         }
1229                         
1230                         if ( ndn.bv_len != patlen ) {
1231                                 goto dn_match_cleanup;
1232                         }
1233                 }
1234
1235                 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
1236
1237 dn_match_cleanup:;
1238                 if ( pat.bv_val != b->a_pat.bv_val ) {
1239                         slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1240                 }
1241
1242                 if ( !got_match ) {
1243                         return 1;
1244                 }
1245         }
1246
1247         return 0;
1248 }
1249
1250 /*
1251  * Record value-dependent access control state
1252  */
1253 #define ACL_RECORD_VALUE_STATE do { \
1254                 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
1255                         state->as_recorded |= ACL_STATE_RECORDED_VD; \
1256                         state->as_vd_acl = a; \
1257                         AC_MEMCPY( state->as_vd_acl_matches, matches, \
1258                                 sizeof( state->as_vd_acl_matches )) ; \
1259                         state->as_vd_acl_count = count; \
1260                         state->as_vd_access = b; \
1261                         state->as_vd_access_count = i; \
1262                 } \
1263         } while( 0 )
1264
1265 static int
1266 acl_mask_dnattr(
1267         Operation               *op,
1268         Entry                   *e,
1269         struct berval           *val,
1270         AccessControl           *a,
1271         Access                  *b,
1272         int                     i,
1273         regmatch_t              *matches,
1274         int                     count,
1275         AccessControlState      *state,
1276         slap_dn_access          *bdn,
1277         struct berval           *opndn )
1278 {
1279         Attribute       *at;
1280         struct berval   bv;
1281         int             rc, match = 0;
1282         const char      *text;
1283         const char      *attr = bdn->a_at->ad_cname.bv_val;
1284
1285         assert( attr != NULL );
1286
1287         if ( BER_BVISEMPTY( opndn ) ) {
1288                 return 1;
1289         }
1290
1291         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1292         bv = *opndn;
1293
1294         /* see if asker is listed in dnattr */
1295         for ( at = attrs_find( e->e_attrs, bdn->a_at );
1296                 at != NULL;
1297                 at = attrs_find( at->a_next, bdn->a_at ) )
1298         {
1299                 if ( value_find_ex( bdn->a_at,
1300                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1301                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1302                         at->a_nvals,
1303                         &bv, op->o_tmpmemctx ) == 0 )
1304                 {
1305                         /* found it */
1306                         match = 1;
1307                         break;
1308                 }
1309         }
1310
1311         if ( match ) {
1312                 /* have a dnattr match. if this is a self clause then
1313                  * the target must also match the op dn.
1314                  */
1315                 if ( bdn->a_self ) {
1316                         /* check if the target is an attribute. */
1317                         if ( val == NULL ) return 1;
1318
1319                         /* target is attribute, check if the attribute value
1320                          * is the op dn.
1321                          */
1322                         rc = value_match( &match, bdn->a_at,
1323                                 bdn->a_at->ad_type->sat_equality, 0,
1324                                 val, &bv, &text );
1325                         /* on match error or no match, fail the ACL clause */
1326                         if ( rc != LDAP_SUCCESS || match != 0 )
1327                                 return 1;
1328                 }
1329
1330         } else {
1331                 /* no dnattr match, check if this is a self clause */
1332                 if ( ! bdn->a_self )
1333                         return 1;
1334
1335                 ACL_RECORD_VALUE_STATE;
1336                 
1337                 /* this is a self clause, check if the target is an
1338                  * attribute.
1339                  */
1340                 if ( val == NULL )
1341                         return 1;
1342
1343                 /* target is attribute, check if the attribute value
1344                  * is the op dn.
1345                  */
1346                 rc = value_match( &match, bdn->a_at,
1347                         bdn->a_at->ad_type->sat_equality, 0,
1348                         val, &bv, &text );
1349
1350                 /* on match error or no match, fail the ACL clause */
1351                 if ( rc != LDAP_SUCCESS || match != 0 )
1352                         return 1;
1353         }
1354
1355         return 0;
1356 }
1357
1358
1359 /*
1360  * slap_acl_mask - modifies mask based upon the given acl and the
1361  * requested access to entry e, attribute attr, value val.  if val
1362  * is null, access to the whole attribute is assumed (all values).
1363  *
1364  * returns      0       access NOT allowed
1365  *              1       access allowed
1366  */
1367
1368 static slap_control_t
1369 slap_acl_mask(
1370         AccessControl           *a,
1371         slap_mask_t             *mask,
1372         Operation               *op,
1373         Entry                   *e,
1374         AttributeDescription    *desc,
1375         struct berval           *val,
1376         int                     nmatch,
1377         regmatch_t              *matches,
1378         int                     count,
1379         AccessControlState      *state )
1380 {
1381         int             i;
1382         Access  *b;
1383 #ifdef LDAP_DEBUG
1384         char accessmaskbuf[ACCESSMASK_MAXLEN];
1385 #if !defined( SLAP_DYNACL ) && defined( SLAPD_ACI_ENABLED )
1386         char accessmaskbuf1[ACCESSMASK_MAXLEN];
1387 #endif /* !SLAP_DYNACL && SLAPD_ACI_ENABLED */
1388 #endif /* DEBUG */
1389         const char *attr;
1390
1391         assert( a != NULL );
1392         assert( mask != NULL );
1393         assert( desc != NULL );
1394
1395         attr = desc->ad_cname.bv_val;
1396
1397         assert( attr != NULL );
1398
1399         Debug( LDAP_DEBUG_ACL,
1400                 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1401                 e->e_dn, attr, 0 );
1402
1403         Debug( LDAP_DEBUG_ACL,
1404                 "=> acl_mask: to %s by \"%s\", (%s) \n",
1405                 val ? "value" : "all values",
1406                 op->o_ndn.bv_val ?  op->o_ndn.bv_val : "",
1407                 accessmask2str( *mask, accessmaskbuf, 1 ) );
1408
1409
1410         if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1411                 && state->as_vd_acl == a )
1412         {
1413                 b = state->as_vd_access;
1414                 i = state->as_vd_access_count;
1415
1416         } else {
1417                 b = a->acl_access;
1418                 i = 1;
1419         }
1420
1421         for ( ; b != NULL; b = b->a_next, i++ ) {
1422                 slap_mask_t oldmask, modmask;
1423
1424                 ACL_INVALIDATE( modmask );
1425
1426                 /* AND <who> clauses */
1427                 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1428                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1429                                 b->a_dn_pat.bv_val, 0, 0);
1430                         /*
1431                          * if access applies to the entry itself, and the
1432                          * user is bound as somebody in the same namespace as
1433                          * the entry, OR the given dn matches the dn pattern
1434                          */
1435                         /*
1436                          * NOTE: styles "anonymous", "users" and "self" 
1437                          * have been moved to enum slap_style_t, whose 
1438                          * value is set in a_dn_style; however, the string
1439                          * is maintaned in a_dn_pat.
1440                          */
1441
1442                         if ( acl_mask_dn( op, e, a, nmatch, matches,
1443                                 &b->a_dn, &op->o_ndn ) )
1444                         {
1445                                 continue;
1446                         }
1447                 }
1448
1449                 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1450                         struct berval   ndn;
1451
1452                         Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1453                                 b->a_realdn_pat.bv_val, 0, 0);
1454                         /*
1455                          * if access applies to the entry itself, and the
1456                          * user is bound as somebody in the same namespace as
1457                          * the entry, OR the given dn matches the dn pattern
1458                          */
1459                         /*
1460                          * NOTE: styles "anonymous", "users" and "self" 
1461                          * have been moved to enum slap_style_t, whose 
1462                          * value is set in a_dn_style; however, the string
1463                          * is maintaned in a_dn_pat.
1464                          */
1465
1466                         if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1467                         {
1468                                 ndn = op->o_conn->c_ndn;
1469                         } else {
1470                                 ndn = op->o_ndn;
1471                         }
1472
1473                         if ( acl_mask_dn( op, e, a, nmatch, matches,
1474                                 &b->a_realdn, &ndn ) )
1475                         {
1476                                 continue;
1477                         }
1478                 }
1479
1480                 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1481                         if ( ! op->o_conn->c_listener ) {
1482                                 continue;
1483                         }
1484                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1485                                 b->a_sockurl_pat.bv_val, 0, 0 );
1486
1487                         if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1488                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1489                                         if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1490                                                         e->e_ndn, nmatch, matches ) ) 
1491                                         {
1492                                                 continue;
1493                                         }
1494
1495                                 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1496                                         struct berval   bv;
1497                                         char buf[ACL_BUF_SIZE];
1498
1499                                         bv.bv_len = sizeof( buf ) - 1;
1500                                         bv.bv_val = buf;
1501                                         if ( string_expand( &bv, &b->a_sockurl_pat,
1502                                                         e->e_ndn, nmatch, matches ) )
1503                                         {
1504                                                 continue;
1505                                         }
1506
1507                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1508                                         {
1509                                                 continue;
1510                                         }
1511
1512                                 } else {
1513                                         if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1514                                         {
1515                                                 continue;
1516                                         }
1517                                 }
1518                         }
1519                 }
1520
1521                 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1522                         if ( !op->o_conn->c_peer_domain.bv_val ) {
1523                                 continue;
1524                         }
1525                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1526                                 b->a_domain_pat.bv_val, 0, 0 );
1527                         if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1528                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1529                                         if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1530                                                         e->e_ndn, nmatch, matches ) ) 
1531                                         {
1532                                                 continue;
1533                                         }
1534                                 } else {
1535                                         char buf[ACL_BUF_SIZE];
1536
1537                                         struct berval   cmp = op->o_conn->c_peer_domain;
1538                                         struct berval   pat = b->a_domain_pat;
1539
1540                                         if ( b->a_domain_expand ) {
1541                                                 struct berval bv;
1542
1543                                                 bv.bv_len = sizeof(buf) - 1;
1544                                                 bv.bv_val = buf;
1545
1546                                                 if ( string_expand(&bv, &b->a_domain_pat,
1547                                                                 e->e_ndn, nmatch, matches) )
1548                                                 {
1549                                                         continue;
1550                                                 }
1551                                                 pat = bv;
1552                                         }
1553
1554                                         if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1555                                                 int offset = cmp.bv_len - pat.bv_len;
1556                                                 if ( offset < 0 ) {
1557                                                         continue;
1558                                                 }
1559
1560                                                 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1561                                                         continue;
1562                                                 }
1563
1564                                                 /* trim the domain */
1565                                                 cmp.bv_val = &cmp.bv_val[ offset ];
1566                                                 cmp.bv_len -= offset;
1567                                         }
1568                                         
1569                                         if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1570                                                 continue;
1571                                         }
1572                                 }
1573                         }
1574                 }
1575
1576                 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1577                         if ( !op->o_conn->c_peer_name.bv_val ) {
1578                                 continue;
1579                         }
1580                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1581                                 b->a_peername_pat.bv_val, 0, 0 );
1582                         if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1583                                 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1584                                         if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1585                                                         e->e_ndn, nmatch, matches ) ) 
1586                                         {
1587                                                 continue;
1588                                         }
1589
1590                                 } else {
1591                                         /* try exact match */
1592                                         if ( b->a_peername_style == ACL_STYLE_BASE ) {
1593                                                 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1594                                                         continue;
1595                                                 }
1596
1597                                         } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1598                                                 struct berval   bv;
1599                                                 char buf[ACL_BUF_SIZE];
1600
1601                                                 bv.bv_len = sizeof( buf ) - 1;
1602                                                 bv.bv_val = buf;
1603                                                 if ( string_expand( &bv, &b->a_peername_pat,
1604                                                                 e->e_ndn, nmatch, matches ) )
1605                                                 {
1606                                                         continue;
1607                                                 }
1608
1609                                                 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1610                                                         continue;
1611                                                 }
1612
1613                                         /* extract IP and try exact match */
1614                                         } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1615                                                 char            *port;
1616                                                 char            buf[] = "255.255.255.255";
1617                                                 struct berval   ip;
1618                                                 unsigned long   addr;
1619                                                 int             port_number = -1;
1620                                                 
1621                                                 if ( strncasecmp( op->o_conn->c_peer_name.bv_val, 
1622                                                                         aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 ) 
1623                                                         continue;
1624
1625                                                 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1626                                                 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1627
1628                                                 port = strrchr( ip.bv_val, ':' );
1629                                                 if ( port ) {
1630                                                         char    *next;
1631                                                         
1632                                                         ip.bv_len = port - ip.bv_val;
1633                                                         ++port;
1634                                                         port_number = strtol( port, &next, 10 );
1635                                                         if ( next[0] != '\0' )
1636                                                                 continue;
1637                                                 }
1638                                                 
1639                                                 /* the port check can be anticipated here */
1640                                                 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1641                                                         continue;
1642                                                 
1643                                                 /* address longer than expected? */
1644                                                 if ( ip.bv_len >= sizeof(buf) )
1645                                                         continue;
1646
1647                                                 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1648                                                 buf[ ip.bv_len ] = '\0';
1649
1650                                                 addr = inet_addr( buf );
1651
1652                                                 /* unable to convert? */
1653                                                 if ( addr == (unsigned long)(-1) )
1654                                                         continue;
1655
1656                                                 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1657                                                         continue;
1658
1659 #ifdef LDAP_PF_LOCAL
1660                                         /* extract path and try exact match */
1661                                         } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1662                                                 struct berval path;
1663                                                 
1664                                                 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1665                                                                         aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1666                                                         continue;
1667
1668                                                 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1669                                                 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1670
1671                                                 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1672                                                         continue;
1673
1674 #endif /* LDAP_PF_LOCAL */
1675
1676                                         /* exact match (very unlikely...) */
1677                                         } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1678                                                         continue;
1679                                         }
1680                                 }
1681                         }
1682                 }
1683
1684                 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1685                         if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1686                                 continue;
1687                         }
1688                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1689                                 b->a_sockname_pat.bv_val, 0, 0 );
1690                         if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1691                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1692                                         if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1693                                                         e->e_ndn, nmatch, matches ) ) 
1694                                         {
1695                                                 continue;
1696                                         }
1697
1698                                 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1699                                         struct berval   bv;
1700                                         char buf[ACL_BUF_SIZE];
1701
1702                                         bv.bv_len = sizeof( buf ) - 1;
1703                                         bv.bv_val = buf;
1704                                         if ( string_expand( &bv, &b->a_sockname_pat,
1705                                                         e->e_ndn, nmatch, matches ) )
1706                                         {
1707                                                 continue;
1708                                         }
1709
1710                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1711                                                 continue;
1712                                         }
1713
1714                                 } else {
1715                                         if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1716                                                 continue;
1717                                         }
1718                                 }
1719                         }
1720                 }
1721
1722                 if ( b->a_dn_at != NULL ) {
1723                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1724                                         matches, count, state,
1725                                         &b->a_dn, &op->o_ndn ) )
1726                         {
1727                                 continue;
1728                         }
1729                 }
1730
1731                 if ( b->a_realdn_at != NULL ) {
1732                         struct berval   ndn;
1733
1734                         if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1735                         {
1736                                 ndn = op->o_conn->c_ndn;
1737                         } else {
1738                                 ndn = op->o_ndn;
1739                         }
1740
1741                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1742                                         matches, count, state,
1743                                         &b->a_realdn, &ndn ) )
1744                         {
1745                                 continue;
1746                         }
1747                 }
1748
1749                 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1750                         struct berval bv;
1751                         struct berval ndn = BER_BVNULL;
1752                         int rc;
1753
1754                         if ( op->o_ndn.bv_len == 0 ) {
1755                                 continue;
1756                         }
1757
1758                         /* b->a_group is an unexpanded entry name, expanded it should be an 
1759                          * entry with objectclass group* and we test to see if odn is one of
1760                          * the values in the attribute group
1761                          */
1762                         /* see if asker is listed in dnattr */
1763                         if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1764                                 char            buf[ACL_BUF_SIZE];
1765                                 int             tmp_nmatch;
1766                                 regmatch_t      tmp_matches[2],
1767                                                 *tmp_matchesp = tmp_matches;
1768
1769                                 bv.bv_len = sizeof(buf) - 1;
1770                                 bv.bv_val = buf;
1771
1772                                 rc = 0;
1773
1774                                 switch ( a->acl_dn_style ) {
1775                                 case ACL_STYLE_REGEX:
1776                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1777                                                 tmp_matchesp = matches;
1778                                                 tmp_nmatch = nmatch;
1779                                                 break;
1780                                         }
1781
1782                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1783                                 case ACL_STYLE_BASE:
1784                                         tmp_matches[0].rm_so = 0;
1785                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1786                                         tmp_nmatch = 1;
1787                                         break;
1788
1789                                 case ACL_STYLE_ONE:
1790                                 case ACL_STYLE_SUBTREE:
1791                                 case ACL_STYLE_CHILDREN:
1792                                         tmp_matches[0].rm_so = 0;
1793                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1794                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1795                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1796                                         tmp_nmatch = 2;
1797                                         break;
1798
1799                                 default:
1800                                         /* error */
1801                                         rc = 1;
1802                                         break;
1803                                 }
1804
1805                                 if ( rc ) {
1806                                         continue;
1807                                 }
1808                                 
1809                                 if ( string_expand( &bv, &b->a_group_pat,
1810                                                 e->e_nname.bv_val,
1811                                                 tmp_nmatch, tmp_matchesp ) )
1812                                 {
1813                                         continue;
1814                                 }
1815
1816                                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1817                                                 op->o_tmpmemctx ) != LDAP_SUCCESS )
1818                                 {
1819                                         /* did not expand to a valid dn */
1820                                         continue;
1821                                 }
1822
1823                                 bv = ndn;
1824
1825                         } else {
1826                                 bv = b->a_group_pat;
1827                         }
1828
1829                         rc = backend_group( op, e, &bv, &op->o_ndn,
1830                                 b->a_group_oc, b->a_group_at );
1831
1832                         if ( ndn.bv_val ) {
1833                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1834                         }
1835
1836                         if ( rc != 0 ) {
1837                                 continue;
1838                         }
1839                 }
1840
1841                 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1842                         struct berval   bv;
1843                         char            buf[ACL_BUF_SIZE];
1844
1845                         if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1846                                 int             tmp_nmatch;
1847                                 regmatch_t      tmp_matches[2],
1848                                                 *tmp_matchesp = tmp_matches;
1849                                 int             rc = 0;
1850
1851                                 bv.bv_len = sizeof( buf ) - 1;
1852                                 bv.bv_val = buf;
1853
1854                                 rc = 0;
1855
1856                                 switch ( a->acl_dn_style ) {
1857                                 case ACL_STYLE_REGEX:
1858                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1859                                                 tmp_matchesp = matches;
1860                                                 tmp_nmatch = nmatch;
1861                                                 break;
1862                                         }
1863
1864                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1865                                 case ACL_STYLE_BASE:
1866                                         tmp_matches[0].rm_so = 0;
1867                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1868                                         tmp_nmatch = 1;
1869                                         break;
1870
1871                                 case ACL_STYLE_ONE:
1872                                 case ACL_STYLE_SUBTREE:
1873                                 case ACL_STYLE_CHILDREN:
1874                                         tmp_matches[0].rm_so = 0;
1875                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1876                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1877                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1878                                         tmp_nmatch = 2;
1879                                         break;
1880
1881                                 default:
1882                                         /* error */
1883                                         rc = 1;
1884                                         break;
1885                                 }
1886
1887                                 if ( rc ) {
1888                                         continue;
1889                                 }
1890                                 
1891                                 if ( string_expand( &bv, &b->a_set_pat,
1892                                                 e->e_nname.bv_val,
1893                                                 tmp_nmatch, tmp_matchesp ) )
1894                                 {
1895                                         continue;
1896                                 }
1897
1898                         } else {
1899                                 bv = b->a_set_pat;
1900                         }
1901                         
1902                         if ( aci_match_set( &bv, op, e, 0 ) == 0 ) {
1903                                 continue;
1904                         }
1905                 }
1906
1907                 if ( b->a_authz.sai_ssf ) {
1908                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1909                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
1910                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
1911                                 continue;
1912                         }
1913                 }
1914
1915                 if ( b->a_authz.sai_transport_ssf ) {
1916                         Debug( LDAP_DEBUG_ACL,
1917                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1918                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1919                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
1920                                 continue;
1921                         }
1922                 }
1923
1924                 if ( b->a_authz.sai_tls_ssf ) {
1925                         Debug( LDAP_DEBUG_ACL,
1926                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1927                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1928                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
1929                                 continue;
1930                         }
1931                 }
1932
1933                 if ( b->a_authz.sai_sasl_ssf ) {
1934                         Debug( LDAP_DEBUG_ACL,
1935                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1936                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1937                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
1938                                 continue;
1939                         }
1940                 }
1941
1942 #ifdef SLAP_DYNACL
1943                 if ( b->a_dynacl ) {
1944                         slap_dynacl_t   *da;
1945                         slap_access_t   tgrant, tdeny;
1946
1947                         /* this case works different from the others above.
1948                          * since aci's themselves give permissions, we need
1949                          * to first check b->a_access_mask, the ACL's access level.
1950                          */
1951                         if ( BER_BVISEMPTY( &e->e_nname ) ) {
1952                                 /* no ACIs in the root DSE */
1953                                 continue;
1954                         }
1955
1956                         /* first check if the right being requested
1957                          * is allowed by the ACL clause.
1958                          */
1959                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1960                                 continue;
1961                         }
1962
1963                         /* start out with nothing granted, nothing denied */
1964                         ACL_INIT(tgrant);
1965                         ACL_INIT(tdeny);
1966
1967                         for ( da = b->a_dynacl; da; da = da->da_next ) {
1968                                 slap_access_t   grant, deny;
1969
1970                                 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1971
1972                                 tgrant |= grant;
1973                                 tdeny |= deny;
1974                         }
1975
1976                         /* remove anything that the ACL clause does not allow */
1977                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1978                         tdeny &= ACL_PRIV_MASK;
1979
1980                         /* see if we have anything to contribute */
1981                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
1982                                 continue;
1983                         }
1984
1985                         /* this could be improved by changing slap_acl_mask so that it can deal with
1986                          * by clauses that return grant/deny pairs.  Right now, it does either
1987                          * additive or subtractive rights, but not both at the same time.  So,
1988                          * we need to combine the grant/deny pair into a single rights mask in
1989                          * a smart way:  if either grant or deny is "empty", then we use the
1990                          * opposite as is, otherwise we remove any denied rights from the grant
1991                          * rights mask and construct an additive mask.
1992                          */
1993                         if (ACL_IS_INVALID(tdeny)) {
1994                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
1995
1996                         } else if (ACL_IS_INVALID(tgrant)) {
1997                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1998
1999                         } else {
2000                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2001                         }
2002
2003                 } else
2004 #else /* !SLAP_DYNACL */
2005
2006 #ifdef SLAPD_ACI_ENABLED
2007                 if ( b->a_aci_at != NULL ) {
2008                         Attribute       *at;
2009                         slap_access_t   grant, deny, tgrant, tdeny;
2010                         struct berval   parent_ndn;
2011                         BerVarray       bvals = NULL;
2012                         int             ret, stop;
2013
2014                         /* this case works different from the others above.
2015                          * since aci's themselves give permissions, we need
2016                          * to first check b->a_access_mask, the ACL's access level.
2017                          */
2018
2019                         if ( BER_BVISEMPTY( &e->e_nname ) ) {
2020                                 /* no ACIs in the root DSE */
2021                                 continue;
2022                         }
2023
2024                         /* first check if the right being requested
2025                          * is allowed by the ACL clause.
2026                          */
2027                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
2028                                 continue;
2029                         }
2030                         /* start out with nothing granted, nothing denied */
2031                         ACL_INIT(tgrant);
2032                         ACL_INIT(tdeny);
2033
2034                         /* get the aci attribute */
2035                         at = attr_find( e->e_attrs, b->a_aci_at );
2036                         if ( at != NULL ) {
2037 #if 0
2038                                 /* FIXME: this breaks acl caching;
2039                                  * see also ACL_RECORD_VALUE_STATE below */
2040                                 ACL_RECORD_VALUE_STATE;
2041 #endif
2042                                 /* the aci is an multi-valued attribute.  The
2043                                 * rights are determined by OR'ing the individual
2044                                 * rights given by the acis.
2045                                 */
2046                                 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2047                                         if (aci_mask( op,
2048                                                 e, desc, val,
2049                                                 &at->a_nvals[i],
2050                                                 nmatch, matches,
2051                                                 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0)
2052                                         {
2053                                                 tgrant |= grant;
2054                                                 tdeny |= deny;
2055                                         }
2056                                 }
2057                                 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2058                                           accessmask2str(tgrant, accessmaskbuf, 1), 
2059                                           accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2060
2061                         }
2062                         /* If the entry level aci didn't contain anything valid for the 
2063                          * current operation, climb up the tree and evaluate the
2064                          * acis with scope set to subtree
2065                          */
2066                         if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
2067                                 dnParent( &e->e_nname, &parent_ndn );
2068                                 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
2069                                         Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
2070                                         ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
2071                                         switch(ret){
2072                                         case LDAP_SUCCESS :
2073                                                 stop = 0;
2074                                                 if (!bvals){
2075                                                         break;
2076                                                 }
2077
2078                                                 for( i = 0; bvals[i].bv_val != NULL; i++){
2079 #if 0
2080                                                         /* FIXME: this breaks acl caching;
2081                                                          * see also ACL_RECORD_VALUE_STATE above */
2082                                                         ACL_RECORD_VALUE_STATE;
2083 #endif
2084                                                         if (aci_mask(op, e, desc, val, &bvals[i],
2085                                                                         nmatch, matches,
2086                                                                         &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2087                                                         {
2088                                                                 tgrant |= grant;
2089                                                                 tdeny |= deny;
2090                                                                 /* evaluation stops as soon as either a "deny" or a 
2091                                                                  * "grant" directive matches.
2092                                                                  */
2093                                                                 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
2094                                                                         stop = 1;
2095                                                                 }
2096                                                         }
2097                                                         Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n", 
2098                                                                 accessmask2str(tgrant, accessmaskbuf, 1),
2099                                                                 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2100                                                 }
2101                                                 break;
2102
2103                                         case LDAP_NO_SUCH_ATTRIBUTE:
2104                                                 /* just go on if the aci-Attribute is not present in
2105                                                  * the current entry 
2106                                                  */
2107                                                 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
2108                                                 stop = 0;
2109                                                 break;
2110
2111                                         case LDAP_NO_SUCH_OBJECT:
2112                                                 /* We have reached the base object */
2113                                                 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
2114                                                 stop = 1;
2115                                                 break;
2116
2117                                         default:
2118                                                 stop = 1;
2119                                                 break;
2120                                         }
2121                                         if (stop){
2122                                                 break;
2123                                         }
2124                                         dnParent( &parent_ndn, &parent_ndn );
2125                                 }
2126                         }
2127
2128
2129                         /* remove anything that the ACL clause does not allow */
2130                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
2131                         tdeny &= ACL_PRIV_MASK;
2132
2133                         /* see if we have anything to contribute */
2134                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
2135                                 continue;
2136                         }
2137
2138                         /* this could be improved by changing slap_acl_mask so that it can deal with
2139                          * by clauses that return grant/deny pairs.  Right now, it does either
2140                          * additive or subtractive rights, but not both at the same time.  So,
2141                          * we need to combine the grant/deny pair into a single rights mask in
2142                          * a smart way:  if either grant or deny is "empty", then we use the
2143                          * opposite as is, otherwise we remove any denied rights from the grant
2144                          * rights mask and construct an additive mask.
2145                          */
2146                         if (ACL_IS_INVALID(tdeny)) {
2147                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
2148
2149                         } else if (ACL_IS_INVALID(tgrant)) {
2150                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2151
2152                         } else {
2153                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2154                         }
2155
2156                 } else
2157 #endif /* SLAPD_ACI_ENABLED */
2158 #endif /* !SLAP_DYNACL */
2159                 {
2160                         modmask = b->a_access_mask;
2161                 }
2162
2163                 Debug( LDAP_DEBUG_ACL,
2164                         "<= acl_mask: [%d] applying %s (%s)\n",
2165                         i, accessmask2str( modmask, accessmaskbuf, 1 ), 
2166                         b->a_type == ACL_CONTINUE
2167                                 ? "continue"
2168                                 : b->a_type == ACL_BREAK
2169                                         ? "break"
2170                                         : "stop" );
2171                 /* save old mask */
2172                 oldmask = *mask;
2173
2174                 if( ACL_IS_ADDITIVE(modmask) ) {
2175                         /* add privs */
2176                         ACL_PRIV_SET( *mask, modmask );
2177
2178                         /* cleanup */
2179                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2180
2181                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
2182                         /* substract privs */
2183                         ACL_PRIV_CLR( *mask, modmask );
2184
2185                         /* cleanup */
2186                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2187
2188                 } else {
2189                         /* assign privs */
2190                         *mask = modmask;
2191                 }
2192
2193                 Debug( LDAP_DEBUG_ACL,
2194                         "<= acl_mask: [%d] mask: %s\n",
2195                         i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
2196
2197                 if( b->a_type == ACL_CONTINUE ) {
2198                         continue;
2199
2200                 } else if ( b->a_type == ACL_BREAK ) {
2201                         return ACL_BREAK;
2202
2203                 } else {
2204                         return ACL_STOP;
2205                 }
2206         }
2207
2208         /* implicit "by * none" clause */
2209         ACL_INIT(*mask);
2210
2211         Debug( LDAP_DEBUG_ACL,
2212                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
2213                 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
2214         return ACL_STOP;
2215 }
2216
2217 /*
2218  * acl_check_modlist - check access control on the given entry to see if
2219  * it allows the given modifications by the user associated with op.
2220  * returns      1       if mods allowed ok
2221  *              0       mods not allowed
2222  */
2223
2224 int
2225 acl_check_modlist(
2226         Operation       *op,
2227         Entry   *e,
2228         Modifications   *mlist
2229 )
2230 {
2231         struct berval *bv;
2232         AccessControlState state = ACL_STATE_INIT;
2233         Backend *be;
2234         int be_null = 0;
2235         int ret = 1; /* default is access allowed */
2236
2237         be = op->o_bd;
2238         if ( be == NULL ) {
2239                 be = LDAP_STAILQ_FIRST(&backendDB);
2240                 be_null = 1;
2241                 op->o_bd = be;
2242         }
2243         assert( be != NULL );
2244
2245         /* short circuit root database access */
2246         if ( be_isroot( op ) ) {
2247                 Debug( LDAP_DEBUG_ACL,
2248                         "<= acl_access_allowed: granted to database root\n",
2249                     0, 0, 0 );
2250                 goto done;
2251         }
2252
2253         /* use backend default access if no backend acls */
2254         if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
2255                 Debug( LDAP_DEBUG_ACL,
2256                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
2257                         access2str( ACL_WRITE ),
2258                         op->o_bd->be_dfltaccess >= ACL_WRITE
2259                                 ? "granted" : "denied",
2260                         op->o_dn.bv_val );
2261                 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
2262                 goto done;
2263         }
2264
2265         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
2266                 /*
2267                  * Internal mods are ignored by ACL_WRITE checking
2268                  */
2269                 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
2270                         Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
2271                                 " modify access granted\n",
2272                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2273                         continue;
2274                 }
2275
2276                 /*
2277                  * no-user-modification operational attributes are ignored
2278                  * by ACL_WRITE checking as any found here are not provided
2279                  * by the user
2280                  */
2281                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
2282                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
2283                                 " modify access granted\n",
2284                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2285                         continue;
2286                 }
2287
2288                 switch ( mlist->sml_op ) {
2289                 case LDAP_MOD_REPLACE:
2290                         /*
2291                          * We must check both permission to delete the whole
2292                          * attribute and permission to add the specific attributes.
2293                          * This prevents abuse from selfwriters.
2294                          */
2295                         if ( ! access_allowed( op, e,
2296                                 mlist->sml_desc, NULL, ACL_WDEL, &state ) )
2297                         {
2298                                 ret = 0;
2299                                 goto done;
2300                         }
2301
2302                         if ( mlist->sml_values == NULL ) break;
2303
2304                         /* fall thru to check value to add */
2305
2306                 case LDAP_MOD_ADD:
2307                         assert( mlist->sml_values != NULL );
2308
2309                         for ( bv = mlist->sml_nvalues
2310                                         ? mlist->sml_nvalues : mlist->sml_values;
2311                                 bv->bv_val != NULL; bv++ )
2312                         {
2313                                 if ( ! access_allowed( op, e,
2314                                         mlist->sml_desc, bv, ACL_WADD, &state ) )
2315                                 {
2316                                         ret = 0;
2317                                         goto done;
2318                                 }
2319                         }
2320                         break;
2321
2322                 case LDAP_MOD_DELETE:
2323                         if ( mlist->sml_values == NULL ) {
2324                                 if ( ! access_allowed( op, e,
2325                                         mlist->sml_desc, NULL, ACL_WDEL, NULL ) )
2326                                 {
2327                                         ret = 0;
2328                                         goto done;
2329                                 }
2330                                 break;
2331                         }
2332                         for ( bv = mlist->sml_nvalues
2333                                         ? mlist->sml_nvalues : mlist->sml_values;
2334                                 bv->bv_val != NULL; bv++ )
2335                         {
2336                                 if ( ! access_allowed( op, e,
2337                                         mlist->sml_desc, bv, ACL_WDEL, &state ) )
2338                                 {
2339                                         ret = 0;
2340                                         goto done;
2341                                 }
2342                         }
2343                         break;
2344
2345                 case SLAP_MOD_SOFTADD:
2346                         /* allow adding attribute via modrdn thru */
2347                         break;
2348
2349                 default:
2350                         assert( 0 );
2351                         /* not reached */
2352                         ret = 0;
2353                         break;
2354                 }
2355         }
2356
2357 done:
2358         if (be_null) op->o_bd = NULL;
2359         return( ret );
2360 }
2361
2362 static int
2363 aci_get_part(
2364         struct berval   *list,
2365         int             ix,
2366         char            sep,
2367         struct berval   *bv )
2368 {
2369         int     len;
2370         char    *p;
2371
2372         if ( bv ) {
2373                 BER_BVZERO( bv );
2374         }
2375         len = list->bv_len;
2376         p = list->bv_val;
2377         while ( len >= 0 && --ix >= 0 ) {
2378                 while ( --len >= 0 && *p++ != sep )
2379                         ;
2380         }
2381         while ( len >= 0 && *p == ' ' ) {
2382                 len--;
2383                 p++;
2384         }
2385         if ( len < 0 ) {
2386                 return -1;
2387         }
2388
2389         if ( !bv ) {
2390                 return 0;
2391         }
2392
2393         bv->bv_val = p;
2394         while ( --len >= 0 && *p != sep ) {
2395                 bv->bv_len++;
2396                 p++;
2397         }
2398         while ( bv->bv_len > 0 && *--p == ' ' ) {
2399                 bv->bv_len--;
2400         }
2401         
2402         return bv->bv_len;
2403 }
2404
2405 typedef struct aci_set_gather_t {
2406         SetCookie               *cookie;
2407         BerVarray               bvals;
2408 } aci_set_gather_t;
2409
2410 static int
2411 aci_set_cb_gather( Operation *op, SlapReply *rs )
2412 {
2413         aci_set_gather_t        *p = (aci_set_gather_t *)op->o_callback->sc_private;
2414         
2415         if ( rs->sr_type == REP_SEARCH ) {
2416                 BerValue        bvals[ 2 ];
2417                 BerVarray       bvalsp = NULL;
2418                 int             j;
2419
2420                 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2421                         AttributeDescription    *desc = rs->sr_attrs[ j ].an_desc;
2422                         
2423                         if ( desc == slap_schema.si_ad_entryDN ) {
2424                                 bvalsp = bvals;
2425                                 bvals[ 0 ] = rs->sr_entry->e_nname;
2426                                 BER_BVZERO( &bvals[ 1 ] );
2427
2428                         } else {
2429                                 Attribute       *a;
2430
2431                                 a = attr_find( rs->sr_entry->e_attrs, desc );
2432                                 if ( a != NULL ) {
2433                                         int     i;
2434
2435                                         for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2436                                                 ;
2437
2438                                         bvalsp = a->a_nvals;
2439                                 }
2440                         }
2441                 }
2442
2443                 if ( bvals ) {
2444                         p->bvals = slap_set_join( p->cookie, p->bvals,
2445                                         ( '|' | SLAP_SET_RREF ), bvalsp );
2446                 }
2447
2448         } else {
2449                 assert( rs->sr_type == REP_RESULT );
2450         }
2451
2452         return 0;
2453 }
2454
2455 BerVarray
2456 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2457 {
2458         AciSetCookie            *cp = (AciSetCookie *)cookie;
2459         int                     rc = 0;
2460         LDAPURLDesc             *ludp = NULL;
2461         Operation               op2 = { 0 };
2462         SlapReply               rs = {REP_RESULT};
2463         AttributeName           anlist[ 2 ], *anlistp = NULL;
2464         int                     nattrs = 0;
2465         slap_callback           cb = { NULL, aci_set_cb_gather, NULL, NULL };
2466         aci_set_gather_t        p = { 0 };
2467         const char              *text = NULL;
2468         static struct berval    defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2469
2470         /* this routine needs to return the bervals instead of
2471          * plain strings, since syntax is not known.  It should
2472          * also return the syntax or some "comparison cookie".
2473          */
2474         if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2475                 return aci_set_gather2( cookie, name, desc );
2476         }
2477
2478         rc = ldap_url_parse( name->bv_val, &ludp );
2479         if ( rc != LDAP_URL_SUCCESS ) {
2480                 rc = LDAP_PROTOCOL_ERROR;
2481                 goto url_done;
2482         }
2483         
2484         if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2485         {
2486                 /* host part must be empty */
2487                 /* extensions parts must be empty */
2488                 rc = LDAP_PROTOCOL_ERROR;
2489                 goto url_done;
2490         }
2491
2492         /* Grab the searchbase and see if an appropriate database can be found */
2493         ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2494         rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2495                         &op2.o_req_ndn, cp->op->o_tmpmemctx );
2496         BER_BVZERO( &op2.o_req_dn );
2497         if ( rc != LDAP_SUCCESS ) {
2498                 goto url_done;
2499         }
2500
2501         op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2502         if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2503                 rc = LDAP_NO_SUCH_OBJECT;
2504                 goto url_done;
2505         }
2506
2507         /* Grab the filter */
2508         if ( ludp->lud_filter ) {
2509                 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2510                                 cp->op->o_tmpmemctx );
2511                 
2512         } else {
2513                 op2.ors_filterstr = defaultFilter_bv;
2514         }
2515
2516         op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
2517         if ( op2.ors_filter == NULL ) {
2518                 rc = LDAP_PROTOCOL_ERROR;
2519                 goto url_done;
2520         }
2521
2522         /* Grab the scope */
2523         op2.ors_scope = ludp->lud_scope;
2524
2525         /* Grap the attributes */
2526         if ( ludp->lud_attrs ) {
2527                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2528                         ;
2529
2530                 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2531                                 cp->op->o_tmpmemctx );
2532
2533                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2534                         ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2535                         anlistp[ nattrs ].an_desc = NULL;
2536                         rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2537                                         &anlistp[ nattrs ].an_desc, &text );
2538                         if ( rc != LDAP_SUCCESS ) {
2539                                 goto url_done;
2540                         }
2541                 }
2542
2543         } else {
2544                 anlistp = anlist;
2545         }
2546
2547         anlistp[ nattrs ].an_name = desc->ad_cname;
2548         anlistp[ nattrs ].an_desc = desc;
2549
2550         BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2551         
2552         p.cookie = cookie;
2553         
2554         op2.o_hdr = cp->op->o_hdr;
2555         op2.o_tag = LDAP_REQ_SEARCH;
2556         op2.o_ndn = op2.o_bd->be_rootndn;
2557         op2.o_callback = &cb;
2558         op2.o_time = slap_get_time();
2559         op2.o_do_not_cache = 1;
2560         op2.o_is_auth_check = 0;
2561         ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
2562         op2.ors_slimit = SLAP_NO_LIMIT;
2563         op2.ors_tlimit = SLAP_NO_LIMIT;
2564         op2.ors_attrs = anlistp;
2565         op2.ors_attrsonly = 0;
2566         op2.o_private = cp->op->o_private;
2567
2568         cb.sc_private = &p;
2569
2570         rc = op2.o_bd->be_search( &op2, &rs );
2571         if ( rc != 0 ) {
2572                 goto url_done;
2573         }
2574
2575 url_done:;
2576         if ( op2.ors_filter ) {
2577                 filter_free_x( cp->op, op2.ors_filter );
2578         }
2579         if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2580                 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2581         }
2582         if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2583                 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2584         }
2585         if ( ludp ) {
2586                 ldap_free_urldesc( ludp );
2587         }
2588         if ( anlistp && anlistp != anlist ) {
2589                 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2590         }
2591
2592         return p.bvals;
2593 }
2594
2595 BerVarray
2596 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2597 {
2598         AciSetCookie    *cp = (AciSetCookie *)cookie;
2599         BerVarray       bvals = NULL;
2600         struct berval   ndn;
2601         int             rc = 0;
2602
2603         /* this routine needs to return the bervals instead of
2604          * plain strings, since syntax is not known.  It should
2605          * also return the syntax or some "comparison cookie".
2606          */
2607         rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2608         if ( rc == LDAP_SUCCESS ) {
2609                 if ( desc == slap_schema.si_ad_entryDN ) {
2610                         bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2611                                         cp->op->o_tmpmemctx );
2612                         bvals[ 0 ] = ndn;
2613                         BER_BVZERO( &bvals[ 1 ] );
2614                         BER_BVZERO( &ndn );
2615
2616                 } else {
2617                         backend_attribute( cp->op,
2618                                 cp->e, &ndn, desc, &bvals, ACL_NONE );
2619                 }
2620
2621                 if ( !BER_BVISNULL( &ndn ) ) {
2622                         slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2623                 }
2624         }
2625
2626         return bvals;
2627 }
2628
2629 static int
2630 aci_match_set (
2631         struct berval *subj,
2632         Operation *op,
2633         Entry *e,
2634         int setref
2635 )
2636 {
2637         struct berval   set = BER_BVNULL;
2638         int             rc = 0;
2639         AciSetCookie    cookie;
2640
2641         if ( setref == 0 ) {
2642                 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2643
2644         } else {
2645                 struct berval           subjdn, ndn = BER_BVNULL;
2646                 struct berval           setat;
2647                 BerVarray               bvals;
2648                 const char              *text;
2649                 AttributeDescription    *desc = NULL;
2650
2651                 /* format of string is "entry/setAttrName" */
2652                 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2653                         return 0;
2654                 }
2655
2656                 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2657                         setat = aci_bv_set_attr;
2658                 }
2659
2660                 /*
2661                  * NOTE: dnNormalize honors the ber_len field
2662                  * as the length of the dn to be normalized
2663                  */
2664                 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2665                         if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2666                         {
2667                                 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2668                                 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2669                                         int     i;
2670
2671                                         set = bvals[0];
2672                                         BER_BVZERO( &bvals[0] );
2673                                         for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2674                                                 /* count */ ;
2675                                         bvals[0].bv_val = bvals[i-1].bv_val;
2676                                         BER_BVZERO( &bvals[i-1] );
2677                                 }
2678                                 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2679                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2680                         }
2681                 }
2682         }
2683
2684         if ( !BER_BVISNULL( &set ) ) {
2685                 cookie.op = op;
2686                 cookie.e = e;
2687                 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2688                         &op->o_ndn, &e->e_nname, NULL ) > 0 );
2689                 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2690         }
2691
2692         return(rc);
2693 }
2694
2695 #ifdef SLAPD_ACI_ENABLED
2696 static int
2697 aci_list_map_rights(
2698         struct berval *list )
2699 {
2700         struct berval bv;
2701         slap_access_t mask;
2702         int i;
2703
2704         ACL_INIT(mask);
2705         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2706                 if (bv.bv_len <= 0)
2707                         continue;
2708                 switch (*bv.bv_val) {
2709                 case 'c':
2710                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2711                         break;
2712                 case 's':
2713                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2714                          * the right 's' to mean "set", but in the examples states
2715                          * that the right 's' means "search".  The latter definition
2716                          * is used here.
2717                          */
2718                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2719                         break;
2720                 case 'r':
2721                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
2722                         break;
2723                 case 'w':
2724                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2725                         break;
2726                 case 'x':
2727                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
2728                          * define any equivalent to the AUTH right, so I've just used
2729                          * 'x' for now.
2730                          */
2731                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2732                         break;
2733                 default:
2734                         break;
2735                 }
2736
2737         }
2738         return(mask);
2739 }
2740
2741 static int
2742 aci_list_has_attr(
2743         struct berval *list,
2744         const struct berval *attr,
2745         struct berval *val )
2746 {
2747         struct berval bv, left, right;
2748         int i;
2749
2750         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2751                 if (aci_get_part(&bv, 0, '=', &left) < 0
2752                         || aci_get_part(&bv, 1, '=', &right) < 0)
2753                 {
2754                         if (ber_bvstrcasecmp(attr, &bv) == 0)
2755                                 return(1);
2756                 } else if (val == NULL) {
2757                         if (ber_bvstrcasecmp(attr, &left) == 0)
2758                                 return(1);
2759                 } else {
2760                         if (ber_bvstrcasecmp(attr, &left) == 0) {
2761                                 /* this is experimental code that implements a
2762                                  * simple (prefix) match of the attribute value.
2763                                  * the ACI draft does not provide for aci's that
2764                                  * apply to specific values, but it would be
2765                                  * nice to have.  If the <attr> part of an aci's
2766                                  * rights list is of the form <attr>=<value>,
2767                                  * that means the aci applies only to attrs with
2768                                  * the given value.  Furthermore, if the attr is
2769                                  * of the form <attr>=<value>*, then <value> is
2770                                  * treated as a prefix, and the aci applies to 
2771                                  * any value with that prefix.
2772                                  *
2773                                  * Ideally, this would allow r.e. matches.
2774                                  */
2775                                 if (aci_get_part(&right, 0, '*', &left) < 0
2776                                         || right.bv_len <= left.bv_len)
2777                                 {
2778                                         if (ber_bvstrcasecmp(val, &right) == 0)
2779                                                 return(1);
2780                                 } else if (val->bv_len >= left.bv_len) {
2781                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2782                                                 return(1);
2783                                 }
2784                         }
2785                 }
2786         }
2787         return(0);
2788 }
2789
2790 static slap_access_t
2791 aci_list_get_attr_rights(
2792         struct berval *list,
2793         const struct berval *attr,
2794         struct berval *val )
2795 {
2796     struct berval bv;
2797     slap_access_t mask;
2798     int i;
2799
2800         /* loop through each rights/attr pair, skip first part (action) */
2801         ACL_INIT(mask);
2802         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2803                 if (aci_list_has_attr(&bv, attr, val) == 0)
2804                         continue;
2805                 if (aci_get_part(list, i, ';', &bv) < 0)
2806                         continue;
2807                 mask |= aci_list_map_rights(&bv);
2808         }
2809         return(mask);
2810 }
2811
2812 static int
2813 aci_list_get_rights(
2814         struct berval *list,
2815         const struct berval *attr,
2816         struct berval *val,
2817         slap_access_t *grant,
2818         slap_access_t *deny )
2819 {
2820     struct berval perm, actn;
2821     slap_access_t *mask;
2822     int i, found;
2823
2824         if (attr == NULL || attr->bv_len == 0 
2825                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2826                 attr = &aci_bv_br_entry;
2827         }
2828
2829         found = 0;
2830         ACL_INIT(*grant);
2831         ACL_INIT(*deny);
2832         /* loop through each permissions clause */
2833         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2834                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2835                         continue;
2836                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2837                         mask = grant;
2838                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2839                         mask = deny;
2840                 } else {
2841                         continue;
2842                 }
2843
2844                 found = 1;
2845                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2846                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2847         }
2848         return(found);
2849 }
2850
2851 static int
2852 aci_group_member (
2853         struct berval   *subj,
2854         struct berval   *defgrpoc,
2855         struct berval   *defgrpat,
2856         Operation       *op,
2857         Entry           *e,
2858         int             nmatch,
2859         regmatch_t      *matches
2860 )
2861 {
2862         struct berval subjdn;
2863         struct berval grpoc;
2864         struct berval grpat;
2865         ObjectClass *grp_oc = NULL;
2866         AttributeDescription *grp_ad = NULL;
2867         const char *text;
2868         int rc;
2869
2870         /* format of string is "group/objectClassValue/groupAttrName" */
2871         if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2872                 return(0);
2873         }
2874
2875         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2876                 grpoc = *defgrpoc;
2877         }
2878
2879         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2880                 grpat = *defgrpat;
2881         }
2882
2883         rc = slap_bv2ad( &grpat, &grp_ad, &text );
2884         if( rc != LDAP_SUCCESS ) {
2885                 rc = 0;
2886                 goto done;
2887         }
2888         rc = 0;
2889
2890         grp_oc = oc_bvfind( &grpoc );
2891
2892         if (grp_oc != NULL && grp_ad != NULL ) {
2893                 char buf[ACL_BUF_SIZE];
2894                 struct berval bv, ndn;
2895                 bv.bv_len = sizeof( buf ) - 1;
2896                 bv.bv_val = (char *)&buf;
2897                 if ( string_expand(&bv, &subjdn,
2898                                 e->e_ndn, nmatch, matches) )
2899                 {
2900                         rc = LDAP_OTHER;
2901                         goto done;
2902                 }
2903                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2904                         rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2905                                 grp_oc, grp_ad ) == 0 );
2906                         slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2907                 }
2908         }
2909
2910 done:
2911         return(rc);
2912 }
2913
2914 static int
2915 aci_mask(
2916         Operation               *op,
2917         Entry                   *e,
2918         AttributeDescription    *desc,
2919         struct berval           *val,
2920         struct berval           *aci,
2921         int                     nmatch,
2922         regmatch_t              *matches,
2923         slap_access_t           *grant,
2924         slap_access_t           *deny,
2925         slap_aci_scope_t        asserted_scope
2926 )
2927 {
2928         struct berval           bv, scope, perms, type, sdn;
2929         int                     rc;
2930                 
2931
2932         assert( !BER_BVISNULL( &desc->ad_cname ) );
2933
2934         /* parse an aci of the form:
2935                 oid # scope # action;rights;attr;rights;attr 
2936                         $ action;rights;attr;rights;attr # type # subject
2937
2938            [NOTE: the following comment is very outdated,
2939            as the draft version it refers to (Ando, 2004-11-20)].
2940
2941            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2942            a full description of the format for this attribute.
2943            Differences: "this" in the draft is "self" here, and
2944            "self" and "public" is in the position of type.
2945
2946            <scope> = {entry|children|subtree}
2947            <type> = {public|users|access-id|subtree|onelevel|children|
2948                      self|dnattr|group|role|set|set-ref}
2949
2950            This routine now supports scope={ENTRY,CHILDREN}
2951            with the semantics:
2952              - ENTRY applies to "entry" and "subtree";
2953              - CHILDREN aplies to "children" and "subtree"
2954          */
2955
2956         /* check that the aci has all 5 components */
2957         if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2958                 return 0;
2959         }
2960
2961         /* check that the aci family is supported */
2962         if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2963                 return 0;
2964         }
2965
2966         /* check that the scope matches */
2967         if ( aci_get_part( aci, 1, '#', &scope ) < 0 ) {
2968                 return 0;
2969         }
2970
2971         /* note: scope can be either ENTRY or CHILDREN;
2972          * they respectively match "entry" and "children" in bv
2973          * both match "subtree" */
2974         switch ( asserted_scope ) {
2975         case SLAP_ACI_SCOPE_ENTRY:
2976                 if ( ber_bvstrcasecmp( &scope, &aci_bv_entry ) != 0
2977                                 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2978                 {
2979                         return 0;
2980                 }
2981                 break;
2982
2983         case SLAP_ACI_SCOPE_CHILDREN:
2984                 if ( ber_bvstrcasecmp( &scope, &aci_bv_children ) != 0
2985                                 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2986                 {
2987                         return 0;
2988                 }
2989                 break;
2990
2991         default:
2992                 return 0;
2993         }
2994
2995         /* get the list of permissions clauses, bail if empty */
2996         if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
2997                 return 0;
2998         }
2999
3000         /* check if any permissions allow desired access */
3001         if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
3002                 return 0;
3003         }
3004
3005         /* see if we have a DN match */
3006         if ( aci_get_part( aci, 3, '#', &type ) < 0 ) {
3007                 return 0;
3008         }
3009
3010         /* see if we have a public (i.e. anonymous) access */
3011         if ( ber_bvstrcasecmp( &aci_bv_public, &type ) == 0 ) {
3012                 return 1;
3013         }
3014         
3015         /* otherwise require an identity */
3016         if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
3017                 return 0;
3018         }
3019
3020         /* see if we have a users access */
3021         if ( ber_bvstrcasecmp( &aci_bv_users, &type ) == 0 ) {
3022                 return 1;
3023         }
3024         
3025         /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
3026          * just grab all the berval up to its end (ITS#3303).
3027          * NOTE: the problem could be solved by providing the DN with
3028          * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would 
3029          * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
3030 #if 0
3031         if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
3032                 return 0;
3033         }
3034 #endif
3035         sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
3036         sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
3037
3038         if ( ber_bvstrcasecmp( &aci_bv_access_id, &type ) == 0 ) {
3039                 struct berval ndn;
3040                 
3041                 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3042                 if ( rc != LDAP_SUCCESS ) {
3043                         return 0;
3044                 }
3045
3046                 if ( dn_match( &op->o_ndn, &ndn ) ) {
3047                         rc = 1;
3048                 }
3049                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3050
3051                 return rc;
3052
3053         } else if ( ber_bvstrcasecmp( &aci_bv_subtree, &type ) == 0 ) {
3054                 struct berval ndn;
3055                 
3056                 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3057                 if ( rc != LDAP_SUCCESS ) {
3058                         return 0;
3059                 }
3060
3061                 if ( dnIsSuffix( &op->o_ndn, &ndn ) ) {
3062                         rc = 1;
3063                 }
3064                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3065
3066                 return rc;
3067
3068         } else if ( ber_bvstrcasecmp( &aci_bv_onelevel, &type ) == 0 ) {
3069                 struct berval ndn, pndn;
3070                 
3071                 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3072                 if ( rc != LDAP_SUCCESS ) {
3073                         return 0;
3074                 }
3075
3076                 dnParent( &ndn, &pndn );
3077
3078                 if ( dn_match( &op->o_ndn, &pndn ) ) {
3079                         rc = 1;
3080                 }
3081                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3082
3083                 return rc;
3084
3085         } else if ( ber_bvstrcasecmp( &aci_bv_children, &type ) == 0 ) {
3086                 struct berval ndn;
3087                 
3088                 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
3089                 if ( rc != LDAP_SUCCESS ) {
3090                         return 0;
3091                 }
3092
3093                 if ( !dn_match( &op->o_ndn, &ndn )
3094                                 && dnIsSuffix( &op->o_ndn, &ndn ) )
3095                 {
3096                         rc = 1;
3097                 }
3098                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
3099
3100                 return rc;
3101
3102         } else if ( ber_bvstrcasecmp( &aci_bv_self, &type ) == 0 ) {
3103                 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
3104                         return 1;
3105                 }
3106
3107         } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &type ) == 0 ) {
3108                 Attribute               *at;
3109                 AttributeDescription    *ad = NULL;
3110                 const char              *text;
3111
3112                 rc = slap_bv2ad( &sdn, &ad, &text );
3113
3114                 if( rc != LDAP_SUCCESS ) {
3115                         return 0;
3116                 }
3117
3118                 rc = 0;
3119
3120                 for ( at = attrs_find( e->e_attrs, ad );
3121                                 at != NULL;
3122                                 at = attrs_find( at->a_next, ad ) )
3123                 {
3124                         if ( value_find_ex( ad,
3125                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
3126                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
3127                                 at->a_nvals,
3128                                 &op->o_ndn, op->o_tmpmemctx ) == 0 )
3129                         {
3130                                 rc = 1;
3131                                 break;
3132                         }
3133                 }
3134
3135                 return rc;
3136
3137         } else if ( ber_bvstrcasecmp( &aci_bv_group, &type ) == 0 ) {
3138                 if ( aci_group_member( &sdn, &aci_bv_group_class,
3139                                 &aci_bv_group_attr, op, e, nmatch, matches ) )
3140                 {
3141                         return 1;
3142                 }
3143
3144         } else if ( ber_bvstrcasecmp( &aci_bv_role, &type ) == 0 ) {
3145                 if ( aci_group_member( &sdn, &aci_bv_role_class,
3146                                 &aci_bv_role_attr, op, e, nmatch, matches ) )
3147                 {
3148                         return 1;
3149                 }
3150
3151         } else if ( ber_bvstrcasecmp( &aci_bv_set, &type ) == 0 ) {
3152                 if ( aci_match_set( &sdn, op, e, 0 ) ) {
3153                         return 1;
3154                 }
3155
3156         } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &type ) == 0 ) {
3157                 if ( aci_match_set( &sdn, op, e, 1 ) ) {
3158                         return 1;
3159                 }
3160         }
3161
3162         return 0;
3163 }
3164
3165 #ifdef SLAP_DYNACL
3166 /*
3167  * FIXME: there is a silly dependence that makes it difficult
3168  * to move ACIs in a run-time loadable module under the "dynacl" 
3169  * umbrella, because sets share some helpers with ACIs.
3170  */
3171 static int
3172 dynacl_aci_parse( const char *fname, int lineno, slap_style_t sty, const char *right, void **privp )
3173 {
3174         AttributeDescription    *ad = NULL;
3175         const char              *text = NULL;
3176
3177         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
3178                 fprintf( stderr, "%s: line %d: "
3179                         "inappropriate style \"%s\" in \"aci\" by clause\n",
3180                         fname, lineno, style_strings[sty] );
3181                 return -1;
3182         }
3183
3184         if ( right != NULL && *right != '\0' ) {
3185                 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
3186                         fprintf( stderr,
3187                                 "%s: line %d: aci \"%s\": %s\n",
3188                                 fname, lineno, right, text );
3189                         return -1;
3190                 }
3191
3192         } else {
3193                 ad = slap_schema.si_ad_aci;
3194         }
3195
3196         if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
3197                 fprintf( stderr, "%s: line %d: "
3198                         "aci \"%s\": inappropriate syntax: %s\n",
3199                         fname, lineno, right,
3200                         ad->ad_type->sat_syntax_oid );
3201                 return -1;
3202         }
3203
3204         *privp = (void *)ad;
3205
3206         return 0;
3207 }
3208
3209 static int
3210 dynacl_aci_unparse( void *priv, struct berval *bv )
3211 {
3212         AttributeDescription    *ad = ( AttributeDescription * )priv;
3213         char *ptr;
3214
3215         assert( ad != NULL );
3216
3217         bv->bv_val = ch_malloc( STRLENOF(" aci=") + ad->ad_cname.bv_len + 1 );
3218         ptr = lutil_strcopy( bv->bv_val, " aci=" );
3219         ptr = lutil_strcopy( ptr, ad->ad_cname.bv_val );
3220         bv->bv_len = ptr - bv->bv_val;
3221
3222         return 0;
3223 }
3224
3225
3226 static int
3227 dynacl_aci_mask(
3228                 void                    *priv,
3229                 Operation               *op,
3230                 Entry                   *e,
3231                 AttributeDescription    *desc,
3232                 struct berval           *val,
3233                 int                     nmatch,
3234                 regmatch_t              *matches,
3235                 slap_access_t           *grantp,
3236                 slap_access_t           *denyp )
3237 {
3238         AttributeDescription    *ad = ( AttributeDescription * )priv;
3239         Attribute               *at;
3240         slap_access_t           tgrant, tdeny, grant, deny;
3241 #ifdef LDAP_DEBUG
3242         char                    accessmaskbuf[ACCESSMASK_MAXLEN];
3243         char                    accessmaskbuf1[ACCESSMASK_MAXLEN];
3244 #endif /* LDAP_DEBUG */
3245
3246         /* start out with nothing granted, nothing denied */
3247         ACL_INIT(tgrant);
3248         ACL_INIT(tdeny);
3249
3250         /* get the aci attribute */
3251         at = attr_find( e->e_attrs, ad );
3252         if ( at != NULL ) {
3253                 int             i;
3254
3255                 /* the aci is an multi-valued attribute.  The
3256                  * rights are determined by OR'ing the individual
3257                  * rights given by the acis.
3258                  */
3259                 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
3260                         if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
3261                                         nmatch, matches, &grant, &deny,
3262                                         SLAP_ACI_SCOPE_ENTRY ) != 0 )
3263                         {
3264                                 tgrant |= grant;
3265                                 tdeny |= deny;
3266                         }
3267                 }
3268                 
3269                 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
3270                           accessmask2str( tgrant, accessmaskbuf, 1 ), 
3271                           accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3272         }
3273
3274         /* If the entry level aci didn't contain anything valid for the 
3275          * current operation, climb up the tree and evaluate the
3276          * acis with scope set to subtree
3277          */
3278         if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
3279                 struct berval   parent_ndn;
3280
3281 #if 1
3282                 /* to solve the chicken'n'egg problem of accessing
3283                  * the OpenLDAPaci attribute, the direct access
3284                  * to the entry's attribute is unchecked; however,
3285                  * further accesses to OpenLDAPaci values in the 
3286                  * ancestors occur through backend_attribute(), i.e.
3287                  * with the identity of the operation, requiring
3288                  * further access checking.  For uniformity, this
3289                  * makes further requests occur as the rootdn, if
3290                  * any, i.e. searching for the OpenLDAPaci attribute
3291                  * is considered an internal search.  If this is not
3292                  * acceptable, then the same check needs be performed
3293                  * when accessing the entry's attribute. */
3294                 Operation       op2 = *op;
3295
3296                 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
3297                         op2.o_dn = op->o_bd->be_rootdn;
3298                         op2.o_ndn = op->o_bd->be_rootndn;
3299                 }
3300 #endif
3301
3302                 dnParent( &e->e_nname, &parent_ndn );
3303                 while ( !BER_BVISEMPTY( &parent_ndn ) ){
3304                         int             i;
3305                         BerVarray       bvals = NULL;
3306                         int             ret, stop;
3307
3308                         Debug( LDAP_DEBUG_ACL, "checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
3309                         ret = backend_attribute( &op2, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
3310
3311                         switch ( ret ) {
3312                         case LDAP_SUCCESS :
3313                                 stop = 0;
3314                                 if ( !bvals ) {
3315                                         break;
3316                                 }
3317
3318                                 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++) {
3319                                         if ( aci_mask( op, e, desc, val,
3320                                                         &bvals[i],
3321                                                         nmatch, matches,
3322                                                         &grant, &deny,
3323                                                         SLAP_ACI_SCOPE_CHILDREN ) != 0 )
3324                                         {
3325                                                 tgrant |= grant;
3326                                                 tdeny |= deny;
3327                                                 /* evaluation stops as soon as either a "deny" or a 
3328                                                  * "grant" directive matches.
3329                                                  */
3330                                                 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
3331                                                         stop = 1;
3332                                                 }
3333                                         }
3334                                         Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n", 
3335                                                 accessmask2str( tgrant, accessmaskbuf, 1 ),
3336                                                 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
3337                                 }
3338                                 break;
3339
3340                         case LDAP_NO_SUCH_ATTRIBUTE:
3341                                 /* just go on if the aci-Attribute is not present in
3342                                  * the current entry 
3343                                  */
3344                                 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
3345                                 stop = 0;
3346                                 break;
3347
3348                         case LDAP_NO_SUCH_OBJECT:
3349                                 /* We have reached the base object */
3350                                 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
3351                                 stop = 1;
3352                                 break;
3353
3354                         default:
3355                                 stop = 1;
3356                                 break;
3357                         }
3358
3359                         if ( stop ) {
3360                                 break;
3361                         }
3362                         dnParent( &parent_ndn, &parent_ndn );
3363                 }
3364         }
3365
3366         *grantp = tgrant;
3367         *denyp = tdeny;
3368
3369         return 0;
3370 }
3371
3372 /* need to register this at some point */
3373 static slap_dynacl_t    dynacl_aci = {
3374         "aci",
3375         dynacl_aci_parse,
3376         dynacl_aci_unparse,
3377         dynacl_aci_mask,
3378         NULL,
3379         NULL,
3380         NULL
3381 };
3382
3383 #endif /* SLAP_DYNACL */
3384
3385 #endif  /* SLAPD_ACI_ENABLED */
3386
3387 #ifdef SLAP_DYNACL
3388
3389 /*
3390  * dynamic ACL infrastructure
3391  */
3392 static slap_dynacl_t    *da_list = NULL;
3393
3394 int
3395 slap_dynacl_register( slap_dynacl_t *da )
3396 {
3397         slap_dynacl_t   *tmp;
3398
3399         for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
3400                 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
3401                         break;
3402                 }
3403         }
3404
3405         if ( tmp != NULL ) {
3406                 return -1;
3407         }
3408         
3409         if ( da->da_mask == NULL ) {
3410                 return -1;
3411         }
3412         
3413         da->da_private = NULL;
3414         da->da_next = da_list;
3415         da_list = da;
3416
3417         return 0;
3418 }
3419
3420 static slap_dynacl_t *
3421 slap_dynacl_next( slap_dynacl_t *da )
3422 {
3423         if ( da ) {
3424                 return da->da_next;
3425         }
3426         return da_list;
3427 }
3428
3429 slap_dynacl_t *
3430 slap_dynacl_get( const char *name )
3431 {
3432         slap_dynacl_t   *da;
3433
3434         for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
3435                 if ( strcasecmp( da->da_name, name ) == 0 ) {
3436                         break;
3437                 }
3438         }
3439
3440         return da;
3441 }
3442 #endif /* SLAP_DYNACL */
3443
3444 int
3445 acl_init( void )
3446 {
3447         int             i, rc;
3448 #ifdef SLAP_DYNACL
3449         slap_dynacl_t   *known_dynacl[] = {
3450 #ifdef SLAPD_ACI_ENABLED
3451                 &dynacl_aci,
3452 #endif  /* SLAPD_ACI_ENABLED */
3453                 NULL
3454         };
3455
3456         for ( i = 0; known_dynacl[ i ]; i++ ) {
3457                 rc = slap_dynacl_register( known_dynacl[ i ] ); 
3458                 if ( rc ) {
3459                         return rc;
3460                 }
3461         }
3462 #endif /* SLAP_DYNACL */
3463
3464         return 0;
3465 }
3466
3467 static int
3468 string_expand(
3469         struct berval   *bv,
3470         struct berval   *pat,
3471         char            *match,
3472         int             nmatch,
3473         regmatch_t      *matches)
3474 {
3475         ber_len_t       size;
3476         char   *sp;
3477         char   *dp;
3478         int     flag;
3479
3480         size = 0;
3481         bv->bv_val[0] = '\0';
3482         bv->bv_len--; /* leave space for lone $ */
3483
3484         flag = 0;
3485         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
3486                 sp < pat->bv_val + pat->bv_len ; sp++ )
3487         {
3488                 /* did we previously see a $ */
3489                 if ( flag ) {
3490                         if ( flag == 1 && *sp == '$' ) {
3491                                 *dp++ = '$';
3492                                 size++;
3493                                 flag = 0;
3494
3495                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
3496                                 flag = 2;
3497
3498                         } else if ( *sp >= '0' && *sp <= '9' ) {
3499                                 int     n;
3500                                 int     i;
3501                                 int     l;
3502
3503                                 n = *sp - '0';
3504
3505                                 if ( flag == 2 ) {
3506                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
3507                                                 if ( *sp >= '0' && *sp <= '9' ) {
3508                                                         n = 10*n + ( *sp - '0' );
3509                                                 }
3510                                         }
3511
3512                                         if ( *sp != /*'{'*/ '}' ) {
3513                                                 /* FIXME: error */
3514                                                 return 1;
3515                                         }
3516                                 }
3517
3518                                 if ( n >= nmatch ) {
3519                                         /* FIXME: error */
3520                                         return 1;
3521                                 }
3522                                 
3523                                 *dp = '\0';
3524                                 i = matches[n].rm_so;
3525                                 l = matches[n].rm_eo; 
3526                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
3527                                         *dp++ = match[i];
3528                                 }
3529                                 *dp = '\0';
3530
3531                                 flag = 0;
3532                         }
3533                 } else {
3534                         if (*sp == '$') {
3535                                 flag = 1;
3536                         } else {
3537                                 *dp++ = *sp;
3538                                 size++;
3539                         }
3540                 }
3541         }
3542
3543         if ( flag ) {
3544                 /* must have ended with a single $ */
3545                 *dp++ = '$';
3546                 size++;
3547         }
3548
3549         *dp = '\0';
3550         bv->bv_len = size;
3551
3552         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
3553         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
3554
3555         return 0;
3556 }
3557
3558 static int
3559 regex_matches(
3560         struct berval   *pat,           /* pattern to expand and match against */
3561         char            *str,           /* string to match against pattern */
3562         char            *buf,           /* buffer with $N expansion variables */
3563         int             nmatch, /* size of the matches array */
3564         regmatch_t      *matches        /* offsets in buffer for $N expansion variables */
3565 )
3566 {
3567         regex_t re;
3568         char newbuf[ACL_BUF_SIZE];
3569         struct berval bv;
3570         int     rc;
3571
3572         bv.bv_len = sizeof( newbuf ) - 1;
3573         bv.bv_val = newbuf;
3574
3575         if (str == NULL) {
3576                 str = "";
3577         };
3578
3579         string_expand( &bv, pat, buf, nmatch, matches );
3580         rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
3581         if ( rc ) {
3582                 char error[ACL_BUF_SIZE];
3583                 regerror( rc, &re, error, sizeof( error ) );
3584
3585                 Debug( LDAP_DEBUG_TRACE,
3586                     "compile( \"%s\", \"%s\") failed %s\n",
3587                         pat->bv_val, str, error );
3588                 return( 0 );
3589         }
3590
3591         rc = regexec( &re, str, 0, NULL, 0 );
3592         regfree( &re );
3593
3594         Debug( LDAP_DEBUG_TRACE,
3595             "=> regex_matches: string:   %s\n", str, 0, 0 );
3596         Debug( LDAP_DEBUG_TRACE,
3597             "=> regex_matches: rc: %d %s\n",
3598                 rc, !rc ? "matches" : "no matches", 0 );
3599         return( !rc );
3600 }
3601