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