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