]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
reject registrations when back-monitor is not configured
[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                                         int     rdnlen = -1, sep = 0;
855
856                                         if ( dnlen <= patlen )
857                                                 continue;
858
859                                         if ( patlen > 0 ) {
860                                                 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
861                                                         continue;
862                                                 sep = 1;
863                                         }
864
865                                         rdnlen = dn_rdnlen( NULL, &e->e_nname );
866                                         if ( rdnlen != dnlen - patlen - sep )
867                                                 continue;
868
869                                 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
870                                         if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
871                                                 continue;
872
873                                 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
874                                         if ( dnlen <= patlen )
875                                                 continue;
876                                         if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
877                                                 continue;
878                                 }
879
880                                 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
881                                         continue;
882                         }
883
884                         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
885                                 *count, 0, 0 );
886                 }
887
888                 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
889                         matches[0].rm_so = matches[0].rm_eo = -1;
890                         continue;
891                 }
892
893                 /* Is this ACL only for a specific value? */
894                 if ( a->acl_attrval.bv_len ) {
895                         if ( val == NULL ) {
896                                 continue;
897                         }
898
899                         if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
900                                 state->as_recorded |= ACL_STATE_RECORDED_VD;
901                                 state->as_vd_acl = a;
902                                 state->as_vd_acl_count = *count;
903                                 state->as_vd_access = a->acl_access;
904                                 state->as_vd_access_count = 1;
905                                 ACL_INVALIDATE( state->as_vd_acl_mask );
906                         }
907
908                         if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
909                                 Debug( LDAP_DEBUG_ACL,
910                                         "acl_get: valpat %s\n",
911                                         a->acl_attrval.bv_val, 0, 0 );
912                                 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
913                                 {
914                                         continue;
915                                 }
916
917                         } else {
918                                 int match = 0;
919                                 const char *text;
920                                 Debug( LDAP_DEBUG_ACL,
921                                         "acl_get: val %s\n",
922                                         a->acl_attrval.bv_val, 0, 0 );
923         
924                                 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
925                                         if (value_match( &match, desc,
926                                                 /* desc->ad_type->sat_equality */ a->acl_attrval_mr, 0,
927                                                 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
928                                                         match )
929                                                 continue;
930                                         
931                                 } else {
932                                         int             patlen, vdnlen;
933         
934                                         patlen = a->acl_attrval.bv_len;
935                                         vdnlen = val->bv_len;
936         
937                                         if ( vdnlen < patlen )
938                                                 continue;
939         
940                                         if ( a->acl_attrval_style == ACL_STYLE_BASE ) {
941                                                 if ( vdnlen > patlen )
942                                                         continue;
943         
944                                         } else if ( a->acl_attrval_style == ACL_STYLE_ONE ) {
945                                                 int rdnlen = -1;
946         
947                                                 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
948                                                         continue;
949         
950                                                 rdnlen = dn_rdnlen( NULL, val );
951                                                 if ( rdnlen != vdnlen - patlen - 1 )
952                                                         continue;
953         
954                                         } else if ( a->acl_attrval_style == ACL_STYLE_SUBTREE ) {
955                                                 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
956                                                         continue;
957         
958                                         } else if ( a->acl_attrval_style == ACL_STYLE_CHILDREN ) {
959                                                 if ( vdnlen <= patlen )
960                                                         continue;
961         
962                                                 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
963                                                         continue;
964                                         }
965         
966                                         if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
967                                                 continue;
968                                 }
969                         }
970                 }
971
972                 if ( a->acl_filter != NULL ) {
973                         ber_int_t rc = test_filter( NULL, e, a->acl_filter );
974                         if ( rc != LDAP_COMPARE_TRUE ) {
975                                 continue;
976                         }
977                 }
978
979                 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
980                        *count, attr, 0);
981                 return a;
982         }
983
984         Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
985         return( NULL );
986 }
987
988 static int
989 acl_mask_dn(
990         Operation               *op,
991         Entry                   *e,
992         AccessControl           *a,
993         int                     nmatch,
994         regmatch_t              *matches,
995         slap_dn_access          *b,
996         struct berval           *opndn )
997 {
998         /*
999          * if access applies to the entry itself, and the
1000          * user is bound as somebody in the same namespace as
1001          * the entry, OR the given dn matches the dn pattern
1002          */
1003         /*
1004          * NOTE: styles "anonymous", "users" and "self" 
1005          * have been moved to enum slap_style_t, whose 
1006          * value is set in a_dn_style; however, the string
1007          * is maintaned in a_dn_pat.
1008          */
1009         if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
1010                 if ( !BER_BVISEMPTY( opndn ) ) {
1011                         return 1;
1012                 }
1013
1014         } else if ( b->a_style == ACL_STYLE_USERS ) {
1015                 if ( BER_BVISEMPTY( opndn ) ) {
1016                         return 1;
1017                 }
1018
1019         } else if ( b->a_style == ACL_STYLE_SELF ) {
1020                 struct berval   ndn, selfndn;
1021                 int             level;
1022
1023                 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
1024                         return 1;
1025                 }
1026
1027                 level = b->a_self_level;
1028                 if ( level < 0 ) {
1029                         selfndn = *opndn;
1030                         ndn = e->e_nname;
1031                         level = -level;
1032
1033                 } else {
1034                         ndn = *opndn;
1035                         selfndn = e->e_nname;
1036                 }
1037
1038                 for ( ; level > 0; level-- ) {
1039                         if ( BER_BVISEMPTY( &ndn ) ) {
1040                                 break;
1041                         }
1042                         dnParent( &ndn, &ndn );
1043                 }
1044                         
1045                 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
1046                 {
1047                         return 1;
1048                 }
1049
1050         } else if ( b->a_style == ACL_STYLE_REGEX ) {
1051                 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
1052                         int             tmp_nmatch;
1053                         regmatch_t      tmp_matches[2],
1054                                         *tmp_matchesp = tmp_matches;
1055
1056                         int             rc = 0;
1057
1058                         switch ( a->acl_dn_style ) {
1059                         case ACL_STYLE_REGEX:
1060                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1061                                         tmp_matchesp = matches;
1062                                         tmp_nmatch = nmatch;
1063                                         break;
1064                                 }
1065                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1066
1067                         case ACL_STYLE_BASE:
1068                                 tmp_matches[0].rm_so = 0;
1069                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1070                                 tmp_nmatch = 1;
1071                                 break;
1072
1073                         case ACL_STYLE_ONE:
1074                         case ACL_STYLE_SUBTREE:
1075                         case ACL_STYLE_CHILDREN:
1076                                 tmp_matches[0].rm_so = 0;
1077                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1078                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1079                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1080                                 tmp_nmatch = 2;
1081                                 break;
1082
1083                         default:
1084                                 /* error */
1085                                 rc = 1;
1086                                 break;
1087                         }
1088
1089                         if ( rc ) {
1090                                 return 1;
1091                         }
1092
1093                         if ( !regex_matches( &b->a_pat, opndn->bv_val,
1094                                 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
1095                         {
1096                                 return 1;
1097                         }
1098                 }
1099
1100         } else {
1101                 struct berval   pat;
1102                 ber_len_t       patlen, odnlen;
1103                 int             got_match = 0;
1104
1105                 if ( e->e_dn == NULL )
1106                         return 1;
1107
1108                 if ( b->a_expand ) {
1109                         struct berval   bv;
1110                         char            buf[ACL_BUF_SIZE];
1111                         
1112                         int             tmp_nmatch;
1113                         regmatch_t      tmp_matches[2],
1114                                         *tmp_matchesp = tmp_matches;
1115
1116                         int             rc = 0;
1117
1118                         bv.bv_len = sizeof( buf ) - 1;
1119                         bv.bv_val = buf;
1120
1121                         switch ( a->acl_dn_style ) {
1122                         case ACL_STYLE_REGEX:
1123                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1124                                         tmp_matchesp = matches;
1125                                         tmp_nmatch = nmatch;
1126                                         break;
1127                                 }
1128                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1129
1130                         case ACL_STYLE_BASE:
1131                                 tmp_matches[0].rm_so = 0;
1132                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1133                                 tmp_nmatch = 1;
1134                                 break;
1135
1136                         case ACL_STYLE_ONE:
1137                         case ACL_STYLE_SUBTREE:
1138                         case ACL_STYLE_CHILDREN:
1139                                 tmp_matches[0].rm_so = 0;
1140                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
1141                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1142                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
1143                                 tmp_nmatch = 2;
1144                                 break;
1145
1146                         default:
1147                                 /* error */
1148                                 rc = 1;
1149                                 break;
1150                         }
1151
1152                         if ( rc ) {
1153                                 return 1;
1154                         }
1155
1156                         if ( acl_string_expand( &bv, &b->a_pat, 
1157                                         e->e_nname.bv_val,
1158                                         tmp_nmatch, tmp_matchesp ) )
1159                         {
1160                                 return 1;
1161                         }
1162                         
1163                         if ( dnNormalize(0, NULL, NULL, &bv,
1164                                         &pat, op->o_tmpmemctx )
1165                                         != LDAP_SUCCESS )
1166                         {
1167                                 /* did not expand to a valid dn */
1168                                 return 1;
1169                         }
1170
1171                 } else {
1172                         pat = b->a_pat;
1173                 }
1174
1175                 patlen = pat.bv_len;
1176                 odnlen = opndn->bv_len;
1177                 if ( odnlen < patlen ) {
1178                         goto dn_match_cleanup;
1179
1180                 }
1181
1182                 if ( b->a_style == ACL_STYLE_BASE ) {
1183                         /* base dn -- entire object DN must match */
1184                         if ( odnlen != patlen ) {
1185                                 goto dn_match_cleanup;
1186                         }
1187
1188                 } else if ( b->a_style == ACL_STYLE_ONE ) {
1189                         int rdnlen = -1;
1190
1191                         if ( odnlen <= patlen ) {
1192                                 goto dn_match_cleanup;
1193                         }
1194
1195                         if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1196                                 goto dn_match_cleanup;
1197                         }
1198
1199                         rdnlen = dn_rdnlen( NULL, opndn );
1200                         if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
1201                                 goto dn_match_cleanup;
1202                         }
1203
1204                 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
1205                         if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1206                                 goto dn_match_cleanup;
1207                         }
1208
1209                 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
1210                         if ( odnlen <= patlen ) {
1211                                 goto dn_match_cleanup;
1212                         }
1213
1214                         if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
1215                                 goto dn_match_cleanup;
1216                         }
1217
1218                 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
1219                         int             level = b->a_level;
1220                         struct berval   ndn;
1221
1222                         if ( odnlen <= patlen ) {
1223                                 goto dn_match_cleanup;
1224                         }
1225
1226                         if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
1227                         {
1228                                 goto dn_match_cleanup;
1229                         }
1230                         
1231                         ndn = *opndn;
1232                         for ( ; level > 0; level-- ) {
1233                                 if ( BER_BVISEMPTY( &ndn ) ) {
1234                                         goto dn_match_cleanup;
1235                                 }
1236                                 dnParent( &ndn, &ndn );
1237                                 if ( ndn.bv_len < patlen ) {
1238                                         goto dn_match_cleanup;
1239                                 }
1240                         }
1241                         
1242                         if ( ndn.bv_len != patlen ) {
1243                                 goto dn_match_cleanup;
1244                         }
1245                 }
1246
1247                 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
1248
1249 dn_match_cleanup:;
1250                 if ( pat.bv_val != b->a_pat.bv_val ) {
1251                         slap_sl_free( pat.bv_val, op->o_tmpmemctx );
1252                 }
1253
1254                 if ( !got_match ) {
1255                         return 1;
1256                 }
1257         }
1258
1259         return 0;
1260 }
1261
1262 /*
1263  * Record value-dependent access control state
1264  */
1265 #define ACL_RECORD_VALUE_STATE do { \
1266                 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
1267                         state->as_recorded |= ACL_STATE_RECORDED_VD; \
1268                         state->as_vd_acl = a; \
1269                         AC_MEMCPY( state->as_vd_acl_matches, matches, \
1270                                 sizeof( state->as_vd_acl_matches )) ; \
1271                         state->as_vd_acl_count = count; \
1272                         state->as_vd_access = b; \
1273                         state->as_vd_access_count = i; \
1274                 } \
1275         } while( 0 )
1276
1277 static int
1278 acl_mask_dnattr(
1279         Operation               *op,
1280         Entry                   *e,
1281         struct berval           *val,
1282         AccessControl           *a,
1283         Access                  *b,
1284         int                     i,
1285         regmatch_t              *matches,
1286         int                     count,
1287         AccessControlState      *state,
1288         slap_dn_access          *bdn,
1289         struct berval           *opndn )
1290 {
1291         Attribute       *at;
1292         struct berval   bv;
1293         int             rc, match = 0;
1294         const char      *text;
1295         const char      *attr = bdn->a_at->ad_cname.bv_val;
1296
1297         assert( attr != NULL );
1298
1299         if ( BER_BVISEMPTY( opndn ) ) {
1300                 return 1;
1301         }
1302
1303         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
1304         bv = *opndn;
1305
1306         /* see if asker is listed in dnattr */
1307         for ( at = attrs_find( e->e_attrs, bdn->a_at );
1308                 at != NULL;
1309                 at = attrs_find( at->a_next, bdn->a_at ) )
1310         {
1311                 if ( value_find_ex( bdn->a_at,
1312                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1313                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1314                         at->a_nvals,
1315                         &bv, op->o_tmpmemctx ) == 0 )
1316                 {
1317                         /* found it */
1318                         match = 1;
1319                         break;
1320                 }
1321         }
1322
1323         if ( match ) {
1324                 /* have a dnattr match. if this is a self clause then
1325                  * the target must also match the op dn.
1326                  */
1327                 if ( bdn->a_self ) {
1328                         /* check if the target is an attribute. */
1329                         if ( val == NULL ) return 1;
1330
1331                         /* target is attribute, check if the attribute value
1332                          * is the op dn.
1333                          */
1334                         rc = value_match( &match, bdn->a_at,
1335                                 bdn->a_at->ad_type->sat_equality, 0,
1336                                 val, &bv, &text );
1337                         /* on match error or no match, fail the ACL clause */
1338                         if ( rc != LDAP_SUCCESS || match != 0 )
1339                                 return 1;
1340                 }
1341
1342         } else {
1343                 /* no dnattr match, check if this is a self clause */
1344                 if ( ! bdn->a_self )
1345                         return 1;
1346
1347                 ACL_RECORD_VALUE_STATE;
1348                 
1349                 /* this is a self clause, check if the target is an
1350                  * attribute.
1351                  */
1352                 if ( val == NULL )
1353                         return 1;
1354
1355                 /* target is attribute, check if the attribute value
1356                  * is the op dn.
1357                  */
1358                 rc = value_match( &match, bdn->a_at,
1359                         bdn->a_at->ad_type->sat_equality, 0,
1360                         val, &bv, &text );
1361
1362                 /* on match error or no match, fail the ACL clause */
1363                 if ( rc != LDAP_SUCCESS || match != 0 )
1364                         return 1;
1365         }
1366
1367         return 0;
1368 }
1369
1370
1371 /*
1372  * slap_acl_mask - modifies mask based upon the given acl and the
1373  * requested access to entry e, attribute attr, value val.  if val
1374  * is null, access to the whole attribute is assumed (all values).
1375  *
1376  * returns      0       access NOT allowed
1377  *              1       access allowed
1378  */
1379
1380 static slap_control_t
1381 slap_acl_mask(
1382         AccessControl           *a,
1383         slap_mask_t             *mask,
1384         Operation               *op,
1385         Entry                   *e,
1386         AttributeDescription    *desc,
1387         struct berval           *val,
1388         int                     nmatch,
1389         regmatch_t              *matches,
1390         int                     count,
1391         AccessControlState      *state )
1392 {
1393         int             i;
1394         Access          *b;
1395 #ifdef LDAP_DEBUG
1396         char            accessmaskbuf[ACCESSMASK_MAXLEN];
1397 #if !defined( SLAP_DYNACL ) && defined( SLAPD_ACI_ENABLED )
1398         char            accessmaskbuf1[ACCESSMASK_MAXLEN];
1399 #endif /* !SLAP_DYNACL && SLAPD_ACI_ENABLED */
1400 #endif /* DEBUG */
1401         const char      *attr;
1402         slap_mask_t     a2pmask = ACL_ACCESS2PRIV( *mask );
1403
1404         assert( a != NULL );
1405         assert( mask != NULL );
1406         assert( desc != NULL );
1407
1408         attr = desc->ad_cname.bv_val;
1409
1410         assert( attr != NULL );
1411
1412         Debug( LDAP_DEBUG_ACL,
1413                 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1414                 e->e_dn, attr, 0 );
1415
1416         Debug( LDAP_DEBUG_ACL,
1417                 "=> acl_mask: to %s by \"%s\", (%s) \n",
1418                 val ? "value" : "all values",
1419                 op->o_ndn.bv_val ?  op->o_ndn.bv_val : "",
1420                 accessmask2str( *mask, accessmaskbuf, 1 ) );
1421
1422
1423         if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1424                 && state->as_vd_acl == a )
1425         {
1426                 b = state->as_vd_access;
1427                 i = state->as_vd_access_count;
1428
1429         } else {
1430                 b = a->acl_access;
1431                 i = 1;
1432         }
1433
1434         for ( ; b != NULL; b = b->a_next, i++ ) {
1435                 slap_mask_t oldmask, modmask;
1436
1437                 ACL_INVALIDATE( modmask );
1438
1439                 /* AND <who> clauses */
1440                 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1441                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1442                                 b->a_dn_pat.bv_val, 0, 0);
1443                         /*
1444                          * if access applies to the entry itself, and the
1445                          * user is bound as somebody in the same namespace as
1446                          * the entry, OR the given dn matches the dn pattern
1447                          */
1448                         /*
1449                          * NOTE: styles "anonymous", "users" and "self" 
1450                          * have been moved to enum slap_style_t, whose 
1451                          * value is set in a_dn_style; however, the string
1452                          * is maintaned in a_dn_pat.
1453                          */
1454
1455                         if ( acl_mask_dn( op, e, a, nmatch, matches,
1456                                 &b->a_dn, &op->o_ndn ) )
1457                         {
1458                                 continue;
1459                         }
1460                 }
1461
1462                 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1463                         struct berval   ndn;
1464
1465                         Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1466                                 b->a_realdn_pat.bv_val, 0, 0);
1467                         /*
1468                          * if access applies to the entry itself, and the
1469                          * user is bound as somebody in the same namespace as
1470                          * the entry, OR the given dn matches the dn pattern
1471                          */
1472                         /*
1473                          * NOTE: styles "anonymous", "users" and "self" 
1474                          * have been moved to enum slap_style_t, whose 
1475                          * value is set in a_dn_style; however, the string
1476                          * is maintaned in a_dn_pat.
1477                          */
1478
1479                         if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1480                         {
1481                                 ndn = op->o_conn->c_ndn;
1482                         } else {
1483                                 ndn = op->o_ndn;
1484                         }
1485
1486                         if ( acl_mask_dn( op, e, a, nmatch, matches,
1487                                 &b->a_realdn, &ndn ) )
1488                         {
1489                                 continue;
1490                         }
1491                 }
1492
1493                 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1494                         if ( ! op->o_conn->c_listener ) {
1495                                 continue;
1496                         }
1497                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1498                                 b->a_sockurl_pat.bv_val, 0, 0 );
1499
1500                         if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1501                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1502                                         if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1503                                                         e->e_ndn, nmatch, matches ) ) 
1504                                         {
1505                                                 continue;
1506                                         }
1507
1508                                 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1509                                         struct berval   bv;
1510                                         char buf[ACL_BUF_SIZE];
1511
1512                                         bv.bv_len = sizeof( buf ) - 1;
1513                                         bv.bv_val = buf;
1514                                         if ( acl_string_expand( &bv, &b->a_sockurl_pat,
1515                                                         e->e_ndn, nmatch, matches ) )
1516                                         {
1517                                                 continue;
1518                                         }
1519
1520                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1521                                         {
1522                                                 continue;
1523                                         }
1524
1525                                 } else {
1526                                         if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1527                                         {
1528                                                 continue;
1529                                         }
1530                                 }
1531                         }
1532                 }
1533
1534                 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1535                         if ( !op->o_conn->c_peer_domain.bv_val ) {
1536                                 continue;
1537                         }
1538                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1539                                 b->a_domain_pat.bv_val, 0, 0 );
1540                         if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1541                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1542                                         if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1543                                                         e->e_ndn, nmatch, matches ) ) 
1544                                         {
1545                                                 continue;
1546                                         }
1547                                 } else {
1548                                         char buf[ACL_BUF_SIZE];
1549
1550                                         struct berval   cmp = op->o_conn->c_peer_domain;
1551                                         struct berval   pat = b->a_domain_pat;
1552
1553                                         if ( b->a_domain_expand ) {
1554                                                 struct berval bv;
1555
1556                                                 bv.bv_len = sizeof(buf) - 1;
1557                                                 bv.bv_val = buf;
1558
1559                                                 if ( acl_string_expand(&bv, &b->a_domain_pat,
1560                                                                 e->e_ndn, nmatch, matches) )
1561                                                 {
1562                                                         continue;
1563                                                 }
1564                                                 pat = bv;
1565                                         }
1566
1567                                         if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1568                                                 int offset = cmp.bv_len - pat.bv_len;
1569                                                 if ( offset < 0 ) {
1570                                                         continue;
1571                                                 }
1572
1573                                                 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1574                                                         continue;
1575                                                 }
1576
1577                                                 /* trim the domain */
1578                                                 cmp.bv_val = &cmp.bv_val[ offset ];
1579                                                 cmp.bv_len -= offset;
1580                                         }
1581                                         
1582                                         if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1583                                                 continue;
1584                                         }
1585                                 }
1586                         }
1587                 }
1588
1589                 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1590                         if ( !op->o_conn->c_peer_name.bv_val ) {
1591                                 continue;
1592                         }
1593                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1594                                 b->a_peername_pat.bv_val, 0, 0 );
1595                         if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1596                                 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1597                                         if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1598                                                         e->e_ndn, nmatch, matches ) ) 
1599                                         {
1600                                                 continue;
1601                                         }
1602
1603                                 } else {
1604                                         /* try exact match */
1605                                         if ( b->a_peername_style == ACL_STYLE_BASE ) {
1606                                                 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1607                                                         continue;
1608                                                 }
1609
1610                                         } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1611                                                 struct berval   bv;
1612                                                 char buf[ACL_BUF_SIZE];
1613
1614                                                 bv.bv_len = sizeof( buf ) - 1;
1615                                                 bv.bv_val = buf;
1616                                                 if ( acl_string_expand( &bv, &b->a_peername_pat,
1617                                                                 e->e_ndn, nmatch, matches ) )
1618                                                 {
1619                                                         continue;
1620                                                 }
1621
1622                                                 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1623                                                         continue;
1624                                                 }
1625
1626                                         /* extract IP and try exact match */
1627                                         } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1628                                                 char            *port;
1629                                                 char            buf[] = "255.255.255.255";
1630                                                 struct berval   ip;
1631                                                 unsigned long   addr;
1632                                                 int             port_number = -1;
1633                                                 
1634                                                 if ( strncasecmp( op->o_conn->c_peer_name.bv_val, 
1635                                                                         aci_bv[ ACI_BV_IP_EQ ].bv_val,
1636                                                                         aci_bv[ ACI_BV_IP_EQ ].bv_len ) != 0 ) 
1637                                                         continue;
1638
1639                                                 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv[ ACI_BV_IP_EQ ].bv_len;
1640                                                 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv[ ACI_BV_IP_EQ ].bv_len;
1641
1642                                                 port = strrchr( ip.bv_val, ':' );
1643                                                 if ( port ) {
1644                                                         char    *next;
1645                                                         
1646                                                         ip.bv_len = port - ip.bv_val;
1647                                                         ++port;
1648                                                         port_number = strtol( port, &next, 10 );
1649                                                         if ( next[0] != '\0' )
1650                                                                 continue;
1651                                                 }
1652                                                 
1653                                                 /* the port check can be anticipated here */
1654                                                 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1655                                                         continue;
1656                                                 
1657                                                 /* address longer than expected? */
1658                                                 if ( ip.bv_len >= sizeof(buf) )
1659                                                         continue;
1660
1661                                                 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1662                                                 buf[ ip.bv_len ] = '\0';
1663
1664                                                 addr = inet_addr( buf );
1665
1666                                                 /* unable to convert? */
1667                                                 if ( addr == (unsigned long)(-1) )
1668                                                         continue;
1669
1670                                                 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1671                                                         continue;
1672
1673 #ifdef LDAP_PF_LOCAL
1674                                         /* extract path and try exact match */
1675                                         } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1676                                                 struct berval path;
1677                                                 
1678                                                 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1679                                                                         aci_bv[ ACI_BV_PATH_EQ ].bv_val,
1680                                                                         aci_bv[ ACI_BV_PATH_EQ ].bv_len ) != 0 )
1681                                                         continue;
1682
1683                                                 path.bv_val = op->o_conn->c_peer_name.bv_val
1684                                                         + aci_bv[ ACI_BV_PATH_EQ ].bv_len;
1685                                                 path.bv_len = op->o_conn->c_peer_name.bv_len
1686                                                         - aci_bv[ ACI_BV_PATH_EQ ].bv_len;
1687
1688                                                 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1689                                                         continue;
1690
1691 #endif /* LDAP_PF_LOCAL */
1692
1693                                         /* exact match (very unlikely...) */
1694                                         } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1695                                                         continue;
1696                                         }
1697                                 }
1698                         }
1699                 }
1700
1701                 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1702                         if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1703                                 continue;
1704                         }
1705                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1706                                 b->a_sockname_pat.bv_val, 0, 0 );
1707                         if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1708                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1709                                         if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1710                                                         e->e_ndn, nmatch, matches ) ) 
1711                                         {
1712                                                 continue;
1713                                         }
1714
1715                                 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1716                                         struct berval   bv;
1717                                         char buf[ACL_BUF_SIZE];
1718
1719                                         bv.bv_len = sizeof( buf ) - 1;
1720                                         bv.bv_val = buf;
1721                                         if ( acl_string_expand( &bv, &b->a_sockname_pat,
1722                                                         e->e_ndn, nmatch, matches ) )
1723                                         {
1724                                                 continue;
1725                                         }
1726
1727                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1728                                                 continue;
1729                                         }
1730
1731                                 } else {
1732                                         if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1733                                                 continue;
1734                                         }
1735                                 }
1736                         }
1737                 }
1738
1739                 if ( b->a_dn_at != NULL ) {
1740                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1741                                         matches, count, state,
1742                                         &b->a_dn, &op->o_ndn ) )
1743                         {
1744                                 continue;
1745                         }
1746                 }
1747
1748                 if ( b->a_realdn_at != NULL ) {
1749                         struct berval   ndn;
1750
1751                         if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1752                         {
1753                                 ndn = op->o_conn->c_ndn;
1754                         } else {
1755                                 ndn = op->o_ndn;
1756                         }
1757
1758                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1759                                         matches, count, state,
1760                                         &b->a_realdn, &ndn ) )
1761                         {
1762                                 continue;
1763                         }
1764                 }
1765
1766                 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1767                         struct berval bv;
1768                         struct berval ndn = BER_BVNULL;
1769                         int rc;
1770
1771                         if ( op->o_ndn.bv_len == 0 ) {
1772                                 continue;
1773                         }
1774
1775                         Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1776                                 b->a_group_pat.bv_val, 0, 0 );
1777
1778                         /* b->a_group is an unexpanded entry name, expanded it should be an 
1779                          * entry with objectclass group* and we test to see if odn is one of
1780                          * the values in the attribute group
1781                          */
1782                         /* see if asker is listed in dnattr */
1783                         if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1784                                 char            buf[ACL_BUF_SIZE];
1785                                 int             tmp_nmatch;
1786                                 regmatch_t      tmp_matches[2],
1787                                                 *tmp_matchesp = tmp_matches;
1788
1789                                 bv.bv_len = sizeof(buf) - 1;
1790                                 bv.bv_val = buf;
1791
1792                                 rc = 0;
1793
1794                                 switch ( a->acl_dn_style ) {
1795                                 case ACL_STYLE_REGEX:
1796                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1797                                                 tmp_matchesp = matches;
1798                                                 tmp_nmatch = nmatch;
1799                                                 break;
1800                                         }
1801
1802                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1803                                 case ACL_STYLE_BASE:
1804                                         tmp_matches[0].rm_so = 0;
1805                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1806                                         tmp_nmatch = 1;
1807                                         break;
1808
1809                                 case ACL_STYLE_ONE:
1810                                 case ACL_STYLE_SUBTREE:
1811                                 case ACL_STYLE_CHILDREN:
1812                                         tmp_matches[0].rm_so = 0;
1813                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1814                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1815                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1816                                         tmp_nmatch = 2;
1817                                         break;
1818
1819                                 default:
1820                                         /* error */
1821                                         rc = 1;
1822                                         break;
1823                                 }
1824
1825                                 if ( rc ) {
1826                                         continue;
1827                                 }
1828                                 
1829                                 if ( acl_string_expand( &bv, &b->a_group_pat,
1830                                                 e->e_nname.bv_val,
1831                                                 tmp_nmatch, tmp_matchesp ) )
1832                                 {
1833                                         continue;
1834                                 }
1835
1836                                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1837                                                 op->o_tmpmemctx ) != LDAP_SUCCESS )
1838                                 {
1839                                         /* did not expand to a valid dn */
1840                                         continue;
1841                                 }
1842
1843                                 bv = ndn;
1844
1845                         } else {
1846                                 bv = b->a_group_pat;
1847                         }
1848
1849                         rc = backend_group( op, e, &bv, &op->o_ndn,
1850                                 b->a_group_oc, b->a_group_at );
1851
1852                         if ( ndn.bv_val ) {
1853                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1854                         }
1855
1856                         if ( rc != 0 ) {
1857                                 continue;
1858                         }
1859                 }
1860
1861                 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1862                         struct berval   bv;
1863                         char            buf[ACL_BUF_SIZE];
1864
1865                         Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1866                                 b->a_set_pat.bv_val, 0, 0 );
1867
1868                         if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1869                                 int             tmp_nmatch;
1870                                 regmatch_t      tmp_matches[2],
1871                                                 *tmp_matchesp = tmp_matches;
1872                                 int             rc = 0;
1873
1874                                 bv.bv_len = sizeof( buf ) - 1;
1875                                 bv.bv_val = buf;
1876
1877                                 rc = 0;
1878
1879                                 switch ( a->acl_dn_style ) {
1880                                 case ACL_STYLE_REGEX:
1881                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1882                                                 tmp_matchesp = matches;
1883                                                 tmp_nmatch = nmatch;
1884                                                 break;
1885                                         }
1886
1887                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1888                                 case ACL_STYLE_BASE:
1889                                         tmp_matches[0].rm_so = 0;
1890                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1891                                         tmp_nmatch = 1;
1892                                         break;
1893
1894                                 case ACL_STYLE_ONE:
1895                                 case ACL_STYLE_SUBTREE:
1896                                 case ACL_STYLE_CHILDREN:
1897                                         tmp_matches[0].rm_so = 0;
1898                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1899                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1900                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1901                                         tmp_nmatch = 2;
1902                                         break;
1903
1904                                 default:
1905                                         /* error */
1906                                         rc = 1;
1907                                         break;
1908                                 }
1909
1910                                 if ( rc ) {
1911                                         continue;
1912                                 }
1913                                 
1914                                 if ( acl_string_expand( &bv, &b->a_set_pat,
1915                                                 e->e_nname.bv_val,
1916                                                 tmp_nmatch, tmp_matchesp ) )
1917                                 {
1918                                         continue;
1919                                 }
1920
1921                         } else {
1922                                 bv = b->a_set_pat;
1923                         }
1924                         
1925                         if ( acl_match_set( &bv, op, e, 0 ) == 0 ) {
1926                                 continue;
1927                         }
1928                 }
1929
1930                 if ( b->a_authz.sai_ssf ) {
1931                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1932                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
1933                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
1934                                 continue;
1935                         }
1936                 }
1937
1938                 if ( b->a_authz.sai_transport_ssf ) {
1939                         Debug( LDAP_DEBUG_ACL,
1940                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1941                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1942                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
1943                                 continue;
1944                         }
1945                 }
1946
1947                 if ( b->a_authz.sai_tls_ssf ) {
1948                         Debug( LDAP_DEBUG_ACL,
1949                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1950                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1951                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
1952                                 continue;
1953                         }
1954                 }
1955
1956                 if ( b->a_authz.sai_sasl_ssf ) {
1957                         Debug( LDAP_DEBUG_ACL,
1958                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1959                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1960                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
1961                                 continue;
1962                         }
1963                 }
1964
1965 #ifdef SLAP_DYNACL
1966                 if ( b->a_dynacl ) {
1967                         slap_dynacl_t   *da;
1968                         slap_access_t   tgrant, tdeny;
1969
1970                         Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1971                                 0, 0, 0 );
1972
1973                         /* this case works different from the others above.
1974                          * since aci's themselves give permissions, we need
1975                          * to first check b->a_access_mask, the ACL's access level.
1976                          */
1977                         if ( BER_BVISEMPTY( &e->e_nname ) ) {
1978                                 /* no ACIs in the root DSE */
1979                                 continue;
1980                         }
1981
1982                         /* first check if the right being requested
1983                          * is allowed by the ACL clause.
1984                          */
1985                         if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1986                                 continue;
1987                         }
1988
1989                         /* start out with nothing granted, nothing denied */
1990                         ACL_INIT(tgrant);
1991                         ACL_INIT(tdeny);
1992
1993                         for ( da = b->a_dynacl; da; da = da->da_next ) {
1994                                 slap_access_t   grant,
1995                                                 deny;
1996
1997                                 ACL_INIT(grant);
1998                                 ACL_INIT(deny);
1999
2000                                 Debug( LDAP_DEBUG_ACL, "    <= check a_dynacl: %s\n",
2001                                         da->da_name, 0, 0 );
2002
2003                                 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
2004
2005                                 tgrant |= grant;
2006                                 tdeny |= deny;
2007                         }
2008
2009                         /* remove anything that the ACL clause does not allow */
2010                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
2011                         tdeny &= ACL_PRIV_MASK;
2012
2013                         /* see if we have anything to contribute */
2014                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
2015                                 continue;
2016                         }
2017
2018                         /* this could be improved by changing slap_acl_mask so that it can deal with
2019                          * by clauses that return grant/deny pairs.  Right now, it does either
2020                          * additive or subtractive rights, but not both at the same time.  So,
2021                          * we need to combine the grant/deny pair into a single rights mask in
2022                          * a smart way:  if either grant or deny is "empty", then we use the
2023                          * opposite as is, otherwise we remove any denied rights from the grant
2024                          * rights mask and construct an additive mask.
2025                          */
2026                         if (ACL_IS_INVALID(tdeny)) {
2027                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
2028
2029                         } else if (ACL_IS_INVALID(tgrant)) {
2030                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2031
2032                         } else {
2033                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2034                         }
2035
2036                 } else
2037 #else /* !SLAP_DYNACL */
2038
2039 #ifdef SLAPD_ACI_ENABLED
2040                 if ( b->a_aci_at != NULL ) {
2041                         Attribute       *at;
2042                         slap_access_t   grant, deny, tgrant, tdeny;
2043                         struct berval   parent_ndn;
2044                         BerVarray       bvals = NULL;
2045                         int             ret, stop;
2046
2047                         Debug( LDAP_DEBUG_ACL, "    <= check a_aci_at: %s\n",
2048                                 b->a_aci_at->ad_cname.bv_val, 0, 0 );
2049
2050                         /* this case works different from the others above.
2051                          * since aci's themselves give permissions, we need
2052                          * to first check b->a_access_mask, the ACL's access level.
2053                          */
2054
2055                         if ( BER_BVISEMPTY( &e->e_nname ) ) {
2056                                 /* no ACIs in the root DSE */
2057                                 continue;
2058                         }
2059
2060                         /* first check if the right being requested
2061                          * is allowed by the ACL clause.
2062                          */
2063                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
2064                                 continue;
2065                         }
2066                         /* start out with nothing granted, nothing denied */
2067                         ACL_INIT(tgrant);
2068                         ACL_INIT(tdeny);
2069
2070                         /* get the aci attribute */
2071                         at = attr_find( e->e_attrs, b->a_aci_at );
2072                         if ( at != NULL ) {
2073 #if 0
2074                                 /* FIXME: this breaks acl caching;
2075                                  * see also ACL_RECORD_VALUE_STATE below */
2076                                 ACL_RECORD_VALUE_STATE;
2077 #endif
2078                                 /* the aci is an multi-valued attribute.  The
2079                                 * rights are determined by OR'ing the individual
2080                                 * rights given by the acis.
2081                                 */
2082                                 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2083                                         if ( aci_mask( op,
2084                                                 e, desc, val,
2085                                                 &at->a_nvals[i],
2086                                                 nmatch, matches,
2087                                                 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0 )
2088                                         {
2089                                                 tgrant |= grant;
2090                                                 tdeny |= deny;
2091                                         }
2092                                 }
2093                                 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2094                                           accessmask2str(tgrant, accessmaskbuf, 1), 
2095                                           accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2096
2097                         }
2098                         /* If the entry level aci didn't contain anything valid for the 
2099                          * current operation, climb up the tree and evaluate the
2100                          * acis with scope set to subtree
2101                          */
2102                         if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
2103                                 dnParent( &e->e_nname, &parent_ndn );
2104                                 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
2105                                         Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
2106                                         ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
2107                                         switch(ret){
2108                                         case LDAP_SUCCESS :
2109                                                 stop = 0;
2110                                                 if (!bvals){
2111                                                         break;
2112                                                 }
2113
2114                                                 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++ ) {
2115 #if 0
2116                                                         /* FIXME: this breaks acl caching;
2117                                                          * see also ACL_RECORD_VALUE_STATE above */
2118                                                         ACL_RECORD_VALUE_STATE;
2119 #endif
2120                                                         if ( aci_mask( op, e, desc, val, &bvals[i],
2121                                                                         nmatch, matches,
2122                                                                         &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2123                                                         {
2124                                                                 tgrant |= grant;
2125                                                                 tdeny |= deny;
2126                                                                 /* evaluation stops as soon as either a "deny" or a 
2127                                                                  * "grant" directive matches.
2128                                                                  */
2129                                                                 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
2130                                                                         stop = 1;
2131                                                                 }
2132                                                         }
2133                                                         Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n", 
2134                                                                 accessmask2str(tgrant, accessmaskbuf, 1),
2135                                                                 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2136                                                 }
2137                                                 break;
2138
2139                                         case LDAP_NO_SUCH_ATTRIBUTE:
2140                                                 /* just go on if the aci-Attribute is not present in
2141                                                  * the current entry 
2142                                                  */
2143                                                 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
2144                                                 stop = 0;
2145                                                 break;
2146
2147                                         case LDAP_NO_SUCH_OBJECT:
2148                                                 /* We have reached the base object */
2149                                                 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
2150                                                 stop = 1;
2151                                                 break;
2152
2153                                         default:
2154                                                 stop = 1;
2155                                                 break;
2156                                         }
2157                                         if (stop){
2158                                                 break;
2159                                         }
2160                                         dnParent( &parent_ndn, &parent_ndn );
2161                                 }
2162                         }
2163
2164
2165                         /* remove anything that the ACL clause does not allow */
2166                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
2167                         tdeny &= ACL_PRIV_MASK;
2168
2169                         /* see if we have anything to contribute */
2170                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
2171                                 continue;
2172                         }
2173
2174                         /* this could be improved by changing slap_acl_mask so that it can deal with
2175                          * by clauses that return grant/deny pairs.  Right now, it does either
2176                          * additive or subtractive rights, but not both at the same time.  So,
2177                          * we need to combine the grant/deny pair into a single rights mask in
2178                          * a smart way:  if either grant or deny is "empty", then we use the
2179                          * opposite as is, otherwise we remove any denied rights from the grant
2180                          * rights mask and construct an additive mask.
2181                          */
2182                         if (ACL_IS_INVALID(tdeny)) {
2183                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
2184
2185                         } else if (ACL_IS_INVALID(tgrant)) {
2186                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2187
2188                         } else {
2189                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2190                         }
2191
2192                 } else
2193 #endif /* SLAPD_ACI_ENABLED */
2194 #endif /* !SLAP_DYNACL */
2195                 {
2196                         modmask = b->a_access_mask;
2197                 }
2198
2199                 Debug( LDAP_DEBUG_ACL,
2200                         "<= acl_mask: [%d] applying %s (%s)\n",
2201                         i, accessmask2str( modmask, accessmaskbuf, 1 ), 
2202                         b->a_type == ACL_CONTINUE
2203                                 ? "continue"
2204                                 : b->a_type == ACL_BREAK
2205                                         ? "break"
2206                                         : "stop" );
2207                 /* save old mask */
2208                 oldmask = *mask;
2209
2210                 if( ACL_IS_ADDITIVE(modmask) ) {
2211                         /* add privs */
2212                         ACL_PRIV_SET( *mask, modmask );
2213
2214                         /* cleanup */
2215                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2216
2217                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
2218                         /* substract privs */
2219                         ACL_PRIV_CLR( *mask, modmask );
2220
2221                         /* cleanup */
2222                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2223
2224                 } else {
2225                         /* assign privs */
2226                         *mask = modmask;
2227                 }
2228
2229                 a2pmask = *mask;
2230
2231                 Debug( LDAP_DEBUG_ACL,
2232                         "<= acl_mask: [%d] mask: %s\n",
2233                         i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
2234
2235                 if( b->a_type == ACL_CONTINUE ) {
2236                         continue;
2237
2238                 } else if ( b->a_type == ACL_BREAK ) {
2239                         return ACL_BREAK;
2240
2241                 } else {
2242                         return ACL_STOP;
2243                 }
2244         }
2245
2246         /* implicit "by * none" clause */
2247         ACL_INIT(*mask);
2248
2249         Debug( LDAP_DEBUG_ACL,
2250                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
2251                 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
2252         return ACL_STOP;
2253 }
2254
2255 /*
2256  * acl_check_modlist - check access control on the given entry to see if
2257  * it allows the given modifications by the user associated with op.
2258  * returns      1       if mods allowed ok
2259  *              0       mods not allowed
2260  */
2261
2262 int
2263 acl_check_modlist(
2264         Operation       *op,
2265         Entry   *e,
2266         Modifications   *mlist )
2267 {
2268         struct berval *bv;
2269         AccessControlState state = ACL_STATE_INIT;
2270         Backend *be;
2271         int be_null = 0;
2272         int ret = 1; /* default is access allowed */
2273
2274         be = op->o_bd;
2275         if ( be == NULL ) {
2276                 be = LDAP_STAILQ_FIRST(&backendDB);
2277                 be_null = 1;
2278                 op->o_bd = be;
2279         }
2280         assert( be != NULL );
2281
2282         /* short circuit root database access */
2283         if ( be_isroot( op ) ) {
2284                 Debug( LDAP_DEBUG_ACL,
2285                         "<= acl_access_allowed: granted to database root\n",
2286                     0, 0, 0 );
2287                 goto done;
2288         }
2289
2290         /* use backend default access if no backend acls */
2291         if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
2292                 Debug( LDAP_DEBUG_ACL,
2293                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
2294                         access2str( ACL_WRITE ),
2295                         op->o_bd->be_dfltaccess >= ACL_WRITE
2296                                 ? "granted" : "denied",
2297                         op->o_dn.bv_val );
2298                 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
2299                 goto done;
2300         }
2301
2302         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
2303                 /*
2304                  * Internal mods are ignored by ACL_WRITE checking
2305                  */
2306                 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
2307                         Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
2308                                 " modify access granted\n",
2309                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2310                         continue;
2311                 }
2312
2313                 /*
2314                  * no-user-modification operational attributes are ignored
2315                  * by ACL_WRITE checking as any found here are not provided
2316                  * by the user
2317                  */
2318                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
2319                                 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
2320                 {
2321                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
2322                                 " modify access granted\n",
2323                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2324                         continue;
2325                 }
2326
2327                 switch ( mlist->sml_op ) {
2328                 case LDAP_MOD_REPLACE:
2329                         /*
2330                          * We must check both permission to delete the whole
2331                          * attribute and permission to add the specific attributes.
2332                          * This prevents abuse from selfwriters.
2333                          */
2334                         if ( ! access_allowed( op, e,
2335                                 mlist->sml_desc, NULL,
2336                                 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2337                                 &state ) )
2338                         {
2339                                 ret = 0;
2340                                 goto done;
2341                         }
2342
2343                         if ( mlist->sml_values == NULL ) break;
2344
2345                         /* fall thru to check value to add */
2346
2347                 case LDAP_MOD_ADD:
2348                         assert( mlist->sml_values != NULL );
2349
2350                         for ( bv = mlist->sml_nvalues
2351                                         ? mlist->sml_nvalues : mlist->sml_values;
2352                                 bv->bv_val != NULL; bv++ )
2353                         {
2354                                 if ( ! access_allowed( op, e,
2355                                         mlist->sml_desc, bv,
2356                                         ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
2357                                         &state ) )
2358                                 {
2359                                         ret = 0;
2360                                         goto done;
2361                                 }
2362                         }
2363                         break;
2364
2365                 case LDAP_MOD_DELETE:
2366                         if ( mlist->sml_values == NULL ) {
2367                                 if ( ! access_allowed( op, e,
2368                                         mlist->sml_desc, NULL,
2369                                         ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2370                                         NULL ) )
2371                                 {
2372                                         ret = 0;
2373                                         goto done;
2374                                 }
2375                                 break;
2376                         }
2377                         for ( bv = mlist->sml_nvalues
2378                                         ? mlist->sml_nvalues : mlist->sml_values;
2379                                 bv->bv_val != NULL; bv++ )
2380                         {
2381                                 if ( ! access_allowed( op, e,
2382                                         mlist->sml_desc, bv,
2383                                         ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2384                                         &state ) )
2385                                 {
2386                                         ret = 0;
2387                                         goto done;
2388                                 }
2389                         }
2390                         break;
2391
2392                 case SLAP_MOD_SOFTADD:
2393                         /* allow adding attribute via modrdn thru */
2394                         break;
2395
2396                 default:
2397                         assert( 0 );
2398                         /* not reached */
2399                         ret = 0;
2400                         break;
2401                 }
2402         }
2403
2404 done:
2405         if (be_null) op->o_bd = NULL;
2406         return( ret );
2407 }
2408
2409 int
2410 acl_get_part(
2411         struct berval   *list,
2412         int             ix,
2413         char            sep,
2414         struct berval   *bv )
2415 {
2416         int     len;
2417         char    *p;
2418
2419         if ( bv ) {
2420                 BER_BVZERO( bv );
2421         }
2422         len = list->bv_len;
2423         p = list->bv_val;
2424         while ( len >= 0 && --ix >= 0 ) {
2425                 while ( --len >= 0 && *p++ != sep )
2426                         ;
2427         }
2428         while ( len >= 0 && *p == ' ' ) {
2429                 len--;
2430                 p++;
2431         }
2432         if ( len < 0 ) {
2433                 return -1;
2434         }
2435
2436         if ( !bv ) {
2437                 return 0;
2438         }
2439
2440         bv->bv_val = p;
2441         while ( --len >= 0 && *p != sep ) {
2442                 bv->bv_len++;
2443                 p++;
2444         }
2445         while ( bv->bv_len > 0 && *--p == ' ' ) {
2446                 bv->bv_len--;
2447         }
2448         
2449         return bv->bv_len;
2450 }
2451
2452 typedef struct acl_set_gather_t {
2453         SetCookie               *cookie;
2454         BerVarray               bvals;
2455 } acl_set_gather_t;
2456
2457 static int
2458 acl_set_cb_gather( Operation *op, SlapReply *rs )
2459 {
2460         acl_set_gather_t        *p = (acl_set_gather_t *)op->o_callback->sc_private;
2461         
2462         if ( rs->sr_type == REP_SEARCH ) {
2463                 BerValue        bvals[ 2 ];
2464                 BerVarray       bvalsp = NULL;
2465                 int             j;
2466
2467                 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2468                         AttributeDescription    *desc = rs->sr_attrs[ j ].an_desc;
2469                         
2470                         if ( desc == slap_schema.si_ad_entryDN ) {
2471                                 bvalsp = bvals;
2472                                 bvals[ 0 ] = rs->sr_entry->e_nname;
2473                                 BER_BVZERO( &bvals[ 1 ] );
2474
2475                         } else {
2476                                 Attribute       *a;
2477
2478                                 a = attr_find( rs->sr_entry->e_attrs, desc );
2479                                 if ( a != NULL ) {
2480                                         int     i;
2481
2482                                         for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2483                                                 ;
2484
2485                                         bvalsp = a->a_nvals;
2486                                 }
2487                         }
2488                 }
2489
2490                 if ( bvals ) {
2491                         p->bvals = slap_set_join( p->cookie, p->bvals,
2492                                         ( '|' | SLAP_SET_RREF ), bvalsp );
2493                 }
2494
2495         } else {
2496                 assert( rs->sr_type == REP_RESULT );
2497         }
2498
2499         return 0;
2500 }
2501
2502 BerVarray
2503 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2504 {
2505         AclSetCookie            *cp = (AclSetCookie *)cookie;
2506         int                     rc = 0;
2507         LDAPURLDesc             *ludp = NULL;
2508         Operation               op2 = { 0 };
2509         SlapReply               rs = {REP_RESULT};
2510         AttributeName           anlist[ 2 ], *anlistp = NULL;
2511         int                     nattrs = 0;
2512         slap_callback           cb = { NULL, acl_set_cb_gather, NULL, NULL };
2513         acl_set_gather_t        p = { 0 };
2514         const char              *text = NULL;
2515         static struct berval    defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2516
2517         /* this routine needs to return the bervals instead of
2518          * plain strings, since syntax is not known.  It should
2519          * also return the syntax or some "comparison cookie".
2520          */
2521         if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2522                 return acl_set_gather2( cookie, name, desc );
2523         }
2524
2525         rc = ldap_url_parse( name->bv_val, &ludp );
2526         if ( rc != LDAP_URL_SUCCESS ) {
2527                 rc = LDAP_PROTOCOL_ERROR;
2528                 goto url_done;
2529         }
2530         
2531         if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2532         {
2533                 /* host part must be empty */
2534                 /* extensions parts must be empty */
2535                 rc = LDAP_PROTOCOL_ERROR;
2536                 goto url_done;
2537         }
2538
2539         /* Grab the searchbase and see if an appropriate database can be found */
2540         ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2541         rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2542                         &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2543         BER_BVZERO( &op2.o_req_dn );
2544         if ( rc != LDAP_SUCCESS ) {
2545                 goto url_done;
2546         }
2547
2548         op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2549         if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2550                 rc = LDAP_NO_SUCH_OBJECT;
2551                 goto url_done;
2552         }
2553
2554         /* Grab the filter */
2555         if ( ludp->lud_filter ) {
2556                 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2557                                 cp->asc_op->o_tmpmemctx );
2558                 
2559         } else {
2560                 op2.ors_filterstr = defaultFilter_bv;
2561         }
2562
2563         op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2564         if ( op2.ors_filter == NULL ) {
2565                 rc = LDAP_PROTOCOL_ERROR;
2566                 goto url_done;
2567         }
2568
2569         /* Grab the scope */
2570         op2.ors_scope = ludp->lud_scope;
2571
2572         /* Grap the attributes */
2573         if ( ludp->lud_attrs ) {
2574                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2575                         ;
2576
2577                 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2578                                 cp->asc_op->o_tmpmemctx );
2579
2580                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2581                         ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2582                         anlistp[ nattrs ].an_desc = NULL;
2583                         rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2584                                         &anlistp[ nattrs ].an_desc, &text );
2585                         if ( rc != LDAP_SUCCESS ) {
2586                                 goto url_done;
2587                         }
2588                 }
2589
2590         } else {
2591                 anlistp = anlist;
2592         }
2593
2594         anlistp[ nattrs ].an_name = desc->ad_cname;
2595         anlistp[ nattrs ].an_desc = desc;
2596
2597         BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2598         
2599         p.cookie = cookie;
2600         
2601         op2.o_hdr = cp->asc_op->o_hdr;
2602         op2.o_tag = LDAP_REQ_SEARCH;
2603         op2.o_ndn = op2.o_bd->be_rootndn;
2604         op2.o_callback = &cb;
2605         op2.o_time = slap_get_time();
2606         op2.o_do_not_cache = 1;
2607         op2.o_is_auth_check = 0;
2608         ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2609         op2.ors_slimit = SLAP_NO_LIMIT;
2610         op2.ors_tlimit = SLAP_NO_LIMIT;
2611         op2.ors_attrs = anlistp;
2612         op2.ors_attrsonly = 0;
2613         op2.o_private = cp->asc_op->o_private;
2614
2615         cb.sc_private = &p;
2616
2617         rc = op2.o_bd->be_search( &op2, &rs );
2618         if ( rc != 0 ) {
2619                 goto url_done;
2620         }
2621
2622 url_done:;
2623         if ( op2.ors_filter ) {
2624                 filter_free_x( cp->asc_op, op2.ors_filter );
2625         }
2626         if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2627                 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2628         }
2629         if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2630                 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2631         }
2632         if ( ludp ) {
2633                 ldap_free_urldesc( ludp );
2634         }
2635         if ( anlistp && anlistp != anlist ) {
2636                 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2637         }
2638
2639         return p.bvals;
2640 }
2641
2642 BerVarray
2643 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2644 {
2645         AclSetCookie    *cp = (AclSetCookie *)cookie;
2646         BerVarray       bvals = NULL;
2647         struct berval   ndn;
2648         int             rc = 0;
2649
2650         /* this routine needs to return the bervals instead of
2651          * plain strings, since syntax is not known.  It should
2652          * also return the syntax or some "comparison cookie".
2653          */
2654         rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2655         if ( rc == LDAP_SUCCESS ) {
2656                 if ( desc == slap_schema.si_ad_entryDN ) {
2657                         bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2658                                         cp->asc_op->o_tmpmemctx );
2659                         bvals[ 0 ] = ndn;
2660                         BER_BVZERO( &bvals[ 1 ] );
2661                         BER_BVZERO( &ndn );
2662
2663                 } else {
2664                         backend_attribute( cp->asc_op,
2665                                 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2666                 }
2667
2668                 if ( !BER_BVISNULL( &ndn ) ) {
2669                         slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2670                 }
2671         }
2672
2673         return bvals;
2674 }
2675
2676 int
2677 acl_match_set (
2678         struct berval *subj,
2679         Operation *op,
2680         Entry *e,
2681         int setref )
2682 {
2683         struct berval   set = BER_BVNULL;
2684         int             rc = 0;
2685         AclSetCookie    cookie;
2686
2687         if ( setref == 0 ) {
2688                 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2689
2690         } else {
2691                 struct berval           subjdn, ndn = BER_BVNULL;
2692                 struct berval           setat;
2693                 BerVarray               bvals;
2694                 const char              *text;
2695                 AttributeDescription    *desc = NULL;
2696
2697                 /* format of string is "entry/setAttrName" */
2698                 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2699                         return 0;
2700                 }
2701
2702                 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2703                         setat = aci_bv[ ACI_BV_SET_ATTR ];
2704                 }
2705
2706                 /*
2707                  * NOTE: dnNormalize honors the ber_len field
2708                  * as the length of the dn to be normalized
2709                  */
2710                 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2711                         if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2712                         {
2713                                 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2714                                 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2715                                         int     i;
2716
2717                                         set = bvals[0];
2718                                         BER_BVZERO( &bvals[0] );
2719                                         for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2720                                                 /* count */ ;
2721                                         bvals[0].bv_val = bvals[i-1].bv_val;
2722                                         BER_BVZERO( &bvals[i-1] );
2723                                 }
2724                                 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2725                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2726                         }
2727                 }
2728         }
2729
2730         if ( !BER_BVISNULL( &set ) ) {
2731                 cookie.asc_op = op;
2732                 cookie.asc_e = e;
2733                 rc = ( slap_set_filter(
2734                         acl_set_gather,
2735                         (SetCookie *)&cookie, &set,
2736                         &op->o_ndn, &e->e_nname, NULL ) > 0 );
2737                 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2738         }
2739
2740         return(rc);
2741 }
2742
2743 #ifdef SLAP_DYNACL
2744
2745 /*
2746  * dynamic ACL infrastructure
2747  */
2748 static slap_dynacl_t    *da_list = NULL;
2749
2750 int
2751 slap_dynacl_register( slap_dynacl_t *da )
2752 {
2753         slap_dynacl_t   *tmp;
2754
2755         for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2756                 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2757                         break;
2758                 }
2759         }
2760
2761         if ( tmp != NULL ) {
2762                 return -1;
2763         }
2764         
2765         if ( da->da_mask == NULL ) {
2766                 return -1;
2767         }
2768         
2769         da->da_private = NULL;
2770         da->da_next = da_list;
2771         da_list = da;
2772
2773         return 0;
2774 }
2775
2776 static slap_dynacl_t *
2777 slap_dynacl_next( slap_dynacl_t *da )
2778 {
2779         if ( da ) {
2780                 return da->da_next;
2781         }
2782         return da_list;
2783 }
2784
2785 slap_dynacl_t *
2786 slap_dynacl_get( const char *name )
2787 {
2788         slap_dynacl_t   *da;
2789
2790         for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2791                 if ( strcasecmp( da->da_name, name ) == 0 ) {
2792                         break;
2793                 }
2794         }
2795
2796         return da;
2797 }
2798 #endif /* SLAP_DYNACL */
2799
2800 int
2801 acl_init( void )
2802 {
2803         int     rc = 0;
2804
2805 #ifdef SLAPD_ACI_ENABLED
2806 #ifdef SLAP_DYNACL
2807         rc = dynacl_aci_init();
2808 #else /* !SLAP_DYNACL */
2809         rc = aci_init();
2810 #endif /* !SLAP_DYNACL */
2811         if ( rc != 0 ) {
2812                 return rc;
2813         }
2814 #endif /* SLAPD_ACI_ENABLED */
2815
2816         return rc;
2817 }
2818
2819 int
2820 acl_string_expand(
2821         struct berval   *bv,
2822         struct berval   *pat,
2823         char            *match,
2824         int             nmatch,
2825         regmatch_t      *matches)
2826 {
2827         ber_len_t       size;
2828         char   *sp;
2829         char   *dp;
2830         int     flag;
2831
2832         size = 0;
2833         bv->bv_val[0] = '\0';
2834         bv->bv_len--; /* leave space for lone $ */
2835
2836         flag = 0;
2837         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2838                 sp < pat->bv_val + pat->bv_len ; sp++ )
2839         {
2840                 /* did we previously see a $ */
2841                 if ( flag ) {
2842                         if ( flag == 1 && *sp == '$' ) {
2843                                 *dp++ = '$';
2844                                 size++;
2845                                 flag = 0;
2846
2847                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2848                                 flag = 2;
2849
2850                         } else if ( *sp >= '0' && *sp <= '9' ) {
2851                                 int     n;
2852                                 int     i;
2853                                 int     l;
2854
2855                                 n = *sp - '0';
2856
2857                                 if ( flag == 2 ) {
2858                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2859                                                 if ( *sp >= '0' && *sp <= '9' ) {
2860                                                         n = 10*n + ( *sp - '0' );
2861                                                 }
2862                                         }
2863
2864                                         if ( *sp != /*'{'*/ '}' ) {
2865                                                 /* FIXME: error */
2866                                                 return 1;
2867                                         }
2868                                 }
2869
2870                                 if ( n >= nmatch ) {
2871                                         /* FIXME: error */
2872                                         return 1;
2873                                 }
2874                                 
2875                                 *dp = '\0';
2876                                 i = matches[n].rm_so;
2877                                 l = matches[n].rm_eo; 
2878                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2879                                         *dp++ = match[i];
2880                                 }
2881                                 *dp = '\0';
2882
2883                                 flag = 0;
2884                         }
2885                 } else {
2886                         if (*sp == '$') {
2887                                 flag = 1;
2888                         } else {
2889                                 *dp++ = *sp;
2890                                 size++;
2891                         }
2892                 }
2893         }
2894
2895         if ( flag ) {
2896                 /* must have ended with a single $ */
2897                 *dp++ = '$';
2898                 size++;
2899         }
2900
2901         *dp = '\0';
2902         bv->bv_len = size;
2903
2904         Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2905         Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2906
2907         return 0;
2908 }
2909
2910 static int
2911 regex_matches(
2912         struct berval   *pat,           /* pattern to expand and match against */
2913         char            *str,           /* string to match against pattern */
2914         char            *buf,           /* buffer with $N expansion variables */
2915         int             nmatch, /* size of the matches array */
2916         regmatch_t      *matches        /* offsets in buffer for $N expansion variables */
2917 )
2918 {
2919         regex_t re;
2920         char newbuf[ACL_BUF_SIZE];
2921         struct berval bv;
2922         int     rc;
2923
2924         bv.bv_len = sizeof( newbuf ) - 1;
2925         bv.bv_val = newbuf;
2926
2927         if (str == NULL) {
2928                 str = "";
2929         };
2930
2931         acl_string_expand( &bv, pat, buf, nmatch, matches );
2932         rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2933         if ( rc ) {
2934                 char error[ACL_BUF_SIZE];
2935                 regerror( rc, &re, error, sizeof( error ) );
2936
2937                 Debug( LDAP_DEBUG_TRACE,
2938                     "compile( \"%s\", \"%s\") failed %s\n",
2939                         pat->bv_val, str, error );
2940                 return( 0 );
2941         }
2942
2943         rc = regexec( &re, str, 0, NULL, 0 );
2944         regfree( &re );
2945
2946         Debug( LDAP_DEBUG_TRACE,
2947             "=> regex_matches: string:   %s\n", str, 0, 0 );
2948         Debug( LDAP_DEBUG_TRACE,
2949             "=> regex_matches: rc: %d %s\n",
2950                 rc, !rc ? "matches" : "no matches", 0 );
2951         return( !rc );
2952 }
2953