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