]> git.sur5r.net Git - openldap/blob - servers/slapd/backglue.c
Moved IDL cache code to standalone functions.
[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 if (rs->sr_type == REP_SEARCH) {
191                         rs->sr_err = send_search_entry( op, rs );
192                 } else {
193                         rs->sr_err = send_search_reference( op, rs );
194                 }
195                 op->o_callback = tmp;
196                 return rs->sr_err;
197
198         default:
199                 if (rs->sr_err == LDAP_SUCCESS || gs->err != LDAP_SUCCESS)
200                         gs->err = rs->sr_err;
201                 if (gs->err == LDAP_SUCCESS && gs->matched) {
202                         ch_free (gs->matched);
203                         gs->matched = NULL;
204                         gs->matchlen = 0;
205                 }
206                 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
207                         int len;
208                         len = strlen (rs->sr_matched);
209                         if (len > gs->matchlen) {
210                                 if (gs->matched)
211                                         ch_free (gs->matched);
212                                 gs->matched = ch_strdup (rs->sr_matched);
213                                 gs->matchlen = len;
214                         }
215                 }
216                 if (rs->sr_ref) {
217                         int i, j, k;
218                         BerVarray new;
219
220                         for (i=0; rs->sr_ref[i].bv_val; i++);
221
222                         j = gs->nrefs;
223                         if (!j) {
224                                 new = ch_malloc ((i+1)*sizeof(struct berval));
225                         } else {
226                                 new = ch_realloc(gs->refs,
227                                         (j+i+1)*sizeof(struct berval));
228                         }
229                         for (k=0; k<i; j++,k++) {
230                                 ber_dupbv( &new[j], &rs->sr_ref[k] );
231                         }
232                         new[j].bv_val = NULL;
233                         gs->nrefs = j;
234                         gs->refs = new;
235                 }
236         }
237 }
238
239 static int
240 glue_back_search ( Operation *op, SlapReply *rs )
241 {
242         BackendDB *b0 = op->o_bd;
243         glueinfo *gi = (glueinfo *) b0->bd_info;
244         int i;
245         long stoptime = 0;
246         glue_state gs = {0, 0, NULL, 0, NULL, NULL};
247         slap_callback cb = { glue_back_response };
248         int scope0, slimit0, tlimit0;
249         struct berval dn, ndn;
250
251         cb.sc_private = &gs;
252
253         gs.prevcb = op->o_callback;
254
255         if (op->ors_tlimit) {
256                 stoptime = slap_get_time () + op->ors_tlimit;
257         }
258
259         switch (op->ors_scope) {
260         case LDAP_SCOPE_BASE:
261                 op->o_bd = glue_back_select (b0, op->o_req_ndn.bv_val);
262
263                 if (op->o_bd && op->o_bd->be_search) {
264                         rs->sr_err = op->o_bd->be_search( op, rs );
265                 } else {
266                         send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
267                                       "No search target found");
268                 }
269                 return rs->sr_err;
270
271         case LDAP_SCOPE_ONELEVEL:
272         case LDAP_SCOPE_SUBTREE:
273                 op->o_callback = &cb;
274                 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
275                 scope0 = op->ors_scope;
276                 slimit0 = op->ors_slimit;
277                 tlimit0 = op->ors_tlimit;
278                 dn = op->o_req_dn;
279                 ndn = op->o_req_ndn;
280
281                 /*
282                  * Execute in reverse order, most general first 
283                  */
284                 for (i = gi->nodes-1; i >= 0; i--) {
285                         if (!gi->n[i].be || !gi->n[i].be->be_search)
286                                 continue;
287                         if (tlimit0) {
288                                 op->ors_tlimit = stoptime - slap_get_time ();
289                                 if (op->ors_tlimit <= 0) {
290                                         rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
291                                         break;
292                                 }
293                         }
294                         if (slimit0) {
295                                 op->ors_slimit = slimit0 - rs->sr_nentries;
296                                 if (op->ors_slimit <= 0) {
297                                         rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
298                                         break;
299                                 }
300                         }
301                         rs->sr_err = 0;
302                         /*
303                          * check for abandon 
304                          */
305                         if (op->o_abandon) {
306                                 goto done;
307                         }
308                         op->o_bd = gi->n[i].be;
309                         if (scope0 == LDAP_SCOPE_ONELEVEL && 
310                                 dn_match(&gi->n[i].pdn, &ndn)) {
311                                 op->ors_scope = LDAP_SCOPE_BASE;
312                                 op->o_req_dn = op->o_bd->be_suffix[0];
313                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
314                                 rs->sr_err = op->o_bd->be_search(op, rs);
315
316                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
317                                 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn)) {
318                                 op->o_req_dn = op->o_bd->be_suffix[0];
319                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
320                                 rs->sr_err = op->o_bd->be_search( op, rs );
321
322                         } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
323                                 rs->sr_err = op->o_bd->be_search( op, rs );
324                         }
325
326                         switch ( gs.err ) {
327
328                         /*
329                          * Add errors that should result in dropping
330                          * the search
331                          */
332                         case LDAP_SIZELIMIT_EXCEEDED:
333                         case LDAP_TIMELIMIT_EXCEEDED:
334                         case LDAP_ADMINLIMIT_EXCEEDED:
335                                 goto end_of_loop;
336                         
337                         default:
338                                 break;
339                         }
340                 }
341 end_of_loop:;
342                 op->ors_scope = scope0;
343                 op->ors_slimit = slimit0;
344                 op->ors_tlimit = tlimit0;
345                 op->o_req_dn = dn;
346                 op->o_req_ndn = ndn;
347
348                 break;
349         }
350         op->o_callback = gs.prevcb;
351         rs->sr_err = gs.err;
352         rs->sr_matched = gs.matched;
353         rs->sr_ref = gs.refs;
354
355         send_ldap_result( op, rs );
356
357 done:
358         op->o_bd = b0;
359         if (gs.matched)
360                 free (gs.matched);
361         if (gs.refs)
362                 ber_bvarray_free(gs.refs);
363         return rs->sr_err;
364 }
365
366
367 static int
368 glue_tool_entry_open (
369         BackendDB *b0,
370         int mode
371 )
372 {
373         /* We don't know which backend to talk to yet, so just
374          * remember the mode and move on...
375          */
376
377         glueMode = mode;
378         glueBack = NULL;
379
380         return 0;
381 }
382
383 static int
384 glue_tool_entry_close (
385         BackendDB *b0
386 )
387 {
388         int rc = 0;
389
390         if (glueBack) {
391                 if (!glueBack->be_entry_close)
392                         return 0;
393                 rc = glueBack->be_entry_close (glueBack);
394         }
395         return rc;
396 }
397
398 static ID
399 glue_tool_entry_first (
400         BackendDB *b0
401 )
402 {
403         glueinfo *gi = (glueinfo *) b0->bd_info;
404         int i;
405
406         /* If we're starting from scratch, start at the most general */
407         if (!glueBack) {
408                 for (i = gi->nodes-1; i >= 0; i--) {
409                         if (gi->n[i].be->be_entry_open &&
410                             gi->n[i].be->be_entry_first) {
411                                 glueBack = gi->n[i].be;
412                                 break;
413                         }
414                 }
415
416         }
417         if (!glueBack || glueBack->be_entry_open (glueBack, glueMode) != 0)
418                 return NOID;
419
420         return glueBack->be_entry_first (glueBack);
421 }
422
423 static ID
424 glue_tool_entry_next (
425         BackendDB *b0
426 )
427 {
428         glueinfo *gi = (glueinfo *) b0->bd_info;
429         int i;
430         ID rc;
431
432         if (!glueBack || !glueBack->be_entry_next)
433                 return NOID;
434
435         rc = glueBack->be_entry_next (glueBack);
436
437         /* If we ran out of entries in one database, move on to the next */
438         if (rc == NOID) {
439                 glueBack->be_entry_close (glueBack);
440                 for (i=0; i<gi->nodes; i++) {
441                         if (gi->n[i].be == glueBack)
442                                 break;
443                 }
444                 if (i == 0) {
445                         glueBack = NULL;
446                         rc = NOID;
447                 } else {
448                         glueBack = gi->n[i-1].be;
449                         rc = glue_tool_entry_first (b0);
450                 }
451         }
452         return rc;
453 }
454
455 static Entry *
456 glue_tool_entry_get (
457         BackendDB *b0,
458         ID id
459 )
460 {
461         if (!glueBack || !glueBack->be_entry_get)
462                 return NULL;
463
464         return glueBack->be_entry_get (glueBack, id);
465 }
466
467 static ID
468 glue_tool_entry_put (
469         BackendDB *b0,
470         Entry *e,
471         struct berval *text
472 )
473 {
474         BackendDB *be;
475         int rc;
476
477         be = glue_back_select (b0, e->e_ndn);
478         if (!be->be_entry_put)
479                 return NOID;
480
481         if (!glueBack) {
482                 rc = be->be_entry_open (be, glueMode);
483                 if (rc != 0)
484                         return NOID;
485         } else if (be != glueBack) {
486                 /* If this entry belongs in a different branch than the
487                  * previous one, close the current database and open the
488                  * new one.
489                  */
490                 glueBack->be_entry_close (glueBack);
491                 rc = be->be_entry_open (be, glueMode);
492                 if (rc != 0)
493                         return NOID;
494         }
495         glueBack = be;
496         return be->be_entry_put (be, e, text);
497 }
498
499 static int
500 glue_tool_entry_reindex (
501         BackendDB *b0,
502         ID id
503 )
504 {
505         if (!glueBack || !glueBack->be_entry_reindex)
506                 return -1;
507
508         return glueBack->be_entry_reindex (glueBack, id);
509 }
510
511 static int
512 glue_tool_sync (
513         BackendDB *b0
514 )
515 {
516         glueinfo *gi = (glueinfo *) b0->bd_info;
517         int i;
518
519         /* just sync everyone */
520         for (i = 0; i<gi->nodes; i++)
521                 if (gi->n[i].be->be_sync)
522                         gi->n[i].be->be_sync (gi->n[i].be);
523         return 0;
524 }
525
526 int
527 glue_sub_init( )
528 {
529         int i, j;
530         int cont = num_subordinates;
531         BackendDB *b1, *be;
532         BackendInfo *bi = NULL;
533         glueinfo *gi;
534
535         /* While there are subordinate backends, search backwards through the
536          * backends and connect them to their superior.
537          */
538         for (i = nBackendDB - 1, b1=&backendDB[i]; cont && i>=0; b1--,i--) {
539                 if (SLAP_GLUE_SUBORDINATE ( b1 ) ) {
540                         /* The last database cannot be a subordinate of noone */
541                         if (i == nBackendDB - 1) {
542                                 b1->be_flags ^= SLAP_BFLAG_GLUE_SUBORDINATE;
543                         }
544                         continue;
545                 }
546                 gi = NULL;
547                 for (j = i-1, be=&backendDB[j]; j>=0; be--,j--) {
548                         if ( ! SLAP_GLUE_SUBORDINATE( be ) ) {
549                                 continue;
550                         }
551                         /* We will only link it once */
552                         if ( SLAP_GLUE_LINKED( be ) ) {
553                                 continue;
554                         }
555                         if (!dnIsSuffix(&be->be_nsuffix[0], &b1->be_nsuffix[0])) {
556                                 continue;
557                         }
558                         cont--;
559                         be->be_flags |= SLAP_BFLAG_GLUE_LINKED;
560                         if (gi == NULL) {
561                                 /* We create a copy of the superior's be
562                                  * structure, pointing to all of its original
563                                  * information. Then we replace elements of
564                                  * the superior's info with our own. The copy
565                                  * is used whenever we have operations to pass
566                                  * down to the real database.
567                                  */
568                                 b1->be_flags |= SLAP_BFLAG_GLUE_INSTANCE;
569                                 gi = (glueinfo *)ch_malloc(sizeof(glueinfo));
570                                 gi->nodes = 0;
571                                 gi->bd = *b1;
572                                 gi->bi = *b1->bd_info;
573                                 bi = (BackendInfo *)gi;
574                                 bi->bi_open = glue_back_open;
575                                 bi->bi_close = glue_back_close;
576                                 bi->bi_db_open = glue_back_db_open;
577                                 bi->bi_db_close = glue_back_db_close;
578                                 bi->bi_db_destroy = glue_back_db_destroy;
579
580                                 bi->bi_op_search = glue_back_search;
581
582                                 /*
583                                  * hooks for slap tools
584                                  */
585                                 bi->bi_tool_entry_open = glue_tool_entry_open;
586                                 bi->bi_tool_entry_close = glue_tool_entry_close;
587                                 bi->bi_tool_entry_first = glue_tool_entry_first;
588                                 bi->bi_tool_entry_next = glue_tool_entry_next;
589                                 bi->bi_tool_entry_get = glue_tool_entry_get;
590                                 bi->bi_tool_entry_put = glue_tool_entry_put;
591                                 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
592                                 bi->bi_tool_sync = glue_tool_sync;
593                         } else {
594                                 gi = (glueinfo *)ch_realloc(gi,
595                                         sizeof(glueinfo) +
596                                         gi->nodes * sizeof(gluenode));
597                         }
598                         gi->n[gi->nodes].be = be;
599                         dnParent( &be->be_nsuffix[0], &gi->n[gi->nodes].pdn ); 
600                         gi->nodes++;
601                 }
602                 if (gi) {
603                         /* One more node for the master */
604                         gi = (glueinfo *)ch_realloc(gi,
605                                 sizeof(glueinfo) + gi->nodes * sizeof(gluenode));
606                         gi->n[gi->nodes].be = &gi->bd;
607                         dnParent( &b1->be_nsuffix[0], &gi->n[gi->nodes].pdn );
608                         gi->nodes++;
609                         b1->bd_info = (BackendInfo *)gi;
610                 }
611         }
612         /* If there are any unresolved subordinates left, something is wrong */
613         return cont;
614 }