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