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