]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
fix Release configuration
[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;
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_client_addr,
291                                 edn, matches ) )
292                         {
293                                 Debug( LDAP_DEBUG_ACL,
294                                     "<= acl_access_allowed: matched by clause #%d access %s\n",
295                                     i, (b->a_access & ~ACL_SELF) >= access ?
296                                     "granted" : "denied", 0 );
297
298                                 return( (b->a_access & ~ACL_SELF) >= access );
299                         }
300                 }
301                 if ( b->a_domainpat != NULL ) {
302                         Debug( LDAP_DEBUG_ARGS, "<= check a_domainpath: %s\n",
303                                 b->a_domainpat, 0, 0 );
304                         if ( regex_matches( b->a_domainpat, conn->c_client_name,
305                                 edn, matches ) ) 
306                         {
307                                 Debug( LDAP_DEBUG_ACL,
308                                     "<= acl_access_allowed: matched by clause #%d access %s\n",
309                                     i, (b->a_access & ~ACL_SELF) >= access ?
310                                     "granted" : "denied", 0 );
311
312                                 return( (b->a_access & ~ACL_SELF) >= access );
313                         }
314                 }
315                 if ( b->a_dnattr != NULL && op->o_ndn != NULL ) {
316                         Debug( LDAP_DEBUG_ARGS, "<= check a_dnattr: %s\n",
317                                 b->a_dnattr, 0, 0);
318                         /* see if asker is listed in dnattr */
319                         if ( (at = attr_find( e->e_attrs, b->a_dnattr )) != NULL && 
320                                 value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0 )
321                         {
322                                 if ( (b->a_access & ACL_SELF) && 
323                                         (val == NULL || value_cmp( &bv, val, at->a_syntax, 2 )) )
324                                 {
325                                         continue;
326                                 }
327
328                                 Debug( LDAP_DEBUG_ACL,
329                                     "<= acl_acces_allowed: matched by clause #%d access %s\n",
330                                     i, (b->a_access & ~ACL_SELF) >= access ?
331                                     "granted" : "denied", 0 );
332
333                                 return( (b->a_access & ~ACL_SELF) >= access );
334                         }
335
336                         /* asker not listed in dnattr - check for self access */
337                         if ( ! (b->a_access & ACL_SELF) || val == NULL ||
338                                 value_cmp( &bv, val, at->a_syntax, 2 ) != 0 )
339                         {
340                                 continue;
341                         }
342
343                         Debug( LDAP_DEBUG_ACL,
344                                 "<= acl_access_allowed: matched by clause #%d (self) access %s\n",
345                             i, (b->a_access & ~ACL_SELF) >= access ? "granted"
346                             : "denied", 0 );
347
348                         return( (b->a_access & ~ACL_SELF) >= access );
349                 }
350 #ifdef SLAPD_ACLGROUPS
351                 if ( b->a_group != NULL && op->o_ndn != NULL ) {
352                         char buf[1024];
353
354                         /* b->a_group is an unexpanded entry name, expanded it should be an 
355                          * entry with objectclass group* and we test to see if odn is one of
356                          * the values in the attribute group
357                          */
358                         /* see if asker is listed in dnattr */
359                         string_expand(buf, sizeof(buf), b->a_group, edn, matches);
360                         (void) dn_normalize_case(buf);
361
362                         if (backend_group(be, e, buf, odn,
363                                 b->a_objectclassvalue, b->a_groupattrname) == 0)
364                         {
365                                 Debug( LDAP_DEBUG_ACL,
366                                         "<= acl_access_allowed: matched by clause #%d (group) access granted\n",
367                                         i, 0, 0 );
368                                 return( (b->a_access & ~ACL_SELF) >= access );
369                         }
370                 }
371 #endif /* SLAPD_ACLGROUPS */
372         }
373
374         Debug( LDAP_DEBUG_ACL,
375                 "<= acl_access_allowed: %s by default (no matching by)\n",
376             default_access >= access ? "granted" : "denied", 0, 0 );
377
378         return( default_access >= access );
379 }
380
381 /*
382  * acl_check_modlist - check access control on the given entry to see if
383  * it allows the given modifications by the user associated with op.
384  * returns      LDAP_SUCCESS    mods allowed ok
385  *              anything else   mods not allowed - return is an error
386  *                              code indicating the problem
387  */
388
389 int
390 acl_check_modlist(
391     Backend     *be,
392     Connection  *conn,
393     Operation   *op,
394     Entry       *e,
395     LDAPModList *mlist
396 )
397 {
398         int             i;
399         struct acl      *a;
400         char    *edn = e->e_ndn;
401
402         for ( ; mlist != NULL; mlist = mlist->ml_next ) {
403                 regmatch_t       matches[MAXREMATCHES];
404
405                 /* the lastmod attributes are ignored by ACL checking */
406                 if ( strcasecmp( mlist->ml_type, "modifiersname" ) == 0 ||
407                         strcasecmp( mlist->ml_type, "modifytimestamp" ) == 0 ||
408                         strcasecmp( mlist->ml_type, "creatorsname" ) == 0 ||
409                         strcasecmp( mlist->ml_type, "createtimestamp" ) == 0 ) 
410                 {
411                         Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access allowed\n",
412                                 mlist->ml_type, 0, 0 );
413                         continue;
414                 }
415
416                 a = acl_get_applicable( be, op, e, mlist->ml_type,
417                         MAXREMATCHES, matches );
418
419                 switch ( mlist->ml_op & ~LDAP_MOD_BVALUES ) {
420                 case LDAP_MOD_REPLACE:
421                 case LDAP_MOD_ADD:
422                         if ( mlist->ml_bvalues == NULL ) {
423                                 break;
424                         }
425                         for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
426                                 if ( ! acl_access_allowed( a, be, conn, e, mlist->ml_bvalues[i], 
427                                         op, ACL_WRITE, edn, matches) ) 
428                                 {
429                                         return( LDAP_INSUFFICIENT_ACCESS );
430                                 }
431                         }
432                         break;
433
434                 case LDAP_MOD_DELETE:
435                         if ( mlist->ml_bvalues == NULL ) {
436                                 if ( ! acl_access_allowed( a, be, conn, e,
437                                         NULL, op, ACL_WRITE, edn, matches) ) 
438                                 {
439                                         return( LDAP_INSUFFICIENT_ACCESS );
440                                 }
441                                 break;
442                         }
443                         for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
444                                 if ( ! acl_access_allowed( a, be, conn, e, mlist->ml_bvalues[i], 
445                                         op, ACL_WRITE, edn, matches) ) 
446                                 {
447                                         return( LDAP_INSUFFICIENT_ACCESS );
448                                 }
449                         }
450                         break;
451                 }
452         }
453
454         return( LDAP_SUCCESS );
455 }
456
457 static void
458 string_expand(
459         char *newbuf,
460         int bufsiz,
461         char *pat,
462         char *match,
463         regmatch_t *matches)
464 {
465         int     size;
466         char   *sp;
467         char   *dp;
468         int     flag;
469
470         size = 0;
471         newbuf[0] = '\0';
472
473         flag = 0;
474         for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
475                 /* did we previously see a $ */
476                 if (flag) {
477                         if (*sp == '$') {
478                                 *dp++ = '$';
479                                 size++;
480                         } else if (*sp >= '0' && *sp <= '9' ) {
481                                 int     n;
482                                 int     i;
483                                 int     l;
484
485                                 n = *sp - '0';
486                                 *dp = '\0';
487                                 i = matches[n].rm_so;
488                                 l = matches[n].rm_eo; 
489                                 for ( ; size < 512 && i < l; size++, i++ ) {
490                                         *dp++ = match[i];
491                                         size++;
492                                 }
493                                 *dp = '\0';
494                         }
495                         flag = 0;
496                 } else {
497                         if (*sp == '$') {
498                                 flag = 1;
499                         } else {
500                                 *dp++ = *sp;
501                                 size++;
502                         }
503                 }
504         }
505         *dp = '\0';
506
507         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %s\n", pat, 0, 0 );
508         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
509 }
510
511 static int
512 regex_matches(
513         char *pat,                              /* pattern to expand and match against */
514         char *str,                              /* string to match against pattern */
515         char *buf,                              /* buffer with $N expansion variables */
516         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
517 )
518 {
519         regex_t re;
520         char newbuf[512];
521         int     rc;
522
523         string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
524         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
525                 char error[512];
526                 regerror(rc, &re, error, sizeof(error));
527
528                 Debug( LDAP_DEBUG_TRACE,
529                     "compile( \"%s\", \"%s\") failed %s\n",
530                         pat, str, error );
531                 return( 0 );
532         }
533
534         rc = regexec(&re, str, 0, NULL, 0);
535         regfree( &re );
536
537         Debug( LDAP_DEBUG_TRACE,
538             "=> regex_matches: string:   %s\n", str, 0, 0 );
539         Debug( LDAP_DEBUG_TRACE,
540             "=> regex_matches: rc: %d %s\n",
541                 rc, !rc ? "matches" : "no matches", 0 );
542         return( !rc );
543 }
544