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