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