]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
Modified make system to support CVS instead of RCS
[openldap] / servers / slapd / acl.c
1 /* acl.c - routines to parse and check acl's */
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <netdb.h>
9 #ifdef sunos5
10 #include "regexpr.h"
11 #else
12 #include "regex.h"
13 #endif
14 #include "slap.h"
15
16 extern Attribute        *attr_find();
17 extern char             *re_comp();
18 extern struct acl       *global_acl;
19 extern int              global_default_access;
20 extern char             *access2str();
21 extern char             *dn_normalize_case();
22
23 int             acl_access_allowed();
24 int             access_allowed();
25 struct acl      *acl_get_applicable();
26
27 static int      regex_matches();
28
29 extern pthread_mutex_t  regex_mutex;
30
31 /*
32  * access_allowed - check whether dn is allowed the requested access
33  * to entry e, attribute attr, value val.  if val is null, access to
34  * the whole attribute is assumed (all values).  this routine finds
35  * the applicable acl and calls acl_access_allowed() to make the
36  * decision.
37  *
38  * returns      0       access NOT allowed
39  *              1       access allowed
40  */
41
42 int
43 access_allowed(
44     Backend             *be,
45     Connection          *conn,
46     Operation           *op,
47     Entry               *e,
48     char                *attr,
49     struct berval       *val,
50     char                *dn,
51     int                 access
52 )
53 {
54         int             rc;
55         struct acl      *a;
56
57         if ( be == NULL ) {
58                 return( 0 );
59         }
60
61         a = acl_get_applicable( be, op, e, attr );
62         rc = acl_access_allowed( a, be, conn, e, val, op, access );
63
64         return( rc );
65 }
66
67 /*
68  * acl_get_applicable - return the acl applicable to entry e, attribute
69  * attr.  the acl returned is suitable for use in subsequent calls to
70  * acl_access_allowed().
71  */
72
73 struct acl *
74 acl_get_applicable(
75     Backend             *be,
76     Operation           *op,
77     Entry               *e,
78     char                *attr
79 )
80 {
81         int             i;
82         struct acl      *a;
83         char            *edn;
84
85         Debug( LDAP_DEBUG_ACL, "=> acl_get: entry (%s) attr (%s)\n", e->e_dn,
86             attr, 0 );
87
88         if ( be_isroot( be, op->o_dn ) ) {
89                 Debug( LDAP_DEBUG_ACL,
90                     "<= acl_get: no acl applicable to database root\n", 0, 0,
91                     0 );
92                 return( NULL );
93         }
94
95         /* check for a backend-specific acl that matches the entry */
96         for ( i = 1, a = be->be_acl; a != NULL; a = a->acl_next, i++ ) {
97                 if ( a->acl_dnpat != NULL ) {
98                         edn = dn_normalize_case( strdup( e->e_dn ) );
99                         if ( ! regex_matches( a->acl_dnpat, edn ) ) {
100                                 free( edn );
101                                 continue;
102                         }
103                         free( edn );
104                 }
105                 if ( a->acl_filter != NULL ) {
106                         if ( test_filter( NULL, NULL, NULL, e, a->acl_filter )
107                             != 0 ) {
108                                 continue;
109                         }
110                 }
111                 if ( attr == NULL || a->acl_attrs == NULL ||
112                     charray_inlist( a->acl_attrs, attr ) ) {
113                         Debug( LDAP_DEBUG_ACL, "<= acl_get: backend acl #%d\n",
114                             i, e->e_dn, attr );
115                         return( a );
116                 }
117         }
118
119         /* check for a global acl that matches the entry */
120         for ( i = 1, a = global_acl; a != NULL; a = a->acl_next, i++ ) {
121                 if ( a->acl_dnpat != NULL ) {
122                         edn = dn_normalize_case( strdup( e->e_dn ) );
123                         if ( ! regex_matches( a->acl_dnpat, edn ) ) {
124                                 free( edn );
125                                 continue;
126                         }
127                         free( edn );
128                 }
129                 if ( a->acl_filter != NULL ) {
130                         if ( test_filter( NULL, NULL, NULL, e, a->acl_filter )
131                             != 0 ) {
132                                 continue;
133                         }
134                 }
135                 if ( attr == NULL || a->acl_attrs == NULL || charray_inlist(
136                     a->acl_attrs, attr ) ) {
137                         Debug( LDAP_DEBUG_ACL, "<= acl_get: global acl #%d\n",
138                             i, e->e_dn, attr );
139                         return( a );
140                 }
141         }
142         Debug( LDAP_DEBUG_ACL, "<= acl_get: no match\n", 0, 0, 0 );
143
144         return( NULL );
145 }
146
147 /*
148  * acl_access_allowed - check whether the given acl allows dn the
149  * requested access to entry e, attribute attr, value val.  if val
150  * is null, access to the whole attribute is assumed (all values).
151  *
152  * returns      0       access NOT allowed
153  *              1       access allowed
154  */
155
156 int
157 acl_access_allowed(
158     struct acl          *a,
159     Backend             *be,
160     Connection          *conn,
161     Entry               *e,
162     struct berval       *val,
163     Operation           *op,
164     int                 access
165 )
166 {
167         int             i;
168         char            *edn, *odn;
169         struct access   *b;
170         Attribute       *at;
171         struct berval   bv;
172         int             default_access;
173
174         Debug( LDAP_DEBUG_ACL, "=> acl: %s access to value \"%s\" by \"%s\"\n",
175             access2str( access ), val ? val->bv_val : "any", op->o_dn ?
176             op->o_dn : "" );
177
178         if ( be_isroot( be, op->o_dn ) ) {
179                 Debug( LDAP_DEBUG_ACL, "<= acl: granted to database root\n",
180                     0, 0, 0 );
181                 return( 1 );
182         }
183
184         default_access = be->be_dfltaccess ? be->be_dfltaccess :
185             global_default_access;
186         if ( a == NULL ) {
187                 Debug( LDAP_DEBUG_ACL,
188                     "<= acl: %s by default (no matching to)\n",
189                     default_access >= access ? "granted" : "denied", 0, 0 );
190                 return( default_access >= access );
191         }
192
193         odn = NULL;
194         if ( op->o_dn != NULL ) {
195                 odn = dn_normalize_case( strdup( op->o_dn ) );
196                 bv.bv_val = odn;
197                 bv.bv_len = strlen( odn );
198         }
199         for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
200                 if ( b->a_dnpat != NULL ) {
201                         /*
202                          * if access applies to the entry itself, and the
203                          * user is bound as somebody in the same namespace as
204                          * the entry, OR the given dn matches the dn pattern
205                          */
206                         if ( strcasecmp( b->a_dnpat, "self" ) == 0 && op->o_dn
207                             != NULL && *(op->o_dn) && e->e_dn != NULL ) {
208                                 edn = dn_normalize_case( strdup( e->e_dn ) );
209                                 if ( strcasecmp( edn, op->o_dn ) == 0 ) {
210                                         free( edn );
211                                         if ( odn ) free( odn );
212                                         Debug( LDAP_DEBUG_ACL,
213                                     "<= acl: matched by clause #%d access %s\n",
214                                             i, (b->a_access & ~ACL_SELF) >=
215                                             access ? "granted" : "denied", 0 );
216
217                                         return( (b->a_access & ~ACL_SELF)
218                                             >= access );
219                                 }
220                                 free( edn );
221                         } else {
222                                 if ( regex_matches( b->a_dnpat, odn ) ) {
223                                         if ( odn ) free( odn );
224                                         Debug( LDAP_DEBUG_ACL,
225                                     "<= acl: matched by clause #%d access %s\n",
226                                     i, (b->a_access & ~ACL_SELF) >= access ?
227                                             "granted" : "denied", 0 );
228
229                                         return( (b->a_access & ~ACL_SELF)
230                                             >= access );
231                                 }
232                         }
233                 }
234                 if ( b->a_addrpat != NULL ) {
235                         if ( regex_matches( b->a_addrpat, conn->c_addr ) ) {
236                                 if ( odn ) free( odn );
237                                 Debug( LDAP_DEBUG_ACL,
238                                     "<= acl: matched by clause #%d access %s\n",
239                                     i, (b->a_access & ~ACL_SELF) >= access ?
240                                     "granted" : "denied", 0 );
241
242                                 return( (b->a_access & ~ACL_SELF) >= access );
243                         }
244                 }
245                 if ( b->a_domainpat != NULL ) {
246                         if ( regex_matches( b->a_domainpat, conn->c_domain ) ) {
247                                 if ( odn ) free( odn );
248                                 Debug( LDAP_DEBUG_ACL,
249                                     "<= acl: matched by clause #%d access %s\n",
250                                     i, (b->a_access & ~ACL_SELF) >= access ?
251                                     "granted" : "denied", 0 );
252
253                                 return( (b->a_access & ~ACL_SELF) >= access );
254                         }
255                 }
256                 if ( b->a_dnattr != NULL && op->o_dn != NULL ) {
257                         /* see if asker is listed in dnattr */
258                         if ( (at = attr_find( e->e_attrs, b->a_dnattr ))
259                             != NULL && value_find( at->a_vals, &bv,
260                             at->a_syntax, 3 ) == 0 )
261                         {
262                                 if ( (b->a_access & ACL_SELF) && (val == NULL
263                                     || value_cmp( &bv, val, at->a_syntax,
264                                     2 )) ) {
265                                         continue;
266                                 }
267
268                                 if ( odn ) free( odn );
269                                 Debug( LDAP_DEBUG_ACL,
270                                     "<= acl: matched by clause #%d access %s\n",
271                                     i, (b->a_access & ~ACL_SELF) >= access ?
272                                     "granted" : "denied", 0 );
273
274                                 return( (b->a_access & ~ACL_SELF) >= access );
275                         }
276
277                         /* asker not listed in dnattr - check for self access */
278                         if ( ! (b->a_access & ACL_SELF) || val == NULL ||
279                             value_cmp( &bv, val, at->a_syntax, 2 ) != 0 ) {
280                                 continue;
281                         }
282
283                         if ( odn ) free( odn );
284                         Debug( LDAP_DEBUG_ACL,
285                             "<= acl: matched by clause #%d (self) access %s\n",
286                             i, (b->a_access & ~ACL_SELF) >= access ? "granted"
287                             : "denied", 0 );
288
289                         return( (b->a_access & ~ACL_SELF) >= access );
290                 }
291         }
292
293         if ( odn ) free( odn );
294         Debug( LDAP_DEBUG_ACL, "<= acl: %s by default (no matching by)\n",
295             default_access >= access ? "granted" : "denied", 0, 0 );
296
297         return( default_access >= access );
298 }
299
300 /*
301  * acl_check_mods - check access control on the given entry to see if
302  * it allows the given modifications by the user associated with op.
303  * returns      LDAP_SUCCESS    mods allowed ok
304  *              anything else   mods not allowed - return is an error
305  *                              code indicating the problem
306  */
307
308 int
309 acl_check_mods(
310     Backend     *be,
311     Connection  *conn,
312     Operation   *op,
313     Entry       *e,
314     LDAPMod     *mods
315 )
316 {
317         int             i;
318         struct acl      *a;
319
320         for ( ; mods != NULL; mods = mods->mod_next ) {
321                 if ( strcasecmp( mods->mod_type, "modifiersname" ) == 0 ||
322                     strcasecmp( mods->mod_type, "modifytimestamp" ) == 0 ) {
323                         continue;
324                 }
325
326                 a = acl_get_applicable( be, op, e, mods->mod_type );
327
328                 switch ( mods->mod_op & ~LDAP_MOD_BVALUES ) {
329                 case LDAP_MOD_REPLACE:
330                 case LDAP_MOD_ADD:
331                         if ( mods->mod_bvalues == NULL ) {
332                                 break;
333                         }
334                         for ( i = 0; mods->mod_bvalues[i] != NULL; i++ ) {
335                                 if ( ! acl_access_allowed( a, be, conn, e,
336                                     mods->mod_bvalues[i], op, ACL_WRITE ) ) {
337                                         return( LDAP_INSUFFICIENT_ACCESS );
338                                 }
339                         }
340                         break;
341
342                 case LDAP_MOD_DELETE:
343                         if ( mods->mod_bvalues == NULL ) {
344                                 if ( ! acl_access_allowed( a, be, conn, e,
345                                     NULL, op, ACL_WRITE ) ) {
346                                         return( LDAP_INSUFFICIENT_ACCESS );
347                                 }
348                                 break;
349                         }
350                         for ( i = 0; mods->mod_bvalues[i] != NULL; i++ ) {
351                                 if ( ! acl_access_allowed( a, be, conn, e,
352                                     mods->mod_bvalues[i], op, ACL_WRITE ) ) {
353                                         return( LDAP_INSUFFICIENT_ACCESS );
354                                 }
355                         }
356                         break;
357                 }
358         }
359
360         return( LDAP_SUCCESS );
361 }
362
363 #ifdef sunos5
364
365 static int
366 regex_matches( char *pat, char *str )
367 {
368         char    *e;
369         int     rc;
370
371         if ( (e = compile( pat, NULL, NULL )) == NULL ) {
372                 Debug( LDAP_DEBUG_ANY,
373                     "compile( \"%s\", \"%s\") failed\n", pat, str, 0 );
374                 return( 0 );
375         }
376         rc = step( str ? str : "", e );
377         free( e );
378
379         return( rc );
380 }
381
382 #else /* sunos5 */
383
384 static int
385 regex_matches( char *pat, char *str )
386 {
387         char    *e;
388         int     rc;
389
390         pthread_mutex_lock( &regex_mutex );
391         if ( (e = re_comp( pat )) != NULL ) {
392                 Debug( LDAP_DEBUG_ANY,
393                     "re_comp( \"%s\", \"%s\") failed because (%s)\n", pat, str,
394                     e );
395                 pthread_mutex_unlock( &regex_mutex );
396                 return( 0 );
397         }
398         rc = re_exec( str ? str : "" );
399         pthread_mutex_unlock( &regex_mutex );
400
401         return( rc == 1 );
402 }
403
404 #endif /* sunos5 */