]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
ACIs almost entirely factored out of slapd
[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 const struct berval     aci_bv[] = {
46         BER_BVC("entry"),
47         BER_BVC("children"),
48         BER_BVC("onelevel"),
49         BER_BVC("subtree"),
50         BER_BVC("[entry]"),
51         BER_BVC("[all]"),
52         BER_BVC("access-id"),
53 #if 0
54         BER_BVC("anonymous"),
55 #endif
56         BER_BVC("public"),
57         BER_BVC("users"),
58         BER_BVC("self"),
59         BER_BVC("dnattr"),
60         BER_BVC("group"),
61         BER_BVC("role"),
62         BER_BVC("set"),
63         BER_BVC("set-ref"),
64         BER_BVC("grant"),
65         BER_BVC("deny"),
66
67         BER_BVC("IP="),
68 #ifdef LDAP_PF_LOCAL
69         BER_BVC("PATH="),
70 #if 0
71         BER_BVC(LDAP_DIRSEP),
72 #endif
73 #endif /* LDAP_PF_LOCAL */
74         
75         BER_BVC(SLAPD_GROUP_CLASS),
76         BER_BVC(SLAPD_GROUP_ATTR),
77         BER_BVC(SLAPD_ROLE_CLASS),
78         BER_BVC(SLAPD_ROLE_ATTR),
79
80         BER_BVC(SLAPD_ACI_SET_ATTR)
81 };
82
83 static AccessControl * slap_acl_get(
84         AccessControl *ac, int *count,
85         Operation *op, Entry *e,
86         AttributeDescription *desc,
87         struct berval *val,
88         int nmatch, regmatch_t *matches,
89         AccessControlState *state );
90
91 static slap_control_t slap_acl_mask(
92         AccessControl *ac, slap_mask_t *mask,
93         Operation *op, Entry *e,
94         AttributeDescription *desc,
95         struct berval *val,
96         int nmatch,
97         regmatch_t *matches,
98         int count,
99         AccessControlState *state );
100
101 static int      regex_matches(
102         struct berval *pat, char *str, char *buf,
103         int nmatch, regmatch_t *matches);
104
105 typedef struct AclSetCookie {
106         Operation *op;
107         Entry *e;
108 } AclSetCookie;
109
110 SLAP_SET_GATHER acl_set_gather;
111 SLAP_SET_GATHER acl_set_gather2;
112
113 /*
114  * access_allowed - check whether op->o_ndn is allowed the requested access
115  * to entry e, attribute attr, value val.  if val is null, access to
116  * the whole attribute is assumed (all values).
117  *
118  * This routine loops through all access controls and calls
119  * slap_acl_mask() on each applicable access control.
120  * The loop exits when a definitive answer is reached or
121  * or no more controls remain.
122  *
123  * returns:
124  *              0       access denied
125  *              1       access granted
126  *
127  * Notes:
128  * - can be legally called with op == NULL
129  * - can be legally called with op->o_bd == NULL
130  */
131
132 #ifdef SLAP_OVERLAY_ACCESS
133 int
134 slap_access_always_allowed(
135         Operation               *op,
136         Entry                   *e,
137         AttributeDescription    *desc,
138         struct berval           *val,
139         slap_access_t           access,
140         AccessControlState      *state,
141         slap_mask_t             *maskp )
142 {
143         assert( maskp != NULL );
144
145         ACL_PRIV_SET( *maskp, ACL_ACCESS2PRIV( access ) );
146
147         return 1;
148 }
149
150 int
151 slap_access_allowed(
152         Operation               *op,
153         Entry                   *e,
154         AttributeDescription    *desc,
155         struct berval           *val,
156         slap_access_t           access,
157         AccessControlState      *state,
158         slap_mask_t             *maskp )
159 {
160         int                             ret = 1;
161         int                             count;
162         AccessControl                   *a = NULL;
163
164 #ifdef LDAP_DEBUG
165         char                            accessmaskbuf[ACCESSMASK_MAXLEN];
166 #endif
167         slap_mask_t                     mask;
168         slap_control_t                  control;
169         slap_access_t                   access_level;
170         const char                      *attr;
171         regmatch_t                      matches[MAXREMATCHES];
172         int                             st_same_attr = 0;
173
174         assert( op != NULL );
175         assert( e != NULL );
176         assert( desc != NULL );
177         assert( maskp != NULL );
178
179         access_level = ACL_LEVEL( access );
180         attr = desc->ad_cname.bv_val;
181
182         assert( attr != NULL );
183
184         /* grant database root access */
185         if ( be_isroot( op ) ) {
186                 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
187                 mask = ACL_LVL_MANAGE;
188                 goto done;
189         }
190
191         /*
192          * no-user-modification operational attributes are ignored
193          * by ACL_WRITE checking as any found here are not provided
194          * by the user
195          */
196         if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
197                 && desc != slap_schema.si_ad_entry
198                 && desc != slap_schema.si_ad_children )
199         {
200                 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
201                         " %s access granted\n",
202                         attr, 0, 0 );
203                 goto done;
204         }
205
206         /* use backend default access if no backend acls */
207         if ( op->o_bd->be_acl == NULL ) {
208                 int     i;
209
210                 Debug( LDAP_DEBUG_ACL,
211                         "=> slap_access_allowed: backend default %s "
212                         "access %s to \"%s\"\n",
213                         access2str( access ),
214                         op->o_bd->be_dfltaccess >= access_level ? "granted" : "denied",
215                         op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
216                 ret = op->o_bd->be_dfltaccess >= access_level;
217
218                 mask = ACL_PRIV_LEVEL;
219                 for ( i = ACL_NONE; i <= op->o_bd->be_dfltaccess; i++ ) {
220                         ACL_PRIV_SET( mask, ACL_ACCESS2PRIV( i ) );
221                 }
222
223                 goto done;
224         }
225
226         ret = 0;
227         control = ACL_BREAK;
228
229         if ( st_same_attr ) {
230                 assert( state->as_vd_acl != NULL );
231
232                 a = state->as_vd_acl;
233                 count = state->as_vd_acl_count;
234                 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
235                         mask = state->as_vd_acl_mask;
236                         AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
237                         goto vd_access;
238                 }
239
240         } else {
241                 if ( state ) state->as_vi_acl = NULL;
242                 a = NULL;
243                 ACL_PRIV_ASSIGN( mask, *maskp );
244                 count = 0;
245                 memset( matches, '\0', sizeof( matches ) );
246         }
247
248         while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
249                 MAXREMATCHES, matches, state ) ) != NULL )
250         {
251                 int i;
252
253                 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
254                         Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
255                                 (int)matches[i].rm_so, (int)matches[i].rm_eo );
256                         if ( matches[i].rm_so <= matches[0].rm_eo ) {
257                                 int n;
258                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
259                                         Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
260                                 }
261                         }
262                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
263                 }
264
265                 if ( state ) {
266                         if ( state->as_vi_acl == a &&
267                                 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
268                         {
269                                 Debug( LDAP_DEBUG_ACL,
270                                         "=> slap_access_allowed: result from state (%s)\n",
271                                         attr, 0, 0 );
272                                 ret = state->as_result;
273                                 goto done;
274                         } else {
275                                 Debug( LDAP_DEBUG_ACL,
276                                         "=> slap_access_allowed: no res from state (%s)\n",
277                                         attr, 0, 0 );
278                         }
279                 }
280
281 vd_access:
282                 control = slap_acl_mask( a, &mask, op,
283                         e, desc, val, MAXREMATCHES, matches, count, state );
284
285                 if ( control != ACL_BREAK ) {
286                         break;
287                 }
288
289                 memset( matches, '\0', sizeof( matches ) );
290         }
291
292         if ( ACL_IS_INVALID( mask ) ) {
293                 Debug( LDAP_DEBUG_ACL,
294                         "=> slap_access_allowed: \"%s\" (%s) invalid!\n",
295                         e->e_dn, attr, 0 );
296                 ACL_PRIV_ASSIGN( mask, *maskp );
297
298         } else if ( control == ACL_BREAK ) {
299                 Debug( LDAP_DEBUG_ACL,
300                         "=> slap_access_allowed: no more rules\n", 0, 0, 0 );
301
302                 goto done;
303         }
304
305         ret = ACL_GRANT( mask, access );
306
307         Debug( LDAP_DEBUG_ACL,
308                 "=> slap_access_allowed: %s access %s by %s\n",
309                 access2str( access ), ret ? "granted" : "denied",
310                 accessmask2str( mask, accessmaskbuf, 1 ) );
311
312 done:
313         ACL_PRIV_ASSIGN( *maskp, mask );
314         return ret;
315 }
316
317 int
318 fe_access_allowed(
319         Operation               *op,
320         Entry                   *e,
321         AttributeDescription    *desc,
322         struct berval           *val,
323         slap_access_t           access,
324         AccessControlState      *state,
325         slap_mask_t             *maskp )
326 {
327         BackendDB               *be_orig;
328         int                     rc;
329
330         /*
331          * NOTE: control gets here if FIXME
332          * if an appropriate backend cannot be selected for the operation,
333          * we assume that the frontend should handle this
334          * FIXME: should select_backend() take care of this,
335          * and return frontendDB instead of NULL?  maybe for some value
336          * of the flags?
337          */
338         be_orig = op->o_bd;
339
340         op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
341         if ( op->o_bd == NULL ) {
342                 op->o_bd = frontendDB;
343         }
344         rc = slap_access_allowed( op, e, desc, val, access, state, maskp );
345         op->o_bd = be_orig;
346
347         return rc;
348 }
349
350 int
351 access_allowed_mask(
352         Operation               *op,
353         Entry                   *e,
354         AttributeDescription    *desc,
355         struct berval           *val,
356         slap_access_t           access,
357         AccessControlState      *state,
358         slap_mask_t             *maskp )
359 {
360         int                             ret = 1;
361         AccessControl                   *a = NULL;
362         int                             be_null = 0;
363
364 #ifdef LDAP_DEBUG
365         char                            accessmaskbuf[ACCESSMASK_MAXLEN];
366 #endif
367         slap_mask_t                     mask;
368         slap_access_t                   access_level;
369         const char                      *attr;
370         int                             st_same_attr = 0;
371         static AccessControlState       state_init = ACL_STATE_INIT;
372
373         assert( e != NULL );
374         assert( desc != NULL );
375
376         access_level = ACL_LEVEL( access );
377
378         assert( access_level > ACL_NONE );
379
380         ACL_INIT( mask );
381         if ( maskp ) ACL_INVALIDATE( *maskp );
382
383         attr = desc->ad_cname.bv_val;
384
385         assert( attr != NULL );
386
387         if ( op && op->o_is_auth_check &&
388                 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
389         {
390                 access = ACL_AUTH;
391         }
392
393         if ( state ) {
394                 if ( state->as_vd_ad == desc ) {
395                         if ( state->as_recorded ) {
396                                 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
397                                         val == NULL )
398                                 {
399                                         return state->as_result;
400
401                                 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
402                                         val != NULL && state->as_vd_acl == NULL )
403                                 {
404                                         return state->as_result;
405                                 }
406                         }
407                         st_same_attr = 1;
408                 } else {
409                         *state = state_init;
410                 }
411
412                 state->as_vd_ad = desc;
413         }
414
415         Debug( LDAP_DEBUG_ACL,
416                 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
417                 access2str( access ), e->e_dn, attr );
418
419         if ( op == NULL ) {
420                 /* no-op call */
421                 goto done;
422         }
423
424         if ( op->o_bd == NULL ) {
425                 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
426                 be_null = 1;
427
428 #ifdef LDAP_DEVEL
429                 /*
430                  * FIXME: experimental; use first backend rules
431                  * iff there is no global_acl (ITS#3100) */
432                 if ( frontendDB->be_acl != NULL ) {
433                         op->o_bd = frontendDB;
434                 }
435 #endif /* LDAP_DEVEL */
436         }
437         assert( op->o_bd != NULL );
438
439         /* this is enforced in backend_add() */
440         if ( op->o_bd->bd_info->bi_access_allowed ) {
441                 /* delegate to backend */
442                 ret = op->o_bd->bd_info->bi_access_allowed( op, e,
443                                 desc, val, access, state, &mask );
444
445         } else {
446                 BackendDB       *be_orig = op->o_bd;
447
448                 /* use default (but pass through frontend
449                  * for global ACL overlays) */
450                 op->o_bd = frontendDB;
451                 ret = frontendDB->bd_info->bi_access_allowed( op, e,
452                                 desc, val, access, state, &mask );
453                 op->o_bd = be_orig;
454         }
455
456         if ( !ret ) {
457                 if ( ACL_IS_INVALID( mask ) ) {
458                         Debug( LDAP_DEBUG_ACL,
459                                 "=> access_allowed: \"%s\" (%s) invalid!\n",
460                                 e->e_dn, attr, 0 );
461                         ACL_INIT( mask );
462
463                 } else {
464                         Debug( LDAP_DEBUG_ACL,
465                                 "=> access_allowed: no more rules\n", 0, 0, 0 );
466
467                         goto done;
468                 }
469         }
470
471         Debug( LDAP_DEBUG_ACL,
472                 "=> access_allowed: %s access %s by %s\n",
473                 access2str( access ), ret ? "granted" : "denied",
474                 accessmask2str( mask, accessmaskbuf, 1 ) );
475
476 done:
477         if ( state != NULL ) {
478                 /* If not value-dependent, save ACL in case of more attrs */
479                 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
480                         state->as_vi_acl = a;
481                         state->as_result = ret;
482                 }
483                 state->as_recorded |= ACL_STATE_RECORDED;
484         }
485         if ( be_null ) op->o_bd = NULL;
486         if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
487         return ret;
488 }
489
490 #else /* !SLAP_OVERLAY_ACCESS */
491
492 int
493 access_allowed_mask(
494         Operation               *op,
495         Entry                   *e,
496         AttributeDescription    *desc,
497         struct berval           *val,
498         slap_access_t           access,
499         AccessControlState      *state,
500         slap_mask_t             *maskp )
501 {
502         int                             ret = 1;
503         int                             count;
504         AccessControl                   *a = NULL;
505         Backend                         *be;
506         int                             be_null = 0;
507
508 #ifdef LDAP_DEBUG
509         char                            accessmaskbuf[ACCESSMASK_MAXLEN];
510 #endif
511         slap_mask_t                     mask;
512         slap_control_t                  control;
513         slap_access_t                   access_level;
514         const char                      *attr;
515         regmatch_t                      matches[MAXREMATCHES];
516         int                             st_same_attr = 0;
517         static AccessControlState       state_init = ACL_STATE_INIT;
518
519         assert( e != NULL );
520         assert( desc != NULL );
521
522         access_level = ACL_LEVEL( access );
523
524         assert( access_level > ACL_NONE );
525         if ( maskp ) ACL_INVALIDATE( *maskp );
526
527         attr = desc->ad_cname.bv_val;
528
529         assert( attr != NULL );
530
531         if ( op && op->o_is_auth_check &&
532                 ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
533         {
534                 access = ACL_AUTH;
535         }
536
537         if ( state ) {
538                 if ( state->as_vd_ad == desc ) {
539                         if ( state->as_recorded ) {
540                                 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
541                                         val == NULL )
542                                 {
543                                         return state->as_result;
544
545                                 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
546                                         val != NULL && state->as_vd_acl == NULL )
547                                 {
548                                         return state->as_result;
549                                 }
550                         }
551                         st_same_attr = 1;
552                 } else {
553                         *state = state_init;
554                 }
555
556                 state->as_vd_ad=desc;
557         }
558
559         Debug( LDAP_DEBUG_ACL,
560                 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
561                 access2str( access ), e->e_dn, attr );
562
563         if ( op == NULL ) {
564                 /* no-op call */
565                 goto done;
566         }
567
568         be = op->o_bd;
569         if ( be == NULL ) {
570                 be = LDAP_STAILQ_FIRST(&backendDB);
571                 be_null = 1;
572 #ifdef LDAP_DEVEL
573                 /*
574                  * FIXME: experimental; use first backend rules
575                  * iff there is no global_acl (ITS#3100) */
576                 if ( frontendDB->be_acl == NULL ) 
577 #endif
578                 {
579                         op->o_bd = be;
580                 }
581         }
582         assert( be != NULL );
583
584         /* grant database root access */
585         if ( be_isroot( op ) ) {
586                 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
587                 if ( maskp ) {
588                         mask = ACL_LVL_MANAGE;
589                 }
590
591                 goto done;
592         }
593
594         /*
595          * no-user-modification operational attributes are ignored
596          * by ACL_WRITE checking as any found here are not provided
597          * by the user
598          */
599         if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
600                 && desc != slap_schema.si_ad_entry
601                 && desc != slap_schema.si_ad_children )
602         {
603                 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
604                         " %s access granted\n",
605                         attr, 0, 0 );
606                 goto done;
607         }
608
609         /* use backend default access if no backend acls */
610         if ( be->be_acl == NULL ) {
611                 Debug( LDAP_DEBUG_ACL,
612                         "=> access_allowed: backend default %s "
613                         "access %s to \"%s\"\n",
614                         access2str( access ),
615                         be->be_dfltaccess >= access_level ? "granted" : "denied",
616                         op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
617                 ret = be->be_dfltaccess >= access_level;
618
619                 if ( maskp ) {
620                         int     i;
621
622                         mask = ACL_PRIV_LEVEL;
623                         for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
624                                 mask |= ACL_ACCESS2PRIV( i );
625                         }
626                 }
627
628                 goto done;
629
630 #ifdef notdef
631         /* be is always non-NULL */
632         /* use global default access if no global acls */
633         } else if ( be == NULL && frontendDB->be_acl == NULL ) {
634                 Debug( LDAP_DEBUG_ACL,
635                         "=> access_allowed: global default %s access %s to \"%s\"\n",
636                         access2str( access ),
637                         frontendDB->be_dfltaccess >= access_level ?
638                                 "granted" : "denied", op->o_dn.bv_val );
639                 ret = frontendDB->be_dfltaccess >= access_level;
640
641                 if ( maskp ) {
642                         int     i;
643
644                         mask = ACL_PRIV_LEVEL;
645                         for ( i = ACL_NONE; i <= global_default_access; i++ ) {
646                                 mask |= ACL_ACCESS2PRIV( i );
647                         }
648                 }
649
650                 goto done;
651 #endif
652         }
653
654         ret = 0;
655         control = ACL_BREAK;
656
657         if ( st_same_attr ) {
658                 assert( state->as_vd_acl != NULL );
659
660                 a = state->as_vd_acl;
661                 count = state->as_vd_acl_count;
662                 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
663                         mask = state->as_vd_acl_mask;
664                         AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
665                         goto vd_access;
666                 }
667
668         } else {
669                 if ( state ) state->as_vi_acl = NULL;
670                 a = NULL;
671                 ACL_INIT(mask);
672                 count = 0;
673                 memset( matches, '\0', sizeof(matches) );
674         }
675
676         while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
677                 MAXREMATCHES, matches, state ) ) != NULL )
678         {
679                 int i;
680
681                 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
682                         Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
683                                 (int)matches[i].rm_so, (int)matches[i].rm_eo );
684                         if ( matches[i].rm_so <= matches[0].rm_eo ) {
685                                 int n;
686                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
687                                         Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
688                                 }
689                         }
690                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
691                 }
692
693                 if ( state ) {
694                         if ( state->as_vi_acl == a &&
695                                 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
696                         {
697                                 Debug( LDAP_DEBUG_ACL,
698                                         "access_allowed: result from state (%s)\n",
699                                         attr, 0, 0 );
700                                 ret = state->as_result;
701                                 goto done;
702                         } else {
703                                 Debug( LDAP_DEBUG_ACL,
704                                         "access_allowed: no res from state (%s)\n",
705                                         attr, 0, 0 );
706                         }
707                 }
708
709 vd_access:
710                 control = slap_acl_mask( a, &mask, op,
711                         e, desc, val, MAXREMATCHES, matches, count, state );
712
713                 if ( control != ACL_BREAK ) {
714                         break;
715                 }
716
717                 memset( matches, '\0', sizeof(matches) );
718         }
719
720         if ( ACL_IS_INVALID( mask ) ) {
721                 Debug( LDAP_DEBUG_ACL,
722                         "=> access_allowed: \"%s\" (%s) invalid!\n",
723                         e->e_dn, attr, 0 );
724                 ACL_INIT(mask);
725
726         } else if ( control == ACL_BREAK ) {
727                 Debug( LDAP_DEBUG_ACL,
728                         "=> access_allowed: no more rules\n", 0, 0, 0 );
729
730                 goto done;
731         }
732
733         Debug( LDAP_DEBUG_ACL,
734                 "=> access_allowed: %s access %s by %s\n",
735                 access2str( access ),
736                 ACL_GRANT(mask, access) ? "granted" : "denied",
737                 accessmask2str( mask, accessmaskbuf, 1 ) );
738
739         ret = ACL_GRANT(mask, access);
740
741 done:
742         if ( state != NULL ) {
743                 /* If not value-dependent, save ACL in case of more attrs */
744                 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
745                         state->as_vi_acl = a;
746                         state->as_result = ret;
747                 }
748                 state->as_recorded |= ACL_STATE_RECORDED;
749         }
750         if ( be_null ) op->o_bd = NULL;
751         if ( maskp ) *maskp = mask;
752         return ret;
753 }
754
755 #endif /* SLAP_OVERLAY_ACCESS */
756
757 /*
758  * slap_acl_get - return the acl applicable to entry e, attribute
759  * attr.  the acl returned is suitable for use in subsequent calls to
760  * acl_access_allowed().
761  */
762
763 static AccessControl *
764 slap_acl_get(
765         AccessControl *a,
766         int                     *count,
767         Operation       *op,
768         Entry           *e,
769         AttributeDescription *desc,
770         struct berval   *val,
771         int                     nmatch,
772         regmatch_t      *matches,
773         AccessControlState *state )
774 {
775         const char *attr;
776         int dnlen, patlen;
777         AccessControl *prev;
778
779         assert( e != NULL );
780         assert( count != NULL );
781         assert( desc != NULL );
782
783         attr = desc->ad_cname.bv_val;
784
785         assert( attr != NULL );
786
787         if( a == NULL ) {
788                 if( op->o_bd == NULL ) {
789                         a = frontendDB->be_acl;
790                 } else {
791                         a = op->o_bd->be_acl;
792                 }
793                 prev = NULL;
794
795                 assert( a != NULL );
796
797         } else {
798                 prev = a;
799                 a = a->acl_next;
800         }
801
802         dnlen = e->e_nname.bv_len;
803
804         for ( ; a != NULL; a = a->acl_next ) {
805                 (*count) ++;
806
807                 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
808                         if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
809                                 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n", 
810                                         *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
811                                 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
812                                         continue;
813
814                         } else {
815                                 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n", 
816                                         *count, a->acl_dn_pat.bv_val, 0 );
817                                 patlen = a->acl_dn_pat.bv_len;
818                                 if ( dnlen < patlen )
819                                         continue;
820
821                                 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
822                                         /* base dn -- entire object DN must match */
823                                         if ( dnlen != patlen )
824                                                 continue;
825
826                                 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
827                                         int     rdnlen = -1, sep = 0;
828
829                                         if ( dnlen <= patlen )
830                                                 continue;
831
832                                         if ( patlen > 0 ) {
833                                                 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
834                                                         continue;
835                                                 sep = 1;
836                                         }
837
838                                         rdnlen = dn_rdnlen( NULL, &e->e_nname );
839                                         if ( rdnlen != dnlen - patlen - sep )
840                                                 continue;
841
842                                 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
843                                         if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
844                                                 continue;
845
846                                 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
847                                         if ( dnlen <= patlen )
848                                                 continue;
849                                         if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
850                                                 continue;
851                                 }
852
853                                 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
854                                         continue;
855                         }
856
857                         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
858                                 *count, 0, 0 );
859                 }
860
861                 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
862                         matches[0].rm_so = matches[0].rm_eo = -1;
863                         continue;
864                 }
865
866                 /* Is this ACL only for a specific value? */
867                 if ( a->acl_attrval.bv_len ) {
868                         if ( val == NULL ) {
869                                 continue;
870                         }
871
872                         if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
873                                 state->as_recorded |= ACL_STATE_RECORDED_VD;
874                                 state->as_vd_acl = a;
875                                 state->as_vd_acl_count = *count;
876                                 state->as_vd_access = a->acl_access;
877                                 state->as_vd_access_count = 1;
878                                 ACL_INVALIDATE( state->as_vd_acl_mask );
879                         }
880
881                         if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
882                                 Debug( LDAP_DEBUG_ACL,
883                                         "acl_get: valpat %s\n",
884                                         a->acl_attrval.bv_val, 0, 0 );
885                                 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
886                                 {
887                                         continue;
888                                 }
889
890                         } else {
891                                 int match = 0;
892                                 const char *text;
893                                 Debug( LDAP_DEBUG_ACL,
894                                         "acl_get: val %s\n",
895                                         a->acl_attrval.bv_val, 0, 0 );
896         
897                                 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
898                                         if (value_match( &match, desc,
899                                                 /* desc->ad_type->sat_equality */ a->acl_attrval_mr, 0,
900                                                 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
901                                                         match )
902                                                 continue;
903                                         
904                                 } else {
905                                         int             patlen, vdnlen;
906         
907                                         patlen = a->acl_attrval.bv_len;
908                                         vdnlen = val->bv_len;
909         
910                                         if ( vdnlen < patlen )
911                                                 continue;
912         
913                                         if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
914                                                 if ( vdnlen > patlen )
915                                                         continue;
916         
917                                         } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
918                                                 int rdnlen = -1;
919         
920                                                 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
921                                                         continue;
922         
923                                                 rdnlen = dn_rdnlen( NULL, val );
924                                                 if ( rdnlen != vdnlen - patlen - 1 )
925                                                         continue;
926         
927                                         } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
928                                                 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
929                                                         continue;
930         
931                                         } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
932                                                 if ( vdnlen <= patlen )
933                                                         continue;
934         
935                                                 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
936                                                         continue;
937                                         }
938         
939                                         if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
940                                                 continue;
941                                 }
942                         }
943                 }
944
945                 if ( a->acl_filter != NULL ) {
946                         ber_int_t rc = test_filter( NULL, e, a->acl_filter );
947                         if ( rc != LDAP_COMPARE_TRUE ) {
948                                 continue;
949                         }
950                 }
951
952                 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
953                        *count, attr, 0);
954                 return a;
955         }
956
957         Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
958         return( NULL );
959 }
960
961 static int
962 acl_mask_dn(
963         Operation               *op,
964         Entry                   *e,
965         AccessControl           *a,
966         int                     nmatch,
967         regmatch_t              *matches,
968         slap_dn_access          *b,
969         struct berval           *opndn )
970 {
971         /*
972          * if access applies to the entry itself, and the
973          * user is bound as somebody in the same namespace as
974          * the entry, OR the given dn matches the dn pattern
975          */
976         /*
977          * NOTE: styles "anonymous", "users" and "self" 
978          * have been moved to enum slap_style_t, whose 
979          * value is set in a_dn_style; however, the string
980          * is maintaned in a_dn_pat.
981          */
982         if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
983                 if ( !BER_BVISEMPTY( opndn ) ) {
984                         return 1;
985                 }
986
987         } else if ( b->a_style == ACL_STYLE_USERS ) {
988                 if ( BER_BVISEMPTY( opndn ) ) {
989                         return 1;
990                 }
991
992         } else if ( b->a_style == ACL_STYLE_SELF ) {
993                 struct berval   ndn, selfndn;
994                 int             level;
995
996                 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
997                         return 1;
998                 }
999
1000                 level = b->a_self_level;
1001                 if ( level < 0 ) {
1002                         selfndn = *opndn;
1003                         ndn = e->e_nname;
1004                         level = -level;
1005
1006                 } else {
1007                         ndn = *opndn;
1008                         selfndn = e->e_nname;
1009                 }
1010
1011                 for ( ; level > 0; level-- ) {
1012                         if ( BER_BVISEMPTY( &ndn ) ) {
1013                                 break;
1014                         }
1015                         dnParent( &ndn, &ndn );
1016                 }
1017                         
1018                 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
1019                 {
1020                         return 1;
1021                 }
1022
1023         } else if ( b->a_style == ACL_STYLE_REGEX ) {
1024                 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
1025                         int             tmp_nmatch;
1026                         regmatch_t      tmp_matches[2],
1027                                         *tmp_matchesp = tmp_matches;
1028
1029                         int             rc = 0;
1030
1031                         switch ( a->acl_dn_style ) {
1032                         case ACL_STYLE_REGEX:
1033                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1034                                         tmp_matchesp = matches;
1035                                         tmp_nmatch = nmatch;
1036                                         break;
1037                                 }
1038                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1039
1040                         case ACL_STYLE_BASE:
1041                                 tmp_matches[0].rm_so = 0;
1042                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1043                                 tmp_nmatch = 1;
1044                                 break;
1045
1046                         case ACL_STYLE_ONE:
1047                         case ACL_STYLE_SUBTREE:
1048                         case ACL_STYLE_CHILDREN:
1049                                 tmp_matches[0].rm_so = 0;
1050                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1051                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1052                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1053                                 tmp_nmatch = 2;
1054                                 break;
1055
1056                         default:
1057                                 /* error */
1058                                 rc = 1;
1059                                 break;
1060                         }
1061
1062                         if ( rc ) {
1063                                 return 1;
1064                         }
1065
1066                         if ( !regex_matches( &b->a_pat, opndn->bv_val,
1067                                 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
1068                         {
1069                                 return 1;
1070                         }
1071                 }
1072
1073         } else {
1074                 struct berval   pat;
1075                 ber_len_t       patlen, odnlen;
1076                 int             got_match = 0;
1077
1078                 if ( e->e_dn == NULL )
1079                         return 1;
1080
1081                 if ( b->a_expand ) {
1082                         struct berval   bv;
1083                         char            buf[ACL_BUF_SIZE];
1084                         
1085                         int             tmp_nmatch;
1086                         regmatch_t      tmp_matches[2],
1087                                         *tmp_matchesp = tmp_matches;
1088
1089                         int             rc = 0;
1090
1091                         bv.bv_len = sizeof( buf ) - 1;
1092                         bv.bv_val = buf;
1093
1094                         switch ( a->acl_dn_style ) {
1095                         case ACL_STYLE_REGEX:
1096                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1097                                         tmp_matchesp = matches;
1098                                         tmp_nmatch = nmatch;
1099                                         break;
1100                                 }
1101                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1102
1103                         case ACL_STYLE_BASE:
1104                                 tmp_matches[0].rm_so = 0;
1105                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1106                                 tmp_nmatch = 1;
1107                                 break;
1108
1109                         case ACL_STYLE_ONE:
1110                         case ACL_STYLE_SUBTREE:
1111                         case ACL_STYLE_CHILDREN:
1112                                 tmp_matches[0].rm_so = 0;
1113                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1114                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1115                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1116                                 tmp_nmatch = 2;
1117                                 break;
1118
1119                         default:
1120                                 /* error */
1121                                 rc = 1;
1122                                 break;
1123                         }
1124
1125                         if ( rc ) {
1126                                 return 1;
1127                         }
1128
1129                         if ( acl_string_expand( &bv, &b->a_pat, 
1130                                         e->e_nname.bv_val,
1131                                         tmp_nmatch, tmp_matchesp ) )
1132                         {
1133                                 return 1;
1134                         }
1135                         
1136                         if ( dnNormalize(0, NULL, NULL, &bv,
1137                                         &pat, op->o_tmpmemctx )
1138                                         != LDAP_SUCCESS )
1139                         {
1140                                 /* did not expand to a valid dn */
1141                                 return 1;
1142                         }
1143
1144                 } else {
1145                         pat = b->a_pat;
1146                 }
1147
1148                 patlen = pat.bv_len;
1149                 odnlen = opndn->bv_len;
1150                 if ( odnlen < patlen ) {
1151                         goto dn_match_cleanup;
1152
1153                 }
1154
1155                 if ( b->a_style == ACL_STYLE_BASE ) {
1156                         /* base dn -- entire object DN must match */
1157                         if ( odnlen != patlen ) {
1158                                 goto dn_match_cleanup;
1159                         }
1160
1161                 } else if ( b->a_style == ACL_STYLE_ONE ) {
1162                         int rdnlen = -1;
1163
1164                         if ( odnlen <= patlen ) {
1165                                 goto dn_match_cleanup;
1166                         }
1167
1168                         if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1169                                 goto dn_match_cleanup;
1170                         }
1171
1172                         rdnlen = dn_rdnlen( NULL, opndn );
1173                         if ( rdnlen != odnlen - patlen - 1 ) {
1174                                 goto dn_match_cleanup;
1175                         }
1176
1177                 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
1178                         if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1179                                 goto dn_match_cleanup;
1180                         }
1181
1182                 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
1183                         if ( odnlen <= patlen ) {
1184                                 goto dn_match_cleanup;
1185                         }
1186
1187                         if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1188                                 goto dn_match_cleanup;
1189                         }
1190
1191                 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
1192                         int level;
1193                         struct berval ndn;
1194
1195                         if ( odnlen <= patlen ) {
1196                                 goto dn_match_cleanup;
1197                         }
1198
1199                         if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
1200                         {
1201                                 goto dn_match_cleanup;
1202                         }
1203                         
1204                         level = b->a_level;
1205                         ndn = *opndn;
1206                         for ( ; level > 0; level-- ) {
1207                                 if ( BER_BVISEMPTY( &ndn ) ) {
1208                                         goto dn_match_cleanup;
1209                                 }
1210                                 dnParent( &ndn, &ndn );
1211                                 if ( ndn.bv_len < patlen ) {
1212                                         goto dn_match_cleanup;
1213                                 }
1214                         }
1215                         
1216                         if ( ndn.bv_len != patlen ) {
1217                                 goto dn_match_cleanup;
1218                         }
1219                 }
1220
1221                 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
1222
1223 dn_match_cleanup:;
1224                 if ( pat.bv_val != b->a_pat.bv_val ) {
1225                         slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1226                 }
1227
1228                 if ( !got_match ) {
1229                         return 1;
1230                 }
1231         }
1232
1233         return 0;
1234 }
1235
1236 /*
1237  * Record value-dependent access control state
1238  */
1239 #define ACL_RECORD_VALUE_STATE do { \
1240                 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
1241                         state->as_recorded |= ACL_STATE_RECORDED_VD; \
1242                         state->as_vd_acl = a; \
1243                         AC_MEMCPY( state->as_vd_acl_matches, matches, \
1244                                 sizeof( state->as_vd_acl_matches )) ; \
1245                         state->as_vd_acl_count = count; \
1246                         state->as_vd_access = b; \
1247                         state->as_vd_access_count = i; \
1248                 } \
1249         } while( 0 )
1250
1251 static int
1252 acl_mask_dnattr(
1253         Operation               *op,
1254         Entry                   *e,
1255         struct berval           *val,
1256         AccessControl           *a,
1257         Access                  *b,
1258         int                     i,
1259         regmatch_t              *matches,
1260         int                     count,
1261         AccessControlState      *state,
1262         slap_dn_access          *bdn,
1263         struct berval           *opndn )
1264 {
1265         Attribute       *at;
1266         struct berval   bv;
1267         int             rc, match = 0;
1268         const char      *text;
1269         const char      *attr = bdn->a_at->ad_cname.bv_val;
1270
1271         assert( attr != NULL );
1272
1273         if ( BER_BVISEMPTY( opndn ) ) {
1274                 return 1;
1275         }
1276
1277         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1278         bv = *opndn;
1279
1280         /* see if asker is listed in dnattr */
1281         for ( at = attrs_find( e->e_attrs, bdn->a_at );
1282                 at != NULL;
1283                 at = attrs_find( at->a_next, bdn->a_at ) )
1284         {
1285                 if ( value_find_ex( bdn->a_at,
1286                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1287                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1288                         at->a_nvals,
1289                         &bv, op->o_tmpmemctx ) == 0 )
1290                 {
1291                         /* found it */
1292                         match = 1;
1293                         break;
1294                 }
1295         }
1296
1297         if ( match ) {
1298                 /* have a dnattr match. if this is a self clause then
1299                  * the target must also match the op dn.
1300                  */
1301                 if ( bdn->a_self ) {
1302                         /* check if the target is an attribute. */
1303                         if ( val == NULL ) return 1;
1304
1305                         /* target is attribute, check if the attribute value
1306                          * is the op dn.
1307                          */
1308                         rc = value_match( &match, bdn->a_at,
1309                                 bdn->a_at->ad_type->sat_equality, 0,
1310                                 val, &bv, &text );
1311                         /* on match error or no match, fail the ACL clause */
1312                         if ( rc != LDAP_SUCCESS || match != 0 )
1313                                 return 1;
1314                 }
1315
1316         } else {
1317                 /* no dnattr match, check if this is a self clause */
1318                 if ( ! bdn->a_self )
1319                         return 1;
1320
1321                 ACL_RECORD_VALUE_STATE;
1322                 
1323                 /* this is a self clause, check if the target is an
1324                  * attribute.
1325                  */
1326                 if ( val == NULL )
1327                         return 1;
1328
1329                 /* target is attribute, check if the attribute value
1330                  * is the op dn.
1331                  */
1332                 rc = value_match( &match, bdn->a_at,
1333                         bdn->a_at->ad_type->sat_equality, 0,
1334                         val, &bv, &text );
1335
1336                 /* on match error or no match, fail the ACL clause */
1337                 if ( rc != LDAP_SUCCESS || match != 0 )
1338                         return 1;
1339         }
1340
1341         return 0;
1342 }
1343
1344
1345 /*
1346  * slap_acl_mask - modifies mask based upon the given acl and the
1347  * requested access to entry e, attribute attr, value val.  if val
1348  * is null, access to the whole attribute is assumed (all values).
1349  *
1350  * returns      0       access NOT allowed
1351  *              1       access allowed
1352  */
1353
1354 static slap_control_t
1355 slap_acl_mask(
1356         AccessControl           *a,
1357         slap_mask_t             *mask,
1358         Operation               *op,
1359         Entry                   *e,
1360         AttributeDescription    *desc,
1361         struct berval           *val,
1362         int                     nmatch,
1363         regmatch_t              *matches,
1364         int                     count,
1365         AccessControlState      *state )
1366 {
1367         int             i;
1368         Access          *b;
1369 #ifdef LDAP_DEBUG
1370         char            accessmaskbuf[ACCESSMASK_MAXLEN];
1371 #if !defined( SLAP_DYNACL ) && defined( SLAPD_ACI_ENABLED )
1372         char            accessmaskbuf1[ACCESSMASK_MAXLEN];
1373 #endif /* !SLAP_DYNACL && SLAPD_ACI_ENABLED */
1374 #endif /* DEBUG */
1375         const char      *attr;
1376         slap_mask_t     a2pmask = ACL_ACCESS2PRIV( *mask );
1377
1378         assert( a != NULL );
1379         assert( mask != NULL );
1380         assert( desc != NULL );
1381
1382         attr = desc->ad_cname.bv_val;
1383
1384         assert( attr != NULL );
1385
1386         Debug( LDAP_DEBUG_ACL,
1387                 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1388                 e->e_dn, attr, 0 );
1389
1390         Debug( LDAP_DEBUG_ACL,
1391                 "=> acl_mask: to %s by \"%s\", (%s) \n",
1392                 val ? "value" : "all values",
1393                 op->o_ndn.bv_val ?  op->o_ndn.bv_val : "",
1394                 accessmask2str( *mask, accessmaskbuf, 1 ) );
1395
1396
1397         if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1398                 && state->as_vd_acl == a )
1399         {
1400                 b = state->as_vd_access;
1401                 i = state->as_vd_access_count;
1402
1403         } else {
1404                 b = a->acl_access;
1405                 i = 1;
1406         }
1407
1408         for ( ; b != NULL; b = b->a_next, i++ ) {
1409                 slap_mask_t oldmask, modmask;
1410
1411                 ACL_INVALIDATE( modmask );
1412
1413                 /* AND <who> clauses */
1414                 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1415                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1416                                 b->a_dn_pat.bv_val, 0, 0);
1417                         /*
1418                          * if access applies to the entry itself, and the
1419                          * user is bound as somebody in the same namespace as
1420                          * the entry, OR the given dn matches the dn pattern
1421                          */
1422                         /*
1423                          * NOTE: styles "anonymous", "users" and "self" 
1424                          * have been moved to enum slap_style_t, whose 
1425                          * value is set in a_dn_style; however, the string
1426                          * is maintaned in a_dn_pat.
1427                          */
1428
1429                         if ( acl_mask_dn( op, e, a, nmatch, matches,
1430                                 &b->a_dn, &op->o_ndn ) )
1431                         {
1432                                 continue;
1433                         }
1434                 }
1435
1436                 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1437                         struct berval   ndn;
1438
1439                         Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1440                                 b->a_realdn_pat.bv_val, 0, 0);
1441                         /*
1442                          * if access applies to the entry itself, and the
1443                          * user is bound as somebody in the same namespace as
1444                          * the entry, OR the given dn matches the dn pattern
1445                          */
1446                         /*
1447                          * NOTE: styles "anonymous", "users" and "self" 
1448                          * have been moved to enum slap_style_t, whose 
1449                          * value is set in a_dn_style; however, the string
1450                          * is maintaned in a_dn_pat.
1451                          */
1452
1453                         if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1454                         {
1455                                 ndn = op->o_conn->c_ndn;
1456                         } else {
1457                                 ndn = op->o_ndn;
1458                         }
1459
1460                         if ( acl_mask_dn( op, e, a, nmatch, matches,
1461                                 &b->a_realdn, &ndn ) )
1462                         {
1463                                 continue;
1464                         }
1465                 }
1466
1467                 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1468                         if ( ! op->o_conn->c_listener ) {
1469                                 continue;
1470                         }
1471                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1472                                 b->a_sockurl_pat.bv_val, 0, 0 );
1473
1474                         if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1475                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1476                                         if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1477                                                         e->e_ndn, nmatch, matches ) ) 
1478                                         {
1479                                                 continue;
1480                                         }
1481
1482                                 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1483                                         struct berval   bv;
1484                                         char buf[ACL_BUF_SIZE];
1485
1486                                         bv.bv_len = sizeof( buf ) - 1;
1487                                         bv.bv_val = buf;
1488                                         if ( acl_string_expand( &bv, &b->a_sockurl_pat,
1489                                                         e->e_ndn, nmatch, matches ) )
1490                                         {
1491                                                 continue;
1492                                         }
1493
1494                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1495                                         {
1496                                                 continue;
1497                                         }
1498
1499                                 } else {
1500                                         if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1501                                         {
1502                                                 continue;
1503                                         }
1504                                 }
1505                         }
1506                 }
1507
1508                 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1509                         if ( !op->o_conn->c_peer_domain.bv_val ) {
1510                                 continue;
1511                         }
1512                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1513                                 b->a_domain_pat.bv_val, 0, 0 );
1514                         if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1515                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1516                                         if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1517                                                         e->e_ndn, nmatch, matches ) ) 
1518                                         {
1519                                                 continue;
1520                                         }
1521                                 } else {
1522                                         char buf[ACL_BUF_SIZE];
1523
1524                                         struct berval   cmp = op->o_conn->c_peer_domain;
1525                                         struct berval   pat = b->a_domain_pat;
1526
1527                                         if ( b->a_domain_expand ) {
1528                                                 struct berval bv;
1529
1530                                                 bv.bv_len = sizeof(buf) - 1;
1531                                                 bv.bv_val = buf;
1532
1533                                                 if ( acl_string_expand(&bv, &b->a_domain_pat,
1534                                                                 e->e_ndn, nmatch, matches) )
1535                                                 {
1536                                                         continue;
1537                                                 }
1538                                                 pat = bv;
1539                                         }
1540
1541                                         if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1542                                                 int offset = cmp.bv_len - pat.bv_len;
1543                                                 if ( offset < 0 ) {
1544                                                         continue;
1545                                                 }
1546
1547                                                 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1548                                                         continue;
1549                                                 }
1550
1551                                                 /* trim the domain */
1552                                                 cmp.bv_val = &cmp.bv_val[ offset ];
1553                                                 cmp.bv_len -= offset;
1554                                         }
1555                                         
1556                                         if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1557                                                 continue;
1558                                         }
1559                                 }
1560                         }
1561                 }
1562
1563                 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1564                         if ( !op->o_conn->c_peer_name.bv_val ) {
1565                                 continue;
1566                         }
1567                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1568                                 b->a_peername_pat.bv_val, 0, 0 );
1569                         if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1570                                 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1571                                         if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1572                                                         e->e_ndn, nmatch, matches ) ) 
1573                                         {
1574                                                 continue;
1575                                         }
1576
1577                                 } else {
1578                                         /* try exact match */
1579                                         if ( b->a_peername_style == ACL_STYLE_BASE ) {
1580                                                 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1581                                                         continue;
1582                                                 }
1583
1584                                         } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1585                                                 struct berval   bv;
1586                                                 char buf[ACL_BUF_SIZE];
1587
1588                                                 bv.bv_len = sizeof( buf ) - 1;
1589                                                 bv.bv_val = buf;
1590                                                 if ( acl_string_expand( &bv, &b->a_peername_pat,
1591                                                                 e->e_ndn, nmatch, matches ) )
1592                                                 {
1593                                                         continue;
1594                                                 }
1595
1596                                                 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1597                                                         continue;
1598                                                 }
1599
1600                                         /* extract IP and try exact match */
1601                                         } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1602                                                 char            *port;
1603                                                 char            buf[] = "255.255.255.255";
1604                                                 struct berval   ip;
1605                                                 unsigned long   addr;
1606                                                 int             port_number = -1;
1607                                                 
1608                                                 if ( strncasecmp( op->o_conn->c_peer_name.bv_val, 
1609                                                                         aci_bv[ ACI_BV_IP_EQ ].bv_val,
1610                                                                         aci_bv[ ACI_BV_IP_EQ ].bv_len ) != 0 ) 
1611                                                         continue;
1612
1613                                                 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv[ ACI_BV_IP_EQ ].bv_len;
1614                                                 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv[ ACI_BV_IP_EQ ].bv_len;
1615
1616                                                 port = strrchr( ip.bv_val, ':' );
1617                                                 if ( port ) {
1618                                                         char    *next;
1619                                                         
1620                                                         ip.bv_len = port - ip.bv_val;
1621                                                         ++port;
1622                                                         port_number = strtol( port, &next, 10 );
1623                                                         if ( next[0] != '\0' )
1624                                                                 continue;
1625                                                 }
1626                                                 
1627                                                 /* the port check can be anticipated here */
1628                                                 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1629                                                         continue;
1630                                                 
1631                                                 /* address longer than expected? */
1632                                                 if ( ip.bv_len >= sizeof(buf) )
1633                                                         continue;
1634
1635                                                 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1636                                                 buf[ ip.bv_len ] = '\0';
1637
1638                                                 addr = inet_addr( buf );
1639
1640                                                 /* unable to convert? */
1641                                                 if ( addr == (unsigned long)(-1) )
1642                                                         continue;
1643
1644                                                 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1645                                                         continue;
1646
1647 #ifdef LDAP_PF_LOCAL
1648                                         /* extract path and try exact match */
1649                                         } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1650                                                 struct berval path;
1651                                                 
1652                                                 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1653                                                                         aci_bv[ ACI_BV_PATH_EQ ].bv_val,
1654                                                                         aci_bv[ ACI_BV_PATH_EQ ].bv_len ) != 0 )
1655                                                         continue;
1656
1657                                                 path.bv_val = op->o_conn->c_peer_name.bv_val
1658                                                         + aci_bv[ ACI_BV_PATH_EQ ].bv_len;
1659                                                 path.bv_len = op->o_conn->c_peer_name.bv_len
1660                                                         - aci_bv[ ACI_BV_PATH_EQ ].bv_len;
1661
1662                                                 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1663                                                         continue;
1664
1665 #endif /* LDAP_PF_LOCAL */
1666
1667                                         /* exact match (very unlikely...) */
1668                                         } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1669                                                         continue;
1670                                         }
1671                                 }
1672                         }
1673                 }
1674
1675                 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1676                         if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1677                                 continue;
1678                         }
1679                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1680                                 b->a_sockname_pat.bv_val, 0, 0 );
1681                         if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1682                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1683                                         if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1684                                                         e->e_ndn, nmatch, matches ) ) 
1685                                         {
1686                                                 continue;
1687                                         }
1688
1689                                 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1690                                         struct berval   bv;
1691                                         char buf[ACL_BUF_SIZE];
1692
1693                                         bv.bv_len = sizeof( buf ) - 1;
1694                                         bv.bv_val = buf;
1695                                         if ( acl_string_expand( &bv, &b->a_sockname_pat,
1696                                                         e->e_ndn, nmatch, matches ) )
1697                                         {
1698                                                 continue;
1699                                         }
1700
1701                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1702                                                 continue;
1703                                         }
1704
1705                                 } else {
1706                                         if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1707                                                 continue;
1708                                         }
1709                                 }
1710                         }
1711                 }
1712
1713                 if ( b->a_dn_at != NULL ) {
1714                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1715                                         matches, count, state,
1716                                         &b->a_dn, &op->o_ndn ) )
1717                         {
1718                                 continue;
1719                         }
1720                 }
1721
1722                 if ( b->a_realdn_at != NULL ) {
1723                         struct berval   ndn;
1724
1725                         if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1726                         {
1727                                 ndn = op->o_conn->c_ndn;
1728                         } else {
1729                                 ndn = op->o_ndn;
1730                         }
1731
1732                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1733                                         matches, count, state,
1734                                         &b->a_realdn, &ndn ) )
1735                         {
1736                                 continue;
1737                         }
1738                 }
1739
1740                 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1741                         struct berval bv;
1742                         struct berval ndn = BER_BVNULL;
1743                         int rc;
1744
1745                         if ( op->o_ndn.bv_len == 0 ) {
1746                                 continue;
1747                         }
1748
1749                         Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1750                                 b->a_group_pat.bv_val, 0, 0 );
1751
1752                         /* b->a_group is an unexpanded entry name, expanded it should be an 
1753                          * entry with objectclass group* and we test to see if odn is one of
1754                          * the values in the attribute group
1755                          */
1756                         /* see if asker is listed in dnattr */
1757                         if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1758                                 char            buf[ACL_BUF_SIZE];
1759                                 int             tmp_nmatch;
1760                                 regmatch_t      tmp_matches[2],
1761                                                 *tmp_matchesp = tmp_matches;
1762
1763                                 bv.bv_len = sizeof(buf) - 1;
1764                                 bv.bv_val = buf;
1765
1766                                 rc = 0;
1767
1768                                 switch ( a->acl_dn_style ) {
1769                                 case ACL_STYLE_REGEX:
1770                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1771                                                 tmp_matchesp = matches;
1772                                                 tmp_nmatch = nmatch;
1773                                                 break;
1774                                         }
1775
1776                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1777                                 case ACL_STYLE_BASE:
1778                                         tmp_matches[0].rm_so = 0;
1779                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1780                                         tmp_nmatch = 1;
1781                                         break;
1782
1783                                 case ACL_STYLE_ONE:
1784                                 case ACL_STYLE_SUBTREE:
1785                                 case ACL_STYLE_CHILDREN:
1786                                         tmp_matches[0].rm_so = 0;
1787                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1788                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1789                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1790                                         tmp_nmatch = 2;
1791                                         break;
1792
1793                                 default:
1794                                         /* error */
1795                                         rc = 1;
1796                                         break;
1797                                 }
1798
1799                                 if ( rc ) {
1800                                         continue;
1801                                 }
1802                                 
1803                                 if ( acl_string_expand( &bv, &b->a_group_pat,
1804                                                 e->e_nname.bv_val,
1805                                                 tmp_nmatch, tmp_matchesp ) )
1806                                 {
1807                                         continue;
1808                                 }
1809
1810                                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1811                                                 op->o_tmpmemctx ) != LDAP_SUCCESS )
1812                                 {
1813                                         /* did not expand to a valid dn */
1814                                         continue;
1815                                 }
1816
1817                                 bv = ndn;
1818
1819                         } else {
1820                                 bv = b->a_group_pat;
1821                         }
1822
1823                         rc = backend_group( op, e, &bv, &op->o_ndn,
1824                                 b->a_group_oc, b->a_group_at );
1825
1826                         if ( ndn.bv_val ) {
1827                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1828                         }
1829
1830                         if ( rc != 0 ) {
1831                                 continue;
1832                         }
1833                 }
1834
1835                 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1836                         struct berval   bv;
1837                         char            buf[ACL_BUF_SIZE];
1838
1839                         Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1840                                 b->a_set_pat.bv_val, 0, 0 );
1841
1842                         if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1843                                 int             tmp_nmatch;
1844                                 regmatch_t      tmp_matches[2],
1845                                                 *tmp_matchesp = tmp_matches;
1846                                 int             rc = 0;
1847
1848                                 bv.bv_len = sizeof( buf ) - 1;
1849                                 bv.bv_val = buf;
1850
1851                                 rc = 0;
1852
1853                                 switch ( a->acl_dn_style ) {
1854                                 case ACL_STYLE_REGEX:
1855                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1856                                                 tmp_matchesp = matches;
1857                                                 tmp_nmatch = nmatch;
1858                                                 break;
1859                                         }
1860
1861                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1862                                 case ACL_STYLE_BASE:
1863                                         tmp_matches[0].rm_so = 0;
1864                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1865                                         tmp_nmatch = 1;
1866                                         break;
1867
1868                                 case ACL_STYLE_ONE:
1869                                 case ACL_STYLE_SUBTREE:
1870                                 case ACL_STYLE_CHILDREN:
1871                                         tmp_matches[0].rm_so = 0;
1872                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1873                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1874                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1875                                         tmp_nmatch = 2;
1876                                         break;
1877
1878                                 default:
1879                                         /* error */
1880                                         rc = 1;
1881                                         break;
1882                                 }
1883
1884                                 if ( rc ) {
1885                                         continue;
1886                                 }
1887                                 
1888                                 if ( acl_string_expand( &bv, &b->a_set_pat,
1889                                                 e->e_nname.bv_val,
1890                                                 tmp_nmatch, tmp_matchesp ) )
1891                                 {
1892                                         continue;
1893                                 }
1894
1895                         } else {
1896                                 bv = b->a_set_pat;
1897                         }
1898                         
1899                         if ( acl_match_set( &bv, op, e, 0 ) == 0 ) {
1900                                 continue;
1901                         }
1902                 }
1903
1904                 if ( b->a_authz.sai_ssf ) {
1905                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1906                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
1907                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
1908                                 continue;
1909                         }
1910                 }
1911
1912                 if ( b->a_authz.sai_transport_ssf ) {
1913                         Debug( LDAP_DEBUG_ACL,
1914                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1915                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1916                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
1917                                 continue;
1918                         }
1919                 }
1920
1921                 if ( b->a_authz.sai_tls_ssf ) {
1922                         Debug( LDAP_DEBUG_ACL,
1923                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1924                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1925                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
1926                                 continue;
1927                         }
1928                 }
1929
1930                 if ( b->a_authz.sai_sasl_ssf ) {
1931                         Debug( LDAP_DEBUG_ACL,
1932                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1933                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1934                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
1935                                 continue;
1936                         }
1937                 }
1938
1939 #ifdef SLAP_DYNACL
1940                 if ( b->a_dynacl ) {
1941                         slap_dynacl_t   *da;
1942                         slap_access_t   tgrant, tdeny;
1943
1944                         Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1945                                 0, 0, 0 );
1946
1947                         /* this case works different from the others above.
1948                          * since aci's themselves give permissions, we need
1949                          * to first check b->a_access_mask, the ACL's access level.
1950                          */
1951                         if ( BER_BVISEMPTY( &e->e_nname ) ) {
1952                                 /* no ACIs in the root DSE */
1953                                 continue;
1954                         }
1955
1956                         /* first check if the right being requested
1957                          * is allowed by the ACL clause.
1958                          */
1959                         if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1960                                 continue;
1961                         }
1962
1963                         /* start out with nothing granted, nothing denied */
1964                         ACL_INIT(tgrant);
1965                         ACL_INIT(tdeny);
1966
1967                         for ( da = b->a_dynacl; da; da = da->da_next ) {
1968                                 slap_access_t   grant,
1969                                                 deny;
1970
1971                                 ACL_INIT(grant);
1972                                 ACL_INIT(deny);
1973
1974                                 Debug( LDAP_DEBUG_ACL, "    <= check a_dynacl: %s\n",
1975                                         da->da_name, 0, 0 );
1976
1977                                 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1978
1979                                 tgrant |= grant;
1980                                 tdeny |= deny;
1981                         }
1982
1983                         /* remove anything that the ACL clause does not allow */
1984                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1985                         tdeny &= ACL_PRIV_MASK;
1986
1987                         /* see if we have anything to contribute */
1988                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
1989                                 continue;
1990                         }
1991
1992                         /* this could be improved by changing slap_acl_mask so that it can deal with
1993                          * by clauses that return grant/deny pairs.  Right now, it does either
1994                          * additive or subtractive rights, but not both at the same time.  So,
1995                          * we need to combine the grant/deny pair into a single rights mask in
1996                          * a smart way:  if either grant or deny is "empty", then we use the
1997                          * opposite as is, otherwise we remove any denied rights from the grant
1998                          * rights mask and construct an additive mask.
1999                          */
2000                         if (ACL_IS_INVALID(tdeny)) {
2001                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
2002
2003                         } else if (ACL_IS_INVALID(tgrant)) {
2004                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2005
2006                         } else {
2007                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2008                         }
2009
2010                 } else
2011 #else /* !SLAP_DYNACL */
2012
2013 #ifdef SLAPD_ACI_ENABLED
2014                 if ( b->a_aci_at != NULL ) {
2015                         Attribute       *at;
2016                         slap_access_t   grant, deny, tgrant, tdeny;
2017                         struct berval   parent_ndn;
2018                         BerVarray       bvals = NULL;
2019                         int             ret, stop;
2020
2021                         Debug( LDAP_DEBUG_ACL, "    <= check a_aci_at: %s\n",
2022                                 b->a_aci_at->ad_cname.bv_val, 0, 0 );
2023
2024                         /* this case works different from the others above.
2025                          * since aci's themselves give permissions, we need
2026                          * to first check b->a_access_mask, the ACL's access level.
2027                          */
2028
2029                         if ( BER_BVISEMPTY( &e->e_nname ) ) {
2030                                 /* no ACIs in the root DSE */
2031                                 continue;
2032                         }
2033
2034                         /* first check if the right being requested
2035                          * is allowed by the ACL clause.
2036                          */
2037                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
2038                                 continue;
2039                         }
2040                         /* start out with nothing granted, nothing denied */
2041                         ACL_INIT(tgrant);
2042                         ACL_INIT(tdeny);
2043
2044                         /* get the aci attribute */
2045                         at = attr_find( e->e_attrs, b->a_aci_at );
2046                         if ( at != NULL ) {
2047 #if 0
2048                                 /* FIXME: this breaks acl caching;
2049                                  * see also ACL_RECORD_VALUE_STATE below */
2050                                 ACL_RECORD_VALUE_STATE;
2051 #endif
2052                                 /* the aci is an multi-valued attribute.  The
2053                                 * rights are determined by OR'ing the individual
2054                                 * rights given by the acis.
2055                                 */
2056                                 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2057                                         if ( aci_mask( op,
2058                                                 e, desc, val,
2059                                                 &at->a_nvals[i],
2060                                                 nmatch, matches,
2061                                                 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0 )
2062                                         {
2063                                                 tgrant |= grant;
2064                                                 tdeny |= deny;
2065                                         }
2066                                 }
2067                                 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2068                                           accessmask2str(tgrant, accessmaskbuf, 1), 
2069                                           accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2070
2071                         }
2072                         /* If the entry level aci didn't contain anything valid for the 
2073                          * current operation, climb up the tree and evaluate the
2074                          * acis with scope set to subtree
2075                          */
2076                         if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
2077                                 dnParent( &e->e_nname, &parent_ndn );
2078                                 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
2079                                         Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
2080                                         ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
2081                                         switch(ret){
2082                                         case LDAP_SUCCESS :
2083                                                 stop = 0;
2084                                                 if (!bvals){
2085                                                         break;
2086                                                 }
2087
2088                                                 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++ ) {
2089 #if 0
2090                                                         /* FIXME: this breaks acl caching;
2091                                                          * see also ACL_RECORD_VALUE_STATE above */
2092                                                         ACL_RECORD_VALUE_STATE;
2093 #endif
2094                                                         if ( aci_mask( op, e, desc, val, &bvals[i],
2095                                                                         nmatch, matches,
2096                                                                         &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2097                                                         {
2098                                                                 tgrant |= grant;
2099                                                                 tdeny |= deny;
2100                                                                 /* evaluation stops as soon as either a "deny" or a 
2101                                                                  * "grant" directive matches.
2102                                                                  */
2103                                                                 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
2104                                                                         stop = 1;
2105                                                                 }
2106                                                         }
2107                                                         Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n", 
2108                                                                 accessmask2str(tgrant, accessmaskbuf, 1),
2109                                                                 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2110                                                 }
2111                                                 break;
2112
2113                                         case LDAP_NO_SUCH_ATTRIBUTE:
2114                                                 /* just go on if the aci-Attribute is not present in
2115                                                  * the current entry 
2116                                                  */
2117                                                 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
2118                                                 stop = 0;
2119                                                 break;
2120
2121                                         case LDAP_NO_SUCH_OBJECT:
2122                                                 /* We have reached the base object */
2123                                                 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
2124                                                 stop = 1;
2125                                                 break;
2126
2127                                         default:
2128                                                 stop = 1;
2129                                                 break;
2130                                         }
2131                                         if (stop){
2132                                                 break;
2133                                         }
2134                                         dnParent( &parent_ndn, &parent_ndn );
2135                                 }
2136                         }
2137
2138
2139                         /* remove anything that the ACL clause does not allow */
2140                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
2141                         tdeny &= ACL_PRIV_MASK;
2142
2143                         /* see if we have anything to contribute */
2144                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
2145                                 continue;
2146                         }
2147
2148                         /* this could be improved by changing slap_acl_mask so that it can deal with
2149                          * by clauses that return grant/deny pairs.  Right now, it does either
2150                          * additive or subtractive rights, but not both at the same time.  So,
2151                          * we need to combine the grant/deny pair into a single rights mask in
2152                          * a smart way:  if either grant or deny is "empty", then we use the
2153                          * opposite as is, otherwise we remove any denied rights from the grant
2154                          * rights mask and construct an additive mask.
2155                          */
2156                         if (ACL_IS_INVALID(tdeny)) {
2157                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
2158
2159                         } else if (ACL_IS_INVALID(tgrant)) {
2160                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2161
2162                         } else {
2163                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2164                         }
2165
2166                 } else
2167 #endif /* SLAPD_ACI_ENABLED */
2168 #endif /* !SLAP_DYNACL */
2169                 {
2170                         modmask = b->a_access_mask;
2171                 }
2172
2173                 Debug( LDAP_DEBUG_ACL,
2174                         "<= acl_mask: [%d] applying %s (%s)\n",
2175                         i, accessmask2str( modmask, accessmaskbuf, 1 ), 
2176                         b->a_type == ACL_CONTINUE
2177                                 ? "continue"
2178                                 : b->a_type == ACL_BREAK
2179                                         ? "break"
2180                                         : "stop" );
2181                 /* save old mask */
2182                 oldmask = *mask;
2183
2184                 if( ACL_IS_ADDITIVE(modmask) ) {
2185                         /* add privs */
2186                         ACL_PRIV_SET( *mask, modmask );
2187
2188                         /* cleanup */
2189                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2190
2191                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
2192                         /* substract privs */
2193                         ACL_PRIV_CLR( *mask, modmask );
2194
2195                         /* cleanup */
2196                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2197
2198                 } else {
2199                         /* assign privs */
2200                         *mask = modmask;
2201                 }
2202
2203                 a2pmask = *mask;
2204
2205                 Debug( LDAP_DEBUG_ACL,
2206                         "<= acl_mask: [%d] mask: %s\n",
2207                         i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
2208
2209                 if( b->a_type == ACL_CONTINUE ) {
2210                         continue;
2211
2212                 } else if ( b->a_type == ACL_BREAK ) {
2213                         return ACL_BREAK;
2214
2215                 } else {
2216                         return ACL_STOP;
2217                 }
2218         }
2219
2220         /* implicit "by * none" clause */
2221         ACL_INIT(*mask);
2222
2223         Debug( LDAP_DEBUG_ACL,
2224                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
2225                 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
2226         return ACL_STOP;
2227 }
2228
2229 /*
2230  * acl_check_modlist - check access control on the given entry to see if
2231  * it allows the given modifications by the user associated with op.
2232  * returns      1       if mods allowed ok
2233  *              0       mods not allowed
2234  */
2235
2236 int
2237 acl_check_modlist(
2238         Operation       *op,
2239         Entry   *e,
2240         Modifications   *mlist
2241 )
2242 {
2243         struct berval *bv;
2244         AccessControlState state = ACL_STATE_INIT;
2245         Backend *be;
2246         int be_null = 0;
2247         int ret = 1; /* default is access allowed */
2248
2249         be = op->o_bd;
2250         if ( be == NULL ) {
2251                 be = LDAP_STAILQ_FIRST(&backendDB);
2252                 be_null = 1;
2253                 op->o_bd = be;
2254         }
2255         assert( be != NULL );
2256
2257         /* short circuit root database access */
2258         if ( be_isroot( op ) ) {
2259                 Debug( LDAP_DEBUG_ACL,
2260                         "<= acl_access_allowed: granted to database root\n",
2261                     0, 0, 0 );
2262                 goto done;
2263         }
2264
2265         /* use backend default access if no backend acls */
2266         if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
2267                 Debug( LDAP_DEBUG_ACL,
2268                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
2269                         access2str( ACL_WRITE ),
2270                         op->o_bd->be_dfltaccess >= ACL_WRITE
2271                                 ? "granted" : "denied",
2272                         op->o_dn.bv_val );
2273                 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
2274                 goto done;
2275         }
2276
2277         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
2278                 /*
2279                  * Internal mods are ignored by ACL_WRITE checking
2280                  */
2281                 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
2282                         Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
2283                                 " modify access granted\n",
2284                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2285                         continue;
2286                 }
2287
2288                 /*
2289                  * no-user-modification operational attributes are ignored
2290                  * by ACL_WRITE checking as any found here are not provided
2291                  * by the user
2292                  */
2293                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
2294                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
2295                                 " modify access granted\n",
2296                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2297                         continue;
2298                 }
2299
2300                 switch ( mlist->sml_op ) {
2301                 case LDAP_MOD_REPLACE:
2302                         /*
2303                          * We must check both permission to delete the whole
2304                          * attribute and permission to add the specific attributes.
2305                          * This prevents abuse from selfwriters.
2306                          */
2307                         if ( ! access_allowed( op, e,
2308                                 mlist->sml_desc, NULL, ACL_WDEL, &state ) )
2309                         {
2310                                 ret = 0;
2311                                 goto done;
2312                         }
2313
2314                         if ( mlist->sml_values == NULL ) break;
2315
2316                         /* fall thru to check value to add */
2317
2318                 case LDAP_MOD_ADD:
2319                         assert( mlist->sml_values != NULL );
2320
2321                         for ( bv = mlist->sml_nvalues
2322                                         ? mlist->sml_nvalues : mlist->sml_values;
2323                                 bv->bv_val != NULL; bv++ )
2324                         {
2325                                 if ( ! access_allowed( op, e,
2326                                         mlist->sml_desc, bv, ACL_WADD, &state ) )
2327                                 {
2328                                         ret = 0;
2329                                         goto done;
2330                                 }
2331                         }
2332                         break;
2333
2334                 case LDAP_MOD_DELETE:
2335                         if ( mlist->sml_values == NULL ) {
2336                                 if ( ! access_allowed( op, e,
2337                                         mlist->sml_desc, NULL, ACL_WDEL, NULL ) )
2338                                 {
2339                                         ret = 0;
2340                                         goto done;
2341                                 }
2342                                 break;
2343                         }
2344                         for ( bv = mlist->sml_nvalues
2345                                         ? mlist->sml_nvalues : mlist->sml_values;
2346                                 bv->bv_val != NULL; bv++ )
2347                         {
2348                                 if ( ! access_allowed( op, e,
2349                                         mlist->sml_desc, bv, ACL_WDEL, &state ) )
2350                                 {
2351                                         ret = 0;
2352                                         goto done;
2353                                 }
2354                         }
2355                         break;
2356
2357                 case SLAP_MOD_SOFTADD:
2358                         /* allow adding attribute via modrdn thru */
2359                         break;
2360
2361                 default:
2362                         assert( 0 );
2363                         /* not reached */
2364                         ret = 0;
2365                         break;
2366                 }
2367         }
2368
2369 done:
2370         if (be_null) op->o_bd = NULL;
2371         return( ret );
2372 }
2373
2374 int
2375 acl_get_part(
2376         struct berval   *list,
2377         int             ix,
2378         char            sep,
2379         struct berval   *bv )
2380 {
2381         int     len;
2382         char    *p;
2383
2384         if ( bv ) {
2385                 BER_BVZERO( bv );
2386         }
2387         len = list->bv_len;
2388         p = list->bv_val;
2389         while ( len >= 0 && --ix >= 0 ) {
2390                 while ( --len >= 0 && *p++ != sep )
2391                         ;
2392         }
2393         while ( len >= 0 && *p == ' ' ) {
2394                 len--;
2395                 p++;
2396         }
2397         if ( len < 0 ) {
2398                 return -1;
2399         }
2400
2401         if ( !bv ) {
2402                 return 0;
2403         }
2404
2405         bv->bv_val = p;
2406         while ( --len >= 0 && *p != sep ) {
2407                 bv->bv_len++;
2408                 p++;
2409         }
2410         while ( bv->bv_len > 0 && *--p == ' ' ) {
2411                 bv->bv_len--;
2412         }
2413         
2414         return bv->bv_len;
2415 }
2416
2417 typedef struct acl_set_gather_t {
2418         SetCookie               *cookie;
2419         BerVarray               bvals;
2420 } acl_set_gather_t;
2421
2422 static int
2423 acl_set_cb_gather( Operation *op, SlapReply *rs )
2424 {
2425         acl_set_gather_t        *p = (acl_set_gather_t *)op->o_callback->sc_private;
2426         
2427         if ( rs->sr_type == REP_SEARCH ) {
2428                 BerValue        bvals[ 2 ];
2429                 BerVarray       bvalsp = NULL;
2430                 int             j;
2431
2432                 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2433                         AttributeDescription    *desc = rs->sr_attrs[ j ].an_desc;
2434                         
2435                         if ( desc == slap_schema.si_ad_entryDN ) {
2436                                 bvalsp = bvals;
2437                                 bvals[ 0 ] = rs->sr_entry->e_nname;
2438                                 BER_BVZERO( &bvals[ 1 ] );
2439
2440                         } else {
2441                                 Attribute       *a;
2442
2443                                 a = attr_find( rs->sr_entry->e_attrs, desc );
2444                                 if ( a != NULL ) {
2445                                         int     i;
2446
2447                                         for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2448                                                 ;
2449
2450                                         bvalsp = a->a_nvals;
2451                                 }
2452                         }
2453                 }
2454
2455                 if ( bvals ) {
2456                         p->bvals = slap_set_join( p->cookie, p->bvals,
2457                                         ( '|' | SLAP_SET_RREF ), bvalsp );
2458                 }
2459
2460         } else {
2461                 assert( rs->sr_type == REP_RESULT );
2462         }
2463
2464         return 0;
2465 }
2466
2467 BerVarray
2468 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2469 {
2470         AclSetCookie            *cp = (AclSetCookie *)cookie;
2471         int                     rc = 0;
2472         LDAPURLDesc             *ludp = NULL;
2473         Operation               op2 = { 0 };
2474         SlapReply               rs = {REP_RESULT};
2475         AttributeName           anlist[ 2 ], *anlistp = NULL;
2476         int                     nattrs = 0;
2477         slap_callback           cb = { NULL, acl_set_cb_gather, NULL, NULL };
2478         acl_set_gather_t        p = { 0 };
2479         const char              *text = NULL;
2480         static struct berval    defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2481
2482         /* this routine needs to return the bervals instead of
2483          * plain strings, since syntax is not known.  It should
2484          * also return the syntax or some "comparison cookie".
2485          */
2486         if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2487                 return acl_set_gather2( cookie, name, desc );
2488         }
2489
2490         rc = ldap_url_parse( name->bv_val, &ludp );
2491         if ( rc != LDAP_URL_SUCCESS ) {
2492                 rc = LDAP_PROTOCOL_ERROR;
2493                 goto url_done;
2494         }
2495         
2496         if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2497         {
2498                 /* host part must be empty */
2499                 /* extensions parts must be empty */
2500                 rc = LDAP_PROTOCOL_ERROR;
2501                 goto url_done;
2502         }
2503
2504         /* Grab the searchbase and see if an appropriate database can be found */
2505         ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2506         rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2507                         &op2.o_req_ndn, cp->op->o_tmpmemctx );
2508         BER_BVZERO( &op2.o_req_dn );
2509         if ( rc != LDAP_SUCCESS ) {
2510                 goto url_done;
2511         }
2512
2513         op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2514         if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2515                 rc = LDAP_NO_SUCH_OBJECT;
2516                 goto url_done;
2517         }
2518
2519         /* Grab the filter */
2520         if ( ludp->lud_filter ) {
2521                 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2522                                 cp->op->o_tmpmemctx );
2523                 
2524         } else {
2525                 op2.ors_filterstr = defaultFilter_bv;
2526         }
2527
2528         op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
2529         if ( op2.ors_filter == NULL ) {
2530                 rc = LDAP_PROTOCOL_ERROR;
2531                 goto url_done;
2532         }
2533
2534         /* Grab the scope */
2535         op2.ors_scope = ludp->lud_scope;
2536
2537         /* Grap the attributes */
2538         if ( ludp->lud_attrs ) {
2539                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2540                         ;
2541
2542                 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2543                                 cp->op->o_tmpmemctx );
2544
2545                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2546                         ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2547                         anlistp[ nattrs ].an_desc = NULL;
2548                         rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2549                                         &anlistp[ nattrs ].an_desc, &text );
2550                         if ( rc != LDAP_SUCCESS ) {
2551                                 goto url_done;
2552                         }
2553                 }
2554
2555         } else {
2556                 anlistp = anlist;
2557         }
2558
2559         anlistp[ nattrs ].an_name = desc->ad_cname;
2560         anlistp[ nattrs ].an_desc = desc;
2561
2562         BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2563         
2564         p.cookie = cookie;
2565         
2566         op2.o_hdr = cp->op->o_hdr;
2567         op2.o_tag = LDAP_REQ_SEARCH;
2568         op2.o_ndn = op2.o_bd->be_rootndn;
2569         op2.o_callback = &cb;
2570         op2.o_time = slap_get_time();
2571         op2.o_do_not_cache = 1;
2572         op2.o_is_auth_check = 0;
2573         ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
2574         op2.ors_slimit = SLAP_NO_LIMIT;
2575         op2.ors_tlimit = SLAP_NO_LIMIT;
2576         op2.ors_attrs = anlistp;
2577         op2.ors_attrsonly = 0;
2578         op2.o_private = cp->op->o_private;
2579
2580         cb.sc_private = &p;
2581
2582         rc = op2.o_bd->be_search( &op2, &rs );
2583         if ( rc != 0 ) {
2584                 goto url_done;
2585         }
2586
2587 url_done:;
2588         if ( op2.ors_filter ) {
2589                 filter_free_x( cp->op, op2.ors_filter );
2590         }
2591         if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2592                 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2593         }
2594         if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2595                 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2596         }
2597         if ( ludp ) {
2598                 ldap_free_urldesc( ludp );
2599         }
2600         if ( anlistp && anlistp != anlist ) {
2601                 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2602         }
2603
2604         return p.bvals;
2605 }
2606
2607 BerVarray
2608 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2609 {
2610         AclSetCookie    *cp = (AclSetCookie *)cookie;
2611         BerVarray       bvals = NULL;
2612         struct berval   ndn;
2613         int             rc = 0;
2614
2615         /* this routine needs to return the bervals instead of
2616          * plain strings, since syntax is not known.  It should
2617          * also return the syntax or some "comparison cookie".
2618          */
2619         rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2620         if ( rc == LDAP_SUCCESS ) {
2621                 if ( desc == slap_schema.si_ad_entryDN ) {
2622                         bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2623                                         cp->op->o_tmpmemctx );
2624                         bvals[ 0 ] = ndn;
2625                         BER_BVZERO( &bvals[ 1 ] );
2626                         BER_BVZERO( &ndn );
2627
2628                 } else {
2629                         backend_attribute( cp->op,
2630                                 cp->e, &ndn, desc, &bvals, ACL_NONE );
2631                 }
2632
2633                 if ( !BER_BVISNULL( &ndn ) ) {
2634                         slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2635                 }
2636         }
2637
2638         return bvals;
2639 }
2640
2641 int
2642 acl_match_set (
2643         struct berval *subj,
2644         Operation *op,
2645         Entry *e,
2646         int setref )
2647 {
2648         struct berval   set = BER_BVNULL;
2649         int             rc = 0;
2650         AclSetCookie    cookie;
2651
2652         if ( setref == 0 ) {
2653                 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2654
2655         } else {
2656                 struct berval           subjdn, ndn = BER_BVNULL;
2657                 struct berval           setat;
2658                 BerVarray               bvals;
2659                 const char              *text;
2660                 AttributeDescription    *desc = NULL;
2661
2662                 /* format of string is "entry/setAttrName" */
2663                 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2664                         return 0;
2665                 }
2666
2667                 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2668                         setat = aci_bv[ ACI_BV_SET_ATTR ];
2669                 }
2670
2671                 /*
2672                  * NOTE: dnNormalize honors the ber_len field
2673                  * as the length of the dn to be normalized
2674                  */
2675                 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2676                         if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2677                         {
2678                                 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2679                                 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2680                                         int     i;
2681
2682                                         set = bvals[0];
2683                                         BER_BVZERO( &bvals[0] );
2684                                         for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2685                                                 /* count */ ;
2686                                         bvals[0].bv_val = bvals[i-1].bv_val;
2687                                         BER_BVZERO( &bvals[i-1] );
2688                                 }
2689                                 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2690                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2691                         }
2692                 }
2693         }
2694
2695         if ( !BER_BVISNULL( &set ) ) {
2696                 cookie.op = op;
2697                 cookie.e = e;
2698                 rc = ( slap_set_filter( acl_set_gather, (SetCookie *)&cookie, &set,
2699                         &op->o_ndn, &e->e_nname, NULL ) > 0 );
2700                 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2701         }
2702
2703         return(rc);
2704 }
2705
2706 #ifdef SLAP_DYNACL
2707
2708 /*
2709  * dynamic ACL infrastructure
2710  */
2711 static slap_dynacl_t    *da_list = NULL;
2712
2713 int
2714 slap_dynacl_register( slap_dynacl_t *da )
2715 {
2716         slap_dynacl_t   *tmp;
2717
2718         for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2719                 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2720                         break;
2721                 }
2722         }
2723
2724         if ( tmp != NULL ) {
2725                 return -1;
2726         }
2727         
2728         if ( da->da_mask == NULL ) {
2729                 return -1;
2730         }
2731         
2732         da->da_private = NULL;
2733         da->da_next = da_list;
2734         da_list = da;
2735
2736         return 0;
2737 }
2738
2739 static slap_dynacl_t *
2740 slap_dynacl_next( slap_dynacl_t *da )
2741 {
2742         if ( da ) {
2743                 return da->da_next;
2744         }
2745         return da_list;
2746 }
2747
2748 slap_dynacl_t *
2749 slap_dynacl_get( const char *name )
2750 {
2751         slap_dynacl_t   *da;
2752
2753         for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2754                 if ( strcasecmp( da->da_name, name ) == 0 ) {
2755                         break;
2756                 }
2757         }
2758
2759         return da;
2760 }
2761 #endif /* SLAP_DYNACL */
2762
2763 int
2764 acl_init( void )
2765 {
2766 #ifdef SLAP_DYNACL
2767         int     rc;
2768
2769 #ifdef SLAPD_ACI_ENABLED
2770         rc = dynacl_aci_init();
2771         if ( rc != 0 ) {
2772                 return rc;
2773         }
2774 #endif /* SLAPD_ACI_ENABLED */
2775
2776 #endif /* SLAP_DYNACL */
2777
2778         return 0;
2779 }
2780
2781 int
2782 acl_string_expand(
2783         struct berval   *bv,
2784         struct berval   *pat,
2785         char            *match,
2786         int             nmatch,
2787         regmatch_t      *matches)
2788 {
2789         ber_len_t       size;
2790         char   *sp;
2791         char   *dp;
2792         int     flag;
2793
2794         size = 0;
2795         bv->bv_val[0] = '\0';
2796         bv->bv_len--; /* leave space for lone $ */
2797
2798         flag = 0;
2799         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2800                 sp < pat->bv_val + pat->bv_len ; sp++ )
2801         {
2802                 /* did we previously see a $ */
2803                 if ( flag ) {
2804                         if ( flag == 1 && *sp == '$' ) {
2805                                 *dp++ = '$';
2806                                 size++;
2807                                 flag = 0;
2808
2809                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2810                                 flag = 2;
2811
2812                         } else if ( *sp >= '0' && *sp <= '9' ) {
2813                                 int     n;
2814                                 int     i;
2815                                 int     l;
2816
2817                                 n = *sp - '0';
2818
2819                                 if ( flag == 2 ) {
2820                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2821                                                 if ( *sp >= '0' && *sp <= '9' ) {
2822                                                         n = 10*n + ( *sp - '0' );
2823                                                 }
2824                                         }
2825
2826                                         if ( *sp != /*'{'*/ '}' ) {
2827                                                 /* FIXME: error */
2828                                                 return 1;
2829                                         }
2830                                 }
2831
2832                                 if ( n >= nmatch ) {
2833                                         /* FIXME: error */
2834                                         return 1;
2835                                 }
2836                                 
2837                                 *dp = '\0';
2838                                 i = matches[n].rm_so;
2839                                 l = matches[n].rm_eo; 
2840                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2841                                         *dp++ = match[i];
2842                                 }
2843                                 *dp = '\0';
2844
2845                                 flag = 0;
2846                         }
2847                 } else {
2848                         if (*sp == '$') {
2849                                 flag = 1;
2850                         } else {
2851                                 *dp++ = *sp;
2852                                 size++;
2853                         }
2854                 }
2855         }
2856
2857         if ( flag ) {
2858                 /* must have ended with a single $ */
2859                 *dp++ = '$';
2860                 size++;
2861         }
2862
2863         *dp = '\0';
2864         bv->bv_len = size;
2865
2866         Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2867         Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2868
2869         return 0;
2870 }
2871
2872 static int
2873 regex_matches(
2874         struct berval   *pat,           /* pattern to expand and match against */
2875         char            *str,           /* string to match against pattern */
2876         char            *buf,           /* buffer with $N expansion variables */
2877         int             nmatch, /* size of the matches array */
2878         regmatch_t      *matches        /* offsets in buffer for $N expansion variables */
2879 )
2880 {
2881         regex_t re;
2882         char newbuf[ACL_BUF_SIZE];
2883         struct berval bv;
2884         int     rc;
2885
2886         bv.bv_len = sizeof( newbuf ) - 1;
2887         bv.bv_val = newbuf;
2888
2889         if (str == NULL) {
2890                 str = "";
2891         };
2892
2893         acl_string_expand( &bv, pat, buf, nmatch, matches );
2894         rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2895         if ( rc ) {
2896                 char error[ACL_BUF_SIZE];
2897                 regerror( rc, &re, error, sizeof( error ) );
2898
2899                 Debug( LDAP_DEBUG_TRACE,
2900                     "compile( \"%s\", \"%s\") failed %s\n",
2901                         pat->bv_val, str, error );
2902                 return( 0 );
2903         }
2904
2905         rc = regexec( &re, str, 0, NULL, 0 );
2906         regfree( &re );
2907
2908         Debug( LDAP_DEBUG_TRACE,
2909             "=> regex_matches: string:   %s\n", str, 0, 0 );
2910         Debug( LDAP_DEBUG_TRACE,
2911             "=> regex_matches: rc: %d %s\n",
2912                 rc, !rc ? "matches" : "no matches", 0 );
2913         return( !rc );
2914 }
2915