]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
fix ACL who logging
[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                         Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1222                                 b->a_group_pat.bv_val, 0, 0 );
1223
1224                         /* b->a_group is an unexpanded entry name, expanded it should be an 
1225                          * entry with objectclass group* and we test to see if odn is one of
1226                          * the values in the attribute group
1227                          */
1228                         /* see if asker is listed in dnattr */
1229                         if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1230                                 char buf[ACL_BUF_SIZE];
1231                                 bv.bv_len = sizeof(buf) - 1;
1232                                 bv.bv_val = buf; 
1233
1234                                 string_expand( &bv, &b->a_group_pat, e->e_ndn, matches );
1235                                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
1236                                         /* did not expand to a valid dn */
1237                                         continue;
1238                                 }
1239
1240                                 bv = ndn;
1241
1242                         } else {
1243                                 bv = b->a_group_pat;
1244                         }
1245
1246                         rc = backend_group( op, e, &bv, &op->o_ndn,
1247                                 b->a_group_oc, b->a_group_at );
1248
1249                         if ( ndn.bv_val ) free( ndn.bv_val );
1250
1251                         if ( rc != 0 ) {
1252                                 continue;
1253                         }
1254                 }
1255
1256                 if ( b->a_set_pat.bv_len != 0 ) {
1257                         struct berval   bv;
1258                         char    buf[ACL_BUF_SIZE];
1259
1260                         Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1261                                 b->a_set_pat.bv_val, 0, 0 );
1262
1263                         if ( b->a_set_style == ACL_STYLE_REGEX ) {
1264                                 bv.bv_len = sizeof(buf) - 1;
1265                                 bv.bv_val = buf;
1266                                 string_expand( &bv, &b->a_set_pat, e->e_ndn, matches );
1267                         } else {
1268                                 bv = b->a_set_pat;
1269                         }
1270
1271                         if ( aci_match_set( &bv, op, e, 0 ) == 0)  {
1272                                 continue;
1273                         }
1274                 }
1275
1276                 if ( b->a_authz.sai_ssf ) {
1277 #ifdef NEW_LOGGING
1278                         LDAP_LOG( ACL, DETAIL1, 
1279                                 "acl_mask: conn %lu  check a_authz.sai_ssf: ACL %u > OP %u\n",
1280                                 op->o_connid, b->a_authz.sai_ssf, op->o_ssf  );
1281 #else
1282                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1283                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
1284 #endif
1285                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
1286                                 continue;
1287                         }
1288                 }
1289
1290                 if ( b->a_authz.sai_transport_ssf ) {
1291 #ifdef NEW_LOGGING
1292                         LDAP_LOG( ACL, DETAIL1, 
1293                                 "acl_mask: conn %lu  check a_authz.sai_transport_ssf: "
1294                                 "ACL %u > OP %u\n",
1295                                 op->o_connid, b->a_authz.sai_transport_ssf, 
1296                                 op->o_transport_ssf  );
1297 #else
1298                         Debug( LDAP_DEBUG_ACL,
1299                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1300                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1301 #endif
1302                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
1303                                 continue;
1304                         }
1305                 }
1306
1307                 if ( b->a_authz.sai_tls_ssf ) {
1308 #ifdef NEW_LOGGING
1309                         LDAP_LOG( ACL, DETAIL1, 
1310                                 "acl_mask: conn %lu  check a_authz.sai_tls_ssf: ACL %u > "
1311                                 "OP %u\n",
1312                                 op->o_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf  );
1313 #else
1314                         Debug( LDAP_DEBUG_ACL,
1315                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1316                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1317 #endif
1318                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
1319                                 continue;
1320                         }
1321                 }
1322
1323                 if ( b->a_authz.sai_sasl_ssf ) {
1324 #ifdef NEW_LOGGING
1325                         LDAP_LOG( ACL, DETAIL1, 
1326                            "acl_mask: conn %lu check a_authz.sai_sasl_ssf: " 
1327                            "ACL %u > OP %u\n",
1328                                 op->o_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf );
1329 #else
1330                         Debug( LDAP_DEBUG_ACL,
1331                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1332                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1333 #endif
1334                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
1335                                 continue;
1336                         }
1337                 }
1338
1339 #ifdef SLAPD_ACI_ENABLED
1340                 if ( b->a_aci_at != NULL ) {
1341                         Attribute       *at;
1342                         slap_access_t grant, deny, tgrant, tdeny;
1343                         struct berval parent_ndn, old_parent_ndn;
1344                         BerVarray bvals = NULL;
1345                         int ret,stop;
1346
1347                         Debug( LDAP_DEBUG_ACL, "    <= check a_aci_at: %s\n",
1348                                 b->a_aci_at->ad_cname.bv_val, 0, 0 );
1349
1350                         /* this case works different from the others above.
1351                          * since aci's themselves give permissions, we need
1352                          * to first check b->a_access_mask, the ACL's access level.
1353                          */
1354
1355                         if ( e->e_nname.bv_len == 0 ) {
1356                                 /* no ACIs in the root DSE */
1357                                 continue;
1358                         }
1359
1360                         /* first check if the right being requested
1361                          * is allowed by the ACL clause.
1362                          */
1363                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1364                                 continue;
1365                         }
1366                         /* start out with nothing granted, nothing denied */
1367                         ACL_INIT(tgrant);
1368                         ACL_INIT(tdeny);
1369
1370                         /* get the aci attribute */
1371                         at = attr_find( e->e_attrs, b->a_aci_at );
1372                         if ( at != NULL ) {
1373 #if 0
1374                                 /* FIXME: this breaks acl caching;
1375                                  * see also ACL_RECORD_VALUE_STATE below */
1376                                 ACL_RECORD_VALUE_STATE;
1377 #endif
1378                                 /* the aci is an multi-valued attribute.  The
1379                                 * rights are determined by OR'ing the individual
1380                                 * rights given by the acis.
1381                                 */
1382                                 for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
1383                                         if (aci_mask( op,
1384                                                 e, desc, val,
1385                                                 &at->a_nvals[i],
1386                                                 matches, &grant, &deny,  &aci_bv_entry ) != 0)
1387                                         {
1388                                                 tgrant |= grant;
1389                                                 tdeny |= deny;
1390                                         }
1391                                 }
1392                                 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1393                                           accessmask2str(tgrant,accessmaskbuf), 
1394                                           accessmask2str(tdeny, accessmaskbuf1), 0);
1395
1396                         }
1397                         /* If the entry level aci didn't contain anything valid for the 
1398                          * current operation, climb up the tree and evaluate the
1399                          * acis with scope set to subtree
1400                          */
1401                         if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1402                                 dnParent(&(e->e_nname), &parent_ndn);
1403                                 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1404                                         old_parent_ndn = parent_ndn;
1405                                         Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1406                                         ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
1407                                         switch(ret){
1408                                         case LDAP_SUCCESS :
1409                                                 stop = 0;
1410                                                 if (!bvals){
1411                                                         break;
1412                                                 }
1413
1414                                                 for( i = 0; bvals[i].bv_val != NULL; i++){
1415 #if 0
1416                                                         /* FIXME: this breaks acl caching;
1417                                                          * see also ACL_RECORD_VALUE_STATE above */
1418                                                         ACL_RECORD_VALUE_STATE;
1419 #endif
1420                                                         if (aci_mask(op, e, desc, val, &bvals[i], matches,
1421                                                                         &grant, &deny, &aci_bv_children) != 0) {
1422                                                                 tgrant |= grant;
1423                                                                 tdeny |= deny;
1424                                                                 /* evaluation stops as soon as either a "deny" or a 
1425                                                                  * "grant" directive matches.
1426                                                                  */
1427                                                                 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1428                                                                         stop = 1;
1429                                                                 }
1430                                                         }
1431                                                         Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n", 
1432                                                                 accessmask2str(tgrant,accessmaskbuf),
1433                                                                 accessmask2str(tdeny, accessmaskbuf1), 0);
1434                                                 }
1435                                                 break;
1436
1437                                         case LDAP_NO_SUCH_ATTRIBUTE:
1438                                                 /* just go on if the aci-Attribute is not present in
1439                                                  * the current entry 
1440                                                  */
1441                                                 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1442                                                 stop = 0;
1443                                                 break;
1444
1445                                         case LDAP_NO_SUCH_OBJECT:
1446                                                 /* We have reached the base object */
1447                                                 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1448                                                 stop = 1;
1449                                                 break;
1450
1451                                         default:
1452                                                 stop = 1;
1453                                                 break;
1454                                         }
1455                                         if (stop){
1456                                                 break;
1457                                         }
1458                                         dnParent(&old_parent_ndn, &parent_ndn);
1459                                 }
1460                         }
1461
1462
1463                         /* remove anything that the ACL clause does not allow */
1464                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1465                         tdeny &= ACL_PRIV_MASK;
1466
1467                         /* see if we have anything to contribute */
1468                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
1469                                 continue;
1470                         }
1471
1472                         /* this could be improved by changing acl_mask so that it can deal with
1473                          * by clauses that return grant/deny pairs.  Right now, it does either
1474                          * additive or subtractive rights, but not both at the same time.  So,
1475                          * we need to combine the grant/deny pair into a single rights mask in
1476                          * a smart way:  if either grant or deny is "empty", then we use the
1477                          * opposite as is, otherwise we remove any denied rights from the grant
1478                          * rights mask and construct an additive mask.
1479                          */
1480                         if (ACL_IS_INVALID(tdeny)) {
1481                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
1482
1483                         } else if (ACL_IS_INVALID(tgrant)) {
1484                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1485
1486                         } else {
1487                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1488                         }
1489
1490                 } else
1491 #endif
1492                 {
1493                         modmask = b->a_access_mask;
1494                 }
1495
1496 #ifdef NEW_LOGGING
1497                 LDAP_LOG( ACL, RESULTS, 
1498                            "acl_mask: [%d] applying %s (%s)\n",
1499                            i, accessmask2str( modmask, accessmaskbuf),
1500                            b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
1501                            ? "break" : "stop"  );
1502 #else
1503                 Debug( LDAP_DEBUG_ACL,
1504                         "<= acl_mask: [%d] applying %s (%s)\n",
1505                         i, accessmask2str( modmask, accessmaskbuf ), 
1506                         b->a_type == ACL_CONTINUE
1507                                 ? "continue"
1508                                 : b->a_type == ACL_BREAK
1509                                         ? "break"
1510                                         : "stop" );
1511 #endif
1512                 /* save old mask */
1513                 oldmask = *mask;
1514
1515                 if( ACL_IS_ADDITIVE(modmask) ) {
1516                         /* add privs */
1517                         ACL_PRIV_SET( *mask, modmask );
1518
1519                         /* cleanup */
1520                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1521
1522                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1523                         /* substract privs */
1524                         ACL_PRIV_CLR( *mask, modmask );
1525
1526                         /* cleanup */
1527                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1528
1529                 } else {
1530                         /* assign privs */
1531                         *mask = modmask;
1532                 }
1533
1534 #ifdef NEW_LOGGING
1535                 LDAP_LOG( ACL, DETAIL1, 
1536                            "acl_mask: conn %lu  [%d] mask: %s\n",
1537                            op->o_connid, i, accessmask2str( *mask, accessmaskbuf)  );
1538 #else
1539                 Debug( LDAP_DEBUG_ACL,
1540                         "<= acl_mask: [%d] mask: %s\n",
1541                         i, accessmask2str(*mask, accessmaskbuf), 0 );
1542 #endif
1543
1544                 if( b->a_type == ACL_CONTINUE ) {
1545                         continue;
1546
1547                 } else if ( b->a_type == ACL_BREAK ) {
1548                         return ACL_BREAK;
1549
1550                 } else {
1551                         return ACL_STOP;
1552                 }
1553         }
1554
1555         /* implicit "by * none" clause */
1556         ACL_INIT(*mask);
1557
1558 #ifdef NEW_LOGGING
1559         LDAP_LOG( ACL, RESULTS, 
1560                    "acl_mask: conn %lu  no more <who> clauses, returning %d (stop)\n",
1561                    op->o_connid, accessmask2str( *mask, accessmaskbuf) , 0 );
1562 #else
1563         Debug( LDAP_DEBUG_ACL,
1564                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1565                 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1566 #endif
1567         return ACL_STOP;
1568 }
1569
1570 /*
1571  * acl_check_modlist - check access control on the given entry to see if
1572  * it allows the given modifications by the user associated with op.
1573  * returns      1       if mods allowed ok
1574  *                      0       mods not allowed
1575  */
1576
1577 int
1578 acl_check_modlist(
1579         Operation       *op,
1580         Entry   *e,
1581         Modifications   *mlist
1582 )
1583 {
1584         struct berval *bv;
1585         AccessControlState state = ACL_STATE_INIT;
1586         Backend *be;
1587         int be_null = 0;
1588         int ret = 1; /* default is access allowed */
1589
1590         be = op->o_bd;
1591         if ( be == NULL ) {
1592                 be = &backends[0];
1593                 be_null = 1;
1594                 op->o_bd = be;
1595         }
1596         assert( be != NULL );
1597
1598         /* short circuit root database access */
1599         if ( be_isroot( op ) ) {
1600 #ifdef NEW_LOGGING
1601                 LDAP_LOG( ACL, DETAIL1, 
1602                            "acl_check_modlist: conn %lu  access granted to root user\n",
1603                            op->o_connid, 0, 0 );
1604 #else
1605                 Debug( LDAP_DEBUG_ACL,
1606                         "<= acl_access_allowed: granted to database root\n",
1607                     0, 0, 0 );
1608 #endif
1609                 goto done;
1610         }
1611
1612         /* use backend default access if no backend acls */
1613         if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1614 #ifdef NEW_LOGGING
1615                 LDAP_LOG( ACL, DETAIL1, 
1616                         "acl_check_modlist: backend default %s access %s to \"%s\"\n",
1617                         access2str( ACL_WRITE ),
1618                         op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", 
1619                         op->o_dn.bv_val  );
1620 #else
1621                 Debug( LDAP_DEBUG_ACL,
1622                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
1623                         access2str( ACL_WRITE ),
1624                         op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1625 #endif
1626                 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1627                 goto done;
1628         }
1629
1630         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1631                 /*
1632                  * no-user-modification operational attributes are ignored
1633                  * by ACL_WRITE checking as any found here are not provided
1634                  * by the user
1635                  */
1636                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1637 #ifdef NEW_LOGGING
1638                         LDAP_LOG( ACL, DETAIL1, 
1639                                    "acl_check_modlist: conn %lu  no-user-mod %s: modify access granted\n",
1640                                    op->o_connid, mlist->sml_desc->ad_cname.bv_val , 0 );
1641 #else
1642                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1643                                 " modify access granted\n",
1644                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1645 #endif
1646                         continue;
1647                 }
1648
1649                 switch ( mlist->sml_op ) {
1650                 case LDAP_MOD_REPLACE:
1651                         /*
1652                          * We must check both permission to delete the whole
1653                          * attribute and permission to add the specific attributes.
1654                          * This prevents abuse from selfwriters.
1655                          */
1656                         if ( ! access_allowed( op, e,
1657                                 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1658                         {
1659                                 ret = 0;
1660                                 goto done;
1661                         }
1662
1663                         if ( mlist->sml_values == NULL ) break;
1664
1665                         /* fall thru to check value to add */
1666
1667                 case LDAP_MOD_ADD:
1668                         assert( mlist->sml_values != NULL );
1669
1670                         for ( bv = mlist->sml_nvalues
1671                                         ? mlist->sml_nvalues : mlist->sml_values;
1672                                 bv->bv_val != NULL; bv++ )
1673                         {
1674                                 if ( ! access_allowed( op, e,
1675                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1676                                 {
1677                                         ret = 0;
1678                                         goto done;
1679                                 }
1680                         }
1681                         break;
1682
1683                 case LDAP_MOD_DELETE:
1684                         if ( mlist->sml_values == NULL ) {
1685                                 if ( ! access_allowed( op, e,
1686                                         mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1687                                 {
1688                                         ret = 0;
1689                                         goto done;
1690                                 }
1691                                 break;
1692                         }
1693                         for ( bv = mlist->sml_nvalues
1694                                         ? mlist->sml_nvalues : mlist->sml_values;
1695                                 bv->bv_val != NULL; bv++ )
1696                         {
1697                                 if ( ! access_allowed( op, e,
1698                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1699                                 {
1700                                         ret = 0;
1701                                         goto done;
1702                                 }
1703                         }
1704                         break;
1705
1706                 case SLAP_MOD_SOFTADD:
1707                         /* allow adding attribute via modrdn thru */
1708                         break;
1709
1710                 default:
1711                         assert( 0 );
1712                         /* not reached */
1713                         ret = 0;
1714                         break;
1715                 }
1716         }
1717
1718 done:
1719         if (be_null) op->o_bd = NULL;
1720         return( ret );
1721 }
1722
1723 static int
1724 aci_get_part(
1725         struct berval *list,
1726         int ix,
1727         char sep,
1728         struct berval *bv )
1729 {
1730         int len;
1731         char *p;
1732
1733         if (bv) {
1734                 BER_BVZERO( bv );
1735         }
1736         len = list->bv_len;
1737         p = list->bv_val;
1738         while (len >= 0 && --ix >= 0) {
1739                 while (--len >= 0 && *p++ != sep) ;
1740         }
1741         while (len >= 0 && *p == ' ') {
1742                 len--;
1743                 p++;
1744         }
1745         if (len < 0)
1746                 return(-1);
1747
1748         if (!bv)
1749                 return(0);
1750
1751         bv->bv_val = p;
1752         while (--len >= 0 && *p != sep) {
1753                 bv->bv_len++;
1754                 p++;
1755         }
1756         while (bv->bv_len > 0 && *--p == ' ')
1757                 bv->bv_len--;
1758         return(bv->bv_len);
1759 }
1760
1761 BerVarray
1762 aci_set_gather (SetCookie *cookie, struct berval *name, struct berval *attr)
1763 {
1764         AciSetCookie *cp = (AciSetCookie *)cookie;
1765         BerVarray bvals = NULL;
1766         struct berval ndn;
1767
1768         /* this routine needs to return the bervals instead of
1769          * plain strings, since syntax is not known.  It should
1770          * also return the syntax or some "comparison cookie".
1771          */
1772
1773         if (dnNormalize(0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx) == LDAP_SUCCESS) {
1774                 const char *text;
1775                 AttributeDescription *desc = NULL;
1776                 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1777                         backend_attribute(cp->op,
1778                                 cp->e, &ndn, desc, &bvals, ACL_NONE);
1779                 }
1780                 sl_free(ndn.bv_val, cp->op->o_tmpmemctx);
1781         }
1782         return(bvals);
1783 }
1784
1785 static int
1786 aci_match_set (
1787         struct berval *subj,
1788         Operation *op,
1789         Entry *e,
1790         int setref
1791 )
1792 {
1793         struct berval set = BER_BVNULL;
1794         int rc = 0;
1795         AciSetCookie cookie;
1796
1797         if (setref == 0) {
1798                 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
1799
1800         } else {
1801                 struct berval subjdn, ndn = BER_BVNULL;
1802                 struct berval setat;
1803                 BerVarray bvals;
1804                 const char *text;
1805                 AttributeDescription *desc = NULL;
1806
1807                 /* format of string is "entry/setAttrName" */
1808                 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1809                         return(0);
1810                 }
1811
1812                 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1813                         setat = aci_bv_set_attr;
1814                 }
1815
1816                 /*
1817                  * NOTE: dnNormalize honors the ber_len field
1818                  * as the length of the dn to be normalized
1819                  */
1820                 if ( slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS ) {
1821                         if ( dnNormalize(0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS )
1822                         {
1823                                 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
1824                                 if ( bvals != NULL && bvals[0].bv_val != NULL ) {
1825                                         int i;
1826                                         set = bvals[0];
1827                                         bvals[0].bv_val = NULL;
1828                                         for (i=1;bvals[i].bv_val;i++);
1829                                         bvals[0].bv_val = bvals[i-1].bv_val;
1830                                         bvals[i-1].bv_val = NULL;
1831                                 }
1832                                 ber_bvarray_free_x(bvals, op->o_tmpmemctx);
1833                                 sl_free(ndn.bv_val, op->o_tmpmemctx);
1834                         }
1835                 }
1836         }
1837
1838         if (set.bv_val != NULL) {
1839                 cookie.op = op;
1840                 cookie.e = e;
1841                 rc = (slap_set_filter(aci_set_gather, (SetCookie *)&cookie, &set,
1842                         &op->o_ndn, &e->e_nname, NULL) > 0);
1843                 sl_free(set.bv_val, op->o_tmpmemctx);
1844         }
1845
1846         return(rc);
1847 }
1848
1849 #ifdef SLAPD_ACI_ENABLED
1850 static int
1851 aci_list_map_rights(
1852         struct berval *list )
1853 {
1854         struct berval bv;
1855         slap_access_t mask;
1856         int i;
1857
1858         ACL_INIT(mask);
1859         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1860                 if (bv.bv_len <= 0)
1861                         continue;
1862                 switch (*bv.bv_val) {
1863                 case 'c':
1864                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1865                         break;
1866                 case 's':
1867                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1868                          * the right 's' to mean "set", but in the examples states
1869                          * that the right 's' means "search".  The latter definition
1870                          * is used here.
1871                          */
1872                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1873                         break;
1874                 case 'r':
1875                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
1876                         break;
1877                 case 'w':
1878                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1879                         break;
1880                 case 'x':
1881                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
1882                          * define any equivalent to the AUTH right, so I've just used
1883                          * 'x' for now.
1884                          */
1885                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1886                         break;
1887                 default:
1888                         break;
1889                 }
1890
1891         }
1892         return(mask);
1893 }
1894
1895 static int
1896 aci_list_has_attr(
1897         struct berval *list,
1898         const struct berval *attr,
1899         struct berval *val )
1900 {
1901         struct berval bv, left, right;
1902         int i;
1903
1904         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1905                 if (aci_get_part(&bv, 0, '=', &left) < 0
1906                         || aci_get_part(&bv, 1, '=', &right) < 0)
1907                 {
1908                         if (ber_bvstrcasecmp(attr, &bv) == 0)
1909                                 return(1);
1910                 } else if (val == NULL) {
1911                         if (ber_bvstrcasecmp(attr, &left) == 0)
1912                                 return(1);
1913                 } else {
1914                         if (ber_bvstrcasecmp(attr, &left) == 0) {
1915                                 /* this is experimental code that implements a
1916                                  * simple (prefix) match of the attribute value.
1917                                  * the ACI draft does not provide for aci's that
1918                                  * apply to specific values, but it would be
1919                                  * nice to have.  If the <attr> part of an aci's
1920                                  * rights list is of the form <attr>=<value>,
1921                                  * that means the aci applies only to attrs with
1922                                  * the given value.  Furthermore, if the attr is
1923                                  * of the form <attr>=<value>*, then <value> is
1924                                  * treated as a prefix, and the aci applies to 
1925                                  * any value with that prefix.
1926                                  *
1927                                  * Ideally, this would allow r.e. matches.
1928                                  */
1929                                 if (aci_get_part(&right, 0, '*', &left) < 0
1930                                         || right.bv_len <= left.bv_len)
1931                                 {
1932                                         if (ber_bvstrcasecmp(val, &right) == 0)
1933                                                 return(1);
1934                                 } else if (val->bv_len >= left.bv_len) {
1935                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1936                                                 return(1);
1937                                 }
1938                         }
1939                 }
1940         }
1941         return(0);
1942 }
1943
1944 static slap_access_t
1945 aci_list_get_attr_rights(
1946         struct berval *list,
1947         const struct berval *attr,
1948         struct berval *val )
1949 {
1950     struct berval bv;
1951     slap_access_t mask;
1952     int i;
1953
1954         /* loop through each rights/attr pair, skip first part (action) */
1955         ACL_INIT(mask);
1956         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1957                 if (aci_list_has_attr(&bv, attr, val) == 0)
1958                         continue;
1959                 if (aci_get_part(list, i, ';', &bv) < 0)
1960                         continue;
1961                 mask |= aci_list_map_rights(&bv);
1962         }
1963         return(mask);
1964 }
1965
1966 static int
1967 aci_list_get_rights(
1968         struct berval *list,
1969         const struct berval *attr,
1970         struct berval *val,
1971         slap_access_t *grant,
1972         slap_access_t *deny )
1973 {
1974     struct berval perm, actn;
1975     slap_access_t *mask;
1976     int i, found;
1977
1978         if (attr == NULL || attr->bv_len == 0 
1979                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1980                 attr = &aci_bv_br_entry;
1981         }
1982
1983         found = 0;
1984         ACL_INIT(*grant);
1985         ACL_INIT(*deny);
1986         /* loop through each permissions clause */
1987         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1988                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1989                         continue;
1990                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1991                         mask = grant;
1992                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1993                         mask = deny;
1994                 } else {
1995                         continue;
1996                 }
1997
1998                 found = 1;
1999                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2000                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2001         }
2002         return(found);
2003 }
2004
2005 static int
2006 aci_group_member (
2007         struct berval *subj,
2008         struct berval *defgrpoc,
2009         struct berval *defgrpat,
2010         Operation               *op,
2011         Entry           *e,
2012         regmatch_t      *matches
2013 )
2014 {
2015         struct berval subjdn;
2016         struct berval grpoc;
2017         struct berval grpat;
2018         ObjectClass *grp_oc = NULL;
2019         AttributeDescription *grp_ad = NULL;
2020         const char *text;
2021         int rc;
2022
2023         /* format of string is "group/objectClassValue/groupAttrName" */
2024         if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2025                 return(0);
2026         }
2027
2028         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2029                 grpoc = *defgrpoc;
2030         }
2031
2032         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2033                 grpat = *defgrpat;
2034         }
2035
2036         rc = slap_bv2ad( &grpat, &grp_ad, &text );
2037         if( rc != LDAP_SUCCESS ) {
2038                 rc = 0;
2039                 goto done;
2040         }
2041         rc = 0;
2042
2043         grp_oc = oc_bvfind( &grpoc );
2044
2045         if (grp_oc != NULL && grp_ad != NULL ) {
2046                 char buf[ACL_BUF_SIZE];
2047                 struct berval bv, ndn;
2048                 bv.bv_len = sizeof( buf ) - 1;
2049                 bv.bv_val = (char *)&buf;
2050                 string_expand(&bv, &subjdn, e->e_ndn, matches);
2051                 if ( dnNormalize(0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
2052                         rc = (backend_group(op, e, &ndn, &op->o_ndn,
2053                                 grp_oc, grp_ad) == 0);
2054                         free( ndn.bv_val );
2055                 }
2056         }
2057
2058 done:
2059         return(rc);
2060 }
2061
2062 static int
2063 aci_mask(
2064     Operation           *op,
2065     Entry                       *e,
2066         AttributeDescription *desc,
2067     struct berval       *val,
2068     struct berval       *aci,
2069         regmatch_t              *matches,
2070         slap_access_t   *grant,
2071         slap_access_t   *deny,
2072         struct berval   *scope
2073 )
2074 {
2075     struct berval bv, perms, sdn;
2076         int rc;
2077                 
2078
2079         assert( desc->ad_cname.bv_val != NULL );
2080
2081         /* parse an aci of the form:
2082                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
2083
2084            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2085            a full description of the format for this attribute.
2086            Differences: "this" in the draft is "self" here, and
2087            "self" and "public" is in the position of dnType.
2088
2089            For now, this routine only supports scope=entry.
2090          */
2091         /* check that the aci has all 5 components */
2092         if (aci_get_part(aci, 4, '#', NULL) < 0)
2093                 return(0);
2094
2095         /* check that the aci family is supported */
2096         if (aci_get_part(aci, 0, '#', &bv) < 0)
2097                 return(0);
2098
2099         /* check that the scope matches */
2100         if (aci_get_part(aci, 1, '#', &bv) < 0
2101                 || ber_bvstrcasecmp( scope, &bv ) != 0)
2102         {
2103                 return(0);
2104         }
2105
2106         /* get the list of permissions clauses, bail if empty */
2107         if (aci_get_part(aci, 2, '#', &perms) <= 0)
2108                 return(0);
2109
2110         /* check if any permissions allow desired access */
2111         if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
2112                 return(0);
2113
2114         /* see if we have a DN match */
2115         if (aci_get_part(aci, 3, '#', &bv) < 0)
2116                 return(0);
2117
2118         /* see if we have a public (i.e. anonymous) access */
2119         if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
2120                 return(1);
2121         }
2122
2123         /* otherwise require an identity */
2124         if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
2125                 return 0;
2126         }
2127
2128         /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
2129          * just grab all the berval up to its end (ITS#3303).
2130          * NOTE: the problem could be solved by providing the DN with
2131          * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would 
2132          * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
2133 #if 0
2134         if (aci_get_part(aci, 4, '#', &sdn) < 0)
2135                 return(0);
2136 #endif
2137         sdn.bv_val = bv.bv_val + bv.bv_len + STRLENOF( "#" );
2138         sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
2139
2140         if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
2141                 struct berval ndn;
2142                 rc = 0;
2143                 if ( dnNormalize(0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
2144                         if (dn_match( &op->o_ndn, &ndn))
2145                                 rc = 1;
2146                         free(ndn.bv_val);
2147                 }
2148                 return (rc);
2149
2150         } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
2151                 if (dn_match(&op->o_ndn, &e->e_nname))
2152                         return(1);
2153
2154         } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
2155                 Attribute *at;
2156                 AttributeDescription *ad = NULL;
2157                 const char *text;
2158
2159                 rc = slap_bv2ad( &sdn, &ad, &text );
2160
2161                 if( rc != LDAP_SUCCESS ) {
2162                         return 0;
2163                 }
2164
2165                 rc = 0;
2166
2167                 bv = op->o_ndn;
2168
2169                 for(at = attrs_find( e->e_attrs, ad );
2170                         at != NULL;
2171                         at = attrs_find( at->a_next, ad ) )
2172                 {
2173                         if (value_find_ex( ad,
2174                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2175                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2176                                 at->a_nvals,
2177                                 &bv, op->o_tmpmemctx) == 0 )
2178                         {
2179                                 rc = 1;
2180                                 break;
2181                         }
2182                 }
2183
2184                 return rc;
2185
2186
2187         } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
2188                 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, op, e, matches))
2189                         return(1);
2190
2191         } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
2192                 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, op, e, matches))
2193                         return(1);
2194
2195         } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
2196                 if (aci_match_set(&sdn, op, e, 0))
2197                         return(1);
2198
2199         } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
2200                 if (aci_match_set(&sdn, op, e, 1))
2201                         return(1);
2202
2203         }
2204
2205         return(0);
2206 }
2207
2208 #endif  /* SLAPD_ACI_ENABLED */
2209
2210 static void
2211 string_expand(
2212         struct berval *bv,
2213         struct berval *pat,
2214         char *match,
2215         regmatch_t *matches)
2216 {
2217         ber_len_t       size;
2218         char   *sp;
2219         char   *dp;
2220         int     flag;
2221
2222         size = 0;
2223         bv->bv_val[0] = '\0';
2224         bv->bv_len--; /* leave space for lone $ */
2225
2226         flag = 0;
2227         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2228                 sp < pat->bv_val + pat->bv_len ; sp++ )
2229         {
2230                 /* did we previously see a $ */
2231                 if ( flag ) {
2232                         if ( flag == 1 && *sp == '$' ) {
2233                                 *dp++ = '$';
2234                                 size++;
2235                                 flag = 0;
2236
2237                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2238                                 flag = 2;
2239
2240                         } else if ( *sp >= '0' && *sp <= '9' ) {
2241                                 int     n;
2242                                 int     i;
2243                                 int     l;
2244
2245                                 n = *sp - '0';
2246
2247                                 if ( flag == 2 ) {
2248                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2249                                                 if ( *sp >= '0' && *sp <= '9' ) {
2250                                                         n = 10*n + ( *sp - '0' );
2251                                                 }
2252                                         }
2253
2254                                         if ( *sp != /*'{'*/ '}' ) {
2255                                                 /* error */
2256                                         }
2257                                 }
2258
2259                                 if ( n >= MAXREMATCHES ) {
2260                                 
2261                                 }
2262                                 
2263                                 *dp = '\0';
2264                                 i = matches[n].rm_so;
2265                                 l = matches[n].rm_eo; 
2266                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2267                                         *dp++ = match[i];
2268                                 }
2269                                 *dp = '\0';
2270
2271                                 flag = 0;
2272                         }
2273                 } else {
2274                         if (*sp == '$') {
2275                                 flag = 1;
2276                         } else {
2277                                 *dp++ = *sp;
2278                                 size++;
2279                         }
2280                 }
2281         }
2282
2283         if ( flag ) {
2284                 /* must have ended with a single $ */
2285                 *dp++ = '$';
2286                 size++;
2287         }
2288
2289         *dp = '\0';
2290         bv->bv_len = size;
2291
2292 #ifdef NEW_LOGGING
2293         LDAP_LOG( ACL, DETAIL1, 
2294            "string_expand:  pattern = %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2295         LDAP_LOG( ACL, DETAIL1, "string_expand:  expanded = %s\n", bv->bv_val, 0, 0 );
2296 #else
2297         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2298         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2299 #endif
2300 }
2301
2302 static int
2303 regex_matches(
2304         struct berval *pat,                     /* pattern to expand and match against */
2305         char *str,                              /* string to match against pattern */
2306         char *buf,                              /* buffer with $N expansion variables */
2307         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
2308 )
2309 {
2310         regex_t re;
2311         char newbuf[ACL_BUF_SIZE];
2312         struct berval bv;
2313         int     rc;
2314
2315         bv.bv_len = sizeof(newbuf) - 1;
2316         bv.bv_val = newbuf;
2317
2318         if(str == NULL) str = "";
2319
2320         string_expand(&bv, pat, buf, matches);
2321         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
2322                 char error[ACL_BUF_SIZE];
2323                 regerror(rc, &re, error, sizeof(error));
2324
2325 #ifdef NEW_LOGGING
2326                 LDAP_LOG( ACL, ERR, 
2327                            "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
2328                            pat->bv_val, str, error  );
2329 #else
2330                 Debug( LDAP_DEBUG_TRACE,
2331                     "compile( \"%s\", \"%s\") failed %s\n",
2332                         pat->bv_val, str, error );
2333 #endif
2334                 return( 0 );
2335         }
2336
2337         rc = regexec(&re, str, 0, NULL, 0);
2338         regfree( &re );
2339
2340 #ifdef NEW_LOGGING
2341         LDAP_LOG( ACL, DETAIL2, "regex_matches: string:   %s\n", str, 0, 0 );
2342         LDAP_LOG( ACL, DETAIL2, "regex_matches: rc:     %d  %s\n",
2343                    rc, rc ? "matches" : "no matches", 0  );
2344 #else
2345         Debug( LDAP_DEBUG_TRACE,
2346             "=> regex_matches: string:   %s\n", str, 0, 0 );
2347         Debug( LDAP_DEBUG_TRACE,
2348             "=> regex_matches: rc: %d %s\n",
2349                 rc, !rc ? "matches" : "no matches", 0 );
2350 #endif
2351         return( !rc );
2352 }
2353