1 /* allowed.c - add allowed attributes based on ACL */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2006-2011 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Pierangelo Masarati for inclusion in
22 * Rationale: return in allowedAttributes the attributes required/allowed
23 * by the objectClasses that are currently present in an object; return
24 * in allowedAttributesEffective the subset of the above that can be written
25 * by the identity that performs the search.
28 * - right now, the overlay assumes that all values of the objectClass
29 * attribute will be returned in rs->sr_entry; this may not be true
30 * in general, but it usually is for back-bdb/back-hdb. To generalize,
31 * the search request should be analyzed, and if allowedAttributes or
32 * allowedAttributesEffective are requested, add objectClass to the
33 * requested attributes
34 * - it assumes that there is no difference between write-add and
36 * - it assumes that access rules do not depend on the values of the
37 * attributes or on the contents of the entry (attr/val, filter, ...)
38 * allowedAttributes and allowedAttributesEffective cannot be used
39 * in filters or in compare
44 /* define SLAPD_OVER_ALLOWED=2 to build as run-time loadable module */
45 #ifdef SLAPD_OVER_ALLOWED
52 * <http://www.redhat.com/archives/fedora-directory-devel/2006-August/msg00007.html>
54 * posted by Andrew Bartlett
57 #define AA_SCHEMA_AT "1.2.840.113556.1.4"
59 static AttributeDescription
60 *ad_allowedChildClasses,
61 *ad_allowedChildClassesEffective,
62 *ad_allowedAttributes,
63 *ad_allowedAttributesEffective;
67 AttributeDescription **ad;
69 { "( " AA_SCHEMA_AT ".911 "
70 "NAME 'allowedChildClasses' "
71 "EQUALITY objectIdentifierMatch "
72 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
74 "DESC 'Child classes allowed for a given object' "
75 "NO-USER-MODIFICATION "
76 "USAGE directoryOperation )", &ad_allowedChildClasses },
77 { "( " AA_SCHEMA_AT ".912 "
78 "NAME 'allowedChildClassesEffective' "
79 "EQUALITY objectIdentifierMatch "
80 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
82 "DESC 'Child classes allowed for a given object according to ACLs' "
83 "NO-USER-MODIFICATION "
84 "USAGE directoryOperation )", &ad_allowedChildClassesEffective },
85 { "( " AA_SCHEMA_AT ".913 "
86 "NAME 'allowedAttributes' "
87 "EQUALITY objectIdentifierMatch "
88 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
90 "DESC 'Attributes allowed for a given object' "
91 "NO-USER-MODIFICATION "
92 "USAGE directoryOperation )", &ad_allowedAttributes },
93 { "( " AA_SCHEMA_AT ".914 "
94 "NAME 'allowedAttributesEffective' "
95 "EQUALITY objectIdentifierMatch "
96 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
98 "DESC 'Attributes allowed for a given object according to ACLs' "
99 "NO-USER-MODIFICATION "
100 "USAGE directoryOperation )", &ad_allowedAttributesEffective },
102 /* TODO: add objectClass stuff? */
108 aa_add_at( AttributeType *at, AttributeType ***atpp )
113 for ( i = 0; (*atpp)[ i ] != NULL; i++ ) {
114 if ( (*atpp)[ i ] == at ) {
119 if ( (*atpp)[ i ] != NULL ) {
124 *atpp = ch_realloc( *atpp, sizeof( AttributeType * ) * ( i + 2 ) );
126 (*atpp)[ i + 1 ] = NULL;
132 aa_add_oc( ObjectClass *oc, ObjectClass ***ocpp, AttributeType ***atpp )
137 for ( ; (*ocpp)[ i ] != NULL; i++ ) {
138 if ( (*ocpp)[ i ] == oc ) {
143 if ( (*ocpp)[ i ] != NULL ) {
148 *ocpp = ch_realloc( *ocpp, sizeof( ObjectClass * ) * ( i + 2 ) );
150 (*ocpp)[ i + 1 ] = NULL;
152 if ( oc->soc_required ) {
155 for ( i = 0; oc->soc_required[ i ] != NULL; i++ ) {
156 aa_add_at( oc->soc_required[ i ], atpp );
160 if ( oc->soc_allowed ) {
163 for ( i = 0; oc->soc_allowed[ i ] != NULL; i++ ) {
164 aa_add_at( oc->soc_allowed[ i ], atpp );
172 aa_operational( Operation *op, SlapReply *rs )
175 AccessControlState acl_state = ACL_STATE_INIT;
177 AttributeType **atp = NULL;
178 ObjectClass **ocp = NULL;
180 #define GOT_NONE (0x0U)
182 #define GOT_CE (0x2U)
184 #define GOT_AE (0x8U)
185 #define GOT_ALL (GOT_C|GOT_CE|GOT_A|GOT_AE)
188 /* only add if requested */
189 if ( SLAP_OPATTRS( rs->sr_attr_flags ) ) {
193 if ( ad_inlist( ad_allowedChildClasses, rs->sr_attrs ) ) {
197 if ( ad_inlist( ad_allowedChildClassesEffective, rs->sr_attrs ) ) {
201 if ( ad_inlist( ad_allowedAttributes, rs->sr_attrs ) ) {
205 if ( ad_inlist( ad_allowedAttributesEffective, rs->sr_attrs ) ) {
210 if ( got == GOT_NONE ) {
211 return SLAP_CB_CONTINUE;
214 /* shouldn't be called without an entry; please check */
215 assert( rs->sr_entry != NULL );
217 for ( ap = &rs->sr_operational_attrs; *ap != NULL; ap = &(*ap)->a_next )
220 /* see caveats; this is not guaranteed for all backends */
221 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_objectClass );
226 /* if client has no access to objectClass attribute; don't compute */
227 if ( !access_allowed( op, rs->sr_entry, slap_schema.si_ad_objectClass,
228 NULL, ACL_READ, &acl_state ) )
230 return SLAP_CB_CONTINUE;
233 for ( v = a->a_nvals; !BER_BVISNULL( v ); v++ ) {
234 ObjectClass *oc = oc_bvfind( v );
236 assert( oc != NULL );
238 /* if client has no access to specific value, don't compute */
239 if ( !access_allowed( op, rs->sr_entry,
240 slap_schema.si_ad_objectClass,
241 &oc->soc_cname, ACL_READ, &acl_state ) )
246 aa_add_oc( oc, &ocp, &atp );
248 if ( oc->soc_sups ) {
251 for ( i = 0; oc->soc_sups[ i ] != NULL; i++ ) {
252 aa_add_oc( oc->soc_sups[ i ], &ocp, &atp );
260 BerVarray bv_allowed = NULL,
262 int i, ja = 0, je = 0;
264 for ( i = 0; atp[ i ] != NULL; i++ )
268 bv_allowed = ber_memalloc( sizeof( struct berval ) * ( i + 1 ) );
270 if ( got & GOT_AE ) {
271 bv_effective = ber_memalloc( sizeof( struct berval ) * ( i + 1 ) );
274 for ( i = 0, ja = 0, je = 0; atp[ i ] != NULL; i++ ) {
276 ber_dupbv( &bv_allowed[ ja ], &atp[ i ]->sat_cname );
280 if ( got & GOT_AE ) {
281 AttributeDescription *ad = NULL;
282 const char *text = NULL;
284 if ( slap_bv2ad( &atp[ i ]->sat_cname, &ad, &text ) ) {
289 if ( access_allowed( op, rs->sr_entry,
290 ad, NULL, ACL_WRITE, NULL ) )
292 ber_dupbv( &bv_effective[ je ], &atp[ i ]->sat_cname );
300 if ( ( got & GOT_A ) && ja > 0 ) {
301 BER_BVZERO( &bv_allowed[ ja ] );
302 *ap = attr_alloc( ad_allowedAttributes );
303 (*ap)->a_vals = bv_allowed;
304 (*ap)->a_nvals = bv_allowed;
305 (*ap)->a_numvals = ja;
309 if ( ( got & GOT_AE ) && je > 0 ) {
310 BER_BVZERO( &bv_effective[ je ] );
311 *ap = attr_alloc( ad_allowedAttributesEffective );
312 (*ap)->a_vals = bv_effective;
313 (*ap)->a_nvals = bv_effective;
314 (*ap)->a_numvals = je;
322 if ( ( got & GOT_C ) || ( got & GOT_CE ) ) {
323 BerVarray bv_allowed = NULL,
325 int i, ja = 0, je = 0;
329 for ( oc_start( &oc ); oc != NULL; oc_next( &oc ) ) {
330 /* we can only add AUXILIARY objectClasses */
331 if ( oc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
339 bv_allowed = ber_memalloc( sizeof( struct berval ) * ( i + 1 ) );
341 if ( got & GOT_CE ) {
342 bv_effective = ber_memalloc( sizeof( struct berval ) * ( i + 1 ) );
345 for ( oc_start( &oc ); oc != NULL; oc_next( &oc ) ) {
346 /* we can only add AUXILIARY objectClasses */
347 if ( oc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
352 ber_dupbv( &bv_allowed[ ja ], &oc->soc_cname );
356 if ( got & GOT_CE ) {
357 if ( !access_allowed( op, rs->sr_entry,
358 slap_schema.si_ad_objectClass,
359 &oc->soc_cname, ACL_WRITE, NULL ) )
364 if ( oc->soc_required ) {
365 for ( i = 0; oc->soc_required[ i ] != NULL; i++ ) {
366 AttributeDescription *ad = NULL;
367 const char *text = NULL;
369 if ( slap_bv2ad( &oc->soc_required[ i ]->sat_cname, &ad, &text ) ) {
374 if ( !access_allowed( op, rs->sr_entry,
375 ad, NULL, ACL_WRITE, NULL ) )
382 ber_dupbv( &bv_effective[ je ], &oc->soc_cname );
388 if ( ( got & GOT_C ) && ja > 0 ) {
389 BER_BVZERO( &bv_allowed[ ja ] );
390 *ap = attr_alloc( ad_allowedChildClasses );
391 (*ap)->a_vals = bv_allowed;
392 (*ap)->a_nvals = bv_allowed;
393 (*ap)->a_numvals = ja;
397 if ( ( got & GOT_CE ) && je > 0 ) {
398 BER_BVZERO( &bv_effective[ je ] );
399 *ap = attr_alloc( ad_allowedChildClassesEffective );
400 (*ap)->a_vals = bv_effective;
401 (*ap)->a_nvals = bv_effective;
402 (*ap)->a_numvals = je;
409 return SLAP_CB_CONTINUE;
412 static slap_overinst aa;
414 #if LDAP_VENDOR_VERSION_MINOR != X && LDAP_VENDOR_VERSION_MINOR <= 3
415 /* backport register_at() from HEAD, to allow building with OL <= 2.3 */
417 register_at( char *def, AttributeDescription **rad, int dupok )
419 LDAPAttributeType *at;
420 int code, freeit = 0;
422 AttributeDescription *ad = NULL;
424 at = ldap_str2attributetype( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
426 Debug( LDAP_DEBUG_ANY,
427 "register_at: AttributeType \"%s\": %s, %s\n",
428 def, ldap_scherr2str(code), err );
432 code = at_add( at, 0, NULL, &err );
434 if ( code == SLAP_SCHERR_ATTR_DUP && dupok ) {
438 ldap_attributetype_free( at );
439 Debug( LDAP_DEBUG_ANY,
440 "register_at: AttributeType \"%s\": %s, %s\n",
441 def, scherr2str(code), err );
445 code = slap_str2ad( at->at_names[0], &ad, &err );
446 if ( freeit || code ) {
447 ldap_attributetype_free( at );
452 Debug( LDAP_DEBUG_ANY, "register_at: AttributeType \"%s\": %s\n",
455 if ( rad ) *rad = ad;
460 #if SLAPD_OVER_ALLOWED == SLAPD_MOD_DYNAMIC
462 #endif /* SLAPD_OVER_ALLOWED == SLAPD_MOD_DYNAMIC */
464 aa_initialize( void )
468 aa.on_bi.bi_type = "allowed";
470 aa.on_bi.bi_operational = aa_operational;
472 /* aa schema integration */
473 for ( i = 0; aa_attrs[i].at; i++ ) {
476 code = register_at( aa_attrs[i].at, aa_attrs[i].ad, 0 );
478 Debug( LDAP_DEBUG_ANY,
479 "aa_initialize: register_at failed\n", 0, 0, 0 );
484 return overlay_register( &aa );
487 #if SLAPD_OVER_ALLOWED == SLAPD_MOD_DYNAMIC
489 init_module( int argc, char *argv[] )
491 return aa_initialize();
493 #endif /* SLAPD_OVER_ALLOWED == SLAPD_MOD_DYNAMIC */
495 #endif /* SLAPD_OVER_ALLOWED */