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