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