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