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