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