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