summaryrefslogtreecommitdiffstats
path: root/contrib/slapd-modules/allowed/allowed.c
blob: a01cf8312933640d9745939632f2aa469adf2e4b (plain)
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/* allowed.c - add allowed attributes based on ACL */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
 * Copyright 2006-2024 The OpenLDAP Foundation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by Pierangelo Masarati for inclusion in
 * OpenLDAP Software.
 */

/*
 * Rationale: return in allowedAttributes the attributes required/allowed
 * by the objectClasses that are currently present in an object; return
 * in allowedAttributesEffective the subset of the above that can be written
 * by the identity that performs the search.
 *
 * Caveats:
 * - right now, the overlay assumes that all values of the objectClass
 *   attribute will be returned in rs->sr_entry; this may not be true
 *   in general, but it usually is for back-mdb.  To generalize,
 *   the search request should be analyzed, and if allowedAttributes or
 *   allowedAttributesEffective are requested, add objectClass to the
 *   requested attributes
 * - it assumes that there is no difference between write-add and 
 *   write-delete
 * - it assumes that access rules do not depend on the values of the 
 *   attributes or on the contents of the entry (attr/val, filter, ...)
 *   allowedAttributes and allowedAttributesEffective cannot be used
 *   in filters or in compare
 */

#include "portable.h"

/* define SLAPD_OVER_ALLOWED=2 to build as run-time loadable module */
#ifdef SLAPD_OVER_ALLOWED

#include "slap.h"

/*
 * NOTE: part of the schema definition reported below is taken
 * from Microsoft schema definitions (OID, NAME, SYNTAX);
 *
 * EQUALITY is taken from
 * <http://www.redhat.com/archives/fedora-directory-devel/2006-August/msg00007.html>
 * (posted by Andrew Bartlett)
 *
 * The rest is guessed.  Specifically
 *
 * DESC briefly describes the purpose
 *
 * NO-USER-MODIFICATION is added to make attributes operational
 *
 * USAGE is set to "dSAOperation" as per ITS#7493,
 * to prevent replication, since this information
 * is generated (based on ACL and identity of request)
 * and not stored.
 */

#define AA_SCHEMA_AT "1.2.840.113556.1.4"

static AttributeDescription
		*ad_allowedChildClasses,
		*ad_allowedChildClassesEffective,
		*ad_allowedAttributes,
		*ad_allowedAttributesEffective;

static struct {
	char *at;
	AttributeDescription **ad;
} aa_attrs[] = {
	{ "( " AA_SCHEMA_AT ".911 "
		"NAME 'allowedChildClasses' "
		"EQUALITY objectIdentifierMatch "
		"SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
		/* added by me :) */
		"DESC 'Child classes allowed for a given object' "
		"NO-USER-MODIFICATION "
		"USAGE dSAOperation )", &ad_allowedChildClasses },
	{ "( " AA_SCHEMA_AT ".912 "
		"NAME 'allowedChildClassesEffective' "
		"EQUALITY objectIdentifierMatch "
		"SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
		/* added by me :) */
		"DESC 'Child classes allowed for a given object according to ACLs' "
		"NO-USER-MODIFICATION "
		"USAGE dSAOperation )", &ad_allowedChildClassesEffective },
	{ "( " AA_SCHEMA_AT ".913 "
		"NAME 'allowedAttributes' "
		"EQUALITY objectIdentifierMatch "
		"SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
		/* added by me :) */
		"DESC 'Attributes allowed for a given object' "
		"NO-USER-MODIFICATION "
		"USAGE dSAOperation )", &ad_allowedAttributes },
	{ "( " AA_SCHEMA_AT ".914 "
		"NAME 'allowedAttributesEffective' "
		"EQUALITY objectIdentifierMatch "
		"SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
		/* added by me :) */
		"DESC 'Attributes allowed for a given object according to ACLs' "
		"NO-USER-MODIFICATION "
		"USAGE dSAOperation )", &ad_allowedAttributesEffective },

	/* TODO: add objectClass stuff? */

	{ NULL, NULL }
};

static int
aa_add_at( AttributeType *at, AttributeType ***atpp )
{
	int		i = 0;

	if ( *atpp ) {
		for ( i = 0; (*atpp)[ i ] != NULL; i++ ) {
			if ( (*atpp)[ i ] == at ) {
				break;
			}
		}
	
		if ( (*atpp)[ i ] != NULL ) {
			return 0;
		}
	}

	*atpp = ch_realloc( *atpp, sizeof( AttributeType * ) * ( i + 2 ) );
	(*atpp)[ i ] = at;
	(*atpp)[ i + 1 ] = NULL;

	return 0;
}

static int
aa_add_oc( ObjectClass *oc, ObjectClass ***ocpp, AttributeType ***atpp )
{
	int		i = 0;

	if ( *ocpp ) {
		for ( ; (*ocpp)[ i ] != NULL; i++ ) {
			if ( (*ocpp)[ i ] == oc ) {
				break;
			}
		}

		if ( (*ocpp)[ i ] != NULL ) {
			return 0;
		}
	}

	*ocpp = ch_realloc( *ocpp, sizeof( ObjectClass * ) * ( i + 2 ) );
	(*ocpp)[ i ] = oc;
	(*ocpp)[ i + 1 ] = NULL;

	if ( oc->soc_required ) {
		int		i;

		for ( i = 0; oc->soc_required[ i ] != NULL; i++ ) {
			aa_add_at( oc->soc_required[ i ], atpp );
		}
	}

	if ( oc->soc_allowed ) {
		int		i;

		for ( i = 0; oc->soc_allowed[ i ] != NULL; i++ ) {
			aa_add_at( oc->soc_allowed[ i ], atpp );
		}
	}

	return 0;
}

static int
aa_operational( Operation *op, SlapReply *rs )
{
	Attribute		*a, **ap;
	AccessControlState	acl_state = ACL_STATE_INIT;
	struct berval		*v;
	AttributeType		**atp = NULL;
	ObjectClass		**ocp = NULL;

#define	GOT_NONE	(0x0U)
#define	GOT_C		(0x1U)
#define	GOT_CE		(0x2U)
#define	GOT_A		(0x4U)
#define	GOT_AE		(0x8U)
#define	GOT_ALL		(GOT_C|GOT_CE|GOT_A|GOT_AE)
	int		got = GOT_NONE;

	/* only add if requested */
	if ( SLAP_OPATTRS( rs->sr_attr_flags ) ) {
		got = GOT_ALL;

	} else {
		if ( ad_inlist( ad_allowedChildClasses, rs->sr_attrs ) ) {
			got |= GOT_C;
		}

		if ( ad_inlist( ad_allowedChildClassesEffective, rs->sr_attrs ) ) {
			got |= GOT_CE;
		}

		if ( ad_inlist( ad_allowedAttributes, rs->sr_attrs ) ) {
			got |= GOT_A;
		}

		if ( ad_inlist( ad_allowedAttributesEffective, rs->sr_attrs ) ) {
			got |= GOT_AE;
		}
	}

	if ( got == GOT_NONE ) {
		return SLAP_CB_CONTINUE;
	}

	/* shouldn't be called without an entry; please check */
	assert( rs->sr_entry != NULL );

	for ( ap = &rs->sr_operational_attrs; *ap != NULL; ap = &(*ap)->a_next )
		/* go to last */ ;

	/* see caveats; this is not guaranteed for all backends */
	a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_objectClass );
	if ( a == NULL ) {
		goto do_oc;
	}

	/* if client has no access to objectClass attribute; don't compute */
	if ( !access_allowed( op, rs->sr_entry, slap_schema.si_ad_objectClass,
				NULL, ACL_READ, &acl_state ) )
	{
		return SLAP_CB_CONTINUE;
	}

	for ( v = a->a_nvals; !BER_BVISNULL( v ); v++ ) {
		ObjectClass	*oc = oc_bvfind( v );

		assert( oc != NULL );

		/* if client has no access to specific value, don't compute */
		if ( !access_allowed( op, rs->sr_entry,
			slap_schema.si_ad_objectClass,
			&oc->soc_cname, ACL_READ, &acl_state ) )
		{
			continue;
		}

		aa_add_oc( oc, &ocp, &atp );

		if ( oc->soc_sups ) {
			int i;

			for ( i = 0; oc->soc_sups[ i ] != NULL; i++ ) {
				aa_add_oc( oc->soc_sups[ i ], &ocp, &atp );
			}
		}
	}

	ch_free( ocp );

	if ( atp != NULL ) {
		BerVarray	bv_allowed = NULL,
				bv_effective = NULL;
		int		i, ja = 0, je = 0;

		for ( i = 0; atp[ i ] != NULL; i++ )
			/* just count */ ;
	
		if ( got & GOT_A ) {
			bv_allowed = ch_calloc( i + 1,  sizeof( struct berval ) );
		}
		if ( got & GOT_AE ) {
			bv_effective = ch_calloc( i + 1, sizeof( struct berval ) );
		}

		for ( i = 0, ja = 0, je = 0; atp[ i ] != NULL; i++ ) {
			if ( got & GOT_A ) {
				ber_dupbv( &bv_allowed[ ja ], &atp[ i ]->sat_cname );
				ja++;
			}

			if ( got & GOT_AE ) {
				AttributeDescription	*ad = NULL;
				const char		*text = NULL;
	
				if ( slap_bv2ad( &atp[ i ]->sat_cname, &ad, &text ) ) {
					/* log? */
					continue;
				}

				if ( access_allowed( op, rs->sr_entry,
					ad, NULL, ACL_WRITE, NULL ) )
				{
					ber_dupbv( &bv_effective[ je ], &atp[ i ]->sat_cname );
					je++;
				}
			}
		}

		ch_free( atp );

		if ( ( got & GOT_A ) && ja > 0 ) {
			*ap = attr_alloc( ad_allowedAttributes );
			(*ap)->a_vals = bv_allowed;
			(*ap)->a_nvals = bv_allowed;
			(*ap)->a_numvals = ja;
			ap = &(*ap)->a_next;
		}

		if ( ( got & GOT_AE ) && je > 0 ) {
			*ap = attr_alloc( ad_allowedAttributesEffective );
			(*ap)->a_vals = bv_effective;
			(*ap)->a_nvals = bv_effective;
			(*ap)->a_numvals = je;
			ap = &(*ap)->a_next;
		}

		*ap = NULL;
	}

do_oc:;
	if ( ( got & GOT_C ) || ( got & GOT_CE ) ) {
		BerVarray	bv_allowed = NULL,
				bv_effective = NULL;
		int		i, ja = 0, je = 0;

		ObjectClass	*oc;

		for ( i = 0, oc_start( &oc ); oc != NULL; oc_next( &oc ) ) {
			/* we can only add AUXILIARY objectClasses */
			if ( oc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
				continue;
			}

			i++;
		}

		if ( got & GOT_C ) {
			bv_allowed = ch_calloc( i + 1,  sizeof( struct berval ) );
		}
		if ( got & GOT_CE ) {
			bv_effective = ch_calloc( i + 1, sizeof( struct berval ) );
		}

		for ( oc_start( &oc ); oc != NULL; oc_next( &oc ) ) {
			/* we can only add AUXILIARY objectClasses */
			if ( oc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
				continue;
			}

			if ( got & GOT_C ) {
				ber_dupbv( &bv_allowed[ ja ], &oc->soc_cname );
				ja++;
			}

			if ( got & GOT_CE ) {
				if ( !access_allowed( op, rs->sr_entry,
					slap_schema.si_ad_objectClass,
					&oc->soc_cname, ACL_WRITE, NULL ) )
				{
					goto done_ce;
				}

				if ( oc->soc_required ) {
					for ( i = 0; oc->soc_required[ i ] != NULL; i++ ) {
						AttributeDescription	*ad = NULL;
						const char		*text = NULL;
	
						if ( slap_bv2ad( &oc->soc_required[ i ]->sat_cname, &ad, &text ) ) {
							/* log? */
							continue;
						}

						if ( !access_allowed( op, rs->sr_entry,
							ad, NULL, ACL_WRITE, NULL ) )
						{
							goto done_ce;
						}
					}
				}

				ber_dupbv( &bv_effective[ je ], &oc->soc_cname );
				je++;
			}
done_ce:;
		}

		if ( ( got & GOT_C ) && ja > 0 ) {
			*ap = attr_alloc( ad_allowedChildClasses );
			(*ap)->a_vals = bv_allowed;
			(*ap)->a_nvals = bv_allowed;
			(*ap)->a_numvals = ja;
			ap = &(*ap)->a_next;
		}

		if ( ( got & GOT_CE ) && je > 0 ) {
			*ap = attr_alloc( ad_allowedChildClassesEffective );
			(*ap)->a_vals = bv_effective;
			(*ap)->a_nvals = bv_effective;
			(*ap)->a_numvals = je;
			ap = &(*ap)->a_next;
		}

		*ap = NULL;
	}

	return SLAP_CB_CONTINUE;
}

static slap_overinst aa;

#if LDAP_VENDOR_VERSION_MINOR != X && LDAP_VENDOR_VERSION_MINOR <= 3
/* backport register_at() from HEAD, to allow building with OL <= 2.3 */
static int
register_at( char *def, AttributeDescription **rad, int dupok )
{
	LDAPAttributeType *at;
	int code, freeit = 0;
	const char *err;
	AttributeDescription *ad = NULL;

	at = ldap_str2attributetype( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
	if ( !at ) {
		Debug( LDAP_DEBUG_ANY,
			"register_at: AttributeType \"%s\": %s, %s\n",
				def, ldap_scherr2str(code), err );
		return code;
	}

	code = at_add( at, 0, NULL, &err );
	if ( code ) {
		if ( code == SLAP_SCHERR_ATTR_DUP && dupok ) {
			freeit = 1;

		} else {
			ldap_attributetype_free( at );
			Debug( LDAP_DEBUG_ANY,
				"register_at: AttributeType \"%s\": %s, %s\n",
				def, scherr2str(code), err );
			return code;
		}
	}
	code = slap_str2ad( at->at_names[0], &ad, &err );
	if ( freeit || code ) {
		ldap_attributetype_free( at );
	} else {
		ldap_memfree( at );
	}
	if ( code ) {
		Debug( LDAP_DEBUG_ANY, "register_at: AttributeType \"%s\": %s\n",
			def, err );
	}
	if ( rad ) *rad = ad;
	return code;
}
#endif

#if SLAPD_OVER_ALLOWED == SLAPD_MOD_DYNAMIC
static
#endif /* SLAPD_OVER_ALLOWED == SLAPD_MOD_DYNAMIC */
int
aa_initialize( void )
{
	int i;

	aa.on_bi.bi_type = "allowed";

	aa.on_bi.bi_flags = SLAPO_BFLAG_SINGLE;
	aa.on_bi.bi_operational = aa_operational;

	/* aa schema integration */
	for ( i = 0; aa_attrs[i].at; i++ ) {
		int code;

		code = register_at( aa_attrs[i].at, aa_attrs[i].ad, 0 );
		if ( code ) {
			Debug( LDAP_DEBUG_ANY,
				"aa_initialize: register_at failed\n" );
			return -1;
		}
	}

	return overlay_register( &aa );
}

#if SLAPD_OVER_ALLOWED == SLAPD_MOD_DYNAMIC
int
init_module( int argc, char *argv[] )
{
	return aa_initialize();
}
#endif /* SLAPD_OVER_ALLOWED == SLAPD_MOD_DYNAMIC */

#endif /* SLAPD_OVER_ALLOWED */