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