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