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 typedef struct collect_info {
46 struct collect_info *ci_next;
49 AttributeDescription *ci_ad[];
53 * inserts a collect_info into on->on_bi.bi_private taking into account
54 * order. this means longer dn's (i.e. more specific dn's) will be found
55 * first when searching, allowing some limited overlap of dn's
58 insert_ordered( slap_overinst *on, collect_info *ci ) {
59 collect_info *find = on->on_bi.bi_private;
60 collect_info *prev = NULL;
66 /* base case - empty list */
67 on->on_bi.bi_private = ci;
70 /* final case - end of list */
75 } else if (find->ci_dn.bv_len <= ci->ci_dn.bv_len) {
76 /* insert into list here */
78 /* entry is head of list */
79 ci->ci_next = on->on_bi.bi_private;
80 on->on_bi.bi_private = ci;
82 /* entry is not head of list */
96 collect_cf( ConfigArgs *c )
98 slap_overinst *on = (slap_overinst *)c->bi;
102 case SLAP_CONFIG_EMIT:
105 for ( ci = on->on_bi.bi_private; ci; ci = ci->ci_next ) {
110 /* calculate the length & malloc memory */
111 bv.bv_len = ci->ci_dn.bv_len + STRLENOF("\"\" ");
112 for (idx=0; idx<ci->ci_ad_num; idx++) {
113 bv.bv_len += ci->ci_ad[idx]->ad_cname.bv_len;
114 if (idx<(ci->ci_ad_num-1)) {
118 bv.bv_val = ch_malloc( bv.bv_len + 1 );
120 /* copy the value and update len */
121 len = snprintf( bv.bv_val, bv.bv_len + 1, "\"%s\" ",
123 ptr = bv.bv_val + len;
124 for (idx=0; idx<ci->ci_ad_num; idx++) {
125 ptr = lutil_strncopy( ptr,
126 ci->ci_ad[idx]->ad_cname.bv_val,
127 ci->ci_ad[idx]->ad_cname.bv_len);
128 if (idx<(ci->ci_ad_num-1)) {
133 bv.bv_len = ptr - bv.bv_val;
135 ber_bvarray_add( &c->rvalue_vals, &bv );
140 case LDAP_MOD_DELETE:
141 if ( c->valx == -1 ) {
142 /* Delete entire attribute */
144 while (( ci = on->on_bi.bi_private )) {
145 on->on_bi.bi_private = ci->ci_next;
146 ch_free( ci->ci_dn.bv_val );
150 /* Delete just one value */
151 collect_info **cip, *ci;
153 cip = (collect_info **)&on->on_bi.bi_private;
154 for ( i=0; i <= c->valx; i++, cip = &ci->ci_next ) ci = *cip;
156 ch_free( ci->ci_dn.bv_val );
161 case SLAP_CONFIG_ADD:
165 struct berval bv, dn;
170 /* count delimiters in attribute argument */
171 arg = strtok(c->argv[2], ",");
174 arg = strtok(NULL, ",");
177 /* allocate config info with room for attribute array */
178 ci = ch_malloc( sizeof( collect_info ) +
179 ( sizeof (AttributeDescription *) * (count + 1)));
181 /* validate and normalize dn */
182 ber_str2bv( c->argv[1], 0, 0, &bv );
183 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL ) ) {
184 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s invalid DN: \"%s\"",
185 c->argv[0], c->argv[1] );
186 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
187 "%s: %s\n", c->log, c->cr_msg, 0 );
191 /* load attribute description for attribute list */
193 for( idx=0; idx<count; idx++) {
194 ci->ci_ad[idx] = NULL;
196 if ( slap_str2ad( arg, &ci->ci_ad[idx], &text ) ) {
197 snprintf( c->cr_msg, sizeof( c->cr_msg ),
198 "%s attribute description unknown: \"%s\"",
200 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
201 "%s: %s\n", c->log, c->cr_msg, 0 );
205 arg++; /* skip to end of argument */
208 arg++; /* skip inner delimiters */
212 /* The on->on_bi.bi_private pointer can be used for
213 * anything this instance of the overlay needs.
215 ci->ci_ad[count] = NULL;
216 ci->ci_ad_num = count;
219 /* creates list of ci's ordered by dn length */
220 insert_ordered ( on, ci );
228 static ConfigTable collectcfg[] = {
229 { "collectinfo", "dn> <attribute", 3, 3, 0,
230 ARG_MAGIC, collect_cf,
231 "( OLcfgOvAt:19.1 NAME 'olcCollectInfo' "
232 "DESC 'DN of entry and attribute to distribute' "
233 "SYNTAX OMsDirectoryString )", NULL, NULL },
234 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
237 static ConfigOCs collectocs[] = {
238 { "( OLcfgOvOc:19.1 "
239 "NAME 'olcCollectConfig' "
240 "DESC 'Collective Attribute configuration' "
241 "SUP olcOverlayConfig "
242 "MAY olcCollectInfo )",
243 Cft_Overlay, collectcfg },
253 slap_overinst *on = (slap_overinst *)be->bd_info;
256 while (( ci = on->on_bi.bi_private )) {
257 on->on_bi.bi_private = ci->ci_next;
258 ch_free( ci->ci_dn.bv_val );
265 collect_modify( Operation *op, SlapReply *rs)
267 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
268 collect_info *ci = on->on_bi.bi_private;
273 for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next) {
274 for (; ci; ci=ci->ci_next ) {
275 /* Is this entry an ancestor of this collectinfo ? */
276 if (!dnIsSuffix(&op->o_req_ndn, &ci->ci_dn)) {
277 /* this collectinfo does not match */
281 /* Is this entry the same as the template DN ? */
282 if ( dn_match(&op->o_req_ndn, &ci->ci_dn)) {
283 /* all changes in this ci are allowed */
287 /* check for collect attributes - disallow modify if present */
288 for(idx=0; idx<ci->ci_ad_num; idx++) {
289 if (ml->sml_desc == ci->ci_ad[idx]) {
290 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
291 snprintf( errMsg, sizeof( errMsg ),
292 "cannot change virtual attribute '%s'",
293 ci->ci_ad[idx]->ad_cname.bv_val);
294 rs->sr_text = errMsg;
295 send_ldap_result( op, rs );
303 return SLAP_CB_CONTINUE;
307 collect_response( Operation *op, SlapReply *rs )
309 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
310 collect_info *ci = on->on_bi.bi_private;
312 /* If we've been configured and the current response is
315 if ( ci && rs->sr_type == REP_SEARCH ) {
318 op->o_bd->bd_info = (BackendInfo *)on->on_info;
320 for (; ci; ci=ci->ci_next ) {
323 /* Is this entry an ancestor of this collectinfo ? */
324 if (!dnIsSuffix(&rs->sr_entry->e_nname, &ci->ci_dn)) {
325 /* collectinfo does not match */
329 /* Is this entry the same as the template DN ? */
330 if ( dn_match(&rs->sr_entry->e_nname, &ci->ci_dn)) {
331 /* dont apply change to parent */
335 /* The current entry may live in a cache, so
336 * don't modify it directly. Make a copy and
337 * work with that instead.
339 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE )) {
340 rs->sr_entry = entry_dup( rs->sr_entry );
341 rs->sr_flags |= REP_ENTRY_MODIFIABLE |
342 REP_ENTRY_MUSTBEFREED;
345 /* Loop for each attribute in this collectinfo */
346 for(idx=0; idx<ci->ci_ad_num; idx++) {
347 BerVarray vals = NULL;
349 /* Extract the values of the desired attribute from
350 * the ancestor entry */
351 rc = backend_attribute( op, NULL, &ci->ci_dn,
352 ci->ci_ad[idx], &vals, ACL_READ );
354 /* If there are any values, merge them into the
355 * current search result
358 attr_merge( rs->sr_entry, ci->ci_ad[idx],
360 ber_bvarray_free_x( vals, op->o_tmpmemctx );
366 /* Default is to just fall through to the normal processing */
367 return SLAP_CB_CONTINUE;
370 static slap_overinst collect;
372 int collect_initialize() {
375 collect.on_bi.bi_type = "collect";
376 collect.on_bi.bi_db_destroy = collect_destroy;
377 collect.on_bi.bi_op_modify = collect_modify;
378 collect.on_response = collect_response;
380 collect.on_bi.bi_cf_ocs = collectocs;
381 code = config_register_schema( collectcfg, collectocs );
382 if ( code ) return code;
384 return overlay_register( &collect );
387 #if SLAPD_OVER_COLLECT == SLAPD_MOD_DYNAMIC
388 int init_module(int argc, char *argv[]) {
389 return collect_initialize();
393 #endif /* SLAPD_OVER_COLLECT */