]> git.sur5r.net Git - openldap/blob - servers/slapd/aclparse.c
Added a conformant sortedResults support which is unable to sort anything.
[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
598                                 split( argv[i], '=', &left, &right );
599                                 split( left, '.', &left, &style );
600                                 if ( style ) {
601                                         split( style, ',', &style, &style_modifier );
602
603                                         if ( strncasecmp( style, "level", STRLENOF( "level" ) ) == 0 ) {
604                                                 split( style, '{', &style, &style_level );
605                                                 if ( style_level != NULL ) {
606                                                         char *p = strchr( style_level, '}' );
607                                                         if ( p == NULL ) {
608                                                                 fprintf( stderr,
609                                                                         "%s: line %d: premature eol: "
610                                                                         "expecting closing '}' in \"level{n}\"\n",
611                                                                         fname, lineno );
612                                                                 acl_usage();
613                                                         } else if ( p == style_level ) {
614                                                                 fprintf( stderr,
615                                                                         "%s: line %d: empty level "
616                                                                         "in \"level{n}\"\n",
617                                                                         fname, lineno );
618                                                                 acl_usage();
619                                                         }
620                                                         p[0] = '\0';
621                                                 }
622                                         }
623                                 }
624
625                                 if ( style == NULL || *style == '\0' ||
626                                         strcasecmp( style, "exact" ) == 0 ||
627                                         strcasecmp( style, "baseObject" ) == 0 ||
628                                         strcasecmp( style, "base" ) == 0 )
629                                 {
630                                         sty = ACL_STYLE_BASE;
631
632                                 } else if ( strcasecmp( style, "onelevel" ) == 0 ||
633                                         strcasecmp( style, "one" ) == 0 )
634                                 {
635                                         sty = ACL_STYLE_ONE;
636
637                                 } else if ( strcasecmp( style, "subtree" ) == 0 ||
638                                         strcasecmp( style, "sub" ) == 0 )
639                                 {
640                                         sty = ACL_STYLE_SUBTREE;
641
642                                 } else if ( strcasecmp( style, "children" ) == 0 ) {
643                                         sty = ACL_STYLE_CHILDREN;
644
645                                 } else if ( strcasecmp( style, "level" ) == 0 )
646                                 {
647                                         char    *next;
648
649                                         level = strtol( style_level, &next, 10 );
650                                         if ( next[0] != '\0' ) {
651                                                 fprintf( stderr,
652                                                         "%s: line %d: unable to parse level "
653                                                         "in \"level{n}\"\n",
654                                                         fname, lineno );
655                                                 acl_usage();
656                                         }
657
658                                         sty = ACL_STYLE_LEVEL;
659
660                                 } else if ( strcasecmp( style, "regex" ) == 0 ) {
661                                         sty = ACL_STYLE_REGEX;
662
663                                 } else if ( strcasecmp( style, "expand" ) == 0 ) {
664                                         sty = ACL_STYLE_EXPAND;
665
666                                 } else if ( strcasecmp( style, "ip" ) == 0 ) {
667                                         sty = ACL_STYLE_IP;
668
669                                 } else if ( strcasecmp( style, "path" ) == 0 ) {
670                                         sty = ACL_STYLE_PATH;
671 #ifndef LDAP_PF_LOCAL
672                                         fprintf( stderr, "%s: line %d: "
673                                                 "path style modifier is useless without local\n",
674                                                 fname, lineno );
675 #endif /* LDAP_PF_LOCAL */
676
677                                 } else {
678                                         fprintf( stderr,
679                                                 "%s: line %d: unknown style \"%s\" in by clause\n",
680                                             fname, lineno, style );
681                                         acl_usage();
682                                 }
683
684                                 if ( style_modifier &&
685                                         strcasecmp( style_modifier, "expand" ) == 0 )
686                                 {
687                                         switch ( sty ) {
688                                         case ACL_STYLE_REGEX:
689                                                 fprintf( stderr, "%s: line %d: "
690                                                         "\"regex\" style implies "
691                                                         "\"expand\" modifier (ignored)\n",
692                                                         fname, lineno );
693                                                 break;
694
695                                         case ACL_STYLE_EXPAND:
696 #if 0
697                                                 /* FIXME: now it's legal... */
698                                                 fprintf( stderr, "%s: line %d: "
699                                                         "\"expand\" style used "
700                                                         "in conjunction with "
701                                                         "\"expand\" modifier (ignored)\n",
702                                                         fname, lineno );
703 #endif
704                                                 break;
705
706                                         default:
707                                                 /* we'll see later if it's pertinent */
708                                                 expand = 1;
709                                                 break;
710                                         }
711                                 }
712
713                                 /* expand in <who> needs regex in <what> */
714                                 if ( ( sty == ACL_STYLE_EXPAND || expand )
715                                                 && a->acl_dn_style != ACL_STYLE_REGEX )
716                                 {
717                                         fprintf( stderr, "%s: line %d: "
718                                                 "\"expand\" style or modifier used "
719                                                 "in conjunction with "
720                                                 "a non-regex <what> clause\n",
721                                                 fname, lineno );
722                                 }
723
724                                 if ( strcasecmp( argv[i], "*" ) == 0 ) {
725                                         ber_str2bv( "*", STRLENOF( "*" ), 1, &bv );
726                                         sty = ACL_STYLE_REGEX;
727
728                                 } else if ( strcasecmp( argv[i], "anonymous" ) == 0 ) {
729                                         ber_str2bv("anonymous", STRLENOF( "anonymous" ), 1, &bv);
730                                         sty = ACL_STYLE_ANONYMOUS;
731
732                                 } else if ( strcasecmp( argv[i], "users" ) == 0 ) {
733                                         ber_str2bv("users", STRLENOF( "users" ), 1, &bv);
734                                         sty = ACL_STYLE_USERS;
735
736                                 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
737                                         ber_str2bv("self", STRLENOF( "self" ), 1, &bv);
738                                         sty = ACL_STYLE_SELF;
739
740                                 } else if ( strcasecmp( left, "dn" ) == 0 ) {
741                                         if ( sty == ACL_STYLE_REGEX ) {
742                                                 b->a_dn_style = ACL_STYLE_REGEX;
743                                                 if ( right == NULL ) {
744                                                         /* no '=' */
745                                                         ber_str2bv("users",
746                                                                 STRLENOF( "users" ),
747                                                                 1, &bv);
748                                                         b->a_dn_style = ACL_STYLE_USERS;
749
750                                                 } else if (*right == '\0' ) {
751                                                         /* dn="" */
752                                                         ber_str2bv("anonymous",
753                                                                 STRLENOF( "anonymous" ),
754                                                                 1, &bv);
755                                                         b->a_dn_style = ACL_STYLE_ANONYMOUS;
756
757                                                 } else if ( strcmp( right, "*" ) == 0 ) {
758                                                         /* dn=* */
759                                                         /* any or users?  users for now */
760                                                         ber_str2bv("users",
761                                                                 STRLENOF( "users" ),
762                                                                 1, &bv);
763                                                         b->a_dn_style = ACL_STYLE_USERS;
764
765                                                 } else if ( strcmp( right, ".+" ) == 0
766                                                         || strcmp( right, "^.+" ) == 0
767                                                         || strcmp( right, ".+$" ) == 0
768                                                         || strcmp( right, "^.+$" ) == 0
769                                                         || strcmp( right, ".+$$" ) == 0
770                                                         || strcmp( right, "^.+$$" ) == 0 )
771                                                 {
772                                                         ber_str2bv("users",
773                                                                 STRLENOF( "users" ),
774                                                                 1, &bv);
775                                                         b->a_dn_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("*",
785                                                                 STRLENOF( "*" ),
786                                                                 1, &bv);
787
788                                                 } else {
789                                                         acl_regex_normalized_dn( right, &bv );
790                                                         if ( !ber_bvccmp( &bv, '*' ) ) {
791                                                                 regtest( fname, lineno, bv.bv_val );
792                                                         }
793                                                 }
794
795                                         } else if ( right == NULL || *right == '\0' ) {
796                                                 fprintf( stderr, "%s: line %d: "
797                                                         "missing \"=\" in (or value after) \"%s\" "
798                                                         "in by clause\n",
799                                                     fname, lineno, left );
800                                                 acl_usage();
801
802                                         } else {
803                                                 ber_str2bv( right, 0, 1, &bv );
804                                         }
805
806                                 } else {
807                                         BER_BVZERO( &bv );
808                                 }
809
810                                 if ( !BER_BVISNULL( &bv ) ) {
811                                         if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
812                                                 fprintf( stderr,
813                                                     "%s: line %d: dn pattern already specified.\n",
814                                                     fname, lineno );
815                                                 acl_usage();
816                                         }
817
818                                         if ( sty != ACL_STYLE_REGEX &&
819                                                         sty != ACL_STYLE_ANONYMOUS &&
820                                                         sty != ACL_STYLE_USERS &&
821                                                         sty != ACL_STYLE_SELF &&
822                                                         expand == 0 )
823                                         {
824                                                 rc = dnNormalize(0, NULL, NULL,
825                                                         &bv, &b->a_dn_pat, NULL);
826                                                 if ( rc != LDAP_SUCCESS ) {
827                                                         fprintf( stderr,
828                                                                 "%s: line %d: bad DN \"%s\" in by DN clause\n",
829                                                                 fname, lineno, bv.bv_val );
830                                                         acl_usage();
831                                                 }
832                                                 free( bv.bv_val );
833
834                                         } else {
835                                                 b->a_dn_pat = bv;
836                                         }
837                                         b->a_dn_style = sty;
838                                         b->a_dn_expand = expand;
839                                         if ( sty == ACL_STYLE_SELF ) {
840                                                 b->a_dn_self_level = level;
841
842                                         } else {
843                                                 if ( level < 0 ) {
844                                                         fprintf( stderr,
845                                                                 "%s: line %d: bad negative level \"%d\" "
846                                                                 "in by DN clause\n",
847                                                                 fname, lineno, level );
848                                                         acl_usage();
849                                                 } else if ( level == 1 ) {
850                                                         fprintf( stderr,
851                                                                 "%s: line %d: \"onelevel\" should be used "
852                                                                 "instead of \"level{1}\" in by DN clause\n",
853                                                                 fname, lineno, 0 );
854                                                 } else if ( level == 0 && sty == ACL_STYLE_LEVEL ) {
855                                                         fprintf( stderr,
856                                                                 "%s: line %d: \"base\" should be used "
857                                                                 "instead of \"level{0}\" in by DN clause\n",
858                                                                 fname, lineno, 0 );
859                                                 }
860
861                                                 b->a_dn_level = level;
862                                         }
863                                         continue;
864                                 }
865
866                                 if ( strcasecmp( left, "dnattr" ) == 0 ) {
867                                         if ( right == NULL || right[0] == '\0' ) {
868                                                 fprintf( stderr, "%s: line %d: "
869                                                         "missing \"=\" in (or value after) \"%s\" "
870                                                         "in by clause\n",
871                                                         fname, lineno, left );
872                                                 acl_usage();
873                                         }
874
875                                         if( b->a_dn_at != NULL ) {
876                                                 fprintf( stderr,
877                                                         "%s: line %d: dnattr already specified.\n",
878                                                         fname, lineno );
879                                                 acl_usage();
880                                         }
881
882                                         rc = slap_str2ad( right, &b->a_dn_at, &text );
883
884                                         if( rc != LDAP_SUCCESS ) {
885                                                 fprintf( stderr,
886                                                         "%s: line %d: dnattr \"%s\": %s\n",
887                                                         fname, lineno, right, text );
888                                                 acl_usage();
889                                         }
890
891
892                                         if( !is_at_syntax( b->a_dn_at->ad_type,
893                                                 SLAPD_DN_SYNTAX ) &&
894                                                 !is_at_syntax( b->a_dn_at->ad_type,
895                                                 SLAPD_NAMEUID_SYNTAX ))
896                                         {
897                                                 fprintf( stderr,
898                                                         "%s: line %d: dnattr \"%s\": "
899                                                         "inappropriate syntax: %s\n",
900                                                         fname, lineno, right,
901                                                         b->a_dn_at->ad_type->sat_syntax_oid );
902                                                 acl_usage();
903                                         }
904
905                                         if( b->a_dn_at->ad_type->sat_equality == NULL ) {
906                                                 fprintf( stderr,
907                                                         "%s: line %d: dnattr \"%s\": "
908                                                         "inappropriate matching (no EQUALITY)\n",
909                                                         fname, lineno, right );
910                                                 acl_usage();
911                                         }
912
913                                         continue;
914                                 }
915
916                                 if ( strncasecmp( left, "group", STRLENOF( "group" ) ) == 0 ) {
917                                         char *name = NULL;
918                                         char *value = NULL;
919
920                                         switch ( sty ) {
921                                         case ACL_STYLE_REGEX:
922                                                 /* legacy, tolerated */
923                                                 fprintf( stderr, "%s: line %d: "
924                                                         "deprecated group style \"regex\"; "
925                                                         "use \"expand\" instead\n",
926                                                         fname, lineno, style );
927                                                 sty = ACL_STYLE_EXPAND;
928                                                 break;
929
930                                         case ACL_STYLE_BASE:
931                                                 /* legal, traditional */
932                                         case ACL_STYLE_EXPAND:
933                                                 /* legal, substring expansion; supersedes regex */
934                                                 break;
935
936                                         default:
937                                                 /* unknown */
938                                                 fprintf( stderr, "%s: line %d: "
939                                                         "inappropriate style \"%s\" in by clause\n",
940                                                         fname, lineno, style );
941                                                 acl_usage();
942                                         }
943
944                                         if ( right == NULL || right[0] == '\0' ) {
945                                                 fprintf( stderr, "%s: line %d: "
946                                                         "missing \"=\" in (or value after) \"%s\" "
947                                                         "in by clause\n",
948                                                         fname, lineno, left );
949                                                 acl_usage();
950                                         }
951
952                                         if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
953                                                 fprintf( stderr,
954                                                         "%s: line %d: group pattern already specified.\n",
955                                                         fname, lineno );
956                                                 acl_usage();
957                                         }
958
959                                         /* format of string is
960                                                 "group/objectClassValue/groupAttrName" */
961                                         if ( ( value = strchr(left, '/') ) != NULL ) {
962                                                 *value++ = '\0';
963                                                 if ( *value && ( name = strchr( value, '/' ) ) != NULL ) {
964                                                         *name++ = '\0';
965                                                 }
966                                         }
967
968                                         b->a_group_style = sty;
969                                         if ( sty == ACL_STYLE_EXPAND ) {
970                                                 acl_regex_normalized_dn( right, &bv );
971                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
972                                                         regtest( fname, lineno, bv.bv_val );
973                                                 }
974                                                 b->a_group_pat = bv;
975
976                                         } else {
977                                                 ber_str2bv( right, 0, 0, &bv );
978                                                 rc = dnNormalize( 0, NULL, NULL, &bv,
979                                                         &b->a_group_pat, NULL );
980                                                 if ( rc != LDAP_SUCCESS ) {
981                                                         fprintf( stderr,
982                                                                 "%s: line %d: bad DN \"%s\"\n",
983                                                                 fname, lineno, right );
984                                                         acl_usage();
985                                                 }
986                                         }
987
988                                         if ( value && *value ) {
989                                                 b->a_group_oc = oc_find( value );
990                                                 *--value = '/';
991
992                                                 if ( b->a_group_oc == NULL ) {
993                                                         fprintf( stderr,
994                                                                 "%s: line %d: group objectclass "
995                                                                 "\"%s\" unknown\n",
996                                                                 fname, lineno, value );
997                                                         acl_usage();
998                                                 }
999
1000                                         } else {
1001                                                 b->a_group_oc = oc_find(SLAPD_GROUP_CLASS);
1002
1003                                                 if( b->a_group_oc == NULL ) {
1004                                                         fprintf( stderr,
1005                                                                 "%s: line %d: group default objectclass "
1006                                                                 "\"%s\" unknown\n",
1007                                                                 fname, lineno, SLAPD_GROUP_CLASS );
1008                                                         acl_usage();
1009                                                 }
1010                                         }
1011
1012                                         if ( is_object_subclass( slap_schema.si_oc_referral,
1013                                                 b->a_group_oc ) )
1014                                         {
1015                                                 fprintf( stderr,
1016                                                         "%s: line %d: group objectclass \"%s\" "
1017                                                         "is subclass of referral\n",
1018                                                         fname, lineno, value );
1019                                                 acl_usage();
1020                                         }
1021
1022                                         if ( is_object_subclass( slap_schema.si_oc_alias,
1023                                                 b->a_group_oc ) )
1024                                         {
1025                                                 fprintf( stderr,
1026                                                         "%s: line %d: group objectclass \"%s\" "
1027                                                         "is subclass of alias\n",
1028                                                         fname, lineno, value );
1029                                                 acl_usage();
1030                                         }
1031
1032                                         if ( name && *name ) {
1033                                                 rc = slap_str2ad( name, &b->a_group_at, &text );
1034
1035                                                 if( rc != LDAP_SUCCESS ) {
1036                                                         fprintf( stderr,
1037                                                                 "%s: line %d: group \"%s\": %s\n",
1038                                                                 fname, lineno, right, text );
1039                                                         acl_usage();
1040                                                 }
1041                                                 *--name = '/';
1042
1043                                         } else {
1044                                                 rc = slap_str2ad( SLAPD_GROUP_ATTR, &b->a_group_at, &text );
1045
1046                                                 if ( rc != LDAP_SUCCESS ) {
1047                                                         fprintf( stderr,
1048                                                                 "%s: line %d: group \"%s\": %s\n",
1049                                                                 fname, lineno, SLAPD_GROUP_ATTR, text );
1050                                                         acl_usage();
1051                                                 }
1052                                         }
1053
1054                                         if ( !is_at_syntax( b->a_group_at->ad_type,
1055                                                 SLAPD_DN_SYNTAX ) &&
1056                                                 !is_at_syntax( b->a_group_at->ad_type,
1057                                                 SLAPD_NAMEUID_SYNTAX ) &&
1058                                                 !is_at_subtype( b->a_group_at->ad_type, slap_schema.si_ad_labeledURI->ad_type ) )
1059                                         {
1060                                                 fprintf( stderr,
1061                                                         "%s: line %d: group \"%s\": inappropriate syntax: %s\n",
1062                                                         fname, lineno, right,
1063                                                         b->a_group_at->ad_type->sat_syntax_oid );
1064                                                 acl_usage();
1065                                         }
1066
1067
1068                                         {
1069                                                 int rc;
1070                                                 struct berval vals[2];
1071
1072                                                 ber_str2bv( b->a_group_oc->soc_oid, 0, 0, &vals[0] );
1073                                                 BER_BVZERO( &vals[1] );
1074
1075                                                 rc = oc_check_allowed( b->a_group_at->ad_type,
1076                                                         vals, NULL );
1077
1078                                                 if( rc != 0 ) {
1079                                                         fprintf( stderr, "%s: line %d: "
1080                                                                 "group: \"%s\" not allowed by \"%s\"\n",
1081                                                                 fname, lineno,
1082                                                                 b->a_group_at->ad_cname.bv_val,
1083                                                                 b->a_group_oc->soc_oid );
1084                                                         acl_usage();
1085                                                 }
1086                                         }
1087                                         continue;
1088                                 }
1089
1090                                 if ( strcasecmp( left, "peername" ) == 0 ) {
1091                                         switch ( sty ) {
1092                                         case ACL_STYLE_REGEX:
1093                                         case ACL_STYLE_BASE:
1094                                                 /* legal, traditional */
1095                                         case ACL_STYLE_EXPAND:
1096                                                 /* cheap replacement to regex for simple expansion */
1097                                         case ACL_STYLE_IP:
1098                                         case ACL_STYLE_PATH:
1099                                                 /* legal, peername specific */
1100                                                 break;
1101
1102                                         default:
1103                                                 fprintf( stderr, "%s: line %d: "
1104                                                         "inappropriate style \"%s\" in by clause\n",
1105                                                     fname, lineno, style );
1106                                                 acl_usage();
1107                                         }
1108
1109                                         if ( right == NULL || right[0] == '\0' ) {
1110                                                 fprintf( stderr, "%s: line %d: "
1111                                                         "missing \"=\" in (or value after) \"%s\" "
1112                                                         "in by clause\n",
1113                                                         fname, lineno, left );
1114                                                 acl_usage();
1115                                         }
1116
1117                                         if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1118                                                 fprintf( stderr, "%s: line %d: "
1119                                                         "peername pattern already specified.\n",
1120                                                         fname, lineno );
1121                                                 acl_usage();
1122                                         }
1123
1124                                         b->a_peername_style = sty;
1125                                         if ( sty == ACL_STYLE_REGEX ) {
1126                                                 acl_regex_normalized_dn( right, &bv );
1127                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1128                                                         regtest( fname, lineno, bv.bv_val );
1129                                                 }
1130                                                 b->a_peername_pat = bv;
1131
1132                                         } else {
1133                                                 ber_str2bv( right, 0, 1, &b->a_peername_pat );
1134
1135                                                 if ( sty == ACL_STYLE_IP ) {
1136                                                         char            *addr = NULL,
1137                                                                         *mask = NULL,
1138                                                                         *port = NULL;
1139
1140                                                         split( right, '{', &addr, &port );
1141                                                         split( addr, '%', &addr, &mask );
1142
1143                                                         b->a_peername_addr = inet_addr( addr );
1144                                                         if ( b->a_peername_addr == (unsigned long)(-1) ) {
1145                                                                 /* illegal address */
1146                                                                 fprintf( stderr, "%s: line %d: "
1147                                                                         "illegal peername address \"%s\".\n",
1148                                                                         fname, lineno, addr );
1149                                                                 acl_usage();
1150                                                         }
1151
1152                                                         b->a_peername_mask = (unsigned long)(-1);
1153                                                         if ( mask != NULL ) {
1154                                                                 b->a_peername_mask = inet_addr( mask );
1155                                                                 if ( b->a_peername_mask ==
1156                                                                         (unsigned long)(-1) )
1157                                                                 {
1158                                                                         /* illegal mask */
1159                                                                         fprintf( stderr, "%s: line %d: "
1160                                                                                 "illegal peername address mask "
1161                                                                                 "\"%s\".\n",
1162                                                                                 fname, lineno, mask );
1163                                                                         acl_usage();
1164                                                                 }
1165                                                         } 
1166
1167                                                         b->a_peername_port = -1;
1168                                                         if ( port ) {
1169                                                                 char    *end = NULL;
1170
1171                                                                 b->a_peername_port = strtol( port, &end, 10 );
1172                                                                 if ( end[0] != '}' ) {
1173                                                                         /* illegal port */
1174                                                                         fprintf( stderr, "%s: line %d: "
1175                                                                                 "illegal peername port specification "
1176                                                                                 "\"{%s}\".\n",
1177                                                                                 fname, lineno, port );
1178                                                                         acl_usage();
1179                                                                 }
1180                                                         }
1181                                                 }
1182                                         }
1183                                         continue;
1184                                 }
1185
1186                                 if ( strcasecmp( left, "sockname" ) == 0 ) {
1187                                         switch ( sty ) {
1188                                         case ACL_STYLE_REGEX:
1189                                         case ACL_STYLE_BASE:
1190                                                 /* legal, traditional */
1191                                         case ACL_STYLE_EXPAND:
1192                                                 /* cheap replacement to regex for simple expansion */
1193                                                 break;
1194
1195                                         default:
1196                                                 /* unknown */
1197                                                 fprintf( stderr, "%s: line %d: "
1198                                                         "inappropriate style \"%s\" in by clause\n",
1199                                                     fname, lineno, style );
1200                                                 acl_usage();
1201                                         }
1202
1203                                         if ( right == NULL || right[0] == '\0' ) {
1204                                                 fprintf( stderr, "%s: line %d: "
1205                                                         "missing \"=\" in (or value after) \"%s\" "
1206                                                         "in by clause\n",
1207                                                         fname, lineno, left );
1208                                                 acl_usage();
1209                                         }
1210
1211                                         if ( !BER_BVISNULL( &b->a_sockname_pat ) ) {
1212                                                 fprintf( stderr, "%s: line %d: "
1213                                                         "sockname pattern already specified.\n",
1214                                                         fname, lineno );
1215                                                 acl_usage();
1216                                         }
1217
1218                                         b->a_sockname_style = sty;
1219                                         if ( sty == ACL_STYLE_REGEX ) {
1220                                                 acl_regex_normalized_dn( right, &bv );
1221                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1222                                                         regtest( fname, lineno, bv.bv_val );
1223                                                 }
1224                                                 b->a_sockname_pat = bv;
1225                                                 
1226                                         } else {
1227                                                 ber_str2bv( right, 0, 1, &b->a_sockname_pat );
1228                                         }
1229                                         continue;
1230                                 }
1231
1232                                 if ( strcasecmp( left, "domain" ) == 0 ) {
1233                                         switch ( sty ) {
1234                                         case ACL_STYLE_REGEX:
1235                                         case ACL_STYLE_BASE:
1236                                         case ACL_STYLE_SUBTREE:
1237                                                 /* legal, traditional */
1238                                                 break;
1239
1240                                         case ACL_STYLE_EXPAND:
1241                                                 /* tolerated: means exact,expand */
1242                                                 if ( expand ) {
1243                                                         fprintf( stderr,
1244                                                                 "%s: line %d: "
1245                                                                 "\"expand\" modifier "
1246                                                                 "with \"expand\" style\n",
1247                                                                 fname, lineno );
1248                                                 }
1249                                                 sty = ACL_STYLE_BASE;
1250                                                 expand = 1;
1251                                                 break;
1252
1253                                         default:
1254                                                 /* unknown */
1255                                                 fprintf( stderr, "%s: line %d: "
1256                                                         "inappropriate style \"%s\" in by clause\n",
1257                                                     fname, lineno, style );
1258                                                 acl_usage();
1259                                         }
1260
1261                                         if ( right == NULL || right[0] == '\0' ) {
1262                                                 fprintf( stderr, "%s: line %d: "
1263                                                         "missing \"=\" in (or value after) \"%s\" "
1264                                                         "in by clause\n",
1265                                                         fname, lineno, left );
1266                                                 acl_usage();
1267                                         }
1268
1269                                         if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1270                                                 fprintf( stderr,
1271                                                         "%s: line %d: domain pattern already specified.\n",
1272                                                         fname, lineno );
1273                                                 acl_usage();
1274                                         }
1275
1276                                         b->a_domain_style = sty;
1277                                         b->a_domain_expand = expand;
1278                                         if ( sty == ACL_STYLE_REGEX ) {
1279                                                 acl_regex_normalized_dn( right, &bv );
1280                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1281                                                         regtest( fname, lineno, bv.bv_val );
1282                                                 }
1283                                                 b->a_domain_pat = bv;
1284
1285                                         } else {
1286                                                 ber_str2bv( right, 0, 1, &b->a_domain_pat );
1287                                         }
1288                                         continue;
1289                                 }
1290
1291                                 if ( strcasecmp( left, "sockurl" ) == 0 ) {
1292                                         switch ( sty ) {
1293                                         case ACL_STYLE_REGEX:
1294                                         case ACL_STYLE_BASE:
1295                                                 /* legal, traditional */
1296                                         case ACL_STYLE_EXPAND:
1297                                                 /* cheap replacement to regex for simple expansion */
1298                                                 break;
1299
1300                                         default:
1301                                                 /* unknown */
1302                                                 fprintf( stderr, "%s: line %d: "
1303                                                         "inappropriate style \"%s\" in by clause\n",
1304                                                     fname, lineno, style );
1305                                                 acl_usage();
1306                                         }
1307
1308                                         if ( right == NULL || right[0] == '\0' ) {
1309                                                 fprintf( stderr, "%s: line %d: "
1310                                                         "missing \"=\" in (or value after) \"%s\" "
1311                                                         "in by clause\n",
1312                                                         fname, lineno, left );
1313                                                 acl_usage();
1314                                         }
1315
1316                                         if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1317                                                 fprintf( stderr,
1318                                                         "%s: line %d: sockurl pattern already specified.\n",
1319                                                         fname, lineno );
1320                                                 acl_usage();
1321                                         }
1322
1323                                         b->a_sockurl_style = sty;
1324                                         if ( sty == ACL_STYLE_REGEX ) {
1325                                                 acl_regex_normalized_dn( right, &bv );
1326                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1327                                                         regtest( fname, lineno, bv.bv_val );
1328                                                 }
1329                                                 b->a_sockurl_pat = bv;
1330                                                 
1331                                         } else {
1332                                                 ber_str2bv( right, 0, 1, &b->a_sockurl_pat );
1333                                         }
1334                                         continue;
1335                                 }
1336
1337                                 if ( strcasecmp( left, "set" ) == 0 ) {
1338                                         switch ( sty ) {
1339                                                 /* deprecated */
1340                                         case ACL_STYLE_REGEX:
1341                                                 fprintf( stderr, "%s: line %d: "
1342                                                         "deprecated set style "
1343                                                         "\"regex\" in <by> clause; "
1344                                                         "use \"expand\" instead\n",
1345                                                         fname, lineno );
1346                                                 sty = ACL_STYLE_EXPAND;
1347                                                 /* FALLTHRU */
1348                                                 
1349                                         case ACL_STYLE_BASE:
1350                                         case ACL_STYLE_EXPAND:
1351                                                 break;
1352
1353                                         default:
1354                                                 fprintf( stderr, "%s: line %d: "
1355                                                         "inappropriate style \"%s\" in by clause\n",
1356                                                         fname, lineno, style );
1357                                                 acl_usage();
1358                                         }
1359
1360                                         if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1361                                                 fprintf( stderr,
1362                                                         "%s: line %d: set attribute already specified.\n",
1363                                                         fname, lineno );
1364                                                 acl_usage();
1365                                         }
1366
1367                                         if ( right == NULL || *right == '\0' ) {
1368                                                 fprintf( stderr,
1369                                                         "%s: line %d: no set is defined\n",
1370                                                         fname, lineno );
1371                                                 acl_usage();
1372                                         }
1373
1374                                         b->a_set_style = sty;
1375                                         ber_str2bv( right, 0, 1, &b->a_set_pat );
1376
1377                                         continue;
1378                                 }
1379
1380 #ifdef SLAP_DYNACL
1381                                 {
1382                                         char            *name = NULL;
1383                                         
1384                                         if ( strcasecmp( left, "aci" ) == 0 ) {
1385                                                 name = "aci";
1386                                                 
1387                                         } else if ( strncasecmp( left, "dynacl/", STRLENOF( "dynacl/" ) ) == 0 ) {
1388                                                 name = &left[ STRLENOF( "dynacl/" ) ];
1389                                         }
1390
1391                                         if ( name ) {
1392                                                 if ( slap_dynacl_config( fname, lineno, b, name, sty, right ) ) {
1393                                                         fprintf( stderr, "%s: line %d: "
1394                                                                 "unable to configure dynacl \"%s\"\n",
1395                                                                 fname, lineno, name );
1396                                                         acl_usage();
1397                                                 }
1398
1399                                                 continue;
1400                                         }
1401                                 }
1402 #else /* ! SLAP_DYNACL */
1403
1404 #ifdef SLAPD_ACI_ENABLED
1405                                 if ( strcasecmp( left, "aci" ) == 0 ) {
1406                                         if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
1407                                                 fprintf( stderr, "%s: line %d: "
1408                                                         "inappropriate style \"%s\" in by clause\n",
1409                                                     fname, lineno, style );
1410                                                 acl_usage();
1411                                         }
1412
1413                                         if( b->a_aci_at != NULL ) {
1414                                                 fprintf( stderr,
1415                                                         "%s: line %d: aci attribute already specified.\n",
1416                                                         fname, lineno );
1417                                                 acl_usage();
1418                                         }
1419
1420                                         if ( right != NULL && *right != '\0' ) {
1421                                                 rc = slap_str2ad( right, &b->a_aci_at, &text );
1422
1423                                                 if( rc != LDAP_SUCCESS ) {
1424                                                         fprintf( stderr,
1425                                                                 "%s: line %d: aci \"%s\": %s\n",
1426                                                                 fname, lineno, right, text );
1427                                                         acl_usage();
1428                                                 }
1429
1430                                         } else {
1431                                                 b->a_aci_at = slap_schema.si_ad_aci;
1432                                         }
1433
1434                                         if( !is_at_syntax( b->a_aci_at->ad_type,
1435                                                 SLAPD_ACI_SYNTAX) )
1436                                         {
1437                                                 fprintf( stderr, "%s: line %d: "
1438                                                         "aci \"%s\": inappropriate syntax: %s\n",
1439                                                         fname, lineno, right,
1440                                                         b->a_aci_at->ad_type->sat_syntax_oid );
1441                                                 acl_usage();
1442                                         }
1443
1444                                         continue;
1445                                 }
1446 #endif /* SLAPD_ACI_ENABLED */
1447 #endif /* ! SLAP_DYNACL */
1448
1449                                 if ( strcasecmp( left, "ssf" ) == 0 ) {
1450                                         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
1451                                                 fprintf( stderr, "%s: line %d: "
1452                                                         "inappropriate style \"%s\" in by clause\n",
1453                                                     fname, lineno, style );
1454                                                 acl_usage();
1455                                         }
1456
1457                                         if ( b->a_authz.sai_ssf ) {
1458                                                 fprintf( stderr,
1459                                                         "%s: line %d: ssf attribute already specified.\n",
1460                                                         fname, lineno );
1461                                                 acl_usage();
1462                                         }
1463
1464                                         if ( right == NULL || *right == '\0' ) {
1465                                                 fprintf( stderr,
1466                                                         "%s: line %d: no ssf is defined\n",
1467                                                         fname, lineno );
1468                                                 acl_usage();
1469                                         }
1470
1471                                         b->a_authz.sai_ssf = strtol( right, &next, 10 );
1472                                         if ( next == NULL || next[0] != '\0' ) {
1473                                                 fprintf( stderr,
1474                                                         "%s: line %d: unable to parse ssf value (%s)\n",
1475                                                         fname, lineno, right );
1476                                                 acl_usage();
1477                                         }
1478
1479                                         if ( !b->a_authz.sai_ssf ) {
1480                                                 fprintf( stderr,
1481                                                         "%s: line %d: invalid ssf value (%s)\n",
1482                                                         fname, lineno, right );
1483                                                 acl_usage();
1484                                         }
1485                                         continue;
1486                                 }
1487
1488                                 if ( strcasecmp( left, "transport_ssf" ) == 0 ) {
1489                                         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
1490                                                 fprintf( stderr, "%s: line %d: "
1491                                                         "inappropriate style \"%s\" in by clause\n",
1492                                                         fname, lineno, style );
1493                                                 acl_usage();
1494                                         }
1495
1496                                         if ( b->a_authz.sai_transport_ssf ) {
1497                                                 fprintf( stderr, "%s: line %d: "
1498                                                         "transport_ssf attribute already specified.\n",
1499                                                         fname, lineno );
1500                                                 acl_usage();
1501                                         }
1502
1503                                         if ( right == NULL || *right == '\0' ) {
1504                                                 fprintf( stderr,
1505                                                         "%s: line %d: no transport_ssf is defined\n",
1506                                                         fname, lineno );
1507                                                 acl_usage();
1508                                         }
1509
1510                                         b->a_authz.sai_transport_ssf = strtol( right, &next, 10 );
1511                                         if ( next == NULL || next[0] != '\0' ) {
1512                                                 fprintf( stderr, "%s: line %d: "
1513                                                         "unable to parse transport_ssf value (%s)\n",
1514                                                         fname, lineno, right );
1515                                                 acl_usage();
1516                                         }
1517
1518                                         if ( !b->a_authz.sai_transport_ssf ) {
1519                                                 fprintf( stderr,
1520                                                         "%s: line %d: invalid transport_ssf value (%s)\n",
1521                                                         fname, lineno, right );
1522                                                 acl_usage();
1523                                         }
1524                                         continue;
1525                                 }
1526
1527                                 if ( strcasecmp( left, "tls_ssf" ) == 0 ) {
1528                                         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
1529                                                 fprintf( stderr, "%s: line %d: "
1530                                                         "inappropriate style \"%s\" in by clause\n",
1531                                                         fname, lineno, style );
1532                                                 acl_usage();
1533                                         }
1534
1535                                         if ( b->a_authz.sai_tls_ssf ) {
1536                                                 fprintf( stderr, "%s: line %d: "
1537                                                         "tls_ssf attribute already specified.\n",
1538                                                         fname, lineno );
1539                                                 acl_usage();
1540                                         }
1541
1542                                         if ( right == NULL || *right == '\0' ) {
1543                                                 fprintf( stderr,
1544                                                         "%s: line %d: no tls_ssf is defined\n",
1545                                                         fname, lineno );
1546                                                 acl_usage();
1547                                         }
1548
1549                                         b->a_authz.sai_tls_ssf = strtol( right, &next, 10 );
1550                                         if ( next == NULL || next[0] != '\0' ) {
1551                                                 fprintf( stderr, "%s: line %d: "
1552                                                         "unable to parse tls_ssf value (%s)\n",
1553                                                         fname, lineno, right );
1554                                                 acl_usage();
1555                                         }
1556
1557                                         if ( !b->a_authz.sai_tls_ssf ) {
1558                                                 fprintf( stderr,
1559                                                         "%s: line %d: invalid tls_ssf value (%s)\n",
1560                                                         fname, lineno, right );
1561                                                 acl_usage();
1562                                         }
1563                                         continue;
1564                                 }
1565
1566                                 if ( strcasecmp( left, "sasl_ssf" ) == 0 ) {
1567                                         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
1568                                                 fprintf( stderr, "%s: line %d: "
1569                                                         "inappropriate style \"%s\" in by clause\n",
1570                                                         fname, lineno, style );
1571                                                 acl_usage();
1572                                         }
1573
1574                                         if ( b->a_authz.sai_sasl_ssf ) {
1575                                                 fprintf( stderr, "%s: line %d: "
1576                                                         "sasl_ssf attribute already specified.\n",
1577                                                         fname, lineno );
1578                                                 acl_usage();
1579                                         }
1580
1581                                         if ( right == NULL || *right == '\0' ) {
1582                                                 fprintf( stderr,
1583                                                         "%s: line %d: no sasl_ssf is defined\n",
1584                                                         fname, lineno );
1585                                                 acl_usage();
1586                                         }
1587
1588                                         b->a_authz.sai_sasl_ssf = strtol( right, &next, 10 );
1589                                         if ( next == NULL || next[0] != '\0' ) {
1590                                                 fprintf( stderr, "%s: line %d: "
1591                                                         "unable to parse sasl_ssf value (%s)\n",
1592                                                         fname, lineno, right );
1593                                                 acl_usage();
1594                                         }
1595
1596                                         if ( !b->a_authz.sai_sasl_ssf ) {
1597                                                 fprintf( stderr,
1598                                                         "%s: line %d: invalid sasl_ssf value (%s)\n",
1599                                                         fname, lineno, right );
1600                                                 acl_usage();
1601                                         }
1602                                         continue;
1603                                 }
1604
1605                                 if ( right != NULL ) {
1606                                         /* unsplit */
1607                                         right[-1] = '=';
1608                                 }
1609                                 break;
1610                         }
1611
1612                         if ( i == argc || ( strcasecmp( left, "stop" ) == 0 ) ) { 
1613                                 /* out of arguments or plain stop */
1614
1615                                 ACL_PRIV_ASSIGN( b->a_access_mask, ACL_PRIV_ADDITIVE );
1616                                 b->a_type = ACL_STOP;
1617
1618                                 access_append( &a->acl_access, b );
1619                                 continue;
1620                         }
1621
1622                         if ( strcasecmp( left, "continue" ) == 0 ) {
1623                                 /* plain continue */
1624
1625                                 ACL_PRIV_ASSIGN( b->a_access_mask, ACL_PRIV_ADDITIVE );
1626                                 b->a_type = ACL_CONTINUE;
1627
1628                                 access_append( &a->acl_access, b );
1629                                 continue;
1630                         }
1631
1632                         if ( strcasecmp( left, "break" ) == 0 ) {
1633                                 /* plain continue */
1634
1635                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1636                                 b->a_type = ACL_BREAK;
1637
1638                                 access_append( &a->acl_access, b );
1639                                 continue;
1640                         }
1641
1642                         if ( strcasecmp( left, "by" ) == 0 ) {
1643                                 /* we've gone too far */
1644                                 --i;
1645                                 ACL_PRIV_ASSIGN( b->a_access_mask, ACL_PRIV_ADDITIVE );
1646                                 b->a_type = ACL_STOP;
1647
1648                                 access_append( &a->acl_access, b );
1649                                 continue;
1650                         }
1651
1652                         /* get <access> */
1653                         if ( strncasecmp( left, "self", 4 ) == 0 ) {
1654                                 b->a_dn_self = 1;
1655                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( &left[4] ) );
1656
1657                         } else {
1658                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( left ) );
1659                         }
1660
1661                         if ( ACL_IS_INVALID( b->a_access_mask ) ) {
1662                                 fprintf( stderr,
1663                                         "%s: line %d: expecting <access> got \"%s\"\n",
1664                                         fname, lineno, left );
1665                                 acl_usage();
1666                         }
1667
1668                         b->a_type = ACL_STOP;
1669
1670                         if ( ++i == argc ) {
1671                                 /* out of arguments or plain stop */
1672                                 access_append( &a->acl_access, b );
1673                                 continue;
1674                         }
1675
1676                         if ( strcasecmp( argv[i], "continue" ) == 0 ) {
1677                                 /* plain continue */
1678                                 b->a_type = ACL_CONTINUE;
1679
1680                         } else if ( strcasecmp( argv[i], "break" ) == 0 ) {
1681                                 /* plain continue */
1682                                 b->a_type = ACL_BREAK;
1683
1684                         } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
1685                                 /* gone to far */
1686                                 i--;
1687                         }
1688
1689                         access_append( &a->acl_access, b );
1690
1691                 } else {
1692                         fprintf( stderr,
1693                                 "%s: line %d: expecting \"to\" "
1694                                 "or \"by\" got \"%s\"\n",
1695                                 fname, lineno, argv[i] );
1696                         acl_usage();
1697                 }
1698         }
1699
1700         /* if we have no real access clause, complain and do nothing */
1701         if ( a == NULL ) {
1702                 fprintf( stderr, "%s: line %d: "
1703                         "warning: no access clause(s) specified in access line\n",
1704                         fname, lineno );
1705
1706         } else {
1707 #ifdef LDAP_DEBUG
1708                 if ( ldap_debug & LDAP_DEBUG_ACL ) {
1709                         print_acl( be, a );
1710                 }
1711 #endif
1712         
1713                 if ( a->acl_access == NULL ) {
1714                         fprintf( stderr, "%s: line %d: "
1715                                 "warning: no by clause(s) specified in access line\n",
1716                                 fname, lineno );
1717                 }
1718
1719                 if ( be != NULL ) {
1720                         if ( !BER_BVISNULL( &be->be_nsuffix[ 1 ] ) ) {
1721                                 fprintf( stderr, "%s: line %d: warning: "
1722                                         "scope checking only applies to single-valued "
1723                                         "suffix databases\n",
1724                                         fname, lineno );
1725                                 /* go ahead, since checking is not authoritative */
1726                         }
1727
1728                         switch ( check_scope( be, a ) ) {
1729                         case ACL_SCOPE_UNKNOWN:
1730                                 fprintf( stderr, "%s: line %d: warning: "
1731                                         "cannot assess the validity of the ACL scope within "
1732                                         "backend naming context\n",
1733                                         fname, lineno );
1734                                 break;
1735
1736                         case ACL_SCOPE_WARN:
1737                                 fprintf( stderr, "%s: line %d: warning: "
1738                                         "ACL could be out of scope within backend naming context\n",
1739                                         fname, lineno );
1740                                 break;
1741
1742                         case ACL_SCOPE_PARTIAL:
1743                                 fprintf( stderr, "%s: line %d: warning: "
1744                                         "ACL appears to be partially out of scope within "
1745                                         "backend naming context\n",
1746                                         fname, lineno );
1747                                 break;
1748
1749                         case ACL_SCOPE_ERR:
1750                                 fprintf( stderr, "%s: line %d: warning: "
1751                                         "ACL appears to be out of scope within "
1752                                         "backend naming context\n",
1753                                         fname, lineno );
1754                                 break;
1755
1756                         default:
1757                                 break;
1758                         }
1759                         acl_append( &be->be_acl, a );
1760
1761                 } else {
1762                         acl_append( &frontendDB->be_acl, a );
1763                 }
1764         }
1765 }
1766
1767 char *
1768 accessmask2str( slap_mask_t mask, char *buf, int debug )
1769 {
1770         int     none = 1;
1771         char    *ptr = buf;
1772
1773         assert( buf != NULL );
1774
1775         if ( ACL_IS_INVALID( mask ) ) {
1776                 return "invalid";
1777         }
1778
1779         buf[0] = '\0';
1780
1781         if ( ACL_IS_LEVEL( mask ) ) {
1782                 if ( ACL_LVL_IS_NONE(mask) ) {
1783                         ptr = lutil_strcopy( ptr, "none" );
1784
1785                 } else if ( ACL_LVL_IS_DISCLOSE(mask) ) {
1786                         ptr = lutil_strcopy( ptr, "disclose" );
1787
1788                 } else if ( ACL_LVL_IS_AUTH(mask) ) {
1789                         ptr = lutil_strcopy( ptr, "auth" );
1790
1791                 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
1792                         ptr = lutil_strcopy( ptr, "compare" );
1793
1794                 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
1795                         ptr = lutil_strcopy( ptr, "search" );
1796
1797                 } else if ( ACL_LVL_IS_READ(mask) ) {
1798                         ptr = lutil_strcopy( ptr, "read" );
1799
1800                 } else if ( ACL_LVL_IS_WRITE(mask) ) {
1801                         ptr = lutil_strcopy( ptr, "write" );
1802
1803                 } else if ( ACL_LVL_IS_MANAGE(mask) ) {
1804                         ptr = lutil_strcopy( ptr, "manage" );
1805
1806                 } else {
1807                         ptr = lutil_strcopy( ptr, "unknown" );
1808                 }
1809                 
1810                 if ( !debug ) {
1811                         *ptr = '\0';
1812                         return buf;
1813                 }
1814                 *ptr++ = '(';
1815         }
1816
1817         if( ACL_IS_ADDITIVE( mask ) ) {
1818                 *ptr++ = '+';
1819
1820         } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
1821                 *ptr++ = '-';
1822
1823         } else {
1824                 *ptr++ = '=';
1825         }
1826
1827         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_MANAGE) ) {
1828                 none = 0;
1829                 *ptr++ = 'm';
1830         } 
1831
1832         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
1833                 none = 0;
1834                 *ptr++ = 'w';
1835         } 
1836
1837         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
1838                 none = 0;
1839                 *ptr++ = 'r';
1840         } 
1841
1842         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
1843                 none = 0;
1844                 *ptr++ = 's';
1845         } 
1846
1847         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
1848                 none = 0;
1849                 *ptr++ = 'c';
1850         } 
1851
1852         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
1853                 none = 0;
1854                 *ptr++ = 'x';
1855         } 
1856
1857         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_DISCLOSE) ) {
1858                 none = 0;
1859                 *ptr++ = 'd';
1860         } 
1861
1862         if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
1863                 none = 0;
1864                 *ptr++ = 'n';
1865         } 
1866
1867         if ( none ) {
1868                 ptr = buf;
1869         }
1870
1871         if ( ACL_IS_LEVEL( mask ) ) {
1872                 *ptr++ = ')';
1873         }
1874
1875         *ptr = '\0';
1876
1877         return buf;
1878 }
1879
1880 slap_mask_t
1881 str2accessmask( const char *str )
1882 {
1883         slap_mask_t     mask;
1884
1885         if( !ASCII_ALPHA(str[0]) ) {
1886                 int i;
1887
1888                 if ( str[0] == '=' ) {
1889                         ACL_INIT(mask);
1890
1891                 } else if( str[0] == '+' ) {
1892                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
1893
1894                 } else if( str[0] == '-' ) {
1895                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
1896
1897                 } else {
1898                         ACL_INVALIDATE(mask);
1899                         return mask;
1900                 }
1901
1902                 for( i=1; str[i] != '\0'; i++ ) {
1903                         if( TOLOWER((unsigned char) str[i]) == 'm' ) {
1904                                 ACL_PRIV_SET(mask, ACL_PRIV_MANAGE);
1905
1906                         } else if( TOLOWER((unsigned char) str[i]) == 'w' ) {
1907                                 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1908
1909                         } else if( TOLOWER((unsigned char) str[i]) == 'r' ) {
1910                                 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1911
1912                         } else if( TOLOWER((unsigned char) str[i]) == 's' ) {
1913                                 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1914
1915                         } else if( TOLOWER((unsigned char) str[i]) == 'c' ) {
1916                                 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1917
1918                         } else if( TOLOWER((unsigned char) str[i]) == 'x' ) {
1919                                 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1920
1921                         } else if( TOLOWER((unsigned char) str[i]) == 'd' ) {
1922                                 ACL_PRIV_SET(mask, ACL_PRIV_DISCLOSE);
1923
1924                         } else if( str[i] != '0' ) {
1925                                 ACL_INVALIDATE(mask);
1926                                 return mask;
1927                         }
1928                 }
1929
1930                 return mask;
1931         }
1932
1933         if ( strcasecmp( str, "none" ) == 0 ) {
1934                 ACL_LVL_ASSIGN_NONE(mask);
1935
1936         } else if ( strcasecmp( str, "disclose" ) == 0 ) {
1937                 ACL_LVL_ASSIGN_DISCLOSE(mask);
1938
1939         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1940                 ACL_LVL_ASSIGN_AUTH(mask);
1941
1942         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1943                 ACL_LVL_ASSIGN_COMPARE(mask);
1944
1945         } else if ( strcasecmp( str, "search" ) == 0 ) {
1946                 ACL_LVL_ASSIGN_SEARCH(mask);
1947
1948         } else if ( strcasecmp( str, "read" ) == 0 ) {
1949                 ACL_LVL_ASSIGN_READ(mask);
1950
1951         } else if ( strcasecmp( str, "write" ) == 0 ) {
1952                 ACL_LVL_ASSIGN_WRITE(mask);
1953
1954         } else if ( strcasecmp( str, "manage" ) == 0 ) {
1955                 ACL_LVL_ASSIGN_MANAGE(mask);
1956
1957         } else {
1958                 ACL_INVALIDATE( mask );
1959         }
1960
1961         return mask;
1962 }
1963
1964 static void
1965 acl_usage( void )
1966 {
1967         fprintf( stderr, "%s%s%s\n",
1968                 "<access clause> ::= access to <what> "
1969                                 "[ by <who> <access> [ <control> ] ]+ \n"
1970                 "<what> ::= * | [dn[.<dnstyle>]=<DN>] [filter=<filter>] [attrs=<attrlist>]\n"
1971                 "<attrlist> ::= <attr> [val[.<attrstyle>]=<value>] | <attr> , <attrlist>\n"
1972                 "<attr> ::= <attrname> | entry | children\n",
1973                 "<who> ::= [ * | anonymous | users | self | dn[.<dnstyle>]=<DN> ]\n"
1974                         "\t[dnattr=<attrname>]\n"
1975                         "\t[group[/<objectclass>[/<attrname>]][.<style>]=<group>]\n"
1976                         "\t[peername[.<peernamestyle>]=<peer>] [sockname[.<style>]=<name>]\n"
1977                         "\t[domain[.<domainstyle>]=<domain>] [sockurl[.<style>]=<url>]\n"
1978 #ifdef SLAPD_ACI_ENABLED
1979                         "\t[aci=<attrname>]\n"
1980 #endif
1981                         "\t[ssf=<n>] [transport_ssf=<n>] [tls_ssf=<n>] [sasl_ssf=<n>]\n",
1982                 "<style> ::= exact | regex | base(Object)\n"
1983                 "<dnstyle> ::= base(Object) | one(level) | sub(tree) | children | "
1984                         "exact | regex\n"
1985                 "<attrstyle> ::= exact | regex | base(Object) | one(level) | "
1986                         "sub(tree) | children\n"
1987                 "<peernamestyle> ::= exact | regex | ip | path\n"
1988                 "<domainstyle> ::= exact | regex | base(Object) | sub(tree)\n"
1989                 "<access> ::= [self]{<level>|<priv>}\n"
1990                 "<level> ::= none|disclose|auth|compare|search|read|write|manage\n"
1991                 "<priv> ::= {=|+|-}{0|d|x|c|s|r|w|m}+\n"
1992                 "<control> ::= [ stop | continue | break ]\n"
1993         );
1994         exit( EXIT_FAILURE );
1995 }
1996
1997 /*
1998  * Set pattern to a "normalized" DN from src.
1999  * At present it simply eats the (optional) space after 
2000  * a RDN separator (,)
2001  * Eventually will evolve in a more complete normalization
2002  */
2003 static void
2004 acl_regex_normalized_dn(
2005         const char *src,
2006         struct berval *pattern )
2007 {
2008         char *str, *p;
2009         ber_len_t len;
2010
2011         str = ch_strdup( src );
2012         len = strlen( src );
2013
2014         for ( p = str; p && p[0]; p++ ) {
2015                 /* escape */
2016                 if ( p[0] == '\\' && p[1] ) {
2017                         /* 
2018                          * if escaping a hex pair we should
2019                          * increment p twice; however, in that 
2020                          * case the second hex number does 
2021                          * no harm
2022                          */
2023                         p++;
2024                 }
2025
2026                 if ( p[0] == ',' && p[1] == ' ' ) {
2027                         char *q;
2028                         
2029                         /*
2030                          * too much space should be an error if we are pedantic
2031                          */
2032                         for ( q = &p[2]; q[0] == ' '; q++ ) {
2033                                 /* DO NOTHING */ ;
2034                         }
2035                         AC_MEMCPY( p+1, q, len-(q-str)+1);
2036                 }
2037         }
2038         pattern->bv_val = str;
2039         pattern->bv_len = p - str;
2040
2041         return;
2042 }
2043
2044 static void
2045 split(
2046     char        *line,
2047     int         splitchar,
2048     char        **left,
2049     char        **right )
2050 {
2051         *left = line;
2052         if ( (*right = strchr( line, splitchar )) != NULL ) {
2053                 *((*right)++) = '\0';
2054         }
2055 }
2056
2057 static void
2058 access_append( Access **l, Access *a )
2059 {
2060         for ( ; *l != NULL; l = &(*l)->a_next ) {
2061                 ;       /* Empty */
2062         }
2063
2064         *l = a;
2065 }
2066
2067 void
2068 acl_append( AccessControl **l, AccessControl *a )
2069 {
2070         for ( ; *l != NULL; l = &(*l)->acl_next ) {
2071                 ;       /* Empty */
2072         }
2073
2074         *l = a;
2075 }
2076
2077 static void
2078 access_free( Access *a )
2079 {
2080         if ( !BER_BVISNULL( &a->a_dn_pat ) ) {
2081                 free( a->a_dn_pat.bv_val );
2082         }
2083         if ( !BER_BVISNULL( &a->a_peername_pat ) ) {
2084                 free( a->a_peername_pat.bv_val );
2085         }
2086         if ( !BER_BVISNULL( &a->a_sockname_pat ) ) {
2087                 free( a->a_sockname_pat.bv_val );
2088         }
2089         if ( !BER_BVISNULL( &a->a_domain_pat ) ) {
2090                 free( a->a_domain_pat.bv_val );
2091         }
2092         if ( !BER_BVISNULL( &a->a_sockurl_pat ) ) {
2093                 free( a->a_sockurl_pat.bv_val );
2094         }
2095         if ( !BER_BVISNULL( &a->a_set_pat ) ) {
2096                 free( a->a_set_pat.bv_val );
2097         }
2098         if ( !BER_BVISNULL( &a->a_group_pat ) ) {
2099                 free( a->a_group_pat.bv_val );
2100         }
2101         free( a );
2102 }
2103
2104 void
2105 acl_free( AccessControl *a )
2106 {
2107         Access *n;
2108         AttributeName *an;
2109
2110         if ( a->acl_filter ) {
2111                 filter_free( a->acl_filter );
2112         }
2113         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
2114                 free ( a->acl_dn_pat.bv_val );
2115         }
2116         if ( a->acl_attrs ) {
2117                 for ( an = a->acl_attrs; !BER_BVISNULL( &an->an_name ); an++ ) {
2118                         free( an->an_name.bv_val );
2119                 }
2120                 free( a->acl_attrs );
2121         }
2122         for ( ; a->acl_access; a->acl_access = n ) {
2123                 n = a->acl_access->a_next;
2124                 access_free( a->acl_access );
2125         }
2126         free( a );
2127 }
2128
2129 /* Because backend_startup uses acl_append to tack on the global_acl to
2130  * the end of each backend's acl, we cannot just take one argument and
2131  * merrily free our way to the end of the list. backend_destroy calls us
2132  * with the be_acl in arg1, and global_acl in arg2 to give us a stopping
2133  * point. config_destroy calls us with global_acl in arg1 and NULL in
2134  * arg2, so we then proceed to polish off the global_acl.
2135  */
2136 void
2137 acl_destroy( AccessControl *a, AccessControl *end )
2138 {
2139         AccessControl *n;
2140
2141         for (; a && a!= end; a=n) {
2142                 n = a->acl_next;
2143                 acl_free( a );
2144         }
2145 }
2146
2147 char *
2148 access2str( slap_access_t access )
2149 {
2150         if ( access == ACL_NONE ) {
2151                 return "none";
2152
2153         } else if ( access == ACL_DISCLOSE ) {
2154                 return "disclose";
2155
2156         } else if ( access == ACL_AUTH ) {
2157                 return "auth";
2158
2159         } else if ( access == ACL_COMPARE ) {
2160                 return "compare";
2161
2162         } else if ( access == ACL_SEARCH ) {
2163                 return "search";
2164
2165         } else if ( access == ACL_READ ) {
2166                 return "read";
2167
2168         } else if ( access == ACL_WRITE ) {
2169                 return "write";
2170
2171         } else if ( access == ACL_MANAGE ) {
2172                 return "manage";
2173
2174         }
2175
2176         return "unknown";
2177 }
2178
2179 slap_access_t
2180 str2access( const char *str )
2181 {
2182         if ( strcasecmp( str, "none" ) == 0 ) {
2183                 return ACL_NONE;
2184
2185         } else if ( strcasecmp( str, "disclose" ) == 0 ) {
2186                 return ACL_DISCLOSE;
2187
2188         } else if ( strcasecmp( str, "auth" ) == 0 ) {
2189                 return ACL_AUTH;
2190
2191         } else if ( strcasecmp( str, "compare" ) == 0 ) {
2192                 return ACL_COMPARE;
2193
2194         } else if ( strcasecmp( str, "search" ) == 0 ) {
2195                 return ACL_SEARCH;
2196
2197         } else if ( strcasecmp( str, "read" ) == 0 ) {
2198                 return ACL_READ;
2199
2200         } else if ( strcasecmp( str, "write" ) == 0 ) {
2201                 return ACL_WRITE;
2202
2203         } else if ( strcasecmp( str, "manage" ) == 0 ) {
2204                 return ACL_MANAGE;
2205         }
2206
2207         return( ACL_INVALID_ACCESS );
2208 }
2209
2210 #define ACLBUF_MAXLEN   8192
2211
2212 static char aclbuf[ACLBUF_MAXLEN];
2213
2214 static char *
2215 access2text( Access *b, char *ptr )
2216 {
2217         char maskbuf[ACCESSMASK_MAXLEN];
2218
2219         ptr = lutil_strcopy( ptr, "\tby" );
2220
2221         if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
2222                 *ptr++ = ' ';
2223                 if ( ber_bvccmp( &b->a_dn_pat, '*' ) ||
2224                         b->a_dn_style == ACL_STYLE_ANONYMOUS ||
2225                         b->a_dn_style == ACL_STYLE_USERS ||
2226                         b->a_dn_style == ACL_STYLE_SELF )
2227                 {
2228                         ptr = lutil_strcopy( ptr, b->a_dn_pat.bv_val );
2229                         if ( b->a_dn_style == ACL_STYLE_SELF && b->a_dn_self_level != 0 ) {
2230                                 int n = sprintf( ptr, ".level{%d}", b->a_dn_self_level );
2231                                 if ( n > 0 ) {
2232                                         ptr += n;
2233                                 } /* else ? */
2234                         }
2235
2236                 } else {
2237                         ptr = lutil_strcopy( ptr, "dn." );
2238                         ptr = lutil_strcopy( ptr, style_strings[b->a_dn_style] );
2239                         if ( b->a_dn_style == ACL_STYLE_LEVEL ) {
2240                                 int n = sprintf( ptr, "{%d}", b->a_dn_level );
2241                                 if ( n > 0 ) {
2242                                         ptr += n;
2243                                 } /* else ? */
2244                         }
2245                         if ( b->a_dn_expand ) {
2246                                 ptr = lutil_strcopy( ptr, ",expand" );
2247                         }
2248                         *ptr++ = '=';
2249                         *ptr++ = '"';
2250                         ptr = lutil_strcopy( ptr, b->a_dn_pat.bv_val );
2251                         *ptr++ = '"';
2252                 }
2253         }
2254
2255         if ( b->a_dn_at != NULL ) {
2256                 ptr = lutil_strcopy( ptr, " dnattr=" );
2257                 ptr = lutil_strcopy( ptr, b->a_dn_at->ad_cname.bv_val );
2258         }
2259
2260         if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
2261                 ptr = lutil_strcopy( ptr, " group/" );
2262                 ptr = lutil_strcopy( ptr, b->a_group_oc ?
2263                         b->a_group_oc->soc_cname.bv_val : "groupOfNames" );
2264                 *ptr++ = '/';
2265                 ptr = lutil_strcopy( ptr, b->a_group_at ?
2266                         b->a_group_at->ad_cname.bv_val : "member" );
2267                 *ptr++ = '.';
2268                 ptr = lutil_strcopy( ptr, style_strings[b->a_group_style] );
2269                 *ptr++ = '=';
2270                 *ptr++ = '"';
2271                 ptr = lutil_strcopy( ptr, b->a_group_pat.bv_val );
2272                 *ptr++ = '"';
2273         }
2274
2275         if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
2276                 ptr = lutil_strcopy( ptr, " peername=\"" );
2277                 ptr = lutil_strcopy( ptr, b->a_peername_pat.bv_val );
2278                 *ptr++ = '"';
2279         }
2280
2281         if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
2282                 ptr = lutil_strcopy( ptr, " sockname=\"" );
2283                 ptr = lutil_strcopy( ptr, b->a_sockname_pat.bv_val );
2284                 *ptr++ = '"';
2285         }
2286
2287         if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
2288                 ptr = lutil_strcopy( ptr, " domain=" );
2289                 ptr = lutil_strcopy( ptr, b->a_domain_pat.bv_val );
2290         }
2291
2292         if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
2293                 ptr = lutil_strcopy( ptr, " sockurl=\"" );
2294                 ptr = lutil_strcopy( ptr, b->a_sockurl_pat.bv_val );
2295                 *ptr++ = '"';
2296         }
2297
2298         if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
2299                 ptr = lutil_strcopy( ptr, " set=\"" );
2300                 ptr = lutil_strcopy( ptr, b->a_set_pat.bv_val );
2301                 *ptr++ = '"';
2302         }
2303
2304 #ifdef SLAP_DYNACL
2305         if ( b->a_dynacl ) {
2306                 slap_dynacl_t   *da;
2307
2308                 for ( da = b->a_dynacl; da; da = da->da_next ) {
2309                         if ( da->da_unparse ) {
2310                                 struct berval bv;
2311                                 (void)( *da->da_unparse )( da->da_private, &bv );
2312                                 ptr = lutil_strcopy( ptr, bv.bv_val );
2313                                 ch_free( bv.bv_val );
2314                         }
2315                 }
2316         }
2317 #else /* ! SLAP_DYNACL */
2318 #ifdef SLAPD_ACI_ENABLED
2319         if ( b->a_aci_at != NULL ) {
2320                 ptr = lutil_strcopy( ptr, " aci=" );
2321                 ptr = lutil_strcopy( ptr, b->a_aci_at->ad_cname.bv_val );
2322         }
2323 #endif
2324 #endif /* SLAP_DYNACL */
2325
2326         /* Security Strength Factors */
2327         if ( b->a_authz.sai_ssf ) {
2328                 ptr += sprintf( ptr, " ssf=%u", 
2329                         b->a_authz.sai_ssf );
2330         }
2331         if ( b->a_authz.sai_transport_ssf ) {
2332                 ptr += sprintf( ptr, " transport_ssf=%u",
2333                         b->a_authz.sai_transport_ssf );
2334         }
2335         if ( b->a_authz.sai_tls_ssf ) {
2336                 ptr += sprintf( ptr, " tls_ssf=%u",
2337                         b->a_authz.sai_tls_ssf );
2338         }
2339         if ( b->a_authz.sai_sasl_ssf ) {
2340                 ptr += sprintf( ptr, " sasl_ssf=%u",
2341                         b->a_authz.sai_sasl_ssf );
2342         }
2343
2344         *ptr++ = ' ';
2345         if ( b->a_dn_self ) ptr = lutil_strcopy( ptr, "self" );
2346         ptr = lutil_strcopy( ptr, accessmask2str( b->a_access_mask, maskbuf, 0 ));
2347         if ( !maskbuf[0] ) ptr--;
2348
2349         if( b->a_type == ACL_BREAK ) {
2350                 ptr = lutil_strcopy( ptr, " break" );
2351
2352         } else if( b->a_type == ACL_CONTINUE ) {
2353                 ptr = lutil_strcopy( ptr, " continue" );
2354
2355         } else if( b->a_type != ACL_STOP ) {
2356                 ptr = lutil_strcopy( ptr, " unknown-control" );
2357         } else {
2358                 if ( !maskbuf[0] ) ptr = lutil_strcopy( ptr, " stop" );
2359         }
2360         *ptr++ = '\n';
2361
2362         return ptr;
2363 }
2364
2365 void
2366 acl_unparse( AccessControl *a, struct berval *bv )
2367 {
2368         Access *b;
2369         char *ptr;
2370         int             to = 0;
2371         struct berval abv;
2372
2373         bv->bv_val = aclbuf;
2374         bv->bv_len = 0;
2375
2376         ptr = bv->bv_val;
2377
2378         ptr = lutil_strcopy( ptr, "to" );
2379         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
2380                 to++;
2381                 ptr = lutil_strcopy( ptr, " dn." );
2382                 ptr = lutil_strcopy( ptr, style_strings[a->acl_dn_style] );
2383                 *ptr++ = '=';
2384                 *ptr++ = '"';
2385                 ptr = lutil_strcopy( ptr, a->acl_dn_pat.bv_val );
2386                 ptr = lutil_strcopy( ptr, "\"\n" );
2387         }
2388
2389         if ( a->acl_filter != NULL ) {
2390                 struct berval   bv = BER_BVNULL;
2391
2392                 to++;
2393                 filter2bv( a->acl_filter, &bv );
2394                 ptr = lutil_strcopy( ptr, " filter=\"" );
2395                 ptr = lutil_strcopy( ptr, bv.bv_val );
2396                 *ptr++ = '"';
2397                 *ptr++ = '\n';
2398                 ch_free( bv.bv_val );
2399         }
2400
2401         if ( a->acl_attrs != NULL ) {
2402                 int     first = 1;
2403                 AttributeName *an;
2404                 to++;
2405
2406                 ptr = lutil_strcopy( ptr, " attrs=" );
2407                 for ( an = a->acl_attrs; an && !BER_BVISNULL( &an->an_name ); an++ ) {
2408                         if ( ! first ) *ptr++ = ',';
2409                         if (an->an_oc) {
2410                                 *ptr++ = an->an_oc_exclude ? '!' : '@';
2411                                 ptr = lutil_strcopy( ptr, an->an_oc->soc_cname.bv_val );
2412
2413                         } else {
2414                                 ptr = lutil_strcopy( ptr, an->an_name.bv_val );
2415                         }
2416                         first = 0;
2417                 }
2418                 *ptr++ = '\n';
2419         }
2420
2421         if ( !BER_BVISEMPTY( &a->acl_attrval ) ) {
2422                 to++;
2423                 ptr = lutil_strcopy( ptr, " val." );
2424                 ptr = lutil_strcopy( ptr, style_strings[a->acl_attrval_style] );
2425                 *ptr++ = '=';
2426                 *ptr++ = '"';
2427                 ptr = lutil_strcopy( ptr, a->acl_attrval.bv_val );
2428                 *ptr++ = '"';
2429                 *ptr++ = '\n';
2430         }
2431
2432         if( !to ) {
2433                 ptr = lutil_strcopy( ptr, " *\n" );
2434         }
2435
2436         for ( b = a->acl_access; b != NULL; b = b->a_next ) {
2437                 ptr = access2text( b, ptr );
2438         }
2439         *ptr = '\0';
2440         bv->bv_len = ptr - bv->bv_val;
2441 }
2442
2443 #ifdef LDAP_DEBUG
2444
2445 static void
2446 print_acl( Backend *be, AccessControl *a )
2447 {
2448         int             to = 0;
2449         Access  *b;
2450         struct berval bv;
2451
2452         acl_unparse( a, &bv );
2453         fprintf( stderr, "%s ACL: access %s\n",
2454                 be == NULL ? "Global" : "Backend", bv.bv_val );
2455 }
2456 #endif /* LDAP_DEBUG */