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