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