1 /* collect.c - Demonstration of overlay code */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2003-2021 The OpenLDAP Foundation.
6 * Portions Copyright 2003 Howard Chu.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
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>.
16 */
17 /* ACKNOWLEDGEMENTS:
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software.
20 */
21
22 #include "portable.h"
23
24 #ifdef SLAPD_OVER_COLLECT
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "slap.h"
32 #include "config.h"
33
34 #include "lutil.h"
35
36 /* This is a cheap hack to implement a collective attribute.
37 *
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.
43 */
44
45 typedef struct collect_info {
46 struct collect_info *ci_next;
47 struct berval ci_dn;
48 int ci_ad_num;
49 AttributeDescription *ci_ad[1];
50 } collect_info;
51
52 static int collect_cf( ConfigArgs *c );
53
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 }
62 };
63
64 static ConfigOCs collectocs[] = {
65 { "( OLcfgOvOc:19.1 "
66 "NAME 'olcCollectConfig' "
67 "DESC 'Collective Attribute configuration' "
68 "SUP olcOverlayConfig "
69 "MAY olcCollectInfo )",
70 Cft_Overlay, collectcfg },
71 { NULL, 0, NULL }
72 };
73
74 /*
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
78 */
79 static void
insert_ordered(slap_overinst * on,collect_info * ci)80 insert_ordered( slap_overinst *on, collect_info *ci ) {
81 collect_info *find = on->on_bi.bi_private;
82 collect_info *prev = NULL;
83 int found = 0;
84
85 while (!found) {
86 if (find == NULL) {
87 if (prev == NULL) {
88 /* base case - empty list */
89 on->on_bi.bi_private = ci;
90 ci->ci_next = NULL;
91 } else {
92 /* final case - end of list */
93 prev->ci_next = ci;
94 ci->ci_next = NULL;
95 }
96 found = 1;
97 } else if (find->ci_dn.bv_len < ci->ci_dn.bv_len) {
98 /* insert into list here */
99 if (prev == NULL) {
100 /* entry is head of list */
101 ci->ci_next = on->on_bi.bi_private;
102 on->on_bi.bi_private = ci;
103 } else {
104 /* entry is not head of list */
105 prev->ci_next = ci;
106 ci->ci_next = find;
107 }
108 found = 1;
109 } else {
110 /* keep looking */
111 prev = find;
112 find = find->ci_next;
113 }
114 }
115 }
116
117 static int
collect_cf(ConfigArgs * c)118 collect_cf( ConfigArgs *c )
119 {
120 slap_overinst *on = (slap_overinst *)c->bi;
121 int rc = 1, idx;
122
123 switch( c->op ) {
124 case SLAP_CONFIG_EMIT:
125 {
126 collect_info *ci;
127 for ( ci = on->on_bi.bi_private; ci; ci = ci->ci_next ) {
128 struct berval bv;
129 char *ptr;
130 int len;
131
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)) {
137 bv.bv_len++;
138 }
139 }
140 bv.bv_val = ch_malloc( bv.bv_len + 1 );
141
142 /* copy the value and update len */
143 len = snprintf( bv.bv_val, bv.bv_len + 1, "\"%s\" ",
144 ci->ci_dn.bv_val);
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)) {
151 *ptr++ = ',';
152 }
153 }
154 *ptr = '\0';
155 bv.bv_len = ptr - bv.bv_val;
156
157 ber_bvarray_add( &c->rvalue_vals, &bv );
158 rc = 0;
159 }
160 }
161 break;
162 case LDAP_MOD_DELETE:
163 if ( c->valx == -1 ) {
164 /* Delete entire attribute */
165 collect_info *ci;
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 );
169 ch_free( ci );
170 }
171 } else {
172 /* Delete just one value */
173 collect_info **cip, *ci;
174 int i;
175 cip = (collect_info **)&on->on_bi.bi_private;
176 ci = *cip;
177 for ( i=0; i < c->valx; i++ ) {
178 cip = &ci->ci_next;
179 ci = *cip;
180 }
181 *cip = ci->ci_next;
182 ch_free( ci->ci_dn.bv_val );
183 ch_free( ci );
184 }
185 rc = 0;
186 break;
187 case SLAP_CONFIG_ADD:
188 case LDAP_MOD_ADD:
189 {
190 collect_info *ci;
191 struct berval bv, dn;
192 const char *text;
193 int idx, count=0;
194 char *arg;
195
196 /* count delimiters in attribute argument */
197 arg = strtok(c->argv[2], ",");
198 while (arg!=NULL) {
199 count++;
200 arg = strtok(NULL, ",");
201 }
202
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 );
210 return ARG_BAD_CONF;
211 }
212
213 /* check for duplicate DNs */
214 for ( ci = (collect_info *)on->on_bi.bi_private; ci;
215 ci = ci->ci_next ) {
216 /* If new DN is longest, there are no possible matches */
217 if ( dn.bv_len > ci->ci_dn.bv_len ) {
218 ci = NULL;
219 break;
220 }
221 if ( bvmatch( &dn, &ci->ci_dn )) {
222 break;
223 }
224 }
225 if ( ci ) {
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 );
230 return ARG_BAD_CONF;
231 }
232
233 /* allocate config info with room for attribute array */
234 ci = ch_malloc( sizeof( collect_info ) +
235 sizeof( AttributeDescription * ) * count );
236
237 /* load attribute description for attribute list */
238 arg = c->argv[2];
239 for( idx=0; idx<count; idx++) {
240 ci->ci_ad[idx] = NULL;
241
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\"",
245 c->argv[0], arg);
246 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
247 "%s: %s\n", c->log, c->cr_msg, 0 );
248 ch_free( ci );
249 return ARG_BAD_CONF;
250 }
251 while(*arg!='\0') {
252 arg++; /* skip to end of argument */
253 }
254 if (idx<count-1) {
255 arg++; /* skip inner delimiters */
256 }
257 }
258
259 /* The on->on_bi.bi_private pointer can be used for
260 * anything this instance of the overlay needs.
261 */
262 ci->ci_ad[count] = NULL;
263 ci->ci_ad_num = count;
264 ci->ci_dn = dn;
265
266 /* creates list of ci's ordered by dn length */
267 insert_ordered ( on, ci );
268
269 /* New ci wasn't simply appended to end, adjust its
270 * position in the config entry's a_vals
271 */
272 if ( c->ca_entry && ci->ci_next ) {
273 Attribute *a = attr_find( c->ca_entry->e_attrs,
274 collectcfg[0].ad );
275 if ( a ) {
276 struct berval bv, nbv;
277 collect_info *c2 = (collect_info *)on->on_bi.bi_private;
278 int i, j;
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];
285 }
286 a->a_vals[j] = bv;
287 a->a_nvals[j] = nbv;
288 }
289 }
290
291 rc = 0;
292 }
293 }
294 return rc;
295 }
296
297 static int
collect_destroy(BackendDB * be,ConfigReply * cr)298 collect_destroy(
299 BackendDB *be,
300 ConfigReply *cr
301 )
302 {
303 slap_overinst *on = (slap_overinst *)be->bd_info;
304 collect_info *ci;
305
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 );
309 ch_free( ci );
310 }
311 return 0;
312 }
313
314 static int
collect_modify(Operation * op,SlapReply * rs)315 collect_modify( Operation *op, SlapReply *rs)
316 {
317 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
318 collect_info *ci = on->on_bi.bi_private;
319 Modifications *ml;
320 char errMsg[100];
321 int idx;
322
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 */
328 continue;
329 }
330
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 */
334 continue;
335 }
336
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 );
346 return rs->sr_err;
347 }
348 }
349 }
350
351 }
352
353 return SLAP_CB_CONTINUE;
354 }
355
356 static int
collect_response(Operation * op,SlapReply * rs)357 collect_response( Operation *op, SlapReply *rs )
358 {
359 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
360 collect_info *ci = on->on_bi.bi_private;
361
362 /* If we've been configured and the current response is
363 * a search entry
364 */
365 if ( ci && rs->sr_type == REP_SEARCH ) {
366 int rc;
367
368 op->o_bd->bd_info = (BackendInfo *)on->on_info;
369
370 for (; ci; ci=ci->ci_next ) {
371 int idx=0;
372
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 */
376 continue;
377 }
378
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 */
382 continue;
383 }
384
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.
388 */
389 rs_entry2modifiable( op, rs, on );
390
391 /* Loop for each attribute in this collectinfo */
392 for(idx=0; idx<ci->ci_ad_num; idx++) {
393 BerVarray vals = NULL;
394
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 );
399
400 /* If there are any values, merge them into the
401 * current search result
402 */
403 if ( vals ) {
404 attr_merge_normalize( rs->sr_entry, ci->ci_ad[idx],
405 vals, op->o_tmpmemctx );
406 ber_bvarray_free_x( vals, op->o_tmpmemctx );
407 }
408 }
409 }
410 }
411
412 /* Default is to just fall through to the normal processing */
413 return SLAP_CB_CONTINUE;
414 }
415
416 static slap_overinst collect;
417
collect_initialize()418 int collect_initialize() {
419 int code;
420
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;
425
426 collect.on_bi.bi_cf_ocs = collectocs;
427 code = config_register_schema( collectcfg, collectocs );
428 if ( code ) return code;
429
430 return overlay_register( &collect );
431 }
432
433 #if SLAPD_OVER_COLLECT == SLAPD_MOD_DYNAMIC
init_module(int argc,char * argv[])434 int init_module(int argc, char *argv[]) {
435 return collect_initialize();
436 }
437 #endif
438
439 #endif /* SLAPD_OVER_COLLECT */
440