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