]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
ndn & strcasecmp cleanup
[openldap] / servers / slapd / acl.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/regex.h>
8 #include <ac/socket.h>
9 #include <ac/string.h>
10
11 #include "slap.h"
12
13 static int      regex_matches(char *pat, char *str, char *buf, regmatch_t *matches);
14 static void     string_expand(char *newbuf, int bufsiz, char *pattern,
15                               char *match, regmatch_t *matches);
16
17
18 /*
19  * access_allowed - check whether op->o_ndn is allowed the requested access
20  * to entry e, attribute attr, value val.  if val is null, access to
21  * the whole attribute is assumed (all values).  this routine finds
22  * the applicable acl and calls acl_access_allowed() to make the
23  * decision.
24  *
25  * returns      0       access NOT allowed
26  *              1       access allowed
27  */
28
29 int
30 access_allowed(
31     Backend             *be,
32     Connection          *conn,
33     Operation           *op,
34     Entry               *e,
35     char                *attr,
36     struct berval       *val,
37     int                 access
38 )
39 {
40         int                             rc;
41         struct acl              *a;
42         char            *edn;
43
44         regmatch_t       matches[MAXREMATCHES];
45         int              i;
46         int              n;
47
48         if ( be == NULL ) {
49                 return( 0 );
50         }
51
52         edn = e->e_ndn;
53
54         Debug( LDAP_DEBUG_ACL, "\n=> access_allowed: entry (%s) attr (%s)\n",
55                 e->e_dn, attr, 0 );
56
57         /* the lastmod attributes are ignored by ACL checking */
58         if ( strcasecmp( attr, "modifiersname" ) == 0 ||
59                 strcasecmp( attr, "modifytimestamp" ) == 0 ||
60                 strcasecmp( attr, "creatorsname" ) == 0 ||
61                 strcasecmp( attr, "createtimestamp" ) == 0 )
62         {
63                 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access allowed\n",
64                         attr, 0, 0 );
65                 return(1);
66         }
67
68         memset(matches, 0, sizeof(matches));
69
70         a = acl_get_applicable( be, op, e, attr, MAXREMATCHES, matches );
71
72         if (a) {
73                 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
74                         Debug( LDAP_DEBUG_ARGS, "=> match[%d]: %d %d ", i,
75                                (int)matches[i].rm_so, (int)matches[i].rm_eo );
76
77                         if( matches[i].rm_so <= matches[0].rm_eo ) {
78                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
79                                         Debug( LDAP_DEBUG_ARGS, "%c", edn[n], 0, 0 );
80                                 }
81                         }
82                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
83                 }
84         }
85
86         rc = acl_access_allowed( a, be, conn, e, val, op, access, edn, matches );
87
88         Debug( LDAP_DEBUG_ACL, "\n=> access_allowed: exit (%s) attr (%s)\n",
89                 e->e_dn, attr, 0);
90
91         return( rc );
92 }
93
94 /*
95  * acl_get_applicable - return the acl applicable to entry e, attribute
96  * attr.  the acl returned is suitable for use in subsequent calls to
97  * acl_access_allowed().
98  */
99
100 struct acl *
101 acl_get_applicable(
102     Backend             *be,
103     Operation           *op,
104     Entry               *e,
105     char                *attr,
106     int                 nmatch,
107     regmatch_t  *matches
108 )
109 {
110         int             i, j;
111         struct acl      *a;
112     char                *edn;
113
114         Debug( LDAP_DEBUG_ACL, "\n=> acl_get: entry (%s) attr (%s)\n",
115                 e->e_dn, attr, 0 );
116
117         if ( be_isroot( be, op->o_ndn ) ) {
118                 Debug( LDAP_DEBUG_ACL,
119                     "<= acl_get: no acl applicable to database root\n", 0, 0,
120                     0 );
121                 return( NULL );
122         }
123
124     edn = e->e_ndn;
125
126         Debug( LDAP_DEBUG_ARGS, "=> acl_get: edn %s\n", edn, 0, 0 );
127
128         /* check for a backend-specific acl that matches the entry */
129         for ( i = 1, a = be->be_acl; a != NULL; a = a->acl_next, i++ ) {
130                 if (a->acl_dnpat != NULL) {
131                         Debug( LDAP_DEBUG_TRACE, "=> dnpat: [%d] %s nsub: %d\n", 
132                                 i, a->acl_dnpat, (int) a->acl_dnre.re_nsub);
133
134                         if (regexec(&a->acl_dnre, edn, nmatch, matches, 0))
135                                 continue;
136                         else
137                                 Debug( LDAP_DEBUG_TRACE, "=> acl_get:[%d]  backend ACL match\n",
138                                         i, 0, 0);
139                 }
140
141                 if ( a->acl_filter != NULL ) {
142                         if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
143                                 continue;
144                         }
145                 }
146
147         Debug( LDAP_DEBUG_ARGS, "=> acl_get: [%d] check attr %s\n", i, attr, 0);
148
149                 if ( attr == NULL || a->acl_attrs == NULL ||
150                         charray_inlist( a->acl_attrs, attr ) )
151                 {
152                         Debug( LDAP_DEBUG_ACL, "<= acl_get: [%d] backend acl %s attr: %s\n",
153                                 i, e->e_dn, attr );
154                         return( a );
155                 }
156                 matches[0].rm_so = matches[0].rm_eo = -1;
157         }
158
159         /* check for a global acl that matches the entry */
160         for ( i = 1, a = global_acl; a != NULL; a = a->acl_next, i++ ) {
161                 if (a->acl_dnpat != NULL) {
162                         Debug( LDAP_DEBUG_TRACE, "=> dnpat: [%d] %s nsub: %d\n", 
163                                 i, a->acl_dnpat, (int) a->acl_dnre.re_nsub);
164
165                         if (regexec(&a->acl_dnre, edn, nmatch, matches, 0)) {
166                                 continue;
167                         } else {
168                                 Debug( LDAP_DEBUG_TRACE, "=> acl_get: [%d] global ACL match\n",
169                                         i, 0, 0);
170                         }
171                 }
172
173                 if ( a->acl_filter != NULL ) {
174                         if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
175                                 continue;
176                         }
177                 }
178
179                 Debug( LDAP_DEBUG_ARGS, "=> acl_get: [%d] check attr\n", i, 0, 0);
180
181                 if ( attr == NULL || a->acl_attrs == NULL ||
182                         charray_inlist( a->acl_attrs, attr ) )
183                 {
184                         Debug( LDAP_DEBUG_ACL, "<= acl_get: [%d] global acl %s attr: %s\n",
185                                 i, e->e_dn, attr );
186                         return( a );
187                 }
188
189                 matches[0].rm_so = matches[0].rm_eo = -1;
190         }
191
192         Debug( LDAP_DEBUG_ACL, "<= acl_get: no match\n", 0, 0, 0 );
193         return( NULL );
194 }
195
196 /*
197  * acl_access_allowed - check whether the given acl allows dn the
198  * requested access to entry e, attribute attr, value val.  if val
199  * is null, access to the whole attribute is assumed (all values).
200  *
201  * returns      0       access NOT allowed
202  *              1       access allowed
203  */
204
205 int
206 acl_access_allowed(
207     struct acl          *a,
208     Backend             *be,
209     Connection          *conn,
210     Entry               *e,
211     struct berval       *val,
212     Operation           *op,
213     int                 access,
214         char            *edn,
215         regmatch_t      *matches
216 )
217 {
218         int             i;
219         char            *odn;
220         struct access   *b;
221         Attribute       *at;
222         struct berval   bv;
223         int             default_access;
224
225         Debug( LDAP_DEBUG_ACL,
226                 "\n=> acl_access_allowed: %s access to entry \"%s\"\n",
227                 access2str( access ), e->e_dn, 0 );
228
229         Debug( LDAP_DEBUG_ACL,
230                 "\n=> acl_access_allowed: %s access to value \"%s\" by \"%s\"\n",
231             access2str( access ),
232                 val ? val->bv_val : "any",
233                 op->o_ndn ?  op->o_ndn : "" );
234
235         if ( be_isroot( be, op->o_ndn ) ) {
236                 Debug( LDAP_DEBUG_ACL,
237                         "<= acl_access_allowed: granted to database root\n",
238                     0, 0, 0 );
239                 return( 1 );
240         }
241
242         default_access = be->be_dfltaccess ? be->be_dfltaccess : global_default_access;
243
244         if ( a == NULL ) {
245                 Debug( LDAP_DEBUG_ACL,
246                     "<= acl_access_allowed: %s by default (no matching to)\n",
247                     default_access >= access ? "granted" : "denied", 0, 0 );
248                 return( default_access >= access );
249         }
250
251         odn = op->o_ndn;
252
253         if ( odn != NULL ) {
254                 bv.bv_val = odn;
255                 bv.bv_len = strlen( odn );
256         }
257
258         for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
259                 if ( b->a_dnpat != NULL ) {
260                         Debug( LDAP_DEBUG_TRACE, "<= check a_dnpat: %s\n",
261                                 b->a_dnpat, 0, 0);
262                         /*
263                          * if access applies to the entry itself, and the
264                          * user is bound as somebody in the same namespace as
265                          * the entry, OR the given dn matches the dn pattern
266                          */
267                         if ( strcasecmp( b->a_dnpat, "self" ) == 0 && 
268                                 op->o_ndn != NULL && *(op->o_ndn) && e->e_dn != NULL ) 
269                         {
270                                 if ( strcmp( edn, op->o_ndn ) == 0 ) {
271                                         Debug( LDAP_DEBUG_ACL,
272                                         "<= acl_access_allowed: matched by clause #%d access %s\n",
273                                             i, (b->a_access & ~ACL_SELF) >=
274                                             access ? "granted" : "denied", 0 );
275
276                                         return( (b->a_access & ~ACL_SELF) >= access );
277                                 }
278                         } else {
279                                 if ( regex_matches( b->a_dnpat, odn, edn, matches ) ) {
280                                         Debug( LDAP_DEBUG_ACL,
281                                     "<= acl_access_allowed: matched by clause #%d access %s\n",
282                                     i, (b->a_access & ~ACL_SELF) >= access ?
283                                             "granted" : "denied", 0 );
284
285                                         return( (b->a_access & ~ACL_SELF) >= access );
286                                 }
287                         }
288                 }
289                 if ( b->a_addrpat != NULL ) {
290                         if ( regex_matches( b->a_addrpat, conn->c_addr, edn, matches ) ) {
291                                 Debug( LDAP_DEBUG_ACL,
292                                     "<= acl_access_allowed: matched by clause #%d access %s\n",
293                                     i, (b->a_access & ~ACL_SELF) >= access ?
294                                     "granted" : "denied", 0 );
295
296                                 return( (b->a_access & ~ACL_SELF) >= access );
297                         }
298                 }
299                 if ( b->a_domainpat != NULL ) {
300                         Debug( LDAP_DEBUG_ARGS, "<= check a_domainpath: %s\n",
301                                 b->a_domainpat, 0, 0 );
302                         if ( regex_matches( b->a_domainpat, conn->c_domain, edn, matches ) ) 
303                         {
304                                 Debug( LDAP_DEBUG_ACL,
305                                     "<= acl_access_allowed: matched by clause #%d access %s\n",
306                                     i, (b->a_access & ~ACL_SELF) >= access ?
307                                     "granted" : "denied", 0 );
308
309                                 return( (b->a_access & ~ACL_SELF) >= access );
310                         }
311                 }
312                 if ( b->a_dnattr != NULL && op->o_ndn != NULL ) {
313                         Debug( LDAP_DEBUG_ARGS, "<= check a_dnattr: %s\n",
314                                 b->a_dnattr, 0, 0);
315                         /* see if asker is listed in dnattr */
316                         if ( (at = attr_find( e->e_attrs, b->a_dnattr )) != NULL && 
317                                 value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0 )
318                         {
319                                 if ( (b->a_access & ACL_SELF) && 
320                                         (val == NULL || value_cmp( &bv, val, at->a_syntax, 2 )) )
321                                 {
322                                         continue;
323                                 }
324
325                                 Debug( LDAP_DEBUG_ACL,
326                                     "<= acl_acces_allowed: matched by clause #%d access %s\n",
327                                     i, (b->a_access & ~ACL_SELF) >= access ?
328                                     "granted" : "denied", 0 );
329
330                                 return( (b->a_access & ~ACL_SELF) >= access );
331                         }
332
333                         /* asker not listed in dnattr - check for self access */
334                         if ( ! (b->a_access & ACL_SELF) || val == NULL ||
335                                 value_cmp( &bv, val, at->a_syntax, 2 ) != 0 )
336                         {
337                                 continue;
338                         }
339
340                         Debug( LDAP_DEBUG_ACL,
341                                 "<= acl_access_allowed: matched by clause #%d (self) access %s\n",
342                             i, (b->a_access & ~ACL_SELF) >= access ? "granted"
343                             : "denied", 0 );
344
345                         return( (b->a_access & ~ACL_SELF) >= access );
346                 }
347 #ifdef SLAPD_ACLGROUPS
348                 if ( b->a_group != NULL && op->o_ndn != NULL ) {
349                         char buf[1024];
350
351                         /* b->a_group is an unexpanded entry name, expanded it should be an 
352                          * entry with objectclass group* and we test to see if odn is one of
353                          * the values in the attribute group
354                          */
355                         /* see if asker is listed in dnattr */
356                         string_expand(buf, sizeof(buf), b->a_group, edn, matches);
357                         (void) dn_normalize_case(buf);
358
359                         if (be_group(be, e, buf, odn,
360                                 b->a_objectclassvalue, b->a_groupattrname) == 0)
361                         {
362                                 Debug( LDAP_DEBUG_ACL,
363                                         "<= acl_access_allowed: matched by clause #%d (group) access granted\n",
364                                         i, 0, 0 );
365                                 return( (b->a_access & ~ACL_SELF) >= access );
366                         }
367                 }
368 #endif /* SLAPD_ACLGROUPS */
369         }
370
371         Debug( LDAP_DEBUG_ACL,
372                 "<= acl_access_allowed: %s by default (no matching by)\n",
373             default_access >= access ? "granted" : "denied", 0, 0 );
374
375         return( default_access >= access );
376 }
377
378 /*
379  * acl_check_modlist - check access control on the given entry to see if
380  * it allows the given modifications by the user associated with op.
381  * returns      LDAP_SUCCESS    mods allowed ok
382  *              anything else   mods not allowed - return is an error
383  *                              code indicating the problem
384  */
385
386 int
387 acl_check_modlist(
388     Backend     *be,
389     Connection  *conn,
390     Operation   *op,
391     Entry       *e,
392     LDAPModList *mlist
393 )
394 {
395         int             i;
396         struct acl      *a;
397         char    *edn = e->e_ndn;
398
399         for ( ; mlist != NULL; mlist = mlist->ml_next ) {
400                 regmatch_t       matches[MAXREMATCHES];
401
402                 /* the lastmod attributes are ignored by ACL checking */
403                 if ( strcasecmp( mlist->ml_type, "modifiersname" ) == 0 ||
404                         strcasecmp( mlist->ml_type, "modifytimestamp" ) == 0 ||
405                         strcasecmp( mlist->ml_type, "creatorsname" ) == 0 ||
406                         strcasecmp( mlist->ml_type, "createtimestamp" ) == 0 ) 
407                 {
408                         Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access allowed\n",
409                                 mlist->ml_type, 0, 0 );
410                         continue;
411                 }
412
413                 a = acl_get_applicable( be, op, e, mlist->ml_type,
414                         MAXREMATCHES, matches );
415
416                 switch ( mlist->ml_op & ~LDAP_MOD_BVALUES ) {
417                 case LDAP_MOD_REPLACE:
418                 case LDAP_MOD_ADD:
419                         if ( mlist->ml_bvalues == NULL ) {
420                                 break;
421                         }
422                         for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
423                                 if ( ! acl_access_allowed( a, be, conn, e, mlist->ml_bvalues[i], 
424                                         op, ACL_WRITE, edn, matches) ) 
425                                 {
426                                         return( LDAP_INSUFFICIENT_ACCESS );
427                                 }
428                         }
429                         break;
430
431                 case LDAP_MOD_DELETE:
432                         if ( mlist->ml_bvalues == NULL ) {
433                                 if ( ! acl_access_allowed( a, be, conn, e,
434                                         NULL, op, ACL_WRITE, edn, matches) ) 
435                                 {
436                                         return( LDAP_INSUFFICIENT_ACCESS );
437                                 }
438                                 break;
439                         }
440                         for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
441                                 if ( ! acl_access_allowed( a, be, conn, e, mlist->ml_bvalues[i], 
442                                         op, ACL_WRITE, edn, matches) ) 
443                                 {
444                                         return( LDAP_INSUFFICIENT_ACCESS );
445                                 }
446                         }
447                         break;
448                 }
449         }
450
451         return( LDAP_SUCCESS );
452 }
453
454 static void
455 string_expand(
456         char *newbuf,
457         int bufsiz,
458         char *pat,
459         char *match,
460         regmatch_t *matches)
461 {
462         int     size;
463         char   *sp;
464         char   *dp;
465         int     flag;
466
467         size = 0;
468         newbuf[0] = '\0';
469
470         flag = 0;
471         for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
472                 /* did we previously see a $ */
473                 if (flag) {
474                         if (*sp == '$') {
475                                 *dp++ = '$';
476                                 size++;
477                         } else if (*sp >= '0' && *sp <= '9' ) {
478                                 int     n;
479                                 int     i;
480                                 char   *ep;
481                                 int     l;
482
483                                 n = *sp - '0';
484                                 *dp = '\0';
485                                 i = matches[n].rm_so;
486                                 l = matches[n].rm_eo; 
487                                 for ( ; size < 512 && i < l; size++, i++ ) {
488                                         *dp++ = match[i];
489                                         size++;
490                                 }
491                                 *dp = '\0';
492                         }
493                         flag = 0;
494                 } else {
495                         if (*sp == '$') {
496                                 flag = 1;
497                         } else {
498                                 *dp++ = *sp;
499                                 size++;
500                         }
501                 }
502         }
503         *dp = '\0';
504
505         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %s\n", pat, 0, 0 );
506         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
507 }
508
509 static int
510 regex_matches(
511         char *pat,                              /* pattern to expand and match against */
512         char *str,                              /* string to match against pattern */
513         char *buf,                              /* buffer with $N expansion variables */
514         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
515 )
516 {
517         regex_t re;
518         char newbuf[512];
519         int     rc;
520
521         string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
522         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
523                 char error[512];
524                 regerror(rc, &re, error, sizeof(error));
525
526                 Debug( LDAP_DEBUG_TRACE,
527                     "compile( \"%s\", \"%s\") failed %s\n",
528                         pat, str, error );
529                 return( 0 );
530         }
531
532         rc = regexec(&re, str, 0, NULL, 0);
533         regfree( &re );
534
535         Debug( LDAP_DEBUG_TRACE,
536             "=> regex_matches: string:   %s\n", str, 0, 0 );
537         Debug( LDAP_DEBUG_TRACE,
538             "=> regex_matches: rc: %d %s\n",
539                 rc, !rc ? "matches" : "no matches", 0 );
540         return( !rc );
541 }
542