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