]> git.sur5r.net Git - openldap/blob - servers/slapd/backglue.c
c2d62179133220e61c1faded0189572f470a08ae
[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-2004 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                 assert( gi->n[i].be->be_nsuffix );
80
81                 if (dnIsSuffix(&bv, &gi->n[i].be->be_nsuffix[0])) {
82                         return gi->n[i].be;
83                 }
84         }
85         return NULL;
86 }
87
88 /* This function will only be called in tool mode */
89 static int
90 glue_back_open (
91         BackendInfo *bi
92 )
93 {
94         int rc = 0;
95         static int glueOpened = 0;
96
97         if (glueOpened) return 0;
98
99         glueOpened = 1;
100
101         /* If we were invoked in tool mode, open all the underlying backends */
102         if (slapMode & SLAP_TOOL_MODE) {
103                 rc = backend_startup (NULL);
104         } /* other case is impossible */
105         return rc;
106 }
107
108 /* This function will only be called in tool mode */
109 static int
110 glue_back_close (
111         BackendInfo *bi
112 )
113 {
114         static int glueClosed = 0;
115         int rc = 0;
116
117         if (glueClosed) return 0;
118
119         glueClosed = 1;
120
121         if (slapMode & SLAP_TOOL_MODE) {
122                 rc = backend_shutdown (NULL);
123         }
124         return rc;
125 }
126
127 static int
128 glue_back_db_open (
129         BackendDB *be
130 )
131 {
132         glueinfo *gi = (glueinfo *) be->bd_info;
133         static int glueOpened = 0;
134         int rc = 0;
135
136         if (glueOpened) return 0;
137
138         glueOpened = 1;
139
140         gi->bd.be_acl = be->be_acl;
141         gi->bd.be_pending_csn_list = be->be_pending_csn_list;
142
143         if (gi->bd.bd_info->bi_db_open)
144                 rc = gi->bd.bd_info->bi_db_open(&gi->bd);
145
146         return rc;
147 }
148
149 static int
150 glue_back_db_close (
151         BackendDB *be
152 )
153 {
154         glueinfo *gi = (glueinfo *) be->bd_info;
155         static int glueClosed = 0;
156
157         if (glueClosed) return 0;
158
159         glueClosed = 1;
160
161         /* Close the master */
162         if (gi->bd.bd_info->bi_db_close)
163                 gi->bd.bd_info->bi_db_close( &gi->bd );
164
165         return 0;
166 }
167
168 static int
169 glue_back_db_destroy (
170         BackendDB *be
171 )
172 {
173         glueinfo *gi = (glueinfo *) be->bd_info;
174
175         if (gi->bd.bd_info->bi_db_destroy)
176                 gi->bd.bd_info->bi_db_destroy( &gi->bd );
177         free (gi);
178         return 0;
179 }
180
181 typedef struct glue_state {
182         int err;
183         int slimit;
184         int matchlen;
185         char *matched;
186         int nrefs;
187         BerVarray refs;
188 } glue_state;
189
190 static int
191 glue_back_response ( Operation *op, SlapReply *rs )
192 {
193         glue_state *gs = op->o_callback->sc_private;
194
195         switch(rs->sr_type) {
196         case REP_SEARCH:
197                 if ( gs->slimit != -1 && rs->sr_nentries >= gs->slimit ) {
198                         rs->sr_err = gs->err = LDAP_SIZELIMIT_EXCEEDED;
199                         return -1;
200                 }
201                 /* fallthru */
202         case REP_SEARCHREF:
203                 return SLAP_CB_CONTINUE;
204
205         default:
206                 if (rs->sr_err == LDAP_SUCCESS ||
207                         rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
208                         rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
209                         rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
210                                 gs->err != LDAP_SUCCESS)
211                         gs->err = rs->sr_err;
212                 if (gs->err == LDAP_SUCCESS && gs->matched) {
213                         ch_free (gs->matched);
214                         gs->matched = NULL;
215                         gs->matchlen = 0;
216                 }
217                 if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
218                         int len;
219                         len = strlen (rs->sr_matched);
220                         if (len > gs->matchlen) {
221                                 if (gs->matched)
222                                         ch_free (gs->matched);
223                                 gs->matched = ch_strdup (rs->sr_matched);
224                                 gs->matchlen = len;
225                         }
226                 }
227                 if (rs->sr_ref) {
228                         int i, j, k;
229                         BerVarray new;
230
231                         for (i=0; rs->sr_ref[i].bv_val; i++);
232
233                         j = gs->nrefs;
234                         if (!j) {
235                                 new = ch_malloc ((i+1)*sizeof(struct berval));
236                         } else {
237                                 new = ch_realloc(gs->refs,
238                                         (j+i+1)*sizeof(struct berval));
239                         }
240                         for (k=0; k<i; j++,k++) {
241                                 ber_dupbv( &new[j], &rs->sr_ref[k] );
242                         }
243                         new[j].bv_val = NULL;
244                         gs->nrefs = j;
245                         gs->refs = new;
246                 }
247         }
248         return 0;
249 }
250
251 static int
252 glue_back_search ( Operation *op, SlapReply *rs )
253 {
254         BackendDB *b0 = op->o_bd;
255         glueinfo *gi = (glueinfo *) b0->bd_info;
256         int i;
257         long stoptime = 0;
258         glue_state gs = {0, 0, 0, NULL, 0, NULL};
259         slap_callback cb = { NULL, glue_back_response, NULL, NULL };
260         int scope0, slimit0, tlimit0;
261         struct berval dn, ndn;
262
263         cb.sc_private = &gs;
264
265         cb.sc_next = op->o_callback;
266
267         stoptime = slap_get_time () + op->ors_tlimit;
268
269         switch (op->ors_scope) {
270         case LDAP_SCOPE_BASE:
271                 op->o_bd = glue_back_select (b0, op->o_req_ndn.bv_val);
272
273                 if (op->o_bd && op->o_bd->be_search) {
274                         rs->sr_err = op->o_bd->be_search( op, rs );
275                 } else {
276                         send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
277                                       "No search target found");
278                 }
279                 return rs->sr_err;
280
281         case LDAP_SCOPE_ONELEVEL:
282         case LDAP_SCOPE_SUBTREE:
283 #ifdef LDAP_SCOPE_SUBORDINATE
284         case LDAP_SCOPE_SUBORDINATE: /* FIXME */
285 #endif
286
287                 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
288                         op->o_bd = glue_back_select (b0, op->o_req_ndn.bv_val);
289
290                         if (op->o_bd && op->o_bd->be_search) {
291                                 rs->sr_err = op->o_bd->be_search( op, rs );
292                         } else {
293                                 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
294                                               "No search target found");
295                         }
296                         return rs->sr_err;
297                 }
298
299                 op->o_callback = &cb;
300                 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
301                 scope0 = op->ors_scope;
302                 slimit0 = gs.slimit = op->ors_slimit;
303                 tlimit0 = op->ors_tlimit;
304                 dn = op->o_req_dn;
305                 ndn = op->o_req_ndn;
306
307                 /*
308                  * Execute in reverse order, most general first 
309                  */
310                 for (i = gi->nodes-1; i >= 0; i--) {
311                         if (!gi->n[i].be || !gi->n[i].be->be_search)
312                                 continue;
313                         if (tlimit0 != -1) {
314                                 op->ors_tlimit = stoptime - slap_get_time ();
315                                 if (op->ors_tlimit <= 0) {
316                                         rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
317                                         break;
318                                 }
319                         }
320                         if (slimit0 != -1) {
321                                 op->ors_slimit = slimit0 - rs->sr_nentries;
322                                 if (op->ors_slimit < 0) {
323                                         rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
324                                         break;
325                                 }
326                         }
327                         rs->sr_err = 0;
328                         /*
329                          * check for abandon 
330                          */
331                         if (op->o_abandon) {
332                                 goto end_of_loop;
333                         }
334                         op->o_bd = gi->n[i].be;
335
336                         assert( op->o_bd->be_suffix );
337                         assert( op->o_bd->be_nsuffix );
338                         
339                         if (scope0 == LDAP_SCOPE_ONELEVEL && 
340                                 dn_match(&gi->n[i].pdn, &ndn))
341                         {
342                                 op->ors_scope = LDAP_SCOPE_BASE;
343                                 op->o_req_dn = op->o_bd->be_suffix[0];
344                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
345                                 rs->sr_err = op->o_bd->be_search(op, rs);
346
347                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
348                                 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
349                         {
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         if ( !op->o_abandon ) {
383                 op->o_callback = cb.sc_next;
384                 rs->sr_err = gs.err;
385                 rs->sr_matched = gs.matched;
386                 rs->sr_ref = gs.refs;
387
388                 send_ldap_result( op, rs );
389         }
390
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->be_entry_first ||
451                 glueBack->be_entry_open (glueBack, glueMode) != 0)
452                 return NOID;
453
454         return glueBack->be_entry_first (glueBack);
455 }
456
457 static ID
458 glue_tool_entry_next (
459         BackendDB *b0
460 )
461 {
462         glueinfo *gi = (glueinfo *) b0->bd_info;
463         int i;
464         ID rc;
465
466         if (!glueBack || !glueBack->be_entry_next)
467                 return NOID;
468
469         rc = glueBack->be_entry_next (glueBack);
470
471         /* If we ran out of entries in one database, move on to the next */
472         while (rc == NOID) {
473                 if ( glueBack && glueBack->be_entry_close )
474                         glueBack->be_entry_close (glueBack);
475                 for (i=0; i<gi->nodes; i++) {
476                         if (gi->n[i].be == glueBack)
477                                 break;
478                 }
479                 if (i == 0) {
480                         glueBack = NULL;
481                         break;
482                 } else {
483                         glueBack = gi->n[i-1].be;
484                         rc = glue_tool_entry_first (b0);
485                 }
486         }
487         return rc;
488 }
489
490 static Entry *
491 glue_tool_entry_get (
492         BackendDB *b0,
493         ID id
494 )
495 {
496         if (!glueBack || !glueBack->be_entry_get)
497                 return NULL;
498
499         return glueBack->be_entry_get (glueBack, id);
500 }
501
502 static ID
503 glue_tool_entry_put (
504         BackendDB *b0,
505         Entry *e,
506         struct berval *text
507 )
508 {
509         BackendDB *be;
510         int rc;
511
512         be = glue_back_select (b0, e->e_ndn);
513         if (!be->be_entry_put)
514                 return NOID;
515
516         if (!glueBack) {
517                 rc = be->be_entry_open (be, glueMode);
518                 if (rc != 0)
519                         return NOID;
520         } else if (be != glueBack) {
521                 /* If this entry belongs in a different branch than the
522                  * previous one, close the current database and open the
523                  * new one.
524                  */
525                 glueBack->be_entry_close (glueBack);
526                 rc = be->be_entry_open (be, glueMode);
527                 if (rc != 0)
528                         return NOID;
529         }
530         glueBack = be;
531         return be->be_entry_put (be, e, text);
532 }
533
534 static int
535 glue_tool_entry_reindex (
536         BackendDB *b0,
537         ID id
538 )
539 {
540         if (!glueBack || !glueBack->be_entry_reindex)
541                 return -1;
542
543         return glueBack->be_entry_reindex (glueBack, id);
544 }
545
546 static int
547 glue_tool_sync (
548         BackendDB *b0
549 )
550 {
551         glueinfo *gi = (glueinfo *) b0->bd_info;
552         int i;
553
554         /* just sync everyone */
555         for (i = 0; i<gi->nodes; i++)
556                 if (gi->n[i].be->be_sync)
557                         gi->n[i].be->be_sync (gi->n[i].be);
558         return 0;
559 }
560
561 int
562 glue_sub_init( )
563 {
564         int i, j;
565         int cont = num_subordinates;
566         BackendDB *b1, *be;
567         BackendInfo *bi = NULL;
568         glueinfo *gi;
569
570         /* While there are subordinate backends, search backwards through the
571          * backends and connect them to their superior.
572          */
573         for (i = nBackendDB - 1, b1=&backendDB[i]; cont && i>=0; b1--,i--) {
574                 if (SLAP_GLUE_SUBORDINATE ( b1 ) ) {
575                         /* The last database cannot be a subordinate of noone */
576                         if (i == nBackendDB - 1) {
577                                 SLAP_DBFLAGS(b1) ^= SLAP_DBFLAG_GLUE_SUBORDINATE;
578                         }
579                         continue;
580                 }
581                 gi = NULL;
582                 for (j = i-1, be=&backendDB[j]; j>=0; be--,j--) {
583                         if ( ! SLAP_GLUE_SUBORDINATE( be ) ) {
584                                 continue;
585                         }
586                         /* We will only link it once */
587                         if ( SLAP_GLUE_LINKED( be ) ) {
588                                 continue;
589                         }
590                         assert( be->be_nsuffix );
591                         assert( b1->be_nsuffix );
592                         if (!dnIsSuffix(&be->be_nsuffix[0], &b1->be_nsuffix[0])) {
593                                 continue;
594                         }
595                         cont--;
596                         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_GLUE_LINKED;
597                         if (gi == NULL) {
598                                 /* We create a copy of the superior's be
599                                  * structure, pointing to all of its original
600                                  * information. Then we replace elements of
601                                  * the superior's info with our own. The copy
602                                  * is used whenever we have operations to pass
603                                  * down to the real database.
604                                  */
605                                 SLAP_DBFLAGS(b1) |= SLAP_DBFLAG_GLUE_INSTANCE;
606                                 gi = (glueinfo *)ch_malloc(sizeof(glueinfo));
607                                 gi->nodes = 0;
608                                 gi->bd = *b1;
609                                 gi->bi = *b1->bd_info;
610                                 bi = (BackendInfo *)gi;
611                                 bi->bi_open = glue_back_open;
612                                 bi->bi_close = glue_back_close;
613                                 bi->bi_db_open = glue_back_db_open;
614                                 bi->bi_db_close = glue_back_db_close;
615                                 bi->bi_db_destroy = glue_back_db_destroy;
616
617                                 bi->bi_op_search = glue_back_search;
618
619                                 /*
620                                  * hooks for slap tools
621                                  */
622                                 bi->bi_tool_entry_open = glue_tool_entry_open;
623                                 bi->bi_tool_entry_close = glue_tool_entry_close;
624                                 bi->bi_tool_entry_first = glue_tool_entry_first;
625                                 bi->bi_tool_entry_next = glue_tool_entry_next;
626                                 bi->bi_tool_entry_get = glue_tool_entry_get;
627                                 bi->bi_tool_entry_put = glue_tool_entry_put;
628                                 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
629                                 bi->bi_tool_sync = glue_tool_sync;
630                                 /* FIXME : will support later */
631                                 bi->bi_tool_dn2id_get = 0;
632                                 bi->bi_tool_id2entry_get = 0;
633                                 bi->bi_tool_entry_modify = 0;
634                         } else {
635                                 gi = (glueinfo *)ch_realloc(gi,
636                                         sizeof(glueinfo) +
637                                         gi->nodes * sizeof(gluenode));
638                         }
639                         gi->n[gi->nodes].be = be;
640                         dnParent( &be->be_nsuffix[0], &gi->n[gi->nodes].pdn ); 
641                         gi->nodes++;
642                 }
643                 if (gi) {
644                         /* One more node for the master */
645                         gi = (glueinfo *)ch_realloc(gi,
646                                 sizeof(glueinfo) + gi->nodes * sizeof(gluenode));
647                         gi->n[gi->nodes].be = &gi->bd;
648                         dnParent( &b1->be_nsuffix[0], &gi->n[gi->nodes].pdn );
649                         gi->nodes++;
650                         b1->bd_info = (BackendInfo *)gi;
651                 }
652         }
653         /* If there are any unresolved subordinates left, something is wrong */
654         return cont;
655 }