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