]> git.sur5r.net Git - openldap/blob - servers/slapd/backglue.c
Cleaned up search, use dn_issuffixbv
[openldap] / servers / slapd / backglue.c
1 /* backglue.c - backend glue routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 2001 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  * The only configuration items that are needed for this backend are
14  * the suffixes, and they should be identical to suffixes of other
15  * backends that are being configured. The suffixes must be listed
16  * in order from longest to shortest, (most-specific to least-specific)
17  * in order for the selection to work. Every backend that is being glued
18  * must be fully configured as usual.
19  *
20  * The purpose of this backend is to allow you to split a single database
21  * into pieces (for load balancing purposes, whatever) but still be able
22  * to treat it as a single database after it's been split. As such, each
23  * of the glued backends should have identical rootdn and rootpw.
24  *
25  * If you need more elaborate configuration, you probably should be using
26  * back-meta instead.
27  *  -- Howard Chu
28  */
29
30 #include "portable.h"
31
32 #include <stdio.h>
33
34 #include <ac/socket.h>
35
36 #define SLAPD_TOOLS
37 #include "slap.h"
38
39 typedef struct gluenode {
40         BackendDB *be;
41         char *pdn;
42 } gluenode;
43
44 typedef struct glueinfo {
45         int nodes;
46         gluenode n[1];
47 } glueinfo;
48
49 /* Just like select_backend, but only for our backends */
50 static BackendDB *
51 glue_back_select (
52         BackendDB *be,
53         const char *dn
54 )
55 {
56         glueinfo *gi = (glueinfo *) be->be_private;
57         struct berval bv;
58         int i;
59
60         bv.bv_len = strlen(dn);
61         bv.bv_val = dn;
62
63         for (i = 0; be->be_nsuffix[i]; i++) {
64                 if (dn_issuffixbv (&bv, be->be_nsuffix[i]))
65                         return gi->n[i].be;
66         }
67         return NULL;
68 }
69
70 static int
71 glue_back_db_open (
72         BackendDB *be
73 )
74 {
75         glueinfo *gi;
76         int i, j, k;
77         int ok;
78
79         /*
80          * Done already? 
81          */
82         if (be->be_private)
83                 return 0;
84
85         for (i = 0; be->be_nsuffix[i]; i++);
86
87         gi = (struct glueinfo *) ch_calloc (1, sizeof (glueinfo) +
88                 (i-1) * sizeof (gluenode) );
89
90         be->be_private = gi;
91
92         if (!gi)
93                 return 1;
94
95         gi->nodes = i;
96         be->be_glueflags = SLAP_GLUE_INSTANCE;
97
98         /*
99          * For each of our suffixes, find the real backend that handles this 
100          * suffix. 
101          */
102         for (i = 0; be->be_nsuffix[i]; i++) {
103                 for (j = 0; j < nbackends; j++) {
104                         if (be == &backends[j])
105                                 continue;
106                         ok = 0;
107                         for (k = 0; backends[j].be_nsuffix &&
108                              backends[j].be_nsuffix[k]; k++) {
109                                 if (be->be_nsuffix[i]->bv_len !=
110                                     backends[j].be_nsuffix[k]->bv_len)
111                                         continue;
112                                 if (!strcmp (backends[j].be_nsuffix[k]->bv_val,
113                                              be->be_nsuffix[i]->bv_val)) {
114                                         ok = 1;
115                                         break;
116                                 }
117                         }
118                         if (ok) {
119                                 gi->n[i].be = &backends[j];
120                                 gi->n[i].pdn = dn_parent (NULL,
121                                                  be->be_nsuffix[i]->bv_val);
122                                 if (i < gi->nodes - 1)
123                                         gi->n[i].be->be_glueflags =
124                                                 SLAP_GLUE_SUBORDINATE;
125                                 break;
126                         }
127                 }
128         }
129
130         /* If we were invoked in tool mode, open all the underlying backends */
131         if (slapMode & SLAP_TOOL_MODE) {
132                 for (i = 0; be->be_nsuffix[i]; i++) {
133                         backend_startup (gi->n[i].be);
134                 }
135         }
136         return 0;
137 }
138
139 static int
140 glue_back_db_close (
141         BackendDB *be
142 )
143 {
144         glueinfo *gi = (glueinfo *) be->be_private;
145
146         if (slapMode & SLAP_TOOL_MODE) {
147                 int i;
148                 for (i = 0; be->be_nsuffix[i]; i++) {
149                         backend_shutdown (gi->n[i].be);
150                 }
151         }
152         return 0;
153 }
154 int
155 glue_back_db_destroy (
156         BackendDB *be
157 )
158 {
159         free (be->be_private);
160         return 0;
161 }
162
163 int
164 glue_back_bind (
165         BackendDB *b0,
166         Connection *conn,
167         Operation *op,
168         const char *dn,
169         const char *ndn,
170         int method,
171         struct berval *cred,
172         char **edn
173 )
174 {
175         BackendDB *be;
176         int rc;
177
178         be = glue_back_select (b0, ndn);
179
180         if (be && be->be_bind) {
181                 conn->c_authz_backend = be;
182                 rc = be->be_bind (be, conn, op, dn, ndn, method, cred, edn);
183         } else {
184                 rc = LDAP_UNWILLING_TO_PERFORM;
185                 send_ldap_result (conn, op, rc, NULL, "No bind target found",
186                                   NULL, NULL);
187         }
188         return rc;
189 }
190
191 typedef struct glue_state {
192         int err;
193         int nentries;
194         int matchlen;
195         char *matched;
196         int nrefs;
197         struct berval **refs;
198 } glue_state;
199
200 void
201 glue_back_response (
202         Connection *conn,
203         Operation *op,
204         ber_tag_t tag,
205         ber_int_t msgid,
206         ber_int_t err,
207         const char *matched,
208         const char *text,
209         struct berval **ref,
210         const char *resoid,
211         struct berval *resdata,
212         struct berval *sasldata,
213         LDAPControl **ctrls
214 )
215 {
216         glue_state *gs = op->o_glue;
217
218         if (err == LDAP_SUCCESS || gs->err != LDAP_SUCCESS)
219                 gs->err = err;
220         if (gs->err == LDAP_SUCCESS && gs->matched) {
221                 free (gs->matched);
222                 gs->matchlen = 0;
223         }
224         if (gs->err != LDAP_SUCCESS && matched) {
225                 int len;
226                 len = strlen (matched);
227                 if (len > gs->matchlen) {
228                         if (gs->matched)
229                                 free (gs->matched);
230                         gs->matched = ch_strdup (matched);
231                         gs->matchlen = len;
232                 }
233         }
234         if (ref) {
235                 int i, j, k;
236                 struct berval **new;
237
238                 for (i=0; ref[i]; i++);
239
240                 j = gs->nrefs;
241                 if (!j) {
242                         new = ch_malloc ((i+1)*sizeof(struct berval *));
243                 } else {
244                         new = ch_realloc(gs->refs,
245                                 (j+i+1)*sizeof(struct berval *));
246                 }
247                 for (k=0; k<i; j++,k++) {
248                         new[j] = ber_bvdup(ref[k]);
249                 }
250                 new[j] = NULL;
251                 gs->nrefs = j;
252                 gs->refs = new;
253         }
254 }
255
256 void
257 glue_back_sresult (
258         Connection *c,
259         Operation *op,
260         ber_int_t err,
261         const char *matched,
262         const char *text,
263         struct berval **refs,
264         LDAPControl **ctrls,
265         int nentries
266 )
267 {
268         glue_state *gs = op->o_glue;
269
270         gs->nentries += nentries;
271         glue_back_response (c, op, 0, 0, err, matched, text, refs,
272                             NULL, NULL, NULL, ctrls);
273 }
274
275 int
276 glue_back_search (
277         BackendDB *b0,
278         Connection *conn,
279         Operation *op,
280         const char *dn,
281         const char *ndn,
282         int scope,
283         int deref,
284         int slimit,
285         int tlimit,
286         Filter *filter,
287         const char *filterstr,
288         char **attrs,
289         int attrsonly
290 )
291 {
292         glueinfo *gi = (glueinfo *) b0->be_private;
293         BackendDB *be;
294         int i, rc, t2limit = 0, s2limit = 0;
295         long stoptime = 0;
296         glue_state gs = {0};
297         struct berval bv;
298
299
300         if (tlimit)
301                 stoptime = slap_get_time () + tlimit;
302
303         switch (scope) {
304         case LDAP_SCOPE_BASE:
305                 be = glue_back_select (b0, ndn);
306
307                 if (be && be->be_search) {
308                         rc = be->be_search (be, conn, op, dn, ndn, scope,
309                                    deref, slimit, tlimit, filter, filterstr,
310                                             attrs, attrsonly);
311                 } else {
312                         rc = LDAP_UNWILLING_TO_PERFORM;
313                         send_ldap_result (conn, op, rc, NULL,
314                                       "No search target found", NULL, NULL);
315                 }
316                 return rc;
317
318         case LDAP_SCOPE_ONELEVEL:
319         case LDAP_SCOPE_SUBTREE:
320                 op->o_glue = &gs;
321                 op->o_sresult = glue_back_sresult;
322                 op->o_response = glue_back_response;
323                 bv.bv_len = strlen(ndn);
324                 bv.bv_val = ndn;
325
326                 /*
327                  * Execute in reverse order, most general first 
328                  */
329                 for (i = gi->nodes-1; i >= 0; i--) {
330                         if (!gi->n[i].be || !gi->n[i].be->be_search)
331                                 continue;
332                         if (tlimit) {
333                                 t2limit = stoptime - slap_get_time ();
334                                 if (t2limit <= 0)
335                                         break;
336                         }
337                         if (slimit) {
338                                 s2limit = slimit - gs.nentries;
339                                 if (s2limit <= 0)
340                                         break;
341                         }
342                         /*
343                          * check for abandon 
344                          */
345                         ldap_pvt_thread_mutex_lock (&op->o_abandonmutex);
346                         rc = op->o_abandon;
347                         ldap_pvt_thread_mutex_unlock (&op->o_abandonmutex);
348                         if (rc) {
349                                 rc = 0;
350                                 goto done;
351                         }
352                         be = gi->n[i].be;
353                         if (scope == LDAP_SCOPE_ONELEVEL && 
354                                 !strcmp (gi->n[i].pdn, ndn)) {
355                                 rc = be->be_search (be, conn, op,
356                                                     b0->be_suffix[i],
357                                                     b0->be_nsuffix[i]->bv_val,
358                                                     LDAP_SCOPE_BASE, deref,
359                                         s2limit, t2limit, filter, filterstr,
360                                                     attrs, attrsonly);
361                         } else if (scope == LDAP_SCOPE_SUBTREE &&
362                                 dn_issuffixbv (b0->be_nsuffix[i], &bv)) {
363                                 rc = be->be_search (be, conn, op,
364                                                     b0->be_suffix[i],
365                                                     b0->be_nsuffix[i]->bv_val,
366                                                     scope, deref,
367                                         s2limit, t2limit, filter, filterstr,
368                                                     attrs, attrsonly);
369                         } else if (dn_issuffix (&bv, b0->be_nsuffix[i])) {
370                                 rc = be->be_search (be, conn, op,
371                                                     dn, ndn, scope, deref,
372                                         s2limit, t2limit, filter, filterstr,
373                                                     attrs, attrsonly);
374                         }
375                 }
376                 break;
377         }
378         op->o_sresult = NULL;
379         op->o_response = NULL;
380         op->o_glue = NULL;
381
382         send_search_result (conn, op, gs.err, gs.matched, NULL, gs.refs,
383                             NULL, gs.nentries);
384 done:
385         if (gs.matched)
386                 free (gs.matched);
387         if (gs.refs)
388                 ber_bvecfree(gs.refs);
389         return rc;
390 }
391
392 int
393 glue_back_compare (
394         BackendDB *b0,
395         Connection *conn,
396         Operation *op,
397         const char *dn,
398         const char *ndn,
399         AttributeAssertion *ava
400 )
401 {
402         BackendDB *be;
403         int rc;
404
405         be = glue_back_select (b0, ndn);
406
407         if (be && be->be_compare) {
408                 rc = be->be_compare (be, conn, op, dn, ndn, ava);
409         } else {
410                 rc = LDAP_UNWILLING_TO_PERFORM;
411                 send_ldap_result (conn, op, rc, NULL, "No compare target found",
412                                   NULL, NULL);
413         }
414         return rc;
415 }
416
417 int
418 glue_back_modify (
419         BackendDB *b0,
420         Connection *conn,
421         Operation *op,
422         const char *dn,
423         const char *ndn,
424         Modifications *mod
425 )
426 {
427         BackendDB *be;
428         int rc;
429
430         be = glue_back_select (b0, ndn);
431
432         if (be && be->be_modify) {
433                 rc = be->be_modify (be, conn, op, dn, ndn, mod);
434         } else {
435                 rc = LDAP_UNWILLING_TO_PERFORM;
436                 send_ldap_result (conn, op, rc, NULL, "No modify target found",
437                                   NULL, NULL);
438         }
439         return rc;
440 }
441
442 int
443 glue_back_modrdn (
444         BackendDB *b0,
445         Connection *conn,
446         Operation *op,
447         const char *dn,
448         const char *ndn,
449         const char *newrdn,
450         int del,
451         const char *newsup
452 )
453 {
454         BackendDB *be;
455         int rc;
456
457         be = glue_back_select (b0, ndn);
458
459         if (be && be->be_modrdn) {
460                 rc = be->be_modrdn (be, conn, op, dn, ndn, newrdn, del, newsup);
461         } else {
462                 rc = LDAP_UNWILLING_TO_PERFORM;
463                 send_ldap_result (conn, op, rc, NULL, "No modrdn target found",
464                                   NULL, NULL);
465         }
466         return rc;
467 }
468
469 int
470 glue_back_add (
471         BackendDB *b0,
472         Connection *conn,
473         Operation *op,
474         Entry *e
475 )
476 {
477         BackendDB *be;
478         int rc;
479
480         be = glue_back_select (b0, e->e_ndn);
481
482         if (be && be->be_add) {
483                 rc = be->be_add (be, conn, op, e);
484         } else {
485                 rc = LDAP_UNWILLING_TO_PERFORM;
486                 send_ldap_result (conn, op, rc, NULL, "No add target found",
487                                   NULL, NULL);
488         }
489         return rc;
490 }
491
492 int
493 glue_back_delete (
494         BackendDB *b0,
495         Connection *conn,
496         Operation *op,
497         const char *dn,
498         const char *ndn
499 )
500 {
501         BackendDB *be;
502         int rc;
503
504         be = glue_back_select (b0, ndn);
505
506         if (be && be->be_delete) {
507                 rc = be->be_delete (be, conn, op, dn, ndn);
508         } else {
509                 rc = LDAP_UNWILLING_TO_PERFORM;
510                 send_ldap_result (conn, op, rc, NULL, "No delete target found",
511                                   NULL, NULL);
512         }
513         return rc;
514 }
515
516 int
517 glue_back_release_rw (
518         BackendDB *b0,
519         Connection *conn,
520         Operation *op,
521         Entry *e,
522         int rw
523 )
524 {
525         BackendDB *be;
526         int rc;
527
528         be = glue_back_select (b0, e->e_ndn);
529
530         if (be && be->be_release) {
531                 rc = be->be_release (be, conn, op, e, rw);
532         } else {
533                 entry_free (e);
534                 rc = 0;
535         }
536         return rc;
537 }
538
539 int
540 glue_back_group (
541         BackendDB *b0,
542         Connection *conn,
543         Operation *op,
544         Entry *target,
545         const char *ndn,
546         const char *ondn,
547         ObjectClass *oc,
548         AttributeDescription * ad
549 )
550 {
551         BackendDB *be;
552         int rc;
553
554         be = glue_back_select (b0, ndn);
555
556         if (be && be->be_group) {
557                 rc = be->be_group (be, conn, op, target, ndn, ondn, oc, ad);
558         } else {
559                 rc = LDAP_UNWILLING_TO_PERFORM;
560         }
561         return rc;
562 }
563
564 int
565 glue_back_attribute (
566         BackendDB *b0,
567         Connection *conn,
568         Operation *op,
569         Entry *target,
570         const char *ndn,
571         AttributeDescription *ad,
572         struct berval ***vals
573 )
574 {
575         BackendDB *be;
576         int rc;
577
578         be = glue_back_select (b0, ndn);
579
580         if (be && be->be_attribute) {
581                 rc = be->be_attribute (be, conn, op, target, ndn, ad, vals);
582         } else {
583                 rc = LDAP_UNWILLING_TO_PERFORM;
584         }
585         return rc;
586 }
587
588 int
589 glue_back_referrals (
590         BackendDB *b0,
591         Connection *conn,
592         Operation *op,
593         const char *dn,
594         const char *ndn,
595         const char **text
596 )
597 {
598         BackendDB *be;
599         int rc;
600
601         be = glue_back_select (b0, ndn);
602
603         if (be && be->be_chk_referrals) {
604                 rc = be->be_chk_referrals (be, conn, op, dn, ndn, text);
605         } else {
606                 rc = LDAP_SUCCESS;;
607         }
608         return rc;
609 }
610
611 static int glueMode;
612 static int glueBack;
613
614 int
615 glue_tool_entry_open (
616         BackendDB *b0,
617         int mode
618 )
619 {
620         /* We don't know which backend to talk to yet, so just
621          * remember the mode and move on...
622          */
623
624         glueMode = mode;
625         glueBack = -1;
626
627         return 0;
628 }
629
630 int
631 glue_tool_entry_close (
632         BackendDB *b0
633 )
634 {
635         glueinfo *gi = (glueinfo *) b0->be_private;
636         int i, rc = 0;
637
638         i = glueBack;
639         if (i >= 0) {
640                 if (!gi->n[i].be->be_entry_close)
641                         return 0;
642                 rc = gi->n[i].be->be_entry_close (gi->n[i].be);
643                 glueBack = -1;
644         }
645         return rc;
646 }
647
648 ID
649 glue_tool_entry_first (
650         BackendDB *b0
651 )
652 {
653         glueinfo *gi = (glueinfo *) b0->be_private;
654         int i;
655
656         /* If we're starting from scratch, start at the most general */
657         if (glueBack == -1) {
658                 for (i = gi->nodes-1; i >= 0; i--) {
659                         if (gi->n[i].be->be_entry_open &&
660                             gi->n[i].be->be_entry_first)
661                                 break;
662                 }
663         } else {
664                 i = glueBack;
665         }
666         if (gi->n[i].be->be_entry_open (gi->n[i].be, glueMode) != 0)
667                 return NOID;
668         glueBack = i;
669
670         return gi->n[i].be->be_entry_first (gi->n[i].be);
671 }
672
673 ID
674 glue_tool_entry_next (
675         BackendDB *b0
676 )
677 {
678         glueinfo *gi = (glueinfo *) b0->be_private;
679         int i, rc;
680
681         i = glueBack;
682         rc = gi->n[i].be->be_entry_next (gi->n[i].be);
683
684         /* If we ran out of entries in one database, move on to the next */
685         if (rc == NOID) {
686                 gi->n[i].be->be_entry_close (gi->n[i].be);
687                 i--;
688                 glueBack = i;
689                 if (i < 0)
690                         rc = NOID;
691                 else
692                         rc = glue_tool_entry_first (b0);
693         }
694         return rc;
695 }
696
697 Entry *
698 glue_tool_entry_get (
699         BackendDB *b0,
700         ID id
701 )
702 {
703         glueinfo *gi = (glueinfo *) b0->be_private;
704         int i = glueBack;
705
706         return gi->n[i].be->be_entry_get (gi->n[i].be, id);
707 }
708
709 ID
710 glue_tool_entry_put (
711         BackendDB *b0,
712         Entry *e
713 )
714 {
715         glueinfo *gi = (glueinfo *) b0->be_private;
716         BackendDB *be;
717         int i, rc;
718
719         be = glue_back_select (b0, e->e_ndn);
720         if (!be->be_entry_put)
721                 return NOID;
722
723         i = glueBack;
724         if (i < 0) {
725                 rc = be->be_entry_open (be, glueMode);
726                 if (rc != 0)
727                         return NOID;
728                 glueBack = i;
729         } else if (be != gi->n[i].be) {
730                 /* If this entry belongs in a different branch than the
731                  * previous one, close the current database and open the
732                  * new one.
733                  */
734                 gi->n[i].be->be_entry_close (gi->n[i].be);
735                 glueBack = -1;
736                 for (i = 0; b0->be_nsuffix[i]; i++)
737                         if (gi->n[i].be == be)
738                                 break;
739                 rc = be->be_entry_open (be, glueMode);
740                 if (rc != 0)
741                         return NOID;
742                 glueBack = i;
743         }
744         return be->be_entry_put (be, e);
745 }
746
747 int
748 glue_tool_entry_reindex (
749         BackendDB *b0,
750         ID id
751 )
752 {
753         glueinfo *gi = (glueinfo *) b0->be_private;
754         int i = glueBack;
755
756         if (!gi->n[i].be->be_entry_reindex)
757                 return -1;
758
759         return gi->n[i].be->be_entry_reindex (gi->n[i].be, id);
760 }
761
762 int
763 glue_tool_sync (
764         BackendDB *b0
765 )
766 {
767         glueinfo *gi = (glueinfo *) b0->be_private;
768         int i;
769
770         /* just sync everyone */
771         for (i = 0; b0->be_nsuffix[i]; i++)
772                 if (gi->n[i].be->be_sync)
773                         gi->n[i].be->be_sync (gi->n[i].be);
774         return 0;
775 }
776
777 int
778 glue_back_initialize (
779         BackendInfo *bi
780 )
781 {
782         bi->bi_open = 0;
783         bi->bi_config = 0;
784         bi->bi_close = 0;
785         bi->bi_destroy = 0;
786
787         bi->bi_db_init = 0;
788         bi->bi_db_config = 0;
789         bi->bi_db_open = glue_back_db_open;
790         bi->bi_db_close = glue_back_db_close;
791         bi->bi_db_destroy = glue_back_db_destroy;
792
793         bi->bi_op_bind = glue_back_bind;
794         bi->bi_op_unbind = 0;
795         bi->bi_op_search = glue_back_search;
796         bi->bi_op_compare = glue_back_compare;
797         bi->bi_op_modify = glue_back_modify;
798         bi->bi_op_modrdn = glue_back_modrdn;
799         bi->bi_op_add = glue_back_add;
800         bi->bi_op_delete = glue_back_delete;
801         bi->bi_op_abandon = 0;
802
803         bi->bi_extended = 0;
804
805         bi->bi_entry_release_rw = glue_back_release_rw;
806         bi->bi_acl_group = glue_back_group;
807         bi->bi_acl_attribute = glue_back_attribute;
808         bi->bi_chk_referrals = glue_back_referrals;
809
810         /*
811          * hooks for slap tools
812          */
813         bi->bi_tool_entry_open = glue_tool_entry_open;
814         bi->bi_tool_entry_close = glue_tool_entry_close;
815         bi->bi_tool_entry_first = glue_tool_entry_first;
816         bi->bi_tool_entry_next = glue_tool_entry_next;
817         bi->bi_tool_entry_get = glue_tool_entry_get;
818         bi->bi_tool_entry_put = glue_tool_entry_put;
819         bi->bi_tool_entry_reindex = glue_tool_entry_reindex;
820         bi->bi_tool_sync = glue_tool_sync;
821
822         bi->bi_connection_init = 0;
823         bi->bi_connection_destroy = 0;
824
825         return 0;
826 }