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