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