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