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