]> git.sur5r.net Git - openldap/blob - servers/slapd/aclparse.c
ea1eef7e146505e91258f3b589b37813789b41a9
[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 vals[2];
556
557                                                 vals[0].bv_val = b->a_group_oc->soc_oid;
558                                                 vals[0].bv_len = strlen(vals[0].bv_val);
559                                                 vals[1].bv_val = NULL;
560
561
562                                                 rc = oc_check_allowed( b->a_group_at->ad_type, vals, NULL );
563
564                                                 if( rc != 0 ) {
565                                                         fprintf( stderr,
566                                                                 "%s: line %d: group: \"%s\" not allowed by \"%s\"\n",
567                                                                 fname, lineno,
568                                                                 b->a_group_at->ad_cname.bv_val,
569                                                                 b->a_group_oc->soc_oid );
570                                                         acl_usage();
571                                                 }
572                                         }
573                                         continue;
574                                 }
575
576                                 if ( strcasecmp( left, "peername" ) == 0 ) {
577                                         if ( right == NULL || right[ 0 ] == '\0' ) {
578                                                 fprintf( stderr,
579                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
580                                                         fname, lineno, left );
581                                                 acl_usage();
582                                         }
583
584                                         if( b->a_peername_pat != NULL ) {
585                                                 fprintf( stderr,
586                                                         "%s: line %d: peername pattern already specified.\n",
587                                                         fname, lineno );
588                                                 acl_usage();
589                                         }
590
591                                         b->a_peername_style = sty;
592                                         if (sty == ACL_STYLE_REGEX) {
593                                                 bv.bv_val = right;
594                                                 acl_regex_normalized_dn( &bv );
595                                                 regtest(fname, lineno, bv.bv_val);
596                                                 b->a_peername_pat = bv.bv_val;
597                                         } else {
598                                                 b->a_peername_pat = ch_strdup( right );
599                                         }
600                                         continue;
601                                 }
602
603                                 if ( strcasecmp( left, "sockname" ) == 0 ) {
604                                         if ( right == NULL || right[ 0 ] == '\0' ) {
605                                                 fprintf( stderr,
606                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
607                                                         fname, lineno, left );
608                                                 acl_usage();
609                                         }
610
611                                         if( b->a_sockname_pat != NULL ) {
612                                                 fprintf( stderr,
613                                                         "%s: line %d: sockname pattern already specified.\n",
614                                                         fname, lineno );
615                                                 acl_usage();
616                                         }
617
618                                         b->a_sockname_style = sty;
619                                         if (sty == ACL_STYLE_REGEX) {
620                                                 bv.bv_val = right;
621                                                 acl_regex_normalized_dn( &bv );
622                                                 regtest(fname, lineno, bv.bv_val);
623                                                 b->a_sockname_pat = bv.bv_val;
624                                         } else {
625                                                 b->a_sockname_pat = ch_strdup( right );
626                                         }
627                                         continue;
628                                 }
629
630                                 if ( strcasecmp( left, "domain" ) == 0 ) {
631                                         if ( right == NULL || right[ 0 ] == '\0' ) {
632                                                 fprintf( stderr,
633                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
634                                                         fname, lineno, left );
635                                                 acl_usage();
636                                         }
637
638                                         if( b->a_domain_pat != NULL ) {
639                                                 fprintf( stderr,
640                                                         "%s: line %d: domain pattern already specified.\n",
641                                                         fname, lineno );
642                                                 acl_usage();
643                                         }
644
645                                         b->a_domain_style = sty;
646                                         if (sty == ACL_STYLE_REGEX) {
647                                                 bv.bv_val = right;
648                                                 acl_regex_normalized_dn( &bv );
649                                                 regtest(fname, lineno, bv.bv_val);
650                                                 b->a_domain_pat = bv.bv_val;
651                                         } else {
652                                                 b->a_domain_pat = ch_strdup( right );
653                                         }
654                                         continue;
655                                 }
656
657                                 if ( strcasecmp( left, "sockurl" ) == 0 ) {
658                                         if ( right == NULL || right[ 0 ] == '\0' ) {
659                                                 fprintf( stderr,
660                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
661                                                         fname, lineno, left );
662                                                 acl_usage();
663                                         }
664
665                                         if( b->a_sockurl_pat != NULL ) {
666                                                 fprintf( stderr,
667                                                         "%s: line %d: sockurl pattern already specified.\n",
668                                                         fname, lineno );
669                                                 acl_usage();
670                                         }
671
672                                         b->a_sockurl_style = sty;
673                                         if (sty == ACL_STYLE_REGEX) {
674                                                 bv.bv_val = right;
675                                                 acl_regex_normalized_dn( &bv );
676                                                 regtest(fname, lineno, bv.bv_val);
677                                                 b->a_sockurl_pat = bv.bv_val;
678                                         } else {
679                                                 b->a_sockurl_pat = ch_strdup( right );
680                                         }
681                                         continue;
682                                 }
683
684                                 if ( strcasecmp( left, "set" ) == 0 ) {
685                                         if( b->a_set_pat.bv_len != 0 ) {
686                                                 fprintf( stderr,
687                                                         "%s: line %d: set attribute already specified.\n",
688                                                         fname, lineno );
689                                                 acl_usage();
690                                         }
691
692                                         if ( right == NULL || *right == '\0' ) {
693                                                 fprintf( stderr,
694                                                         "%s: line %d: no set is defined\n",
695                                                         fname, lineno );
696                                                 acl_usage();
697                                         }
698
699                                         b->a_set_style = sty;
700                                         ber_str2bv( right, 0, 1, &b->a_set_pat );
701
702                                         continue;
703                                 }
704
705 #ifdef SLAPD_ACI_ENABLED
706                                 if ( strcasecmp( left, "aci" ) == 0 ) {
707                                         if( b->a_aci_at != NULL ) {
708                                                 fprintf( stderr,
709                                                         "%s: line %d: aci attribute already specified.\n",
710                                                         fname, lineno );
711                                                 acl_usage();
712                                         }
713
714                                         if ( right != NULL && *right != '\0' ) {
715                                                 rc = slap_str2ad( right, &b->a_aci_at, &text );
716
717                                                 if( rc != LDAP_SUCCESS ) {
718                                                         fprintf( stderr,
719                                                                 "%s: line %d: aci \"%s\": %s\n",
720                                                                 fname, lineno, right, text );
721                                                         acl_usage();
722                                                 }
723
724                                         } else {
725                                                 rc = slap_str2ad( SLAPD_ACI_ATTR, &b->a_aci_at, &text );
726
727                                                 if( rc != LDAP_SUCCESS ) {
728                                                         fprintf( stderr,
729                                                                 "%s: line %d: aci \"%s\": %s\n",
730                                                                 fname, lineno, SLAPD_ACI_ATTR, text );
731                                                         acl_usage();
732                                                 }
733                                         }
734
735                                         if( !is_at_syntax( b->a_aci_at->ad_type,
736                                                 SLAPD_ACI_SYNTAX) )
737                                         {
738                                                 fprintf( stderr,
739                                                         "%s: line %d: aci \"%s\": inappropriate syntax: %s\n",
740                                                         fname, lineno, right,
741                                                         b->a_aci_at->ad_type->sat_syntax_oid );
742                                                 acl_usage();
743                                         }
744
745                                         continue;
746                                 }
747 #endif /* SLAPD_ACI_ENABLED */
748
749                                 if ( strcasecmp( left, "ssf" ) == 0 ) {
750                                         if( b->a_authz.sai_ssf ) {
751                                                 fprintf( stderr,
752                                                         "%s: line %d: ssf attribute already specified.\n",
753                                                         fname, lineno );
754                                                 acl_usage();
755                                         }
756
757                                         if ( right == NULL || *right == '\0' ) {
758                                                 fprintf( stderr,
759                                                         "%s: line %d: no ssf is defined\n",
760                                                         fname, lineno );
761                                                 acl_usage();
762                                         }
763
764                                         b->a_authz.sai_ssf = atoi( right );
765
766                                         if( !b->a_authz.sai_ssf ) {
767                                                 fprintf( stderr,
768                                                         "%s: line %d: invalid ssf value (%s)\n",
769                                                         fname, lineno, right );
770                                                 acl_usage();
771                                         }
772                                         continue;
773                                 }
774
775                                 if ( strcasecmp( left, "transport_ssf" ) == 0 ) {
776                                         if( b->a_authz.sai_transport_ssf ) {
777                                                 fprintf( stderr,
778                                                         "%s: line %d: transport_ssf attribute already specified.\n",
779                                                         fname, lineno );
780                                                 acl_usage();
781                                         }
782
783                                         if ( right == NULL || *right == '\0' ) {
784                                                 fprintf( stderr,
785                                                         "%s: line %d: no transport_ssf is defined\n",
786                                                         fname, lineno );
787                                                 acl_usage();
788                                         }
789
790                                         b->a_authz.sai_transport_ssf = atoi( right );
791
792                                         if( !b->a_authz.sai_transport_ssf ) {
793                                                 fprintf( stderr,
794                                                         "%s: line %d: invalid transport_ssf value (%s)\n",
795                                                         fname, lineno, right );
796                                                 acl_usage();
797                                         }
798                                         continue;
799                                 }
800
801                                 if ( strcasecmp( left, "tls_ssf" ) == 0 ) {
802                                         if( b->a_authz.sai_tls_ssf ) {
803                                                 fprintf( stderr,
804                                                         "%s: line %d: tls_ssf attribute already specified.\n",
805                                                         fname, lineno );
806                                                 acl_usage();
807                                         }
808
809                                         if ( right == NULL || *right == '\0' ) {
810                                                 fprintf( stderr,
811                                                         "%s: line %d: no tls_ssf is defined\n",
812                                                         fname, lineno );
813                                                 acl_usage();
814                                         }
815
816                                         b->a_authz.sai_tls_ssf = atoi( right );
817
818                                         if( !b->a_authz.sai_tls_ssf ) {
819                                                 fprintf( stderr,
820                                                         "%s: line %d: invalid tls_ssf value (%s)\n",
821                                                         fname, lineno, right );
822                                                 acl_usage();
823                                         }
824                                         continue;
825                                 }
826
827                                 if ( strcasecmp( left, "sasl_ssf" ) == 0 ) {
828                                         if( b->a_authz.sai_sasl_ssf ) {
829                                                 fprintf( stderr,
830                                                         "%s: line %d: sasl_ssf attribute already specified.\n",
831                                                         fname, lineno );
832                                                 acl_usage();
833                                         }
834
835                                         if ( right == NULL || *right == '\0' ) {
836                                                 fprintf( stderr,
837                                                         "%s: line %d: no sasl_ssf is defined\n",
838                                                         fname, lineno );
839                                                 acl_usage();
840                                         }
841
842                                         b->a_authz.sai_sasl_ssf = atoi( right );
843
844                                         if( !b->a_authz.sai_sasl_ssf ) {
845                                                 fprintf( stderr,
846                                                         "%s: line %d: invalid sasl_ssf value (%s)\n",
847                                                         fname, lineno, right );
848                                                 acl_usage();
849                                         }
850                                         continue;
851                                 }
852
853                                 if( right != NULL ) {
854                                         /* unsplit */
855                                         right[-1] = '=';
856                                 }
857                                 break;
858                         }
859
860                         if( i == argc || ( strcasecmp( left, "stop" ) == 0 )) { 
861                                 /* out of arguments or plain stop */
862
863                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
864                                 b->a_type = ACL_STOP;
865
866                                 access_append( &a->acl_access, b );
867                                 continue;
868                         }
869
870                         if( strcasecmp( left, "continue" ) == 0 ) {
871                                 /* plain continue */
872
873                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
874                                 b->a_type = ACL_CONTINUE;
875
876                                 access_append( &a->acl_access, b );
877                                 continue;
878                         }
879
880                         if( strcasecmp( left, "break" ) == 0 ) {
881                                 /* plain continue */
882
883                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
884                                 b->a_type = ACL_BREAK;
885
886                                 access_append( &a->acl_access, b );
887                                 continue;
888                         }
889
890                         if ( strcasecmp( left, "by" ) == 0 ) {
891                                 /* we've gone too far */
892                                 --i;
893                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
894                                 b->a_type = ACL_STOP;
895
896                                 access_append( &a->acl_access, b );
897                                 continue;
898                         }
899
900                         /* get <access> */
901                         if( strncasecmp( left, "self", 4 ) == 0 ) {
902                                 b->a_dn_self = 1;
903                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( &left[4] ) );
904
905                         } else {
906                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( left ) );
907                         }
908
909                         if( ACL_IS_INVALID( b->a_access_mask ) ) {
910                                 fprintf( stderr,
911                                         "%s: line %d: expecting <access> got \"%s\"\n",
912                                         fname, lineno, left );
913                                 acl_usage();
914                         }
915
916                         b->a_type = ACL_STOP;
917
918                         if( ++i == argc ) {
919                                 /* out of arguments or plain stop */
920                                 access_append( &a->acl_access, b );
921                                 continue;
922                         }
923
924                         if( strcasecmp( argv[i], "continue" ) == 0 ) {
925                                 /* plain continue */
926                                 b->a_type = ACL_CONTINUE;
927
928                         } else if( strcasecmp( argv[i], "break" ) == 0 ) {
929                                 /* plain continue */
930                                 b->a_type = ACL_BREAK;
931
932                         } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
933                                 /* gone to far */
934                                 i--;
935                         }
936
937                         access_append( &a->acl_access, b );
938
939                 } else {
940                         fprintf( stderr,
941                     "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
942                             fname, lineno, argv[i] );
943                         acl_usage();
944                 }
945         }
946
947         /* if we have no real access clause, complain and do nothing */
948         if ( a == NULL ) {
949                         fprintf( stderr,
950                                 "%s: line %d: warning: no access clause(s) specified in access line\n",
951                             fname, lineno );
952
953         } else {
954 #ifdef LDAP_DEBUG
955                 if (ldap_debug & LDAP_DEBUG_ACL)
956                         print_acl(be, a);
957 #endif
958         
959                 if ( a->acl_access == NULL ) {
960                         fprintf( stderr,
961                         "%s: line %d: warning: no by clause(s) specified in access line\n",
962                             fname, lineno );
963                 }
964
965                 if ( be != NULL ) {
966                         acl_append( &be->be_acl, a );
967                 } else {
968                         acl_append( &global_acl, a );
969                 }
970         }
971 }
972
973 char *
974 accessmask2str( slap_mask_t mask, char *buf )
975 {
976         int none=1;
977         char *ptr = buf;
978
979         assert( buf != NULL );
980
981         if ( ACL_IS_INVALID( mask ) ) {
982                 return "invalid";
983         }
984
985         buf[0] = '\0';
986
987         if ( ACL_IS_LEVEL( mask ) ) {
988                 if ( ACL_LVL_IS_NONE(mask) ) {
989                         ptr = slap_strcopy( ptr, "none" );
990
991                 } else if ( ACL_LVL_IS_AUTH(mask) ) {
992                         ptr = slap_strcopy( ptr, "auth" );
993
994                 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
995                         ptr = slap_strcopy( ptr, "compare" );
996
997                 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
998                         ptr = slap_strcopy( ptr, "search" );
999
1000                 } else if ( ACL_LVL_IS_READ(mask) ) {
1001                         ptr = slap_strcopy( ptr, "read" );
1002
1003                 } else if ( ACL_LVL_IS_WRITE(mask) ) {
1004                         ptr = slap_strcopy( ptr, "write" );
1005                 } else {
1006                         ptr = slap_strcopy( ptr, "unknown" );
1007                 }
1008                 
1009                 *ptr++ = '(';
1010         }
1011
1012         if( ACL_IS_ADDITIVE( mask ) ) {
1013                 *ptr++ = '+';
1014
1015         } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
1016                 *ptr++ = '-';
1017
1018         } else {
1019                 *ptr++ = '=';
1020         }
1021
1022         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
1023                 none = 0;
1024                 *ptr++ = 'w';
1025         } 
1026
1027         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
1028                 none = 0;
1029                 *ptr++ = 'r';
1030         } 
1031
1032         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
1033                 none = 0;
1034                 *ptr++ = 's';
1035         } 
1036
1037         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
1038                 none = 0;
1039                 *ptr++ = 'c';
1040         } 
1041
1042         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
1043                 none = 0;
1044                 *ptr++ = 'x';
1045         } 
1046
1047         if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
1048                 none = 0;
1049                 *ptr++ = 'n';
1050         } 
1051
1052         if ( none ) {
1053                 *ptr++ = '0';
1054         }
1055
1056         if ( ACL_IS_LEVEL( mask ) ) {
1057                 *ptr++ = ')';
1058         }
1059
1060         *ptr = '\0';
1061
1062         return buf;
1063 }
1064
1065 slap_mask_t
1066 str2accessmask( const char *str )
1067 {
1068         slap_mask_t     mask;
1069
1070         if( !ASCII_ALPHA(str[0]) ) {
1071                 int i;
1072
1073                 if ( str[0] == '=' ) {
1074                         ACL_INIT(mask);
1075
1076                 } else if( str[0] == '+' ) {
1077                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
1078
1079                 } else if( str[0] == '-' ) {
1080                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
1081
1082                 } else {
1083                         ACL_INVALIDATE(mask);
1084                         return mask;
1085                 }
1086
1087                 for( i=1; str[i] != '\0'; i++ ) {
1088                         if( TOLOWER(str[i]) == 'w' ) {
1089                                 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1090
1091                         } else if( TOLOWER(str[i]) == 'r' ) {
1092                                 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1093
1094                         } else if( TOLOWER(str[i]) == 's' ) {
1095                                 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1096
1097                         } else if( TOLOWER(str[i]) == 'c' ) {
1098                                 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1099
1100                         } else if( TOLOWER(str[i]) == 'x' ) {
1101                                 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1102
1103                         } else if( str[i] != '0' ) {
1104                                 ACL_INVALIDATE(mask);
1105                                 return mask;
1106                         }
1107                 }
1108
1109                 return mask;
1110         }
1111
1112         if ( strcasecmp( str, "none" ) == 0 ) {
1113                 ACL_LVL_ASSIGN_NONE(mask);
1114
1115         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1116                 ACL_LVL_ASSIGN_AUTH(mask);
1117
1118         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1119                 ACL_LVL_ASSIGN_COMPARE(mask);
1120
1121         } else if ( strcasecmp( str, "search" ) == 0 ) {
1122                 ACL_LVL_ASSIGN_SEARCH(mask);
1123
1124         } else if ( strcasecmp( str, "read" ) == 0 ) {
1125                 ACL_LVL_ASSIGN_READ(mask);
1126
1127         } else if ( strcasecmp( str, "write" ) == 0 ) {
1128                 ACL_LVL_ASSIGN_WRITE(mask);
1129
1130         } else {
1131                 ACL_INVALIDATE( mask );
1132         }
1133
1134         return mask;
1135 }
1136
1137 static void
1138 acl_usage( void )
1139 {
1140         fprintf( stderr, "\n"
1141                 "<access clause> ::= access to <what> "
1142                                 "[ by <who> <access> [ <control> ] ]+ \n"
1143                 "<what> ::= * | [dn[.<dnstyle>]=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n"
1144                 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
1145                 "<attr> ::= <attrname> | entry | children\n"
1146                 "<who> ::= [ * | anonymous | users | self | dn[.<dnstyle>]=<regex> ]\n"
1147                         "\t[dnattr=<attrname>]\n"
1148                         "\t[group[/<objectclass>[/<attrname>]][.<style>]=<regex>]\n"
1149                         "\t[peername[.<style>]=<regex>] [sockname[.<style>]=<regex>]\n"
1150                         "\t[domain[.<style>]=<regex>] [sockurl[.<style>]=<regex>]\n"
1151 #ifdef SLAPD_ACI_ENABLED
1152                         "\t[aci=<attrname>]\n"
1153 #endif
1154                         "\t[ssf=<n>] [transport_ssf=<n>] [tls_ssf=<n>] [sasl_ssf=<n>]\n"
1155                 "<dnstyle> ::= regex | base | exact (alias of base) | one | sub | children\n"
1156                 "<style> ::= regex | base | exact (alias of base)\n"
1157                 "<groupflags> ::= R\n"
1158                 "<access> ::= [self]{<level>|<priv>}\n"
1159                 "<level> ::= none | auth | compare | search | read | write\n"
1160                 "<priv> ::= {=|+|-}{w|r|s|c|x}+\n"
1161                 "<control> ::= [ stop | continue | break ]\n"
1162                 );
1163         exit( EXIT_FAILURE );
1164 }
1165
1166 /*
1167  * At present it simply eats the (optional) space after 
1168  * a RDN separator (,)
1169  * Eventually will evolve in a more complete normalization
1170  *
1171  * Note that the input berval only needs bv_val, it ignores
1172  * the input bv_len and sets it on return.
1173  */
1174 static void
1175 acl_regex_normalized_dn(
1176         struct berval *pattern
1177 )
1178 {
1179         char *str, *p;
1180
1181         str = ch_strdup( pattern->bv_val );
1182
1183         for ( p = str; p && p[ 0 ]; p++ ) {
1184                 /* escape */
1185                 if ( p[ 0 ] == '\\' ) {
1186                         /* 
1187                          * if escaping a hex pair we should
1188                          * increment p twice; however, in that 
1189                          * case the second hex number does 
1190                          * no harm
1191                          */
1192                         p++;
1193                 }
1194
1195                 if ( p[ 0 ] == ',' ) {
1196                         if ( p[ 1 ] == ' ' ) {
1197                                 char *q;
1198                         
1199                                 /*
1200                                  * too much space should be 
1201                                  * an error if we are pedantic
1202                                  */
1203                                 for ( q = &p[ 2 ]; q[ 0 ] == ' '; q++ ) {
1204                                         /* DO NOTHING */ ;
1205                                 }
1206                                 AC_MEMCPY( p+1, q, pattern->bv_len-(q-str)+1);
1207                         }
1208                 }
1209         }
1210         pattern->bv_val = str;
1211         pattern->bv_len = p-str;
1212
1213         return;
1214 }
1215
1216 static void
1217 split(
1218     char        *line,
1219     int         splitchar,
1220     char        **left,
1221     char        **right
1222 )
1223 {
1224         *left = line;
1225         if ( (*right = strchr( line, splitchar )) != NULL ) {
1226                 *((*right)++) = '\0';
1227         }
1228 }
1229
1230 static void
1231 access_append( Access **l, Access *a )
1232 {
1233         for ( ; *l != NULL; l = &(*l)->a_next )
1234                 ;       /* NULL */
1235
1236         *l = a;
1237 }
1238
1239 void
1240 acl_append( AccessControl **l, AccessControl *a )
1241 {
1242         for ( ; *l != NULL; l = &(*l)->acl_next )
1243                 ;       /* NULL */
1244
1245         *l = a;
1246 }
1247
1248 static void
1249 access_free( Access *a )
1250 {
1251         if ( a->a_dn_pat.bv_val )
1252                 free ( a->a_dn_pat.bv_val );
1253         if ( a->a_peername_pat )
1254                 free ( a->a_peername_pat );
1255         if ( a->a_sockname_pat )
1256                 free ( a->a_sockname_pat );
1257         if ( a->a_domain_pat )
1258                 free ( a->a_domain_pat );
1259         if ( a->a_sockurl_pat )
1260                 free ( a->a_sockurl_pat );
1261         if ( a->a_set_pat.bv_len )
1262                 free ( a->a_set_pat.bv_val );
1263         if ( a->a_group_pat.bv_len )
1264                 free ( a->a_group_pat.bv_val );
1265         free( a );
1266 }
1267
1268 void
1269 acl_free( AccessControl *a )
1270 {
1271         Access *n;
1272         AttributeName *an;
1273
1274         if ( a->acl_filter )
1275                 filter_free( a->acl_filter );
1276         if ( a->acl_dn_pat.bv_len )
1277                 free ( a->acl_dn_pat.bv_val );
1278         for (; a->acl_attrs; a->acl_attrs = an) {
1279                 an = a->acl_attrs->an_next;
1280                 free( a->acl_attrs->an_name.bv_val );
1281                 free( a->acl_attrs );
1282         }
1283         for (; a->acl_access; a->acl_access = n) {
1284                 n = a->acl_access->a_next;
1285                 access_free( a->acl_access );
1286         }
1287         free( a );
1288 }
1289
1290 /* Because backend_startup uses acl_append to tack on the global_acl to
1291  * the end of each backend's acl, we cannot just take one argument and
1292  * merrily free our way to the end of the list. backend_destroy calls us
1293  * with the be_acl in arg1, and global_acl in arg2 to give us a stopping
1294  * point. config_destroy calls us with global_acl in arg1 and NULL in
1295  * arg2, so we then proceed to polish off the global_acl.
1296  */
1297 void
1298 acl_destroy( AccessControl *a, AccessControl *end )
1299 {
1300         AccessControl *n;
1301
1302         for (; a && a!= end; a=n) {
1303                 n = a->acl_next;
1304                 acl_free( a );
1305         }
1306 }
1307
1308 char *
1309 access2str( slap_access_t access )
1310 {
1311         if ( access == ACL_NONE ) {
1312                 return "none";
1313
1314         } else if ( access == ACL_AUTH ) {
1315                 return "auth";
1316
1317         } else if ( access == ACL_COMPARE ) {
1318                 return "compare";
1319
1320         } else if ( access == ACL_SEARCH ) {
1321                 return "search";
1322
1323         } else if ( access == ACL_READ ) {
1324                 return "read";
1325
1326         } else if ( access == ACL_WRITE ) {
1327                 return "write";
1328         }
1329
1330         return "unknown";
1331 }
1332
1333 slap_access_t
1334 str2access( const char *str )
1335 {
1336         if ( strcasecmp( str, "none" ) == 0 ) {
1337                 return ACL_NONE;
1338
1339         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1340                 return ACL_AUTH;
1341
1342         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1343                 return ACL_COMPARE;
1344
1345         } else if ( strcasecmp( str, "search" ) == 0 ) {
1346                 return ACL_SEARCH;
1347
1348         } else if ( strcasecmp( str, "read" ) == 0 ) {
1349                 return ACL_READ;
1350
1351         } else if ( strcasecmp( str, "write" ) == 0 ) {
1352                 return ACL_WRITE;
1353         }
1354
1355         return( ACL_INVALID_ACCESS );
1356 }
1357
1358 #ifdef LDAP_DEBUG
1359
1360 static char *style_strings[5] = {
1361                         "regex",
1362                         "base",
1363                         "one",
1364                         "subtree",
1365                         "children"
1366                 };
1367
1368
1369 static void
1370 print_access( Access *b )
1371 {
1372         char maskbuf[ACCESSMASK_MAXLEN];
1373
1374         fprintf( stderr, "\tby" );
1375
1376         if ( b->a_dn_pat.bv_len != 0 ) {
1377                 if( strcmp(b->a_dn_pat.bv_val, "*") == 0
1378                         || strcmp(b->a_dn_pat.bv_val, "users") == 0 
1379                         || strcmp(b->a_dn_pat.bv_val, "anonymous") == 0 
1380                         || strcmp(b->a_dn_pat.bv_val, "self") == 0 )
1381                 {
1382                         fprintf( stderr, " %s", b->a_dn_pat.bv_val );
1383
1384                 } else {
1385                         fprintf( stderr, " dn.%s=%s", style_strings[b->a_dn_style], b->a_dn_pat.bv_val );
1386                 }
1387         }
1388
1389         if ( b->a_dn_at != NULL ) {
1390                 fprintf( stderr, " dnattr=%s", b->a_dn_at->ad_cname.bv_val );
1391         }
1392
1393         if ( b->a_group_pat.bv_len ) {
1394                 fprintf( stderr, " group=%s", b->a_group_pat.bv_val );
1395
1396                 if ( b->a_group_oc ) {
1397                         fprintf( stderr, " objectClass: %s",
1398                                 b->a_group_oc->soc_oclass.oc_oid );
1399
1400                         if ( b->a_group_at ) {
1401                                 fprintf( stderr, " attributeType: %s", b->a_group_at->ad_cname.bv_val );
1402                         }
1403                 }
1404     }
1405
1406         if ( b->a_peername_pat != NULL ) {
1407                 fprintf( stderr, " peername=%s", b->a_peername_pat );
1408         }
1409
1410         if ( b->a_sockname_pat != NULL ) {
1411                 fprintf( stderr, " sockname=%s", b->a_sockname_pat );
1412         }
1413
1414         if ( b->a_domain_pat != NULL ) {
1415                 fprintf( stderr, " domain=%s", b->a_domain_pat );
1416         }
1417
1418         if ( b->a_sockurl_pat != NULL ) {
1419                 fprintf( stderr, " sockurl=%s", b->a_sockurl_pat );
1420         }
1421
1422 #ifdef SLAPD_ACI_ENABLED
1423         if ( b->a_aci_at != NULL ) {
1424                 fprintf( stderr, " aci=%s", b->a_aci_at->ad_cname.bv_val );
1425         }
1426 #endif
1427
1428         /* Security Strength Factors */
1429         if ( b->a_authz.sai_ssf ) {
1430                 fprintf( stderr, " ssf=%u",
1431                         b->a_authz.sai_ssf );
1432         }
1433         if ( b->a_authz.sai_transport_ssf ) {
1434                 fprintf( stderr, " transport_ssf=%u",
1435                         b->a_authz.sai_transport_ssf );
1436         }
1437         if ( b->a_authz.sai_tls_ssf ) {
1438                 fprintf( stderr, " tls_ssf=%u",
1439                         b->a_authz.sai_tls_ssf );
1440         }
1441         if ( b->a_authz.sai_sasl_ssf ) {
1442                 fprintf( stderr, " sasl_ssf=%u",
1443                         b->a_authz.sai_sasl_ssf );
1444         }
1445
1446         fprintf( stderr, " %s%s",
1447                 b->a_dn_self ? "self" : "",
1448                 accessmask2str( b->a_access_mask, maskbuf ) );
1449
1450         if( b->a_type == ACL_BREAK ) {
1451                 fprintf( stderr, " break" );
1452
1453         } else if( b->a_type == ACL_CONTINUE ) {
1454                 fprintf( stderr, " continue" );
1455
1456         } else if( b->a_type != ACL_STOP ) {
1457                 fprintf( stderr, " unknown-control" );
1458         }
1459
1460         fprintf( stderr, "\n" );
1461 }
1462
1463
1464 static void
1465 print_acl( Backend *be, AccessControl *a )
1466 {
1467         int             to = 0;
1468         Access  *b;
1469
1470         fprintf( stderr, "%s ACL: access to",
1471                 be == NULL ? "Global" : "Backend" );
1472
1473         if ( a->acl_dn_pat.bv_len != 0 ) {
1474                 to++;
1475                 fprintf( stderr, " dn.%s=%s\n",
1476                         style_strings[a->acl_dn_style], a->acl_dn_pat.bv_val );
1477         }
1478
1479         if ( a->acl_filter != NULL ) {
1480                 to++;
1481                 fprintf( stderr, " filter=" );
1482                 filter_print( a->acl_filter );
1483                 fprintf( stderr, "\n" );
1484         }
1485
1486         if ( a->acl_attrs != NULL ) {
1487                 int     first = 1;
1488                 AttributeName *an;
1489                 to++;
1490
1491                 fprintf( stderr, " attrs=" );
1492                 for ( an = a->acl_attrs; an; an=an->an_next ) {
1493                         if ( ! first ) {
1494                                 fprintf( stderr, "," );
1495                         }
1496                         fputs( an->an_name.bv_val, stderr );
1497                         first = 0;
1498                 }
1499                 fprintf(  stderr, "\n" );
1500         }
1501
1502         if( !to ) {
1503                 fprintf( stderr, " *\n" );
1504         }
1505
1506         for ( b = a->acl_access; b != NULL; b = b->a_next ) {
1507                 print_access( b );
1508         }
1509
1510         fprintf( stderr, "\n" );
1511 }
1512
1513 #endif /* LDAP_DEBUG */