]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
don't care about empty matched/text #ifdef LDAP_NULL_IS_NULL
[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                                                         char    *next;
1607                                                         
1608                                                         ip.bv_len = port - ip.bv_val;
1609                                                         ++port;
1610                                                         port_number = strtol( port, &next, 10 );
1611                                                         if ( next[0] != '\0' )
1612                                                                 continue;
1613                                                 }
1614                                                 
1615                                                 /* the port check can be anticipated here */
1616                                                 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1617                                                         continue;
1618                                                 
1619                                                 /* address longer than expected? */
1620                                                 if ( ip.bv_len >= sizeof(buf) )
1621                                                         continue;
1622
1623                                                 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1624                                                 buf[ ip.bv_len ] = '\0';
1625
1626                                                 addr = inet_addr( buf );
1627
1628                                                 /* unable to convert? */
1629                                                 if ( addr == (unsigned long)(-1) )
1630                                                         continue;
1631
1632                                                 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1633                                                         continue;
1634
1635 #ifdef LDAP_PF_LOCAL
1636                                         /* extract path and try exact match */
1637                                         } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1638                                                 struct berval path;
1639                                                 
1640                                                 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1641                                                                         acl_bv_path_eq.bv_val,
1642                                                                         acl_bv_path_eq.bv_len ) != 0 )
1643                                                         continue;
1644
1645                                                 path.bv_val = op->o_conn->c_peer_name.bv_val
1646                                                         + acl_bv_path_eq.bv_len;
1647                                                 path.bv_len = op->o_conn->c_peer_name.bv_len
1648                                                         - acl_bv_path_eq.bv_len;
1649
1650                                                 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1651                                                         continue;
1652
1653 #endif /* LDAP_PF_LOCAL */
1654
1655                                         /* exact match (very unlikely...) */
1656                                         } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1657                                                         continue;
1658                                         }
1659                                 }
1660                         }
1661                 }
1662
1663                 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1664                         if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1665                                 continue;
1666                         }
1667                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1668                                 b->a_sockname_pat.bv_val, 0, 0 );
1669                         if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1670                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1671                                         if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1672                                                         e->e_ndn, nmatch, matches ) ) 
1673                                         {
1674                                                 continue;
1675                                         }
1676
1677                                 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1678                                         struct berval   bv;
1679                                         char buf[ACL_BUF_SIZE];
1680
1681                                         bv.bv_len = sizeof( buf ) - 1;
1682                                         bv.bv_val = buf;
1683                                         if ( acl_string_expand( &bv, &b->a_sockname_pat,
1684                                                         e->e_ndn, nmatch, matches ) )
1685                                         {
1686                                                 continue;
1687                                         }
1688
1689                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1690                                                 continue;
1691                                         }
1692
1693                                 } else {
1694                                         if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1695                                                 continue;
1696                                         }
1697                                 }
1698                         }
1699                 }
1700
1701                 if ( b->a_dn_at != NULL ) {
1702                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1703                                         matches, count, state,
1704                                         &b->a_dn, &op->o_ndn ) )
1705                         {
1706                                 continue;
1707                         }
1708                 }
1709
1710                 if ( b->a_realdn_at != NULL ) {
1711                         struct berval   ndn;
1712
1713                         if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1714                         {
1715                                 ndn = op->o_conn->c_ndn;
1716                         } else {
1717                                 ndn = op->o_ndn;
1718                         }
1719
1720                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1721                                         matches, count, state,
1722                                         &b->a_realdn, &ndn ) )
1723                         {
1724                                 continue;
1725                         }
1726                 }
1727
1728                 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1729                         struct berval bv;
1730                         struct berval ndn = BER_BVNULL;
1731                         int rc;
1732
1733                         if ( op->o_ndn.bv_len == 0 ) {
1734                                 continue;
1735                         }
1736
1737                         Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1738                                 b->a_group_pat.bv_val, 0, 0 );
1739
1740                         /* b->a_group is an unexpanded entry name, expanded it should be an 
1741                          * entry with objectclass group* and we test to see if odn is one of
1742                          * the values in the attribute group
1743                          */
1744                         /* see if asker is listed in dnattr */
1745                         if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1746                                 char            buf[ACL_BUF_SIZE];
1747                                 int             tmp_nmatch;
1748                                 regmatch_t      tmp_matches[2],
1749                                                 *tmp_matchesp = tmp_matches;
1750
1751                                 bv.bv_len = sizeof(buf) - 1;
1752                                 bv.bv_val = buf;
1753
1754                                 rc = 0;
1755
1756                                 switch ( a->acl_dn_style ) {
1757                                 case ACL_STYLE_REGEX:
1758                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1759                                                 tmp_matchesp = matches;
1760                                                 tmp_nmatch = nmatch;
1761                                                 break;
1762                                         }
1763
1764                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1765                                 case ACL_STYLE_BASE:
1766                                         tmp_matches[0].rm_so = 0;
1767                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1768                                         tmp_nmatch = 1;
1769                                         break;
1770
1771                                 case ACL_STYLE_ONE:
1772                                 case ACL_STYLE_SUBTREE:
1773                                 case ACL_STYLE_CHILDREN:
1774                                         tmp_matches[0].rm_so = 0;
1775                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1776                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1777                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1778                                         tmp_nmatch = 2;
1779                                         break;
1780
1781                                 default:
1782                                         /* error */
1783                                         rc = 1;
1784                                         break;
1785                                 }
1786
1787                                 if ( rc ) {
1788                                         continue;
1789                                 }
1790                                 
1791                                 if ( acl_string_expand( &bv, &b->a_group_pat,
1792                                                 e->e_nname.bv_val,
1793                                                 tmp_nmatch, tmp_matchesp ) )
1794                                 {
1795                                         continue;
1796                                 }
1797
1798                                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1799                                                 op->o_tmpmemctx ) != LDAP_SUCCESS )
1800                                 {
1801                                         /* did not expand to a valid dn */
1802                                         continue;
1803                                 }
1804
1805                                 bv = ndn;
1806
1807                         } else {
1808                                 bv = b->a_group_pat;
1809                         }
1810
1811                         rc = backend_group( op, e, &bv, &op->o_ndn,
1812                                 b->a_group_oc, b->a_group_at );
1813
1814                         if ( ndn.bv_val ) {
1815                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1816                         }
1817
1818                         if ( rc != 0 ) {
1819                                 continue;
1820                         }
1821                 }
1822
1823                 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1824                         struct berval   bv;
1825                         char            buf[ACL_BUF_SIZE];
1826
1827                         Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1828                                 b->a_set_pat.bv_val, 0, 0 );
1829
1830                         if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1831                                 int             tmp_nmatch;
1832                                 regmatch_t      tmp_matches[2],
1833                                                 *tmp_matchesp = tmp_matches;
1834                                 int             rc = 0;
1835
1836                                 bv.bv_len = sizeof( buf ) - 1;
1837                                 bv.bv_val = buf;
1838
1839                                 rc = 0;
1840
1841                                 switch ( a->acl_dn_style ) {
1842                                 case ACL_STYLE_REGEX:
1843                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1844                                                 tmp_matchesp = matches;
1845                                                 tmp_nmatch = nmatch;
1846                                                 break;
1847                                         }
1848
1849                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1850                                 case ACL_STYLE_BASE:
1851                                         tmp_matches[0].rm_so = 0;
1852                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1853                                         tmp_nmatch = 1;
1854                                         break;
1855
1856                                 case ACL_STYLE_ONE:
1857                                 case ACL_STYLE_SUBTREE:
1858                                 case ACL_STYLE_CHILDREN:
1859                                         tmp_matches[0].rm_so = 0;
1860                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1861                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1862                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1863                                         tmp_nmatch = 2;
1864                                         break;
1865
1866                                 default:
1867                                         /* error */
1868                                         rc = 1;
1869                                         break;
1870                                 }
1871
1872                                 if ( rc ) {
1873                                         continue;
1874                                 }
1875                                 
1876                                 if ( acl_string_expand( &bv, &b->a_set_pat,
1877                                                 e->e_nname.bv_val,
1878                                                 tmp_nmatch, tmp_matchesp ) )
1879                                 {
1880                                         continue;
1881                                 }
1882
1883                         } else {
1884                                 bv = b->a_set_pat;
1885                         }
1886                         
1887                         if ( acl_match_set( &bv, op, e, NULL ) == 0 ) {
1888                                 continue;
1889                         }
1890                 }
1891
1892                 if ( b->a_authz.sai_ssf ) {
1893                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1894                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
1895                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
1896                                 continue;
1897                         }
1898                 }
1899
1900                 if ( b->a_authz.sai_transport_ssf ) {
1901                         Debug( LDAP_DEBUG_ACL,
1902                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1903                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1904                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
1905                                 continue;
1906                         }
1907                 }
1908
1909                 if ( b->a_authz.sai_tls_ssf ) {
1910                         Debug( LDAP_DEBUG_ACL,
1911                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1912                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1913                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
1914                                 continue;
1915                         }
1916                 }
1917
1918                 if ( b->a_authz.sai_sasl_ssf ) {
1919                         Debug( LDAP_DEBUG_ACL,
1920                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1921                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1922                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
1923                                 continue;
1924                         }
1925                 }
1926
1927 #ifdef SLAP_DYNACL
1928                 if ( b->a_dynacl ) {
1929                         slap_dynacl_t   *da;
1930                         slap_access_t   tgrant, tdeny;
1931
1932                         Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1933                                 0, 0, 0 );
1934
1935                         /* this case works different from the others above.
1936                          * since dynamic ACL's themselves give permissions, we need
1937                          * to first check b->a_access_mask, the ACL's access level.
1938                          */
1939                         /* first check if the right being requested
1940                          * is allowed by the ACL clause.
1941                          */
1942                         if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1943                                 continue;
1944                         }
1945
1946                         /* start out with nothing granted, nothing denied */
1947                         ACL_INIT(tgrant);
1948                         ACL_INIT(tdeny);
1949
1950                         for ( da = b->a_dynacl; da; da = da->da_next ) {
1951                                 slap_access_t   grant,
1952                                                 deny;
1953
1954                                 ACL_INIT(grant);
1955                                 ACL_INIT(deny);
1956
1957                                 Debug( LDAP_DEBUG_ACL, "    <= check a_dynacl: %s\n",
1958                                         da->da_name, 0, 0 );
1959
1960                                 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1961
1962                                 tgrant |= grant;
1963                                 tdeny |= deny;
1964                         }
1965
1966                         /* remove anything that the ACL clause does not allow */
1967                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1968                         tdeny &= ACL_PRIV_MASK;
1969
1970                         /* see if we have anything to contribute */
1971                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
1972                                 continue;
1973                         }
1974
1975                         /* this could be improved by changing slap_acl_mask so that it can deal with
1976                          * by clauses that return grant/deny pairs.  Right now, it does either
1977                          * additive or subtractive rights, but not both at the same time.  So,
1978                          * we need to combine the grant/deny pair into a single rights mask in
1979                          * a smart way:  if either grant or deny is "empty", then we use the
1980                          * opposite as is, otherwise we remove any denied rights from the grant
1981                          * rights mask and construct an additive mask.
1982                          */
1983                         if (ACL_IS_INVALID(tdeny)) {
1984                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
1985
1986                         } else if (ACL_IS_INVALID(tgrant)) {
1987                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1988
1989                         } else {
1990                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1991                         }
1992
1993                 } else
1994 #else /* !SLAP_DYNACL */
1995
1996                 /* NOTE: this entire block can be eliminated when SLAP_DYNACL
1997                  * moves outside of LDAP_DEVEL */
1998 #ifdef SLAPD_ACI_ENABLED
1999                 if ( b->a_aci_at != NULL ) {
2000                         Attribute       *at;
2001                         slap_access_t   grant, deny, tgrant, tdeny;
2002                         struct berval   parent_ndn;
2003                         BerVarray       bvals = NULL;
2004                         int             ret, stop;
2005 #ifdef LDAP_DEBUG
2006                         char            accessmaskbuf1[ACCESSMASK_MAXLEN];
2007 #endif /* DEBUG */
2008
2009                         Debug( LDAP_DEBUG_ACL, "    <= check a_aci_at: %s\n",
2010                                 b->a_aci_at->ad_cname.bv_val, 0, 0 );
2011
2012                         /* this case works different from the others above.
2013                          * since aci's themselves give permissions, we need
2014                          * to first check b->a_access_mask, the ACL's access level.
2015                          */
2016
2017                         if ( BER_BVISEMPTY( &e->e_nname ) ) {
2018                                 /* no ACIs in the root DSE */
2019                                 continue;
2020                         }
2021
2022                         /* first check if the right being requested
2023                          * is allowed by the ACL clause.
2024                          */
2025                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
2026                                 continue;
2027                         }
2028                         /* start out with nothing granted, nothing denied */
2029                         ACL_INIT(tgrant);
2030                         ACL_INIT(tdeny);
2031
2032                         /* get the aci attribute */
2033                         at = attr_find( e->e_attrs, b->a_aci_at );
2034                         if ( at != NULL ) {
2035 #if 0
2036                                 /* FIXME: this breaks acl caching;
2037                                  * see also ACL_RECORD_VALUE_STATE below */
2038                                 ACL_RECORD_VALUE_STATE;
2039 #endif
2040                                 /* the aci is an multi-valued attribute.  The
2041                                 * rights are determined by OR'ing the individual
2042                                 * rights given by the acis.
2043                                 */
2044                                 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2045                                         if ( aci_mask( op,
2046                                                 e, desc, val,
2047                                                 &at->a_nvals[i],
2048                                                 nmatch, matches,
2049                                                 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0 )
2050                                         {
2051                                                 tgrant |= grant;
2052                                                 tdeny |= deny;
2053                                         }
2054                                 }
2055                                 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2056                                           accessmask2str(tgrant, accessmaskbuf, 1), 
2057                                           accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2058
2059                         }
2060                         /* If the entry level aci didn't contain anything valid for the 
2061                          * current operation, climb up the tree and evaluate the
2062                          * acis with scope set to subtree
2063                          */
2064                         if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
2065                                 dnParent( &e->e_nname, &parent_ndn );
2066                                 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
2067                                         Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
2068                                         ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
2069                                         switch(ret){
2070                                         case LDAP_SUCCESS :
2071                                                 stop = 0;
2072                                                 if (!bvals){
2073                                                         break;
2074                                                 }
2075
2076                                                 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++ ) {
2077 #if 0
2078                                                         /* FIXME: this breaks acl caching;
2079                                                          * see also ACL_RECORD_VALUE_STATE above */
2080                                                         ACL_RECORD_VALUE_STATE;
2081 #endif
2082                                                         if ( aci_mask( op, e, desc, val, &bvals[i],
2083                                                                         nmatch, matches,
2084                                                                         &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2085                                                         {
2086                                                                 tgrant |= grant;
2087                                                                 tdeny |= deny;
2088                                                                 /* evaluation stops as soon as either a "deny" or a 
2089                                                                  * "grant" directive matches.
2090                                                                  */
2091                                                                 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
2092                                                                         stop = 1;
2093                                                                 }
2094                                                         }
2095                                                         Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n", 
2096                                                                 accessmask2str(tgrant, accessmaskbuf, 1),
2097                                                                 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
2098                                                 }
2099                                                 break;
2100
2101                                         case LDAP_NO_SUCH_ATTRIBUTE:
2102                                                 /* just go on if the aci-Attribute is not present in
2103                                                  * the current entry 
2104                                                  */
2105                                                 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
2106                                                 stop = 0;
2107                                                 break;
2108
2109                                         case LDAP_NO_SUCH_OBJECT:
2110                                                 /* We have reached the base object */
2111                                                 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
2112                                                 stop = 1;
2113                                                 break;
2114
2115                                         default:
2116                                                 stop = 1;
2117                                                 break;
2118                                         }
2119                                         if (stop){
2120                                                 break;
2121                                         }
2122                                         dnParent( &parent_ndn, &parent_ndn );
2123                                 }
2124                         }
2125
2126
2127                         /* remove anything that the ACL clause does not allow */
2128                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
2129                         tdeny &= ACL_PRIV_MASK;
2130
2131                         /* see if we have anything to contribute */
2132                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
2133                                 continue;
2134                         }
2135
2136                         /* this could be improved by changing slap_acl_mask so that it can deal with
2137                          * by clauses that return grant/deny pairs.  Right now, it does either
2138                          * additive or subtractive rights, but not both at the same time.  So,
2139                          * we need to combine the grant/deny pair into a single rights mask in
2140                          * a smart way:  if either grant or deny is "empty", then we use the
2141                          * opposite as is, otherwise we remove any denied rights from the grant
2142                          * rights mask and construct an additive mask.
2143                          */
2144                         if (ACL_IS_INVALID(tdeny)) {
2145                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
2146
2147                         } else if (ACL_IS_INVALID(tgrant)) {
2148                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
2149
2150                         } else {
2151                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
2152                         }
2153
2154                 } else
2155 #endif /* SLAPD_ACI_ENABLED */
2156 #endif /* !SLAP_DYNACL */
2157                 {
2158                         modmask = b->a_access_mask;
2159                 }
2160
2161                 Debug( LDAP_DEBUG_ACL,
2162                         "<= acl_mask: [%d] applying %s (%s)\n",
2163                         i, accessmask2str( modmask, accessmaskbuf, 1 ), 
2164                         b->a_type == ACL_CONTINUE
2165                                 ? "continue"
2166                                 : b->a_type == ACL_BREAK
2167                                         ? "break"
2168                                         : "stop" );
2169                 /* save old mask */
2170                 oldmask = *mask;
2171
2172                 if( ACL_IS_ADDITIVE(modmask) ) {
2173                         /* add privs */
2174                         ACL_PRIV_SET( *mask, modmask );
2175
2176                         /* cleanup */
2177                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2178
2179                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
2180                         /* substract privs */
2181                         ACL_PRIV_CLR( *mask, modmask );
2182
2183                         /* cleanup */
2184                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
2185
2186                 } else {
2187                         /* assign privs */
2188                         *mask = modmask;
2189                 }
2190
2191                 a2pmask = *mask;
2192
2193                 Debug( LDAP_DEBUG_ACL,
2194                         "<= acl_mask: [%d] mask: %s\n",
2195                         i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
2196
2197                 if( b->a_type == ACL_CONTINUE ) {
2198                         continue;
2199
2200                 } else if ( b->a_type == ACL_BREAK ) {
2201                         return ACL_BREAK;
2202
2203                 } else {
2204                         return ACL_STOP;
2205                 }
2206         }
2207
2208         /* implicit "by * none" clause */
2209         ACL_INIT(*mask);
2210
2211         Debug( LDAP_DEBUG_ACL,
2212                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
2213                 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
2214         return ACL_STOP;
2215 }
2216
2217 /*
2218  * acl_check_modlist - check access control on the given entry to see if
2219  * it allows the given modifications by the user associated with op.
2220  * returns      1       if mods allowed ok
2221  *              0       mods not allowed
2222  */
2223
2224 int
2225 acl_check_modlist(
2226         Operation       *op,
2227         Entry   *e,
2228         Modifications   *mlist )
2229 {
2230         struct berval *bv;
2231         AccessControlState state = ACL_STATE_INIT;
2232         Backend *be;
2233         int be_null = 0;
2234         int ret = 1; /* default is access allowed */
2235
2236         be = op->o_bd;
2237         if ( be == NULL ) {
2238                 be = LDAP_STAILQ_FIRST(&backendDB);
2239                 be_null = 1;
2240                 op->o_bd = be;
2241         }
2242         assert( be != NULL );
2243
2244         /* short circuit root database access */
2245         if ( be_isroot( op ) ) {
2246                 Debug( LDAP_DEBUG_ACL,
2247                         "<= acl_access_allowed: granted to database root\n",
2248                     0, 0, 0 );
2249                 goto done;
2250         }
2251
2252         /* use backend default access if no backend acls */
2253         if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
2254                 Debug( LDAP_DEBUG_ACL,
2255                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
2256                         access2str( ACL_WRITE ),
2257                         op->o_bd->be_dfltaccess >= ACL_WRITE
2258                                 ? "granted" : "denied",
2259                         op->o_dn.bv_val );
2260                 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
2261                 goto done;
2262         }
2263
2264         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
2265                 /*
2266                  * Internal mods are ignored by ACL_WRITE checking
2267                  */
2268                 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
2269                         Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
2270                                 " modify access granted\n",
2271                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2272                         continue;
2273                 }
2274
2275                 /*
2276                  * no-user-modification operational attributes are ignored
2277                  * by ACL_WRITE checking as any found here are not provided
2278                  * by the user
2279                  */
2280                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
2281                                 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
2282                 {
2283                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
2284                                 " modify access granted\n",
2285                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
2286                         continue;
2287                 }
2288
2289                 switch ( mlist->sml_op ) {
2290                 case LDAP_MOD_REPLACE:
2291                         /*
2292                          * We must check both permission to delete the whole
2293                          * attribute and permission to add the specific attributes.
2294                          * This prevents abuse from selfwriters.
2295                          */
2296                         if ( ! access_allowed( op, e,
2297                                 mlist->sml_desc, NULL,
2298                                 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2299                                 &state ) )
2300                         {
2301                                 ret = 0;
2302                                 goto done;
2303                         }
2304
2305                         if ( mlist->sml_values == NULL ) break;
2306
2307                         /* fall thru to check value to add */
2308
2309                 case LDAP_MOD_ADD:
2310                         assert( mlist->sml_values != NULL );
2311
2312                         for ( bv = mlist->sml_nvalues
2313                                         ? mlist->sml_nvalues : mlist->sml_values;
2314                                 bv->bv_val != NULL; bv++ )
2315                         {
2316                                 if ( ! access_allowed( op, e,
2317                                         mlist->sml_desc, bv,
2318                                         ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
2319                                         &state ) )
2320                                 {
2321                                         ret = 0;
2322                                         goto done;
2323                                 }
2324                         }
2325                         break;
2326
2327                 case LDAP_MOD_DELETE:
2328                         if ( mlist->sml_values == NULL ) {
2329                                 if ( ! access_allowed( op, e,
2330                                         mlist->sml_desc, NULL,
2331                                         ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2332                                         NULL ) )
2333                                 {
2334                                         ret = 0;
2335                                         goto done;
2336                                 }
2337                                 break;
2338                         }
2339                         for ( bv = mlist->sml_nvalues
2340                                         ? mlist->sml_nvalues : mlist->sml_values;
2341                                 bv->bv_val != NULL; bv++ )
2342                         {
2343                                 if ( ! access_allowed( op, e,
2344                                         mlist->sml_desc, bv,
2345                                         ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
2346                                         &state ) )
2347                                 {
2348                                         ret = 0;
2349                                         goto done;
2350                                 }
2351                         }
2352                         break;
2353
2354                 case SLAP_MOD_SOFTADD:
2355                         /* allow adding attribute via modrdn thru */
2356                         break;
2357
2358                 default:
2359                         assert( 0 );
2360                         /* not reached */
2361                         ret = 0;
2362                         break;
2363                 }
2364         }
2365
2366 done:
2367         if (be_null) op->o_bd = NULL;
2368         return( ret );
2369 }
2370
2371 int
2372 acl_get_part(
2373         struct berval   *list,
2374         int             ix,
2375         char            sep,
2376         struct berval   *bv )
2377 {
2378         int     len;
2379         char    *p;
2380
2381         if ( bv ) {
2382                 BER_BVZERO( bv );
2383         }
2384         len = list->bv_len;
2385         p = list->bv_val;
2386         while ( len >= 0 && --ix >= 0 ) {
2387                 while ( --len >= 0 && *p++ != sep )
2388                         ;
2389         }
2390         while ( len >= 0 && *p == ' ' ) {
2391                 len--;
2392                 p++;
2393         }
2394         if ( len < 0 ) {
2395                 return -1;
2396         }
2397
2398         if ( !bv ) {
2399                 return 0;
2400         }
2401
2402         bv->bv_val = p;
2403         while ( --len >= 0 && *p != sep ) {
2404                 bv->bv_len++;
2405                 p++;
2406         }
2407         while ( bv->bv_len > 0 && *--p == ' ' ) {
2408                 bv->bv_len--;
2409         }
2410         
2411         return bv->bv_len;
2412 }
2413
2414 typedef struct acl_set_gather_t {
2415         SetCookie               *cookie;
2416         BerVarray               bvals;
2417 } acl_set_gather_t;
2418
2419 static int
2420 acl_set_cb_gather( Operation *op, SlapReply *rs )
2421 {
2422         acl_set_gather_t        *p = (acl_set_gather_t *)op->o_callback->sc_private;
2423         
2424         if ( rs->sr_type == REP_SEARCH ) {
2425                 BerValue        bvals[ 2 ];
2426                 BerVarray       bvalsp = NULL;
2427                 int             j;
2428
2429                 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2430                         AttributeDescription    *desc = rs->sr_attrs[ j ].an_desc;
2431                         
2432                         if ( desc == slap_schema.si_ad_entryDN ) {
2433                                 bvalsp = bvals;
2434                                 bvals[ 0 ] = rs->sr_entry->e_nname;
2435                                 BER_BVZERO( &bvals[ 1 ] );
2436
2437                         } else {
2438                                 Attribute       *a;
2439
2440                                 a = attr_find( rs->sr_entry->e_attrs, desc );
2441                                 if ( a != NULL ) {
2442                                         int     i;
2443
2444                                         for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2445                                                 ;
2446
2447                                         bvalsp = a->a_nvals;
2448                                 }
2449                         }
2450                 }
2451
2452                 if ( bvals ) {
2453                         p->bvals = slap_set_join( p->cookie, p->bvals,
2454                                         ( '|' | SLAP_SET_RREF ), bvalsp );
2455                 }
2456
2457         } else {
2458                 assert( rs->sr_type == REP_RESULT );
2459         }
2460
2461         return 0;
2462 }
2463
2464 BerVarray
2465 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2466 {
2467         AclSetCookie            *cp = (AclSetCookie *)cookie;
2468         int                     rc = 0;
2469         LDAPURLDesc             *ludp = NULL;
2470         Operation               op2 = { 0 };
2471         SlapReply               rs = {REP_RESULT};
2472         AttributeName           anlist[ 2 ], *anlistp = NULL;
2473         int                     nattrs = 0;
2474         slap_callback           cb = { NULL, acl_set_cb_gather, NULL, NULL };
2475         acl_set_gather_t        p = { 0 };
2476         const char              *text = NULL;
2477         static struct berval    defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2478
2479         /* this routine needs to return the bervals instead of
2480          * plain strings, since syntax is not known.  It should
2481          * also return the syntax or some "comparison cookie".
2482          */
2483         if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2484                 return acl_set_gather2( cookie, name, desc );
2485         }
2486
2487         rc = ldap_url_parse( name->bv_val, &ludp );
2488         if ( rc != LDAP_URL_SUCCESS ) {
2489                 rc = LDAP_PROTOCOL_ERROR;
2490                 goto url_done;
2491         }
2492         
2493         if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2494         {
2495                 /* host part must be empty */
2496                 /* extensions parts must be empty */
2497                 rc = LDAP_PROTOCOL_ERROR;
2498                 goto url_done;
2499         }
2500
2501         /* Grab the searchbase and see if an appropriate database can be found */
2502         ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2503         rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2504                         &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2505         BER_BVZERO( &op2.o_req_dn );
2506         if ( rc != LDAP_SUCCESS ) {
2507                 goto url_done;
2508         }
2509
2510         op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2511         if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2512                 rc = LDAP_NO_SUCH_OBJECT;
2513                 goto url_done;
2514         }
2515
2516         /* Grab the filter */
2517         if ( ludp->lud_filter ) {
2518                 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2519                                 cp->asc_op->o_tmpmemctx );
2520                 
2521         } else {
2522                 op2.ors_filterstr = defaultFilter_bv;
2523         }
2524
2525         op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2526         if ( op2.ors_filter == NULL ) {
2527                 rc = LDAP_PROTOCOL_ERROR;
2528                 goto url_done;
2529         }
2530
2531         /* Grab the scope */
2532         op2.ors_scope = ludp->lud_scope;
2533
2534         /* Grap the attributes */
2535         if ( ludp->lud_attrs ) {
2536                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2537                         ;
2538
2539                 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2540                                 cp->asc_op->o_tmpmemctx );
2541
2542                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2543                         ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2544                         anlistp[ nattrs ].an_desc = NULL;
2545                         rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2546                                         &anlistp[ nattrs ].an_desc, &text );
2547                         if ( rc != LDAP_SUCCESS ) {
2548                                 goto url_done;
2549                         }
2550                 }
2551
2552         } else {
2553                 anlistp = anlist;
2554         }
2555
2556         anlistp[ nattrs ].an_name = desc->ad_cname;
2557         anlistp[ nattrs ].an_desc = desc;
2558
2559         BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2560         
2561         p.cookie = cookie;
2562         
2563         op2.o_hdr = cp->asc_op->o_hdr;
2564         op2.o_tag = LDAP_REQ_SEARCH;
2565         op2.o_ndn = op2.o_bd->be_rootndn;
2566         op2.o_callback = &cb;
2567         slap_op_time( &op2.o_time, &op2.o_tincr );
2568         op2.o_do_not_cache = 1;
2569         op2.o_is_auth_check = 0;
2570         ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2571         op2.ors_slimit = SLAP_NO_LIMIT;
2572         op2.ors_tlimit = SLAP_NO_LIMIT;
2573         op2.ors_attrs = anlistp;
2574         op2.ors_attrsonly = 0;
2575         op2.o_private = cp->asc_op->o_private;
2576
2577         cb.sc_private = &p;
2578
2579         rc = op2.o_bd->be_search( &op2, &rs );
2580         if ( rc != 0 ) {
2581                 goto url_done;
2582         }
2583
2584 url_done:;
2585         if ( op2.ors_filter ) {
2586                 filter_free_x( cp->asc_op, op2.ors_filter );
2587         }
2588         if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2589                 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2590         }
2591         if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2592                 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2593         }
2594         if ( ludp ) {
2595                 ldap_free_urldesc( ludp );
2596         }
2597         if ( anlistp && anlistp != anlist ) {
2598                 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2599         }
2600
2601         return p.bvals;
2602 }
2603
2604 BerVarray
2605 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2606 {
2607         AclSetCookie    *cp = (AclSetCookie *)cookie;
2608         BerVarray       bvals = NULL;
2609         struct berval   ndn;
2610         int             rc = 0;
2611
2612         /* this routine needs to return the bervals instead of
2613          * plain strings, since syntax is not known.  It should
2614          * also return the syntax or some "comparison cookie".
2615          */
2616         rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2617         if ( rc == LDAP_SUCCESS ) {
2618                 if ( desc == slap_schema.si_ad_entryDN ) {
2619                         bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2620                                         cp->asc_op->o_tmpmemctx );
2621                         bvals[ 0 ] = ndn;
2622                         BER_BVZERO( &bvals[ 1 ] );
2623                         BER_BVZERO( &ndn );
2624
2625                 } else {
2626                         backend_attribute( cp->asc_op,
2627                                 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2628                 }
2629
2630                 if ( !BER_BVISNULL( &ndn ) ) {
2631                         slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2632                 }
2633         }
2634
2635         return bvals;
2636 }
2637
2638 int
2639 acl_match_set (
2640         struct berval *subj,
2641         Operation *op,
2642         Entry *e,
2643         struct berval *default_set_attribute )
2644 {
2645         struct berval   set = BER_BVNULL;
2646         int             rc = 0;
2647         AclSetCookie    cookie;
2648
2649         if ( default_set_attribute == NULL ) {
2650                 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2651
2652         } else {
2653                 struct berval           subjdn, ndn = BER_BVNULL;
2654                 struct berval           setat;
2655                 BerVarray               bvals;
2656                 const char              *text;
2657                 AttributeDescription    *desc = NULL;
2658
2659                 /* format of string is "entry/setAttrName" */
2660                 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2661                         return 0;
2662                 }
2663
2664                 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2665                         setat = *default_set_attribute;
2666                 }
2667
2668                 /*
2669                  * NOTE: dnNormalize honors the ber_len field
2670                  * as the length of the dn to be normalized
2671                  */
2672                 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2673                         if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2674                         {
2675                                 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2676                                 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2677                                         int     i;
2678
2679                                         set = bvals[0];
2680                                         BER_BVZERO( &bvals[0] );
2681                                         for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2682                                                 /* count */ ;
2683                                         bvals[0].bv_val = bvals[i-1].bv_val;
2684                                         BER_BVZERO( &bvals[i-1] );
2685                                 }
2686                                 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2687                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2688                         }
2689                 }
2690         }
2691
2692         if ( !BER_BVISNULL( &set ) ) {
2693                 cookie.asc_op = op;
2694                 cookie.asc_e = e;
2695                 rc = ( slap_set_filter(
2696                         acl_set_gather,
2697                         (SetCookie *)&cookie, &set,
2698                         &op->o_ndn, &e->e_nname, NULL ) > 0 );
2699                 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2700         }
2701
2702         return(rc);
2703 }
2704
2705 #ifdef SLAP_DYNACL
2706
2707 /*
2708  * dynamic ACL infrastructure
2709  */
2710 static slap_dynacl_t    *da_list = NULL;
2711
2712 int
2713 slap_dynacl_register( slap_dynacl_t *da )
2714 {
2715         slap_dynacl_t   *tmp;
2716
2717         for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2718                 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2719                         break;
2720                 }
2721         }
2722
2723         if ( tmp != NULL ) {
2724                 return -1;
2725         }
2726         
2727         if ( da->da_mask == NULL ) {
2728                 return -1;
2729         }
2730         
2731         da->da_private = NULL;
2732         da->da_next = da_list;
2733         da_list = da;
2734
2735         return 0;
2736 }
2737
2738 static slap_dynacl_t *
2739 slap_dynacl_next( slap_dynacl_t *da )
2740 {
2741         if ( da ) {
2742                 return da->da_next;
2743         }
2744         return da_list;
2745 }
2746
2747 slap_dynacl_t *
2748 slap_dynacl_get( const char *name )
2749 {
2750         slap_dynacl_t   *da;
2751
2752         for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2753                 if ( strcasecmp( da->da_name, name ) == 0 ) {
2754                         break;
2755                 }
2756         }
2757
2758         return da;
2759 }
2760 #endif /* SLAP_DYNACL */
2761
2762 /*
2763  * statically built-in dynamic ACL initialization
2764  */
2765 static int (*acl_init_func[])( void ) = {
2766 #ifdef SLAPD_ACI_ENABLED
2767 #ifdef SLAP_DYNACL
2768         dynacl_aci_init,
2769 #else /* !SLAP_DYNACL */
2770         aci_init,
2771 #endif /* !SLAP_DYNACL */
2772 #endif /* SLAPD_ACI_ENABLED */
2773
2774         NULL
2775 };
2776
2777 int
2778 acl_init( void )
2779 {
2780         int     i, rc;
2781
2782         for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2783                 rc = (*(acl_init_func[ i ]))();
2784                 if ( rc != 0 ) {
2785                         return rc;
2786                 }
2787         }
2788
2789         return 0;
2790 }
2791
2792 int
2793 acl_string_expand(
2794         struct berval   *bv,
2795         struct berval   *pat,
2796         char            *match,
2797         int             nmatch,
2798         regmatch_t      *matches)
2799 {
2800         ber_len_t       size;
2801         char   *sp;
2802         char   *dp;
2803         int     flag;
2804
2805         size = 0;
2806         bv->bv_val[0] = '\0';
2807         bv->bv_len--; /* leave space for lone $ */
2808
2809         flag = 0;
2810         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2811                 sp < pat->bv_val + pat->bv_len ; sp++ )
2812         {
2813                 /* did we previously see a $ */
2814                 if ( flag ) {
2815                         if ( flag == 1 && *sp == '$' ) {
2816                                 *dp++ = '$';
2817                                 size++;
2818                                 flag = 0;
2819
2820                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2821                                 flag = 2;
2822
2823                         } else if ( *sp >= '0' && *sp <= '9' ) {
2824                                 int     n;
2825                                 int     i;
2826                                 int     l;
2827
2828                                 n = *sp - '0';
2829
2830                                 if ( flag == 2 ) {
2831                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2832                                                 if ( *sp >= '0' && *sp <= '9' ) {
2833                                                         n = 10*n + ( *sp - '0' );
2834                                                 }
2835                                         }
2836
2837                                         if ( *sp != /*'{'*/ '}' ) {
2838                                                 /* FIXME: error */
2839                                                 return 1;
2840                                         }
2841                                 }
2842
2843                                 if ( n >= nmatch ) {
2844                                         /* FIXME: error */
2845                                         return 1;
2846                                 }
2847                                 
2848                                 *dp = '\0';
2849                                 i = matches[n].rm_so;
2850                                 l = matches[n].rm_eo; 
2851                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2852                                         *dp++ = match[i];
2853                                 }
2854                                 *dp = '\0';
2855
2856                                 flag = 0;
2857                         }
2858                 } else {
2859                         if (*sp == '$') {
2860                                 flag = 1;
2861                         } else {
2862                                 *dp++ = *sp;
2863                                 size++;
2864                         }
2865                 }
2866         }
2867
2868         if ( flag ) {
2869                 /* must have ended with a single $ */
2870                 *dp++ = '$';
2871                 size++;
2872         }
2873
2874         *dp = '\0';
2875         bv->bv_len = size;
2876
2877         Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2878         Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2879
2880         return 0;
2881 }
2882
2883 static int
2884 regex_matches(
2885         struct berval   *pat,           /* pattern to expand and match against */
2886         char            *str,           /* string to match against pattern */
2887         char            *buf,           /* buffer with $N expansion variables */
2888         int             nmatch, /* size of the matches array */
2889         regmatch_t      *matches        /* offsets in buffer for $N expansion variables */
2890 )
2891 {
2892         regex_t re;
2893         char newbuf[ACL_BUF_SIZE];
2894         struct berval bv;
2895         int     rc;
2896
2897         bv.bv_len = sizeof( newbuf ) - 1;
2898         bv.bv_val = newbuf;
2899
2900         if (str == NULL) {
2901                 str = "";
2902         };
2903
2904         acl_string_expand( &bv, pat, buf, nmatch, matches );
2905         rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2906         if ( rc ) {
2907                 char error[ACL_BUF_SIZE];
2908                 regerror( rc, &re, error, sizeof( error ) );
2909
2910                 Debug( LDAP_DEBUG_TRACE,
2911                     "compile( \"%s\", \"%s\") failed %s\n",
2912                         pat->bv_val, str, error );
2913                 return( 0 );
2914         }
2915
2916         rc = regexec( &re, str, 0, NULL, 0 );
2917         regfree( &re );
2918
2919         Debug( LDAP_DEBUG_TRACE,
2920             "=> regex_matches: string:   %s\n", str, 0, 0 );
2921         Debug( LDAP_DEBUG_TRACE,
2922             "=> regex_matches: rc: %d %s\n",
2923                 rc, !rc ? "matches" : "no matches", 0 );
2924         return( !rc );
2925 }
2926