]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/collect.c
f01045a958b7adf73adee725e8b527b7c86f3fe6
[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 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 /*
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
56  */
57 static void
58 insert_ordered( slap_overinst *on, collect_info *ci ) {
59         collect_info *find = on->on_bi.bi_private;
60         collect_info *prev = NULL;
61         int found = 0;
62
63         while (!found) {
64                 if (find == NULL) {
65                         if (prev == NULL) {
66                                 /* base case - empty list */
67                                 on->on_bi.bi_private = ci;
68                                 ci->ci_next = NULL;
69                         } else {
70                                 /* final case - end of list */
71                                 prev->ci_next = ci;
72                                 ci->ci_next = NULL;
73                         }
74                         found = 1;
75                 } else if (find->ci_dn.bv_len <= ci->ci_dn.bv_len) { 
76                         /* insert into list here */
77                         if (prev == NULL) {
78                                 /* entry is head of list */
79                                 ci->ci_next = on->on_bi.bi_private;
80                                 on->on_bi.bi_private = ci;
81                         } else {
82                                 /* entry is not head of list */
83                                 prev->ci_next = ci;
84                                 ci->ci_next = find;
85                         }
86                         found = 1;
87                 } else {
88                         /* keep looking */
89                         prev = find;
90                         find = find->ci_next;
91                 }
92         }
93 }
94
95 static int
96 collect_cf( ConfigArgs *c )
97 {
98         slap_overinst *on = (slap_overinst *)c->bi;
99         int rc = 1, idx;
100
101         switch( c->op ) {
102         case SLAP_CONFIG_EMIT:
103                 {
104                 collect_info *ci;
105                 for ( ci = on->on_bi.bi_private; ci; ci = ci->ci_next ) {
106                         struct berval bv;
107                         char *ptr;
108                         int len;
109
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)) { 
115                                         bv.bv_len++;
116                                 }
117                         }
118                         bv.bv_val = ch_malloc( bv.bv_len + 1 );
119
120                         /* copy the value and update len */
121                         len = snprintf( bv.bv_val, bv.bv_len + 1, "\"%s\" ", 
122                                 ci->ci_dn.bv_val);
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)) {
129                                         *ptr++ = ',';
130                                 }
131                         }
132                         *ptr = '\0';
133                         bv.bv_len = ptr - bv.bv_val;
134
135                         ber_bvarray_add( &c->rvalue_vals, &bv );
136                         rc = 0;
137                 }
138                 }
139                 break;
140         case LDAP_MOD_DELETE:
141                 if ( c->valx == -1 ) {
142                 /* Delete entire attribute */
143                         collect_info *ci;
144                         while (( ci = on->on_bi.bi_private )) {
145                                 on->on_bi.bi_private = ci->ci_next;
146                                 ch_free( ci->ci_dn.bv_val );
147                                 ch_free( ci );
148                         }
149                 } else {
150                 /* Delete just one value */
151                         collect_info **cip, *ci;
152                         int i;
153                         cip = (collect_info **)&on->on_bi.bi_private;
154                         for ( i=0; i <= c->valx; i++, cip = &ci->ci_next ) ci = *cip;
155                         *cip = ci->ci_next;
156                         ch_free( ci->ci_dn.bv_val );
157                         ch_free( ci );
158                 }
159                 rc = 0;
160                 break;
161         case SLAP_CONFIG_ADD:
162         case LDAP_MOD_ADD:
163                 {
164                 collect_info *ci;
165                 struct berval bv, dn;
166                 const char *text;
167                 int idx, count=0;
168                 char *arg;
169
170                 /* count delimiters in attribute argument */
171                 arg = strtok(c->argv[2], ",");
172                 while (arg!=NULL) {
173                         count++;
174                         arg = strtok(NULL, ",");
175                 }
176
177                 /* allocate config info with room for attribute array */
178                 ci = ch_malloc( sizeof( collect_info ) +
179                         sizeof( AttributeDescription * ) * count );
180
181                 /* validate and normalize dn */
182                 ber_str2bv( c->argv[1], 0, 0, &bv );
183                 if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL ) ) {
184                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s invalid DN: \"%s\"",
185                                 c->argv[0], c->argv[1] );
186                         Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
187                                 "%s: %s\n", c->log, c->cr_msg, 0 );
188                         return ARG_BAD_CONF;
189                 }
190
191                 /* load attribute description for attribute list */
192                 arg = c->argv[2];
193                 for( idx=0; idx<count; idx++) {
194                         ci->ci_ad[idx] = NULL;
195
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\"",
199                                         c->argv[0], arg);
200                                 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
201                                         "%s: %s\n", c->log, c->cr_msg, 0 );
202                                 return ARG_BAD_CONF;
203                         }
204                         while(*arg!='\0') {
205                                 arg++; /* skip to end of argument */
206                         }
207                         if (idx<count-1) {
208                                 arg++; /* skip inner delimiters */
209                         }
210                 }
211
212                 /* The on->on_bi.bi_private pointer can be used for
213                  * anything this instance of the overlay needs.
214                  */
215                 ci->ci_ad[count] = NULL;
216                 ci->ci_ad_num = count;
217                 ci->ci_dn = dn;
218
219                 /* creates list of ci's ordered by dn length */ 
220                 insert_ordered ( on, ci );
221
222                 rc = 0;
223                 }
224         }
225         return rc;
226 }
227
228 static ConfigTable collectcfg[] = {
229         { "collectinfo", "dn> <attribute", 3, 3, 0,
230           ARG_MAGIC, collect_cf,
231           "( OLcfgOvAt:19.1 NAME 'olcCollectInfo' "
232           "DESC 'DN of entry and attribute to distribute' "
233           "SYNTAX OMsDirectoryString )", NULL, NULL },
234         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
235 };
236
237 static ConfigOCs collectocs[] = {
238         { "( OLcfgOvOc:19.1 "
239           "NAME 'olcCollectConfig' "
240           "DESC 'Collective Attribute configuration' "
241           "SUP olcOverlayConfig "
242           "MAY olcCollectInfo )",
243           Cft_Overlay, collectcfg },
244         { NULL, 0, NULL }
245 };
246
247 static int
248 collect_destroy(
249         BackendDB *be,
250         ConfigReply *cr
251 )
252 {
253         slap_overinst *on = (slap_overinst *)be->bd_info;
254         collect_info *ci;
255
256         while (( ci = on->on_bi.bi_private )) {
257                 on->on_bi.bi_private = ci->ci_next;
258                 ch_free( ci->ci_dn.bv_val );
259                 ch_free( ci );
260         }
261         return 0;
262 }
263
264 static int
265 collect_modify( Operation *op, SlapReply *rs)
266 {
267         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
268         collect_info *ci = on->on_bi.bi_private;
269         Modifications *ml;
270         char errMsg[100];
271         int idx;
272
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 */
278                                 continue;
279                         }
280
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 */
284                                 continue;
285                         }
286
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 );
296                                         return rs->sr_err;
297                                 }
298                         }
299                 }
300
301         }
302
303         return SLAP_CB_CONTINUE;
304 }
305
306 static int
307 collect_response( Operation *op, SlapReply *rs )
308 {
309         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
310         collect_info *ci = on->on_bi.bi_private;
311
312         /* If we've been configured and the current response is
313          * a search entry
314          */
315         if ( ci && rs->sr_type == REP_SEARCH ) {
316                 int rc;
317
318                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
319
320                 for (; ci; ci=ci->ci_next ) {
321                         int idx=0;
322
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 */
326                                 continue;
327                         }
328
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 */
332                                 continue;
333                         }
334
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.
338                         */
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;
343                         }
344
345                         /* Loop for each attribute in this collectinfo */
346                         for(idx=0; idx<ci->ci_ad_num; idx++) {
347                                 BerVarray vals = NULL;
348
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 );
353
354                                 /* If there are any values, merge them into the
355                                  * current search result
356                                  */
357                                 if ( vals ) {
358                                         attr_merge( rs->sr_entry, ci->ci_ad[idx], 
359                                                 vals, NULL );
360                                         ber_bvarray_free_x( vals, op->o_tmpmemctx );
361                                 }
362                         }
363                 }
364         }
365
366         /* Default is to just fall through to the normal processing */
367         return SLAP_CB_CONTINUE;
368 }
369
370 static slap_overinst collect;
371
372 int collect_initialize() {
373         int code;
374
375         collect.on_bi.bi_type = "collect";
376         collect.on_bi.bi_db_destroy = collect_destroy;
377         collect.on_bi.bi_op_modify = collect_modify;
378         collect.on_response = collect_response;
379
380         collect.on_bi.bi_cf_ocs = collectocs;
381         code = config_register_schema( collectcfg, collectocs );
382         if ( code ) return code;
383
384         return overlay_register( &collect );
385 }
386
387 #if SLAPD_OVER_COLLECT == SLAPD_MOD_DYNAMIC
388 int init_module(int argc, char *argv[]) {
389         return collect_initialize();
390 }
391 #endif
392
393 #endif /* SLAPD_OVER_COLLECT */