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