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