1 /* collect.c - Demonstration of overlay code */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2007 The OpenLDAP Foundation.
6 * Portions Copyright 2003 Howard Chu.
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 the Howard Chu for inclusion
19 * in OpenLDAP Software.
24 #ifdef SLAPD_OVER_COLLECT
28 #include <ac/string.h>
29 #include <ac/socket.h>
34 /* This is a cheap hack to implement a collective attribute.
36 * This demonstration overlay looks for a specified attribute in an
37 * ancestor of a given entry and adds that attribute to the given
38 * entry when it is returned in a search response. It takes no effect
39 * for any other operations. If the ancestor does not exist, there
40 * is no effect. If no attribute was configured, there is no effect.
43 typedef struct collect_info {
44 struct collect_info *ci_next;
46 AttributeDescription *ci_ad;
50 collect_cf( ConfigArgs *c )
52 slap_overinst *on = (slap_overinst *)c->bi;
56 case SLAP_CONFIG_EMIT:
59 for ( ci = on->on_bi.bi_private; ci; ci = ci->ci_next ) {
62 bv.bv_len = ci->ci_dn.bv_len + 3 +
63 ci->ci_ad->ad_cname.bv_len;
64 bv.bv_val = ch_malloc( bv.bv_len + 1 );
65 sprintf( bv.bv_val, "\"%s\" %s", ci->ci_dn.bv_val,
66 ci->ci_ad->ad_cname.bv_val );
67 ber_bvarray_add( &c->rvalue_vals, &bv );
73 if ( c->valx == -1 ) {
74 /* Delete entire attribute */
76 while (( ci = on->on_bi.bi_private )) {
77 on->on_bi.bi_private = ci->ci_next;
78 ch_free( ci->ci_dn.bv_val );
82 /* Delete just one value */
83 collect_info **cip, *ci;
85 cip = (collect_info **)&on->on_bi.bi_private;
86 for ( i=0; i <= c->valx; i++, cip = &ci->ci_next ) ci = *cip;
88 ch_free( ci->ci_dn.bv_val );
99 AttributeDescription *ad = NULL;
101 ber_str2bv( c->argv[1], 0, 0, &bv );
102 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL ) ) {
103 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s invalid DN: \"%s\"",
104 c->argv[0], c->argv[1] );
105 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
106 "%s: %s\n", c->log, c->cr_msg, 0 );
109 if ( slap_str2ad( c->argv[2], &ad, &text ) ) {
110 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s attribute description unknown: \"%s\"",
111 c->argv[0], c->argv[2] );
112 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
113 "%s: %s\n", c->log, c->cr_msg, 0 );
117 /* The on->on_bi.bi_private pointer can be used for
118 * anything this instance of the overlay needs.
120 ci = ch_malloc( sizeof( collect_info ));
123 ci->ci_next = on->on_bi.bi_private;
124 on->on_bi.bi_private = ci;
131 static ConfigTable collectcfg[] = {
132 { "collectinfo", "dn> <attribute", 3, 3, 0,
133 ARG_MAGIC, collect_cf,
134 "( OLcfgOvAt:19.1 NAME 'olcCollectInfo' "
135 "DESC 'DN of entry and attribute to distribute' "
136 "SYNTAX OMsDirectoryString )", NULL, NULL },
137 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
140 static ConfigOCs collectocs[] = {
141 { "( OLcfgOvOc:19.1 "
142 "NAME 'olcCollectConfig' "
143 "DESC 'Collective Attribute configuration' "
144 "SUP olcOverlayConfig "
145 "MAY olcCollectInfo )",
146 Cft_Overlay, collectcfg },
156 slap_overinst *on = (slap_overinst *)be->bd_info;
159 while (( ci = on->on_bi.bi_private )) {
160 on->on_bi.bi_private = ci->ci_next;
161 ch_free( ci->ci_dn.bv_val );
168 collect_response( Operation *op, SlapReply *rs )
170 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
171 collect_info *ci = on->on_bi.bi_private;
173 /* If we've been configured and the current response is
176 if ( ci && rs->sr_type == REP_SEARCH ) {
179 op->o_bd->bd_info = (BackendInfo *)on->on_info;
181 for (; ci; ci=ci->ci_next ) {
182 BerVarray vals = NULL;
184 /* Is our configured entry an ancestor of this one? */
185 if ( !dnIsSuffix( &rs->sr_entry->e_nname, &ci->ci_dn ))
188 /* Extract the values of the desired attribute from
191 rc = backend_attribute( op, NULL, &ci->ci_dn, ci->ci_ad, &vals, ACL_READ );
193 /* If there are any values, merge them into the
197 /* The current entry may live in a cache, so
198 * don't modify it directly. Make a copy and
199 * work with that instead.
201 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE )) {
202 rs->sr_entry = entry_dup( rs->sr_entry );
203 rs->sr_flags |= REP_ENTRY_MODIFIABLE |
204 REP_ENTRY_MUSTBEFREED;
206 attr_merge( rs->sr_entry, ci->ci_ad, vals, NULL );
207 ber_bvarray_free_x( vals, op->o_tmpmemctx );
211 /* Default is to just fall through to the normal processing */
212 return SLAP_CB_CONTINUE;
215 static slap_overinst collect;
217 int collect_initialize() {
220 collect.on_bi.bi_type = "collect";
221 collect.on_bi.bi_db_destroy = collect_destroy;
222 collect.on_response = collect_response;
224 collect.on_bi.bi_cf_ocs = collectocs;
225 code = config_register_schema( collectcfg, collectocs );
226 if ( code ) return code;
228 return overlay_register( &collect );
231 #if SLAPD_OVER_COLLECT == SLAPD_MOD_DYNAMIC
232 int init_module(int argc, char *argv[]) {
233 return collect_initialize();
237 #endif /* SLAPD_OVER_COLLECT */