acl.c 9.55 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* acl.c - routines to parse and check acl's */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#ifdef sunos5
#include "regexpr.h"
#else
#include "regex.h"
#endif
#include "slap.h"

extern Attribute	*attr_find();
extern char		*re_comp();
extern struct acl	*global_acl;
extern int		global_default_access;
extern char		*access2str();
extern char		*dn_normalize_case();

int		acl_access_allowed();
int		access_allowed();
struct acl	*acl_get_applicable();

static int	regex_matches();

extern pthread_mutex_t	regex_mutex;

/*
 * access_allowed - check whether dn is allowed the requested access
 * to entry e, attribute attr, value val.  if val is null, access to
 * the whole attribute is assumed (all values).  this routine finds
 * the applicable acl and calls acl_access_allowed() to make the
 * decision.
 *
 * returns	0	access NOT allowed
 *		1	access allowed
 */

int
access_allowed(
    Backend		*be,
    Connection		*conn,
    Operation		*op,
    Entry		*e,
    char		*attr,
    struct berval	*val,
    char		*dn,
    int			access
)
{
	int		rc;
	struct acl	*a;

	if ( be == NULL ) {
		return( 0 );
	}

	a = acl_get_applicable( be, op, e, attr );
	rc = acl_access_allowed( a, be, conn, e, val, op, access );

	return( rc );
}

/*
 * acl_get_applicable - return the acl applicable to entry e, attribute
 * attr.  the acl returned is suitable for use in subsequent calls to
 * acl_access_allowed().
 */

struct acl *
acl_get_applicable(
    Backend		*be,
    Operation		*op,
    Entry		*e,
    char		*attr
)
{
	int		i;
	struct acl	*a;
	char		*edn;

	Debug( LDAP_DEBUG_ACL, "=> acl_get: entry (%s) attr (%s)\n", e->e_dn,
	    attr, 0 );

	if ( be_isroot( be, op->o_dn ) ) {
		Debug( LDAP_DEBUG_ACL,
		    "<= acl_get: no acl applicable to database root\n", 0, 0,
		    0 );
		return( NULL );
	}

	/* check for a backend-specific acl that matches the entry */
	for ( i = 1, a = be->be_acl; a != NULL; a = a->acl_next, i++ ) {
		if ( a->acl_dnpat != NULL ) {
			edn = dn_normalize_case( strdup( e->e_dn ) );
			if ( ! regex_matches( a->acl_dnpat, edn ) ) {
				free( edn );
				continue;
			}
			free( edn );
		}
		if ( a->acl_filter != NULL ) {
			if ( test_filter( NULL, NULL, NULL, e, a->acl_filter )
			    != 0 ) {
				continue;
			}
		}
		if ( attr == NULL || a->acl_attrs == NULL ||
		    charray_inlist( a->acl_attrs, attr ) ) {
			Debug( LDAP_DEBUG_ACL, "<= acl_get: backend acl #%d\n",
			    i, e->e_dn, attr );
			return( a );
		}
	}

	/* check for a global acl that matches the entry */
	for ( i = 1, a = global_acl; a != NULL; a = a->acl_next, i++ ) {
		if ( a->acl_dnpat != NULL ) {
			edn = dn_normalize_case( strdup( e->e_dn ) );
			if ( ! regex_matches( a->acl_dnpat, edn ) ) {
				free( edn );
				continue;
			}
			free( edn );
		}
		if ( a->acl_filter != NULL ) {
			if ( test_filter( NULL, NULL, NULL, e, a->acl_filter )
			    != 0 ) {
				continue;
			}
		}
		if ( attr == NULL || a->acl_attrs == NULL || charray_inlist(
		    a->acl_attrs, attr ) ) {
			Debug( LDAP_DEBUG_ACL, "<= acl_get: global acl #%d\n",
			    i, e->e_dn, attr );
			return( a );
		}
	}
	Debug( LDAP_DEBUG_ACL, "<= acl_get: no match\n", 0, 0, 0 );

	return( NULL );
}

/*
 * acl_access_allowed - check whether the given acl allows dn the
 * requested access to entry e, attribute attr, value val.  if val
 * is null, access to the whole attribute is assumed (all values).
 *
 * returns	0	access NOT allowed
 *		1	access allowed
 */

int
acl_access_allowed(
    struct acl		*a,
    Backend		*be,
    Connection		*conn,
    Entry		*e,
    struct berval	*val,
    Operation		*op,
    int			access
)
{
	int		i;
	char		*edn, *odn;
	struct access	*b;
	Attribute	*at;
	struct berval	bv;
	int		default_access;

	Debug( LDAP_DEBUG_ACL, "=> acl: %s access to value \"%s\" by \"%s\"\n",
	    access2str( access ), val ? val->bv_val : "any", op->o_dn ?
	    op->o_dn : "" );

	if ( be_isroot( be, op->o_dn ) ) {
		Debug( LDAP_DEBUG_ACL, "<= acl: granted to database root\n",
		    0, 0, 0 );
		return( 1 );
	}

	default_access = be->be_dfltaccess ? be->be_dfltaccess :
	    global_default_access;
	if ( a == NULL ) {
		Debug( LDAP_DEBUG_ACL,
		    "<= acl: %s by default (no matching to)\n",
		    default_access >= access ? "granted" : "denied", 0, 0 );
		return( default_access >= access );
	}

	odn = NULL;
	if ( op->o_dn != NULL ) {
		odn = dn_normalize_case( strdup( op->o_dn ) );
		bv.bv_val = odn;
		bv.bv_len = strlen( odn );
	}
	for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
		if ( b->a_dnpat != NULL ) {
			/*
			 * if access applies to the entry itself, and the
			 * user is bound as somebody in the same namespace as
			 * the entry, OR the given dn matches the dn pattern
			 */
			if ( strcasecmp( b->a_dnpat, "self" ) == 0 && op->o_dn
			    != NULL && *(op->o_dn) && e->e_dn != NULL ) {
				edn = dn_normalize_case( strdup( e->e_dn ) );
				if ( strcasecmp( edn, op->o_dn ) == 0 ) {
					free( edn );
					if ( odn ) free( odn );
					Debug( LDAP_DEBUG_ACL,
				    "<= acl: matched by clause #%d access %s\n",
					    i, (b->a_access & ~ACL_SELF) >=
					    access ? "granted" : "denied", 0 );

					return( (b->a_access & ~ACL_SELF)
					    >= access );
				}
				free( edn );
			} else {
				if ( regex_matches( b->a_dnpat, odn ) ) {
					if ( odn ) free( odn );
					Debug( LDAP_DEBUG_ACL,
				    "<= acl: matched by clause #%d access %s\n",
				    i, (b->a_access & ~ACL_SELF) >= access ?
					    "granted" : "denied", 0 );

					return( (b->a_access & ~ACL_SELF)
					    >= access );
				}
			}
		}
		if ( b->a_addrpat != NULL ) {
			if ( regex_matches( b->a_addrpat, conn->c_addr ) ) {
				if ( odn ) free( odn );
				Debug( LDAP_DEBUG_ACL,
				    "<= acl: matched by clause #%d access %s\n",
				    i, (b->a_access & ~ACL_SELF) >= access ?
				    "granted" : "denied", 0 );

				return( (b->a_access & ~ACL_SELF) >= access );
			}
		}
		if ( b->a_domainpat != NULL ) {
			if ( regex_matches( b->a_domainpat, conn->c_domain ) ) {
				if ( odn ) free( odn );
				Debug( LDAP_DEBUG_ACL,
				    "<= acl: matched by clause #%d access %s\n",
				    i, (b->a_access & ~ACL_SELF) >= access ?
				    "granted" : "denied", 0 );

				return( (b->a_access & ~ACL_SELF) >= access );
			}
		}
		if ( b->a_dnattr != NULL && op->o_dn != NULL ) {
			/* see if asker is listed in dnattr */
			if ( (at = attr_find( e->e_attrs, b->a_dnattr ))
			    != NULL && value_find( at->a_vals, &bv,
			    at->a_syntax, 3 ) == 0 )
			{
				if ( (b->a_access & ACL_SELF) && (val == NULL
				    || value_cmp( &bv, val, at->a_syntax,
				    2 )) ) {
					continue;
				}

				if ( odn ) free( odn );
				Debug( LDAP_DEBUG_ACL,
				    "<= acl: matched by clause #%d access %s\n",
				    i, (b->a_access & ~ACL_SELF) >= access ?
				    "granted" : "denied", 0 );

				return( (b->a_access & ~ACL_SELF) >= access );
			}

			/* asker not listed in dnattr - check for self access */
			if ( ! (b->a_access & ACL_SELF) || val == NULL ||
			    value_cmp( &bv, val, at->a_syntax, 2 ) != 0 ) {
				continue;
			}

			if ( odn ) free( odn );
			Debug( LDAP_DEBUG_ACL,
			    "<= acl: matched by clause #%d (self) access %s\n",
			    i, (b->a_access & ~ACL_SELF) >= access ? "granted"
			    : "denied", 0 );

			return( (b->a_access & ~ACL_SELF) >= access );
		}
	}

	if ( odn ) free( odn );
	Debug( LDAP_DEBUG_ACL, "<= acl: %s by default (no matching by)\n",
	    default_access >= access ? "granted" : "denied", 0, 0 );

	return( default_access >= access );
}

/*
 * acl_check_mods - check access control on the given entry to see if
 * it allows the given modifications by the user associated with op.
 * returns	LDAP_SUCCESS	mods allowed ok
 *		anything else	mods not allowed - return is an error
 *				code indicating the problem
 */

int
acl_check_mods(
    Backend	*be,
    Connection	*conn,
    Operation	*op,
    Entry	*e,
    LDAPMod	*mods
)
{
	int		i;
	struct acl	*a;

	for ( ; mods != NULL; mods = mods->mod_next ) {
		if ( strcasecmp( mods->mod_type, "modifiersname" ) == 0 ||
		    strcasecmp( mods->mod_type, "modifytimestamp" ) == 0 ) {
			continue;
		}

		a = acl_get_applicable( be, op, e, mods->mod_type );

		switch ( mods->mod_op & ~LDAP_MOD_BVALUES ) {
		case LDAP_MOD_REPLACE:
		case LDAP_MOD_ADD:
			if ( mods->mod_bvalues == NULL ) {
				break;
			}
			for ( i = 0; mods->mod_bvalues[i] != NULL; i++ ) {
				if ( ! acl_access_allowed( a, be, conn, e,
				    mods->mod_bvalues[i], op, ACL_WRITE ) ) {
					return( LDAP_INSUFFICIENT_ACCESS );
				}
			}
			break;

		case LDAP_MOD_DELETE:
			if ( mods->mod_bvalues == NULL ) {
				if ( ! acl_access_allowed( a, be, conn, e,
				    NULL, op, ACL_WRITE ) ) {
					return( LDAP_INSUFFICIENT_ACCESS );
				}
				break;
			}
			for ( i = 0; mods->mod_bvalues[i] != NULL; i++ ) {
				if ( ! acl_access_allowed( a, be, conn, e,
				    mods->mod_bvalues[i], op, ACL_WRITE ) ) {
					return( LDAP_INSUFFICIENT_ACCESS );
				}
			}
			break;
		}
	}

	return( LDAP_SUCCESS );
}

#ifdef sunos5

static int
regex_matches( char *pat, char *str )
{
	char	*e;
	int	rc;

	if ( (e = compile( pat, NULL, NULL )) == NULL ) {
		Debug( LDAP_DEBUG_ANY,
		    "compile( \"%s\", \"%s\") failed\n", pat, str, 0 );
		return( 0 );
	}
	rc = step( str ? str : "", e );
	free( e );

	return( rc );
}

#else /* sunos5 */

static int
regex_matches( char *pat, char *str )
{
	char	*e;
	int	rc;

	pthread_mutex_lock( &regex_mutex );
	if ( (e = re_comp( pat )) != NULL ) {
		Debug( LDAP_DEBUG_ANY,
		    "re_comp( \"%s\", \"%s\") failed because (%s)\n", pat, str,
		    e );
		pthread_mutex_unlock( &regex_mutex );
		return( 0 );
	}
	rc = re_exec( str ? str : "" );
	pthread_mutex_unlock( &regex_mutex );

	return( rc == 1 );
}

#endif /* sunos5 */