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