]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/conn.c
Berkeley DB 4.2 support (DB 4.2 required by default)
[openldap] / servers / slapd / back-meta / conn.c
1 /*
2  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  *
5  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
6  *
7  * This work has been developed to fulfill the requirements
8  * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
9  * to the OpenLDAP Foundation in the hope that it may be useful
10  * to the Open Source community, but WITHOUT ANY WARRANTY.
11  *
12  * Permission is granted to anyone to use this software for any purpose
13  * on any computer system, and to alter it and redistribute it, subject
14  * to the following restrictions:
15  *
16  * 1. The author and SysNet s.n.c. are not responsible for the consequences
17  *    of use of this software, no matter how awful, even if they arise from 
18  *    flaws in it.
19  *
20  * 2. The origin of this software must not be misrepresented, either by
21  *    explicit claim or by omission.  Since few users ever read sources,
22  *    credits should appear in the documentation.
23  *
24  * 3. Altered versions must be plainly marked as such, and must not be
25  *    misrepresented as being the original software.  Since few users
26  *    ever read sources, credits should appear in the documentation.
27  *    SysNet s.n.c. cannot be responsible for the consequences of the
28  *    alterations.
29  *
30  * 4. This notice may not be removed or altered.
31  *
32  *
33  * This software is based on the backend back-ldap, implemented
34  * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
35  * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
36  * contributors. The contribution of the original software to the present
37  * implementation is acknowledged in this copyright statement.
38  *
39  * A special acknowledgement goes to Howard for the overall architecture
40  * (and for borrowing large pieces of code), and to Mark, who implemented
41  * from scratch the attribute/objectclass mapping.
42  *
43  * The original copyright statement follows.
44  *
45  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
46  *
47  * Permission is granted to anyone to use this software for any purpose
48  * on any computer system, and to alter it and redistribute it, subject
49  * to the following restrictions:
50  *
51  * 1. The author is not responsible for the consequences of use of this
52  *    software, no matter how awful, even if they arise from flaws in it.
53  *
54  * 2. The origin of this software must not be misrepresented, either by
55  *    explicit claim or by omission.  Since few users ever read sources,
56  *    credits should appear in the documentation.
57  *
58  * 3. Altered versions must be plainly marked as such, and must not be
59  *    misrepresented as being the original software.  Since few users
60  *    ever read sources, credits should appear in the
61  *    documentation.
62  *
63  * 4. This notice may not be removed or altered.
64  *
65  */
66
67 #include "portable.h"
68
69 #include <stdio.h>
70
71 #include <ac/socket.h>
72 #include <ac/string.h>
73
74
75 #define AVL_INTERNAL
76 #include "slap.h"
77 #include "../back-ldap/back-ldap.h"
78 #include "back-meta.h"
79
80 /*
81  * Set PRINT_CONNTREE larger than 0 to dump the connection tree (debug only)
82  */
83 #define PRINT_CONNTREE 0
84
85 /*
86  * meta_back_conn_cmp
87  *
88  * compares two struct metaconn based on the value of the conn pointer;
89  * used by avl stuff
90  */
91 int
92 meta_back_conn_cmp(
93         const void *c1,
94         const void *c2
95         )
96 {
97         struct metaconn *lc1 = ( struct metaconn * )c1;
98         struct metaconn *lc2 = ( struct metaconn * )c2;
99         
100         return SLAP_PTRCMP( lc1->conn, lc2->conn );
101 }
102
103 /*
104  * meta_back_conn_dup
105  *
106  * returns -1 in case a duplicate struct metaconn has been inserted;
107  * used by avl stuff
108  */
109 int
110 meta_back_conn_dup(
111         void *c1,
112         void *c2
113         )
114 {
115         struct metaconn *lc1 = ( struct metaconn * )c1;
116         struct metaconn *lc2 = ( struct metaconn * )c2;
117
118         return( ( lc1->conn == lc2->conn ) ? -1 : 0 );
119 }
120
121 /*
122  * Debug stuff (got it from libavl)
123  */
124 #if PRINT_CONNTREE > 0
125 static void
126 ravl_print( Avlnode *root, int depth )
127 {
128         int     i;
129         
130         if ( root == 0 ) {
131                 return;
132         }
133         
134         ravl_print( root->avl_right, depth+1 );
135         
136         for ( i = 0; i < depth; i++ ) {
137                 printf( "    " );
138         }
139
140         printf( "c(%d) %d\n", ( ( struct metaconn * )root->avl_data )->conn->c_connid, root->avl_bf );
141         
142         ravl_print( root->avl_left, depth+1 );
143 }
144
145 static void
146 myprint( Avlnode *root )
147 {
148         printf( "********\n" );
149         
150         if ( root == 0 ) {
151                 printf( "\tNULL\n" );
152         } else {
153                 ravl_print( root, 0 );
154         }
155         
156         printf( "********\n" );
157 }
158 #endif /* PRINT_CONNTREE */
159 /*
160  * End of debug stuff
161  */
162
163 /*
164  * metaconn_alloc
165  * 
166  * Allocates a connection structure, making room for all the referenced targets
167  */
168 static struct metaconn *
169 metaconn_alloc( int ntargets )
170 {
171         struct metaconn *lc;
172
173         assert( ntargets > 0 );
174
175         lc = ch_calloc( sizeof( struct metaconn ), 1 );
176         if ( lc == NULL ) {
177                 return NULL;
178         }
179         
180         /*
181          * make it a null-terminated array ...
182          */
183         lc->conns = ch_calloc( sizeof( struct metasingleconn ), ntargets+1 );
184         if ( lc->conns == NULL ) {
185                 free( lc );
186                 return NULL;
187         }
188         lc->conns[ ntargets ].candidate = META_LAST_CONN;
189
190         for ( ; ntargets-- > 0; ) {
191                 lc->conns[ ntargets ].ld = NULL;
192                 lc->conns[ ntargets ].bound_dn.bv_val = NULL;
193                 lc->conns[ ntargets ].bound_dn.bv_len = 0;
194                 lc->conns[ ntargets ].cred.bv_val = NULL;
195                 lc->conns[ ntargets ].cred.bv_len = 0;
196                 lc->conns[ ntargets ].bound = META_UNBOUND;
197         }
198
199         lc->bound_target = META_BOUND_NONE;
200
201         return lc;
202 }
203
204 /*
205  * metaconn_free
206  *
207  * clears a metaconn
208  */
209 static void
210 metaconn_free(
211                 struct metaconn *lc
212 )
213 {
214         if ( !lc ) {
215                 return;
216         }
217         
218         if ( lc->conns ) {
219                 ch_free( lc->conns );
220         }
221
222         free( lc );
223 }
224
225 /*
226  * init_one_conn
227  * 
228  * Initializes one connection
229  */
230 static int
231 init_one_conn(
232                 Operation               *op,
233                 SlapReply               *rs,
234                 struct metatarget       *lt, 
235                 struct metasingleconn   *lsc
236                 )
237 {
238         struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
239         int             vers;
240         dncookie        dc;
241
242         /*
243          * Already init'ed
244          */
245         if ( lsc->ld != NULL ) {
246                 return LDAP_SUCCESS;
247         }
248        
249         /*
250          * Attempts to initialize the connection to the target ds
251          */
252         rs->sr_err = ldap_initialize( &lsc->ld, lt->uri );
253         if ( rs->sr_err != LDAP_SUCCESS ) {
254                 return ldap_back_map_result( rs );
255         }
256
257         /*
258          * Set LDAP version. This will always succeed: If the client
259          * bound with a particular version, then so can we.
260          */
261         vers = op->o_conn->c_protocol;
262         ldap_set_option( lsc->ld, LDAP_OPT_PROTOCOL_VERSION, &vers );
263         /* FIXME: configurable? */
264         ldap_set_option(lsc->ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
265
266         /*
267          * Set the network timeout if set
268          */
269         if (li->network_timeout != 0){
270                 struct timeval network_timeout;
271
272                 network_timeout.tv_usec = 0;
273                 network_timeout.tv_sec = li->network_timeout;
274
275                 ldap_set_option( lsc->ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &network_timeout);
276         }
277
278         /*
279          * Sets a cookie for the rewrite session
280          */
281         ( void )rewrite_session_init( lt->rwmap.rwm_rw, op->o_conn );
282
283         /*
284          * If the connection dn is not null, an attempt to rewrite it is made
285          */
286         if ( op->o_conn->c_dn.bv_len != 0 ) {
287                 dc.rwmap = &lt->rwmap;
288                 dc.conn = op->o_conn;
289                 dc.rs = rs;
290                 dc.ctx = "bindDn";
291                 
292                 /*
293                  * Rewrite the bind dn if needed
294                  */
295                 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
296                                         &lsc->bound_dn) ) {
297                         send_ldap_result( op, rs );
298                         return rs->sr_err;
299                 }
300
301                 assert( lsc->bound_dn.bv_val );
302
303         } else {
304                 ber_str2bv( "", 0, 1, &lsc->bound_dn );
305         }
306
307         lsc->bound = META_UNBOUND;
308
309         /*
310          * The candidate is activated
311          */
312         lsc->candidate = META_CANDIDATE;
313         return LDAP_SUCCESS;
314 }
315
316 /*
317  * meta_back_getconn
318  * 
319  * Prepares the connection structure
320  * 
321  * FIXME: This function needs to receive some info on the type of operation
322  * it is invoked by, so that only the correct pool of candidate targets
323  * is initialized in case no connection was available yet.
324  * 
325  * At present a flag that says whether the candidate target must be unique
326  * is passed; eventually an operation agent will be used.
327  */
328 struct metaconn *
329 meta_back_getconn(
330                 Operation       *op,
331                 SlapReply       *rs,
332                 int             op_type,
333                 struct berval   *ndn,
334                 int             *candidate )
335 {
336         struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
337         struct metaconn *lc, lc_curr;
338         int cached = -1, i = -1, err = LDAP_SUCCESS;
339         int new_conn = 0;
340
341         /* Searches for a metaconn in the avl tree */
342         lc_curr.conn = op->o_conn;
343         ldap_pvt_thread_mutex_lock( &li->conn_mutex );
344         lc = (struct metaconn *)avl_find( li->conntree, 
345                 (caddr_t)&lc_curr, meta_back_conn_cmp );
346         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
347
348         /* Looks like we didn't get a bind. Open a new session... */
349         if ( !lc ) {
350                 lc = metaconn_alloc( li->ntargets );
351                 lc->conn = op->o_conn;
352                 new_conn = 1;
353         }
354
355         /*
356          * looks in cache, if any
357          */
358         if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
359                 cached = i = meta_dncache_get_target( &li->cache, ndn );
360         }
361
362         if ( op_type == META_OP_REQUIRE_SINGLE ) {
363
364                 /*
365                  * tries to get a unique candidate
366                  * (takes care of default target 
367                  */
368                 if ( i < 0 ) {
369                         i = meta_back_select_unique_candidate( li, ndn );
370                 }
371
372                 /*
373                  * if any is found, inits the connection
374                  */
375                 if ( i < 0 ) {
376                         if ( new_conn ) {
377                                 metaconn_free( lc );
378                         }
379
380                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
381                         return NULL;
382                 }
383                                 
384 #ifdef NEW_LOGGING
385                 LDAP_LOG( BACK_META, INFO,
386                         "meta_back_getconn: got target %d for ndn=\"%s\" from cache\n", 
387                         i, ndn->bv_val, 0 );
388 #else /* !NEW_LOGGING */
389                 Debug( LDAP_DEBUG_CACHE,
390         "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n%s",
391                                 i, ndn->bv_val, "" );
392 #endif /* !NEW_LOGGING */
393
394                 /*
395                  * Clear all other candidates
396                  */
397                 ( void )meta_clear_unused_candidates( li, lc, i, 0 );
398
399                 /*
400                  * The target is activated; if needed, it is
401                  * also init'd. In case of error, init_one_conn
402                  * sends the appropriate result.
403                  */
404                 err = init_one_conn( op, rs, li->targets[ i ],
405                                 &lc->conns[ i ] );
406                 if ( err != LDAP_SUCCESS ) {
407                 
408                         /*
409                          * FIXME: in case one target cannot
410                          * be init'd, should the other ones
411                          * be tried?
412                          */
413                         ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
414                         if ( new_conn ) {
415                                 metaconn_free( lc );
416                         }
417                         return NULL;
418                 }
419
420                 if ( candidate ) {
421                         *candidate = i;
422                 }
423
424         /*
425          * require all connections ...
426          */
427         } else if (op_type == META_OP_REQUIRE_ALL) {
428                 for ( i = 0; i < li->ntargets; i++ ) {
429
430                         /*
431                          * The target is activated; if needed, it is
432                          * also init'd
433                          */
434                         int lerr = init_one_conn( op, rs, li->targets[ i ],
435                                         &lc->conns[ i ] );
436                         if ( lerr != LDAP_SUCCESS ) {
437                                 
438                                 /*
439                                  * FIXME: in case one target cannot
440                                  * be init'd, should the other ones
441                                  * be tried?
442                                  */
443                                 ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
444                                 err = lerr;
445                                 continue;
446                         }
447                 }
448
449         /*
450          * if no unique candidate ...
451          */
452         } else {
453                 for ( i = 0; i < li->ntargets; i++ ) {
454                         if ( i == cached 
455                 || meta_back_is_candidate( &li->targets[ i ]->suffix, ndn ) ) {
456
457                                 /*
458                                  * The target is activated; if needed, it is
459                                  * also init'd
460                                  */
461                                 int lerr = init_one_conn( op, rs,
462                                                 li->targets[ i ],
463                                                 &lc->conns[ i ] );
464                                 if ( lerr != LDAP_SUCCESS ) {
465                                 
466                                         /*
467                                          * FIXME: in case one target cannot
468                                          * be init'd, should the other ones
469                                          * be tried?
470                                          */
471                                         ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
472                                         err = lerr;
473                                         continue;
474                                 }
475                         }
476                 }
477         }
478
479         /* clear out init_one_conn non-fatal errors */
480         rs->sr_err = LDAP_SUCCESS;
481         rs->sr_text = NULL;
482
483         if ( new_conn ) {
484                 
485                 /*
486                  * Inserts the newly created metaconn in the avl tree
487                  */
488                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
489                 err = avl_insert( &li->conntree, ( caddr_t )lc,
490                                 meta_back_conn_cmp, meta_back_conn_dup );
491
492 #if PRINT_CONNTREE > 0
493                 myprint( li->conntree );
494 #endif /* PRINT_CONNTREE */
495                 
496                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
497
498 #ifdef NEW_LOGGING
499                 LDAP_LOG( BACK_META, INFO,
500                         "meta_back_getconn: conn %ld inserted\n", lc->conn->c_connid, 0, 0);
501 #else /* !NEW_LOGGING */
502                 Debug( LDAP_DEBUG_TRACE,
503                         "=>meta_back_getconn: conn %ld inserted\n%s%s",
504                         lc->conn->c_connid, "", "" );
505 #endif /* !NEW_LOGGING */
506                 
507                 /*
508                  * Err could be -1 in case a duplicate metaconn is inserted
509                  */
510                 if ( err != 0 ) {
511                         rs->sr_err = LDAP_OTHER;
512                         rs->sr_text = "Internal server error";
513                         metaconn_free( lc );
514                         return NULL;
515                 }
516         } else {
517 #ifdef NEW_LOGGING
518                 LDAP_LOG( BACK_META, INFO,
519                         "meta_back_getconn: conn %ld fetched\n", lc->conn->c_connid, 0, 0 );
520 #else /* !NEW_LOGGING */
521                 Debug( LDAP_DEBUG_TRACE,
522                         "=>meta_back_getconn: conn %ld fetched\n%s%s",
523                         lc->conn->c_connid, "", "" );
524 #endif /* !NEW_LOGGING */
525         }
526         
527         return lc;
528 }
529