]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/collect.c
ITS#5659 "collect" overlay enhancements
[openldap] / servers / slapd / overlays / collect.c
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-2008 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 /* This is a cheap hack to implement a collective attribute.
35  *
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.
41  */
42
43 typedef struct collect_info {
44         struct collect_info *ci_next;
45         struct berval ci_dn;
46         int ci_ad_num;
47         AttributeDescription *ci_ad[];
48 } collect_info;
49
50 /*
51  * inserts a collect_info into on->on_bi.bi_private taking into account
52  * order. this means longer dn's (i.e. more specific dn's) will be found
53  * first when searching, allowing some limited overlap of dn's
54  */
55 static void
56 insert_ordered( slap_overinst *on, collect_info *ci ) {
57         collect_info *find = on->on_bi.bi_private;
58         collect_info *prev = NULL;
59         int found = 0;
60
61         while (!found) {
62                 if (find == NULL) {
63                         if (prev == NULL) {
64                                 /* base case - empty list */
65                                 on->on_bi.bi_private = ci;
66                                 ci->ci_next = NULL;
67                         } else {
68                                 /* final case - end of list */
69                                 prev->ci_next = ci;
70                                 ci->ci_next = NULL;
71                         }
72                         found = 1;
73                 } else if (find->ci_dn.bv_len <= ci->ci_dn.bv_len) { 
74                         /* insert into list here */
75                         if (prev == NULL) {
76                                 /* entry is head of list */
77                                 ci->ci_next = on->on_bi.bi_private;
78                                 on->on_bi.bi_private = ci;
79                         } else {
80                                 /* entry is not head of list */
81                                 prev->ci_next = ci;
82                                 ci->ci_next = find;
83                         }
84                         found = 1;
85                 } else {
86                         /* keep looking */
87                         prev = find;
88                         find = find->ci_next;
89                 }
90         }
91 }
92
93 static int
94 collect_cf( ConfigArgs *c )
95 {
96         slap_overinst *on = (slap_overinst *)c->bi;
97         int rc = 1, idx;
98
99         switch( c->op ) {
100         case SLAP_CONFIG_EMIT:
101                 {
102                 collect_info *ci;
103                 for ( ci = on->on_bi.bi_private; ci; ci = ci->ci_next ) {
104                         struct berval bv;
105                         char *ptr;
106                         int len;
107
108                         /* calculate the length & malloc memory */
109                         bv.bv_len = ci->ci_dn.bv_len + STRLENOF("\"\" ");
110                         for (idx=0; idx<ci->ci_ad_num; idx++) {
111                                 bv.bv_len += ci->ci_ad[idx]->ad_cname.bv_len;
112                                 if (idx<(ci->ci_ad_num-1)) { 
113                                         bv.bv_len++;
114                                 }
115                         }
116                         bv.bv_val = ch_malloc( bv.bv_len + 1 );
117
118                         /* copy the value and update len */
119                         len = snprintf( bv.bv_val, bv.bv_len + 1, "\"%s\" ", 
120                                 ci->ci_dn.bv_val);
121                         ptr = bv.bv_val + len;
122                         for (idx=0; idx<ci->ci_ad_num; idx++) {
123                                 ptr = lutil_strncopy( ptr,
124                                         ci->ci_ad[idx]->ad_cname.bv_val,
125                                         ci->ci_ad[idx]->ad_cname.bv_len);
126                                 if (idx<(ci->ci_ad_num-1)) {
127                                         *ptr++ = ',';
128                                 }
129                         }
130                         *ptr = '\0';
131                         bv.bv_len = ptr - bv.bv_val;
132
133                         ber_bvarray_add( &c->rvalue_vals, &bv );
134                         rc = 0;
135                 }
136                 }
137                 break;
138         case LDAP_MOD_DELETE:
139                 if ( c->valx == -1 ) {
140                 /* Delete entire attribute */
141                         collect_info *ci;
142                         while (( ci = on->on_bi.bi_private )) {
143                                 on->on_bi.bi_private = ci->ci_next;
144                                 ch_free( ci->ci_dn.bv_val );
145                                 ch_free( ci );
146                         }
147                 } else {
148                 /* Delete just one value */
149                         collect_info **cip, *ci;
150                         int i;
151                         cip = (collect_info **)&on->on_bi.bi_private;
152                         for ( i=0; i <= c->valx; i++, cip = &ci->ci_next ) ci = *cip;
153                         *cip = ci->ci_next;
154                         ch_free( ci->ci_dn.bv_val );
155                         ch_free( ci );
156                 }
157                 rc = 0;
158                 break;
159         case SLAP_CONFIG_ADD:
160         case LDAP_MOD_ADD:
161                 {
162                 collect_info *ci;
163                 struct berval bv, dn;
164                 const char *text;
165                 int idx, count=0;
166                 char *arg;
167
168                 /* count delimiters in attribute argument */
169                 arg = strtok(c->argv[2], ",");
170                 while (arg!=NULL) {
171                         count++;
172                         arg = strtok(NULL, ",");
173                 }
174
175                 /* allocate config info with room for attribute array */
176                 ci = ch_malloc( sizeof( collect_info ) +
177                         ( sizeof (AttributeDescription *) * (count + 1)));
178
179                 /* validate and normalize dn */
180                 ber_str2bv( c->argv[1], 0, 0, &bv );
181                 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL ) ) {
182                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s invalid DN: \"%s\"",
183                                 c->argv[0], c->argv[1] );
184                         Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
185                                 "%s: %s\n", c->log, c->cr_msg, 0 );
186                         return ARG_BAD_CONF;
187                 }
188
189                 /* load attribute description for attribute list */
190                 arg = c->argv[2];
191                 for( idx=0; idx<count; idx++) {
192                         ci->ci_ad[idx] = NULL;
193
194                         if ( slap_str2ad( arg, &ci->ci_ad[idx], &text ) ) {
195                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), 
196                                         "%s attribute description unknown: \"%s\"",
197                                         c->argv[0], arg);
198                                 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
199                                         "%s: %s\n", c->log, c->cr_msg, 0 );
200                                 return ARG_BAD_CONF;
201                         }
202                         while(*arg!='\0') {
203                                 arg++; /* skip to end of argument */
204                         }
205                         if (idx<count-1) {
206                                 arg++; /* skip inner delimiters */
207                         }
208                 }
209
210                 /* The on->on_bi.bi_private pointer can be used for
211                  * anything this instance of the overlay needs.
212                  */
213                 ci->ci_ad[count] = NULL;
214                 ci->ci_ad_num = count;
215                 ci->ci_dn = dn;
216
217                 /* creates list of ci's ordered by dn length */ 
218                 insert_ordered ( on, ci );
219
220                 rc = 0;
221                 }
222         }
223         return rc;
224 }
225
226 static ConfigTable collectcfg[] = {
227         { "collectinfo", "dn> <attribute", 3, 3, 0,
228           ARG_MAGIC, collect_cf,
229           "( OLcfgOvAt:19.1 NAME 'olcCollectInfo' "
230           "DESC 'DN of entry and attribute to distribute' "
231           "SYNTAX OMsDirectoryString )", NULL, NULL },
232         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
233 };
234
235 static ConfigOCs collectocs[] = {
236         { "( OLcfgOvOc:19.1 "
237           "NAME 'olcCollectConfig' "
238           "DESC 'Collective Attribute configuration' "
239           "SUP olcOverlayConfig "
240           "MAY olcCollectInfo )",
241           Cft_Overlay, collectcfg },
242         { NULL, 0, NULL }
243 };
244
245 static int
246 collect_destroy(
247         BackendDB *be,
248         ConfigReply *cr
249 )
250 {
251         slap_overinst *on = (slap_overinst *)be->bd_info;
252         collect_info *ci;
253
254         while (( ci = on->on_bi.bi_private )) {
255                 on->on_bi.bi_private = ci->ci_next;
256                 ch_free( ci->ci_dn.bv_val );
257                 ch_free( ci );
258         }
259         return 0;
260 }
261
262 static int
263 collect_modify( Operation *op, SlapReply *rs)
264 {
265         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
266         collect_info *ci = on->on_bi.bi_private;
267         Modifications *ml;
268         char errMsg[100];
269         int rc, idx;
270
271         for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next) {
272                 for (; ci; ci=ci->ci_next ) {
273                         /* Is this entry an ancestor of this collectinfo ? */
274                         if (!dnIsSuffix(&op->o_req_ndn, &ci->ci_dn)) {
275                                 /* this collectinfo does not match */
276                                 continue;
277                         }
278
279                         /* Is this entry the same as the template DN ? */
280                         if ( dn_match(&op->o_req_ndn, &ci->ci_dn)) {
281                                 /* all changes in this ci are allowed */
282                                 continue;
283                         }
284
285                         /* check for collect attributes - disallow modify if present */
286                         for(idx=0; idx<ci->ci_ad_num; idx++) {
287                                 if (ml->sml_desc == ci->ci_ad[idx]) {
288                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
289                                         snprintf( errMsg, sizeof( errMsg ), 
290                                                 "cannot change virtual attribute '%s'",
291                                                 ci->ci_ad[idx]->ad_cname.bv_val);
292                                         rs->sr_text = errMsg;
293                                         send_ldap_result( op, rs );
294                                         return rs->sr_err;
295                                 }
296                         }
297                 }
298
299         }
300
301         return SLAP_CB_CONTINUE;
302 }
303
304 static int
305 collect_response( Operation *op, SlapReply *rs )
306 {
307         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
308         collect_info *ci = on->on_bi.bi_private;
309
310         /* If we've been configured and the current response is
311          * a search entry
312          */
313         if ( ci && rs->sr_type == REP_SEARCH ) {
314                 int rc;
315
316                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
317
318                 for (; ci; ci=ci->ci_next ) {
319                         int idx=0;
320
321                         /* Is this entry an ancestor of this collectinfo ? */
322                         if (!dnIsSuffix(&rs->sr_entry->e_nname, &ci->ci_dn)) {
323                                 /* collectinfo does not match */
324                                 continue;
325                         }
326
327                         /* Is this entry the same as the template DN ? */
328                         if ( dn_match(&rs->sr_entry->e_nname, &ci->ci_dn)) {
329                                 /* dont apply change to parent */
330                                 continue;
331                         }
332
333                         /* The current entry may live in a cache, so
334                         * don't modify it directly. Make a copy and
335                         * work with that instead.
336                         */
337                         if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE )) {
338                                 rs->sr_entry = entry_dup( rs->sr_entry );
339                                 rs->sr_flags |= REP_ENTRY_MODIFIABLE |
340                                         REP_ENTRY_MUSTBEFREED;
341                         }
342
343                         /* Loop for each attribute in this collectinfo */
344                         for(idx=0; idx<ci->ci_ad_num; idx++) {
345                                 BerVarray vals = NULL;
346
347                                 /* Extract the values of the desired attribute from
348                                  * the ancestor entry */
349                                 rc = backend_attribute( op, NULL, &ci->ci_dn, 
350                                         ci->ci_ad[idx], &vals, ACL_READ );
351
352                                 /* If there are any values, merge them into the
353                                  * current search result
354                                  */
355                                 if ( vals ) {
356                                         attr_merge( rs->sr_entry, ci->ci_ad[idx], 
357                                                 vals, NULL );
358                                         ber_bvarray_free_x( vals, op->o_tmpmemctx );
359                                 }
360                         }
361                 }
362         }
363
364         /* Default is to just fall through to the normal processing */
365         return SLAP_CB_CONTINUE;
366 }
367
368 static slap_overinst collect;
369
370 int collect_initialize() {
371         int code;
372
373         collect.on_bi.bi_type = "collect";
374         collect.on_bi.bi_db_destroy = collect_destroy;
375         collect.on_bi.bi_op_modify = collect_modify;
376         collect.on_response = collect_response;
377
378         collect.on_bi.bi_cf_ocs = collectocs;
379         code = config_register_schema( collectcfg, collectocs );
380         if ( code ) return code;
381
382         return overlay_register( &collect );
383 }
384
385 #if SLAPD_OVER_COLLECT == SLAPD_MOD_DYNAMIC
386 int init_module(int argc, char *argv[]) {
387         return collect_initialize();
388 }
389 #endif
390
391 #endif /* SLAPD_OVER_COLLECT */