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