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