]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/conn.c
use asynchronous StartTLS
[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                 ldap_back_send_t        sendok )
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                 goto error_return;
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
218         /* automatically chase referrals ("chase-referrals"/"dont-chase-referrals" statement) */
219         if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
220                 ldap_set_option( lsc->msc_ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
221         }
222
223         /* start TLS ("start-tls"/"try-start-tls" statements) */
224         if ( ( LDAP_BACK_USE_TLS( li ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( li ) ) )
225                         && !ldap_is_ldaps_url( lt->mt_uri ) )
226         {
227 #if 0
228                 int             rc, msgid;
229                 LDAPMessage     *res;
230                 int             retries = 1;
231
232 retry:;
233                 rc = ldap_start_tls( lsc->msc_ld, NULL, NULL, &msgid );
234                 if ( rc == LDAP_SUCCESS ) {
235                         struct timeval  tv = { 0, 0 };
236
237                         rc = ldap_result( lsc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
238                         if ( rc < 0 ) {
239                                 rs->sr_err = LDAP_OTHER;
240
241                         } else if ( rc == 0 ) {
242                                 if ( retries ) {
243                                         retries--;
244                                         tv.tv_sec = 0;
245                                         tv.tv_usec = 100000;
246                                         goto retry;
247                                 }
248                                 rs->sr_err = LDAP_OTHER;
249
250                         } else {
251                                 if ( rc == LDAP_RES_EXTENDED ) {
252                                         rc = ldap_parse_result( lsc->msc_ld, res,
253                                                 &rs->sr_err, NULL, NULL, NULL, NULL, 1 );
254                                         if ( rc != LDAP_SUCCESS ) {
255                                                 rs->sr_err = rc;
256
257                                         /* FIXME: in case a referral 
258                                          * is returned, should we try
259                                          * using it instead of the 
260                                          * configured URI? */
261                                         } else if ( rs->sr_err == LDAP_REFERRAL ) {
262                                                 rs->sr_err = LDAP_OTHER;
263                                                 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
264                                         }
265
266                                 } else {
267                                         ldap_msgfree( res );
268                                         rs->sr_err = LDAP_OTHER;
269                                 }
270                         }
271                 }
272 #else
273                 rs->sr_err = ldap_start_tls_s( lsc->msc_ld, NULL, NULL );
274 #endif
275
276                 /* if StartTLS is requested, only attempt it if the URL
277                  * is not "ldaps://"; this may occur not only in case
278                  * of misconfiguration, but also when used in the chain 
279                  * overlay, where the "uri" can be parsed out of a referral */
280                 if ( rs->sr_err == LDAP_SERVER_DOWN
281                                 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( li ) ) )
282                 {
283                         ldap_unbind_ext_s( lsc->msc_ld, NULL, NULL );
284                         goto error_return;
285                 }
286         }
287
288         /*
289          * Set the network timeout if set
290          */
291         if (li->network_timeout != 0){
292                 struct timeval  network_timeout;
293
294                 network_timeout.tv_usec = 0;
295                 network_timeout.tv_sec = li->network_timeout;
296
297                 ldap_set_option( lsc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
298                                 (void *)&network_timeout );
299         }
300
301         /*
302          * Sets a cookie for the rewrite session
303          */
304         ( void )rewrite_session_init( lt->mt_rwmap.rwm_rw, op->o_conn );
305
306         /*
307          * If the connection DN is not null, an attempt to rewrite it is made
308          */
309         if ( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
310                 dc.rwmap = &lt->mt_rwmap;
311                 dc.conn = op->o_conn;
312                 dc.rs = rs;
313                 dc.ctx = "bindDN";
314                 
315                 /*
316                  * Rewrite the bind dn if needed
317                  */
318                 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
319                                         &lsc->msc_bound_ndn ) )
320                 {
321                         goto error_return;
322                 }
323
324                 /* copy the DN idf needed */
325                 if ( lsc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
326                         ber_dupbv( &lsc->msc_bound_ndn, &op->o_conn->c_dn );
327                 }
328
329                 assert( !BER_BVISNULL( &lsc->msc_bound_ndn ) );
330
331         } else {
332                 ber_str2bv( "", 0, 1, &lsc->msc_bound_ndn );
333         }
334
335         lsc->msc_bound = META_UNBOUND;
336
337 error_return:;
338         if ( rs->sr_err != LDAP_SUCCESS ) {
339                 rs->sr_err = slap_map_api2result( rs );
340                 if ( sendok & LDAP_BACK_SENDERR ) {
341                         send_ldap_result( op, rs );
342                         rs->sr_text = NULL;
343                 }
344
345         } else {
346
347                 /*
348                  * The candidate is activated
349                  */
350                 lsc->msc_candidate = META_CANDIDATE;
351         }
352
353         return rs->sr_err;
354 }
355
356 /*
357  * meta_back_getconn
358  * 
359  * Prepares the connection structure
360  * 
361  * FIXME: This function needs to receive some info on the type of operation
362  * it is invoked by, so that only the correct pool of candidate targets
363  * is initialized in case no connection was available yet.
364  * 
365  * At present a flag that says whether the candidate target must be unique
366  * is passed; eventually an operation agent will be used.
367  */
368 struct metaconn *
369 meta_back_getconn(
370                 Operation               *op,
371                 SlapReply               *rs,
372                 int                     op_type,
373                 struct berval           *ndn,
374                 int                     *candidate,
375                 ldap_back_send_t        sendok )
376 {
377         struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
378         struct metaconn *lc, lc_curr;
379         int             cached = META_TARGET_NONE,
380                         i = META_TARGET_NONE,
381                         err = LDAP_SUCCESS,
382                         new_conn = 0;
383
384         /* Searches for a metaconn in the avl tree */
385         lc_curr.mc_conn = op->o_conn;
386         ldap_pvt_thread_mutex_lock( &li->conn_mutex );
387         lc = (struct metaconn *)avl_find( li->conntree, 
388                 (caddr_t)&lc_curr, meta_back_conn_cmp );
389         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
390
391         /* Looks like we didn't get a bind. Open a new session... */
392         if ( !lc ) {
393                 lc = metaconn_alloc( li->ntargets );
394                 lc->mc_conn = op->o_conn;
395                 new_conn = 1;
396         }
397
398         /*
399          * require all connections ...
400          */
401         if ( op_type == META_OP_REQUIRE_ALL ) {
402                 for ( i = 0; i < li->ntargets; i++ ) {
403
404                         /*
405                          * The target is activated; if needed, it is
406                          * also init'd
407                          */
408                         int lerr = init_one_conn( op, rs, li->targets[ i ],
409                                         &lc->mc_conns[ i ], sendok );
410                         if ( lerr != LDAP_SUCCESS ) {
411                                 
412                                 /*
413                                  * FIXME: in case one target cannot
414                                  * be init'd, should the other ones
415                                  * be tried?
416                                  */
417                                 ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
418                                 err = lerr;
419                                 continue;
420                         }
421                 }
422                 goto done;
423         }
424         
425         /*
426          * looks in cache, if any
427          */
428         if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
429                 cached = i = meta_dncache_get_target( &li->cache, ndn );
430         }
431
432         if ( op_type == META_OP_REQUIRE_SINGLE ) {
433
434                 /*
435                  * tries to get a unique candidate
436                  * (takes care of default target 
437                  */
438                 if ( i == META_TARGET_NONE ) {
439                         i = meta_back_select_unique_candidate( li, ndn );
440                 }
441
442                 /*
443                  * if any is found, inits the connection
444                  */
445                 if ( i == META_TARGET_NONE ) {
446                         if ( new_conn ) {
447                                 metaconn_free( lc );
448                         }
449
450                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
451                         return NULL;
452                 }
453                                 
454                 Debug( LDAP_DEBUG_CACHE,
455         "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n",
456                                 i, ndn->bv_val, 0 );
457
458                 /*
459                  * Clear all other candidates
460                  */
461                 ( void )meta_clear_unused_candidates( li, lc, i, 0 );
462
463                 /*
464                  * The target is activated; if needed, it is
465                  * also init'd. In case of error, init_one_conn
466                  * sends the appropriate result.
467                  */
468                 err = init_one_conn( op, rs, li->targets[ i ],
469                                 &lc->mc_conns[ i ], sendok );
470                 if ( err != LDAP_SUCCESS ) {
471                 
472                         /*
473                          * FIXME: in case one target cannot
474                          * be init'd, should the other ones
475                          * be tried?
476                          */
477                         ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
478                         if ( new_conn ) {
479                                 metaconn_free( lc );
480                         }
481                         return NULL;
482                 }
483
484                 if ( candidate ) {
485                         *candidate = i;
486                 }
487
488         /*
489          * if no unique candidate ...
490          */
491         } else {
492                 for ( i = 0; i < li->ntargets; i++ ) {
493                         if ( i == cached 
494                                 || meta_back_is_candidate( &li->targets[ i ]->mt_nsuffix, ndn ) )
495                         {
496
497                                 /*
498                                  * The target is activated; if needed, it is
499                                  * also init'd
500                                  */
501                                 int lerr = init_one_conn( op, rs,
502                                                 li->targets[ i ],
503                                                 &lc->mc_conns[ i ], sendok );
504                                 if ( lerr != LDAP_SUCCESS ) {
505                                 
506                                         /*
507                                          * FIXME: in case one target cannot
508                                          * be init'd, should the other ones
509                                          * be tried?
510                                          */
511                                         ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
512                                         err = lerr;
513                                         continue;
514                                 }
515                         }
516                 }
517         }
518
519 done:;
520         /* clear out init_one_conn non-fatal errors */
521         rs->sr_err = LDAP_SUCCESS;
522         rs->sr_text = NULL;
523
524         if ( new_conn ) {
525                 
526                 /*
527                  * Inserts the newly created metaconn in the avl tree
528                  */
529                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
530                 err = avl_insert( &li->conntree, ( caddr_t )lc,
531                                 meta_back_conn_cmp, meta_back_conn_dup );
532
533 #if PRINT_CONNTREE > 0
534                 myprint( li->conntree );
535 #endif /* PRINT_CONNTREE */
536                 
537                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
538
539                 Debug( LDAP_DEBUG_TRACE,
540                         "=>meta_back_getconn: conn %ld inserted\n",
541                         lc->mc_conn->c_connid, 0, 0 );
542                 
543                 /*
544                  * Err could be -1 in case a duplicate metaconn is inserted
545                  */
546                 if ( err != 0 ) {
547                         rs->sr_err = LDAP_OTHER;
548                         rs->sr_text = "Internal server error";
549                         metaconn_free( lc );
550                         return NULL;
551                 }
552
553         } else {
554                 Debug( LDAP_DEBUG_TRACE,
555                         "=>meta_back_getconn: conn %ld fetched\n",
556                         lc->mc_conn->c_connid, 0, 0 );
557         }
558         
559         return lc;
560 }
561