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