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