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