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