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