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