]> git.sur5r.net Git - openldap/blob - servers/slapd/aclparse.c
update diagnostics and man pages
[openldap] / servers / slapd / aclparse.c
1 /* aclparse.c - routines to parse and check acl's */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/ctype.h>
32 #include <ac/regex.h>
33 #include <ac/socket.h>
34 #include <ac/string.h>
35 #include <ac/unistd.h>
36
37 #include "slap.h"
38 #include "lber_pvt.h"
39 #include "lutil.h"
40
41 static char *style_strings[] = {
42         "regex",
43         "expand",
44         "base",
45         "one",
46         "subtree",
47         "children",
48         "level",
49         "attrof",
50         "anonymous",
51         "users",
52         "self",
53         "ip",
54         "path",
55         NULL
56 };
57
58 static void             split(char *line, int splitchar, char **left, char **right);
59 static void             access_append(Access **l, Access *a);
60 static void             acl_usage(void) LDAP_GCCATTR((noreturn));
61
62 static void             acl_regex_normalized_dn(const char *src, struct berval *pat);
63
64 #ifdef LDAP_DEBUG
65 static void             print_acl(Backend *be, AccessControl *a);
66 static void             print_access(Access *b);
67 #endif
68
69 static int              check_scope( BackendDB *be, AccessControl *a );
70
71 #ifdef SLAP_DYNACL
72 static int
73 slap_dynacl_config( const char *fname, int lineno, Access *b, const char *name, slap_style_t sty, const char *right )
74 {
75         slap_dynacl_t   *da, *tmp;
76         int             rc = 0;
77
78         for ( da = b->a_dynacl; da; da = da->da_next ) {
79                 if ( strcasecmp( da->da_name, name ) == 0 ) {
80                         fprintf( stderr,
81                                 "%s: line %d: dynacl \"%s\" already specified.\n",
82                                 fname, lineno, name );
83                         acl_usage();
84                 }
85         }
86
87         da = slap_dynacl_get( name );
88         if ( da == NULL ) {
89                 return -1;
90         }
91
92         tmp = ch_malloc( sizeof( slap_dynacl_t ) );
93         *tmp = *da;
94
95         if ( tmp->da_parse ) {
96                 rc = ( *tmp->da_parse )( fname, lineno, sty, right, &tmp->da_private );
97                 if ( rc ) {
98                         ch_free( tmp );
99                         return rc;
100                 }
101         }
102
103         tmp->da_next = b->a_dynacl;
104         b->a_dynacl = tmp;
105
106         return 0;
107 }
108 #endif /* SLAP_DYNACL */
109
110 static void
111 regtest(const char *fname, int lineno, char *pat) {
112         int e;
113         regex_t re;
114
115         char buf[512];
116         unsigned size;
117
118         char *sp;
119         char *dp;
120         int  flag;
121
122         sp = pat;
123         dp = buf;
124         size = 0;
125         buf[0] = '\0';
126
127         for (size = 0, flag = 0; (size < sizeof(buf)) && *sp; sp++) {
128                 if (flag) {
129                         if (*sp == '$'|| (*sp >= '0' && *sp <= '9')) {
130                                 *dp++ = *sp;
131                                 size++;
132                         }
133                         flag = 0;
134
135                 } else {
136                         if (*sp == '$') {
137                                 flag = 1;
138                         } else {
139                                 *dp++ = *sp;
140                                 size++;
141                         }
142                 }
143         }
144
145         *dp = '\0';
146         if ( size >= (sizeof(buf) - 1) ) {
147                 fprintf( stderr,
148                         "%s: line %d: regular expression \"%s\" too large\n",
149                         fname, lineno, pat );
150                 acl_usage();
151         }
152
153         if ((e = regcomp(&re, buf, REG_EXTENDED|REG_ICASE))) {
154                 char error[512];
155                 regerror(e, &re, error, sizeof(error));
156                 fprintf( stderr,
157                         "%s: line %d: regular expression \"%s\" bad because of %s\n",
158                         fname, lineno, pat, error );
159                 acl_usage();
160         }
161         regfree(&re);
162 }
163
164 /*
165  * Experimental
166  *
167  * Check if the pattern of an ACL, if any, matches the scope
168  * of the backend it is defined within.
169  */
170 #define ACL_SCOPE_UNKNOWN       (-2)
171 #define ACL_SCOPE_ERR           (-1)
172 #define ACL_SCOPE_OK            (0)
173 #define ACL_SCOPE_PARTIAL       (1)
174 #define ACL_SCOPE_WARN          (2)
175
176 static int
177 check_scope( BackendDB *be, AccessControl *a )
178 {
179         int             patlen;
180         struct berval   dn;
181
182         dn = be->be_nsuffix[0];
183
184         if ( BER_BVISEMPTY( &dn ) ) {
185                 return ACL_SCOPE_OK;
186         }
187
188         if ( !BER_BVISEMPTY( &a->acl_dn_pat ) ||
189                         a->acl_dn_style != ACL_STYLE_REGEX )
190         {
191                 slap_style_t    style = a->acl_dn_style;
192
193                 if ( style == ACL_STYLE_REGEX ) {
194                         char            dnbuf[SLAP_LDAPDN_MAXLEN + 2];
195                         char            rebuf[SLAP_LDAPDN_MAXLEN + 1];
196                         ber_len_t       rebuflen;
197                         regex_t         re;
198                         int             rc;
199                         
200                         /* add trailing '$' to database suffix to form
201                          * a simple trial regex pattern "<suffix>$" */
202                         AC_MEMCPY( dnbuf, be->be_nsuffix[0].bv_val,
203                                 be->be_nsuffix[0].bv_len );
204                         dnbuf[be->be_nsuffix[0].bv_len] = '$';
205                         dnbuf[be->be_nsuffix[0].bv_len + 1] = '\0';
206
207                         if ( regcomp( &re, dnbuf, REG_EXTENDED|REG_ICASE ) ) {
208                                 return ACL_SCOPE_WARN;
209                         }
210
211                         /* remove trailing ')$', if any, from original
212                          * regex pattern */
213                         rebuflen = a->acl_dn_pat.bv_len;
214                         AC_MEMCPY( rebuf, a->acl_dn_pat.bv_val, rebuflen + 1 );
215                         if ( rebuf[rebuflen - 1] == '$' ) {
216                                 rebuf[--rebuflen] = '\0';
217                         }
218                         while ( rebuflen > be->be_nsuffix[0].bv_len && rebuf[rebuflen - 1] == ')' ) {
219                                 rebuf[--rebuflen] = '\0';
220                         }
221                         if ( rebuflen == be->be_nsuffix[0].bv_len ) {
222                                 rc = ACL_SCOPE_WARN;
223                                 goto regex_done;
224                         }
225
226                         /* not a clear indication of scoping error, though */
227                         rc = regexec( &re, rebuf, 0, NULL, 0 )
228                                 ? ACL_SCOPE_WARN : ACL_SCOPE_OK;
229
230 regex_done:;
231                         regfree( &re );
232                         return rc;
233                 }
234
235                 patlen = a->acl_dn_pat.bv_len;
236                 /* If backend suffix is longer than pattern,
237                  * it is a potential mismatch (in the sense
238                  * that a superior naming context could
239                  * match */
240                 if ( dn.bv_len > patlen ) {
241                         /* base is blatantly wrong */
242                         if ( style == ACL_STYLE_BASE ) return ACL_SCOPE_ERR;
243
244                         /* a style of one can be wrong if there is
245                          * more than one level between the suffix
246                          * and the pattern */
247                         if ( style == ACL_STYLE_ONE ) {
248                                 int     rdnlen = -1, sep = 0;
249
250                                 if ( patlen > 0 ) {
251                                         if ( !DN_SEPARATOR( dn.bv_val[dn.bv_len - patlen - 1] )) {
252                                                 return ACL_SCOPE_ERR;
253                                         }
254                                         sep = 1;
255                                 }
256
257                                 rdnlen = dn_rdnlen( NULL, &dn );
258                                 if ( rdnlen != dn.bv_len - patlen - sep )
259                                         return ACL_SCOPE_ERR;
260                         }
261
262                         /* if the trailing part doesn't match,
263                          * then it's an error */
264                         if ( strcmp( a->acl_dn_pat.bv_val,
265                                 &dn.bv_val[dn.bv_len - patlen] ) != 0 )
266                         {
267                                 return ACL_SCOPE_ERR;
268                         }
269
270                         return ACL_SCOPE_PARTIAL;
271                 }
272
273                 switch ( style ) {
274                 case ACL_STYLE_BASE:
275                 case ACL_STYLE_ONE:
276                 case ACL_STYLE_CHILDREN:
277                 case ACL_STYLE_SUBTREE:
278                         break;
279
280                 default:
281                         assert( 0 );
282                         break;
283                 }
284
285                 if ( dn.bv_len < patlen &&
286                         !DN_SEPARATOR( a->acl_dn_pat.bv_val[patlen - dn.bv_len - 1] ))
287                 {
288                         return ACL_SCOPE_ERR;
289                 }
290
291                 if ( strcmp( &a->acl_dn_pat.bv_val[patlen - dn.bv_len], dn.bv_val )
292                         != 0 )
293                 {
294                         return ACL_SCOPE_ERR;
295                 }
296
297                 return ACL_SCOPE_OK;
298         }
299
300         return ACL_SCOPE_UNKNOWN;
301 }
302
303 void
304 parse_acl(
305     Backend     *be,
306     const char  *fname,
307     int         lineno,
308     int         argc,
309     char        **argv )
310 {
311         int             i;
312         char            *left, *right, *style, *next;
313         struct berval   bv;
314         AccessControl   *a;
315         Access  *b;
316         int rc;
317         const char *text;
318
319         a = NULL;
320         for ( i = 1; i < argc; i++ ) {
321                 /* to clause - select which entries are protected */
322                 if ( strcasecmp( argv[i], "to" ) == 0 ) {
323                         if ( a != NULL ) {
324                                 fprintf( stderr, "%s: line %d: "
325                                         "only one to clause allowed in access line\n",
326                                     fname, lineno );
327                                 acl_usage();
328                         }
329                         a = (AccessControl *) ch_calloc( 1, sizeof(AccessControl) );
330                         for ( ++i; i < argc; i++ ) {
331                                 if ( strcasecmp( argv[i], "by" ) == 0 ) {
332                                         i--;
333                                         break;
334                                 }
335
336                                 if ( strcasecmp( argv[i], "*" ) == 0 ) {
337                                         if ( !BER_BVISEMPTY( &a->acl_dn_pat ) ||
338                                                 a->acl_dn_style != ACL_STYLE_REGEX )
339                                         {
340                                                 fprintf( stderr,
341                                                         "%s: line %d: dn pattern"
342                                                         " already specified in to clause.\n",
343                                                         fname, lineno );
344                                                 acl_usage();
345                                         }
346
347                                         ber_str2bv( "*", STRLENOF( "*" ), 1, &a->acl_dn_pat );
348                                         continue;
349                                 }
350
351                                 split( argv[i], '=', &left, &right );
352                                 split( left, '.', &left, &style );
353
354                                 if ( right == NULL ) {
355                                         fprintf( stderr, "%s: line %d: "
356                                                 "missing \"=\" in \"%s\" in to clause\n",
357                                             fname, lineno, left );
358                                         acl_usage();
359                                 }
360
361                                 if ( strcasecmp( left, "dn" ) == 0 ) {
362                                         if ( !BER_BVISEMPTY( &a->acl_dn_pat ) ||
363                                                 a->acl_dn_style != ACL_STYLE_REGEX )
364                                         {
365                                                 fprintf( stderr,
366                                                         "%s: line %d: dn pattern"
367                                                         " already specified in to clause.\n",
368                                                         fname, lineno );
369                                                 acl_usage();
370                                         }
371
372                                         if ( style == NULL || *style == '\0' ||
373                                                 strcasecmp( style, "baseObject" ) == 0 ||
374                                                 strcasecmp( style, "base" ) == 0 ||
375                                                 strcasecmp( style, "exact" ) == 0 )
376                                         {
377                                                 a->acl_dn_style = ACL_STYLE_BASE;
378                                                 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
379
380                                         } else if ( strcasecmp( style, "oneLevel" ) == 0 ||
381                                                 strcasecmp( style, "one" ) == 0 )
382                                         {
383                                                 a->acl_dn_style = ACL_STYLE_ONE;
384                                                 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
385
386                                         } else if ( strcasecmp( style, "subtree" ) == 0 ||
387                                                 strcasecmp( style, "sub" ) == 0 )
388                                         {
389                                                 if( *right == '\0' ) {
390                                                         ber_str2bv( "*", STRLENOF( "*" ), 1, &a->acl_dn_pat );
391
392                                                 } else {
393                                                         a->acl_dn_style = ACL_STYLE_SUBTREE;
394                                                         ber_str2bv( right, 0, 1, &a->acl_dn_pat );
395                                                 }
396
397                                         } else if ( strcasecmp( style, "children" ) == 0 ) {
398                                                 a->acl_dn_style = ACL_STYLE_CHILDREN;
399                                                 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
400
401                                         } else if ( strcasecmp( style, "regex" ) == 0 ) {
402                                                 a->acl_dn_style = ACL_STYLE_REGEX;
403
404                                                 if ( *right == '\0' ) {
405                                                         /* empty regex should match empty DN */
406                                                         a->acl_dn_style = ACL_STYLE_BASE;
407                                                         ber_str2bv( right, 0, 1, &a->acl_dn_pat );
408
409                                                 } else if ( strcmp(right, "*") == 0 
410                                                         || strcmp(right, ".*") == 0 
411                                                         || strcmp(right, ".*$") == 0 
412                                                         || strcmp(right, "^.*") == 0 
413                                                         || strcmp(right, "^.*$") == 0
414                                                         || strcmp(right, ".*$$") == 0 
415                                                         || strcmp(right, "^.*$$") == 0 )
416                                                 {
417                                                         ber_str2bv( "*", STRLENOF("*"), 1, &a->acl_dn_pat );
418
419                                                 } else {
420                                                         acl_regex_normalized_dn( right, &a->acl_dn_pat );
421                                                 }
422
423                                         } else {
424                                                 fprintf( stderr, "%s: line %d: "
425                                                         "unknown dn style \"%s\" in to clause\n",
426                                                     fname, lineno, style );
427                                                 acl_usage();
428                                         }
429
430                                         continue;
431                                 }
432
433                                 if ( strcasecmp( left, "filter" ) == 0 ) {
434                                         if ( (a->acl_filter = str2filter( right )) == NULL ) {
435                                                 fprintf( stderr,
436                                 "%s: line %d: bad filter \"%s\" in to clause\n",
437                                                     fname, lineno, right );
438                                                 acl_usage();
439                                         }
440
441                                 } else if ( strcasecmp( left, "attr" ) == 0
442                                                 || strcasecmp( left, "attrs" ) == 0 ) {
443                                         a->acl_attrs = str2anlist( a->acl_attrs,
444                                                 right, "," );
445                                         if ( a->acl_attrs == NULL ) {
446                                                 fprintf( stderr,
447                                 "%s: line %d: unknown attr \"%s\" in to clause\n",
448                                                     fname, lineno, right );
449                                                 acl_usage();
450                                         }
451
452                                 } else if ( strncasecmp( left, "val", 3 ) == 0 ) {
453                                         if ( !BER_BVISEMPTY( &a->acl_attrval ) ) {
454                                                 fprintf( stderr,
455                                 "%s: line %d: attr val already specified in to clause.\n",
456                                                         fname, lineno );
457                                                 acl_usage();
458                                         }
459                                         if ( a->acl_attrs == NULL || !BER_BVISEMPTY( &a->acl_attrs[1].an_name ) )
460                                         {
461                                                 fprintf( stderr,
462                                 "%s: line %d: attr val requires a single attribute.\n",
463                                                         fname, lineno );
464                                                 acl_usage();
465                                         }
466                                         ber_str2bv( right, 0, 1, &a->acl_attrval );
467                                         if ( style && strcasecmp( style, "regex" ) == 0 ) {
468                                                 int e = regcomp( &a->acl_attrval_re, a->acl_attrval.bv_val,
469                                                         REG_EXTENDED | REG_ICASE | REG_NOSUB );
470                                                 if ( e ) {
471                                                         char buf[512];
472                                                         regerror( e, &a->acl_attrval_re, buf, sizeof(buf) );
473                                                         fprintf( stderr, "%s: line %d: "
474                                                                 "regular expression \"%s\" bad because of %s\n",
475                                                                 fname, lineno, right, buf );
476                                                         acl_usage();
477                                                 }
478                                                 a->acl_attrval_style = ACL_STYLE_REGEX;
479                                         } else {
480                                                 /* FIXME: if the attribute has DN syntax, we might
481                                                  * allow one, subtree and children styles as well */
482                                                 if ( !strcasecmp( style, "exact" ) ) {
483                                                         a->acl_attrval_style = ACL_STYLE_BASE;
484
485                                                 } else if ( a->acl_attrs[0].an_desc->ad_type->
486                                                         sat_syntax == slap_schema.si_syn_distinguishedName )
487                                                 {
488                                                         if ( !strcasecmp( style, "baseObject" ) ||
489                                                                 !strcasecmp( style, "base" ) )
490                                                         {
491                                                                 a->acl_attrval_style = ACL_STYLE_BASE;
492                                                         } else if ( !strcasecmp( style, "onelevel" ) ||
493                                                                 !strcasecmp( style, "one" ) )
494                                                         {
495                                                                 a->acl_attrval_style = ACL_STYLE_ONE;
496                                                         } else if ( !strcasecmp( style, "subtree" ) ||
497                                                                 !strcasecmp( style, "sub" ) )
498                                                         {
499                                                                 a->acl_attrval_style = ACL_STYLE_SUBTREE;
500                                                         } else if ( !strcasecmp( style, "children" ) ) {
501                                                                 a->acl_attrval_style = ACL_STYLE_CHILDREN;
502                                                         } else {
503                                                                 fprintf( stderr, 
504                                                                         "%s: line %d: unknown val.<style> \"%s\" "
505                                                                         "for attributeType \"%s\" with DN syntax; "
506                                                                         "using \"base\"\n",
507                                                                         fname, lineno, style,
508                                                                         a->acl_attrs[0].an_desc->ad_cname.bv_val );
509                                                                 a->acl_attrval_style = ACL_STYLE_BASE;
510                                                         }
511
512                                                 } else {
513                                                         fprintf( stderr, 
514                                                                 "%s: line %d: unknown val.<style> \"%s\" "
515                                                                 "for attributeType \"%s\"; using \"exact\"\n",
516                                                                 fname, lineno, style,
517                                                                 a->acl_attrs[0].an_desc->ad_cname.bv_val );
518                                                         a->acl_attrval_style = ACL_STYLE_BASE;
519                                                 }
520                                         }
521                                         
522                                 } else {
523                                         fprintf( stderr,
524                                                 "%s: line %d: expecting <what> got \"%s\"\n",
525                                             fname, lineno, left );
526                                         acl_usage();
527                                 }
528                         }
529
530                         if ( !BER_BVISNULL( &a->acl_dn_pat ) && 
531                                         ber_bvccmp( &a->acl_dn_pat, '*' ) )
532                         {
533                                 free( a->acl_dn_pat.bv_val );
534                                 BER_BVZERO( &a->acl_dn_pat );
535                         }
536                         
537                         if ( !BER_BVISEMPTY( &a->acl_dn_pat ) ||
538                                         a->acl_dn_style != ACL_STYLE_REGEX ) 
539                         {
540                                 if ( a->acl_dn_style != ACL_STYLE_REGEX ) {
541                                         struct berval bv;
542                                         rc = dnNormalize( 0, NULL, NULL, &a->acl_dn_pat, &bv, NULL);
543                                         if ( rc != LDAP_SUCCESS ) {
544                                                 fprintf( stderr,
545                                                         "%s: line %d: bad DN \"%s\" in to DN clause\n",
546                                                         fname, lineno, a->acl_dn_pat.bv_val );
547                                                 acl_usage();
548                                         }
549                                         free( a->acl_dn_pat.bv_val );
550                                         a->acl_dn_pat = bv;
551
552                                 } else {
553                                         int e = regcomp( &a->acl_dn_re, a->acl_dn_pat.bv_val,
554                                                 REG_EXTENDED | REG_ICASE );
555                                         if ( e ) {
556                                                 char buf[512];
557                                                 regerror( e, &a->acl_dn_re, buf, sizeof(buf) );
558                                                 fprintf( stderr, "%s: line %d: "
559                                                         "regular expression \"%s\" bad because of %s\n",
560                                                         fname, lineno, right, buf );
561                                                 acl_usage();
562                                         }
563                                 }
564                         }
565
566                 /* by clause - select who has what access to entries */
567                 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
568                         if ( a == NULL ) {
569                                 fprintf( stderr, "%s: line %d: "
570                                         "to clause required before by clause in access line\n",
571                                     fname, lineno );
572                                 acl_usage();
573                         }
574
575                         /*
576                          * by clause consists of <who> and <access>
577                          */
578
579                         b = (Access *) ch_calloc( 1, sizeof(Access) );
580
581                         ACL_INVALIDATE( b->a_access_mask );
582
583                         if ( ++i == argc ) {
584                                 fprintf( stderr,
585                             "%s: line %d: premature eol: expecting <who>\n",
586                                     fname, lineno );
587                                 acl_usage();
588                         }
589
590                         /* get <who> */
591                         for ( ; i < argc; i++ ) {
592                                 slap_style_t    sty = ACL_STYLE_REGEX;
593                                 char            *style_modifier = NULL;
594                                 char            *style_level = NULL;
595                                 int             level = 0;
596                                 int             expand = 0;
597                                 slap_dn_access  *bdn = &b->a_dn;
598                                 int             is_realdn = 0;
599
600                                 split( argv[i], '=', &left, &right );
601                                 split( left, '.', &left, &style );
602                                 if ( style ) {
603                                         split( style, ',', &style, &style_modifier );
604
605                                         if ( strncasecmp( style, "level", STRLENOF( "level" ) ) == 0 ) {
606                                                 split( style, '{', &style, &style_level );
607                                                 if ( style_level != NULL ) {
608                                                         char *p = strchr( style_level, '}' );
609                                                         if ( p == NULL ) {
610                                                                 fprintf( stderr,
611                                                                         "%s: line %d: premature eol: "
612                                                                         "expecting closing '}' in \"level{n}\"\n",
613                                                                         fname, lineno );
614                                                                 acl_usage();
615                                                         } else if ( p == style_level ) {
616                                                                 fprintf( stderr,
617                                                                         "%s: line %d: empty level "
618                                                                         "in \"level{n}\"\n",
619                                                                         fname, lineno );
620                                                                 acl_usage();
621                                                         }
622                                                         p[0] = '\0';
623                                                 }
624                                         }
625                                 }
626
627                                 if ( style == NULL || *style == '\0' ||
628                                         strcasecmp( style, "exact" ) == 0 ||
629                                         strcasecmp( style, "baseObject" ) == 0 ||
630                                         strcasecmp( style, "base" ) == 0 )
631                                 {
632                                         sty = ACL_STYLE_BASE;
633
634                                 } else if ( strcasecmp( style, "onelevel" ) == 0 ||
635                                         strcasecmp( style, "one" ) == 0 )
636                                 {
637                                         sty = ACL_STYLE_ONE;
638
639                                 } else if ( strcasecmp( style, "subtree" ) == 0 ||
640                                         strcasecmp( style, "sub" ) == 0 )
641                                 {
642                                         sty = ACL_STYLE_SUBTREE;
643
644                                 } else if ( strcasecmp( style, "children" ) == 0 ) {
645                                         sty = ACL_STYLE_CHILDREN;
646
647                                 } else if ( strcasecmp( style, "level" ) == 0 )
648                                 {
649                                         char    *next;
650
651                                         level = strtol( style_level, &next, 10 );
652                                         if ( next[0] != '\0' ) {
653                                                 fprintf( stderr,
654                                                         "%s: line %d: unable to parse level "
655                                                         "in \"level{n}\"\n",
656                                                         fname, lineno );
657                                                 acl_usage();
658                                         }
659
660                                         sty = ACL_STYLE_LEVEL;
661
662                                 } else if ( strcasecmp( style, "regex" ) == 0 ) {
663                                         sty = ACL_STYLE_REGEX;
664
665                                 } else if ( strcasecmp( style, "expand" ) == 0 ) {
666                                         sty = ACL_STYLE_EXPAND;
667
668                                 } else if ( strcasecmp( style, "ip" ) == 0 ) {
669                                         sty = ACL_STYLE_IP;
670
671                                 } else if ( strcasecmp( style, "path" ) == 0 ) {
672                                         sty = ACL_STYLE_PATH;
673 #ifndef LDAP_PF_LOCAL
674                                         fprintf( stderr, "%s: line %d: "
675                                                 "path style modifier is useless without local\n",
676                                                 fname, lineno );
677 #endif /* LDAP_PF_LOCAL */
678
679                                 } else {
680                                         fprintf( stderr,
681                                                 "%s: line %d: unknown style \"%s\" in by clause\n",
682                                             fname, lineno, style );
683                                         acl_usage();
684                                 }
685
686                                 if ( style_modifier &&
687                                         strcasecmp( style_modifier, "expand" ) == 0 )
688                                 {
689                                         switch ( sty ) {
690                                         case ACL_STYLE_REGEX:
691                                                 fprintf( stderr, "%s: line %d: "
692                                                         "\"regex\" style implies "
693                                                         "\"expand\" modifier (ignored)\n",
694                                                         fname, lineno );
695                                                 break;
696
697                                         case ACL_STYLE_EXPAND:
698 #if 0
699                                                 /* FIXME: now it's legal... */
700                                                 fprintf( stderr, "%s: line %d: "
701                                                         "\"expand\" style used "
702                                                         "in conjunction with "
703                                                         "\"expand\" modifier (ignored)\n",
704                                                         fname, lineno );
705 #endif
706                                                 break;
707
708                                         default:
709                                                 /* we'll see later if it's pertinent */
710                                                 expand = 1;
711                                                 break;
712                                         }
713                                 }
714
715                                 /* expand in <who> needs regex in <what> */
716                                 if ( ( sty == ACL_STYLE_EXPAND || expand )
717                                                 && a->acl_dn_style != ACL_STYLE_REGEX )
718                                 {
719                                         fprintf( stderr, "%s: line %d: "
720                                                 "\"expand\" style or modifier used "
721                                                 "in conjunction with "
722                                                 "a non-regex <what> clause\n",
723                                                 fname, lineno );
724                                 }
725
726                                 if ( strncasecmp( left, "real", STRLENOF( "real" ) ) == 0 ) {
727                                         is_realdn = 1;
728                                         bdn = &b->a_realdn;
729                                         left += STRLENOF( "real" );
730                                 }
731
732                                 if ( strcasecmp( left, "*" ) == 0 ) {
733                                         if ( is_realdn ) {
734                                                 acl_usage();
735                                         }
736
737                                         ber_str2bv( "*", STRLENOF( "*" ), 1, &bv );
738                                         sty = ACL_STYLE_REGEX;
739
740                                 } else if ( strcasecmp( left, "anonymous" ) == 0 ) {
741                                         ber_str2bv("anonymous", STRLENOF( "anonymous" ), 1, &bv);
742                                         sty = ACL_STYLE_ANONYMOUS;
743
744                                 } else if ( strcasecmp( left, "users" ) == 0 ) {
745                                         ber_str2bv("users", STRLENOF( "users" ), 1, &bv);
746                                         sty = ACL_STYLE_USERS;
747
748                                 } else if ( strcasecmp( left, "self" ) == 0 ) {
749                                         ber_str2bv("self", STRLENOF( "self" ), 1, &bv);
750                                         sty = ACL_STYLE_SELF;
751
752                                 } else if ( strcasecmp( left, "dn" ) == 0 ) {
753                                         if ( sty == ACL_STYLE_REGEX ) {
754                                                 bdn->a_style = ACL_STYLE_REGEX;
755                                                 if ( right == NULL ) {
756                                                         /* no '=' */
757                                                         ber_str2bv("users",
758                                                                 STRLENOF( "users" ),
759                                                                 1, &bv);
760                                                         bdn->a_style = ACL_STYLE_USERS;
761
762                                                 } else if (*right == '\0' ) {
763                                                         /* dn="" */
764                                                         ber_str2bv("anonymous",
765                                                                 STRLENOF( "anonymous" ),
766                                                                 1, &bv);
767                                                         bdn->a_style = ACL_STYLE_ANONYMOUS;
768
769                                                 } else if ( strcmp( right, "*" ) == 0 ) {
770                                                         /* dn=* */
771                                                         /* any or users?  users for now */
772                                                         ber_str2bv("users",
773                                                                 STRLENOF( "users" ),
774                                                                 1, &bv);
775                                                         bdn->a_style = ACL_STYLE_USERS;
776
777                                                 } else if ( strcmp( right, ".+" ) == 0
778                                                         || strcmp( right, "^.+" ) == 0
779                                                         || strcmp( right, ".+$" ) == 0
780                                                         || strcmp( right, "^.+$" ) == 0
781                                                         || strcmp( right, ".+$$" ) == 0
782                                                         || strcmp( right, "^.+$$" ) == 0 )
783                                                 {
784                                                         ber_str2bv("users",
785                                                                 STRLENOF( "users" ),
786                                                                 1, &bv);
787                                                         bdn->a_style = ACL_STYLE_USERS;
788
789                                                 } else if ( strcmp( right, ".*" ) == 0
790                                                         || strcmp( right, "^.*" ) == 0
791                                                         || strcmp( right, ".*$" ) == 0
792                                                         || strcmp( right, "^.*$" ) == 0
793                                                         || strcmp( right, ".*$$" ) == 0
794                                                         || strcmp( right, "^.*$$" ) == 0 )
795                                                 {
796                                                         ber_str2bv("*",
797                                                                 STRLENOF( "*" ),
798                                                                 1, &bv);
799
800                                                 } else {
801                                                         acl_regex_normalized_dn( right, &bv );
802                                                         if ( !ber_bvccmp( &bv, '*' ) ) {
803                                                                 regtest( fname, lineno, bv.bv_val );
804                                                         }
805                                                 }
806
807                                         } else if ( right == NULL || *right == '\0' ) {
808                                                 fprintf( stderr, "%s: line %d: "
809                                                         "missing \"=\" in (or value after) \"%s\" "
810                                                         "in by clause\n",
811                                                     fname, lineno, left );
812                                                 acl_usage();
813
814                                         } else {
815                                                 ber_str2bv( right, 0, 1, &bv );
816                                         }
817
818                                 } else {
819                                         BER_BVZERO( &bv );
820                                 }
821
822                                 if ( !BER_BVISNULL( &bv ) ) {
823                                         if ( !BER_BVISEMPTY( &bdn->a_pat ) ) {
824                                                 fprintf( stderr,
825                                                     "%s: line %d: dn pattern already specified.\n",
826                                                     fname, lineno );
827                                                 acl_usage();
828                                         }
829
830                                         if ( sty != ACL_STYLE_REGEX &&
831                                                         sty != ACL_STYLE_ANONYMOUS &&
832                                                         sty != ACL_STYLE_USERS &&
833                                                         sty != ACL_STYLE_SELF &&
834                                                         expand == 0 )
835                                         {
836                                                 rc = dnNormalize(0, NULL, NULL,
837                                                         &bv, &bdn->a_pat, NULL);
838                                                 if ( rc != LDAP_SUCCESS ) {
839                                                         fprintf( stderr,
840                                                                 "%s: line %d: bad DN \"%s\" in by DN clause\n",
841                                                                 fname, lineno, bv.bv_val );
842                                                         acl_usage();
843                                                 }
844                                                 free( bv.bv_val );
845
846                                         } else {
847                                                 bdn->a_pat = bv;
848                                         }
849                                         bdn->a_style = sty;
850                                         bdn->a_expand = expand;
851                                         if ( sty == ACL_STYLE_SELF ) {
852                                                 bdn->a_self_level = level;
853
854                                         } else {
855                                                 if ( level < 0 ) {
856                                                         fprintf( stderr,
857                                                                 "%s: line %d: bad negative level \"%d\" "
858                                                                 "in by DN clause\n",
859                                                                 fname, lineno, level );
860                                                         acl_usage();
861                                                 } else if ( level == 1 ) {
862                                                         fprintf( stderr,
863                                                                 "%s: line %d: \"onelevel\" should be used "
864                                                                 "instead of \"level{1}\" in by DN clause\n",
865                                                                 fname, lineno, 0 );
866                                                 } else if ( level == 0 && sty == ACL_STYLE_LEVEL ) {
867                                                         fprintf( stderr,
868                                                                 "%s: line %d: \"base\" should be used "
869                                                                 "instead of \"level{0}\" in by DN clause\n",
870                                                                 fname, lineno, 0 );
871                                                 }
872
873                                                 bdn->a_level = level;
874                                         }
875                                         continue;
876                                 }
877
878                                 if ( strcasecmp( left, "dnattr" ) == 0 ) {
879                                         if ( right == NULL || right[0] == '\0' ) {
880                                                 fprintf( stderr, "%s: line %d: "
881                                                         "missing \"=\" in (or value after) \"%s\" "
882                                                         "in by clause\n",
883                                                         fname, lineno, left );
884                                                 acl_usage();
885                                         }
886
887                                         if( bdn->a_at != NULL ) {
888                                                 fprintf( stderr,
889                                                         "%s: line %d: dnattr already specified.\n",
890                                                         fname, lineno );
891                                                 acl_usage();
892                                         }
893
894                                         rc = slap_str2ad( right, &bdn->a_at, &text );
895
896                                         if( rc != LDAP_SUCCESS ) {
897                                                 fprintf( stderr,
898                                                         "%s: line %d: dnattr \"%s\": %s\n",
899                                                         fname, lineno, right, text );
900                                                 acl_usage();
901                                         }
902
903
904                                         if( !is_at_syntax( bdn->a_at->ad_type,
905                                                 SLAPD_DN_SYNTAX ) &&
906                                                 !is_at_syntax( bdn->a_at->ad_type,
907                                                 SLAPD_NAMEUID_SYNTAX ))
908                                         {
909                                                 fprintf( stderr,
910                                                         "%s: line %d: dnattr \"%s\": "
911                                                         "inappropriate syntax: %s\n",
912                                                         fname, lineno, right,
913                                                         bdn->a_at->ad_type->sat_syntax_oid );
914                                                 acl_usage();
915                                         }
916
917                                         if( bdn->a_at->ad_type->sat_equality == NULL ) {
918                                                 fprintf( stderr,
919                                                         "%s: line %d: dnattr \"%s\": "
920                                                         "inappropriate matching (no EQUALITY)\n",
921                                                         fname, lineno, right );
922                                                 acl_usage();
923                                         }
924
925                                         continue;
926                                 }
927
928                                 if ( strncasecmp( left, "group", STRLENOF( "group" ) ) == 0 ) {
929                                         char *name = NULL;
930                                         char *value = NULL;
931
932                                         switch ( sty ) {
933                                         case ACL_STYLE_REGEX:
934                                                 /* legacy, tolerated */
935                                                 fprintf( stderr, "%s: line %d: "
936                                                         "deprecated group style \"regex\"; "
937                                                         "use \"expand\" instead\n",
938                                                         fname, lineno, style );
939                                                 sty = ACL_STYLE_EXPAND;
940                                                 break;
941
942                                         case ACL_STYLE_BASE:
943                                                 /* legal, traditional */
944                                         case ACL_STYLE_EXPAND:
945                                                 /* legal, substring expansion; supersedes regex */
946                                                 break;
947
948                                         default:
949                                                 /* unknown */
950                                                 fprintf( stderr, "%s: line %d: "
951                                                         "inappropriate style \"%s\" in by clause\n",
952                                                         fname, lineno, style );
953                                                 acl_usage();
954                                         }
955
956                                         if ( right == NULL || right[0] == '\0' ) {
957                                                 fprintf( stderr, "%s: line %d: "
958                                                         "missing \"=\" in (or value after) \"%s\" "
959                                                         "in by clause\n",
960                                                         fname, lineno, left );
961                                                 acl_usage();
962                                         }
963
964                                         if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
965                                                 fprintf( stderr,
966                                                         "%s: line %d: group pattern already specified.\n",
967                                                         fname, lineno );
968                                                 acl_usage();
969                                         }
970
971                                         /* format of string is
972                                                 "group/objectClassValue/groupAttrName" */
973                                         if ( ( value = strchr(left, '/') ) != NULL ) {
974                                                 *value++ = '\0';
975                                                 if ( *value && ( name = strchr( value, '/' ) ) != NULL ) {
976                                                         *name++ = '\0';
977                                                 }
978                                         }
979
980                                         b->a_group_style = sty;
981                                         if ( sty == ACL_STYLE_EXPAND ) {
982                                                 acl_regex_normalized_dn( right, &bv );
983                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
984                                                         regtest( fname, lineno, bv.bv_val );
985                                                 }
986                                                 b->a_group_pat = bv;
987
988                                         } else {
989                                                 ber_str2bv( right, 0, 0, &bv );
990                                                 rc = dnNormalize( 0, NULL, NULL, &bv,
991                                                         &b->a_group_pat, NULL );
992                                                 if ( rc != LDAP_SUCCESS ) {
993                                                         fprintf( stderr,
994                                                                 "%s: line %d: bad DN \"%s\"\n",
995                                                                 fname, lineno, right );
996                                                         acl_usage();
997                                                 }
998                                         }
999
1000                                         if ( value && *value ) {
1001                                                 b->a_group_oc = oc_find( value );
1002                                                 *--value = '/';
1003
1004                                                 if ( b->a_group_oc == NULL ) {
1005                                                         fprintf( stderr,
1006                                                                 "%s: line %d: group objectclass "
1007                                                                 "\"%s\" unknown\n",
1008                                                                 fname, lineno, value );
1009                                                         acl_usage();
1010                                                 }
1011
1012                                         } else {
1013                                                 b->a_group_oc = oc_find(SLAPD_GROUP_CLASS);
1014
1015                                                 if( b->a_group_oc == NULL ) {
1016                                                         fprintf( stderr,
1017                                                                 "%s: line %d: group default objectclass "
1018                                                                 "\"%s\" unknown\n",
1019                                                                 fname, lineno, SLAPD_GROUP_CLASS );
1020                                                         acl_usage();
1021                                                 }
1022                                         }
1023
1024                                         if ( is_object_subclass( slap_schema.si_oc_referral,
1025                                                 b->a_group_oc ) )
1026                                         {
1027                                                 fprintf( stderr,
1028                                                         "%s: line %d: group objectclass \"%s\" "
1029                                                         "is subclass of referral\n",
1030                                                         fname, lineno, value );
1031                                                 acl_usage();
1032                                         }
1033
1034                                         if ( is_object_subclass( slap_schema.si_oc_alias,
1035                                                 b->a_group_oc ) )
1036                                         {
1037                                                 fprintf( stderr,
1038                                                         "%s: line %d: group objectclass \"%s\" "
1039                                                         "is subclass of alias\n",
1040                                                         fname, lineno, value );
1041                                                 acl_usage();
1042                                         }
1043
1044                                         if ( name && *name ) {
1045                                                 rc = slap_str2ad( name, &b->a_group_at, &text );
1046
1047                                                 if( rc != LDAP_SUCCESS ) {
1048                                                         fprintf( stderr,
1049                                                                 "%s: line %d: group \"%s\": %s\n",
1050                                                                 fname, lineno, right, text );
1051                                                         acl_usage();
1052                                                 }
1053                                                 *--name = '/';
1054
1055                                         } else {
1056                                                 rc = slap_str2ad( SLAPD_GROUP_ATTR, &b->a_group_at, &text );
1057
1058                                                 if ( rc != LDAP_SUCCESS ) {
1059                                                         fprintf( stderr,
1060                                                                 "%s: line %d: group \"%s\": %s\n",
1061                                                                 fname, lineno, SLAPD_GROUP_ATTR, text );
1062                                                         acl_usage();
1063                                                 }
1064                                         }
1065
1066                                         if ( !is_at_syntax( b->a_group_at->ad_type,
1067                                                 SLAPD_DN_SYNTAX ) &&
1068                                                 !is_at_syntax( b->a_group_at->ad_type,
1069                                                 SLAPD_NAMEUID_SYNTAX ) &&
1070                                                 !is_at_subtype( b->a_group_at->ad_type, slap_schema.si_ad_labeledURI->ad_type ) )
1071                                         {
1072                                                 fprintf( stderr,
1073                                                         "%s: line %d: group \"%s\": inappropriate syntax: %s\n",
1074                                                         fname, lineno, right,
1075                                                         b->a_group_at->ad_type->sat_syntax_oid );
1076                                                 acl_usage();
1077                                         }
1078
1079
1080                                         {
1081                                                 int rc;
1082                                                 struct berval vals[2];
1083
1084                                                 ber_str2bv( b->a_group_oc->soc_oid, 0, 0, &vals[0] );
1085                                                 BER_BVZERO( &vals[1] );
1086
1087                                                 rc = oc_check_allowed( b->a_group_at->ad_type,
1088                                                         vals, NULL );
1089
1090                                                 if( rc != 0 ) {
1091                                                         fprintf( stderr, "%s: line %d: "
1092                                                                 "group: \"%s\" not allowed by \"%s\"\n",
1093                                                                 fname, lineno,
1094                                                                 b->a_group_at->ad_cname.bv_val,
1095                                                                 b->a_group_oc->soc_oid );
1096                                                         acl_usage();
1097                                                 }
1098                                         }
1099                                         continue;
1100                                 }
1101
1102                                 if ( strcasecmp( left, "peername" ) == 0 ) {
1103                                         switch ( sty ) {
1104                                         case ACL_STYLE_REGEX:
1105                                         case ACL_STYLE_BASE:
1106                                                 /* legal, traditional */
1107                                         case ACL_STYLE_EXPAND:
1108                                                 /* cheap replacement to regex for simple expansion */
1109                                         case ACL_STYLE_IP:
1110                                         case ACL_STYLE_PATH:
1111                                                 /* legal, peername specific */
1112                                                 break;
1113
1114                                         default:
1115                                                 fprintf( stderr, "%s: line %d: "
1116                                                         "inappropriate style \"%s\" in by clause\n",
1117                                                     fname, lineno, style );
1118                                                 acl_usage();
1119                                         }
1120
1121                                         if ( right == NULL || right[0] == '\0' ) {
1122                                                 fprintf( stderr, "%s: line %d: "
1123                                                         "missing \"=\" in (or value after) \"%s\" "
1124                                                         "in by clause\n",
1125                                                         fname, lineno, left );
1126                                                 acl_usage();
1127                                         }
1128
1129                                         if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1130                                                 fprintf( stderr, "%s: line %d: "
1131                                                         "peername pattern already specified.\n",
1132                                                         fname, lineno );
1133                                                 acl_usage();
1134                                         }
1135
1136                                         b->a_peername_style = sty;
1137                                         if ( sty == ACL_STYLE_REGEX ) {
1138                                                 acl_regex_normalized_dn( right, &bv );
1139                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1140                                                         regtest( fname, lineno, bv.bv_val );
1141                                                 }
1142                                                 b->a_peername_pat = bv;
1143
1144                                         } else {
1145                                                 ber_str2bv( right, 0, 1, &b->a_peername_pat );
1146
1147                                                 if ( sty == ACL_STYLE_IP ) {
1148                                                         char            *addr = NULL,
1149                                                                         *mask = NULL,
1150                                                                         *port = NULL;
1151
1152                                                         split( right, '{', &addr, &port );
1153                                                         split( addr, '%', &addr, &mask );
1154
1155                                                         b->a_peername_addr = inet_addr( addr );
1156                                                         if ( b->a_peername_addr == (unsigned long)(-1) ) {
1157                                                                 /* illegal address */
1158                                                                 fprintf( stderr, "%s: line %d: "
1159                                                                         "illegal peername address \"%s\".\n",
1160                                                                         fname, lineno, addr );
1161                                                                 acl_usage();
1162                                                         }
1163
1164                                                         b->a_peername_mask = (unsigned long)(-1);
1165                                                         if ( mask != NULL ) {
1166                                                                 b->a_peername_mask = inet_addr( mask );
1167                                                                 if ( b->a_peername_mask ==
1168                                                                         (unsigned long)(-1) )
1169                                                                 {
1170                                                                         /* illegal mask */
1171                                                                         fprintf( stderr, "%s: line %d: "
1172                                                                                 "illegal peername address mask "
1173                                                                                 "\"%s\".\n",
1174                                                                                 fname, lineno, mask );
1175                                                                         acl_usage();
1176                                                                 }
1177                                                         } 
1178
1179                                                         b->a_peername_port = -1;
1180                                                         if ( port ) {
1181                                                                 char    *end = NULL;
1182
1183                                                                 b->a_peername_port = strtol( port, &end, 10 );
1184                                                                 if ( end[0] != '}' ) {
1185                                                                         /* illegal port */
1186                                                                         fprintf( stderr, "%s: line %d: "
1187                                                                                 "illegal peername port specification "
1188                                                                                 "\"{%s}\".\n",
1189                                                                                 fname, lineno, port );
1190                                                                         acl_usage();
1191                                                                 }
1192                                                         }
1193                                                 }
1194                                         }
1195                                         continue;
1196                                 }
1197
1198                                 if ( strcasecmp( left, "sockname" ) == 0 ) {
1199                                         switch ( sty ) {
1200                                         case ACL_STYLE_REGEX:
1201                                         case ACL_STYLE_BASE:
1202                                                 /* legal, traditional */
1203                                         case ACL_STYLE_EXPAND:
1204                                                 /* cheap replacement to regex for simple expansion */
1205                                                 break;
1206
1207                                         default:
1208                                                 /* unknown */
1209                                                 fprintf( stderr, "%s: line %d: "
1210                                                         "inappropriate style \"%s\" in by clause\n",
1211                                                     fname, lineno, style );
1212                                                 acl_usage();
1213                                         }
1214
1215                                         if ( right == NULL || right[0] == '\0' ) {
1216                                                 fprintf( stderr, "%s: line %d: "
1217                                                         "missing \"=\" in (or value after) \"%s\" "
1218                                                         "in by clause\n",
1219                                                         fname, lineno, left );
1220                                                 acl_usage();
1221                                         }
1222
1223                                         if ( !BER_BVISNULL( &b->a_sockname_pat ) ) {
1224                                                 fprintf( stderr, "%s: line %d: "
1225                                                         "sockname pattern already specified.\n",
1226                                                         fname, lineno );
1227                                                 acl_usage();
1228                                         }
1229
1230                                         b->a_sockname_style = sty;
1231                                         if ( sty == ACL_STYLE_REGEX ) {
1232                                                 acl_regex_normalized_dn( right, &bv );
1233                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1234                                                         regtest( fname, lineno, bv.bv_val );
1235                                                 }
1236                                                 b->a_sockname_pat = bv;
1237                                                 
1238                                         } else {
1239                                                 ber_str2bv( right, 0, 1, &b->a_sockname_pat );
1240                                         }
1241                                         continue;
1242                                 }
1243
1244                                 if ( strcasecmp( left, "domain" ) == 0 ) {
1245                                         switch ( sty ) {
1246                                         case ACL_STYLE_REGEX:
1247                                         case ACL_STYLE_BASE:
1248                                         case ACL_STYLE_SUBTREE:
1249                                                 /* legal, traditional */
1250                                                 break;
1251
1252                                         case ACL_STYLE_EXPAND:
1253                                                 /* tolerated: means exact,expand */
1254                                                 if ( expand ) {
1255                                                         fprintf( stderr,
1256                                                                 "%s: line %d: "
1257                                                                 "\"expand\" modifier "
1258                                                                 "with \"expand\" style\n",
1259                                                                 fname, lineno );
1260                                                 }
1261                                                 sty = ACL_STYLE_BASE;
1262                                                 expand = 1;
1263                                                 break;
1264
1265                                         default:
1266                                                 /* unknown */
1267                                                 fprintf( stderr, "%s: line %d: "
1268                                                         "inappropriate style \"%s\" in by clause\n",
1269                                                     fname, lineno, style );
1270                                                 acl_usage();
1271                                         }
1272
1273                                         if ( right == NULL || right[0] == '\0' ) {
1274                                                 fprintf( stderr, "%s: line %d: "
1275                                                         "missing \"=\" in (or value after) \"%s\" "
1276                                                         "in by clause\n",
1277                                                         fname, lineno, left );
1278                                                 acl_usage();
1279                                         }
1280
1281                                         if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1282                                                 fprintf( stderr,
1283                                                         "%s: line %d: domain pattern already specified.\n",
1284                                                         fname, lineno );
1285                                                 acl_usage();
1286                                         }
1287
1288                                         b->a_domain_style = sty;
1289                                         b->a_domain_expand = expand;
1290                                         if ( sty == ACL_STYLE_REGEX ) {
1291                                                 acl_regex_normalized_dn( right, &bv );
1292                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1293                                                         regtest( fname, lineno, bv.bv_val );
1294                                                 }
1295                                                 b->a_domain_pat = bv;
1296
1297                                         } else {
1298                                                 ber_str2bv( right, 0, 1, &b->a_domain_pat );
1299                                         }
1300                                         continue;
1301                                 }
1302
1303                                 if ( strcasecmp( left, "sockurl" ) == 0 ) {
1304                                         switch ( sty ) {
1305                                         case ACL_STYLE_REGEX:
1306                                         case ACL_STYLE_BASE:
1307                                                 /* legal, traditional */
1308                                         case ACL_STYLE_EXPAND:
1309                                                 /* cheap replacement to regex for simple expansion */
1310                                                 break;
1311
1312                                         default:
1313                                                 /* unknown */
1314                                                 fprintf( stderr, "%s: line %d: "
1315                                                         "inappropriate style \"%s\" in by clause\n",
1316                                                     fname, lineno, style );
1317                                                 acl_usage();
1318                                         }
1319
1320                                         if ( right == NULL || right[0] == '\0' ) {
1321                                                 fprintf( stderr, "%s: line %d: "
1322                                                         "missing \"=\" in (or value after) \"%s\" "
1323                                                         "in by clause\n",
1324                                                         fname, lineno, left );
1325                                                 acl_usage();
1326                                         }
1327
1328                                         if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1329                                                 fprintf( stderr,
1330                                                         "%s: line %d: sockurl pattern already specified.\n",
1331                                                         fname, lineno );
1332                                                 acl_usage();
1333                                         }
1334
1335                                         b->a_sockurl_style = sty;
1336                                         if ( sty == ACL_STYLE_REGEX ) {
1337                                                 acl_regex_normalized_dn( right, &bv );
1338                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1339                                                         regtest( fname, lineno, bv.bv_val );
1340                                                 }
1341                                                 b->a_sockurl_pat = bv;
1342                                                 
1343                                         } else {
1344                                                 ber_str2bv( right, 0, 1, &b->a_sockurl_pat );
1345                                         }
1346                                         continue;
1347                                 }
1348
1349                                 if ( strcasecmp( left, "set" ) == 0 ) {
1350                                         switch ( sty ) {
1351                                                 /* deprecated */
1352                                         case ACL_STYLE_REGEX:
1353                                                 fprintf( stderr, "%s: line %d: "
1354                                                         "deprecated set style "
1355                                                         "\"regex\" in <by> clause; "
1356                                                         "use \"expand\" instead\n",
1357                                                         fname, lineno );
1358                                                 sty = ACL_STYLE_EXPAND;
1359                                                 /* FALLTHRU */
1360                                                 
1361                                         case ACL_STYLE_BASE:
1362                                         case ACL_STYLE_EXPAND:
1363                                                 break;
1364
1365                                         default:
1366                                                 fprintf( stderr, "%s: line %d: "
1367                                                         "inappropriate style \"%s\" in by clause\n",
1368                                                         fname, lineno, style );
1369                                                 acl_usage();
1370                                         }
1371
1372                                         if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1373                                                 fprintf( stderr,
1374                                                         "%s: line %d: set attribute already specified.\n",
1375                                                         fname, lineno );
1376                                                 acl_usage();
1377                                         }
1378
1379                                         if ( right == NULL || *right == '\0' ) {
1380                                                 fprintf( stderr,
1381                                                         "%s: line %d: no set is defined\n",
1382                                                         fname, lineno );
1383                                                 acl_usage();
1384                                         }
1385
1386                                         b->a_set_style = sty;
1387                                         ber_str2bv( right, 0, 1, &b->a_set_pat );
1388
1389                                         continue;
1390                                 }
1391
1392 #ifdef SLAP_DYNACL
1393                                 {
1394                                         char            *name = NULL;
1395                                         
1396                                         if ( strcasecmp( left, "aci" ) == 0 ) {
1397                                                 name = "aci";
1398                                                 
1399                                         } else if ( strncasecmp( left, "dynacl/", STRLENOF( "dynacl/" ) ) == 0 ) {
1400                                                 name = &left[ STRLENOF( "dynacl/" ) ];
1401                                         }
1402
1403                                         if ( name ) {
1404                                                 if ( slap_dynacl_config( fname, lineno, b, name, sty, right ) ) {
1405                                                         fprintf( stderr, "%s: line %d: "
1406                                                                 "unable to configure dynacl \"%s\"\n",
1407                                                                 fname, lineno, name );
1408                                                         acl_usage();
1409                                                 }
1410
1411                                                 continue;
1412                                         }
1413                                 }
1414 #else /* ! SLAP_DYNACL */
1415
1416 #ifdef SLAPD_ACI_ENABLED
1417                                 if ( strcasecmp( left, "aci" ) == 0 ) {
1418                                         if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
1419                                                 fprintf( stderr, "%s: line %d: "
1420                                                         "inappropriate style \"%s\" in by clause\n",
1421                                                     fname, lineno, style );
1422                                                 acl_usage();
1423                                         }
1424
1425                                         if( b->a_aci_at != NULL ) {
1426                                                 fprintf( stderr,
1427                                                         "%s: line %d: aci attribute already specified.\n",
1428                                                         fname, lineno );
1429                                                 acl_usage();
1430                                         }
1431
1432                                         if ( right != NULL && *right != '\0' ) {
1433                                                 rc = slap_str2ad( right, &b->a_aci_at, &text );
1434
1435                                                 if( rc != LDAP_SUCCESS ) {
1436                                                         fprintf( stderr,
1437                                                                 "%s: line %d: aci \"%s\": %s\n",
1438                                                                 fname, lineno, right, text );
1439                                                         acl_usage();
1440                                                 }
1441
1442                                         } else {
1443                                                 b->a_aci_at = slap_schema.si_ad_aci;
1444                                         }
1445
1446                                         if( !is_at_syntax( b->a_aci_at->ad_type,
1447                                                 SLAPD_ACI_SYNTAX) )
1448                                         {
1449                                                 fprintf( stderr, "%s: line %d: "
1450                                                         "aci \"%s\": inappropriate syntax: %s\n",
1451                                                         fname, lineno, right,
1452                                                         b->a_aci_at->ad_type->sat_syntax_oid );
1453                                                 acl_usage();
1454                                         }
1455
1456                                         continue;
1457                                 }
1458 #endif /* SLAPD_ACI_ENABLED */
1459 #endif /* ! SLAP_DYNACL */
1460
1461                                 if ( strcasecmp( left, "ssf" ) == 0 ) {
1462                                         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
1463                                                 fprintf( stderr, "%s: line %d: "
1464                                                         "inappropriate style \"%s\" in by clause\n",
1465                                                     fname, lineno, style );
1466                                                 acl_usage();
1467                                         }
1468
1469                                         if ( b->a_authz.sai_ssf ) {
1470                                                 fprintf( stderr,
1471                                                         "%s: line %d: ssf attribute already specified.\n",
1472                                                         fname, lineno );
1473                                                 acl_usage();
1474                                         }
1475
1476                                         if ( right == NULL || *right == '\0' ) {
1477                                                 fprintf( stderr,
1478                                                         "%s: line %d: no ssf is defined\n",
1479                                                         fname, lineno );
1480                                                 acl_usage();
1481                                         }
1482
1483                                         b->a_authz.sai_ssf = strtol( right, &next, 10 );
1484                                         if ( next == NULL || next[0] != '\0' ) {
1485                                                 fprintf( stderr,
1486                                                         "%s: line %d: unable to parse ssf value (%s)\n",
1487                                                         fname, lineno, right );
1488                                                 acl_usage();
1489                                         }
1490
1491                                         if ( !b->a_authz.sai_ssf ) {
1492                                                 fprintf( stderr,
1493                                                         "%s: line %d: invalid ssf value (%s)\n",
1494                                                         fname, lineno, right );
1495                                                 acl_usage();
1496                                         }
1497                                         continue;
1498                                 }
1499
1500                                 if ( strcasecmp( left, "transport_ssf" ) == 0 ) {
1501                                         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
1502                                                 fprintf( stderr, "%s: line %d: "
1503                                                         "inappropriate style \"%s\" in by clause\n",
1504                                                         fname, lineno, style );
1505                                                 acl_usage();
1506                                         }
1507
1508                                         if ( b->a_authz.sai_transport_ssf ) {
1509                                                 fprintf( stderr, "%s: line %d: "
1510                                                         "transport_ssf attribute already specified.\n",
1511                                                         fname, lineno );
1512                                                 acl_usage();
1513                                         }
1514
1515                                         if ( right == NULL || *right == '\0' ) {
1516                                                 fprintf( stderr,
1517                                                         "%s: line %d: no transport_ssf is defined\n",
1518                                                         fname, lineno );
1519                                                 acl_usage();
1520                                         }
1521
1522                                         b->a_authz.sai_transport_ssf = strtol( right, &next, 10 );
1523                                         if ( next == NULL || next[0] != '\0' ) {
1524                                                 fprintf( stderr, "%s: line %d: "
1525                                                         "unable to parse transport_ssf value (%s)\n",
1526                                                         fname, lineno, right );
1527                                                 acl_usage();
1528                                         }
1529
1530                                         if ( !b->a_authz.sai_transport_ssf ) {
1531                                                 fprintf( stderr,
1532                                                         "%s: line %d: invalid transport_ssf value (%s)\n",
1533                                                         fname, lineno, right );
1534                                                 acl_usage();
1535                                         }
1536                                         continue;
1537                                 }
1538
1539                                 if ( strcasecmp( left, "tls_ssf" ) == 0 ) {
1540                                         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
1541                                                 fprintf( stderr, "%s: line %d: "
1542                                                         "inappropriate style \"%s\" in by clause\n",
1543                                                         fname, lineno, style );
1544                                                 acl_usage();
1545                                         }
1546
1547                                         if ( b->a_authz.sai_tls_ssf ) {
1548                                                 fprintf( stderr, "%s: line %d: "
1549                                                         "tls_ssf attribute already specified.\n",
1550                                                         fname, lineno );
1551                                                 acl_usage();
1552                                         }
1553
1554                                         if ( right == NULL || *right == '\0' ) {
1555                                                 fprintf( stderr,
1556                                                         "%s: line %d: no tls_ssf is defined\n",
1557                                                         fname, lineno );
1558                                                 acl_usage();
1559                                         }
1560
1561                                         b->a_authz.sai_tls_ssf = strtol( right, &next, 10 );
1562                                         if ( next == NULL || next[0] != '\0' ) {
1563                                                 fprintf( stderr, "%s: line %d: "
1564                                                         "unable to parse tls_ssf value (%s)\n",
1565                                                         fname, lineno, right );
1566                                                 acl_usage();
1567                                         }
1568
1569                                         if ( !b->a_authz.sai_tls_ssf ) {
1570                                                 fprintf( stderr,
1571                                                         "%s: line %d: invalid tls_ssf value (%s)\n",
1572                                                         fname, lineno, right );
1573                                                 acl_usage();
1574                                         }
1575                                         continue;
1576                                 }
1577
1578                                 if ( strcasecmp( left, "sasl_ssf" ) == 0 ) {
1579                                         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
1580                                                 fprintf( stderr, "%s: line %d: "
1581                                                         "inappropriate style \"%s\" in by clause\n",
1582                                                         fname, lineno, style );
1583                                                 acl_usage();
1584                                         }
1585
1586                                         if ( b->a_authz.sai_sasl_ssf ) {
1587                                                 fprintf( stderr, "%s: line %d: "
1588                                                         "sasl_ssf attribute already specified.\n",
1589                                                         fname, lineno );
1590                                                 acl_usage();
1591                                         }
1592
1593                                         if ( right == NULL || *right == '\0' ) {
1594                                                 fprintf( stderr,
1595                                                         "%s: line %d: no sasl_ssf is defined\n",
1596                                                         fname, lineno );
1597                                                 acl_usage();
1598                                         }
1599
1600                                         b->a_authz.sai_sasl_ssf = strtol( right, &next, 10 );
1601                                         if ( next == NULL || next[0] != '\0' ) {
1602                                                 fprintf( stderr, "%s: line %d: "
1603                                                         "unable to parse sasl_ssf value (%s)\n",
1604                                                         fname, lineno, right );
1605                                                 acl_usage();
1606                                         }
1607
1608                                         if ( !b->a_authz.sai_sasl_ssf ) {
1609                                                 fprintf( stderr,
1610                                                         "%s: line %d: invalid sasl_ssf value (%s)\n",
1611                                                         fname, lineno, right );
1612                                                 acl_usage();
1613                                         }
1614                                         continue;
1615                                 }
1616
1617                                 if ( right != NULL ) {
1618                                         /* unsplit */
1619                                         right[-1] = '=';
1620                                 }
1621                                 break;
1622                         }
1623
1624                         if ( i == argc || ( strcasecmp( left, "stop" ) == 0 ) ) { 
1625                                 /* out of arguments or plain stop */
1626
1627                                 ACL_PRIV_ASSIGN( b->a_access_mask, ACL_PRIV_ADDITIVE );
1628                                 b->a_type = ACL_STOP;
1629
1630                                 access_append( &a->acl_access, b );
1631                                 continue;
1632                         }
1633
1634                         if ( strcasecmp( left, "continue" ) == 0 ) {
1635                                 /* plain continue */
1636
1637                                 ACL_PRIV_ASSIGN( b->a_access_mask, ACL_PRIV_ADDITIVE );
1638                                 b->a_type = ACL_CONTINUE;
1639
1640                                 access_append( &a->acl_access, b );
1641                                 continue;
1642                         }
1643
1644                         if ( strcasecmp( left, "break" ) == 0 ) {
1645                                 /* plain continue */
1646
1647                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1648                                 b->a_type = ACL_BREAK;
1649
1650                                 access_append( &a->acl_access, b );
1651                                 continue;
1652                         }
1653
1654                         if ( strcasecmp( left, "by" ) == 0 ) {
1655                                 /* we've gone too far */
1656                                 --i;
1657                                 ACL_PRIV_ASSIGN( b->a_access_mask, ACL_PRIV_ADDITIVE );
1658                                 b->a_type = ACL_STOP;
1659
1660                                 access_append( &a->acl_access, b );
1661                                 continue;
1662                         }
1663
1664                         /* get <access> */
1665                         if ( strncasecmp( left, "self", STRLENOF( "self" ) ) == 0 ) {
1666                                 b->a_dn_self = 1;
1667                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( &left[ STRLENOF( "self" ) ] ) );
1668
1669                         } else if ( strncasecmp( left, "realself", STRLENOF( "realself" ) ) == 0 ) {
1670                                 b->a_realdn_self = 1;
1671                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( &left[ STRLENOF( "realself" ) ] ) );
1672
1673                         } else {
1674                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( left ) );
1675                         }
1676
1677                         if ( ACL_IS_INVALID( b->a_access_mask ) ) {
1678                                 fprintf( stderr,
1679                                         "%s: line %d: expecting <access> got \"%s\"\n",
1680                                         fname, lineno, left );
1681                                 acl_usage();
1682                         }
1683
1684                         b->a_type = ACL_STOP;
1685
1686                         if ( ++i == argc ) {
1687                                 /* out of arguments or plain stop */
1688                                 access_append( &a->acl_access, b );
1689                                 continue;
1690                         }
1691
1692                         if ( strcasecmp( argv[i], "continue" ) == 0 ) {
1693                                 /* plain continue */
1694                                 b->a_type = ACL_CONTINUE;
1695
1696                         } else if ( strcasecmp( argv[i], "break" ) == 0 ) {
1697                                 /* plain continue */
1698                                 b->a_type = ACL_BREAK;
1699
1700                         } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
1701                                 /* gone to far */
1702                                 i--;
1703                         }
1704
1705                         access_append( &a->acl_access, b );
1706
1707                 } else {
1708                         fprintf( stderr,
1709                                 "%s: line %d: expecting \"to\" "
1710                                 "or \"by\" got \"%s\"\n",
1711                                 fname, lineno, argv[i] );
1712                         acl_usage();
1713                 }
1714         }
1715
1716         /* if we have no real access clause, complain and do nothing */
1717         if ( a == NULL ) {
1718                 fprintf( stderr, "%s: line %d: "
1719                         "warning: no access clause(s) specified in access line\n",
1720                         fname, lineno );
1721
1722         } else {
1723 #ifdef LDAP_DEBUG
1724                 if ( ldap_debug & LDAP_DEBUG_ACL ) {
1725                         print_acl( be, a );
1726                 }
1727 #endif
1728         
1729                 if ( a->acl_access == NULL ) {
1730                         fprintf( stderr, "%s: line %d: "
1731                                 "warning: no by clause(s) specified in access line\n",
1732                                 fname, lineno );
1733                 }
1734
1735                 if ( be != NULL ) {
1736                         if ( !BER_BVISNULL( &be->be_nsuffix[ 1 ] ) ) {
1737                                 fprintf( stderr, "%s: line %d: warning: "
1738                                         "scope checking only applies to single-valued "
1739                                         "suffix databases\n",
1740                                         fname, lineno );
1741                                 /* go ahead, since checking is not authoritative */
1742                         }
1743
1744                         switch ( check_scope( be, a ) ) {
1745                         case ACL_SCOPE_UNKNOWN:
1746                                 fprintf( stderr, "%s: line %d: warning: "
1747                                         "cannot assess the validity of the ACL scope within "
1748                                         "backend naming context\n",
1749                                         fname, lineno );
1750                                 break;
1751
1752                         case ACL_SCOPE_WARN:
1753                                 fprintf( stderr, "%s: line %d: warning: "
1754                                         "ACL could be out of scope within backend naming context\n",
1755                                         fname, lineno );
1756                                 break;
1757
1758                         case ACL_SCOPE_PARTIAL:
1759                                 fprintf( stderr, "%s: line %d: warning: "
1760                                         "ACL appears to be partially out of scope within "
1761                                         "backend naming context\n",
1762                                         fname, lineno );
1763                                 break;
1764
1765                         case ACL_SCOPE_ERR:
1766                                 fprintf( stderr, "%s: line %d: warning: "
1767                                         "ACL appears to be out of scope within "
1768                                         "backend naming context\n",
1769                                         fname, lineno );
1770                                 break;
1771
1772                         default:
1773                                 break;
1774                         }
1775                         acl_append( &be->be_acl, a );
1776
1777                 } else {
1778                         acl_append( &frontendDB->be_acl, a );
1779                 }
1780         }
1781 }
1782
1783 char *
1784 accessmask2str( slap_mask_t mask, char *buf, int debug )
1785 {
1786         int     none = 1;
1787         char    *ptr = buf;
1788
1789         assert( buf != NULL );
1790
1791         if ( ACL_IS_INVALID( mask ) ) {
1792                 return "invalid";
1793         }
1794
1795         buf[0] = '\0';
1796
1797         if ( ACL_IS_LEVEL( mask ) ) {
1798                 if ( ACL_LVL_IS_NONE(mask) ) {
1799                         ptr = lutil_strcopy( ptr, "none" );
1800
1801                 } else if ( ACL_LVL_IS_DISCLOSE(mask) ) {
1802                         ptr = lutil_strcopy( ptr, "disclose" );
1803
1804                 } else if ( ACL_LVL_IS_AUTH(mask) ) {
1805                         ptr = lutil_strcopy( ptr, "auth" );
1806
1807                 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
1808                         ptr = lutil_strcopy( ptr, "compare" );
1809
1810                 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
1811                         ptr = lutil_strcopy( ptr, "search" );
1812
1813                 } else if ( ACL_LVL_IS_READ(mask) ) {
1814                         ptr = lutil_strcopy( ptr, "read" );
1815
1816                 } else if ( ACL_LVL_IS_WRITE(mask) ) {
1817                         ptr = lutil_strcopy( ptr, "write" );
1818
1819                 } else if ( ACL_LVL_IS_MANAGE(mask) ) {
1820                         ptr = lutil_strcopy( ptr, "manage" );
1821
1822                 } else {
1823                         ptr = lutil_strcopy( ptr, "unknown" );
1824                 }
1825                 
1826                 if ( !debug ) {
1827                         *ptr = '\0';
1828                         return buf;
1829                 }
1830                 *ptr++ = '(';
1831         }
1832
1833         if( ACL_IS_ADDITIVE( mask ) ) {
1834                 *ptr++ = '+';
1835
1836         } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
1837                 *ptr++ = '-';
1838
1839         } else {
1840                 *ptr++ = '=';
1841         }
1842
1843         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_MANAGE) ) {
1844                 none = 0;
1845                 *ptr++ = 'm';
1846         } 
1847
1848         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
1849                 none = 0;
1850                 *ptr++ = 'w';
1851         } 
1852
1853         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
1854                 none = 0;
1855                 *ptr++ = 'r';
1856         } 
1857
1858         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
1859                 none = 0;
1860                 *ptr++ = 's';
1861         } 
1862
1863         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
1864                 none = 0;
1865                 *ptr++ = 'c';
1866         } 
1867
1868         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
1869                 none = 0;
1870                 *ptr++ = 'x';
1871         } 
1872
1873         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_DISCLOSE) ) {
1874                 none = 0;
1875                 *ptr++ = 'd';
1876         } 
1877
1878         if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
1879                 none = 0;
1880                 *ptr++ = 'n';
1881         } 
1882
1883         if ( none ) {
1884                 ptr = buf;
1885         }
1886
1887         if ( ACL_IS_LEVEL( mask ) ) {
1888                 *ptr++ = ')';
1889         }
1890
1891         *ptr = '\0';
1892
1893         return buf;
1894 }
1895
1896 slap_mask_t
1897 str2accessmask( const char *str )
1898 {
1899         slap_mask_t     mask;
1900
1901         if( !ASCII_ALPHA(str[0]) ) {
1902                 int i;
1903
1904                 if ( str[0] == '=' ) {
1905                         ACL_INIT(mask);
1906
1907                 } else if( str[0] == '+' ) {
1908                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
1909
1910                 } else if( str[0] == '-' ) {
1911                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
1912
1913                 } else {
1914                         ACL_INVALIDATE(mask);
1915                         return mask;
1916                 }
1917
1918                 for( i=1; str[i] != '\0'; i++ ) {
1919                         if( TOLOWER((unsigned char) str[i]) == 'm' ) {
1920                                 ACL_PRIV_SET(mask, ACL_PRIV_MANAGE);
1921
1922                         } else if( TOLOWER((unsigned char) str[i]) == 'w' ) {
1923                                 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1924
1925                         } else if( TOLOWER((unsigned char) str[i]) == 'r' ) {
1926                                 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1927
1928                         } else if( TOLOWER((unsigned char) str[i]) == 's' ) {
1929                                 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1930
1931                         } else if( TOLOWER((unsigned char) str[i]) == 'c' ) {
1932                                 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1933
1934                         } else if( TOLOWER((unsigned char) str[i]) == 'x' ) {
1935                                 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1936
1937                         } else if( TOLOWER((unsigned char) str[i]) == 'd' ) {
1938                                 ACL_PRIV_SET(mask, ACL_PRIV_DISCLOSE);
1939
1940                         } else if( str[i] != '0' ) {
1941                                 ACL_INVALIDATE(mask);
1942                                 return mask;
1943                         }
1944                 }
1945
1946                 return mask;
1947         }
1948
1949         if ( strcasecmp( str, "none" ) == 0 ) {
1950                 ACL_LVL_ASSIGN_NONE(mask);
1951
1952         } else if ( strcasecmp( str, "disclose" ) == 0 ) {
1953                 ACL_LVL_ASSIGN_DISCLOSE(mask);
1954
1955         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1956                 ACL_LVL_ASSIGN_AUTH(mask);
1957
1958         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1959                 ACL_LVL_ASSIGN_COMPARE(mask);
1960
1961         } else if ( strcasecmp( str, "search" ) == 0 ) {
1962                 ACL_LVL_ASSIGN_SEARCH(mask);
1963
1964         } else if ( strcasecmp( str, "read" ) == 0 ) {
1965                 ACL_LVL_ASSIGN_READ(mask);
1966
1967         } else if ( strcasecmp( str, "write" ) == 0 ) {
1968                 ACL_LVL_ASSIGN_WRITE(mask);
1969
1970         } else if ( strcasecmp( str, "manage" ) == 0 ) {
1971                 ACL_LVL_ASSIGN_MANAGE(mask);
1972
1973         } else {
1974                 ACL_INVALIDATE( mask );
1975         }
1976
1977         return mask;
1978 }
1979
1980 static void
1981 acl_usage( void )
1982 {
1983         fprintf( stderr, "%s%s%s\n",
1984                 "<access clause> ::= access to <what> "
1985                                 "[ by <who> <access> [ <control> ] ]+ \n"
1986                 "<what> ::= * | [dn[.<dnstyle>]=<DN>] [filter=<filter>] [attrs=<attrlist>]\n"
1987                 "<attrlist> ::= <attr> [val[.<attrstyle>]=<value>] | <attr> , <attrlist>\n"
1988                 "<attr> ::= <attrname> | entry | children\n",
1989                 "<who> ::= [ * | anonymous | users | self | dn[.<dnstyle>]=<DN> ]\n"
1990                         "\t[ realanonymous | realusers | realself | realdn[.<dnstyle>]=<DN> ]\n"
1991                         "\t[dnattr=<attrname>]\n"
1992                         "\t[realdnattr=<attrname>]\n"
1993                         "\t[group[/<objectclass>[/<attrname>]][.<style>]=<group>]\n"
1994                         "\t[peername[.<peernamestyle>]=<peer>] [sockname[.<style>]=<name>]\n"
1995                         "\t[domain[.<domainstyle>]=<domain>] [sockurl[.<style>]=<url>]\n"
1996 #ifdef SLAPD_ACI_ENABLED
1997                         "\t[aci=[<attrname>]]\n"
1998 #endif
1999 #ifdef SLAP_DYNACL
2000                         "\t[dynacl/<name>[.<dynstyle>][=<pattern>]]\n"
2001 #endif /* SLAP_DYNACL */
2002                         "\t[ssf=<n>] [transport_ssf=<n>] [tls_ssf=<n>] [sasl_ssf=<n>]\n",
2003                 "<style> ::= exact | regex | base(Object)\n"
2004                 "<dnstyle> ::= base(Object) | one(level) | sub(tree) | children | "
2005                         "exact | regex\n"
2006                 "<attrstyle> ::= exact | regex | base(Object) | one(level) | "
2007                         "sub(tree) | children\n"
2008                 "<peernamestyle> ::= exact | regex | ip | path\n"
2009                 "<domainstyle> ::= exact | regex | base(Object) | sub(tree)\n"
2010                 "<access> ::= [[real]self]{<level>|<priv>}\n"
2011                 "<level> ::= none|disclose|auth|compare|search|read|write|manage\n"
2012                 "<priv> ::= {=|+|-}{0|d|x|c|s|r|w|m}+\n"
2013                 "<control> ::= [ stop | continue | break ]\n"
2014         );
2015         exit( EXIT_FAILURE );
2016 }
2017
2018 /*
2019  * Set pattern to a "normalized" DN from src.
2020  * At present it simply eats the (optional) space after 
2021  * a RDN separator (,)
2022  * Eventually will evolve in a more complete normalization
2023  */
2024 static void
2025 acl_regex_normalized_dn(
2026         const char *src,
2027         struct berval *pattern )
2028 {
2029         char *str, *p;
2030         ber_len_t len;
2031
2032         str = ch_strdup( src );
2033         len = strlen( src );
2034
2035         for ( p = str; p && p[0]; p++ ) {
2036                 /* escape */
2037                 if ( p[0] == '\\' && p[1] ) {
2038                         /* 
2039                          * if escaping a hex pair we should
2040                          * increment p twice; however, in that 
2041                          * case the second hex number does 
2042                          * no harm
2043                          */
2044                         p++;
2045                 }
2046
2047                 if ( p[0] == ',' && p[1] == ' ' ) {
2048                         char *q;
2049                         
2050                         /*
2051                          * too much space should be an error if we are pedantic
2052                          */
2053                         for ( q = &p[2]; q[0] == ' '; q++ ) {
2054                                 /* DO NOTHING */ ;
2055                         }
2056                         AC_MEMCPY( p+1, q, len-(q-str)+1);
2057                 }
2058         }
2059         pattern->bv_val = str;
2060         pattern->bv_len = p - str;
2061
2062         return;
2063 }
2064
2065 static void
2066 split(
2067     char        *line,
2068     int         splitchar,
2069     char        **left,
2070     char        **right )
2071 {
2072         *left = line;
2073         if ( (*right = strchr( line, splitchar )) != NULL ) {
2074                 *((*right)++) = '\0';
2075         }
2076 }
2077
2078 static void
2079 access_append( Access **l, Access *a )
2080 {
2081         for ( ; *l != NULL; l = &(*l)->a_next ) {
2082                 ;       /* Empty */
2083         }
2084
2085         *l = a;
2086 }
2087
2088 void
2089 acl_append( AccessControl **l, AccessControl *a )
2090 {
2091         for ( ; *l != NULL; l = &(*l)->acl_next ) {
2092                 ;       /* Empty */
2093         }
2094
2095         *l = a;
2096 }
2097
2098 static void
2099 access_free( Access *a )
2100 {
2101         if ( !BER_BVISNULL( &a->a_dn_pat ) ) {
2102                 free( a->a_dn_pat.bv_val );
2103         }
2104         if ( !BER_BVISNULL( &a->a_realdn_pat ) ) {
2105                 free( a->a_realdn_pat.bv_val );
2106         }
2107         if ( !BER_BVISNULL( &a->a_peername_pat ) ) {
2108                 free( a->a_peername_pat.bv_val );
2109         }
2110         if ( !BER_BVISNULL( &a->a_sockname_pat ) ) {
2111                 free( a->a_sockname_pat.bv_val );
2112         }
2113         if ( !BER_BVISNULL( &a->a_domain_pat ) ) {
2114                 free( a->a_domain_pat.bv_val );
2115         }
2116         if ( !BER_BVISNULL( &a->a_sockurl_pat ) ) {
2117                 free( a->a_sockurl_pat.bv_val );
2118         }
2119         if ( !BER_BVISNULL( &a->a_set_pat ) ) {
2120                 free( a->a_set_pat.bv_val );
2121         }
2122         if ( !BER_BVISNULL( &a->a_group_pat ) ) {
2123                 free( a->a_group_pat.bv_val );
2124         }
2125         free( a );
2126 }
2127
2128 void
2129 acl_free( AccessControl *a )
2130 {
2131         Access *n;
2132         AttributeName *an;
2133
2134         if ( a->acl_filter ) {
2135                 filter_free( a->acl_filter );
2136         }
2137         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
2138                 free ( a->acl_dn_pat.bv_val );
2139         }
2140         if ( a->acl_attrs ) {
2141                 for ( an = a->acl_attrs; !BER_BVISNULL( &an->an_name ); an++ ) {
2142                         free( an->an_name.bv_val );
2143                 }
2144                 free( a->acl_attrs );
2145         }
2146         for ( ; a->acl_access; a->acl_access = n ) {
2147                 n = a->acl_access->a_next;
2148                 access_free( a->acl_access );
2149         }
2150         free( a );
2151 }
2152
2153 /* Because backend_startup uses acl_append to tack on the global_acl to
2154  * the end of each backend's acl, we cannot just take one argument and
2155  * merrily free our way to the end of the list. backend_destroy calls us
2156  * with the be_acl in arg1, and global_acl in arg2 to give us a stopping
2157  * point. config_destroy calls us with global_acl in arg1 and NULL in
2158  * arg2, so we then proceed to polish off the global_acl.
2159  */
2160 void
2161 acl_destroy( AccessControl *a, AccessControl *end )
2162 {
2163         AccessControl *n;
2164
2165         for (; a && a!= end; a=n) {
2166                 n = a->acl_next;
2167                 acl_free( a );
2168         }
2169 }
2170
2171 char *
2172 access2str( slap_access_t access )
2173 {
2174         if ( access == ACL_NONE ) {
2175                 return "none";
2176
2177         } else if ( access == ACL_DISCLOSE ) {
2178                 return "disclose";
2179
2180         } else if ( access == ACL_AUTH ) {
2181                 return "auth";
2182
2183         } else if ( access == ACL_COMPARE ) {
2184                 return "compare";
2185
2186         } else if ( access == ACL_SEARCH ) {
2187                 return "search";
2188
2189         } else if ( access == ACL_READ ) {
2190                 return "read";
2191
2192         } else if ( access == ACL_WRITE ) {
2193                 return "write";
2194
2195         } else if ( access == ACL_MANAGE ) {
2196                 return "manage";
2197
2198         }
2199
2200         return "unknown";
2201 }
2202
2203 slap_access_t
2204 str2access( const char *str )
2205 {
2206         if ( strcasecmp( str, "none" ) == 0 ) {
2207                 return ACL_NONE;
2208
2209         } else if ( strcasecmp( str, "disclose" ) == 0 ) {
2210                 return ACL_DISCLOSE;
2211
2212         } else if ( strcasecmp( str, "auth" ) == 0 ) {
2213                 return ACL_AUTH;
2214
2215         } else if ( strcasecmp( str, "compare" ) == 0 ) {
2216                 return ACL_COMPARE;
2217
2218         } else if ( strcasecmp( str, "search" ) == 0 ) {
2219                 return ACL_SEARCH;
2220
2221         } else if ( strcasecmp( str, "read" ) == 0 ) {
2222                 return ACL_READ;
2223
2224         } else if ( strcasecmp( str, "write" ) == 0 ) {
2225                 return ACL_WRITE;
2226
2227         } else if ( strcasecmp( str, "manage" ) == 0 ) {
2228                 return ACL_MANAGE;
2229         }
2230
2231         return( ACL_INVALID_ACCESS );
2232 }
2233
2234 #define ACLBUF_MAXLEN   8192
2235
2236 static char aclbuf[ACLBUF_MAXLEN];
2237
2238 static char *
2239 dnaccess2text( slap_dn_access *bdn, char *ptr, int is_realdn )
2240 {
2241         *ptr++ = ' ';
2242
2243         if ( is_realdn ) {
2244                 ptr = lutil_strcopy( ptr, "real" );
2245         }
2246
2247         if ( ber_bvccmp( &bdn->a_pat, '*' ) ||
2248                 bdn->a_style == ACL_STYLE_ANONYMOUS ||
2249                 bdn->a_style == ACL_STYLE_USERS ||
2250                 bdn->a_style == ACL_STYLE_SELF )
2251         {
2252                 if ( is_realdn ) {
2253                         assert( ! ber_bvccmp( &bdn->a_pat, '*' ) );
2254                 }
2255                         
2256                 ptr = lutil_strcopy( ptr, bdn->a_pat.bv_val );
2257                 if ( bdn->a_style == ACL_STYLE_SELF && bdn->a_self_level != 0 ) {
2258                         int n = sprintf( ptr, ".level{%d}", bdn->a_self_level );
2259                         if ( n > 0 ) {
2260                                 ptr += n;
2261                         } /* else ? */
2262                 }
2263
2264         } else {
2265                 ptr = lutil_strcopy( ptr, "dn." );
2266                 ptr = lutil_strcopy( ptr, style_strings[bdn->a_style] );
2267                 if ( bdn->a_style == ACL_STYLE_LEVEL ) {
2268                         int n = sprintf( ptr, "{%d}", bdn->a_level );
2269                         if ( n > 0 ) {
2270                                 ptr += n;
2271                         } /* else ? */
2272                 }
2273                 if ( bdn->a_expand ) {
2274                         ptr = lutil_strcopy( ptr, ",expand" );
2275                 }
2276                 *ptr++ = '=';
2277                 *ptr++ = '"';
2278                 ptr = lutil_strcopy( ptr, bdn->a_pat.bv_val );
2279                 *ptr++ = '"';
2280         }
2281
2282         if ( bdn->a_at != NULL ) {
2283                 *ptr++ = ' ';
2284                 if ( is_realdn ) {
2285                         ptr = lutil_strcopy( ptr, "real" );
2286                 }
2287                 ptr = lutil_strcopy( ptr, "dnattr=" );
2288                 ptr = lutil_strcopy( ptr, bdn->a_at->ad_cname.bv_val );
2289         }
2290
2291         return ptr;
2292 }
2293
2294 static char *
2295 access2text( Access *b, char *ptr )
2296 {
2297         char maskbuf[ACCESSMASK_MAXLEN];
2298
2299         ptr = lutil_strcopy( ptr, "\tby" );
2300
2301         if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
2302                 ptr = dnaccess2text( &b->a_dn, ptr, 0 );
2303         }
2304
2305         if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
2306                 ptr = dnaccess2text( &b->a_realdn, ptr, 1 );
2307         }
2308
2309         if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
2310                 ptr = lutil_strcopy( ptr, " group/" );
2311                 ptr = lutil_strcopy( ptr, b->a_group_oc ?
2312                         b->a_group_oc->soc_cname.bv_val : "groupOfNames" );
2313                 *ptr++ = '/';
2314                 ptr = lutil_strcopy( ptr, b->a_group_at ?
2315                         b->a_group_at->ad_cname.bv_val : "member" );
2316                 *ptr++ = '.';
2317                 ptr = lutil_strcopy( ptr, style_strings[b->a_group_style] );
2318                 *ptr++ = '=';
2319                 *ptr++ = '"';
2320                 ptr = lutil_strcopy( ptr, b->a_group_pat.bv_val );
2321                 *ptr++ = '"';
2322         }
2323
2324         if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
2325                 ptr = lutil_strcopy( ptr, " peername=\"" );
2326                 ptr = lutil_strcopy( ptr, b->a_peername_pat.bv_val );
2327                 *ptr++ = '"';
2328         }
2329
2330         if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
2331                 ptr = lutil_strcopy( ptr, " sockname=\"" );
2332                 ptr = lutil_strcopy( ptr, b->a_sockname_pat.bv_val );
2333                 *ptr++ = '"';
2334         }
2335
2336         if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
2337                 ptr = lutil_strcopy( ptr, " domain=" );
2338                 ptr = lutil_strcopy( ptr, b->a_domain_pat.bv_val );
2339         }
2340
2341         if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
2342                 ptr = lutil_strcopy( ptr, " sockurl=\"" );
2343                 ptr = lutil_strcopy( ptr, b->a_sockurl_pat.bv_val );
2344                 *ptr++ = '"';
2345         }
2346
2347         if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
2348                 ptr = lutil_strcopy( ptr, " set=\"" );
2349                 ptr = lutil_strcopy( ptr, b->a_set_pat.bv_val );
2350                 *ptr++ = '"';
2351         }
2352
2353 #ifdef SLAP_DYNACL
2354         if ( b->a_dynacl ) {
2355                 slap_dynacl_t   *da;
2356
2357                 for ( da = b->a_dynacl; da; da = da->da_next ) {
2358                         if ( da->da_unparse ) {
2359                                 struct berval bv;
2360                                 (void)( *da->da_unparse )( da->da_private, &bv );
2361                                 ptr = lutil_strcopy( ptr, bv.bv_val );
2362                                 ch_free( bv.bv_val );
2363                         }
2364                 }
2365         }
2366 #else /* ! SLAP_DYNACL */
2367 #ifdef SLAPD_ACI_ENABLED
2368         if ( b->a_aci_at != NULL ) {
2369                 ptr = lutil_strcopy( ptr, " aci=" );
2370                 ptr = lutil_strcopy( ptr, b->a_aci_at->ad_cname.bv_val );
2371         }
2372 #endif
2373 #endif /* SLAP_DYNACL */
2374
2375         /* Security Strength Factors */
2376         if ( b->a_authz.sai_ssf ) {
2377                 ptr += sprintf( ptr, " ssf=%u", 
2378                         b->a_authz.sai_ssf );
2379         }
2380         if ( b->a_authz.sai_transport_ssf ) {
2381                 ptr += sprintf( ptr, " transport_ssf=%u",
2382                         b->a_authz.sai_transport_ssf );
2383         }
2384         if ( b->a_authz.sai_tls_ssf ) {
2385                 ptr += sprintf( ptr, " tls_ssf=%u",
2386                         b->a_authz.sai_tls_ssf );
2387         }
2388         if ( b->a_authz.sai_sasl_ssf ) {
2389                 ptr += sprintf( ptr, " sasl_ssf=%u",
2390                         b->a_authz.sai_sasl_ssf );
2391         }
2392
2393         *ptr++ = ' ';
2394         if ( b->a_dn_self ) {
2395                 ptr = lutil_strcopy( ptr, "self" );
2396         } else if ( b->a_realdn_self ) {
2397                 ptr = lutil_strcopy( ptr, "realself" );
2398         }
2399         ptr = lutil_strcopy( ptr, accessmask2str( b->a_access_mask, maskbuf, 0 ));
2400         if ( !maskbuf[0] ) ptr--;
2401
2402         if( b->a_type == ACL_BREAK ) {
2403                 ptr = lutil_strcopy( ptr, " break" );
2404
2405         } else if( b->a_type == ACL_CONTINUE ) {
2406                 ptr = lutil_strcopy( ptr, " continue" );
2407
2408         } else if( b->a_type != ACL_STOP ) {
2409                 ptr = lutil_strcopy( ptr, " unknown-control" );
2410         } else {
2411                 if ( !maskbuf[0] ) ptr = lutil_strcopy( ptr, " stop" );
2412         }
2413         *ptr++ = '\n';
2414
2415         return ptr;
2416 }
2417
2418 void
2419 acl_unparse( AccessControl *a, struct berval *bv )
2420 {
2421         Access *b;
2422         char *ptr;
2423         int             to = 0;
2424         struct berval abv;
2425
2426         bv->bv_val = aclbuf;
2427         bv->bv_len = 0;
2428
2429         ptr = bv->bv_val;
2430
2431         ptr = lutil_strcopy( ptr, "to" );
2432         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
2433                 to++;
2434                 ptr = lutil_strcopy( ptr, " dn." );
2435                 ptr = lutil_strcopy( ptr, style_strings[a->acl_dn_style] );
2436                 *ptr++ = '=';
2437                 *ptr++ = '"';
2438                 ptr = lutil_strcopy( ptr, a->acl_dn_pat.bv_val );
2439                 ptr = lutil_strcopy( ptr, "\"\n" );
2440         }
2441
2442         if ( a->acl_filter != NULL ) {
2443                 struct berval   bv = BER_BVNULL;
2444
2445                 to++;
2446                 filter2bv( a->acl_filter, &bv );
2447                 ptr = lutil_strcopy( ptr, " filter=\"" );
2448                 ptr = lutil_strcopy( ptr, bv.bv_val );
2449                 *ptr++ = '"';
2450                 *ptr++ = '\n';
2451                 ch_free( bv.bv_val );
2452         }
2453
2454         if ( a->acl_attrs != NULL ) {
2455                 int     first = 1;
2456                 AttributeName *an;
2457                 to++;
2458
2459                 ptr = lutil_strcopy( ptr, " attrs=" );
2460                 for ( an = a->acl_attrs; an && !BER_BVISNULL( &an->an_name ); an++ ) {
2461                         if ( ! first ) *ptr++ = ',';
2462                         if (an->an_oc) {
2463                                 *ptr++ = an->an_oc_exclude ? '!' : '@';
2464                                 ptr = lutil_strcopy( ptr, an->an_oc->soc_cname.bv_val );
2465
2466                         } else {
2467                                 ptr = lutil_strcopy( ptr, an->an_name.bv_val );
2468                         }
2469                         first = 0;
2470                 }
2471                 *ptr++ = '\n';
2472         }
2473
2474         if ( !BER_BVISEMPTY( &a->acl_attrval ) ) {
2475                 to++;
2476                 ptr = lutil_strcopy( ptr, " val." );
2477                 ptr = lutil_strcopy( ptr, style_strings[a->acl_attrval_style] );
2478                 *ptr++ = '=';
2479                 *ptr++ = '"';
2480                 ptr = lutil_strcopy( ptr, a->acl_attrval.bv_val );
2481                 *ptr++ = '"';
2482                 *ptr++ = '\n';
2483         }
2484
2485         if( !to ) {
2486                 ptr = lutil_strcopy( ptr, " *\n" );
2487         }
2488
2489         for ( b = a->acl_access; b != NULL; b = b->a_next ) {
2490                 ptr = access2text( b, ptr );
2491         }
2492         *ptr = '\0';
2493         bv->bv_len = ptr - bv->bv_val;
2494 }
2495
2496 #ifdef LDAP_DEBUG
2497
2498 static void
2499 print_acl( Backend *be, AccessControl *a )
2500 {
2501         int             to = 0;
2502         Access  *b;
2503         struct berval bv;
2504
2505         acl_unparse( a, &bv );
2506         fprintf( stderr, "%s ACL: access %s\n",
2507                 be == NULL ? "Global" : "Backend", bv.bv_val );
2508 }
2509 #endif /* LDAP_DEBUG */