1 /* deref.c - dereference overlay */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2016 The OpenLDAP Foundation.
6 * Portions Copyright 2008 Pierangelo Masarati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Pierangelo Masarati
19 * for inclusion in OpenLDAP Software.
24 #ifdef SLAPD_OVER_DEREF
28 #include "ac/string.h"
29 #include "ac/socket.h"
41 * controlValue ::= SEQUENCE OF derefSpec DerefSpec
43 * DerefSpec ::= SEQUENCE {
44 * derefAttr attributeDescription, ; DN-valued
45 * attributes AttributeList }
47 * AttributeList ::= SEQUENCE OF attr AttributeDescription
49 * derefAttr MUST be unique within controlValue
54 * controlValue ::= SEQUENCE OF DerefRes
57 * PartialAttribute ::= SEQUENCE {
58 * type AttributeDescription,
59 * vals SET OF value AttributeValue }
61 * PartialAttributeList ::= SEQUENCE OF
62 * partialAttribute PartialAttribute
64 * DerefRes ::= SEQUENCE {
65 * derefAttr AttributeDescription,
67 * attrVals [0] PartialAttributeList OPTIONAL }
69 * If vals is empty, partialAttribute is omitted.
70 * If all vals in attrVals are empty, attrVals is omitted.
78 * { { member, { GUID, SID } }, { memberOf, { GUID, SID } } }
82 * { { memberOf, "cn=abartlet,cn=users,dc=abartlet,dc=net",
83 * { { GUID, [ "0bc11d00-e431-40a0-8767-344a320142fa" ] },
84 * { SID, [ "S-1-2-3-2345" ] } } },
85 * { memberOf, "cn=ando,cn=users,dc=sys-net,dc=it",
86 * { { GUID, [ "0bc11d00-e431-40a0-8767-344a320142fb" ] },
87 * { SID, [ "S-1-2-3-2346" ] } } } }
93 * { { member, { cn, uid, drink } } }
97 * { { member, "cn=ando,cn=users,dc=sys-net,dc=it",
98 * { { cn, [ "ando", "Pierangelo Masarati" ] },
99 * { uid, [ "ando" ] } } },
100 * { member, "dc=sys-net,dc=it" } }
103 * 3. Security considerations
105 * The control result must not disclose information the client's
106 * identity could not have accessed directly by performing the related
107 * search operations. The presence of a derefVal in the control
108 * response does not imply neither the existence of nor any access
109 * privilege to the corresponding entry. It is merely a consequence
110 * of the read access the client's identity has on the corresponding
114 #define o_deref o_ctrlflag[deref_cid]
115 #define o_ctrlderef o_controls[deref_cid]
117 typedef struct DerefSpec {
118 AttributeDescription *ds_derefAttr;
119 AttributeDescription **ds_attributes;
121 struct DerefSpec *ds_next;
124 typedef struct DerefVal {
125 struct berval dv_derefSpecVal;
126 BerVarray *dv_attrVals;
129 typedef struct DerefRes {
132 struct DerefRes *dr_next;
135 typedef struct deref_cb_t {
136 slap_overinst *dc_on;
140 static int deref_cid;
141 static slap_overinst deref;
151 BerElementBuffer berbuf;
152 BerElement *ber = (BerElement *)&berbuf;
155 DerefSpec *dshead = NULL, **dsp = &dshead;
156 BerVarray attributes = NULL;
158 if ( op->o_deref != SLAP_CONTROL_NONE ) {
159 rs->sr_text = "Dereference control specified multiple times";
160 return LDAP_PROTOCOL_ERROR;
163 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
164 rs->sr_text = "Dereference control value is absent";
165 return LDAP_PROTOCOL_ERROR;
168 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
169 rs->sr_text = "Dereference control value is empty";
170 return LDAP_PROTOCOL_ERROR;
173 ber_init2( ber, &ctrl->ldctl_value, 0 );
175 for ( tag = ber_first_element( ber, &len, &last );
177 tag = ber_next_element( ber, &len, last ) )
179 struct berval derefAttr;
180 DerefSpec *ds, *dstmp;
183 ber_len_t cnt = sizeof(struct berval);
186 if ( ber_scanf( ber, "{m{M}}", &derefAttr, &attributes, &cnt, off ) == LBER_ERROR
189 rs->sr_text = "Dereference control: derefSpec decoding error";
190 rs->sr_err = LDAP_PROTOCOL_ERROR;
194 ds = (DerefSpec *)op->o_tmpcalloc( 1,
195 sizeof(DerefSpec) + sizeof(AttributeDescription *)*(cnt + 1),
197 ds->ds_attributes = (AttributeDescription **)&ds[ 1 ];
200 rc = slap_bv2ad( &derefAttr, &ds->ds_derefAttr, &text );
201 if ( rc != LDAP_SUCCESS ) {
202 rs->sr_text = "Dereference control: derefAttr decoding error";
203 rs->sr_err = LDAP_PROTOCOL_ERROR;
207 for ( dstmp = dshead; dstmp && dstmp != ds; dstmp = dstmp->ds_next ) {
208 if ( dstmp->ds_derefAttr == ds->ds_derefAttr ) {
209 rs->sr_text = "Dereference control: derefAttr must be unique within control";
210 rs->sr_err = LDAP_PROTOCOL_ERROR;
215 if ( !( ds->ds_derefAttr->ad_type->sat_syntax->ssyn_flags & SLAP_SYNTAX_DN )) {
216 if ( ctrl->ldctl_iscritical ) {
217 rs->sr_text = "Dereference control: derefAttr syntax not distinguishedName";
218 rs->sr_err = LDAP_PROTOCOL_ERROR;
222 rs->sr_err = LDAP_SUCCESS;
226 for ( cnt = 0; !BER_BVISNULL( &attributes[ cnt ] ); cnt++ ) {
227 rc = slap_bv2ad( &attributes[ cnt ], &ds->ds_attributes[ cnt ], &text );
228 if ( rc != LDAP_SUCCESS ) {
229 rs->sr_text = "Dereference control: attribute decoding error";
230 rs->sr_err = LDAP_PROTOCOL_ERROR;
235 ber_memfree_x( attributes, op->o_tmpmemctx );
242 op->o_ctrlderef = (void *)dshead;
244 op->o_deref = ctrl->ldctl_iscritical
245 ? SLAP_CONTROL_CRITICAL
246 : SLAP_CONTROL_NONCRITICAL;
248 rs->sr_err = LDAP_SUCCESS;
251 if ( rs->sr_err != LDAP_SUCCESS ) {
254 DerefSpec *dsnext = dshead->ds_next;
255 op->o_tmpfree( dshead, op->o_tmpmemctx );
260 if ( attributes != NULL ) {
261 ber_memfree_x( attributes, op->o_tmpmemctx );
268 deref_cleanup( Operation *op, SlapReply *rs )
270 if ( rs->sr_type == REP_RESULT || rs->sr_err == SLAPD_ABANDON ) {
271 op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
272 op->o_callback = NULL;
274 op->o_tmpfree( op->o_ctrlderef, op->o_tmpmemctx );
275 op->o_ctrlderef = NULL;
278 return SLAP_CB_CONTINUE;
282 deref_response( Operation *op, SlapReply *rs )
284 int rc = SLAP_CB_CONTINUE;
286 if ( rs->sr_type == REP_SEARCH ) {
287 BerElementBuffer berbuf;
288 BerElement *ber = (BerElement *) &berbuf;
289 deref_cb_t *dc = (deref_cb_t *)op->o_callback->sc_private;
291 DerefRes *dr, *drhead = NULL, **drp = &drhead;
292 struct berval bv = BER_BVNULL;
293 int nDerefRes = 0, nDerefVals = 0, nAttrs = 0, nVals = 0;
294 struct berval ctrlval;
295 LDAPControl *ctrl, *ctrlsp[2];
296 AccessControlState acl_state = ACL_STATE_INIT;
297 static char dummy = '\0';
301 rc = overlay_entry_get_ov( op, &rs->sr_entry->e_nname, NULL, NULL, 0, &ebase, dc->dc_on );
302 if ( rc != LDAP_SUCCESS || ebase == NULL ) {
303 return SLAP_CB_CONTINUE;
306 for ( ds = dc->dc_ds; ds; ds = ds->ds_next ) {
307 Attribute *a = attr_find( ebase->e_attrs, ds->ds_derefAttr );
313 if ( !access_allowed( op, rs->sr_entry, a->a_desc,
314 NULL, ACL_READ, &acl_state ) )
319 dr = op->o_tmpcalloc( 1,
320 sizeof( DerefRes ) + ( sizeof( DerefVal ) + sizeof( BerVarray * ) * ds->ds_nattrs ) * ( a->a_numvals + 1 ),
323 dv = dr->dr_vals = (DerefVal *)&dr[ 1 ];
324 bva = (BerVarray *)&dv[ a->a_numvals + 1 ];
326 bv.bv_len += ds->ds_derefAttr->ad_cname.bv_len;
330 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
333 dv[ i ].dv_attrVals = bva;
334 bva += ds->ds_nattrs;
337 if ( !access_allowed( op, rs->sr_entry, a->a_desc,
338 &a->a_nvals[ i ], ACL_READ, &acl_state ) )
340 dv[ i ].dv_derefSpecVal.bv_val = &dummy;
344 ber_dupbv_x( &dv[ i ].dv_derefSpecVal, &a->a_vals[ i ], op->o_tmpmemctx );
345 bv.bv_len += dv[ i ].dv_derefSpecVal.bv_len;
349 rc = overlay_entry_get_ov( op, &a->a_nvals[ i ], NULL, NULL, 0, &e, dc->dc_on );
350 if ( rc == LDAP_SUCCESS && e != NULL ) {
353 if ( access_allowed( op, e, slap_schema.si_ad_entry,
354 NULL, ACL_READ, NULL ) )
356 for ( j = 0; j < ds->ds_nattrs; j++ ) {
359 if ( !access_allowed( op, e, ds->ds_attributes[ j ], NULL,
360 ACL_READ, &acl_state ) )
365 aa = attr_find( e->e_attrs, ds->ds_attributes[ j ] );
367 unsigned k, h, last = aa->a_numvals;
369 ber_bvarray_dup_x( &dv[ i ].dv_attrVals[ j ],
370 aa->a_vals, op->o_tmpmemctx );
372 bv.bv_len += ds->ds_attributes[ j ]->ad_cname.bv_len;
374 for ( k = 0, h = 0; k < aa->a_numvals; k++ ) {
375 if ( !access_allowed( op, e,
378 ACL_READ, &acl_state ) )
380 op->o_tmpfree( dv[ i ].dv_attrVals[ j ][ h ].bv_val,
382 dv[ i ].dv_attrVals[ j ][ h ] = dv[ i ].dv_attrVals[ j ][ --last ];
383 BER_BVZERO( &dv[ i ].dv_attrVals[ j ][ last ] );
386 bv.bv_len += dv[ i ].dv_attrVals[ j ][ h ].bv_len;
395 overlay_entry_release_ov( op, e, 0, dc->dc_on );
403 overlay_entry_release_ov( op, ebase, 0, dc->dc_on );
405 if ( drhead == NULL ) {
406 return SLAP_CB_CONTINUE;
409 /* cook the control value */
410 bv.bv_len += nVals * sizeof(struct berval)
411 + nAttrs * sizeof(struct berval)
412 + nDerefVals * sizeof(DerefVal)
413 + nDerefRes * sizeof(DerefRes);
414 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
416 ber_init2( ber, &bv, LBER_USE_DER );
417 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
419 rc = ber_printf( ber, "{" /*}*/ );
420 for ( dr = drhead; dr != NULL; dr = dr->dr_next ) {
421 for ( i = 0; !BER_BVISNULL( &dr->dr_vals[ i ].dv_derefSpecVal ); i++ ) {
424 if ( dr->dr_vals[ i ].dv_derefSpecVal.bv_val == &dummy ) {
428 rc = ber_printf( ber, "{OO" /*}*/,
429 &dr->dr_spec.ds_derefAttr->ad_cname,
430 &dr->dr_vals[ i ].dv_derefSpecVal );
431 op->o_tmpfree( dr->dr_vals[ i ].dv_derefSpecVal.bv_val, op->o_tmpmemctx );
432 for ( j = 0; j < dr->dr_spec.ds_nattrs; j++ ) {
433 if ( dr->dr_vals[ i ].dv_attrVals[ j ] != NULL ) {
435 rc = ber_printf( ber, "t{" /*}*/,
436 (LBER_CONSTRUCTED|LBER_CLASS_CONTEXT) );
439 rc = ber_printf( ber, "{O[W]}",
440 &dr->dr_spec.ds_attributes[ j ]->ad_cname,
441 dr->dr_vals[ i ].dv_attrVals[ j ] );
442 op->o_tmpfree( dr->dr_vals[ i ].dv_attrVals[ j ],
447 rc = ber_printf( ber, /*{{*/ "}N}" );
449 rc = ber_printf( ber, /*{*/ "}" );
453 rc = ber_printf( ber, /*{*/ "}" );
454 if ( ber_flatten2( ber, &ctrlval, 0 ) == -1 ) {
455 if ( op->o_deref == SLAP_CONTROL_CRITICAL ) {
456 rc = LDAP_CONSTRAINT_VIOLATION;
459 rc = SLAP_CB_CONTINUE;
464 ctrl = op->o_tmpcalloc( 1,
465 sizeof( LDAPControl ) + ctrlval.bv_len + 1,
467 ctrl->ldctl_value.bv_val = (char *)&ctrl[ 1 ];
468 ctrl->ldctl_oid = LDAP_CONTROL_X_DEREF;
469 ctrl->ldctl_iscritical = 0;
470 ctrl->ldctl_value.bv_len = ctrlval.bv_len;
471 AC_MEMCPY( ctrl->ldctl_value.bv_val, ctrlval.bv_val, ctrlval.bv_len );
472 ctrl->ldctl_value.bv_val[ ctrl->ldctl_value.bv_len ] = '\0';
478 slap_add_ctrls( op, rs, ctrlsp );
480 rc = SLAP_CB_CONTINUE;
484 for ( ; drhead != NULL; ) {
485 DerefRes *drnext = drhead->dr_next;
486 op->o_tmpfree( drhead, op->o_tmpmemctx );
490 } else if ( rs->sr_type == REP_RESULT ) {
491 rc = deref_cleanup( op, rs );
498 deref_op_search( Operation *op, SlapReply *rs )
504 sc = op->o_tmpcalloc( 1, sizeof( slap_callback ) + sizeof( deref_cb_t ), op->o_tmpmemctx );
506 dc = (deref_cb_t *)&sc[ 1 ];
507 dc->dc_on = (slap_overinst *)op->o_bd->bd_info;
508 dc->dc_ds = (DerefSpec *)op->o_ctrlderef;
510 sc->sc_response = deref_response;
511 sc->sc_cleanup = deref_cleanup;
512 sc->sc_private = (void *)dc;
514 sc->sc_next = op->o_callback->sc_next;
515 op->o_callback->sc_next = sc;
518 return SLAP_CB_CONTINUE;
522 deref_db_init( BackendDB *be, ConfigReply *cr)
524 if ( ov_count == 0 ) {
527 rc = register_supported_control2( LDAP_CONTROL_X_DEREF,
533 if ( rc != LDAP_SUCCESS ) {
534 Debug( LDAP_DEBUG_ANY,
535 "deref_init: Failed to register control (%d)\n",
545 deref_db_open( BackendDB *be, ConfigReply *cr)
547 return overlay_register_control( be, LDAP_CONTROL_X_DEREF );
550 #ifdef SLAP_CONFIG_DELETE
552 deref_db_destroy( BackendDB *be, ConfigReply *cr)
555 overlay_unregister_control( be, LDAP_CONTROL_X_DEREF );
556 if ( ov_count == 0 ) {
557 unregister_supported_control( LDAP_CONTROL_X_DEREF );
561 #endif /* SLAP_CONFIG_DELETE */
564 deref_initialize(void)
566 deref.on_bi.bi_type = "deref";
567 deref.on_bi.bi_db_init = deref_db_init;
568 deref.on_bi.bi_db_open = deref_db_open;
569 #ifdef SLAP_CONFIG_DELETE
570 deref.on_bi.bi_db_destroy = deref_db_destroy;
571 #endif /* SLAP_CONFIG_DELETE */
572 deref.on_bi.bi_op_search = deref_op_search;
574 return overlay_register( &deref );
577 #if SLAPD_OVER_DEREF == SLAPD_MOD_DYNAMIC
579 init_module( int argc, char *argv[] )
581 return deref_initialize();
583 #endif /* SLAPD_OVER_DEREF == SLAPD_MOD_DYNAMIC */
585 #endif /* SLAPD_OVER_DEREF */