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