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