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