]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/conn.c
just in case, remove cruft (ITS#3804)
[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-2005 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 slap_map_api2result( 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                 /* copy the DN idf needed */
258                 if ( lsc->bound_dn.bv_val == op->o_conn->c_dn.bv_val ) {
259                         ber_dupbv( &lsc->bound_dn, &op->o_conn->c_dn );
260                 }
261
262                 assert( lsc->bound_dn.bv_val );
263
264         } else {
265                 ber_str2bv( "", 0, 1, &lsc->bound_dn );
266         }
267
268         lsc->bound = META_UNBOUND;
269
270         /*
271          * The candidate is activated
272          */
273         lsc->candidate = META_CANDIDATE;
274         return LDAP_SUCCESS;
275 }
276
277 /*
278  * meta_back_getconn
279  * 
280  * Prepares the connection structure
281  * 
282  * FIXME: This function needs to receive some info on the type of operation
283  * it is invoked by, so that only the correct pool of candidate targets
284  * is initialized in case no connection was available yet.
285  * 
286  * At present a flag that says whether the candidate target must be unique
287  * is passed; eventually an operation agent will be used.
288  */
289 struct metaconn *
290 meta_back_getconn(
291                 Operation       *op,
292                 SlapReply       *rs,
293                 int             op_type,
294                 struct berval   *ndn,
295                 int             *candidate )
296 {
297         struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
298         struct metaconn *lc, lc_curr;
299         int cached = -1, i = -1, err = LDAP_SUCCESS;
300         int new_conn = 0;
301
302         /* Searches for a metaconn in the avl tree */
303         lc_curr.conn = op->o_conn;
304         ldap_pvt_thread_mutex_lock( &li->conn_mutex );
305         lc = (struct metaconn *)avl_find( li->conntree, 
306                 (caddr_t)&lc_curr, meta_back_conn_cmp );
307         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
308
309         /* Looks like we didn't get a bind. Open a new session... */
310         if ( !lc ) {
311                 lc = metaconn_alloc( li->ntargets );
312                 lc->conn = op->o_conn;
313                 new_conn = 1;
314         }
315
316         /*
317          * looks in cache, if any
318          */
319         if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
320                 cached = i = meta_dncache_get_target( &li->cache, ndn );
321         }
322
323         if ( op_type == META_OP_REQUIRE_SINGLE ) {
324
325                 /*
326                  * tries to get a unique candidate
327                  * (takes care of default target 
328                  */
329                 if ( i < 0 ) {
330                         i = meta_back_select_unique_candidate( li, ndn );
331                 }
332
333                 /*
334                  * if any is found, inits the connection
335                  */
336                 if ( i < 0 ) {
337                         if ( new_conn ) {
338                                 metaconn_free( lc );
339                         }
340
341                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
342                         rs->sr_text = NULL;
343                         return NULL;
344                 }
345                                 
346 #ifdef NEW_LOGGING
347                 LDAP_LOG( BACK_META, INFO,
348                         "meta_back_getconn: got target %d for ndn=\"%s\" from cache\n", 
349                         i, ndn->bv_val, 0 );
350 #else /* !NEW_LOGGING */
351                 Debug( LDAP_DEBUG_CACHE,
352         "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n%s",
353                                 i, ndn->bv_val, "" );
354 #endif /* !NEW_LOGGING */
355
356                 /*
357                  * Clear all other candidates
358                  */
359                 ( void )meta_clear_unused_candidates( li, lc, i, 0 );
360
361                 /*
362                  * The target is activated; if needed, it is
363                  * also init'd. In case of error, init_one_conn
364                  * sends the appropriate result.
365                  */
366                 err = init_one_conn( op, rs, li->targets[ i ],
367                                 &lc->conns[ i ] );
368                 if ( err != LDAP_SUCCESS ) {
369                 
370                         /*
371                          * FIXME: in case one target cannot
372                          * be init'd, should the other ones
373                          * be tried?
374                          */
375                         ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
376                         if ( new_conn ) {
377                                 metaconn_free( lc );
378                         }
379                         return NULL;
380                 }
381
382                 if ( candidate ) {
383                         *candidate = i;
384                 }
385
386         /*
387          * require all connections ...
388          */
389         } else if (op_type == META_OP_REQUIRE_ALL) {
390                 for ( i = 0; i < li->ntargets; i++ ) {
391
392                         /*
393                          * The target is activated; if needed, it is
394                          * also init'd
395                          */
396                         int lerr = init_one_conn( op, rs, li->targets[ i ],
397                                         &lc->conns[ i ] );
398                         if ( lerr != LDAP_SUCCESS ) {
399                                 
400                                 /*
401                                  * FIXME: in case one target cannot
402                                  * be init'd, should the other ones
403                                  * be tried?
404                                  */
405                                 ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
406                                 err = lerr;
407                                 continue;
408                         }
409                 }
410
411         /*
412          * if no unique candidate ...
413          */
414         } else {
415                 for ( i = 0; i < li->ntargets; i++ ) {
416                         if ( i == cached 
417                 || meta_back_is_candidate( &li->targets[ i ]->suffix, ndn ) ) {
418
419                                 /*
420                                  * The target is activated; if needed, it is
421                                  * also init'd
422                                  */
423                                 int lerr = init_one_conn( op, rs,
424                                                 li->targets[ i ],
425                                                 &lc->conns[ i ] );
426                                 if ( lerr != LDAP_SUCCESS ) {
427                                 
428                                         /*
429                                          * FIXME: in case one target cannot
430                                          * be init'd, should the other ones
431                                          * be tried?
432                                          */
433                                         ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
434                                         err = lerr;
435                                         continue;
436                                 }
437                         }
438                 }
439         }
440
441         /* clear out init_one_conn non-fatal errors */
442         rs->sr_err = LDAP_SUCCESS;
443         rs->sr_text = NULL;
444
445         if ( new_conn ) {
446                 
447                 /*
448                  * Inserts the newly created metaconn in the avl tree
449                  */
450                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
451                 err = avl_insert( &li->conntree, ( caddr_t )lc,
452                                 meta_back_conn_cmp, meta_back_conn_dup );
453
454 #if PRINT_CONNTREE > 0
455                 myprint( li->conntree );
456 #endif /* PRINT_CONNTREE */
457                 
458                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
459
460 #ifdef NEW_LOGGING
461                 LDAP_LOG( BACK_META, INFO,
462                         "meta_back_getconn: conn %ld inserted\n", lc->conn->c_connid, 0, 0);
463 #else /* !NEW_LOGGING */
464                 Debug( LDAP_DEBUG_TRACE,
465                         "=>meta_back_getconn: conn %ld inserted\n%s%s",
466                         lc->conn->c_connid, "", "" );
467 #endif /* !NEW_LOGGING */
468                 
469                 /*
470                  * Err could be -1 in case a duplicate metaconn is inserted
471                  */
472                 if ( err != 0 ) {
473                         rs->sr_err = LDAP_OTHER;
474                         rs->sr_text = "Internal server error";
475                         metaconn_free( lc );
476                         return NULL;
477                 }
478         } else {
479 #ifdef NEW_LOGGING
480                 LDAP_LOG( BACK_META, INFO,
481                         "meta_back_getconn: conn %ld fetched\n", lc->conn->c_connid, 0, 0 );
482 #else /* !NEW_LOGGING */
483                 Debug( LDAP_DEBUG_TRACE,
484                         "=>meta_back_getconn: conn %ld fetched\n%s%s",
485                         lc->conn->c_connid, "", "" );
486 #endif /* !NEW_LOGGING */
487         }
488         
489         return lc;
490 }
491