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