1 /* allowed.c - add allowed attributes based on ACL */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2006-2009 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;
179 BerVarray bv_allowed = NULL,
183 #define GOT_NONE (0x0U)
185 #define GOT_CE (0x2U)
187 #define GOT_AE (0x8U)
188 #define GOT_ALL (GOT_C|GOT_CE|GOT_A|GOT_AE)
191 /* only add if requested */
192 if ( SLAP_OPATTRS( rs->sr_attr_flags ) ) {
196 if ( ad_inlist( ad_allowedChildClasses, rs->sr_attrs ) ) {
200 if ( ad_inlist( ad_allowedChildClassesEffective, rs->sr_attrs ) ) {
204 if ( ad_inlist( ad_allowedAttributes, rs->sr_attrs ) ) {
208 if ( ad_inlist( ad_allowedAttributesEffective, rs->sr_attrs ) ) {
213 if ( got == GOT_NONE ) {
214 return SLAP_CB_CONTINUE;
217 /* shouldn't be called without an entry; please check */
218 assert( rs->sr_entry != NULL );
220 /* see caveats; this is not guaranteed for all backends */
221 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_objectClass );
223 return SLAP_CB_CONTINUE;
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 ) {
249 for ( i = 0; oc->soc_sups[ i ] != NULL; i++ ) {
250 aa_add_oc( oc->soc_sups[ i ], &ocp, &atp );
256 for ( i = 0; atp[ i ] != NULL; i++ )
260 bv_allowed = ber_memalloc( sizeof( struct berval ) * ( i + 1 ) );
262 if ( got & GOT_AE ) {
263 bv_effective = ber_memalloc( sizeof( struct berval ) * ( i + 1 ) );
266 for ( i = 0, ja = 0, je = 0; atp[ i ] != NULL; i++ ) {
268 ber_dupbv( &bv_allowed[ ja ], &atp[ i ]->sat_cname );
272 if ( got & GOT_AE ) {
273 AttributeDescription *ad = NULL;
274 const char *text = NULL;
276 if ( slap_bv2ad( &atp[ i ]->sat_cname, &ad, &text ) ) {
281 if ( access_allowed( op, rs->sr_entry,
282 ad, NULL, ACL_WRITE, NULL ) )
284 ber_dupbv( &bv_effective[ je ], &atp[ i ]->sat_cname );
290 for ( ap = &rs->sr_operational_attrs; *ap != NULL; ap = &(*ap)->a_next )
294 BER_BVZERO( &bv_allowed[ ja ] );
295 *ap = attr_alloc( ad_allowedAttributes );
296 (*ap)->a_vals = bv_allowed;
297 (*ap)->a_nvals = bv_allowed;
301 if ( got & GOT_AE ) {
302 BER_BVZERO( &bv_effective[ je ] );
303 *ap = attr_alloc( ad_allowedAttributesEffective );
304 (*ap)->a_vals = bv_effective;
305 (*ap)->a_nvals = bv_effective;
315 return SLAP_CB_CONTINUE;
318 static slap_overinst aa;
320 #if LDAP_VENDOR_VERSION_MINOR != X && LDAP_VENDOR_VERSION_MINOR <= 3
321 /* backport register_at() from HEAD, to allow building with OL <= 2.3 */
323 register_at( char *def, AttributeDescription **rad, int dupok )
325 LDAPAttributeType *at;
326 int code, freeit = 0;
328 AttributeDescription *ad = NULL;
330 at = ldap_str2attributetype( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
332 Debug( LDAP_DEBUG_ANY,
333 "register_at: AttributeType \"%s\": %s, %s\n",
334 def, ldap_scherr2str(code), err );
338 code = at_add( at, 0, NULL, &err );
340 if ( code == SLAP_SCHERR_ATTR_DUP && dupok ) {
344 ldap_attributetype_free( at );
345 Debug( LDAP_DEBUG_ANY,
346 "register_at: AttributeType \"%s\": %s, %s\n",
347 def, scherr2str(code), err );
351 code = slap_str2ad( at->at_names[0], &ad, &err );
352 if ( freeit || code ) {
353 ldap_attributetype_free( at );
358 Debug( LDAP_DEBUG_ANY, "register_at: AttributeType \"%s\": %s\n",
361 if ( rad ) *rad = ad;
366 #if SLAPD_OVER_ALLOWED == SLAPD_MOD_DYNAMIC
368 #endif /* SLAPD_OVER_ALLOWED == SLAPD_MOD_DYNAMIC */
370 aa_initialize( void )
374 aa.on_bi.bi_type = "aa";
376 aa.on_bi.bi_operational = aa_operational;
378 /* aa schema integration */
379 for ( i = 0; aa_attrs[i].at; i++ ) {
382 code = register_at( aa_attrs[i].at, aa_attrs[i].ad, 0 );
384 Debug( LDAP_DEBUG_ANY,
385 "aa_initialize: register_at failed\n", 0, 0, 0 );
390 return overlay_register( &aa );
393 #if SLAPD_OVER_ALLOWED == SLAPD_MOD_DYNAMIC
395 init_module( int argc, char *argv[] )
397 return aa_initialize();
399 #endif /* SLAPD_OVER_ALLOWED == SLAPD_MOD_DYNAMIC */
401 #endif /* SLAPD_OVER_ALLOWED */