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