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