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