]> git.sur5r.net Git - openldap/blob - servers/slapd/aclparse.c
uses URL extensions to set socket permissions other than default
[openldap] / servers / slapd / aclparse.c
1 /* aclparse.c - routines to parse and check acl's */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/regex.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
17
18 #include "slap.h"
19
20 static void             split(char *line, int splitchar, char **left, char **right);
21 static void             access_append(Access **l, Access *a);
22 static void             acl_usage(void) LDAP_GCCATTR((noreturn));
23
24 static void             acl_regex_normalized_dn(struct berval *pattern);
25
26 #ifdef LDAP_DEBUG
27 static void             print_acl(Backend *be, AccessControl *a);
28 static void             print_access(Access *b);
29 #endif
30
31 static int
32 regtest(const char *fname, int lineno, char *pat) {
33         int e;
34         regex_t re;
35
36         char buf[512];
37         unsigned size;
38
39         char *sp;
40         char *dp;
41         int  flag;
42
43         sp = pat;
44         dp = buf;
45         size = 0;
46         buf[0] = '\0';
47
48         for (size = 0, flag = 0; (size < sizeof(buf)) && *sp; sp++) {
49                 if (flag) {
50                         if (*sp == '$'|| (*sp >= '0' && *sp <= '9')) {
51                                 *dp++ = *sp;
52                                 size++;
53                         }
54                         flag = 0;
55
56                 } else {
57                         if (*sp == '$') {
58                                 flag = 1;
59                         } else {
60                                 *dp++ = *sp;
61                                 size++;
62                         }
63                 }
64         }
65
66         *dp = '\0';
67         if ( size >= (sizeof(buf)-1) ) {
68                 fprintf( stderr,
69                         "%s: line %d: regular expression \"%s\" too large\n",
70                         fname, lineno, pat );
71                 acl_usage();
72         }
73
74         if ((e = regcomp(&re, buf, REG_EXTENDED|REG_ICASE))) {
75                 char error[512];
76                 regerror(e, &re, error, sizeof(error));
77                 fprintf( stderr,
78                         "%s: line %d: regular expression \"%s\" bad because of %s\n",
79                         fname, lineno, pat, error );
80                 acl_usage();
81                 return(0);
82         }
83         regfree(&re);
84         return(1);
85 }
86
87 void
88 parse_acl(
89     Backend     *be,
90     const char  *fname,
91     int         lineno,
92     int         argc,
93     char        **argv
94 )
95 {
96         int             i;
97         char            *left, *right, *style;
98         struct berval   bv;
99         AccessControl   *a;
100         Access  *b;
101         int rc;
102         const char *text;
103
104         a = NULL;
105         for ( i = 1; i < argc; i++ ) {
106                 /* to clause - select which entries are protected */
107                 if ( strcasecmp( argv[i], "to" ) == 0 ) {
108                         if ( a != NULL ) {
109                                 fprintf( stderr,
110                 "%s: line %d: only one to clause allowed in access line\n",
111                                     fname, lineno );
112                                 acl_usage();
113                         }
114                         a = (AccessControl *) ch_calloc( 1, sizeof(AccessControl) );
115                         for ( ++i; i < argc; i++ ) {
116                                 if ( strcasecmp( argv[i], "by" ) == 0 ) {
117                                         i--;
118                                         break;
119                                 }
120
121                                 if ( strcasecmp( argv[i], "*" ) == 0 ) {
122                                         if( a->acl_dn_pat.bv_len != 0 ) {
123                                                 fprintf( stderr,
124                                                         "%s: line %d: dn pattern"
125                                                         " already specified in to clause.\n",
126                                                         fname, lineno );
127                                                 acl_usage();
128                                         }
129
130                                         a->acl_dn_pat.bv_val = ch_strdup( "*" );
131                                         a->acl_dn_pat.bv_len = 1;
132                                         continue;
133                                 }
134
135                                 split( argv[i], '=', &left, &right );
136                                 split( left, '.', &left, &style );
137
138                                 if ( right == NULL ) {
139                                         fprintf( stderr,
140         "%s: line %d: missing \"=\" in \"%s\" in to clause\n",
141                                             fname, lineno, left );
142                                         acl_usage();
143                                 }
144
145                                 if ( strcasecmp( left, "dn" ) == 0 ) {
146                                         if( a->acl_dn_pat.bv_len != 0 ) {
147                                                 fprintf( stderr,
148                                                         "%s: line %d: dn pattern"
149                                                         " already specified in to clause.\n",
150                                                         fname, lineno );
151                                                 acl_usage();
152                                         }
153
154                                         if ( style == NULL || *style == '\0'
155                                                 || strcasecmp( style, "regex" ) == 0 )
156                                         {
157                                                 a->acl_dn_style = ACL_STYLE_REGEX;
158                                                 if ( strcmp(right, "*") == 0 
159                                                         || strcmp(right, ".*") == 0 
160                                                         || strcmp(right, ".*$") == 0 
161                                                         || strcmp(right, "^.*") == 0 
162                                                         || strcmp(right, "^.*$$") == 0
163                                                         || strcmp(right, ".*$$") == 0 
164                                                         || strcmp(right, "^.*$$") == 0 )
165                                                 {
166                                                         a->acl_dn_pat.bv_val = ch_strdup( "*" );
167                                                         a->acl_dn_pat.bv_len = 1;
168
169                                                 } else {
170                                                         a->acl_dn_pat.bv_val = right;
171                                                         acl_regex_normalized_dn( &a->acl_dn_pat );
172                                                 }
173                                         } else if ( strcasecmp( style, "base" ) == 0 ) {
174                                                 a->acl_dn_style = ACL_STYLE_BASE;
175                                                 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
176                                         } else if ( strcasecmp( style, "one" ) == 0 ) {
177                                                 a->acl_dn_style = ACL_STYLE_ONE;
178                                                 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
179                                         } else if ( strcasecmp( style, "subtree" ) == 0 ) {
180                                                 a->acl_dn_style = ACL_STYLE_SUBTREE;
181                                                 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
182                                         } else if ( strcasecmp( style, "children" ) == 0 ) {
183                                                 a->acl_dn_style = ACL_STYLE_CHILDREN;
184                                                 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
185                                         } else {
186                                                 fprintf( stderr,
187         "%s: line %d: unknown dn style \"%s\" in to clause\n",
188                                                     fname, lineno, style );
189                                                 acl_usage();
190                                         }
191
192                                         continue;
193                                 }
194
195                                 if ( strcasecmp( left, "filter" ) == 0 ) {
196                                         if ( (a->acl_filter = str2filter(
197                                             right )) == NULL ) {
198                                                 fprintf( stderr,
199                                 "%s: line %d: bad filter \"%s\" in to clause\n",
200                                                     fname, lineno, right );
201                                                 acl_usage();
202                                         }
203
204                                 } else if ( strncasecmp( left, "attr", 4 ) == 0 ) {
205                                         a->acl_attrs = str2anlist( a->acl_attrs,
206                                                 right, "," );
207                                         if ( a->acl_attrs == NULL ) {
208                                                 fprintf( stderr,
209                                 "%s: line %d: unknown attr \"%s\" in to clause\n",
210                                                     fname, lineno, right );
211                                                 acl_usage();
212                                         }
213                                 } else {
214                                         fprintf( stderr,
215                                                 "%s: line %d: expecting <what> got \"%s\"\n",
216                                             fname, lineno, left );
217                                         acl_usage();
218                                 }
219                         }
220
221                         if ( a->acl_dn_pat.bv_len != 0 && strcmp(a->acl_dn_pat.bv_val, "*") == 0) {
222                                 free( a->acl_dn_pat.bv_val );
223                                 a->acl_dn_pat.bv_val = NULL;
224                                 a->acl_dn_pat.bv_len = 0;
225                         }
226                         
227                         if( a->acl_dn_pat.bv_len != 0 ) {
228                                 if ( a->acl_dn_style != ACL_STYLE_REGEX )
229                                 {
230                                         struct berval bv;
231                                         dnNormalize2( NULL, &a->acl_dn_pat, &bv);
232                                         free( a->acl_dn_pat.bv_val );
233                                         a->acl_dn_pat = bv;
234                                 } else {
235                                         int e = regcomp( &a->acl_dn_re, a->acl_dn_pat.bv_val,
236                                                          REG_EXTENDED | REG_ICASE );
237                                         if ( e ) {
238                                                 char buf[512];
239                                                 regerror( e, &a->acl_dn_re, buf, sizeof(buf) );
240                                                 fprintf( stderr,
241                                         "%s: line %d: regular expression \"%s\" bad because of %s\n",
242                                                          fname, lineno, right, buf );
243                                                 acl_usage();
244                                         }
245                                 }
246                         }
247
248                 /* by clause - select who has what access to entries */
249                 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
250                         if ( a == NULL ) {
251                                 fprintf( stderr,
252                                         "%s: line %d: to clause required before by clause in access line\n",
253                                     fname, lineno );
254                                 acl_usage();
255                         }
256
257                         /*
258                          * by clause consists of <who> and <access>
259                          */
260
261                         b = (Access *) ch_calloc( 1, sizeof(Access) );
262
263                         ACL_INVALIDATE( b->a_access_mask );
264
265                         if ( ++i == argc ) {
266                                 fprintf( stderr,
267                             "%s: line %d: premature eol: expecting <who>\n",
268                                     fname, lineno );
269                                 acl_usage();
270                         }
271
272                         /* get <who> */
273                         for ( ; i < argc; i++ ) {
274                                 slap_style_t sty = ACL_STYLE_REGEX;
275
276                                 split( argv[i], '=', &left, &right );
277                                 split( left, '.', &left, &style );
278                                 if ( style == NULL || *style == '\0'
279                                         || strcasecmp( style, "regex" ) == 0 )
280                                 {
281                                         sty = ACL_STYLE_REGEX;
282                                 } else if ( strcasecmp( style, "exact" ) == 0 ) {
283                                         sty = ACL_STYLE_EXACT;
284                                 } else if ( strcasecmp( style, "base" ) == 0 ) {
285                                         sty = ACL_STYLE_BASE;
286                                 } else if ( strcasecmp( style, "one" ) == 0 ) {
287                                         sty = ACL_STYLE_ONE;
288                                 } else if ( strcasecmp( style, "subtree" ) == 0 ) {
289                                         sty = ACL_STYLE_SUBTREE;
290                                 } else if ( strcasecmp( style, "children" ) == 0 ) {
291                                         sty = ACL_STYLE_CHILDREN;
292                                 } else {
293                                         fprintf( stderr,
294                                                 "%s: line %d: unknown style \"%s\" in by clause\n",
295                                             fname, lineno, style );
296                                         acl_usage();
297                                 }
298
299                                 if ( strcasecmp( argv[i], "*" ) == 0 ) {
300                                         bv.bv_val = ch_strdup( "*" );
301                                         bv.bv_len = 1;
302
303                                 } else if ( strcasecmp( argv[i], "anonymous" ) == 0 ) {
304                                         ber_str2bv("anonymous",
305                                                 sizeof("anonymous")-1,
306                                                 1, &bv);
307
308                                 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
309                                         ber_str2bv("self",
310                                                 sizeof("self")-1,
311                                                 1, &bv);
312
313                                 } else if ( strcasecmp( argv[i], "users" ) == 0 ) {
314                                         ber_str2bv("users",
315                                                 sizeof("users")-1,
316                                                 1, &bv);
317
318                                 } else if ( strcasecmp( left, "dn" ) == 0 ) {
319                                         if ( sty == ACL_STYLE_REGEX ) {
320                                                 b->a_dn_style = ACL_STYLE_REGEX;
321                                                 if( right == NULL ) {
322                                                         /* no '=' */
323                                                         ber_str2bv("users",
324                                                                 sizeof("users")-1,
325                                                                 1, &bv);
326                                                 } else if (*right == '\0' ) {
327                                                         /* dn="" */
328                                                         ber_str2bv("anonymous",
329                                                                 sizeof("anonymous")-1,
330                                                                 1, &bv);
331                                                 } else if ( strcmp( right, "*" ) == 0 ) {
332                                                         /* dn=* */
333                                                         /* any or users?  users for now */
334                                                         ber_str2bv("users",
335                                                                 sizeof("users")-1,
336                                                                 1, &bv);
337                                                 } else if ( strcmp( right, ".+" ) == 0
338                                                         || strcmp( right, "^.+" ) == 0
339                                                         || strcmp( right, ".+$" ) == 0
340                                                         || strcmp( right, "^.+$" ) == 0
341                                                         || strcmp( right, ".+$$" ) == 0
342                                                         || strcmp( right, "^.+$$" ) == 0 )
343                                                 {
344                                                         ber_str2bv("users",
345                                                                 sizeof("users")-1,
346                                                                 1, &bv);
347                                                 } else if ( strcmp( right, ".*" ) == 0
348                                                         || strcmp( right, "^.*" ) == 0
349                                                         || strcmp( right, ".*$" ) == 0
350                                                         || strcmp( right, "^.*$" ) == 0
351                                                         || strcmp( right, ".*$$" ) == 0
352                                                         || strcmp( right, "^.*$$" ) == 0 )
353                                                 {
354                                                         ber_str2bv("*",
355                                                                 sizeof("*")-1,
356                                                                 1, &bv);
357
358                                                 } else {
359                                                         bv.bv_val = right;
360                                                         acl_regex_normalized_dn( &bv );
361                                                         regtest(fname, lineno, bv.bv_val);
362                                                 }
363                                         } else if ( right == NULL || *right == '\0' ) {
364                                                 fprintf( stderr,
365                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
366                                                     fname, lineno, left );
367                                                 acl_usage();
368
369                                         } else {
370                                                 ber_str2bv( right, 0, 1, &bv );
371                                         }
372
373                                 } else {
374                                         bv.bv_val = NULL;
375                                 }
376
377                                 if( bv.bv_val != NULL ) {
378                                         if( b->a_dn_pat.bv_len != 0 ) {
379                                                 fprintf( stderr,
380                                                     "%s: line %d: dn pattern already specified.\n",
381                                                     fname, lineno );
382                                                 acl_usage();
383                                         }
384
385                                         if ( sty != ACL_STYLE_REGEX ) {
386                                                 dnNormalize2(NULL, &bv, &b->a_dn_pat);
387                                                 free(bv.bv_val);
388                                         } else {
389                                                 b->a_dn_pat = bv;
390                                         }
391                                         b->a_dn_style = sty;
392                                         continue;
393                                 }
394
395                                 if ( strcasecmp( left, "dnattr" ) == 0 ) {
396                                         if ( right == NULL || right[ 0 ] == '\0' ) {
397                                                 fprintf( stderr,
398                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
399                                                         fname, lineno, left );
400                                                 acl_usage();
401                                         }
402
403                                         if( b->a_dn_at != NULL ) {
404                                                 fprintf( stderr,
405                                                         "%s: line %d: dnattr already specified.\n",
406                                                         fname, lineno );
407                                                 acl_usage();
408                                         }
409
410                                         rc = slap_str2ad( right, &b->a_dn_at, &text );
411
412                                         if( rc != LDAP_SUCCESS ) {
413                                                 fprintf( stderr,
414                                                         "%s: line %d: dnattr \"%s\": %s\n",
415                                                         fname, lineno, right, text );
416                                                 acl_usage();
417                                         }
418
419
420                                         if( !is_at_syntax( b->a_dn_at->ad_type,
421                                                 SLAPD_DN_SYNTAX ) &&
422                                                 !is_at_syntax( b->a_dn_at->ad_type,
423                                                 SLAPD_NAMEUID_SYNTAX ))
424                                         {
425                                                 fprintf( stderr,
426                                                         "%s: line %d: dnattr \"%s\": "
427                                                         "inappropriate syntax: %s\n",
428                                                         fname, lineno, right,
429                                                         b->a_dn_at->ad_type->sat_syntax_oid );
430                                                 acl_usage();
431                                         }
432
433                                         continue;
434                                 }
435
436                                 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
437                                         fprintf( stderr,
438                                                 "%s: line %d: inappropriate style \"%s\" in by clause\n",
439                                             fname, lineno, style );
440                                         acl_usage();
441                                 }
442
443                                 if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
444                                         char *name = NULL;
445                                         char *value = NULL;
446
447                                         if ( right == NULL || right[ 0 ] == '\0' ) {
448                                                 fprintf( stderr,
449                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
450                                                         fname, lineno, left );
451                                                 acl_usage();
452                                         }
453
454                                         if( b->a_group_pat.bv_len ) {
455                                                 fprintf( stderr,
456                                                         "%s: line %d: group pattern already specified.\n",
457                                                         fname, lineno );
458                                                 acl_usage();
459                                         }
460
461                                         /* format of string is "group/objectClassValue/groupAttrName" */
462                                         if ((value = strchr(left, '/')) != NULL) {
463                                                 *value++ = '\0';
464                                                 if (*value
465                                                         && (name = strchr(value, '/')) != NULL)
466                                                 {
467                                                         *name++ = '\0';
468                                                 }
469                                         }
470
471                                         b->a_group_style = sty;
472                                         if (sty == ACL_STYLE_REGEX) {
473                                                 bv.bv_val = right;
474                                                 acl_regex_normalized_dn( &bv );
475                                                 regtest(fname, lineno, bv.bv_val);
476                                                 b->a_group_pat = bv;
477                                         } else {
478                                                 ber_str2bv( right, 0, 0, &bv );
479                                                 dnNormalize2( NULL, &bv, &b->a_group_pat );
480                                         }
481
482                                         if (value && *value) {
483                                                 b->a_group_oc = oc_find( value );
484                                                 *--value = '/';
485
486                                                 if( b->a_group_oc == NULL ) {
487                                                         fprintf( stderr,
488                                                                 "%s: line %d: group objectclass "
489                                                                 "\"%s\" unknown\n",
490                                                                 fname, lineno, value );
491                                                         acl_usage();
492                                                 }
493                                         } else {
494                                                 b->a_group_oc = oc_find(SLAPD_GROUP_CLASS);
495
496                                                 if( b->a_group_oc == NULL ) {
497                                                         fprintf( stderr,
498                                                                 "%s: line %d: group default objectclass "
499                                                                 "\"%s\" unknown\n",
500                                                                 fname, lineno, SLAPD_GROUP_CLASS );
501                                                         acl_usage();
502                                                 }
503                                         }
504
505                                         if( is_object_subclass( b->a_group_oc,
506                                                 slap_schema.si_oc_referral ) )
507                                         {
508                                                 fprintf( stderr,
509                                                         "%s: line %d: group objectclass \"%s\" "
510                                                         "is subclass of referral\n",
511                                                         fname, lineno, value );
512                                                 acl_usage();
513                                         }
514
515                                         if( is_object_subclass( b->a_group_oc,
516                                                 slap_schema.si_oc_alias ) )
517                                         {
518                                                 fprintf( stderr,
519                                                         "%s: line %d: group objectclass \"%s\" "
520                                                         "is subclass of alias\n",
521                                                         fname, lineno, value );
522                                                 acl_usage();
523                                         }
524
525                                         if (name && *name) {
526                                                 rc = slap_str2ad( name, &b->a_group_at, &text );
527
528                                                 if( rc != LDAP_SUCCESS ) {
529                                                         fprintf( stderr,
530                                                                 "%s: line %d: group \"%s\": %s\n",
531                                                                 fname, lineno, right, text );
532                                                         acl_usage();
533                                                 }
534                                                 *--name = '/';
535                                         } else {
536                                                 rc = slap_str2ad( SLAPD_GROUP_ATTR, &b->a_group_at, &text );
537
538                                                 if( rc != LDAP_SUCCESS ) {
539                                                         fprintf( stderr,
540                                                                 "%s: line %d: group \"%s\": %s\n",
541                                                                 fname, lineno, SLAPD_GROUP_ATTR, text );
542                                                         acl_usage();
543                                                 }
544                                         }
545
546                                         if( !is_at_syntax( b->a_group_at->ad_type,
547                                                 SLAPD_DN_SYNTAX ) &&
548                                             !is_at_syntax( b->a_group_at->ad_type,
549                                                 SLAPD_NAMEUID_SYNTAX ) )
550                                         {
551                                                 fprintf( stderr,
552                                                         "%s: line %d: group \"%s\": inappropriate syntax: %s\n",
553                                                         fname, lineno, right,
554                                                         b->a_group_at->ad_type->sat_syntax_oid );
555                                                 acl_usage();
556                                         }
557
558
559                                         {
560                                                 int rc;
561                                                 struct berval vals[2];
562
563                                                 vals[0].bv_val = b->a_group_oc->soc_oid;
564                                                 vals[0].bv_len = strlen(vals[0].bv_val);
565                                                 vals[1].bv_val = NULL;
566
567
568                                                 rc = oc_check_allowed( b->a_group_at->ad_type, vals, NULL );
569
570                                                 if( rc != 0 ) {
571                                                         fprintf( stderr,
572                                                                 "%s: line %d: group: \"%s\" not allowed by \"%s\"\n",
573                                                                 fname, lineno,
574                                                                 b->a_group_at->ad_cname.bv_val,
575                                                                 b->a_group_oc->soc_oid );
576                                                         acl_usage();
577                                                 }
578                                         }
579                                         continue;
580                                 }
581
582                                 if ( strcasecmp( left, "peername" ) == 0 ) {
583                                         if ( right == NULL || right[ 0 ] == '\0' ) {
584                                                 fprintf( stderr,
585                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
586                                                         fname, lineno, left );
587                                                 acl_usage();
588                                         }
589
590                                         if( b->a_peername_pat.bv_len ) {
591                                                 fprintf( stderr,
592                                                         "%s: line %d: peername pattern already specified.\n",
593                                                         fname, lineno );
594                                                 acl_usage();
595                                         }
596
597                                         b->a_peername_style = sty;
598                                         if (sty == ACL_STYLE_REGEX) {
599                                                 bv.bv_val = right;
600                                                 acl_regex_normalized_dn( &bv );
601                                                 regtest(fname, lineno, bv.bv_val);
602                                                 b->a_peername_pat = bv;
603                                         } else {
604                                                 ber_str2bv( right, 0, 1, &b->a_peername_pat );
605                                         }
606                                         continue;
607                                 }
608
609                                 if ( strcasecmp( left, "sockname" ) == 0 ) {
610                                         if ( right == NULL || right[ 0 ] == '\0' ) {
611                                                 fprintf( stderr,
612                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
613                                                         fname, lineno, left );
614                                                 acl_usage();
615                                         }
616
617                                         if( b->a_sockname_pat.bv_len ) {
618                                                 fprintf( stderr,
619                                                         "%s: line %d: sockname pattern already specified.\n",
620                                                         fname, lineno );
621                                                 acl_usage();
622                                         }
623
624                                         b->a_sockname_style = sty;
625                                         if (sty == ACL_STYLE_REGEX) {
626                                                 bv.bv_val = right;
627                                                 acl_regex_normalized_dn( &bv );
628                                                 regtest(fname, lineno, bv.bv_val);
629                                                 b->a_sockname_pat = bv;
630                                         } else {
631                                                 ber_str2bv( right, 0, 1, &b->a_sockname_pat );
632                                         }
633                                         continue;
634                                 }
635
636                                 if ( strcasecmp( left, "domain" ) == 0 ) {
637                                         if ( right == NULL || right[ 0 ] == '\0' ) {
638                                                 fprintf( stderr,
639                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
640                                                         fname, lineno, left );
641                                                 acl_usage();
642                                         }
643
644                                         if( b->a_domain_pat.bv_len ) {
645                                                 fprintf( stderr,
646                                                         "%s: line %d: domain pattern already specified.\n",
647                                                         fname, lineno );
648                                                 acl_usage();
649                                         }
650
651                                         b->a_domain_style = sty;
652                                         if (sty == ACL_STYLE_REGEX) {
653                                                 bv.bv_val = right;
654                                                 acl_regex_normalized_dn( &bv );
655                                                 regtest(fname, lineno, bv.bv_val);
656                                                 b->a_domain_pat = bv;
657                                         } else {
658                                                 ber_str2bv( right, 0, 1, &b->a_domain_pat );
659                                         }
660                                         continue;
661                                 }
662
663                                 if ( strcasecmp( left, "sockurl" ) == 0 ) {
664                                         if ( right == NULL || right[ 0 ] == '\0' ) {
665                                                 fprintf( stderr,
666                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
667                                                         fname, lineno, left );
668                                                 acl_usage();
669                                         }
670
671                                         if( b->a_sockurl_pat.bv_len ) {
672                                                 fprintf( stderr,
673                                                         "%s: line %d: sockurl pattern already specified.\n",
674                                                         fname, lineno );
675                                                 acl_usage();
676                                         }
677
678                                         b->a_sockurl_style = sty;
679                                         if (sty == ACL_STYLE_REGEX) {
680                                                 bv.bv_val = right;
681                                                 acl_regex_normalized_dn( &bv );
682                                                 regtest(fname, lineno, bv.bv_val);
683                                                 b->a_sockurl_pat = bv;
684                                         } else {
685                                                 ber_str2bv( right, 0, 1, &b->a_sockurl_pat );
686                                         }
687                                         continue;
688                                 }
689
690                                 if ( strcasecmp( left, "set" ) == 0 ) {
691                                         if( b->a_set_pat.bv_len != 0 ) {
692                                                 fprintf( stderr,
693                                                         "%s: line %d: set attribute already specified.\n",
694                                                         fname, lineno );
695                                                 acl_usage();
696                                         }
697
698                                         if ( right == NULL || *right == '\0' ) {
699                                                 fprintf( stderr,
700                                                         "%s: line %d: no set is defined\n",
701                                                         fname, lineno );
702                                                 acl_usage();
703                                         }
704
705                                         b->a_set_style = sty;
706                                         ber_str2bv( right, 0, 1, &b->a_set_pat );
707
708                                         continue;
709                                 }
710
711 #ifdef SLAPD_ACI_ENABLED
712                                 if ( strcasecmp( left, "aci" ) == 0 ) {
713                                         if( b->a_aci_at != NULL ) {
714                                                 fprintf( stderr,
715                                                         "%s: line %d: aci attribute already specified.\n",
716                                                         fname, lineno );
717                                                 acl_usage();
718                                         }
719
720                                         if ( right != NULL && *right != '\0' ) {
721                                                 rc = slap_str2ad( right, &b->a_aci_at, &text );
722
723                                                 if( rc != LDAP_SUCCESS ) {
724                                                         fprintf( stderr,
725                                                                 "%s: line %d: aci \"%s\": %s\n",
726                                                                 fname, lineno, right, text );
727                                                         acl_usage();
728                                                 }
729
730                                         } else {
731                                                 b->a_aci_at = slap_schema.si_ad_aci;
732                                         }
733
734                                         if( !is_at_syntax( b->a_aci_at->ad_type,
735                                                 SLAPD_ACI_SYNTAX) )
736                                         {
737                                                 fprintf( stderr,
738                                                         "%s: line %d: aci \"%s\": inappropriate syntax: %s\n",
739                                                         fname, lineno, right,
740                                                         b->a_aci_at->ad_type->sat_syntax_oid );
741                                                 acl_usage();
742                                         }
743
744                                         continue;
745                                 }
746 #endif /* SLAPD_ACI_ENABLED */
747
748                                 if ( strcasecmp( left, "ssf" ) == 0 ) {
749                                         if( b->a_authz.sai_ssf ) {
750                                                 fprintf( stderr,
751                                                         "%s: line %d: ssf attribute already specified.\n",
752                                                         fname, lineno );
753                                                 acl_usage();
754                                         }
755
756                                         if ( right == NULL || *right == '\0' ) {
757                                                 fprintf( stderr,
758                                                         "%s: line %d: no ssf is defined\n",
759                                                         fname, lineno );
760                                                 acl_usage();
761                                         }
762
763                                         b->a_authz.sai_ssf = atoi( right );
764
765                                         if( !b->a_authz.sai_ssf ) {
766                                                 fprintf( stderr,
767                                                         "%s: line %d: invalid ssf value (%s)\n",
768                                                         fname, lineno, right );
769                                                 acl_usage();
770                                         }
771                                         continue;
772                                 }
773
774                                 if ( strcasecmp( left, "transport_ssf" ) == 0 ) {
775                                         if( b->a_authz.sai_transport_ssf ) {
776                                                 fprintf( stderr,
777                                                         "%s: line %d: transport_ssf attribute already specified.\n",
778                                                         fname, lineno );
779                                                 acl_usage();
780                                         }
781
782                                         if ( right == NULL || *right == '\0' ) {
783                                                 fprintf( stderr,
784                                                         "%s: line %d: no transport_ssf is defined\n",
785                                                         fname, lineno );
786                                                 acl_usage();
787                                         }
788
789                                         b->a_authz.sai_transport_ssf = atoi( right );
790
791                                         if( !b->a_authz.sai_transport_ssf ) {
792                                                 fprintf( stderr,
793                                                         "%s: line %d: invalid transport_ssf value (%s)\n",
794                                                         fname, lineno, right );
795                                                 acl_usage();
796                                         }
797                                         continue;
798                                 }
799
800                                 if ( strcasecmp( left, "tls_ssf" ) == 0 ) {
801                                         if( b->a_authz.sai_tls_ssf ) {
802                                                 fprintf( stderr,
803                                                         "%s: line %d: tls_ssf attribute already specified.\n",
804                                                         fname, lineno );
805                                                 acl_usage();
806                                         }
807
808                                         if ( right == NULL || *right == '\0' ) {
809                                                 fprintf( stderr,
810                                                         "%s: line %d: no tls_ssf is defined\n",
811                                                         fname, lineno );
812                                                 acl_usage();
813                                         }
814
815                                         b->a_authz.sai_tls_ssf = atoi( right );
816
817                                         if( !b->a_authz.sai_tls_ssf ) {
818                                                 fprintf( stderr,
819                                                         "%s: line %d: invalid tls_ssf value (%s)\n",
820                                                         fname, lineno, right );
821                                                 acl_usage();
822                                         }
823                                         continue;
824                                 }
825
826                                 if ( strcasecmp( left, "sasl_ssf" ) == 0 ) {
827                                         if( b->a_authz.sai_sasl_ssf ) {
828                                                 fprintf( stderr,
829                                                         "%s: line %d: sasl_ssf attribute already specified.\n",
830                                                         fname, lineno );
831                                                 acl_usage();
832                                         }
833
834                                         if ( right == NULL || *right == '\0' ) {
835                                                 fprintf( stderr,
836                                                         "%s: line %d: no sasl_ssf is defined\n",
837                                                         fname, lineno );
838                                                 acl_usage();
839                                         }
840
841                                         b->a_authz.sai_sasl_ssf = atoi( right );
842
843                                         if( !b->a_authz.sai_sasl_ssf ) {
844                                                 fprintf( stderr,
845                                                         "%s: line %d: invalid sasl_ssf value (%s)\n",
846                                                         fname, lineno, right );
847                                                 acl_usage();
848                                         }
849                                         continue;
850                                 }
851
852                                 if( right != NULL ) {
853                                         /* unsplit */
854                                         right[-1] = '=';
855                                 }
856                                 break;
857                         }
858
859                         if( i == argc || ( strcasecmp( left, "stop" ) == 0 )) { 
860                                 /* out of arguments or plain stop */
861
862                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
863                                 b->a_type = ACL_STOP;
864
865                                 access_append( &a->acl_access, b );
866                                 continue;
867                         }
868
869                         if( strcasecmp( left, "continue" ) == 0 ) {
870                                 /* plain continue */
871
872                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
873                                 b->a_type = ACL_CONTINUE;
874
875                                 access_append( &a->acl_access, b );
876                                 continue;
877                         }
878
879                         if( strcasecmp( left, "break" ) == 0 ) {
880                                 /* plain continue */
881
882                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
883                                 b->a_type = ACL_BREAK;
884
885                                 access_append( &a->acl_access, b );
886                                 continue;
887                         }
888
889                         if ( strcasecmp( left, "by" ) == 0 ) {
890                                 /* we've gone too far */
891                                 --i;
892                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
893                                 b->a_type = ACL_STOP;
894
895                                 access_append( &a->acl_access, b );
896                                 continue;
897                         }
898
899                         /* get <access> */
900                         if( strncasecmp( left, "self", 4 ) == 0 ) {
901                                 b->a_dn_self = 1;
902                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( &left[4] ) );
903
904                         } else {
905                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( left ) );
906                         }
907
908                         if( ACL_IS_INVALID( b->a_access_mask ) ) {
909                                 fprintf( stderr,
910                                         "%s: line %d: expecting <access> got \"%s\"\n",
911                                         fname, lineno, left );
912                                 acl_usage();
913                         }
914
915                         b->a_type = ACL_STOP;
916
917                         if( ++i == argc ) {
918                                 /* out of arguments or plain stop */
919                                 access_append( &a->acl_access, b );
920                                 continue;
921                         }
922
923                         if( strcasecmp( argv[i], "continue" ) == 0 ) {
924                                 /* plain continue */
925                                 b->a_type = ACL_CONTINUE;
926
927                         } else if( strcasecmp( argv[i], "break" ) == 0 ) {
928                                 /* plain continue */
929                                 b->a_type = ACL_BREAK;
930
931                         } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
932                                 /* gone to far */
933                                 i--;
934                         }
935
936                         access_append( &a->acl_access, b );
937
938                 } else {
939                         fprintf( stderr,
940                     "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
941                             fname, lineno, argv[i] );
942                         acl_usage();
943                 }
944         }
945
946         /* if we have no real access clause, complain and do nothing */
947         if ( a == NULL ) {
948                         fprintf( stderr,
949                                 "%s: line %d: warning: no access clause(s) specified in access line\n",
950                             fname, lineno );
951
952         } else {
953 #ifdef LDAP_DEBUG
954                 if (ldap_debug & LDAP_DEBUG_ACL)
955                         print_acl(be, a);
956 #endif
957         
958                 if ( a->acl_access == NULL ) {
959                         fprintf( stderr,
960                         "%s: line %d: warning: no by clause(s) specified in access line\n",
961                             fname, lineno );
962                 }
963
964                 if ( be != NULL ) {
965                         acl_append( &be->be_acl, a );
966                 } else {
967                         acl_append( &global_acl, a );
968                 }
969         }
970 }
971
972 char *
973 accessmask2str( slap_mask_t mask, char *buf )
974 {
975         int none=1;
976         char *ptr = buf;
977
978         assert( buf != NULL );
979
980         if ( ACL_IS_INVALID( mask ) ) {
981                 return "invalid";
982         }
983
984         buf[0] = '\0';
985
986         if ( ACL_IS_LEVEL( mask ) ) {
987                 if ( ACL_LVL_IS_NONE(mask) ) {
988                         ptr = slap_strcopy( ptr, "none" );
989
990                 } else if ( ACL_LVL_IS_AUTH(mask) ) {
991                         ptr = slap_strcopy( ptr, "auth" );
992
993                 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
994                         ptr = slap_strcopy( ptr, "compare" );
995
996                 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
997                         ptr = slap_strcopy( ptr, "search" );
998
999                 } else if ( ACL_LVL_IS_READ(mask) ) {
1000                         ptr = slap_strcopy( ptr, "read" );
1001
1002                 } else if ( ACL_LVL_IS_WRITE(mask) ) {
1003                         ptr = slap_strcopy( ptr, "write" );
1004                 } else {
1005                         ptr = slap_strcopy( ptr, "unknown" );
1006                 }
1007                 
1008                 *ptr++ = '(';
1009         }
1010
1011         if( ACL_IS_ADDITIVE( mask ) ) {
1012                 *ptr++ = '+';
1013
1014         } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
1015                 *ptr++ = '-';
1016
1017         } else {
1018                 *ptr++ = '=';
1019         }
1020
1021         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
1022                 none = 0;
1023                 *ptr++ = 'w';
1024         } 
1025
1026         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
1027                 none = 0;
1028                 *ptr++ = 'r';
1029         } 
1030
1031         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
1032                 none = 0;
1033                 *ptr++ = 's';
1034         } 
1035
1036         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
1037                 none = 0;
1038                 *ptr++ = 'c';
1039         } 
1040
1041         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
1042                 none = 0;
1043                 *ptr++ = 'x';
1044         } 
1045
1046         if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
1047                 none = 0;
1048                 *ptr++ = 'n';
1049         } 
1050
1051         if ( none ) {
1052                 *ptr++ = '0';
1053         }
1054
1055         if ( ACL_IS_LEVEL( mask ) ) {
1056                 *ptr++ = ')';
1057         }
1058
1059         *ptr = '\0';
1060
1061         return buf;
1062 }
1063
1064 slap_mask_t
1065 str2accessmask( const char *str )
1066 {
1067         slap_mask_t     mask;
1068
1069         if( !ASCII_ALPHA(str[0]) ) {
1070                 int i;
1071
1072                 if ( str[0] == '=' ) {
1073                         ACL_INIT(mask);
1074
1075                 } else if( str[0] == '+' ) {
1076                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
1077
1078                 } else if( str[0] == '-' ) {
1079                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
1080
1081                 } else {
1082                         ACL_INVALIDATE(mask);
1083                         return mask;
1084                 }
1085
1086                 for( i=1; str[i] != '\0'; i++ ) {
1087                         if( TOLOWER(str[i]) == 'w' ) {
1088                                 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1089
1090                         } else if( TOLOWER(str[i]) == 'r' ) {
1091                                 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1092
1093                         } else if( TOLOWER(str[i]) == 's' ) {
1094                                 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1095
1096                         } else if( TOLOWER(str[i]) == 'c' ) {
1097                                 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1098
1099                         } else if( TOLOWER(str[i]) == 'x' ) {
1100                                 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1101
1102                         } else if( str[i] != '0' ) {
1103                                 ACL_INVALIDATE(mask);
1104                                 return mask;
1105                         }
1106                 }
1107
1108                 return mask;
1109         }
1110
1111         if ( strcasecmp( str, "none" ) == 0 ) {
1112                 ACL_LVL_ASSIGN_NONE(mask);
1113
1114         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1115                 ACL_LVL_ASSIGN_AUTH(mask);
1116
1117         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1118                 ACL_LVL_ASSIGN_COMPARE(mask);
1119
1120         } else if ( strcasecmp( str, "search" ) == 0 ) {
1121                 ACL_LVL_ASSIGN_SEARCH(mask);
1122
1123         } else if ( strcasecmp( str, "read" ) == 0 ) {
1124                 ACL_LVL_ASSIGN_READ(mask);
1125
1126         } else if ( strcasecmp( str, "write" ) == 0 ) {
1127                 ACL_LVL_ASSIGN_WRITE(mask);
1128
1129         } else {
1130                 ACL_INVALIDATE( mask );
1131         }
1132
1133         return mask;
1134 }
1135
1136 static void
1137 acl_usage( void )
1138 {
1139         fprintf( stderr, "\n"
1140                 "<access clause> ::= access to <what> "
1141                                 "[ by <who> <access> [ <control> ] ]+ \n"
1142                 "<what> ::= * | [dn[.<dnstyle>]=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n"
1143                 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
1144                 "<attr> ::= <attrname> | entry | children\n"
1145                 "<who> ::= [ * | anonymous | users | self | dn[.<dnstyle>]=<regex> ]\n"
1146                         "\t[dnattr=<attrname>]\n"
1147                         "\t[group[/<objectclass>[/<attrname>]][.<style>]=<regex>]\n"
1148                         "\t[peername[.<style>]=<regex>] [sockname[.<style>]=<regex>]\n"
1149                         "\t[domain[.<style>]=<regex>] [sockurl[.<style>]=<regex>]\n"
1150 #ifdef SLAPD_ACI_ENABLED
1151                         "\t[aci=<attrname>]\n"
1152 #endif
1153                         "\t[ssf=<n>] [transport_ssf=<n>] [tls_ssf=<n>] [sasl_ssf=<n>]\n"
1154                 "<dnstyle> ::= regex | base | exact (alias of base) | one | sub | children\n"
1155                 "<style> ::= regex | base | exact (alias of base)\n"
1156                 "<groupflags> ::= R\n"
1157                 "<access> ::= [self]{<level>|<priv>}\n"
1158                 "<level> ::= none | auth | compare | search | read | write\n"
1159                 "<priv> ::= {=|+|-}{w|r|s|c|x}+\n"
1160                 "<control> ::= [ stop | continue | break ]\n"
1161                 );
1162         exit( EXIT_FAILURE );
1163 }
1164
1165 /*
1166  * At present it simply eats the (optional) space after 
1167  * a RDN separator (,)
1168  * Eventually will evolve in a more complete normalization
1169  *
1170  * Note that the input berval only needs bv_val, it ignores
1171  * the input bv_len and sets it on return.
1172  */
1173 static void
1174 acl_regex_normalized_dn(
1175         struct berval *pattern
1176 )
1177 {
1178         char *str, *p;
1179
1180         str = ch_strdup( pattern->bv_val );
1181
1182         for ( p = str; p && p[ 0 ]; p++ ) {
1183                 /* escape */
1184                 if ( p[ 0 ] == '\\' ) {
1185                         /* 
1186                          * if escaping a hex pair we should
1187                          * increment p twice; however, in that 
1188                          * case the second hex number does 
1189                          * no harm
1190                          */
1191                         p++;
1192                 }
1193
1194                 if ( p[ 0 ] == ',' ) {
1195                         if ( p[ 1 ] == ' ' ) {
1196                                 char *q;
1197                         
1198                                 /*
1199                                  * too much space should be 
1200                                  * an error if we are pedantic
1201                                  */
1202                                 for ( q = &p[ 2 ]; q[ 0 ] == ' '; q++ ) {
1203                                         /* DO NOTHING */ ;
1204                                 }
1205                                 AC_MEMCPY( p+1, q, pattern->bv_len-(q-str)+1);
1206                         }
1207                 }
1208         }
1209         pattern->bv_val = str;
1210         pattern->bv_len = p-str;
1211
1212         return;
1213 }
1214
1215 static void
1216 split(
1217     char        *line,
1218     int         splitchar,
1219     char        **left,
1220     char        **right
1221 )
1222 {
1223         *left = line;
1224         if ( (*right = strchr( line, splitchar )) != NULL ) {
1225                 *((*right)++) = '\0';
1226         }
1227 }
1228
1229 static void
1230 access_append( Access **l, Access *a )
1231 {
1232         for ( ; *l != NULL; l = &(*l)->a_next )
1233                 ;       /* NULL */
1234
1235         *l = a;
1236 }
1237
1238 void
1239 acl_append( AccessControl **l, AccessControl *a )
1240 {
1241         for ( ; *l != NULL; l = &(*l)->acl_next )
1242                 ;       /* NULL */
1243
1244         *l = a;
1245 }
1246
1247 static void
1248 access_free( Access *a )
1249 {
1250         if ( a->a_dn_pat.bv_val )
1251                 free ( a->a_dn_pat.bv_val );
1252         if ( a->a_peername_pat.bv_val )
1253                 free ( a->a_peername_pat.bv_val );
1254         if ( a->a_sockname_pat.bv_val )
1255                 free ( a->a_sockname_pat.bv_val );
1256         if ( a->a_domain_pat.bv_val )
1257                 free ( a->a_domain_pat.bv_val );
1258         if ( a->a_sockurl_pat.bv_val )
1259                 free ( a->a_sockurl_pat.bv_val );
1260         if ( a->a_set_pat.bv_len )
1261                 free ( a->a_set_pat.bv_val );
1262         if ( a->a_group_pat.bv_len )
1263                 free ( a->a_group_pat.bv_val );
1264         free( a );
1265 }
1266
1267 void
1268 acl_free( AccessControl *a )
1269 {
1270         Access *n;
1271         AttributeName *an;
1272
1273         if ( a->acl_filter )
1274                 filter_free( a->acl_filter );
1275         if ( a->acl_dn_pat.bv_len )
1276                 free ( a->acl_dn_pat.bv_val );
1277         if ( a->acl_attrs ) {
1278                 for ( an = a->acl_attrs; an->an_name.bv_val; an++ ) {
1279                         free( an->an_name.bv_val );
1280                 }
1281                 free( a->acl_attrs );
1282         }
1283         for (; a->acl_access; a->acl_access = n) {
1284                 n = a->acl_access->a_next;
1285                 access_free( a->acl_access );
1286         }
1287         free( a );
1288 }
1289
1290 /* Because backend_startup uses acl_append to tack on the global_acl to
1291  * the end of each backend's acl, we cannot just take one argument and
1292  * merrily free our way to the end of the list. backend_destroy calls us
1293  * with the be_acl in arg1, and global_acl in arg2 to give us a stopping
1294  * point. config_destroy calls us with global_acl in arg1 and NULL in
1295  * arg2, so we then proceed to polish off the global_acl.
1296  */
1297 void
1298 acl_destroy( AccessControl *a, AccessControl *end )
1299 {
1300         AccessControl *n;
1301
1302         for (; a && a!= end; a=n) {
1303                 n = a->acl_next;
1304                 acl_free( a );
1305         }
1306 }
1307
1308 char *
1309 access2str( slap_access_t access )
1310 {
1311         if ( access == ACL_NONE ) {
1312                 return "none";
1313
1314         } else if ( access == ACL_AUTH ) {
1315                 return "auth";
1316
1317         } else if ( access == ACL_COMPARE ) {
1318                 return "compare";
1319
1320         } else if ( access == ACL_SEARCH ) {
1321                 return "search";
1322
1323         } else if ( access == ACL_READ ) {
1324                 return "read";
1325
1326         } else if ( access == ACL_WRITE ) {
1327                 return "write";
1328         }
1329
1330         return "unknown";
1331 }
1332
1333 slap_access_t
1334 str2access( const char *str )
1335 {
1336         if ( strcasecmp( str, "none" ) == 0 ) {
1337                 return ACL_NONE;
1338
1339         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1340                 return ACL_AUTH;
1341
1342         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1343                 return ACL_COMPARE;
1344
1345         } else if ( strcasecmp( str, "search" ) == 0 ) {
1346                 return ACL_SEARCH;
1347
1348         } else if ( strcasecmp( str, "read" ) == 0 ) {
1349                 return ACL_READ;
1350
1351         } else if ( strcasecmp( str, "write" ) == 0 ) {
1352                 return ACL_WRITE;
1353         }
1354
1355         return( ACL_INVALID_ACCESS );
1356 }
1357
1358 #ifdef LDAP_DEBUG
1359
1360 static char *style_strings[5] = {
1361                         "regex",
1362                         "base",
1363                         "one",
1364                         "subtree",
1365                         "children"
1366                 };
1367
1368
1369 static void
1370 print_access( Access *b )
1371 {
1372         char maskbuf[ACCESSMASK_MAXLEN];
1373
1374         fprintf( stderr, "\tby" );
1375
1376         if ( b->a_dn_pat.bv_len != 0 ) {
1377                 if( strcmp(b->a_dn_pat.bv_val, "*") == 0
1378                         || strcmp(b->a_dn_pat.bv_val, "users") == 0 
1379                         || strcmp(b->a_dn_pat.bv_val, "anonymous") == 0 
1380                         || strcmp(b->a_dn_pat.bv_val, "self") == 0 )
1381                 {
1382                         fprintf( stderr, " %s", b->a_dn_pat.bv_val );
1383
1384                 } else {
1385                         fprintf( stderr, " dn.%s=%s", style_strings[b->a_dn_style], b->a_dn_pat.bv_val );
1386                 }
1387         }
1388
1389         if ( b->a_dn_at != NULL ) {
1390                 fprintf( stderr, " dnattr=%s", b->a_dn_at->ad_cname.bv_val );
1391         }
1392
1393         if ( b->a_group_pat.bv_len ) {
1394                 fprintf( stderr, " group=%s", b->a_group_pat.bv_val );
1395
1396                 if ( b->a_group_oc ) {
1397                         fprintf( stderr, " objectClass: %s",
1398                                 b->a_group_oc->soc_oclass.oc_oid );
1399
1400                         if ( b->a_group_at ) {
1401                                 fprintf( stderr, " attributeType: %s", b->a_group_at->ad_cname.bv_val );
1402                         }
1403                 }
1404     }
1405
1406         if ( b->a_peername_pat.bv_len != 0 ) {
1407                 fprintf( stderr, " peername=%s", b->a_peername_pat.bv_val );
1408         }
1409
1410         if ( b->a_sockname_pat.bv_len != 0 ) {
1411                 fprintf( stderr, " sockname=%s", b->a_sockname_pat.bv_val );
1412         }
1413
1414         if ( b->a_domain_pat.bv_len != 0 ) {
1415                 fprintf( stderr, " domain=%s", b->a_domain_pat.bv_val );
1416         }
1417
1418         if ( b->a_sockurl_pat.bv_len != 0 ) {
1419                 fprintf( stderr, " sockurl=%s", b->a_sockurl_pat.bv_val );
1420         }
1421
1422 #ifdef SLAPD_ACI_ENABLED
1423         if ( b->a_aci_at != NULL ) {
1424                 fprintf( stderr, " aci=%s", b->a_aci_at->ad_cname.bv_val );
1425         }
1426 #endif
1427
1428         /* Security Strength Factors */
1429         if ( b->a_authz.sai_ssf ) {
1430                 fprintf( stderr, " ssf=%u",
1431                         b->a_authz.sai_ssf );
1432         }
1433         if ( b->a_authz.sai_transport_ssf ) {
1434                 fprintf( stderr, " transport_ssf=%u",
1435                         b->a_authz.sai_transport_ssf );
1436         }
1437         if ( b->a_authz.sai_tls_ssf ) {
1438                 fprintf( stderr, " tls_ssf=%u",
1439                         b->a_authz.sai_tls_ssf );
1440         }
1441         if ( b->a_authz.sai_sasl_ssf ) {
1442                 fprintf( stderr, " sasl_ssf=%u",
1443                         b->a_authz.sai_sasl_ssf );
1444         }
1445
1446         fprintf( stderr, " %s%s",
1447                 b->a_dn_self ? "self" : "",
1448                 accessmask2str( b->a_access_mask, maskbuf ) );
1449
1450         if( b->a_type == ACL_BREAK ) {
1451                 fprintf( stderr, " break" );
1452
1453         } else if( b->a_type == ACL_CONTINUE ) {
1454                 fprintf( stderr, " continue" );
1455
1456         } else if( b->a_type != ACL_STOP ) {
1457                 fprintf( stderr, " unknown-control" );
1458         }
1459
1460         fprintf( stderr, "\n" );
1461 }
1462
1463
1464 static void
1465 print_acl( Backend *be, AccessControl *a )
1466 {
1467         int             to = 0;
1468         Access  *b;
1469
1470         fprintf( stderr, "%s ACL: access to",
1471                 be == NULL ? "Global" : "Backend" );
1472
1473         if ( a->acl_dn_pat.bv_len != 0 ) {
1474                 to++;
1475                 fprintf( stderr, " dn.%s=%s\n",
1476                         style_strings[a->acl_dn_style], a->acl_dn_pat.bv_val );
1477         }
1478
1479         if ( a->acl_filter != NULL ) {
1480                 to++;
1481                 fprintf( stderr, " filter=" );
1482                 filter_print( a->acl_filter );
1483                 fprintf( stderr, "\n" );
1484         }
1485
1486         if ( a->acl_attrs != NULL ) {
1487                 int     first = 1;
1488                 AttributeName *an;
1489                 to++;
1490
1491                 fprintf( stderr, " attrs=" );
1492                 for ( an = a->acl_attrs; an && an->an_name.bv_val; an++ ) {
1493                         if ( ! first ) {
1494                                 fprintf( stderr, "," );
1495                         }
1496                         fputs( an->an_name.bv_val, stderr );
1497                         first = 0;
1498                 }
1499                 fprintf(  stderr, "\n" );
1500         }
1501
1502         if( !to ) {
1503                 fprintf( stderr, " *\n" );
1504         }
1505
1506         for ( b = a->acl_access; b != NULL; b = b->a_next ) {
1507                 print_access( b );
1508         }
1509
1510         fprintf( stderr, "\n" );
1511 }
1512
1513 #endif /* LDAP_DEBUG */