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