]> git.sur5r.net Git - openldap/blob - servers/slapd/aclparse.c
61a31f1c577cab5d790e49b480f8337f50e4f9bc
[openldap] / servers / slapd / aclparse.c
1 /* acl.c - routines to parse and check acl's */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/ctype.h>
8 #include <ac/regex.h>
9 #include <ac/socket.h>
10 #include <ac/string.h>
11 #include <ac/unistd.h>
12
13 #include "slap.h"
14
15 static void             split(char *line, int splitchar, char **left, char **right);
16 static void             acl_append(struct acl **l, struct acl *a);
17 static void             access_append(struct access **l, struct access *a);
18 static void             acl_usage(void);
19 #ifdef LDAP_DEBUG
20 static void             print_acl(struct acl *a);
21 static void             print_access(struct access *b);
22 #endif
23
24 static int
25 regtest(char *fname, int lineno, char *pat) {
26         int e;
27         regex_t re;
28
29         char buf[512];
30         unsigned size;
31
32         char *sp;
33         char *dp;
34         int  flag;
35
36         sp = pat;
37         dp = buf;
38         size = 0;
39         buf[0] = '\0';
40
41         for (size = 0, flag = 0; (size < sizeof(buf)) && *sp; sp++) {
42                 if (flag) {
43                         if (*sp == '$'|| (*sp >= '0' && *sp <= '9')) {
44                                 *dp++ = *sp;
45                                 size++;
46                         }
47                         flag = 0;
48
49                 } else {
50                         if (*sp == '$') {
51                                 flag = 1;
52                         } else {
53                                 *dp++ = *sp;
54                                 size++;
55                         }
56                 }
57         }
58
59         *dp = '\0';
60         if ( size >= (sizeof(buf)-1) ) {
61                 fprintf( stderr,
62                         "%s: line %d: regular expression \"%s\" too large\n",
63                         fname, lineno, pat, 0 );
64                 acl_usage();
65         }
66
67         if ((e = regcomp(&re, buf, REG_EXTENDED|REG_ICASE))) {
68                 char error[512];
69                 regerror(e, &re, error, sizeof(error));
70                 fprintf( stderr,
71                         "%s: line %d: regular expression \"%s\" bad because of %s\n",
72                         fname, lineno, pat, error );
73                 acl_usage();
74                 return(0);
75         }
76         regfree(&re);
77         return(1);
78 }
79
80 void
81 parse_acl(
82     Backend     *be,
83     char        *fname,
84     int         lineno,
85     int         argc,
86     char        **argv
87 )
88 {
89         int             i;
90         char            *left, *right;
91         struct acl      *a;
92         struct access   *b;
93
94         a = NULL;
95         for ( i = 1; i < argc; i++ ) {
96                 /* to clause - select which entries are protected */
97                 if ( strcasecmp( argv[i], "to" ) == 0 ) {
98                         if ( a != NULL ) {
99                                 fprintf( stderr,
100                 "%s: line %d: only one to clause allowed in access line\n",
101                                     fname, lineno );
102                                 acl_usage();
103                         }
104                         a = (struct acl *) ch_calloc( 1, sizeof(struct acl) );
105                         for ( ++i; i < argc; i++ ) {
106                                 if ( strcasecmp( argv[i], "by" ) == 0 ) {
107                                         i--;
108                                         break;
109                                 }
110
111                                 if ( strcmp( argv[i], "*" ) == 0 ) {
112                                         int e;
113                                         if ((e = regcomp( &a->acl_dnre, ".*",
114                                                 REG_EXTENDED|REG_ICASE)))
115                                         {
116                                                 char buf[512];
117                                                 regerror(e, &a->acl_dnre, buf, sizeof(buf));
118                                                 fprintf( stderr,
119                                                         "%s: line %d: regular expression \"%s\" bad because of %s\n",
120                                                         fname, lineno, right, buf );
121                                                 acl_usage();
122                                         }
123                                         a->acl_dnpat = ch_strdup( ".*" );
124                                         continue;
125                                 }
126
127                                 split( argv[i], '=', &left, &right );
128                                 if ( right == NULL || *right == '\0' ) {
129                                         fprintf( stderr,
130         "%s: line %d: missing \"=\" in (or value after) \"%s\" in to clause\n",
131                                             fname, lineno, left );
132                                         acl_usage();
133                                 }
134
135                                 if ( strcasecmp( left, "filter" ) == 0 ) {
136                                         if ( (a->acl_filter = str2filter(
137                                             right )) == NULL ) {
138                                                 fprintf( stderr,
139                                 "%s: line %d: bad filter \"%s\" in to clause\n",
140                                                     fname, lineno, right );
141                                                 acl_usage();
142                                         }
143                                 } else if ( strcasecmp( left, "dn" ) == 0 ) {
144                                         int e;
145                                         if ((e = regcomp(&a->acl_dnre, right,
146                                                 REG_EXTENDED|REG_ICASE))) {
147                                                 char buf[512];
148                                                 regerror(e, &a->acl_dnre, buf, sizeof(buf));
149                                                 fprintf( stderr,
150                                 "%s: line %d: regular expression \"%s\" bad because of %s\n",
151                                                         fname, lineno, right, buf );
152                                                 acl_usage();
153
154                                         } else {
155                                                 /* ### Should be normalized, but how? */
156                                                 a->acl_dnpat = dn_upcase(ch_strdup( right ));
157                                         }
158                                 } else if ( strncasecmp( left, "attr", 4 )
159                                     == 0 ) {
160                                         char    **alist;
161
162                                         alist = str2charray( right, "," );
163                                         charray_merge( &a->acl_attrs, alist );
164                                         charray_free( alist );
165                                 } else {
166                                         fprintf( stderr,
167                                                 "%s: line %d: expecting <what> got \"%s\"\n",
168                                             fname, lineno, left );
169                                         acl_usage();
170                                 }
171                         }
172
173                 /* by clause - select who has what access to entries */
174                 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
175                         if ( a == NULL ) {
176                                 fprintf( stderr,
177                                         "%s: line %d: to clause required before by clause in access line\n",
178                                     fname, lineno );
179                                 acl_usage();
180                         }
181                         /*
182                          * by clause consists of <who> and <access>
183                          */
184
185                         b = (struct access *) ch_calloc( 1, sizeof(struct access) );
186
187                         if ( ++i == argc ) {
188                                 fprintf( stderr,
189                             "%s: line %d: premature eol: expecting <who>\n",
190                                     fname, lineno );
191                                 acl_usage();
192                         }
193
194                         /* get <who> */
195                         split( argv[i], '=', &left, &right );
196                         if ( strcmp( argv[i], "*" ) == 0 ) {
197                                 b->a_dnpat = ch_strdup( ".*" );
198                         } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
199                                 b->a_dnpat = ch_strdup( "self" );
200                         } else if ( strcasecmp( left, "dn" ) == 0 ) {
201                                 regtest(fname, lineno, right);
202                                 /* ### Should be normalized, but how? */
203                                 b->a_dnpat = dn_upcase( ch_strdup( right ) );
204                         } else if ( strcasecmp( left, "dnattr" ) == 0 ) {
205                                 b->a_dnattr = ch_strdup( right );
206
207 #ifdef SLAPD_ACLGROUPS
208                         } else if ( strcasecmp( left, "group" ) == 0 ) {
209                                 char *name = NULL;
210                                 char *value = NULL;
211                                 regtest(fname, lineno, right);
212
213                                 /* format of string is "group/objectClassValue/groupAttrName"
214                                  */
215                                 if ((value = strchr(right, '/')) != NULL) {
216                                         *value++ = '\0';
217                                         if (value && *value && (name = strchr(value, '/')) != NULL) 
218                                             *name++ = '\0';
219                                 }
220
221                                 /* ### Should it be normalized? */
222                                 b->a_group = dn_upcase(ch_strdup( right ));
223
224                                 if (value && *value) {
225                                         b->a_objectclassvalue = ch_strdup(value);
226                                         *--value = '/';
227                                 }
228                                 else
229                                         b->a_objectclassvalue = ch_strdup("groupOfNames");
230
231                                 if (name && *name) {
232                                         b->a_groupattrname = ch_strdup(name);
233                                         *--name = '/';
234                                 }
235                                 else
236                                         b->a_groupattrname = ch_strdup("member");
237
238
239
240 #endif /* SLAPD_ACLGROUPS */
241                         } else if ( strcasecmp( left, "domain" ) == 0 ) {
242                                 char    *s;
243                                 regtest(fname, lineno, right);
244                                 b->a_domainpat = ch_strdup( right );
245
246                                 /* normalize the domain */
247                                 for ( s = b->a_domainpat; *s; s++ ) {
248                                         *s = TOLOWER( (unsigned char) *s );
249                                 }
250                         } else if ( strcasecmp( left, "addr" ) == 0 ) {
251                                 regtest(fname, lineno, right);
252                                 b->a_addrpat = ch_strdup( right );
253                         } else {
254                                 fprintf( stderr,
255                                     "%s: line %d: expecting <who> got \"%s\"\n",
256                                     fname, lineno, left );
257                                 acl_usage();
258                         }
259
260                         if ( ++i == argc ) {
261                                 fprintf( stderr,
262                             "%s: line %d: premature eol: expecting <access>\n",
263                                     fname, lineno );
264                                 acl_usage();
265                         }
266
267                         /* get <access> */
268                         split( argv[i], '=', &left, &right );
269                         if ( (b->a_access = str2access( left )) == -1 ) {
270                                 fprintf( stderr,
271                             "%s: line %d: expecting <access> got \"%s\"\n",
272                                     fname, lineno, left );
273                                 acl_usage();
274                         }
275                         access_append( &a->acl_access, b );
276
277                 } else {
278                         fprintf( stderr,
279                     "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
280                             fname, lineno, argv[i] );
281                         acl_usage();
282                 }
283         }
284
285         /* if we have no real access clause, complain and do nothing */
286         if ( a == NULL ) {
287                         fprintf( stderr,
288                                 "%s: line %d: warning: no access clause(s) specified in access line\n",
289                             fname, lineno );
290
291         } else {
292
293 #ifdef LDAP_DEBUG
294                 if (ldap_debug & LDAP_DEBUG_ACL)
295                     print_acl(a);
296 #endif
297         
298                 if ( a->acl_access == NULL ) {
299                         fprintf( stderr,
300                         "%s: line %d: warning: no by clause(s) specified in access line\n",
301                             fname, lineno );
302                 }
303
304                 if ( be != NULL ) {
305                         acl_append( &be->be_acl, a );
306                 } else {
307                         acl_append( &global_acl, a );
308                 }
309         }
310 }
311
312 char *
313 access2str( int access )
314 {
315         static char     buf[12];
316
317         if ( access & ACL_SELF ) {
318                 strcpy( buf, "self" );
319         } else {
320                 buf[0] = '\0';
321         }
322
323         if ( access & ACL_NONE ) {
324                 strcat( buf, "none" );
325         } else if ( access & ACL_COMPARE ) {
326                 strcat( buf, "compare" );
327         } else if ( access & ACL_SEARCH ) {
328                 strcat( buf, "search" );
329         } else if ( access & ACL_READ ) {
330                 strcat( buf, "read" );
331         } else if ( access & ACL_WRITE ) {
332                 strcat( buf, "write" );
333         } else {
334                 strcat( buf, "unknown" );
335         }
336
337         return( buf );
338 }
339
340 int
341 str2access( char *str )
342 {
343         int     access;
344
345         access = 0;
346         if ( strncasecmp( str, "self", 4 ) == 0 ) {
347                 access |= ACL_SELF;
348                 str += 4;
349         }
350
351         if ( strcasecmp( str, "none" ) == 0 ) {
352                 access |= ACL_NONE;
353         } else if ( strcasecmp( str, "compare" ) == 0 ) {
354                 access |= ACL_COMPARE;
355         } else if ( strcasecmp( str, "search" ) == 0 ) {
356                 access |= ACL_SEARCH;
357         } else if ( strcasecmp( str, "read" ) == 0 ) {
358                 access |= ACL_READ;
359         } else if ( strcasecmp( str, "write" ) == 0 ) {
360                 access |= ACL_WRITE;
361         } else {
362                 access = -1;
363         }
364
365         return( access );
366 }
367
368 static void
369 acl_usage( void )
370 {
371         fprintf( stderr, "\n<access clause> ::= access to <what> [ by <who> <access> ]+ \n" );
372         fprintf( stderr, "<what> ::= * | [dn=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n" );
373         fprintf( stderr, "<attrlist> ::= <attr> | <attr> , <attrlist>\n" );
374         fprintf( stderr, "<attr> ::= <attrname> | entry | children\n" );
375         fprintf( stderr, "<who> ::= * | self | dn=<regex> | addr=<regex> |\n\tdomain=<regex> | dnattr=<dnattrname>\n" );
376         fprintf( stderr, "<access> ::= [self]{none | compare | search | read | write }\n" );
377         exit( 1 );
378 }
379
380 static void
381 split(
382     char        *line,
383     int         splitchar,
384     char        **left,
385     char        **right
386 )
387 {
388         *left = line;
389         if ( (*right = strchr( line, splitchar )) != NULL ) {
390                 *((*right)++) = '\0';
391         }
392 }
393
394 static void
395 access_append( struct access **l, struct access *a )
396 {
397         for ( ; *l != NULL; l = &(*l)->a_next )
398                 ;       /* NULL */
399
400         *l = a;
401 }
402
403 static void
404 acl_append( struct acl **l, struct acl *a )
405 {
406         for ( ; *l != NULL; l = &(*l)->acl_next )
407                 ;       /* NULL */
408
409         *l = a;
410 }
411
412 #ifdef LDAP_DEBUG
413
414 static void
415 print_access( struct access *b )
416 {
417         fprintf( stderr, "\tby" );
418
419         if ( b->a_dnpat != NULL ) {
420                 fprintf( stderr, " dn=%s", b->a_dnpat );
421         } else if ( b->a_addrpat != NULL ) {
422                 fprintf( stderr, " addr=%s", b->a_addrpat );
423         } else if ( b->a_domainpat != NULL ) {
424                 fprintf( stderr, " domain=%s", b->a_domainpat );
425         } else if ( b->a_dnattr != NULL ) {
426                 fprintf( stderr, " dnattr=%s", b->a_dnattr );
427         }
428 #ifdef SLAPD_ACLGROUPS
429         else if ( b->a_group != NULL ) {
430                 fprintf( stderr, " group: %s", b->a_group );
431                 if ( b->a_objectclassvalue )
432                         fprintf( stderr, " objectClassValue: %s", b->a_objectclassvalue );
433                 if ( b->a_groupattrname )
434                         fprintf( stderr, " groupAttrName: %s", b->a_groupattrname );
435         }
436 #endif
437         fprintf( stderr, "\n" );
438 }
439
440 static void
441 print_acl( struct acl *a )
442 {
443         int             i;
444         struct access   *b;
445
446         if ( a == NULL ) {
447                 fprintf( stderr, "NULL\n" );
448         }
449         fprintf( stderr, "ACL: access to" );
450         if ( a->acl_filter != NULL ) {
451                 fprintf(  stderr," filter=" );
452                 filter_print( a->acl_filter );
453         }
454         if ( a->acl_dnpat != NULL ) {
455                 fprintf( stderr, " dn=" );
456                 fprintf( stderr, a->acl_dnpat );
457         }
458         if ( a->acl_attrs != NULL ) {
459                 int     first = 1;
460
461                 fprintf( stderr, "\n attrs=" );
462                 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
463                         if ( ! first ) {
464                                 fprintf( stderr, "," );
465                         }
466                         fprintf( stderr, a->acl_attrs[i] );
467                         first = 0;
468                 }
469         }
470         fprintf( stderr, "\n" );
471         for ( b = a->acl_access; b != NULL; b = b->a_next ) {
472                 print_access( b );
473         }
474         fprintf( stderr, "\n" );
475 }
476
477 #endif /* LDAP_DEBUG */