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