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