]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
Don't bother setting up threads if there are no indexed attrs
[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 */
953  #define ACL_RECORD_VALUE_STATE do { \
954                         if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
955                                 state->as_recorded |= ACL_STATE_RECORDED_VD; \
956                                 state->as_vd_acl = a; \
957                                 AC_MEMCPY( state->as_vd_acl_matches, matches, \
958                                         sizeof( state->as_vd_acl_matches )) ; \
959                                 state->as_vd_acl_count = count; \
960                                 state->as_vd_access = b; \
961                                 state->as_vd_access_count = i; \
962                         }\
963         } while( 0 )
964
965 static int
966 acl_mask_dn(
967         Operation               *op,
968         Entry                   *e,
969         AttributeDescription    *desc,
970         struct berval           *val,
971         AccessControl           *a,
972         int                     nmatch,
973         regmatch_t              *matches,
974         slap_dn_access          *bdn,
975         struct berval           *opndn )
976 {
977         /*
978          * if access applies to the entry itself, and the
979          * user is bound as somebody in the same namespace as
980          * the entry, OR the given dn matches the dn pattern
981          */
982         /*
983          * NOTE: styles "anonymous", "users" and "self" 
984          * have been moved to enum slap_style_t, whose 
985          * value is set in a_dn_style; however, the string
986          * is maintained in a_dn_pat.
987          */
988
989         if ( bdn->a_style == ACL_STYLE_ANONYMOUS ) {
990                 if ( !BER_BVISEMPTY( opndn ) ) {
991                         return 1;
992                 }
993
994         } else if ( bdn->a_style == ACL_STYLE_USERS ) {
995                 if ( BER_BVISEMPTY( opndn ) ) {
996                         return 1;
997                 }
998
999         } else if ( bdn->a_style == ACL_STYLE_SELF ) {
1000                 struct berval   ndn, selfndn;
1001                 int             level;
1002
1003                 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
1004                         return 1;
1005                 }
1006
1007                 level = bdn->a_self_level;
1008                 if ( level < 0 ) {
1009                         selfndn = *opndn;
1010                         ndn = e->e_nname;
1011                         level = -level;
1012
1013                 } else {
1014                         ndn = *opndn;
1015                         selfndn = e->e_nname;
1016                 }
1017
1018                 for ( ; level > 0; level-- ) {
1019                         if ( BER_BVISEMPTY( &ndn ) ) {
1020                                 break;
1021                         }
1022                         dnParent( &ndn, &ndn );
1023                 }
1024                         
1025                 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
1026                 {
1027                         return 1;
1028                 }
1029
1030         } else if ( bdn->a_style == ACL_STYLE_REGEX ) {
1031                 if ( !ber_bvccmp( &bdn->a_pat, '*' ) ) {
1032                         int             tmp_nmatch;
1033                         regmatch_t      tmp_matches[2],
1034                                         *tmp_matchesp = tmp_matches;
1035
1036                         int             rc = 0;
1037
1038                         switch ( a->acl_dn_style ) {
1039                         case ACL_STYLE_REGEX:
1040                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1041                                         tmp_matchesp = matches;
1042                                         tmp_nmatch = nmatch;
1043                                         break;
1044                                 }
1045                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1046
1047                         case ACL_STYLE_BASE:
1048                                 tmp_matches[0].rm_so = 0;
1049                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1050                                 tmp_nmatch = 1;
1051                                 break;
1052
1053                         case ACL_STYLE_ONE:
1054                         case ACL_STYLE_SUBTREE:
1055                         case ACL_STYLE_CHILDREN:
1056                                 tmp_matches[0].rm_so = 0;
1057                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1058                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1059                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1060                                 tmp_nmatch = 2;
1061                                 break;
1062
1063                         default:
1064                                 /* error */
1065                                 rc = 1;
1066                                 break;
1067                         }
1068
1069                         if ( rc ) {
1070                                 return 1;
1071                         }
1072
1073                         if ( !regex_matches( &bdn->a_pat, opndn->bv_val,
1074                                 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
1075                         {
1076                                 return 1;
1077                         }
1078                 }
1079
1080         } else {
1081                 struct berval   pat;
1082                 ber_len_t       patlen, odnlen;
1083                 int             got_match = 0;
1084
1085                 if ( e->e_dn == NULL )
1086                         return 1;
1087
1088                 if ( bdn->a_expand ) {
1089                         struct berval   bv;
1090                         char            buf[ACL_BUF_SIZE];
1091                         
1092                         int             tmp_nmatch;
1093                         regmatch_t      tmp_matches[2],
1094                                         *tmp_matchesp = tmp_matches;
1095
1096                         int             rc = 0;
1097
1098                         bv.bv_len = sizeof( buf ) - 1;
1099                         bv.bv_val = buf;
1100
1101                         switch ( a->acl_dn_style ) {
1102                         case ACL_STYLE_REGEX:
1103                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1104                                         tmp_matchesp = matches;
1105                                         tmp_nmatch = nmatch;
1106                                         break;
1107                                 }
1108                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1109
1110                         case ACL_STYLE_BASE:
1111                                 tmp_matches[0].rm_so = 0;
1112                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1113                                 tmp_nmatch = 1;
1114                                 break;
1115
1116                         case ACL_STYLE_ONE:
1117                         case ACL_STYLE_SUBTREE:
1118                         case ACL_STYLE_CHILDREN:
1119                                 tmp_matches[0].rm_so = 0;
1120                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1121                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1122                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1123                                 tmp_nmatch = 2;
1124                                 break;
1125
1126                         default:
1127                                 /* error */
1128                                 rc = 1;
1129                                 break;
1130                         }
1131
1132                         if ( rc ) {
1133                                 return 1;
1134                         }
1135
1136                         if ( acl_string_expand( &bv, &bdn->a_pat, 
1137                                         e->e_nname.bv_val,
1138                                         tmp_nmatch, tmp_matchesp ) )
1139                         {
1140                                 return 1;
1141                         }
1142                         
1143                         if ( dnNormalize(0, NULL, NULL, &bv,
1144                                         &pat, op->o_tmpmemctx )
1145                                         != LDAP_SUCCESS )
1146                         {
1147                                 /* did not expand to a valid dn */
1148                                 return 1;
1149                         }
1150
1151                 } else {
1152                         pat = bdn->a_pat;
1153                 }
1154
1155                 patlen = pat.bv_len;
1156                 odnlen = opndn->bv_len;
1157                 if ( odnlen < patlen ) {
1158                         goto dn_match_cleanup;
1159
1160                 }
1161
1162                 if ( bdn->a_style == ACL_STYLE_BASE ) {
1163                         /* base dn -- entire object DN must match */
1164                         if ( odnlen != patlen ) {
1165                                 goto dn_match_cleanup;
1166                         }
1167
1168                 } else if ( bdn->a_style == ACL_STYLE_ONE ) {
1169                         ber_len_t       rdnlen = 0;
1170
1171                         if ( odnlen <= patlen ) {
1172                                 goto dn_match_cleanup;
1173                         }
1174
1175                         if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1176                                 goto dn_match_cleanup;
1177                         }
1178
1179                         rdnlen = dn_rdnlen( NULL, opndn );
1180                         if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
1181                                 goto dn_match_cleanup;
1182                         }
1183
1184                 } else if ( bdn->a_style == ACL_STYLE_SUBTREE ) {
1185                         if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1186                                 goto dn_match_cleanup;
1187                         }
1188
1189                 } else if ( bdn->a_style == ACL_STYLE_CHILDREN ) {
1190                         if ( odnlen <= patlen ) {
1191                                 goto dn_match_cleanup;
1192                         }
1193
1194                         if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1195                                 goto dn_match_cleanup;
1196                         }
1197
1198                 } else if ( bdn->a_style == ACL_STYLE_LEVEL ) {
1199                         int             level = bdn->a_level;
1200                         struct berval   ndn;
1201
1202                         if ( odnlen <= patlen ) {
1203                                 goto dn_match_cleanup;
1204                         }
1205
1206                         if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
1207                         {
1208                                 goto dn_match_cleanup;
1209                         }
1210                         
1211                         ndn = *opndn;
1212                         for ( ; level > 0; level-- ) {
1213                                 if ( BER_BVISEMPTY( &ndn ) ) {
1214                                         goto dn_match_cleanup;
1215                                 }
1216                                 dnParent( &ndn, &ndn );
1217                                 if ( ndn.bv_len < patlen ) {
1218                                         goto dn_match_cleanup;
1219                                 }
1220                         }
1221                         
1222                         if ( ndn.bv_len != patlen ) {
1223                                 goto dn_match_cleanup;
1224                         }
1225                 }
1226
1227                 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
1228
1229 dn_match_cleanup:;
1230                 if ( pat.bv_val != bdn->a_pat.bv_val ) {
1231                         slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1232                 }
1233
1234                 if ( !got_match ) {
1235                         return 1;
1236                 }
1237         }
1238
1239         return 0;
1240 }
1241
1242 static int
1243 acl_mask_dnattr(
1244         Operation               *op,
1245         Entry                   *e,
1246         struct berval           *val,
1247         AccessControl           *a,
1248         Access                  *b,
1249         int                     i,
1250         regmatch_t              *matches,
1251         int                     count,
1252         AccessControlState      *state,
1253         slap_dn_access          *bdn,
1254         struct berval           *opndn )
1255 {
1256         Attribute       *at;
1257         struct berval   bv;
1258         int             rc, match = 0;
1259         const char      *text;
1260         const char      *attr = bdn->a_at->ad_cname.bv_val;
1261
1262         assert( attr != NULL );
1263
1264         if ( BER_BVISEMPTY( opndn ) ) {
1265                 return 1;
1266         }
1267
1268         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1269         bv = *opndn;
1270
1271         /* see if asker is listed in dnattr */
1272         for ( at = attrs_find( e->e_attrs, bdn->a_at );
1273                 at != NULL;
1274                 at = attrs_find( at->a_next, bdn->a_at ) )
1275         {
1276                 if ( value_find_ex( bdn->a_at,
1277                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1278                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1279                         at->a_nvals,
1280                         &bv, op->o_tmpmemctx ) == 0 )
1281                 {
1282                         /* found it */
1283                         match = 1;
1284                         break;
1285                 }
1286         }
1287
1288         if ( match ) {
1289                 /* have a dnattr match. if this is a self clause then
1290                  * the target must also match the op dn.
1291                  */
1292                 if ( bdn->a_self ) {
1293                         /* check if the target is an attribute. */
1294                         if ( val == NULL ) return 1;
1295
1296                         /* target is attribute, check if the attribute value
1297                          * is the op dn.
1298                          */
1299                         rc = value_match( &match, bdn->a_at,
1300                                 bdn->a_at->ad_type->sat_equality, 0,
1301                                 val, &bv, &text );
1302                         /* on match error or no match, fail the ACL clause */
1303                         if ( rc != LDAP_SUCCESS || match != 0 )
1304                                 return 1;
1305                 }
1306
1307         } else {
1308                 /* no dnattr match, check if this is a self clause */
1309                 if ( ! bdn->a_self )
1310                         return 1;
1311
1312                 ACL_RECORD_VALUE_STATE;
1313                 
1314                 /* this is a self clause, check if the target is an
1315                  * attribute.
1316                  */
1317                 if ( val == NULL )
1318                         return 1;
1319
1320                 /* target is attribute, check if the attribute value
1321                  * is the op dn.
1322                  */
1323                 rc = value_match( &match, bdn->a_at,
1324                         bdn->a_at->ad_type->sat_equality, 0,
1325                         val, &bv, &text );
1326
1327                 /* on match error or no match, fail the ACL clause */
1328                 if ( rc != LDAP_SUCCESS || match != 0 )
1329                         return 1;
1330         }
1331
1332         return 0;
1333 }
1334
1335
1336 /*
1337  * slap_acl_mask - modifies mask based upon the given acl and the
1338  * requested access to entry e, attribute attr, value val.  if val
1339  * is null, access to the whole attribute is assumed (all values).
1340  *
1341  * returns      0       access NOT allowed
1342  *              1       access allowed
1343  */
1344
1345 static slap_control_t
1346 slap_acl_mask(
1347         AccessControl           *a,
1348         slap_mask_t             *mask,
1349         Operation               *op,
1350         Entry                   *e,
1351         AttributeDescription    *desc,
1352         struct berval           *val,
1353         int                     nmatch,
1354         regmatch_t              *matches,
1355         int                     count,
1356         AccessControlState      *state )
1357 {
1358         int             i;
1359         Access          *b;
1360 #ifdef LDAP_DEBUG
1361         char            accessmaskbuf[ACCESSMASK_MAXLEN];
1362 #endif /* DEBUG */
1363         const char      *attr;
1364         slap_mask_t     a2pmask = ACL_ACCESS2PRIV( *mask );
1365
1366         assert( a != NULL );
1367         assert( mask != NULL );
1368         assert( desc != NULL );
1369
1370         attr = desc->ad_cname.bv_val;
1371
1372         assert( attr != NULL );
1373
1374         Debug( LDAP_DEBUG_ACL,
1375                 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1376                 e->e_dn, attr, 0 );
1377
1378         Debug( LDAP_DEBUG_ACL,
1379                 "=> acl_mask: to %s by \"%s\", (%s) \n",
1380                 val ? "value" : "all values",
1381                 op->o_ndn.bv_val ?  op->o_ndn.bv_val : "",
1382                 accessmask2str( *mask, accessmaskbuf, 1 ) );
1383
1384
1385         if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1386                 && state->as_vd_acl == a )
1387         {
1388                 b = state->as_vd_access;
1389                 i = state->as_vd_access_count;
1390
1391         } else {
1392                 b = a->acl_access;
1393                 i = 1;
1394         }
1395
1396         for ( ; b != NULL; b = b->a_next, i++ ) {
1397                 slap_mask_t oldmask, modmask;
1398
1399                 ACL_INVALIDATE( modmask );
1400
1401                 /* AND <who> clauses */
1402                 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1403                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1404                                 b->a_dn_pat.bv_val, 0, 0);
1405                         /*
1406                          * if access applies to the entry itself, and the
1407                          * user is bound as somebody in the same namespace as
1408                          * the entry, OR the given dn matches the dn pattern
1409                          */
1410                         /*
1411                          * NOTE: styles "anonymous", "users" and "self" 
1412                          * have been moved to enum slap_style_t, whose 
1413                          * value is set in a_dn_style; however, the string
1414                          * is maintained in a_dn_pat.
1415                          */
1416
1417                         if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1418                                         &b->a_dn, &op->o_ndn ) )
1419                         {
1420                                 continue;
1421                         }
1422                 }
1423
1424                 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1425                         struct berval   ndn;
1426
1427                         Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1428                                 b->a_realdn_pat.bv_val, 0, 0);
1429                         /*
1430                          * if access applies to the entry itself, and the
1431                          * user is bound as somebody in the same namespace as
1432                          * the entry, OR the given dn matches the dn pattern
1433                          */
1434                         /*
1435                          * NOTE: styles "anonymous", "users" and "self" 
1436                          * have been moved to enum slap_style_t, whose 
1437                          * value is set in a_dn_style; however, the string
1438                          * is maintained in a_dn_pat.
1439                          */
1440
1441                         if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1442                         {
1443                                 ndn = op->o_conn->c_ndn;
1444                         } else {
1445                                 ndn = op->o_ndn;
1446                         }
1447
1448                         if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1449                                         &b->a_realdn, &ndn ) )
1450                         {
1451                                 continue;
1452                         }
1453                 }
1454
1455                 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1456                         if ( ! op->o_conn->c_listener ) {
1457                                 continue;
1458                         }
1459                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1460                                 b->a_sockurl_pat.bv_val, 0, 0 );
1461
1462                         if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1463                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1464                                         if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1465                                                         e->e_ndn, nmatch, matches ) ) 
1466                                         {
1467                                                 continue;
1468                                         }
1469
1470                                 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1471                                         struct berval   bv;
1472                                         char buf[ACL_BUF_SIZE];
1473
1474                                         bv.bv_len = sizeof( buf ) - 1;
1475                                         bv.bv_val = buf;
1476                                         if ( acl_string_expand( &bv, &b->a_sockurl_pat,
1477                                                         e->e_ndn, nmatch, matches ) )
1478                                         {
1479                                                 continue;
1480                                         }
1481
1482                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1483                                         {
1484                                                 continue;
1485                                         }
1486
1487                                 } else {
1488                                         if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1489                                         {
1490                                                 continue;
1491                                         }
1492                                 }
1493                         }
1494                 }
1495
1496                 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1497                         if ( !op->o_conn->c_peer_domain.bv_val ) {
1498                                 continue;
1499                         }
1500                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1501                                 b->a_domain_pat.bv_val, 0, 0 );
1502                         if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1503                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1504                                         if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1505                                                         e->e_ndn, nmatch, matches ) ) 
1506                                         {
1507                                                 continue;
1508                                         }
1509                                 } else {
1510                                         char buf[ACL_BUF_SIZE];
1511
1512                                         struct berval   cmp = op->o_conn->c_peer_domain;
1513                                         struct berval   pat = b->a_domain_pat;
1514
1515                                         if ( b->a_domain_expand ) {
1516                                                 struct berval bv;
1517
1518                                                 bv.bv_len = sizeof(buf) - 1;
1519                                                 bv.bv_val = buf;
1520
1521                                                 if ( acl_string_expand(&bv, &b->a_domain_pat,
1522                                                                 e->e_ndn, nmatch, matches) )
1523                                                 {
1524                                                         continue;
1525                                                 }
1526                                                 pat = bv;
1527                                         }
1528
1529                                         if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1530                                                 int offset = cmp.bv_len - pat.bv_len;
1531                                                 if ( offset < 0 ) {
1532                                                         continue;
1533                                                 }
1534
1535                                                 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1536                                                         continue;
1537                                                 }
1538
1539                                                 /* trim the domain */
1540                                                 cmp.bv_val = &cmp.bv_val[ offset ];
1541                                                 cmp.bv_len -= offset;
1542                                         }
1543                                         
1544                                         if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1545                                                 continue;
1546                                         }
1547                                 }
1548                         }
1549                 }
1550
1551                 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1552                         if ( !op->o_conn->c_peer_name.bv_val ) {
1553                                 continue;
1554                         }
1555                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1556                                 b->a_peername_pat.bv_val, 0, 0 );
1557                         if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1558                                 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1559                                         if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1560                                                         e->e_ndn, nmatch, matches ) ) 
1561                                         {
1562                                                 continue;
1563                                         }
1564
1565                                 } else {
1566                                         /* try exact match */
1567                                         if ( b->a_peername_style == ACL_STYLE_BASE ) {
1568                                                 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1569                                                         continue;
1570                                                 }
1571
1572                                         } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1573                                                 struct berval   bv;
1574                                                 char buf[ACL_BUF_SIZE];
1575
1576                                                 bv.bv_len = sizeof( buf ) - 1;
1577                                                 bv.bv_val = buf;
1578                                                 if ( acl_string_expand( &bv, &b->a_peername_pat,
1579                                                                 e->e_ndn, nmatch, matches ) )
1580                                                 {
1581                                                         continue;
1582                                                 }
1583
1584                                                 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1585                                                         continue;
1586                                                 }
1587
1588                                         /* extract IP and try exact match */
1589                                         } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1590                                                 char            *port;
1591                                                 char            buf[] = "255.255.255.255";
1592                                                 struct berval   ip;
1593                                                 unsigned long   addr;
1594                                                 int             port_number = -1;
1595                                                 
1596                                                 if ( strncasecmp( op->o_conn->c_peer_name.bv_val, 
1597                                                                         acl_bv_ip_eq.bv_val,
1598                                                                         acl_bv_ip_eq.bv_len ) != 0 ) 
1599                                                         continue;
1600
1601                                                 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ip_eq.bv_len;
1602                                                 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ip_eq.bv_len;
1603
1604                                                 port = strrchr( ip.bv_val, ':' );
1605                                                 if ( port ) {
1606                                                         ip.bv_len = port - ip.bv_val;
1607                                                         ++port;
1608                                                         if ( lutil_atoi( &port_number, port ) != 0 )
1609                                                                 continue;
1610                                                 }
1611                                                 
1612                                                 /* the port check can be anticipated here */
1613                                                 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1614                                                         continue;
1615                                                 
1616                                                 /* address longer than expected? */
1617                                                 if ( ip.bv_len >= sizeof(buf) )
1618                                                         continue;
1619
1620                                                 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1621                                                 buf[ ip.bv_len ] = '\0';
1622
1623                                                 addr = inet_addr( buf );
1624
1625                                                 /* unable to convert? */
1626                                                 if ( addr == (unsigned long)(-1) )
1627                                                         continue;
1628
1629                                                 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1630                                                         continue;
1631
1632 #ifdef LDAP_PF_LOCAL
1633                                         /* extract path and try exact match */
1634                                         } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1635                                                 struct berval path;
1636                                                 
1637                                                 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1638                                                                         acl_bv_path_eq.bv_val,
1639                                                                         acl_bv_path_eq.bv_len ) != 0 )
1640                                                         continue;
1641
1642                                                 path.bv_val = op->o_conn->c_peer_name.bv_val
1643                                                         + acl_bv_path_eq.bv_len;
1644                                                 path.bv_len = op->o_conn->c_peer_name.bv_len
1645                                                         - acl_bv_path_eq.bv_len;
1646
1647                                                 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1648                                                         continue;
1649
1650 #endif /* LDAP_PF_LOCAL */
1651
1652                                         /* exact match (very unlikely...) */
1653                                         } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1654                                                         continue;
1655                                         }
1656                                 }
1657                         }
1658                 }
1659
1660                 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1661                         if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1662                                 continue;
1663                         }
1664                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1665                                 b->a_sockname_pat.bv_val, 0, 0 );
1666                         if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1667                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1668                                         if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1669                                                         e->e_ndn, nmatch, matches ) ) 
1670                                         {
1671                                                 continue;
1672                                         }
1673
1674                                 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1675                                         struct berval   bv;
1676                                         char buf[ACL_BUF_SIZE];
1677
1678                                         bv.bv_len = sizeof( buf ) - 1;
1679                                         bv.bv_val = buf;
1680                                         if ( acl_string_expand( &bv, &b->a_sockname_pat,
1681                                                         e->e_ndn, nmatch, matches ) )
1682                                         {
1683                                                 continue;
1684                                         }
1685
1686                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1687                                                 continue;
1688                                         }
1689
1690                                 } else {
1691                                         if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1692                                                 continue;
1693                                         }
1694                                 }
1695                         }
1696                 }
1697
1698                 if ( b->a_dn_at != NULL ) {
1699                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1700                                         matches, count, state,
1701                                         &b->a_dn, &op->o_ndn ) )
1702                         {
1703                                 continue;
1704                         }
1705                 }
1706
1707                 if ( b->a_realdn_at != NULL ) {
1708                         struct berval   ndn;
1709
1710                         if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1711                         {
1712                                 ndn = op->o_conn->c_ndn;
1713                         } else {
1714                                 ndn = op->o_ndn;
1715                         }
1716
1717                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1718                                         matches, count, state,
1719                                         &b->a_realdn, &ndn ) )
1720                         {
1721                                 continue;
1722                         }
1723                 }
1724
1725                 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1726                         struct berval bv;
1727                         struct berval ndn = BER_BVNULL;
1728                         int rc;
1729
1730                         if ( op->o_ndn.bv_len == 0 ) {
1731                                 continue;
1732                         }
1733
1734                         Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1735                                 b->a_group_pat.bv_val, 0, 0 );
1736
1737                         /* b->a_group is an unexpanded entry name, expanded it should be an 
1738                          * entry with objectclass group* and we test to see if odn is one of
1739                          * the values in the attribute group
1740                          */
1741                         /* see if asker is listed in dnattr */
1742                         if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1743                                 char            buf[ACL_BUF_SIZE];
1744                                 int             tmp_nmatch;
1745                                 regmatch_t      tmp_matches[2],
1746                                                 *tmp_matchesp = tmp_matches;
1747
1748                                 bv.bv_len = sizeof(buf) - 1;
1749                                 bv.bv_val = buf;
1750
1751                                 rc = 0;
1752
1753                                 switch ( a->acl_dn_style ) {
1754                                 case ACL_STYLE_REGEX:
1755                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1756                                                 tmp_matchesp = matches;
1757                                                 tmp_nmatch = nmatch;
1758                                                 break;
1759                                         }
1760
1761                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1762                                 case ACL_STYLE_BASE:
1763                                         tmp_matches[0].rm_so = 0;
1764                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1765                                         tmp_nmatch = 1;
1766                                         break;
1767
1768                                 case ACL_STYLE_ONE:
1769                                 case ACL_STYLE_SUBTREE:
1770                                 case ACL_STYLE_CHILDREN:
1771                                         tmp_matches[0].rm_so = 0;
1772                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1773                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1774                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1775                                         tmp_nmatch = 2;
1776                                         break;
1777
1778                                 default:
1779                                         /* error */
1780                                         rc = 1;
1781                                         break;
1782                                 }
1783
1784                                 if ( rc ) {
1785                                         continue;
1786                                 }
1787                                 
1788                                 if ( acl_string_expand( &bv, &b->a_group_pat,
1789                                                 e->e_nname.bv_val,
1790                                                 tmp_nmatch, tmp_matchesp ) )
1791                                 {
1792                                         continue;
1793                                 }
1794
1795                                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1796                                                 op->o_tmpmemctx ) != LDAP_SUCCESS )
1797                                 {
1798                                         /* did not expand to a valid dn */
1799                                         continue;
1800                                 }
1801
1802                                 bv = ndn;
1803
1804                         } else {
1805                                 bv = b->a_group_pat;
1806                         }
1807
1808                         rc = backend_group( op, e, &bv, &op->o_ndn,
1809                                 b->a_group_oc, b->a_group_at );
1810
1811                         if ( ndn.bv_val ) {
1812                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1813                         }
1814
1815                         if ( rc != 0 ) {
1816                                 continue;
1817                         }
1818                 }
1819
1820                 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1821                         struct berval   bv;
1822                         char            buf[ACL_BUF_SIZE];
1823
1824                         Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1825                                 b->a_set_pat.bv_val, 0, 0 );
1826
1827                         if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1828                                 int             tmp_nmatch;
1829                                 regmatch_t      tmp_matches[2],
1830                                                 *tmp_matchesp = tmp_matches;
1831                                 int             rc = 0;
1832
1833                                 bv.bv_len = sizeof( buf ) - 1;
1834                                 bv.bv_val = buf;
1835
1836                                 rc = 0;
1837
1838                                 switch ( a->acl_dn_style ) {
1839                                 case ACL_STYLE_REGEX:
1840                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1841                                                 tmp_matchesp = matches;
1842                                                 tmp_nmatch = nmatch;
1843                                                 break;
1844                                         }
1845
1846                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1847                                 case ACL_STYLE_BASE:
1848                                         tmp_matches[0].rm_so = 0;
1849                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1850                                         tmp_nmatch = 1;
1851                                         break;
1852
1853                                 case ACL_STYLE_ONE:
1854                                 case ACL_STYLE_SUBTREE:
1855                                 case ACL_STYLE_CHILDREN:
1856                                         tmp_matches[0].rm_so = 0;
1857                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1858                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1859                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1860                                         tmp_nmatch = 2;
1861                                         break;
1862
1863                                 default:
1864                                         /* error */
1865                                         rc = 1;
1866                                         break;
1867                                 }
1868
1869                                 if ( rc ) {
1870                                         continue;
1871                                 }
1872                                 
1873                                 if ( acl_string_expand( &bv, &b->a_set_pat,
1874                                                 e->e_nname.bv_val,
1875                                                 tmp_nmatch, tmp_matchesp ) )
1876                                 {
1877                                         continue;
1878                                 }
1879
1880                         } else {
1881                                 bv = b->a_set_pat;
1882                         }
1883                         
1884                         if ( acl_match_set( &bv, op, e, NULL ) == 0 ) {
1885                                 continue;
1886                         }
1887                 }
1888
1889                 if ( b->a_authz.sai_ssf ) {
1890                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1891                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
1892                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
1893                                 continue;
1894                         }
1895                 }
1896
1897                 if ( b->a_authz.sai_transport_ssf ) {
1898                         Debug( LDAP_DEBUG_ACL,
1899                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1900                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1901                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
1902                                 continue;
1903                         }
1904                 }
1905
1906                 if ( b->a_authz.sai_tls_ssf ) {
1907                         Debug( LDAP_DEBUG_ACL,
1908                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1909                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1910                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
1911                                 continue;
1912                         }
1913                 }
1914
1915                 if ( b->a_authz.sai_sasl_ssf ) {
1916                         Debug( LDAP_DEBUG_ACL,
1917                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1918                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1919                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
1920                                 continue;
1921                         }
1922                 }
1923
1924                 /* check for the "self" modifier in the <access> field */
1925                 if ( b->a_dn.a_self ) {
1926                         const char *dummy;
1927                         int rc, match = 0;
1928
1929                         ACL_RECORD_VALUE_STATE;
1930
1931                         /* must have DN syntax */
1932                         if ( desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName &&
1933                                         !is_at_syntax( desc->ad_type, SLAPD_NAMEUID_SYNTAX )) continue;
1934
1935                         /* check if the target is an attribute. */
1936                         if ( val == NULL ) continue;
1937
1938                         /* a DN must be present */
1939                         if ( BER_BVISEMPTY( &op->o_ndn ) ) {
1940                                 continue;
1941                         }
1942
1943                         /* target is attribute, check if the attribute value
1944                          * is the op dn.
1945                          */
1946                         rc = value_match( &match, desc,
1947                                         desc->ad_type->sat_equality, 0,
1948                                         val, &op->o_ndn, &dummy );
1949                         /* on match error or no match, fail the ACL clause */
1950                         if ( rc != LDAP_SUCCESS || match != 0 )
1951                                 continue;
1952                 }
1953 #ifdef SLAP_DYNACL
1954                 if ( b->a_dynacl ) {
1955                         slap_dynacl_t   *da;
1956                         slap_access_t   tgrant, tdeny;
1957
1958                         Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1959                                 0, 0, 0 );
1960
1961                         /* this case works different from the others above.
1962                          * since dynamic ACL's themselves give permissions, we need
1963                          * to first check b->a_access_mask, the ACL's access level.
1964                          */
1965                         /* first check if the right being requested
1966                          * is allowed by the ACL clause.
1967                          */
1968                         if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1969                                 continue;
1970                         }
1971
1972                         /* start out with nothing granted, nothing denied */
1973                         ACL_INVALIDATE(tgrant);
1974                         ACL_INVALIDATE(tdeny);
1975
1976                         for ( da = b->a_dynacl; da; da = da->da_next ) {
1977                                 slap_access_t   grant,
1978                                                 deny;
1979
1980                                 ACL_INVALIDATE(grant);
1981                                 ACL_INVALIDATE(deny);
1982
1983                                 Debug( LDAP_DEBUG_ACL, "    <= check a_dynacl: %s\n",
1984                                         da->da_name, 0, 0 );
1985
1986                                 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1987
1988                                 tgrant |= grant;
1989                                 tdeny |= deny;
1990                         }
1991
1992                         /* remove anything that the ACL clause does not allow */
1993                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1994                         tdeny &= ACL_PRIV_MASK;
1995
1996                         /* see if we have anything to contribute */
1997                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
1998                                 continue;
1999                         }
2000
2001                         /* this could be improved by changing slap_acl_mask so that it can deal with
2002                          * by clauses that return grant/deny pairs.  Right now, it does either
2003                          * additive or subtractive rights, but not both at the same time.  So,
2004                          * we need to combine the grant/deny pair into a single rights mask in
2005                          * a smart way:  if either grant or deny is "empty", then we use the
2006                          * opposite as is, otherwise we remove any denied rights from the grant
2007                          * rights mask and construct an additive mask.
2008                          */
2009                         if (ACL_IS_INVALID(tdeny)) {
2010                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
2011
2012                         } else if (ACL_IS_INVALID(tgrant)) {
2013                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2014
2015                         } else {
2016                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2017                         }
2018
2019                 } else
2020 #else /* !SLAP_DYNACL */
2021
2022                 /* NOTE: this entire block can be eliminated when SLAP_DYNACL
2023                  * moves outside of LDAP_DEVEL */
2024 #ifdef SLAPD_ACI_ENABLED
2025                 if ( b->a_aci_at != NULL ) {
2026                         Attribute       *at;
2027                         slap_access_t   grant, deny, tgrant, tdeny;
2028                         struct berval   parent_ndn;
2029                         BerVarray       bvals = NULL;
2030                         int             ret, stop;
2031 #ifdef LDAP_DEBUG
2032                         char            accessmaskbuf1[ACCESSMASK_MAXLEN];
2033 #endif /* DEBUG */
2034
2035                         Debug( LDAP_DEBUG_ACL, "    <= check a_aci_at: %s\n",
2036                                 b->a_aci_at->ad_cname.bv_val, 0, 0 );
2037
2038                         /* this case works different from the others above.
2039                          * since aci's themselves give permissions, we need
2040                          * to first check b->a_access_mask, the ACL's access level.
2041                          */
2042
2043                         if ( BER_BVISEMPTY( &e->e_nname ) ) {
2044                                 /* no ACIs in the root DSE */
2045                                 continue;
2046                         }
2047
2048                         /* first check if the right being requested
2049                          * is allowed by the ACL clause.
2050                          */
2051                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
2052                                 continue;
2053                         }
2054                         /* start out with nothing granted, nothing denied */
2055                         ACL_INIT(tgrant);
2056                         ACL_INIT(tdeny);
2057
2058                         /* get the aci attribute */
2059                         at = attr_find( e->e_attrs, b->a_aci_at );
2060                         if ( at != NULL ) {
2061 #if 0
2062                                 /* FIXME: this breaks acl caching;
2063                                  * see also ACL_RECORD_VALUE_STATE below */
2064                                 ACL_RECORD_VALUE_STATE;
2065 #endif
2066                                 /* the aci is an multi-valued attribute.  The
2067                                 * rights are determined by OR'ing the individual
2068                                 * rights given by the acis.
2069                                 */
2070                                 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2071                                         if ( aci_mask( op,
2072                                                 e, desc, val,
2073                                                 &at->a_nvals[i],
2074                                                 nmatch, matches,
2075                                                 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0 )
2076                                         {
2077                                                 tgrant |= grant;
2078                                                 tdeny |= deny;
2079                                         }
2080                                 }
2081                                 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2082                                           accessmask2str(tgrant, accessmaskbuf, 1), 
2083                                           accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2084
2085                         }
2086                         /* If the entry level aci didn't contain anything valid for the 
2087                          * current operation, climb up the tree and evaluate the
2088                          * acis with scope set to subtree
2089                          */
2090                         if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
2091                                 dnParent( &e->e_nname, &parent_ndn );
2092                                 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
2093                                         Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
2094                                         ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
2095                                         switch(ret){
2096                                         case LDAP_SUCCESS :
2097                                                 stop = 0;
2098                                                 if (!bvals){
2099                                                         break;
2100                                                 }
2101
2102                                                 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++ ) {
2103 #if 0
2104                                                         /* FIXME: this breaks acl caching;
2105                                                          * see also ACL_RECORD_VALUE_STATE above */
2106                                                         ACL_RECORD_VALUE_STATE;
2107 #endif
2108                                                         if ( aci_mask( op, e, desc, val, &bvals[i],
2109                                                                         nmatch, matches,
2110                                                                         &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2111                                                         {
2112                                                                 tgrant |= grant;
2113                                                                 tdeny |= deny;
2114                                                                 /* evaluation stops as soon as either a "deny" or a 
2115                                                                  * "grant" directive matches.
2116                                                                  */
2117                                                                 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
2118                                                                         stop = 1;
2119                                                                 }
2120                                                         }
2121                                                         Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n", 
2122                                                                 accessmask2str(tgrant, accessmaskbuf, 1),
2123                                                                 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2124                                                 }
2125                                                 break;
2126
2127                                         case LDAP_NO_SUCH_ATTRIBUTE:
2128                                                 /* just go on if the aci-Attribute is not present in
2129                                                  * the current entry 
2130                                                  */
2131                                                 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
2132                                                 stop = 0;
2133                                                 break;
2134
2135                                         case LDAP_NO_SUCH_OBJECT:
2136                                                 /* We have reached the base object */
2137                                                 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
2138                                                 stop = 1;
2139                                                 break;
2140
2141                                         default:
2142                                                 stop = 1;
2143                                                 break;
2144                                         }
2145                                         if (stop){
2146                                                 break;
2147                                         }
2148                                         dnParent( &parent_ndn, &parent_ndn );
2149                                 }
2150                         }
2151
2152
2153                         /* remove anything that the ACL clause does not allow */
2154                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
2155                         tdeny &= ACL_PRIV_MASK;
2156
2157                         /* see if we have anything to contribute */
2158                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
2159                                 continue;
2160                         }
2161
2162                         /* this could be improved by changing slap_acl_mask so that it can deal with
2163                          * by clauses that return grant/deny pairs.  Right now, it does either
2164                          * additive or subtractive rights, but not both at the same time.  So,
2165                          * we need to combine the grant/deny pair into a single rights mask in
2166                          * a smart way:  if either grant or deny is "empty", then we use the
2167                          * opposite as is, otherwise we remove any denied rights from the grant
2168                          * rights mask and construct an additive mask.
2169                          */
2170                         if (ACL_IS_INVALID(tdeny)) {
2171                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
2172
2173                         } else if (ACL_IS_INVALID(tgrant)) {
2174                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2175
2176                         } else {
2177                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2178                         }
2179
2180                 } else
2181 #endif /* SLAPD_ACI_ENABLED */
2182 #endif /* !SLAP_DYNACL */
2183                 {
2184                         modmask = b->a_access_mask;
2185                 }
2186
2187                 Debug( LDAP_DEBUG_ACL,
2188                         "<= acl_mask: [%d] applying %s (%s)\n",
2189                         i, accessmask2str( modmask, accessmaskbuf, 1 ), 
2190                         b->a_type == ACL_CONTINUE
2191                                 ? "continue"
2192                                 : b->a_type == ACL_BREAK
2193                                         ? "break"
2194                                         : "stop" );
2195                 /* save old mask */
2196                 oldmask = *mask;
2197
2198                 if( ACL_IS_ADDITIVE(modmask) ) {
2199                         /* add privs */
2200                         ACL_PRIV_SET( *mask, modmask );
2201
2202                         /* cleanup */
2203                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2204
2205                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
2206                         /* substract privs */
2207                         ACL_PRIV_CLR( *mask, modmask );
2208
2209                         /* cleanup */
2210                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2211
2212                 } else {
2213                         /* assign privs */
2214                         *mask = modmask;
2215                 }
2216
2217                 a2pmask = *mask;
2218
2219                 Debug( LDAP_DEBUG_ACL,
2220                         "<= acl_mask: [%d] mask: %s\n",
2221                         i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
2222
2223                 if( b->a_type == ACL_CONTINUE ) {
2224                         continue;
2225
2226                 } else if ( b->a_type == ACL_BREAK ) {
2227                         return ACL_BREAK;
2228
2229                 } else {
2230                         return ACL_STOP;
2231                 }
2232         }
2233
2234         /* implicit "by * none" clause */
2235         ACL_INIT(*mask);
2236
2237         Debug( LDAP_DEBUG_ACL,
2238                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
2239                 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
2240         return ACL_STOP;
2241 }
2242
2243 /*
2244  * acl_check_modlist - check access control on the given entry to see if
2245  * it allows the given modifications by the user associated with op.
2246  * returns      1       if mods allowed ok
2247  *              0       mods not allowed
2248  */
2249
2250 int
2251 acl_check_modlist(
2252         Operation       *op,
2253         Entry   *e,
2254         Modifications   *mlist )
2255 {
2256         struct berval *bv;
2257         AccessControlState state = ACL_STATE_INIT;
2258         Backend *be;
2259         int be_null = 0;
2260         int ret = 1; /* default is access allowed */
2261
2262         be = op->o_bd;
2263         if ( be == NULL ) {
2264                 be = LDAP_STAILQ_FIRST(&backendDB);
2265                 be_null = 1;
2266                 op->o_bd = be;
2267         }
2268         assert( be != NULL );
2269
2270         /* short circuit root database access */
2271         if ( be_isroot( op ) ) {
2272                 Debug( LDAP_DEBUG_ACL,
2273                         "<= acl_access_allowed: granted to database root\n",
2274                     0, 0, 0 );
2275                 goto done;
2276         }
2277
2278         /* use backend default access if no backend acls */
2279         if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
2280                 Debug( LDAP_DEBUG_ACL,
2281                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
2282                         access2str( ACL_WRITE ),
2283                         op->o_bd->be_dfltaccess >= ACL_WRITE
2284                                 ? "granted" : "denied",
2285                         op->o_dn.bv_val );
2286                 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
2287                 goto done;
2288         }
2289
2290         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
2291                 /*
2292                  * Internal mods are ignored by ACL_WRITE checking
2293                  */
2294                 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
2295                         Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
2296                                 " modify access granted\n",
2297                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2298                         continue;
2299                 }
2300
2301                 /*
2302                  * no-user-modification operational attributes are ignored
2303                  * by ACL_WRITE checking as any found here are not provided
2304                  * by the user
2305                  */
2306                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
2307                                 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
2308                 {
2309                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
2310                                 " modify access granted\n",
2311                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2312                         continue;
2313                 }
2314
2315                 switch ( mlist->sml_op ) {
2316                 case LDAP_MOD_REPLACE:
2317                 case LDAP_MOD_INCREMENT:
2318                         /*
2319                          * We must check both permission to delete the whole
2320                          * attribute and permission to add the specific attributes.
2321                          * This prevents abuse from selfwriters.
2322                          */
2323                         if ( ! access_allowed( op, e,
2324                                 mlist->sml_desc, NULL,
2325                                 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2326                                 &state ) )
2327                         {
2328                                 ret = 0;
2329                                 goto done;
2330                         }
2331
2332                         if ( mlist->sml_values == NULL ) break;
2333
2334                         /* fall thru to check value to add */
2335
2336                 case LDAP_MOD_ADD:
2337                         assert( mlist->sml_values != NULL );
2338
2339                         for ( bv = mlist->sml_nvalues
2340                                         ? mlist->sml_nvalues : mlist->sml_values;
2341                                 bv->bv_val != NULL; bv++ )
2342                         {
2343                                 if ( ! access_allowed( op, e,
2344                                         mlist->sml_desc, bv,
2345                                         ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
2346                                         &state ) )
2347                                 {
2348                                         ret = 0;
2349                                         goto done;
2350                                 }
2351                         }
2352                         break;
2353
2354                 case LDAP_MOD_DELETE:
2355                         if ( mlist->sml_values == NULL ) {
2356                                 if ( ! access_allowed( op, e,
2357                                         mlist->sml_desc, NULL,
2358                                         ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2359                                         NULL ) )
2360                                 {
2361                                         ret = 0;
2362                                         goto done;
2363                                 }
2364                                 break;
2365                         }
2366                         for ( bv = mlist->sml_nvalues
2367                                         ? mlist->sml_nvalues : mlist->sml_values;
2368                                 bv->bv_val != NULL; bv++ )
2369                         {
2370                                 if ( ! access_allowed( op, e,
2371                                         mlist->sml_desc, bv,
2372                                         ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2373                                         &state ) )
2374                                 {
2375                                         ret = 0;
2376                                         goto done;
2377                                 }
2378                         }
2379                         break;
2380
2381                 case SLAP_MOD_SOFTADD:
2382                         /* allow adding attribute via modrdn thru */
2383                         break;
2384
2385                 default:
2386                         assert( 0 );
2387                         /* not reached */
2388                         ret = 0;
2389                         break;
2390                 }
2391         }
2392
2393 done:
2394         if (be_null) op->o_bd = NULL;
2395         return( ret );
2396 }
2397
2398 int
2399 acl_get_part(
2400         struct berval   *list,
2401         int             ix,
2402         char            sep,
2403         struct berval   *bv )
2404 {
2405         int     len;
2406         char    *p;
2407
2408         if ( bv ) {
2409                 BER_BVZERO( bv );
2410         }
2411         len = list->bv_len;
2412         p = list->bv_val;
2413         while ( len >= 0 && --ix >= 0 ) {
2414                 while ( --len >= 0 && *p++ != sep )
2415                         ;
2416         }
2417         while ( len >= 0 && *p == ' ' ) {
2418                 len--;
2419                 p++;
2420         }
2421         if ( len < 0 ) {
2422                 return -1;
2423         }
2424
2425         if ( !bv ) {
2426                 return 0;
2427         }
2428
2429         bv->bv_val = p;
2430         while ( --len >= 0 && *p != sep ) {
2431                 bv->bv_len++;
2432                 p++;
2433         }
2434         while ( bv->bv_len > 0 && *--p == ' ' ) {
2435                 bv->bv_len--;
2436         }
2437         
2438         return bv->bv_len;
2439 }
2440
2441 typedef struct acl_set_gather_t {
2442         SetCookie               *cookie;
2443         BerVarray               bvals;
2444 } acl_set_gather_t;
2445
2446 static int
2447 acl_set_cb_gather( Operation *op, SlapReply *rs )
2448 {
2449         acl_set_gather_t        *p = (acl_set_gather_t *)op->o_callback->sc_private;
2450         
2451         if ( rs->sr_type == REP_SEARCH ) {
2452                 BerValue        bvals[ 2 ];
2453                 BerVarray       bvalsp = NULL;
2454                 int             j;
2455
2456                 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2457                         AttributeDescription    *desc = rs->sr_attrs[ j ].an_desc;
2458                         
2459                         if ( desc == slap_schema.si_ad_entryDN ) {
2460                                 bvalsp = bvals;
2461                                 bvals[ 0 ] = rs->sr_entry->e_nname;
2462                                 BER_BVZERO( &bvals[ 1 ] );
2463
2464                         } else {
2465                                 Attribute       *a;
2466
2467                                 a = attr_find( rs->sr_entry->e_attrs, desc );
2468                                 if ( a != NULL ) {
2469                                         int     i;
2470
2471                                         for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2472                                                 ;
2473
2474                                         bvalsp = a->a_nvals;
2475                                 }
2476                         }
2477                 }
2478
2479                 if ( bvals ) {
2480                         p->bvals = slap_set_join( p->cookie, p->bvals,
2481                                         ( '|' | SLAP_SET_RREF ), bvalsp );
2482                 }
2483
2484         } else {
2485                 assert( rs->sr_type == REP_RESULT );
2486         }
2487
2488         return 0;
2489 }
2490
2491 BerVarray
2492 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2493 {
2494         AclSetCookie            *cp = (AclSetCookie *)cookie;
2495         int                     rc = 0;
2496         LDAPURLDesc             *ludp = NULL;
2497         Operation               op2 = { 0 };
2498         SlapReply               rs = {REP_RESULT};
2499         AttributeName           anlist[ 2 ], *anlistp = NULL;
2500         int                     nattrs = 0;
2501         slap_callback           cb = { NULL, acl_set_cb_gather, NULL, NULL };
2502         acl_set_gather_t        p = { 0 };
2503         const char              *text = NULL;
2504         static struct berval    defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2505
2506         /* this routine needs to return the bervals instead of
2507          * plain strings, since syntax is not known.  It should
2508          * also return the syntax or some "comparison cookie".
2509          */
2510         if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2511                 return acl_set_gather2( cookie, name, desc );
2512         }
2513
2514         rc = ldap_url_parse( name->bv_val, &ludp );
2515         if ( rc != LDAP_URL_SUCCESS ) {
2516                 rc = LDAP_PROTOCOL_ERROR;
2517                 goto url_done;
2518         }
2519         
2520         if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2521         {
2522                 /* host part must be empty */
2523                 /* extensions parts must be empty */
2524                 rc = LDAP_PROTOCOL_ERROR;
2525                 goto url_done;
2526         }
2527
2528         /* Grab the searchbase and see if an appropriate database can be found */
2529         ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2530         rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2531                         &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2532         BER_BVZERO( &op2.o_req_dn );
2533         if ( rc != LDAP_SUCCESS ) {
2534                 goto url_done;
2535         }
2536
2537         op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2538         if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2539                 rc = LDAP_NO_SUCH_OBJECT;
2540                 goto url_done;
2541         }
2542
2543         /* Grab the filter */
2544         if ( ludp->lud_filter ) {
2545                 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2546                                 cp->asc_op->o_tmpmemctx );
2547                 
2548         } else {
2549                 op2.ors_filterstr = defaultFilter_bv;
2550         }
2551
2552         op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2553         if ( op2.ors_filter == NULL ) {
2554                 rc = LDAP_PROTOCOL_ERROR;
2555                 goto url_done;
2556         }
2557
2558         /* Grab the scope */
2559         op2.ors_scope = ludp->lud_scope;
2560
2561         /* Grap the attributes */
2562         if ( ludp->lud_attrs ) {
2563                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2564                         ;
2565
2566                 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2567                                 cp->asc_op->o_tmpmemctx );
2568
2569                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2570                         ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2571                         anlistp[ nattrs ].an_desc = NULL;
2572                         rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2573                                         &anlistp[ nattrs ].an_desc, &text );
2574                         if ( rc != LDAP_SUCCESS ) {
2575                                 goto url_done;
2576                         }
2577                 }
2578
2579         } else {
2580                 anlistp = anlist;
2581         }
2582
2583         anlistp[ nattrs ].an_name = desc->ad_cname;
2584         anlistp[ nattrs ].an_desc = desc;
2585
2586         BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2587         
2588         p.cookie = cookie;
2589         
2590         op2.o_hdr = cp->asc_op->o_hdr;
2591         op2.o_tag = LDAP_REQ_SEARCH;
2592         op2.o_ndn = op2.o_bd->be_rootndn;
2593         op2.o_callback = &cb;
2594         slap_op_time( &op2.o_time, &op2.o_tincr );
2595         op2.o_do_not_cache = 1;
2596         op2.o_is_auth_check = 0;
2597         ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2598         op2.ors_slimit = SLAP_NO_LIMIT;
2599         op2.ors_tlimit = SLAP_NO_LIMIT;
2600         op2.ors_attrs = anlistp;
2601         op2.ors_attrsonly = 0;
2602         op2.o_private = cp->asc_op->o_private;
2603
2604         cb.sc_private = &p;
2605
2606         rc = op2.o_bd->be_search( &op2, &rs );
2607         if ( rc != 0 ) {
2608                 goto url_done;
2609         }
2610
2611 url_done:;
2612         if ( op2.ors_filter ) {
2613                 filter_free_x( cp->asc_op, op2.ors_filter );
2614         }
2615         if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2616                 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2617         }
2618         if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2619                 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2620         }
2621         if ( ludp ) {
2622                 ldap_free_urldesc( ludp );
2623         }
2624         if ( anlistp && anlistp != anlist ) {
2625                 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2626         }
2627
2628         return p.bvals;
2629 }
2630
2631 BerVarray
2632 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2633 {
2634         AclSetCookie    *cp = (AclSetCookie *)cookie;
2635         BerVarray       bvals = NULL;
2636         struct berval   ndn;
2637         int             rc = 0;
2638
2639         /* this routine needs to return the bervals instead of
2640          * plain strings, since syntax is not known.  It should
2641          * also return the syntax or some "comparison cookie".
2642          */
2643         rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2644         if ( rc == LDAP_SUCCESS ) {
2645                 if ( desc == slap_schema.si_ad_entryDN ) {
2646                         bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2647                                         cp->asc_op->o_tmpmemctx );
2648                         bvals[ 0 ] = ndn;
2649                         BER_BVZERO( &bvals[ 1 ] );
2650                         BER_BVZERO( &ndn );
2651
2652                 } else {
2653                         backend_attribute( cp->asc_op,
2654                                 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2655                 }
2656
2657                 if ( !BER_BVISNULL( &ndn ) ) {
2658                         slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2659                 }
2660         }
2661
2662         return bvals;
2663 }
2664
2665 int
2666 acl_match_set (
2667         struct berval *subj,
2668         Operation *op,
2669         Entry *e,
2670         struct berval *default_set_attribute )
2671 {
2672         struct berval   set = BER_BVNULL;
2673         int             rc = 0;
2674         AclSetCookie    cookie;
2675
2676         if ( default_set_attribute == NULL ) {
2677                 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2678
2679         } else {
2680                 struct berval           subjdn, ndn = BER_BVNULL;
2681                 struct berval           setat;
2682                 BerVarray               bvals=NULL;
2683                 const char              *text;
2684                 AttributeDescription    *desc = NULL;
2685
2686                 /* format of string is "entry/setAttrName" */
2687                 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2688                         return 0;
2689                 }
2690
2691                 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2692                         setat = *default_set_attribute;
2693                 }
2694
2695                 /*
2696                  * NOTE: dnNormalize honors the ber_len field
2697                  * as the length of the dn to be normalized
2698                  */
2699                 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2700                         if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2701                         {
2702                                 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2703                                 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2704                                         int     i;
2705
2706                                         set = bvals[0];
2707                                         BER_BVZERO( &bvals[0] );
2708                                         for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2709                                                 /* count */ ;
2710                                         bvals[0].bv_val = bvals[i-1].bv_val;
2711                                         BER_BVZERO( &bvals[i-1] );
2712                                 }
2713                                 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2714                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2715                         }
2716                 }
2717         }
2718
2719         if ( !BER_BVISNULL( &set ) ) {
2720                 cookie.asc_op = op;
2721                 cookie.asc_e = e;
2722                 rc = ( slap_set_filter(
2723                         acl_set_gather,
2724                         (SetCookie *)&cookie, &set,
2725                         &op->o_ndn, &e->e_nname, NULL ) > 0 );
2726                 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2727         }
2728
2729         return(rc);
2730 }
2731
2732 #ifdef SLAP_DYNACL
2733
2734 /*
2735  * dynamic ACL infrastructure
2736  */
2737 static slap_dynacl_t    *da_list = NULL;
2738
2739 int
2740 slap_dynacl_register( slap_dynacl_t *da )
2741 {
2742         slap_dynacl_t   *tmp;
2743
2744         for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2745                 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2746                         break;
2747                 }
2748         }
2749
2750         if ( tmp != NULL ) {
2751                 return -1;
2752         }
2753         
2754         if ( da->da_mask == NULL ) {
2755                 return -1;
2756         }
2757         
2758         da->da_private = NULL;
2759         da->da_next = da_list;
2760         da_list = da;
2761
2762         return 0;
2763 }
2764
2765 static slap_dynacl_t *
2766 slap_dynacl_next( slap_dynacl_t *da )
2767 {
2768         if ( da ) {
2769                 return da->da_next;
2770         }
2771         return da_list;
2772 }
2773
2774 slap_dynacl_t *
2775 slap_dynacl_get( const char *name )
2776 {
2777         slap_dynacl_t   *da;
2778
2779         for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2780                 if ( strcasecmp( da->da_name, name ) == 0 ) {
2781                         break;
2782                 }
2783         }
2784
2785         return da;
2786 }
2787 #endif /* SLAP_DYNACL */
2788
2789 /*
2790  * statically built-in dynamic ACL initialization
2791  */
2792 static int (*acl_init_func[])( void ) = {
2793 #ifdef SLAPD_ACI_ENABLED
2794 #ifdef SLAP_DYNACL
2795         dynacl_aci_init,
2796 #else /* !SLAP_DYNACL */
2797         aci_init,
2798 #endif /* !SLAP_DYNACL */
2799 #endif /* SLAPD_ACI_ENABLED */
2800
2801         NULL
2802 };
2803
2804 int
2805 acl_init( void )
2806 {
2807         int     i, rc;
2808
2809         for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2810                 rc = (*(acl_init_func[ i ]))();
2811                 if ( rc != 0 ) {
2812                         return rc;
2813                 }
2814         }
2815
2816         return 0;
2817 }
2818
2819 int
2820 acl_string_expand(
2821         struct berval   *bv,
2822         struct berval   *pat,
2823         char            *match,
2824         int             nmatch,
2825         regmatch_t      *matches)
2826 {
2827         ber_len_t       size;
2828         char   *sp;
2829         char   *dp;
2830         int     flag;
2831
2832         size = 0;
2833         bv->bv_val[0] = '\0';
2834         bv->bv_len--; /* leave space for lone $ */
2835
2836         flag = 0;
2837         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2838                 sp < pat->bv_val + pat->bv_len ; sp++ )
2839         {
2840                 /* did we previously see a $ */
2841                 if ( flag ) {
2842                         if ( flag == 1 && *sp == '$' ) {
2843                                 *dp++ = '$';
2844                                 size++;
2845                                 flag = 0;
2846
2847                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2848                                 flag = 2;
2849
2850                         } else if ( *sp >= '0' && *sp <= '9' ) {
2851                                 int     n;
2852                                 int     i;
2853                                 int     l;
2854
2855                                 n = *sp - '0';
2856
2857                                 if ( flag == 2 ) {
2858                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2859                                                 if ( *sp >= '0' && *sp <= '9' ) {
2860                                                         n = 10*n + ( *sp - '0' );
2861                                                 }
2862                                         }
2863
2864                                         if ( *sp != /*'{'*/ '}' ) {
2865                                                 /* FIXME: error */
2866                                                 return 1;
2867                                         }
2868                                 }
2869
2870                                 if ( n >= nmatch ) {
2871                                         /* FIXME: error */
2872                                         return 1;
2873                                 }
2874                                 
2875                                 *dp = '\0';
2876                                 i = matches[n].rm_so;
2877                                 l = matches[n].rm_eo; 
2878                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2879                                         *dp++ = match[i];
2880                                 }
2881                                 *dp = '\0';
2882
2883                                 flag = 0;
2884                         }
2885                 } else {
2886                         if (*sp == '$') {
2887                                 flag = 1;
2888                         } else {
2889                                 *dp++ = *sp;
2890                                 size++;
2891                         }
2892                 }
2893         }
2894
2895         if ( flag ) {
2896                 /* must have ended with a single $ */
2897                 *dp++ = '$';
2898                 size++;
2899         }
2900
2901         *dp = '\0';
2902         bv->bv_len = size;
2903
2904         Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2905         Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2906
2907         return 0;
2908 }
2909
2910 static int
2911 regex_matches(
2912         struct berval   *pat,           /* pattern to expand and match against */
2913         char            *str,           /* string to match against pattern */
2914         char            *buf,           /* buffer with $N expansion variables */
2915         int             nmatch, /* size of the matches array */
2916         regmatch_t      *matches        /* offsets in buffer for $N expansion variables */
2917 )
2918 {
2919         regex_t re;
2920         char newbuf[ACL_BUF_SIZE];
2921         struct berval bv;
2922         int     rc;
2923
2924         bv.bv_len = sizeof( newbuf ) - 1;
2925         bv.bv_val = newbuf;
2926
2927         if (str == NULL) {
2928                 str = "";
2929         };
2930
2931         acl_string_expand( &bv, pat, buf, nmatch, matches );
2932         rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2933         if ( rc ) {
2934                 char error[ACL_BUF_SIZE];
2935                 regerror( rc, &re, error, sizeof( error ) );
2936
2937                 Debug( LDAP_DEBUG_TRACE,
2938                     "compile( \"%s\", \"%s\") failed %s\n",
2939                         pat->bv_val, str, error );
2940                 return( 0 );
2941         }
2942
2943         rc = regexec( &re, str, 0, NULL, 0 );
2944         regfree( &re );
2945
2946         Debug( LDAP_DEBUG_TRACE,
2947             "=> regex_matches: string:   %s\n", str, 0, 0 );
2948         Debug( LDAP_DEBUG_TRACE,
2949             "=> regex_matches: rc: %d %s\n",
2950                 rc, !rc ? "matches" : "no matches", 0 );
2951         return( !rc );
2952 }
2953