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