]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/request.c
rename ldap_pvt_init_utils() to ldap_int_utils_init() and provide
[openldap] / libraries / libldap / request.c
index 245c73b9486c417b57cb05aa5c1c6c515520cb11..7699c01236217883e02e522674aedb01d13429a0 100644 (file)
@@ -1,4 +1,8 @@
 /*
+ * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
+/*  Portions
  *  Copyright (c) 1995 Regents of the University of Michigan.
  *  All rights reserved.
  *
@@ -7,10 +11,6 @@
 
 #include "portable.h"
 
-#ifndef lint 
-static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
-#endif
-
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -21,22 +21,18 @@ static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of
 #include <ac/unistd.h>
 
 #include "ldap-int.h"
+#include "lber.h"
 
-#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
 static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPServer *srv, int any ));
 static void use_connection LDAP_P(( LDAP *ld, LDAPConn *lc ));
 static void free_servers LDAP_P(( LDAPServer *srvlist ));
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
-
 
 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
 static LDAPServer *dn2servers LDAP_P(( LDAP *ld, char *dn ));
 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 static BerElement *re_encode_request LDAP_P(( LDAP *ld, BerElement *origber,
     int msgid, char **dnp ));
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 
 BerElement *
@@ -44,7 +40,7 @@ ldap_alloc_ber_with_options( LDAP *ld )
 {
        BerElement      *ber;
 
-       if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULLBER ) {
+    if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULLBER ) {
                ld->ld_errno = LDAP_NO_MEMORY;
 #ifdef STR_TRANSLATION
        } else {
@@ -71,24 +67,47 @@ ldap_set_ber_options( LDAP *ld, BerElement *ber )
 
 
 int
-ldap_send_initial_request( LDAP *ld, unsigned long msgtype, char *dn,
+ldap_send_initial_request(
+       LDAP *ld,
+       unsigned long msgtype,
+       const char *dn,
        BerElement *ber )
 {
-#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
-       LDAPServer      *servers;
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
+       LDAPServer      *servers, *srv;
 
        Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
 
-#if !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) && !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
-       if ( ber_flush( &ld->ld_sb, ber, 1 ) != 0 ) {
-               ld->ld_errno = LDAP_SERVER_DOWN;
-               return( -1 );
+       if ( ! ber_pvt_sb_in_use(&ld->ld_sb ) ) {
+               /* not connected yet */
+
+               if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
+                   NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
+                   strdup( ld->ld_defhost )) == NULL ))
+               {
+                       if (srv != NULL) free( srv );
+                       ber_free( ber, 1 );
+                       ld->ld_errno = LDAP_NO_MEMORY;
+                       return( -1 );
+               }
+
+               srv->lsrv_port = ld->ld_defport;
+
+               if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 ))
+                       == NULL )
+               {
+                       if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
+                       free( (char *)srv );
+                       ber_free( ber, 1 );
+                       ld->ld_errno = LDAP_SERVER_DOWN;
+                       return( -1 );
+               }
+               ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
+
+               Debug( LDAP_DEBUG_TRACE,
+                       "ldap_delayed_open successful, ld_host is %s\n",
+                       ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
        }
 
-       ld->ld_errno = LDAP_SUCCESS;
-       return( ld->ld_msgid );
-#else /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS && !LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
        if (( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_DNS ) == LDAP_OPT_ON )
@@ -113,25 +132,22 @@ ldap_send_initial_request( LDAP *ld, unsigned long msgtype, char *dn,
                        }
                }
 #endif /* LDAP_DEBUG */
-       } else {
+       } else
 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
+       {
                /*
                 * use of DNS is turned off or this is an X.500 DN...
                 * use our default connection
                 */
                servers = NULL;
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
        }       
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
        return( ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, servers,
            NULL, 0 ));
-#endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS && !LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 }
 
 
 
-#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
 int
 ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest
        *parentreq, LDAPServer *srvlist, LDAPConn *lc, int bind )
@@ -254,8 +270,7 @@ ldap_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb,
         * XXX open connection synchronously for now
         */
        if (( lc = (LDAPConn *)calloc( 1, sizeof( LDAPConn ))) == NULL ||
-           ( !use_ldsb && ( sb = (Sockbuf *)calloc( 1, sizeof( Sockbuf )))
-           == NULL )) {
+           ( !use_ldsb && ( (sb = ber_sockbuf_alloc()) == NULL ))) {
                if ( lc != NULL ) {
                        free( (char *)lc );
                }
@@ -279,7 +294,7 @@ ldap_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb,
 
                if ( srv == NULL ) {
                    if ( !use_ldsb ) {
-                       free( (char *)lc->lconn_sb );
+                       ber_sockbuf_free( lc->lconn_sb );
                    }
                    free( (char *)lc );
                    ld->ld_errno = LDAP_SERVER_DOWN;
@@ -309,7 +324,7 @@ ldap_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb,
 
                freepasswd = err = 0;
 
-               if ( ld->ld_rebindproc == NULL ) {
+               if ( ld->ld_rebindproc == 0 ) {
                        binddn = passwd = "";
                        authmethod = LDAP_AUTH_SIMPLE;
                } else {
@@ -399,12 +414,11 @@ ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
                if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
                        ldap_mark_select_clear( ld, lc->lconn_sb );
                        if ( unbind ) {
-                               ldap_send_unbind( ld, lc->lconn_sb );
+                               ldap_send_unbind( ld, lc->lconn_sb, NULL, NULL );
                        }
                        ldap_close_connection( lc->lconn_sb );
-                       if ( lc->lconn_sb->sb_ber.ber_buf != NULL ) {
-                               free( lc->lconn_sb->sb_ber.ber_buf );
-                       }
+                       ber_pvt_sb_destroy( lc->lconn_sb );
+                       ber_clear( &lc->lconn_ber, 1 );
                }
                prevlc = NULL;
                for ( tmplc = ld->ld_conns; tmplc != NULL;
@@ -424,7 +438,7 @@ ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
                        free( lc->lconn_krbinstance );
                }
                if ( lc->lconn_sb != &ld->ld_sb ) {
-                       free( (char *)lc->lconn_sb );
+                       ber_sockbuf_free( lc->lconn_sb );
                }
                free( lc );
                Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
@@ -458,7 +472,7 @@ ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
                    "NeedSocket" : ( lc->lconn_status ==
                    LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
                fprintf( stderr, "  last used: %s\n",
-                   ldap_int_ctime( &lc->lconn_lastused, timebuf ));
+                   ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
                if ( !all ) {
                        break;
                }
@@ -568,10 +582,8 @@ free_servers( LDAPServer *srvlist )
        srvlist = nextsrv;
     }
 }
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
 
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 /*
  * XXX merging of errors in this routine needs to be improved
  */
@@ -686,7 +698,7 @@ ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
                                return( -1 );
                        }
 
-                       if (( srv->lsrv_host = ldap_strdup( tmpref )) == NULL ) {
+                       if (( srv->lsrv_host = strdup( tmpref )) == NULL ) {
                                free( (char *)srv );
                                ber_free( ber, 1 );
                                ld->ld_errno = LDAP_NO_MEMORY;
@@ -781,7 +793,7 @@ re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp )
         * all LDAP requests are sequences that start with a message id,
         * followed by a sequence that is tagged with the operation code
         */
-       if ( ber_scanf( &tmpber, "{i", &along ) != LDAP_TAG_MSGID ||
+       if ( ber_scanf( &tmpber, "{i", /*}*/ &along ) != LDAP_TAG_MSGID ||
            ( tag = ber_skip_tag( &tmpber, &along )) == LBER_DEFAULT ) {
                 ld->ld_errno = LDAP_DECODING_ERROR;
                return( NULL );
@@ -812,9 +824,9 @@ re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp )
        }
 
        if ( tag == LDAP_REQ_BIND ) {
-               rc = ber_printf( ber, "{it{is", msgid, tag, ver, *dnp );
+               rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, *dnp );
        } else {
-               rc = ber_printf( ber, "{it{s", msgid, tag, *dnp );
+               rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, *dnp );
        }
 
        if ( rc == -1 ) {
@@ -824,7 +836,7 @@ re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp )
 
        if ( ber_write( ber, tmpber.ber_ptr, ( tmpber.ber_end -
            tmpber.ber_ptr ), 0 ) != ( tmpber.ber_end - tmpber.ber_ptr ) ||
-           ber_printf( ber, "}}" ) == -1 ) {
+           ber_printf( ber, /*{{*/ "}}" ) == -1 ) {
                ld->ld_errno = LDAP_ENCODING_ERROR;
                ber_free( ber, 1 );
                return( NULL );
@@ -834,7 +846,7 @@ re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp )
        if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
                Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
                    0, 0, 0 );
-               ber_dump( ber, 0 );
+               ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
        }
 #endif /* LDAP_DEBUG */
 
@@ -855,7 +867,6 @@ ldap_find_request_by_msgid( LDAP *ld, int msgid )
 
        return( lr );
 }
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 
 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
@@ -920,9 +931,9 @@ dn2servers( LDAP *ld, char *dn )    /* dn can also be a domain.... */
                        prevsrv = srv;
                        
                        /* copy in info. */
-                       if (( srv->lsrv_host = ldap_strdup( host )) == NULL ||
+                       if (( srv->lsrv_host = strdup( host )) == NULL ||
                            ( server_dn != NULL && ( srv->lsrv_dn =
-                           ldap_strdup( server_dn )) == NULL )) {
+                           strdup( server_dn )) == NULL )) {
                                free_servers( srvlist );
                                srvlist = NULL;
                                break;          /* exit loop & return */