1 /* collect.c - Demonstration of overlay code */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2005 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_response( Operation *op, SlapReply *rs )
52 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
53 collect_info *ci = on->on_bi.bi_private;
55 /* If we've been configured and the current response is
58 if ( ci && rs->sr_type == REP_SEARCH ) {
62 op->o_bd->bd_info = (BackendInfo *)on->on_info;
64 for (; ci; ci=ci->ci_next ) {
65 BerVarray vals = NULL;
67 /* Is our configured entry an ancestor of this one? */
68 rc = rs->sr_entry->e_nname.bv_len - ci->ci_dn.bv_len;
69 if ( rc < 1 || strcmp( rs->sr_entry->e_nname.bv_val + rc,
70 ci->ci_dn.bv_val )) continue;
72 /* Extract the values of the desired attribute from
75 rc = backend_attribute( op, NULL, &ci->ci_dn, ci->ci_ad, &vals, ACL_READ );
77 /* If there are any values, merge them into the
81 /* The current entry may live in a cache, so
82 * don't modify it directly. Make a copy and
83 * work with that instead.
86 new = entry_dup( rs->sr_entry );
88 attr_merge( new, ci->ci_ad, vals, NULL );
89 ber_bvarray_free_x( vals, op->o_tmpmemctx );
95 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
98 /* Default is to just fall through to the normal processing */
99 return SLAP_CB_CONTINUE;
102 static int collect_config(
110 slap_overinst *on = (slap_overinst *) be->bd_info;
111 AttributeDescription *ad = NULL;
113 /* The config syntax is "collectinfo <dn> <attribute-description>"
114 * and only one directive may be specified per overlay instance.
117 if ( strcasecmp( argv[0], "collectinfo" ) == 0 ) {
119 struct berval bv, dn;
122 Debug( LDAP_DEBUG_ANY,
123 "%s: line %d: argument missing in \"collectinfo <dn> <attribute-description>\" line.\n",
127 ber_str2bv( argv[1], 0, 0, &bv );
128 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL ) ) {
129 Debug( LDAP_DEBUG_ANY,
130 "%s: line %d: invalid DN in \"collectinfo\" line: %s.\n",
131 fname, lineno, text );
134 if ( slap_str2ad( argv[2], &ad, &text ) ) {
135 Debug( LDAP_DEBUG_ANY,
136 "%s: line %d: attribute description unknown in \"collectinfo\" line: %s.\n",
137 fname, lineno, text );
141 /* The on->on_bi.bi_private pointer can be used for
142 * anything this instance of the overlay needs.
144 ci = ch_malloc( sizeof( collect_info ));
147 ci->ci_next = on->on_bi.bi_private;
148 on->on_bi.bi_private = ci;
151 return SLAP_CONF_UNKNOWN;
154 static slap_overinst collect;
157 collect.on_bi.bi_type = "collect";
158 collect.on_bi.bi_db_config = collect_config;
159 collect.on_response = collect_response;
161 return overlay_register( &collect );
164 #if SLAPD_OVER_COLLECT == SLAPD_MOD_DYNAMIC
165 int init_module(int argc, char *argv[]) {
166 return collect_init();
170 #endif /* SLAPD_OVER_COLLECT */