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