]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
Sync with HEAD
[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 const struct berval     aci_bv[] = {
46         BER_BVC("entry"),
47         BER_BVC("children"),
48         BER_BVC("onelevel"),
49         BER_BVC("subtree"),
50         BER_BVC("[entry]"),
51         BER_BVC("[all]"),
52         BER_BVC("access-id"),
53 #if 0
54         BER_BVC("anonymous"),
55 #endif
56         BER_BVC("public"),
57         BER_BVC("users"),
58         BER_BVC("self"),
59         BER_BVC("dnattr"),
60         BER_BVC("group"),
61         BER_BVC("role"),
62         BER_BVC("set"),
63         BER_BVC("set-ref"),
64         BER_BVC("grant"),
65         BER_BVC("deny"),
66
67         BER_BVC("IP="),
68 #ifdef LDAP_PF_LOCAL
69         BER_BVC("PATH="),
70 #if 0
71         BER_BVC(LDAP_DIRSEP),
72 #endif
73 #endif /* LDAP_PF_LOCAL */
74         
75         BER_BVC(SLAPD_GROUP_CLASS),
76         BER_BVC(SLAPD_GROUP_ATTR),
77         BER_BVC(SLAPD_ROLE_CLASS),
78         BER_BVC(SLAPD_ROLE_ATTR),
79
80         BER_BVC(SLAPD_ACI_SET_ATTR)
81 };
82
83 static AccessControl * slap_acl_get(
84         AccessControl *ac, int *count,
85         Operation *op, Entry *e,
86         AttributeDescription *desc,
87         struct berval *val,
88         int nmatch, regmatch_t *matches,
89         AccessControlState *state );
90
91 static slap_control_t slap_acl_mask(
92         AccessControl *ac, slap_mask_t *mask,
93         Operation *op, Entry *e,
94         AttributeDescription *desc,
95         struct berval *val,
96         int nmatch,
97         regmatch_t *matches,
98         int count,
99         AccessControlState *state );
100
101 static int      regex_matches(
102         struct berval *pat, char *str, char *buf,
103         int nmatch, regmatch_t *matches);
104
105 typedef struct AclSetCookie {
106         SetCookie       asc_cookie;
107 #define asc_op          asc_cookie.set_op
108         Entry           *asc_e;
109 } AclSetCookie;
110
111 SLAP_SET_GATHER acl_set_gather;
112 SLAP_SET_GATHER acl_set_gather2;
113
114 /*
115  * access_allowed - check whether op->o_ndn is allowed the requested access
116  * to entry e, attribute attr, value val.  if val is null, access to
117  * the whole attribute is assumed (all values).
118  *
119  * This routine loops through all access controls and calls
120  * slap_acl_mask() on each applicable access control.
121  * The loop exits when a definitive answer is reached or
122  * or no more controls remain.
123  *
124  * returns:
125  *              0       access denied
126  *              1       access granted
127  *
128  * Notes:
129  * - can be legally called with op == NULL
130  * - can be legally called with op->o_bd == NULL
131  */
132
133 #ifdef SLAP_OVERLAY_ACCESS
134 int
135 slap_access_always_allowed(
136         Operation               *op,
137         Entry                   *e,
138         AttributeDescription    *desc,
139         struct berval           *val,
140         slap_access_t           access,
141         AccessControlState      *state,
142         slap_mask_t             *maskp )
143 {
144         assert( maskp != NULL );
145
146         /* assign all */
147         ACL_LVL_ASSIGN_MANAGE( *maskp );
148
149         return 1;
150 }
151
152 int
153 slap_access_allowed(
154         Operation               *op,
155         Entry                   *e,
156         AttributeDescription    *desc,
157         struct berval           *val,
158         slap_access_t           access,
159         AccessControlState      *state,
160         slap_mask_t             *maskp )
161 {
162         int                             ret = 1;
163         int                             count;
164         AccessControl                   *a = NULL;
165
166 #ifdef LDAP_DEBUG
167         char                            accessmaskbuf[ACCESSMASK_MAXLEN];
168 #endif
169         slap_mask_t                     mask;
170         slap_control_t                  control;
171         slap_access_t                   access_level;
172         const char                      *attr;
173         regmatch_t                      matches[MAXREMATCHES];
174         int                             st_same_attr = 0;
175
176         assert( op != NULL );
177         assert( e != NULL );
178         assert( desc != NULL );
179         assert( maskp != NULL );
180
181         access_level = ACL_LEVEL( access );
182         attr = desc->ad_cname.bv_val;
183
184         assert( attr != NULL );
185
186         ACL_INIT( mask );
187
188         /* grant database root access */
189         if ( be_isroot( op ) ) {
190                 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
191                 mask = ACL_LVL_MANAGE;
192                 goto done;
193         }
194
195         /*
196          * no-user-modification operational attributes are ignored
197          * by ACL_WRITE checking as any found here are not provided
198          * by the user
199          *
200          * NOTE: but they are not ignored for ACL_MANAGE, because
201          * if we get here it means a non-root user is trying to 
202          * manage data, so we need to check its privileges.
203          */
204         if ( access_level == ACL_WRITE
205                 && is_at_no_user_mod( desc->ad_type )
206                 && desc != slap_schema.si_ad_entry
207                 && desc != slap_schema.si_ad_children )
208         {
209                 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
210                         " %s access granted\n",
211                         attr, 0, 0 );
212                 goto done;
213         }
214
215         /* use backend default access if no backend acls */
216         if ( op->o_bd->be_acl == NULL ) {
217                 int     i;
218
219                 Debug( LDAP_DEBUG_ACL,
220                         "=> slap_access_allowed: backend default %s "
221                         "access %s to \"%s\"\n",
222                         access2str( access ),
223                         op->o_bd->be_dfltaccess >= access_level ? "granted" : "denied",
224                         op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
225                 ret = op->o_bd->be_dfltaccess >= access_level;
226
227                 mask = ACL_PRIV_LEVEL;
228                 for ( i = ACL_NONE; i <= op->o_bd->be_dfltaccess; i++ ) {
229                         ACL_PRIV_SET( mask, ACL_ACCESS2PRIV( i ) );
230                 }
231
232                 goto done;
233         }
234
235         ret = 0;
236         control = ACL_BREAK;
237
238         if ( st_same_attr ) {
239                 assert( state->as_vd_acl != NULL );
240
241                 a = state->as_vd_acl;
242                 count = state->as_vd_acl_count;
243                 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
244                         mask = state->as_vd_acl_mask;
245                         AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
246                         goto vd_access;
247                 }
248
249         } else {
250                 if ( state ) state->as_vi_acl = NULL;
251                 a = NULL;
252                 ACL_PRIV_ASSIGN( mask, *maskp );
253                 count = 0;
254                 memset( matches, '\0', sizeof( matches ) );
255         }
256
257         while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
258                 MAXREMATCHES, matches, state ) ) != NULL )
259         {
260                 int i;
261
262                 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
263                         Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
264                                 (int)matches[i].rm_so, (int)matches[i].rm_eo );
265                         if ( matches[i].rm_so <= matches[0].rm_eo ) {
266                                 int n;
267                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
268                                         Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
269                                 }
270                         }
271                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
272                 }
273
274                 if ( state ) {
275                         if ( state->as_vi_acl == a &&
276                                 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
277                         {
278                                 Debug( LDAP_DEBUG_ACL,
279                                         "=> slap_access_allowed: result from state (%s)\n",
280                                         attr, 0, 0 );
281                                 ret = state->as_result;
282                                 goto done;
283                         } else {
284                                 Debug( LDAP_DEBUG_ACL,
285                                         "=> slap_access_allowed: no res from state (%s)\n",
286                                         attr, 0, 0 );
287                         }
288                 }
289
290 vd_access:
291                 control = slap_acl_mask( a, &mask, op,
292                         e, desc, val, MAXREMATCHES, matches, count, state );
293
294                 if ( control != ACL_BREAK ) {
295                         break;
296                 }
297
298                 memset( matches, '\0', sizeof( matches ) );
299         }
300
301         if ( ACL_IS_INVALID( mask ) ) {
302                 Debug( LDAP_DEBUG_ACL,
303                         "=> slap_access_allowed: \"%s\" (%s) invalid!\n",
304                         e->e_dn, attr, 0 );
305                 ACL_PRIV_ASSIGN( mask, *maskp );
306
307         } else if ( control == ACL_BREAK ) {
308                 Debug( LDAP_DEBUG_ACL,
309                         "=> slap_access_allowed: no more rules\n", 0, 0, 0 );
310
311                 goto done;
312         }
313
314         ret = ACL_GRANT( mask, access );
315
316         Debug( LDAP_DEBUG_ACL,
317                 "=> slap_access_allowed: %s access %s by %s\n",
318                 access2str( access ), ret ? "granted" : "denied",
319                 accessmask2str( mask, accessmaskbuf, 1 ) );
320
321 done:
322         ACL_PRIV_ASSIGN( *maskp, mask );
323         return ret;
324 }
325
326 int
327 fe_access_allowed(
328         Operation               *op,
329         Entry                   *e,
330         AttributeDescription    *desc,
331         struct berval           *val,
332         slap_access_t           access,
333         AccessControlState      *state,
334         slap_mask_t             *maskp )
335 {
336         BackendDB               *be_orig;
337         int                     rc;
338
339         /*
340          * NOTE: control gets here if FIXME
341          * if an appropriate backend cannot be selected for the operation,
342          * we assume that the frontend should handle this
343          * FIXME: should select_backend() take care of this,
344          * and return frontendDB instead of NULL?  maybe for some value
345          * of the flags?
346          */
347         be_orig = op->o_bd;
348
349         op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
350         if ( op->o_bd == NULL ) {
351                 op->o_bd = frontendDB;
352         }
353         rc = slap_access_allowed( op, e, desc, val, access, state, maskp );
354         op->o_bd = be_orig;
355
356         return rc;
357 }
358
359 int
360 access_allowed_mask(
361         Operation               *op,
362         Entry                   *e,
363         AttributeDescription    *desc,
364         struct berval           *val,
365         slap_access_t           access,
366         AccessControlState      *state,
367         slap_mask_t             *maskp )
368 {
369         int                             ret = 1;
370         AccessControl                   *a = NULL;
371         int                             be_null = 0;
372
373 #ifdef LDAP_DEBUG
374         char                            accessmaskbuf[ACCESSMASK_MAXLEN];
375 #endif
376         slap_mask_t                     mask;
377         slap_access_t                   access_level;
378         const char                      *attr;
379         int                             st_same_attr = 0;
380         static AccessControlState       state_init = ACL_STATE_INIT;
381
382         assert( e != NULL );
383         assert( desc != NULL );
384
385         access_level = ACL_LEVEL( access );
386
387         assert( access_level > ACL_NONE );
388
389         ACL_INIT( mask );
390         if ( maskp ) ACL_INVALIDATE( *maskp );
391
392         attr = desc->ad_cname.bv_val;
393
394         assert( attr != NULL );
395
396         if ( op ) {
397                 if ( op->o_is_auth_check &&
398                         ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
399                 {
400                         access = ACL_AUTH;
401
402                 } else if ( get_manageDIT( op ) && access_level == ACL_WRITE &&
403                         desc == slap_schema.si_ad_entry )
404                 {
405                         access = ACL_MANAGE;
406                 }
407         }
408
409         if ( state ) {
410                 if ( state->as_vd_ad == desc ) {
411                         if ( state->as_recorded ) {
412                                 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
413                                         val == NULL )
414                                 {
415                                         return state->as_result;
416
417                                 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
418                                         val != NULL && state->as_vd_acl == NULL )
419                                 {
420                                         return state->as_result;
421                                 }
422                         }
423                         st_same_attr = 1;
424                 } else {
425                         *state = state_init;
426                 }
427
428                 state->as_vd_ad = desc;
429         }
430
431         Debug( LDAP_DEBUG_ACL,
432                 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
433                 access2str( access ), e->e_dn, attr );
434
435         if ( op == NULL ) {
436                 /* no-op call */
437                 goto done;
438         }
439
440         if ( op->o_bd == NULL ) {
441                 op->o_bd = LDAP_STAILQ_FIRST( &backendDB );
442                 be_null = 1;
443
444 #ifdef LDAP_DEVEL
445                 /*
446                  * FIXME: experimental; use first backend rules
447                  * iff there is no global_acl (ITS#3100) */
448                 if ( frontendDB->be_acl != NULL ) {
449                         op->o_bd = frontendDB;
450                 }
451 #endif /* LDAP_DEVEL */
452         }
453         assert( op->o_bd != NULL );
454
455         /* this is enforced in backend_add() */
456         if ( op->o_bd->bd_info->bi_access_allowed ) {
457                 /* delegate to backend */
458                 ret = op->o_bd->bd_info->bi_access_allowed( op, e,
459                                 desc, val, access, state, &mask );
460
461         } else {
462                 BackendDB       *be_orig = op->o_bd;
463
464                 /* use default (but pass through frontend
465                  * for global ACL overlays) */
466                 op->o_bd = frontendDB;
467                 ret = frontendDB->bd_info->bi_access_allowed( op, e,
468                                 desc, val, access, state, &mask );
469                 op->o_bd = be_orig;
470         }
471
472         if ( !ret ) {
473                 if ( ACL_IS_INVALID( mask ) ) {
474                         Debug( LDAP_DEBUG_ACL,
475                                 "=> access_allowed: \"%s\" (%s) invalid!\n",
476                                 e->e_dn, attr, 0 );
477                         ACL_INIT( mask );
478
479                 } else {
480                         Debug( LDAP_DEBUG_ACL,
481                                 "=> access_allowed: no more rules\n", 0, 0, 0 );
482
483                         goto done;
484                 }
485         }
486
487         Debug( LDAP_DEBUG_ACL,
488                 "=> access_allowed: %s access %s by %s\n",
489                 access2str( access ), ret ? "granted" : "denied",
490                 accessmask2str( mask, accessmaskbuf, 1 ) );
491
492 done:
493         if ( state != NULL ) {
494                 /* If not value-dependent, save ACL in case of more attrs */
495                 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
496                         state->as_vi_acl = a;
497                         state->as_result = ret;
498                 }
499                 state->as_recorded |= ACL_STATE_RECORDED;
500         }
501         if ( be_null ) op->o_bd = NULL;
502         if ( maskp ) ACL_PRIV_ASSIGN( *maskp, mask );
503         return ret;
504 }
505
506 #else /* !SLAP_OVERLAY_ACCESS */
507
508 int
509 access_allowed_mask(
510         Operation               *op,
511         Entry                   *e,
512         AttributeDescription    *desc,
513         struct berval           *val,
514         slap_access_t           access,
515         AccessControlState      *state,
516         slap_mask_t             *maskp )
517 {
518         int                             ret = 1;
519         int                             count;
520         AccessControl                   *a = NULL;
521         Backend                         *be;
522         int                             be_null = 0;
523
524 #ifdef LDAP_DEBUG
525         char                            accessmaskbuf[ACCESSMASK_MAXLEN];
526 #endif
527         slap_mask_t                     mask;
528         slap_control_t                  control;
529         slap_access_t                   access_level;
530         const char                      *attr;
531         regmatch_t                      matches[MAXREMATCHES];
532         int                             st_same_attr = 0;
533         static AccessControlState       state_init = ACL_STATE_INIT;
534
535         assert( e != NULL );
536         assert( desc != NULL );
537
538         access_level = ACL_LEVEL( access );
539
540         assert( access_level > ACL_NONE );
541         if ( maskp ) ACL_INVALIDATE( *maskp );
542
543         attr = desc->ad_cname.bv_val;
544
545         assert( attr != NULL );
546
547         if ( op ) {
548                 if ( op->o_is_auth_check &&
549                         ( access_level == ACL_SEARCH || access_level == ACL_READ ) )
550                 {
551                         access = ACL_AUTH;
552
553                 } else if ( get_manageDIT( op ) && access_level == ACL_WRITE &&
554                         desc == slap_schema.si_ad_entry )
555                 {
556                         access = ACL_MANAGE;
557                 }
558         }
559
560         if ( state ) {
561                 if ( state->as_vd_ad == desc ) {
562                         if ( state->as_recorded ) {
563                                 if ( ( state->as_recorded & ACL_STATE_RECORDED_NV ) &&
564                                         val == NULL )
565                                 {
566                                         return state->as_result;
567
568                                 } else if ( ( state->as_recorded & ACL_STATE_RECORDED_VD ) &&
569                                         val != NULL && state->as_vd_acl == NULL )
570                                 {
571                                         return state->as_result;
572                                 }
573                         }
574                         st_same_attr = 1;
575                 } else {
576                         *state = state_init;
577                 }
578
579                 state->as_vd_ad=desc;
580         }
581
582         Debug( LDAP_DEBUG_ACL,
583                 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
584                 access2str( access ), e->e_dn, attr );
585
586         if ( op == NULL ) {
587                 /* no-op call */
588                 goto done;
589         }
590
591         be = op->o_bd;
592         if ( be == NULL ) {
593                 be = LDAP_STAILQ_FIRST(&backendDB);
594                 be_null = 1;
595 #ifdef LDAP_DEVEL
596                 /*
597                  * FIXME: experimental; use first backend rules
598                  * iff there is no global_acl (ITS#3100) */
599                 if ( frontendDB->be_acl == NULL ) 
600 #endif
601                 {
602                         op->o_bd = be;
603                 }
604         }
605         assert( be != NULL );
606
607         /* grant database root access */
608         if ( be_isroot( op ) ) {
609                 Debug( LDAP_DEBUG_ACL, "<= root access granted\n", 0, 0, 0 );
610                 if ( maskp ) {
611                         mask = ACL_LVL_MANAGE;
612                 }
613
614                 goto done;
615         }
616
617         /*
618          * no-user-modification operational attributes are ignored
619          * by ACL_WRITE checking as any found here are not provided
620          * by the user
621          *
622          * NOTE: but they are not ignored for ACL_MANAGE, because
623          * if we get here it means a non-root user is trying to 
624          * manage data, so we need to check its privileges.
625          */
626         if ( access_level == ACL_WRITE && is_at_no_user_mod( desc->ad_type )
627                 && desc != slap_schema.si_ad_entry
628                 && desc != slap_schema.si_ad_children )
629         {
630                 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
631                         " %s access granted\n",
632                         attr, 0, 0 );
633                 goto done;
634         }
635
636         /* use backend default access if no backend acls */
637         if ( be->be_acl == NULL ) {
638                 Debug( LDAP_DEBUG_ACL,
639                         "=> access_allowed: backend default %s "
640                         "access %s to \"%s\"\n",
641                         access2str( access ),
642                         be->be_dfltaccess >= access_level ? "granted" : "denied",
643                         op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
644                 ret = be->be_dfltaccess >= access_level;
645
646                 if ( maskp ) {
647                         int     i;
648
649                         mask = ACL_PRIV_LEVEL;
650                         for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
651                                 mask |= ACL_ACCESS2PRIV( i );
652                         }
653                 }
654
655                 goto done;
656
657 #ifdef notdef
658         /* be is always non-NULL */
659         /* use global default access if no global acls */
660         } else if ( be == NULL && frontendDB->be_acl == NULL ) {
661                 Debug( LDAP_DEBUG_ACL,
662                         "=> access_allowed: global default %s access %s to \"%s\"\n",
663                         access2str( access ),
664                         frontendDB->be_dfltaccess >= access_level ?
665                                 "granted" : "denied", op->o_dn.bv_val );
666                 ret = frontendDB->be_dfltaccess >= access_level;
667
668                 if ( maskp ) {
669                         int     i;
670
671                         mask = ACL_PRIV_LEVEL;
672                         for ( i = ACL_NONE; i <= global_default_access; i++ ) {
673                                 mask |= ACL_ACCESS2PRIV( i );
674                         }
675                 }
676
677                 goto done;
678 #endif
679         }
680
681         ret = 0;
682         control = ACL_BREAK;
683
684         if ( st_same_attr ) {
685                 assert( state->as_vd_acl != NULL );
686
687                 a = state->as_vd_acl;
688                 count = state->as_vd_acl_count;
689                 if ( !ACL_IS_INVALID( state->as_vd_acl_mask ) ) {
690                         mask = state->as_vd_acl_mask;
691                         AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
692                         goto vd_access;
693                 }
694
695         } else {
696                 if ( state ) state->as_vi_acl = NULL;
697                 a = NULL;
698                 ACL_INIT(mask);
699                 count = 0;
700                 memset( matches, '\0', sizeof(matches) );
701         }
702
703         while ( ( a = slap_acl_get( a, &count, op, e, desc, val,
704                 MAXREMATCHES, matches, state ) ) != NULL )
705         {
706                 int i;
707
708                 for ( i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++ ) {
709                         Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
710                                 (int)matches[i].rm_so, (int)matches[i].rm_eo );
711                         if ( matches[i].rm_so <= matches[0].rm_eo ) {
712                                 int n;
713                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++ ) {
714                                         Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
715                                 }
716                         }
717                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
718                 }
719
720                 if ( state ) {
721                         if ( state->as_vi_acl == a &&
722                                 ( state->as_recorded & ACL_STATE_RECORDED_NV ) )
723                         {
724                                 Debug( LDAP_DEBUG_ACL,
725                                         "access_allowed: result from state (%s)\n",
726                                         attr, 0, 0 );
727                                 ret = state->as_result;
728                                 goto done;
729                         } else {
730                                 Debug( LDAP_DEBUG_ACL,
731                                         "access_allowed: no res from state (%s)\n",
732                                         attr, 0, 0 );
733                         }
734                 }
735
736 vd_access:
737                 control = slap_acl_mask( a, &mask, op,
738                         e, desc, val, MAXREMATCHES, matches, count, state );
739
740                 if ( control != ACL_BREAK ) {
741                         break;
742                 }
743
744                 memset( matches, '\0', sizeof(matches) );
745         }
746
747         if ( ACL_IS_INVALID( mask ) ) {
748                 Debug( LDAP_DEBUG_ACL,
749                         "=> access_allowed: \"%s\" (%s) invalid!\n",
750                         e->e_dn, attr, 0 );
751                 ACL_INIT(mask);
752
753         } else if ( control == ACL_BREAK ) {
754                 Debug( LDAP_DEBUG_ACL,
755                         "=> access_allowed: no more rules\n", 0, 0, 0 );
756
757                 goto done;
758         }
759
760         Debug( LDAP_DEBUG_ACL,
761                 "=> access_allowed: %s access %s by %s\n",
762                 access2str( access ),
763                 ACL_GRANT(mask, access) ? "granted" : "denied",
764                 accessmask2str( mask, accessmaskbuf, 1 ) );
765
766         ret = ACL_GRANT(mask, access);
767
768 done:
769         if ( state != NULL ) {
770                 /* If not value-dependent, save ACL in case of more attrs */
771                 if ( !( state->as_recorded & ACL_STATE_RECORDED_VD ) ) {
772                         state->as_vi_acl = a;
773                         state->as_result = ret;
774                 }
775                 state->as_recorded |= ACL_STATE_RECORDED;
776         }
777         if ( be_null ) op->o_bd = NULL;
778         if ( maskp ) *maskp = mask;
779         return ret;
780 }
781
782 #endif /* SLAP_OVERLAY_ACCESS */
783
784 /*
785  * slap_acl_get - return the acl applicable to entry e, attribute
786  * attr.  the acl returned is suitable for use in subsequent calls to
787  * acl_access_allowed().
788  */
789
790 static AccessControl *
791 slap_acl_get(
792         AccessControl *a,
793         int                     *count,
794         Operation       *op,
795         Entry           *e,
796         AttributeDescription *desc,
797         struct berval   *val,
798         int                     nmatch,
799         regmatch_t      *matches,
800         AccessControlState *state )
801 {
802         const char *attr;
803         int dnlen, patlen;
804         AccessControl *prev;
805
806         assert( e != NULL );
807         assert( count != NULL );
808         assert( desc != NULL );
809
810         attr = desc->ad_cname.bv_val;
811
812         assert( attr != NULL );
813
814         if( a == NULL ) {
815                 if( op->o_bd == NULL ) {
816                         a = frontendDB->be_acl;
817                 } else {
818                         a = op->o_bd->be_acl;
819                 }
820                 prev = NULL;
821
822                 assert( a != NULL );
823
824         } else {
825                 prev = a;
826                 a = a->acl_next;
827         }
828
829         dnlen = e->e_nname.bv_len;
830
831         for ( ; a != NULL; a = a->acl_next ) {
832                 (*count) ++;
833
834                 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
835                         if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
836                                 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n", 
837                                         *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
838                                 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
839                                         continue;
840
841                         } else {
842                                 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n", 
843                                         *count, a->acl_dn_pat.bv_val, 0 );
844                                 patlen = a->acl_dn_pat.bv_len;
845                                 if ( dnlen < patlen )
846                                         continue;
847
848                                 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
849                                         /* base dn -- entire object DN must match */
850                                         if ( dnlen != patlen )
851                                                 continue;
852
853                                 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
854                                         ber_len_t       rdnlen = 0;
855                                         int             sep = 0;
856
857                                         if ( dnlen <= patlen )
858                                                 continue;
859
860                                         if ( patlen > 0 ) {
861                                                 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
862                                                         continue;
863                                                 sep = 1;
864                                         }
865
866                                         rdnlen = dn_rdnlen( NULL, &e->e_nname );
867                                         if ( rdnlen != dnlen - patlen - sep )
868                                                 continue;
869
870                                 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
871                                         if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
872                                                 continue;
873
874                                 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
875                                         if ( dnlen <= patlen )
876                                                 continue;
877                                         if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
878                                                 continue;
879                                 }
880
881                                 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
882                                         continue;
883                         }
884
885                         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
886                                 *count, 0, 0 );
887                 }
888
889                 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
890                         matches[0].rm_so = matches[0].rm_eo = -1;
891                         continue;
892                 }
893
894                 /* Is this ACL only for a specific value? */
895                 if ( a->acl_attrval.bv_len ) {
896                         if ( val == NULL ) {
897                                 continue;
898                         }
899
900                         if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
901                                 state->as_recorded |= ACL_STATE_RECORDED_VD;
902                                 state->as_vd_acl = a;
903                                 state->as_vd_acl_count = *count;
904                                 state->as_vd_access = a->acl_access;
905                                 state->as_vd_access_count = 1;
906                                 ACL_INVALIDATE( state->as_vd_acl_mask );
907                         }
908
909                         if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
910                                 Debug( LDAP_DEBUG_ACL,
911                                         "acl_get: valpat %s\n",
912                                         a->acl_attrval.bv_val, 0, 0 );
913                                 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
914                                 {
915                                         continue;
916                                 }
917
918                         } else {
919                                 int match = 0;
920                                 const char *text;
921                                 Debug( LDAP_DEBUG_ACL,
922                                         "acl_get: val %s\n",
923                                         a->acl_attrval.bv_val, 0, 0 );
924         
925                                 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
926                                         if (value_match( &match, desc,
927                                                 /* desc->ad_type->sat_equality */ a->acl_attrval_mr, 0,
928                                                 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
929                                                         match )
930                                                 continue;
931                                         
932                                 } else {
933                                         int             patlen, vdnlen;
934         
935                                         patlen = a->acl_attrval.bv_len;
936                                         vdnlen = val->bv_len;
937         
938                                         if ( vdnlen < patlen )
939                                                 continue;
940         
941                                         if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
942                                                 if ( vdnlen > patlen )
943                                                         continue;
944         
945                                         } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
946                                                 ber_len_t       rdnlen = 0;
947         
948                                                 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
949                                                         continue;
950         
951                                                 rdnlen = dn_rdnlen( NULL, val );
952                                                 if ( rdnlen != vdnlen - patlen - 1 )
953                                                         continue;
954         
955                                         } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
956                                                 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
957                                                         continue;
958         
959                                         } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
960                                                 if ( vdnlen <= patlen )
961                                                         continue;
962         
963                                                 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
964                                                         continue;
965                                         }
966         
967                                         if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
968                                                 continue;
969                                 }
970                         }
971                 }
972
973                 if ( a->acl_filter != NULL ) {
974                         ber_int_t rc = test_filter( NULL, e, a->acl_filter );
975                         if ( rc != LDAP_COMPARE_TRUE ) {
976                                 continue;
977                         }
978                 }
979
980                 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
981                        *count, attr, 0);
982                 return a;
983         }
984
985         Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
986         return( NULL );
987 }
988
989 static int
990 acl_mask_dn(
991         Operation               *op,
992         Entry                   *e,
993         AccessControl           *a,
994         int                     nmatch,
995         regmatch_t              *matches,
996         slap_dn_access          *b,
997         struct berval           *opndn )
998 {
999         /*
1000          * if access applies to the entry itself, and the
1001          * user is bound as somebody in the same namespace as
1002          * the entry, OR the given dn matches the dn pattern
1003          */
1004         /*
1005          * NOTE: styles "anonymous", "users" and "self" 
1006          * have been moved to enum slap_style_t, whose 
1007          * value is set in a_dn_style; however, the string
1008          * is maintaned in a_dn_pat.
1009          */
1010         if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
1011                 if ( !BER_BVISEMPTY( opndn ) ) {
1012                         return 1;
1013                 }
1014
1015         } else if ( b->a_style == ACL_STYLE_USERS ) {
1016                 if ( BER_BVISEMPTY( opndn ) ) {
1017                         return 1;
1018                 }
1019
1020         } else if ( b->a_style == ACL_STYLE_SELF ) {
1021                 struct berval   ndn, selfndn;
1022                 int             level;
1023
1024                 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
1025                         return 1;
1026                 }
1027
1028                 level = b->a_self_level;
1029                 if ( level < 0 ) {
1030                         selfndn = *opndn;
1031                         ndn = e->e_nname;
1032                         level = -level;
1033
1034                 } else {
1035                         ndn = *opndn;
1036                         selfndn = e->e_nname;
1037                 }
1038
1039                 for ( ; level > 0; level-- ) {
1040                         if ( BER_BVISEMPTY( &ndn ) ) {
1041                                 break;
1042                         }
1043                         dnParent( &ndn, &ndn );
1044                 }
1045                         
1046                 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
1047                 {
1048                         return 1;
1049                 }
1050
1051         } else if ( b->a_style == ACL_STYLE_REGEX ) {
1052                 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
1053                         int             tmp_nmatch;
1054                         regmatch_t      tmp_matches[2],
1055                                         *tmp_matchesp = tmp_matches;
1056
1057                         int             rc = 0;
1058
1059                         switch ( a->acl_dn_style ) {
1060                         case ACL_STYLE_REGEX:
1061                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1062                                         tmp_matchesp = matches;
1063                                         tmp_nmatch = nmatch;
1064                                         break;
1065                                 }
1066                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1067
1068                         case ACL_STYLE_BASE:
1069                                 tmp_matches[0].rm_so = 0;
1070                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1071                                 tmp_nmatch = 1;
1072                                 break;
1073
1074                         case ACL_STYLE_ONE:
1075                         case ACL_STYLE_SUBTREE:
1076                         case ACL_STYLE_CHILDREN:
1077                                 tmp_matches[0].rm_so = 0;
1078                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1079                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1080                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1081                                 tmp_nmatch = 2;
1082                                 break;
1083
1084                         default:
1085                                 /* error */
1086                                 rc = 1;
1087                                 break;
1088                         }
1089
1090                         if ( rc ) {
1091                                 return 1;
1092                         }
1093
1094                         if ( !regex_matches( &b->a_pat, opndn->bv_val,
1095                                 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
1096                         {
1097                                 return 1;
1098                         }
1099                 }
1100
1101         } else {
1102                 struct berval   pat;
1103                 ber_len_t       patlen, odnlen;
1104                 int             got_match = 0;
1105
1106                 if ( e->e_dn == NULL )
1107                         return 1;
1108
1109                 if ( b->a_expand ) {
1110                         struct berval   bv;
1111                         char            buf[ACL_BUF_SIZE];
1112                         
1113                         int             tmp_nmatch;
1114                         regmatch_t      tmp_matches[2],
1115                                         *tmp_matchesp = tmp_matches;
1116
1117                         int             rc = 0;
1118
1119                         bv.bv_len = sizeof( buf ) - 1;
1120                         bv.bv_val = buf;
1121
1122                         switch ( a->acl_dn_style ) {
1123                         case ACL_STYLE_REGEX:
1124                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1125                                         tmp_matchesp = matches;
1126                                         tmp_nmatch = nmatch;
1127                                         break;
1128                                 }
1129                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1130
1131                         case ACL_STYLE_BASE:
1132                                 tmp_matches[0].rm_so = 0;
1133                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1134                                 tmp_nmatch = 1;
1135                                 break;
1136
1137                         case ACL_STYLE_ONE:
1138                         case ACL_STYLE_SUBTREE:
1139                         case ACL_STYLE_CHILDREN:
1140                                 tmp_matches[0].rm_so = 0;
1141                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1142                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1143                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1144                                 tmp_nmatch = 2;
1145                                 break;
1146
1147                         default:
1148                                 /* error */
1149                                 rc = 1;
1150                                 break;
1151                         }
1152
1153                         if ( rc ) {
1154                                 return 1;
1155                         }
1156
1157                         if ( acl_string_expand( &bv, &b->a_pat, 
1158                                         e->e_nname.bv_val,
1159                                         tmp_nmatch, tmp_matchesp ) )
1160                         {
1161                                 return 1;
1162                         }
1163                         
1164                         if ( dnNormalize(0, NULL, NULL, &bv,
1165                                         &pat, op->o_tmpmemctx )
1166                                         != LDAP_SUCCESS )
1167                         {
1168                                 /* did not expand to a valid dn */
1169                                 return 1;
1170                         }
1171
1172                 } else {
1173                         pat = b->a_pat;
1174                 }
1175
1176                 patlen = pat.bv_len;
1177                 odnlen = opndn->bv_len;
1178                 if ( odnlen < patlen ) {
1179                         goto dn_match_cleanup;
1180
1181                 }
1182
1183                 if ( b->a_style == ACL_STYLE_BASE ) {
1184                         /* base dn -- entire object DN must match */
1185                         if ( odnlen != patlen ) {
1186                                 goto dn_match_cleanup;
1187                         }
1188
1189                 } else if ( b->a_style == ACL_STYLE_ONE ) {
1190                         ber_len_t       rdnlen = 0;
1191
1192                         if ( odnlen <= patlen ) {
1193                                 goto dn_match_cleanup;
1194                         }
1195
1196                         if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1197                                 goto dn_match_cleanup;
1198                         }
1199
1200                         rdnlen = dn_rdnlen( NULL, opndn );
1201                         if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
1202                                 goto dn_match_cleanup;
1203                         }
1204
1205                 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
1206                         if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1207                                 goto dn_match_cleanup;
1208                         }
1209
1210                 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
1211                         if ( odnlen <= patlen ) {
1212                                 goto dn_match_cleanup;
1213                         }
1214
1215                         if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1216                                 goto dn_match_cleanup;
1217                         }
1218
1219                 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
1220                         int             level = b->a_level;
1221                         struct berval   ndn;
1222
1223                         if ( odnlen <= patlen ) {
1224                                 goto dn_match_cleanup;
1225                         }
1226
1227                         if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
1228                         {
1229                                 goto dn_match_cleanup;
1230                         }
1231                         
1232                         ndn = *opndn;
1233                         for ( ; level > 0; level-- ) {
1234                                 if ( BER_BVISEMPTY( &ndn ) ) {
1235                                         goto dn_match_cleanup;
1236                                 }
1237                                 dnParent( &ndn, &ndn );
1238                                 if ( ndn.bv_len < patlen ) {
1239                                         goto dn_match_cleanup;
1240                                 }
1241                         }
1242                         
1243                         if ( ndn.bv_len != patlen ) {
1244                                 goto dn_match_cleanup;
1245                         }
1246                 }
1247
1248                 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
1249
1250 dn_match_cleanup:;
1251                 if ( pat.bv_val != b->a_pat.bv_val ) {
1252                         slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1253                 }
1254
1255                 if ( !got_match ) {
1256                         return 1;
1257                 }
1258         }
1259
1260         return 0;
1261 }
1262
1263 /*
1264  * Record value-dependent access control state
1265  */
1266 #define ACL_RECORD_VALUE_STATE do { \
1267                 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
1268                         state->as_recorded |= ACL_STATE_RECORDED_VD; \
1269                         state->as_vd_acl = a; \
1270                         AC_MEMCPY( state->as_vd_acl_matches, matches, \
1271                                 sizeof( state->as_vd_acl_matches )) ; \
1272                         state->as_vd_acl_count = count; \
1273                         state->as_vd_access = b; \
1274                         state->as_vd_access_count = i; \
1275                 } \
1276         } while( 0 )
1277
1278 static int
1279 acl_mask_dnattr(
1280         Operation               *op,
1281         Entry                   *e,
1282         struct berval           *val,
1283         AccessControl           *a,
1284         Access                  *b,
1285         int                     i,
1286         regmatch_t              *matches,
1287         int                     count,
1288         AccessControlState      *state,
1289         slap_dn_access          *bdn,
1290         struct berval           *opndn )
1291 {
1292         Attribute       *at;
1293         struct berval   bv;
1294         int             rc, match = 0;
1295         const char      *text;
1296         const char      *attr = bdn->a_at->ad_cname.bv_val;
1297
1298         assert( attr != NULL );
1299
1300         if ( BER_BVISEMPTY( opndn ) ) {
1301                 return 1;
1302         }
1303
1304         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1305         bv = *opndn;
1306
1307         /* see if asker is listed in dnattr */
1308         for ( at = attrs_find( e->e_attrs, bdn->a_at );
1309                 at != NULL;
1310                 at = attrs_find( at->a_next, bdn->a_at ) )
1311         {
1312                 if ( value_find_ex( bdn->a_at,
1313                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1314                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1315                         at->a_nvals,
1316                         &bv, op->o_tmpmemctx ) == 0 )
1317                 {
1318                         /* found it */
1319                         match = 1;
1320                         break;
1321                 }
1322         }
1323
1324         if ( match ) {
1325                 /* have a dnattr match. if this is a self clause then
1326                  * the target must also match the op dn.
1327                  */
1328                 if ( bdn->a_self ) {
1329                         /* check if the target is an attribute. */
1330                         if ( val == NULL ) return 1;
1331
1332                         /* target is attribute, check if the attribute value
1333                          * is the op dn.
1334                          */
1335                         rc = value_match( &match, bdn->a_at,
1336                                 bdn->a_at->ad_type->sat_equality, 0,
1337                                 val, &bv, &text );
1338                         /* on match error or no match, fail the ACL clause */
1339                         if ( rc != LDAP_SUCCESS || match != 0 )
1340                                 return 1;
1341                 }
1342
1343         } else {
1344                 /* no dnattr match, check if this is a self clause */
1345                 if ( ! bdn->a_self )
1346                         return 1;
1347
1348                 ACL_RECORD_VALUE_STATE;
1349                 
1350                 /* this is a self clause, check if the target is an
1351                  * attribute.
1352                  */
1353                 if ( val == NULL )
1354                         return 1;
1355
1356                 /* target is attribute, check if the attribute value
1357                  * is the op dn.
1358                  */
1359                 rc = value_match( &match, bdn->a_at,
1360                         bdn->a_at->ad_type->sat_equality, 0,
1361                         val, &bv, &text );
1362
1363                 /* on match error or no match, fail the ACL clause */
1364                 if ( rc != LDAP_SUCCESS || match != 0 )
1365                         return 1;
1366         }
1367
1368         return 0;
1369 }
1370
1371
1372 /*
1373  * slap_acl_mask - modifies mask based upon the given acl and the
1374  * requested access to entry e, attribute attr, value val.  if val
1375  * is null, access to the whole attribute is assumed (all values).
1376  *
1377  * returns      0       access NOT allowed
1378  *              1       access allowed
1379  */
1380
1381 static slap_control_t
1382 slap_acl_mask(
1383         AccessControl           *a,
1384         slap_mask_t             *mask,
1385         Operation               *op,
1386         Entry                   *e,
1387         AttributeDescription    *desc,
1388         struct berval           *val,
1389         int                     nmatch,
1390         regmatch_t              *matches,
1391         int                     count,
1392         AccessControlState      *state )
1393 {
1394         int             i;
1395         Access          *b;
1396 #ifdef LDAP_DEBUG
1397         char            accessmaskbuf[ACCESSMASK_MAXLEN];
1398 #if !defined( SLAP_DYNACL ) && defined( SLAPD_ACI_ENABLED )
1399         char            accessmaskbuf1[ACCESSMASK_MAXLEN];
1400 #endif /* !SLAP_DYNACL && SLAPD_ACI_ENABLED */
1401 #endif /* DEBUG */
1402         const char      *attr;
1403         slap_mask_t     a2pmask = ACL_ACCESS2PRIV( *mask );
1404
1405         assert( a != NULL );
1406         assert( mask != NULL );
1407         assert( desc != NULL );
1408
1409         attr = desc->ad_cname.bv_val;
1410
1411         assert( attr != NULL );
1412
1413         Debug( LDAP_DEBUG_ACL,
1414                 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1415                 e->e_dn, attr, 0 );
1416
1417         Debug( LDAP_DEBUG_ACL,
1418                 "=> acl_mask: to %s by \"%s\", (%s) \n",
1419                 val ? "value" : "all values",
1420                 op->o_ndn.bv_val ?  op->o_ndn.bv_val : "",
1421                 accessmask2str( *mask, accessmaskbuf, 1 ) );
1422
1423
1424         if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1425                 && state->as_vd_acl == a )
1426         {
1427                 b = state->as_vd_access;
1428                 i = state->as_vd_access_count;
1429
1430         } else {
1431                 b = a->acl_access;
1432                 i = 1;
1433         }
1434
1435         for ( ; b != NULL; b = b->a_next, i++ ) {
1436                 slap_mask_t oldmask, modmask;
1437
1438                 ACL_INVALIDATE( modmask );
1439
1440                 /* AND <who> clauses */
1441                 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1442                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1443                                 b->a_dn_pat.bv_val, 0, 0);
1444                         /*
1445                          * if access applies to the entry itself, and the
1446                          * user is bound as somebody in the same namespace as
1447                          * the entry, OR the given dn matches the dn pattern
1448                          */
1449                         /*
1450                          * NOTE: styles "anonymous", "users" and "self" 
1451                          * have been moved to enum slap_style_t, whose 
1452                          * value is set in a_dn_style; however, the string
1453                          * is maintaned in a_dn_pat.
1454                          */
1455
1456                         if ( acl_mask_dn( op, e, a, nmatch, matches,
1457                                 &b->a_dn, &op->o_ndn ) )
1458                         {
1459                                 continue;
1460                         }
1461                 }
1462
1463                 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1464                         struct berval   ndn;
1465
1466                         Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1467                                 b->a_realdn_pat.bv_val, 0, 0);
1468                         /*
1469                          * if access applies to the entry itself, and the
1470                          * user is bound as somebody in the same namespace as
1471                          * the entry, OR the given dn matches the dn pattern
1472                          */
1473                         /*
1474                          * NOTE: styles "anonymous", "users" and "self" 
1475                          * have been moved to enum slap_style_t, whose 
1476                          * value is set in a_dn_style; however, the string
1477                          * is maintaned in a_dn_pat.
1478                          */
1479
1480                         if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1481                         {
1482                                 ndn = op->o_conn->c_ndn;
1483                         } else {
1484                                 ndn = op->o_ndn;
1485                         }
1486
1487                         if ( acl_mask_dn( op, e, a, nmatch, matches,
1488                                 &b->a_realdn, &ndn ) )
1489                         {
1490                                 continue;
1491                         }
1492                 }
1493
1494                 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1495                         if ( ! op->o_conn->c_listener ) {
1496                                 continue;
1497                         }
1498                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1499                                 b->a_sockurl_pat.bv_val, 0, 0 );
1500
1501                         if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1502                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1503                                         if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1504                                                         e->e_ndn, nmatch, matches ) ) 
1505                                         {
1506                                                 continue;
1507                                         }
1508
1509                                 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1510                                         struct berval   bv;
1511                                         char buf[ACL_BUF_SIZE];
1512
1513                                         bv.bv_len = sizeof( buf ) - 1;
1514                                         bv.bv_val = buf;
1515                                         if ( acl_string_expand( &bv, &b->a_sockurl_pat,
1516                                                         e->e_ndn, nmatch, matches ) )
1517                                         {
1518                                                 continue;
1519                                         }
1520
1521                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1522                                         {
1523                                                 continue;
1524                                         }
1525
1526                                 } else {
1527                                         if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1528                                         {
1529                                                 continue;
1530                                         }
1531                                 }
1532                         }
1533                 }
1534
1535                 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1536                         if ( !op->o_conn->c_peer_domain.bv_val ) {
1537                                 continue;
1538                         }
1539                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1540                                 b->a_domain_pat.bv_val, 0, 0 );
1541                         if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1542                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1543                                         if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1544                                                         e->e_ndn, nmatch, matches ) ) 
1545                                         {
1546                                                 continue;
1547                                         }
1548                                 } else {
1549                                         char buf[ACL_BUF_SIZE];
1550
1551                                         struct berval   cmp = op->o_conn->c_peer_domain;
1552                                         struct berval   pat = b->a_domain_pat;
1553
1554                                         if ( b->a_domain_expand ) {
1555                                                 struct berval bv;
1556
1557                                                 bv.bv_len = sizeof(buf) - 1;
1558                                                 bv.bv_val = buf;
1559
1560                                                 if ( acl_string_expand(&bv, &b->a_domain_pat,
1561                                                                 e->e_ndn, nmatch, matches) )
1562                                                 {
1563                                                         continue;
1564                                                 }
1565                                                 pat = bv;
1566                                         }
1567
1568                                         if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1569                                                 int offset = cmp.bv_len - pat.bv_len;
1570                                                 if ( offset < 0 ) {
1571                                                         continue;
1572                                                 }
1573
1574                                                 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1575                                                         continue;
1576                                                 }
1577
1578                                                 /* trim the domain */
1579                                                 cmp.bv_val = &cmp.bv_val[ offset ];
1580                                                 cmp.bv_len -= offset;
1581                                         }
1582                                         
1583                                         if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1584                                                 continue;
1585                                         }
1586                                 }
1587                         }
1588                 }
1589
1590                 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1591                         if ( !op->o_conn->c_peer_name.bv_val ) {
1592                                 continue;
1593                         }
1594                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1595                                 b->a_peername_pat.bv_val, 0, 0 );
1596                         if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1597                                 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1598                                         if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1599                                                         e->e_ndn, nmatch, matches ) ) 
1600                                         {
1601                                                 continue;
1602                                         }
1603
1604                                 } else {
1605                                         /* try exact match */
1606                                         if ( b->a_peername_style == ACL_STYLE_BASE ) {
1607                                                 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1608                                                         continue;
1609                                                 }
1610
1611                                         } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1612                                                 struct berval   bv;
1613                                                 char buf[ACL_BUF_SIZE];
1614
1615                                                 bv.bv_len = sizeof( buf ) - 1;
1616                                                 bv.bv_val = buf;
1617                                                 if ( acl_string_expand( &bv, &b->a_peername_pat,
1618                                                                 e->e_ndn, nmatch, matches ) )
1619                                                 {
1620                                                         continue;
1621                                                 }
1622
1623                                                 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1624                                                         continue;
1625                                                 }
1626
1627                                         /* extract IP and try exact match */
1628                                         } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1629                                                 char            *port;
1630                                                 char            buf[] = "255.255.255.255";
1631                                                 struct berval   ip;
1632                                                 unsigned long   addr;
1633                                                 int             port_number = -1;
1634                                                 
1635                                                 if ( strncasecmp( op->o_conn->c_peer_name.bv_val, 
1636                                                                         aci_bv[ ACI_BV_IP_EQ ].bv_val,
1637                                                                         aci_bv[ ACI_BV_IP_EQ ].bv_len ) != 0 ) 
1638                                                         continue;
1639
1640                                                 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv[ ACI_BV_IP_EQ ].bv_len;
1641                                                 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv[ ACI_BV_IP_EQ ].bv_len;
1642
1643                                                 port = strrchr( ip.bv_val, ':' );
1644                                                 if ( port ) {
1645                                                         char    *next;
1646                                                         
1647                                                         ip.bv_len = port - ip.bv_val;
1648                                                         ++port;
1649                                                         port_number = strtol( port, &next, 10 );
1650                                                         if ( next[0] != '\0' )
1651                                                                 continue;
1652                                                 }
1653                                                 
1654                                                 /* the port check can be anticipated here */
1655                                                 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1656                                                         continue;
1657                                                 
1658                                                 /* address longer than expected? */
1659                                                 if ( ip.bv_len >= sizeof(buf) )
1660                                                         continue;
1661
1662                                                 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1663                                                 buf[ ip.bv_len ] = '\0';
1664
1665                                                 addr = inet_addr( buf );
1666
1667                                                 /* unable to convert? */
1668                                                 if ( addr == (unsigned long)(-1) )
1669                                                         continue;
1670
1671                                                 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1672                                                         continue;
1673
1674 #ifdef LDAP_PF_LOCAL
1675                                         /* extract path and try exact match */
1676                                         } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1677                                                 struct berval path;
1678                                                 
1679                                                 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1680                                                                         aci_bv[ ACI_BV_PATH_EQ ].bv_val,
1681                                                                         aci_bv[ ACI_BV_PATH_EQ ].bv_len ) != 0 )
1682                                                         continue;
1683
1684                                                 path.bv_val = op->o_conn->c_peer_name.bv_val
1685                                                         + aci_bv[ ACI_BV_PATH_EQ ].bv_len;
1686                                                 path.bv_len = op->o_conn->c_peer_name.bv_len
1687                                                         - aci_bv[ ACI_BV_PATH_EQ ].bv_len;
1688
1689                                                 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1690                                                         continue;
1691
1692 #endif /* LDAP_PF_LOCAL */
1693
1694                                         /* exact match (very unlikely...) */
1695                                         } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1696                                                         continue;
1697                                         }
1698                                 }
1699                         }
1700                 }
1701
1702                 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1703                         if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1704                                 continue;
1705                         }
1706                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1707                                 b->a_sockname_pat.bv_val, 0, 0 );
1708                         if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1709                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1710                                         if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1711                                                         e->e_ndn, nmatch, matches ) ) 
1712                                         {
1713                                                 continue;
1714                                         }
1715
1716                                 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1717                                         struct berval   bv;
1718                                         char buf[ACL_BUF_SIZE];
1719
1720                                         bv.bv_len = sizeof( buf ) - 1;
1721                                         bv.bv_val = buf;
1722                                         if ( acl_string_expand( &bv, &b->a_sockname_pat,
1723                                                         e->e_ndn, nmatch, matches ) )
1724                                         {
1725                                                 continue;
1726                                         }
1727
1728                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1729                                                 continue;
1730                                         }
1731
1732                                 } else {
1733                                         if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1734                                                 continue;
1735                                         }
1736                                 }
1737                         }
1738                 }
1739
1740                 if ( b->a_dn_at != NULL ) {
1741                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1742                                         matches, count, state,
1743                                         &b->a_dn, &op->o_ndn ) )
1744                         {
1745                                 continue;
1746                         }
1747                 }
1748
1749                 if ( b->a_realdn_at != NULL ) {
1750                         struct berval   ndn;
1751
1752                         if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1753                         {
1754                                 ndn = op->o_conn->c_ndn;
1755                         } else {
1756                                 ndn = op->o_ndn;
1757                         }
1758
1759                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1760                                         matches, count, state,
1761                                         &b->a_realdn, &ndn ) )
1762                         {
1763                                 continue;
1764                         }
1765                 }
1766
1767                 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1768                         struct berval bv;
1769                         struct berval ndn = BER_BVNULL;
1770                         int rc;
1771
1772                         if ( op->o_ndn.bv_len == 0 ) {
1773                                 continue;
1774                         }
1775
1776                         Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1777                                 b->a_group_pat.bv_val, 0, 0 );
1778
1779                         /* b->a_group is an unexpanded entry name, expanded it should be an 
1780                          * entry with objectclass group* and we test to see if odn is one of
1781                          * the values in the attribute group
1782                          */
1783                         /* see if asker is listed in dnattr */
1784                         if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1785                                 char            buf[ACL_BUF_SIZE];
1786                                 int             tmp_nmatch;
1787                                 regmatch_t      tmp_matches[2],
1788                                                 *tmp_matchesp = tmp_matches;
1789
1790                                 bv.bv_len = sizeof(buf) - 1;
1791                                 bv.bv_val = buf;
1792
1793                                 rc = 0;
1794
1795                                 switch ( a->acl_dn_style ) {
1796                                 case ACL_STYLE_REGEX:
1797                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1798                                                 tmp_matchesp = matches;
1799                                                 tmp_nmatch = nmatch;
1800                                                 break;
1801                                         }
1802
1803                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1804                                 case ACL_STYLE_BASE:
1805                                         tmp_matches[0].rm_so = 0;
1806                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1807                                         tmp_nmatch = 1;
1808                                         break;
1809
1810                                 case ACL_STYLE_ONE:
1811                                 case ACL_STYLE_SUBTREE:
1812                                 case ACL_STYLE_CHILDREN:
1813                                         tmp_matches[0].rm_so = 0;
1814                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1815                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1816                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1817                                         tmp_nmatch = 2;
1818                                         break;
1819
1820                                 default:
1821                                         /* error */
1822                                         rc = 1;
1823                                         break;
1824                                 }
1825
1826                                 if ( rc ) {
1827                                         continue;
1828                                 }
1829                                 
1830                                 if ( acl_string_expand( &bv, &b->a_group_pat,
1831                                                 e->e_nname.bv_val,
1832                                                 tmp_nmatch, tmp_matchesp ) )
1833                                 {
1834                                         continue;
1835                                 }
1836
1837                                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1838                                                 op->o_tmpmemctx ) != LDAP_SUCCESS )
1839                                 {
1840                                         /* did not expand to a valid dn */
1841                                         continue;
1842                                 }
1843
1844                                 bv = ndn;
1845
1846                         } else {
1847                                 bv = b->a_group_pat;
1848                         }
1849
1850                         rc = backend_group( op, e, &bv, &op->o_ndn,
1851                                 b->a_group_oc, b->a_group_at );
1852
1853                         if ( ndn.bv_val ) {
1854                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1855                         }
1856
1857                         if ( rc != 0 ) {
1858                                 continue;
1859                         }
1860                 }
1861
1862                 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1863                         struct berval   bv;
1864                         char            buf[ACL_BUF_SIZE];
1865
1866                         Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1867                                 b->a_set_pat.bv_val, 0, 0 );
1868
1869                         if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1870                                 int             tmp_nmatch;
1871                                 regmatch_t      tmp_matches[2],
1872                                                 *tmp_matchesp = tmp_matches;
1873                                 int             rc = 0;
1874
1875                                 bv.bv_len = sizeof( buf ) - 1;
1876                                 bv.bv_val = buf;
1877
1878                                 rc = 0;
1879
1880                                 switch ( a->acl_dn_style ) {
1881                                 case ACL_STYLE_REGEX:
1882                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1883                                                 tmp_matchesp = matches;
1884                                                 tmp_nmatch = nmatch;
1885                                                 break;
1886                                         }
1887
1888                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1889                                 case ACL_STYLE_BASE:
1890                                         tmp_matches[0].rm_so = 0;
1891                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1892                                         tmp_nmatch = 1;
1893                                         break;
1894
1895                                 case ACL_STYLE_ONE:
1896                                 case ACL_STYLE_SUBTREE:
1897                                 case ACL_STYLE_CHILDREN:
1898                                         tmp_matches[0].rm_so = 0;
1899                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1900                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1901                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1902                                         tmp_nmatch = 2;
1903                                         break;
1904
1905                                 default:
1906                                         /* error */
1907                                         rc = 1;
1908                                         break;
1909                                 }
1910
1911                                 if ( rc ) {
1912                                         continue;
1913                                 }
1914                                 
1915                                 if ( acl_string_expand( &bv, &b->a_set_pat,
1916                                                 e->e_nname.bv_val,
1917                                                 tmp_nmatch, tmp_matchesp ) )
1918                                 {
1919                                         continue;
1920                                 }
1921
1922                         } else {
1923                                 bv = b->a_set_pat;
1924                         }
1925                         
1926                         if ( acl_match_set( &bv, op, e, 0 ) == 0 ) {
1927                                 continue;
1928                         }
1929                 }
1930
1931                 if ( b->a_authz.sai_ssf ) {
1932                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1933                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
1934                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
1935                                 continue;
1936                         }
1937                 }
1938
1939                 if ( b->a_authz.sai_transport_ssf ) {
1940                         Debug( LDAP_DEBUG_ACL,
1941                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1942                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1943                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
1944                                 continue;
1945                         }
1946                 }
1947
1948                 if ( b->a_authz.sai_tls_ssf ) {
1949                         Debug( LDAP_DEBUG_ACL,
1950                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1951                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1952                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
1953                                 continue;
1954                         }
1955                 }
1956
1957                 if ( b->a_authz.sai_sasl_ssf ) {
1958                         Debug( LDAP_DEBUG_ACL,
1959                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1960                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1961                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
1962                                 continue;
1963                         }
1964                 }
1965
1966 #ifdef SLAP_DYNACL
1967                 if ( b->a_dynacl ) {
1968                         slap_dynacl_t   *da;
1969                         slap_access_t   tgrant, tdeny;
1970
1971                         Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1972                                 0, 0, 0 );
1973
1974                         /* this case works different from the others above.
1975                          * since aci's themselves give permissions, we need
1976                          * to first check b->a_access_mask, the ACL's access level.
1977                          */
1978                         if ( BER_BVISEMPTY( &e->e_nname ) ) {
1979                                 /* no ACIs in the root DSE */
1980                                 continue;
1981                         }
1982
1983                         /* first check if the right being requested
1984                          * is allowed by the ACL clause.
1985                          */
1986                         if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1987                                 continue;
1988                         }
1989
1990                         /* start out with nothing granted, nothing denied */
1991                         ACL_INIT(tgrant);
1992                         ACL_INIT(tdeny);
1993
1994                         for ( da = b->a_dynacl; da; da = da->da_next ) {
1995                                 slap_access_t   grant,
1996                                                 deny;
1997
1998                                 ACL_INIT(grant);
1999                                 ACL_INIT(deny);
2000
2001                                 Debug( LDAP_DEBUG_ACL, "    <= check a_dynacl: %s\n",
2002                                         da->da_name, 0, 0 );
2003
2004                                 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
2005
2006                                 tgrant |= grant;
2007                                 tdeny |= deny;
2008                         }
2009
2010                         /* remove anything that the ACL clause does not allow */
2011                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
2012                         tdeny &= ACL_PRIV_MASK;
2013
2014                         /* see if we have anything to contribute */
2015                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
2016                                 continue;
2017                         }
2018
2019                         /* this could be improved by changing slap_acl_mask so that it can deal with
2020                          * by clauses that return grant/deny pairs.  Right now, it does either
2021                          * additive or subtractive rights, but not both at the same time.  So,
2022                          * we need to combine the grant/deny pair into a single rights mask in
2023                          * a smart way:  if either grant or deny is "empty", then we use the
2024                          * opposite as is, otherwise we remove any denied rights from the grant
2025                          * rights mask and construct an additive mask.
2026                          */
2027                         if (ACL_IS_INVALID(tdeny)) {
2028                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
2029
2030                         } else if (ACL_IS_INVALID(tgrant)) {
2031                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2032
2033                         } else {
2034                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2035                         }
2036
2037                 } else
2038 #else /* !SLAP_DYNACL */
2039
2040 #ifdef SLAPD_ACI_ENABLED
2041                 if ( b->a_aci_at != NULL ) {
2042                         Attribute       *at;
2043                         slap_access_t   grant, deny, tgrant, tdeny;
2044                         struct berval   parent_ndn;
2045                         BerVarray       bvals = NULL;
2046                         int             ret, stop;
2047
2048                         Debug( LDAP_DEBUG_ACL, "    <= check a_aci_at: %s\n",
2049                                 b->a_aci_at->ad_cname.bv_val, 0, 0 );
2050
2051                         /* this case works different from the others above.
2052                          * since aci's themselves give permissions, we need
2053                          * to first check b->a_access_mask, the ACL's access level.
2054                          */
2055
2056                         if ( BER_BVISEMPTY( &e->e_nname ) ) {
2057                                 /* no ACIs in the root DSE */
2058                                 continue;
2059                         }
2060
2061                         /* first check if the right being requested
2062                          * is allowed by the ACL clause.
2063                          */
2064                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
2065                                 continue;
2066                         }
2067                         /* start out with nothing granted, nothing denied */
2068                         ACL_INIT(tgrant);
2069                         ACL_INIT(tdeny);
2070
2071                         /* get the aci attribute */
2072                         at = attr_find( e->e_attrs, b->a_aci_at );
2073                         if ( at != NULL ) {
2074 #if 0
2075                                 /* FIXME: this breaks acl caching;
2076                                  * see also ACL_RECORD_VALUE_STATE below */
2077                                 ACL_RECORD_VALUE_STATE;
2078 #endif
2079                                 /* the aci is an multi-valued attribute.  The
2080                                 * rights are determined by OR'ing the individual
2081                                 * rights given by the acis.
2082                                 */
2083                                 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2084                                         if ( aci_mask( op,
2085                                                 e, desc, val,
2086                                                 &at->a_nvals[i],
2087                                                 nmatch, matches,
2088                                                 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0 )
2089                                         {
2090                                                 tgrant |= grant;
2091                                                 tdeny |= deny;
2092                                         }
2093                                 }
2094                                 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2095                                           accessmask2str(tgrant, accessmaskbuf, 1), 
2096                                           accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2097
2098                         }
2099                         /* If the entry level aci didn't contain anything valid for the 
2100                          * current operation, climb up the tree and evaluate the
2101                          * acis with scope set to subtree
2102                          */
2103                         if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
2104                                 dnParent( &e->e_nname, &parent_ndn );
2105                                 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
2106                                         Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
2107                                         ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
2108                                         switch(ret){
2109                                         case LDAP_SUCCESS :
2110                                                 stop = 0;
2111                                                 if (!bvals){
2112                                                         break;
2113                                                 }
2114
2115                                                 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++ ) {
2116 #if 0
2117                                                         /* FIXME: this breaks acl caching;
2118                                                          * see also ACL_RECORD_VALUE_STATE above */
2119                                                         ACL_RECORD_VALUE_STATE;
2120 #endif
2121                                                         if ( aci_mask( op, e, desc, val, &bvals[i],
2122                                                                         nmatch, matches,
2123                                                                         &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2124                                                         {
2125                                                                 tgrant |= grant;
2126                                                                 tdeny |= deny;
2127                                                                 /* evaluation stops as soon as either a "deny" or a 
2128                                                                  * "grant" directive matches.
2129                                                                  */
2130                                                                 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
2131                                                                         stop = 1;
2132                                                                 }
2133                                                         }
2134                                                         Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n", 
2135                                                                 accessmask2str(tgrant, accessmaskbuf, 1),
2136                                                                 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2137                                                 }
2138                                                 break;
2139
2140                                         case LDAP_NO_SUCH_ATTRIBUTE:
2141                                                 /* just go on if the aci-Attribute is not present in
2142                                                  * the current entry 
2143                                                  */
2144                                                 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
2145                                                 stop = 0;
2146                                                 break;
2147
2148                                         case LDAP_NO_SUCH_OBJECT:
2149                                                 /* We have reached the base object */
2150                                                 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
2151                                                 stop = 1;
2152                                                 break;
2153
2154                                         default:
2155                                                 stop = 1;
2156                                                 break;
2157                                         }
2158                                         if (stop){
2159                                                 break;
2160                                         }
2161                                         dnParent( &parent_ndn, &parent_ndn );
2162                                 }
2163                         }
2164
2165
2166                         /* remove anything that the ACL clause does not allow */
2167                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
2168                         tdeny &= ACL_PRIV_MASK;
2169
2170                         /* see if we have anything to contribute */
2171                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
2172                                 continue;
2173                         }
2174
2175                         /* this could be improved by changing slap_acl_mask so that it can deal with
2176                          * by clauses that return grant/deny pairs.  Right now, it does either
2177                          * additive or subtractive rights, but not both at the same time.  So,
2178                          * we need to combine the grant/deny pair into a single rights mask in
2179                          * a smart way:  if either grant or deny is "empty", then we use the
2180                          * opposite as is, otherwise we remove any denied rights from the grant
2181                          * rights mask and construct an additive mask.
2182                          */
2183                         if (ACL_IS_INVALID(tdeny)) {
2184                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
2185
2186                         } else if (ACL_IS_INVALID(tgrant)) {
2187                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2188
2189                         } else {
2190                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2191                         }
2192
2193                 } else
2194 #endif /* SLAPD_ACI_ENABLED */
2195 #endif /* !SLAP_DYNACL */
2196                 {
2197                         modmask = b->a_access_mask;
2198                 }
2199
2200                 Debug( LDAP_DEBUG_ACL,
2201                         "<= acl_mask: [%d] applying %s (%s)\n",
2202                         i, accessmask2str( modmask, accessmaskbuf, 1 ), 
2203                         b->a_type == ACL_CONTINUE
2204                                 ? "continue"
2205                                 : b->a_type == ACL_BREAK
2206                                         ? "break"
2207                                         : "stop" );
2208                 /* save old mask */
2209                 oldmask = *mask;
2210
2211                 if( ACL_IS_ADDITIVE(modmask) ) {
2212                         /* add privs */
2213                         ACL_PRIV_SET( *mask, modmask );
2214
2215                         /* cleanup */
2216                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2217
2218                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
2219                         /* substract privs */
2220                         ACL_PRIV_CLR( *mask, modmask );
2221
2222                         /* cleanup */
2223                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2224
2225                 } else {
2226                         /* assign privs */
2227                         *mask = modmask;
2228                 }
2229
2230                 a2pmask = *mask;
2231
2232                 Debug( LDAP_DEBUG_ACL,
2233                         "<= acl_mask: [%d] mask: %s\n",
2234                         i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
2235
2236                 if( b->a_type == ACL_CONTINUE ) {
2237                         continue;
2238
2239                 } else if ( b->a_type == ACL_BREAK ) {
2240                         return ACL_BREAK;
2241
2242                 } else {
2243                         return ACL_STOP;
2244                 }
2245         }
2246
2247         /* implicit "by * none" clause */
2248         ACL_INIT(*mask);
2249
2250         Debug( LDAP_DEBUG_ACL,
2251                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
2252                 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
2253         return ACL_STOP;
2254 }
2255
2256 /*
2257  * acl_check_modlist - check access control on the given entry to see if
2258  * it allows the given modifications by the user associated with op.
2259  * returns      1       if mods allowed ok
2260  *              0       mods not allowed
2261  */
2262
2263 int
2264 acl_check_modlist(
2265         Operation       *op,
2266         Entry   *e,
2267         Modifications   *mlist )
2268 {
2269         struct berval *bv;
2270         AccessControlState state = ACL_STATE_INIT;
2271         Backend *be;
2272         int be_null = 0;
2273         int ret = 1; /* default is access allowed */
2274
2275         be = op->o_bd;
2276         if ( be == NULL ) {
2277                 be = LDAP_STAILQ_FIRST(&backendDB);
2278                 be_null = 1;
2279                 op->o_bd = be;
2280         }
2281         assert( be != NULL );
2282
2283         /* short circuit root database access */
2284         if ( be_isroot( op ) ) {
2285                 Debug( LDAP_DEBUG_ACL,
2286                         "<= acl_access_allowed: granted to database root\n",
2287                     0, 0, 0 );
2288                 goto done;
2289         }
2290
2291         /* use backend default access if no backend acls */
2292         if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
2293                 Debug( LDAP_DEBUG_ACL,
2294                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
2295                         access2str( ACL_WRITE ),
2296                         op->o_bd->be_dfltaccess >= ACL_WRITE
2297                                 ? "granted" : "denied",
2298                         op->o_dn.bv_val );
2299                 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
2300                 goto done;
2301         }
2302
2303         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
2304                 /*
2305                  * Internal mods are ignored by ACL_WRITE checking
2306                  */
2307                 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
2308                         Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
2309                                 " modify access granted\n",
2310                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2311                         continue;
2312                 }
2313
2314                 /*
2315                  * no-user-modification operational attributes are ignored
2316                  * by ACL_WRITE checking as any found here are not provided
2317                  * by the user
2318                  */
2319                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
2320                                 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
2321                 {
2322                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
2323                                 " modify access granted\n",
2324                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2325                         continue;
2326                 }
2327
2328                 switch ( mlist->sml_op ) {
2329                 case LDAP_MOD_REPLACE:
2330                         /*
2331                          * We must check both permission to delete the whole
2332                          * attribute and permission to add the specific attributes.
2333                          * This prevents abuse from selfwriters.
2334                          */
2335                         if ( ! access_allowed( op, e,
2336                                 mlist->sml_desc, NULL,
2337                                 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2338                                 &state ) )
2339                         {
2340                                 ret = 0;
2341                                 goto done;
2342                         }
2343
2344                         if ( mlist->sml_values == NULL ) break;
2345
2346                         /* fall thru to check value to add */
2347
2348                 case LDAP_MOD_ADD:
2349                         assert( mlist->sml_values != NULL );
2350
2351                         for ( bv = mlist->sml_nvalues
2352                                         ? mlist->sml_nvalues : mlist->sml_values;
2353                                 bv->bv_val != NULL; bv++ )
2354                         {
2355                                 if ( ! access_allowed( op, e,
2356                                         mlist->sml_desc, bv,
2357                                         ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
2358                                         &state ) )
2359                                 {
2360                                         ret = 0;
2361                                         goto done;
2362                                 }
2363                         }
2364                         break;
2365
2366                 case LDAP_MOD_DELETE:
2367                         if ( mlist->sml_values == NULL ) {
2368                                 if ( ! access_allowed( op, e,
2369                                         mlist->sml_desc, NULL,
2370                                         ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2371                                         NULL ) )
2372                                 {
2373                                         ret = 0;
2374                                         goto done;
2375                                 }
2376                                 break;
2377                         }
2378                         for ( bv = mlist->sml_nvalues
2379                                         ? mlist->sml_nvalues : mlist->sml_values;
2380                                 bv->bv_val != NULL; bv++ )
2381                         {
2382                                 if ( ! access_allowed( op, e,
2383                                         mlist->sml_desc, bv,
2384                                         ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2385                                         &state ) )
2386                                 {
2387                                         ret = 0;
2388                                         goto done;
2389                                 }
2390                         }
2391                         break;
2392
2393                 case SLAP_MOD_SOFTADD:
2394                         /* allow adding attribute via modrdn thru */
2395                         break;
2396
2397                 default:
2398                         assert( 0 );
2399                         /* not reached */
2400                         ret = 0;
2401                         break;
2402                 }
2403         }
2404
2405 done:
2406         if (be_null) op->o_bd = NULL;
2407         return( ret );
2408 }
2409
2410 int
2411 acl_get_part(
2412         struct berval   *list,
2413         int             ix,
2414         char            sep,
2415         struct berval   *bv )
2416 {
2417         int     len;
2418         char    *p;
2419
2420         if ( bv ) {
2421                 BER_BVZERO( bv );
2422         }
2423         len = list->bv_len;
2424         p = list->bv_val;
2425         while ( len >= 0 && --ix >= 0 ) {
2426                 while ( --len >= 0 && *p++ != sep )
2427                         ;
2428         }
2429         while ( len >= 0 && *p == ' ' ) {
2430                 len--;
2431                 p++;
2432         }
2433         if ( len < 0 ) {
2434                 return -1;
2435         }
2436
2437         if ( !bv ) {
2438                 return 0;
2439         }
2440
2441         bv->bv_val = p;
2442         while ( --len >= 0 && *p != sep ) {
2443                 bv->bv_len++;
2444                 p++;
2445         }
2446         while ( bv->bv_len > 0 && *--p == ' ' ) {
2447                 bv->bv_len--;
2448         }
2449         
2450         return bv->bv_len;
2451 }
2452
2453 typedef struct acl_set_gather_t {
2454         SetCookie               *cookie;
2455         BerVarray               bvals;
2456 } acl_set_gather_t;
2457
2458 static int
2459 acl_set_cb_gather( Operation *op, SlapReply *rs )
2460 {
2461         acl_set_gather_t        *p = (acl_set_gather_t *)op->o_callback->sc_private;
2462         
2463         if ( rs->sr_type == REP_SEARCH ) {
2464                 BerValue        bvals[ 2 ];
2465                 BerVarray       bvalsp = NULL;
2466                 int             j;
2467
2468                 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2469                         AttributeDescription    *desc = rs->sr_attrs[ j ].an_desc;
2470                         
2471                         if ( desc == slap_schema.si_ad_entryDN ) {
2472                                 bvalsp = bvals;
2473                                 bvals[ 0 ] = rs->sr_entry->e_nname;
2474                                 BER_BVZERO( &bvals[ 1 ] );
2475
2476                         } else {
2477                                 Attribute       *a;
2478
2479                                 a = attr_find( rs->sr_entry->e_attrs, desc );
2480                                 if ( a != NULL ) {
2481                                         int     i;
2482
2483                                         for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2484                                                 ;
2485
2486                                         bvalsp = a->a_nvals;
2487                                 }
2488                         }
2489                 }
2490
2491                 if ( bvals ) {
2492                         p->bvals = slap_set_join( p->cookie, p->bvals,
2493                                         ( '|' | SLAP_SET_RREF ), bvalsp );
2494                 }
2495
2496         } else {
2497                 assert( rs->sr_type == REP_RESULT );
2498         }
2499
2500         return 0;
2501 }
2502
2503 BerVarray
2504 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2505 {
2506         AclSetCookie            *cp = (AclSetCookie *)cookie;
2507         int                     rc = 0;
2508         LDAPURLDesc             *ludp = NULL;
2509         Operation               op2 = { 0 };
2510         SlapReply               rs = {REP_RESULT};
2511         AttributeName           anlist[ 2 ], *anlistp = NULL;
2512         int                     nattrs = 0;
2513         slap_callback           cb = { NULL, acl_set_cb_gather, NULL, NULL };
2514         acl_set_gather_t        p = { 0 };
2515         const char              *text = NULL;
2516         static struct berval    defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2517
2518         /* this routine needs to return the bervals instead of
2519          * plain strings, since syntax is not known.  It should
2520          * also return the syntax or some "comparison cookie".
2521          */
2522         if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2523                 return acl_set_gather2( cookie, name, desc );
2524         }
2525
2526         rc = ldap_url_parse( name->bv_val, &ludp );
2527         if ( rc != LDAP_URL_SUCCESS ) {
2528                 rc = LDAP_PROTOCOL_ERROR;
2529                 goto url_done;
2530         }
2531         
2532         if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2533         {
2534                 /* host part must be empty */
2535                 /* extensions parts must be empty */
2536                 rc = LDAP_PROTOCOL_ERROR;
2537                 goto url_done;
2538         }
2539
2540         /* Grab the searchbase and see if an appropriate database can be found */
2541         ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2542         rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2543                         &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2544         BER_BVZERO( &op2.o_req_dn );
2545         if ( rc != LDAP_SUCCESS ) {
2546                 goto url_done;
2547         }
2548
2549         op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2550         if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2551                 rc = LDAP_NO_SUCH_OBJECT;
2552                 goto url_done;
2553         }
2554
2555         /* Grab the filter */
2556         if ( ludp->lud_filter ) {
2557                 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2558                                 cp->asc_op->o_tmpmemctx );
2559                 
2560         } else {
2561                 op2.ors_filterstr = defaultFilter_bv;
2562         }
2563
2564         op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2565         if ( op2.ors_filter == NULL ) {
2566                 rc = LDAP_PROTOCOL_ERROR;
2567                 goto url_done;
2568         }
2569
2570         /* Grab the scope */
2571         op2.ors_scope = ludp->lud_scope;
2572
2573         /* Grap the attributes */
2574         if ( ludp->lud_attrs ) {
2575                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2576                         ;
2577
2578                 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2579                                 cp->asc_op->o_tmpmemctx );
2580
2581                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2582                         ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2583                         anlistp[ nattrs ].an_desc = NULL;
2584                         rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2585                                         &anlistp[ nattrs ].an_desc, &text );
2586                         if ( rc != LDAP_SUCCESS ) {
2587                                 goto url_done;
2588                         }
2589                 }
2590
2591         } else {
2592                 anlistp = anlist;
2593         }
2594
2595         anlistp[ nattrs ].an_name = desc->ad_cname;
2596         anlistp[ nattrs ].an_desc = desc;
2597
2598         BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2599         
2600         p.cookie = cookie;
2601         
2602         op2.o_hdr = cp->asc_op->o_hdr;
2603         op2.o_tag = LDAP_REQ_SEARCH;
2604         op2.o_ndn = op2.o_bd->be_rootndn;
2605         op2.o_callback = &cb;
2606         slap_op_time( &op2.o_time, &op2.o_tincr );
2607         op2.o_do_not_cache = 1;
2608         op2.o_is_auth_check = 0;
2609         ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2610         op2.ors_slimit = SLAP_NO_LIMIT;
2611         op2.ors_tlimit = SLAP_NO_LIMIT;
2612         op2.ors_attrs = anlistp;
2613         op2.ors_attrsonly = 0;
2614         op2.o_private = cp->asc_op->o_private;
2615
2616         cb.sc_private = &p;
2617
2618         rc = op2.o_bd->be_search( &op2, &rs );
2619         if ( rc != 0 ) {
2620                 goto url_done;
2621         }
2622
2623 url_done:;
2624         if ( op2.ors_filter ) {
2625                 filter_free_x( cp->asc_op, op2.ors_filter );
2626         }
2627         if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2628                 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2629         }
2630         if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2631                 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2632         }
2633         if ( ludp ) {
2634                 ldap_free_urldesc( ludp );
2635         }
2636         if ( anlistp && anlistp != anlist ) {
2637                 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2638         }
2639
2640         return p.bvals;
2641 }
2642
2643 BerVarray
2644 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2645 {
2646         AclSetCookie    *cp = (AclSetCookie *)cookie;
2647         BerVarray       bvals = NULL;
2648         struct berval   ndn;
2649         int             rc = 0;
2650
2651         /* this routine needs to return the bervals instead of
2652          * plain strings, since syntax is not known.  It should
2653          * also return the syntax or some "comparison cookie".
2654          */
2655         rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2656         if ( rc == LDAP_SUCCESS ) {
2657                 if ( desc == slap_schema.si_ad_entryDN ) {
2658                         bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2659                                         cp->asc_op->o_tmpmemctx );
2660                         bvals[ 0 ] = ndn;
2661                         BER_BVZERO( &bvals[ 1 ] );
2662                         BER_BVZERO( &ndn );
2663
2664                 } else {
2665                         backend_attribute( cp->asc_op,
2666                                 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2667                 }
2668
2669                 if ( !BER_BVISNULL( &ndn ) ) {
2670                         slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2671                 }
2672         }
2673
2674         return bvals;
2675 }
2676
2677 int
2678 acl_match_set (
2679         struct berval *subj,
2680         Operation *op,
2681         Entry *e,
2682         int setref )
2683 {
2684         struct berval   set = BER_BVNULL;
2685         int             rc = 0;
2686         AclSetCookie    cookie;
2687
2688         if ( setref == 0 ) {
2689                 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2690
2691         } else {
2692                 struct berval           subjdn, ndn = BER_BVNULL;
2693                 struct berval           setat;
2694                 BerVarray               bvals;
2695                 const char              *text;
2696                 AttributeDescription    *desc = NULL;
2697
2698                 /* format of string is "entry/setAttrName" */
2699                 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2700                         return 0;
2701                 }
2702
2703                 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2704                         setat = aci_bv[ ACI_BV_SET_ATTR ];
2705                 }
2706
2707                 /*
2708                  * NOTE: dnNormalize honors the ber_len field
2709                  * as the length of the dn to be normalized
2710                  */
2711                 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2712                         if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2713                         {
2714                                 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2715                                 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2716                                         int     i;
2717
2718                                         set = bvals[0];
2719                                         BER_BVZERO( &bvals[0] );
2720                                         for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2721                                                 /* count */ ;
2722                                         bvals[0].bv_val = bvals[i-1].bv_val;
2723                                         BER_BVZERO( &bvals[i-1] );
2724                                 }
2725                                 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2726                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2727                         }
2728                 }
2729         }
2730
2731         if ( !BER_BVISNULL( &set ) ) {
2732                 cookie.asc_op = op;
2733                 cookie.asc_e = e;
2734                 rc = ( slap_set_filter(
2735                         acl_set_gather,
2736                         (SetCookie *)&cookie, &set,
2737                         &op->o_ndn, &e->e_nname, NULL ) > 0 );
2738                 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2739         }
2740
2741         return(rc);
2742 }
2743
2744 #ifdef SLAP_DYNACL
2745
2746 /*
2747  * dynamic ACL infrastructure
2748  */
2749 static slap_dynacl_t    *da_list = NULL;
2750
2751 int
2752 slap_dynacl_register( slap_dynacl_t *da )
2753 {
2754         slap_dynacl_t   *tmp;
2755
2756         for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2757                 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2758                         break;
2759                 }
2760         }
2761
2762         if ( tmp != NULL ) {
2763                 return -1;
2764         }
2765         
2766         if ( da->da_mask == NULL ) {
2767                 return -1;
2768         }
2769         
2770         da->da_private = NULL;
2771         da->da_next = da_list;
2772         da_list = da;
2773
2774         return 0;
2775 }
2776
2777 static slap_dynacl_t *
2778 slap_dynacl_next( slap_dynacl_t *da )
2779 {
2780         if ( da ) {
2781                 return da->da_next;
2782         }
2783         return da_list;
2784 }
2785
2786 slap_dynacl_t *
2787 slap_dynacl_get( const char *name )
2788 {
2789         slap_dynacl_t   *da;
2790
2791         for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2792                 if ( strcasecmp( da->da_name, name ) == 0 ) {
2793                         break;
2794                 }
2795         }
2796
2797         return da;
2798 }
2799 #endif /* SLAP_DYNACL */
2800
2801 int
2802 acl_init( void )
2803 {
2804         int     rc = 0;
2805
2806 #ifdef SLAPD_ACI_ENABLED
2807 #ifdef SLAP_DYNACL
2808         rc = dynacl_aci_init();
2809 #else /* !SLAP_DYNACL */
2810         rc = aci_init();
2811 #endif /* !SLAP_DYNACL */
2812         if ( rc != 0 ) {
2813                 return rc;
2814         }
2815 #endif /* SLAPD_ACI_ENABLED */
2816
2817         return rc;
2818 }
2819
2820 int
2821 acl_string_expand(
2822         struct berval   *bv,
2823         struct berval   *pat,
2824         char            *match,
2825         int             nmatch,
2826         regmatch_t      *matches)
2827 {
2828         ber_len_t       size;
2829         char   *sp;
2830         char   *dp;
2831         int     flag;
2832
2833         size = 0;
2834         bv->bv_val[0] = '\0';
2835         bv->bv_len--; /* leave space for lone $ */
2836
2837         flag = 0;
2838         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2839                 sp < pat->bv_val + pat->bv_len ; sp++ )
2840         {
2841                 /* did we previously see a $ */
2842                 if ( flag ) {
2843                         if ( flag == 1 && *sp == '$' ) {
2844                                 *dp++ = '$';
2845                                 size++;
2846                                 flag = 0;
2847
2848                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2849                                 flag = 2;
2850
2851                         } else if ( *sp >= '0' && *sp <= '9' ) {
2852                                 int     n;
2853                                 int     i;
2854                                 int     l;
2855
2856                                 n = *sp - '0';
2857
2858                                 if ( flag == 2 ) {
2859                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2860                                                 if ( *sp >= '0' && *sp <= '9' ) {
2861                                                         n = 10*n + ( *sp - '0' );
2862                                                 }
2863                                         }
2864
2865                                         if ( *sp != /*'{'*/ '}' ) {
2866                                                 /* FIXME: error */
2867                                                 return 1;
2868                                         }
2869                                 }
2870
2871                                 if ( n >= nmatch ) {
2872                                         /* FIXME: error */
2873                                         return 1;
2874                                 }
2875                                 
2876                                 *dp = '\0';
2877                                 i = matches[n].rm_so;
2878                                 l = matches[n].rm_eo; 
2879                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2880                                         *dp++ = match[i];
2881                                 }
2882                                 *dp = '\0';
2883
2884                                 flag = 0;
2885                         }
2886                 } else {
2887                         if (*sp == '$') {
2888                                 flag = 1;
2889                         } else {
2890                                 *dp++ = *sp;
2891                                 size++;
2892                         }
2893                 }
2894         }
2895
2896         if ( flag ) {
2897                 /* must have ended with a single $ */
2898                 *dp++ = '$';
2899                 size++;
2900         }
2901
2902         *dp = '\0';
2903         bv->bv_len = size;
2904
2905         Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2906         Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2907
2908         return 0;
2909 }
2910
2911 static int
2912 regex_matches(
2913         struct berval   *pat,           /* pattern to expand and match against */
2914         char            *str,           /* string to match against pattern */
2915         char            *buf,           /* buffer with $N expansion variables */
2916         int             nmatch, /* size of the matches array */
2917         regmatch_t      *matches        /* offsets in buffer for $N expansion variables */
2918 )
2919 {
2920         regex_t re;
2921         char newbuf[ACL_BUF_SIZE];
2922         struct berval bv;
2923         int     rc;
2924
2925         bv.bv_len = sizeof( newbuf ) - 1;
2926         bv.bv_val = newbuf;
2927
2928         if (str == NULL) {
2929                 str = "";
2930         };
2931
2932         acl_string_expand( &bv, pat, buf, nmatch, matches );
2933         rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2934         if ( rc ) {
2935                 char error[ACL_BUF_SIZE];
2936                 regerror( rc, &re, error, sizeof( error ) );
2937
2938                 Debug( LDAP_DEBUG_TRACE,
2939                     "compile( \"%s\", \"%s\") failed %s\n",
2940                         pat->bv_val, str, error );
2941                 return( 0 );
2942         }
2943
2944         rc = regexec( &re, str, 0, NULL, 0 );
2945         regfree( &re );
2946
2947         Debug( LDAP_DEBUG_TRACE,
2948             "=> regex_matches: string:   %s\n", str, 0, 0 );
2949         Debug( LDAP_DEBUG_TRACE,
2950             "=> regex_matches: rc: %d %s\n",
2951                 rc, !rc ? "matches" : "no matches", 0 );
2952         return( !rc );
2953 }
2954