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