]> git.sur5r.net Git - openldap/blob - servers/slapd/aclparse.c
ITS#4273 fix from HEAD
[openldap] / servers / slapd / aclparse.c
1 /* aclparse.c - routines to parse and check acl's */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/ctype.h>
32 #include <ac/regex.h>
33 #include <ac/socket.h>
34 #include <ac/string.h>
35 #include <ac/unistd.h>
36
37 #include "slap.h"
38 #include "lber_pvt.h"
39 #include "lutil.h"
40
41 static const char style_base[] = "base";
42 char *style_strings[] = {
43         "regex",
44         "expand",
45         "exact",
46         "one",
47         "subtree",
48         "children",
49         "level",
50         "attrof",
51         "anonymous",
52         "users",
53         "self",
54         "ip",
55         "path",
56         NULL
57 };
58
59 static void             split(char *line, int splitchar, char **left, char **right);
60 static void             access_append(Access **l, Access *a);
61 static int              acl_usage(void);
62
63 static void             acl_regex_normalized_dn(const char *src, struct berval *pat);
64
65 #ifdef LDAP_DEBUG
66 static void             print_acl(Backend *be, AccessControl *a);
67 #endif
68
69 static int              check_scope( BackendDB *be, AccessControl *a );
70
71 #ifdef SLAP_DYNACL
72 static int
73 slap_dynacl_config(
74         const char *fname,
75         int lineno,
76         Access *b,
77         const char *name,
78         const char *opts,
79         slap_style_t sty,
80         const char *right )
81 {
82         slap_dynacl_t   *da, *tmp;
83         int             rc = 0;
84
85         for ( da = b->a_dynacl; da; da = da->da_next ) {
86                 if ( strcasecmp( da->da_name, name ) == 0 ) {
87                         Debug( LDAP_DEBUG_ANY,
88                                 "%s: line %d: dynacl \"%s\" already specified.\n",
89                                 fname, lineno, name );
90                         return acl_usage();
91                 }
92         }
93
94         da = slap_dynacl_get( name );
95         if ( da == NULL ) {
96                 return -1;
97         }
98
99         tmp = ch_malloc( sizeof( slap_dynacl_t ) );
100         *tmp = *da;
101
102         if ( tmp->da_parse ) {
103                 rc = ( *tmp->da_parse )( fname, lineno, opts, sty, right, &tmp->da_private );
104                 if ( rc ) {
105                         ch_free( tmp );
106                         return rc;
107                 }
108         }
109
110         tmp->da_next = b->a_dynacl;
111         b->a_dynacl = tmp;
112
113         return 0;
114 }
115 #endif /* SLAP_DYNACL */
116
117 static void
118 regtest(const char *fname, int lineno, char *pat) {
119         int e;
120         regex_t re;
121
122         char            buf[ SLAP_TEXT_BUFLEN ];
123         unsigned        size;
124
125         char *sp;
126         char *dp;
127         int  flag;
128
129         sp = pat;
130         dp = buf;
131         size = 0;
132         buf[0] = '\0';
133
134         for (size = 0, flag = 0; (size < sizeof(buf)) && *sp; sp++) {
135                 if (flag) {
136                         if (*sp == '$'|| (*sp >= '0' && *sp <= '9')) {
137                                 *dp++ = *sp;
138                                 size++;
139                         }
140                         flag = 0;
141
142                 } else {
143                         if (*sp == '$') {
144                                 flag = 1;
145                         } else {
146                                 *dp++ = *sp;
147                                 size++;
148                         }
149                 }
150         }
151
152         *dp = '\0';
153         if ( size >= (sizeof(buf) - 1) ) {
154                 Debug( LDAP_DEBUG_ANY,
155                         "%s: line %d: regular expression \"%s\" too large\n",
156                         fname, lineno, pat );
157                 (void)acl_usage();
158                 exit( EXIT_FAILURE );
159         }
160
161         if ((e = regcomp(&re, buf, REG_EXTENDED|REG_ICASE))) {
162                 char error[ SLAP_TEXT_BUFLEN ];
163
164                 regerror(e, &re, error, sizeof(error));
165
166                 snprintf( buf, sizeof( buf ),
167                         "regular expression \"%s\" bad because of %s",
168                         pat, error );
169                 Debug( LDAP_DEBUG_ANY,
170                         "%s: line %d: %s\n",
171                         fname, lineno, buf );
172                 acl_usage();
173                 exit( EXIT_FAILURE );
174         }
175         regfree(&re);
176 }
177
178 /*
179  * Experimental
180  *
181  * Check if the pattern of an ACL, if any, matches the scope
182  * of the backend it is defined within.
183  */
184 #define ACL_SCOPE_UNKNOWN       (-2)
185 #define ACL_SCOPE_ERR           (-1)
186 #define ACL_SCOPE_OK            (0)
187 #define ACL_SCOPE_PARTIAL       (1)
188 #define ACL_SCOPE_WARN          (2)
189
190 static int
191 check_scope( BackendDB *be, AccessControl *a )
192 {
193         ber_len_t       patlen;
194         struct berval   dn;
195
196         dn = be->be_nsuffix[0];
197
198         if ( BER_BVISEMPTY( &dn ) ) {
199                 return ACL_SCOPE_OK;
200         }
201
202         if ( !BER_BVISEMPTY( &a->acl_dn_pat ) ||
203                         a->acl_dn_style != ACL_STYLE_REGEX )
204         {
205                 slap_style_t    style = a->acl_dn_style;
206
207                 if ( style == ACL_STYLE_REGEX ) {
208                         char            dnbuf[SLAP_LDAPDN_MAXLEN + 2];
209                         char            rebuf[SLAP_LDAPDN_MAXLEN + 1];
210                         ber_len_t       rebuflen;
211                         regex_t         re;
212                         int             rc;
213                         
214                         /* add trailing '$' to database suffix to form
215                          * a simple trial regex pattern "<suffix>$" */
216                         AC_MEMCPY( dnbuf, be->be_nsuffix[0].bv_val,
217                                 be->be_nsuffix[0].bv_len );
218                         dnbuf[be->be_nsuffix[0].bv_len] = '$';
219                         dnbuf[be->be_nsuffix[0].bv_len + 1] = '\0';
220
221                         if ( regcomp( &re, dnbuf, REG_EXTENDED|REG_ICASE ) ) {
222                                 return ACL_SCOPE_WARN;
223                         }
224
225                         /* remove trailing ')$', if any, from original
226                          * regex pattern */
227                         rebuflen = a->acl_dn_pat.bv_len;
228                         AC_MEMCPY( rebuf, a->acl_dn_pat.bv_val, rebuflen + 1 );
229                         if ( rebuf[rebuflen - 1] == '$' ) {
230                                 rebuf[--rebuflen] = '\0';
231                         }
232                         while ( rebuflen > be->be_nsuffix[0].bv_len && rebuf[rebuflen - 1] == ')' ) {
233                                 rebuf[--rebuflen] = '\0';
234                         }
235                         if ( rebuflen == be->be_nsuffix[0].bv_len ) {
236                                 rc = ACL_SCOPE_WARN;
237                                 goto regex_done;
238                         }
239
240                         /* not a clear indication of scoping error, though */
241                         rc = regexec( &re, rebuf, 0, NULL, 0 )
242                                 ? ACL_SCOPE_WARN : ACL_SCOPE_OK;
243
244 regex_done:;
245                         regfree( &re );
246                         return rc;
247                 }
248
249                 patlen = a->acl_dn_pat.bv_len;
250                 /* If backend suffix is longer than pattern,
251                  * it is a potential mismatch (in the sense
252                  * that a superior naming context could
253                  * match */
254                 if ( dn.bv_len > patlen ) {
255                         /* base is blatantly wrong */
256                         if ( style == ACL_STYLE_BASE ) return ACL_SCOPE_ERR;
257
258                         /* a style of one can be wrong if there is
259                          * more than one level between the suffix
260                          * and the pattern */
261                         if ( style == ACL_STYLE_ONE ) {
262                                 ber_len_t       rdnlen = 0;
263                                 int             sep = 0;
264
265                                 if ( patlen > 0 ) {
266                                         if ( !DN_SEPARATOR( dn.bv_val[dn.bv_len - patlen - 1] )) {
267                                                 return ACL_SCOPE_ERR;
268                                         }
269                                         sep = 1;
270                                 }
271
272                                 rdnlen = dn_rdnlen( NULL, &dn );
273                                 if ( rdnlen != dn.bv_len - patlen - sep )
274                                         return ACL_SCOPE_ERR;
275                         }
276
277                         /* if the trailing part doesn't match,
278                          * then it's an error */
279                         if ( strcmp( a->acl_dn_pat.bv_val,
280                                 &dn.bv_val[dn.bv_len - patlen] ) != 0 )
281                         {
282                                 return ACL_SCOPE_ERR;
283                         }
284
285                         return ACL_SCOPE_PARTIAL;
286                 }
287
288                 switch ( style ) {
289                 case ACL_STYLE_BASE:
290                 case ACL_STYLE_ONE:
291                 case ACL_STYLE_CHILDREN:
292                 case ACL_STYLE_SUBTREE:
293                         break;
294
295                 default:
296                         assert( 0 );
297                         break;
298                 }
299
300                 if ( dn.bv_len < patlen &&
301                         !DN_SEPARATOR( a->acl_dn_pat.bv_val[patlen - dn.bv_len - 1] ))
302                 {
303                         return ACL_SCOPE_ERR;
304                 }
305
306                 if ( strcmp( &a->acl_dn_pat.bv_val[patlen - dn.bv_len], dn.bv_val )
307                         != 0 )
308                 {
309                         return ACL_SCOPE_ERR;
310                 }
311
312                 return ACL_SCOPE_OK;
313         }
314
315         return ACL_SCOPE_UNKNOWN;
316 }
317
318 int
319 parse_acl(
320         Backend *be,
321         const char      *fname,
322         int             lineno,
323         int             argc,
324         char            **argv,
325         int             pos )
326 {
327         int             i;
328         char            *left, *right, *style;
329         struct berval   bv;
330         AccessControl   *a;
331         Access  *b;
332         int rc;
333         const char *text;
334
335         a = NULL;
336         for ( i = 1; i < argc; i++ ) {
337                 /* to clause - select which entries are protected */
338                 if ( strcasecmp( argv[i], "to" ) == 0 ) {
339                         if ( a != NULL ) {
340                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
341                                         "only one to clause allowed in access line\n",
342                                     fname, lineno, 0 );
343                                 return acl_usage();
344                         }
345                         a = (AccessControl *) ch_calloc( 1, sizeof(AccessControl) );
346                         for ( ++i; i < argc; i++ ) {
347                                 if ( strcasecmp( argv[i], "by" ) == 0 ) {
348                                         i--;
349                                         break;
350                                 }
351
352                                 if ( strcasecmp( argv[i], "*" ) == 0 ) {
353                                         if ( !BER_BVISEMPTY( &a->acl_dn_pat ) ||
354                                                 a->acl_dn_style != ACL_STYLE_REGEX )
355                                         {
356                                                 Debug( LDAP_DEBUG_ANY,
357                                                         "%s: line %d: dn pattern"
358                                                         " already specified in to clause.\n",
359                                                         fname, lineno, 0 );
360                                                 return acl_usage();
361                                         }
362
363                                         ber_str2bv( "*", STRLENOF( "*" ), 1, &a->acl_dn_pat );
364                                         continue;
365                                 }
366
367                                 split( argv[i], '=', &left, &right );
368                                 split( left, '.', &left, &style );
369
370                                 if ( right == NULL ) {
371                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
372                                                 "missing \"=\" in \"%s\" in to clause\n",
373                                             fname, lineno, left );
374                                         return acl_usage();
375                                 }
376
377                                 if ( strcasecmp( left, "dn" ) == 0 ) {
378                                         if ( !BER_BVISEMPTY( &a->acl_dn_pat ) ||
379                                                 a->acl_dn_style != ACL_STYLE_REGEX )
380                                         {
381                                                 Debug( LDAP_DEBUG_ANY,
382                                                         "%s: line %d: dn pattern"
383                                                         " already specified in to clause.\n",
384                                                         fname, lineno, 0 );
385                                                 return acl_usage();
386                                         }
387
388                                         if ( style == NULL || *style == '\0' ||
389                                                 strcasecmp( style, "baseObject" ) == 0 ||
390                                                 strcasecmp( style, "base" ) == 0 ||
391                                                 strcasecmp( style, "exact" ) == 0 )
392                                         {
393                                                 a->acl_dn_style = ACL_STYLE_BASE;
394                                                 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
395
396                                         } else if ( strcasecmp( style, "oneLevel" ) == 0 ||
397                                                 strcasecmp( style, "one" ) == 0 )
398                                         {
399                                                 a->acl_dn_style = ACL_STYLE_ONE;
400                                                 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
401
402                                         } else if ( strcasecmp( style, "subtree" ) == 0 ||
403                                                 strcasecmp( style, "sub" ) == 0 )
404                                         {
405                                                 if( *right == '\0' ) {
406                                                         ber_str2bv( "*", STRLENOF( "*" ), 1, &a->acl_dn_pat );
407
408                                                 } else {
409                                                         a->acl_dn_style = ACL_STYLE_SUBTREE;
410                                                         ber_str2bv( right, 0, 1, &a->acl_dn_pat );
411                                                 }
412
413                                         } else if ( strcasecmp( style, "children" ) == 0 ) {
414                                                 a->acl_dn_style = ACL_STYLE_CHILDREN;
415                                                 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
416
417                                         } else if ( strcasecmp( style, "regex" ) == 0 ) {
418                                                 a->acl_dn_style = ACL_STYLE_REGEX;
419
420                                                 if ( *right == '\0' ) {
421                                                         /* empty regex should match empty DN */
422                                                         a->acl_dn_style = ACL_STYLE_BASE;
423                                                         ber_str2bv( right, 0, 1, &a->acl_dn_pat );
424
425                                                 } else if ( strcmp(right, "*") == 0 
426                                                         || strcmp(right, ".*") == 0 
427                                                         || strcmp(right, ".*$") == 0 
428                                                         || strcmp(right, "^.*") == 0 
429                                                         || strcmp(right, "^.*$") == 0
430                                                         || strcmp(right, ".*$$") == 0 
431                                                         || strcmp(right, "^.*$$") == 0 )
432                                                 {
433                                                         ber_str2bv( "*", STRLENOF("*"), 1, &a->acl_dn_pat );
434
435                                                 } else {
436                                                         acl_regex_normalized_dn( right, &a->acl_dn_pat );
437                                                 }
438
439                                         } else {
440                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
441                                                         "unknown dn style \"%s\" in to clause\n",
442                                                     fname, lineno, style );
443                                                 return acl_usage();
444                                         }
445
446                                         continue;
447                                 }
448
449                                 if ( strcasecmp( left, "filter" ) == 0 ) {
450                                         if ( (a->acl_filter = str2filter( right )) == NULL ) {
451                                                 Debug( LDAP_DEBUG_ANY,
452                                 "%s: line %d: bad filter \"%s\" in to clause\n",
453                                                     fname, lineno, right );
454                                                 return acl_usage();
455                                         }
456
457                                 } else if ( strcasecmp( left, "attr" ) == 0             /* TOLERATED */
458                                                 || strcasecmp( left, "attrs" ) == 0 )   /* DOCUMENTED */
459                                 {
460                                         if ( strcasecmp( left, "attr" ) == 0 ) {
461                                                 Debug( LDAP_DEBUG_ANY,
462                                                         "%s: line %d: \"attr\" "
463                                                         "is deprecated (and undocumented); "
464                                                         "use \"attrs\" instead.\n",
465                                                         fname, lineno, 0 );
466                                         }
467
468                                         a->acl_attrs = str2anlist( a->acl_attrs,
469                                                 right, "," );
470                                         if ( a->acl_attrs == NULL ) {
471                                                 Debug( LDAP_DEBUG_ANY,
472                                 "%s: line %d: unknown attr \"%s\" in to clause\n",
473                                                     fname, lineno, right );
474                                                 return acl_usage();
475                                         }
476
477                                 } else if ( strncasecmp( left, "val", 3 ) == 0 ) {
478                                         char    *mr;
479                                         
480                                         if ( !BER_BVISEMPTY( &a->acl_attrval ) ) {
481                                                 Debug( LDAP_DEBUG_ANY,
482                                 "%s: line %d: attr val already specified in to clause.\n",
483                                                         fname, lineno, 0 );
484                                                 return acl_usage();
485                                         }
486                                         if ( a->acl_attrs == NULL || !BER_BVISEMPTY( &a->acl_attrs[1].an_name ) )
487                                         {
488                                                 Debug( LDAP_DEBUG_ANY,
489                                 "%s: line %d: attr val requires a single attribute.\n",
490                                                         fname, lineno, 0 );
491                                                 return acl_usage();
492                                         }
493
494                                         ber_str2bv( right, 0, 1, &a->acl_attrval );
495                                         a->acl_attrval_style = ACL_STYLE_BASE;
496
497                                         mr = strchr( left, '/' );
498                                         if ( mr != NULL ) {
499                                                 mr[ 0 ] = '\0';
500                                                 mr++;
501
502                                                 a->acl_attrval_mr = mr_find( mr );
503                                                 if ( a->acl_attrval_mr == NULL ) {
504                                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
505                                                                 "invalid matching rule \"%s\".\n",
506                                                                 fname, lineno, mr );
507                                                         return acl_usage();
508                                                 }
509
510                                                 if( !mr_usable_with_at( a->acl_attrval_mr, a->acl_attrs[ 0 ].an_desc->ad_type ) )
511                                                 {
512                                                         char    buf[ SLAP_TEXT_BUFLEN ];
513
514                                                         snprintf( buf, sizeof( buf ),
515                                                                 "matching rule \"%s\" use "
516                                                                 "with attr \"%s\" not appropriate.",
517                                                                 mr, a->acl_attrs[ 0 ].an_name.bv_val );
518                                                                 
519
520                                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: %s\n",
521                                                                 fname, lineno, buf );
522                                                         return acl_usage();
523                                                 }
524                                         }
525                                         
526                                         if ( style != NULL ) {
527                                                 if ( strcasecmp( style, "regex" ) == 0 ) {
528                                                         int e = regcomp( &a->acl_attrval_re, a->acl_attrval.bv_val,
529                                                                 REG_EXTENDED | REG_ICASE | REG_NOSUB );
530                                                         if ( e ) {
531                                                                 char    err[SLAP_TEXT_BUFLEN],
532                                                                         buf[ SLAP_TEXT_BUFLEN ];
533
534                                                                 regerror( e, &a->acl_attrval_re, err, sizeof( err ) );
535
536                                                                 snprintf( buf, sizeof( buf ),
537                                                                         "regular expression \"%s\" bad because of %s",
538                                                                         right, err );
539
540                                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: %s\n",
541                                                                         fname, lineno, buf );
542                                                                 return acl_usage();
543                                                         }
544                                                         a->acl_attrval_style = ACL_STYLE_REGEX;
545
546                                                 } else {
547                                                         /* FIXME: if the attribute has DN syntax, we might
548                                                          * allow one, subtree and children styles as well */
549                                                         if ( !strcasecmp( style, "base" ) ||
550                                                                 !strcasecmp( style, "exact" ) ) {
551                                                                 a->acl_attrval_style = ACL_STYLE_BASE;
552
553                                                         } else if ( a->acl_attrs[0].an_desc->ad_type->
554                                                                 sat_syntax == slap_schema.si_syn_distinguishedName )
555                                                         {
556                                                                 struct berval   bv;
557
558                                                                 if ( !strcasecmp( style, "baseObject" ) ||
559                                                                         !strcasecmp( style, "base" ) )
560                                                                 {
561                                                                         a->acl_attrval_style = ACL_STYLE_BASE;
562                                                                 } else if ( !strcasecmp( style, "onelevel" ) ||
563                                                                         !strcasecmp( style, "one" ) )
564                                                                 {
565                                                                         a->acl_attrval_style = ACL_STYLE_ONE;
566                                                                 } else if ( !strcasecmp( style, "subtree" ) ||
567                                                                         !strcasecmp( style, "sub" ) )
568                                                                 {
569                                                                         a->acl_attrval_style = ACL_STYLE_SUBTREE;
570                                                                 } else if ( !strcasecmp( style, "children" ) ) {
571                                                                         a->acl_attrval_style = ACL_STYLE_CHILDREN;
572                                                                 } else {
573                                                                         char    buf[ SLAP_TEXT_BUFLEN ];
574
575                                                                         /* FIXME: should be an error */
576
577                                                                         snprintf( buf, sizeof( buf ),
578                                                                                 "unknown val.<style> \"%s\" "
579                                                                                 "for attributeType \"%s\" with DN syntax; "
580                                                                                 "using \"base\""
581                                                                                 SLAPD_CONF_UNKNOWN_IGNORED ".",
582                                                                                 style,
583                                                                                 a->acl_attrs[0].an_desc->ad_cname.bv_val );
584
585                                                                         Debug( LDAP_DEBUG_CONFIG | LDAP_DEBUG_ACL, 
586                                                                                 "%s: line %d: %s\n",
587                                                                                 fname, lineno, buf );
588 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
589                                                                         return acl_usage();
590 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
591                                                                         a->acl_attrval_style = ACL_STYLE_BASE;
592                                                                 }
593
594                                                                 bv = a->acl_attrval;
595                                                                 rc = dnNormalize( 0, NULL, NULL, &bv, &a->acl_attrval, NULL );
596                                                                 if ( rc != LDAP_SUCCESS ) {
597                                                                         char    buf[ SLAP_TEXT_BUFLEN ];
598
599                                                                         snprintf( buf, sizeof( buf ),
600                                                                                 "unable to normalize DN \"%s\" "
601                                                                                 "for attributeType \"%s\" (%d).",
602                                                                                 bv.bv_val,
603                                                                                 a->acl_attrs[0].an_desc->ad_cname.bv_val,
604                                                                                 rc );
605                                                                         Debug( LDAP_DEBUG_ANY, 
606                                                                                 "%s: line %d: %s\n",
607                                                                                 fname, lineno, buf );
608                                                                         return acl_usage();
609                                                                 }
610                                                                 ber_memfree( bv.bv_val );
611
612                                                         } else {
613                                                                 char    buf[ SLAP_TEXT_BUFLEN ];
614
615                                                                 /* FIXME: should be an error */
616
617                                                                 snprintf( buf, sizeof( buf ),
618                                                                         "unknown val.<style> \"%s\" "
619                                                                         "for attributeType \"%s\"; using \"exact\""
620                                                                         SLAPD_CONF_UNKNOWN_IGNORED ".",
621                                                                         style, a->acl_attrs[0].an_desc->ad_cname.bv_val );
622                                                                 Debug( LDAP_DEBUG_CONFIG | LDAP_DEBUG_ACL, 
623                                                                         "%s: line %d: %s\n",
624                                                                         fname, lineno, buf );
625 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
626                                                                 return acl_usage();
627 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
628                                                                 a->acl_attrval_style = ACL_STYLE_BASE;
629                                                         }
630                                                 }
631                                         }
632
633                                         /* Check for appropriate matching rule */
634                                         if ( a->acl_attrval_style != ACL_STYLE_REGEX ) {
635                                                 if ( a->acl_attrval_mr == NULL ) {
636                                                         a->acl_attrval_mr = a->acl_attrs[ 0 ].an_desc->ad_type->sat_equality;
637                                                 }
638
639                                                 if ( a->acl_attrval_mr == NULL ) {
640                                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
641                                                                 "attr \"%s\" must have an EQUALITY matching rule.\n",
642                                                                 fname, lineno, a->acl_attrs[ 0 ].an_name.bv_val );
643                                                         return acl_usage();
644                                                 }
645                                         }
646
647                                 } else {
648                                         Debug( LDAP_DEBUG_ANY,
649                                                 "%s: line %d: expecting <what> got \"%s\"\n",
650                                             fname, lineno, left );
651                                         return acl_usage();
652                                 }
653                         }
654
655                         if ( !BER_BVISNULL( &a->acl_dn_pat ) && 
656                                         ber_bvccmp( &a->acl_dn_pat, '*' ) )
657                         {
658                                 free( a->acl_dn_pat.bv_val );
659                                 BER_BVZERO( &a->acl_dn_pat );
660                                 a->acl_dn_style = ACL_STYLE_REGEX;
661                         }
662                         
663                         if ( !BER_BVISEMPTY( &a->acl_dn_pat ) ||
664                                         a->acl_dn_style != ACL_STYLE_REGEX ) 
665                         {
666                                 if ( a->acl_dn_style != ACL_STYLE_REGEX ) {
667                                         struct berval bv;
668                                         rc = dnNormalize( 0, NULL, NULL, &a->acl_dn_pat, &bv, NULL);
669                                         if ( rc != LDAP_SUCCESS ) {
670                                                 Debug( LDAP_DEBUG_ANY,
671                                                         "%s: line %d: bad DN \"%s\" in to DN clause\n",
672                                                         fname, lineno, a->acl_dn_pat.bv_val );
673                                                 return acl_usage();
674                                         }
675                                         free( a->acl_dn_pat.bv_val );
676                                         a->acl_dn_pat = bv;
677
678                                 } else {
679                                         int e = regcomp( &a->acl_dn_re, a->acl_dn_pat.bv_val,
680                                                 REG_EXTENDED | REG_ICASE );
681                                         if ( e ) {
682                                                 char    err[ SLAP_TEXT_BUFLEN ],
683                                                         buf[ SLAP_TEXT_BUFLEN ];
684
685                                                 regerror( e, &a->acl_dn_re, err, sizeof( err ) );
686                                                 snprintf( buf, sizeof( buf ),
687                                                         "regular expression \"%s\" bad because of %s",
688                                                         right, err );
689                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: %s\n",
690                                                         fname, lineno, buf );
691                                                 return acl_usage();
692                                         }
693                                 }
694                         }
695
696                 /* by clause - select who has what access to entries */
697                 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
698                         if ( a == NULL ) {
699                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
700                                         "to clause required before by clause in access line\n",
701                                         fname, lineno, 0 );
702                                 return acl_usage();
703                         }
704
705                         /*
706                          * by clause consists of <who> and <access>
707                          */
708
709                         b = (Access *) ch_calloc( 1, sizeof(Access) );
710
711                         ACL_INVALIDATE( b->a_access_mask );
712
713                         if ( ++i == argc ) {
714                                 Debug( LDAP_DEBUG_ANY,
715                                         "%s: line %d: premature EOL: expecting <who>\n",
716                                         fname, lineno, 0 );
717                                 return acl_usage();
718                         }
719
720                         /* get <who> */
721                         for ( ; i < argc; i++ ) {
722                                 slap_style_t    sty = ACL_STYLE_REGEX;
723                                 char            *style_modifier = NULL;
724                                 char            *style_level = NULL;
725                                 int             level = 0;
726                                 int             expand = 0;
727                                 slap_dn_access  *bdn = &b->a_dn;
728                                 int             is_realdn = 0;
729
730                                 split( argv[i], '=', &left, &right );
731                                 split( left, '.', &left, &style );
732                                 if ( style ) {
733                                         split( style, ',', &style, &style_modifier );
734
735                                         if ( strncasecmp( style, "level", STRLENOF( "level" ) ) == 0 ) {
736                                                 split( style, '{', &style, &style_level );
737                                                 if ( style_level != NULL ) {
738                                                         char *p = strchr( style_level, '}' );
739                                                         if ( p == NULL ) {
740                                                                 Debug( LDAP_DEBUG_ANY,
741                                                                         "%s: line %d: premature eol: "
742                                                                         "expecting closing '}' in \"level{n}\"\n",
743                                                                         fname, lineno, 0 );
744                                                                 return acl_usage();
745                                                         } else if ( p == style_level ) {
746                                                                 Debug( LDAP_DEBUG_ANY,
747                                                                         "%s: line %d: empty level "
748                                                                         "in \"level{n}\"\n",
749                                                                         fname, lineno, 0 );
750                                                                 return acl_usage();
751                                                         }
752                                                         p[0] = '\0';
753                                                 }
754                                         }
755                                 }
756
757                                 if ( style == NULL || *style == '\0' ||
758                                         strcasecmp( style, "exact" ) == 0 ||
759                                         strcasecmp( style, "baseObject" ) == 0 ||
760                                         strcasecmp( style, "base" ) == 0 )
761                                 {
762                                         sty = ACL_STYLE_BASE;
763
764                                 } else if ( strcasecmp( style, "onelevel" ) == 0 ||
765                                         strcasecmp( style, "one" ) == 0 )
766                                 {
767                                         sty = ACL_STYLE_ONE;
768
769                                 } else if ( strcasecmp( style, "subtree" ) == 0 ||
770                                         strcasecmp( style, "sub" ) == 0 )
771                                 {
772                                         sty = ACL_STYLE_SUBTREE;
773
774                                 } else if ( strcasecmp( style, "children" ) == 0 ) {
775                                         sty = ACL_STYLE_CHILDREN;
776
777                                 } else if ( strcasecmp( style, "level" ) == 0 )
778                                 {
779                                         if ( lutil_atoi( &level, style_level ) != 0 ) {
780                                                 Debug( LDAP_DEBUG_ANY,
781                                                         "%s: line %d: unable to parse level "
782                                                         "in \"level{n}\"\n",
783                                                         fname, lineno, 0 );
784                                                 return acl_usage();
785                                         }
786
787                                         sty = ACL_STYLE_LEVEL;
788
789                                 } else if ( strcasecmp( style, "regex" ) == 0 ) {
790                                         sty = ACL_STYLE_REGEX;
791
792                                 } else if ( strcasecmp( style, "expand" ) == 0 ) {
793                                         sty = ACL_STYLE_EXPAND;
794
795                                 } else if ( strcasecmp( style, "ip" ) == 0 ) {
796                                         sty = ACL_STYLE_IP;
797
798                                 } else if ( strcasecmp( style, "path" ) == 0 ) {
799                                         sty = ACL_STYLE_PATH;
800 #ifndef LDAP_PF_LOCAL
801                                         Debug( LDAP_DEBUG_CONFIG | LDAP_DEBUG_ACL,
802                                                 "%s: line %d: "
803                                                 "\"path\" style modifier is useless without local"
804                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
805                                                 fname, lineno, 0 );
806 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
807                                         return acl_usage();
808 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
809 #endif /* LDAP_PF_LOCAL */
810
811                                 } else {
812                                         Debug( LDAP_DEBUG_ANY,
813                                                 "%s: line %d: unknown style \"%s\" in by clause\n",
814                                                 fname, lineno, style );
815                                         return acl_usage();
816                                 }
817
818                                 if ( style_modifier &&
819                                         strcasecmp( style_modifier, "expand" ) == 0 )
820                                 {
821                                         switch ( sty ) {
822                                         case ACL_STYLE_REGEX:
823                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
824                                                         "\"regex\" style implies "
825                                                         "\"expand\" modifier" 
826                                                         SLAPD_CONF_UNKNOWN_IGNORED ".\n",
827                                                         fname, lineno, 0 );
828 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
829                                                 return acl_usage();
830 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
831                                                 break;
832
833                                         case ACL_STYLE_EXPAND:
834                                                 break;
835
836                                         default:
837                                                 /* we'll see later if it's pertinent */
838                                                 expand = 1;
839                                                 break;
840                                         }
841                                 }
842
843                                 /* expand in <who> needs regex in <what> */
844                                 if ( ( sty == ACL_STYLE_EXPAND || expand )
845                                                 && a->acl_dn_style != ACL_STYLE_REGEX )
846                                 {
847                                         Debug( LDAP_DEBUG_CONFIG | LDAP_DEBUG_ACL, "%s: line %d: "
848                                                 "\"expand\" style or modifier used "
849                                                 "in conjunction with "
850                                                 "a non-regex <what> clause"
851                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
852                                                 fname, lineno, 0 );
853 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
854                                                 return acl_usage();
855 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
856                                 }
857
858                                 if ( strncasecmp( left, "real", STRLENOF( "real" ) ) == 0 ) {
859                                         is_realdn = 1;
860                                         bdn = &b->a_realdn;
861                                         left += STRLENOF( "real" );
862                                 }
863
864                                 if ( strcasecmp( left, "*" ) == 0 ) {
865                                         if ( is_realdn ) {
866                                                 return acl_usage();
867                                         }
868
869                                         ber_str2bv( "*", STRLENOF( "*" ), 1, &bv );
870                                         sty = ACL_STYLE_REGEX;
871
872                                 } else if ( strcasecmp( left, "anonymous" ) == 0 ) {
873                                         ber_str2bv("anonymous", STRLENOF( "anonymous" ), 1, &bv);
874                                         sty = ACL_STYLE_ANONYMOUS;
875
876                                 } else if ( strcasecmp( left, "users" ) == 0 ) {
877                                         ber_str2bv("users", STRLENOF( "users" ), 1, &bv);
878                                         sty = ACL_STYLE_USERS;
879
880                                 } else if ( strcasecmp( left, "self" ) == 0 ) {
881                                         ber_str2bv("self", STRLENOF( "self" ), 1, &bv);
882                                         sty = ACL_STYLE_SELF;
883
884                                 } else if ( strcasecmp( left, "dn" ) == 0 ) {
885                                         if ( sty == ACL_STYLE_REGEX ) {
886                                                 bdn->a_style = ACL_STYLE_REGEX;
887                                                 if ( right == NULL ) {
888                                                         /* no '=' */
889                                                         ber_str2bv("users",
890                                                                 STRLENOF( "users" ),
891                                                                 1, &bv);
892                                                         bdn->a_style = ACL_STYLE_USERS;
893
894                                                 } else if (*right == '\0' ) {
895                                                         /* dn="" */
896                                                         ber_str2bv("anonymous",
897                                                                 STRLENOF( "anonymous" ),
898                                                                 1, &bv);
899                                                         bdn->a_style = ACL_STYLE_ANONYMOUS;
900
901                                                 } else if ( strcmp( right, "*" ) == 0 ) {
902                                                         /* dn=* */
903                                                         /* any or users?  users for now */
904                                                         ber_str2bv("users",
905                                                                 STRLENOF( "users" ),
906                                                                 1, &bv);
907                                                         bdn->a_style = ACL_STYLE_USERS;
908
909                                                 } else if ( strcmp( right, ".+" ) == 0
910                                                         || strcmp( right, "^.+" ) == 0
911                                                         || strcmp( right, ".+$" ) == 0
912                                                         || strcmp( right, "^.+$" ) == 0
913                                                         || strcmp( right, ".+$$" ) == 0
914                                                         || strcmp( right, "^.+$$" ) == 0 )
915                                                 {
916                                                         ber_str2bv("users",
917                                                                 STRLENOF( "users" ),
918                                                                 1, &bv);
919                                                         bdn->a_style = ACL_STYLE_USERS;
920
921                                                 } else if ( strcmp( right, ".*" ) == 0
922                                                         || strcmp( right, "^.*" ) == 0
923                                                         || strcmp( right, ".*$" ) == 0
924                                                         || strcmp( right, "^.*$" ) == 0
925                                                         || strcmp( right, ".*$$" ) == 0
926                                                         || strcmp( right, "^.*$$" ) == 0 )
927                                                 {
928                                                         ber_str2bv("*",
929                                                                 STRLENOF( "*" ),
930                                                                 1, &bv);
931
932                                                 } else {
933                                                         acl_regex_normalized_dn( right, &bv );
934                                                         if ( !ber_bvccmp( &bv, '*' ) ) {
935                                                                 regtest( fname, lineno, bv.bv_val );
936                                                         }
937                                                 }
938
939                                         } else if ( right == NULL || *right == '\0' ) {
940                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
941                                                         "missing \"=\" in (or value after) \"%s\" "
942                                                         "in by clause\n",
943                                                         fname, lineno, left );
944                                                 return acl_usage();
945
946                                         } else {
947                                                 ber_str2bv( right, 0, 1, &bv );
948                                         }
949
950                                 } else {
951                                         BER_BVZERO( &bv );
952                                 }
953
954                                 if ( !BER_BVISNULL( &bv ) ) {
955                                         if ( !BER_BVISEMPTY( &bdn->a_pat ) ) {
956                                                 Debug( LDAP_DEBUG_ANY,
957                                                         "%s: line %d: dn pattern already specified.\n",
958                                                         fname, lineno, 0 );
959                                                 return acl_usage();
960                                         }
961
962                                         if ( sty != ACL_STYLE_REGEX &&
963                                                         sty != ACL_STYLE_ANONYMOUS &&
964                                                         sty != ACL_STYLE_USERS &&
965                                                         sty != ACL_STYLE_SELF &&
966                                                         expand == 0 )
967                                         {
968                                                 rc = dnNormalize(0, NULL, NULL,
969                                                         &bv, &bdn->a_pat, NULL);
970                                                 if ( rc != LDAP_SUCCESS ) {
971                                                         Debug( LDAP_DEBUG_ANY,
972                                                                 "%s: line %d: bad DN \"%s\" in by DN clause\n",
973                                                                 fname, lineno, bv.bv_val );
974                                                         return acl_usage();
975                                                 }
976                                                 free( bv.bv_val );
977                                                 if ( sty == ACL_STYLE_BASE
978                                                         && be != NULL
979                                                         && !BER_BVISNULL( &be->be_rootndn )
980                                                         && dn_match( &bdn->a_pat, &be->be_rootndn ) )
981                                                 {
982                                                         Debug( LDAP_DEBUG_ANY,
983                                                                 "%s: line %d: rootdn is always granted "
984                                                                 "unlimited privileges.\n",
985                                                                 fname, lineno, 0 );
986                                                 }
987
988                                         } else {
989                                                 bdn->a_pat = bv;
990                                         }
991                                         bdn->a_style = sty;
992                                         if ( expand ) {
993                                                 char    *exp;
994                                                 int     gotit = 0;
995
996                                                 for ( exp = strchr( bdn->a_pat.bv_val, '$' );
997                                                                 exp && (ber_len_t)(exp - bdn->a_pat.bv_val)
998                                                                         < bdn->a_pat.bv_len;
999                                                                 exp = strchr( exp, '$' ) )
1000                                                 {
1001                                                         if ( isdigit( exp[ 1 ] ) ) {
1002                                                                 gotit = 1;
1003                                                                 break;
1004                                                         }
1005                                                 }
1006
1007                                                 if ( gotit == 1 ) {
1008                                                         bdn->a_expand = expand;
1009
1010                                                 } else {
1011                                                         Debug( LDAP_DEBUG_ANY,
1012                                                                 "%s: line %d: \"expand\" used "
1013                                                                 "with no expansions in \"pattern\""
1014                                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
1015                                                                 fname, lineno, 0 );
1016 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
1017                                                         return acl_usage();
1018 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
1019                                                 } 
1020                                         }
1021                                         if ( sty == ACL_STYLE_SELF ) {
1022                                                 bdn->a_self_level = level;
1023
1024                                         } else {
1025                                                 if ( level < 0 ) {
1026                                                         Debug( LDAP_DEBUG_ANY,
1027                                                                 "%s: line %d: bad negative level \"%d\" "
1028                                                                 "in by DN clause\n",
1029                                                                 fname, lineno, level );
1030                                                         return acl_usage();
1031                                                 } else if ( level == 1 ) {
1032                                                         Debug( LDAP_DEBUG_ANY,
1033                                                                 "%s: line %d: \"onelevel\" should be used "
1034                                                                 "instead of \"level{1}\" in by DN clause\n",
1035                                                                 fname, lineno, 0 );
1036                                                 } else if ( level == 0 && sty == ACL_STYLE_LEVEL ) {
1037                                                         Debug( LDAP_DEBUG_ANY,
1038                                                                 "%s: line %d: \"base\" should be used "
1039                                                                 "instead of \"level{0}\" in by DN clause\n",
1040                                                                 fname, lineno, 0 );
1041                                                 }
1042
1043                                                 bdn->a_level = level;
1044                                         }
1045                                         continue;
1046                                 }
1047
1048                                 if ( strcasecmp( left, "dnattr" ) == 0 ) {
1049                                         if ( right == NULL || right[0] == '\0' ) {
1050                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1051                                                         "missing \"=\" in (or value after) \"%s\" "
1052                                                         "in by clause\n",
1053                                                         fname, lineno, left );
1054                                                 return acl_usage();
1055                                         }
1056
1057                                         if( bdn->a_at != NULL ) {
1058                                                 Debug( LDAP_DEBUG_ANY,
1059                                                         "%s: line %d: dnattr already specified.\n",
1060                                                         fname, lineno, 0 );
1061                                                 return acl_usage();
1062                                         }
1063
1064                                         rc = slap_str2ad( right, &bdn->a_at, &text );
1065
1066                                         if( rc != LDAP_SUCCESS ) {
1067                                                 char    buf[ SLAP_TEXT_BUFLEN ];
1068
1069                                                 snprintf( buf, sizeof( buf ),
1070                                                         "dnattr \"%s\": %s",
1071                                                         right, text );
1072                                                 Debug( LDAP_DEBUG_ANY,
1073                                                         "%s: line %d: %s\n",
1074                                                         fname, lineno, buf );
1075                                                 return acl_usage();
1076                                         }
1077
1078
1079                                         if( !is_at_syntax( bdn->a_at->ad_type,
1080                                                 SLAPD_DN_SYNTAX ) &&
1081                                                 !is_at_syntax( bdn->a_at->ad_type,
1082                                                 SLAPD_NAMEUID_SYNTAX ))
1083                                         {
1084                                                 char    buf[ SLAP_TEXT_BUFLEN ];
1085
1086                                                 snprintf( buf, sizeof( buf ),
1087                                                         "dnattr \"%s\": "
1088                                                         "inappropriate syntax: %s\n",
1089                                                         right,
1090                                                         bdn->a_at->ad_type->sat_syntax_oid );
1091                                                 Debug( LDAP_DEBUG_ANY,
1092                                                         "%s: line %d: %s\n",
1093                                                         fname, lineno, buf );
1094                                                 return acl_usage();
1095                                         }
1096
1097                                         if( bdn->a_at->ad_type->sat_equality == NULL ) {
1098                                                 Debug( LDAP_DEBUG_ANY,
1099                                                         "%s: line %d: dnattr \"%s\": "
1100                                                         "inappropriate matching (no EQUALITY)\n",
1101                                                         fname, lineno, right );
1102                                                 return acl_usage();
1103                                         }
1104
1105                                         continue;
1106                                 }
1107
1108                                 if ( strncasecmp( left, "group", STRLENOF( "group" ) ) == 0 ) {
1109                                         char *name = NULL;
1110                                         char *value = NULL;
1111
1112                                         switch ( sty ) {
1113                                         case ACL_STYLE_REGEX:
1114                                                 /* legacy, tolerated */
1115                                                 Debug( LDAP_DEBUG_CONFIG | LDAP_DEBUG_ACL,
1116                                                         "%s: line %d: "
1117                                                         "deprecated group style \"regex\"; "
1118                                                         "use \"expand\" instead.\n",
1119                                                         fname, lineno, 0 );
1120                                                 sty = ACL_STYLE_EXPAND;
1121                                                 break;
1122
1123                                         case ACL_STYLE_BASE:
1124                                                 /* legal, traditional */
1125                                         case ACL_STYLE_EXPAND:
1126                                                 /* legal, substring expansion; supersedes regex */
1127                                                 break;
1128
1129                                         default:
1130                                                 /* unknown */
1131                                                 Debug( LDAP_DEBUG_ANY,
1132                                                         "%s: line %d: "
1133                                                         "inappropriate style \"%s\" in by clause.\n",
1134                                                         fname, lineno, style );
1135                                                 return acl_usage();
1136                                         }
1137
1138                                         if ( right == NULL || right[0] == '\0' ) {
1139                                                 Debug( LDAP_DEBUG_ANY,
1140                                                         "%s: line %d: "
1141                                                         "missing \"=\" in (or value after) \"%s\" "
1142                                                         "in by clause.\n",
1143                                                         fname, lineno, left );
1144                                                 return acl_usage();
1145                                         }
1146
1147                                         if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1148                                                 Debug( LDAP_DEBUG_ANY,
1149                                                         "%s: line %d: group pattern already specified.\n",
1150                                                         fname, lineno, 0 );
1151                                                 return acl_usage();
1152                                         }
1153
1154                                         /* format of string is
1155                                                 "group/objectClassValue/groupAttrName" */
1156                                         if ( ( value = strchr(left, '/') ) != NULL ) {
1157                                                 *value++ = '\0';
1158                                                 if ( *value && ( name = strchr( value, '/' ) ) != NULL ) {
1159                                                         *name++ = '\0';
1160                                                 }
1161                                         }
1162
1163                                         b->a_group_style = sty;
1164                                         if ( sty == ACL_STYLE_EXPAND ) {
1165                                                 acl_regex_normalized_dn( right, &bv );
1166                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1167                                                         regtest( fname, lineno, bv.bv_val );
1168                                                 }
1169                                                 b->a_group_pat = bv;
1170
1171                                         } else {
1172                                                 ber_str2bv( right, 0, 0, &bv );
1173                                                 rc = dnNormalize( 0, NULL, NULL, &bv,
1174                                                         &b->a_group_pat, NULL );
1175                                                 if ( rc != LDAP_SUCCESS ) {
1176                                                         Debug( LDAP_DEBUG_ANY,
1177                                                                 "%s: line %d: bad DN \"%s\".\n",
1178                                                                 fname, lineno, right );
1179                                                         return acl_usage();
1180                                                 }
1181                                         }
1182
1183                                         if ( value && *value ) {
1184                                                 b->a_group_oc = oc_find( value );
1185                                                 *--value = '/';
1186
1187                                                 if ( b->a_group_oc == NULL ) {
1188                                                         Debug( LDAP_DEBUG_ANY,
1189                                                                 "%s: line %d: group objectclass "
1190                                                                 "\"%s\" unknown.\n",
1191                                                                 fname, lineno, value );
1192                                                         return acl_usage();
1193                                                 }
1194
1195                                         } else {
1196                                                 b->a_group_oc = oc_find( SLAPD_GROUP_CLASS );
1197
1198                                                 if( b->a_group_oc == NULL ) {
1199                                                         Debug( LDAP_DEBUG_ANY,
1200                                                                 "%s: line %d: group default objectclass "
1201                                                                 "\"%s\" unknown.\n",
1202                                                                 fname, lineno, SLAPD_GROUP_CLASS );
1203                                                         return acl_usage();
1204                                                 }
1205                                         }
1206
1207                                         if ( is_object_subclass( slap_schema.si_oc_referral,
1208                                                 b->a_group_oc ) )
1209                                         {
1210                                                 Debug( LDAP_DEBUG_ANY,
1211                                                         "%s: line %d: group objectclass \"%s\" "
1212                                                         "is subclass of referral.\n",
1213                                                         fname, lineno, value );
1214                                                 return acl_usage();
1215                                         }
1216
1217                                         if ( is_object_subclass( slap_schema.si_oc_alias,
1218                                                 b->a_group_oc ) )
1219                                         {
1220                                                 Debug( LDAP_DEBUG_ANY,
1221                                                         "%s: line %d: group objectclass \"%s\" "
1222                                                         "is subclass of alias.\n",
1223                                                         fname, lineno, value );
1224                                                 return acl_usage();
1225                                         }
1226
1227                                         if ( name && *name ) {
1228                                                 rc = slap_str2ad( name, &b->a_group_at, &text );
1229
1230                                                 if( rc != LDAP_SUCCESS ) {
1231                                                         char    buf[ SLAP_TEXT_BUFLEN ];
1232
1233                                                         snprintf( buf, sizeof( buf ),
1234                                                                 "group \"%s\": %s.",
1235                                                                 right, text );
1236                                                         Debug( LDAP_DEBUG_ANY,
1237                                                                 "%s: line %d: %s\n",
1238                                                                 fname, lineno, buf );
1239                                                         return acl_usage();
1240                                                 }
1241                                                 *--name = '/';
1242
1243                                         } else {
1244                                                 rc = slap_str2ad( SLAPD_GROUP_ATTR, &b->a_group_at, &text );
1245
1246                                                 if ( rc != LDAP_SUCCESS ) {
1247                                                         char    buf[ SLAP_TEXT_BUFLEN ];
1248
1249                                                         snprintf( buf, sizeof( buf ),
1250                                                                 "group \"%s\": %s.",
1251                                                                 SLAPD_GROUP_ATTR, text );
1252                                                         Debug( LDAP_DEBUG_ANY,
1253                                                                 "%s: line %d: %s\n",
1254                                                                 fname, lineno, buf );
1255                                                         return acl_usage();
1256                                                 }
1257                                         }
1258
1259                                         if ( !is_at_syntax( b->a_group_at->ad_type,
1260                                                 SLAPD_DN_SYNTAX ) &&
1261                                                 !is_at_syntax( b->a_group_at->ad_type,
1262                                                 SLAPD_NAMEUID_SYNTAX ) &&
1263                                                 !is_at_subtype( b->a_group_at->ad_type, slap_schema.si_ad_labeledURI->ad_type ) )
1264                                         {
1265                                                 char    buf[ SLAP_TEXT_BUFLEN ];
1266
1267                                                 snprintf( buf, sizeof( buf ),
1268                                                         "group \"%s\": inappropriate syntax: %s.",
1269                                                         right,
1270                                                         b->a_group_at->ad_type->sat_syntax_oid );
1271                                                 Debug( LDAP_DEBUG_ANY,
1272                                                         "%s: line %d: %s\n",
1273                                                         fname, lineno, buf );
1274                                                 return acl_usage();
1275                                         }
1276
1277
1278                                         {
1279                                                 int rc;
1280                                                 struct berval vals[2];
1281
1282                                                 ber_str2bv( b->a_group_oc->soc_oid, 0, 0, &vals[0] );
1283                                                 BER_BVZERO( &vals[1] );
1284
1285                                                 rc = oc_check_allowed( b->a_group_at->ad_type,
1286                                                         vals, NULL );
1287
1288                                                 if( rc != 0 ) {
1289                                                         char    buf[ SLAP_TEXT_BUFLEN ];
1290
1291                                                         snprintf( buf, sizeof( buf ),
1292                                                                 "group: \"%s\" not allowed by \"%s\".",
1293                                                                 b->a_group_at->ad_cname.bv_val,
1294                                                                 b->a_group_oc->soc_oid );
1295                                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: %s\n",
1296                                                                 fname, lineno, buf );
1297                                                         return acl_usage();
1298                                                 }
1299                                         }
1300                                         continue;
1301                                 }
1302
1303                                 if ( strcasecmp( left, "peername" ) == 0 ) {
1304                                         switch ( sty ) {
1305                                         case ACL_STYLE_REGEX:
1306                                         case ACL_STYLE_BASE:
1307                                                 /* legal, traditional */
1308                                         case ACL_STYLE_EXPAND:
1309                                                 /* cheap replacement to regex for simple expansion */
1310                                         case ACL_STYLE_IP:
1311                                         case ACL_STYLE_PATH:
1312                                                 /* legal, peername specific */
1313                                                 break;
1314
1315                                         default:
1316                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1317                                                         "inappropriate style \"%s\" in by clause.\n",
1318                                                     fname, lineno, style );
1319                                                 return acl_usage();
1320                                         }
1321
1322                                         if ( right == NULL || right[0] == '\0' ) {
1323                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1324                                                         "missing \"=\" in (or value after) \"%s\" "
1325                                                         "in by clause.\n",
1326                                                         fname, lineno, left );
1327                                                 return acl_usage();
1328                                         }
1329
1330                                         if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1331                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1332                                                         "peername pattern already specified.\n",
1333                                                         fname, lineno, 0 );
1334                                                 return acl_usage();
1335                                         }
1336
1337                                         b->a_peername_style = sty;
1338                                         if ( sty == ACL_STYLE_REGEX ) {
1339                                                 acl_regex_normalized_dn( right, &bv );
1340                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1341                                                         regtest( fname, lineno, bv.bv_val );
1342                                                 }
1343                                                 b->a_peername_pat = bv;
1344
1345                                         } else {
1346                                                 ber_str2bv( right, 0, 1, &b->a_peername_pat );
1347
1348                                                 if ( sty == ACL_STYLE_IP ) {
1349                                                         char            *addr = NULL,
1350                                                                         *mask = NULL,
1351                                                                         *port = NULL;
1352
1353                                                         split( right, '{', &addr, &port );
1354                                                         split( addr, '%', &addr, &mask );
1355
1356                                                         b->a_peername_addr = inet_addr( addr );
1357                                                         if ( b->a_peername_addr == (unsigned long)(-1) ) {
1358                                                                 /* illegal address */
1359                                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1360                                                                         "illegal peername address \"%s\".\n",
1361                                                                         fname, lineno, addr );
1362                                                                 return acl_usage();
1363                                                         }
1364
1365                                                         b->a_peername_mask = (unsigned long)(-1);
1366                                                         if ( mask != NULL ) {
1367                                                                 b->a_peername_mask = inet_addr( mask );
1368                                                                 if ( b->a_peername_mask ==
1369                                                                         (unsigned long)(-1) )
1370                                                                 {
1371                                                                         /* illegal mask */
1372                                                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1373                                                                                 "illegal peername address mask "
1374                                                                                 "\"%s\".\n",
1375                                                                                 fname, lineno, mask );
1376                                                                         return acl_usage();
1377                                                                 }
1378                                                         } 
1379
1380                                                         b->a_peername_port = -1;
1381                                                         if ( port ) {
1382                                                                 char    *end = NULL;
1383
1384                                                                 b->a_peername_port = strtol( port, &end, 10 );
1385                                                                 if ( end == port || end[0] != '}' ) {
1386                                                                         /* illegal port */
1387                                                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1388                                                                                 "illegal peername port specification "
1389                                                                                 "\"{%s}\".\n",
1390                                                                                 fname, lineno, port );
1391                                                                         return acl_usage();
1392                                                                 }
1393                                                         }
1394                                                 }
1395                                         }
1396                                         continue;
1397                                 }
1398
1399                                 if ( strcasecmp( left, "sockname" ) == 0 ) {
1400                                         switch ( sty ) {
1401                                         case ACL_STYLE_REGEX:
1402                                         case ACL_STYLE_BASE:
1403                                                 /* legal, traditional */
1404                                         case ACL_STYLE_EXPAND:
1405                                                 /* cheap replacement to regex for simple expansion */
1406                                                 break;
1407
1408                                         default:
1409                                                 /* unknown */
1410                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1411                                                         "inappropriate style \"%s\" in by clause\n",
1412                                                     fname, lineno, style );
1413                                                 return acl_usage();
1414                                         }
1415
1416                                         if ( right == NULL || right[0] == '\0' ) {
1417                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1418                                                         "missing \"=\" in (or value after) \"%s\" "
1419                                                         "in by clause\n",
1420                                                         fname, lineno, left );
1421                                                 return acl_usage();
1422                                         }
1423
1424                                         if ( !BER_BVISNULL( &b->a_sockname_pat ) ) {
1425                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1426                                                         "sockname pattern already specified.\n",
1427                                                         fname, lineno, 0 );
1428                                                 return acl_usage();
1429                                         }
1430
1431                                         b->a_sockname_style = sty;
1432                                         if ( sty == ACL_STYLE_REGEX ) {
1433                                                 acl_regex_normalized_dn( right, &bv );
1434                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1435                                                         regtest( fname, lineno, bv.bv_val );
1436                                                 }
1437                                                 b->a_sockname_pat = bv;
1438                                                 
1439                                         } else {
1440                                                 ber_str2bv( right, 0, 1, &b->a_sockname_pat );
1441                                         }
1442                                         continue;
1443                                 }
1444
1445                                 if ( strcasecmp( left, "domain" ) == 0 ) {
1446                                         switch ( sty ) {
1447                                         case ACL_STYLE_REGEX:
1448                                         case ACL_STYLE_BASE:
1449                                         case ACL_STYLE_SUBTREE:
1450                                                 /* legal, traditional */
1451                                                 break;
1452
1453                                         case ACL_STYLE_EXPAND:
1454                                                 /* tolerated: means exact,expand */
1455                                                 if ( expand ) {
1456                                                         Debug( LDAP_DEBUG_ANY,
1457                                                                 "%s: line %d: "
1458                                                                 "\"expand\" modifier "
1459                                                                 "with \"expand\" style.\n",
1460                                                                 fname, lineno, 0 );
1461                                                 }
1462                                                 sty = ACL_STYLE_BASE;
1463                                                 expand = 1;
1464                                                 break;
1465
1466                                         default:
1467                                                 /* unknown */
1468                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1469                                                         "inappropriate style \"%s\" in by clause.\n",
1470                                                     fname, lineno, style );
1471                                                 return acl_usage();
1472                                         }
1473
1474                                         if ( right == NULL || right[0] == '\0' ) {
1475                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1476                                                         "missing \"=\" in (or value after) \"%s\" "
1477                                                         "in by clause.\n",
1478                                                         fname, lineno, left );
1479                                                 return acl_usage();
1480                                         }
1481
1482                                         if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1483                                                 Debug( LDAP_DEBUG_ANY,
1484                                                         "%s: line %d: domain pattern already specified.\n",
1485                                                         fname, lineno, 0 );
1486                                                 return acl_usage();
1487                                         }
1488
1489                                         b->a_domain_style = sty;
1490                                         b->a_domain_expand = expand;
1491                                         if ( sty == ACL_STYLE_REGEX ) {
1492                                                 acl_regex_normalized_dn( right, &bv );
1493                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1494                                                         regtest( fname, lineno, bv.bv_val );
1495                                                 }
1496                                                 b->a_domain_pat = bv;
1497
1498                                         } else {
1499                                                 ber_str2bv( right, 0, 1, &b->a_domain_pat );
1500                                         }
1501                                         continue;
1502                                 }
1503
1504                                 if ( strcasecmp( left, "sockurl" ) == 0 ) {
1505                                         switch ( sty ) {
1506                                         case ACL_STYLE_REGEX:
1507                                         case ACL_STYLE_BASE:
1508                                                 /* legal, traditional */
1509                                         case ACL_STYLE_EXPAND:
1510                                                 /* cheap replacement to regex for simple expansion */
1511                                                 break;
1512
1513                                         default:
1514                                                 /* unknown */
1515                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1516                                                         "inappropriate style \"%s\" in by clause.\n",
1517                                                     fname, lineno, style );
1518                                                 return acl_usage();
1519                                         }
1520
1521                                         if ( right == NULL || right[0] == '\0' ) {
1522                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1523                                                         "missing \"=\" in (or value after) \"%s\" "
1524                                                         "in by clause.\n",
1525                                                         fname, lineno, left );
1526                                                 return acl_usage();
1527                                         }
1528
1529                                         if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1530                                                 Debug( LDAP_DEBUG_ANY,
1531                                                         "%s: line %d: sockurl pattern already specified.\n",
1532                                                         fname, lineno, 0 );
1533                                                 return acl_usage();
1534                                         }
1535
1536                                         b->a_sockurl_style = sty;
1537                                         if ( sty == ACL_STYLE_REGEX ) {
1538                                                 acl_regex_normalized_dn( right, &bv );
1539                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1540                                                         regtest( fname, lineno, bv.bv_val );
1541                                                 }
1542                                                 b->a_sockurl_pat = bv;
1543                                                 
1544                                         } else {
1545                                                 ber_str2bv( right, 0, 1, &b->a_sockurl_pat );
1546                                         }
1547                                         continue;
1548                                 }
1549
1550                                 if ( strcasecmp( left, "set" ) == 0 ) {
1551                                         switch ( sty ) {
1552                                                 /* deprecated */
1553                                         case ACL_STYLE_REGEX:
1554                                                 Debug( LDAP_DEBUG_CONFIG | LDAP_DEBUG_ACL,
1555                                                         "%s: line %d: "
1556                                                         "deprecated set style "
1557                                                         "\"regex\" in <by> clause; "
1558                                                         "use \"expand\" instead.\n",
1559                                                         fname, lineno, 0 );
1560                                                 sty = ACL_STYLE_EXPAND;
1561                                                 /* FALLTHRU */
1562                                                 
1563                                         case ACL_STYLE_BASE:
1564                                         case ACL_STYLE_EXPAND:
1565                                                 break;
1566
1567                                         default:
1568                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1569                                                         "inappropriate style \"%s\" in by clause.\n",
1570                                                         fname, lineno, style );
1571                                                 return acl_usage();
1572                                         }
1573
1574                                         if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1575                                                 Debug( LDAP_DEBUG_ANY,
1576                                                         "%s: line %d: set attribute already specified.\n",
1577                                                         fname, lineno, 0 );
1578                                                 return acl_usage();
1579                                         }
1580
1581                                         if ( right == NULL || *right == '\0' ) {
1582                                                 Debug( LDAP_DEBUG_ANY,
1583                                                         "%s: line %d: no set is defined.\n",
1584                                                         fname, lineno, 0 );
1585                                                 return acl_usage();
1586                                         }
1587
1588                                         b->a_set_style = sty;
1589                                         ber_str2bv( right, 0, 1, &b->a_set_pat );
1590
1591                                         continue;
1592                                 }
1593
1594 #ifdef SLAP_DYNACL
1595                                 {
1596                                         char            *name = NULL,
1597                                                         *opts = NULL;
1598                                         
1599                                         if ( strcasecmp( left, "aci" ) == 0 ) {
1600                                                 name = "aci";
1601                                                 
1602                                         } else if ( strncasecmp( left, "dynacl/", STRLENOF( "dynacl/" ) ) == 0 ) {
1603                                                 name = &left[ STRLENOF( "dynacl/" ) ];
1604                                                 opts = strchr( name, '/' );
1605                                                 if ( opts ) {
1606                                                         opts[ 0 ] = '\0';
1607                                                         opts++;
1608                                                 }
1609                                         }
1610
1611                                         if ( name ) {
1612                                                 if ( slap_dynacl_config( fname, lineno, b, name, opts, sty, right ) ) {
1613                                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1614                                                                 "unable to configure dynacl \"%s\".\n",
1615                                                                 fname, lineno, name );
1616                                                         return acl_usage();
1617                                                 }
1618
1619                                                 continue;
1620                                         }
1621                                 }
1622 #else /* ! SLAP_DYNACL */
1623
1624 #ifdef SLAPD_ACI_ENABLED
1625                                 if ( strcasecmp( left, "aci" ) == 0 ) {
1626                                         if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
1627                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1628                                                         "inappropriate style \"%s\" in by clause.\n",
1629                                                     fname, lineno, style );
1630                                                 return acl_usage();
1631                                         }
1632
1633                                         if( b->a_aci_at != NULL ) {
1634                                                 Debug( LDAP_DEBUG_ANY,
1635                                                         "%s: line %d: ACI attribute already specified.\n",
1636                                                         fname, lineno, 0 );
1637                                                 return acl_usage();
1638                                         }
1639
1640                                         if ( right != NULL && *right != '\0' ) {
1641                                                 rc = slap_str2ad( right, &b->a_aci_at, &text );
1642
1643                                                 if( rc != LDAP_SUCCESS ) {
1644                                                         char    buf[ SLAP_TEXT_BUFLEN ];
1645
1646                                                         snprintf( buf, sizeof( buf ),
1647                                                                 "aci \"%s\": %s.",
1648                                                                 right, text );
1649                                                         Debug( LDAP_DEBUG_ANY,
1650                                                                 "%s: line %d: %s\n",
1651                                                                 fname, lineno, buf );
1652                                                         return acl_usage();
1653                                                 }
1654
1655                                         } else {
1656                                                 b->a_aci_at = slap_ad_aci;
1657                                         }
1658
1659                                         if( !is_at_syntax( b->a_aci_at->ad_type,
1660                                                 SLAPD_ACI_SYNTAX) )
1661                                         {
1662                                                 char    buf[ SLAP_TEXT_BUFLEN ];
1663
1664                                                 snprintf( buf, sizeof( buf ),
1665                                                         "ACI \"%s\": inappropriate syntax: %s.",
1666                                                         right,
1667                                                         b->a_aci_at->ad_type->sat_syntax_oid );
1668                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: %s\n",
1669                                                         fname, lineno, buf );
1670                                                 return acl_usage();
1671                                         }
1672
1673                                         continue;
1674                                 }
1675 #endif /* SLAPD_ACI_ENABLED */
1676 #endif /* ! SLAP_DYNACL */
1677
1678                                 if ( strcasecmp( left, "ssf" ) == 0 ) {
1679                                         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
1680                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1681                                                         "inappropriate style \"%s\" in by clause.\n",
1682                                                     fname, lineno, style );
1683                                                 return acl_usage();
1684                                         }
1685
1686                                         if ( b->a_authz.sai_ssf ) {
1687                                                 Debug( LDAP_DEBUG_ANY,
1688                                                         "%s: line %d: ssf attribute already specified.\n",
1689                                                         fname, lineno, 0 );
1690                                                 return acl_usage();
1691                                         }
1692
1693                                         if ( right == NULL || *right == '\0' ) {
1694                                                 Debug( LDAP_DEBUG_ANY,
1695                                                         "%s: line %d: no ssf is defined.\n",
1696                                                         fname, lineno, 0 );
1697                                                 return acl_usage();
1698                                         }
1699
1700                                         if ( lutil_atou( &b->a_authz.sai_ssf, right ) != 0 ) {
1701                                                 Debug( LDAP_DEBUG_ANY,
1702                                                         "%s: line %d: unable to parse ssf value (%s).\n",
1703                                                         fname, lineno, right );
1704                                                 return acl_usage();
1705                                         }
1706
1707                                         if ( !b->a_authz.sai_ssf ) {
1708                                                 Debug( LDAP_DEBUG_ANY,
1709                                                         "%s: line %d: invalid ssf value (%s).\n",
1710                                                         fname, lineno, right );
1711                                                 return acl_usage();
1712                                         }
1713                                         continue;
1714                                 }
1715
1716                                 if ( strcasecmp( left, "transport_ssf" ) == 0 ) {
1717                                         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
1718                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1719                                                         "inappropriate style \"%s\" in by clause.\n",
1720                                                         fname, lineno, style );
1721                                                 return acl_usage();
1722                                         }
1723
1724                                         if ( b->a_authz.sai_transport_ssf ) {
1725                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1726                                                         "transport_ssf attribute already specified.\n",
1727                                                         fname, lineno, 0 );
1728                                                 return acl_usage();
1729                                         }
1730
1731                                         if ( right == NULL || *right == '\0' ) {
1732                                                 Debug( LDAP_DEBUG_ANY,
1733                                                         "%s: line %d: no transport_ssf is defined.\n",
1734                                                         fname, lineno, 0 );
1735                                                 return acl_usage();
1736                                         }
1737
1738                                         if ( lutil_atou( &b->a_authz.sai_transport_ssf, right ) != 0 ) {
1739                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1740                                                         "unable to parse transport_ssf value (%s).\n",
1741                                                         fname, lineno, right );
1742                                                 return acl_usage();
1743                                         }
1744
1745                                         if ( !b->a_authz.sai_transport_ssf ) {
1746                                                 Debug( LDAP_DEBUG_ANY,
1747                                                         "%s: line %d: invalid transport_ssf value (%s).\n",
1748                                                         fname, lineno, right );
1749                                                 return acl_usage();
1750                                         }
1751                                         continue;
1752                                 }
1753
1754                                 if ( strcasecmp( left, "tls_ssf" ) == 0 ) {
1755                                         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
1756                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1757                                                         "inappropriate style \"%s\" in by clause.\n",
1758                                                         fname, lineno, style );
1759                                                 return acl_usage();
1760                                         }
1761
1762                                         if ( b->a_authz.sai_tls_ssf ) {
1763                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1764                                                         "tls_ssf attribute already specified.\n",
1765                                                         fname, lineno, 0 );
1766                                                 return acl_usage();
1767                                         }
1768
1769                                         if ( right == NULL || *right == '\0' ) {
1770                                                 Debug( LDAP_DEBUG_ANY,
1771                                                         "%s: line %d: no tls_ssf is defined\n",
1772                                                         fname, lineno, 0 );
1773                                                 return acl_usage();
1774                                         }
1775
1776                                         if ( lutil_atou( &b->a_authz.sai_tls_ssf, right ) != 0 ) {
1777                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1778                                                         "unable to parse tls_ssf value (%s).\n",
1779                                                         fname, lineno, right );
1780                                                 return acl_usage();
1781                                         }
1782
1783                                         if ( !b->a_authz.sai_tls_ssf ) {
1784                                                 Debug( LDAP_DEBUG_ANY,
1785                                                         "%s: line %d: invalid tls_ssf value (%s).\n",
1786                                                         fname, lineno, right );
1787                                                 return acl_usage();
1788                                         }
1789                                         continue;
1790                                 }
1791
1792                                 if ( strcasecmp( left, "sasl_ssf" ) == 0 ) {
1793                                         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
1794                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1795                                                         "inappropriate style \"%s\" in by clause.\n",
1796                                                         fname, lineno, style );
1797                                                 return acl_usage();
1798                                         }
1799
1800                                         if ( b->a_authz.sai_sasl_ssf ) {
1801                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1802                                                         "sasl_ssf attribute already specified.\n",
1803                                                         fname, lineno, 0 );
1804                                                 return acl_usage();
1805                                         }
1806
1807                                         if ( right == NULL || *right == '\0' ) {
1808                                                 Debug( LDAP_DEBUG_ANY,
1809                                                         "%s: line %d: no sasl_ssf is defined.\n",
1810                                                         fname, lineno, 0 );
1811                                                 return acl_usage();
1812                                         }
1813
1814                                         if ( lutil_atou( &b->a_authz.sai_sasl_ssf, right ) != 0 ) {
1815                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1816                                                         "unable to parse sasl_ssf value (%s).\n",
1817                                                         fname, lineno, right );
1818                                                 return acl_usage();
1819                                         }
1820
1821                                         if ( !b->a_authz.sai_sasl_ssf ) {
1822                                                 Debug( LDAP_DEBUG_ANY,
1823                                                         "%s: line %d: invalid sasl_ssf value (%s).\n",
1824                                                         fname, lineno, right );
1825                                                 return acl_usage();
1826                                         }
1827                                         continue;
1828                                 }
1829
1830                                 if ( right != NULL ) {
1831                                         /* unsplit */
1832                                         right[-1] = '=';
1833                                 }
1834                                 break;
1835                         }
1836
1837                         if ( i == argc || ( strcasecmp( left, "stop" ) == 0 ) ) { 
1838                                 /* out of arguments or plain stop */
1839
1840                                 ACL_PRIV_ASSIGN( b->a_access_mask, ACL_PRIV_ADDITIVE );
1841                                 b->a_type = ACL_STOP;
1842
1843                                 access_append( &a->acl_access, b );
1844                                 continue;
1845                         }
1846
1847                         if ( strcasecmp( left, "continue" ) == 0 ) {
1848                                 /* plain continue */
1849
1850                                 ACL_PRIV_ASSIGN( b->a_access_mask, ACL_PRIV_ADDITIVE );
1851                                 b->a_type = ACL_CONTINUE;
1852
1853                                 access_append( &a->acl_access, b );
1854                                 continue;
1855                         }
1856
1857                         if ( strcasecmp( left, "break" ) == 0 ) {
1858                                 /* plain continue */
1859
1860                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1861                                 b->a_type = ACL_BREAK;
1862
1863                                 access_append( &a->acl_access, b );
1864                                 continue;
1865                         }
1866
1867                         if ( strcasecmp( left, "by" ) == 0 ) {
1868                                 /* we've gone too far */
1869                                 --i;
1870                                 ACL_PRIV_ASSIGN( b->a_access_mask, ACL_PRIV_ADDITIVE );
1871                                 b->a_type = ACL_STOP;
1872
1873                                 access_append( &a->acl_access, b );
1874                                 continue;
1875                         }
1876
1877                         /* get <access> */
1878                         if ( strncasecmp( left, "self", STRLENOF( "self" ) ) == 0 ) {
1879                                 b->a_dn_self = 1;
1880                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( &left[ STRLENOF( "self" ) ] ) );
1881
1882                         } else if ( strncasecmp( left, "realself", STRLENOF( "realself" ) ) == 0 ) {
1883                                 b->a_realdn_self = 1;
1884                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( &left[ STRLENOF( "realself" ) ] ) );
1885
1886                         } else {
1887                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( left ) );
1888                         }
1889
1890                         if ( ACL_IS_INVALID( b->a_access_mask ) ) {
1891                                 Debug( LDAP_DEBUG_ANY,
1892                                         "%s: line %d: expecting <access> got \"%s\".\n",
1893                                         fname, lineno, left );
1894                                 return acl_usage();
1895                         }
1896
1897                         b->a_type = ACL_STOP;
1898
1899                         if ( ++i == argc ) {
1900                                 /* out of arguments or plain stop */
1901                                 access_append( &a->acl_access, b );
1902                                 continue;
1903                         }
1904
1905                         if ( strcasecmp( argv[i], "continue" ) == 0 ) {
1906                                 /* plain continue */
1907                                 b->a_type = ACL_CONTINUE;
1908
1909                         } else if ( strcasecmp( argv[i], "break" ) == 0 ) {
1910                                 /* plain continue */
1911                                 b->a_type = ACL_BREAK;
1912
1913                         } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
1914                                 /* gone to far */
1915                                 i--;
1916                         }
1917
1918                         access_append( &a->acl_access, b );
1919
1920                 } else {
1921                         Debug( LDAP_DEBUG_ANY,
1922                                 "%s: line %d: expecting \"to\" "
1923                                 "or \"by\" got \"%s\"\n",
1924                                 fname, lineno, argv[i] );
1925                         return acl_usage();
1926                 }
1927         }
1928
1929         /* if we have no real access clause, complain and do nothing */
1930         if ( a == NULL ) {
1931                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1932                         "warning: no access clause(s) "
1933                         "specified in access line"
1934                         SLAPD_CONF_UNKNOWN_IGNORED ".\n",
1935                         fname, lineno, 0 );
1936 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
1937                 return acl_usage();
1938 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
1939
1940         } else {
1941 #ifdef LDAP_DEBUG
1942                 if ( ldap_debug & LDAP_DEBUG_ACL ) {
1943                         print_acl( be, a );
1944                 }
1945 #endif
1946         
1947                 if ( a->acl_access == NULL ) {
1948                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1949                                 "warning: no by clause(s) "
1950                                 "specified in access line"
1951                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
1952                                 fname, lineno, 0 );
1953 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
1954                         return acl_usage();
1955 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
1956                 }
1957
1958                 if ( be != NULL ) {
1959                         if ( !BER_BVISNULL( &be->be_nsuffix[ 1 ] ) ) {
1960                                 Debug( LDAP_DEBUG_ACL, "%s: line %d: warning: "
1961                                         "scope checking only applies to single-valued "
1962                                         "suffix databases\n",
1963                                         fname, lineno, 0 );
1964                                 /* go ahead, since checking is not authoritative */
1965                         }
1966
1967                         switch ( check_scope( be, a ) ) {
1968                         case ACL_SCOPE_UNKNOWN:
1969                                 Debug( LDAP_DEBUG_ACL, "%s: line %d: warning: "
1970                                         "cannot assess the validity of the ACL scope within "
1971                                         "backend naming context\n",
1972                                         fname, lineno, 0 );
1973                                 break;
1974
1975                         case ACL_SCOPE_WARN:
1976                                 Debug( LDAP_DEBUG_ACL, "%s: line %d: warning: "
1977                                         "ACL could be out of scope within backend naming context\n",
1978                                         fname, lineno, 0 );
1979                                 break;
1980
1981                         case ACL_SCOPE_PARTIAL:
1982                                 Debug( LDAP_DEBUG_ACL, "%s: line %d: warning: "
1983                                         "ACL appears to be partially out of scope within "
1984                                         "backend naming context\n",
1985                                         fname, lineno, 0 );
1986                                 break;
1987
1988                         case ACL_SCOPE_ERR:
1989                                 Debug( LDAP_DEBUG_ACL, "%s: line %d: warning: "
1990                                         "ACL appears to be out of scope within "
1991                                         "backend naming context\n",
1992                                         fname, lineno, 0 );
1993                                 break;
1994
1995                         default:
1996                                 break;
1997                         }
1998                         acl_append( &be->be_acl, a, pos );
1999
2000                 } else {
2001                         acl_append( &frontendDB->be_acl, a, pos );
2002                 }
2003         }
2004
2005         return 0;
2006 }
2007
2008 char *
2009 accessmask2str( slap_mask_t mask, char *buf, int debug )
2010 {
2011         int     none = 1;
2012         char    *ptr = buf;
2013
2014         assert( buf != NULL );
2015
2016         if ( ACL_IS_INVALID( mask ) ) {
2017                 return "invalid";
2018         }
2019
2020         buf[0] = '\0';
2021
2022         if ( ACL_IS_LEVEL( mask ) ) {
2023                 if ( ACL_LVL_IS_NONE(mask) ) {
2024                         ptr = lutil_strcopy( ptr, "none" );
2025
2026                 } else if ( ACL_LVL_IS_DISCLOSE(mask) ) {
2027                         ptr = lutil_strcopy( ptr, "disclose" );
2028
2029                 } else if ( ACL_LVL_IS_AUTH(mask) ) {
2030                         ptr = lutil_strcopy( ptr, "auth" );
2031
2032                 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
2033                         ptr = lutil_strcopy( ptr, "compare" );
2034
2035                 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
2036                         ptr = lutil_strcopy( ptr, "search" );
2037
2038                 } else if ( ACL_LVL_IS_READ(mask) ) {
2039                         ptr = lutil_strcopy( ptr, "read" );
2040
2041                 } else if ( ACL_LVL_IS_WRITE(mask) ) {
2042                         ptr = lutil_strcopy( ptr, "write" );
2043
2044                 } else if ( ACL_LVL_IS_WADD(mask) ) {
2045                         ptr = lutil_strcopy( ptr, "add" );
2046
2047                 } else if ( ACL_LVL_IS_WDEL(mask) ) {
2048                         ptr = lutil_strcopy( ptr, "delete" );
2049
2050                 } else if ( ACL_LVL_IS_MANAGE(mask) ) {
2051                         ptr = lutil_strcopy( ptr, "manage" );
2052
2053                 } else {
2054                         ptr = lutil_strcopy( ptr, "unknown" );
2055                 }
2056                 
2057                 if ( !debug ) {
2058                         *ptr = '\0';
2059                         return buf;
2060                 }
2061                 *ptr++ = '(';
2062         }
2063
2064         if( ACL_IS_ADDITIVE( mask ) ) {
2065                 *ptr++ = '+';
2066
2067         } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
2068                 *ptr++ = '-';
2069
2070         } else {
2071                 *ptr++ = '=';
2072         }
2073
2074         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_MANAGE) ) {
2075                 none = 0;
2076                 *ptr++ = 'm';
2077         } 
2078
2079         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
2080                 none = 0;
2081                 *ptr++ = 'w';
2082
2083         } else if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WADD) ) {
2084                 none = 0;
2085                 *ptr++ = 'a';
2086
2087         } else if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WDEL) ) {
2088                 none = 0;
2089                 *ptr++ = 'z';
2090         } 
2091
2092         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
2093                 none = 0;
2094                 *ptr++ = 'r';
2095         } 
2096
2097         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
2098                 none = 0;
2099                 *ptr++ = 's';
2100         } 
2101
2102         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
2103                 none = 0;
2104                 *ptr++ = 'c';
2105         } 
2106
2107         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
2108                 none = 0;
2109                 *ptr++ = 'x';
2110         } 
2111
2112         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_DISCLOSE) ) {
2113                 none = 0;
2114                 *ptr++ = 'd';
2115         } 
2116
2117         if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
2118                 none = 0;
2119                 *ptr++ = '0';
2120         } 
2121
2122         if ( none ) {
2123                 ptr = buf;
2124         }
2125
2126         if ( ACL_IS_LEVEL( mask ) ) {
2127                 *ptr++ = ')';
2128         }
2129
2130         *ptr = '\0';
2131
2132         return buf;
2133 }
2134
2135 slap_mask_t
2136 str2accessmask( const char *str )
2137 {
2138         slap_mask_t     mask;
2139
2140         if( !ASCII_ALPHA(str[0]) ) {
2141                 int i;
2142
2143                 if ( str[0] == '=' ) {
2144                         ACL_INIT(mask);
2145
2146                 } else if( str[0] == '+' ) {
2147                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
2148
2149                 } else if( str[0] == '-' ) {
2150                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
2151
2152                 } else {
2153                         ACL_INVALIDATE(mask);
2154                         return mask;
2155                 }
2156
2157                 for( i=1; str[i] != '\0'; i++ ) {
2158                         if( TOLOWER((unsigned char) str[i]) == 'm' ) {
2159                                 ACL_PRIV_SET(mask, ACL_PRIV_MANAGE);
2160
2161                         } else if( TOLOWER((unsigned char) str[i]) == 'w' ) {
2162                                 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2163
2164                         } else if( TOLOWER((unsigned char) str[i]) == 'a' ) {
2165                                 ACL_PRIV_SET(mask, ACL_PRIV_WADD);
2166
2167                         } else if( TOLOWER((unsigned char) str[i]) == 'z' ) {
2168                                 ACL_PRIV_SET(mask, ACL_PRIV_WDEL);
2169
2170                         } else if( TOLOWER((unsigned char) str[i]) == 'r' ) {
2171                                 ACL_PRIV_SET(mask, ACL_PRIV_READ);
2172
2173                         } else if( TOLOWER((unsigned char) str[i]) == 's' ) {
2174                                 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2175
2176                         } else if( TOLOWER((unsigned char) str[i]) == 'c' ) {
2177                                 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2178
2179                         } else if( TOLOWER((unsigned char) str[i]) == 'x' ) {
2180                                 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2181
2182                         } else if( TOLOWER((unsigned char) str[i]) == 'd' ) {
2183                                 ACL_PRIV_SET(mask, ACL_PRIV_DISCLOSE);
2184
2185                         } else if( str[i] != '0' ) {
2186                                 ACL_INVALIDATE(mask);
2187                                 return mask;
2188                         }
2189                 }
2190
2191                 return mask;
2192         }
2193
2194         if ( strcasecmp( str, "none" ) == 0 ) {
2195                 ACL_LVL_ASSIGN_NONE(mask);
2196
2197         } else if ( strcasecmp( str, "disclose" ) == 0 ) {
2198                 ACL_LVL_ASSIGN_DISCLOSE(mask);
2199
2200         } else if ( strcasecmp( str, "auth" ) == 0 ) {
2201                 ACL_LVL_ASSIGN_AUTH(mask);
2202
2203         } else if ( strcasecmp( str, "compare" ) == 0 ) {
2204                 ACL_LVL_ASSIGN_COMPARE(mask);
2205
2206         } else if ( strcasecmp( str, "search" ) == 0 ) {
2207                 ACL_LVL_ASSIGN_SEARCH(mask);
2208
2209         } else if ( strcasecmp( str, "read" ) == 0 ) {
2210                 ACL_LVL_ASSIGN_READ(mask);
2211
2212         } else if ( strcasecmp( str, "add" ) == 0 ) {
2213                 ACL_LVL_ASSIGN_WADD(mask);
2214
2215         } else if ( strcasecmp( str, "delete" ) == 0 ) {
2216                 ACL_LVL_ASSIGN_WDEL(mask);
2217
2218         } else if ( strcasecmp( str, "write" ) == 0 ) {
2219                 ACL_LVL_ASSIGN_WRITE(mask);
2220
2221         } else if ( strcasecmp( str, "manage" ) == 0 ) {
2222                 ACL_LVL_ASSIGN_MANAGE(mask);
2223
2224         } else {
2225                 ACL_INVALIDATE( mask );
2226         }
2227
2228         return mask;
2229 }
2230
2231 static int
2232 acl_usage( void )
2233 {
2234         char *access =
2235                 "<access clause> ::= access to <what> "
2236                                 "[ by <who> <access> [ <control> ] ]+ \n";
2237         char *what =
2238                 "<what> ::= * | [dn[.<dnstyle>]=<DN>] [filter=<filter>] [attrs=<attrspec>]\n"
2239                 "<attrspec> ::= <attrname> [val[/<matchingRule>][.<attrstyle>]=<value>] | <attrlist>\n"
2240                 "<attrlist> ::= <attr> [ , <attrlist> ]\n"
2241                 "<attr> ::= <attrname> | @<objectClass> | !<objectClass> | entry | children\n";
2242
2243         char *who =
2244                 "<who> ::= [ * | anonymous | users | self | dn[.<dnstyle>]=<DN> ]\n"
2245                         "\t[ realanonymous | realusers | realself | realdn[.<dnstyle>]=<DN> ]\n"
2246                         "\t[dnattr=<attrname>]\n"
2247                         "\t[realdnattr=<attrname>]\n"
2248                         "\t[group[/<objectclass>[/<attrname>]][.<style>]=<group>]\n"
2249                         "\t[peername[.<peernamestyle>]=<peer>] [sockname[.<style>]=<name>]\n"
2250                         "\t[domain[.<domainstyle>]=<domain>] [sockurl[.<style>]=<url>]\n"
2251 #ifdef SLAP_DYNACL
2252                         "\t[dynacl/<name>[/<options>][.<dynstyle>][=<pattern>]]\n"
2253 #else /* ! SLAP_DYNACL */
2254 #ifdef SLAPD_ACI_ENABLED
2255                         "\t[aci[=<attrname>]]\n"
2256 #endif /* SLAPD_ACI_ENABLED */
2257 #endif /* ! SLAP_DYNACL */
2258                         "\t[ssf=<n>] [transport_ssf=<n>] [tls_ssf=<n>] [sasl_ssf=<n>]\n"
2259                 "<style> ::= exact | regex | base(Object)\n"
2260                 "<dnstyle> ::= base(Object) | one(level) | sub(tree) | children | "
2261                         "exact | regex\n"
2262                 "<attrstyle> ::= exact | regex | base(Object) | one(level) | "
2263                         "sub(tree) | children\n"
2264                 "<peernamestyle> ::= exact | regex | ip | path\n"
2265                 "<domainstyle> ::= exact | regex | base(Object) | sub(tree)\n"
2266                 "<access> ::= [[real]self]{<level>|<priv>}\n"
2267                 "<level> ::= none|disclose|auth|compare|search|read|{write|add|delete}|manage\n"
2268                 "<priv> ::= {=|+|-}{0|d|x|c|s|r|{w|a|z}|m}+\n"
2269                 "<control> ::= [ stop | continue | break ]\n"
2270 #ifdef SLAP_DYNACL
2271 #ifdef SLAPD_ACI_ENABLED
2272                 "dynacl:\n"
2273                 "\t<name>=ACI\t<pattern>=<attrname>\n"
2274 #endif /* SLAPD_ACI_ENABLED */
2275 #endif /* ! SLAP_DYNACL */
2276                 "";
2277
2278         Debug( LDAP_DEBUG_ANY, "%s%s%s\n", access, what, who );
2279
2280         return 1;
2281 }
2282
2283 /*
2284  * Set pattern to a "normalized" DN from src.
2285  * At present it simply eats the (optional) space after 
2286  * a RDN separator (,)
2287  * Eventually will evolve in a more complete normalization
2288  */
2289 static void
2290 acl_regex_normalized_dn(
2291         const char *src,
2292         struct berval *pattern )
2293 {
2294         char *str, *p;
2295         ber_len_t len;
2296
2297         str = ch_strdup( src );
2298         len = strlen( src );
2299
2300         for ( p = str; p && p[0]; p++ ) {
2301                 /* escape */
2302                 if ( p[0] == '\\' && p[1] ) {
2303                         /* 
2304                          * if escaping a hex pair we should
2305                          * increment p twice; however, in that 
2306                          * case the second hex number does 
2307                          * no harm
2308                          */
2309                         p++;
2310                 }
2311
2312                 if ( p[0] == ',' && p[1] == ' ' ) {
2313                         char *q;
2314                         
2315                         /*
2316                          * too much space should be an error if we are pedantic
2317                          */
2318                         for ( q = &p[2]; q[0] == ' '; q++ ) {
2319                                 /* DO NOTHING */ ;
2320                         }
2321                         AC_MEMCPY( p+1, q, len-(q-str)+1);
2322                 }
2323         }
2324         pattern->bv_val = str;
2325         pattern->bv_len = p - str;
2326
2327         return;
2328 }
2329
2330 static void
2331 split(
2332     char        *line,
2333     int         splitchar,
2334     char        **left,
2335     char        **right )
2336 {
2337         *left = line;
2338         if ( (*right = strchr( line, splitchar )) != NULL ) {
2339                 *((*right)++) = '\0';
2340         }
2341 }
2342
2343 static void
2344 access_append( Access **l, Access *a )
2345 {
2346         for ( ; *l != NULL; l = &(*l)->a_next ) {
2347                 ;       /* Empty */
2348         }
2349
2350         *l = a;
2351 }
2352
2353 void
2354 acl_append( AccessControl **l, AccessControl *a, int pos )
2355 {
2356         int i;
2357
2358         for (i=0 ; i != pos && *l != NULL; l = &(*l)->acl_next, i++ ) {
2359                 ;       /* Empty */
2360         }
2361         if ( *l && a )
2362                 a->acl_next = *l;
2363         *l = a;
2364 }
2365
2366 static void
2367 access_free( Access *a )
2368 {
2369         if ( !BER_BVISNULL( &a->a_dn_pat ) ) {
2370                 free( a->a_dn_pat.bv_val );
2371         }
2372         if ( !BER_BVISNULL( &a->a_realdn_pat ) ) {
2373                 free( a->a_realdn_pat.bv_val );
2374         }
2375         if ( !BER_BVISNULL( &a->a_peername_pat ) ) {
2376                 free( a->a_peername_pat.bv_val );
2377         }
2378         if ( !BER_BVISNULL( &a->a_sockname_pat ) ) {
2379                 free( a->a_sockname_pat.bv_val );
2380         }
2381         if ( !BER_BVISNULL( &a->a_domain_pat ) ) {
2382                 free( a->a_domain_pat.bv_val );
2383         }
2384         if ( !BER_BVISNULL( &a->a_sockurl_pat ) ) {
2385                 free( a->a_sockurl_pat.bv_val );
2386         }
2387         if ( !BER_BVISNULL( &a->a_set_pat ) ) {
2388                 free( a->a_set_pat.bv_val );
2389         }
2390         if ( !BER_BVISNULL( &a->a_group_pat ) ) {
2391                 free( a->a_group_pat.bv_val );
2392         }
2393 #ifdef SLAP_DYNACL
2394         if ( a->a_dynacl != NULL ) {
2395                 slap_dynacl_t   *da;
2396                 for ( da = a->a_dynacl; da; ) {
2397                         slap_dynacl_t   *tmp = da;
2398
2399                         da = da->da_next;
2400
2401                         if ( tmp->da_destroy ) {
2402                                 tmp->da_destroy( tmp->da_private );
2403                         }
2404
2405                         ch_free( tmp );
2406                 }
2407         }
2408 #endif /* SLAP_DYNACL */
2409         free( a );
2410 }
2411
2412 void
2413 acl_free( AccessControl *a )
2414 {
2415         Access *n;
2416         AttributeName *an;
2417
2418         if ( a->acl_filter ) {
2419                 filter_free( a->acl_filter );
2420         }
2421         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
2422                 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
2423                         regfree( &a->acl_dn_re );
2424                 }
2425                 free ( a->acl_dn_pat.bv_val );
2426         }
2427         if ( a->acl_attrs ) {
2428                 for ( an = a->acl_attrs; !BER_BVISNULL( &an->an_name ); an++ ) {
2429                         free( an->an_name.bv_val );
2430                 }
2431                 free( a->acl_attrs );
2432         }
2433         for ( ; a->acl_access; a->acl_access = n ) {
2434                 n = a->acl_access->a_next;
2435                 access_free( a->acl_access );
2436         }
2437         free( a );
2438 }
2439
2440 /* Because backend_startup uses acl_append to tack on the global_acl to
2441  * the end of each backend's acl, we cannot just take one argument and
2442  * merrily free our way to the end of the list. backend_destroy calls us
2443  * with the be_acl in arg1, and global_acl in arg2 to give us a stopping
2444  * point. config_destroy calls us with global_acl in arg1 and NULL in
2445  * arg2, so we then proceed to polish off the global_acl.
2446  */
2447 void
2448 acl_destroy( AccessControl *a, AccessControl *end )
2449 {
2450         AccessControl *n;
2451
2452         for ( ; a && a != end; a = n ) {
2453                 n = a->acl_next;
2454                 acl_free( a );
2455         }
2456 }
2457
2458 char *
2459 access2str( slap_access_t access )
2460 {
2461         if ( access == ACL_NONE ) {
2462                 return "none";
2463
2464         } else if ( access == ACL_DISCLOSE ) {
2465                 return "disclose";
2466
2467         } else if ( access == ACL_AUTH ) {
2468                 return "auth";
2469
2470         } else if ( access == ACL_COMPARE ) {
2471                 return "compare";
2472
2473         } else if ( access == ACL_SEARCH ) {
2474                 return "search";
2475
2476         } else if ( access == ACL_READ ) {
2477                 return "read";
2478
2479         } else if ( access == ACL_WRITE ) {
2480                 return "write";
2481
2482         } else if ( access == ACL_WADD ) {
2483                 return "add";
2484
2485         } else if ( access == ACL_WDEL ) {
2486                 return "delete";
2487
2488         } else if ( access == ACL_MANAGE ) {
2489                 return "manage";
2490
2491         }
2492
2493         return "unknown";
2494 }
2495
2496 slap_access_t
2497 str2access( const char *str )
2498 {
2499         if ( strcasecmp( str, "none" ) == 0 ) {
2500                 return ACL_NONE;
2501
2502         } else if ( strcasecmp( str, "disclose" ) == 0 ) {
2503 #ifndef SLAP_ACL_HONOR_DISCLOSE
2504                 Debug( LDAP_DEBUG_ACL, "str2access: warning, "
2505                         "\"disclose\" privilege disabled.\n",
2506                 0, 0, 0 );
2507 #endif /* SLAP_ACL_HONOR_DISCLOSE */
2508                 return ACL_DISCLOSE;
2509
2510         } else if ( strcasecmp( str, "auth" ) == 0 ) {
2511                 return ACL_AUTH;
2512
2513         } else if ( strcasecmp( str, "compare" ) == 0 ) {
2514                 return ACL_COMPARE;
2515
2516         } else if ( strcasecmp( str, "search" ) == 0 ) {
2517                 return ACL_SEARCH;
2518
2519         } else if ( strcasecmp( str, "read" ) == 0 ) {
2520                 return ACL_READ;
2521
2522         } else if ( strcasecmp( str, "write" ) == 0 ) {
2523                 return ACL_WRITE;
2524
2525         } else if ( strcasecmp( str, "add" ) == 0 ) {
2526                 return ACL_WADD;
2527
2528         } else if ( strcasecmp( str, "delete" ) == 0 ) {
2529                 return ACL_WDEL;
2530
2531         } else if ( strcasecmp( str, "manage" ) == 0 ) {
2532                 return ACL_MANAGE;
2533         }
2534
2535         return( ACL_INVALID_ACCESS );
2536 }
2537
2538 #define ACLBUF_MAXLEN   8192
2539
2540 static char aclbuf[ACLBUF_MAXLEN];
2541
2542 static char *
2543 dnaccess2text( slap_dn_access *bdn, char *ptr, int is_realdn )
2544 {
2545         *ptr++ = ' ';
2546
2547         if ( is_realdn ) {
2548                 ptr = lutil_strcopy( ptr, "real" );
2549         }
2550
2551         if ( ber_bvccmp( &bdn->a_pat, '*' ) ||
2552                 bdn->a_style == ACL_STYLE_ANONYMOUS ||
2553                 bdn->a_style == ACL_STYLE_USERS ||
2554                 bdn->a_style == ACL_STYLE_SELF )
2555         {
2556                 if ( is_realdn ) {
2557                         assert( ! ber_bvccmp( &bdn->a_pat, '*' ) );
2558                 }
2559                         
2560                 ptr = lutil_strcopy( ptr, bdn->a_pat.bv_val );
2561                 if ( bdn->a_style == ACL_STYLE_SELF && bdn->a_self_level != 0 ) {
2562                         int n = sprintf( ptr, ".level{%d}", bdn->a_self_level );
2563                         if ( n > 0 ) {
2564                                 ptr += n;
2565                         } /* else ? */
2566                 }
2567
2568         } else {
2569                 ptr = lutil_strcopy( ptr, "dn." );
2570                 if ( bdn->a_style == ACL_STYLE_BASE )
2571                         ptr = lutil_strcopy( ptr, style_base );
2572                 else 
2573                         ptr = lutil_strcopy( ptr, style_strings[bdn->a_style] );
2574                 if ( bdn->a_style == ACL_STYLE_LEVEL ) {
2575                         int n = sprintf( ptr, "{%d}", bdn->a_level );
2576                         if ( n > 0 ) {
2577                                 ptr += n;
2578                         } /* else ? */
2579                 }
2580                 if ( bdn->a_expand ) {
2581                         ptr = lutil_strcopy( ptr, ",expand" );
2582                 }
2583                 *ptr++ = '=';
2584                 *ptr++ = '"';
2585                 ptr = lutil_strcopy( ptr, bdn->a_pat.bv_val );
2586                 *ptr++ = '"';
2587         }
2588         return ptr;
2589 }
2590
2591 static char *
2592 access2text( Access *b, char *ptr )
2593 {
2594         char maskbuf[ACCESSMASK_MAXLEN];
2595
2596         ptr = lutil_strcopy( ptr, "\tby" );
2597
2598         if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
2599                 ptr = dnaccess2text( &b->a_dn, ptr, 0 );
2600         }
2601         if ( b->a_dn_at ) {
2602                 ptr = lutil_strcopy( ptr, " dnattr=" );
2603                 ptr = lutil_strcopy( ptr, b->a_dn_at->ad_cname.bv_val );
2604         }
2605
2606         if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
2607                 ptr = dnaccess2text( &b->a_realdn, ptr, 1 );
2608         }
2609         if ( b->a_realdn_at ) {
2610                 ptr = lutil_strcopy( ptr, " realdnattr=" );
2611                 ptr = lutil_strcopy( ptr, b->a_realdn_at->ad_cname.bv_val );
2612         }
2613
2614         if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
2615                 ptr = lutil_strcopy( ptr, " group/" );
2616                 ptr = lutil_strcopy( ptr, b->a_group_oc ?
2617                         b->a_group_oc->soc_cname.bv_val : SLAPD_GROUP_CLASS );
2618                 *ptr++ = '/';
2619                 ptr = lutil_strcopy( ptr, b->a_group_at ?
2620                         b->a_group_at->ad_cname.bv_val : SLAPD_GROUP_ATTR );
2621                 *ptr++ = '.';
2622                 ptr = lutil_strcopy( ptr, style_strings[b->a_group_style] );
2623                 *ptr++ = '=';
2624                 *ptr++ = '"';
2625                 ptr = lutil_strcopy( ptr, b->a_group_pat.bv_val );
2626                 *ptr++ = '"';
2627         }
2628
2629         if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
2630                 ptr = lutil_strcopy( ptr, " peername" );
2631                 *ptr++ = '.';
2632                 ptr = lutil_strcopy( ptr, style_strings[b->a_peername_style] );
2633                 *ptr++ = '=';
2634                 *ptr++ = '"';
2635                 ptr = lutil_strcopy( ptr, b->a_peername_pat.bv_val );
2636                 *ptr++ = '"';
2637         }
2638
2639         if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
2640                 ptr = lutil_strcopy( ptr, " sockname" );
2641                 *ptr++ = '.';
2642                 ptr = lutil_strcopy( ptr, style_strings[b->a_sockname_style] );
2643                 *ptr++ = '=';
2644                 *ptr++ = '"';
2645                 ptr = lutil_strcopy( ptr, b->a_sockname_pat.bv_val );
2646                 *ptr++ = '"';
2647         }
2648
2649         if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
2650                 ptr = lutil_strcopy( ptr, " domain" );
2651                 *ptr++ = '.';
2652                 ptr = lutil_strcopy( ptr, style_strings[b->a_domain_style] );
2653                 if ( b->a_domain_expand ) {
2654                         ptr = lutil_strcopy( ptr, ",expand" );
2655                 }
2656                 *ptr++ = '=';
2657                 ptr = lutil_strcopy( ptr, b->a_domain_pat.bv_val );
2658         }
2659
2660         if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
2661                 ptr = lutil_strcopy( ptr, " sockurl" );
2662                 *ptr++ = '.';
2663                 ptr = lutil_strcopy( ptr, style_strings[b->a_sockurl_style] );
2664                 *ptr++ = '=';
2665                 *ptr++ = '"';
2666                 ptr = lutil_strcopy( ptr, b->a_sockurl_pat.bv_val );
2667                 *ptr++ = '"';
2668         }
2669
2670         if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
2671                 ptr = lutil_strcopy( ptr, " set" );
2672                 *ptr++ = '.';
2673                 ptr = lutil_strcopy( ptr, style_strings[b->a_set_style] );
2674                 *ptr++ = '=';
2675                 *ptr++ = '"';
2676                 ptr = lutil_strcopy( ptr, b->a_set_pat.bv_val );
2677                 *ptr++ = '"';
2678         }
2679
2680 #ifdef SLAP_DYNACL
2681         if ( b->a_dynacl ) {
2682                 slap_dynacl_t   *da;
2683
2684                 for ( da = b->a_dynacl; da; da = da->da_next ) {
2685                         if ( da->da_unparse ) {
2686                                 struct berval bv = BER_BVNULL;
2687                                 (void)( *da->da_unparse )( da->da_private, &bv );
2688                                 assert( !BER_BVISNULL( &bv ) );
2689                                 ptr = lutil_strcopy( ptr, bv.bv_val );
2690                                 ch_free( bv.bv_val );
2691                         }
2692                 }
2693         }
2694 #else /* ! SLAP_DYNACL */
2695 #ifdef SLAPD_ACI_ENABLED
2696         if ( b->a_aci_at != NULL ) {
2697                 ptr = lutil_strcopy( ptr, " aci=" );
2698                 ptr = lutil_strcopy( ptr, b->a_aci_at->ad_cname.bv_val );
2699         }
2700 #endif
2701 #endif /* SLAP_DYNACL */
2702
2703         /* Security Strength Factors */
2704         if ( b->a_authz.sai_ssf ) {
2705                 ptr += sprintf( ptr, " ssf=%u", 
2706                         b->a_authz.sai_ssf );
2707         }
2708         if ( b->a_authz.sai_transport_ssf ) {
2709                 ptr += sprintf( ptr, " transport_ssf=%u",
2710                         b->a_authz.sai_transport_ssf );
2711         }
2712         if ( b->a_authz.sai_tls_ssf ) {
2713                 ptr += sprintf( ptr, " tls_ssf=%u",
2714                         b->a_authz.sai_tls_ssf );
2715         }
2716         if ( b->a_authz.sai_sasl_ssf ) {
2717                 ptr += sprintf( ptr, " sasl_ssf=%u",
2718                         b->a_authz.sai_sasl_ssf );
2719         }
2720
2721         *ptr++ = ' ';
2722         if ( b->a_dn_self ) {
2723                 ptr = lutil_strcopy( ptr, "self" );
2724         } else if ( b->a_realdn_self ) {
2725                 ptr = lutil_strcopy( ptr, "realself" );
2726         }
2727         ptr = lutil_strcopy( ptr, accessmask2str( b->a_access_mask, maskbuf, 0 ));
2728         if ( !maskbuf[0] ) ptr--;
2729
2730         if( b->a_type == ACL_BREAK ) {
2731                 ptr = lutil_strcopy( ptr, " break" );
2732
2733         } else if( b->a_type == ACL_CONTINUE ) {
2734                 ptr = lutil_strcopy( ptr, " continue" );
2735
2736         } else if( b->a_type != ACL_STOP ) {
2737                 ptr = lutil_strcopy( ptr, " unknown-control" );
2738         } else {
2739                 if ( !maskbuf[0] ) ptr = lutil_strcopy( ptr, " stop" );
2740         }
2741         *ptr++ = '\n';
2742
2743         return ptr;
2744 }
2745
2746 void
2747 acl_unparse( AccessControl *a, struct berval *bv )
2748 {
2749         Access  *b;
2750         char    *ptr;
2751         int     to = 0;
2752
2753         bv->bv_val = aclbuf;
2754         bv->bv_len = 0;
2755
2756         ptr = bv->bv_val;
2757
2758         ptr = lutil_strcopy( ptr, "to" );
2759         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
2760                 to++;
2761                 ptr = lutil_strcopy( ptr, " dn." );
2762                 if ( a->acl_dn_style == ACL_STYLE_BASE )
2763                         ptr = lutil_strcopy( ptr, style_base );
2764                 else
2765                         ptr = lutil_strcopy( ptr, style_strings[a->acl_dn_style] );
2766                 *ptr++ = '=';
2767                 *ptr++ = '"';
2768                 ptr = lutil_strcopy( ptr, a->acl_dn_pat.bv_val );
2769                 ptr = lutil_strcopy( ptr, "\"\n" );
2770         }
2771
2772         if ( a->acl_filter != NULL ) {
2773                 struct berval   bv = BER_BVNULL;
2774
2775                 to++;
2776                 filter2bv( a->acl_filter, &bv );
2777                 ptr = lutil_strcopy( ptr, " filter=\"" );
2778                 ptr = lutil_strcopy( ptr, bv.bv_val );
2779                 *ptr++ = '"';
2780                 *ptr++ = '\n';
2781                 ch_free( bv.bv_val );
2782         }
2783
2784         if ( a->acl_attrs != NULL ) {
2785                 int     first = 1;
2786                 AttributeName *an;
2787                 to++;
2788
2789                 ptr = lutil_strcopy( ptr, " attrs=" );
2790                 for ( an = a->acl_attrs; an && !BER_BVISNULL( &an->an_name ); an++ ) {
2791                         if ( ! first ) *ptr++ = ',';
2792                         if (an->an_oc) {
2793                                 *ptr++ = an->an_oc_exclude ? '!' : '@';
2794                                 ptr = lutil_strcopy( ptr, an->an_oc->soc_cname.bv_val );
2795
2796                         } else {
2797                                 ptr = lutil_strcopy( ptr, an->an_name.bv_val );
2798                         }
2799                         first = 0;
2800                 }
2801                 *ptr++ = '\n';
2802         }
2803
2804         if ( !BER_BVISEMPTY( &a->acl_attrval ) ) {
2805                 to++;
2806                 ptr = lutil_strcopy( ptr, " val." );
2807                 if ( a->acl_attrval_style == ACL_STYLE_BASE &&
2808                         a->acl_attrs[0].an_desc->ad_type->sat_syntax ==
2809                                 slap_schema.si_syn_distinguishedName )
2810                         ptr = lutil_strcopy( ptr, style_base );
2811                 else
2812                         ptr = lutil_strcopy( ptr, style_strings[a->acl_attrval_style] );
2813                 *ptr++ = '=';
2814                 *ptr++ = '"';
2815                 ptr = lutil_strcopy( ptr, a->acl_attrval.bv_val );
2816                 *ptr++ = '"';
2817                 *ptr++ = '\n';
2818         }
2819
2820         if( !to ) {
2821                 ptr = lutil_strcopy( ptr, " *\n" );
2822         }
2823
2824         for ( b = a->acl_access; b != NULL; b = b->a_next ) {
2825                 ptr = access2text( b, ptr );
2826         }
2827         *ptr = '\0';
2828         bv->bv_len = ptr - bv->bv_val;
2829 }
2830
2831 #ifdef LDAP_DEBUG
2832 static void
2833 print_acl( Backend *be, AccessControl *a )
2834 {
2835         struct berval bv;
2836
2837         acl_unparse( a, &bv );
2838         fprintf( stderr, "%s ACL: access %s\n",
2839                 be == NULL ? "Global" : "Backend", bv.bv_val );
2840 }
2841 #endif /* LDAP_DEBUG */