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