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