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