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