]> git.sur5r.net Git - openldap/blob - servers/slapd/backglue.c
Berkeley DB 4.2 support (DB 4.2 required by default)
[openldap] / servers / slapd / backglue.c
1 /* backglue.c - backend glue routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 2001-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 /*
9  * Functions to glue a bunch of other backends into a single tree.
10  * All of the glued backends must share a common suffix. E.g., you
11  * can glue o=foo and ou=bar,o=foo but you can't glue o=foo and o=bar.
12  *
13  * This uses the backend structures and routines extensively, but is
14  * not an actual backend of its own. To use it you must add a "subordinate"
15  * keyword to the configuration of other backends. Subordinates will
16  * automatically be connected to their parent backend.
17  *
18  * The purpose of these functions is to allow you to split a single database
19  * into pieces (for load balancing purposes, whatever) but still be able
20  * to treat it as a single database after it's been split. As such, each
21  * of the glued backends should have identical rootdn and rootpw.
22  *
23  * If you need more elaborate configuration, you probably should be using
24  * back-meta instead.
25  *  -- Howard Chu
26  */
27
28 #include "portable.h"
29
30 #include <stdio.h>
31
32 #include <ac/string.h>
33 #include <ac/socket.h>
34
35 #define SLAPD_TOOLS
36 #include "slap.h"
37
38 typedef struct gluenode {
39         BackendDB *be;
40         struct berval pdn;
41 } gluenode;
42
43 typedef struct glueinfo {
44         BackendInfo bi;
45         BackendDB bd;
46         int nodes;
47         gluenode n[1];
48 } glueinfo;
49
50 static int glueMode;
51 static BackendDB *glueBack;
52
53 static slap_response glue_back_response;
54
55 /* Just like select_backend, but only for our backends */
56 static BackendDB *
57 glue_back_select (
58         BackendDB *be,
59         const char *dn
60 )
61 {
62         glueinfo *gi = (glueinfo *) be->bd_info;
63         struct berval bv;
64         int i;
65
66         bv.bv_len = strlen(dn);
67         bv.bv_val = (char *) dn;
68
69         for (i = 0; i<gi->nodes; i++) {
70                 if (dnIsSuffix(&bv, &gi->n[i].be->be_nsuffix[0])) {
71                         return gi->n[i].be;
72                 }
73         }
74         return NULL;
75 }
76
77 /* This function will only be called in tool mode */
78 static int
79 glue_back_open (
80         BackendInfo *bi
81 )
82 {
83         int rc = 0;
84         static int glueOpened = 0;
85
86         if (glueOpened) return 0;
87
88         glueOpened = 1;
89
90         /* If we were invoked in tool mode, open all the underlying backends */
91         if (slapMode & SLAP_TOOL_MODE) {
92                 rc = backend_startup (NULL);
93         } /* other case is impossible */
94         return rc;
95 }
96
97 /* This function will only be called in tool mode */
98 static int
99 glue_back_close (
100         BackendInfo *bi
101 )
102 {
103         static int glueClosed = 0;
104         int rc = 0;
105
106         if (glueClosed) return 0;
107
108         glueClosed = 1;
109
110         if (slapMode & SLAP_TOOL_MODE) {
111                 rc = backend_shutdown (NULL);
112         }
113         return rc;
114 }
115
116 static int
117 glue_back_db_open (
118         BackendDB *be
119 )
120 {
121         glueinfo *gi = (glueinfo *) be->bd_info;
122         static int glueOpened = 0;
123         int rc = 0;
124
125         if (glueOpened) return 0;
126
127         glueOpened = 1;
128
129         gi->bd.be_acl = be->be_acl;
130
131         if (gi->bd.bd_info->bi_db_open)
132                 rc = gi->bd.bd_info->bi_db_open(&gi->bd);
133
134         return rc;
135 }
136
137 static int
138 glue_back_db_close (
139         BackendDB *be
140 )
141 {
142         glueinfo *gi = (glueinfo *) be->bd_info;
143         static int glueClosed = 0;
144
145         if (glueClosed) return 0;
146
147         glueClosed = 1;
148
149         /* Close the master */
150         if (gi->bd.bd_info->bi_db_close)
151                 gi->bd.bd_info->bi_db_close( &gi->bd );
152
153         return 0;
154 }
155
156 static int
157 glue_back_db_destroy (
158         BackendDB *be
159 )
160 {
161         glueinfo *gi = (glueinfo *) be->bd_info;
162
163         if (gi->bd.bd_info->bi_db_destroy)
164                 gi->bd.bd_info->bi_db_destroy( &gi->bd );
165         free (gi);
166         return 0;
167 }
168
169 typedef struct glue_state {
170         int err;
171         int matchlen;
172         char *matched;
173         int nrefs;
174         BerVarray refs;
175         slap_callback *prevcb;
176 } glue_state;
177
178 static int
179 glue_back_response ( Operation *op, SlapReply *rs )
180 {
181         glue_state *gs = op->o_callback->sc_private;
182         slap_callback *tmp = op->o_callback;
183
184         switch(rs->sr_type) {
185         case REP_SEARCH:
186         case REP_SEARCHREF:
187                 op->o_callback = gs->prevcb;
188                 if (op->o_callback && op->o_callback->sc_response) {
189                         rs->sr_err = op->o_callback->sc_response( op, rs );
190                 } else rs->sr_err = SLAP_CB_CONTINUE;
191                 op->o_callback = tmp;
192                 return rs->sr_err;
193
194         default:
195                 if (rs->sr_err == LDAP_SUCCESS || gs->err != LDAP_SUCCESS)
196                         gs->err = rs->sr_err;
197                 if (gs->err == LDAP_SUCCESS && gs->matched) {
198                         ch_free (gs->matched);
199                         gs->matched = NULL;
200                         gs->matchlen = 0;
201                 }
202                 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
203                         int len;
204                         len = strlen (rs->sr_matched);
205                         if (len > gs->matchlen) {
206                                 if (gs->matched)
207                                         ch_free (gs->matched);
208                                 gs->matched = ch_strdup (rs->sr_matched);
209                                 gs->matchlen = len;
210                         }
211                 }
212                 if (rs->sr_ref) {
213                         int i, j, k;
214                         BerVarray new;
215
216                         for (i=0; rs->sr_ref[i].bv_val; i++);
217
218                         j = gs->nrefs;
219                         if (!j) {
220                                 new = ch_malloc ((i+1)*sizeof(struct berval));
221                         } else {
222                                 new = ch_realloc(gs->refs,
223                                         (j+i+1)*sizeof(struct berval));
224                         }
225                         for (k=0; k<i; j++,k++) {
226                                 ber_dupbv( &new[j], &rs->sr_ref[k] );
227                         }
228                         new[j].bv_val = NULL;
229                         gs->nrefs = j;
230                         gs->refs = new;
231                 }
232         }
233         return 0;
234 }
235
236 static int
237 glue_back_search ( Operation *op, SlapReply *rs )
238 {
239         BackendDB *b0 = op->o_bd;
240         glueinfo *gi = (glueinfo *) b0->bd_info;
241         int i;
242         long stoptime = 0;
243         glue_state gs = {0, 0, NULL, 0, NULL, NULL};
244         slap_callback cb = { glue_back_response, NULL };
245         int scope0, slimit0, tlimit0;
246         struct berval dn, ndn;
247
248         cb.sc_private = &gs;
249
250         gs.prevcb = op->o_callback;
251
252         if (op->ors_tlimit) {
253                 stoptime = slap_get_time () + op->ors_tlimit;
254         }
255
256         switch (op->ors_scope) {
257         case LDAP_SCOPE_BASE:
258                 op->o_bd = glue_back_select (b0, op->o_req_ndn.bv_val);
259
260                 if (op->o_bd && op->o_bd->be_search) {
261                         rs->sr_err = op->o_bd->be_search( op, rs );
262                 } else {
263                         send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
264                                       "No search target found");
265                 }
266                 return rs->sr_err;
267
268         case LDAP_SCOPE_ONELEVEL:
269         case LDAP_SCOPE_SUBTREE:
270                 op->o_callback = &cb;
271                 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
272                 scope0 = op->ors_scope;
273                 slimit0 = op->ors_slimit;
274                 tlimit0 = op->ors_tlimit;
275                 dn = op->o_req_dn;
276                 ndn = op->o_req_ndn;
277
278                 /*
279                  * Execute in reverse order, most general first 
280                  */
281                 for (i = gi->nodes-1; i >= 0; i--) {
282                         if (!gi->n[i].be || !gi->n[i].be->be_search)
283                                 continue;
284                         if (tlimit0) {
285                                 op->ors_tlimit = stoptime - slap_get_time ();
286                                 if (op->ors_tlimit <= 0) {
287                                         rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
288                                         break;
289                                 }
290                         }
291                         if (slimit0) {
292                                 op->ors_slimit = slimit0 - rs->sr_nentries;
293                                 if (op->ors_slimit <= 0) {
294                                         rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
295                                         break;
296                                 }
297                         }
298                         rs->sr_err = 0;
299                         /*
300                          * check for abandon 
301                          */
302                         if (op->o_abandon) {
303                                 goto done;
304                         }
305                         op->o_bd = gi->n[i].be;
306                         if (scope0 == LDAP_SCOPE_ONELEVEL && 
307                                 dn_match(&gi->n[i].pdn, &ndn)) {
308                                 op->ors_scope = LDAP_SCOPE_BASE;
309                                 op->o_req_dn = op->o_bd->be_suffix[0];
310                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
311                                 rs->sr_err = op->o_bd->be_search(op, rs);
312
313                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
314                                 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn)) {
315                                 op->o_req_dn = op->o_bd->be_suffix[0];
316                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
317                                 rs->sr_err = op->o_bd->be_search( op, rs );
318
319                         } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
320                                 rs->sr_err = op->o_bd->be_search( op, rs );
321                         }
322
323                         switch ( gs.err ) {
324
325                         /*
326                          * Add errors that should result in dropping
327                          * the search
328                          */
329                         case LDAP_SIZELIMIT_EXCEEDED:
330                         case LDAP_TIMELIMIT_EXCEEDED:
331                         case LDAP_ADMINLIMIT_EXCEEDED:
332                                 goto end_of_loop;
333                         
334                         default:
335                                 break;
336                         }
337                 }
338 end_of_loop:;
339                 op->ors_scope = scope0;
340                 op->ors_slimit = slimit0;
341                 op->ors_tlimit = tlimit0;
342                 op->o_req_dn = dn;
343                 op->o_req_ndn = ndn;
344
345                 break;
346         }
347         op->o_callback = gs.prevcb;
348         rs->sr_err = gs.err;
349         rs->sr_matched = gs.matched;
350         rs->sr_ref = gs.refs;
351
352         send_ldap_result( op, rs );
353
354 done:
355         op->o_bd = b0;
356         if (gs.matched)
357                 free (gs.matched);
358         if (gs.refs)
359                 ber_bvarray_free(gs.refs);
360         return rs->sr_err;
361 }
362
363
364 static int
365 glue_tool_entry_open (
366         BackendDB *b0,
367         int mode
368 )
369 {
370         /* We don't know which backend to talk to yet, so just
371          * remember the mode and move on...
372          */
373
374         glueMode = mode;
375         glueBack = NULL;
376
377         return 0;
378 }
379
380 static int
381 glue_tool_entry_close (
382         BackendDB *b0
383 )
384 {
385         int rc = 0;
386
387         if (glueBack) {
388                 if (!glueBack->be_entry_close)
389                         return 0;
390                 rc = glueBack->be_entry_close (glueBack);
391         }
392         return rc;
393 }
394
395 static ID
396 glue_tool_entry_first (
397         BackendDB *b0
398 )
399 {
400         glueinfo *gi = (glueinfo *) b0->bd_info;
401         int i;
402
403         /* If we're starting from scratch, start at the most general */
404         if (!glueBack) {
405                 for (i = gi->nodes-1; i >= 0; i--) {
406                         if (gi->n[i].be->be_entry_open &&
407                             gi->n[i].be->be_entry_first) {
408                                 glueBack = gi->n[i].be;
409                                 break;
410                         }
411                 }
412
413         }
414         if (!glueBack || glueBack->be_entry_open (glueBack, glueMode) != 0)
415                 return NOID;
416
417         return glueBack->be_entry_first (glueBack);
418 }
419
420 static ID
421 glue_tool_entry_next (
422         BackendDB *b0
423 )
424 {
425         glueinfo *gi = (glueinfo *) b0->bd_info;
426         int i;
427         ID rc;
428
429         if (!glueBack || !glueBack->be_entry_next)
430                 return NOID;
431
432         rc = glueBack->be_entry_next (glueBack);
433
434         /* If we ran out of entries in one database, move on to the next */
435         if (rc == NOID) {
436                 glueBack->be_entry_close (glueBack);
437                 for (i=0; i<gi->nodes; i++) {
438                         if (gi->n[i].be == glueBack)
439                                 break;
440                 }
441                 if (i == 0) {
442                         glueBack = NULL;
443                         rc = NOID;
444                 } else {
445                         glueBack = gi->n[i-1].be;
446                         rc = glue_tool_entry_first (b0);
447                 }
448         }
449         return rc;
450 }
451
452 static Entry *
453 glue_tool_entry_get (
454         BackendDB *b0,
455         ID id
456 )
457 {
458         if (!glueBack || !glueBack->be_entry_get)
459                 return NULL;
460
461         return glueBack->be_entry_get (glueBack, id);
462 }
463
464 static ID
465 glue_tool_entry_put (
466         BackendDB *b0,
467         Entry *e,
468         struct berval *text
469 )
470 {
471         BackendDB *be;
472         int rc;
473
474         be = glue_back_select (b0, e->e_ndn);
475         if (!be->be_entry_put)
476                 return NOID;
477
478         if (!glueBack) {
479                 rc = be->be_entry_open (be, glueMode);
480                 if (rc != 0)
481                         return NOID;
482         } else if (be != glueBack) {
483                 /* If this entry belongs in a different branch than the
484                  * previous one, close the current database and open the
485                  * new one.
486                  */
487                 glueBack->be_entry_close (glueBack);
488                 rc = be->be_entry_open (be, glueMode);
489                 if (rc != 0)
490                         return NOID;
491         }
492         glueBack = be;
493         return be->be_entry_put (be, e, text);
494 }
495
496 static int
497 glue_tool_entry_reindex (
498         BackendDB *b0,
499         ID id
500 )
501 {
502         if (!glueBack || !glueBack->be_entry_reindex)
503                 return -1;
504
505         return glueBack->be_entry_reindex (glueBack, id);
506 }
507
508 static int
509 glue_tool_sync (
510         BackendDB *b0
511 )
512 {
513         glueinfo *gi = (glueinfo *) b0->bd_info;
514         int i;
515
516         /* just sync everyone */
517         for (i = 0; i<gi->nodes; i++)
518                 if (gi->n[i].be->be_sync)
519                         gi->n[i].be->be_sync (gi->n[i].be);
520         return 0;
521 }
522
523 int
524 glue_sub_init( )
525 {
526         int i, j;
527         int cont = num_subordinates;
528         BackendDB *b1, *be;
529         BackendInfo *bi = NULL;
530         glueinfo *gi;
531
532         /* While there are subordinate backends, search backwards through the
533          * backends and connect them to their superior.
534          */
535         for (i = nBackendDB - 1, b1=&backendDB[i]; cont && i>=0; b1--,i--) {
536                 if (SLAP_GLUE_SUBORDINATE ( b1 ) ) {
537                         /* The last database cannot be a subordinate of noone */
538                         if (i == nBackendDB - 1) {
539                                 b1->be_flags ^= SLAP_BFLAG_GLUE_SUBORDINATE;
540                         }
541                         continue;
542                 }
543                 gi = NULL;
544                 for (j = i-1, be=&backendDB[j]; j>=0; be--,j--) {
545                         if ( ! SLAP_GLUE_SUBORDINATE( be ) ) {
546                                 continue;
547                         }
548                         /* We will only link it once */
549                         if ( SLAP_GLUE_LINKED( be ) ) {
550                                 continue;
551                         }
552                         if (!dnIsSuffix(&be->be_nsuffix[0], &b1->be_nsuffix[0])) {
553                                 continue;
554                         }
555                         cont--;
556                         be->be_flags |= SLAP_BFLAG_GLUE_LINKED;
557                         if (gi == NULL) {
558                                 /* We create a copy of the superior's be
559                                  * structure, pointing to all of its original
560                                  * information. Then we replace elements of
561                                  * the superior's info with our own. The copy
562                                  * is used whenever we have operations to pass
563                                  * down to the real database.
564                                  */
565                                 b1->be_flags |= SLAP_BFLAG_GLUE_INSTANCE;
566                                 gi = (glueinfo *)ch_malloc(sizeof(glueinfo));
567                                 gi->nodes = 0;
568                                 gi->bd = *b1;
569                                 gi->bi = *b1->bd_info;
570                                 bi = (BackendInfo *)gi;
571                                 bi->bi_open = glue_back_open;
572                                 bi->bi_close = glue_back_close;
573                                 bi->bi_db_open = glue_back_db_open;
574                                 bi->bi_db_close = glue_back_db_close;
575                                 bi->bi_db_destroy = glue_back_db_destroy;
576
577                                 bi->bi_op_search = glue_back_search;
578
579                                 /*
580                                  * hooks for slap tools
581                                  */
582                                 bi->bi_tool_entry_open = glue_tool_entry_open;
583                                 bi->bi_tool_entry_close = glue_tool_entry_close;
584                                 bi->bi_tool_entry_first = glue_tool_entry_first;
585                                 bi->bi_tool_entry_next = glue_tool_entry_next;
586                                 bi->bi_tool_entry_get = glue_tool_entry_get;
587                                 bi->bi_tool_entry_put = glue_tool_entry_put;
588                                 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
589                                 bi->bi_tool_sync = glue_tool_sync;
590                                 /* FIXME : will support later */
591                                 bi->bi_tool_dn2id_get = 0;
592                                 bi->bi_tool_id2entry_get = 0;
593                                 bi->bi_tool_entry_modify = 0;
594                         } else {
595                                 gi = (glueinfo *)ch_realloc(gi,
596                                         sizeof(glueinfo) +
597                                         gi->nodes * sizeof(gluenode));
598                         }
599                         gi->n[gi->nodes].be = be;
600                         dnParent( &be->be_nsuffix[0], &gi->n[gi->nodes].pdn ); 
601                         gi->nodes++;
602                 }
603                 if (gi) {
604                         /* One more node for the master */
605                         gi = (glueinfo *)ch_realloc(gi,
606                                 sizeof(glueinfo) + gi->nodes * sizeof(gluenode));
607                         gi->n[gi->nodes].be = &gi->bd;
608                         dnParent( &b1->be_nsuffix[0], &gi->n[gi->nodes].pdn );
609                         gi->nodes++;
610                         b1->bd_info = (BackendInfo *)gi;
611                 }
612         }
613         /* If there are any unresolved subordinates left, something is wrong */
614         return cont;
615 }