1 /* allowed.c - add allowed attributes based on ACL */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2006-2013 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
50 * NOTE: part of the schema definition reported below is taken
51 * from Microsoft schema definitions (OID, NAME, SYNTAX);
53 * EQUALITY is taken from
54 * <http://www.redhat.com/archives/fedora-directory-devel/2006-August/msg00007.html>
55 * (posted by Andrew Bartlett)
57 * The rest is guessed. Specifically
59 * DESC briefly describes the purpose
61 * NO-USER-MODIFICATION is added to make attributes operational
63 * USAGE is set to "dSAOperation" as per ITS#7493,
64 * to prevent replication, since this information
65 * is generated (based on ACL and identity of request)
69 #define AA_SCHEMA_AT "1.2.840.113556.1.4"
71 static AttributeDescription
72 *ad_allowedChildClasses,
73 *ad_allowedChildClassesEffective,
74 *ad_allowedAttributes,
75 *ad_allowedAttributesEffective;
79 AttributeDescription **ad;
81 { "( " AA_SCHEMA_AT ".911 "
82 "NAME 'allowedChildClasses' "
83 "EQUALITY objectIdentifierMatch "
84 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
86 "DESC 'Child classes allowed for a given object' "
87 "NO-USER-MODIFICATION "
88 "USAGE dSAOperation )", &ad_allowedChildClasses },
89 { "( " AA_SCHEMA_AT ".912 "
90 "NAME 'allowedChildClassesEffective' "
91 "EQUALITY objectIdentifierMatch "
92 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
94 "DESC 'Child classes allowed for a given object according to ACLs' "
95 "NO-USER-MODIFICATION "
96 "USAGE dSAOperation )", &ad_allowedChildClassesEffective },
97 { "( " AA_SCHEMA_AT ".913 "
98 "NAME 'allowedAttributes' "
99 "EQUALITY objectIdentifierMatch "
100 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
102 "DESC 'Attributes allowed for a given object' "
103 "NO-USER-MODIFICATION "
104 "USAGE dSAOperation )", &ad_allowedAttributes },
105 { "( " AA_SCHEMA_AT ".914 "
106 "NAME 'allowedAttributesEffective' "
107 "EQUALITY objectIdentifierMatch "
108 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
110 "DESC 'Attributes allowed for a given object according to ACLs' "
111 "NO-USER-MODIFICATION "
112 "USAGE dSAOperation )", &ad_allowedAttributesEffective },
114 /* TODO: add objectClass stuff? */
120 aa_add_at( AttributeType *at, AttributeType ***atpp )
125 for ( i = 0; (*atpp)[ i ] != NULL; i++ ) {
126 if ( (*atpp)[ i ] == at ) {
131 if ( (*atpp)[ i ] != NULL ) {
136 *atpp = ch_realloc( *atpp, sizeof( AttributeType * ) * ( i + 2 ) );
138 (*atpp)[ i + 1 ] = NULL;
144 aa_add_oc( ObjectClass *oc, ObjectClass ***ocpp, AttributeType ***atpp )
149 for ( ; (*ocpp)[ i ] != NULL; i++ ) {
150 if ( (*ocpp)[ i ] == oc ) {
155 if ( (*ocpp)[ i ] != NULL ) {
160 *ocpp = ch_realloc( *ocpp, sizeof( ObjectClass * ) * ( i + 2 ) );
162 (*ocpp)[ i + 1 ] = NULL;
164 if ( oc->soc_required ) {
167 for ( i = 0; oc->soc_required[ i ] != NULL; i++ ) {
168 aa_add_at( oc->soc_required[ i ], atpp );
172 if ( oc->soc_allowed ) {
175 for ( i = 0; oc->soc_allowed[ i ] != NULL; i++ ) {
176 aa_add_at( oc->soc_allowed[ i ], atpp );
184 aa_operational( Operation *op, SlapReply *rs )
187 AccessControlState acl_state = ACL_STATE_INIT;
189 AttributeType **atp = NULL;
190 ObjectClass **ocp = NULL;
192 #define GOT_NONE (0x0U)
194 #define GOT_CE (0x2U)
196 #define GOT_AE (0x8U)
197 #define GOT_ALL (GOT_C|GOT_CE|GOT_A|GOT_AE)
200 /* only add if requested */
201 if ( SLAP_OPATTRS( rs->sr_attr_flags ) ) {
205 if ( ad_inlist( ad_allowedChildClasses, rs->sr_attrs ) ) {
209 if ( ad_inlist( ad_allowedChildClassesEffective, rs->sr_attrs ) ) {
213 if ( ad_inlist( ad_allowedAttributes, rs->sr_attrs ) ) {
217 if ( ad_inlist( ad_allowedAttributesEffective, rs->sr_attrs ) ) {
222 if ( got == GOT_NONE ) {
223 return SLAP_CB_CONTINUE;
226 /* shouldn't be called without an entry; please check */
227 assert( rs->sr_entry != NULL );
229 for ( ap = &rs->sr_operational_attrs; *ap != NULL; ap = &(*ap)->a_next )
232 /* see caveats; this is not guaranteed for all backends */
233 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_objectClass );
238 /* if client has no access to objectClass attribute; don't compute */
239 if ( !access_allowed( op, rs->sr_entry, slap_schema.si_ad_objectClass,
240 NULL, ACL_READ, &acl_state ) )
242 return SLAP_CB_CONTINUE;
245 for ( v = a->a_nvals; !BER_BVISNULL( v ); v++ ) {
246 ObjectClass *oc = oc_bvfind( v );
248 assert( oc != NULL );
250 /* if client has no access to specific value, don't compute */
251 if ( !access_allowed( op, rs->sr_entry,
252 slap_schema.si_ad_objectClass,
253 &oc->soc_cname, ACL_READ, &acl_state ) )
258 aa_add_oc( oc, &ocp, &atp );
260 if ( oc->soc_sups ) {
263 for ( i = 0; oc->soc_sups[ i ] != NULL; i++ ) {
264 aa_add_oc( oc->soc_sups[ i ], &ocp, &atp );
272 BerVarray bv_allowed = NULL,
274 int i, ja = 0, je = 0;
276 for ( i = 0; atp[ i ] != NULL; i++ )
280 bv_allowed = ber_memalloc( sizeof( struct berval ) * ( i + 1 ) );
282 if ( got & GOT_AE ) {
283 bv_effective = ber_memalloc( sizeof( struct berval ) * ( i + 1 ) );
286 for ( i = 0, ja = 0, je = 0; atp[ i ] != NULL; i++ ) {
288 ber_dupbv( &bv_allowed[ ja ], &atp[ i ]->sat_cname );
292 if ( got & GOT_AE ) {
293 AttributeDescription *ad = NULL;
294 const char *text = NULL;
296 if ( slap_bv2ad( &atp[ i ]->sat_cname, &ad, &text ) ) {
301 if ( access_allowed( op, rs->sr_entry,
302 ad, NULL, ACL_WRITE, NULL ) )
304 ber_dupbv( &bv_effective[ je ], &atp[ i ]->sat_cname );
312 if ( ( got & GOT_A ) && ja > 0 ) {
313 BER_BVZERO( &bv_allowed[ ja ] );
314 *ap = attr_alloc( ad_allowedAttributes );
315 (*ap)->a_vals = bv_allowed;
316 (*ap)->a_nvals = bv_allowed;
317 (*ap)->a_numvals = ja;
321 if ( ( got & GOT_AE ) && je > 0 ) {
322 BER_BVZERO( &bv_effective[ je ] );
323 *ap = attr_alloc( ad_allowedAttributesEffective );
324 (*ap)->a_vals = bv_effective;
325 (*ap)->a_nvals = bv_effective;
326 (*ap)->a_numvals = je;
334 if ( ( got & GOT_C ) || ( got & GOT_CE ) ) {
335 BerVarray bv_allowed = NULL,
337 int i, ja = 0, je = 0;
341 for ( oc_start( &oc ); oc != NULL; oc_next( &oc ) ) {
342 /* we can only add AUXILIARY objectClasses */
343 if ( oc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
351 bv_allowed = ber_memalloc( sizeof( struct berval ) * ( i + 1 ) );
353 if ( got & GOT_CE ) {
354 bv_effective = ber_memalloc( sizeof( struct berval ) * ( i + 1 ) );
357 for ( oc_start( &oc ); oc != NULL; oc_next( &oc ) ) {
358 /* we can only add AUXILIARY objectClasses */
359 if ( oc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
364 ber_dupbv( &bv_allowed[ ja ], &oc->soc_cname );
368 if ( got & GOT_CE ) {
369 if ( !access_allowed( op, rs->sr_entry,
370 slap_schema.si_ad_objectClass,
371 &oc->soc_cname, ACL_WRITE, NULL ) )
376 if ( oc->soc_required ) {
377 for ( i = 0; oc->soc_required[ i ] != NULL; i++ ) {
378 AttributeDescription *ad = NULL;
379 const char *text = NULL;
381 if ( slap_bv2ad( &oc->soc_required[ i ]->sat_cname, &ad, &text ) ) {
386 if ( !access_allowed( op, rs->sr_entry,
387 ad, NULL, ACL_WRITE, NULL ) )
394 ber_dupbv( &bv_effective[ je ], &oc->soc_cname );
400 if ( ( got & GOT_C ) && ja > 0 ) {
401 BER_BVZERO( &bv_allowed[ ja ] );
402 *ap = attr_alloc( ad_allowedChildClasses );
403 (*ap)->a_vals = bv_allowed;
404 (*ap)->a_nvals = bv_allowed;
405 (*ap)->a_numvals = ja;
409 if ( ( got & GOT_CE ) && je > 0 ) {
410 BER_BVZERO( &bv_effective[ je ] );
411 *ap = attr_alloc( ad_allowedChildClassesEffective );
412 (*ap)->a_vals = bv_effective;
413 (*ap)->a_nvals = bv_effective;
414 (*ap)->a_numvals = je;
421 return SLAP_CB_CONTINUE;
424 static slap_overinst aa;
426 #if LDAP_VENDOR_VERSION_MINOR != X && LDAP_VENDOR_VERSION_MINOR <= 3
427 /* backport register_at() from HEAD, to allow building with OL <= 2.3 */
429 register_at( char *def, AttributeDescription **rad, int dupok )
431 LDAPAttributeType *at;
432 int code, freeit = 0;
434 AttributeDescription *ad = NULL;
436 at = ldap_str2attributetype( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
438 Debug( LDAP_DEBUG_ANY,
439 "register_at: AttributeType \"%s\": %s, %s\n",
440 def, ldap_scherr2str(code), err );
444 code = at_add( at, 0, NULL, &err );
446 if ( code == SLAP_SCHERR_ATTR_DUP && dupok ) {
450 ldap_attributetype_free( at );
451 Debug( LDAP_DEBUG_ANY,
452 "register_at: AttributeType \"%s\": %s, %s\n",
453 def, scherr2str(code), err );
457 code = slap_str2ad( at->at_names[0], &ad, &err );
458 if ( freeit || code ) {
459 ldap_attributetype_free( at );
464 Debug( LDAP_DEBUG_ANY, "register_at: AttributeType \"%s\": %s\n",
467 if ( rad ) *rad = ad;
472 #if SLAPD_OVER_ALLOWED == SLAPD_MOD_DYNAMIC
474 #endif /* SLAPD_OVER_ALLOWED == SLAPD_MOD_DYNAMIC */
476 aa_initialize( void )
480 aa.on_bi.bi_type = "allowed";
482 aa.on_bi.bi_operational = aa_operational;
484 /* aa schema integration */
485 for ( i = 0; aa_attrs[i].at; i++ ) {
488 code = register_at( aa_attrs[i].at, aa_attrs[i].ad, 0 );
490 Debug( LDAP_DEBUG_ANY,
491 "aa_initialize: register_at failed\n", 0, 0, 0 );
496 return overlay_register( &aa );
499 #if SLAPD_OVER_ALLOWED == SLAPD_MOD_DYNAMIC
501 init_module( int argc, char *argv[] )
503 return aa_initialize();
505 #endif /* SLAPD_OVER_ALLOWED == SLAPD_MOD_DYNAMIC */
507 #endif /* SLAPD_OVER_ALLOWED */