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