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