Commit e77d8670 authored by Yang Tse's avatar Yang Tse
Browse files

unicode NTLM SSPI: cleanup

Reduce the number of #ifdef UNICODE directives used in source files.
parent 1b27bb44
Loading
Loading
Loading
Loading
+11 −3
Original line number Original line Diff line number Diff line
#ifndef __LIB_CONFIG_WIN32CE_H
#ifndef HEADER_CURL_CONFIG_WIN32CE_H
#define __LIB_CONFIG_WIN32CE_H
#define HEADER_CURL_CONFIG_WIN32CE_H
/***************************************************************************
/***************************************************************************
 *                                  _   _ ____  _
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *  Project                     ___| | | |  _ \| |
@@ -427,6 +427,14 @@
/*                       WinCE                                      */
/*                       WinCE                                      */
/* ---------------------------------------------------------------- */
/* ---------------------------------------------------------------- */


#ifndef UNICODE
#  define UNICODE
#endif

#ifndef _UNICODE
#  define _UNICODE
#endif

#define CURL_DISABLE_FILE 1
#define CURL_DISABLE_FILE 1
#define CURL_DISABLE_TELNET 1
#define CURL_DISABLE_TELNET 1
#define CURL_DISABLE_LDAP 1
#define CURL_DISABLE_LDAP 1
@@ -437,4 +445,4 @@


extern int stat(const char *path,struct stat *buffer );
extern int stat(const char *path,struct stat *buffer );


#endif /* __LIB_CONFIG_WIN32CE_H */
#endif /* HEADER_CURL_CONFIG_WIN32CE_H */
+9 −16
Original line number Original line Diff line number Diff line
@@ -22,8 +22,7 @@


#include "setup.h"
#include "setup.h"


#if defined(USE_WIN32_IDN) || \
#if defined(USE_WIN32_IDN) || (defined(USE_WINDOWS_SSPI) && defined(UNICODE))
   (defined(USE_WINDOWS_SSPI) && (defined(_WIN32_WCE) || defined(UNICODE)))


 /*
 /*
  * MultiByte conversions using Windows kernel32 library.
  * MultiByte conversions using Windows kernel32 library.
@@ -45,13 +44,12 @@ wchar_t *Curl_convert_UTF8_to_wchar(const char *str_utf8)
  if(str_utf8) {
  if(str_utf8) {
    int str_w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
    int str_w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
                                        str_utf8, -1, NULL, 0);
                                        str_utf8, -1, NULL, 0);
    if(str_w_len) {
    if(str_w_len > 0) {
      str_w = malloc(str_w_len * sizeof(wchar_t));
      str_w = malloc(str_w_len * sizeof(wchar_t));
      if(str_w) {
      if(str_w) {
        if(MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w,
        if(MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w,
                               str_w_len) == 0) {
                               str_w_len) == 0) {
          free(str_w);
          Curl_safefree(str_w);
          str_w = NULL;
        }
        }
      }
      }
    }
    }
@@ -60,30 +58,25 @@ wchar_t *Curl_convert_UTF8_to_wchar(const char *str_utf8)
  return str_w;
  return str_w;
}
}


const char *Curl_convert_wchar_to_UTF8(const wchar_t *str_w)
char *Curl_convert_wchar_to_UTF8(const wchar_t *str_w)
{
{
  char *str_utf8 = NULL;
  char *str_utf8 = NULL;


  if(str_w) {
  if(str_w) {
    size_t str_utf8_len = WideCharToMultiByte(CP_UTF8, 0, str_w, -1, NULL,
    int str_utf8_len = WideCharToMultiByte(CP_UTF8, 0, str_w, -1, NULL,
                                           0, NULL, NULL);
                                           0, NULL, NULL);
    if(str_utf8_len) {
    if(str_utf8_len > 0) {
      str_utf8 = malloc(str_utf8_len * sizeof(wchar_t));
      str_utf8 = malloc(str_utf8_len * sizeof(wchar_t));
      if(str_utf8) {
      if(str_utf8) {
        if(WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, str_utf8_len,
        if(WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, str_utf8_len,
                               NULL, FALSE) == 0) {
                               NULL, FALSE) == 0) {
          (void) GetLastError();
          Curl_safefree(str_utf8);
          free((void *)str_utf8);
          str_utf8 = NULL;
        }
        }
      }
      }
    }
    }
    else {
      (void) GetLastError();
    }
  }
  }


  return str_utf8;
  return str_utf8;
}
}


#endif /* USE_WIN32_IDN || (USE_WINDOWS_SSPI && (_WIN32_WCE || UNICODE)) */
#endif /* USE_WIN32_IDN || (USE_WINDOWS_SSPI && UNICODE) */
+56 −6
Original line number Original line Diff line number Diff line
@@ -23,18 +23,68 @@
 ***************************************************************************/
 ***************************************************************************/
#include "setup.h"
#include "setup.h"


#if defined(USE_WIN32_IDN) || \
#if defined(USE_WIN32_IDN) || (defined(USE_WINDOWS_SSPI) && defined(UNICODE))
   (defined(USE_WINDOWS_SSPI) && (defined(_WIN32_WCE) || defined(UNICODE)))


 /*
 /*
  * MultiByte conversions using Windows kernel32 library.
  * MultiByte conversions using Windows kernel32 library.
  */
  */


#include <tchar.h>

wchar_t *Curl_convert_UTF8_to_wchar(const char *str_utf8);
wchar_t *Curl_convert_UTF8_to_wchar(const char *str_utf8);
const char *Curl_convert_wchar_to_UTF8(const wchar_t *str_w);
char *Curl_convert_wchar_to_UTF8(const wchar_t *str_w);

#endif /* USE_WIN32_IDN || (USE_WINDOWS_SSPI && UNICODE) */


#if defined(USE_WIN32_IDN) || defined(USE_WINDOWS_SSPI)

/*
 * Macros Curl_convert_UTF8_to_tchar(), Curl_convert_tchar_to_UTF8()
 * and Curl_unicodefree() main purpose is to minimize the number of
 * preprocessor conditional directives needed by code using these
 * to differentiate UNICODE from non-UNICODE builds.
 *
 * When building with UNICODE defined, this two macros
 * Curl_convert_UTF8_to_tchar() and Curl_convert_tchar_to_UTF8()
 * return a pointer to a newly allocated memory area holding result.
 * When the result is no longer needed, allocated memory is intended
 * to be free'ed with Curl_unicodefree().
 *
 * When building without UNICODE defined, this macros
 * Curl_convert_UTF8_to_tchar() and Curl_convert_tchar_to_UTF8()
 * return the pointer received as argument. Curl_unicodefree() does
 * no actual free'ing of this pointer it is simply set to NULL.
 */

#ifdef UNICODE

#define Curl_convert_UTF8_to_tchar(ptr) Curl_convert_UTF8_to_wchar((ptr))
#define Curl_convert_tchar_to_UTF8(ptr) Curl_convert_wchar_to_UTF8((ptr))
#define Curl_unicodefree(ptr) \
  do {if((ptr)) {free((ptr)); (ptr) = NULL;}} WHILE_FALSE

typedef union {
  unsigned short       *tchar_ptr;
  const unsigned short *const_tchar_ptr;
  unsigned short       *tbyte_ptr;
  const unsigned short *const_tbyte_ptr;
} xcharp_u;

#else

#define Curl_convert_UTF8_to_tchar(ptr) (ptr)
#define Curl_convert_tchar_to_UTF8(ptr) (ptr)
#define Curl_unicodefree(ptr) \
  do {(ptr) = NULL;} WHILE_FALSE

typedef union {
  char                *tchar_ptr;
  const char          *const_tchar_ptr;
  unsigned char       *tbyte_ptr;
  const unsigned char *const_tbyte_ptr;
} xcharp_u;

#endif /* UNICODE */


#endif /* USE_WIN32_IDN || (USE_WINDOWS_SSPI && (_WIN32_WCE || UNICODE)) */
#endif /* USE_WIN32_IDN || USE_WINDOWS_SSPI */


#endif /* HEADER_CURL_MULTIBYTE_H */
#endif /* HEADER_CURL_MULTIBYTE_H */
+60 −70
Original line number Original line Diff line number Diff line
@@ -33,10 +33,6 @@


#define DEBUG_ME 0
#define DEBUG_ME 0


#ifdef USE_WINDOWS_SSPI
#  include <tchar.h>
#endif

#include "urldata.h"
#include "urldata.h"
#include "non-ascii.h"
#include "non-ascii.h"
#include "sendf.h"
#include "sendf.h"
@@ -44,6 +40,7 @@
#include "curl_ntlm_core.h"
#include "curl_ntlm_core.h"
#include "curl_gethostname.h"
#include "curl_gethostname.h"
#include "curl_multibyte.h"
#include "curl_multibyte.h"
#include "warnless.h"
#include "curl_memory.h"
#include "curl_memory.h"


#ifdef USE_WINDOWS_SSPI
#ifdef USE_WINDOWS_SSPI
@@ -242,7 +239,7 @@ CURLcode Curl_ntlm_decode_type2_message(struct SessionHandle *data,
    free(buffer);
    free(buffer);
    return CURLE_OUT_OF_MEMORY;
    return CURLE_OUT_OF_MEMORY;
  }
  }
  ntlm->n_type_2 = (unsigned long)size;
  ntlm->n_type_2 = curlx_uztoul(size);
  memcpy(ntlm->type_2, buffer, size);
  memcpy(ntlm->type_2, buffer, size);
#else
#else
  ntlm->flags = 0;
  ntlm->flags = 0;
@@ -276,19 +273,16 @@ CURLcode Curl_ntlm_decode_type2_message(struct SessionHandle *data,
#ifdef USE_WINDOWS_SSPI
#ifdef USE_WINDOWS_SSPI
void Curl_ntlm_sspi_cleanup(struct ntlmdata *ntlm)
void Curl_ntlm_sspi_cleanup(struct ntlmdata *ntlm)
{
{
  if(ntlm->type_2) {
  Curl_safefree(ntlm->type_2);
    free(ntlm->type_2);
    ntlm->type_2 = NULL;
  }
  if(ntlm->has_handles) {
  if(ntlm->has_handles) {
    s_pSecFn->DeleteSecurityContext(&ntlm->c_handle);
    s_pSecFn->DeleteSecurityContext(&ntlm->c_handle);
    s_pSecFn->FreeCredentialsHandle(&ntlm->handle);
    s_pSecFn->FreeCredentialsHandle(&ntlm->handle);
    ntlm->has_handles = 0;
    ntlm->has_handles = 0;
  }
  }
  if(ntlm->p_identity) {
  if(ntlm->p_identity) {
    if(ntlm->identity.User) free(ntlm->identity.User);
    Curl_safefree(ntlm->identity.User);
    if(ntlm->identity.Password) free(ntlm->identity.Password);
    Curl_safefree(ntlm->identity.Password);
    if(ntlm->identity.Domain) free(ntlm->identity.Domain);
    Curl_safefree(ntlm->identity.Domain);
    ntlm->p_identity = NULL;
    ntlm->p_identity = NULL;
  }
  }
}
}
@@ -355,87 +349,83 @@ CURLcode Curl_ntlm_create_type1_message(const char *userp,
  SecBufferDesc desc;
  SecBufferDesc desc;
  SECURITY_STATUS status;
  SECURITY_STATUS status;
  unsigned long attrs;
  unsigned long attrs;
  const TCHAR *useranddomain;
  xcharp_u useranddomain;
  const TCHAR *user;
  xcharp_u user, dup_user;
  const TCHAR *domain = TEXT("");
  xcharp_u domain, dup_domain;
  xcharp_u passwd, dup_passwd;
  size_t domlen = 0;
  size_t domlen = 0;
  TimeStamp tsDummy; /* For Windows 9x compatibility of SSPI calls */
  TimeStamp tsDummy; /* For Windows 9x compatibility of SSPI calls */


  domain.const_tchar_ptr = TEXT("");

  Curl_ntlm_sspi_cleanup(ntlm);
  Curl_ntlm_sspi_cleanup(ntlm);


  if(userp && *userp) {
  if(userp && *userp) {
#ifdef UNICODE

    useranddomain = Curl_convert_UTF8_to_wchar(userp);
    /* null initialize ntlm identity's data to allow proper cleanup */
    if(useranddomain == NULL)
    ntlm->p_identity = &ntlm->identity;
    memset(ntlm->p_identity, 0, sizeof(*ntlm->p_identity));

    useranddomain.tchar_ptr = Curl_convert_UTF8_to_tchar((char *)userp);
    if(!useranddomain.tchar_ptr)
      return CURLE_OUT_OF_MEMORY;
      return CURLE_OUT_OF_MEMORY;
#else
    useranddomain = userp;
#endif


    user = _tcschr(useranddomain, TEXT('\\'));
    user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('\\'));
    if(!user)
    if(!user.const_tchar_ptr)
      user = _tcschr(useranddomain, TEXT('/'));
      user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('/'));


    if(user) {
    if(user.tchar_ptr) {
      domain = useranddomain;
      domain.tchar_ptr = useranddomain.tchar_ptr;
      domlen = user - useranddomain;
      domlen = user.tchar_ptr - useranddomain.tchar_ptr;
      user++;
      user.tchar_ptr++;
    }
    }
    else {
    else {
      user = useranddomain;
      user.tchar_ptr = useranddomain.tchar_ptr;
      domain = TEXT("");
      domain.const_tchar_ptr = TEXT("");
      domlen = 0;
      domlen = 0;
    }
    }


    /* note: initialize all of this before doing the mallocs so that
    /* setup ntlm identity's user and length */
     * it can be cleaned up later without leaking memory.
    dup_user.tchar_ptr = _tcsdup(user.tchar_ptr);
     */
    if(!dup_user.tchar_ptr) {
    ntlm->p_identity = &ntlm->identity;
      Curl_unicodefree(useranddomain.tchar_ptr);
    memset(ntlm->p_identity, 0, sizeof(*ntlm->p_identity));

#ifdef UNICODE
    if((ntlm->identity.User = (unsigned short *)_wcsdup(user)) == NULL) {
      free((void *)useranddomain);
      return CURLE_OUT_OF_MEMORY;
      return CURLE_OUT_OF_MEMORY;
    }
    }
#else
    ntlm->identity.User = dup_user.tbyte_ptr;
    if((ntlm->identity.User = (unsigned char *)strdup(user)) == NULL)
    ntlm->identity.UserLength = curlx_uztoul(_tcslen(dup_user.tchar_ptr));
      return CURLE_OUT_OF_MEMORY;
    dup_user.tchar_ptr = NULL;
#endif

    ntlm->identity.UserLength = (unsigned long)_tcslen(user);
    /* setup ntlm identity's domain and length */

    dup_domain.tchar_ptr = malloc(sizeof(TCHAR) * (domlen + 1));
    ntlm->identity.Domain = malloc(sizeof(TCHAR) * (domlen + 1));
    if(!dup_domain.tchar_ptr) {
    if(ntlm->identity.Domain == NULL) {
      Curl_unicodefree(useranddomain.tchar_ptr);
#ifdef UNICODE
      free((void *)useranddomain);
#endif
      return CURLE_OUT_OF_MEMORY;
      return CURLE_OUT_OF_MEMORY;
    }
    }
    _tcsncpy((TCHAR *)ntlm->identity.Domain, domain, domlen);
    _tcsncpy(dup_domain.tchar_ptr, domain.tchar_ptr, domlen);
    ntlm->identity.Domain[domlen] = TEXT('\0');
    *(dup_domain.tchar_ptr + domlen) = TEXT('\0');
    ntlm->identity.DomainLength = (unsigned long)domlen;
    ntlm->identity.Domain = dup_domain.tbyte_ptr;
    ntlm->identity.DomainLength = curlx_uztoul(domlen);
    dup_domain.tchar_ptr = NULL;


#ifdef UNICODE
    Curl_unicodefree(useranddomain.tchar_ptr);
    free((void *)useranddomain);
#endif


#ifdef UNICODE
    /* setup ntlm identity's password and length */
    ntlm->identity.Password = (unsigned short *)
    passwd.tchar_ptr = Curl_convert_UTF8_to_tchar((char *)passwdp);
      Curl_convert_UTF8_to_wchar(passwdp);
    if(!passwd.tchar_ptr)
    if(ntlm->identity.Password == NULL)
      return CURLE_OUT_OF_MEMORY;
      return CURLE_OUT_OF_MEMORY;
#else
    dup_passwd.tchar_ptr = _tcsdup(passwd.tchar_ptr);
    if((ntlm->identity.Password = (unsigned char *)strdup(passwdp)) == NULL)
    if(!dup_passwd.tchar_ptr) {
      Curl_unicodefree(passwd.tchar_ptr);
      return CURLE_OUT_OF_MEMORY;
      return CURLE_OUT_OF_MEMORY;
#endif
    }
    ntlm->identity.PasswordLength =
    ntlm->identity.Password = dup_passwd.tbyte_ptr;
      (unsigned long)_tcslen((TCHAR *)ntlm->identity.Password);
    ntlm->identity.PasswordLength = curlx_uztoul(_tcslen(dup_passwd.tchar_ptr));
    dup_passwd.tchar_ptr = NULL;


#ifdef UNICODE
    Curl_unicodefree(passwd.tchar_ptr);
    ntlm->identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;

#else
    /* setup ntlm identity's flags */
    ntlm->identity.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
    ntlm->identity.Flags = SECFLAG_WINNT_AUTH_IDENTITY;
#endif
  }
  }
  else
  else
    ntlm->p_identity = NULL;
    ntlm->p_identity = NULL;
+9 −1
Original line number Original line Diff line number Diff line
@@ -7,7 +7,7 @@
 *                            | (__| |_| |  _ <| |___
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *                             \___|\___/|_| \_\_____|
 *
 *
 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 *
 * This software is licensed as described in the file COPYING, which
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * you should have received as part of this distribution. The terms
@@ -163,6 +163,14 @@ void Curl_ntlm_sspi_cleanup(struct ntlmdata *ntlm);
#define NTLMFLAG_NEGOTIATE_56                    (1<<31)
#define NTLMFLAG_NEGOTIATE_56                    (1<<31)
/* Indicates that 56-bit encryption is supported. */
/* Indicates that 56-bit encryption is supported. */


#ifdef UNICODE
#  define SECFLAG_WINNT_AUTH_IDENTITY \
     (unsigned long)SEC_WINNT_AUTH_IDENTITY_UNICODE
#else
#  define SECFLAG_WINNT_AUTH_IDENTITY \
     (unsigned long)SEC_WINNT_AUTH_IDENTITY_ANSI
#endif

#endif /* BUILDING_CURL_NTLM_MSGS_C */
#endif /* BUILDING_CURL_NTLM_MSGS_C */


#endif /* USE_NTLM */
#endif /* USE_NTLM */
Loading