]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
Fix aci link error.
[openldap] / servers / slapd / acl.c
1 /* acl.c - routines to parse and check acl's */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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 #include "sets.h"
18
19 static AccessControl * acl_get(
20         AccessControl *ac, int *count,
21         Backend *be, Operation *op,
22         Entry *e,
23         AttributeDescription *desc,
24         int nmatches, regmatch_t *matches );
25
26 static slap_control_t acl_mask(
27         AccessControl *ac, slap_access_mask_t *mask,
28         Backend *be, Connection *conn, Operation *op,
29         Entry *e,
30         AttributeDescription *desc,
31         struct berval *val,
32         regmatch_t *matches );
33
34 #ifdef SLAPD_ACI_ENABLED
35 static int aci_mask(
36         Backend *be,
37     Connection *conn,
38         Operation *op,
39         Entry *e,
40         AttributeDescription *desc,
41         struct berval *val,
42         struct berval *aci,
43         regmatch_t *matches,
44         slap_access_t *grant,
45         slap_access_t *deny );
46 #endif
47
48 static int      regex_matches(
49         char *pat, char *str, char *buf, regmatch_t *matches);
50 static void     string_expand(
51         char *newbuf, int bufsiz, char *pattern,
52         char *match, regmatch_t *matches);
53
54 char **aci_set_gather (void *cookie, char *name, char *attr);
55 static int aci_match_set ( struct berval *subj, Backend *be,
56     Entry *e, Connection *conn, Operation *op, int setref );
57
58 /*
59  * access_allowed - check whether op->o_ndn is allowed the requested access
60  * to entry e, attribute attr, value val.  if val is null, access to
61  * the whole attribute is assumed (all values).
62  *
63  * This routine loops through all access controls and calls
64  * acl_mask() on each applicable access control.
65  * The loop exits when a definitive answer is reached or
66  * or no more controls remain.
67  *
68  * returns:
69  *              0       access denied
70  *              1       access granted
71  */
72
73 int
74 access_allowed(
75     Backend             *be,
76     Connection          *conn,
77     Operation           *op,
78     Entry               *e,
79         AttributeDescription    *desc,
80     struct berval       *val,
81     slap_access_t       access )
82 {
83         int                             count;
84         AccessControl   *a;
85 #ifdef LDAP_DEBUG
86         char accessmaskbuf[ACCESSMASK_MAXLEN];
87 #endif
88         slap_access_mask_t mask;
89         slap_control_t control;
90
91         const char *attr = desc ? desc->ad_cname->bv_val : NULL;
92
93         regmatch_t       matches[MAXREMATCHES];
94
95         Debug( LDAP_DEBUG_ACL,
96                 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
97             access2str( access ),
98                 e->e_dn, attr );
99
100         assert( be != NULL );
101         assert( e != NULL );
102         assert( attr != NULL );
103         assert( access > ACL_NONE );
104
105         /* grant database root access */
106         if ( be != NULL && be_isroot( be, op->o_ndn ) ) {
107                 Debug( LDAP_DEBUG_ACL,
108                     "<= root access granted\n",
109                         0, 0, 0 );
110                 return 1;
111         }
112
113         /*
114          * no-user-modification operational attributes are ignored
115          * by ACL_WRITE checking as any found here are not provided
116          * by the user
117          */
118         if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type ) )
119         {
120                 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
121                         " %s access granted\n",
122                         attr, 0, 0 );
123                 return 1;
124         }
125
126         /* use backend default access if no backend acls */
127         if( be != NULL && be->be_acl == NULL ) {
128                 Debug( LDAP_DEBUG_ACL,
129                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
130                         access2str( access ),
131                         be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn );
132
133                 return be->be_dfltaccess >= access;
134
135 #ifdef notdef
136         /* be is always non-NULL */
137         /* use global default access if no global acls */
138         } else if ( be == NULL && global_acl == NULL ) {
139                 Debug( LDAP_DEBUG_ACL,
140                         "=> access_allowed: global default %s access %s to \"%s\"\n",
141                         access2str( access ),
142                         global_default_access >= access ? "granted" : "denied", op->o_dn );
143
144                 return global_default_access >= access;
145 #endif
146         }
147
148         ACL_INIT(mask);
149         memset(matches, '\0', sizeof(matches));
150         
151         control = ACL_BREAK;
152         a = NULL;
153         count = 0;
154
155         while( a = acl_get( a, &count, be, op, e, desc, MAXREMATCHES, matches ) )
156         {
157                 int i;
158
159                 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
160                         Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
161                                (int)matches[i].rm_so, (int)matches[i].rm_eo );
162
163                         if( matches[i].rm_so <= matches[0].rm_eo ) {
164                                 int n;
165                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
166                                         Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
167                                 }
168                         }
169                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
170                 }
171
172                 control = acl_mask( a, &mask, be, conn, op,
173                         e, desc, val, matches );
174
175                 if ( control != ACL_BREAK ) {
176                         break;
177                 }
178
179                 memset(matches, '\0', sizeof(matches));
180         }
181
182         if ( ACL_IS_INVALID( mask ) ) {
183                 Debug( LDAP_DEBUG_ACL,
184                         "=> access_allowed: \"%s\" (%s) invalid!\n",
185                         e->e_dn, attr, 0 );
186                 ACL_INIT( mask );
187
188         } else if ( control == ACL_BREAK ) {
189                 Debug( LDAP_DEBUG_ACL,
190                         "=> access_allowed: no more rules\n", 0, 0, 0);
191                 ACL_INIT( mask );
192         }
193
194         Debug( LDAP_DEBUG_ACL,
195                 "=> access_allowed: %s access %s by %s\n",
196                 access2str( access ),
197                 ACL_GRANT(mask, access) ? "granted" : "denied",
198                 accessmask2str( mask, accessmaskbuf ) );
199
200         return ACL_GRANT(mask, access);
201 }
202
203 /*
204  * acl_get - return the acl applicable to entry e, attribute
205  * attr.  the acl returned is suitable for use in subsequent calls to
206  * acl_access_allowed().
207  */
208
209 static AccessControl *
210 acl_get(
211         AccessControl *a,
212         int                     *count,
213     Backend             *be,
214     Operation   *op,
215     Entry               *e,
216         AttributeDescription *desc,
217     int                 nmatch,
218     regmatch_t  *matches )
219 {
220         const char *attr;
221         int dnlen, patlen;
222
223         assert( e != NULL );
224         assert( count != NULL );
225
226         attr = desc ? desc->ad_cname->bv_val : NULL;
227
228         if( a == NULL ) {
229                 if( be == NULL ) {
230                         a = global_acl;
231                 } else {
232                         a = be->be_acl;
233                 }
234
235                 assert( a != NULL );
236
237         } else {
238                 a = a->acl_next;
239         }
240
241         dnlen = strlen(e->e_ndn);
242
243         for ( ; a != NULL; a = a->acl_next ) {
244                 (*count) ++;
245
246                 if (a->acl_dn_pat != NULL) {
247                         if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
248                                 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n", 
249                                         *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub );
250
251                                 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
252                                         continue;
253
254                         } else {
255                                 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n", 
256                                         *count, a->acl_dn_pat, 0 );
257
258                                 patlen = strlen( a->acl_dn_pat );
259                                 if ( dnlen < patlen )
260                                         continue;
261
262                                 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
263                                         /* base dn -- entire object DN must match */
264                                         if ( dnlen != patlen )
265                                                 continue;
266
267                                 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
268                                         char *rdn;
269                                         int rdnlen = -1;
270
271                                         if ( dnlen <= patlen )
272                                                 continue;
273
274                                         if ( e->e_ndn[dnlen - patlen - 1] != ',' )
275                                                 continue;
276
277                                         rdn = dn_rdn( NULL, e->e_ndn );
278                                         if ( rdn != NULL ) {
279                                                 rdnlen = strlen( rdn );
280                                                 ch_free( rdn );
281                                         }
282                                         if ( rdnlen != dnlen - patlen - 1 )
283                                                 continue;
284
285                                 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
286                                         if ( dnlen > patlen && e->e_ndn[dnlen - patlen - 1] != ',' )
287                                                 continue;
288
289                                 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
290                                         if ( dnlen <= patlen )
291                                                 continue;
292                                         if ( e->e_ndn[dnlen - patlen - 1] != ',' )
293                                                 continue;
294                                 }
295
296                                 if ( strcmp( a->acl_dn_pat, e->e_ndn + dnlen - patlen ) != 0 )
297                                         continue;
298                         }
299
300                         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
301                                 *count, 0, 0 );
302                 }
303
304                 if ( a->acl_filter != NULL ) {
305                         ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
306                         if ( rc != LDAP_COMPARE_TRUE ) {
307                                 continue;
308                         }
309                 }
310
311         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
312                         *count, attr, 0);
313
314                 if ( attr == NULL || a->acl_attrs == NULL ||
315                         ad_inlist( desc, a->acl_attrs ) )
316                 {
317                         Debug( LDAP_DEBUG_ACL,
318                                 "<= acl_get: [%d] acl %s attr: %s\n",
319                                 *count, e->e_dn, attr );
320                         return a;
321                 }
322                 matches[0].rm_so = matches[0].rm_eo = -1;
323         }
324
325         Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
326         return( NULL );
327 }
328
329
330 /*
331  * acl_mask - modifies mask based upon the given acl and the
332  * requested access to entry e, attribute attr, value val.  if val
333  * is null, access to the whole attribute is assumed (all values).
334  *
335  * returns      0       access NOT allowed
336  *              1       access allowed
337  */
338
339 static slap_control_t
340 acl_mask(
341     AccessControl       *a,
342         slap_access_mask_t *mask,
343     Backend             *be,
344     Connection  *conn,
345     Operation   *op,
346     Entry               *e,
347         AttributeDescription *desc,
348     struct berval       *val,
349         regmatch_t      *matches
350 )
351 {
352         int             i, odnlen, patlen;
353         Access  *b;
354 #ifdef LDAP_DEBUG
355         char accessmaskbuf[ACCESSMASK_MAXLEN];
356 #endif
357         const char *attr = desc ? desc->ad_cname->bv_val : NULL;
358
359         assert( a != NULL );
360         assert( mask != NULL );
361
362         Debug( LDAP_DEBUG_ACL,
363                 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
364                 e->e_dn, attr, 0 );
365
366         Debug( LDAP_DEBUG_ACL,
367                 "=> acl_mask: to value \"%s\" by \"%s\", (%s) \n",
368                 val ? val->bv_val : "*",
369                 op->o_ndn ?  op->o_ndn : "",
370                 accessmask2str( *mask, accessmaskbuf ) );
371
372         for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
373                 slap_access_mask_t oldmask, modmask;
374
375                 ACL_INVALIDATE( modmask );
376
377                 /* AND <who> clauses */
378                 if ( b->a_dn_pat != NULL ) {
379                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
380                                 b->a_dn_pat, 0, 0);
381                         /*
382                          * if access applies to the entry itself, and the
383                          * user is bound as somebody in the same namespace as
384                          * the entry, OR the given dn matches the dn pattern
385                          */
386                         if ( strcmp( b->a_dn_pat, "anonymous" ) == 0 ) {
387                                 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
388                                         continue;
389                                 }
390
391                         } else if ( strcmp( b->a_dn_pat, "users" ) == 0 ) {
392                                 if (op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
393                                         continue;
394                                 }
395
396                         } else if ( strcmp( b->a_dn_pat, "self" ) == 0 ) {
397                                 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
398                                         continue;
399                                 }
400                                 
401                                 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn ) != 0 ) {
402                                         continue;
403                                 }
404
405                         } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
406                                 if ( strcmp( b->a_dn_pat, "*" ) != 0 ) {
407                                         int ret = regex_matches( b->a_dn_pat,
408                                                 op->o_ndn, e->e_ndn, matches );
409
410                                         if( ret == 0 ) {
411                                                 continue;
412                                         }
413                                 }
414
415                         } else {
416                                 if ( e->e_dn == NULL )
417                                         continue;
418
419                                 patlen = strlen( b->a_dn_pat );
420                                 odnlen = strlen( op->o_ndn );
421                                 if ( odnlen < patlen )
422                                         continue;
423
424                                 if ( b->a_dn_style == ACL_STYLE_BASE ) {
425                                         /* base dn -- entire object DN must match */
426                                         if ( odnlen != patlen )
427                                                 continue;
428
429                                 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
430                                         char *rdn;
431                                         int rdnlen = -1;
432
433                                         if ( odnlen <= patlen )
434                                                 continue;
435
436                                         if ( op->o_ndn[odnlen - patlen - 1] != ',' )
437                                                 continue;
438
439                                         rdn = dn_rdn( NULL, op->o_ndn );
440                                         if ( rdn != NULL ) {
441                                                 rdnlen = strlen( rdn );
442                                                 ch_free( rdn );
443                                         }
444                                         if ( rdnlen != odnlen - patlen - 1 )
445                                                 continue;
446
447                                 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
448                                         if ( odnlen > patlen && op->o_ndn[odnlen - patlen - 1] != ',' )
449                                                 continue;
450
451                                 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
452                                         if ( odnlen <= patlen )
453                                                 continue;
454                                         if ( op->o_ndn[odnlen - patlen - 1] != ',' )
455                                                 continue;
456                                 }
457
458                                 if ( strcmp( b->a_dn_pat, op->o_ndn + odnlen - patlen ) != 0 )
459                                         continue;
460
461                         }
462                 }
463
464                 if ( b->a_sockurl_pat != NULL ) {
465                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
466                                 b->a_sockurl_pat, 0, 0 );
467
468                         if ( strcmp( b->a_sockurl_pat, "*" ) != 0) {
469                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
470                                         if (!regex_matches( b->a_sockurl_pat, conn->c_listener_url,
471                                                         e->e_ndn, matches ) ) 
472                                         {
473                                                 continue;
474                                         }
475                                 } else {
476                                         if ( strcasecmp( b->a_sockurl_pat, conn->c_listener_url ) == 0 )
477                                                 continue;
478                                 }
479                         }
480                 }
481
482                 if ( b->a_domain_pat != NULL ) {
483                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
484                                 b->a_domain_pat, 0, 0 );
485
486                         if ( strcmp( b->a_domain_pat, "*" ) != 0) {
487                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
488                                         if (!regex_matches( b->a_domain_pat, conn->c_peer_domain,
489                                                         e->e_ndn, matches ) ) 
490                                         {
491                                                 continue;
492                                         }
493                                 } else {
494                                         if ( strcasecmp( b->a_domain_pat, conn->c_peer_domain ) == 0 )
495                                                 continue;
496                                 }
497                         }
498                 }
499
500                 if ( b->a_peername_pat != NULL ) {
501                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
502                                 b->a_peername_pat, 0, 0 );
503
504                         if ( strcmp( b->a_peername_pat, "*" ) != 0) {
505                                 if ( b->a_peername_style == ACL_STYLE_REGEX) {
506                                         if (!regex_matches( b->a_peername_pat, conn->c_peer_name,
507                                                         e->e_ndn, matches ) ) 
508                                         {
509                                                 continue;
510                                         }
511                                 } else {
512                                         if ( strcasecmp( b->a_peername_pat, conn->c_peer_name ) == 0 )
513                                                 continue;
514                                 }
515                         }
516                 }
517
518                 if ( b->a_sockname_pat != NULL ) {
519                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
520                                 b->a_sockname_pat, 0, 0 );
521
522                         if ( strcmp( b->a_sockname_pat, "*" ) != 0) {
523                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
524                                         if (!regex_matches( b->a_sockname_pat, conn->c_sock_name,
525                                                         e->e_ndn, matches ) ) 
526                                         {
527                                                 continue;
528                                         }
529                                 } else {
530                                         if ( strcasecmp( b->a_sockname_pat, conn->c_sock_name ) == 0 )
531                                                 continue;
532                                 }
533                         }
534                 }
535
536                 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
537                         Attribute       *at;
538                         struct berval   bv;
539                         int rc, match = 0;
540                         const char *text;
541                         const char *desc = b->a_dn_at->ad_cname->bv_val;
542
543                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
544                                 desc, 0, 0);
545
546                         bv.bv_val = op->o_ndn;
547                         bv.bv_len = strlen( bv.bv_val );
548
549                         /* see if asker is listed in dnattr */
550                         for( at = attrs_find( e->e_attrs, b->a_dn_at );
551                                 at != NULL;
552                                 at = attrs_find( at->a_next, b->a_dn_at ) )
553                         {
554                                 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
555                                         /* found it */
556                                         match = 1;
557                                         break;
558                                 }
559                         }
560
561                         if( match ) {
562                                 /* have a dnattr match. if this is a self clause then
563                                  * the target must also match the op dn.
564                                  */
565                                 if ( b->a_dn_self ) {
566                                         /* check if the target is an attribute. */
567                                         if ( val == NULL )
568                                                 continue;
569                                         /* target is attribute, check if the attribute value
570                                          * is the op dn.
571                                          */
572                                         rc = value_match( &match, b->a_dn_at,
573                                                 b->a_dn_at->ad_type->sat_equality, 0,
574                                                 val, &bv, &text );
575                                         /* on match error or no match, fail the ACL clause */
576                                         if (rc != LDAP_SUCCESS || match != 0 )
577                                                 continue;
578                                 }
579                         } else {
580                                 /* no dnattr match, check if this is a self clause */
581                                 if ( ! b->a_dn_self )
582                                         continue;
583                                 /* this is a self clause, check if the target is an
584                                  * attribute.
585                                  */
586                                 if ( val == NULL )
587                                         continue;
588                                 /* target is attribute, check if the attribute value
589                                  * is the op dn.
590                                  */
591                                 rc = value_match( &match, b->a_dn_at,
592                                         b->a_dn_at->ad_type->sat_equality, 0,
593                                         val, &bv, &text );
594
595                                 /* on match error or no match, fail the ACL clause */
596                                 if (rc != LDAP_SUCCESS || match != 0 )
597                                         continue;
598                         }
599                 }
600
601                 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
602                         char buf[1024];
603
604                         /* b->a_group is an unexpanded entry name, expanded it should be an 
605                          * entry with objectclass group* and we test to see if odn is one of
606                          * the values in the attribute group
607                          */
608                         /* see if asker is listed in dnattr */
609                         if ( b->a_group_style == ACL_STYLE_REGEX ) {
610                                 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
611                                 if ( dn_normalize(buf) == NULL ) {
612                                         /* did not expand to a valid dn */
613                                         continue;
614                                 }
615                         } else {
616                                 strncpy( buf, b->a_group_pat, sizeof(buf) - 1 );
617                                 buf[sizeof(buf) - 1] = 0;
618                         }
619
620                         if (backend_group(be, e, buf, op->o_ndn,
621                                 b->a_group_oc, b->a_group_at) != 0)
622                         {
623                                 continue;
624                         }
625                 }
626
627                 if ( b->a_set_pat != NULL ) {
628                         struct berval bv;
629
630                         bv.bv_val = b->a_set_pat;
631                         bv.bv_len = strlen(b->a_set_pat);
632                         if (aci_match_set( &bv, be, e, conn, op, 0 ) == 0) {
633                                 continue;
634                         }
635                 }
636
637 #ifdef SLAPD_ACI_ENABLED
638                 if ( b->a_aci_at != NULL ) {
639                         Attribute       *at;
640                         slap_access_t grant, deny, tgrant, tdeny;
641
642                         /* this case works different from the others above.
643                          * since aci's themselves give permissions, we need
644                          * to first check b->a_mask, the ACL's access level.
645                          */
646
647                         if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
648                                 continue;
649                         }
650
651                         if ( e->e_dn == NULL ) {
652                                 continue;
653                         }
654
655                         /* first check if the right being requested
656                          * is allowed by the ACL clause.
657                          */
658                         if ( ! ACL_GRANT( b->a_mask, *mask ) ) {
659                                 continue;
660                         }
661
662                         /* get the aci attribute */
663                         at = attr_find( e->e_attrs, b->a_aci_at );
664                         if ( at == NULL ) {
665                                 continue;
666                         }
667
668                         /* start out with nothing granted, nothing denied */
669                         ACL_INIT(tgrant);
670                         ACL_INIT(tdeny);
671
672                         /* the aci is an multi-valued attribute.  The
673                          * rights are determined by OR'ing the individual
674                          * rights given by the acis.
675                          */
676                         for ( i = 0; at->a_vals[i] != NULL; i++ ) {
677                                 if (aci_mask( be, conn, op,
678                                         e, desc, val, at->a_vals[i],
679                                         matches, &grant, &deny ) != 0)
680                                 {
681                                         tgrant |= grant;
682                                         tdeny |= deny;
683                                 }
684                         }
685
686                         /* remove anything that the ACL clause does not allow */
687                         tgrant &= b->a_mask & ACL_PRIV_MASK;
688                         tdeny &= ACL_PRIV_MASK;
689
690                         /* see if we have anything to contribute */
691                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
692                                 continue;
693                         }
694
695                         /* this could be improved by changing acl_mask so that it can deal with
696                          * by clauses that return grant/deny pairs.  Right now, it does either
697                          * additive or subtractive rights, but not both at the same time.  So,
698                          * we need to combine the grant/deny pair into a single rights mask in
699                          * a smart way:  if either grant or deny is "empty", then we use the
700                          * opposite as is, otherwise we remove any denied rights from the grant
701                          * rights mask and construct an additive mask.
702                          */
703                         if (ACL_IS_INVALID(tdeny)) {
704                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
705
706                         } else if (ACL_IS_INVALID(tgrant)) {
707                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
708
709                         } else {
710                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
711                         }
712
713                 } else
714 #endif
715                 {
716                         modmask = b->a_mask;
717                 }
718
719
720                 Debug( LDAP_DEBUG_ACL,
721                         "<= acl_mask: [%d] applying %s (%s)\n",
722                         i, accessmask2str( modmask, accessmaskbuf ), 
723                         b->a_type == ACL_CONTINUE
724                                 ? "continue"
725                                 : b->a_type == ACL_BREAK
726                                         ? "break"
727                                         : "stop" );
728
729                 /* save old mask */
730                 oldmask = *mask;
731
732                 if( ACL_IS_ADDITIVE(modmask) ) {
733                         /* add privs */
734                         ACL_PRIV_SET( *mask, modmask );
735
736                         /* cleanup */
737                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
738
739                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
740                         /* substract privs */
741                         ACL_PRIV_CLR( *mask, modmask );
742
743                         /* cleanup */
744                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
745
746                 } else {
747                         /* assign privs */
748                         *mask = modmask;
749                 }
750
751                 Debug( LDAP_DEBUG_ACL,
752                         "<= acl_mask: [%d] mask: %s\n",
753                         i, accessmask2str(*mask, accessmaskbuf), 0 );
754
755                 if( b->a_type == ACL_CONTINUE ) {
756                         continue;
757
758                 } else if ( b->a_type == ACL_BREAK ) {
759                         return ACL_BREAK;
760
761                 } else {
762                         return ACL_STOP;
763                 }
764         }
765
766         Debug( LDAP_DEBUG_ACL,
767                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
768                 accessmask2str(*mask, accessmaskbuf), 0, 0 );
769         return ACL_STOP;
770 }
771
772 /*
773  * acl_check_modlist - check access control on the given entry to see if
774  * it allows the given modifications by the user associated with op.
775  * returns      1       if mods allowed ok
776  *                      0       mods not allowed
777  */
778
779 int
780 acl_check_modlist(
781     Backend     *be,
782     Connection  *conn,
783     Operation   *op,
784     Entry       *e,
785     Modifications       *mlist
786 )
787 {
788         int             i;
789
790         assert( be != NULL );
791
792         /* short circuit root database access */
793         if ( be_isroot( be, op->o_ndn ) ) {
794                 Debug( LDAP_DEBUG_ACL,
795                         "<= acl_access_allowed: granted to database root\n",
796                     0, 0, 0 );
797                 return 1;
798         }
799
800         /* use backend default access if no backend acls */
801         if( be != NULL && be->be_acl == NULL ) {
802                 Debug( LDAP_DEBUG_ACL,
803                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
804                         access2str( ACL_WRITE ),
805                         be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
806
807                 return be->be_dfltaccess >= ACL_WRITE;
808
809 #ifdef notdef
810         /* be is always non-NULL */
811         /* use global default access if no global acls */
812         } else if ( be == NULL && global_acl == NULL ) {
813                 Debug( LDAP_DEBUG_ACL,
814                         "=> access_allowed: global default %s access %s to \"%s\"\n",
815                         access2str( ACL_WRITE ),
816                         global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
817
818                 return global_default_access >= ACL_WRITE;
819 #endif
820         }
821
822         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
823                 /*
824                  * no-user-modification operational attributes are ignored
825                  * by ACL_WRITE checking as any found here are not provided
826                  * by the user
827                  */
828                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
829                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
830                                 " modify access granted\n",
831                                 mlist->sml_desc->ad_cname->bv_val, 0, 0 );
832                         continue;
833                 }
834
835                 switch ( mlist->sml_op ) {
836                 case LDAP_MOD_REPLACE:
837                 case LDAP_MOD_ADD:
838                         if ( mlist->sml_bvalues == NULL ) {
839                                 break;
840                         }
841                         for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
842                                 if ( ! access_allowed( be, conn, op, e,
843                                         mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
844                                 {
845                                         return( 0 );
846                                 }
847                         }
848                         break;
849
850                 case LDAP_MOD_DELETE:
851                         if ( mlist->sml_bvalues == NULL ) {
852                                 if ( ! access_allowed( be, conn, op, e,
853                                         mlist->sml_desc, NULL, ACL_WRITE ) )
854                                 {
855                                         return( 0 );
856                                 }
857                                 break;
858                         }
859                         for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
860                                 if ( ! access_allowed( be, conn, op, e,
861                                         mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
862                                 {
863                                         return( 0 );
864                                 }
865                         }
866                         break;
867                 }
868         }
869
870         return( 1 );
871 }
872
873 static char *
874 aci_bvstrdup( struct berval *bv )
875 {
876         char *s;
877
878         s = (char *)ch_malloc(bv->bv_len + 1);
879         if (s != NULL) {
880                 memcpy(s, bv->bv_val, bv->bv_len);
881                 s[bv->bv_len] = 0;
882         }
883         return(s);
884 }
885
886 static int
887 aci_strbvcmp(
888         const char *s,
889         struct berval *bv )
890 {
891         int res, len;
892
893         res = strncasecmp( s, bv->bv_val, bv->bv_len );
894         if (res)
895                 return(res);
896         len = strlen(s);
897         if (len > (int)bv->bv_len)
898                 return(1);
899         if (len < (int)bv->bv_len)
900                 return(-1);
901         return(0);
902 }
903
904 static int
905 aci_get_part(
906         struct berval *list,
907         int ix,
908         char sep,
909         struct berval *bv )
910 {
911         int len;
912         char *p;
913
914         if (bv) {
915                 bv->bv_len = 0;
916                 bv->bv_val = NULL;
917         }
918         len = list->bv_len;
919         p = list->bv_val;
920         while (len >= 0 && --ix >= 0) {
921                 while (--len >= 0 && *p++ != sep) ;
922         }
923         while (len >= 0 && *p == ' ') {
924                 len--;
925                 p++;
926         }
927         if (len < 0)
928                 return(-1);
929
930         if (!bv)
931                 return(0);
932
933         bv->bv_val = p;
934         while (--len >= 0 && *p != sep) {
935                 bv->bv_len++;
936                 p++;
937         }
938         while (bv->bv_len > 0 && *--p == ' ')
939                 bv->bv_len--;
940         return(bv->bv_len);
941 }
942
943 char **
944 aci_set_gather (void *cookie, char *name, char *attr)
945 {
946         struct {
947         Backend *be;
948         Entry *e;
949         Connection *conn;
950         Operation *op;
951         } *cp = (void *)cookie;
952         struct berval **bvals = NULL;
953         char **vals = NULL;
954         char *ndn;
955         int i;
956
957         /* this routine needs to return the bervals instead of
958          * plain strings, since syntax is not known.  It should
959          * also return the syntax or some "comparison cookie".
960          */
961
962         if ((ndn = ch_strdup(name)) != NULL) {
963                 if (dn_normalize(ndn) != NULL) {
964                         char *text;
965                         AttributeDescription *desc = NULL;
966                         if (slap_str2ad(attr, &desc, &text) == 0) {
967                                 backend_attribute(cp->be, NULL /*cp->conn*/,
968                                                                         NULL /*cp->op*/, cp->e,
969                                                                         ndn, desc, &bvals);
970                                 if (bvals != NULL) {
971                                         for (i = 0; bvals[i] != NULL; i++) { }
972                                         vals = ch_calloc(i + 1, sizeof(char *));
973                                         if (vals != NULL) {
974                                                 while (--i >= 0) {
975                                                         vals[i] = bvals[i]->bv_val;
976                                                         bvals[i]->bv_val = NULL;
977                                                 }
978                                         }
979                                         ber_bvecfree(bvals);
980                                 }
981                                 ad_free(desc, 1);
982                         }
983                 }
984                 ch_free(ndn);
985         }
986         return(vals);
987 }
988
989 static int
990 aci_match_set (
991         struct berval *subj,
992     Backend *be,
993     Entry *e,
994     Connection *conn,
995     Operation *op,
996     int setref
997 )
998 {
999         char *set = NULL;
1000         int rc = 0;
1001         struct {
1002         Backend *be;
1003         Entry *e;
1004         Connection *conn;
1005         Operation *op;
1006         } cookie;
1007
1008         if (setref == 0) {
1009                 set = aci_bvstrdup(subj);
1010         } else {
1011                 struct berval bv;
1012                 char *subjdn;
1013                 char *setat;
1014                 struct berval **bvals;
1015                 char *text;
1016                 AttributeDescription *desc = NULL;
1017
1018                 /* format of string is "entry/setAttrName" */
1019                 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1020                         return(0);
1021                 }
1022
1023                 subjdn = aci_bvstrdup(&bv);
1024                 if ( subjdn == NULL ) {
1025                         return(0);
1026                 }
1027
1028                 if ( aci_get_part(subj, 1, '/', &bv) < 0 ) {
1029                         setat = ch_strdup( SLAPD_ACI_SET_ATTR );
1030                 } else {
1031                         setat = aci_bvstrdup(&bv);
1032                 }
1033                 if ( setat != NULL ) {
1034                         if ( dn_normalize(subjdn) != NULL
1035                                 && slap_str2ad(setat, &desc, &text) == 0 )
1036                         {
1037                                 backend_attribute(be, NULL, NULL, e,
1038                                                                 subjdn, desc, &bvals);
1039                                 ad_free(desc, 1);
1040                                 if ( bvals != NULL ) {
1041                                         if ( bvals[0] != NULL )
1042                                                 set = ch_strdup(bvals[0]->bv_val);
1043                                         ber_bvecfree(bvals);
1044                                 }
1045                         }
1046                         ch_free(setat);
1047                 }
1048                 ch_free(subjdn);
1049         }
1050
1051         if (set != NULL) {
1052                 cookie.be = be;
1053                 cookie.e = e;
1054                 cookie.conn = conn;
1055                 cookie.op = op;
1056                 rc = (set_filter(aci_set_gather, &cookie, set, op->o_ndn, e->e_ndn, NULL) > 0);
1057                 ch_free(set);
1058         }
1059         return(rc);
1060 }
1061
1062 #ifdef SLAPD_ACI_ENABLED
1063 static int
1064 aci_list_map_rights(
1065         struct berval *list )
1066 {
1067         struct berval bv;
1068         slap_access_t mask;
1069         int i;
1070
1071         ACL_INIT(mask);
1072         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1073                 if (bv.bv_len <= 0)
1074                         continue;
1075                 switch (*bv.bv_val) {
1076                 case 'c':
1077                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1078                         break;
1079                 case 's':
1080                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1081                          * the right 's' to mean "set", but in the examples states
1082                          * that the right 's' means "search".  The latter definition
1083                          * is used here.
1084                          */
1085                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1086                         break;
1087                 case 'r':
1088                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
1089                         break;
1090                 case 'w':
1091                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1092                         break;
1093                 case 'x':
1094                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
1095                          * define any equivalent to the AUTH right, so I've just used
1096                          * 'x' for now.
1097                          */
1098                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1099                         break;
1100                 default:
1101                         break;
1102                 }
1103
1104         }
1105         return(mask);
1106 }
1107
1108 static int
1109 aci_list_has_attr(
1110         struct berval *list,
1111         const char *attr,
1112         struct berval *val )
1113 {
1114         struct berval bv, left, right;
1115         int i;
1116
1117         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1118                 if (aci_get_part(&bv, 0, '=', &left) < 0
1119                         || aci_get_part(&bv, 1, '=', &right) < 0)
1120                 {
1121                         if (aci_strbvcmp(attr, &bv) == 0)
1122                                 return(1);
1123                 } else if (val == NULL) {
1124                         if (aci_strbvcmp(attr, &left) == 0)
1125                                 return(1);
1126                 } else {
1127                         if (aci_strbvcmp(attr, &left) == 0) {
1128                                 /* this is experimental code that implements a
1129                                  * simple (prefix) match of the attribute value.
1130                                  * the ACI draft does not provide for aci's that
1131                                  * apply to specific values, but it would be
1132                                  * nice to have.  If the <attr> part of an aci's
1133                                  * rights list is of the form <attr>=<value>,
1134                                  * that means the aci applies only to attrs with
1135                                  * the given value.  Furthermore, if the attr is
1136                                  * of the form <attr>=<value>*, then <value> is
1137                                  * treated as a prefix, and the aci applies to 
1138                                  * any value with that prefix.
1139                                  *
1140                                  * Ideally, this would allow r.e. matches.
1141                                  */
1142                                 if (aci_get_part(&right, 0, '*', &left) < 0
1143                                         || right.bv_len <= left.bv_len)
1144                                 {
1145                                         if (aci_strbvcmp(val->bv_val, &right) == 0)
1146                                                 return(1);
1147                                 } else if (val->bv_len >= left.bv_len) {
1148                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1149                                                 return(1);
1150                                 }
1151                         }
1152                 }
1153         }
1154         return(0);
1155 }
1156
1157 static slap_access_t
1158 aci_list_get_attr_rights(
1159         struct berval *list,
1160         const char *attr,
1161         struct berval *val )
1162 {
1163     struct berval bv;
1164     slap_access_t mask;
1165     int i;
1166
1167         /* loop through each rights/attr pair, skip first part (action) */
1168         ACL_INIT(mask);
1169         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1170                 if (aci_list_has_attr(&bv, attr, val) == 0)
1171                         continue;
1172                 if (aci_get_part(list, i, ';', &bv) < 0)
1173                         continue;
1174                 mask |= aci_list_map_rights(&bv);
1175         }
1176         return(mask);
1177 }
1178
1179 static int
1180 aci_list_get_rights(
1181         struct berval *list,
1182         const char *attr,
1183         struct berval *val,
1184         slap_access_t *grant,
1185         slap_access_t *deny )
1186 {
1187     struct berval perm, actn;
1188     slap_access_t *mask;
1189     int i, found;
1190
1191         if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
1192                 attr = "[entry]";
1193         }
1194
1195         found = 0;
1196         ACL_INIT(*grant);
1197         ACL_INIT(*deny);
1198         /* loop through each permissions clause */
1199         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1200                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1201                         continue;
1202                 if (aci_strbvcmp( "grant", &actn ) == 0) {
1203                         mask = grant;
1204                 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
1205                         mask = deny;
1206                 } else {
1207                         continue;
1208                 }
1209
1210                 found = 1;
1211                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1212                 *mask |= aci_list_get_attr_rights(&perm, "[all]", NULL);
1213         }
1214         return(found);
1215 }
1216
1217 static int
1218 aci_group_member (
1219         struct berval *subj,
1220         const char *defgrpoc,
1221         const char *defgrpat,
1222     Backend             *be,
1223     Entry               *e,
1224     Operation           *op,
1225         regmatch_t      *matches
1226 )
1227 {
1228         struct berval bv;
1229         char *subjdn, *grpdn = NULL;
1230         char *grpoc;
1231         char *grpat;
1232         ObjectClass *grp_oc = NULL;
1233         AttributeDescription *grp_ad = NULL;
1234         char *text;
1235         int rc;
1236
1237         /* format of string is "group/objectClassValue/groupAttrName" */
1238         if (aci_get_part(subj, 0, '/', &bv) < 0) {
1239                 return(0);
1240         }
1241
1242         subjdn = aci_bvstrdup(&bv);
1243         if (subjdn == NULL) {
1244                 return(0);
1245         }
1246
1247         if (aci_get_part(subj, 1, '/', &bv) < 0) {
1248                 grpoc = ch_strdup( defgrpoc );
1249         } else {
1250                 grpoc = aci_bvstrdup(&bv);
1251         }
1252
1253         if (aci_get_part(subj, 2, '/', &bv) < 0) {
1254                 grpat = ch_strdup( defgrpat );
1255         } else {
1256                 grpat = aci_bvstrdup(&bv);
1257         }
1258
1259         rc = slap_str2ad( grpat, &grp_ad, &text );
1260         if( rc != LDAP_SUCCESS ) {
1261                 rc = 0;
1262                 goto done;
1263         }
1264         rc = 0;
1265
1266         grp_oc = oc_find( grpoc );
1267         grpdn = (char *)ch_malloc(1024);
1268
1269         if (grp_oc != NULL && grp_ad != NULL && grpdn != NULL) {
1270                 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
1271                 if ( dn_normalize(grpdn) != NULL ) {
1272                         rc = (backend_group(be, e, grpdn, op->o_ndn, grp_oc, grp_ad) == 0);
1273                 }
1274         }
1275
1276 done:
1277         if( grp_ad != NULL ) ad_free( grp_ad, 1 );
1278         ch_free(grpdn);
1279         ch_free(grpat);
1280         ch_free(grpoc);
1281         ch_free(subjdn);
1282         return(rc);
1283 }
1284
1285 static int
1286 aci_mask(
1287     Backend                     *be,
1288     Connection          *conn,
1289     Operation           *op,
1290     Entry                       *e,
1291         AttributeDescription *desc,
1292     struct berval       *val,
1293     struct berval       *aci,
1294         regmatch_t              *matches,
1295         slap_access_t   *grant,
1296         slap_access_t   *deny
1297 )
1298 {
1299     struct berval bv, perms, sdn;
1300     char *subjdn;
1301         int rc;
1302         char *attr = desc->ad_cname->bv_val;
1303
1304         /* parse an aci of the form:
1305                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1306
1307            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1308            a full description of the format for this attribute.
1309
1310            For now, this routine only supports scope=entry.
1311          */
1312
1313         /* check that the aci has all 5 components */
1314         if (aci_get_part(aci, 4, '#', NULL) < 0)
1315                 return(0);
1316
1317         /* check that the aci family is supported */
1318         if (aci_get_part(aci, 0, '#', &bv) < 0)
1319                 return(0);
1320
1321         /* check that the scope is "entry" */
1322         if (aci_get_part(aci, 1, '#', &bv) < 0
1323                 || aci_strbvcmp( "entry", &bv ) != 0)
1324         {
1325                 return(0);
1326         }
1327
1328         /* get the list of permissions clauses, bail if empty */
1329         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1330                 return(0);
1331
1332         /* check if any permissions allow desired access */
1333         if (aci_list_get_rights(&perms, attr, val, grant, deny) == 0)
1334                 return(0);
1335
1336         /* see if we have a DN match */
1337         if (aci_get_part(aci, 3, '#', &bv) < 0)
1338                 return(0);
1339
1340         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1341                 return(0);
1342
1343         if (aci_strbvcmp( "access-id", &bv ) == 0) {
1344                 subjdn = aci_bvstrdup(&sdn);
1345                 if (subjdn == NULL)
1346                         return(0);
1347                 rc = 1;
1348                 if ( dn_normalize(subjdn) != NULL )
1349                         if (strcasecmp(op->o_ndn, subjdn) != 0)
1350                                 rc = 0;
1351                 ch_free(subjdn);
1352                 return(rc);
1353         }
1354
1355         if (aci_strbvcmp( "self", &bv ) == 0) {
1356                 if (strcasecmp(op->o_ndn, e->e_ndn) == 0)
1357                         return(1);
1358
1359         } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
1360                 char *dnattr = aci_bvstrdup(&sdn);
1361                 Attribute *at;
1362                 AttributeDescription *ad = NULL;
1363                 const char *text;
1364
1365                 rc = slap_str2ad( dnattr, &ad, &text );
1366                 ch_free( dnattr );
1367
1368                 if( rc != LDAP_SUCCESS ) {
1369                         return 0;
1370                 }
1371
1372                 rc = 0;
1373
1374                 bv.bv_val = op->o_ndn;
1375                 bv.bv_len = strlen( bv.bv_val );
1376
1377                 for(at = attrs_find( e->e_attrs, ad );
1378                         at != NULL;
1379                         at = attrs_find( at->a_next, ad ) )
1380                 {
1381                         if (value_find( ad, at->a_vals, &bv) == 0 ) {
1382                                 rc = 1;
1383                                 break;
1384                         }
1385                 }
1386
1387                 ad_free( ad, 1 );
1388                 return rc;
1389
1390
1391         } else if (aci_strbvcmp( "group", &bv ) == 0) {
1392                 if (aci_group_member(&sdn, SLAPD_GROUP_CLASS, SLAPD_GROUP_ATTR, be, e, op, matches))
1393                         return(1);
1394
1395         } else if (aci_strbvcmp( "role", &bv ) == 0) {
1396                 if (aci_group_member(&sdn, SLAPD_ROLE_CLASS, SLAPD_ROLE_ATTR, be, e, op, matches))
1397                         return(1);
1398
1399         } else if (aci_strbvcmp( "set", &bv ) == 0) {
1400                 if (aci_match_set(&sdn, be, e, conn, op, 0))
1401                         return(1);
1402
1403         } else if (aci_strbvcmp( "set-ref", &bv ) == 0) {
1404                 if (aci_match_set(&sdn, be, e, conn, op, 1))
1405                         return(1);
1406
1407         }
1408
1409         return(0);
1410 }
1411
1412 #endif  /* SLAPD_ACI_ENABLED */
1413
1414 static void
1415 string_expand(
1416         char *newbuf,
1417         int bufsiz,
1418         char *pat,
1419         char *match,
1420         regmatch_t *matches)
1421 {
1422         int     size;
1423         char   *sp;
1424         char   *dp;
1425         int     flag;
1426
1427         size = 0;
1428         newbuf[0] = '\0';
1429         bufsiz--; /* leave space for lone $ */
1430
1431         flag = 0;
1432         for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
1433                 /* did we previously see a $ */
1434                 if (flag) {
1435                         if (*sp == '$') {
1436                                 *dp++ = '$';
1437                                 size++;
1438                         } else if (*sp >= '0' && *sp <= '9' ) {
1439                                 int     n;
1440                                 int     i;
1441                                 int     l;
1442
1443                                 n = *sp - '0';
1444                                 *dp = '\0';
1445                                 i = matches[n].rm_so;
1446                                 l = matches[n].rm_eo; 
1447                                 for ( ; size < 512 && i < l; size++, i++ ) {
1448                                         *dp++ = match[i];
1449                                         size++;
1450                                 }
1451                                 *dp = '\0';
1452                         }
1453                         flag = 0;
1454                 } else {
1455                         if (*sp == '$') {
1456                                 flag = 1;
1457                         } else {
1458                                 *dp++ = *sp;
1459                                 size++;
1460                         }
1461                 }
1462         }
1463
1464         if (flag) {
1465                 /* must have ended with a single $ */
1466                 *dp++ = '$';
1467                 size++;
1468         }
1469
1470         *dp = '\0';
1471
1472         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %s\n", pat, 0, 0 );
1473         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1474 }
1475
1476 static int
1477 regex_matches(
1478         char *pat,                              /* pattern to expand and match against */
1479         char *str,                              /* string to match against pattern */
1480         char *buf,                              /* buffer with $N expansion variables */
1481         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
1482 )
1483 {
1484         regex_t re;
1485         char newbuf[512];
1486         int     rc;
1487
1488         if(str == NULL) str = "";
1489
1490         string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1491         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1492                 char error[512];
1493                 regerror(rc, &re, error, sizeof(error));
1494
1495                 Debug( LDAP_DEBUG_TRACE,
1496                     "compile( \"%s\", \"%s\") failed %s\n",
1497                         pat, str, error );
1498                 return( 0 );
1499         }
1500
1501         rc = regexec(&re, str, 0, NULL, 0);
1502         regfree( &re );
1503
1504         Debug( LDAP_DEBUG_TRACE,
1505             "=> regex_matches: string:   %s\n", str, 0, 0 );
1506         Debug( LDAP_DEBUG_TRACE,
1507             "=> regex_matches: rc: %d %s\n",
1508                 rc, !rc ? "matches" : "no matches", 0 );
1509         return( !rc );
1510 }
1511