]> git.sur5r.net Git - openldap/blob - servers/slapd/backglue.c
ITS#2895 store a copy in the entry cache
[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         case LDAP_SCOPE_SUBORDINATE: /* FIXME */
288                 op->o_callback = &cb;
289                 rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
290                 scope0 = op->ors_scope;
291                 if ( gs.is_slimit ) {
292                         slimit0 = gs.slimit = op->ors_slimit;
293                 }
294                 tlimit0 = op->ors_tlimit;
295                 dn = op->o_req_dn;
296                 ndn = op->o_req_ndn;
297
298                 /*
299                  * Execute in reverse order, most general first 
300                  */
301                 for (i = gi->nodes-1; i >= 0; i--) {
302                         if (!gi->n[i].be || !gi->n[i].be->be_search)
303                                 continue;
304                         if (tlimit0) {
305                                 op->ors_tlimit = stoptime - slap_get_time ();
306                                 if (op->ors_tlimit <= 0) {
307                                         rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
308                                         break;
309                                 }
310                         }
311                         if ( gs.is_slimit ) {
312                                 op->ors_slimit = slimit0 - rs->sr_nentries;
313                                 if (op->ors_slimit < 0) {
314                                         rs->sr_err = gs.err = LDAP_SIZELIMIT_EXCEEDED;
315                                         break;
316                                 }
317                         }
318                         rs->sr_err = 0;
319                         /*
320                          * check for abandon 
321                          */
322                         if (op->o_abandon) {
323                                 goto done;
324                         }
325                         op->o_bd = gi->n[i].be;
326                         if (scope0 == LDAP_SCOPE_ONELEVEL && 
327                                 dn_match(&gi->n[i].pdn, &ndn))
328                         {
329                                 op->ors_scope = LDAP_SCOPE_BASE;
330                                 op->o_req_dn = op->o_bd->be_suffix[0];
331                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
332                                 rs->sr_err = op->o_bd->be_search(op, rs);
333
334                         } else if (scope0 == LDAP_SCOPE_SUBTREE &&
335                                 dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
336                         {
337                                 op->o_req_dn = op->o_bd->be_suffix[0];
338                                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
339                                 rs->sr_err = op->o_bd->be_search( op, rs );
340
341                         } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
342                                 rs->sr_err = op->o_bd->be_search( op, rs );
343                         }
344
345                         switch ( gs.err ) {
346
347                         /*
348                          * Add errors that should result in dropping
349                          * the search
350                          */
351                         case LDAP_SIZELIMIT_EXCEEDED:
352                         case LDAP_TIMELIMIT_EXCEEDED:
353                         case LDAP_ADMINLIMIT_EXCEEDED:
354                                 goto end_of_loop;
355                         
356                         default:
357                                 break;
358                         }
359                 }
360 end_of_loop:;
361                 op->ors_scope = scope0;
362                 if ( gs.is_slimit ) {
363                         op->ors_slimit = slimit0;
364                 }
365                 op->ors_tlimit = tlimit0;
366                 op->o_req_dn = dn;
367                 op->o_req_ndn = ndn;
368
369                 break;
370         }
371         op->o_callback = cb.sc_next;
372         rs->sr_err = gs.err;
373         rs->sr_matched = gs.matched;
374         rs->sr_ref = gs.refs;
375
376         send_ldap_result( op, rs );
377
378 done:
379         op->o_bd = b0;
380         if (gs.matched)
381                 free (gs.matched);
382         if (gs.refs)
383                 ber_bvarray_free(gs.refs);
384         return rs->sr_err;
385 }
386
387
388 static int
389 glue_tool_entry_open (
390         BackendDB *b0,
391         int mode
392 )
393 {
394         /* We don't know which backend to talk to yet, so just
395          * remember the mode and move on...
396          */
397
398         glueMode = mode;
399         glueBack = NULL;
400
401         return 0;
402 }
403
404 static int
405 glue_tool_entry_close (
406         BackendDB *b0
407 )
408 {
409         int rc = 0;
410
411         if (glueBack) {
412                 if (!glueBack->be_entry_close)
413                         return 0;
414                 rc = glueBack->be_entry_close (glueBack);
415         }
416         return rc;
417 }
418
419 static ID
420 glue_tool_entry_first (
421         BackendDB *b0
422 )
423 {
424         glueinfo *gi = (glueinfo *) b0->bd_info;
425         int i;
426
427         /* If we're starting from scratch, start at the most general */
428         if (!glueBack) {
429                 for (i = gi->nodes-1; i >= 0; i--) {
430                         if (gi->n[i].be->be_entry_open &&
431                             gi->n[i].be->be_entry_first) {
432                                 glueBack = gi->n[i].be;
433                                 break;
434                         }
435                 }
436
437         }
438         if (!glueBack || glueBack->be_entry_open (glueBack, glueMode) != 0)
439                 return NOID;
440
441         return glueBack->be_entry_first (glueBack);
442 }
443
444 static ID
445 glue_tool_entry_next (
446         BackendDB *b0
447 )
448 {
449         glueinfo *gi = (glueinfo *) b0->bd_info;
450         int i;
451         ID rc;
452
453         if (!glueBack || !glueBack->be_entry_next)
454                 return NOID;
455
456         rc = glueBack->be_entry_next (glueBack);
457
458         /* If we ran out of entries in one database, move on to the next */
459         if (rc == NOID) {
460                 glueBack->be_entry_close (glueBack);
461                 for (i=0; i<gi->nodes; i++) {
462                         if (gi->n[i].be == glueBack)
463                                 break;
464                 }
465                 if (i == 0) {
466                         glueBack = NULL;
467                         rc = NOID;
468                 } else {
469                         glueBack = gi->n[i-1].be;
470                         rc = glue_tool_entry_first (b0);
471                 }
472         }
473         return rc;
474 }
475
476 static Entry *
477 glue_tool_entry_get (
478         BackendDB *b0,
479         ID id
480 )
481 {
482         if (!glueBack || !glueBack->be_entry_get)
483                 return NULL;
484
485         return glueBack->be_entry_get (glueBack, id);
486 }
487
488 static ID
489 glue_tool_entry_put (
490         BackendDB *b0,
491         Entry *e,
492         struct berval *text
493 )
494 {
495         BackendDB *be;
496         int rc;
497
498         be = glue_back_select (b0, e->e_ndn);
499         if (!be->be_entry_put)
500                 return NOID;
501
502         if (!glueBack) {
503                 rc = be->be_entry_open (be, glueMode);
504                 if (rc != 0)
505                         return NOID;
506         } else if (be != glueBack) {
507                 /* If this entry belongs in a different branch than the
508                  * previous one, close the current database and open the
509                  * new one.
510                  */
511                 glueBack->be_entry_close (glueBack);
512                 rc = be->be_entry_open (be, glueMode);
513                 if (rc != 0)
514                         return NOID;
515         }
516         glueBack = be;
517         return be->be_entry_put (be, e, text);
518 }
519
520 static int
521 glue_tool_entry_reindex (
522         BackendDB *b0,
523         ID id
524 )
525 {
526         if (!glueBack || !glueBack->be_entry_reindex)
527                 return -1;
528
529         return glueBack->be_entry_reindex (glueBack, id);
530 }
531
532 static int
533 glue_tool_sync (
534         BackendDB *b0
535 )
536 {
537         glueinfo *gi = (glueinfo *) b0->bd_info;
538         int i;
539
540         /* just sync everyone */
541         for (i = 0; i<gi->nodes; i++)
542                 if (gi->n[i].be->be_sync)
543                         gi->n[i].be->be_sync (gi->n[i].be);
544         return 0;
545 }
546
547 int
548 glue_sub_init( )
549 {
550         int i, j;
551         int cont = num_subordinates;
552         BackendDB *b1, *be;
553         BackendInfo *bi = NULL;
554         glueinfo *gi;
555
556         /* While there are subordinate backends, search backwards through the
557          * backends and connect them to their superior.
558          */
559         for (i = nBackendDB - 1, b1=&backendDB[i]; cont && i>=0; b1--,i--) {
560                 if (SLAP_GLUE_SUBORDINATE ( b1 ) ) {
561                         /* The last database cannot be a subordinate of noone */
562                         if (i == nBackendDB - 1) {
563                                 b1->be_flags ^= SLAP_BFLAG_GLUE_SUBORDINATE;
564                         }
565                         continue;
566                 }
567                 gi = NULL;
568                 for (j = i-1, be=&backendDB[j]; j>=0; be--,j--) {
569                         if ( ! SLAP_GLUE_SUBORDINATE( be ) ) {
570                                 continue;
571                         }
572                         /* We will only link it once */
573                         if ( SLAP_GLUE_LINKED( be ) ) {
574                                 continue;
575                         }
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 }