]> git.sur5r.net Git - openldap/blob - servers/slapd/aclparse.c
fix for select_backend suggested G. Gombas (ITS 1090)
[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_access_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                                                 !is_at_syntax( b->a_dn_at->ad_type,
398                                                 SLAPD_NAMEUID_SYNTAX ))
399                                         {
400                                                 fprintf( stderr,
401                                                         "%s: line %d: dnattr \"%s\": "
402                                                         "inappropriate syntax: %s\n",
403                                                         fname, lineno, right,
404                                                         b->a_dn_at->ad_type->sat_syntax_oid );
405                                                 acl_usage();
406                                         }
407
408                                         continue;
409                                 }
410
411                                 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
412                                         fprintf( stderr,
413                                                 "%s: line %d: inappropriate style \"%s\" in by clause\n",
414                                             fname, lineno, style );
415                                         acl_usage();
416                                 }
417
418                                 if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
419                                         char *name = NULL;
420                                         char *value = NULL;
421
422                                         if( b->a_group_pat != NULL ) {
423                                                 fprintf( stderr,
424                                                         "%s: line %d: group pattern already specified.\n",
425                                                         fname, lineno );
426                                                 acl_usage();
427                                         }
428
429                                         /* format of string is "group/objectClassValue/groupAttrName" */
430                                         if ((value = strchr(left, '/')) != NULL) {
431                                                 *value++ = '\0';
432                                                 if (*value
433                                                         && (name = strchr(value, '/')) != NULL)
434                                                 {
435                                                         *name++ = '\0';
436                                                 }
437                                         }
438
439                                         b->a_group_style = sty;
440                                         if (sty == ACL_STYLE_REGEX) {
441                                                 regtest(fname, lineno, right);
442                                                 b->a_group_pat = ch_strdup( right );
443                                         } else {
444                                                 b->a_group_pat = ch_strdup( right );
445                                                 dn_normalize(b->a_group_pat);
446                                         }
447
448                                         if (value && *value) {
449                                                 b->a_group_oc = oc_find( value );
450                                                 *--value = '/';
451
452                                                 if( b->a_group_oc == NULL ) {
453                                                         fprintf( stderr,
454                                                                 "%s: line %d: group objectclass "
455                                                                 "\"%s\" unknown\n",
456                                                                 fname, lineno, value );
457                                                         acl_usage();
458                                                 }
459                                         } else {
460                                                 b->a_group_oc = oc_find(SLAPD_GROUP_CLASS);
461
462                                                 if( b->a_group_oc == NULL ) {
463                                                         fprintf( stderr,
464                                                                 "%s: line %d: group default objectclass "
465                                                                 "\"%s\" unknown\n",
466                                                                 fname, lineno, SLAPD_GROUP_CLASS );
467                                                         acl_usage();
468                                                 }
469                                         }
470
471                                         if( is_object_subclass( b->a_group_oc,
472                                                 slap_schema.si_oc_referral ) )
473                                         {
474                                                 fprintf( stderr,
475                                                         "%s: line %d: group objectclass \"%s\" "
476                                                         "is subclass of referral\n",
477                                                         fname, lineno, value );
478                                                 acl_usage();
479                                         }
480
481                                         if( is_object_subclass( b->a_group_oc,
482                                                 slap_schema.si_oc_alias ) )
483                                         {
484                                                 fprintf( stderr,
485                                                         "%s: line %d: group objectclass \"%s\" "
486                                                         "is subclass of alias\n",
487                                                         fname, lineno, value );
488                                                 acl_usage();
489                                         }
490
491                                         if (name && *name) {
492                                                 rc = slap_str2ad( name, &b->a_group_at, &text );
493
494                                                 if( rc != LDAP_SUCCESS ) {
495                                                         fprintf( stderr,
496                                                                 "%s: line %d: group \"%s\": %s\n",
497                                                                 fname, lineno, right, text );
498                                                         acl_usage();
499                                                 }
500                                                 *--name = '/';
501                                         } else {
502                                                 rc = slap_str2ad( SLAPD_GROUP_ATTR, &b->a_group_at, &text );
503
504                                                 if( rc != LDAP_SUCCESS ) {
505                                                         fprintf( stderr,
506                                                                 "%s: line %d: group \"%s\": %s\n",
507                                                                 fname, lineno, SLAPD_GROUP_ATTR, text );
508                                                         acl_usage();
509                                                 }
510                                         }
511
512                                         if( !is_at_syntax( b->a_group_at->ad_type,
513                                                 SLAPD_DN_SYNTAX ) &&
514                                             !is_at_syntax( b->a_group_at->ad_type,
515                                                 SLAPD_NAMEUID_SYNTAX ) )
516                                         {
517                                                 fprintf( stderr,
518                                                         "%s: line %d: group \"%s\": inappropriate syntax: %s\n",
519                                                         fname, lineno, right,
520                                                         b->a_group_at->ad_type->sat_syntax_oid );
521                                                 acl_usage();
522                                         }
523
524
525                                         {
526                                                 int rc;
527                                                 struct berval val;
528                                                 struct berval *vals[2];
529
530                                                 val.bv_val = b->a_group_oc->soc_oid;
531                                                 val.bv_len = strlen(val.bv_val);
532                                                 vals[0] = &val;
533                                                 vals[1] = NULL;
534
535
536                                                 rc = oc_check_allowed( b->a_group_at->ad_type, vals );
537
538                                                 if( rc != 0 ) {
539                                                         fprintf( stderr,
540                                                                 "%s: line %d: group: \"%s\" not allowed by \"%s\"\n",
541                                                                 fname, lineno,
542                                                                 b->a_group_at->ad_cname->bv_val,
543                                                                 b->a_group_oc->soc_oid );
544                                                         acl_usage();
545                                                 }
546                                         }
547                                         continue;
548                                 }
549
550                                 if ( strcasecmp( left, "peername" ) == 0 ) {
551                                         if( b->a_peername_pat != NULL ) {
552                                                 fprintf( stderr,
553                                                         "%s: line %d: peername pattern already specified.\n",
554                                                         fname, lineno );
555                                                 acl_usage();
556                                         }
557
558                                         b->a_peername_style = sty;
559                                         if (sty == ACL_STYLE_REGEX) {
560                                                 regtest(fname, lineno, right);
561                                         }
562                                         b->a_peername_pat = ch_strdup( right );
563                                         continue;
564                                 }
565
566                                 if ( strcasecmp( left, "sockname" ) == 0 ) {
567                                         if( b->a_sockname_pat != NULL ) {
568                                                 fprintf( stderr,
569                                                         "%s: line %d: sockname pattern already specified.\n",
570                                                         fname, lineno );
571                                                 acl_usage();
572                                         }
573
574                                         b->a_sockname_style = sty;
575                                         if (sty == ACL_STYLE_REGEX) {
576                                                 regtest(fname, lineno, right);
577                                         }
578                                         b->a_sockname_pat = ch_strdup( right );
579                                         continue;
580                                 }
581
582                                 if ( strcasecmp( left, "domain" ) == 0 ) {
583                                         if( b->a_domain_pat != NULL ) {
584                                                 fprintf( stderr,
585                                                         "%s: line %d: domain pattern already specified.\n",
586                                                         fname, lineno );
587                                                 acl_usage();
588                                         }
589
590                                         b->a_domain_style = sty;
591                                         if (sty == ACL_STYLE_REGEX) {
592                                                 regtest(fname, lineno, right);
593                                         }
594                                         b->a_domain_pat = ch_strdup( right );
595                                         continue;
596                                 }
597
598                                 if ( strcasecmp( left, "sockurl" ) == 0 ) {
599                                         if( b->a_sockurl_pat != NULL ) {
600                                                 fprintf( stderr,
601                                                         "%s: line %d: sockurl pattern already specified.\n",
602                                                         fname, lineno );
603                                                 acl_usage();
604                                         }
605
606                                         b->a_sockurl_style = sty;
607                                         if (sty == ACL_STYLE_REGEX) {
608                                                 regtest(fname, lineno, right);
609                                         }
610                                         b->a_sockurl_pat = ch_strdup( right );
611                                         continue;
612                                 }
613
614                                 if ( strcasecmp( left, "set" ) == 0 ) {
615                                         if( b->a_set_pat != NULL ) {
616                                                 fprintf( stderr,
617                                                         "%s: line %d: set attribute already specified.\n",
618                                                         fname, lineno );
619                                                 acl_usage();
620                                         }
621
622                                         if ( right == NULL || *right == '\0' ) {
623                                                 fprintf( stderr,
624                                                         "%s: line %d: no set is defined\n",
625                                                         fname, lineno );
626                                                 acl_usage();
627                                         }
628
629                                         b->a_set_style = sty;
630                                         b->a_set_pat = ch_strdup(right);
631
632                                         continue;
633                                 }
634
635 #ifdef SLAPD_ACI_ENABLED
636                                 if ( strcasecmp( left, "aci" ) == 0 ) {
637                                         if( b->a_aci_at != NULL ) {
638                                                 fprintf( stderr,
639                                                         "%s: line %d: aci attribute already specified.\n",
640                                                         fname, lineno );
641                                                 acl_usage();
642                                         }
643
644                                         if ( right != NULL && *right != '\0' ) {
645                                                 rc = slap_str2ad( right, &b->a_aci_at, &text );
646
647                                                 if( rc != LDAP_SUCCESS ) {
648                                                         fprintf( stderr,
649                                                                 "%s: line %d: aci \"%s\": %s\n",
650                                                                 fname, lineno, right, text );
651                                                         acl_usage();
652                                                 }
653
654                                         } else {
655                                                 rc = slap_str2ad( SLAPD_ACI_ATTR, &b->a_aci_at, &text );
656
657                                                 if( rc != LDAP_SUCCESS ) {
658                                                         fprintf( stderr,
659                                                                 "%s: line %d: aci \"%s\": %s\n",
660                                                                 fname, lineno, SLAPD_ACI_ATTR, text );
661                                                         acl_usage();
662                                                 }
663                                         }
664
665                                         if( !is_at_syntax( b->a_aci_at->ad_type,
666                                                 SLAPD_ACI_SYNTAX) )
667                                         {
668                                                 fprintf( stderr,
669                                                         "%s: line %d: aci \"%s\": inappropriate syntax: %s\n",
670                                                         fname, lineno, right,
671                                                         b->a_aci_at->ad_type->sat_syntax_oid );
672                                                 acl_usage();
673                                         }
674
675                                         continue;
676                                 }
677 #endif /* SLAPD_ACI_ENABLED */
678
679                                 if ( strcasecmp( left, "ssf" ) == 0 ) {
680                                         if( b->a_authz.sai_ssf ) {
681                                                 fprintf( stderr,
682                                                         "%s: line %d: ssf attribute already specified.\n",
683                                                         fname, lineno );
684                                                 acl_usage();
685                                         }
686
687                                         if ( right == NULL || *right == '\0' ) {
688                                                 fprintf( stderr,
689                                                         "%s: line %d: no ssf is defined\n",
690                                                         fname, lineno );
691                                                 acl_usage();
692                                         }
693
694                                         b->a_authz.sai_ssf = atoi( right );
695
696                                         if( !b->a_authz.sai_ssf ) {
697                                                 fprintf( stderr,
698                                                         "%s: line %d: invalid ssf value (%s)\n",
699                                                         fname, lineno, right );
700                                                 acl_usage();
701                                         }
702                                         continue;
703                                 }
704
705                                 if ( strcasecmp( left, "transport_ssf" ) == 0 ) {
706                                         if( b->a_authz.sai_transport_ssf ) {
707                                                 fprintf( stderr,
708                                                         "%s: line %d: transport_ssf attribute already specified.\n",
709                                                         fname, lineno );
710                                                 acl_usage();
711                                         }
712
713                                         if ( right == NULL || *right == '\0' ) {
714                                                 fprintf( stderr,
715                                                         "%s: line %d: no transport_ssf is defined\n",
716                                                         fname, lineno );
717                                                 acl_usage();
718                                         }
719
720                                         b->a_authz.sai_transport_ssf = atoi( right );
721
722                                         if( !b->a_authz.sai_transport_ssf ) {
723                                                 fprintf( stderr,
724                                                         "%s: line %d: invalid transport_ssf value (%s)\n",
725                                                         fname, lineno, right );
726                                                 acl_usage();
727                                         }
728                                         continue;
729                                 }
730
731                                 if ( strcasecmp( left, "tls_ssf" ) == 0 ) {
732                                         if( b->a_authz.sai_tls_ssf ) {
733                                                 fprintf( stderr,
734                                                         "%s: line %d: tls_ssf attribute already specified.\n",
735                                                         fname, lineno );
736                                                 acl_usage();
737                                         }
738
739                                         if ( right == NULL || *right == '\0' ) {
740                                                 fprintf( stderr,
741                                                         "%s: line %d: no tls_ssf is defined\n",
742                                                         fname, lineno );
743                                                 acl_usage();
744                                         }
745
746                                         b->a_authz.sai_tls_ssf = atoi( right );
747
748                                         if( !b->a_authz.sai_tls_ssf ) {
749                                                 fprintf( stderr,
750                                                         "%s: line %d: invalid tls_ssf value (%s)\n",
751                                                         fname, lineno, right );
752                                                 acl_usage();
753                                         }
754                                         continue;
755                                 }
756
757                                 if ( strcasecmp( left, "sasl_ssf" ) == 0 ) {
758                                         if( b->a_authz.sai_sasl_ssf ) {
759                                                 fprintf( stderr,
760                                                         "%s: line %d: sasl_ssf attribute already specified.\n",
761                                                         fname, lineno );
762                                                 acl_usage();
763                                         }
764
765                                         if ( right == NULL || *right == '\0' ) {
766                                                 fprintf( stderr,
767                                                         "%s: line %d: no sasl_ssf is defined\n",
768                                                         fname, lineno );
769                                                 acl_usage();
770                                         }
771
772                                         b->a_authz.sai_sasl_ssf = atoi( right );
773
774                                         if( !b->a_authz.sai_sasl_ssf ) {
775                                                 fprintf( stderr,
776                                                         "%s: line %d: invalid sasl_ssf value (%s)\n",
777                                                         fname, lineno, right );
778                                                 acl_usage();
779                                         }
780                                         continue;
781                                 }
782
783                                 if( right != NULL ) {
784                                         /* unsplit */
785                                         right[-1] = '=';
786                                 }
787                                 break;
788                         }
789
790                         if( i == argc || ( strcasecmp( left, "stop" ) == 0 )) { 
791                                 /* out of arguments or plain stop */
792
793                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
794                                 b->a_type = ACL_STOP;
795
796                                 access_append( &a->acl_access, b );
797                                 continue;
798                         }
799
800                         if( strcasecmp( left, "continue" ) == 0 ) {
801                                 /* plain continue */
802
803                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
804                                 b->a_type = ACL_CONTINUE;
805
806                                 access_append( &a->acl_access, b );
807                                 continue;
808                         }
809
810                         if( strcasecmp( left, "break" ) == 0 ) {
811                                 /* plain continue */
812
813                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
814                                 b->a_type = ACL_BREAK;
815
816                                 access_append( &a->acl_access, b );
817                                 continue;
818                         }
819
820                         if ( strcasecmp( left, "by" ) == 0 ) {
821                                 /* we've gone too far */
822                                 --i;
823                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
824                                 b->a_type = ACL_STOP;
825
826                                 access_append( &a->acl_access, b );
827                                 continue;
828                         }
829
830                         /* get <access> */
831                         if( strncasecmp( left, "self", 4 ) == 0 ) {
832                                 b->a_dn_self = 1;
833                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( &left[4] ) );
834
835                         } else {
836                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( left ) );
837                         }
838
839                         if( ACL_IS_INVALID( b->a_access_mask ) ) {
840                                 fprintf( stderr,
841                                         "%s: line %d: expecting <access> got \"%s\"\n",
842                                         fname, lineno, left );
843                                 acl_usage();
844                         }
845
846                         b->a_type = ACL_STOP;
847
848                         if( ++i == argc ) {
849                                 /* out of arguments or plain stop */
850                                 access_append( &a->acl_access, b );
851                                 continue;
852                         }
853
854                         if( strcasecmp( argv[i], "continue" ) == 0 ) {
855                                 /* plain continue */
856                                 b->a_type = ACL_CONTINUE;
857
858                         } else if( strcasecmp( argv[i], "break" ) == 0 ) {
859                                 /* plain continue */
860                                 b->a_type = ACL_BREAK;
861
862                         } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
863                                 /* gone to far */
864                                 i--;
865                         }
866
867                         access_append( &a->acl_access, b );
868
869                 } else {
870                         fprintf( stderr,
871                     "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
872                             fname, lineno, argv[i] );
873                         acl_usage();
874                 }
875         }
876
877         /* if we have no real access clause, complain and do nothing */
878         if ( a == NULL ) {
879                         fprintf( stderr,
880                                 "%s: line %d: warning: no access clause(s) specified in access line\n",
881                             fname, lineno );
882
883         } else {
884 #ifdef LDAP_DEBUG
885                 if (ldap_debug & LDAP_DEBUG_ACL)
886                         print_acl(be, a);
887 #endif
888         
889                 if ( a->acl_access == NULL ) {
890                         fprintf( stderr,
891                         "%s: line %d: warning: no by clause(s) specified in access line\n",
892                             fname, lineno );
893                 }
894
895                 if ( be != NULL ) {
896                         acl_append( &be->be_acl, a );
897                 } else {
898                         acl_append( &global_acl, a );
899                 }
900         }
901 }
902
903 char *
904 accessmask2str( slap_mask_t mask, char *buf )
905 {
906         int none=1;
907
908         assert( buf != NULL );
909
910         if ( ACL_IS_INVALID( mask ) ) {
911                 return "invalid";
912         }
913
914         buf[0] = '\0';
915
916         if ( ACL_IS_LEVEL( mask ) ) {
917                 if ( ACL_LVL_IS_NONE(mask) ) {
918                         strcat( buf, "none" );
919
920                 } else if ( ACL_LVL_IS_AUTH(mask) ) {
921                         strcat( buf, "auth" );
922
923                 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
924                         strcat( buf, "compare" );
925
926                 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
927                         strcat( buf, "search" );
928
929                 } else if ( ACL_LVL_IS_READ(mask) ) {
930                         strcat( buf, "read" );
931
932                 } else if ( ACL_LVL_IS_WRITE(mask) ) {
933                         strcat( buf, "write" );
934                 } else {
935                         strcat( buf, "unknown" );
936                 }
937                 
938                 strcat(buf, " (");
939         }
940
941         if( ACL_IS_ADDITIVE( mask ) ) {
942                 strcat( buf, "+" );
943
944         } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
945                 strcat( buf, "-" );
946
947         } else {
948                 strcat( buf, "=" );
949         }
950
951         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
952                 none = 0;
953                 strcat( buf, "w" );
954         } 
955
956         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
957                 none = 0;
958                 strcat( buf, "r" );
959         } 
960
961         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
962                 none = 0;
963                 strcat( buf, "s" );
964         } 
965
966         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
967                 none = 0;
968                 strcat( buf, "c" );
969         } 
970
971         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
972                 none = 0;
973                 strcat( buf, "x" );
974         } 
975
976         if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
977                 none = 0;
978                 strcat( buf, "n" );
979         } 
980
981         if ( none ) {
982                 strcat( buf, "0" );
983         }
984
985         if ( ACL_IS_LEVEL( mask ) ) {
986                 strcat(buf, ")");
987         } 
988         return buf;
989 }
990
991 slap_mask_t
992 str2accessmask( const char *str )
993 {
994         slap_mask_t     mask;
995
996         if( !ASCII_ALPHA(str[0]) ) {
997                 int i;
998
999                 if ( str[0] == '=' ) {
1000                         ACL_INIT(mask);
1001
1002                 } else if( str[0] == '+' ) {
1003                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
1004
1005                 } else if( str[0] == '-' ) {
1006                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
1007
1008                 } else {
1009                         ACL_INVALIDATE(mask);
1010                         return mask;
1011                 }
1012
1013                 for( i=1; str[i] != '\0'; i++ ) {
1014                         if( TOLOWER(str[i]) == 'w' ) {
1015                                 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1016
1017                         } else if( TOLOWER(str[i]) == 'r' ) {
1018                                 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1019
1020                         } else if( TOLOWER(str[i]) == 's' ) {
1021                                 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1022
1023                         } else if( TOLOWER(str[i]) == 'c' ) {
1024                                 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1025
1026                         } else if( TOLOWER(str[i]) == 'x' ) {
1027                                 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1028
1029                         } else if( str[i] != '0' ) {
1030                                 ACL_INVALIDATE(mask);
1031                                 return mask;
1032                         }
1033                 }
1034
1035                 return mask;
1036         }
1037
1038         if ( strcasecmp( str, "none" ) == 0 ) {
1039                 ACL_LVL_ASSIGN_NONE(mask);
1040
1041         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1042                 ACL_LVL_ASSIGN_AUTH(mask);
1043
1044         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1045                 ACL_LVL_ASSIGN_COMPARE(mask);
1046
1047         } else if ( strcasecmp( str, "search" ) == 0 ) {
1048                 ACL_LVL_ASSIGN_SEARCH(mask);
1049
1050         } else if ( strcasecmp( str, "read" ) == 0 ) {
1051                 ACL_LVL_ASSIGN_READ(mask);
1052
1053         } else if ( strcasecmp( str, "write" ) == 0 ) {
1054                 ACL_LVL_ASSIGN_WRITE(mask);
1055
1056         } else {
1057                 ACL_INVALIDATE( mask );
1058         }
1059
1060         return mask;
1061 }
1062
1063 static void
1064 acl_usage( void )
1065 {
1066         fprintf( stderr, "\n"
1067                 "<access clause> ::= access to <what> "
1068                                 "[ by <who> <access> <control> ]+ \n"
1069                 "<what> ::= * | [dn=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n"
1070                 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
1071                 "<attr> ::= <attrname> | entry | children\n"
1072                 "<who> ::= [ * | anonymous | users | self | dn=<regex> ]\n"
1073                         "\t[dnattr=<attrname>]\n"
1074                         "\t[group[/<objectclass>[/<attrname>]]=<regex>]\n"
1075                         "\t[peername=<regex>] [sockname=<regex>]\n"
1076                         "\t[domain=<regex>] [sockurl=<regex>]\n"
1077 #ifdef SLAPD_ACI_ENABLED
1078                         "\t[aci=<attrname>]\n"
1079 #endif
1080                         "\t[ssf=<n>] [transport_ssf=<n>] [tls_ssf=<n>] [sasl_ssf=<n>]\n"
1081                 "<access> ::= [self]{<level>|<priv>}\n"
1082                 "<level> ::= none | auth | compare | search | read | write\n"
1083                 "<priv> ::= {=|+|-}{w|r|s|c|x}+\n"
1084                 "<control> ::= [ stop | continue | break ]\n"
1085                 );
1086         exit( EXIT_FAILURE );
1087 }
1088
1089 static void
1090 split(
1091     char        *line,
1092     int         splitchar,
1093     char        **left,
1094     char        **right
1095 )
1096 {
1097         *left = line;
1098         if ( (*right = strchr( line, splitchar )) != NULL ) {
1099                 *((*right)++) = '\0';
1100         }
1101 }
1102
1103 static void
1104 access_append( Access **l, Access *a )
1105 {
1106         for ( ; *l != NULL; l = &(*l)->a_next )
1107                 ;       /* NULL */
1108
1109         *l = a;
1110 }
1111
1112 void
1113 acl_append( AccessControl **l, AccessControl *a )
1114 {
1115         for ( ; *l != NULL; l = &(*l)->acl_next )
1116                 ;       /* NULL */
1117
1118         *l = a;
1119 }
1120
1121 char *
1122 access2str( slap_access_t access )
1123 {
1124         if ( access == ACL_NONE ) {
1125                 return "none";
1126
1127         } else if ( access == ACL_AUTH ) {
1128                 return "auth";
1129
1130         } else if ( access == ACL_COMPARE ) {
1131                 return "compare";
1132
1133         } else if ( access == ACL_SEARCH ) {
1134                 return "search";
1135
1136         } else if ( access == ACL_READ ) {
1137                 return "read";
1138
1139         } else if ( access == ACL_WRITE ) {
1140                 return "write";
1141         }
1142
1143         return "unknown";
1144 }
1145
1146 slap_access_t
1147 str2access( const char *str )
1148 {
1149         if ( strcasecmp( str, "none" ) == 0 ) {
1150                 return ACL_NONE;
1151
1152         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1153                 return ACL_AUTH;
1154
1155         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1156                 return ACL_COMPARE;
1157
1158         } else if ( strcasecmp( str, "search" ) == 0 ) {
1159                 return ACL_SEARCH;
1160
1161         } else if ( strcasecmp( str, "read" ) == 0 ) {
1162                 return ACL_READ;
1163
1164         } else if ( strcasecmp( str, "write" ) == 0 ) {
1165                 return ACL_WRITE;
1166         }
1167
1168         return( ACL_INVALID_ACCESS );
1169 }
1170
1171 #ifdef LDAP_DEBUG
1172
1173 static char *style_strings[5] = {
1174                         "regex",
1175                         "base",
1176                         "one",
1177                         "subtree",
1178                         "children"
1179                 };
1180
1181
1182 static void
1183 print_access( Access *b )
1184 {
1185         char maskbuf[ACCESSMASK_MAXLEN];
1186
1187         fprintf( stderr, "\tby" );
1188
1189         if ( b->a_dn_pat != NULL ) {
1190                 if( strcmp(b->a_dn_pat, "*") == 0
1191                         || strcmp(b->a_dn_pat, "users") == 0 
1192                         || strcmp(b->a_dn_pat, "anonymous") == 0 
1193                         || strcmp(b->a_dn_pat, "self") == 0 )
1194                 {
1195                         fprintf( stderr, " %s", b->a_dn_pat );
1196
1197                 } else {
1198                         fprintf( stderr, " dn.%s=%s", style_strings[b->a_dn_style], b->a_dn_pat );
1199                 }
1200         }
1201
1202         if ( b->a_dn_at != NULL ) {
1203                 fprintf( stderr, " dnattr=%s", b->a_dn_at->ad_cname->bv_val );
1204         }
1205
1206         if ( b->a_group_pat != NULL ) {
1207                 fprintf( stderr, " group=%s", b->a_group_pat );
1208
1209                 if ( b->a_group_oc ) {
1210                         fprintf( stderr, " objectClass: %s",
1211                                 b->a_group_oc->soc_oclass.oc_oid );
1212
1213                         if ( b->a_group_at ) {
1214                                 fprintf( stderr, " attributeType: %s", b->a_group_at->ad_cname->bv_val );
1215                         }
1216                 }
1217     }
1218
1219         if ( b->a_peername_pat != NULL ) {
1220                 fprintf( stderr, " peername=%s", b->a_peername_pat );
1221         }
1222
1223         if ( b->a_sockname_pat != NULL ) {
1224                 fprintf( stderr, " sockname=%s", b->a_sockname_pat );
1225         }
1226
1227         if ( b->a_domain_pat != NULL ) {
1228                 fprintf( stderr, " domain=%s", b->a_domain_pat );
1229         }
1230
1231         if ( b->a_sockurl_pat != NULL ) {
1232                 fprintf( stderr, " sockurl=%s", b->a_sockurl_pat );
1233         }
1234
1235 #ifdef SLAPD_ACI_ENABLED
1236         if ( b->a_aci_at != NULL ) {
1237                 fprintf( stderr, " aci=%s", b->a_aci_at->ad_cname->bv_val );
1238         }
1239 #endif
1240
1241         /* Security Strength Factors */
1242         if ( b->a_authz.sai_ssf ) {
1243                 fprintf( stderr, " ssf=%u",
1244                         b->a_authz.sai_ssf );
1245         }
1246         if ( b->a_authz.sai_transport_ssf ) {
1247                 fprintf( stderr, " transport_ssf=%u",
1248                         b->a_authz.sai_transport_ssf );
1249         }
1250         if ( b->a_authz.sai_tls_ssf ) {
1251                 fprintf( stderr, " tls_ssf=%u",
1252                         b->a_authz.sai_tls_ssf );
1253         }
1254         if ( b->a_authz.sai_sasl_ssf ) {
1255                 fprintf( stderr, " sasl_ssf=%u",
1256                         b->a_authz.sai_sasl_ssf );
1257         }
1258
1259         fprintf( stderr, " %s%s",
1260                 b->a_dn_self ? "self" : "",
1261                 accessmask2str( b->a_access_mask, maskbuf ) );
1262
1263         if( b->a_type == ACL_BREAK ) {
1264                 fprintf( stderr, " break" );
1265
1266         } else if( b->a_type == ACL_CONTINUE ) {
1267                 fprintf( stderr, " continue" );
1268
1269         } else if( b->a_type != ACL_STOP ) {
1270                 fprintf( stderr, " unknown-control" );
1271         }
1272
1273         fprintf( stderr, "\n" );
1274 }
1275
1276
1277 static void
1278 print_acl( Backend *be, AccessControl *a )
1279 {
1280         int             to = 0;
1281         Access  *b;
1282
1283         fprintf( stderr, "%s ACL: access to",
1284                 be == NULL ? "Global" : "Backend" );
1285
1286         if ( a->acl_dn_pat != NULL ) {
1287                 to++;
1288                 fprintf( stderr, " dn.%s=%s\n",
1289                         style_strings[a->acl_dn_style], a->acl_dn_pat );
1290         }
1291
1292         if ( a->acl_filter != NULL ) {
1293                 to++;
1294                 fprintf( stderr, " filter=" );
1295                 filter_print( a->acl_filter );
1296                 fprintf( stderr, "\n" );
1297         }
1298
1299         if ( a->acl_attrs != NULL ) {
1300                 int     i, first = 1;
1301                 to++;
1302
1303                 fprintf( stderr, " attrs=" );
1304                 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
1305                         if ( ! first ) {
1306                                 fprintf( stderr, "," );
1307                         }
1308                         fprintf( stderr, a->acl_attrs[i] );
1309                         first = 0;
1310                 }
1311                 fprintf(  stderr, "\n" );
1312         }
1313
1314         if( !to ) {
1315                 fprintf( stderr, " *\n" );
1316         }
1317
1318         for ( b = a->acl_access; b != NULL; b = b->a_next ) {
1319                 print_access( b );
1320         }
1321
1322         fprintf( stderr, "\n" );
1323 }
1324
1325 #endif /* LDAP_DEBUG */