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