]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
Fix is_object_subclass and implicitly add 'top' to structural
[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
18 static AccessControl * acl_get(
19         AccessControl *ac, int *count,
20         Backend *be, Operation *op,
21         Entry *e,
22         AttributeDescription *desc,
23         int nmatches, regmatch_t *matches );
24
25 static slap_control_t acl_mask(
26         AccessControl *ac, slap_access_mask_t *mask,
27         Backend *be, Connection *conn, Operation *op,
28         Entry *e,
29         AttributeDescription *desc,
30         struct berval *val,
31         regmatch_t *matches );
32
33 #ifdef SLAPD_ACI_ENABLED
34 static int aci_mask(
35         Backend *be,
36     Connection *conn,
37         Operation *op,
38         Entry *e,
39         AttributeDescription *desc,
40         struct berval *val,
41         struct berval *aci,
42         regmatch_t *matches,
43         slap_access_t *grant,
44         slap_access_t *deny );
45 #endif
46
47 static int      regex_matches(
48         char *pat, char *str, char *buf, regmatch_t *matches);
49 static void     string_expand(
50         char *newbuf, int bufsiz, char *pattern,
51         char *match, regmatch_t *matches);
52
53
54 /*
55  * access_allowed - check whether op->o_ndn is allowed the requested access
56  * to entry e, attribute attr, value val.  if val is null, access to
57  * the whole attribute is assumed (all values).
58  *
59  * This routine loops through all access controls and calls
60  * acl_mask() on each applicable access control.
61  * The loop exits when a definitive answer is reached or
62  * or no more controls remain.
63  *
64  * returns:
65  *              0       access denied
66  *              1       access granted
67  */
68
69 int
70 access_allowed(
71     Backend             *be,
72     Connection          *conn,
73     Operation           *op,
74     Entry               *e,
75         AttributeDescription    *desc,
76     struct berval       *val,
77     slap_access_t       access )
78 {
79         int                             count;
80         AccessControl   *a;
81 #ifdef LDAP_DEBUG
82         char accessmaskbuf[ACCESSMASK_MAXLEN];
83 #endif
84         slap_access_mask_t mask;
85         slap_control_t control;
86
87         const char *attr = desc ? desc->ad_cname->bv_val : NULL;
88
89         regmatch_t       matches[MAXREMATCHES];
90
91         Debug( LDAP_DEBUG_ACL,
92                 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
93             access2str( access ),
94                 e->e_dn, attr );
95
96         assert( be != NULL );
97         assert( e != NULL );
98         assert( attr != NULL );
99         assert( access > ACL_NONE );
100
101         /* grant database root access */
102         if ( be != NULL && be_isroot( be, op->o_ndn ) ) {
103                 Debug( LDAP_DEBUG_ACL,
104                     "<= root access granted\n",
105                         0, 0, 0 );
106                 return 1;
107         }
108
109         /*
110          * no-user-modification operational attributes are ignored
111          * by ACL_WRITE checking as any found here are not provided
112          * by the user
113          */
114         if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type ) )
115         {
116                 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
117                         " %s access granted\n",
118                         attr, 0, 0 );
119                 return 1;
120         }
121
122         /* use backend default access if no backend acls */
123         if( be != NULL && be->be_acl == NULL ) {
124                 Debug( LDAP_DEBUG_ACL,
125                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
126                         access2str( access ),
127                         be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn );
128
129                 return be->be_dfltaccess >= access;
130
131 #ifdef notdef
132         /* be is always non-NULL */
133         /* use global default access if no global acls */
134         } else if ( be == NULL && global_acl == NULL ) {
135                 Debug( LDAP_DEBUG_ACL,
136                         "=> access_allowed: global default %s access %s to \"%s\"\n",
137                         access2str( access ),
138                         global_default_access >= access ? "granted" : "denied", op->o_dn );
139
140                 return global_default_access >= access;
141 #endif
142         }
143
144         ACL_INIT(mask);
145         memset(matches, '\0', sizeof(matches));
146         
147         control = ACL_BREAK;
148         a = NULL;
149         count = 0;
150
151         while( a = acl_get( a, &count, be, op, e, desc, MAXREMATCHES, matches ) )
152         {
153                 int i;
154
155                 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
156                         Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
157                                (int)matches[i].rm_so, (int)matches[i].rm_eo );
158
159                         if( matches[i].rm_so <= matches[0].rm_eo ) {
160                                 int n;
161                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
162                                         Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
163                                 }
164                         }
165                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
166                 }
167
168                 control = acl_mask( a, &mask, be, conn, op,
169                         e, desc, val, matches );
170
171                 if ( control != ACL_BREAK ) {
172                         break;
173                 }
174
175                 memset(matches, '\0', sizeof(matches));
176         }
177
178         if ( ACL_IS_INVALID( mask ) ) {
179                 Debug( LDAP_DEBUG_ACL,
180                         "=> access_allowed: \"%s\" (%s) invalid!\n",
181                         e->e_dn, attr, 0 );
182                 ACL_INIT( mask );
183
184         } else if ( control == ACL_BREAK ) {
185                 Debug( LDAP_DEBUG_ACL,
186                         "=> access_allowed: no more rules\n", 0, 0, 0);
187                 ACL_INIT( mask );
188         }
189
190         Debug( LDAP_DEBUG_ACL,
191                 "=> access_allowed: %s access %s by %s\n",
192                 access2str( access ),
193                 ACL_GRANT(mask, access) ? "granted" : "denied",
194                 accessmask2str( mask, accessmaskbuf ) );
195
196         return ACL_GRANT(mask, access);
197 }
198
199 /*
200  * acl_get - return the acl applicable to entry e, attribute
201  * attr.  the acl returned is suitable for use in subsequent calls to
202  * acl_access_allowed().
203  */
204
205 static AccessControl *
206 acl_get(
207         AccessControl *a,
208         int                     *count,
209     Backend             *be,
210     Operation   *op,
211     Entry               *e,
212         AttributeDescription *desc,
213     int                 nmatch,
214     regmatch_t  *matches )
215 {
216         const char *attr;
217         int dnlen, patlen;
218
219         assert( e != NULL );
220         assert( count != NULL );
221
222         attr = desc ? desc->ad_cname->bv_val : NULL;
223
224         if( a == NULL ) {
225                 if( be == NULL ) {
226                         a = global_acl;
227                 } else {
228                         a = be->be_acl;
229                 }
230
231                 assert( a != NULL );
232
233         } else {
234                 a = a->acl_next;
235         }
236
237         dnlen = strlen(e->e_ndn);
238
239         for ( ; a != NULL; a = a->acl_next ) {
240                 (*count) ++;
241
242                 if (a->acl_dn_pat != NULL) {
243                         if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
244                                 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n", 
245                                         *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub );
246
247                                 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
248                                         continue;
249
250                         } else {
251                                 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n", 
252                                         *count, a->acl_dn_pat, 0 );
253
254                                 patlen = strlen( a->acl_dn_pat );
255                                 if ( dnlen < patlen )
256                                         continue;
257
258                                 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
259                                         /* base dn -- entire object DN must match */
260                                         if ( dnlen != patlen )
261                                                 continue;
262
263                                 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
264                                         char *rdn;
265                                         int rdnlen = -1;
266
267                                         if ( dnlen <= patlen )
268                                                 continue;
269
270                                         if ( e->e_ndn[dnlen - patlen - 1] != ',' )
271                                                 continue;
272
273                                         rdn = dn_rdn( NULL, e->e_ndn );
274                                         if ( rdn != NULL ) {
275                                                 rdnlen = strlen( rdn );
276                                                 ch_free( rdn );
277                                         }
278                                         if ( rdnlen != dnlen - patlen - 1 )
279                                                 continue;
280
281                                 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
282                                         if ( dnlen > patlen && e->e_ndn[dnlen - patlen - 1] != ',' )
283                                                 continue;
284
285                                 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
286                                         if ( dnlen <= patlen )
287                                                 continue;
288                                         if ( e->e_ndn[dnlen - patlen - 1] != ',' )
289                                                 continue;
290                                 }
291
292                                 if ( strcmp( a->acl_dn_pat, e->e_ndn + dnlen - patlen ) != 0 )
293                                         continue;
294                         }
295
296                         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
297                                 *count, 0, 0 );
298                 }
299
300                 if ( a->acl_filter != NULL ) {
301                         ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
302                         if ( rc != LDAP_COMPARE_TRUE ) {
303                                 continue;
304                         }
305                 }
306
307         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
308                         *count, attr, 0);
309
310                 if ( attr == NULL || a->acl_attrs == NULL ||
311                         ad_inlist( desc, a->acl_attrs ) )
312                 {
313                         Debug( LDAP_DEBUG_ACL,
314                                 "<= acl_get: [%d] acl %s attr: %s\n",
315                                 *count, e->e_dn, attr );
316                         return a;
317                 }
318                 matches[0].rm_so = matches[0].rm_eo = -1;
319         }
320
321         Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
322         return( NULL );
323 }
324
325
326 /*
327  * acl_mask - modifies mask based upon the given acl and the
328  * requested access to entry e, attribute attr, value val.  if val
329  * is null, access to the whole attribute is assumed (all values).
330  *
331  * returns      0       access NOT allowed
332  *              1       access allowed
333  */
334
335 static slap_control_t
336 acl_mask(
337     AccessControl       *a,
338         slap_access_mask_t *mask,
339     Backend             *be,
340     Connection  *conn,
341     Operation   *op,
342     Entry               *e,
343         AttributeDescription *desc,
344     struct berval       *val,
345         regmatch_t      *matches
346 )
347 {
348         int             i, odnlen, patlen;
349         Access  *b;
350 #ifdef LDAP_DEBUG
351         char accessmaskbuf[ACCESSMASK_MAXLEN];
352 #endif
353         const char *attr = desc ? desc->ad_cname->bv_val : NULL;
354
355         assert( a != NULL );
356         assert( mask != NULL );
357
358         Debug( LDAP_DEBUG_ACL,
359                 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
360                 e->e_dn, attr, 0 );
361
362         Debug( LDAP_DEBUG_ACL,
363                 "=> acl_mask: to value \"%s\" by \"%s\", (%s) \n",
364                 val ? val->bv_val : "*",
365                 op->o_ndn ?  op->o_ndn : "",
366                 accessmask2str( *mask, accessmaskbuf ) );
367
368         for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
369                 slap_access_mask_t oldmask, modmask;
370
371                 ACL_INVALIDATE( modmask );
372
373                 /* AND <who> clauses */
374                 if ( b->a_dn_pat != NULL ) {
375                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
376                                 b->a_dn_pat, 0, 0);
377                         /*
378                          * if access applies to the entry itself, and the
379                          * user is bound as somebody in the same namespace as
380                          * the entry, OR the given dn matches the dn pattern
381                          */
382                         if ( strcmp( b->a_dn_pat, "anonymous" ) == 0 ) {
383                                 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
384                                         continue;
385                                 }
386
387                         } else if ( strcmp( b->a_dn_pat, "users" ) == 0 ) {
388                                 if (op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
389                                         continue;
390                                 }
391
392                         } else if ( strcmp( b->a_dn_pat, "self" ) == 0 ) {
393                                 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
394                                         continue;
395                                 }
396                                 
397                                 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn ) != 0 ) {
398                                         continue;
399                                 }
400
401                         } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
402                                 if ( strcmp( b->a_dn_pat, "*" ) != 0 ) {
403                                         int ret = regex_matches( b->a_dn_pat,
404                                                 op->o_ndn, e->e_ndn, matches );
405
406                                         if( ret == 0 ) {
407                                                 continue;
408                                         }
409                                 }
410
411                         } else {
412                                 if ( e->e_dn == NULL )
413                                         continue;
414
415                                 patlen = strlen( b->a_dn_pat );
416                                 odnlen = strlen( op->o_ndn );
417                                 if ( odnlen < patlen )
418                                         continue;
419
420                                 if ( b->a_dn_style == ACL_STYLE_BASE ) {
421                                         /* base dn -- entire object DN must match */
422                                         if ( odnlen != patlen )
423                                                 continue;
424
425                                 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
426                                         char *rdn;
427                                         int rdnlen = -1;
428
429                                         if ( odnlen <= patlen )
430                                                 continue;
431
432                                         if ( op->o_ndn[odnlen - patlen - 1] != ',' )
433                                                 continue;
434
435                                         rdn = dn_rdn( NULL, op->o_ndn );
436                                         if ( rdn != NULL ) {
437                                                 rdnlen = strlen( rdn );
438                                                 ch_free( rdn );
439                                         }
440                                         if ( rdnlen != odnlen - patlen - 1 )
441                                                 continue;
442
443                                 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
444                                         if ( odnlen > patlen && op->o_ndn[odnlen - patlen - 1] != ',' )
445                                                 continue;
446
447                                 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
448                                         if ( odnlen <= patlen )
449                                                 continue;
450                                         if ( op->o_ndn[odnlen - patlen - 1] != ',' )
451                                                 continue;
452                                 }
453
454                                 if ( strcmp( b->a_dn_pat, op->o_ndn + odnlen - patlen ) != 0 )
455                                         continue;
456
457                         }
458                 }
459
460                 if ( b->a_sockurl_pat != NULL ) {
461                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
462                                 b->a_sockurl_pat, 0, 0 );
463
464                         if ( strcmp( b->a_sockurl_pat, "*" ) != 0) {
465                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
466                                         if (!regex_matches( b->a_sockurl_pat, conn->c_listener_url,
467                                                         e->e_ndn, matches ) ) 
468                                         {
469                                                 continue;
470                                         }
471                                 } else {
472                                         if ( strcasecmp( b->a_sockurl_pat, conn->c_listener_url ) == 0 )
473                                                 continue;
474                                 }
475                         }
476                 }
477
478                 if ( b->a_domain_pat != NULL ) {
479                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
480                                 b->a_domain_pat, 0, 0 );
481
482                         if ( strcmp( b->a_domain_pat, "*" ) != 0) {
483                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
484                                         if (!regex_matches( b->a_domain_pat, conn->c_peer_domain,
485                                                         e->e_ndn, matches ) ) 
486                                         {
487                                                 continue;
488                                         }
489                                 } else {
490                                         if ( strcasecmp( b->a_domain_pat, conn->c_peer_domain ) == 0 )
491                                                 continue;
492                                 }
493                         }
494                 }
495
496                 if ( b->a_peername_pat != NULL ) {
497                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
498                                 b->a_peername_pat, 0, 0 );
499
500                         if ( strcmp( b->a_peername_pat, "*" ) != 0) {
501                                 if ( b->a_peername_style == ACL_STYLE_REGEX) {
502                                         if (!regex_matches( b->a_peername_pat, conn->c_peer_name,
503                                                         e->e_ndn, matches ) ) 
504                                         {
505                                                 continue;
506                                         }
507                                 } else {
508                                         if ( strcasecmp( b->a_peername_pat, conn->c_peer_name ) == 0 )
509                                                 continue;
510                                 }
511                         }
512                 }
513
514                 if ( b->a_sockname_pat != NULL ) {
515                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
516                                 b->a_sockname_pat, 0, 0 );
517
518                         if ( strcmp( b->a_sockname_pat, "*" ) != 0) {
519                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
520                                         if (!regex_matches( b->a_sockname_pat, conn->c_sock_name,
521                                                         e->e_ndn, matches ) ) 
522                                         {
523                                                 continue;
524                                         }
525                                 } else {
526                                         if ( strcasecmp( b->a_sockname_pat, conn->c_sock_name ) == 0 )
527                                                 continue;
528                                 }
529                         }
530                 }
531
532                 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
533                         Attribute       *at;
534                         struct berval   bv;
535                         int rc, match = 0;
536                         const char *text;
537                         const char *desc = b->a_dn_at->ad_cname->bv_val;
538
539                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
540                                 desc, 0, 0);
541
542                         bv.bv_val = op->o_ndn;
543                         bv.bv_len = strlen( bv.bv_val );
544
545                         /* see if asker is listed in dnattr */
546                         for( at = attrs_find( e->e_attrs, b->a_dn_at );
547                                 at != NULL;
548                                 at = attrs_find( at->a_next, b->a_dn_at ) )
549                         {
550                                 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
551                                         /* found it */
552                                         match = 1;
553                                         break;
554                                 }
555                         }
556
557                         if( match ) {
558                                 /* have a dnattr match. if this is a self clause then
559                                  * the target must also match the op dn.
560                                  */
561                                 if ( b->a_dn_self ) {
562                                         /* check if the target is an attribute. */
563                                         if ( val == NULL )
564                                                 continue;
565                                         /* target is attribute, check if the attribute value
566                                          * is the op dn.
567                                          */
568                                         rc = value_match(       &match, b->a_dn_at,
569                                                                                 b->a_dn_at->ad_type->sat_equality,
570                                                                                 val, &bv, &text );
571                                         /* on match error or no match, fail the ACL clause */
572                                         if (rc != LDAP_SUCCESS || match != 0 )
573                                                 continue;
574                                 }
575                         } else {
576                                 /* no dnattr match, check if this is a self clause */
577                                 if ( ! b->a_dn_self )
578                                         continue;
579                                 /* this is a self clause, check if the target is an
580                                  * attribute.
581                                  */
582                                 if ( val == NULL )
583                                         continue;
584                                 /* target is attribute, check if the attribute value
585                                  * is the op dn.
586                                  */
587                                 rc = value_match(       &match, b->a_dn_at,
588                                                                         b->a_dn_at->ad_type->sat_equality,
589                                                                         val, &bv, &text );
590                                 /* on match error or no match, fail the ACL clause */
591                                 if (rc != LDAP_SUCCESS || match != 0 )
592                                         continue;
593                         }
594                 }
595
596                 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
597                         char buf[1024];
598
599                         /* b->a_group is an unexpanded entry name, expanded it should be an 
600                          * entry with objectclass group* and we test to see if odn is one of
601                          * the values in the attribute group
602                          */
603                         /* see if asker is listed in dnattr */
604                         if ( b->a_group_style != ACL_STYLE_REGEX ) {
605                                 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
606                                 if ( dn_normalize(buf) == NULL ) {
607                                         /* did not expand to a valid dn */
608                                         continue;
609                                 }
610                         } else {
611                                 strncpy( buf, b->a_group_pat, sizeof(buf) - 1 );
612                                 buf[sizeof(buf) - 1] = 0;
613                         }
614
615                         if (backend_group(be, e, buf, op->o_ndn,
616                                 b->a_group_oc, b->a_group_at) != 0)
617                         {
618                                 continue;
619                         }
620                 }
621
622 #ifdef SLAPD_ACI_ENABLED
623                 if ( b->a_aci_at != NULL ) {
624                         Attribute       *at;
625                         slap_access_t grant, deny, tgrant, tdeny;
626
627                         /* this case works different from the others above.
628                          * since aci's themselves give permissions, we need
629                          * to first check b->a_mask, the ACL's access level.
630                          */
631
632                         if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
633                                 continue;
634                         }
635
636                         if ( e->e_dn == NULL ) {
637                                 continue;
638                         }
639
640                         /* first check if the right being requested
641                          * is allowed by the ACL clause.
642                          */
643                         if ( ! ACL_GRANT( b->a_mask, *mask ) ) {
644                                 continue;
645                         }
646
647                         /* get the aci attribute */
648                         at = attr_find( e->e_attrs, b->a_aci_at );
649                         if ( at == NULL ) {
650                                 continue;
651                         }
652
653                         /* start out with nothing granted, nothing denied */
654                         ACL_INIT(tgrant);
655                         ACL_INIT(tdeny);
656
657                         /* the aci is an multi-valued attribute.  The
658                          * rights are determined by OR'ing the individual
659                          * rights given by the acis.
660                          */
661                         for ( i = 0; at->a_vals[i] != NULL; i++ ) {
662                                 if (aci_mask( be, conn, op,
663                                         e, desc, val, at->a_vals[i],
664                                         matches, &grant, &deny ) != 0)
665                                 {
666                                         tgrant |= grant;
667                                         tdeny |= deny;
668                                 }
669                         }
670
671                         /* remove anything that the ACL clause does not allow */
672                         tgrant &= b->a_mask & ACL_PRIV_MASK;
673                         tdeny &= ACL_PRIV_MASK;
674
675                         /* see if we have anything to contribute */
676                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
677                                 continue;
678                         }
679
680                         /* this could be improved by changing acl_mask so that it can deal with
681                          * by clauses that return grant/deny pairs.  Right now, it does either
682                          * additive or subtractive rights, but not both at the same time.  So,
683                          * we need to combine the grant/deny pair into a single rights mask in
684                          * a smart way:  if either grant or deny is "empty", then we use the
685                          * opposite as is, otherwise we remove any denied rights from the grant
686                          * rights mask and construct an additive mask.
687                          */
688                         if (ACL_IS_INVALID(tdeny)) {
689                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
690
691                         } else if (ACL_IS_INVALID(tgrant)) {
692                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
693
694                         } else {
695                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
696                         }
697
698                 } else
699 #endif
700                 {
701                         modmask = b->a_mask;
702                 }
703
704
705                 Debug( LDAP_DEBUG_ACL,
706                         "<= acl_mask: [%d] applying %s (%s)\n",
707                         i, accessmask2str( modmask, accessmaskbuf ), 
708                         b->a_type == ACL_CONTINUE
709                                 ? "continue"
710                                 : b->a_type == ACL_BREAK
711                                         ? "break"
712                                         : "stop" );
713
714                 /* save old mask */
715                 oldmask = *mask;
716
717                 if( ACL_IS_ADDITIVE(modmask) ) {
718                         /* add privs */
719                         ACL_PRIV_SET( *mask, modmask );
720
721                         /* cleanup */
722                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
723
724                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
725                         /* substract privs */
726                         ACL_PRIV_CLR( *mask, modmask );
727
728                         /* cleanup */
729                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
730
731                 } else {
732                         /* assign privs */
733                         *mask = modmask;
734                 }
735
736                 Debug( LDAP_DEBUG_ACL,
737                         "<= acl_mask: [%d] mask: %s\n",
738                         i, accessmask2str(*mask, accessmaskbuf), 0 );
739
740                 if( b->a_type == ACL_CONTINUE ) {
741                         continue;
742
743                 } else if ( b->a_type == ACL_BREAK ) {
744                         return ACL_BREAK;
745
746                 } else {
747                         return ACL_STOP;
748                 }
749         }
750
751         Debug( LDAP_DEBUG_ACL,
752                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
753                 accessmask2str(*mask, accessmaskbuf), 0, 0 );
754         return ACL_STOP;
755 }
756
757 /*
758  * acl_check_modlist - check access control on the given entry to see if
759  * it allows the given modifications by the user associated with op.
760  * returns      1       if mods allowed ok
761  *                      0       mods not allowed
762  */
763
764 int
765 acl_check_modlist(
766     Backend     *be,
767     Connection  *conn,
768     Operation   *op,
769     Entry       *e,
770     Modifications       *mlist
771 )
772 {
773         int             i;
774
775         assert( be != NULL );
776
777         /* short circuit root database access */
778         if ( be_isroot( be, op->o_ndn ) ) {
779                 Debug( LDAP_DEBUG_ACL,
780                         "<= acl_access_allowed: granted to database root\n",
781                     0, 0, 0 );
782                 return 1;
783         }
784
785         /* use backend default access if no backend acls */
786         if( be != NULL && be->be_acl == NULL ) {
787                 Debug( LDAP_DEBUG_ACL,
788                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
789                         access2str( ACL_WRITE ),
790                         be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
791
792                 return be->be_dfltaccess >= ACL_WRITE;
793
794 #ifdef notdef
795         /* be is always non-NULL */
796         /* use global default access if no global acls */
797         } else if ( be == NULL && global_acl == NULL ) {
798                 Debug( LDAP_DEBUG_ACL,
799                         "=> access_allowed: global default %s access %s to \"%s\"\n",
800                         access2str( ACL_WRITE ),
801                         global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
802
803                 return global_default_access >= ACL_WRITE;
804 #endif
805         }
806
807         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
808                 /*
809                  * no-user-modification operational attributes are ignored
810                  * by ACL_WRITE checking as any found here are not provided
811                  * by the user
812                  */
813                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
814                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
815                                 " modify access granted\n",
816                                 mlist->sml_desc->ad_cname->bv_val, 0, 0 );
817                         continue;
818                 }
819
820                 switch ( mlist->sml_op ) {
821                 case LDAP_MOD_REPLACE:
822                 case LDAP_MOD_ADD:
823                         if ( mlist->sml_bvalues == NULL ) {
824                                 break;
825                         }
826                         for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
827                                 if ( ! access_allowed( be, conn, op, e,
828                                         mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
829                                 {
830                                         return( 0 );
831                                 }
832                         }
833                         break;
834
835                 case LDAP_MOD_DELETE:
836                         if ( mlist->sml_bvalues == NULL ) {
837                                 if ( ! access_allowed( be, conn, op, e,
838                                         mlist->sml_desc, NULL, ACL_WRITE ) )
839                                 {
840                                         return( 0 );
841                                 }
842                                 break;
843                         }
844                         for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
845                                 if ( ! access_allowed( be, conn, op, e,
846                                         mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
847                                 {
848                                         return( 0 );
849                                 }
850                         }
851                         break;
852                 }
853         }
854
855         return( 1 );
856 }
857
858 #ifdef SLAPD_ACI_ENABLED
859 static char *
860 aci_bvstrdup( struct berval *bv )
861 {
862         char *s;
863
864         s = (char *)ch_malloc(bv->bv_len + 1);
865         if (s != NULL) {
866                 memcpy(s, bv->bv_val, bv->bv_len);
867                 s[bv->bv_len] = 0;
868         }
869         return(s);
870 }
871
872 static int
873 aci_strbvcmp(
874         const char *s,
875         struct berval *bv )
876 {
877         int res, len;
878
879         res = strncasecmp( s, bv->bv_val, bv->bv_len );
880         if (res)
881                 return(res);
882         len = strlen(s);
883         if (len > (int)bv->bv_len)
884                 return(1);
885         if (len < (int)bv->bv_len)
886                 return(-1);
887         return(0);
888 }
889
890 static int
891 aci_get_part(
892         struct berval *list,
893         int ix,
894         char sep,
895         struct berval *bv )
896 {
897         int len;
898         char *p;
899
900         if (bv) {
901                 bv->bv_len = 0;
902                 bv->bv_val = NULL;
903         }
904         len = list->bv_len;
905         p = list->bv_val;
906         while (len >= 0 && --ix >= 0) {
907                 while (--len >= 0 && *p++ != sep) ;
908         }
909         while (len >= 0 && *p == ' ') {
910                 len--;
911                 p++;
912         }
913         if (len < 0)
914                 return(-1);
915
916         if (!bv)
917                 return(0);
918
919         bv->bv_val = p;
920         while (--len >= 0 && *p != sep) {
921                 bv->bv_len++;
922                 p++;
923         }
924         while (bv->bv_len > 0 && *--p == ' ')
925                 bv->bv_len--;
926         return(bv->bv_len);
927 }
928
929 static int
930 aci_list_map_rights(
931         struct berval *list )
932 {
933         struct berval bv;
934         slap_access_t mask;
935         int i;
936
937         ACL_INIT(mask);
938         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
939                 if (bv.bv_len <= 0)
940                         continue;
941                 switch (*bv.bv_val) {
942                 case 'c':
943                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
944                         break;
945                 case 's':
946                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
947                          * the right 's' to mean "set", but in the examples states
948                          * that the right 's' means "search".  The latter definition
949                          * is used here.
950                          */
951                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
952                         break;
953                 case 'r':
954                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
955                         break;
956                 case 'w':
957                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
958                         break;
959                 case 'x':
960                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
961                          * define any equivalent to the AUTH right, so I've just used
962                          * 'x' for now.
963                          */
964                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
965                         break;
966                 default:
967                         break;
968                 }
969
970         }
971         return(mask);
972 }
973
974 static int
975 aci_list_has_attr(
976         struct berval *list,
977         const char *attr,
978         struct berval *val )
979 {
980         struct berval bv, left, right;
981         int i;
982
983         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
984                 if (aci_get_part(&bv, 0, '=', &left) < 0
985                         || aci_get_part(&bv, 1, '=', &right) < 0)
986                 {
987                         if (aci_strbvcmp(attr, &bv) == 0)
988                                 return(1);
989                 } else if (val == NULL) {
990                         if (aci_strbvcmp(attr, &left) == 0)
991                                 return(1);
992                 } else {
993                         if (aci_strbvcmp(attr, &left) == 0) {
994                                 /* this is experimental code that implements a
995                                  * simple (prefix) match of the attribute value.
996                                  * the ACI draft does not provide for aci's that
997                                  * apply to specific values, but it would be
998                                  * nice to have.  If the <attr> part of an aci's
999                                  * rights list is of the form <attr>=<value>,
1000                                  * that means the aci applies only to attrs with
1001                                  * the given value.  Furthermore, if the attr is
1002                                  * of the form <attr>=<value>*, then <value> is
1003                                  * treated as a prefix, and the aci applies to 
1004                                  * any value with that prefix.
1005                                  *
1006                                  * Ideally, this would allow r.e. matches.
1007                                  */
1008                                 if (aci_get_part(&right, 0, '*', &left) < 0
1009                                         || right.bv_len <= left.bv_len)
1010                                 {
1011                                         if (aci_strbvcmp(val->bv_val, &right) == 0)
1012                                                 return(1);
1013                                 } else if (val->bv_len >= left.bv_len) {
1014                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1015                                                 return(1);
1016                                 }
1017                         }
1018                 }
1019         }
1020         return(0);
1021 }
1022
1023 static slap_access_t
1024 aci_list_get_attr_rights(
1025         struct berval *list,
1026         const char *attr,
1027         struct berval *val )
1028 {
1029     struct berval bv;
1030     slap_access_t mask;
1031     int i;
1032
1033         /* loop through each rights/attr pair, skip first part (action) */
1034         ACL_INIT(mask);
1035         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1036                 if (aci_list_has_attr(&bv, attr, val) == 0)
1037                         continue;
1038                 if (aci_get_part(list, i, ';', &bv) < 0)
1039                         continue;
1040                 mask |= aci_list_map_rights(&bv);
1041         }
1042         return(mask);
1043 }
1044
1045 static int
1046 aci_list_get_rights(
1047         struct berval *list,
1048         const char *attr,
1049         struct berval *val,
1050         slap_access_t *grant,
1051         slap_access_t *deny )
1052 {
1053     struct berval perm, actn;
1054     slap_access_t *mask;
1055     int i, found;
1056
1057         if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
1058                 attr = "[entry]";
1059         }
1060
1061         found = 0;
1062         ACL_INIT(*grant);
1063         ACL_INIT(*deny);
1064         /* loop through each permissions clause */
1065         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1066                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1067                         continue;
1068                 if (aci_strbvcmp( "grant", &actn ) == 0) {
1069                         mask = grant;
1070                 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
1071                         mask = deny;
1072                 } else {
1073                         continue;
1074                 }
1075
1076                 found = 1;
1077                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1078                 *mask |= aci_list_get_attr_rights(&perm, "[all]", NULL);
1079         }
1080         return(found);
1081 }
1082
1083 static int
1084 aci_group_member (
1085         struct berval *subj,
1086         const char *defgrpoc,
1087         const char *defgrpat,
1088     Backend             *be,
1089     Entry               *e,
1090     Operation           *op,
1091         regmatch_t      *matches
1092 )
1093 {
1094         struct berval bv;
1095         char *subjdn, *grpdn = NULL;
1096         char *grpoc;
1097         char *grpat;
1098         ObjectClass *grp_oc = NULL;
1099         AttributeDescription *grp_ad = NULL;
1100         char *text;
1101         int rc;
1102
1103         /* format of string is "group/objectClassValue/groupAttrName" */
1104         if (aci_get_part(subj, 0, '/', &bv) < 0) {
1105                 return(0);
1106         }
1107
1108         subjdn = aci_bvstrdup(&bv);
1109         if (subjdn == NULL) {
1110                 return(0);
1111         }
1112
1113         if (aci_get_part(subj, 1, '/', &bv) < 0) {
1114                 grpoc = ch_strdup( defgrpoc );
1115         } else {
1116                 grpoc = aci_bvstrdup(&bv);
1117         }
1118
1119         if (aci_get_part(subj, 2, '/', &bv) < 0) {
1120                 grpat = ch_strdup( defgrpat );
1121         } else {
1122                 grpat = aci_bvstrdup(&bv);
1123         }
1124
1125         rc = slap_str2ad( grpat, &grp_ad, &text );
1126         if( rc != LDAP_SUCCESS ) {
1127                 rc = 0;
1128                 goto done;
1129         }
1130         rc = 0;
1131
1132         grp_oc = oc_find( grpoc );
1133         grpdn = (char *)ch_malloc(1024);
1134
1135         if (grp_oc != NULL && grp_ad != NULL && grpdn != NULL) {
1136                 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
1137                 if ( dn_normalize(grpdn) != NULL ) {
1138                         rc = (backend_group(be, e, grpdn, op->o_ndn, grp_oc, grp_ad) == 0);
1139                 }
1140         }
1141
1142 done:
1143         if( grp_ad != NULL ) ad_free( grp_ad, 1 );
1144         ch_free(grpdn);
1145         ch_free(grpat);
1146         ch_free(grpoc);
1147         ch_free(subjdn);
1148         return(rc);
1149 }
1150
1151 static int
1152 aci_mask(
1153     Backend                     *be,
1154     Connection          *conn,
1155     Operation           *op,
1156     Entry                       *e,
1157         AttributeDescription *desc,
1158     struct berval       *val,
1159     struct berval       *aci,
1160         regmatch_t              *matches,
1161         slap_access_t   *grant,
1162         slap_access_t   *deny
1163 )
1164 {
1165     struct berval bv, perms, sdn;
1166     char *subjdn;
1167         int rc;
1168         char *attr = desc->ad_cname->bv_val;
1169
1170         /* parse an aci of the form:
1171                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1172
1173            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1174            a full description of the format for this attribute.
1175
1176            For now, this routine only supports scope=entry.
1177          */
1178
1179         /* check that the aci has all 5 components */
1180         if (aci_get_part(aci, 4, '#', NULL) < 0)
1181                 return(0);
1182
1183         /* check that the aci family is supported */
1184         if (aci_get_part(aci, 0, '#', &bv) < 0)
1185                 return(0);
1186
1187         /* check that the scope is "entry" */
1188         if (aci_get_part(aci, 1, '#', &bv) < 0
1189                 || aci_strbvcmp( "entry", &bv ) != 0)
1190         {
1191                 return(0);
1192         }
1193
1194         /* get the list of permissions clauses, bail if empty */
1195         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1196                 return(0);
1197
1198         /* check if any permissions allow desired access */
1199         if (aci_list_get_rights(&perms, attr, val, grant, deny) == 0)
1200                 return(0);
1201
1202         /* see if we have a DN match */
1203         if (aci_get_part(aci, 3, '#', &bv) < 0)
1204                 return(0);
1205
1206         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1207                 return(0);
1208
1209         if (aci_strbvcmp( "access-id", &bv ) == 0) {
1210                 subjdn = aci_bvstrdup(&sdn);
1211                 if (subjdn == NULL)
1212                         return(0);
1213                 rc = 1;
1214                 if ( dn_normalize(subjdn) != NULL )
1215                         if (strcasecmp(op->o_ndn, subjdn) != 0)
1216                                 rc = 0;
1217                 ch_free(subjdn);
1218                 return(rc);
1219         }
1220
1221         if (aci_strbvcmp( "self", &bv ) == 0) {
1222                 if (strcasecmp(op->o_ndn, e->e_ndn) == 0)
1223                         return(1);
1224
1225         } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
1226                 char *dnattr = aci_bvstrdup(&sdn);
1227                 Attribute *at;
1228                 AttributeDescription *ad = NULL;
1229                 const char *text;
1230
1231                 rc = slap_str2ad( dnattr, &ad, &text );
1232                 ch_free( dnattr );
1233
1234                 if( rc != LDAP_SUCCESS ) {
1235                         return 0;
1236                 }
1237
1238                 rc = 0;
1239
1240                 bv.bv_val = op->o_ndn;
1241                 bv.bv_len = strlen( bv.bv_val );
1242
1243                 for(at = attrs_find( e->e_attrs, ad );
1244                         at != NULL;
1245                         at = attrs_find( at->a_next, ad ) )
1246                 {
1247                         if (value_find( ad, at->a_vals, &bv) == 0 ) {
1248                                 rc = 1;
1249                                 break;
1250                         }
1251                 }
1252
1253                 ad_free( ad, 1 );
1254                 return rc;
1255
1256
1257         } else if (aci_strbvcmp( "group", &bv ) == 0) {
1258                 if (aci_group_member(&sdn, SLAPD_GROUP_CLASS, SLAPD_GROUP_ATTR, be, e, op, matches))
1259                         return(1);
1260
1261         } else if (aci_strbvcmp( "role", &bv ) == 0) {
1262                 if (aci_group_member(&sdn, SLAPD_ROLE_CLASS, SLAPD_ROLE_ATTR, be, e, op, matches))
1263                         return(1);
1264
1265         }
1266
1267         return(0);
1268 }
1269
1270 #endif  /* SLAPD_ACI_ENABLED */
1271
1272 static void
1273 string_expand(
1274         char *newbuf,
1275         int bufsiz,
1276         char *pat,
1277         char *match,
1278         regmatch_t *matches)
1279 {
1280         int     size;
1281         char   *sp;
1282         char   *dp;
1283         int     flag;
1284
1285         size = 0;
1286         newbuf[0] = '\0';
1287         bufsiz--; /* leave space for lone $ */
1288
1289         flag = 0;
1290         for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
1291                 /* did we previously see a $ */
1292                 if (flag) {
1293                         if (*sp == '$') {
1294                                 *dp++ = '$';
1295                                 size++;
1296                         } else if (*sp >= '0' && *sp <= '9' ) {
1297                                 int     n;
1298                                 int     i;
1299                                 int     l;
1300
1301                                 n = *sp - '0';
1302                                 *dp = '\0';
1303                                 i = matches[n].rm_so;
1304                                 l = matches[n].rm_eo; 
1305                                 for ( ; size < 512 && i < l; size++, i++ ) {
1306                                         *dp++ = match[i];
1307                                         size++;
1308                                 }
1309                                 *dp = '\0';
1310                         }
1311                         flag = 0;
1312                 } else {
1313                         if (*sp == '$') {
1314                                 flag = 1;
1315                         } else {
1316                                 *dp++ = *sp;
1317                                 size++;
1318                         }
1319                 }
1320         }
1321
1322         if (flag) {
1323                 /* must have ended with a single $ */
1324                 *dp++ = '$';
1325                 size++;
1326         }
1327
1328         *dp = '\0';
1329
1330         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %s\n", pat, 0, 0 );
1331         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1332 }
1333
1334 static int
1335 regex_matches(
1336         char *pat,                              /* pattern to expand and match against */
1337         char *str,                              /* string to match against pattern */
1338         char *buf,                              /* buffer with $N expansion variables */
1339         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
1340 )
1341 {
1342         regex_t re;
1343         char newbuf[512];
1344         int     rc;
1345
1346         if(str == NULL) str = "";
1347
1348         string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1349         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1350                 char error[512];
1351                 regerror(rc, &re, error, sizeof(error));
1352
1353                 Debug( LDAP_DEBUG_TRACE,
1354                     "compile( \"%s\", \"%s\") failed %s\n",
1355                         pat, str, error );
1356                 return( 0 );
1357         }
1358
1359         rc = regexec(&re, str, 0, NULL, 0);
1360         regfree( &re );
1361
1362         Debug( LDAP_DEBUG_TRACE,
1363             "=> regex_matches: string:   %s\n", str, 0, 0 );
1364         Debug( LDAP_DEBUG_TRACE,
1365             "=> regex_matches: rc: %d %s\n",
1366                 rc, !rc ? "matches" : "no matches", 0 );
1367         return( !rc );
1368 }
1369