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