]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
3bcd3065c6a12a872b2fef38156fa1b2db76c748
[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-2005 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 #ifdef LDAP_SLAPI
41 #include "slapi/slapi.h"
42 #endif /* LDAPI_SLAPI */
43
44 #define ACL_BUF_SIZE    1024    /* use most appropriate size */
45
46 /*
47  * speed up compares
48  */
49 static struct berval 
50         aci_bv_entry            = BER_BVC("entry"),
51         aci_bv_children         = BER_BVC("children"),
52         aci_bv_onelevel         = BER_BVC("onelevel"),
53         aci_bv_subtree          = BER_BVC("subtree"),
54         aci_bv_br_entry         = BER_BVC("[entry]"),
55         aci_bv_br_all           = BER_BVC("[all]"),
56         aci_bv_access_id        = BER_BVC("access-id"),
57 #if 0
58         aci_bv_anonymous        = BER_BVC("anonymous"),
59 #endif
60         aci_bv_public           = BER_BVC("public"),
61         aci_bv_users            = BER_BVC("users"),
62         aci_bv_self             = BER_BVC("self"),
63         aci_bv_dnattr           = BER_BVC("dnattr"),
64         aci_bv_group            = BER_BVC("group"),
65         aci_bv_role             = BER_BVC("role"),
66         aci_bv_set              = BER_BVC("set"),
67         aci_bv_set_ref          = BER_BVC("set-ref"),
68         aci_bv_grant            = BER_BVC("grant"),
69         aci_bv_deny             = BER_BVC("deny"),
70
71         aci_bv_ip_eq            = BER_BVC("IP="),
72 #ifdef LDAP_PF_LOCAL
73         aci_bv_path_eq          = BER_BVC("PATH="),
74 #if 0
75         aci_bv_dirsep           = BER_BVC(LDAP_DIRSEP),
76 #endif
77 #endif /* LDAP_PF_LOCAL */
78         
79         aci_bv_group_class      = BER_BVC(SLAPD_GROUP_CLASS),
80         aci_bv_group_attr       = BER_BVC(SLAPD_GROUP_ATTR),
81         aci_bv_role_class       = BER_BVC(SLAPD_ROLE_CLASS),
82         aci_bv_role_attr        = BER_BVC(SLAPD_ROLE_ATTR),
83         aci_bv_set_attr         = BER_BVC(SLAPD_ACI_SET_ATTR);
84
85 typedef enum slap_aci_scope_t {
86         SLAP_ACI_SCOPE_ENTRY            = 0x1,
87         SLAP_ACI_SCOPE_CHILDREN         = 0x2,
88         SLAP_ACI_SCOPE_SUBTREE          = ( SLAP_ACI_SCOPE_ENTRY | SLAP_ACI_SCOPE_CHILDREN )
89 } slap_aci_scope_t;
90
91 static AccessControl * acl_get(
92         AccessControl *ac, int *count,
93         Operation *op, Entry *e,
94         AttributeDescription *desc,
95         struct berval *val,
96         int nmatch, regmatch_t *matches,
97         AccessControlState *state );
98
99 static slap_control_t acl_mask(
100         AccessControl *ac, slap_mask_t *mask,
101         Operation *op, Entry *e,
102         AttributeDescription *desc,
103         struct berval *val,
104         int nmatch,
105         regmatch_t *matches,
106         int count,
107         AccessControlState *state );
108
109 #ifdef SLAPD_ACI_ENABLED
110 static int aci_mask(
111         Operation *op, Entry *e,
112         AttributeDescription *desc,
113         struct berval *val,
114         struct berval *aci,
115         int nmatch,
116         regmatch_t *matches,
117         slap_access_t *grant,
118         slap_access_t *deny,
119         slap_aci_scope_t scope);
120 #endif /* SLAPD_ACI_ENABLED */
121
122 static int      regex_matches(
123         struct berval *pat, char *str, char *buf,
124         int nmatch, regmatch_t *matches);
125 static int      string_expand(
126         struct berval *newbuf, struct berval *pattern,
127         char *match, int nmatch, regmatch_t *matches);
128
129 typedef struct AciSetCookie {
130         Operation *op;
131         Entry *e;
132 } AciSetCookie;
133
134 SLAP_SET_GATHER aci_set_gather;
135 SLAP_SET_GATHER aci_set_gather2;
136 static int aci_match_set ( struct berval *subj, Operation *op,
137     Entry *e, int setref );
138
139 /*
140  * access_allowed - check whether op->o_ndn is allowed the requested access
141  * to entry e, attribute attr, value val.  if val is null, access to
142  * the whole attribute is assumed (all values).
143  *
144  * This routine loops through all access controls and calls
145  * acl_mask() on each applicable access control.
146  * The loop exits when a definitive answer is reached or
147  * or no more controls remain.
148  *
149  * returns:
150  *              0       access denied
151  *              1       access granted
152  *
153  * Notes:
154  * - can be legally called with op == NULL
155  * - can be legally called with op->o_bd == NULL
156  */
157
158 int
159 access_allowed_mask(
160         Operation               *op,
161         Entry                   *e,
162         AttributeDescription    *desc,
163         struct berval           *val,
164         slap_access_t           access,
165         AccessControlState      *state,
166         slap_mask_t             *maskp )
167 {
168         int                             ret = 1;
169         int                             count;
170         AccessControl                   *a = NULL;
171         Backend                         *be;
172         int                             be_null = 0;
173
174 #ifdef LDAP_DEBUG
175         char                            accessmaskbuf[ACCESSMASK_MAXLEN];
176 #endif
177         slap_mask_t                     mask;
178         slap_control_t                  control;
179         slap_access_t                   access_level;
180         const char                      *attr;
181         regmatch_t                      matches[MAXREMATCHES];
182         int                             st_same_attr = 0;
183         static AccessControlState       state_init = ACL_STATE_INIT;
184
185         assert( e != NULL );
186         assert( desc != NULL );
187
188         access_level = ACL_LEVEL( access );
189
190         assert( access_level > ACL_NONE );
191         if ( maskp ) ACL_INVALIDATE( *maskp );
192
193         attr = desc->ad_cname.bv_val;
194
195         assert( attr != NULL );
196
197         if( op && op->o_is_auth_check &&
198                 ( access_level == ACL_SEARCH || access_level == ACL_READ ))
199         {
200                 access = ACL_AUTH;
201         }
202
203         if( state ) {
204                 if ( state->as_vd_ad==desc) {
205                         if ( state->as_recorded ) {
206                                 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
207                                         val == NULL )
208                                 {
209                                         return state->as_result;
210                                 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
211                                         val != NULL && state->as_vd_acl == NULL )
212                                 {
213                                         return state->as_result;
214                                 }
215                         }
216                         st_same_attr = 1;
217                 } else {
218                         *state = state_init;
219                 }
220
221                 state->as_vd_ad=desc;
222         }
223
224         Debug( LDAP_DEBUG_ACL,
225                 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
226             access2str( access ), e->e_dn, attr );
227
228         if ( op == NULL ) {
229                 /* no-op call */
230                 goto done;
231         }
232
233         be = op->o_bd;
234         if ( be == NULL ) {
235                 be = LDAP_STAILQ_FIRST(&backendDB);
236                 be_null = 1;
237 #ifdef LDAP_DEVEL
238                 /*
239                  * FIXME: experimental; use first backend rules
240                  * iff there is no global_acl (ITS#3100) */
241                 if ( frontendDB->be_acl == NULL ) 
242 #endif
243                 {
244                         op->o_bd = be;
245                 }
246         }
247         assert( be != NULL );
248
249 #ifdef LDAP_SLAPI
250         if ( op->o_pb != NULL ) {
251                 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
252                 if ( ret == 0 ) {
253                         /* ACL plugin denied access */
254                         goto done;
255                 }
256         }
257 #endif /* LDAP_SLAPI */
258
259         /* grant database root access */
260         if ( /* be != NULL && */ be_isroot( op ) ) {
261                 Debug( LDAP_DEBUG_ACL,
262                     "<= root access granted\n",
263                         0, 0, 0 );
264                 if ( maskp ) {
265                         mask = ACL_LVL_MANAGE;
266                 }
267
268                 goto done;
269         }
270
271         /*
272          * no-user-modification operational attributes are ignored
273          * by ACL_WRITE checking as any found here are not provided
274          * by the user
275          */
276         if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
277                 && desc != slap_schema.si_ad_entry
278                 && desc != slap_schema.si_ad_children )
279         {
280                 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
281                         " %s access granted\n",
282                         attr, 0, 0 );
283                 goto done;
284         }
285
286         /* use backend default access if no backend acls */
287         if ( /* be != NULL && */ be->be_acl == NULL ) {
288                 Debug( LDAP_DEBUG_ACL,
289                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
290                         access2str( access ),
291                         be->be_dfltaccess >= access_level ? "granted" : "denied",
292                         op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
293                 ret = be->be_dfltaccess >= access_level;
294
295                 if ( maskp ) {
296                         int     i;
297
298                         mask = ACL_PRIV_LEVEL;
299                         for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
300                                 mask |= ACL_ACCESS2PRIV( i );
301                         }
302                 }
303
304                 goto done;
305
306 #ifdef notdef
307         /* be is always non-NULL */
308         /* use global default access if no global acls */
309         } else if ( be == NULL && frontendDB->be_acl == NULL ) {
310                 Debug( LDAP_DEBUG_ACL,
311                         "=> access_allowed: global default %s access %s to \"%s\"\n",
312                         access2str( access ),
313                         frontendDB->be_dfltaccess >= access_level ? "granted" : "denied", op->o_dn.bv_val );
314                 ret = frontendDB->be_dfltaccess >= access_level;
315
316                 if ( maskp ) {
317                         int     i;
318
319                         mask = ACL_PRIV_LEVEL;
320                         for ( i = ACL_NONE; i <= global_default_access; i++ ) {
321                                 mask |= ACL_ACCESS2PRIV( i );
322                         }
323                 }
324
325                 goto done;
326 #endif
327         }
328
329         ret = 0;
330         control = ACL_BREAK;
331
332         if( st_same_attr ) {
333                 assert( state->as_vd_acl != NULL );
334
335                 a = state->as_vd_acl;
336                 count = state->as_vd_acl_count;
337                 if ( !ACL_IS_INVALID( state->as_vd_acl_mask )) {
338                         mask = state->as_vd_acl_mask;
339                         AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
340                         goto vd_access;
341                 }
342
343         } else {
344                 if ( state ) state->as_vi_acl = NULL;
345                 a = NULL;
346                 ACL_INIT(mask);
347                 count = 0;
348                 memset(matches, '\0', sizeof(matches));
349         }
350
351         while((a = acl_get( a, &count, op, e, desc, val,
352                 MAXREMATCHES, matches, state )) != NULL)
353         {
354                 int i;
355
356                 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
357                         Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
358                             (int)matches[i].rm_so, (int)matches[i].rm_eo );
359                         if( matches[i].rm_so <= matches[0].rm_eo ) {
360                                 int n;
361                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
362                                         Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
363                                 }
364                         }
365                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
366                 }
367
368                 if (state) {
369                         if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
370                                 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
371                                 ret = state->as_result;
372                                 goto done;
373                         } else {
374                                 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
375                         }
376                 }
377
378 vd_access:
379                 control = acl_mask( a, &mask, op,
380                         e, desc, val, MAXREMATCHES, matches, count, state );
381
382                 if ( control != ACL_BREAK ) {
383                         break;
384                 }
385
386                 memset(matches, '\0', sizeof(matches));
387         }
388
389         if ( ACL_IS_INVALID( mask ) ) {
390                 Debug( LDAP_DEBUG_ACL,
391                         "=> access_allowed: \"%s\" (%s) invalid!\n",
392                         e->e_dn, attr, 0 );
393                 ACL_INIT(mask);
394
395         } else if ( control == ACL_BREAK ) {
396                 Debug( LDAP_DEBUG_ACL,
397                         "=> access_allowed: no more rules\n", 0, 0, 0);
398
399                 goto done;
400         }
401
402         Debug( LDAP_DEBUG_ACL,
403                 "=> access_allowed: %s access %s by %s\n",
404                 access2str( access ),
405                 ACL_GRANT(mask, access) ? "granted" : "denied",
406                 accessmask2str( mask, accessmaskbuf, 1 ) );
407
408         ret = ACL_GRANT(mask, access);
409
410 done:
411         if( state != NULL ) {
412                 /* If not value-dependent, save ACL in case of more attrs */
413                 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
414                         state->as_vi_acl = a;
415                         state->as_result = ret;
416                 }
417                 state->as_recorded |= ACL_STATE_RECORDED;
418         }
419         if (be_null) op->o_bd = NULL;
420         if ( maskp ) *maskp = mask;
421         return ret;
422 }
423
424
425 /*
426  * acl_get - return the acl applicable to entry e, attribute
427  * attr.  the acl returned is suitable for use in subsequent calls to
428  * acl_access_allowed().
429  */
430
431 static AccessControl *
432 acl_get(
433         AccessControl *a,
434         int                     *count,
435         Operation       *op,
436         Entry           *e,
437         AttributeDescription *desc,
438         struct berval   *val,
439         int                     nmatch,
440         regmatch_t      *matches,
441         AccessControlState *state )
442 {
443         const char *attr;
444         int dnlen, patlen;
445         AccessControl *prev;
446
447         assert( e != NULL );
448         assert( count != NULL );
449         assert( desc != NULL );
450
451         attr = desc->ad_cname.bv_val;
452
453         assert( attr != NULL );
454
455         if( a == NULL ) {
456                 if( op->o_bd == NULL ) {
457                         a = frontendDB->be_acl;
458                 } else {
459                         a = op->o_bd->be_acl;
460                 }
461                 prev = NULL;
462
463                 assert( a != NULL );
464
465         } else {
466                 prev = a;
467                 a = a->acl_next;
468         }
469
470         dnlen = e->e_nname.bv_len;
471
472         for ( ; a != NULL; a = a->acl_next ) {
473                 (*count) ++;
474
475                 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
476                         if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
477                                 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n", 
478                                         *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
479                                 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
480                                         continue;
481
482                         } else {
483                                 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n", 
484                                         *count, a->acl_dn_pat.bv_val, 0 );
485                                 patlen = a->acl_dn_pat.bv_len;
486                                 if ( dnlen < patlen )
487                                         continue;
488
489                                 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
490                                         /* base dn -- entire object DN must match */
491                                         if ( dnlen != patlen )
492                                                 continue;
493
494                                 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
495                                         int     rdnlen = -1, sep = 0;
496
497                                         if ( dnlen <= patlen )
498                                                 continue;
499
500                                         if ( patlen > 0 ) {
501                                                 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
502                                                         continue;
503                                                 sep = 1;
504                                         }
505
506                                         rdnlen = dn_rdnlen( NULL, &e->e_nname );
507                                         if ( rdnlen != dnlen - patlen - sep )
508                                                 continue;
509
510                                 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
511                                         if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
512                                                 continue;
513
514                                 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
515                                         if ( dnlen <= patlen )
516                                                 continue;
517                                         if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
518                                                 continue;
519                                 }
520
521                                 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
522                                         continue;
523                         }
524
525                         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
526                                 *count, 0, 0 );
527                 }
528
529                 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
530                         matches[0].rm_so = matches[0].rm_eo = -1;
531                         continue;
532                 }
533
534                 /* Is this ACL only for a specific value? */
535                 if ( a->acl_attrval.bv_len ) {
536                         if ( val == NULL ) {
537                                 continue;
538                         }
539
540                         if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
541                                 state->as_recorded |= ACL_STATE_RECORDED_VD;
542                                 state->as_vd_acl = prev;
543                                 state->as_vd_acl_count = *count;
544                                 state->as_vd_access = a->acl_access;
545                                 state->as_vd_access_count = 1;
546                                 ACL_INVALIDATE( state->as_vd_acl_mask );
547                         }
548
549                         if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
550                                 Debug( LDAP_DEBUG_ACL,
551                                         "acl_get: valpat %s\n",
552                                         a->acl_attrval.bv_val, 0, 0 );
553                                 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
554                                 {
555                                         continue;
556                                 }
557
558                         } else {
559                                 int match = 0;
560                                 const char *text;
561                                 Debug( LDAP_DEBUG_ACL,
562                                         "acl_get: val %s\n",
563                                         a->acl_attrval.bv_val, 0, 0 );
564         
565                                 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
566                                         if (value_match( &match, desc,
567                                                 desc->ad_type->sat_equality, 0,
568                                                 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
569                                                         match )
570                                                 continue;
571                                         
572                                 } else {
573                                         int             patlen, vdnlen;
574         
575                                         patlen = a->acl_attrval.bv_len;
576                                         vdnlen = val->bv_len;
577         
578                                         if ( vdnlen < patlen )
579                                                 continue;
580         
581                                         if ( a->acl_dn_style == ACL_STYLE_BASE ) {
582                                                 if ( vdnlen > patlen )
583                                                         continue;
584         
585                                         } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
586                                                 int rdnlen = -1;
587         
588                                                 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
589                                                         continue;
590         
591                                                 rdnlen = dn_rdnlen( NULL, val );
592                                                 if ( rdnlen != vdnlen - patlen - 1 )
593                                                         continue;
594         
595                                         } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
596                                                 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
597                                                         continue;
598         
599                                         } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
600                                                 if ( vdnlen <= patlen )
601                                                         continue;
602         
603                                                 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
604                                                         continue;
605                                         }
606         
607                                         if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
608                                                 continue;
609                                 }
610                         }
611                 }
612
613                 if ( a->acl_filter != NULL ) {
614                         ber_int_t rc = test_filter( NULL, e, a->acl_filter );
615                         if ( rc != LDAP_COMPARE_TRUE ) {
616                                 continue;
617                         }
618                 }
619
620                 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
621                        *count, attr, 0);
622                 return a;
623         }
624
625         Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
626         return( NULL );
627 }
628
629 static int
630 acl_mask_dn(
631         Operation               *op,
632         Entry                   *e,
633         AccessControl           *a,
634         int                     nmatch,
635         regmatch_t              *matches,
636         slap_dn_access          *b,
637         struct berval           *opndn )
638 {
639         /*
640          * if access applies to the entry itself, and the
641          * user is bound as somebody in the same namespace as
642          * the entry, OR the given dn matches the dn pattern
643          */
644         /*
645          * NOTE: styles "anonymous", "users" and "self" 
646          * have been moved to enum slap_style_t, whose 
647          * value is set in a_dn_style; however, the string
648          * is maintaned in a_dn_pat.
649          */
650         if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
651                 if ( !BER_BVISEMPTY( opndn ) ) {
652                         return 1;
653                 }
654
655         } else if ( b->a_style == ACL_STYLE_USERS ) {
656                 if ( BER_BVISEMPTY( opndn ) ) {
657                         return 1;
658                 }
659
660         } else if ( b->a_style == ACL_STYLE_SELF ) {
661                 struct berval   ndn, selfndn;
662                 int             level;
663
664                 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
665                         return 1;
666                 }
667
668                 level = b->a_self_level;
669                 if ( level < 0 ) {
670                         selfndn = *opndn;
671                         ndn = e->e_nname;
672                         level = -level;
673
674                 } else {
675                         ndn = *opndn;
676                         selfndn = e->e_nname;
677                 }
678
679                 for ( ; level > 0; level-- ) {
680                         if ( BER_BVISEMPTY( &ndn ) ) {
681                                 break;
682                         }
683                         dnParent( &ndn, &ndn );
684                 }
685                         
686                 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
687                 {
688                         return 1;
689                 }
690
691         } else if ( b->a_style == ACL_STYLE_REGEX ) {
692                 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
693                         int             tmp_nmatch;
694                         regmatch_t      tmp_matches[2],
695                                         *tmp_matchesp = tmp_matches;
696
697                         int             rc = 0;
698
699                         switch ( a->acl_dn_style ) {
700                         case ACL_STYLE_REGEX:
701                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
702                                         tmp_matchesp = matches;
703                                         tmp_nmatch = nmatch;
704                                         break;
705                                 }
706                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
707
708                         case ACL_STYLE_BASE:
709                                 tmp_matches[0].rm_so = 0;
710                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
711                                 tmp_nmatch = 1;
712                                 break;
713
714                         case ACL_STYLE_ONE:
715                         case ACL_STYLE_SUBTREE:
716                         case ACL_STYLE_CHILDREN:
717                                 tmp_matches[0].rm_so = 0;
718                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
719                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
720                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
721                                 tmp_nmatch = 2;
722                                 break;
723
724                         default:
725                                 /* error */
726                                 rc = 1;
727                                 break;
728                         }
729
730                         if ( rc ) {
731                                 return 1;
732                         }
733
734                         if ( !regex_matches( &b->a_pat, opndn->bv_val,
735                                 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
736                         {
737                                 return 1;
738                         }
739                 }
740
741         } else {
742                 struct berval   pat;
743                 ber_len_t       patlen, odnlen;
744                 int             got_match = 0;
745
746                 if ( e->e_dn == NULL )
747                         return 1;
748
749                 if ( b->a_expand ) {
750                         struct berval   bv;
751                         char            buf[ACL_BUF_SIZE];
752                         
753                         int             tmp_nmatch;
754                         regmatch_t      tmp_matches[2],
755                                         *tmp_matchesp = tmp_matches;
756
757                         int             rc = 0;
758
759                         bv.bv_len = sizeof( buf ) - 1;
760                         bv.bv_val = buf;
761
762                         switch ( a->acl_dn_style ) {
763                         case ACL_STYLE_REGEX:
764                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
765                                         tmp_matchesp = matches;
766                                         tmp_nmatch = nmatch;
767                                         break;
768                                 }
769                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
770
771                         case ACL_STYLE_BASE:
772                                 tmp_matches[0].rm_so = 0;
773                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
774                                 tmp_nmatch = 1;
775                                 break;
776
777                         case ACL_STYLE_ONE:
778                         case ACL_STYLE_SUBTREE:
779                         case ACL_STYLE_CHILDREN:
780                                 tmp_matches[0].rm_so = 0;
781                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
782                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
783                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
784                                 tmp_nmatch = 2;
785                                 break;
786
787                         default:
788                                 /* error */
789                                 rc = 1;
790                                 break;
791                         }
792
793                         if ( rc ) {
794                                 return 1;
795                         }
796
797                         if ( string_expand( &bv, &b->a_pat, 
798                                         e->e_nname.bv_val,
799                                         tmp_nmatch, tmp_matchesp ) )
800                         {
801                                 return 1;
802                         }
803                         
804                         if ( dnNormalize(0, NULL, NULL, &bv,
805                                         &pat, op->o_tmpmemctx )
806                                         != LDAP_SUCCESS )
807                         {
808                                 /* did not expand to a valid dn */
809                                 return 1;
810                         }
811
812                 } else {
813                         pat = b->a_pat;
814                 }
815
816                 patlen = pat.bv_len;
817                 odnlen = opndn->bv_len;
818                 if ( odnlen < patlen ) {
819                         goto dn_match_cleanup;
820
821                 }
822
823                 if ( b->a_style == ACL_STYLE_BASE ) {
824                         /* base dn -- entire object DN must match */
825                         if ( odnlen != patlen ) {
826                                 goto dn_match_cleanup;
827                         }
828
829                 } else if ( b->a_style == ACL_STYLE_ONE ) {
830                         int rdnlen = -1;
831
832                         if ( odnlen <= patlen ) {
833                                 goto dn_match_cleanup;
834                         }
835
836                         if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
837                                 goto dn_match_cleanup;
838                         }
839
840                         rdnlen = dn_rdnlen( NULL, opndn );
841                         if ( rdnlen != odnlen - patlen - 1 ) {
842                                 goto dn_match_cleanup;
843                         }
844
845                 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
846                         if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
847                                 goto dn_match_cleanup;
848                         }
849
850                 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
851                         if ( odnlen <= patlen ) {
852                                 goto dn_match_cleanup;
853                         }
854
855                         if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
856                                 goto dn_match_cleanup;
857                         }
858
859                 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
860                         int level;
861                         struct berval ndn;
862
863                         if ( odnlen <= patlen ) {
864                                 goto dn_match_cleanup;
865                         }
866
867                         if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
868                         {
869                                 goto dn_match_cleanup;
870                         }
871                         
872                         level = b->a_level;
873                         ndn = *opndn;
874                         for ( ; level > 0; level-- ) {
875                                 if ( BER_BVISEMPTY( &ndn ) ) {
876                                         goto dn_match_cleanup;
877                                 }
878                                 dnParent( &ndn, &ndn );
879                                 if ( ndn.bv_len < patlen ) {
880                                         goto dn_match_cleanup;
881                                 }
882                         }
883                         
884                         if ( ndn.bv_len != patlen ) {
885                                 goto dn_match_cleanup;
886                         }
887                 }
888
889                 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
890
891 dn_match_cleanup:;
892                 if ( pat.bv_val != b->a_pat.bv_val ) {
893                         slap_sl_free( pat.bv_val, op->o_tmpmemctx );
894                 }
895
896                 if ( !got_match ) {
897                         return 1;
898                 }
899         }
900
901         return 0;
902 }
903
904 /*
905  * Record value-dependent access control state
906  */
907 #define ACL_RECORD_VALUE_STATE do { \
908                 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
909                         state->as_recorded |= ACL_STATE_RECORDED_VD; \
910                         state->as_vd_acl = a; \
911                         AC_MEMCPY( state->as_vd_acl_matches, matches, \
912                                 sizeof( state->as_vd_acl_matches )) ; \
913                         state->as_vd_acl_count = count; \
914                         state->as_vd_access = b; \
915                         state->as_vd_access_count = i; \
916                 } \
917         } while( 0 )
918
919 static int
920 acl_mask_dnattr(
921         Operation               *op,
922         Entry                   *e,
923         struct berval           *val,
924         AccessControl           *a,
925         Access                  *b,
926         int                     i,
927         regmatch_t              *matches,
928         int                     count,
929         AccessControlState      *state,
930         slap_dn_access          *bdn,
931         struct berval           *opndn )
932 {
933         Attribute       *at;
934         struct berval   bv;
935         int             rc, match = 0;
936         const char      *text;
937         const char      *attr = bdn->a_at->ad_cname.bv_val;
938
939         assert( attr != NULL );
940
941         if ( BER_BVISEMPTY( opndn ) ) {
942                 return 1;
943         }
944
945         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
946         bv = *opndn;
947
948         /* see if asker is listed in dnattr */
949         for ( at = attrs_find( e->e_attrs, bdn->a_at );
950                 at != NULL;
951                 at = attrs_find( at->a_next, bdn->a_at ) )
952         {
953                 if ( value_find_ex( bdn->a_at,
954                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
955                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
956                         at->a_nvals,
957                         &bv, op->o_tmpmemctx ) == 0 )
958                 {
959                         /* found it */
960                         match = 1;
961                         break;
962                 }
963         }
964
965         if ( match ) {
966                 /* have a dnattr match. if this is a self clause then
967                  * the target must also match the op dn.
968                  */
969                 if ( bdn->a_self ) {
970                         /* check if the target is an attribute. */
971                         if ( val == NULL ) return 1;
972
973                         /* target is attribute, check if the attribute value
974                          * is the op dn.
975                          */
976                         rc = value_match( &match, bdn->a_at,
977                                 bdn->a_at->ad_type->sat_equality, 0,
978                                 val, &bv, &text );
979                         /* on match error or no match, fail the ACL clause */
980                         if ( rc != LDAP_SUCCESS || match != 0 )
981                                 return 1;
982                 }
983
984         } else {
985                 /* no dnattr match, check if this is a self clause */
986                 if ( ! bdn->a_self )
987                         return 1;
988
989                 ACL_RECORD_VALUE_STATE;
990                 
991                 /* this is a self clause, check if the target is an
992                  * attribute.
993                  */
994                 if ( val == NULL )
995                         return 1;
996
997                 /* target is attribute, check if the attribute value
998                  * is the op dn.
999                  */
1000                 rc = value_match( &match, bdn->a_at,
1001                         bdn->a_at->ad_type->sat_equality, 0,
1002                         val, &bv, &text );
1003
1004                 /* on match error or no match, fail the ACL clause */
1005                 if ( rc != LDAP_SUCCESS || match != 0 )
1006                         return 1;
1007         }
1008
1009         return 0;
1010 }
1011
1012
1013 /*
1014  * acl_mask - modifies mask based upon the given acl and the
1015  * requested access to entry e, attribute attr, value val.  if val
1016  * is null, access to the whole attribute is assumed (all values).
1017  *
1018  * returns      0       access NOT allowed
1019  *              1       access allowed
1020  */
1021
1022 static slap_control_t
1023 acl_mask(
1024         AccessControl           *a,
1025         slap_mask_t             *mask,
1026         Operation               *op,
1027         Entry                   *e,
1028         AttributeDescription    *desc,
1029         struct berval           *val,
1030         int                     nmatch,
1031         regmatch_t              *matches,
1032         int                     count,
1033         AccessControlState      *state )
1034 {
1035         int             i;
1036         Access  *b;
1037 #ifdef LDAP_DEBUG
1038         char accessmaskbuf[ACCESSMASK_MAXLEN];
1039 #if !defined( SLAP_DYNACL ) && defined( SLAPD_ACI_ENABLED )
1040         char accessmaskbuf1[ACCESSMASK_MAXLEN];
1041 #endif /* !SLAP_DYNACL && SLAPD_ACI_ENABLED */
1042 #endif /* DEBUG */
1043         const char *attr;
1044
1045         assert( a != NULL );
1046         assert( mask != NULL );
1047         assert( desc != NULL );
1048
1049         attr = desc->ad_cname.bv_val;
1050
1051         assert( attr != NULL );
1052
1053         Debug( LDAP_DEBUG_ACL,
1054                 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1055                 e->e_dn, attr, 0 );
1056
1057         Debug( LDAP_DEBUG_ACL,
1058                 "=> acl_mask: to %s by \"%s\", (%s) \n",
1059                 val ? "value" : "all values",
1060                 op->o_ndn.bv_val ?  op->o_ndn.bv_val : "",
1061                 accessmask2str( *mask, accessmaskbuf, 1) );
1062
1063
1064         if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1065                 && state->as_vd_acl == a )
1066         {
1067                 b = state->as_vd_access;
1068                 i = state->as_vd_access_count;
1069
1070         } else {
1071                 b = a->acl_access;
1072                 i = 1;
1073         }
1074
1075         for ( ; b != NULL; b = b->a_next, i++ ) {
1076                 slap_mask_t oldmask, modmask;
1077
1078                 ACL_INVALIDATE( modmask );
1079
1080                 /* AND <who> clauses */
1081                 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1082                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1083                                 b->a_dn_pat.bv_val, 0, 0);
1084                         /*
1085                          * if access applies to the entry itself, and the
1086                          * user is bound as somebody in the same namespace as
1087                          * the entry, OR the given dn matches the dn pattern
1088                          */
1089                         /*
1090                          * NOTE: styles "anonymous", "users" and "self" 
1091                          * have been moved to enum slap_style_t, whose 
1092                          * value is set in a_dn_style; however, the string
1093                          * is maintaned in a_dn_pat.
1094                          */
1095
1096                         if ( acl_mask_dn( op, e, a, nmatch, matches,
1097                                 &b->a_dn, &op->o_ndn ) )
1098                         {
1099                                 continue;
1100                         }
1101                 }
1102
1103                 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1104                         struct berval   ndn;
1105
1106                         Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1107                                 b->a_realdn_pat.bv_val, 0, 0);
1108                         /*
1109                          * if access applies to the entry itself, and the
1110                          * user is bound as somebody in the same namespace as
1111                          * the entry, OR the given dn matches the dn pattern
1112                          */
1113                         /*
1114                          * NOTE: styles "anonymous", "users" and "self" 
1115                          * have been moved to enum slap_style_t, whose 
1116                          * value is set in a_dn_style; however, the string
1117                          * is maintaned in a_dn_pat.
1118                          */
1119
1120                         if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1121                         {
1122                                 ndn = op->o_conn->c_ndn;
1123                         } else {
1124                                 ndn = op->o_ndn;
1125                         }
1126
1127                         if ( acl_mask_dn( op, e, a, nmatch, matches,
1128                                 &b->a_realdn, &ndn ) )
1129                         {
1130                                 continue;
1131                         }
1132                 }
1133
1134                 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1135                         if ( ! op->o_conn->c_listener ) {
1136                                 continue;
1137                         }
1138                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1139                                 b->a_sockurl_pat.bv_val, 0, 0 );
1140
1141                         if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1142                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1143                                         if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1144                                                         e->e_ndn, nmatch, matches ) ) 
1145                                         {
1146                                                 continue;
1147                                         }
1148
1149                                 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1150                                         struct berval   bv;
1151                                         char buf[ACL_BUF_SIZE];
1152
1153                                         bv.bv_len = sizeof( buf ) - 1;
1154                                         bv.bv_val = buf;
1155                                         if ( string_expand( &bv, &b->a_sockurl_pat,
1156                                                         e->e_ndn, nmatch, matches ) )
1157                                         {
1158                                                 continue;
1159                                         }
1160
1161                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1162                                         {
1163                                                 continue;
1164                                         }
1165
1166                                 } else {
1167                                         if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1168                                         {
1169                                                 continue;
1170                                         }
1171                                 }
1172                         }
1173                 }
1174
1175                 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1176                         if ( !op->o_conn->c_peer_domain.bv_val ) {
1177                                 continue;
1178                         }
1179                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1180                                 b->a_domain_pat.bv_val, 0, 0 );
1181                         if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1182                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1183                                         if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1184                                                         e->e_ndn, nmatch, matches ) ) 
1185                                         {
1186                                                 continue;
1187                                         }
1188                                 } else {
1189                                         char buf[ACL_BUF_SIZE];
1190
1191                                         struct berval   cmp = op->o_conn->c_peer_domain;
1192                                         struct berval   pat = b->a_domain_pat;
1193
1194                                         if ( b->a_domain_expand ) {
1195                                                 struct berval bv;
1196
1197                                                 bv.bv_len = sizeof(buf) - 1;
1198                                                 bv.bv_val = buf;
1199
1200                                                 if ( string_expand(&bv, &b->a_domain_pat,
1201                                                                 e->e_ndn, nmatch, matches) )
1202                                                 {
1203                                                         continue;
1204                                                 }
1205                                                 pat = bv;
1206                                         }
1207
1208                                         if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1209                                                 int offset = cmp.bv_len - pat.bv_len;
1210                                                 if ( offset < 0 ) {
1211                                                         continue;
1212                                                 }
1213
1214                                                 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1215                                                         continue;
1216                                                 }
1217
1218                                                 /* trim the domain */
1219                                                 cmp.bv_val = &cmp.bv_val[ offset ];
1220                                                 cmp.bv_len -= offset;
1221                                         }
1222                                         
1223                                         if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1224                                                 continue;
1225                                         }
1226                                 }
1227                         }
1228                 }
1229
1230                 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1231                         if ( !op->o_conn->c_peer_name.bv_val ) {
1232                                 continue;
1233                         }
1234                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1235                                 b->a_peername_pat.bv_val, 0, 0 );
1236                         if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1237                                 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1238                                         if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1239                                                         e->e_ndn, nmatch, matches ) ) 
1240                                         {
1241                                                 continue;
1242                                         }
1243
1244                                 } else {
1245                                         /* try exact match */
1246                                         if ( b->a_peername_style == ACL_STYLE_BASE ) {
1247                                                 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1248                                                         continue;
1249                                                 }
1250
1251                                         } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1252                                                 struct berval   bv;
1253                                                 char buf[ACL_BUF_SIZE];
1254
1255                                                 bv.bv_len = sizeof( buf ) - 1;
1256                                                 bv.bv_val = buf;
1257                                                 if ( string_expand( &bv, &b->a_peername_pat,
1258                                                                 e->e_ndn, nmatch, matches ) )
1259                                                 {
1260                                                         continue;
1261                                                 }
1262
1263                                                 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1264                                                         continue;
1265                                                 }
1266
1267                                         /* extract IP and try exact match */
1268                                         } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1269                                                 char            *port;
1270                                                 char            buf[] = "255.255.255.255";
1271                                                 struct berval   ip;
1272                                                 unsigned long   addr;
1273                                                 int             port_number = -1;
1274                                                 
1275                                                 if ( strncasecmp( op->o_conn->c_peer_name.bv_val, 
1276                                                                         aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 ) 
1277                                                         continue;
1278
1279                                                 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1280                                                 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1281
1282                                                 port = strrchr( ip.bv_val, ':' );
1283                                                 if ( port ) {
1284                                                         char    *next;
1285                                                         
1286                                                         ip.bv_len = port - ip.bv_val;
1287                                                         ++port;
1288                                                         port_number = strtol( port, &next, 10 );
1289                                                         if ( next[0] != '\0' )
1290                                                                 continue;
1291                                                 }
1292                                                 
1293                                                 /* the port check can be anticipated here */
1294                                                 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1295                                                         continue;
1296                                                 
1297                                                 /* address longer than expected? */
1298                                                 if ( ip.bv_len >= sizeof(buf) )
1299                                                         continue;
1300
1301                                                 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1302                                                 buf[ ip.bv_len ] = '\0';
1303
1304                                                 addr = inet_addr( buf );
1305
1306                                                 /* unable to convert? */
1307                                                 if ( addr == (unsigned long)(-1) )
1308                                                         continue;
1309
1310                                                 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1311                                                         continue;
1312
1313 #ifdef LDAP_PF_LOCAL
1314                                         /* extract path and try exact match */
1315                                         } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1316                                                 struct berval path;
1317                                                 
1318                                                 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1319                                                                         aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1320                                                         continue;
1321
1322                                                 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1323                                                 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1324
1325                                                 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1326                                                         continue;
1327
1328 #endif /* LDAP_PF_LOCAL */
1329
1330                                         /* exact match (very unlikely...) */
1331                                         } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1332                                                         continue;
1333                                         }
1334                                 }
1335                         }
1336                 }
1337
1338                 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1339                         if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1340                                 continue;
1341                         }
1342                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1343                                 b->a_sockname_pat.bv_val, 0, 0 );
1344                         if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1345                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1346                                         if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1347                                                         e->e_ndn, nmatch, matches ) ) 
1348                                         {
1349                                                 continue;
1350                                         }
1351
1352                                 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1353                                         struct berval   bv;
1354                                         char buf[ACL_BUF_SIZE];
1355
1356                                         bv.bv_len = sizeof( buf ) - 1;
1357                                         bv.bv_val = buf;
1358                                         if ( string_expand( &bv, &b->a_sockname_pat,
1359                                                         e->e_ndn, nmatch, matches ) )
1360                                         {
1361                                                 continue;
1362                                         }
1363
1364                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1365                                                 continue;
1366                                         }
1367
1368                                 } else {
1369                                         if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1370                                                 continue;
1371                                         }
1372                                 }
1373                         }
1374                 }
1375
1376                 if ( b->a_dn_at != NULL ) {
1377                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1378                                         matches, count, state,
1379                                         &b->a_dn, &op->o_ndn ) )
1380                         {
1381                                 continue;
1382                         }
1383                 }
1384
1385                 if ( b->a_realdn_at != NULL ) {
1386                         struct berval   ndn;
1387
1388                         if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1389                         {
1390                                 ndn = op->o_conn->c_ndn;
1391                         } else {
1392                                 ndn = op->o_ndn;
1393                         }
1394
1395                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1396                                         matches, count, state,
1397                                         &b->a_realdn, &ndn ) )
1398                         {
1399                                 continue;
1400                         }
1401                 }
1402
1403                 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1404                         struct berval bv;
1405                         struct berval ndn = BER_BVNULL;
1406                         int rc;
1407
1408                         if ( op->o_ndn.bv_len == 0 ) {
1409                                 continue;
1410                         }
1411
1412                         /* b->a_group is an unexpanded entry name, expanded it should be an 
1413                          * entry with objectclass group* and we test to see if odn is one of
1414                          * the values in the attribute group
1415                          */
1416                         /* see if asker is listed in dnattr */
1417                         if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1418                                 char            buf[ACL_BUF_SIZE];
1419                                 int             tmp_nmatch;
1420                                 regmatch_t      tmp_matches[2],
1421                                                 *tmp_matchesp = tmp_matches;
1422
1423                                 bv.bv_len = sizeof(buf) - 1;
1424                                 bv.bv_val = buf;
1425
1426                                 rc = 0;
1427
1428                                 switch ( a->acl_dn_style ) {
1429                                 case ACL_STYLE_REGEX:
1430                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1431                                                 tmp_matchesp = matches;
1432                                                 tmp_nmatch = nmatch;
1433                                                 break;
1434                                         }
1435
1436                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1437                                 case ACL_STYLE_BASE:
1438                                         tmp_matches[0].rm_so = 0;
1439                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1440                                         tmp_nmatch = 1;
1441                                         break;
1442
1443                                 case ACL_STYLE_ONE:
1444                                 case ACL_STYLE_SUBTREE:
1445                                 case ACL_STYLE_CHILDREN:
1446                                         tmp_matches[0].rm_so = 0;
1447                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1448                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1449                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1450                                         tmp_nmatch = 2;
1451                                         break;
1452
1453                                 default:
1454                                         /* error */
1455                                         rc = 1;
1456                                         break;
1457                                 }
1458
1459                                 if ( rc ) {
1460                                         continue;
1461                                 }
1462                                 
1463                                 if ( string_expand( &bv, &b->a_group_pat,
1464                                                 e->e_nname.bv_val,
1465                                                 tmp_nmatch, tmp_matchesp ) )
1466                                 {
1467                                         continue;
1468                                 }
1469
1470                                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1471                                                 op->o_tmpmemctx ) != LDAP_SUCCESS )
1472                                 {
1473                                         /* did not expand to a valid dn */
1474                                         continue;
1475                                 }
1476
1477                                 bv = ndn;
1478
1479                         } else {
1480                                 bv = b->a_group_pat;
1481                         }
1482
1483                         rc = backend_group( op, e, &bv, &op->o_ndn,
1484                                 b->a_group_oc, b->a_group_at );
1485
1486                         if ( ndn.bv_val ) {
1487                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1488                         }
1489
1490                         if ( rc != 0 ) {
1491                                 continue;
1492                         }
1493                 }
1494
1495                 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1496                         struct berval   bv;
1497                         char            buf[ACL_BUF_SIZE];
1498
1499                         if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1500                                 int             tmp_nmatch;
1501                                 regmatch_t      tmp_matches[2],
1502                                                 *tmp_matchesp = tmp_matches;
1503                                 int             rc = 0;
1504
1505                                 bv.bv_len = sizeof( buf ) - 1;
1506                                 bv.bv_val = buf;
1507
1508                                 rc = 0;
1509
1510                                 switch ( a->acl_dn_style ) {
1511                                 case ACL_STYLE_REGEX:
1512                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1513                                                 tmp_matchesp = matches;
1514                                                 tmp_nmatch = nmatch;
1515                                                 break;
1516                                         }
1517
1518                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1519                                 case ACL_STYLE_BASE:
1520                                         tmp_matches[0].rm_so = 0;
1521                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1522                                         tmp_nmatch = 1;
1523                                         break;
1524
1525                                 case ACL_STYLE_ONE:
1526                                 case ACL_STYLE_SUBTREE:
1527                                 case ACL_STYLE_CHILDREN:
1528                                         tmp_matches[0].rm_so = 0;
1529                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1530                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1531                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1532                                         tmp_nmatch = 2;
1533                                         break;
1534
1535                                 default:
1536                                         /* error */
1537                                         rc = 1;
1538                                         break;
1539                                 }
1540
1541                                 if ( rc ) {
1542                                         continue;
1543                                 }
1544                                 
1545                                 if ( string_expand( &bv, &b->a_set_pat,
1546                                                 e->e_nname.bv_val,
1547                                                 tmp_nmatch, tmp_matchesp ) )
1548                                 {
1549                                         continue;
1550                                 }
1551
1552                         } else {
1553                                 bv = b->a_set_pat;
1554                         }
1555                         
1556                         if ( aci_match_set( &bv, op, e, 0 ) == 0 ) {
1557                                 continue;
1558                         }
1559                 }
1560
1561                 if ( b->a_authz.sai_ssf ) {
1562                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1563                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
1564                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
1565                                 continue;
1566                         }
1567                 }
1568
1569                 if ( b->a_authz.sai_transport_ssf ) {
1570                         Debug( LDAP_DEBUG_ACL,
1571                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1572                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1573                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
1574                                 continue;
1575                         }
1576                 }
1577
1578                 if ( b->a_authz.sai_tls_ssf ) {
1579                         Debug( LDAP_DEBUG_ACL,
1580                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1581                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1582                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
1583                                 continue;
1584                         }
1585                 }
1586
1587                 if ( b->a_authz.sai_sasl_ssf ) {
1588                         Debug( LDAP_DEBUG_ACL,
1589                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1590                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1591                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
1592                                 continue;
1593                         }
1594                 }
1595
1596 #ifdef SLAP_DYNACL
1597                 if ( b->a_dynacl ) {
1598                         slap_dynacl_t   *da;
1599                         slap_access_t   tgrant, tdeny;
1600
1601                         /* this case works different from the others above.
1602                          * since aci's themselves give permissions, we need
1603                          * to first check b->a_access_mask, the ACL's access level.
1604                          */
1605                         if ( BER_BVISEMPTY( &e->e_nname ) ) {
1606                                 /* no ACIs in the root DSE */
1607                                 continue;
1608                         }
1609
1610                         /* first check if the right being requested
1611                          * is allowed by the ACL clause.
1612                          */
1613                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1614                                 continue;
1615                         }
1616
1617                         /* start out with nothing granted, nothing denied */
1618                         ACL_INIT(tgrant);
1619                         ACL_INIT(tdeny);
1620
1621                         for ( da = b->a_dynacl; da; da = da->da_next ) {
1622                                 slap_access_t   grant, deny;
1623
1624                                 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1625
1626                                 tgrant |= grant;
1627                                 tdeny |= deny;
1628                         }
1629
1630                         /* remove anything that the ACL clause does not allow */
1631                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1632                         tdeny &= ACL_PRIV_MASK;
1633
1634                         /* see if we have anything to contribute */
1635                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
1636                                 continue;
1637                         }
1638
1639                         /* this could be improved by changing acl_mask so that it can deal with
1640                          * by clauses that return grant/deny pairs.  Right now, it does either
1641                          * additive or subtractive rights, but not both at the same time.  So,
1642                          * we need to combine the grant/deny pair into a single rights mask in
1643                          * a smart way:  if either grant or deny is "empty", then we use the
1644                          * opposite as is, otherwise we remove any denied rights from the grant
1645                          * rights mask and construct an additive mask.
1646                          */
1647                         if (ACL_IS_INVALID(tdeny)) {
1648                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
1649
1650                         } else if (ACL_IS_INVALID(tgrant)) {
1651                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1652
1653                         } else {
1654                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1655                         }
1656
1657                 } else
1658 #else /* !SLAP_DYNACL */
1659
1660 #ifdef SLAPD_ACI_ENABLED
1661                 if ( b->a_aci_at != NULL ) {
1662                         Attribute       *at;
1663                         slap_access_t   grant, deny, tgrant, tdeny;
1664                         struct berval   parent_ndn;
1665                         BerVarray       bvals = NULL;
1666                         int             ret, stop;
1667
1668                         /* this case works different from the others above.
1669                          * since aci's themselves give permissions, we need
1670                          * to first check b->a_access_mask, the ACL's access level.
1671                          */
1672
1673                         if ( BER_BVISEMPTY( &e->e_nname ) ) {
1674                                 /* no ACIs in the root DSE */
1675                                 continue;
1676                         }
1677
1678                         /* first check if the right being requested
1679                          * is allowed by the ACL clause.
1680                          */
1681                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1682                                 continue;
1683                         }
1684                         /* start out with nothing granted, nothing denied */
1685                         ACL_INIT(tgrant);
1686                         ACL_INIT(tdeny);
1687
1688                         /* get the aci attribute */
1689                         at = attr_find( e->e_attrs, b->a_aci_at );
1690                         if ( at != NULL ) {
1691 #if 0
1692                                 /* FIXME: this breaks acl caching;
1693                                  * see also ACL_RECORD_VALUE_STATE below */
1694                                 ACL_RECORD_VALUE_STATE;
1695 #endif
1696                                 /* the aci is an multi-valued attribute.  The
1697                                 * rights are determined by OR'ing the individual
1698                                 * rights given by the acis.
1699                                 */
1700                                 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
1701                                         if (aci_mask( op,
1702                                                 e, desc, val,
1703                                                 &at->a_nvals[i],
1704                                                 nmatch, matches,
1705                                                 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0)
1706                                         {
1707                                                 tgrant |= grant;
1708                                                 tdeny |= deny;
1709                                         }
1710                                 }
1711                                 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1712                                           accessmask2str(tgrant, accessmaskbuf, 1), 
1713                                           accessmask2str(tdeny, accessmaskbuf1, 1), 0);
1714
1715                         }
1716                         /* If the entry level aci didn't contain anything valid for the 
1717                          * current operation, climb up the tree and evaluate the
1718                          * acis with scope set to subtree
1719                          */
1720                         if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
1721                                 dnParent( &e->e_nname, &parent_ndn );
1722                                 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
1723                                         Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1724                                         ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
1725                                         switch(ret){
1726                                         case LDAP_SUCCESS :
1727                                                 stop = 0;
1728                                                 if (!bvals){
1729                                                         break;
1730                                                 }
1731
1732                                                 for( i = 0; bvals[i].bv_val != NULL; i++){
1733 #if 0
1734                                                         /* FIXME: this breaks acl caching;
1735                                                          * see also ACL_RECORD_VALUE_STATE above */
1736                                                         ACL_RECORD_VALUE_STATE;
1737 #endif
1738                                                         if (aci_mask(op, e, desc, val, &bvals[i],
1739                                                                         nmatch, matches,
1740                                                                         &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
1741                                                         {
1742                                                                 tgrant |= grant;
1743                                                                 tdeny |= deny;
1744                                                                 /* evaluation stops as soon as either a "deny" or a 
1745                                                                  * "grant" directive matches.
1746                                                                  */
1747                                                                 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1748                                                                         stop = 1;
1749                                                                 }
1750                                                         }
1751                                                         Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n", 
1752                                                                 accessmask2str(tgrant, accessmaskbuf, 1),
1753                                                                 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
1754                                                 }
1755                                                 break;
1756
1757                                         case LDAP_NO_SUCH_ATTRIBUTE:
1758                                                 /* just go on if the aci-Attribute is not present in
1759                                                  * the current entry 
1760                                                  */
1761                                                 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1762                                                 stop = 0;
1763                                                 break;
1764
1765                                         case LDAP_NO_SUCH_OBJECT:
1766                                                 /* We have reached the base object */
1767                                                 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1768                                                 stop = 1;
1769                                                 break;
1770
1771                                         default:
1772                                                 stop = 1;
1773                                                 break;
1774                                         }
1775                                         if (stop){
1776                                                 break;
1777                                         }
1778                                         dnParent( &parent_ndn, &parent_ndn );
1779                                 }
1780                         }
1781
1782
1783                         /* remove anything that the ACL clause does not allow */
1784                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1785                         tdeny &= ACL_PRIV_MASK;
1786
1787                         /* see if we have anything to contribute */
1788                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
1789                                 continue;
1790                         }
1791
1792                         /* this could be improved by changing acl_mask so that it can deal with
1793                          * by clauses that return grant/deny pairs.  Right now, it does either
1794                          * additive or subtractive rights, but not both at the same time.  So,
1795                          * we need to combine the grant/deny pair into a single rights mask in
1796                          * a smart way:  if either grant or deny is "empty", then we use the
1797                          * opposite as is, otherwise we remove any denied rights from the grant
1798                          * rights mask and construct an additive mask.
1799                          */
1800                         if (ACL_IS_INVALID(tdeny)) {
1801                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
1802
1803                         } else if (ACL_IS_INVALID(tgrant)) {
1804                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1805
1806                         } else {
1807                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1808                         }
1809
1810                 } else
1811 #endif /* SLAPD_ACI_ENABLED */
1812 #endif /* !SLAP_DYNACL */
1813                 {
1814                         modmask = b->a_access_mask;
1815                 }
1816
1817                 Debug( LDAP_DEBUG_ACL,
1818                         "<= acl_mask: [%d] applying %s (%s)\n",
1819                         i, accessmask2str( modmask, accessmaskbuf, 1 ), 
1820                         b->a_type == ACL_CONTINUE
1821                                 ? "continue"
1822                                 : b->a_type == ACL_BREAK
1823                                         ? "break"
1824                                         : "stop" );
1825                 /* save old mask */
1826                 oldmask = *mask;
1827
1828                 if( ACL_IS_ADDITIVE(modmask) ) {
1829                         /* add privs */
1830                         ACL_PRIV_SET( *mask, modmask );
1831
1832                         /* cleanup */
1833                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1834
1835                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1836                         /* substract privs */
1837                         ACL_PRIV_CLR( *mask, modmask );
1838
1839                         /* cleanup */
1840                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1841
1842                 } else {
1843                         /* assign privs */
1844                         *mask = modmask;
1845                 }
1846
1847                 Debug( LDAP_DEBUG_ACL,
1848                         "<= acl_mask: [%d] mask: %s\n",
1849                         i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1850
1851                 if( b->a_type == ACL_CONTINUE ) {
1852                         continue;
1853
1854                 } else if ( b->a_type == ACL_BREAK ) {
1855                         return ACL_BREAK;
1856
1857                 } else {
1858                         return ACL_STOP;
1859                 }
1860         }
1861
1862         /* implicit "by * none" clause */
1863         ACL_INIT(*mask);
1864
1865         Debug( LDAP_DEBUG_ACL,
1866                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1867                 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1868         return ACL_STOP;
1869 }
1870
1871 /*
1872  * acl_check_modlist - check access control on the given entry to see if
1873  * it allows the given modifications by the user associated with op.
1874  * returns      1       if mods allowed ok
1875  *              0       mods not allowed
1876  */
1877
1878 int
1879 acl_check_modlist(
1880         Operation       *op,
1881         Entry   *e,
1882         Modifications   *mlist
1883 )
1884 {
1885         struct berval *bv;
1886         AccessControlState state = ACL_STATE_INIT;
1887         Backend *be;
1888         int be_null = 0;
1889         int ret = 1; /* default is access allowed */
1890
1891         be = op->o_bd;
1892         if ( be == NULL ) {
1893                 be = LDAP_STAILQ_FIRST(&backendDB);
1894                 be_null = 1;
1895                 op->o_bd = be;
1896         }
1897         assert( be != NULL );
1898
1899         /* short circuit root database access */
1900         if ( be_isroot( op ) ) {
1901                 Debug( LDAP_DEBUG_ACL,
1902                         "<= acl_access_allowed: granted to database root\n",
1903                     0, 0, 0 );
1904                 goto done;
1905         }
1906
1907         /* use backend default access if no backend acls */
1908         if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1909                 Debug( LDAP_DEBUG_ACL,
1910                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
1911                         access2str( ACL_WRITE ),
1912                         op->o_bd->be_dfltaccess >= ACL_WRITE
1913                                 ? "granted" : "denied",
1914                         op->o_dn.bv_val );
1915                 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1916                 goto done;
1917         }
1918
1919         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1920                 /*
1921                  * no-user-modification operational attributes are ignored
1922                  * by ACL_WRITE checking as any found here are not provided
1923                  * by the user
1924                  */
1925                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1926                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1927                                 " modify access granted\n",
1928                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1929                         continue;
1930                 }
1931
1932                 switch ( mlist->sml_op ) {
1933                 case LDAP_MOD_REPLACE:
1934                         /*
1935                          * We must check both permission to delete the whole
1936                          * attribute and permission to add the specific attributes.
1937                          * This prevents abuse from selfwriters.
1938                          */
1939                         if ( ! access_allowed( op, e,
1940                                 mlist->sml_desc, NULL, ACL_WDEL, &state ) )
1941                         {
1942                                 ret = 0;
1943                                 goto done;
1944                         }
1945
1946                         if ( mlist->sml_values == NULL ) break;
1947
1948                         /* fall thru to check value to add */
1949
1950                 case LDAP_MOD_ADD:
1951                         assert( mlist->sml_values != NULL );
1952
1953                         for ( bv = mlist->sml_nvalues
1954                                         ? mlist->sml_nvalues : mlist->sml_values;
1955                                 bv->bv_val != NULL; bv++ )
1956                         {
1957                                 if ( ! access_allowed( op, e,
1958                                         mlist->sml_desc, bv, ACL_WADD, &state ) )
1959                                 {
1960                                         ret = 0;
1961                                         goto done;
1962                                 }
1963                         }
1964                         break;
1965
1966                 case LDAP_MOD_DELETE:
1967                         if ( mlist->sml_values == NULL ) {
1968                                 if ( ! access_allowed( op, e,
1969                                         mlist->sml_desc, NULL, ACL_WDEL, NULL ) )
1970                                 {
1971                                         ret = 0;
1972                                         goto done;
1973                                 }
1974                                 break;
1975                         }
1976                         for ( bv = mlist->sml_nvalues
1977                                         ? mlist->sml_nvalues : mlist->sml_values;
1978                                 bv->bv_val != NULL; bv++ )
1979                         {
1980                                 if ( ! access_allowed( op, e,
1981                                         mlist->sml_desc, bv, ACL_WDEL, &state ) )
1982                                 {
1983                                         ret = 0;
1984                                         goto done;
1985                                 }
1986                         }
1987                         break;
1988
1989                 case SLAP_MOD_SOFTADD:
1990                         /* allow adding attribute via modrdn thru */
1991                         break;
1992
1993                 default:
1994                         assert( 0 );
1995                         /* not reached */
1996                         ret = 0;
1997                         break;
1998                 }
1999         }
2000
2001 done:
2002         if (be_null) op->o_bd = NULL;
2003         return( ret );
2004 }
2005
2006 static int
2007 aci_get_part(
2008         struct berval   *list,
2009         int             ix,
2010         char            sep,
2011         struct berval   *bv )
2012 {
2013         int     len;
2014         char    *p;
2015
2016         if ( bv ) {
2017                 BER_BVZERO( bv );
2018         }
2019         len = list->bv_len;
2020         p = list->bv_val;
2021         while ( len >= 0 && --ix >= 0 ) {
2022                 while ( --len >= 0 && *p++ != sep )
2023                         ;
2024         }
2025         while ( len >= 0 && *p == ' ' ) {
2026                 len--;
2027                 p++;
2028         }
2029         if ( len < 0 ) {
2030                 return -1;
2031         }
2032
2033         if ( !bv ) {
2034                 return 0;
2035         }
2036
2037         bv->bv_val = p;
2038         while ( --len >= 0 && *p != sep ) {
2039                 bv->bv_len++;
2040                 p++;
2041         }
2042         while ( bv->bv_len > 0 && *--p == ' ' ) {
2043                 bv->bv_len--;
2044         }
2045         
2046         return bv->bv_len;
2047 }
2048
2049 typedef struct aci_set_gather_t {
2050         SetCookie               *cookie;
2051         BerVarray               bvals;
2052 } aci_set_gather_t;
2053
2054 static int
2055 aci_set_cb_gather( Operation *op, SlapReply *rs )
2056 {
2057         aci_set_gather_t        *p = (aci_set_gather_t *)op->o_callback->sc_private;
2058         
2059         if ( rs->sr_type == REP_SEARCH ) {
2060                 BerValue        bvals[ 2 ];
2061                 BerVarray       bvalsp = NULL;
2062                 int             j;
2063
2064                 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2065                         AttributeDescription    *desc = rs->sr_attrs[ j ].an_desc;
2066                         
2067                         if ( desc == slap_schema.si_ad_entryDN ) {
2068                                 bvalsp = bvals;
2069                                 bvals[ 0 ] = rs->sr_entry->e_nname;
2070                                 BER_BVZERO( &bvals[ 1 ] );
2071
2072                         } else {
2073                                 Attribute       *a;
2074
2075                                 a = attr_find( rs->sr_entry->e_attrs, desc );
2076                                 if ( a != NULL ) {
2077                                         int     i;
2078
2079                                         for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2080                                                 ;
2081
2082                                         bvalsp = a->a_nvals;
2083                                 }
2084                         }
2085                 }
2086
2087                 if ( bvals ) {
2088                         p->bvals = slap_set_join( p->cookie, p->bvals,
2089                                         ( '|' | SLAP_SET_RREF ), bvalsp );
2090                 }
2091
2092         } else {
2093                 assert( rs->sr_type == REP_RESULT );
2094         }
2095
2096         return 0;
2097 }
2098
2099 BerVarray
2100 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2101 {
2102         AciSetCookie            *cp = (AciSetCookie *)cookie;
2103         int                     rc = 0;
2104         LDAPURLDesc             *ludp = NULL;
2105         Operation               op2 = { 0 };
2106         SlapReply               rs = {REP_RESULT};
2107         AttributeName           anlist[ 2 ], *anlistp = NULL;
2108         int                     nattrs = 0;
2109         slap_callback           cb = { NULL, aci_set_cb_gather, NULL, NULL };
2110         aci_set_gather_t        p = { 0 };
2111         const char              *text = NULL;
2112         static struct berval    defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2113
2114         /* this routine needs to return the bervals instead of
2115          * plain strings, since syntax is not known.  It should
2116          * also return the syntax or some "comparison cookie".
2117          */
2118         if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2119                 return aci_set_gather2( cookie, name, desc );
2120         }
2121
2122         rc = ldap_url_parse( name->bv_val, &ludp );
2123         if ( rc != LDAP_URL_SUCCESS ) {
2124                 rc = LDAP_PROTOCOL_ERROR;
2125                 goto url_done;
2126         }
2127         
2128         if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2129         {
2130                 /* host part must be empty */
2131                 /* extensions parts must be empty */
2132                 rc = LDAP_PROTOCOL_ERROR;
2133                 goto url_done;
2134         }
2135
2136         /* Grab the searchbase and see if an appropriate database can be found */
2137         ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2138         rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2139                         &op2.o_req_ndn, cp->op->o_tmpmemctx );
2140         BER_BVZERO( &op2.o_req_dn );
2141         if ( rc != LDAP_SUCCESS ) {
2142                 goto url_done;
2143         }
2144
2145         op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2146         if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2147                 rc = LDAP_NO_SUCH_OBJECT;
2148                 goto url_done;
2149         }
2150
2151         /* Grab the filter */
2152         if ( ludp->lud_filter ) {
2153                 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2154                                 cp->op->o_tmpmemctx );
2155                 
2156         } else {
2157                 op2.ors_filterstr = defaultFilter_bv;
2158         }
2159
2160         op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
2161         if ( op2.ors_filter == NULL ) {
2162                 rc = LDAP_PROTOCOL_ERROR;
2163                 goto url_done;
2164         }
2165
2166         /* Grab the scope */
2167         op2.ors_scope = ludp->lud_scope;
2168
2169         /* Grap the attributes */
2170         if ( ludp->lud_attrs ) {
2171                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2172                         ;
2173
2174                 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2175                                 cp->op->o_tmpmemctx );
2176
2177                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2178                         ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2179                         anlistp[ nattrs ].an_desc = NULL;
2180                         rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2181                                         &anlistp[ nattrs ].an_desc, &text );
2182                         if ( rc != LDAP_SUCCESS ) {
2183                                 goto url_done;
2184                         }
2185                 }
2186
2187         } else {
2188                 anlistp = anlist;
2189         }
2190
2191         anlistp[ nattrs ].an_name = desc->ad_cname;
2192         anlistp[ nattrs ].an_desc = desc;
2193
2194         BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2195         
2196         p.cookie = cookie;
2197         
2198         op2.o_hdr = cp->op->o_hdr;
2199         op2.o_tag = LDAP_REQ_SEARCH;
2200         op2.o_ndn = op2.o_bd->be_rootndn;
2201         op2.o_callback = &cb;
2202         op2.o_time = slap_get_time();
2203         op2.o_do_not_cache = 1;
2204         op2.o_is_auth_check = 0;
2205         ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
2206         op2.ors_slimit = SLAP_NO_LIMIT;
2207         op2.ors_tlimit = SLAP_NO_LIMIT;
2208         op2.ors_attrs = anlistp;
2209         op2.ors_attrsonly = 0;
2210         op2.o_private = cp->op->o_private;
2211
2212         cb.sc_private = &p;
2213
2214         rc = op2.o_bd->be_search( &op2, &rs );
2215         if ( rc != 0 ) {
2216                 goto url_done;
2217         }
2218
2219 url_done:;
2220         if ( op2.ors_filter ) {
2221                 filter_free_x( cp->op, op2.ors_filter );
2222         }
2223         if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2224                 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2225         }
2226         if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2227                 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2228         }
2229         if ( ludp ) {
2230                 ldap_free_urldesc( ludp );
2231         }
2232         if ( anlistp && anlistp != anlist ) {
2233                 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2234         }
2235
2236         return p.bvals;
2237 }
2238
2239 BerVarray
2240 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2241 {
2242         AciSetCookie    *cp = (AciSetCookie *)cookie;
2243         BerVarray       bvals = NULL;
2244         struct berval   ndn;
2245         int             rc = 0;
2246
2247         /* this routine needs to return the bervals instead of
2248          * plain strings, since syntax is not known.  It should
2249          * also return the syntax or some "comparison cookie".
2250          */
2251         rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2252         if ( rc == LDAP_SUCCESS ) {
2253                 if ( desc == slap_schema.si_ad_entryDN ) {
2254                         bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2255                                         cp->op->o_tmpmemctx );
2256                         bvals[ 0 ] = ndn;
2257                         BER_BVZERO( &bvals[ 1 ] );
2258                         BER_BVZERO( &ndn );
2259
2260                 } else {
2261                         backend_attribute( cp->op,
2262                                 cp->e, &ndn, desc, &bvals, ACL_NONE );
2263                 }
2264
2265                 if ( !BER_BVISNULL( &ndn ) ) {
2266                         slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2267                 }
2268         }
2269
2270         return bvals;
2271 }
2272
2273 static int
2274 aci_match_set (
2275         struct berval *subj,
2276         Operation *op,
2277         Entry *e,
2278         int setref
2279 )
2280 {
2281         struct berval   set = BER_BVNULL;
2282         int             rc = 0;
2283         AciSetCookie    cookie;
2284
2285         if ( setref == 0 ) {
2286                 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2287
2288         } else {
2289                 struct berval           subjdn, ndn = BER_BVNULL;
2290                 struct berval           setat;
2291                 BerVarray               bvals;
2292                 const char              *text;
2293                 AttributeDescription    *desc = NULL;
2294
2295                 /* format of string is "entry/setAttrName" */
2296                 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2297                         return 0;
2298                 }
2299
2300                 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2301                         setat = aci_bv_set_attr;
2302                 }
2303
2304                 /*
2305                  * NOTE: dnNormalize honors the ber_len field
2306                  * as the length of the dn to be normalized
2307                  */
2308                 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2309                         if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2310                         {
2311                                 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2312                                 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2313                                         int     i;
2314
2315                                         set = bvals[0];
2316                                         BER_BVZERO( &bvals[0] );
2317                                         for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2318                                                 /* count */ ;
2319                                         bvals[0].bv_val = bvals[i-1].bv_val;
2320                                         BER_BVZERO( &bvals[i-1] );
2321                                 }
2322                                 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2323                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2324                         }
2325                 }
2326         }
2327
2328         if ( !BER_BVISNULL( &set ) ) {
2329                 cookie.op = op;
2330                 cookie.e = e;
2331                 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2332                         &op->o_ndn, &e->e_nname, NULL ) > 0 );
2333                 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2334         }
2335
2336         return(rc);
2337 }
2338
2339 #ifdef SLAPD_ACI_ENABLED
2340 static int
2341 aci_list_map_rights(
2342         struct berval *list )
2343 {
2344         struct berval bv;
2345         slap_access_t mask;
2346         int i;
2347
2348         ACL_INIT(mask);
2349         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2350                 if (bv.bv_len <= 0)
2351                         continue;
2352                 switch (*bv.bv_val) {
2353                 case 'c':
2354                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2355                         break;
2356                 case 's':
2357                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2358                          * the right 's' to mean "set", but in the examples states
2359                          * that the right 's' means "search".  The latter definition
2360                          * is used here.
2361                          */
2362                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2363                         break;
2364                 case 'r':
2365                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
2366                         break;
2367                 case 'w':
2368                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2369                         break;
2370                 case 'x':
2371                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
2372                          * define any equivalent to the AUTH right, so I've just used
2373                          * 'x' for now.
2374                          */
2375                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2376                         break;
2377                 default:
2378                         break;
2379                 }
2380
2381         }
2382         return(mask);
2383 }
2384
2385 static int
2386 aci_list_has_attr(
2387         struct berval *list,
2388         const struct berval *attr,
2389         struct berval *val )
2390 {
2391         struct berval bv, left, right;
2392         int i;
2393
2394         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2395                 if (aci_get_part(&bv, 0, '=', &left) < 0
2396                         || aci_get_part(&bv, 1, '=', &right) < 0)
2397                 {
2398                         if (ber_bvstrcasecmp(attr, &bv) == 0)
2399                                 return(1);
2400                 } else if (val == NULL) {
2401                         if (ber_bvstrcasecmp(attr, &left) == 0)
2402                                 return(1);
2403                 } else {
2404                         if (ber_bvstrcasecmp(attr, &left) == 0) {
2405                                 /* this is experimental code that implements a
2406                                  * simple (prefix) match of the attribute value.
2407                                  * the ACI draft does not provide for aci's that
2408                                  * apply to specific values, but it would be
2409                                  * nice to have.  If the <attr> part of an aci's
2410                                  * rights list is of the form <attr>=<value>,
2411                                  * that means the aci applies only to attrs with
2412                                  * the given value.  Furthermore, if the attr is
2413                                  * of the form <attr>=<value>*, then <value> is
2414                                  * treated as a prefix, and the aci applies to 
2415                                  * any value with that prefix.
2416                                  *
2417                                  * Ideally, this would allow r.e. matches.
2418                                  */
2419                                 if (aci_get_part(&right, 0, '*', &left) < 0
2420                                         || right.bv_len <= left.bv_len)
2421                                 {
2422                                         if (ber_bvstrcasecmp(val, &right) == 0)
2423                                                 return(1);
2424                                 } else if (val->bv_len >= left.bv_len) {
2425                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2426                                                 return(1);
2427                                 }
2428                         }
2429                 }
2430         }
2431         return(0);
2432 }
2433
2434 static slap_access_t
2435 aci_list_get_attr_rights(
2436         struct berval *list,
2437         const struct berval *attr,
2438         struct berval *val )
2439 {
2440     struct berval bv;
2441     slap_access_t mask;
2442     int i;
2443
2444         /* loop through each rights/attr pair, skip first part (action) */
2445         ACL_INIT(mask);
2446         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2447                 if (aci_list_has_attr(&bv, attr, val) == 0)
2448                         continue;
2449                 if (aci_get_part(list, i, ';', &bv) < 0)
2450                         continue;
2451                 mask |= aci_list_map_rights(&bv);
2452         }
2453         return(mask);
2454 }
2455
2456 static int
2457 aci_list_get_rights(
2458         struct berval *list,
2459         const struct berval *attr,
2460         struct berval *val,
2461         slap_access_t *grant,
2462         slap_access_t *deny )
2463 {
2464     struct berval perm, actn;
2465     slap_access_t *mask;
2466     int i, found;
2467
2468         if (attr == NULL || attr->bv_len == 0 
2469                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2470                 attr = &aci_bv_br_entry;
2471         }
2472
2473         found = 0;
2474         ACL_INIT(*grant);
2475         ACL_INIT(*deny);
2476         /* loop through each permissions clause */
2477         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2478                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2479                         continue;
2480                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2481                         mask = grant;
2482                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2483                         mask = deny;
2484                 } else {
2485                         continue;
2486                 }
2487
2488                 found = 1;
2489                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2490                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2491         }
2492         return(found);
2493 }
2494
2495 static int
2496 aci_group_member (
2497         struct berval   *subj,
2498         struct berval   *defgrpoc,
2499         struct berval   *defgrpat,
2500         Operation       *op,
2501         Entry           *e,
2502         int             nmatch,
2503         regmatch_t      *matches
2504 )
2505 {
2506         struct berval subjdn;
2507         struct berval grpoc;
2508         struct berval grpat;
2509         ObjectClass *grp_oc = NULL;
2510         AttributeDescription *grp_ad = NULL;
2511         const char *text;
2512         int rc;
2513
2514         /* format of string is "group/objectClassValue/groupAttrName" */
2515         if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2516                 return(0);
2517         }
2518
2519         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2520                 grpoc = *defgrpoc;
2521         }
2522
2523         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2524                 grpat = *defgrpat;
2525         }
2526
2527         rc = slap_bv2ad( &grpat, &grp_ad, &text );
2528         if( rc != LDAP_SUCCESS ) {
2529                 rc = 0;
2530                 goto done;
2531         }
2532         rc = 0;
2533
2534         grp_oc = oc_bvfind( &grpoc );
2535
2536         if (grp_oc != NULL && grp_ad != NULL ) {
2537                 char buf[ACL_BUF_SIZE];
2538                 struct berval bv, ndn;
2539                 bv.bv_len = sizeof( buf ) - 1;
2540                 bv.bv_val = (char *)&buf;
2541                 if ( string_expand(&bv, &subjdn,
2542                                 e->e_ndn, nmatch, matches) )
2543                 {
2544                         rc = LDAP_OTHER;
2545                         goto done;
2546                 }
2547                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2548                         rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2549                                 grp_oc, grp_ad ) == 0 );
2550                         slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2551                 }
2552         }
2553
2554 done:
2555         return(rc);
2556 }
2557
2558 static int
2559 aci_mask(
2560         Operation               *op,
2561         Entry                   *e,
2562         AttributeDescription    *desc,
2563         struct berval           *val,
2564         struct berval           *aci,
2565         int                     nmatch,
2566         regmatch_t              *matches,
2567         slap_access_t           *grant,
2568         slap_access_t           *deny,
2569         slap_aci_scope_t        asserted_scope
2570 )
2571 {
2572         struct berval           bv, scope, perms, type, sdn;
2573         int                     rc;
2574                 
2575
2576         assert( !BER_BVISNULL( &desc->ad_cname ) );
2577
2578         /* parse an aci of the form:
2579                 oid # scope # action;rights;attr;rights;attr 
2580                         $ action;rights;attr;rights;attr # type # subject
2581
2582            [NOTE: the following comment is very outdated,
2583            as the draft version it refers to (Ando, 2004-11-20)].
2584
2585            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2586            a full description of the format for this attribute.
2587            Differences: "this" in the draft is "self" here, and
2588            "self" and "public" is in the position of type.
2589
2590            <scope> = {entry|children|subtree}
2591            <type> = {public|users|access-id|subtree|onelevel|children|
2592                      self|dnattr|group|role|set|set-ref}
2593
2594            This routine now supports scope={ENTRY,CHILDREN}
2595            with the semantics:
2596              - ENTRY applies to "entry" and "subtree";
2597              - CHILDREN aplies to "children" and "subtree"
2598          */
2599
2600         /* check that the aci has all 5 components */
2601         if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2602                 return 0;
2603         }
2604
2605         /* check that the aci family is supported */
2606         if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2607                 return 0;
2608         }
2609
2610         /* check that the scope matches */
2611         if ( aci_get_part( aci, 1, '#', &scope ) < 0 ) {
2612                 return 0;
2613         }
2614
2615         /* note: scope can be either ENTRY or CHILDREN;
2616          * they respectively match "entry" and "children" in bv
2617          * both match "subtree" */
2618         switch ( asserted_scope ) {
2619         case SLAP_ACI_SCOPE_ENTRY:
2620                 if ( ber_bvstrcasecmp( &scope, &aci_bv_entry ) != 0
2621                                 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2622                 {
2623                         return 0;
2624                 }
2625                 break;
2626
2627         case SLAP_ACI_SCOPE_CHILDREN:
2628                 if ( ber_bvstrcasecmp( &scope, &aci_bv_children ) != 0
2629                                 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2630                 {
2631                         return 0;
2632                 }
2633                 break;
2634
2635         default:
2636                 return 0;
2637         }
2638
2639         /* get the list of permissions clauses, bail if empty */
2640         if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
2641                 return 0;
2642         }
2643
2644         /* check if any permissions allow desired access */
2645         if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
2646                 return 0;
2647         }
2648
2649         /* see if we have a DN match */
2650         if ( aci_get_part( aci, 3, '#', &type ) < 0 ) {
2651                 return 0;
2652         }
2653
2654         /* see if we have a public (i.e. anonymous) access */
2655         if ( ber_bvstrcasecmp( &aci_bv_public, &type ) == 0 ) {
2656                 return 1;
2657         }
2658         
2659         /* otherwise require an identity */
2660         if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
2661                 return 0;
2662         }
2663
2664         /* see if we have a users access */
2665         if ( ber_bvstrcasecmp( &aci_bv_users, &type ) == 0 ) {
2666                 return 1;
2667         }
2668         
2669         /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
2670          * just grab all the berval up to its end (ITS#3303).
2671          * NOTE: the problem could be solved by providing the DN with
2672          * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would 
2673          * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
2674 #if 0
2675         if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
2676                 return 0;
2677         }
2678 #endif
2679         sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
2680         sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
2681
2682         if ( ber_bvstrcasecmp( &aci_bv_access_id, &type ) == 0 ) {
2683                 struct berval ndn;
2684                 
2685                 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2686                 if ( rc != LDAP_SUCCESS ) {
2687                         return 0;
2688                 }
2689
2690                 if ( dn_match( &op->o_ndn, &ndn ) ) {
2691                         rc = 1;
2692                 }
2693                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2694
2695                 return rc;
2696
2697         } else if ( ber_bvstrcasecmp( &aci_bv_subtree, &type ) == 0 ) {
2698                 struct berval ndn;
2699                 
2700                 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2701                 if ( rc != LDAP_SUCCESS ) {
2702                         return 0;
2703                 }
2704
2705                 if ( dnIsSuffix( &op->o_ndn, &ndn ) ) {
2706                         rc = 1;
2707                 }
2708                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2709
2710                 return rc;
2711
2712         } else if ( ber_bvstrcasecmp( &aci_bv_onelevel, &type ) == 0 ) {
2713                 struct berval ndn, pndn;
2714                 
2715                 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2716                 if ( rc != LDAP_SUCCESS ) {
2717                         return 0;
2718                 }
2719
2720                 dnParent( &ndn, &pndn );
2721
2722                 if ( dn_match( &op->o_ndn, &pndn ) ) {
2723                         rc = 1;
2724                 }
2725                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2726
2727                 return rc;
2728
2729         } else if ( ber_bvstrcasecmp( &aci_bv_children, &type ) == 0 ) {
2730                 struct berval ndn;
2731                 
2732                 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2733                 if ( rc != LDAP_SUCCESS ) {
2734                         return 0;
2735                 }
2736
2737                 if ( !dn_match( &op->o_ndn, &ndn )
2738                                 && dnIsSuffix( &op->o_ndn, &ndn ) )
2739                 {
2740                         rc = 1;
2741                 }
2742                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2743
2744                 return rc;
2745
2746         } else if ( ber_bvstrcasecmp( &aci_bv_self, &type ) == 0 ) {
2747                 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
2748                         return 1;
2749                 }
2750
2751         } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &type ) == 0 ) {
2752                 Attribute               *at;
2753                 AttributeDescription    *ad = NULL;
2754                 const char              *text;
2755
2756                 rc = slap_bv2ad( &sdn, &ad, &text );
2757
2758                 if( rc != LDAP_SUCCESS ) {
2759                         return 0;
2760                 }
2761
2762                 rc = 0;
2763
2764                 for ( at = attrs_find( e->e_attrs, ad );
2765                                 at != NULL;
2766                                 at = attrs_find( at->a_next, ad ) )
2767                 {
2768                         if ( value_find_ex( ad,
2769                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2770                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2771                                 at->a_nvals,
2772                                 &op->o_ndn, op->o_tmpmemctx ) == 0 )
2773                         {
2774                                 rc = 1;
2775                                 break;
2776                         }
2777                 }
2778
2779                 return rc;
2780
2781         } else if ( ber_bvstrcasecmp( &aci_bv_group, &type ) == 0 ) {
2782                 if ( aci_group_member( &sdn, &aci_bv_group_class,
2783                                 &aci_bv_group_attr, op, e, nmatch, matches ) )
2784                 {
2785                         return 1;
2786                 }
2787
2788         } else if ( ber_bvstrcasecmp( &aci_bv_role, &type ) == 0 ) {
2789                 if ( aci_group_member( &sdn, &aci_bv_role_class,
2790                                 &aci_bv_role_attr, op, e, nmatch, matches ) )
2791                 {
2792                         return 1;
2793                 }
2794
2795         } else if ( ber_bvstrcasecmp( &aci_bv_set, &type ) == 0 ) {
2796                 if ( aci_match_set( &sdn, op, e, 0 ) ) {
2797                         return 1;
2798                 }
2799
2800         } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &type ) == 0 ) {
2801                 if ( aci_match_set( &sdn, op, e, 1 ) ) {
2802                         return 1;
2803                 }
2804         }
2805
2806         return 0;
2807 }
2808
2809 #ifdef SLAP_DYNACL
2810 /*
2811  * FIXME: there is a silly dependence that makes it difficult
2812  * to move ACIs in a run-time loadable module under the "dynacl" 
2813  * umbrella, because sets share some helpers with ACIs.
2814  */
2815 static int
2816 dynacl_aci_parse( const char *fname, int lineno, slap_style_t sty, const char *right, void **privp )
2817 {
2818         AttributeDescription    *ad = NULL;
2819         const char              *text = NULL;
2820
2821         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
2822                 fprintf( stderr, "%s: line %d: "
2823                         "inappropriate style \"%s\" in \"aci\" by clause\n",
2824                         fname, lineno, sty );
2825                 return -1;
2826         }
2827
2828         if ( right != NULL && *right != '\0' ) {
2829                 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
2830                         fprintf( stderr,
2831                                 "%s: line %d: aci \"%s\": %s\n",
2832                                 fname, lineno, right, text );
2833                         return -1;
2834                 }
2835
2836         } else {
2837                 ad = slap_schema.si_ad_aci;
2838         }
2839
2840         if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
2841                 fprintf( stderr, "%s: line %d: "
2842                         "aci \"%s\": inappropriate syntax: %s\n",
2843                         fname, lineno, right,
2844                         ad->ad_type->sat_syntax_oid );
2845                 return -1;
2846         }
2847
2848         *privp = (void *)ad;
2849
2850         return 0;
2851 }
2852
2853 static int
2854 dynacl_aci_unparse( void *priv, struct berval *bv )
2855 {
2856         AttributeDescription    *ad = ( AttributeDescription * )priv;
2857         char *ptr;
2858
2859         assert( ad );
2860
2861         bv->bv_val = ch_malloc( STRLENOF(" aci=") + ad->ad_cname.bv_len + 1 );
2862         ptr = lutil_strcopy( bv->bv_val, " aci=" );
2863         ptr = lutil_strcopy( ptr, ad->ad_cname.bv_val );
2864         bv->bv_len = ptr - bv->bv_val;
2865
2866         return 0;
2867 }
2868
2869
2870 static int
2871 dynacl_aci_mask(
2872                 void                    *priv,
2873                 Operation               *op,
2874                 Entry                   *e,
2875                 AttributeDescription    *desc,
2876                 struct berval           *val,
2877                 int                     nmatch,
2878                 regmatch_t              *matches,
2879                 slap_access_t           *grantp,
2880                 slap_access_t           *denyp )
2881 {
2882         AttributeDescription    *ad = ( AttributeDescription * )priv;
2883         Attribute               *at;
2884         slap_access_t           tgrant, tdeny, grant, deny;
2885 #ifdef LDAP_DEBUG
2886         char                    accessmaskbuf[ACCESSMASK_MAXLEN];
2887         char                    accessmaskbuf1[ACCESSMASK_MAXLEN];
2888 #endif /* LDAP_DEBUG */
2889
2890         /* start out with nothing granted, nothing denied */
2891         ACL_INIT(tgrant);
2892         ACL_INIT(tdeny);
2893
2894         /* get the aci attribute */
2895         at = attr_find( e->e_attrs, ad );
2896         if ( at != NULL ) {
2897                 int             i;
2898
2899                 /* the aci is an multi-valued attribute.  The
2900                  * rights are determined by OR'ing the individual
2901                  * rights given by the acis.
2902                  */
2903                 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2904                         if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
2905                                         nmatch, matches, &grant, &deny,
2906                                         SLAP_ACI_SCOPE_ENTRY ) != 0 )
2907                         {
2908                                 tgrant |= grant;
2909                                 tdeny |= deny;
2910                         }
2911                 }
2912                 
2913                 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2914                           accessmask2str( tgrant, accessmaskbuf, 1 ), 
2915                           accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
2916         }
2917
2918         /* If the entry level aci didn't contain anything valid for the 
2919          * current operation, climb up the tree and evaluate the
2920          * acis with scope set to subtree
2921          */
2922         if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
2923                 struct berval   parent_ndn;
2924
2925 #if 1
2926                 /* to solve the chicken'n'egg problem of accessing
2927                  * the OpenLDAPaci attribute, the direct access
2928                  * to the entry's attribute is unchecked; however,
2929                  * further accesses to OpenLDAPaci values in the 
2930                  * ancestors occur through backend_attribute(), i.e.
2931                  * with the identity of the operation, requiring
2932                  * further access checking.  For uniformity, this
2933                  * makes further requests occur as the rootdn, if
2934                  * any, i.e. searching for the OpenLDAPaci attribute
2935                  * is considered an internal search.  If this is not
2936                  * acceptable, then the same check needs be performed
2937                  * when accessing the entry's attribute. */
2938                 Operation       op2 = *op;
2939
2940                 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
2941                         op2.o_dn = op->o_bd->be_rootdn;
2942                         op2.o_ndn = op->o_bd->be_rootndn;
2943                 }
2944 #endif
2945
2946                 dnParent( &e->e_nname, &parent_ndn );
2947                 while ( !BER_BVISEMPTY( &parent_ndn ) ){
2948                         int             i;
2949                         BerVarray       bvals = NULL;
2950                         int             ret, stop;
2951
2952                         Debug( LDAP_DEBUG_ACL, "checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
2953                         ret = backend_attribute( &op2, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
2954
2955                         switch ( ret ) {
2956                         case LDAP_SUCCESS :
2957                                 stop = 0;
2958                                 if ( !bvals ) {
2959                                         break;
2960                                 }
2961
2962                                 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++) {
2963                                         if ( aci_mask( op, e, desc, val,
2964                                                         &bvals[i],
2965                                                         nmatch, matches,
2966                                                         &grant, &deny,
2967                                                         SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2968                                         {
2969                                                 tgrant |= grant;
2970                                                 tdeny |= deny;
2971                                                 /* evaluation stops as soon as either a "deny" or a 
2972                                                  * "grant" directive matches.
2973                                                  */
2974                                                 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
2975                                                         stop = 1;
2976                                                 }
2977                                         }
2978                                         Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n", 
2979                                                 accessmask2str( tgrant, accessmaskbuf, 1 ),
2980                                                 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
2981                                 }
2982                                 break;
2983
2984                         case LDAP_NO_SUCH_ATTRIBUTE:
2985                                 /* just go on if the aci-Attribute is not present in
2986                                  * the current entry 
2987                                  */
2988                                 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
2989                                 stop = 0;
2990                                 break;
2991
2992                         case LDAP_NO_SUCH_OBJECT:
2993                                 /* We have reached the base object */
2994                                 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
2995                                 stop = 1;
2996                                 break;
2997
2998                         default:
2999                                 stop = 1;
3000                                 break;
3001                         }
3002
3003                         if ( stop ) {
3004                                 break;
3005                         }
3006                         dnParent( &parent_ndn, &parent_ndn );
3007                 }
3008         }
3009
3010         *grantp = tgrant;
3011         *denyp = tdeny;
3012
3013         return 0;
3014 }
3015
3016 /* need to register this at some point */
3017 static slap_dynacl_t    dynacl_aci = {
3018         "aci",
3019         dynacl_aci_parse,
3020         dynacl_aci_unparse,
3021         dynacl_aci_mask,
3022         NULL,
3023         NULL,
3024         NULL
3025 };
3026
3027 #endif /* SLAP_DYNACL */
3028
3029 #endif  /* SLAPD_ACI_ENABLED */
3030
3031 #ifdef SLAP_DYNACL
3032
3033 /*
3034  * dynamic ACL infrastructure
3035  */
3036 static slap_dynacl_t    *da_list = NULL;
3037
3038 int
3039 slap_dynacl_register( slap_dynacl_t *da )
3040 {
3041         slap_dynacl_t   *tmp;
3042
3043         for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
3044                 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
3045                         break;
3046                 }
3047         }
3048
3049         if ( tmp != NULL ) {
3050                 return -1;
3051         }
3052         
3053         if ( da->da_mask == NULL ) {
3054                 return -1;
3055         }
3056         
3057         da->da_private = NULL;
3058         da->da_next = da_list;
3059         da_list = da;
3060
3061         return 0;
3062 }
3063
3064 static slap_dynacl_t *
3065 slap_dynacl_next( slap_dynacl_t *da )
3066 {
3067         if ( da ) {
3068                 return da->da_next;
3069         }
3070         return da_list;
3071 }
3072
3073 slap_dynacl_t *
3074 slap_dynacl_get( const char *name )
3075 {
3076         slap_dynacl_t   *da;
3077
3078         for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
3079                 if ( strcasecmp( da->da_name, name ) == 0 ) {
3080                         break;
3081                 }
3082         }
3083
3084         return da;
3085 }
3086 #endif /* SLAP_DYNACL */
3087
3088 int
3089 acl_init( void )
3090 {
3091         int             i, rc;
3092 #ifdef SLAP_DYNACL
3093         slap_dynacl_t   *known_dynacl[] = {
3094 #ifdef SLAPD_ACI_ENABLED
3095                 &dynacl_aci,
3096 #endif  /* SLAPD_ACI_ENABLED */
3097                 NULL
3098         };
3099
3100         for ( i = 0; known_dynacl[ i ]; i++ ) {
3101                 rc = slap_dynacl_register( known_dynacl[ i ] ); 
3102                 if ( rc ) {
3103                         return rc;
3104                 }
3105         }
3106 #endif /* SLAP_DYNACL */
3107
3108         return 0;
3109 }
3110
3111 static int
3112 string_expand(
3113         struct berval   *bv,
3114         struct berval   *pat,
3115         char            *match,
3116         int             nmatch,
3117         regmatch_t      *matches)
3118 {
3119         ber_len_t       size;
3120         char   *sp;
3121         char   *dp;
3122         int     flag;
3123
3124         size = 0;
3125         bv->bv_val[0] = '\0';
3126         bv->bv_len--; /* leave space for lone $ */
3127
3128         flag = 0;
3129         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
3130                 sp < pat->bv_val + pat->bv_len ; sp++ )
3131         {
3132                 /* did we previously see a $ */
3133                 if ( flag ) {
3134                         if ( flag == 1 && *sp == '$' ) {
3135                                 *dp++ = '$';
3136                                 size++;
3137                                 flag = 0;
3138
3139                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
3140                                 flag = 2;
3141
3142                         } else if ( *sp >= '0' && *sp <= '9' ) {
3143                                 int     n;
3144                                 int     i;
3145                                 int     l;
3146
3147                                 n = *sp - '0';
3148
3149                                 if ( flag == 2 ) {
3150                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
3151                                                 if ( *sp >= '0' && *sp <= '9' ) {
3152                                                         n = 10*n + ( *sp - '0' );
3153                                                 }
3154                                         }
3155
3156                                         if ( *sp != /*'{'*/ '}' ) {
3157                                                 /* FIXME: error */
3158                                                 return 1;
3159                                         }
3160                                 }
3161
3162                                 if ( n >= nmatch ) {
3163                                         /* FIXME: error */
3164                                         return 1;
3165                                 }
3166                                 
3167                                 *dp = '\0';
3168                                 i = matches[n].rm_so;
3169                                 l = matches[n].rm_eo; 
3170                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
3171                                         *dp++ = match[i];
3172                                 }
3173                                 *dp = '\0';
3174
3175                                 flag = 0;
3176                         }
3177                 } else {
3178                         if (*sp == '$') {
3179                                 flag = 1;
3180                         } else {
3181                                 *dp++ = *sp;
3182                                 size++;
3183                         }
3184                 }
3185         }
3186
3187         if ( flag ) {
3188                 /* must have ended with a single $ */
3189                 *dp++ = '$';
3190                 size++;
3191         }
3192
3193         *dp = '\0';
3194         bv->bv_len = size;
3195
3196         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
3197         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
3198
3199         return 0;
3200 }
3201
3202 static int
3203 regex_matches(
3204         struct berval   *pat,           /* pattern to expand and match against */
3205         char            *str,           /* string to match against pattern */
3206         char            *buf,           /* buffer with $N expansion variables */
3207         int             nmatch, /* size of the matches array */
3208         regmatch_t      *matches        /* offsets in buffer for $N expansion variables */
3209 )
3210 {
3211         regex_t re;
3212         char newbuf[ACL_BUF_SIZE];
3213         struct berval bv;
3214         int     rc;
3215
3216         bv.bv_len = sizeof( newbuf ) - 1;
3217         bv.bv_val = newbuf;
3218
3219         if (str == NULL) {
3220                 str = "";
3221         };
3222
3223         string_expand( &bv, pat, buf, nmatch, matches );
3224         rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
3225         if ( rc ) {
3226                 char error[ACL_BUF_SIZE];
3227                 regerror( rc, &re, error, sizeof( error ) );
3228
3229                 Debug( LDAP_DEBUG_TRACE,
3230                     "compile( \"%s\", \"%s\") failed %s\n",
3231                         pat->bv_val, str, error );
3232                 return( 0 );
3233         }
3234
3235         rc = regexec( &re, str, 0, NULL, 0 );
3236         regfree( &re );
3237
3238         Debug( LDAP_DEBUG_TRACE,
3239             "=> regex_matches: string:   %s\n", str, 0, 0 );
3240         Debug( LDAP_DEBUG_TRACE,
3241             "=> regex_matches: rc: %d %s\n",
3242                 rc, !rc ? "matches" : "no matches", 0 );
3243         return( !rc );
3244 }
3245