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