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