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