allowed.c 11.9 KB
Newer Older
1
2
3
4
/* allowed.c - add allowed attributes based on ACL */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
5
 * Copyright 2006-2020 The OpenLDAP Foundation.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * 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
30
 *   in general, but it usually is for back-mdb.  To generalize,
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 *   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"

/*
Pierangelo Masarati's avatar
Pierangelo Masarati committed
50
51
 * NOTE: part of the schema definition reported below is taken
 * from Microsoft schema definitions (OID, NAME, SYNTAX);
52
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
53
 * EQUALITY is taken from
54
 * <http://www.redhat.com/archives/fedora-directory-devel/2006-August/msg00007.html>
Pierangelo Masarati's avatar
Pierangelo Masarati committed
55
 * (posted by Andrew Bartlett)
56
 *
Pierangelo Masarati's avatar
Pierangelo Masarati committed
57
58
59
60
61
62
63
64
65
66
 * 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.
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 */

#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 "
88
		"USAGE dSAOperation )", &ad_allowedChildClasses },
89
90
91
92
93
94
95
	{ "( " 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 "
96
		"USAGE dSAOperation )", &ad_allowedChildClassesEffective },
97
98
99
100
101
102
103
	{ "( " 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 "
104
		"USAGE dSAOperation )", &ad_allowedAttributes },
105
106
107
108
109
110
111
	{ "( " 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 "
112
		"USAGE dSAOperation )", &ad_allowedAttributesEffective },
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

	/* 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 );

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

232
233
234
	/* 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 ) {
235
		goto do_oc;
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
	}

	/* 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 ) {
261
262
			int i;

263
264
265
266
267
268
			for ( i = 0; oc->soc_sups[ i ] != NULL; i++ ) {
				aa_add_oc( oc->soc_sups[ i ], &ocp, &atp );
			}
		}
	}

269
270
	ch_free( ocp );

271
	if ( atp != NULL ) {
272
273
274
275
		BerVarray	bv_allowed = NULL,
				bv_effective = NULL;
		int		i, ja = 0, je = 0;

276
277
278
279
		for ( i = 0; atp[ i ] != NULL; i++ )
			/* just count */ ;
	
		if ( got & GOT_A ) {
280
			bv_allowed = ch_calloc( i + 1,  sizeof( struct berval ) );
281
282
		}
		if ( got & GOT_AE ) {
283
			bv_effective = ch_calloc( i + 1, sizeof( struct berval ) );
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
		}

		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++;
				}
			}
		}

310
		ch_free( atp );
311

312
		if ( ( got & GOT_A ) && ja > 0 ) {
313
314
315
			*ap = attr_alloc( ad_allowedAttributes );
			(*ap)->a_vals = bv_allowed;
			(*ap)->a_nvals = bv_allowed;
316
			(*ap)->a_numvals = ja;
317
318
			ap = &(*ap)->a_next;
		}
319

320
		if ( ( got & GOT_AE ) && je > 0 ) {
321
322
323
			*ap = attr_alloc( ad_allowedAttributesEffective );
			(*ap)->a_vals = bv_effective;
			(*ap)->a_nvals = bv_effective;
324
			(*ap)->a_numvals = je;
325
326
			ap = &(*ap)->a_next;
		}
327

328
329
330
		*ap = NULL;
	}

331
332
333
334
do_oc:;
	if ( ( got & GOT_C ) || ( got & GOT_CE ) ) {
		BerVarray	bv_allowed = NULL,
				bv_effective = NULL;
335
		int		i, ja = 0, je = 0;
336
337
338

		ObjectClass	*oc;

339
		for ( i = 0, oc_start( &oc ); oc != NULL; oc_next( &oc ) ) {
340
341
			/* we can only add AUXILIARY objectClasses */
			if ( oc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
342
343
344
345
346
347
348
				continue;
			}

			i++;
		}

		if ( got & GOT_C ) {
349
			bv_allowed = ch_calloc( i + 1,  sizeof( struct berval ) );
350
351
		}
		if ( got & GOT_CE ) {
352
			bv_effective = ch_calloc( i + 1, sizeof( struct berval ) );
353
354
355
		}

		for ( oc_start( &oc ); oc != NULL; oc_next( &oc ) ) {
356
357
			/* we can only add AUXILIARY objectClasses */
			if ( oc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
358
359
360
361
362
363
364
365
366
				continue;
			}

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

			if ( got & GOT_CE ) {
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
				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;
						}
					}
				}

392
393
394
				ber_dupbv( &bv_effective[ je ], &oc->soc_cname );
				je++;
			}
395
done_ce:;
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
		}

		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;
	}
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

	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",
461
			def, err );
462
463
464
465
466
467
468
469
470
471
472
473
474
475
	}
	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;

476
	aa.on_bi.bi_type = "allowed";
477

478
	aa.on_bi.bi_flags = SLAPO_BFLAG_SINGLE;
479
480
481
482
483
484
485
486
487
	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,
488
				"aa_initialize: register_at failed\n" );
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
			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 */