]> git.sur5r.net Git - openldap/blob - servers/slapd/aclparse.c
Do not log attribute value
[openldap] / servers / slapd / aclparse.c
1 /* aclparse.c - routines to parse and check acl's */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/regex.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
17
18 #include "slap.h"
19
20 static void             split(char *line, int splitchar, char **left, char **right);
21 static void             access_append(Access **l, Access *a);
22 static void             acl_usage(void) LDAP_GCCATTR((noreturn));
23
24 #ifdef LDAP_DEBUG
25 static void             print_acl(Backend *be, AccessControl *a);
26 static void             print_access(Access *b);
27 #endif
28
29 static int
30 regtest(const char *fname, int lineno, char *pat) {
31         int e;
32         regex_t re;
33
34         char buf[512];
35         unsigned size;
36
37         char *sp;
38         char *dp;
39         int  flag;
40
41         sp = pat;
42         dp = buf;
43         size = 0;
44         buf[0] = '\0';
45
46         for (size = 0, flag = 0; (size < sizeof(buf)) && *sp; sp++) {
47                 if (flag) {
48                         if (*sp == '$'|| (*sp >= '0' && *sp <= '9')) {
49                                 *dp++ = *sp;
50                                 size++;
51                         }
52                         flag = 0;
53
54                 } else {
55                         if (*sp == '$') {
56                                 flag = 1;
57                         } else {
58                                 *dp++ = *sp;
59                                 size++;
60                         }
61                 }
62         }
63
64         *dp = '\0';
65         if ( size >= (sizeof(buf)-1) ) {
66                 fprintf( stderr,
67                         "%s: line %d: regular expression \"%s\" too large\n",
68                         fname, lineno, pat );
69                 acl_usage();
70         }
71
72         if ((e = regcomp(&re, buf, REG_EXTENDED|REG_ICASE))) {
73                 char error[512];
74                 regerror(e, &re, error, sizeof(error));
75                 fprintf( stderr,
76                         "%s: line %d: regular expression \"%s\" bad because of %s\n",
77                         fname, lineno, pat, error );
78                 acl_usage();
79                 return(0);
80         }
81         regfree(&re);
82         return(1);
83 }
84
85 void
86 parse_acl(
87     Backend     *be,
88     const char  *fname,
89     int         lineno,
90     int         argc,
91     char        **argv
92 )
93 {
94         int             i;
95         char            *left, *right, *style;
96         AccessControl   *a;
97         Access  *b;
98         int rc;
99         const char *text;
100
101         a = NULL;
102         for ( i = 1; i < argc; i++ ) {
103                 /* to clause - select which entries are protected */
104                 if ( strcasecmp( argv[i], "to" ) == 0 ) {
105                         if ( a != NULL ) {
106                                 fprintf( stderr,
107                 "%s: line %d: only one to clause allowed in access line\n",
108                                     fname, lineno );
109                                 acl_usage();
110                         }
111                         a = (AccessControl *) ch_calloc( 1, sizeof(AccessControl) );
112                         a->acl_filter = NULL;
113                         a->acl_dn_pat = NULL;
114                         a->acl_attrs  = NULL;
115                         a->acl_access = NULL;
116                         a->acl_next   = NULL;
117                         for ( ++i; i < argc; i++ ) {
118                                 if ( strcasecmp( argv[i], "by" ) == 0 ) {
119                                         i--;
120                                         break;
121                                 }
122
123                                 if ( strcasecmp( argv[i], "*" ) == 0 ) {
124                                         if( a->acl_dn_pat != NULL ) {
125                                                 fprintf( stderr,
126                                                         "%s: line %d: dn pattern"
127                                                         " already specified in to clause.\n",
128                                                         fname, lineno );
129                                                 acl_usage();
130                                         }
131
132                                         a->acl_dn_pat = ch_strdup( "*" );
133                                         continue;
134                                 }
135
136                                 split( argv[i], '=', &left, &right );
137                                 split( left, '.', &left, &style );
138
139                                 if ( right == NULL || *right == '\0' ) {
140                                         fprintf( stderr,
141         "%s: line %d: missing \"=\" in (or value after) \"%s\" in to clause\n",
142                                             fname, lineno, left );
143                                         acl_usage();
144                                 }
145
146                                 if ( strcasecmp( left, "dn" ) == 0 ) {
147                                         if( a->acl_dn_pat != NULL ) {
148                                                 fprintf( stderr,
149                                                         "%s: line %d: dn pattern"
150                                                         " already specified in to clause.\n",
151                                                         fname, lineno );
152                                                 acl_usage();
153                                         }
154
155                                         if ( style == NULL || *style == '\0'
156                                                 || strcasecmp( style, "regex" ) == 0 )
157                                         {
158                                                 a->acl_dn_style = ACL_STYLE_REGEX;
159                                                 if ( strcmp(right, "*") == 0 
160                                                         || strcmp(right, ".*") == 0 
161                                                         || strcmp(right, ".*$") == 0 
162                                                         || strcmp(right, "^.*") == 0 
163                                                         || strcmp(right, "^.*$$") == 0
164                                                         || strcmp(right, ".*$$") == 0 
165                                                         || strcmp(right, "^.*$$") == 0 )
166                                                 {
167                                                         a->acl_dn_pat = ch_strdup( "*" );
168
169                                                 } else {
170                                                         a->acl_dn_pat = ch_strdup( right );
171                                                 }
172                                         } else if ( strcasecmp( style, "base" ) == 0 ) {
173                                                 a->acl_dn_style = ACL_STYLE_BASE;
174                                                 a->acl_dn_pat = ch_strdup( right );
175
176                                         } else if ( strcasecmp( style, "one" ) == 0 ) {
177                                                 a->acl_dn_style = ACL_STYLE_ONE;
178                                                 a->acl_dn_pat = ch_strdup( right );
179
180                                         } else if ( strcasecmp( style, "subtree" ) == 0 ) {
181                                                 a->acl_dn_style = ACL_STYLE_SUBTREE;
182                                                 a->acl_dn_pat = ch_strdup( right );
183
184                                         } else if ( strcasecmp( style, "children" ) == 0 ) {
185                                                 a->acl_dn_style = ACL_STYLE_CHILDREN;
186                                                 a->acl_dn_pat = ch_strdup( right );
187
188                                         } else {
189                                                 fprintf( stderr,
190         "%s: line %d: unknown dn style \"%s\" in to clause\n",
191                                                     fname, lineno, style );
192                                                 acl_usage();
193                                         }
194
195                                         continue;
196                                 }
197
198                                 if ( strcasecmp( left, "filter" ) == 0 ) {
199                                         if ( (a->acl_filter = str2filter(
200                                             right )) == NULL ) {
201                                                 fprintf( stderr,
202                                 "%s: line %d: bad filter \"%s\" in to clause\n",
203                                                     fname, lineno, right );
204                                                 acl_usage();
205                                         }
206
207                                 } else if ( strncasecmp( left, "attr", 4 ) == 0 ) {
208                                         char    **alist;
209
210                                         alist = str2charray( right, "," );
211                                         charray_merge( &a->acl_attrs, alist );
212                                         charray_free( alist );
213
214                                 } else {
215                                         fprintf( stderr,
216                                                 "%s: line %d: expecting <what> got \"%s\"\n",
217                                             fname, lineno, left );
218                                         acl_usage();
219                                 }
220                         }
221
222                         if ( a->acl_dn_pat != NULL && strcmp(a->acl_dn_pat, "*") == 0) {
223                                 free( a->acl_dn_pat );
224                                 a->acl_dn_pat = NULL;
225                         }
226                         
227                         if( a->acl_dn_pat != NULL ) {
228                                 if ( a->acl_dn_style != ACL_STYLE_REGEX )
229                                 {
230                                         dn_normalize(a->acl_dn_pat);
231
232                                 } else {
233                                         int e = regcomp( &a->acl_dn_re, a->acl_dn_pat,
234                                                          REG_EXTENDED | REG_ICASE );
235                                         if ( e ) {
236                                                 char buf[512];
237                                                 regerror( e, &a->acl_dn_re, buf, sizeof(buf) );
238                                                 fprintf( stderr,
239                                         "%s: line %d: regular expression \"%s\" bad because of %s\n",
240                                                          fname, lineno, right, buf );
241                                                 acl_usage();
242                                         }
243                                 }
244                         }
245
246                 /* by clause - select who has what access to entries */
247                 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
248                         if ( a == NULL ) {
249                                 fprintf( stderr,
250                                         "%s: line %d: to clause required before by clause in access line\n",
251                                     fname, lineno );
252                                 acl_usage();
253                         }
254
255                         /*
256                          * by clause consists of <who> and <access>
257                          */
258
259                         b = (Access *) ch_calloc( 1, sizeof(Access) );
260
261                         ACL_INVALIDATE( b->a_mask );
262
263                         if ( ++i == argc ) {
264                                 fprintf( stderr,
265                             "%s: line %d: premature eol: expecting <who>\n",
266                                     fname, lineno );
267                                 acl_usage();
268                         }
269
270                         /* get <who> */
271                         for ( ; i < argc; i++ ) {
272                                 char *pat;
273                                 slap_style_t sty = ACL_STYLE_REGEX;
274
275                                 split( argv[i], '=', &left, &right );
276                                 split( left, '.', &left, &style );
277                                 if ( style == NULL || *style == '\0'
278                                         || strcasecmp( style, "regex" ) == 0 )
279                                 {
280                                         sty = ACL_STYLE_REGEX;
281                                 } else if ( strcasecmp( style, "exact" ) == 0 ) {
282                                         sty = ACL_STYLE_EXACT;
283                                 } else if ( strcasecmp( style, "base" ) == 0 ) {
284                                         sty = ACL_STYLE_BASE;
285                                 } else if ( strcasecmp( style, "one" ) == 0 ) {
286                                         sty = ACL_STYLE_ONE;
287                                 } else if ( strcasecmp( style, "subtree" ) == 0 ) {
288                                         sty = ACL_STYLE_SUBTREE;
289                                 } else if ( strcasecmp( style, "children" ) == 0 ) {
290                                         sty = ACL_STYLE_CHILDREN;
291                                 } else {
292                                         fprintf( stderr,
293                                                 "%s: line %d: unknown style \"%s\" in by clause\n",
294                                             fname, lineno, style );
295                                         acl_usage();
296                                 }
297
298                                 if ( strcasecmp( argv[i], "*" ) == 0 ) {
299                                         pat = ch_strdup( "*" );
300
301                                 } else if ( strcasecmp( argv[i], "anonymous" ) == 0 ) {
302                                         pat = ch_strdup( "anonymous" );
303
304                                 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
305                                         pat = ch_strdup( "self" );
306
307                                 } else if ( strcasecmp( argv[i], "users" ) == 0 ) {
308                                         pat = ch_strdup( "users" );
309
310                                 } else if ( strcasecmp( left, "dn" ) == 0 ) {
311                                         if ( sty == ACL_STYLE_REGEX ) {
312                                                 b->a_dn_style = ACL_STYLE_REGEX;
313                                                 if( right == NULL ) {
314                                                         /* no '=' */
315                                                         pat = ch_strdup( "users" );
316
317                                                 } else if (*right == '\0' ) {
318                                                         /* dn="" */
319                                                         pat = ch_strdup( "anonymous" );
320
321                                                 } else if ( strcmp( right, "*" ) == 0 ) {
322                                                         /* dn=* */
323                                                         /* any or users?  users for now */
324                                                         pat = ch_strdup( "users" );
325
326                                                 } else if ( strcmp( right, ".+" ) == 0
327                                                         || strcmp( right, "^.+" ) == 0
328                                                         || strcmp( right, ".+$" ) == 0
329                                                         || strcmp( right, "^.+$" ) == 0
330                                                         || strcmp( right, ".+$$" ) == 0
331                                                         || strcmp( right, "^.+$$" ) == 0 )
332                                                 {
333                                                         pat = ch_strdup( "users" );
334
335                                                 } else if ( strcmp( right, ".*" ) == 0
336                                                         || strcmp( right, "^.*" ) == 0
337                                                         || strcmp( right, ".*$" ) == 0
338                                                         || strcmp( right, "^.*$" ) == 0
339                                                         || strcmp( right, ".*$$" ) == 0
340                                                         || strcmp( right, "^.*$$" ) == 0 )
341                                                 {
342                                                         pat = ch_strdup( "*" );
343
344                                                 } else {
345                                                         regtest(fname, lineno, right);
346                                                         pat = ch_strdup( right );
347                                                 }
348                                         } else if ( right == NULL || *right == '\0' ) {
349                                                 fprintf( stderr,
350                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
351                                                     fname, lineno, left );
352                                                 acl_usage();
353
354                                         } else {
355                                                 pat = ch_strdup( right );
356                                         }
357
358                                 } else {
359                                         pat = NULL;
360                                 }
361
362                                 if( pat != NULL ) {
363                                         if( b->a_dn_pat != NULL ) {
364                                                 fprintf( stderr,
365                                                     "%s: line %d: dn pattern already specified.\n",
366                                                     fname, lineno );
367                                                 acl_usage();
368                                         }
369
370                                         b->a_dn_pat = pat;
371                                         b->a_dn_style = sty;
372                                         if ( sty != ACL_STYLE_REGEX )
373                                                 dn_normalize(pat);
374                                         continue;
375                                 }
376
377                                 if ( strcasecmp( left, "dnattr" ) == 0 ) {
378                                         if( b->a_dn_at != NULL ) {
379                                                 fprintf( stderr,
380                                                         "%s: line %d: dnattr already specified.\n",
381                                                         fname, lineno );
382                                                 acl_usage();
383                                         }
384
385                                         rc = slap_str2ad( right, &b->a_dn_at, &text );
386
387                                         if( rc != LDAP_SUCCESS ) {
388                                                 fprintf( stderr,
389                                                         "%s: line %d: dnattr \"%s\": %s\n",
390                                                         fname, lineno, right, text );
391                                                 acl_usage();
392                                         }
393
394
395                                         if( !is_at_syntax( b->a_dn_at->ad_type,
396                                                 SLAPD_DN_SYNTAX ) )
397                                         {
398                                                 fprintf( stderr,
399                                                         "%s: line %d: dnattr \"%s\": "
400                                                         "inappropriate syntax: %s\n",
401                                                         fname, lineno, right,
402                                                         b->a_dn_at->ad_type->sat_syntax_oid );
403                                                 acl_usage();
404                                         }
405
406                                         continue;
407                                 }
408
409                                 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
410                                         fprintf( stderr,
411                                                 "%s: line %d: inappropriate style \"%s\" in by clause\n",
412                                             fname, lineno, style );
413                                         acl_usage();
414                                 }
415
416                                 if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
417                                         char *name = NULL;
418                                         char *value = NULL;
419
420                                         if( b->a_group_pat != NULL ) {
421                                                 fprintf( stderr,
422                                                         "%s: line %d: group pattern already specified.\n",
423                                                         fname, lineno );
424                                                 acl_usage();
425                                         }
426
427                                         /* format of string is "group/objectClassValue/groupAttrName" */
428                                         if ((value = strchr(left, '/')) != NULL) {
429                                                 *value++ = '\0';
430                                                 if (*value
431                                                         && (name = strchr(value, '/')) != NULL)
432                                                 {
433                                                         *name++ = '\0';
434                                                 }
435                                         }
436
437                                         b->a_group_style = sty;
438                                         if (sty == ACL_STYLE_REGEX) {
439                                                 regtest(fname, lineno, right);
440                                                 b->a_group_pat = ch_strdup( right );
441                                         } else {
442                                                 b->a_group_pat = ch_strdup( right );
443                                                 dn_normalize(b->a_group_pat);
444                                         }
445
446                                         if (value && *value) {
447                                                 b->a_group_oc = oc_find( value );
448                                                 *--value = '/';
449
450                                                 if( b->a_group_oc == NULL ) {
451                                                         fprintf( stderr,
452                                                                 "%s: line %d: group objectclass "
453                                                                 "\"%s\" unknown\n",
454                                                                 fname, lineno, value );
455                                                         acl_usage();
456                                                 }
457                                         } else {
458                                                 b->a_group_oc = oc_find(SLAPD_GROUP_CLASS);
459
460                                                 if( b->a_group_oc == NULL ) {
461                                                         fprintf( stderr,
462                                                                 "%s: line %d: group default objectclass "
463                                                                 "\"%s\" unknown\n",
464                                                                 fname, lineno, SLAPD_GROUP_CLASS );
465                                                         acl_usage();
466                                                 }
467                                         }
468
469                                         if( is_object_subclass( b->a_group_oc,
470                                                 slap_schema.si_oc_referral ) )
471                                         {
472                                                 fprintf( stderr,
473                                                         "%s: line %d: group objectclass \"%s\" "
474                                                         "is subclass of referral\n",
475                                                         fname, lineno, value );
476                                                 acl_usage();
477                                         }
478
479                                         if( is_object_subclass( b->a_group_oc,
480                                                 slap_schema.si_oc_alias ) )
481                                         {
482                                                 fprintf( stderr,
483                                                         "%s: line %d: group objectclass \"%s\" "
484                                                         "is subclass of alias\n",
485                                                         fname, lineno, value );
486                                                 acl_usage();
487                                         }
488
489                                         if (name && *name) {
490                                                 rc = slap_str2ad( name, &b->a_group_at, &text );
491
492                                                 if( rc != LDAP_SUCCESS ) {
493                                                         fprintf( stderr,
494                                                                 "%s: line %d: group \"%s\": %s\n",
495                                                                 fname, lineno, right, text );
496                                                         acl_usage();
497                                                 }
498                                                 *--name = '/';
499                                         } else {
500                                                 rc = slap_str2ad( SLAPD_GROUP_ATTR, &b->a_group_at, &text );
501
502                                                 if( rc != LDAP_SUCCESS ) {
503                                                         fprintf( stderr,
504                                                                 "%s: line %d: group \"%s\": %s\n",
505                                                                 fname, lineno, SLAPD_GROUP_ATTR, text );
506                                                         acl_usage();
507                                                 }
508                                         }
509
510                                         if( !is_at_syntax( b->a_group_at->ad_type,
511                                                 SLAPD_DN_SYNTAX ) )
512                                         {
513                                                 fprintf( stderr,
514                                                         "%s: line %d: group \"%s\": inappropriate syntax: %s\n",
515                                                         fname, lineno, right,
516                                                         b->a_group_at->ad_type->sat_syntax_oid );
517                                                 acl_usage();
518                                         }
519
520
521                                         {
522                                                 int rc;
523                                                 struct berval val;
524                                                 struct berval *vals[2];
525
526                                                 val.bv_val = b->a_group_oc->soc_oid;
527                                                 val.bv_len = strlen(val.bv_val);
528                                                 vals[0] = &val;
529                                                 vals[1] = NULL;
530
531
532                                                 rc = oc_check_allowed( b->a_group_at->ad_type, vals );
533
534                                                 if( rc != 0 ) {
535                                                         fprintf( stderr,
536                                                                 "%s: line %d: group: \"%s\" not allowed by \"%s\"\n",
537                                                                 fname, lineno,
538                                                                 b->a_group_at->ad_cname->bv_val,
539                                                                 b->a_group_oc->soc_oid );
540                                                         acl_usage();
541                                                 }
542                                         }
543                                         continue;
544                                 }
545
546                                 if ( strcasecmp( left, "peername" ) == 0 ) {
547                                         if( b->a_peername_pat != NULL ) {
548                                                 fprintf( stderr,
549                                                         "%s: line %d: peername pattern already specified.\n",
550                                                         fname, lineno );
551                                                 acl_usage();
552                                         }
553
554                                         b->a_peername_style = sty;
555                                         if (sty == ACL_STYLE_REGEX) {
556                                                 regtest(fname, lineno, right);
557                                         }
558                                         b->a_peername_pat = ch_strdup( right );
559                                         continue;
560                                 }
561
562                                 if ( strcasecmp( left, "sockname" ) == 0 ) {
563                                         if( b->a_sockname_pat != NULL ) {
564                                                 fprintf( stderr,
565                                                         "%s: line %d: sockname pattern already specified.\n",
566                                                         fname, lineno );
567                                                 acl_usage();
568                                         }
569
570                                         b->a_sockname_style = sty;
571                                         if (sty == ACL_STYLE_REGEX) {
572                                                 regtest(fname, lineno, right);
573                                         }
574                                         b->a_sockname_pat = ch_strdup( right );
575                                         continue;
576                                 }
577
578                                 if ( strcasecmp( left, "domain" ) == 0 ) {
579                                         if( b->a_domain_pat != NULL ) {
580                                                 fprintf( stderr,
581                                                         "%s: line %d: domain pattern already specified.\n",
582                                                         fname, lineno );
583                                                 acl_usage();
584                                         }
585
586                                         b->a_domain_style = sty;
587                                         if (sty == ACL_STYLE_REGEX) {
588                                                 regtest(fname, lineno, right);
589                                         }
590                                         b->a_domain_pat = ch_strdup( right );
591                                         continue;
592                                 }
593
594                                 if ( strcasecmp( left, "sockurl" ) == 0 ) {
595                                         if( b->a_sockurl_pat != NULL ) {
596                                                 fprintf( stderr,
597                                                         "%s: line %d: sockurl pattern already specified.\n",
598                                                         fname, lineno );
599                                                 acl_usage();
600                                         }
601
602                                         b->a_sockurl_style = sty;
603                                         if (sty == ACL_STYLE_REGEX) {
604                                                 regtest(fname, lineno, right);
605                                         }
606                                         b->a_sockurl_pat = ch_strdup( right );
607                                         continue;
608                                 }
609
610                                 if ( strcasecmp( left, "set" ) == 0 ) {
611                                         if( b->a_set_pat != NULL ) {
612                                                 fprintf( stderr,
613                                                         "%s: line %d: set attribute already specified.\n",
614                                                         fname, lineno );
615                                                 acl_usage();
616                                         }
617
618                                         if ( right == NULL || *right == '\0' ) {
619                                                 fprintf( stderr,
620                                                         "%s: line %d: no set is defined\n",
621                                                         fname, lineno );
622                                                 acl_usage();
623                                         }
624
625                                         b->a_set_style = sty;
626                                         b->a_set_pat = ch_strdup(right);
627
628                                         continue;
629                                 }
630
631 #ifdef SLAPD_ACI_ENABLED
632                                 if ( strcasecmp( left, "aci" ) == 0 ) {
633                                         if( b->a_aci_at != NULL ) {
634                                                 fprintf( stderr,
635                                                         "%s: line %d: aci attribute already specified.\n",
636                                                         fname, lineno );
637                                                 acl_usage();
638                                         }
639
640                                         if ( right != NULL && *right != '\0' ) {
641                                                 rc = slap_str2ad( right, &b->a_aci_at, &text );
642
643                                                 if( rc != LDAP_SUCCESS ) {
644                                                         fprintf( stderr,
645                                                                 "%s: line %d: aci \"%s\": %s\n",
646                                                                 fname, lineno, right, text );
647                                                         acl_usage();
648                                                 }
649
650                                         } else {
651                                                 rc = slap_str2ad( SLAPD_ACI_ATTR, &b->a_aci_at, &text );
652
653                                                 if( rc != LDAP_SUCCESS ) {
654                                                         fprintf( stderr,
655                                                                 "%s: line %d: aci \"%s\": %s\n",
656                                                                 fname, lineno, SLAPD_ACI_ATTR, text );
657                                                         acl_usage();
658                                                 }
659                                         }
660
661                                         if( !is_at_syntax( b->a_aci_at->ad_type,
662                                                 SLAPD_ACI_SYNTAX) )
663                                         {
664                                                 fprintf( stderr,
665                                                         "%s: line %d: aci \"%s\": inappropriate syntax: %s\n",
666                                                         fname, lineno, right,
667                                                         b->a_aci_at->ad_type->sat_syntax_oid );
668                                                 acl_usage();
669                                         }
670
671                                         continue;
672                                 }
673 #endif /* SLAPD_ACI_ENABLED */
674
675                                 if( right != NULL ) {
676                                         /* unsplit */
677                                         right[-1] = '=';
678                                 }
679                                 break;
680                         }
681
682                         if( i == argc || ( strcasecmp( left, "stop" ) == 0 )) { 
683                                 /* out of arguments or plain stop */
684
685                                 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
686                                 b->a_type = ACL_STOP;
687
688                                 access_append( &a->acl_access, b );
689                                 continue;
690                         }
691
692                         if( strcasecmp( left, "continue" ) == 0 ) {
693                                 /* plain continue */
694
695                                 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
696                                 b->a_type = ACL_CONTINUE;
697
698                                 access_append( &a->acl_access, b );
699                                 continue;
700                         }
701
702                         if( strcasecmp( left, "break" ) == 0 ) {
703                                 /* plain continue */
704
705                                 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
706                                 b->a_type = ACL_BREAK;
707
708                                 access_append( &a->acl_access, b );
709                                 continue;
710                         }
711
712                         if ( strcasecmp( left, "by" ) == 0 ) {
713                                 /* we've gone too far */
714                                 --i;
715                                 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
716                                 b->a_type = ACL_STOP;
717
718                                 access_append( &a->acl_access, b );
719                                 continue;
720                         }
721
722                         /* get <access> */
723                         if( strncasecmp( left, "self", 4 ) == 0 ) {
724                                 b->a_dn_self = 1;
725                                 ACL_PRIV_ASSIGN( b->a_mask, str2accessmask( &left[4] ) );
726
727                         } else {
728                                 ACL_PRIV_ASSIGN( b->a_mask, str2accessmask( left ) );
729                         }
730
731                         if( ACL_IS_INVALID( b->a_mask ) ) {
732                                 fprintf( stderr,
733                                         "%s: line %d: expecting <access> got \"%s\"\n",
734                                         fname, lineno, left );
735                                 acl_usage();
736                         }
737
738                         b->a_type = ACL_STOP;
739
740                         if( ++i == argc ) {
741                                 /* out of arguments or plain stop */
742                                 access_append( &a->acl_access, b );
743                                 continue;
744                         }
745
746                         if( strcasecmp( argv[i], "continue" ) == 0 ) {
747                                 /* plain continue */
748                                 b->a_type = ACL_CONTINUE;
749
750                         } else if( strcasecmp( argv[i], "break" ) == 0 ) {
751                                 /* plain continue */
752                                 b->a_type = ACL_BREAK;
753
754                         } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
755                                 /* gone to far */
756                                 i--;
757                         }
758
759                         access_append( &a->acl_access, b );
760
761                 } else {
762                         fprintf( stderr,
763                     "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
764                             fname, lineno, argv[i] );
765                         acl_usage();
766                 }
767         }
768
769         /* if we have no real access clause, complain and do nothing */
770         if ( a == NULL ) {
771                         fprintf( stderr,
772                                 "%s: line %d: warning: no access clause(s) specified in access line\n",
773                             fname, lineno );
774
775         } else {
776 #ifdef LDAP_DEBUG
777                 if (ldap_debug & LDAP_DEBUG_ACL)
778                         print_acl(be, a);
779 #endif
780         
781                 if ( a->acl_access == NULL ) {
782                         fprintf( stderr,
783                         "%s: line %d: warning: no by clause(s) specified in access line\n",
784                             fname, lineno );
785                 }
786
787                 if ( be != NULL ) {
788                         acl_append( &be->be_acl, a );
789                 } else {
790                         acl_append( &global_acl, a );
791                 }
792         }
793 }
794
795 char *
796 accessmask2str( slap_access_mask_t mask, char *buf )
797 {
798         int none=1;
799
800         assert( buf != NULL );
801
802         if ( ACL_IS_INVALID( mask ) ) {
803                 return "invalid";
804         }
805
806         buf[0] = '\0';
807
808         if ( ACL_IS_LEVEL( mask ) ) {
809                 if ( ACL_LVL_IS_NONE(mask) ) {
810                         strcat( buf, "none" );
811
812                 } else if ( ACL_LVL_IS_AUTH(mask) ) {
813                         strcat( buf, "auth" );
814
815                 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
816                         strcat( buf, "compare" );
817
818                 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
819                         strcat( buf, "search" );
820
821                 } else if ( ACL_LVL_IS_READ(mask) ) {
822                         strcat( buf, "read" );
823
824                 } else if ( ACL_LVL_IS_WRITE(mask) ) {
825                         strcat( buf, "write" );
826                 } else {
827                         strcat( buf, "unknown" );
828                 }
829                 
830                 strcat(buf, " (");
831         }
832
833         if( ACL_IS_ADDITIVE( mask ) ) {
834                 strcat( buf, "+" );
835
836         } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
837                 strcat( buf, "-" );
838
839         } else {
840                 strcat( buf, "=" );
841         }
842
843         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
844                 none = 0;
845                 strcat( buf, "w" );
846         } 
847
848         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
849                 none = 0;
850                 strcat( buf, "r" );
851         } 
852
853         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
854                 none = 0;
855                 strcat( buf, "s" );
856         } 
857
858         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
859                 none = 0;
860                 strcat( buf, "c" );
861         } 
862
863         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
864                 none = 0;
865                 strcat( buf, "x" );
866         } 
867
868         if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
869                 none = 0;
870                 strcat( buf, "n" );
871         } 
872
873         if ( none ) {
874                 strcat( buf, "0" );
875         }
876
877         if ( ACL_IS_LEVEL( mask ) ) {
878                 strcat(buf, ")");
879         } 
880         return buf;
881 }
882
883 slap_access_mask_t
884 str2accessmask( const char *str )
885 {
886         slap_access_mask_t      mask;
887
888         if( !ASCII_ALPHA(str[0]) ) {
889                 int i;
890
891                 if ( str[0] == '=' ) {
892                         ACL_INIT(mask);
893
894                 } else if( str[0] == '+' ) {
895                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
896
897                 } else if( str[0] == '-' ) {
898                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
899
900                 } else {
901                         ACL_INVALIDATE(mask);
902                         return mask;
903                 }
904
905                 for( i=1; str[i] != '\0'; i++ ) {
906                         if( TOLOWER(str[i]) == 'w' ) {
907                                 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
908
909                         } else if( TOLOWER(str[i]) == 'r' ) {
910                                 ACL_PRIV_SET(mask, ACL_PRIV_READ);
911
912                         } else if( TOLOWER(str[i]) == 's' ) {
913                                 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
914
915                         } else if( TOLOWER(str[i]) == 'c' ) {
916                                 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
917
918                         } else if( TOLOWER(str[i]) == 'x' ) {
919                                 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
920
921                         } else if( str[i] != '0' ) {
922                                 ACL_INVALIDATE(mask);
923                                 return mask;
924                         }
925                 }
926
927                 return mask;
928         }
929
930         if ( strcasecmp( str, "none" ) == 0 ) {
931                 ACL_LVL_ASSIGN_NONE(mask);
932
933         } else if ( strcasecmp( str, "auth" ) == 0 ) {
934                 ACL_LVL_ASSIGN_AUTH(mask);
935
936         } else if ( strcasecmp( str, "compare" ) == 0 ) {
937                 ACL_LVL_ASSIGN_COMPARE(mask);
938
939         } else if ( strcasecmp( str, "search" ) == 0 ) {
940                 ACL_LVL_ASSIGN_SEARCH(mask);
941
942         } else if ( strcasecmp( str, "read" ) == 0 ) {
943                 ACL_LVL_ASSIGN_READ(mask);
944
945         } else if ( strcasecmp( str, "write" ) == 0 ) {
946                 ACL_LVL_ASSIGN_WRITE(mask);
947
948         } else {
949                 ACL_INVALIDATE( mask );
950         }
951
952         return mask;
953 }
954
955 static void
956 acl_usage( void )
957 {
958         fprintf( stderr, "\n"
959                 "<access clause> ::= access to <what> "
960                                 "[ by <who> <access> <control> ]+ \n"
961                 "<what> ::= * | [dn=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n"
962                 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
963                 "<attr> ::= <attrname> | entry | children\n"
964                 "<who> ::= [ * | anonymous | users | self | dn=<regex> ]\n"
965                         "\t[dnattr=<attrname>]\n"
966                         "\t[group[/<objectclass>[/<attrname>]]=<regex>]\n"
967                         "\t[peername=<regex>] [sockname=<regex>]\n"
968                         "\t[domain=<regex>] [sockurl=<regex>]\n"
969 #ifdef SLAPD_ACI_ENABLED
970                         "\t[aci=<attrname>]\n"
971 #endif
972                 "<access> ::= [self]{<level>|<priv>}\n"
973                 "<level> ::= none | auth | compare | search | read | write\n"
974                 "<priv> ::= {=|+|-}{w|r|s|c|x}+\n"
975                 "<control> ::= [ stop | continue | break ]\n"
976                 );
977         exit( EXIT_FAILURE );
978 }
979
980 static void
981 split(
982     char        *line,
983     int         splitchar,
984     char        **left,
985     char        **right
986 )
987 {
988         *left = line;
989         if ( (*right = strchr( line, splitchar )) != NULL ) {
990                 *((*right)++) = '\0';
991         }
992 }
993
994 static void
995 access_append( Access **l, Access *a )
996 {
997         for ( ; *l != NULL; l = &(*l)->a_next )
998                 ;       /* NULL */
999
1000         *l = a;
1001 }
1002
1003 void
1004 acl_append( AccessControl **l, AccessControl *a )
1005 {
1006         for ( ; *l != NULL; l = &(*l)->acl_next )
1007                 ;       /* NULL */
1008
1009         *l = a;
1010 }
1011
1012 char *
1013 access2str( slap_access_t access )
1014 {
1015         if ( access == ACL_NONE ) {
1016                 return "none";
1017
1018         } else if ( access == ACL_AUTH ) {
1019                 return "auth";
1020
1021         } else if ( access == ACL_COMPARE ) {
1022                 return "compare";
1023
1024         } else if ( access == ACL_SEARCH ) {
1025                 return "search";
1026
1027         } else if ( access == ACL_READ ) {
1028                 return "read";
1029
1030         } else if ( access == ACL_WRITE ) {
1031                 return "write";
1032         }
1033
1034         return "unknown";
1035 }
1036
1037 slap_access_t
1038 str2access( const char *str )
1039 {
1040         if ( strcasecmp( str, "none" ) == 0 ) {
1041                 return ACL_NONE;
1042
1043         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1044                 return ACL_AUTH;
1045
1046         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1047                 return ACL_COMPARE;
1048
1049         } else if ( strcasecmp( str, "search" ) == 0 ) {
1050                 return ACL_SEARCH;
1051
1052         } else if ( strcasecmp( str, "read" ) == 0 ) {
1053                 return ACL_READ;
1054
1055         } else if ( strcasecmp( str, "write" ) == 0 ) {
1056                 return ACL_WRITE;
1057         }
1058
1059         return( ACL_INVALID_ACCESS );
1060 }
1061
1062 #ifdef LDAP_DEBUG
1063
1064 static char *style_strings[5] = {
1065                         "regex",
1066                         "base",
1067                         "one",
1068                         "subtree",
1069                         "children"
1070                 };
1071
1072
1073 static void
1074 print_access( Access *b )
1075 {
1076         char maskbuf[ACCESSMASK_MAXLEN];
1077
1078         fprintf( stderr, "\tby" );
1079
1080         if ( b->a_dn_pat != NULL ) {
1081                 if( strcmp(b->a_dn_pat, "*") == 0
1082                         || strcmp(b->a_dn_pat, "users") == 0 
1083                         || strcmp(b->a_dn_pat, "anonymous") == 0 
1084                         || strcmp(b->a_dn_pat, "self") == 0 )
1085                 {
1086                         fprintf( stderr, " %s", b->a_dn_pat );
1087
1088                 } else {
1089                         fprintf( stderr, " dn.%s=%s", style_strings[b->a_dn_style], b->a_dn_pat );
1090                 }
1091         }
1092
1093         if ( b->a_dn_at != NULL ) {
1094                 fprintf( stderr, " dnattr=%s", b->a_dn_at->ad_cname->bv_val );
1095         }
1096
1097         if ( b->a_group_pat != NULL ) {
1098                 fprintf( stderr, " group=%s", b->a_group_pat );
1099
1100                 if ( b->a_group_oc ) {
1101                         fprintf( stderr, " objectClass: %s",
1102                                 b->a_group_oc->soc_oclass.oc_oid );
1103
1104                         if ( b->a_group_at ) {
1105                                 fprintf( stderr, " attributeType: %s", b->a_group_at->ad_cname->bv_val );
1106                         }
1107                 }
1108     }
1109
1110         if ( b->a_peername_pat != NULL ) {
1111                 fprintf( stderr, " peername=%s", b->a_peername_pat );
1112         }
1113
1114         if ( b->a_sockname_pat != NULL ) {
1115                 fprintf( stderr, " sockname=%s", b->a_sockname_pat );
1116         }
1117
1118         if ( b->a_domain_pat != NULL ) {
1119                 fprintf( stderr, " domain=%s", b->a_domain_pat );
1120         }
1121
1122         if ( b->a_sockurl_pat != NULL ) {
1123                 fprintf( stderr, " sockurl=%s", b->a_sockurl_pat );
1124         }
1125
1126 #ifdef SLAPD_ACI_ENABLED
1127         if ( b->a_aci_at != NULL ) {
1128                 fprintf( stderr, " aci=%s", b->a_aci_at->ad_cname->bv_val );
1129         }
1130 #endif
1131
1132         fprintf( stderr, " %s%s",
1133                 b->a_dn_self ? "self" : "",
1134                 accessmask2str( b->a_mask, maskbuf ) );
1135
1136         if( b->a_type == ACL_BREAK ) {
1137                 fprintf( stderr, " break" );
1138
1139         } else if( b->a_type == ACL_CONTINUE ) {
1140                 fprintf( stderr, " continue" );
1141
1142         } else if( b->a_type != ACL_STOP ) {
1143                 fprintf( stderr, " unknown-control" );
1144         }
1145
1146         fprintf( stderr, "\n" );
1147 }
1148
1149
1150 static void
1151 print_acl( Backend *be, AccessControl *a )
1152 {
1153         int             to = 0;
1154         Access  *b;
1155
1156         fprintf( stderr, "%s ACL: access to",
1157                 be == NULL ? "Global" : "Backend" );
1158
1159         if ( a->acl_dn_pat != NULL ) {
1160                 to++;
1161                 fprintf( stderr, " dn.%s=%s\n",
1162                         style_strings[a->acl_dn_style], a->acl_dn_pat );
1163         }
1164
1165         if ( a->acl_filter != NULL ) {
1166                 to++;
1167                 fprintf( stderr, " filter=" );
1168                 filter_print( a->acl_filter );
1169                 fprintf( stderr, "\n" );
1170         }
1171
1172         if ( a->acl_attrs != NULL ) {
1173                 int     i, first = 1;
1174                 to++;
1175
1176                 fprintf( stderr, " attrs=" );
1177                 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
1178                         if ( ! first ) {
1179                                 fprintf( stderr, "," );
1180                         }
1181                         fprintf( stderr, a->acl_attrs[i] );
1182                         first = 0;
1183                 }
1184                 fprintf(  stderr, "\n" );
1185         }
1186
1187         if( !to ) {
1188                 fprintf( stderr, " *\n" );
1189         }
1190
1191         for ( b = a->acl_access; b != NULL; b = b->a_next ) {
1192                 print_access( b );
1193         }
1194
1195         fprintf( stderr, "\n" );
1196 }
1197
1198 #endif /* LDAP_DEBUG */