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