]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
fix possible uninitialized use of nmods
[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 ( bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) ) {
609                                 if ( op->o_ndn.bv_len != 0 ) {
610                                         continue;
611                                 }
612
613                         } else if ( bvmatch( &b->a_dn_pat, &aci_bv_users ) ) {
614                                 if ( op->o_ndn.bv_len == 0 ) {
615                                         continue;
616                                 }
617
618                         } else if ( bvmatch( &b->a_dn_pat, &aci_bv_self ) ) {
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                         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                         /* b->a_group is an unexpanded entry name, expanded it should be an 
931                          * entry with objectclass group* and we test to see if odn is one of
932                          * the values in the attribute group
933                          */
934                         /* see if asker is listed in dnattr */
935                         if ( b->a_group_style == ACL_STYLE_REGEX ) {
936                                 char buf[ACL_BUF_SIZE];
937                                 bv.bv_len = sizeof(buf) - 1;
938                                 bv.bv_val = buf; 
939
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
946                                 bv = ndn;
947
948                         } else {
949                                 bv = b->a_group_pat;
950                         }
951
952                         rc = backend_group( be, conn, op, e, &bv, &op->o_ndn,
953                                 b->a_group_oc, b->a_group_at );
954
955                         if ( ndn.bv_val ) free( ndn.bv_val );
956
957                         if ( rc != 0 ) {
958                                 continue;
959                         }
960                 }
961
962                 if ( b->a_set_pat.bv_len != 0 ) {
963                         if (aci_match_set( &b->a_set_pat, be, e, conn, op, 0 ) == 0) {
964                                 continue;
965                         }
966                 }
967
968                 if ( b->a_authz.sai_ssf ) {
969 #ifdef NEW_LOGGING
970                         LDAP_LOG( ACL, DETAIL1, 
971                                 "acl_mask: conn %lu  check a_authz.sai_ssf: ACL %u > OP %u\n",
972                                 conn->c_connid, b->a_authz.sai_ssf, op->o_ssf  );
973 #else
974                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
975                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
976 #endif
977                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
978                                 continue;
979                         }
980                 }
981
982                 if ( b->a_authz.sai_transport_ssf ) {
983 #ifdef NEW_LOGGING
984                         LDAP_LOG( ACL, DETAIL1, 
985                                 "acl_mask: conn %lu  check a_authz.sai_transport_ssf: "
986                                 "ACL %u > OP %u\n",
987                                 conn->c_connid, b->a_authz.sai_transport_ssf, 
988                                 op->o_transport_ssf  );
989 #else
990                         Debug( LDAP_DEBUG_ACL,
991                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
992                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
993 #endif
994                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
995                                 continue;
996                         }
997                 }
998
999                 if ( b->a_authz.sai_tls_ssf ) {
1000 #ifdef NEW_LOGGING
1001                         LDAP_LOG( ACL, DETAIL1, 
1002                                 "acl_mask: conn %lu  check a_authz.sai_tls_ssf: ACL %u > "
1003                                 "OP %u\n",
1004                                 conn->c_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf  );
1005 #else
1006                         Debug( LDAP_DEBUG_ACL,
1007                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1008                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1009 #endif
1010                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
1011                                 continue;
1012                         }
1013                 }
1014
1015                 if ( b->a_authz.sai_sasl_ssf ) {
1016 #ifdef NEW_LOGGING
1017                         LDAP_LOG( ACL, DETAIL1, 
1018                            "acl_mask: conn %lu check a_authz.sai_sasl_ssf: " 
1019                            "ACL %u > OP %u\n",
1020                                 conn->c_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf );
1021 #else
1022                         Debug( LDAP_DEBUG_ACL,
1023                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1024                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1025 #endif
1026                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
1027                                 continue;
1028                         }
1029                 }
1030
1031 #ifdef SLAPD_ACI_ENABLED
1032                 if ( b->a_aci_at != NULL ) {
1033                         Attribute       *at;
1034                         slap_access_t grant, deny, tgrant, tdeny;
1035
1036                         /* this case works different from the others above.
1037                          * since aci's themselves give permissions, we need
1038                          * to first check b->a_access_mask, the ACL's access level.
1039                          */
1040
1041                         if ( e->e_nname.bv_len == 0 ) {
1042                                 /* no ACIs in the root DSE */
1043                                 continue;
1044                         }
1045
1046                         /* first check if the right being requested
1047                          * is allowed by the ACL clause.
1048                          */
1049                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1050                                 continue;
1051                         }
1052
1053                         /* get the aci attribute */
1054                         at = attr_find( e->e_attrs, b->a_aci_at );
1055                         if ( at == NULL ) {
1056                                 continue;
1057                         }
1058
1059                         ACL_RECORD_VALUE_STATE;
1060
1061                         /* start out with nothing granted, nothing denied */
1062                         ACL_INIT(tgrant);
1063                         ACL_INIT(tdeny);
1064
1065                         /* the aci is an multi-valued attribute.  The
1066                          * rights are determined by OR'ing the individual
1067                          * rights given by the acis.
1068                          */
1069                         for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
1070                                 if (aci_mask( be, conn, op,
1071                                         e, desc, val, &at->a_vals[i],
1072                                         matches, &grant, &deny ) != 0)
1073                                 {
1074                                         tgrant |= grant;
1075                                         tdeny |= deny;
1076                                 }
1077                         }
1078
1079                         /* remove anything that the ACL clause does not allow */
1080                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1081                         tdeny &= ACL_PRIV_MASK;
1082
1083                         /* see if we have anything to contribute */
1084                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
1085                                 continue;
1086                         }
1087
1088                         /* this could be improved by changing acl_mask so that it can deal with
1089                          * by clauses that return grant/deny pairs.  Right now, it does either
1090                          * additive or subtractive rights, but not both at the same time.  So,
1091                          * we need to combine the grant/deny pair into a single rights mask in
1092                          * a smart way:  if either grant or deny is "empty", then we use the
1093                          * opposite as is, otherwise we remove any denied rights from the grant
1094                          * rights mask and construct an additive mask.
1095                          */
1096                         if (ACL_IS_INVALID(tdeny)) {
1097                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
1098
1099                         } else if (ACL_IS_INVALID(tgrant)) {
1100                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1101
1102                         } else {
1103                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1104                         }
1105
1106                 } else
1107 #endif
1108                 {
1109                         modmask = b->a_access_mask;
1110                 }
1111
1112 #ifdef NEW_LOGGING
1113                 LDAP_LOG( ACL, RESULTS, 
1114                            "acl_mask: [%d] applying %s (%s)\n",
1115                            i, accessmask2str( modmask, accessmaskbuf),
1116                            b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
1117                            ? "break" : "stop"  );
1118 #else
1119                 Debug( LDAP_DEBUG_ACL,
1120                         "<= acl_mask: [%d] applying %s (%s)\n",
1121                         i, accessmask2str( modmask, accessmaskbuf ), 
1122                         b->a_type == ACL_CONTINUE
1123                                 ? "continue"
1124                                 : b->a_type == ACL_BREAK
1125                                         ? "break"
1126                                         : "stop" );
1127 #endif
1128                 /* save old mask */
1129                 oldmask = *mask;
1130
1131                 if( ACL_IS_ADDITIVE(modmask) ) {
1132                         /* add privs */
1133                         ACL_PRIV_SET( *mask, modmask );
1134
1135                         /* cleanup */
1136                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1137
1138                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1139                         /* substract privs */
1140                         ACL_PRIV_CLR( *mask, modmask );
1141
1142                         /* cleanup */
1143                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1144
1145                 } else {
1146                         /* assign privs */
1147                         *mask = modmask;
1148                 }
1149
1150 #ifdef NEW_LOGGING
1151                 LDAP_LOG( ACL, DETAIL1, 
1152                            "acl_mask: conn %lu  [%d] mask: %s\n",
1153                            conn->c_connid, i, accessmask2str( *mask, accessmaskbuf)  );
1154 #else
1155                 Debug( LDAP_DEBUG_ACL,
1156                         "<= acl_mask: [%d] mask: %s\n",
1157                         i, accessmask2str(*mask, accessmaskbuf), 0 );
1158 #endif
1159
1160                 if( b->a_type == ACL_CONTINUE ) {
1161                         continue;
1162
1163                 } else if ( b->a_type == ACL_BREAK ) {
1164                         return ACL_BREAK;
1165
1166                 } else {
1167                         return ACL_STOP;
1168                 }
1169         }
1170
1171         /* implicit "by * none" clause */
1172         ACL_INIT(*mask);
1173
1174 #ifdef NEW_LOGGING
1175         LDAP_LOG( ACL, RESULTS, 
1176                    "acl_mask: conn %lu  no more <who> clauses, returning %d (stop)\n",
1177                    conn->c_connid, accessmask2str( *mask, accessmaskbuf) , 0 );
1178 #else
1179         Debug( LDAP_DEBUG_ACL,
1180                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1181                 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1182 #endif
1183         return ACL_STOP;
1184 }
1185
1186 /*
1187  * acl_check_modlist - check access control on the given entry to see if
1188  * it allows the given modifications by the user associated with op.
1189  * returns      1       if mods allowed ok
1190  *                      0       mods not allowed
1191  */
1192
1193 int
1194 acl_check_modlist(
1195     Backend     *be,
1196     Connection  *conn,
1197     Operation   *op,
1198     Entry       *e,
1199     Modifications       *mlist
1200 )
1201 {
1202         struct berval *bv;
1203
1204         assert( be != NULL );
1205
1206         /* short circuit root database access */
1207         if ( be_isroot( be, &op->o_ndn ) ) {
1208 #ifdef NEW_LOGGING
1209                 LDAP_LOG( ACL, DETAIL1, 
1210                            "acl_check_modlist: conn %lu  access granted to root user\n",
1211                            conn->c_connid, 0, 0 );
1212 #else
1213                 Debug( LDAP_DEBUG_ACL,
1214                         "<= acl_access_allowed: granted to database root\n",
1215                     0, 0, 0 );
1216 #endif
1217                 return 1;
1218         }
1219
1220         /* use backend default access if no backend acls */
1221         if( be != NULL && be->be_acl == NULL ) {
1222 #ifdef NEW_LOGGING
1223                 LDAP_LOG( ACL, DETAIL1, 
1224                         "acl_check_modlist: backend default %s access %s to \"%s\"\n",
1225                         access2str( ACL_WRITE ),
1226                         be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", 
1227                         op->o_dn.bv_val  );
1228 #else
1229                 Debug( LDAP_DEBUG_ACL,
1230                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
1231                         access2str( ACL_WRITE ),
1232                         be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1233 #endif
1234                 return be->be_dfltaccess >= ACL_WRITE;
1235
1236 #ifdef notdef
1237         /* be is always non-NULL */
1238         /* use global default access if no global acls */
1239         } else if ( be == NULL && global_acl == NULL ) {
1240 #ifdef NEW_LOGGING
1241                 LDAP_LOG( ACL, DETAIL1, 
1242                         "acl_check_modlist: global default %s access %s to \"%s\"\n",
1243                    access2str( ACL_WRITE ),
1244                    global_default_access >= ACL_WRITE ? "granted" : "denied", 
1245                    op->o_dn  );
1246 #else
1247                 Debug( LDAP_DEBUG_ACL,
1248                         "=> access_allowed: global default %s access %s to \"%s\"\n",
1249                         access2str( ACL_WRITE ),
1250                         global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1251 #endif
1252                 return global_default_access >= ACL_WRITE;
1253 #endif
1254         }
1255
1256         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1257                 static AccessControlState state_init = ACL_STATE_INIT;
1258                 AccessControlState state;
1259
1260                 /*
1261                  * no-user-modification operational attributes are ignored
1262                  * by ACL_WRITE checking as any found here are not provided
1263                  * by the user
1264                  */
1265                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1266 #ifdef NEW_LOGGING
1267                         LDAP_LOG( ACL, DETAIL1, 
1268                                    "acl_check_modlist: conn %lu  no-user-mod %s: modify access granted\n",
1269                                    conn->c_connid, mlist->sml_desc->ad_cname.bv_val , 0 );
1270 #else
1271                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1272                                 " modify access granted\n",
1273                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1274 #endif
1275                         continue;
1276                 }
1277
1278                 state = state_init;
1279
1280                 switch ( mlist->sml_op ) {
1281                 case LDAP_MOD_REPLACE:
1282                         /*
1283                          * We must check both permission to delete the whole
1284                          * attribute and permission to add the specific attributes.
1285                          * This prevents abuse from selfwriters.
1286                          */
1287                         if ( ! access_allowed( be, conn, op, e,
1288                                 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1289                         {
1290                                 return( 0 );
1291                         }
1292
1293                         if ( mlist->sml_bvalues == NULL ) break;
1294
1295                         /* fall thru to check value to add */
1296
1297                 case LDAP_MOD_ADD:
1298                         assert( mlist->sml_bvalues != NULL );
1299
1300                         for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ ) {
1301                                 if ( ! access_allowed( be, conn, op, e,
1302                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1303                                 {
1304                                         return( 0 );
1305                                 }
1306                         }
1307                         break;
1308
1309                 case LDAP_MOD_DELETE:
1310                         if ( mlist->sml_bvalues == NULL ) {
1311                                 if ( ! access_allowed( be, conn, op, e,
1312                                         mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1313                                 {
1314                                         return( 0 );
1315                                 }
1316                                 break;
1317                         }
1318                         for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ ) {
1319                                 if ( ! access_allowed( be, conn, op, e,
1320                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1321                                 {
1322                                         return( 0 );
1323                                 }
1324                         }
1325                         break;
1326
1327                 case SLAP_MOD_SOFTADD:
1328                         /* allow adding attribute via modrdn thru */
1329                         break;
1330
1331                 default:
1332                         assert( 0 );
1333                         return( 0 );
1334                 }
1335         }
1336
1337         return( 1 );
1338 }
1339
1340 static int
1341 aci_get_part(
1342         struct berval *list,
1343         int ix,
1344         char sep,
1345         struct berval *bv )
1346 {
1347         int len;
1348         char *p;
1349
1350         if (bv) {
1351                 bv->bv_len = 0;
1352                 bv->bv_val = NULL;
1353         }
1354         len = list->bv_len;
1355         p = list->bv_val;
1356         while (len >= 0 && --ix >= 0) {
1357                 while (--len >= 0 && *p++ != sep) ;
1358         }
1359         while (len >= 0 && *p == ' ') {
1360                 len--;
1361                 p++;
1362         }
1363         if (len < 0)
1364                 return(-1);
1365
1366         if (!bv)
1367                 return(0);
1368
1369         bv->bv_val = p;
1370         while (--len >= 0 && *p != sep) {
1371                 bv->bv_len++;
1372                 p++;
1373         }
1374         while (bv->bv_len > 0 && *--p == ' ')
1375                 bv->bv_len--;
1376         return(bv->bv_len);
1377 }
1378
1379 BerVarray
1380 aci_set_gather (void *cookie, struct berval *name, struct berval *attr)
1381 {
1382         AciSetCookie *cp = cookie;
1383         BerVarray bvals = NULL;
1384         struct berval ndn;
1385
1386         /* this routine needs to return the bervals instead of
1387          * plain strings, since syntax is not known.  It should
1388          * also return the syntax or some "comparison cookie".
1389          */
1390
1391         if (dnNormalize2(NULL, name, &ndn) == LDAP_SUCCESS) {
1392                 const char *text;
1393                 AttributeDescription *desc = NULL;
1394                 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1395                         backend_attribute(cp->be, NULL, cp->op,
1396                                 cp->e, &ndn, desc, &bvals);
1397                 }
1398                 free(ndn.bv_val);
1399         }
1400         return(bvals);
1401 }
1402
1403 static int
1404 aci_match_set (
1405         struct berval *subj,
1406     Backend *be,
1407     Entry *e,
1408     Connection *conn,
1409     Operation *op,
1410     int setref
1411 )
1412 {
1413         struct berval set = { 0, NULL };
1414         int rc = 0;
1415         AciSetCookie cookie;
1416
1417         if (setref == 0) {
1418                 ber_dupbv( &set, subj );
1419         } else {
1420                 struct berval subjdn, ndn = { 0, NULL };
1421                 struct berval setat;
1422                 BerVarray bvals;
1423                 const char *text;
1424                 AttributeDescription *desc = NULL;
1425
1426                 /* format of string is "entry/setAttrName" */
1427                 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1428                         return(0);
1429                 }
1430
1431                 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1432                         setat.bv_val = SLAPD_ACI_SET_ATTR;
1433                         setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1434                 }
1435
1436                 if ( setat.bv_val != NULL ) {
1437                         /*
1438                          * NOTE: dnNormalize2 honors the ber_len field
1439                          * as the length of the dn to be normalized
1440                          */
1441                         if ( dnNormalize2(NULL, &subjdn, &ndn) == LDAP_SUCCESS
1442                                 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1443                         {
1444                                 backend_attribute(be, NULL, op, e,
1445                                         &ndn, desc, &bvals);
1446                                 if ( bvals != NULL ) {
1447                                         if ( bvals[0].bv_val != NULL ) {
1448                                                 int i;
1449                                                 set = bvals[0];
1450                                                 bvals[0].bv_val = NULL;
1451                                                 for (i=1;bvals[i].bv_val;i++);
1452                                                 bvals[0].bv_val = bvals[i-1].bv_val;
1453                                                 bvals[i-1].bv_val = NULL;
1454                                         }
1455                                         ber_bvarray_free(bvals);
1456                                 }
1457                         }
1458                         if (ndn.bv_val)
1459                                 free(ndn.bv_val);
1460                 }
1461         }
1462
1463         if (set.bv_val != NULL) {
1464                 cookie.be = be;
1465                 cookie.e = e;
1466                 cookie.conn = conn;
1467                 cookie.op = op;
1468                 rc = (slap_set_filter(aci_set_gather, &cookie, &set,
1469                         &op->o_ndn, &e->e_nname, NULL) > 0);
1470                 ch_free(set.bv_val);
1471         }
1472         return(rc);
1473 }
1474
1475 #ifdef SLAPD_ACI_ENABLED
1476 static int
1477 aci_list_map_rights(
1478         struct berval *list )
1479 {
1480         struct berval bv;
1481         slap_access_t mask;
1482         int i;
1483
1484         ACL_INIT(mask);
1485         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1486                 if (bv.bv_len <= 0)
1487                         continue;
1488                 switch (*bv.bv_val) {
1489                 case 'c':
1490                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1491                         break;
1492                 case 's':
1493                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1494                          * the right 's' to mean "set", but in the examples states
1495                          * that the right 's' means "search".  The latter definition
1496                          * is used here.
1497                          */
1498                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1499                         break;
1500                 case 'r':
1501                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
1502                         break;
1503                 case 'w':
1504                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1505                         break;
1506                 case 'x':
1507                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
1508                          * define any equivalent to the AUTH right, so I've just used
1509                          * 'x' for now.
1510                          */
1511                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1512                         break;
1513                 default:
1514                         break;
1515                 }
1516
1517         }
1518         return(mask);
1519 }
1520
1521 static int
1522 aci_list_has_attr(
1523         struct berval *list,
1524         const struct berval *attr,
1525         struct berval *val )
1526 {
1527         struct berval bv, left, right;
1528         int i;
1529
1530         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1531                 if (aci_get_part(&bv, 0, '=', &left) < 0
1532                         || aci_get_part(&bv, 1, '=', &right) < 0)
1533                 {
1534                         if (ber_bvstrcasecmp(attr, &bv) == 0)
1535                                 return(1);
1536                 } else if (val == NULL) {
1537                         if (ber_bvstrcasecmp(attr, &left) == 0)
1538                                 return(1);
1539                 } else {
1540                         if (ber_bvstrcasecmp(attr, &left) == 0) {
1541                                 /* this is experimental code that implements a
1542                                  * simple (prefix) match of the attribute value.
1543                                  * the ACI draft does not provide for aci's that
1544                                  * apply to specific values, but it would be
1545                                  * nice to have.  If the <attr> part of an aci's
1546                                  * rights list is of the form <attr>=<value>,
1547                                  * that means the aci applies only to attrs with
1548                                  * the given value.  Furthermore, if the attr is
1549                                  * of the form <attr>=<value>*, then <value> is
1550                                  * treated as a prefix, and the aci applies to 
1551                                  * any value with that prefix.
1552                                  *
1553                                  * Ideally, this would allow r.e. matches.
1554                                  */
1555                                 if (aci_get_part(&right, 0, '*', &left) < 0
1556                                         || right.bv_len <= left.bv_len)
1557                                 {
1558                                         if (ber_bvstrcasecmp(val, &right) == 0)
1559                                                 return(1);
1560                                 } else if (val->bv_len >= left.bv_len) {
1561                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1562                                                 return(1);
1563                                 }
1564                         }
1565                 }
1566         }
1567         return(0);
1568 }
1569
1570 static slap_access_t
1571 aci_list_get_attr_rights(
1572         struct berval *list,
1573         const struct berval *attr,
1574         struct berval *val )
1575 {
1576     struct berval bv;
1577     slap_access_t mask;
1578     int i;
1579
1580         /* loop through each rights/attr pair, skip first part (action) */
1581         ACL_INIT(mask);
1582         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1583                 if (aci_list_has_attr(&bv, attr, val) == 0)
1584                         continue;
1585                 if (aci_get_part(list, i, ';', &bv) < 0)
1586                         continue;
1587                 mask |= aci_list_map_rights(&bv);
1588         }
1589         return(mask);
1590 }
1591
1592 static int
1593 aci_list_get_rights(
1594         struct berval *list,
1595         const struct berval *attr,
1596         struct berval *val,
1597         slap_access_t *grant,
1598         slap_access_t *deny )
1599 {
1600     struct berval perm, actn;
1601     slap_access_t *mask;
1602     int i, found;
1603
1604         if (attr == NULL || attr->bv_len == 0 
1605                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1606                 attr = &aci_bv_br_entry;
1607         }
1608
1609         found = 0;
1610         ACL_INIT(*grant);
1611         ACL_INIT(*deny);
1612         /* loop through each permissions clause */
1613         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1614                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1615                         continue;
1616                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1617                         mask = grant;
1618                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1619                         mask = deny;
1620                 } else {
1621                         continue;
1622                 }
1623
1624                 found = 1;
1625                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1626                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1627         }
1628         return(found);
1629 }
1630
1631 static int
1632 aci_group_member (
1633         struct berval *subj,
1634         struct berval *defgrpoc,
1635         struct berval *defgrpat,
1636     Backend             *be,
1637     Entry               *e,
1638     Connection          *conn,
1639     Operation           *op,
1640         regmatch_t      *matches
1641 )
1642 {
1643         struct berval subjdn;
1644         struct berval grpoc;
1645         struct berval grpat;
1646         ObjectClass *grp_oc = NULL;
1647         AttributeDescription *grp_ad = NULL;
1648         const char *text;
1649         int rc;
1650
1651         /* format of string is "group/objectClassValue/groupAttrName" */
1652         if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1653                 return(0);
1654         }
1655
1656         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1657                 grpoc = *defgrpoc;
1658         }
1659
1660         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1661                 grpat = *defgrpat;
1662         }
1663
1664         rc = slap_bv2ad( &grpat, &grp_ad, &text );
1665         if( rc != LDAP_SUCCESS ) {
1666                 rc = 0;
1667                 goto done;
1668         }
1669         rc = 0;
1670
1671         grp_oc = oc_bvfind( &grpoc );
1672
1673         if (grp_oc != NULL && grp_ad != NULL ) {
1674                 char buf[ACL_BUF_SIZE];
1675                 struct berval bv, ndn;
1676                 bv.bv_len = sizeof( buf ) - 1;
1677                 bv.bv_val = (char *)&buf;
1678                 string_expand(&bv, &subjdn, e->e_ndn, matches);
1679                 if ( dnNormalize2(NULL, &bv, &ndn) == LDAP_SUCCESS ) {
1680                         rc = (backend_group(be, conn, op, e, &ndn, &op->o_ndn,
1681                                 grp_oc, grp_ad) == 0);
1682                         free( ndn.bv_val );
1683                 }
1684         }
1685
1686 done:
1687         return(rc);
1688 }
1689
1690 static int
1691 aci_mask(
1692     Backend                     *be,
1693     Connection          *conn,
1694     Operation           *op,
1695     Entry                       *e,
1696         AttributeDescription *desc,
1697     struct berval       *val,
1698     struct berval       *aci,
1699         regmatch_t              *matches,
1700         slap_access_t   *grant,
1701         slap_access_t   *deny
1702 )
1703 {
1704     struct berval bv, perms, sdn;
1705         int rc;
1706                 
1707
1708         assert( desc->ad_cname.bv_val != NULL );
1709
1710         /* parse an aci of the form:
1711                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1712
1713            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1714            a full description of the format for this attribute.
1715            Differences: "this" in the draft is "self" here, and
1716            "self" and "public" is in the position of dnType.
1717
1718            For now, this routine only supports scope=entry.
1719          */
1720
1721         /* check that the aci has all 5 components */
1722         if (aci_get_part(aci, 4, '#', NULL) < 0)
1723                 return(0);
1724
1725         /* check that the aci family is supported */
1726         if (aci_get_part(aci, 0, '#', &bv) < 0)
1727                 return(0);
1728
1729         /* check that the scope is "entry" */
1730         if (aci_get_part(aci, 1, '#', &bv) < 0
1731                 || ber_bvstrcasecmp( &aci_bv_entry, &bv ) != 0)
1732         {
1733                 return(0);
1734         }
1735
1736         /* get the list of permissions clauses, bail if empty */
1737         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1738                 return(0);
1739
1740         /* check if any permissions allow desired access */
1741         if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
1742                 return(0);
1743
1744         /* see if we have a DN match */
1745         if (aci_get_part(aci, 3, '#', &bv) < 0)
1746                 return(0);
1747
1748         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1749                 return(0);
1750
1751         if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
1752                 struct berval ndn;
1753                 rc = 0;
1754                 if ( dnNormalize2(NULL, &sdn, &ndn) == LDAP_SUCCESS ) {
1755                         if (dn_match( &op->o_ndn, &ndn))
1756                                 rc = 1;
1757                         free(ndn.bv_val);
1758                 }
1759                 return (rc);
1760
1761         } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
1762                 return(1);
1763
1764         } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
1765                 if (dn_match(&op->o_ndn, &e->e_nname))
1766                         return(1);
1767
1768         } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
1769                 Attribute *at;
1770                 AttributeDescription *ad = NULL;
1771                 const char *text;
1772
1773                 rc = slap_bv2ad( &sdn, &ad, &text );
1774
1775                 if( rc != LDAP_SUCCESS ) {
1776                         return 0;
1777                 }
1778
1779                 rc = 0;
1780
1781                 bv = op->o_ndn;
1782
1783                 for(at = attrs_find( e->e_attrs, ad );
1784                         at != NULL;
1785                         at = attrs_find( at->a_next, ad ) )
1786                 {
1787                         if (value_find( ad, at->a_vals, &bv) == 0 ) {
1788                                 rc = 1;
1789                                 break;
1790                         }
1791                 }
1792
1793                 return rc;
1794
1795
1796         } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
1797                 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, be, e, conn, op, matches))
1798                         return(1);
1799
1800         } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
1801                 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, be, e, conn, op, matches))
1802                         return(1);
1803
1804         } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
1805                 if (aci_match_set(&sdn, be, e, conn, op, 0))
1806                         return(1);
1807
1808         } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
1809                 if (aci_match_set(&sdn, be, e, conn, op, 1))
1810                         return(1);
1811
1812         }
1813
1814         return(0);
1815 }
1816
1817 #endif  /* SLAPD_ACI_ENABLED */
1818
1819 static void
1820 string_expand(
1821         struct berval *bv,
1822         struct berval *pat,
1823         char *match,
1824         regmatch_t *matches)
1825 {
1826         ber_len_t       size;
1827         char   *sp;
1828         char   *dp;
1829         int     flag;
1830
1831         size = 0;
1832         bv->bv_val[0] = '\0';
1833         bv->bv_len--; /* leave space for lone $ */
1834
1835         flag = 0;
1836         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
1837                 sp < pat->bv_val + pat->bv_len ; sp++ )
1838         {
1839                 /* did we previously see a $ */
1840                 if ( flag ) {
1841                         if ( flag == 1 && *sp == '$' ) {
1842                                 *dp++ = '$';
1843                                 size++;
1844                                 flag = 0;
1845
1846                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
1847                                 flag = 2;
1848
1849                         } else if ( *sp >= '0' && *sp <= '9' ) {
1850                                 int     n;
1851                                 int     i;
1852                                 int     l;
1853
1854                                 n = *sp - '0';
1855
1856                                 if ( flag == 2 ) {
1857                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
1858                                                 if ( *sp >= '0' && *sp <= '9' ) {
1859                                                         n = 10*n + ( *sp - '0' );
1860                                                 }
1861                                         }
1862
1863                                         if ( *sp != /*'{'*/ '}' ) {
1864                                                 /* error */
1865                                         }
1866                                 }
1867
1868                                 if ( n >= MAXREMATCHES ) {
1869                                 
1870                                 }
1871                                 
1872                                 *dp = '\0';
1873                                 i = matches[n].rm_so;
1874                                 l = matches[n].rm_eo; 
1875                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
1876                                         *dp++ = match[i];
1877                                 }
1878                                 *dp = '\0';
1879
1880                                 flag = 0;
1881                         }
1882                 } else {
1883                         if (*sp == '$') {
1884                                 flag = 1;
1885                         } else {
1886                                 *dp++ = *sp;
1887                                 size++;
1888                         }
1889                 }
1890         }
1891
1892         if ( flag ) {
1893                 /* must have ended with a single $ */
1894                 *dp++ = '$';
1895                 size++;
1896         }
1897
1898         *dp = '\0';
1899         bv->bv_len = size;
1900
1901 #ifdef NEW_LOGGING
1902         LDAP_LOG( ACL, DETAIL1, 
1903            "string_expand:  pattern = %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
1904         LDAP_LOG( ACL, DETAIL1, "string_expand:  expanded = %s\n", bv->bv_val, 0, 0 );
1905 #else
1906         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
1907         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
1908 #endif
1909 }
1910
1911 static int
1912 regex_matches(
1913         struct berval *pat,                     /* pattern to expand and match against */
1914         char *str,                              /* string to match against pattern */
1915         char *buf,                              /* buffer with $N expansion variables */
1916         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
1917 )
1918 {
1919         regex_t re;
1920         char newbuf[ACL_BUF_SIZE];
1921         struct berval bv;
1922         int     rc;
1923
1924         bv.bv_len = sizeof(newbuf) - 1;
1925         bv.bv_val = newbuf;
1926
1927         if(str == NULL) str = "";
1928
1929         string_expand(&bv, pat, buf, matches);
1930         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1931                 char error[ACL_BUF_SIZE];
1932                 regerror(rc, &re, error, sizeof(error));
1933
1934 #ifdef NEW_LOGGING
1935                 LDAP_LOG( ACL, ERR, 
1936                            "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
1937                            pat->bv_val, str, error  );
1938 #else
1939                 Debug( LDAP_DEBUG_TRACE,
1940                     "compile( \"%s\", \"%s\") failed %s\n",
1941                         pat->bv_val, str, error );
1942 #endif
1943                 return( 0 );
1944         }
1945
1946         rc = regexec(&re, str, 0, NULL, 0);
1947         regfree( &re );
1948
1949 #ifdef NEW_LOGGING
1950         LDAP_LOG( ACL, DETAIL2, "regex_matches: string:   %s\n", str, 0, 0 );
1951         LDAP_LOG( ACL, DETAIL2, "regex_matches: rc:     %d  %s\n",
1952                    rc, rc ? "matches" : "no matches", 0  );
1953 #else
1954         Debug( LDAP_DEBUG_TRACE,
1955             "=> regex_matches: string:   %s\n", str, 0, 0 );
1956         Debug( LDAP_DEBUG_TRACE,
1957             "=> regex_matches: rc: %d %s\n",
1958                 rc, !rc ? "matches" : "no matches", 0 );
1959 #endif
1960         return( !rc );
1961 }
1962