]> 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         case LDAP_SCOPE_SUBORDINATE: /* FIXME */
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                         {
312                                 op->ors_scope = LDAP_SCOPE_BASE;
313                                 op->o_req_dn = op->o_bd->be_suffix[0];
314                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
315                                 rs->sr_err = op->o_bd->be_search(op, rs);
316
317                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
318                                 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
319                         {
320                                 op->o_req_dn = op->o_bd->be_suffix[0];
321                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
322                                 rs->sr_err = op->o_bd->be_search( op, rs );
323
324                         } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
325                                 rs->sr_err = op->o_bd->be_search( op, rs );
326                         }
327
328                         switch ( gs.err ) {
329
330                         /*
331                          * Add errors that should result in dropping
332                          * the search
333                          */
334                         case LDAP_SIZELIMIT_EXCEEDED:
335                         case LDAP_TIMELIMIT_EXCEEDED:
336                         case LDAP_ADMINLIMIT_EXCEEDED:
337                                 goto end_of_loop;
338                         
339                         default:
340                                 break;
341                         }
342                 }
343 end_of_loop:;
344                 op->ors_scope = scope0;
345                 op->ors_slimit = slimit0;
346                 op->ors_tlimit = tlimit0;
347                 op->o_req_dn = dn;
348                 op->o_req_ndn = ndn;
349
350                 break;
351         }
352         op->o_callback = cb.sc_next;
353         rs->sr_err = gs.err;
354         rs->sr_matched = gs.matched;
355         rs->sr_ref = gs.refs;
356
357         send_ldap_result( op, rs );
358
359 done:
360         op->o_bd = b0;
361         if (gs.matched)
362                 free (gs.matched);
363         if (gs.refs)
364                 ber_bvarray_free(gs.refs);
365         return rs->sr_err;
366 }
367
368
369 static int
370 glue_tool_entry_open (
371         BackendDB *b0,
372         int mode
373 )
374 {
375         /* We don't know which backend to talk to yet, so just
376          * remember the mode and move on...
377          */
378
379         glueMode = mode;
380         glueBack = NULL;
381
382         return 0;
383 }
384
385 static int
386 glue_tool_entry_close (
387         BackendDB *b0
388 )
389 {
390         int rc = 0;
391
392         if (glueBack) {
393                 if (!glueBack->be_entry_close)
394                         return 0;
395                 rc = glueBack->be_entry_close (glueBack);
396         }
397         return rc;
398 }
399
400 static ID
401 glue_tool_entry_first (
402         BackendDB *b0
403 )
404 {
405         glueinfo *gi = (glueinfo *) b0->bd_info;
406         int i;
407
408         /* If we're starting from scratch, start at the most general */
409         if (!glueBack) {
410                 for (i = gi->nodes-1; i >= 0; i--) {
411                         if (gi->n[i].be->be_entry_open &&
412                             gi->n[i].be->be_entry_first) {
413                                 glueBack = gi->n[i].be;
414                                 break;
415                         }
416                 }
417
418         }
419         if (!glueBack || glueBack->be_entry_open (glueBack, glueMode) != 0)
420                 return NOID;
421
422         return glueBack->be_entry_first (glueBack);
423 }
424
425 static ID
426 glue_tool_entry_next (
427         BackendDB *b0
428 )
429 {
430         glueinfo *gi = (glueinfo *) b0->bd_info;
431         int i;
432         ID rc;
433
434         if (!glueBack || !glueBack->be_entry_next)
435                 return NOID;
436
437         rc = glueBack->be_entry_next (glueBack);
438
439         /* If we ran out of entries in one database, move on to the next */
440         if (rc == NOID) {
441                 glueBack->be_entry_close (glueBack);
442                 for (i=0; i<gi->nodes; i++) {
443                         if (gi->n[i].be == glueBack)
444                                 break;
445                 }
446                 if (i == 0) {
447                         glueBack = NULL;
448                         rc = NOID;
449                 } else {
450                         glueBack = gi->n[i-1].be;
451                         rc = glue_tool_entry_first (b0);
452                 }
453         }
454         return rc;
455 }
456
457 static Entry *
458 glue_tool_entry_get (
459         BackendDB *b0,
460         ID id
461 )
462 {
463         if (!glueBack || !glueBack->be_entry_get)
464                 return NULL;
465
466         return glueBack->be_entry_get (glueBack, id);
467 }
468
469 static ID
470 glue_tool_entry_put (
471         BackendDB *b0,
472         Entry *e,
473         struct berval *text
474 )
475 {
476         BackendDB *be;
477         int rc;
478
479         be = glue_back_select (b0, e->e_ndn);
480         if (!be->be_entry_put)
481                 return NOID;
482
483         if (!glueBack) {
484                 rc = be->be_entry_open (be, glueMode);
485                 if (rc != 0)
486                         return NOID;
487         } else if (be != glueBack) {
488                 /* If this entry belongs in a different branch than the
489                  * previous one, close the current database and open the
490                  * new one.
491                  */
492                 glueBack->be_entry_close (glueBack);
493                 rc = be->be_entry_open (be, glueMode);
494                 if (rc != 0)
495                         return NOID;
496         }
497         glueBack = be;
498         return be->be_entry_put (be, e, text);
499 }
500
501 static int
502 glue_tool_entry_reindex (
503         BackendDB *b0,
504         ID id
505 )
506 {
507         if (!glueBack || !glueBack->be_entry_reindex)
508                 return -1;
509
510         return glueBack->be_entry_reindex (glueBack, id);
511 }
512
513 static int
514 glue_tool_sync (
515         BackendDB *b0
516 )
517 {
518         glueinfo *gi = (glueinfo *) b0->bd_info;
519         int i;
520
521         /* just sync everyone */
522         for (i = 0; i<gi->nodes; i++)
523                 if (gi->n[i].be->be_sync)
524                         gi->n[i].be->be_sync (gi->n[i].be);
525         return 0;
526 }
527
528 int
529 glue_sub_init( )
530 {
531         int i, j;
532         int cont = num_subordinates;
533         BackendDB *b1, *be;
534         BackendInfo *bi = NULL;
535         glueinfo *gi;
536
537         /* While there are subordinate backends, search backwards through the
538          * backends and connect them to their superior.
539          */
540         for (i = nBackendDB - 1, b1=&backendDB[i]; cont && i>=0; b1--,i--) {
541                 if (SLAP_GLUE_SUBORDINATE ( b1 ) ) {
542                         /* The last database cannot be a subordinate of noone */
543                         if (i == nBackendDB - 1) {
544                                 b1->be_flags ^= SLAP_BFLAG_GLUE_SUBORDINATE;
545                         }
546                         continue;
547                 }
548                 gi = NULL;
549                 for (j = i-1, be=&backendDB[j]; j>=0; be--,j--) {
550                         if ( ! SLAP_GLUE_SUBORDINATE( be ) ) {
551                                 continue;
552                         }
553                         /* We will only link it once */
554                         if ( SLAP_GLUE_LINKED( be ) ) {
555                                 continue;
556                         }
557                         if (!dnIsSuffix(&be->be_nsuffix[0], &b1->be_nsuffix[0])) {
558                                 continue;
559                         }
560                         cont--;
561                         be->be_flags |= SLAP_BFLAG_GLUE_LINKED;
562                         if (gi == NULL) {
563                                 /* We create a copy of the superior's be
564                                  * structure, pointing to all of its original
565                                  * information. Then we replace elements of
566                                  * the superior's info with our own. The copy
567                                  * is used whenever we have operations to pass
568                                  * down to the real database.
569                                  */
570                                 b1->be_flags |= SLAP_BFLAG_GLUE_INSTANCE;
571                                 gi = (glueinfo *)ch_malloc(sizeof(glueinfo));
572                                 gi->nodes = 0;
573                                 gi->bd = *b1;
574                                 gi->bi = *b1->bd_info;
575                                 bi = (BackendInfo *)gi;
576                                 bi->bi_open = glue_back_open;
577                                 bi->bi_close = glue_back_close;
578                                 bi->bi_db_open = glue_back_db_open;
579                                 bi->bi_db_close = glue_back_db_close;
580                                 bi->bi_db_destroy = glue_back_db_destroy;
581
582                                 bi->bi_op_search = glue_back_search;
583
584                                 /*
585                                  * hooks for slap tools
586                                  */
587                                 bi->bi_tool_entry_open = glue_tool_entry_open;
588                                 bi->bi_tool_entry_close = glue_tool_entry_close;
589                                 bi->bi_tool_entry_first = glue_tool_entry_first;
590                                 bi->bi_tool_entry_next = glue_tool_entry_next;
591                                 bi->bi_tool_entry_get = glue_tool_entry_get;
592                                 bi->bi_tool_entry_put = glue_tool_entry_put;
593                                 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
594                                 bi->bi_tool_sync = glue_tool_sync;
595                                 /* FIXME : will support later */
596                                 bi->bi_tool_dn2id_get = 0;
597                                 bi->bi_tool_id2entry_get = 0;
598                                 bi->bi_tool_entry_modify = 0;
599                         } else {
600                                 gi = (glueinfo *)ch_realloc(gi,
601                                         sizeof(glueinfo) +
602                                         gi->nodes * sizeof(gluenode));
603                         }
604                         gi->n[gi->nodes].be = be;
605                         dnParent( &be->be_nsuffix[0], &gi->n[gi->nodes].pdn ); 
606                         gi->nodes++;
607                 }
608                 if (gi) {
609                         /* One more node for the master */
610                         gi = (glueinfo *)ch_realloc(gi,
611                                 sizeof(glueinfo) + gi->nodes * sizeof(gluenode));
612                         gi->n[gi->nodes].be = &gi->bd;
613                         dnParent( &b1->be_nsuffix[0], &gi->n[gi->nodes].pdn );
614                         gi->nodes++;
615                         b1->bd_info = (BackendInfo *)gi;
616                 }
617         }
618         /* If there are any unresolved subordinates left, something is wrong */
619         return cont;
620 }