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