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