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