1
1
/* collect.c - Demonstration of overlay code */
2
/* $OpenLDAP: pkg/ldap/servers/slapd/overlays/collect.c,v 1.5.2.4 2008/02/11 23:26:48 kurt Exp $ */
2
/* $OpenLDAP: pkg/ldap/servers/slapd/overlays/collect.c,v 1.5.2.8 2009/01/22 00:01:12 kurt Exp $ */
3
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5
* Copyright 2003-2008 The OpenLDAP Foundation.
5
* Copyright 2003-2009 The OpenLDAP Foundation.
6
6
* Portions Copyright 2003 Howard Chu.
7
7
* All rights reserved.
43
45
typedef struct collect_info {
44
46
struct collect_info *ci_next;
45
47
struct berval ci_dn;
46
AttributeDescription *ci_ad;
49
AttributeDescription *ci_ad[1];
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 */
50
96
collect_cf( ConfigArgs *c )
52
98
slap_overinst *on = (slap_overinst *)c->bi;
56
102
case SLAP_CONFIG_EMIT:
59
105
for ( ci = on->on_bi.bi_private; ci; ci = ci->ci_next ) {
63
bv.bv_len = ci->ci_dn.bv_len + STRLENOF("\"\" ") +
64
ci->ci_ad->ad_cname.bv_len;
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)) {
65
118
bv.bv_val = ch_malloc( bv.bv_len + 1 );
66
len = snprintf( bv.bv_val, bv.bv_len + 1, "\"%s\" %s",
67
ci->ci_dn.bv_val, ci->ci_ad->ad_cname.bv_val );
68
assert( len == bv.bv_len );
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;
69
135
ber_bvarray_add( &c->rvalue_vals, &bv );
99
165
struct berval bv, dn;
100
166
const char *text;
101
AttributeDescription *ad = NULL;
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 );
181
/* validate and normalize dn */
103
182
ber_str2bv( c->argv[1], 0, 0, &bv );
104
183
if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL ) ) {
105
184
snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s invalid DN: \"%s\"",
108
187
"%s: %s\n", c->log, c->cr_msg, 0 );
109
188
return ARG_BAD_CONF;
111
if ( slap_str2ad( c->argv[2], &ad, &text ) ) {
112
snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s attribute description unknown: \"%s\"",
113
c->argv[0], c->argv[2] );
114
Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
115
"%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 */
119
212
/* The on->on_bi.bi_private pointer can be used for
120
213
* anything this instance of the overlay needs.
122
ci = ch_malloc( sizeof( collect_info ));
215
ci->ci_ad[count] = NULL;
216
ci->ci_ad_num = count;
125
ci->ci_next = on->on_bi.bi_private;
126
on->on_bi.bi_private = ci;
219
/* creates list of ci's ordered by dn length */
220
insert_ordered ( on, ci );
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;
170
307
collect_response( Operation *op, SlapReply *rs )
172
309
slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
181
318
op->o_bd->bd_info = (BackendInfo *)on->on_info;
183
320
for (; ci; ci=ci->ci_next ) {
184
BerVarray vals = NULL;
186
/* Is our configured entry an ancestor of this one? */
187
if ( !dnIsSuffix( &rs->sr_entry->e_nname, &ci->ci_dn ))
190
/* Extract the values of the desired attribute from
193
rc = backend_attribute( op, NULL, &ci->ci_dn, ci->ci_ad, &vals, ACL_READ );
195
/* If there are any values, merge them into the
199
/* The current entry may live in a cache, so
200
* don't modify it directly. Make a copy and
201
* work with that instead.
203
if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE )) {
204
rs->sr_entry = entry_dup( rs->sr_entry );
205
rs->sr_flags |= REP_ENTRY_MODIFIABLE |
206
REP_ENTRY_MUSTBEFREED;
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 );
208
attr_merge( rs->sr_entry, ci->ci_ad, vals, NULL );
209
ber_bvarray_free_x( vals, op->o_tmpmemctx );
213
366
/* Default is to just fall through to the normal processing */
214
367
return SLAP_CB_CONTINUE;