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