1 /* collect.c - Demonstration of overlay code */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2008 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>
36 /* This is a cheap hack to implement a collective attribute.
38 * This demonstration overlay looks for a specified attribute in an
39 * ancestor of a given entry and adds that attribute to the given
40 * entry when it is returned in a search response. It takes no effect
41 * for any other operations. If the ancestor does not exist, there
42 * is no effect. If no attribute was configured, there is no effect.
45 /* Use C99 flexible array member if supported, C89 "struct hack" otherwise. */
46 #define FLEXIBLE_STRUCT_SIZE(stype, mtype, mcount) \
47 (sizeof(stype) + sizeof(mtype) * ((mcount) - (0 FLEXIBLE_ARRAY_MEMBER)))
48 #if __STDC_VERSION__>=199901L || (defined __GNUC__ && !defined __STRICT_ANSI__)
49 #define FLEXIBLE_ARRAY_MEMBER
51 #define FLEXIBLE_ARRAY_MEMBER +1
54 typedef struct collect_info {
55 struct collect_info *ci_next;
58 AttributeDescription *ci_ad[FLEXIBLE_ARRAY_MEMBER];
62 * inserts a collect_info into on->on_bi.bi_private taking into account
63 * order. this means longer dn's (i.e. more specific dn's) will be found
64 * first when searching, allowing some limited overlap of dn's
67 insert_ordered( slap_overinst *on, collect_info *ci ) {
68 collect_info *find = on->on_bi.bi_private;
69 collect_info *prev = NULL;
75 /* base case - empty list */
76 on->on_bi.bi_private = ci;
79 /* final case - end of list */
84 } else if (find->ci_dn.bv_len <= ci->ci_dn.bv_len) {
85 /* insert into list here */
87 /* entry is head of list */
88 ci->ci_next = on->on_bi.bi_private;
89 on->on_bi.bi_private = ci;
91 /* entry is not head of list */
105 collect_cf( ConfigArgs *c )
107 slap_overinst *on = (slap_overinst *)c->bi;
111 case SLAP_CONFIG_EMIT:
114 for ( ci = on->on_bi.bi_private; ci; ci = ci->ci_next ) {
119 /* calculate the length & malloc memory */
120 bv.bv_len = ci->ci_dn.bv_len + STRLENOF("\"\" ");
121 for (idx=0; idx<ci->ci_ad_num; idx++) {
122 bv.bv_len += ci->ci_ad[idx]->ad_cname.bv_len;
123 if (idx<(ci->ci_ad_num-1)) {
127 bv.bv_val = ch_malloc( bv.bv_len + 1 );
129 /* copy the value and update len */
130 len = snprintf( bv.bv_val, bv.bv_len + 1, "\"%s\" ",
132 ptr = bv.bv_val + len;
133 for (idx=0; idx<ci->ci_ad_num; idx++) {
134 ptr = lutil_strncopy( ptr,
135 ci->ci_ad[idx]->ad_cname.bv_val,
136 ci->ci_ad[idx]->ad_cname.bv_len);
137 if (idx<(ci->ci_ad_num-1)) {
142 bv.bv_len = ptr - bv.bv_val;
144 ber_bvarray_add( &c->rvalue_vals, &bv );
149 case LDAP_MOD_DELETE:
150 if ( c->valx == -1 ) {
151 /* Delete entire attribute */
153 while (( ci = on->on_bi.bi_private )) {
154 on->on_bi.bi_private = ci->ci_next;
155 ch_free( ci->ci_dn.bv_val );
159 /* Delete just one value */
160 collect_info **cip, *ci;
162 cip = (collect_info **)&on->on_bi.bi_private;
163 for ( i=0; i <= c->valx; i++, cip = &ci->ci_next ) ci = *cip;
165 ch_free( ci->ci_dn.bv_val );
170 case SLAP_CONFIG_ADD:
174 struct berval bv, dn;
179 /* count delimiters in attribute argument */
180 arg = strtok(c->argv[2], ",");
183 arg = strtok(NULL, ",");
186 /* allocate config info with room for attribute array */
187 ci = ch_malloc( FLEXIBLE_STRUCT_SIZE( collect_info,
188 AttributeDescription *, count + 1 ));
190 /* validate and normalize dn */
191 ber_str2bv( c->argv[1], 0, 0, &bv );
192 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL ) ) {
193 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s invalid DN: \"%s\"",
194 c->argv[0], c->argv[1] );
195 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
196 "%s: %s\n", c->log, c->cr_msg, 0 );
200 /* load attribute description for attribute list */
202 for( idx=0; idx<count; idx++) {
203 ci->ci_ad[idx] = NULL;
205 if ( slap_str2ad( arg, &ci->ci_ad[idx], &text ) ) {
206 snprintf( c->cr_msg, sizeof( c->cr_msg ),
207 "%s attribute description unknown: \"%s\"",
209 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
210 "%s: %s\n", c->log, c->cr_msg, 0 );
214 arg++; /* skip to end of argument */
217 arg++; /* skip inner delimiters */
221 /* The on->on_bi.bi_private pointer can be used for
222 * anything this instance of the overlay needs.
224 ci->ci_ad[count] = NULL;
225 ci->ci_ad_num = count;
228 /* creates list of ci's ordered by dn length */
229 insert_ordered ( on, ci );
237 static ConfigTable collectcfg[] = {
238 { "collectinfo", "dn> <attribute", 3, 3, 0,
239 ARG_MAGIC, collect_cf,
240 "( OLcfgOvAt:19.1 NAME 'olcCollectInfo' "
241 "DESC 'DN of entry and attribute to distribute' "
242 "SYNTAX OMsDirectoryString )", NULL, NULL },
243 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
246 static ConfigOCs collectocs[] = {
247 { "( OLcfgOvOc:19.1 "
248 "NAME 'olcCollectConfig' "
249 "DESC 'Collective Attribute configuration' "
250 "SUP olcOverlayConfig "
251 "MAY olcCollectInfo )",
252 Cft_Overlay, collectcfg },
262 slap_overinst *on = (slap_overinst *)be->bd_info;
265 while (( ci = on->on_bi.bi_private )) {
266 on->on_bi.bi_private = ci->ci_next;
267 ch_free( ci->ci_dn.bv_val );
274 collect_modify( Operation *op, SlapReply *rs)
276 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
277 collect_info *ci = on->on_bi.bi_private;
282 for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next) {
283 for (; ci; ci=ci->ci_next ) {
284 /* Is this entry an ancestor of this collectinfo ? */
285 if (!dnIsSuffix(&op->o_req_ndn, &ci->ci_dn)) {
286 /* this collectinfo does not match */
290 /* Is this entry the same as the template DN ? */
291 if ( dn_match(&op->o_req_ndn, &ci->ci_dn)) {
292 /* all changes in this ci are allowed */
296 /* check for collect attributes - disallow modify if present */
297 for(idx=0; idx<ci->ci_ad_num; idx++) {
298 if (ml->sml_desc == ci->ci_ad[idx]) {
299 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
300 snprintf( errMsg, sizeof( errMsg ),
301 "cannot change virtual attribute '%s'",
302 ci->ci_ad[idx]->ad_cname.bv_val);
303 rs->sr_text = errMsg;
304 send_ldap_result( op, rs );
312 return SLAP_CB_CONTINUE;
316 collect_response( Operation *op, SlapReply *rs )
318 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
319 collect_info *ci = on->on_bi.bi_private;
321 /* If we've been configured and the current response is
324 if ( ci && rs->sr_type == REP_SEARCH ) {
327 op->o_bd->bd_info = (BackendInfo *)on->on_info;
329 for (; ci; ci=ci->ci_next ) {
332 /* Is this entry an ancestor of this collectinfo ? */
333 if (!dnIsSuffix(&rs->sr_entry->e_nname, &ci->ci_dn)) {
334 /* collectinfo does not match */
338 /* Is this entry the same as the template DN ? */
339 if ( dn_match(&rs->sr_entry->e_nname, &ci->ci_dn)) {
340 /* dont apply change to parent */
344 /* The current entry may live in a cache, so
345 * don't modify it directly. Make a copy and
346 * work with that instead.
348 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE )) {
349 rs->sr_entry = entry_dup( rs->sr_entry );
350 rs->sr_flags |= REP_ENTRY_MODIFIABLE |
351 REP_ENTRY_MUSTBEFREED;
354 /* Loop for each attribute in this collectinfo */
355 for(idx=0; idx<ci->ci_ad_num; idx++) {
356 BerVarray vals = NULL;
358 /* Extract the values of the desired attribute from
359 * the ancestor entry */
360 rc = backend_attribute( op, NULL, &ci->ci_dn,
361 ci->ci_ad[idx], &vals, ACL_READ );
363 /* If there are any values, merge them into the
364 * current search result
367 attr_merge( rs->sr_entry, ci->ci_ad[idx],
369 ber_bvarray_free_x( vals, op->o_tmpmemctx );
375 /* Default is to just fall through to the normal processing */
376 return SLAP_CB_CONTINUE;
379 static slap_overinst collect;
381 int collect_initialize() {
384 collect.on_bi.bi_type = "collect";
385 collect.on_bi.bi_db_destroy = collect_destroy;
386 collect.on_bi.bi_op_modify = collect_modify;
387 collect.on_response = collect_response;
389 collect.on_bi.bi_cf_ocs = collectocs;
390 code = config_register_schema( collectcfg, collectocs );
391 if ( code ) return code;
393 return overlay_register( &collect );
396 #if SLAPD_OVER_COLLECT == SLAPD_MOD_DYNAMIC
397 int init_module(int argc, char *argv[]) {
398 return collect_initialize();
402 #endif /* SLAPD_OVER_COLLECT */