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>
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;
47 AttributeDescription *ci_ad[];
51 * inserts a collect_info into on->on_bi.bi_private taking into account
52 * order. this means longer dn's (i.e. more specific dn's) will be found
53 * first when searching, allowing some limited overlap of dn's
56 insert_ordered( slap_overinst *on, collect_info *ci ) {
57 collect_info *find = on->on_bi.bi_private;
58 collect_info *prev = NULL;
64 /* base case - empty list */
65 on->on_bi.bi_private = ci;
68 /* final case - end of list */
73 } else if (find->ci_dn.bv_len <= ci->ci_dn.bv_len) {
74 /* insert into list here */
76 /* entry is head of list */
77 ci->ci_next = on->on_bi.bi_private;
78 on->on_bi.bi_private = ci;
80 /* entry is not head of list */
94 collect_cf( ConfigArgs *c )
96 slap_overinst *on = (slap_overinst *)c->bi;
100 case SLAP_CONFIG_EMIT:
103 for ( ci = on->on_bi.bi_private; ci; ci = ci->ci_next ) {
108 /* calculate the length & malloc memory */
109 bv.bv_len = ci->ci_dn.bv_len + STRLENOF("\"\" ");
110 for (idx=0; idx<ci->ci_ad_num; idx++) {
111 bv.bv_len += ci->ci_ad[idx]->ad_cname.bv_len;
112 if (idx<(ci->ci_ad_num-1)) {
116 bv.bv_val = ch_malloc( bv.bv_len + 1 );
118 /* copy the value and update len */
119 len = snprintf( bv.bv_val, bv.bv_len + 1, "\"%s\" ",
121 ptr = bv.bv_val + len;
122 for (idx=0; idx<ci->ci_ad_num; idx++) {
123 ptr = lutil_strncopy( ptr,
124 ci->ci_ad[idx]->ad_cname.bv_val,
125 ci->ci_ad[idx]->ad_cname.bv_len);
126 if (idx<(ci->ci_ad_num-1)) {
131 bv.bv_len = ptr - bv.bv_val;
133 ber_bvarray_add( &c->rvalue_vals, &bv );
138 case LDAP_MOD_DELETE:
139 if ( c->valx == -1 ) {
140 /* Delete entire attribute */
142 while (( ci = on->on_bi.bi_private )) {
143 on->on_bi.bi_private = ci->ci_next;
144 ch_free( ci->ci_dn.bv_val );
148 /* Delete just one value */
149 collect_info **cip, *ci;
151 cip = (collect_info **)&on->on_bi.bi_private;
152 for ( i=0; i <= c->valx; i++, cip = &ci->ci_next ) ci = *cip;
154 ch_free( ci->ci_dn.bv_val );
159 case SLAP_CONFIG_ADD:
163 struct berval bv, dn;
168 /* count delimiters in attribute argument */
169 arg = strtok(c->argv[2], ",");
172 arg = strtok(NULL, ",");
175 /* allocate config info with room for attribute array */
176 ci = ch_malloc( sizeof( collect_info ) +
177 ( sizeof (AttributeDescription *) * (count + 1)));
179 /* validate and normalize dn */
180 ber_str2bv( c->argv[1], 0, 0, &bv );
181 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL ) ) {
182 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s invalid DN: \"%s\"",
183 c->argv[0], c->argv[1] );
184 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
185 "%s: %s\n", c->log, c->cr_msg, 0 );
189 /* load attribute description for attribute list */
191 for( idx=0; idx<count; idx++) {
192 ci->ci_ad[idx] = NULL;
194 if ( slap_str2ad( arg, &ci->ci_ad[idx], &text ) ) {
195 snprintf( c->cr_msg, sizeof( c->cr_msg ),
196 "%s attribute description unknown: \"%s\"",
198 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
199 "%s: %s\n", c->log, c->cr_msg, 0 );
203 arg++; /* skip to end of argument */
206 arg++; /* skip inner delimiters */
210 /* The on->on_bi.bi_private pointer can be used for
211 * anything this instance of the overlay needs.
213 ci->ci_ad[count] = NULL;
214 ci->ci_ad_num = count;
217 /* creates list of ci's ordered by dn length */
218 insert_ordered ( on, ci );
226 static ConfigTable collectcfg[] = {
227 { "collectinfo", "dn> <attribute", 3, 3, 0,
228 ARG_MAGIC, collect_cf,
229 "( OLcfgOvAt:19.1 NAME 'olcCollectInfo' "
230 "DESC 'DN of entry and attribute to distribute' "
231 "SYNTAX OMsDirectoryString )", NULL, NULL },
232 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
235 static ConfigOCs collectocs[] = {
236 { "( OLcfgOvOc:19.1 "
237 "NAME 'olcCollectConfig' "
238 "DESC 'Collective Attribute configuration' "
239 "SUP olcOverlayConfig "
240 "MAY olcCollectInfo )",
241 Cft_Overlay, collectcfg },
251 slap_overinst *on = (slap_overinst *)be->bd_info;
254 while (( ci = on->on_bi.bi_private )) {
255 on->on_bi.bi_private = ci->ci_next;
256 ch_free( ci->ci_dn.bv_val );
263 collect_modify( Operation *op, SlapReply *rs)
265 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
266 collect_info *ci = on->on_bi.bi_private;
271 for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next) {
272 for (; ci; ci=ci->ci_next ) {
273 /* Is this entry an ancestor of this collectinfo ? */
274 if (!dnIsSuffix(&op->o_req_ndn, &ci->ci_dn)) {
275 /* this collectinfo does not match */
279 /* Is this entry the same as the template DN ? */
280 if ( dn_match(&op->o_req_ndn, &ci->ci_dn)) {
281 /* all changes in this ci are allowed */
285 /* check for collect attributes - disallow modify if present */
286 for(idx=0; idx<ci->ci_ad_num; idx++) {
287 if (ml->sml_desc == ci->ci_ad[idx]) {
288 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
289 snprintf( errMsg, sizeof( errMsg ),
290 "cannot change virtual attribute '%s'",
291 ci->ci_ad[idx]->ad_cname.bv_val);
292 rs->sr_text = errMsg;
293 send_ldap_result( op, rs );
301 return SLAP_CB_CONTINUE;
305 collect_response( Operation *op, SlapReply *rs )
307 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
308 collect_info *ci = on->on_bi.bi_private;
310 /* If we've been configured and the current response is
313 if ( ci && rs->sr_type == REP_SEARCH ) {
316 op->o_bd->bd_info = (BackendInfo *)on->on_info;
318 for (; ci; ci=ci->ci_next ) {
321 /* Is this entry an ancestor of this collectinfo ? */
322 if (!dnIsSuffix(&rs->sr_entry->e_nname, &ci->ci_dn)) {
323 /* collectinfo does not match */
327 /* Is this entry the same as the template DN ? */
328 if ( dn_match(&rs->sr_entry->e_nname, &ci->ci_dn)) {
329 /* dont apply change to parent */
333 /* The current entry may live in a cache, so
334 * don't modify it directly. Make a copy and
335 * work with that instead.
337 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE )) {
338 rs->sr_entry = entry_dup( rs->sr_entry );
339 rs->sr_flags |= REP_ENTRY_MODIFIABLE |
340 REP_ENTRY_MUSTBEFREED;
343 /* Loop for each attribute in this collectinfo */
344 for(idx=0; idx<ci->ci_ad_num; idx++) {
345 BerVarray vals = NULL;
347 /* Extract the values of the desired attribute from
348 * the ancestor entry */
349 rc = backend_attribute( op, NULL, &ci->ci_dn,
350 ci->ci_ad[idx], &vals, ACL_READ );
352 /* If there are any values, merge them into the
353 * current search result
356 attr_merge( rs->sr_entry, ci->ci_ad[idx],
358 ber_bvarray_free_x( vals, op->o_tmpmemctx );
364 /* Default is to just fall through to the normal processing */
365 return SLAP_CB_CONTINUE;
368 static slap_overinst collect;
370 int collect_initialize() {
373 collect.on_bi.bi_type = "collect";
374 collect.on_bi.bi_db_destroy = collect_destroy;
375 collect.on_bi.bi_op_modify = collect_modify;
376 collect.on_response = collect_response;
378 collect.on_bi.bi_cf_ocs = collectocs;
379 code = config_register_schema( collectcfg, collectocs );
380 if ( code ) return code;
382 return overlay_register( &collect );
385 #if SLAPD_OVER_COLLECT == SLAPD_MOD_DYNAMIC
386 int init_module(int argc, char *argv[]) {
387 return collect_initialize();
391 #endif /* SLAPD_OVER_COLLECT */