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