]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
4e7a8e2cdf1a2ca986d8f61d18a406c1e9ad7c0f
[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 %d %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 %d 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 %d 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 %d 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 %d 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 %d 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 %d     \"%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 %d  no more rules\n", conn->c_connid ));
310 #else
311                 Debug( LDAP_DEBUG_ACL,
312                         "=> access_allowed: no more rules\n", 0, 0, 0);
313 #endif
314
315                 goto done;
316         }
317
318 #ifdef NEW_LOGGING
319         LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
320                 "access_allowed: conn %d  %s access %s by %s\n",
321                 conn->c_connid,
322                 access2str( access ),
323                 ACL_GRANT( mask, access ) ? "granted" : "denied",
324                 accessmask2str( mask, accessmaskbuf ) ));
325 #else
326         Debug( LDAP_DEBUG_ACL,
327                 "=> access_allowed: %s access %s by %s\n",
328                 access2str( access ),
329                 ACL_GRANT(mask, access) ? "granted" : "denied",
330                 accessmask2str( mask, accessmaskbuf ) );
331 #endif
332
333         ret = ACL_GRANT(mask, access);
334
335 done:
336         if( state != NULL ) {
337                 state->as_recorded |= ACL_STATE_RECORDED;
338                 state->as_result = ret;
339         }
340         return ret;
341 }
342
343 /*
344  * acl_get - return the acl applicable to entry e, attribute
345  * attr.  the acl returned is suitable for use in subsequent calls to
346  * acl_access_allowed().
347  */
348
349 static AccessControl *
350 acl_get(
351         AccessControl *a,
352         int                     *count,
353     Backend             *be,
354     Operation   *op,
355     Entry               *e,
356         AttributeDescription *desc,
357     int                 nmatch,
358     regmatch_t  *matches )
359 {
360         const char *attr;
361         int dnlen, patlen;
362
363         assert( e != NULL );
364         assert( count != NULL );
365         assert( desc != NULL );
366
367         attr = desc->ad_cname.bv_val;
368
369         assert( attr != NULL );
370
371         if( a == NULL ) {
372                 if( be == NULL ) {
373                         a = global_acl;
374                 } else {
375                         a = be->be_acl;
376                 }
377
378                 assert( a != NULL );
379
380         } else {
381                 a = a->acl_next;
382         }
383
384         dnlen = e->e_nname.bv_len;
385
386         for ( ; a != NULL; a = a->acl_next ) {
387                 (*count) ++;
388
389                 if (a->acl_dn_pat.bv_len != 0) {
390                         if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
391 #ifdef NEW_LOGGING
392                                 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
393                                            "acl_get: dnpat [%d] %s nsub: %d\n",
394                                            *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub ));
395 #else
396                                 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n", 
397                                         *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
398 #endif
399                                 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
400                                         continue;
401
402                         } else {
403 #ifdef NEW_LOGGING
404                                 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
405                                            "acl_get: dn [%d] %s\n",
406                                            *count, a->acl_dn_pat.bv_val ));
407 #else
408                                 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n", 
409                                         *count, a->acl_dn_pat.bv_val, 0 );
410 #endif
411                                 patlen = a->acl_dn_pat.bv_len;
412                                 if ( dnlen < patlen )
413                                         continue;
414
415                                 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
416                                         /* base dn -- entire object DN must match */
417                                         if ( dnlen != patlen )
418                                                 continue;
419
420                                 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
421                                         int rdnlen = -1;
422
423                                         if ( dnlen <= patlen )
424                                                 continue;
425
426                                         if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
427                                                 continue;
428
429                                         rdnlen = dn_rdnlen( NULL, &e->e_nname );
430                                         if ( rdnlen != dnlen - patlen - 1 )
431                                                 continue;
432
433                                 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
434                                         if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
435                                                 continue;
436
437                                 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
438                                         if ( dnlen <= patlen )
439                                                 continue;
440                                         if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
441                                                 continue;
442                                 }
443
444                                 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
445                                         continue;
446                         }
447
448 #ifdef NEW_LOGGING
449                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
450                                    "acl_get: [%d] matched\n",
451                                    *count ));
452 #else
453                         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
454                                 *count, 0, 0 );
455 #endif
456                 }
457
458                 if ( a->acl_filter != NULL ) {
459                         ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
460                         if ( rc != LDAP_COMPARE_TRUE ) {
461                                 continue;
462                         }
463                 }
464
465 #ifdef NEW_LOGGING
466                 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
467                            "acl_get: [%d] check attr %s\n",
468                            *count, attr ));
469 #else
470                 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
471                        *count, attr, 0);
472 #endif
473                 if ( attr == NULL || a->acl_attrs == NULL ||
474                         ad_inlist( desc, a->acl_attrs ) )
475                 {
476 #ifdef NEW_LOGGING
477                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
478                                    "acl_get:  [%d] acl %s attr: %s\n",
479                                    *count, e->e_dn, attr ));
480 #else
481                         Debug( LDAP_DEBUG_ACL,
482                                 "<= acl_get: [%d] acl %s attr: %s\n",
483                                 *count, e->e_dn, attr );
484 #endif
485                         return a;
486                 }
487                 matches[0].rm_so = matches[0].rm_eo = -1;
488         }
489
490 #ifdef NEW_LOGGING
491         LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
492                    "acl_get: done.\n" ));
493 #else
494         Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
495 #endif
496         return( NULL );
497 }
498
499 /*
500  * Record value-dependent access control state
501  */
502 #define ACL_RECORD_VALUE_STATE do { \
503                 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
504                         state->as_recorded |= ACL_STATE_RECORDED_VD; \
505                         state->as_vd_acl = a; \
506                         AC_MEMCPY( state->as_vd_acl_matches, matches, \
507                                 sizeof( state->as_vd_acl_matches )) ; \
508                         state->as_vd_acl_count = count; \
509                         state->as_vd_access = b; \
510                         state->as_vd_access_count = i; \
511                 } \
512         } while( 0 )
513
514 /*
515  * acl_mask - modifies mask based upon the given acl and the
516  * requested access to entry e, attribute attr, value val.  if val
517  * is null, access to the whole attribute is assumed (all values).
518  *
519  * returns      0       access NOT allowed
520  *              1       access allowed
521  */
522
523 static slap_control_t
524 acl_mask(
525     AccessControl       *a,
526         slap_mask_t *mask,
527     Backend             *be,
528     Connection  *conn,
529     Operation   *op,
530     Entry               *e,
531         AttributeDescription *desc,
532     struct berval       *val,
533         regmatch_t      *matches,
534         int     count,
535         AccessControlState *state )
536 {
537         int             i, odnlen, patlen;
538         int             vd_recorded = 0;
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 %d  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 %d  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, '*' ) == 0 ) {
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 %d  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, '*' ) != 0) {
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 %d  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, '*' ) != 0) {
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 %d  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, '*' ) != 0) {
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 %d  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, '*' ) != 0) {
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 && op->o_ndn.bv_len != 0 ) {
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 #ifdef NEW_LOGGING
781                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
782                                    "acl_mask: conn %d  check a_dn_pat: %s\n",
783                                    conn->c_connid, attr ));
784 #else
785                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
786                                 attr, 0, 0);
787 #endif
788                         bv = op->o_ndn;
789
790                         /* see if asker is listed in dnattr */
791                         for( at = attrs_find( e->e_attrs, b->a_dn_at );
792                                 at != NULL;
793                                 at = attrs_find( at->a_next, b->a_dn_at ) )
794                         {
795                                 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
796                                         /* found it */
797                                         match = 1;
798                                         break;
799                                 }
800                         }
801
802                         if( match ) {
803                                 /* have a dnattr match. if this is a self clause then
804                                  * the target must also match the op dn.
805                                  */
806                                 if ( b->a_dn_self ) {
807                                         /* check if the target is an attribute. */
808                                         if ( val == NULL )
809                                                 continue;
810                                         /* target is attribute, check if the attribute value
811                                          * is the op dn.
812                                          */
813                                         rc = value_match( &match, b->a_dn_at,
814                                                 b->a_dn_at->ad_type->sat_equality, 0,
815                                                 val, &bv, &text );
816                                         /* on match error or no match, fail the ACL clause */
817                                         if (rc != LDAP_SUCCESS || match != 0 )
818                                                 continue;
819                                 }
820                         } else {
821                                 /* no dnattr match, check if this is a self clause */
822                                 if ( ! b->a_dn_self )
823                                         continue;
824
825                                 ACL_RECORD_VALUE_STATE;
826                                 
827                                 /* this is a self clause, check if the target is an
828                                  * attribute.
829                                  */
830                                 if ( val == NULL )
831                                         continue;
832
833                                 /* target is attribute, check if the attribute value
834                                  * is the op dn.
835                                  */
836                                 rc = value_match( &match, b->a_dn_at,
837                                         b->a_dn_at->ad_type->sat_equality, 0,
838                                         val, &bv, &text );
839
840                                 /* on match error or no match, fail the ACL clause */
841                                 if (rc != LDAP_SUCCESS || match != 0 )
842                                         continue;
843                         }
844                 }
845
846                 if ( b->a_group_pat.bv_len && op->o_ndn.bv_len ) {
847                         char buf[1024];
848                         struct berval bv = { sizeof(buf) - 1, buf };
849                         struct berval ndn = { 0, NULL };
850                         int rc;
851
852                         /* b->a_group is an unexpanded entry name, expanded it should be an 
853                          * entry with objectclass group* and we test to see if odn is one of
854                          * the values in the attribute group
855                          */
856                         /* see if asker is listed in dnattr */
857                         if ( b->a_group_style == ACL_STYLE_REGEX ) {
858                                 string_expand(&bv, &b->a_group_pat, e->e_ndn, matches);
859                                 if ( dnNormalize2(NULL, &bv, &ndn) != LDAP_SUCCESS ) {
860                                         /* did not expand to a valid dn */
861                                         continue;
862                                 }
863                                 bv = ndn;
864                         } else {
865                                 bv = b->a_group_pat;
866                         }
867
868                         rc = backend_group(be, conn, op, e, &bv, &op->o_ndn,
869                                 b->a_group_oc, b->a_group_at);
870                         if ( ndn.bv_val )
871                                 free( ndn.bv_val );
872                         if ( rc != 0 ) {
873                                 continue;
874                         }
875                 }
876
877                 if ( b->a_set_pat.bv_len != 0 ) {
878                         if (aci_match_set( &b->a_set_pat, be, e, conn, op, 0 ) == 0) {
879                                 continue;
880                         }
881                 }
882
883                 if ( b->a_authz.sai_ssf ) {
884 #ifdef NEW_LOGGING
885                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
886                                    "acl_mask: conn %d  check a_authz.sai_ssf: ACL %u > OP %u\n",
887                                    conn->c_connid, b->a_authz.sai_ssf, op->o_ssf ));
888 #else
889                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
890                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
891 #endif
892                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
893                                 continue;
894                         }
895                 }
896
897                 if ( b->a_authz.sai_transport_ssf ) {
898 #ifdef NEW_LOGGING
899                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
900                                    "acl_mask: conn %d  check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
901                                    conn->c_connid, b->a_authz.sai_transport_ssf, op->o_transport_ssf ));
902 #else
903                         Debug( LDAP_DEBUG_ACL,
904                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
905                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
906 #endif
907                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
908                                 continue;
909                         }
910                 }
911
912                 if ( b->a_authz.sai_tls_ssf ) {
913 #ifdef NEW_LOGGING
914                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
915                                    "acl_mask: conn %d  check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
916                                    conn->c_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf ));
917 #else
918                         Debug( LDAP_DEBUG_ACL,
919                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
920                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
921 #endif
922                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
923                                 continue;
924                         }
925                 }
926
927                 if ( b->a_authz.sai_sasl_ssf ) {
928 #ifdef NEW_LOGGING
929                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
930                                    "acl_mask: conn %d check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
931                                    conn->c_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf ));
932 #else
933                         Debug( LDAP_DEBUG_ACL,
934                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
935                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
936 #endif
937                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
938                                 continue;
939                         }
940                 }
941
942 #ifdef SLAPD_ACI_ENABLED
943                 if ( b->a_aci_at != NULL ) {
944                         Attribute       *at;
945                         slap_access_t grant, deny, tgrant, tdeny;
946
947                         /* this case works different from the others above.
948                          * since aci's themselves give permissions, we need
949                          * to first check b->a_access_mask, the ACL's access level.
950                          */
951
952                         if ( e->e_nname.bv_len == 0 ) {
953                                 /* no ACIs in the root DSE */
954                                 continue;
955                         }
956
957                         /* first check if the right being requested
958                          * is allowed by the ACL clause.
959                          */
960                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
961                                 continue;
962                         }
963
964                         /* get the aci attribute */
965                         at = attr_find( e->e_attrs, b->a_aci_at );
966                         if ( at == NULL ) {
967                                 continue;
968                         }
969
970                         ACL_RECORD_VALUE_STATE;
971
972                         /* start out with nothing granted, nothing denied */
973                         ACL_INIT(tgrant);
974                         ACL_INIT(tdeny);
975
976                         /* the aci is an multi-valued attribute.  The
977                          * rights are determined by OR'ing the individual
978                          * rights given by the acis.
979                          */
980                         for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
981                                 if (aci_mask( be, conn, op,
982                                         e, desc, val, &at->a_vals[i],
983                                         matches, &grant, &deny ) != 0)
984                                 {
985                                         tgrant |= grant;
986                                         tdeny |= deny;
987                                 }
988                         }
989
990                         /* remove anything that the ACL clause does not allow */
991                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
992                         tdeny &= ACL_PRIV_MASK;
993
994                         /* see if we have anything to contribute */
995                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
996                                 continue;
997                         }
998
999                         /* this could be improved by changing acl_mask so that it can deal with
1000                          * by clauses that return grant/deny pairs.  Right now, it does either
1001                          * additive or subtractive rights, but not both at the same time.  So,
1002                          * we need to combine the grant/deny pair into a single rights mask in
1003                          * a smart way:  if either grant or deny is "empty", then we use the
1004                          * opposite as is, otherwise we remove any denied rights from the grant
1005                          * rights mask and construct an additive mask.
1006                          */
1007                         if (ACL_IS_INVALID(tdeny)) {
1008                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
1009
1010                         } else if (ACL_IS_INVALID(tgrant)) {
1011                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1012
1013                         } else {
1014                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1015                         }
1016
1017                 } else
1018 #endif
1019                 {
1020                         modmask = b->a_access_mask;
1021                 }
1022
1023 #ifdef NEW_LOGGING
1024                 LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
1025                            "acl_mask: conn %d  [%d] applying %s (%s)\n",
1026                            conn->c_connid, i, accessmask2str( modmask, accessmaskbuf),
1027                            b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
1028                            ? "break" : "stop" ));
1029 #else
1030                 Debug( LDAP_DEBUG_ACL,
1031                         "<= acl_mask: [%d] applying %s (%s)\n",
1032                         i, accessmask2str( modmask, accessmaskbuf ), 
1033                         b->a_type == ACL_CONTINUE
1034                                 ? "continue"
1035                                 : b->a_type == ACL_BREAK
1036                                         ? "break"
1037                                         : "stop" );
1038 #endif
1039                 /* save old mask */
1040                 oldmask = *mask;
1041
1042                 if( ACL_IS_ADDITIVE(modmask) ) {
1043                         /* add privs */
1044                         ACL_PRIV_SET( *mask, modmask );
1045
1046                         /* cleanup */
1047                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1048
1049                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1050                         /* substract privs */
1051                         ACL_PRIV_CLR( *mask, modmask );
1052
1053                         /* cleanup */
1054                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1055
1056                 } else {
1057                         /* assign privs */
1058                         *mask = modmask;
1059                 }
1060
1061 #ifdef NEW_LOGGING
1062                 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1063                            "acl_mask: conn %d  [%d] mask: %s\n",
1064                            conn->c_connid, i, accessmask2str( *mask, accessmaskbuf) ));
1065 #else
1066                 Debug( LDAP_DEBUG_ACL,
1067                         "<= acl_mask: [%d] mask: %s\n",
1068                         i, accessmask2str(*mask, accessmaskbuf), 0 );
1069 #endif
1070
1071                 if( b->a_type == ACL_CONTINUE ) {
1072                         continue;
1073
1074                 } else if ( b->a_type == ACL_BREAK ) {
1075                         return ACL_BREAK;
1076
1077                 } else {
1078                         return ACL_STOP;
1079                 }
1080         }
1081
1082         /* implicit "by * none" clause */
1083         ACL_INIT(*mask);
1084
1085 #ifdef NEW_LOGGING
1086         LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
1087                    "acl_mask: conn %d  no more <who> clauses, returning %d (stop)\n",
1088                    conn->c_connid, accessmask2str( *mask, accessmaskbuf) ));
1089 #else
1090         Debug( LDAP_DEBUG_ACL,
1091                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1092                 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1093 #endif
1094         return ACL_STOP;
1095 }
1096
1097 /*
1098  * acl_check_modlist - check access control on the given entry to see if
1099  * it allows the given modifications by the user associated with op.
1100  * returns      1       if mods allowed ok
1101  *                      0       mods not allowed
1102  */
1103
1104 int
1105 acl_check_modlist(
1106     Backend     *be,
1107     Connection  *conn,
1108     Operation   *op,
1109     Entry       *e,
1110     Modifications       *mlist
1111 )
1112 {
1113         struct berval *bv;
1114
1115         assert( be != NULL );
1116
1117         /* short circuit root database access */
1118         if ( be_isroot( be, &op->o_ndn ) ) {
1119 #ifdef NEW_LOGGING
1120                 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
1121                            "acl_check_modlist: conn %d  access granted to root user\n",
1122                            conn->c_connid ));
1123 #else
1124                 Debug( LDAP_DEBUG_ACL,
1125                         "<= acl_access_allowed: granted to database root\n",
1126                     0, 0, 0 );
1127 #endif
1128                 return 1;
1129         }
1130
1131         /* use backend default access if no backend acls */
1132         if( be != NULL && be->be_acl == NULL ) {
1133 #ifdef NEW_LOGGING
1134                 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1135                            "acl_check_modlist: conn %d  backend default %s access %s to \"%s\"\n",
1136                            conn->c_connid, access2str( ACL_WRITE ),
1137                            be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val ));
1138 #else
1139                 Debug( LDAP_DEBUG_ACL,
1140                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
1141                         access2str( ACL_WRITE ),
1142                         be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1143 #endif
1144                 return be->be_dfltaccess >= ACL_WRITE;
1145
1146 #ifdef notdef
1147         /* be is always non-NULL */
1148         /* use global default access if no global acls */
1149         } else if ( be == NULL && global_acl == NULL ) {
1150 #ifdef NEW_LOGGING
1151                 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1152                            "acl_check_modlist: conn %d  global default %s access %s to \"%s\"\n",
1153                            conn->c_connid, access2str( ACL_WRITE ),
1154                            global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn ));
1155 #else
1156                 Debug( LDAP_DEBUG_ACL,
1157                         "=> access_allowed: global default %s access %s to \"%s\"\n",
1158                         access2str( ACL_WRITE ),
1159                         global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1160 #endif
1161                 return global_default_access >= ACL_WRITE;
1162 #endif
1163         }
1164
1165         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1166                 static AccessControlState state_init = ACL_STATE_INIT;
1167                 AccessControlState state;
1168
1169                 /*
1170                  * no-user-modification operational attributes are ignored
1171                  * by ACL_WRITE checking as any found here are not provided
1172                  * by the user
1173                  */
1174                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1175 #ifdef NEW_LOGGING
1176                         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1177                                    "acl_check_modlist: conn %d  no-user-mod %s: modify access granted\n",
1178                                    conn->c_connid, mlist->sml_desc->ad_cname.bv_val ));
1179 #else
1180                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1181                                 " modify access granted\n",
1182                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1183 #endif
1184                         continue;
1185                 }
1186
1187                 state = state_init;
1188
1189                 switch ( mlist->sml_op ) {
1190                 case LDAP_MOD_REPLACE:
1191                         /*
1192                          * We must check both permission to delete the whole
1193                          * attribute and permission to add the specific attributes.
1194                          * This prevents abuse from selfwriters.
1195                          */
1196                         if ( ! access_allowed( be, conn, op, e,
1197                                 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1198                         {
1199                                 return( 0 );
1200                         }
1201
1202                         if ( mlist->sml_bvalues == NULL ) break;
1203
1204                         /* fall thru to check value to add */
1205
1206                 case LDAP_MOD_ADD:
1207                         assert( mlist->sml_bvalues != NULL );
1208
1209                         for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ ) {
1210                                 if ( ! access_allowed( be, conn, op, e,
1211                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1212                                 {
1213                                         return( 0 );
1214                                 }
1215                         }
1216                         break;
1217
1218                 case LDAP_MOD_DELETE:
1219                         if ( mlist->sml_bvalues == NULL ) {
1220                                 if ( ! access_allowed( be, conn, op, e,
1221                                         mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1222                                 {
1223                                         return( 0 );
1224                                 }
1225                                 break;
1226                         }
1227                         for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ ) {
1228                                 if ( ! access_allowed( be, conn, op, e,
1229                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1230                                 {
1231                                         return( 0 );
1232                                 }
1233                         }
1234                         break;
1235
1236                 case SLAP_MOD_SOFTADD:
1237                         /* allow adding attribute via modrdn thru */
1238                         break;
1239
1240                 default:
1241                         assert( 0 );
1242                         return( 0 );
1243                 }
1244         }
1245
1246         return( 1 );
1247 }
1248
1249 static char *
1250 aci_bvstrdup( struct berval *bv )
1251 {
1252         char *s;
1253
1254         s = (char *)ch_malloc(bv->bv_len + 1);
1255         if (s != NULL) {
1256                 AC_MEMCPY(s, bv->bv_val, bv->bv_len);
1257                 s[bv->bv_len] = 0;
1258         }
1259         return(s);
1260 }
1261
1262 static int
1263 aci_get_part(
1264         struct berval *list,
1265         int ix,
1266         char sep,
1267         struct berval *bv )
1268 {
1269         int len;
1270         char *p;
1271
1272         if (bv) {
1273                 bv->bv_len = 0;
1274                 bv->bv_val = NULL;
1275         }
1276         len = list->bv_len;
1277         p = list->bv_val;
1278         while (len >= 0 && --ix >= 0) {
1279                 while (--len >= 0 && *p++ != sep) ;
1280         }
1281         while (len >= 0 && *p == ' ') {
1282                 len--;
1283                 p++;
1284         }
1285         if (len < 0)
1286                 return(-1);
1287
1288         if (!bv)
1289                 return(0);
1290
1291         bv->bv_val = p;
1292         while (--len >= 0 && *p != sep) {
1293                 bv->bv_len++;
1294                 p++;
1295         }
1296         while (bv->bv_len > 0 && *--p == ' ')
1297                 bv->bv_len--;
1298         return(bv->bv_len);
1299 }
1300
1301 BerVarray
1302 aci_set_gather (void *cookie, struct berval *name, struct berval *attr)
1303 {
1304         AciSetCookie *cp = cookie;
1305         BerVarray bvals = NULL;
1306         struct berval ndn;
1307
1308         /* this routine needs to return the bervals instead of
1309          * plain strings, since syntax is not known.  It should
1310          * also return the syntax or some "comparison cookie".
1311          */
1312
1313         if (dnNormalize2(NULL, name, &ndn) == LDAP_SUCCESS) {
1314                 const char *text;
1315                 AttributeDescription *desc = NULL;
1316                 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1317                         backend_attribute(cp->be, NULL, NULL,
1318                                 cp->e, &ndn, desc, &bvals);
1319                 }
1320                 free(ndn.bv_val);
1321         }
1322         return(bvals);
1323 }
1324
1325 static int
1326 aci_match_set (
1327         struct berval *subj,
1328     Backend *be,
1329     Entry *e,
1330     Connection *conn,
1331     Operation *op,
1332     int setref
1333 )
1334 {
1335         struct berval set = { 0, NULL };
1336         int rc = 0;
1337         AciSetCookie cookie;
1338
1339         if (setref == 0) {
1340                 ber_dupbv( &set, subj );
1341         } else {
1342                 struct berval subjdn, ndn = { 0, NULL };
1343                 struct berval setat;
1344                 BerVarray bvals;
1345                 const char *text;
1346                 AttributeDescription *desc = NULL;
1347
1348                 /* format of string is "entry/setAttrName" */
1349                 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1350                         return(0);
1351                 }
1352
1353                 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1354                         setat.bv_val = SLAPD_ACI_SET_ATTR;
1355                         setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1356                 }
1357
1358                 if ( setat.bv_val != NULL ) {
1359                         /*
1360                          * NOTE: dnNormalize2 honors the ber_len field
1361                          * as the length of the dn to be normalized
1362                          */
1363                         if ( dnNormalize2(NULL, &subjdn, &ndn) == LDAP_SUCCESS
1364                                 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1365                         {
1366                                 backend_attribute(be, NULL, NULL, e,
1367                                         &ndn, desc, &bvals);
1368                                 if ( bvals != NULL ) {
1369                                         if ( bvals[0].bv_val != NULL ) {
1370                                                 int i;
1371                                                 set = bvals[0];
1372                                                 bvals[0].bv_val = NULL;
1373                                                 for (i=1;bvals[i].bv_val;i++);
1374                                                 bvals[0].bv_val = bvals[i-1].bv_val;
1375                                                 bvals[i-1].bv_val = NULL;
1376                                         }
1377                                         ber_bvarray_free(bvals);
1378                                 }
1379                         }
1380                         if (ndn.bv_val)
1381                                 free(ndn.bv_val);
1382                 }
1383         }
1384
1385         if (set.bv_val != NULL) {
1386                 cookie.be = be;
1387                 cookie.e = e;
1388                 cookie.conn = conn;
1389                 cookie.op = op;
1390                 rc = (slap_set_filter(aci_set_gather, &cookie, &set,
1391                         &op->o_ndn, &e->e_nname, NULL) > 0);
1392                 ch_free(set.bv_val);
1393         }
1394         return(rc);
1395 }
1396
1397 #ifdef SLAPD_ACI_ENABLED
1398 static int
1399 aci_list_map_rights(
1400         struct berval *list )
1401 {
1402         struct berval bv;
1403         slap_access_t mask;
1404         int i;
1405
1406         ACL_INIT(mask);
1407         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1408                 if (bv.bv_len <= 0)
1409                         continue;
1410                 switch (*bv.bv_val) {
1411                 case 'c':
1412                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1413                         break;
1414                 case 's':
1415                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1416                          * the right 's' to mean "set", but in the examples states
1417                          * that the right 's' means "search".  The latter definition
1418                          * is used here.
1419                          */
1420                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1421                         break;
1422                 case 'r':
1423                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
1424                         break;
1425                 case 'w':
1426                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1427                         break;
1428                 case 'x':
1429                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
1430                          * define any equivalent to the AUTH right, so I've just used
1431                          * 'x' for now.
1432                          */
1433                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1434                         break;
1435                 default:
1436                         break;
1437                 }
1438
1439         }
1440         return(mask);
1441 }
1442
1443 static int
1444 aci_list_has_attr(
1445         struct berval *list,
1446         const struct berval *attr,
1447         struct berval *val )
1448 {
1449         struct berval bv, left, right;
1450         int i;
1451
1452         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1453                 if (aci_get_part(&bv, 0, '=', &left) < 0
1454                         || aci_get_part(&bv, 1, '=', &right) < 0)
1455                 {
1456                         if (ber_bvstrcasecmp(attr, &bv) == 0)
1457                                 return(1);
1458                 } else if (val == NULL) {
1459                         if (ber_bvstrcasecmp(attr, &left) == 0)
1460                                 return(1);
1461                 } else {
1462                         if (ber_bvstrcasecmp(attr, &left) == 0) {
1463                                 /* this is experimental code that implements a
1464                                  * simple (prefix) match of the attribute value.
1465                                  * the ACI draft does not provide for aci's that
1466                                  * apply to specific values, but it would be
1467                                  * nice to have.  If the <attr> part of an aci's
1468                                  * rights list is of the form <attr>=<value>,
1469                                  * that means the aci applies only to attrs with
1470                                  * the given value.  Furthermore, if the attr is
1471                                  * of the form <attr>=<value>*, then <value> is
1472                                  * treated as a prefix, and the aci applies to 
1473                                  * any value with that prefix.
1474                                  *
1475                                  * Ideally, this would allow r.e. matches.
1476                                  */
1477                                 if (aci_get_part(&right, 0, '*', &left) < 0
1478                                         || right.bv_len <= left.bv_len)
1479                                 {
1480                                         if (ber_bvstrcasecmp(val, &right) == 0)
1481                                                 return(1);
1482                                 } else if (val->bv_len >= left.bv_len) {
1483                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1484                                                 return(1);
1485                                 }
1486                         }
1487                 }
1488         }
1489         return(0);
1490 }
1491
1492 static slap_access_t
1493 aci_list_get_attr_rights(
1494         struct berval *list,
1495         const struct berval *attr,
1496         struct berval *val )
1497 {
1498     struct berval bv;
1499     slap_access_t mask;
1500     int i;
1501
1502         /* loop through each rights/attr pair, skip first part (action) */
1503         ACL_INIT(mask);
1504         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1505                 if (aci_list_has_attr(&bv, attr, val) == 0)
1506                         continue;
1507                 if (aci_get_part(list, i, ';', &bv) < 0)
1508                         continue;
1509                 mask |= aci_list_map_rights(&bv);
1510         }
1511         return(mask);
1512 }
1513
1514 static int
1515 aci_list_get_rights(
1516         struct berval *list,
1517         const struct berval *attr,
1518         struct berval *val,
1519         slap_access_t *grant,
1520         slap_access_t *deny )
1521 {
1522     struct berval perm, actn;
1523     slap_access_t *mask;
1524     int i, found;
1525
1526         if (attr == NULL || attr->bv_len == 0 
1527                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1528                 attr = &aci_bv_br_entry;
1529         }
1530
1531         found = 0;
1532         ACL_INIT(*grant);
1533         ACL_INIT(*deny);
1534         /* loop through each permissions clause */
1535         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1536                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1537                         continue;
1538                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1539                         mask = grant;
1540                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1541                         mask = deny;
1542                 } else {
1543                         continue;
1544                 }
1545
1546                 found = 1;
1547                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1548                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1549         }
1550         return(found);
1551 }
1552
1553 static int
1554 aci_group_member (
1555         struct berval *subj,
1556         struct berval *defgrpoc,
1557         struct berval *defgrpat,
1558     Backend             *be,
1559     Entry               *e,
1560     Connection          *conn,
1561     Operation           *op,
1562         regmatch_t      *matches
1563 )
1564 {
1565         struct berval bv;
1566         struct berval subjdn;
1567         struct berval grpoc;
1568         struct berval grpat;
1569         ObjectClass *grp_oc = NULL;
1570         AttributeDescription *grp_ad = NULL;
1571         const char *text;
1572         int rc;
1573
1574         /* format of string is "group/objectClassValue/groupAttrName" */
1575         if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1576                 return(0);
1577         }
1578
1579         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1580                 grpoc = *defgrpoc;
1581         }
1582
1583         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1584                 grpat = *defgrpat;
1585         }
1586
1587         rc = slap_bv2ad( &grpat, &grp_ad, &text );
1588         if( rc != LDAP_SUCCESS ) {
1589                 rc = 0;
1590                 goto done;
1591         }
1592         rc = 0;
1593
1594         grp_oc = oc_bvfind( &grpoc );
1595
1596         if (grp_oc != NULL && grp_ad != NULL ) {
1597                 struct berval ndn;
1598                 bv.bv_val = (char *)ch_malloc(1024);
1599                 bv.bv_len = 1024;
1600                 string_expand(&bv, &subjdn, e->e_ndn, matches);
1601                 if ( dnNormalize2(NULL, &bv, &ndn) == LDAP_SUCCESS ) {
1602                         rc = (backend_group(be, conn, op, e, &ndn, &op->o_ndn, grp_oc, grp_ad) == 0);
1603                         free( ndn.bv_val );
1604                 }
1605                 ch_free(bv.bv_val);
1606         }
1607
1608 done:
1609         return(rc);
1610 }
1611
1612 static struct berval GroupClass = {
1613         sizeof(SLAPD_GROUP_CLASS)-1, SLAPD_GROUP_CLASS };
1614 static struct berval GroupAttr = {
1615         sizeof(SLAPD_GROUP_ATTR)-1, SLAPD_GROUP_ATTR };
1616 static struct berval RoleClass = {
1617         sizeof(SLAPD_ROLE_CLASS)-1, SLAPD_ROLE_CLASS };
1618 static struct berval RoleAttr = {
1619         sizeof(SLAPD_ROLE_ATTR)-1, SLAPD_ROLE_ATTR };
1620
1621 static int
1622 aci_mask(
1623     Backend                     *be,
1624     Connection          *conn,
1625     Operation           *op,
1626     Entry                       *e,
1627         AttributeDescription *desc,
1628     struct berval       *val,
1629     struct berval       *aci,
1630         regmatch_t              *matches,
1631         slap_access_t   *grant,
1632         slap_access_t   *deny
1633 )
1634 {
1635     struct berval bv, perms, sdn;
1636         int rc;
1637                 
1638
1639         assert( desc->ad_cname.bv_val != NULL );
1640
1641         /* parse an aci of the form:
1642                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1643
1644            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1645            a full description of the format for this attribute.
1646
1647            For now, this routine only supports scope=entry.
1648          */
1649
1650         /* check that the aci has all 5 components */
1651         if (aci_get_part(aci, 4, '#', NULL) < 0)
1652                 return(0);
1653
1654         /* check that the aci family is supported */
1655         if (aci_get_part(aci, 0, '#', &bv) < 0)
1656                 return(0);
1657
1658         /* check that the scope is "entry" */
1659         if (aci_get_part(aci, 1, '#', &bv) < 0
1660                 || ber_bvstrcasecmp( &aci_bv_entry, &bv ) != 0)
1661         {
1662                 return(0);
1663         }
1664
1665         /* get the list of permissions clauses, bail if empty */
1666         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1667                 return(0);
1668
1669         /* check if any permissions allow desired access */
1670         if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
1671                 return(0);
1672
1673         /* see if we have a DN match */
1674         if (aci_get_part(aci, 3, '#', &bv) < 0)
1675                 return(0);
1676
1677         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1678                 return(0);
1679
1680         if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
1681                 struct berval ndn;
1682                 rc = 1;
1683                 if ( dnNormalize2(NULL, &sdn, &ndn) == LDAP_SUCCESS ) {
1684                         if (!dn_match( &op->o_ndn, &ndn))
1685                                 rc = 0;
1686                         free(ndn.bv_val);
1687                 }
1688                 return(rc);
1689         }
1690
1691         if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
1692                 if (dn_match(&op->o_ndn, &e->e_nname))
1693                         return(1);
1694
1695         } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
1696                 Attribute *at;
1697                 AttributeDescription *ad = NULL;
1698                 const char *text;
1699
1700                 rc = slap_bv2ad( &sdn, &ad, &text );
1701
1702                 if( rc != LDAP_SUCCESS ) {
1703                         return 0;
1704                 }
1705
1706                 rc = 0;
1707
1708                 bv = op->o_ndn;
1709
1710                 for(at = attrs_find( e->e_attrs, ad );
1711                         at != NULL;
1712                         at = attrs_find( at->a_next, ad ) )
1713                 {
1714                         if (value_find( ad, at->a_vals, &bv) == 0 ) {
1715                                 rc = 1;
1716                                 break;
1717                         }
1718                 }
1719
1720                 return rc;
1721
1722
1723         } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
1724                 if (aci_group_member(&sdn, &GroupClass, &GroupAttr, be, e, conn, op, matches))
1725                         return(1);
1726
1727         } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
1728                 if (aci_group_member(&sdn, &RoleClass, &RoleAttr, be, e, conn, op, matches))
1729                         return(1);
1730
1731         } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
1732                 if (aci_match_set(&sdn, be, e, conn, op, 0))
1733                         return(1);
1734
1735         } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
1736                 if (aci_match_set(&sdn, be, e, conn, op, 1))
1737                         return(1);
1738
1739         }
1740
1741         return(0);
1742 }
1743
1744 #endif  /* SLAPD_ACI_ENABLED */
1745
1746 static void
1747 string_expand(
1748         struct berval *bv,
1749         struct berval *pat,
1750         char *match,
1751         regmatch_t *matches)
1752 {
1753         ber_len_t       size;
1754         char   *sp;
1755         char   *dp;
1756         int     flag;
1757
1758         size = 0;
1759         bv->bv_val[0] = '\0';
1760         bv->bv_len--; /* leave space for lone $ */
1761
1762         flag = 0;
1763         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
1764                 sp < pat->bv_val + pat->bv_len ; sp++) {
1765                 /* did we previously see a $ */
1766                 if (flag) {
1767                         if (*sp == '$') {
1768                                 *dp++ = '$';
1769                                 size++;
1770                         } else if (*sp >= '0' && *sp <= '9' ) {
1771                                 int     n;
1772                                 int     i;
1773                                 int     l;
1774
1775                                 n = *sp - '0';
1776                                 *dp = '\0';
1777                                 i = matches[n].rm_so;
1778                                 l = matches[n].rm_eo; 
1779                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
1780                                         *dp++ = match[i];
1781                                         size++;
1782                                 }
1783                                 *dp = '\0';
1784                         }
1785                         flag = 0;
1786                 } else {
1787                         if (*sp == '$') {
1788                                 flag = 1;
1789                         } else {
1790                                 *dp++ = *sp;
1791                                 size++;
1792                         }
1793                 }
1794         }
1795
1796         if (flag) {
1797                 /* must have ended with a single $ */
1798                 *dp++ = '$';
1799                 size++;
1800         }
1801
1802         *dp = '\0';
1803         bv->bv_len = size;
1804
1805 #ifdef NEW_LOGGING
1806         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1807                    "string_expand:  pattern = %.*s\n", pat->bv_len, pat->bv_val ));
1808         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1809                    "string_expand:  expanded = %s\n", bv->bv_val ));
1810 #else
1811         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %.*s\n", pat->bv_len, pat->bv_val, 0 );
1812         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
1813 #endif
1814 }
1815
1816 static int
1817 regex_matches(
1818         struct berval *pat,                     /* pattern to expand and match against */
1819         char *str,                              /* string to match against pattern */
1820         char *buf,                              /* buffer with $N expansion variables */
1821         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
1822 )
1823 {
1824         regex_t re;
1825         char newbuf[512];
1826         struct berval bv = {sizeof(newbuf), newbuf};
1827         int     rc;
1828
1829         if(str == NULL) str = "";
1830
1831         string_expand(&bv, pat, buf, matches);
1832         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1833                 char error[512];
1834                 regerror(rc, &re, error, sizeof(error));
1835
1836 #ifdef NEW_LOGGING
1837                 LDAP_LOG(( "aci", LDAP_LEVEL_ERR,
1838                            "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
1839                            pat->bv_val, str, error ));
1840 #else
1841                 Debug( LDAP_DEBUG_TRACE,
1842                     "compile( \"%s\", \"%s\") failed %s\n",
1843                         pat->bv_val, str, error );
1844 #endif
1845                 return( 0 );
1846         }
1847
1848         rc = regexec(&re, str, 0, NULL, 0);
1849         regfree( &re );
1850
1851 #ifdef NEW_LOGGING
1852         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1853                    "regex_matches: string:   %s\n", str ));
1854         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1855                    "regex_matches: rc:  %d  %s\n",
1856                    rc, rc ? "matches" : "no matches" ));
1857 #else
1858         Debug( LDAP_DEBUG_TRACE,
1859             "=> regex_matches: string:   %s\n", str, 0, 0 );
1860         Debug( LDAP_DEBUG_TRACE,
1861             "=> regex_matches: rc: %d %s\n",
1862                 rc, !rc ? "matches" : "no matches", 0 );
1863 #endif
1864         return( !rc );
1865 }
1866