]> git.sur5r.net Git - openldap/blob - servers/slapd/aclparse.c
Import changes from devel
[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                                         {
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 ( strcasecmp( left, "ssf" ) == 0 ) {
676                                         if( b->a_authz.sai_ssf ) {
677                                                 fprintf( stderr,
678                                                         "%s: line %d: ssf attribute already specified.\n",
679                                                         fname, lineno );
680                                                 acl_usage();
681                                         }
682
683                                         if ( right == NULL || *right == '\0' ) {
684                                                 fprintf( stderr,
685                                                         "%s: line %d: no ssf is defined\n",
686                                                         fname, lineno );
687                                                 acl_usage();
688                                         }
689
690                                         b->a_authz.sai_ssf = atoi( right );
691
692                                         if( !b->a_authz.sai_ssf ) {
693                                                 fprintf( stderr,
694                                                         "%s: line %d: invalid ssf value (%s)\n",
695                                                         fname, lineno, right );
696                                                 acl_usage();
697                                         }
698                                         continue;
699                                 }
700
701                                 if ( strcasecmp( left, "transport_ssf" ) == 0 ) {
702                                         if( b->a_authz.sai_transport_ssf ) {
703                                                 fprintf( stderr,
704                                                         "%s: line %d: transport_ssf attribute already specified.\n",
705                                                         fname, lineno );
706                                                 acl_usage();
707                                         }
708
709                                         if ( right == NULL || *right == '\0' ) {
710                                                 fprintf( stderr,
711                                                         "%s: line %d: no transport_ssf is defined\n",
712                                                         fname, lineno );
713                                                 acl_usage();
714                                         }
715
716                                         b->a_authz.sai_transport_ssf = atoi( right );
717
718                                         if( !b->a_authz.sai_transport_ssf ) {
719                                                 fprintf( stderr,
720                                                         "%s: line %d: invalid transport_ssf value (%s)\n",
721                                                         fname, lineno, right );
722                                                 acl_usage();
723                                         }
724                                         continue;
725                                 }
726
727                                 if ( strcasecmp( left, "tls_ssf" ) == 0 ) {
728                                         if( b->a_authz.sai_tls_ssf ) {
729                                                 fprintf( stderr,
730                                                         "%s: line %d: tls_ssf attribute already specified.\n",
731                                                         fname, lineno );
732                                                 acl_usage();
733                                         }
734
735                                         if ( right == NULL || *right == '\0' ) {
736                                                 fprintf( stderr,
737                                                         "%s: line %d: no tls_ssf is defined\n",
738                                                         fname, lineno );
739                                                 acl_usage();
740                                         }
741
742                                         b->a_authz.sai_tls_ssf = atoi( right );
743
744                                         if( !b->a_authz.sai_tls_ssf ) {
745                                                 fprintf( stderr,
746                                                         "%s: line %d: invalid tls_ssf value (%s)\n",
747                                                         fname, lineno, right );
748                                                 acl_usage();
749                                         }
750                                         continue;
751                                 }
752
753                                 if ( strcasecmp( left, "sasl_ssf" ) == 0 ) {
754                                         if( b->a_authz.sai_sasl_ssf ) {
755                                                 fprintf( stderr,
756                                                         "%s: line %d: sasl_ssf attribute already specified.\n",
757                                                         fname, lineno );
758                                                 acl_usage();
759                                         }
760
761                                         if ( right == NULL || *right == '\0' ) {
762                                                 fprintf( stderr,
763                                                         "%s: line %d: no sasl_ssf is defined\n",
764                                                         fname, lineno );
765                                                 acl_usage();
766                                         }
767
768                                         b->a_authz.sai_sasl_ssf = atoi( right );
769
770                                         if( !b->a_authz.sai_sasl_ssf ) {
771                                                 fprintf( stderr,
772                                                         "%s: line %d: invalid sasl_ssf value (%s)\n",
773                                                         fname, lineno, right );
774                                                 acl_usage();
775                                         }
776                                         continue;
777                                 }
778
779                                 if( right != NULL ) {
780                                         /* unsplit */
781                                         right[-1] = '=';
782                                 }
783                                 break;
784                         }
785
786                         if( i == argc || ( strcasecmp( left, "stop" ) == 0 )) { 
787                                 /* out of arguments or plain stop */
788
789                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
790                                 b->a_type = ACL_STOP;
791
792                                 access_append( &a->acl_access, b );
793                                 continue;
794                         }
795
796                         if( strcasecmp( left, "continue" ) == 0 ) {
797                                 /* plain continue */
798
799                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
800                                 b->a_type = ACL_CONTINUE;
801
802                                 access_append( &a->acl_access, b );
803                                 continue;
804                         }
805
806                         if( strcasecmp( left, "break" ) == 0 ) {
807                                 /* plain continue */
808
809                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
810                                 b->a_type = ACL_BREAK;
811
812                                 access_append( &a->acl_access, b );
813                                 continue;
814                         }
815
816                         if ( strcasecmp( left, "by" ) == 0 ) {
817                                 /* we've gone too far */
818                                 --i;
819                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
820                                 b->a_type = ACL_STOP;
821
822                                 access_append( &a->acl_access, b );
823                                 continue;
824                         }
825
826                         /* get <access> */
827                         if( strncasecmp( left, "self", 4 ) == 0 ) {
828                                 b->a_dn_self = 1;
829                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( &left[4] ) );
830
831                         } else {
832                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( left ) );
833                         }
834
835                         if( ACL_IS_INVALID( b->a_access_mask ) ) {
836                                 fprintf( stderr,
837                                         "%s: line %d: expecting <access> got \"%s\"\n",
838                                         fname, lineno, left );
839                                 acl_usage();
840                         }
841
842                         b->a_type = ACL_STOP;
843
844                         if( ++i == argc ) {
845                                 /* out of arguments or plain stop */
846                                 access_append( &a->acl_access, b );
847                                 continue;
848                         }
849
850                         if( strcasecmp( argv[i], "continue" ) == 0 ) {
851                                 /* plain continue */
852                                 b->a_type = ACL_CONTINUE;
853
854                         } else if( strcasecmp( argv[i], "break" ) == 0 ) {
855                                 /* plain continue */
856                                 b->a_type = ACL_BREAK;
857
858                         } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
859                                 /* gone to far */
860                                 i--;
861                         }
862
863                         access_append( &a->acl_access, b );
864
865                 } else {
866                         fprintf( stderr,
867                     "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
868                             fname, lineno, argv[i] );
869                         acl_usage();
870                 }
871         }
872
873         /* if we have no real access clause, complain and do nothing */
874         if ( a == NULL ) {
875                         fprintf( stderr,
876                                 "%s: line %d: warning: no access clause(s) specified in access line\n",
877                             fname, lineno );
878
879         } else {
880 #ifdef LDAP_DEBUG
881                 if (ldap_debug & LDAP_DEBUG_ACL)
882                         print_acl(be, a);
883 #endif
884         
885                 if ( a->acl_access == NULL ) {
886                         fprintf( stderr,
887                         "%s: line %d: warning: no by clause(s) specified in access line\n",
888                             fname, lineno );
889                 }
890
891                 if ( be != NULL ) {
892                         acl_append( &be->be_acl, a );
893                 } else {
894                         acl_append( &global_acl, a );
895                 }
896         }
897 }
898
899 char *
900 accessmask2str( slap_mask_t mask, char *buf )
901 {
902         int none=1;
903
904         assert( buf != NULL );
905
906         if ( ACL_IS_INVALID( mask ) ) {
907                 return "invalid";
908         }
909
910         buf[0] = '\0';
911
912         if ( ACL_IS_LEVEL( mask ) ) {
913                 if ( ACL_LVL_IS_NONE(mask) ) {
914                         strcat( buf, "none" );
915
916                 } else if ( ACL_LVL_IS_AUTH(mask) ) {
917                         strcat( buf, "auth" );
918
919                 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
920                         strcat( buf, "compare" );
921
922                 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
923                         strcat( buf, "search" );
924
925                 } else if ( ACL_LVL_IS_READ(mask) ) {
926                         strcat( buf, "read" );
927
928                 } else if ( ACL_LVL_IS_WRITE(mask) ) {
929                         strcat( buf, "write" );
930                 } else {
931                         strcat( buf, "unknown" );
932                 }
933                 
934                 strcat(buf, " (");
935         }
936
937         if( ACL_IS_ADDITIVE( mask ) ) {
938                 strcat( buf, "+" );
939
940         } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
941                 strcat( buf, "-" );
942
943         } else {
944                 strcat( buf, "=" );
945         }
946
947         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
948                 none = 0;
949                 strcat( buf, "w" );
950         } 
951
952         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
953                 none = 0;
954                 strcat( buf, "r" );
955         } 
956
957         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
958                 none = 0;
959                 strcat( buf, "s" );
960         } 
961
962         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
963                 none = 0;
964                 strcat( buf, "c" );
965         } 
966
967         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
968                 none = 0;
969                 strcat( buf, "x" );
970         } 
971
972         if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
973                 none = 0;
974                 strcat( buf, "n" );
975         } 
976
977         if ( none ) {
978                 strcat( buf, "0" );
979         }
980
981         if ( ACL_IS_LEVEL( mask ) ) {
982                 strcat(buf, ")");
983         } 
984         return buf;
985 }
986
987 slap_mask_t
988 str2accessmask( const char *str )
989 {
990         slap_mask_t     mask;
991
992         if( !ASCII_ALPHA(str[0]) ) {
993                 int i;
994
995                 if ( str[0] == '=' ) {
996                         ACL_INIT(mask);
997
998                 } else if( str[0] == '+' ) {
999                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
1000
1001                 } else if( str[0] == '-' ) {
1002                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
1003
1004                 } else {
1005                         ACL_INVALIDATE(mask);
1006                         return mask;
1007                 }
1008
1009                 for( i=1; str[i] != '\0'; i++ ) {
1010                         if( TOLOWER(str[i]) == 'w' ) {
1011                                 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1012
1013                         } else if( TOLOWER(str[i]) == 'r' ) {
1014                                 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1015
1016                         } else if( TOLOWER(str[i]) == 's' ) {
1017                                 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1018
1019                         } else if( TOLOWER(str[i]) == 'c' ) {
1020                                 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1021
1022                         } else if( TOLOWER(str[i]) == 'x' ) {
1023                                 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1024
1025                         } else if( str[i] != '0' ) {
1026                                 ACL_INVALIDATE(mask);
1027                                 return mask;
1028                         }
1029                 }
1030
1031                 return mask;
1032         }
1033
1034         if ( strcasecmp( str, "none" ) == 0 ) {
1035                 ACL_LVL_ASSIGN_NONE(mask);
1036
1037         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1038                 ACL_LVL_ASSIGN_AUTH(mask);
1039
1040         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1041                 ACL_LVL_ASSIGN_COMPARE(mask);
1042
1043         } else if ( strcasecmp( str, "search" ) == 0 ) {
1044                 ACL_LVL_ASSIGN_SEARCH(mask);
1045
1046         } else if ( strcasecmp( str, "read" ) == 0 ) {
1047                 ACL_LVL_ASSIGN_READ(mask);
1048
1049         } else if ( strcasecmp( str, "write" ) == 0 ) {
1050                 ACL_LVL_ASSIGN_WRITE(mask);
1051
1052         } else {
1053                 ACL_INVALIDATE( mask );
1054         }
1055
1056         return mask;
1057 }
1058
1059 static void
1060 acl_usage( void )
1061 {
1062         fprintf( stderr, "\n"
1063                 "<access clause> ::= access to <what> "
1064                                 "[ by <who> <access> <control> ]+ \n"
1065                 "<what> ::= * | [dn=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n"
1066                 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
1067                 "<attr> ::= <attrname> | entry | children\n"
1068                 "<who> ::= [ * | anonymous | users | self | dn=<regex> ]\n"
1069                         "\t[dnattr=<attrname>]\n"
1070                         "\t[group[/<objectclass>[/<attrname>]]=<regex>]\n"
1071                         "\t[peername=<regex>] [sockname=<regex>]\n"
1072                         "\t[domain=<regex>] [sockurl=<regex>]\n"
1073 #ifdef SLAPD_ACI_ENABLED
1074                         "\t[aci=<attrname>]\n"
1075 #endif
1076                         "\t[ssf=<n>] [transport_ssf=<n>] [tls_ssf=<n>] [sasl_ssf=<n>]\n"
1077                 "<access> ::= [self]{<level>|<priv>}\n"
1078                 "<level> ::= none | auth | compare | search | read | write\n"
1079                 "<priv> ::= {=|+|-}{w|r|s|c|x}+\n"
1080                 "<control> ::= [ stop | continue | break ]\n"
1081                 );
1082         exit( EXIT_FAILURE );
1083 }
1084
1085 static void
1086 split(
1087     char        *line,
1088     int         splitchar,
1089     char        **left,
1090     char        **right
1091 )
1092 {
1093         *left = line;
1094         if ( (*right = strchr( line, splitchar )) != NULL ) {
1095                 *((*right)++) = '\0';
1096         }
1097 }
1098
1099 static void
1100 access_append( Access **l, Access *a )
1101 {
1102         for ( ; *l != NULL; l = &(*l)->a_next )
1103                 ;       /* NULL */
1104
1105         *l = a;
1106 }
1107
1108 void
1109 acl_append( AccessControl **l, AccessControl *a )
1110 {
1111         for ( ; *l != NULL; l = &(*l)->acl_next )
1112                 ;       /* NULL */
1113
1114         *l = a;
1115 }
1116
1117 char *
1118 access2str( slap_access_t access )
1119 {
1120         if ( access == ACL_NONE ) {
1121                 return "none";
1122
1123         } else if ( access == ACL_AUTH ) {
1124                 return "auth";
1125
1126         } else if ( access == ACL_COMPARE ) {
1127                 return "compare";
1128
1129         } else if ( access == ACL_SEARCH ) {
1130                 return "search";
1131
1132         } else if ( access == ACL_READ ) {
1133                 return "read";
1134
1135         } else if ( access == ACL_WRITE ) {
1136                 return "write";
1137         }
1138
1139         return "unknown";
1140 }
1141
1142 slap_access_t
1143 str2access( const char *str )
1144 {
1145         if ( strcasecmp( str, "none" ) == 0 ) {
1146                 return ACL_NONE;
1147
1148         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1149                 return ACL_AUTH;
1150
1151         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1152                 return ACL_COMPARE;
1153
1154         } else if ( strcasecmp( str, "search" ) == 0 ) {
1155                 return ACL_SEARCH;
1156
1157         } else if ( strcasecmp( str, "read" ) == 0 ) {
1158                 return ACL_READ;
1159
1160         } else if ( strcasecmp( str, "write" ) == 0 ) {
1161                 return ACL_WRITE;
1162         }
1163
1164         return( ACL_INVALID_ACCESS );
1165 }
1166
1167 #ifdef LDAP_DEBUG
1168
1169 static char *style_strings[5] = {
1170                         "regex",
1171                         "base",
1172                         "one",
1173                         "subtree",
1174                         "children"
1175                 };
1176
1177
1178 static void
1179 print_access( Access *b )
1180 {
1181         char maskbuf[ACCESSMASK_MAXLEN];
1182
1183         fprintf( stderr, "\tby" );
1184
1185         if ( b->a_dn_pat != NULL ) {
1186                 if( strcmp(b->a_dn_pat, "*") == 0
1187                         || strcmp(b->a_dn_pat, "users") == 0 
1188                         || strcmp(b->a_dn_pat, "anonymous") == 0 
1189                         || strcmp(b->a_dn_pat, "self") == 0 )
1190                 {
1191                         fprintf( stderr, " %s", b->a_dn_pat );
1192
1193                 } else {
1194                         fprintf( stderr, " dn.%s=%s", style_strings[b->a_dn_style], b->a_dn_pat );
1195                 }
1196         }
1197
1198         if ( b->a_dn_at != NULL ) {
1199                 fprintf( stderr, " dnattr=%s", b->a_dn_at->ad_cname->bv_val );
1200         }
1201
1202         if ( b->a_group_pat != NULL ) {
1203                 fprintf( stderr, " group=%s", b->a_group_pat );
1204
1205                 if ( b->a_group_oc ) {
1206                         fprintf( stderr, " objectClass: %s",
1207                                 b->a_group_oc->soc_oclass.oc_oid );
1208
1209                         if ( b->a_group_at ) {
1210                                 fprintf( stderr, " attributeType: %s", b->a_group_at->ad_cname->bv_val );
1211                         }
1212                 }
1213     }
1214
1215         if ( b->a_peername_pat != NULL ) {
1216                 fprintf( stderr, " peername=%s", b->a_peername_pat );
1217         }
1218
1219         if ( b->a_sockname_pat != NULL ) {
1220                 fprintf( stderr, " sockname=%s", b->a_sockname_pat );
1221         }
1222
1223         if ( b->a_domain_pat != NULL ) {
1224                 fprintf( stderr, " domain=%s", b->a_domain_pat );
1225         }
1226
1227         if ( b->a_sockurl_pat != NULL ) {
1228                 fprintf( stderr, " sockurl=%s", b->a_sockurl_pat );
1229         }
1230
1231 #ifdef SLAPD_ACI_ENABLED
1232         if ( b->a_aci_at != NULL ) {
1233                 fprintf( stderr, " aci=%s", b->a_aci_at->ad_cname->bv_val );
1234         }
1235 #endif
1236
1237         /* Security Strength Factors */
1238         if ( b->a_authz.sai_ssf ) {
1239                 fprintf( stderr, " ssf=%u",
1240                         b->a_authz.sai_ssf );
1241         }
1242         if ( b->a_authz.sai_transport_ssf ) {
1243                 fprintf( stderr, " transport_ssf=%u",
1244                         b->a_authz.sai_transport_ssf );
1245         }
1246         if ( b->a_authz.sai_tls_ssf ) {
1247                 fprintf( stderr, " tls_ssf=%u",
1248                         b->a_authz.sai_tls_ssf );
1249         }
1250         if ( b->a_authz.sai_sasl_ssf ) {
1251                 fprintf( stderr, " sasl_ssf=%u",
1252                         b->a_authz.sai_sasl_ssf );
1253         }
1254
1255         fprintf( stderr, " %s%s",
1256                 b->a_dn_self ? "self" : "",
1257                 accessmask2str( b->a_access_mask, maskbuf ) );
1258
1259         if( b->a_type == ACL_BREAK ) {
1260                 fprintf( stderr, " break" );
1261
1262         } else if( b->a_type == ACL_CONTINUE ) {
1263                 fprintf( stderr, " continue" );
1264
1265         } else if( b->a_type != ACL_STOP ) {
1266                 fprintf( stderr, " unknown-control" );
1267         }
1268
1269         fprintf( stderr, "\n" );
1270 }
1271
1272
1273 static void
1274 print_acl( Backend *be, AccessControl *a )
1275 {
1276         int             to = 0;
1277         Access  *b;
1278
1279         fprintf( stderr, "%s ACL: access to",
1280                 be == NULL ? "Global" : "Backend" );
1281
1282         if ( a->acl_dn_pat != NULL ) {
1283                 to++;
1284                 fprintf( stderr, " dn.%s=%s\n",
1285                         style_strings[a->acl_dn_style], a->acl_dn_pat );
1286         }
1287
1288         if ( a->acl_filter != NULL ) {
1289                 to++;
1290                 fprintf( stderr, " filter=" );
1291                 filter_print( a->acl_filter );
1292                 fprintf( stderr, "\n" );
1293         }
1294
1295         if ( a->acl_attrs != NULL ) {
1296                 int     i, first = 1;
1297                 to++;
1298
1299                 fprintf( stderr, " attrs=" );
1300                 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
1301                         if ( ! first ) {
1302                                 fprintf( stderr, "," );
1303                         }
1304                         fprintf( stderr, a->acl_attrs[i] );
1305                         first = 0;
1306                 }
1307                 fprintf(  stderr, "\n" );
1308         }
1309
1310         if( !to ) {
1311                 fprintf( stderr, " *\n" );
1312         }
1313
1314         for ( b = a->acl_access; b != NULL; b = b->a_next ) {
1315                 print_access( b );
1316         }
1317
1318         fprintf( stderr, "\n" );
1319 }
1320
1321 #endif /* LDAP_DEBUG */