]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-meta/conn.c
Fix prev commit, return generated passwd
[openldap] / servers / slapd / back-meta / conn.c
index 66f820c979a0f269e3c28f4d119bd0a8c402ca79..c68cde490fa8e7ca343260531e6c7e477e3fe194 100644 (file)
@@ -1,7 +1,24 @@
-/*
- * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1999-2003 The OpenLDAP Foundation.
+ * All rights reserved.
  *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* ACKNOWLEDGEMENTS:
+ * This work was initially developed by the Howard Chu for inclusion
+ * in OpenLDAP Software and subsequently enhanced by Pierangelo
+ * Masarati.
+ */
+/* This is an altered version */
+/*
  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
  *
  * This work has been developed to fulfill the requirements
@@ -97,8 +114,7 @@ meta_back_conn_cmp(
        struct metaconn *lc1 = ( struct metaconn * )c1;
         struct metaconn *lc2 = ( struct metaconn * )c2;
        
-       return ( ( lc1->conn < lc2->conn ) ? -1 :
-                       ( ( lc1->conn > lc2-> conn ) ? 1 : 0 ) );
+       return SLAP_PTRCMP( lc1->conn, lc2->conn );
 }
 
 /*
@@ -170,7 +186,6 @@ static struct metaconn *
 metaconn_alloc( int ntargets )
 {
        struct metaconn *lc;
-       int i;
 
        assert( ntargets > 0 );
 
@@ -182,21 +197,20 @@ metaconn_alloc( int ntargets )
        /*
         * make it a null-terminated array ...
         */
-       lc->conns = ch_calloc( sizeof( struct metasingleconn ), ntargets+1 );
+       lc->conns = ch_calloc( sizeof( struct metasingleconn ), ntargets+1 );
        if ( lc->conns == NULL ) {
                free( lc );
                return NULL;
        }
-
-       for ( i = 0; i < ntargets; i++ ) {
-               lc->conns[ i ] =
-                       ch_calloc( sizeof( struct metasingleconn ), 1 );
-               if ( lc->conns[ i ] == NULL ) {
-                       charray_free( ( char ** )lc->conns );
-                       free( lc->conns );
-                       free( lc );
-                       return NULL;
-               }
+       lc->conns[ ntargets ].candidate = META_LAST_CONN;
+
+       for ( ; ntargets-- > 0; ) {
+               lc->conns[ ntargets ].ld = NULL;
+               lc->conns[ ntargets ].bound_dn.bv_val = NULL;
+               lc->conns[ ntargets ].bound_dn.bv_len = 0;
+               lc->conns[ ntargets ].cred.bv_val = NULL;
+               lc->conns[ ntargets ].cred.bv_len = 0;
+               lc->conns[ ntargets ].bound = META_UNBOUND;
        }
 
        lc->bound_target = META_BOUND_NONE;
@@ -219,12 +233,7 @@ metaconn_free(
        }
        
        if ( lc->conns ) {
-               int i;
-
-               for ( i = 0; lc->conns[ i ] != NULL; ++i ) {
-                       free( lc->conns[ i ] );
-               }
-               charray_free( ( char ** )lc->conns );
+               ch_free( lc->conns );
        }
 
        free( lc );
@@ -237,14 +246,15 @@ metaconn_free(
  */
 static int
 init_one_conn(
-               Connection *conn, 
-               Operation *op, 
-               struct metatarget *lt, 
-               int vers,
-               struct metasingleconn *lsc
+               Operation               *op,
+               SlapReply               *rs,
+               struct metatarget       *lt, 
+               struct metasingleconn   *lsc
                )
 {
-       int err;
+       struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
+       int             vers;
+       dncookie        dc;
 
        /*
         * Already init'ed
@@ -256,62 +266,53 @@ init_one_conn(
        /*
         * Attempts to initialize the connection to the target ds
         */
-       err = ldap_initialize( &lsc->ld, lt->uri );
-       if ( err != LDAP_SUCCESS ) {
-               return ldap_back_map_result( err );
+       rs->sr_err = ldap_initialize( &lsc->ld, lt->uri );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
+               return ldap_back_map_result( rs );
        }
-       
+
        /*
         * Set LDAP version. This will always succeed: If the client
         * bound with a particular version, then so can we.
         */
+       vers = op->o_conn->c_protocol;
        ldap_set_option( lsc->ld, LDAP_OPT_PROTOCOL_VERSION, &vers );
+       /* FIXME: configurable? */
+       ldap_set_option(lsc->ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
+
+       /*
+        * Set the network timeout if set
+        */
+       if (li->network_timeout != 0){
+               struct timeval network_timeout;
+
+               network_timeout.tv_usec = 0;
+               network_timeout.tv_sec = li->network_timeout;
+
+               ldap_set_option( lsc->ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &network_timeout);
+       }
 
        /*
         * Sets a cookie for the rewrite session
         */
-       ( void )rewrite_session_init( lt->rwinfo, conn );
+       ( void )rewrite_session_init( lt->rwmap.rwm_rw, op->o_conn );
 
        /*
         * If the connection dn is not null, an attempt to rewrite it is made
         */
-       if ( conn->c_cdn.bv_len != 0 ) {
+       if ( op->o_conn->c_dn.bv_len != 0 ) {
+               dc.rwmap = &lt->rwmap;
+               dc.conn = op->o_conn;
+               dc.rs = rs;
+               dc.ctx = "bindDn";
                
                /*
                 * Rewrite the bind dn if needed
                 */
-               lsc->bound_dn.bv_val = NULL;
-               switch ( rewrite_session( lt->rwinfo, "bindDn",
-                                       conn->c_cdn.bv_val, conn, 
-                                       &lsc->bound_dn.bv_val ) ) {
-               case REWRITE_REGEXEC_OK:
-                       if ( lsc->bound_dn.bv_val == NULL ) {
-                               ber_dupbv( &lsc->bound_dn, &conn->c_cdn );
-                       }
-#ifdef NEW_LOGGING
-                       LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
-                                       "[rw] bindDn: \"%s\" -> \"%s\"\n",
-                                       conn->c_cdn.bv_val, lsc->bound_dn.bv_val ));
-#else /* !NEW_LOGGING */
-                       Debug( LDAP_DEBUG_ARGS,
-                                       "rw> bindDn: \"%s\" -> \"%s\"\n",
-                                       conn->c_cdn.bv_val, lsc->bound_dn.bv_val, 0 );
-#endif /* !NEW_LOGGING */
-                       break;
-                       
-               case REWRITE_REGEXEC_UNWILLING:
-                       send_ldap_result( conn, op,
-                                       LDAP_UNWILLING_TO_PERFORM,
-                                       NULL, "Unwilling to perform",
-                                       NULL, NULL );
-                       return LDAP_UNWILLING_TO_PERFORM;
-                       
-               case REWRITE_REGEXEC_ERR:
-                       send_ldap_result( conn, op,
-                                       LDAP_OPERATIONS_ERROR,
-                                       NULL, "Operations error",
-                                       NULL, NULL );
-                       return LDAP_OPERATIONS_ERROR;
+               if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
+                                       &lsc->bound_dn) ) {
+                       send_ldap_result( op, rs );
+                       return rs->sr_err;
                }
 
                assert( lsc->bound_dn.bv_val );
@@ -343,19 +344,19 @@ init_one_conn(
  */
 struct metaconn *
 meta_back_getconn(
-               struct metainfo *li,
-               Connection      *conn,
                Operation       *op,
+               SlapReply       *rs,
                int             op_type,
                struct berval   *ndn,
                int             *candidate )
 {
+       struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
        struct metaconn *lc, lc_curr;
-       int vers, cached = -1, i = -1, err = LDAP_SUCCESS;
+       int cached = -1, i = -1, err = LDAP_SUCCESS;
        int new_conn = 0;
 
        /* Searches for a metaconn in the avl tree */
-       lc_curr.conn = conn;
+       lc_curr.conn = op->o_conn;
        ldap_pvt_thread_mutex_lock( &li->conn_mutex );
        lc = (struct metaconn *)avl_find( li->conntree, 
                (caddr_t)&lc_curr, meta_back_conn_cmp );
@@ -364,12 +365,10 @@ meta_back_getconn(
        /* Looks like we didn't get a bind. Open a new session... */
        if ( !lc ) {
                lc = metaconn_alloc( li->ntargets );
-               lc->conn = conn;
+               lc->conn = op->o_conn;
                new_conn = 1;
        }
 
-       vers = conn->c_protocol;
-
        /*
         * looks in cache, if any
         */
@@ -395,17 +394,14 @@ meta_back_getconn(
                                metaconn_free( lc );
                        }
 
-                       send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
-                               NULL, "", NULL, NULL );
-
+                       rs->sr_err = LDAP_NO_SUCH_OBJECT;
                        return NULL;
                }
                                
 #ifdef NEW_LOGGING
-               LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
-                               "meta_back_getconn: got target %d"
-                               " for ndn=\"%s\" from cache\n", 
-                               i, ndn->bv_val ));
+               LDAP_LOG( BACK_META, INFO,
+                       "meta_back_getconn: got target %d for ndn=\"%s\" from cache\n", 
+                       i, ndn->bv_val, 0 );
 #else /* !NEW_LOGGING */
                Debug( LDAP_DEBUG_CACHE,
        "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n%s",
@@ -422,8 +418,8 @@ meta_back_getconn(
                 * also init'd. In case of error, init_one_conn
                 * sends the appropriate result.
                 */
-               err = init_one_conn( conn, op, li->targets[ i ],
-                               vers, lc->conns[ i ] );
+               err = init_one_conn( op, rs, li->targets[ i ],
+                               &lc->conns[ i ] );
                if ( err != LDAP_SUCCESS ) {
                
                        /*
@@ -431,7 +427,7 @@ meta_back_getconn(
                         * be init'd, should the other ones
                         * be tried?
                         */
-                       ( void )meta_clear_one_candidate( lc->conns[ i ], 1 );
+                       ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
                        if ( new_conn ) {
                                metaconn_free( lc );
                        }
@@ -452,8 +448,8 @@ meta_back_getconn(
                         * The target is activated; if needed, it is
                         * also init'd
                         */
-                       int lerr = init_one_conn( conn, op, li->targets[ i ],
-                                       vers, lc->conns[ i ] );
+                       int lerr = init_one_conn( op, rs, li->targets[ i ],
+                                       &lc->conns[ i ] );
                        if ( lerr != LDAP_SUCCESS ) {
                                
                                /*
@@ -461,7 +457,7 @@ meta_back_getconn(
                                 * be init'd, should the other ones
                                 * be tried?
                                 */
-                               ( void )meta_clear_one_candidate( lc->conns[ i ], 1 );
+                               ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
                                err = lerr;
                                continue;
                        }
@@ -479,9 +475,9 @@ meta_back_getconn(
                                 * The target is activated; if needed, it is
                                 * also init'd
                                 */
-                               int lerr = init_one_conn( conn, op,
+                               int lerr = init_one_conn( op, rs,
                                                li->targets[ i ],
-                                               vers, lc->conns[ i ] );
+                                               &lc->conns[ i ] );
                                if ( lerr != LDAP_SUCCESS ) {
                                
                                        /*
@@ -489,7 +485,7 @@ meta_back_getconn(
                                         * be init'd, should the other ones
                                         * be tried?
                                         */
-                                       ( void )meta_clear_one_candidate( lc->conns[ i ], 1 );
+                                       ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
                                        err = lerr;
                                        continue;
                                }
@@ -497,6 +493,10 @@ meta_back_getconn(
                }
        }
 
+       /* clear out init_one_conn non-fatal errors */
+       rs->sr_err = LDAP_SUCCESS;
+       rs->sr_text = NULL;
+
        if ( new_conn ) {
                
                /*
@@ -513,9 +513,8 @@ meta_back_getconn(
                ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
 
 #ifdef NEW_LOGGING
-               LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
-                               "meta_back_getconn: conn %ld inserted\n",
-                               lc->conn->c_connid ));
+               LDAP_LOG( BACK_META, INFO,
+                       "meta_back_getconn: conn %ld inserted\n", lc->conn->c_connid, 0, 0);
 #else /* !NEW_LOGGING */
                Debug( LDAP_DEBUG_TRACE,
                        "=>meta_back_getconn: conn %ld inserted\n%s%s",
@@ -526,16 +525,15 @@ meta_back_getconn(
                 * Err could be -1 in case a duplicate metaconn is inserted
                 */
                if ( err != 0 ) {
-                       send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
-                       NULL, "Internal server error", NULL, NULL );
+                       rs->sr_err = LDAP_OTHER;
+                       rs->sr_text = "Internal server error";
                        metaconn_free( lc );
                        return NULL;
                }
        } else {
 #ifdef NEW_LOGGING
-               LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
-                               "meta_back_getconn: conn %ld fetched\n",
-                               lc->conn->c_connid ));
+               LDAP_LOG( BACK_META, INFO,
+                       "meta_back_getconn: conn %ld fetched\n", lc->conn->c_connid, 0, 0 );
 #else /* !NEW_LOGGING */
                Debug( LDAP_DEBUG_TRACE,
                        "=>meta_back_getconn: conn %ld fetched\n%s%s",