]> git.sur5r.net Git - openldap/blob - servers/slapd/aclparse.c
Fix dnValidate
[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 = str2anlist( 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
1062         *ptr = '\0';
1063
1064         return buf;
1065 }
1066
1067 slap_mask_t
1068 str2accessmask( const char *str )
1069 {
1070         slap_mask_t     mask;
1071
1072         if( !ASCII_ALPHA(str[0]) ) {
1073                 int i;
1074
1075                 if ( str[0] == '=' ) {
1076                         ACL_INIT(mask);
1077
1078                 } else if( str[0] == '+' ) {
1079                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
1080
1081                 } else if( str[0] == '-' ) {
1082                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
1083
1084                 } else {
1085                         ACL_INVALIDATE(mask);
1086                         return mask;
1087                 }
1088
1089                 for( i=1; str[i] != '\0'; i++ ) {
1090                         if( TOLOWER(str[i]) == 'w' ) {
1091                                 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1092
1093                         } else if( TOLOWER(str[i]) == 'r' ) {
1094                                 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1095
1096                         } else if( TOLOWER(str[i]) == 's' ) {
1097                                 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1098
1099                         } else if( TOLOWER(str[i]) == 'c' ) {
1100                                 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1101
1102                         } else if( TOLOWER(str[i]) == 'x' ) {
1103                                 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1104
1105                         } else if( str[i] != '0' ) {
1106                                 ACL_INVALIDATE(mask);
1107                                 return mask;
1108                         }
1109                 }
1110
1111                 return mask;
1112         }
1113
1114         if ( strcasecmp( str, "none" ) == 0 ) {
1115                 ACL_LVL_ASSIGN_NONE(mask);
1116
1117         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1118                 ACL_LVL_ASSIGN_AUTH(mask);
1119
1120         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1121                 ACL_LVL_ASSIGN_COMPARE(mask);
1122
1123         } else if ( strcasecmp( str, "search" ) == 0 ) {
1124                 ACL_LVL_ASSIGN_SEARCH(mask);
1125
1126         } else if ( strcasecmp( str, "read" ) == 0 ) {
1127                 ACL_LVL_ASSIGN_READ(mask);
1128
1129         } else if ( strcasecmp( str, "write" ) == 0 ) {
1130                 ACL_LVL_ASSIGN_WRITE(mask);
1131
1132         } else {
1133                 ACL_INVALIDATE( mask );
1134         }
1135
1136         return mask;
1137 }
1138
1139 static void
1140 acl_usage( void )
1141 {
1142         fprintf( stderr, "\n"
1143                 "<access clause> ::= access to <what> "
1144                                 "[ by <who> <access> [ <control> ] ]+ \n"
1145                 "<what> ::= * | [dn[.<dnstyle>]=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n"
1146                 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
1147                 "<attr> ::= <attrname> | entry | children\n"
1148                 "<who> ::= [ * | anonymous | users | self | dn[.<dnstyle>]=<regex> ]\n"
1149                         "\t[dnattr=<attrname>]\n"
1150                         "\t[group[/<objectclass>[/<attrname>]][.<style>]=<regex>]\n"
1151                         "\t[peername[.<style>]=<regex>] [sockname[.<style>]=<regex>]\n"
1152                         "\t[domain[.<style>]=<regex>] [sockurl[.<style>]=<regex>]\n"
1153 #ifdef SLAPD_ACI_ENABLED
1154                         "\t[aci=<attrname>]\n"
1155 #endif
1156                         "\t[ssf=<n>] [transport_ssf=<n>] [tls_ssf=<n>] [sasl_ssf=<n>]\n"
1157                 "<dnstyle> ::= regex | base | exact (alias of base) | one | sub | children\n"
1158                 "<style> ::= regex | base | exact (alias of base)\n"
1159                 "<groupflags> ::= R\n"
1160                 "<access> ::= [self]{<level>|<priv>}\n"
1161                 "<level> ::= none | auth | compare | search | read | write\n"
1162                 "<priv> ::= {=|+|-}{w|r|s|c|x}+\n"
1163                 "<control> ::= [ stop | continue | break ]\n"
1164                 );
1165         exit( EXIT_FAILURE );
1166 }
1167
1168 /*
1169  * At present it simply eats the (optional) space after 
1170  * a RDN separator (,)
1171  * Eventually will evolve in a more complete normalization
1172  *
1173  * Note that the input berval only needs bv_val, it ignores
1174  * the input bv_len and sets it on return.
1175  */
1176 static void
1177 acl_regex_normalized_dn(
1178         struct berval *pattern
1179 )
1180 {
1181         char *str, *p;
1182
1183         str = ch_strdup( pattern->bv_val );
1184
1185         for ( p = str; p && p[ 0 ]; p++ ) {
1186                 /* escape */
1187                 if ( p[ 0 ] == '\\' ) {
1188                         /* 
1189                          * if escaping a hex pair we should
1190                          * increment p twice; however, in that 
1191                          * case the second hex number does 
1192                          * no harm
1193                          */
1194                         p++;
1195                 }
1196
1197                 if ( p[ 0 ] == ',' ) {
1198                         if ( p[ 1 ] == ' ' ) {
1199                                 char *q;
1200                         
1201                                 /*
1202                                  * too much space should be 
1203                                  * an error if we are pedantic
1204                                  */
1205                                 for ( q = &p[ 2 ]; q[ 0 ] == ' '; q++ ) {
1206                                         /* DO NOTHING */ ;
1207                                 }
1208                                 AC_MEMCPY( p+1, q, pattern->bv_len-(q-str)+1);
1209                         }
1210                 }
1211         }
1212         pattern->bv_val = str;
1213         pattern->bv_len = p-str;
1214
1215         return;
1216 }
1217
1218 static void
1219 split(
1220     char        *line,
1221     int         splitchar,
1222     char        **left,
1223     char        **right
1224 )
1225 {
1226         *left = line;
1227         if ( (*right = strchr( line, splitchar )) != NULL ) {
1228                 *((*right)++) = '\0';
1229         }
1230 }
1231
1232 static void
1233 access_append( Access **l, Access *a )
1234 {
1235         for ( ; *l != NULL; l = &(*l)->a_next )
1236                 ;       /* NULL */
1237
1238         *l = a;
1239 }
1240
1241 void
1242 acl_append( AccessControl **l, AccessControl *a )
1243 {
1244         for ( ; *l != NULL; l = &(*l)->acl_next )
1245                 ;       /* NULL */
1246
1247         *l = a;
1248 }
1249
1250 static void
1251 access_free( Access *a )
1252 {
1253         if ( a->a_dn_pat.bv_val )
1254                 free ( a->a_dn_pat.bv_val );
1255         if ( a->a_peername_pat )
1256                 free ( a->a_peername_pat );
1257         if ( a->a_sockname_pat )
1258                 free ( a->a_sockname_pat );
1259         if ( a->a_domain_pat )
1260                 free ( a->a_domain_pat );
1261         if ( a->a_sockurl_pat )
1262                 free ( a->a_sockurl_pat );
1263         if ( a->a_set_pat.bv_len )
1264                 free ( a->a_set_pat.bv_val );
1265         if ( a->a_group_pat.bv_len )
1266                 free ( a->a_group_pat.bv_val );
1267         free( a );
1268 }
1269
1270 void
1271 acl_free( AccessControl *a )
1272 {
1273         Access *n;
1274         AttributeName *an;
1275
1276         if ( a->acl_filter )
1277                 filter_free( a->acl_filter );
1278         if ( a->acl_dn_pat.bv_len )
1279                 free ( a->acl_dn_pat.bv_val );
1280         for (; a->acl_attrs; a->acl_attrs = an) {
1281                 an = a->acl_attrs->an_next;
1282                 free( a->acl_attrs->an_name.bv_val );
1283                 free( a->acl_attrs );
1284         }
1285         for (; a->acl_access; a->acl_access = n) {
1286                 n = a->acl_access->a_next;
1287                 access_free( a->acl_access );
1288         }
1289         free( a );
1290 }
1291
1292 /* Because backend_startup uses acl_append to tack on the global_acl to
1293  * the end of each backend's acl, we cannot just take one argument and
1294  * merrily free our way to the end of the list. backend_destroy calls us
1295  * with the be_acl in arg1, and global_acl in arg2 to give us a stopping
1296  * point. config_destroy calls us with global_acl in arg1 and NULL in
1297  * arg2, so we then proceed to polish off the global_acl.
1298  */
1299 void
1300 acl_destroy( AccessControl *a, AccessControl *end )
1301 {
1302         AccessControl *n;
1303
1304         for (; a && a!= end; a=n) {
1305                 n = a->acl_next;
1306                 acl_free( a );
1307         }
1308 }
1309
1310 char *
1311 access2str( slap_access_t access )
1312 {
1313         if ( access == ACL_NONE ) {
1314                 return "none";
1315
1316         } else if ( access == ACL_AUTH ) {
1317                 return "auth";
1318
1319         } else if ( access == ACL_COMPARE ) {
1320                 return "compare";
1321
1322         } else if ( access == ACL_SEARCH ) {
1323                 return "search";
1324
1325         } else if ( access == ACL_READ ) {
1326                 return "read";
1327
1328         } else if ( access == ACL_WRITE ) {
1329                 return "write";
1330         }
1331
1332         return "unknown";
1333 }
1334
1335 slap_access_t
1336 str2access( const char *str )
1337 {
1338         if ( strcasecmp( str, "none" ) == 0 ) {
1339                 return ACL_NONE;
1340
1341         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1342                 return ACL_AUTH;
1343
1344         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1345                 return ACL_COMPARE;
1346
1347         } else if ( strcasecmp( str, "search" ) == 0 ) {
1348                 return ACL_SEARCH;
1349
1350         } else if ( strcasecmp( str, "read" ) == 0 ) {
1351                 return ACL_READ;
1352
1353         } else if ( strcasecmp( str, "write" ) == 0 ) {
1354                 return ACL_WRITE;
1355         }
1356
1357         return( ACL_INVALID_ACCESS );
1358 }
1359
1360 #ifdef LDAP_DEBUG
1361
1362 static char *style_strings[5] = {
1363                         "regex",
1364                         "base",
1365                         "one",
1366                         "subtree",
1367                         "children"
1368                 };
1369
1370
1371 static void
1372 print_access( Access *b )
1373 {
1374         char maskbuf[ACCESSMASK_MAXLEN];
1375
1376         fprintf( stderr, "\tby" );
1377
1378         if ( b->a_dn_pat.bv_len != 0 ) {
1379                 if( strcmp(b->a_dn_pat.bv_val, "*") == 0
1380                         || strcmp(b->a_dn_pat.bv_val, "users") == 0 
1381                         || strcmp(b->a_dn_pat.bv_val, "anonymous") == 0 
1382                         || strcmp(b->a_dn_pat.bv_val, "self") == 0 )
1383                 {
1384                         fprintf( stderr, " %s", b->a_dn_pat.bv_val );
1385
1386                 } else {
1387                         fprintf( stderr, " dn.%s=%s", style_strings[b->a_dn_style], b->a_dn_pat.bv_val );
1388                 }
1389         }
1390
1391         if ( b->a_dn_at != NULL ) {
1392                 fprintf( stderr, " dnattr=%s", b->a_dn_at->ad_cname.bv_val );
1393         }
1394
1395         if ( b->a_group_pat.bv_len ) {
1396                 fprintf( stderr, " group=%s", b->a_group_pat.bv_val );
1397
1398                 if ( b->a_group_oc ) {
1399                         fprintf( stderr, " objectClass: %s",
1400                                 b->a_group_oc->soc_oclass.oc_oid );
1401
1402                         if ( b->a_group_at ) {
1403                                 fprintf( stderr, " attributeType: %s", b->a_group_at->ad_cname.bv_val );
1404                         }
1405                 }
1406     }
1407
1408         if ( b->a_peername_pat != NULL ) {
1409                 fprintf( stderr, " peername=%s", b->a_peername_pat );
1410         }
1411
1412         if ( b->a_sockname_pat != NULL ) {
1413                 fprintf( stderr, " sockname=%s", b->a_sockname_pat );
1414         }
1415
1416         if ( b->a_domain_pat != NULL ) {
1417                 fprintf( stderr, " domain=%s", b->a_domain_pat );
1418         }
1419
1420         if ( b->a_sockurl_pat != NULL ) {
1421                 fprintf( stderr, " sockurl=%s", b->a_sockurl_pat );
1422         }
1423
1424 #ifdef SLAPD_ACI_ENABLED
1425         if ( b->a_aci_at != NULL ) {
1426                 fprintf( stderr, " aci=%s", b->a_aci_at->ad_cname.bv_val );
1427         }
1428 #endif
1429
1430         /* Security Strength Factors */
1431         if ( b->a_authz.sai_ssf ) {
1432                 fprintf( stderr, " ssf=%u",
1433                         b->a_authz.sai_ssf );
1434         }
1435         if ( b->a_authz.sai_transport_ssf ) {
1436                 fprintf( stderr, " transport_ssf=%u",
1437                         b->a_authz.sai_transport_ssf );
1438         }
1439         if ( b->a_authz.sai_tls_ssf ) {
1440                 fprintf( stderr, " tls_ssf=%u",
1441                         b->a_authz.sai_tls_ssf );
1442         }
1443         if ( b->a_authz.sai_sasl_ssf ) {
1444                 fprintf( stderr, " sasl_ssf=%u",
1445                         b->a_authz.sai_sasl_ssf );
1446         }
1447
1448         fprintf( stderr, " %s%s",
1449                 b->a_dn_self ? "self" : "",
1450                 accessmask2str( b->a_access_mask, maskbuf ) );
1451
1452         if( b->a_type == ACL_BREAK ) {
1453                 fprintf( stderr, " break" );
1454
1455         } else if( b->a_type == ACL_CONTINUE ) {
1456                 fprintf( stderr, " continue" );
1457
1458         } else if( b->a_type != ACL_STOP ) {
1459                 fprintf( stderr, " unknown-control" );
1460         }
1461
1462         fprintf( stderr, "\n" );
1463 }
1464
1465
1466 static void
1467 print_acl( Backend *be, AccessControl *a )
1468 {
1469         int             to = 0;
1470         Access  *b;
1471
1472         fprintf( stderr, "%s ACL: access to",
1473                 be == NULL ? "Global" : "Backend" );
1474
1475         if ( a->acl_dn_pat.bv_len != 0 ) {
1476                 to++;
1477                 fprintf( stderr, " dn.%s=%s\n",
1478                         style_strings[a->acl_dn_style], a->acl_dn_pat.bv_val );
1479         }
1480
1481         if ( a->acl_filter != NULL ) {
1482                 to++;
1483                 fprintf( stderr, " filter=" );
1484                 filter_print( a->acl_filter );
1485                 fprintf( stderr, "\n" );
1486         }
1487
1488         if ( a->acl_attrs != NULL ) {
1489                 int     first = 1;
1490                 AttributeName *an;
1491                 to++;
1492
1493                 fprintf( stderr, " attrs=" );
1494                 for ( an = a->acl_attrs; an; an=an->an_next ) {
1495                         if ( ! first ) {
1496                                 fprintf( stderr, "," );
1497                         }
1498                         fputs( an->an_name.bv_val, stderr );
1499                         first = 0;
1500                 }
1501                 fprintf(  stderr, "\n" );
1502         }
1503
1504         if( !to ) {
1505                 fprintf( stderr, " *\n" );
1506         }
1507
1508         for ( b = a->acl_access; b != NULL; b = b->a_next ) {
1509                 print_access( b );
1510         }
1511
1512         fprintf( stderr, "\n" );
1513 }
1514
1515 #endif /* LDAP_DEBUG */