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