]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/conn.c
StartTLS (ITS#3507) + chain overlay fixes and improvements
[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->mc_conn, lc2->mc_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->mc_conn == lc2->mc_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 )->mc_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->mc_conns = ch_calloc( sizeof( struct metasingleconn ), ntargets+1 );
140         if ( lc->mc_conns == NULL ) {
141                 free( lc );
142                 return NULL;
143         }
144         lc->mc_conns[ ntargets ].msc_candidate = META_LAST_CONN;
145
146         for ( ; ntargets-- > 0; ) {
147                 lc->mc_conns[ ntargets ].msc_ld = NULL;
148                 BER_BVZERO( &lc->mc_conns[ ntargets ].msc_bound_ndn );
149                 BER_BVZERO( &lc->mc_conns[ ntargets ].msc_cred );
150                 lc->mc_conns[ ntargets ].msc_bound = META_UNBOUND;
151         }
152
153         lc->mc_bound_target = META_BOUND_NONE;
154
155         return lc;
156 }
157
158 /*
159  * metaconn_free
160  *
161  * clears a metaconn
162  */
163 static void
164 metaconn_free(
165                 struct metaconn *lc
166 )
167 {
168         if ( !lc ) {
169                 return;
170         }
171         
172         if ( lc->mc_conns ) {
173                 ch_free( lc->mc_conns );
174         }
175
176         free( lc );
177 }
178
179 /*
180  * init_one_conn
181  * 
182  * Initializes one connection
183  */
184 static int
185 init_one_conn(
186                 Operation               *op,
187                 SlapReply               *rs,
188                 struct metatarget       *lt, 
189                 struct metasingleconn   *lsc
190                 )
191 {
192         struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
193         int             vers;
194         dncookie        dc;
195
196         /*
197          * Already init'ed
198          */
199         if ( lsc->msc_ld != NULL ) {
200                 return LDAP_SUCCESS;
201         }
202        
203         /*
204          * Attempts to initialize the connection to the target ds
205          */
206         rs->sr_err = ldap_initialize( &lsc->msc_ld, lt->mt_uri );
207         if ( rs->sr_err != LDAP_SUCCESS ) {
208                 return slap_map_api2result( rs );
209         }
210
211         /*
212          * Set LDAP version. This will always succeed: If the client
213          * bound with a particular version, then so can we.
214          */
215         vers = op->o_conn->c_protocol;
216         ldap_set_option( lsc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &vers );
217         /* FIXME: configurable? */
218         ldap_set_option( lsc->msc_ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
219
220         /*
221          * Set the network timeout if set
222          */
223         if (li->network_timeout != 0){
224                 struct timeval  network_timeout;
225
226                 network_timeout.tv_usec = 0;
227                 network_timeout.tv_sec = li->network_timeout;
228
229                 ldap_set_option( lsc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
230                                 (void *)&network_timeout );
231         }
232
233         /*
234          * Sets a cookie for the rewrite session
235          */
236         ( void )rewrite_session_init( lt->mt_rwmap.rwm_rw, op->o_conn );
237
238         /*
239          * If the connection DN is not null, an attempt to rewrite it is made
240          */
241         if ( op->o_conn->c_dn.bv_len != 0 ) {
242                 dc.rwmap = &lt->mt_rwmap;
243                 dc.conn = op->o_conn;
244                 dc.rs = rs;
245                 dc.ctx = "bindDN";
246                 
247                 /*
248                  * Rewrite the bind dn if needed
249                  */
250                 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
251                                         &lsc->msc_bound_ndn ) )
252                 {
253                         send_ldap_result( op, rs );
254                         return rs->sr_err;
255                 }
256
257                 /* copy the DN idf needed */
258                 if ( lsc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
259                         ber_dupbv( &lsc->msc_bound_ndn, &op->o_conn->c_dn );
260                 }
261
262                 assert( lsc->msc_bound_ndn.bv_val );
263
264         } else {
265                 ber_str2bv( "", 0, 1, &lsc->msc_bound_ndn );
266         }
267
268         lsc->msc_bound = META_UNBOUND;
269
270         /*
271          * The candidate is activated
272          */
273         lsc->msc_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 = META_TARGET_NONE,
300                         i = META_TARGET_NONE,
301                         err = LDAP_SUCCESS,
302                         new_conn = 0;
303
304         /* Searches for a metaconn in the avl tree */
305         lc_curr.mc_conn = op->o_conn;
306         ldap_pvt_thread_mutex_lock( &li->conn_mutex );
307         lc = (struct metaconn *)avl_find( li->conntree, 
308                 (caddr_t)&lc_curr, meta_back_conn_cmp );
309         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
310
311         /* Looks like we didn't get a bind. Open a new session... */
312         if ( !lc ) {
313                 lc = metaconn_alloc( li->ntargets );
314                 lc->mc_conn = op->o_conn;
315                 new_conn = 1;
316         }
317
318         /*
319          * require all connections ...
320          */
321         if ( op_type == META_OP_REQUIRE_ALL ) {
322                 for ( i = 0; i < li->ntargets; i++ ) {
323
324                         /*
325                          * The target is activated; if needed, it is
326                          * also init'd
327                          */
328                         int lerr = init_one_conn( op, rs, li->targets[ i ],
329                                         &lc->mc_conns[ i ] );
330                         if ( lerr != LDAP_SUCCESS ) {
331                                 
332                                 /*
333                                  * FIXME: in case one target cannot
334                                  * be init'd, should the other ones
335                                  * be tried?
336                                  */
337                                 ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
338                                 err = lerr;
339                                 continue;
340                         }
341                 }
342                 goto done;
343         }
344         
345         /*
346          * looks in cache, if any
347          */
348         if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
349                 cached = i = meta_dncache_get_target( &li->cache, ndn );
350         }
351
352         if ( op_type == META_OP_REQUIRE_SINGLE ) {
353
354                 /*
355                  * tries to get a unique candidate
356                  * (takes care of default target 
357                  */
358                 if ( i == META_TARGET_NONE ) {
359                         i = meta_back_select_unique_candidate( li, ndn );
360                 }
361
362                 /*
363                  * if any is found, inits the connection
364                  */
365                 if ( i == META_TARGET_NONE ) {
366                         if ( new_conn ) {
367                                 metaconn_free( lc );
368                         }
369
370                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
371                         return NULL;
372                 }
373                                 
374                 Debug( LDAP_DEBUG_CACHE,
375         "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n",
376                                 i, ndn->bv_val, 0 );
377
378                 /*
379                  * Clear all other candidates
380                  */
381                 ( void )meta_clear_unused_candidates( li, lc, i, 0 );
382
383                 /*
384                  * The target is activated; if needed, it is
385                  * also init'd. In case of error, init_one_conn
386                  * sends the appropriate result.
387                  */
388                 err = init_one_conn( op, rs, li->targets[ i ],
389                                 &lc->mc_conns[ i ] );
390                 if ( err != LDAP_SUCCESS ) {
391                 
392                         /*
393                          * FIXME: in case one target cannot
394                          * be init'd, should the other ones
395                          * be tried?
396                          */
397                         ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
398                         if ( new_conn ) {
399                                 metaconn_free( lc );
400                         }
401                         return NULL;
402                 }
403
404                 if ( candidate ) {
405                         *candidate = i;
406                 }
407
408         /*
409          * if no unique candidate ...
410          */
411         } else {
412                 for ( i = 0; i < li->ntargets; i++ ) {
413                         if ( i == cached 
414                                 || meta_back_is_candidate( &li->targets[ i ]->mt_nsuffix, ndn ) )
415                         {
416
417                                 /*
418                                  * The target is activated; if needed, it is
419                                  * also init'd
420                                  */
421                                 int lerr = init_one_conn( op, rs,
422                                                 li->targets[ i ],
423                                                 &lc->mc_conns[ i ] );
424                                 if ( lerr != LDAP_SUCCESS ) {
425                                 
426                                         /*
427                                          * FIXME: in case one target cannot
428                                          * be init'd, should the other ones
429                                          * be tried?
430                                          */
431                                         ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
432                                         err = lerr;
433                                         continue;
434                                 }
435                         }
436                 }
437         }
438
439 done:;
440         /* clear out init_one_conn non-fatal errors */
441         rs->sr_err = LDAP_SUCCESS;
442         rs->sr_text = NULL;
443
444         if ( new_conn ) {
445                 
446                 /*
447                  * Inserts the newly created metaconn in the avl tree
448                  */
449                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
450                 err = avl_insert( &li->conntree, ( caddr_t )lc,
451                                 meta_back_conn_cmp, meta_back_conn_dup );
452
453 #if PRINT_CONNTREE > 0
454                 myprint( li->conntree );
455 #endif /* PRINT_CONNTREE */
456                 
457                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
458
459                 Debug( LDAP_DEBUG_TRACE,
460                         "=>meta_back_getconn: conn %ld inserted\n",
461                         lc->mc_conn->c_connid, 0, 0 );
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
473         } else {
474                 Debug( LDAP_DEBUG_TRACE,
475                         "=>meta_back_getconn: conn %ld fetched\n",
476                         lc->mc_conn->c_connid, 0, 0 );
477         }
478         
479         return lc;
480 }
481