[WLDAP32] Sync with Wine Staging 4.18. CORE-16441
[reactos.git] / dll / win32 / wldap32 / init.c
index b1a46cd..5b600a2 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
-#include <config.h>
-
-//#include "wine/port.h"
-#include <wine/debug.h>
-
-//#include <stdarg.h>
-
-//#include "windef.h"
-//#include "winbase.h"
-//#include "winnls.h"
+#include "config.h"
+#include "wine/port.h"
 
+#include <stdio.h>
+#include <stdarg.h>
 #ifdef HAVE_LDAP_H
 #include <ldap.h>
 #endif
 
+#include "windef.h"
+#include "winbase.h"
+#include "winnls.h"
+#include "wine/winternl.h"
+
 #include "winldap_private.h"
-//#include "wldap32.h"
+#include "wldap32.h"
+#include "wine/debug.h"
 
 #ifdef HAVE_LDAP
 /* Should eventually be determined by the algorithm documented on MSDN. */
@@ -63,10 +63,9 @@ static char **split_hostnames( const char *hostnames )
         p++;
     }
 
-    res = HeapAlloc( GetProcessHeap(), 0, (i + 1) * sizeof(char *) );
-    if (!res)
+    if (!(res = heap_alloc( (i + 1) * sizeof(char *) )))
     {
-        HeapFree( GetProcessHeap(), 0, str );
+        heap_free( str );
         return NULL;
     }
 
@@ -75,7 +74,7 @@ static char **split_hostnames( const char *hostnames )
 
     q = p;
     i = 0;
-    
+
     while (*p)
     {
         if (p[1] != '\0')
@@ -86,7 +85,7 @@ static char **split_hostnames( const char *hostnames )
                 res[i] = strdupU( q );
                 if (!res[i]) goto oom;
                 i++;
-            
+
                 while (isspace( *p )) p++;
                 q = p;
             }
@@ -101,30 +100,29 @@ static char **split_hostnames( const char *hostnames )
     }
     res[i] = NULL;
 
-    HeapFree( GetProcessHeap(), 0, str );
+    heap_free( str );
     return res;
 
 oom:
     while (i > 0) strfreeU( res[--i] );
 
-    HeapFree( GetProcessHeap(), 0, res );
-    HeapFree( GetProcessHeap(), 0, str );
+    heap_free( res );
+    heap_free( str );
 
     return NULL;
 }
 
 /* Determine if a URL starts with a known LDAP scheme */
-static int has_ldap_scheme( char *url )
+static BOOL has_ldap_scheme( char *url )
 {
-    if (!strncasecmp( url, "ldap://", 7 ) || 
-        !strncasecmp( url, "ldaps://", 8 ) ||
-        !strncasecmp( url, "ldapi://", 8 ) ||
-        !strncasecmp( url, "cldap://", 8 )) return 1;
-    return 0;
+    return !_strnicmp( url, "ldap://", 7 )  ||
+           !_strnicmp( url, "ldaps://", 8 ) ||
+           !_strnicmp( url, "ldapi://", 8 ) ||
+           !_strnicmp( url, "cldap://", 8 );
 }
 
 /* Flatten an array of hostnames into a space separated string of URLs.
- * Prepend a given scheme and append a given portnumber to each hostname
+ * Prepend a given scheme and append a given port number to each hostname
  * if necessary.
  */
 static char *join_hostnames( const char *scheme, char **hostnames, ULONG portnumber )
@@ -156,9 +154,7 @@ static char *join_hostnames( const char *scheme, char **hostnames, ULONG portnum
     }
 
     size += (i - 1) * strlen( sep );
-    res = HeapAlloc( GetProcessHeap(), 0, size + 1 );
-    if (!res) return NULL;
+    if (!(res = heap_alloc( size + 1 ))) return NULL;
 
     p = res;
     for (v = hostnames; *v; v++)
@@ -208,6 +204,17 @@ static char *urlify_hostnames( const char *scheme, char *hostnames, ULONG port )
 
 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
 
+#ifdef HAVE_LDAP
+static LDAP *create_context( const char *url )
+{
+    LDAP *ld;
+    int version = LDAP_VERSION3;
+    if (ldap_initialize( &ld, url ) != LDAP_SUCCESS) return NULL;
+    ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
+    return ld;
+}
+#endif
+
 /***********************************************************************
  *      cldap_openA     (WLDAP32.@)
  *
@@ -244,7 +251,7 @@ exit:
  *
  * PARAMS
  *  hostname   [I] Name of the host to connect to.
- *  portnumber [I] Portnumber to use.
+ *  portnumber [I] Port number to use.
  *
  * RETURNS
  *  Success: Pointer to an LDAP context.
@@ -254,8 +261,8 @@ exit:
  *  The hostname string can be a space separated string of hostnames,
  *  in which case the LDAP runtime will try to connect to the hosts
  *  in order, until a connection can be made. A hostname may have a
- *  trailing portnumber (separated from the hostname by a ':'), which 
- *  will take precedence over the portnumber supplied as a parameter
+ *  trailing port number (separated from the hostname by a ':'), which
+ *  will take precedence over the port number supplied as a parameter
  *  to this function.
  */
 WLDAP32_LDAP * CDECL cldap_openW( PWCHAR hostname, ULONG portnumber )
@@ -278,7 +285,7 @@ WLDAP32_LDAP * CDECL cldap_openW( PWCHAR hostname, ULONG portnumber )
     url = urlify_hostnames( "cldap://", hostnameU, portnumber );
     if (!url) goto exit;
 
-    ldap_initialize( &ld, url );
+    ld = create_context( url );
 
 exit:
     strfreeU( hostnameU );
@@ -321,7 +328,7 @@ ULONG CDECL ldap_connect( WLDAP32_LDAP *ld, struct l_timeval *timeout )
  *
  * See ldap_initW.
  */
-WLDAP32_LDAP *  CDECL ldap_initA( PCHAR hostname, ULONG portnumber )
+WLDAP32_LDAP *  CDECL ldap_initA( const PCHAR hostname, ULONG portnumber )
 {
 #ifdef HAVE_LDAP
     WLDAP32_LDAP *ld = NULL;
@@ -352,7 +359,7 @@ exit:
  *
  * PARAMS
  *  hostname   [I] Name of the host to connect to.
- *  portnumber [I] Portnumber to use.
+ *  portnumber [I] Port number to use.
  *
  * RETURNS
  *  Success: Pointer to an LDAP context.
@@ -362,12 +369,12 @@ exit:
  *  The hostname string can be a space separated string of hostnames,
  *  in which case the LDAP runtime will try to connect to the hosts
  *  in order, until a connection can be made. A hostname may have a
- *  trailing portnumber (separated from the hostname by a ':'), which 
- *  will take precedence over the portnumber supplied as a parameter
+ *  trailing port number (separated from the hostname by a ':'), which
+ *  will take precedence over the port number supplied as a parameter
  *  to this function. The connection will not be made until the first
  *  LDAP function that needs it is called.
  */
-WLDAP32_LDAP * CDECL ldap_initW( PWCHAR hostname, ULONG portnumber )
+WLDAP32_LDAP * CDECL ldap_initW( const PWCHAR hostname, ULONG portnumber )
 {
 #ifdef HAVE_LDAP
     LDAP *ld = NULL;
@@ -387,7 +394,7 @@ WLDAP32_LDAP * CDECL ldap_initW( PWCHAR hostname, ULONG portnumber )
     url = urlify_hostnames( "ldap://", hostnameU, portnumber );
     if (!url) goto exit;
 
-    ldap_initialize( &ld, url );
+    ld = create_context( url );
 
 exit:
     strfreeU( hostnameU );
@@ -435,7 +442,7 @@ exit:
  *
  * PARAMS
  *  hostname   [I] Name of the host to connect to.
- *  portnumber [I] Portnumber to use.
+ *  portnumber [I] Port number to use.
  *
  * RETURNS
  *  Success: Pointer to an LDAP context.
@@ -445,8 +452,8 @@ exit:
  *  The hostname string can be a space separated string of hostnames,
  *  in which case the LDAP runtime will try to connect to the hosts
  *  in order, until a connection can be made. A hostname may have a
- *  trailing portnumber (separated from the hostname by a ':'), which 
- *  will take precedence over the portnumber supplied as a parameter
+ *  trailing port number (separated from the hostname by a ':'), which
+ *  will take precedence over the port number supplied as a parameter
  *  to this function.
  */
 WLDAP32_LDAP * CDECL ldap_openW( PWCHAR hostname, ULONG portnumber )
@@ -469,7 +476,7 @@ WLDAP32_LDAP * CDECL ldap_openW( PWCHAR hostname, ULONG portnumber )
     url = urlify_hostnames( "ldap://", hostnameU, portnumber );
     if (!url) goto exit;
 
-    ldap_initialize( &ld, url );
+    ld = create_context( url );
 
 exit:
     strfreeU( hostnameU );
@@ -516,7 +523,7 @@ WLDAP32_LDAP * CDECL ldap_sslinitA( PCHAR hostname, ULONG portnumber, int secure
  *
  * PARAMS
  *  hostname   [I] Name of the host to connect to.
- *  portnumber [I] Portnumber to use.
+ *  portnumber [I] Port number to use.
  *  secure     [I] Ask the server to create an SSL connection.
  *
  * RETURNS
@@ -527,8 +534,8 @@ WLDAP32_LDAP * CDECL ldap_sslinitA( PCHAR hostname, ULONG portnumber, int secure
  *  The hostname string can be a space separated string of hostnames,
  *  in which case the LDAP runtime will try to connect to the hosts
  *  in order, until a connection can be made. A hostname may have a
- *  trailing portnumber (separated from the hostname by a ':'), which 
- *  will take precedence over the portnumber supplied as a parameter
+ *  trailing port number (separated from the hostname by a ':'), which
+ *  will take precedence over the port number supplied as a parameter
  *  to this function. The connection will not be made until the first
  *  LDAP function that needs it is called.
  */