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