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