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