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