1 /* cloak.c - Overlay to hide some attribute except if explicitely requested */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2008-2010 The OpenLDAP Foundation.
6 * Portions Copyright 2008 Emmanuel Dreyfus
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 originally developed by the Emmanuel Dreyfus for
19 * inclusion in OpenLDAP Software.
24 #ifdef SLAPD_OVER_CLOAK
28 #include <ac/string.h>
29 #include <ac/socket.h>
35 enum { CLOAK_ATTR = 1 };
37 typedef struct cloak_info_t {
39 AttributeDescription *ci_ad;
40 struct cloak_info_t *ci_next;
43 #define CLOAK_USAGE "\"cloak-attr <attr> [<class>]\": "
46 cloak_cfgen( ConfigArgs *c )
48 slap_overinst *on = (slap_overinst *)c->bi;
49 cloak_info_t *ci = (cloak_info_t *)on->on_bi.bi_private;
53 if ( c->op == SLAP_CONFIG_EMIT ) {
56 for ( i = 0; ci; i++, ci = ci->ci_next ) {
60 assert( ci->ci_ad != NULL );
62 if ( ci->ci_oc != NULL )
63 len = snprintf( c->cr_msg,
65 SLAP_X_ORDERED_FMT "%s %s", i,
66 ci->ci_ad->ad_cname.bv_val,
67 ci->ci_oc->soc_cname.bv_val );
69 len = snprintf( c->cr_msg,
71 SLAP_X_ORDERED_FMT "%s", i,
72 ci->ci_ad->ad_cname.bv_val );
74 bv.bv_val = c->cr_msg;
76 value_add_one( &c->rvalue_vals, &bv );
87 } else if ( c->op == LDAP_MOD_DELETE ) {
88 cloak_info_t *ci_next;
92 for ( ci_next = ci, i = 0;
93 ci_next, c->valx < 0 || i < c->valx;
96 ci_next = ci->ci_next;
98 ch_free ( ci->ci_ad );
99 if ( ci->ci_oc != NULL )
100 ch_free ( ci->ci_oc );
104 ci = (cloak_info_t *)on->on_bi.bi_private;
117 ObjectClass *oc = NULL;
118 AttributeDescription *ad = NULL;
120 cloak_info_t **cip = NULL;
121 cloak_info_t *ci_next = NULL;
123 if ( c->argc == 3 ) {
124 oc = oc_find( c->argv[ 2 ] );
129 "unable to find ObjectClass \"%s\"",
131 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
132 c->log, c->cr_msg, 0 );
137 rc = slap_str2ad( c->argv[ 1 ], &ad, &text );
138 if ( rc != LDAP_SUCCESS ) {
139 snprintf( c->cr_msg, sizeof( c->cr_msg ), CLOAK_USAGE
140 "unable to find AttributeDescription \"%s\"",
142 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
143 c->log, c->cr_msg, 0 );
147 for ( i = 0, cip = (cloak_info_t **)&on->on_bi.bi_private;
148 c->valx < 0 || i < c->valx, *cip;
149 i++, cip = &(*cip)->ci_next ) {
150 if ( c->valx >= 0 && *cip == NULL ) {
151 snprintf( c->cr_msg, sizeof( c->cr_msg ),
153 "invalid index {%d}\n",
155 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
156 c->log, c->cr_msg, 0 );
162 *cip = (cloak_info_t *)ch_calloc( 1, sizeof( cloak_info_t ) );
165 (*cip)->ci_next = ci_next;
180 cloak_search_cb( Operation *op, SlapReply *rs )
187 assert( op && op->o_callback && rs );
189 if ( rs->sr_type != REP_SEARCH || !rs->sr_entry ) {
190 slap_freeself_cb( op, rs );
191 return ( SLAP_CB_CONTINUE );
198 * First perform a quick scan for an attribute to cloak
200 for ( ci = (cloak_info_t *)sc->sc_private; ci; ci = ci->ci_next ) {
203 if ( ci->ci_oc != NULL &&
204 !is_entry_objectclass_or_sub( e, ci->ci_oc ) )
207 for ( a = e->e_attrs; a; a = a->a_next )
208 if ( a->a_desc == ci->ci_ad )
216 * Nothing found to cloak
219 return ( SLAP_CB_CONTINUE );
222 * We are now committed to cloak an attribute.
224 if ( rs->sr_flags & REP_ENTRY_MODIFIABLE )
229 for ( ci = (cloak_info_t *)sc->sc_private; ci; ci = ci->ci_next ) {
233 for ( pa = NULL, a = me->e_attrs;
235 pa = a, a = a->a_next ) {
237 if ( a->a_desc != ci->ci_ad )
240 Debug( LDAP_DEBUG_TRACE, "cloak_search_cb: cloak %s\n",
241 a->a_desc->ad_cname.bv_val,
245 pa->a_next = a->a_next;
247 me->e_attrs = a->a_next;
255 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED )
259 rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
262 return ( SLAP_CB_CONTINUE );
266 cloak_search( Operation *op, SlapReply *rs )
268 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
269 cloak_info_t *ci = (cloak_info_t *)on->on_bi.bi_private;
272 if ( op->ors_attrsonly ||
274 get_manageDSAit( op ) )
275 return SLAP_CB_CONTINUE;
277 sc = op->o_tmpcalloc( 1, sizeof( *sc ), op->o_tmpmemctx );
278 sc->sc_response = cloak_search_cb;
279 sc->sc_cleanup = slap_freeself_cb;
280 sc->sc_next = op->o_callback;
284 return SLAP_CB_CONTINUE;
287 static slap_overinst cloak_ovl;
289 static ConfigTable cloakcfg[] = {
290 { "cloak-attr", "attribute [class]",
291 2, 3, 0, ARG_MAGIC|CLOAK_ATTR, cloak_cfgen,
292 "( OLcfgCtAt:4.1 NAME 'olcCloakAttribute' "
293 "DESC 'Cloaked attribute: attribute [class]' "
294 "EQUALITY caseIgnoreMatch "
295 "SYNTAX OMsDirectoryString "
296 "X-ORDERED 'VALUES' )",
298 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
301 static ConfigOCs cloakocs[] = {
303 "NAME 'olcCloakConfig' "
304 "DESC 'Attribute cloak configuration' "
305 "SUP olcOverlayConfig "
306 "MAY ( olcCloakAttribute ) )",
307 Cft_Overlay, cloakcfg },
311 #if SLAPD_OVER_CLOAK == SLAPD_MOD_DYNAMIC
315 cloak_initialize( void ) {
317 cloak_ovl.on_bi.bi_type = "cloak";
318 cloak_ovl.on_bi.bi_op_search = cloak_search;
319 cloak_ovl.on_bi.bi_cf_ocs = cloakocs;
321 rc = config_register_schema ( cloakcfg, cloakocs );
325 return overlay_register( &cloak_ovl );
328 #if SLAPD_OVER_CLOAK == SLAPD_MOD_DYNAMIC
329 int init_module(int argc, char *argv[]) {
330 return cloak_initialize();
334 #endif /* defined(SLAPD_OVER_CLOAK) */