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