1 /* collect.c - Demonstration of overlay code */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2012 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[1];
52 static int collect_cf( ConfigArgs *c );
54 static ConfigTable collectcfg[] = {
55 { "collectinfo", "dn> <attribute", 3, 3, 0,
56 ARG_MAGIC, collect_cf,
57 "( OLcfgOvAt:19.1 NAME 'olcCollectInfo' "
58 "DESC 'DN of entry and attribute to distribute' "
59 "EQUALITY caseIgnoreMatch "
60 "SYNTAX OMsDirectoryString )", NULL, NULL },
61 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
64 static ConfigOCs collectocs[] = {
66 "NAME 'olcCollectConfig' "
67 "DESC 'Collective Attribute configuration' "
68 "SUP olcOverlayConfig "
69 "MAY olcCollectInfo )",
70 Cft_Overlay, collectcfg },
75 * inserts a collect_info into on->on_bi.bi_private taking into account
76 * order. this means longer dn's (i.e. more specific dn's) will be found
77 * first when searching, allowing some limited overlap of dn's
80 insert_ordered( slap_overinst *on, collect_info *ci ) {
81 collect_info *find = on->on_bi.bi_private;
82 collect_info *prev = NULL;
88 /* base case - empty list */
89 on->on_bi.bi_private = ci;
92 /* final case - end of list */
97 } else if (find->ci_dn.bv_len < ci->ci_dn.bv_len) {
98 /* insert into list here */
100 /* entry is head of list */
101 ci->ci_next = on->on_bi.bi_private;
102 on->on_bi.bi_private = ci;
104 /* entry is not head of list */
112 find = find->ci_next;
118 collect_cf( ConfigArgs *c )
120 slap_overinst *on = (slap_overinst *)c->bi;
124 case SLAP_CONFIG_EMIT:
127 for ( ci = on->on_bi.bi_private; ci; ci = ci->ci_next ) {
132 /* calculate the length & malloc memory */
133 bv.bv_len = ci->ci_dn.bv_len + STRLENOF("\"\" ");
134 for (idx=0; idx<ci->ci_ad_num; idx++) {
135 bv.bv_len += ci->ci_ad[idx]->ad_cname.bv_len;
136 if (idx<(ci->ci_ad_num-1)) {
140 bv.bv_val = ch_malloc( bv.bv_len + 1 );
142 /* copy the value and update len */
143 len = snprintf( bv.bv_val, bv.bv_len + 1, "\"%s\" ",
145 ptr = bv.bv_val + len;
146 for (idx=0; idx<ci->ci_ad_num; idx++) {
147 ptr = lutil_strncopy( ptr,
148 ci->ci_ad[idx]->ad_cname.bv_val,
149 ci->ci_ad[idx]->ad_cname.bv_len);
150 if (idx<(ci->ci_ad_num-1)) {
155 bv.bv_len = ptr - bv.bv_val;
157 ber_bvarray_add( &c->rvalue_vals, &bv );
162 case LDAP_MOD_DELETE:
163 if ( c->valx == -1 ) {
164 /* Delete entire attribute */
166 while (( ci = on->on_bi.bi_private )) {
167 on->on_bi.bi_private = ci->ci_next;
168 ch_free( ci->ci_dn.bv_val );
172 /* Delete just one value */
173 collect_info **cip, *ci;
175 cip = (collect_info **)&on->on_bi.bi_private;
177 for ( i=0; i < c->valx; i++ ) {
182 ch_free( ci->ci_dn.bv_val );
187 case SLAP_CONFIG_ADD:
191 struct berval bv, dn;
196 /* count delimiters in attribute argument */
197 arg = strtok(c->argv[2], ",");
200 arg = strtok(NULL, ",");
203 /* validate and normalize dn */
204 ber_str2bv( c->argv[1], 0, 0, &bv );
205 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL ) ) {
206 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s invalid DN: \"%s\"",
207 c->argv[0], c->argv[1] );
208 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
209 "%s: %s\n", c->log, c->cr_msg, 0 );
213 /* check for duplicate DNs */
214 for ( ci = (collect_info *)on->on_bi.bi_private; ci;
216 /* If new DN is longest, there are no possible matches */
217 if ( dn.bv_len > ci->ci_dn.bv_len ) {
221 if ( bvmatch( &dn, &ci->ci_dn )) {
226 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s DN already configured: \"%s\"",
227 c->argv[0], c->argv[1] );
228 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
229 "%s: %s\n", c->log, c->cr_msg, 0 );
233 /* allocate config info with room for attribute array */
234 ci = ch_malloc( sizeof( collect_info ) +
235 sizeof( AttributeDescription * ) * count );
237 /* load attribute description for attribute list */
239 for( idx=0; idx<count; idx++) {
240 ci->ci_ad[idx] = NULL;
242 if ( slap_str2ad( arg, &ci->ci_ad[idx], &text ) ) {
243 snprintf( c->cr_msg, sizeof( c->cr_msg ),
244 "%s attribute description unknown: \"%s\"",
246 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
247 "%s: %s\n", c->log, c->cr_msg, 0 );
252 arg++; /* skip to end of argument */
255 arg++; /* skip inner delimiters */
259 /* The on->on_bi.bi_private pointer can be used for
260 * anything this instance of the overlay needs.
262 ci->ci_ad[count] = NULL;
263 ci->ci_ad_num = count;
266 /* creates list of ci's ordered by dn length */
267 insert_ordered ( on, ci );
269 /* New ci wasn't simply appended to end, adjust its
270 * position in the config entry's a_vals
272 if ( c->ca_entry && ci->ci_next ) {
273 Attribute *a = attr_find( c->ca_entry->e_attrs,
276 struct berval bv, nbv;
277 collect_info *c2 = (collect_info *)on->on_bi.bi_private;
279 for ( i=0; c2 != ci; i++, c2 = c2->ci_next );
280 bv = a->a_vals[a->a_numvals-1];
281 nbv = a->a_nvals[a->a_numvals-1];
282 for ( j=a->a_numvals-1; j>i; j-- ) {
283 a->a_vals[j] = a->a_vals[j-1];
284 a->a_nvals[j] = a->a_nvals[j-1];
303 slap_overinst *on = (slap_overinst *)be->bd_info;
306 while (( ci = on->on_bi.bi_private )) {
307 on->on_bi.bi_private = ci->ci_next;
308 ch_free( ci->ci_dn.bv_val );
315 collect_modify( Operation *op, SlapReply *rs)
317 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
318 collect_info *ci = on->on_bi.bi_private;
323 for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next) {
324 for (; ci; ci=ci->ci_next ) {
325 /* Is this entry an ancestor of this collectinfo ? */
326 if (!dnIsSuffix(&op->o_req_ndn, &ci->ci_dn)) {
327 /* this collectinfo does not match */
331 /* Is this entry the same as the template DN ? */
332 if ( dn_match(&op->o_req_ndn, &ci->ci_dn)) {
333 /* all changes in this ci are allowed */
337 /* check for collect attributes - disallow modify if present */
338 for(idx=0; idx<ci->ci_ad_num; idx++) {
339 if (ml->sml_desc == ci->ci_ad[idx]) {
340 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
341 snprintf( errMsg, sizeof( errMsg ),
342 "cannot change virtual attribute '%s'",
343 ci->ci_ad[idx]->ad_cname.bv_val);
344 rs->sr_text = errMsg;
345 send_ldap_result( op, rs );
353 return SLAP_CB_CONTINUE;
357 collect_response( Operation *op, SlapReply *rs )
359 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
360 collect_info *ci = on->on_bi.bi_private;
362 /* If we've been configured and the current response is
365 if ( ci && rs->sr_type == REP_SEARCH ) {
368 op->o_bd->bd_info = (BackendInfo *)on->on_info;
370 for (; ci; ci=ci->ci_next ) {
373 /* Is this entry an ancestor of this collectinfo ? */
374 if (!dnIsSuffix(&rs->sr_entry->e_nname, &ci->ci_dn)) {
375 /* collectinfo does not match */
379 /* Is this entry the same as the template DN ? */
380 if ( dn_match(&rs->sr_entry->e_nname, &ci->ci_dn)) {
381 /* dont apply change to parent */
385 /* The current entry may live in a cache, so
386 * don't modify it directly. Make a copy and
387 * work with that instead.
389 rs_entry2modifiable( op, rs, on );
391 /* Loop for each attribute in this collectinfo */
392 for(idx=0; idx<ci->ci_ad_num; idx++) {
393 BerVarray vals = NULL;
395 /* Extract the values of the desired attribute from
396 * the ancestor entry */
397 rc = backend_attribute( op, NULL, &ci->ci_dn,
398 ci->ci_ad[idx], &vals, ACL_READ );
400 /* If there are any values, merge them into the
401 * current search result
404 attr_merge( rs->sr_entry, ci->ci_ad[idx],
406 ber_bvarray_free_x( vals, op->o_tmpmemctx );
412 /* Default is to just fall through to the normal processing */
413 return SLAP_CB_CONTINUE;
416 static slap_overinst collect;
418 int collect_initialize() {
421 collect.on_bi.bi_type = "collect";
422 collect.on_bi.bi_db_destroy = collect_destroy;
423 collect.on_bi.bi_op_modify = collect_modify;
424 collect.on_response = collect_response;
426 collect.on_bi.bi_cf_ocs = collectocs;
427 code = config_register_schema( collectcfg, collectocs );
428 if ( code ) return code;
430 return overlay_register( &collect );
433 #if SLAPD_OVER_COLLECT == SLAPD_MOD_DYNAMIC
434 int init_module(int argc, char *argv[]) {
435 return collect_initialize();
439 #endif /* SLAPD_OVER_COLLECT */