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