]> git.sur5r.net Git - openldap/blob - servers/slapd/backglue.c
NVALUES: fix a couple of value_find_ex() calls
[openldap] / servers / slapd / backglue.c
1 /* backglue.c - backend glue routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 2001-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 /*
9  * Functions to glue a bunch of other backends into a single tree.
10  * All of the glued backends must share a common suffix. E.g., you
11  * can glue o=foo and ou=bar,o=foo but you can't glue o=foo and o=bar.
12  *
13  * This uses the backend structures and routines extensively, but is
14  * not an actual backend of its own. To use it you must add a "subordinate"
15  * keyword to the configuration of other backends. Subordinates will
16  * automatically be connected to their parent backend.
17  *
18  * The purpose of these functions is to allow you to split a single database
19  * into pieces (for load balancing purposes, whatever) but still be able
20  * to treat it as a single database after it's been split. As such, each
21  * of the glued backends should have identical rootdn and rootpw.
22  *
23  * If you need more elaborate configuration, you probably should be using
24  * back-meta instead.
25  *  -- Howard Chu
26  */
27
28 #include "portable.h"
29
30 #include <stdio.h>
31
32 #include <ac/string.h>
33 #include <ac/socket.h>
34
35 #define SLAPD_TOOLS
36 #include "slap.h"
37
38 typedef struct gluenode {
39         BackendDB *be;
40         struct berval pdn;
41 } gluenode;
42
43 typedef struct glueinfo {
44         BackendInfo bi;
45         BackendDB bd;
46         int nodes;
47         gluenode n[1];
48 } glueinfo;
49
50 static int glueMode;
51 static BackendDB *glueBack;
52
53 /* Just like select_backend, but only for our backends */
54 static BackendDB *
55 glue_back_select (
56         BackendDB *be,
57         const char *dn
58 )
59 {
60         glueinfo *gi = (glueinfo *) be->bd_info;
61         struct berval bv;
62         int i;
63
64         bv.bv_len = strlen(dn);
65         bv.bv_val = (char *) dn;
66
67         for (i = 0; i<gi->nodes; i++) {
68                 if (dnIsSuffix(&bv, &gi->n[i].be->be_nsuffix[0])) {
69                         return gi->n[i].be;
70                 }
71         }
72         return NULL;
73 }
74
75 /* This function will only be called in tool mode */
76 static int
77 glue_back_open (
78         BackendInfo *bi
79 )
80 {
81         int rc = 0;
82         static int glueOpened = 0;
83
84         if (glueOpened) return 0;
85
86         glueOpened = 1;
87
88         /* If we were invoked in tool mode, open all the underlying backends */
89         if (slapMode & SLAP_TOOL_MODE) {
90                 rc = backend_startup (NULL);
91         } /* other case is impossible */
92         return rc;
93 }
94
95 /* This function will only be called in tool mode */
96 static int
97 glue_back_close (
98         BackendInfo *bi
99 )
100 {
101         static int glueClosed = 0;
102         int rc = 0;
103
104         if (glueClosed) return 0;
105
106         glueClosed = 1;
107
108         if (slapMode & SLAP_TOOL_MODE) {
109                 rc = backend_shutdown (NULL);
110         }
111         return rc;
112 }
113
114 static int
115 glue_back_db_open (
116         BackendDB *be
117 )
118 {
119         glueinfo *gi = (glueinfo *) be->bd_info;
120         static int glueOpened = 0;
121         int rc = 0;
122
123         if (glueOpened) return 0;
124
125         glueOpened = 1;
126
127         gi->bd.be_acl = be->be_acl;
128
129         if (gi->bd.bd_info->bi_db_open)
130                 rc = gi->bd.bd_info->bi_db_open(&gi->bd);
131
132         return rc;
133 }
134
135 static int
136 glue_back_db_close (
137         BackendDB *be
138 )
139 {
140         glueinfo *gi = (glueinfo *) be->bd_info;
141         static int glueClosed = 0;
142
143         if (glueClosed) return 0;
144
145         glueClosed = 1;
146
147         /* Close the master */
148         if (gi->bd.bd_info->bi_db_close)
149                 gi->bd.bd_info->bi_db_close( &gi->bd );
150
151         return 0;
152 }
153
154 static int
155 glue_back_db_destroy (
156         BackendDB *be
157 )
158 {
159         glueinfo *gi = (glueinfo *) be->bd_info;
160
161         if (gi->bd.bd_info->bi_db_destroy)
162                 gi->bd.bd_info->bi_db_destroy( &gi->bd );
163         free (gi);
164         return 0;
165 }
166
167 typedef struct glue_state {
168         int err;
169         int nentries;
170         int matchlen;
171         char *matched;
172         int nrefs;
173         BerVarray refs;
174         slap_callback *prevcb;
175 } glue_state;
176
177 static void
178 glue_back_response (
179         Connection *conn,
180         Operation *op,
181         ber_tag_t tag,
182         ber_int_t msgid,
183         ber_int_t err,
184         const char *matched,
185         const char *text,
186         BerVarray ref,
187         const char *resoid,
188         struct berval *resdata,
189         struct berval *sasldata,
190         LDAPControl **ctrls
191 )
192 {
193         glue_state *gs = op->o_callback->sc_private;
194
195         if (err == LDAP_SUCCESS || gs->err != LDAP_SUCCESS)
196                 gs->err = err;
197         if (gs->err == LDAP_SUCCESS && gs->matched) {
198                 free (gs->matched);
199                 gs->matched = NULL;
200                 gs->matchlen = 0;
201         }
202         if (gs->err != LDAP_SUCCESS && matched) {
203                 int len;
204                 len = strlen (matched);
205                 if (len > gs->matchlen) {
206                         if (gs->matched)
207                                 free (gs->matched);
208                         gs->matched = ch_strdup (matched);
209                         gs->matchlen = len;
210                 }
211         }
212         if (ref) {
213                 int i, j, k;
214                 BerVarray new;
215
216                 for (i=0; ref[i].bv_val; i++);
217
218                 j = gs->nrefs;
219                 if (!j) {
220                         new = ch_malloc ((i+1)*sizeof(struct berval));
221                 } else {
222                         new = ch_realloc(gs->refs,
223                                 (j+i+1)*sizeof(struct berval));
224                 }
225                 for (k=0; k<i; j++,k++) {
226                         ber_dupbv( &new[j], &ref[k] );
227                 }
228                 new[j].bv_val = NULL;
229                 gs->nrefs = j;
230                 gs->refs = new;
231         }
232 }
233
234 static void
235 glue_back_sresult (
236         Connection *c,
237         Operation *op,
238         ber_int_t err,
239         const char *matched,
240         const char *text,
241         BerVarray refs,
242         LDAPControl **ctrls,
243         int nentries
244 )
245 {
246         glue_state *gs = op->o_callback->sc_private;
247
248         gs->nentries += nentries;
249         glue_back_response (c, op, 0, 0, err, matched, text, refs,
250                             NULL, NULL, NULL, ctrls);
251 }
252
253 static int
254 glue_back_sendentry (
255         BackendDB *be,
256         Connection *c,
257         Operation *op,
258         Entry *e,
259         AttributeName *an,
260         int ao,
261         LDAPControl **ctrls
262 )
263 {
264         slap_callback *tmp = op->o_callback;
265         glue_state *gs = tmp->sc_private;
266         int rc;
267
268         op->o_callback = gs->prevcb;
269         if (op->o_callback && op->o_callback->sc_sendentry) {
270                 rc = op->o_callback->sc_sendentry(be, c, op, e, an, ao, ctrls);
271         } else {
272                 rc = send_search_entry(be, c, op, e, an, ao, ctrls);
273         }
274         op->o_callback = tmp;
275         return rc;
276 }
277
278 static int
279 glue_back_sendreference (
280         BackendDB *be,
281         Connection *c,
282         Operation *op,
283         Entry *e,
284         BerVarray bv,
285         LDAPControl **ctrls,
286         BerVarray *v2
287 )
288 {
289         slap_callback *tmp = op->o_callback;
290         glue_state *gs = tmp->sc_private;
291         int rc;
292
293         op->o_callback = gs->prevcb;
294         if (op->o_callback && op->o_callback->sc_sendreference) {
295                 rc = op->o_callback->sc_sendreference( be, c, op, e, bv, ctrls, v2 );
296         } else {
297                 rc = send_search_reference( be, c, op, e, bv, ctrls, v2 );
298         }
299         op->o_callback = tmp;
300         return rc;
301 }
302
303 static int
304 glue_back_search (
305         BackendDB *b0,
306         Connection *conn,
307         Operation *op,
308         struct berval *dn,
309         struct berval *ndn,
310         int scope,
311         int deref,
312         int slimit,
313         int tlimit,
314         Filter *filter,
315         struct berval *filterstr,
316         AttributeName *attrs,
317         int attrsonly
318 )
319 {
320         glueinfo *gi = (glueinfo *) b0->bd_info;
321         BackendDB *be;
322         int i, rc = 0, t2limit = 0, s2limit = 0;
323         long stoptime = 0;
324         glue_state gs = {0, 0, 0, NULL, 0, NULL, NULL};
325         slap_callback cb;
326
327         cb.sc_response = glue_back_response;
328         cb.sc_sresult = glue_back_sresult;
329         cb.sc_sendentry = glue_back_sendentry;
330         cb.sc_private = &gs;
331
332         gs.prevcb = op->o_callback;
333
334         if (tlimit) {
335                 stoptime = slap_get_time () + tlimit;
336         }
337
338         switch (scope) {
339         case LDAP_SCOPE_BASE:
340                 be = glue_back_select (b0, ndn->bv_val);
341
342                 if (be && be->be_search) {
343                         rc = be->be_search (be, conn, op, dn, ndn, scope,
344                                    deref, slimit, tlimit, filter, filterstr,
345                                             attrs, attrsonly);
346                 } else {
347                         rc = LDAP_UNWILLING_TO_PERFORM;
348                         send_ldap_result (conn, op, rc, NULL,
349                                       "No search target found", NULL, NULL);
350                 }
351                 return rc;
352
353         case LDAP_SCOPE_ONELEVEL:
354         case LDAP_SCOPE_SUBTREE:
355                 op->o_callback = &cb;
356                 rc = gs.err = LDAP_UNWILLING_TO_PERFORM;
357
358                 /*
359                  * Execute in reverse order, most general first 
360                  */
361                 for (i = gi->nodes-1; i >= 0; i--) {
362                         if (!gi->n[i].be || !gi->n[i].be->be_search)
363                                 continue;
364                         if (tlimit) {
365                                 t2limit = stoptime - slap_get_time ();
366                                 if (t2limit <= 0) {
367                                         rc = gs.err = LDAP_TIMELIMIT_EXCEEDED;
368                                         break;
369                                 }
370                         }
371                         if (slimit) {
372                                 s2limit = slimit - gs.nentries;
373                                 if (s2limit <= 0) {
374                                         rc = gs.err = LDAP_SIZELIMIT_EXCEEDED;
375                                         break;
376                                 }
377                         }
378                         rc = 0;
379                         /*
380                          * check for abandon 
381                          */
382                         if (op->o_abandon) {
383                                 goto done;
384                         }
385                         be = gi->n[i].be;
386                         if (scope == LDAP_SCOPE_ONELEVEL && 
387                                 dn_match(&gi->n[i].pdn, ndn)) {
388                                 rc = be->be_search (be, conn, op,
389                                         &be->be_suffix[0], &be->be_nsuffix[0],
390                                         LDAP_SCOPE_BASE, deref,
391                                         s2limit, t2limit, filter, filterstr,
392                                         attrs, attrsonly);
393
394                         } else if (scope == LDAP_SCOPE_SUBTREE &&
395                                 dnIsSuffix(&be->be_nsuffix[0], ndn)) {
396                                 rc = be->be_search (be, conn, op,
397                                         &be->be_suffix[0], &be->be_nsuffix[0],
398                                         scope, deref,
399                                         s2limit, t2limit, filter, filterstr,
400                                         attrs, attrsonly);
401
402                         } else if (dnIsSuffix(ndn, &be->be_nsuffix[0])) {
403                                 rc = be->be_search (be, conn, op, dn, ndn,
404                                         scope, deref,
405                                         s2limit, t2limit, filter, filterstr,
406                                         attrs, attrsonly);
407                         }
408
409                         switch ( gs.err ) {
410
411                         /*
412                          * Add errors that should result in dropping
413                          * the search
414                          */
415                         case LDAP_SIZELIMIT_EXCEEDED:
416                         case LDAP_TIMELIMIT_EXCEEDED:
417                         case LDAP_ADMINLIMIT_EXCEEDED:
418                                 goto end_of_loop;
419                         
420                         default:
421                                 break;
422                         }
423                 }
424 end_of_loop:;
425                 break;
426         }
427         op->o_callback = gs.prevcb;
428
429         send_search_result (conn, op, gs.err, gs.matched, NULL,
430                 gs.refs, NULL, gs.nentries);
431
432 done:
433         if (gs.matched)
434                 free (gs.matched);
435         if (gs.refs)
436                 ber_bvarray_free(gs.refs);
437         return rc;
438 }
439
440
441 static int
442 glue_tool_entry_open (
443         BackendDB *b0,
444         int mode
445 )
446 {
447         /* We don't know which backend to talk to yet, so just
448          * remember the mode and move on...
449          */
450
451         glueMode = mode;
452         glueBack = NULL;
453
454         return 0;
455 }
456
457 static int
458 glue_tool_entry_close (
459         BackendDB *b0
460 )
461 {
462         int rc = 0;
463
464         if (glueBack) {
465                 if (!glueBack->be_entry_close)
466                         return 0;
467                 rc = glueBack->be_entry_close (glueBack);
468         }
469         return rc;
470 }
471
472 static ID
473 glue_tool_entry_first (
474         BackendDB *b0
475 )
476 {
477         glueinfo *gi = (glueinfo *) b0->bd_info;
478         int i;
479
480         /* If we're starting from scratch, start at the most general */
481         if (!glueBack) {
482                 for (i = gi->nodes-1; i >= 0; i--) {
483                         if (gi->n[i].be->be_entry_open &&
484                             gi->n[i].be->be_entry_first) {
485                                 glueBack = gi->n[i].be;
486                                 break;
487                         }
488                 }
489
490         }
491         if (!glueBack || glueBack->be_entry_open (glueBack, glueMode) != 0)
492                 return NOID;
493
494         return glueBack->be_entry_first (glueBack);
495 }
496
497 static ID
498 glue_tool_entry_next (
499         BackendDB *b0
500 )
501 {
502         glueinfo *gi = (glueinfo *) b0->bd_info;
503         int i;
504         ID rc;
505
506         if (!glueBack || !glueBack->be_entry_next)
507                 return NOID;
508
509         rc = glueBack->be_entry_next (glueBack);
510
511         /* If we ran out of entries in one database, move on to the next */
512         if (rc == NOID) {
513                 glueBack->be_entry_close (glueBack);
514                 for (i=0; i<gi->nodes; i++) {
515                         if (gi->n[i].be == glueBack)
516                                 break;
517                 }
518                 if (i == 0) {
519                         glueBack = NULL;
520                         rc = NOID;
521                 } else {
522                         glueBack = gi->n[i-1].be;
523                         rc = glue_tool_entry_first (b0);
524                 }
525         }
526         return rc;
527 }
528
529 static Entry *
530 glue_tool_entry_get (
531         BackendDB *b0,
532         ID id
533 )
534 {
535         if (!glueBack || !glueBack->be_entry_get)
536                 return NULL;
537
538         return glueBack->be_entry_get (glueBack, id);
539 }
540
541 static ID
542 glue_tool_entry_put (
543         BackendDB *b0,
544         Entry *e,
545         struct berval *text
546 )
547 {
548         BackendDB *be;
549         int rc;
550
551         be = glue_back_select (b0, e->e_ndn);
552         if (!be->be_entry_put)
553                 return NOID;
554
555         if (!glueBack) {
556                 rc = be->be_entry_open (be, glueMode);
557                 if (rc != 0)
558                         return NOID;
559         } else if (be != glueBack) {
560                 /* If this entry belongs in a different branch than the
561                  * previous one, close the current database and open the
562                  * new one.
563                  */
564                 glueBack->be_entry_close (glueBack);
565                 rc = be->be_entry_open (be, glueMode);
566                 if (rc != 0)
567                         return NOID;
568         }
569         glueBack = be;
570         return be->be_entry_put (be, e, text);
571 }
572
573 static int
574 glue_tool_entry_reindex (
575         BackendDB *b0,
576         ID id
577 )
578 {
579         if (!glueBack || !glueBack->be_entry_reindex)
580                 return -1;
581
582         return glueBack->be_entry_reindex (glueBack, id);
583 }
584
585 static int
586 glue_tool_sync (
587         BackendDB *b0
588 )
589 {
590         glueinfo *gi = (glueinfo *) b0->bd_info;
591         int i;
592
593         /* just sync everyone */
594         for (i = 0; i<gi->nodes; i++)
595                 if (gi->n[i].be->be_sync)
596                         gi->n[i].be->be_sync (gi->n[i].be);
597         return 0;
598 }
599
600 int
601 glue_sub_init( )
602 {
603         int i, j;
604         int cont = num_subordinates;
605         BackendDB *b1, *be;
606         BackendInfo *bi = NULL;
607         glueinfo *gi;
608
609         /* While there are subordinate backends, search backwards through the
610          * backends and connect them to their superior.
611          */
612         for (i = nBackendDB - 1, b1=&backendDB[i]; cont && i>=0; b1--,i--) {
613                 if (SLAP_GLUE_SUBORDINATE ( b1 ) ) {
614                         /* The last database cannot be a subordinate of noone */
615                         if (i == nBackendDB - 1) {
616                                 b1->be_flags ^= SLAP_BFLAG_GLUE_SUBORDINATE;
617                         }
618                         continue;
619                 }
620                 gi = NULL;
621                 for (j = i-1, be=&backendDB[j]; j>=0; be--,j--) {
622                         if ( ! SLAP_GLUE_SUBORDINATE( be ) ) {
623                                 continue;
624                         }
625                         /* We will only link it once */
626                         if ( SLAP_GLUE_LINKED( be ) ) {
627                                 continue;
628                         }
629                         if (!dnIsSuffix(&be->be_nsuffix[0], &b1->be_nsuffix[0])) {
630                                 continue;
631                         }
632                         cont--;
633                         be->be_flags |= SLAP_BFLAG_GLUE_LINKED;
634                         if (gi == NULL) {
635                                 /* We create a copy of the superior's be
636                                  * structure, pointing to all of its original
637                                  * information. Then we replace elements of
638                                  * the superior's info with our own. The copy
639                                  * is used whenever we have operations to pass
640                                  * down to the real database.
641                                  */
642                                 b1->be_flags |= SLAP_BFLAG_GLUE_INSTANCE;
643                                 gi = (glueinfo *)ch_malloc(sizeof(glueinfo));
644                                 gi->nodes = 0;
645                                 gi->bd = *b1;
646                                 gi->bi = *b1->bd_info;
647                                 bi = (BackendInfo *)gi;
648                                 bi->bi_open = glue_back_open;
649                                 bi->bi_close = glue_back_close;
650                                 bi->bi_db_open = glue_back_db_open;
651                                 bi->bi_db_close = glue_back_db_close;
652                                 bi->bi_db_destroy = glue_back_db_destroy;
653
654                                 bi->bi_op_search = glue_back_search;
655
656                                 /*
657                                  * hooks for slap tools
658                                  */
659                                 bi->bi_tool_entry_open = glue_tool_entry_open;
660                                 bi->bi_tool_entry_close = glue_tool_entry_close;
661                                 bi->bi_tool_entry_first = glue_tool_entry_first;
662                                 bi->bi_tool_entry_next = glue_tool_entry_next;
663                                 bi->bi_tool_entry_get = glue_tool_entry_get;
664                                 bi->bi_tool_entry_put = glue_tool_entry_put;
665                                 bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
666                                 bi->bi_tool_sync = glue_tool_sync;
667                         } else {
668                                 gi = (glueinfo *)ch_realloc(gi,
669                                         sizeof(glueinfo) +
670                                         gi->nodes * sizeof(gluenode));
671                         }
672                         gi->n[gi->nodes].be = be;
673                         dnParent( &be->be_nsuffix[0], &gi->n[gi->nodes].pdn ); 
674                         gi->nodes++;
675                 }
676                 if (gi) {
677                         /* One more node for the master */
678                         gi = (glueinfo *)ch_realloc(gi,
679                                 sizeof(glueinfo) + gi->nodes * sizeof(gluenode));
680                         gi->n[gi->nodes].be = &gi->bd;
681                         dnParent( &b1->be_nsuffix[0], &gi->n[gi->nodes].pdn );
682                         gi->nodes++;
683                         b1->bd_info = bi;
684                 }
685         }
686         /* If there are any unresolved subordinates left, something is wrong */
687         return cont;
688 }