Commit f396d947 authored by Steve Holme's avatar Steve Holme Committed by Yang Tse
Browse files

http NTLM: Tidied up more inconsistent spacing.

Moved NTLMSSP_SIGNATURE, HOSTNAME_MAX, SHORTPAIR and LONGQUARTET definitions in ready for move to curl_ntlm.c.

Used separate variables for Windows SSPI and native code to ease moving of code to curl_ntlm.c.

Fixed typographical erros where SPPI should be SSPI.

Fixed compilation warnings on 64-bit builds when calling Windows SSPI functions.
parent 657d02fb
Loading
Loading
Loading
Loading
+99 −92
Original line number Diff line number Diff line
@@ -62,9 +62,6 @@
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>

/* "NTLMSSP" signature is always in ASCII regardless of the platform */
#define NTLMSSP_SIGNATURE "\x4e\x54\x4c\x4d\x53\x53\x50"

#ifdef USE_SSLEAY
#include "ssluse.h"
#    ifdef USE_OPENSSL
@@ -139,6 +136,12 @@
/* The last #include file should be: */
#include "memdebug.h"

/* Hostname buffer size */
#define HOSTNAME_MAX 1024

/* "NTLMSSP" signature is always in ASCII regardless of the platform */
#define NTLMSSP_SIGNATURE "\x4e\x54\x4c\x4d\x53\x53\x50"

#ifndef USE_NTRESPONSES
/* Define this to make the type-3 message include the NT response message */
#define USE_NTRESPONSES 1
@@ -148,16 +151,9 @@
#define USE_NTLM2SESSION 1
#endif

#ifndef USE_WINDOWS_SSPI
/* this function converts from the little endian format used in the incoming
   package to whatever endian format we're using natively */
static unsigned int readint_le(unsigned char *buf) /* must point to a
                                                      4 bytes buffer*/
{
  return ((unsigned int)buf[0]) | ((unsigned int)buf[1] << 8) |
    ((unsigned int)buf[2] << 16) | ((unsigned int)buf[3] << 24);
}
#endif
#define SHORTPAIR(x) ((x) & 0xff), (((x) >> 8) & 0xff)
#define LONGQUARTET(x) ((x) & 0xff), (((x) >> 8) & 0xff), \
  (((x) >> 16) & 0xff), (((x) >> 24) & 0xff)

#if DEBUG_ME
# define DEBUG_OUT(x) x
@@ -240,6 +236,17 @@ static void ntlm_print_hex(FILE *handle, const char *buf, size_t len)
# define DEBUG_OUT(x)
#endif

#ifndef USE_WINDOWS_SSPI
/* this function converts from the little endian format used in the incoming
   package to whatever endian format we're using natively */
static unsigned int readint_le(unsigned char *buf) /* must point to a
                                                      4 bytes buffer*/
{
  return ((unsigned int)buf[0]) | ((unsigned int)buf[1] << 8) |
    ((unsigned int)buf[2] << 16) | ((unsigned int)buf[3] << 24);
}
#endif

/*
  (*) = A "security buffer" is a triplet consisting of two shorts and one
  long:
@@ -250,7 +257,6 @@ static void ntlm_print_hex(FILE *handle, const char *buf, size_t len)
     beginning of the NTLM message, in bytes.
*/


CURLntlm Curl_input_ntlm(struct connectdata *conn,
                         bool proxy,   /* if proxy or not */
                         const char *header) /* rest of the www-authenticate:
@@ -307,7 +313,7 @@ CURLntlm Curl_input_ntlm(struct connectdata *conn,
        free(buffer);
        return CURLE_OUT_OF_MEMORY;
      }
      ntlm->n_type_2 = size;
      ntlm->n_type_2 = (unsigned long)size;
      memcpy(ntlm->type_2, buffer, size);
#else
      ntlm->flags = 0;
@@ -341,6 +347,7 @@ CURLntlm Curl_input_ntlm(struct connectdata *conn,
      ntlm->state = NTLMSTATE_TYPE1; /* we should sent away a type-1 */
    }
  }

  return CURLNTLM_FINE;
}

@@ -510,7 +517,6 @@ static void lm_resp(const unsigned char *keys,
#endif
}


/*
 * Set up lanmanager hashed password
 */
@@ -625,6 +631,7 @@ static CURLcode mk_nt_hash(struct SessionHandle *data,
  }

  free(pw);

  return CURLE_OK;
}
#endif
@@ -632,7 +639,6 @@ static CURLcode mk_nt_hash(struct SessionHandle *data,
#endif

#ifdef USE_WINDOWS_SSPI

static void ntlm_sspi_cleanup(struct ntlmdata *ntlm)
{
  if(ntlm->type_2) {
@@ -651,15 +657,8 @@ static void ntlm_sspi_cleanup(struct ntlmdata *ntlm)
    ntlm->p_identity = NULL;
  }
}

#endif

#define SHORTPAIR(x) ((x) & 0xff), (((x) >> 8) & 0xff)
#define LONGQUARTET(x) ((x) & 0xff), (((x) >> 8)&0xff), \
  (((x) >>16)&0xff), (((x)>>24) & 0xff)

#define HOSTNAME_MAX 1024

#ifndef USE_WINDOWS_SSPI
/* copy the source to the destination and fill in zeroes in every
   other destination byte! */
@@ -999,18 +998,10 @@ CURLcode Curl_output_ntlm_sso(struct connectdata *conn,
CURLcode Curl_output_ntlm(struct connectdata *conn,
                          bool proxy)
{
  const char *domain = ""; /* empty */
  char host[HOSTNAME_MAX + 1] = ""; /* empty */
#ifndef USE_WINDOWS_SSPI
  size_t domlen = 0;
  size_t hostlen = 0;
  size_t hostoff; /* host name offset */
  size_t domoff;  /* domain name offset */
#endif
  size_t size;
  char *base64 = NULL;
  unsigned char ntlmbuf[1024]; /* enough, unless the user+host+domain is very
                                  long */
  unsigned char ntlmbuf[1024]; /* enough, unless the user + host + domain is
                                  very long */

  /* point to the address of the pointer that holds the string to sent to the
     server, which is for a plain host or for a HTTP proxy */
@@ -1067,17 +1058,32 @@ CURLcode Curl_output_ntlm(struct connectdata *conn,
  switch(ntlm->state) {
  case NTLMSTATE_TYPE1:
  default: /* for the weird cases we (re)start here */

    /* Create and send a type-1 message:

    Index Description          Content
    0     NTLMSSP Signature    Null-terminated ASCII "NTLMSSP"
                               (0x4e544c4d53535000)
    8     NTLM Message Type    long (0x01000000)
    12    Flags                long
    16    Supplied Domain      security buffer(*)
    24    Supplied Workstation security buffer(*)
    32    start of data block

    */
#ifdef USE_WINDOWS_SSPI
  {
    SecBuffer buf;
    SecBufferDesc desc;
    SECURITY_STATUS status;
    ULONG attrs;
    const char *dest = "";
    const char *user;
    size_t domlen = 0;
    const char *domain = "";
    size_t userlen = 0;
    size_t domlen = 0;
    size_t passwdlen = 0;
    TimeStamp tsDummy; /* For Windows 9x compatibility of SPPI calls */
    TimeStamp tsDummy; /* For Windows 9x compatibility of SSPI calls */

    ntlm_sspi_cleanup(ntlm);

@@ -1111,23 +1117,23 @@ CURLcode Curl_output_ntlm(struct connectdata *conn,
      if((ntlm->identity.User = (unsigned char *)strdup(user)) == NULL)
        return CURLE_OUT_OF_MEMORY;

      ntlm->identity.UserLength = userlen;
      ntlm->identity.UserLength = (unsigned long)userlen;
      if((ntlm->identity.Password = (unsigned char *)strdup(passwdp)) == NULL)
        return CURLE_OUT_OF_MEMORY;

      ntlm->identity.PasswordLength = strlen(passwdp);
      ntlm->identity.PasswordLength = (unsigned long)strlen(passwdp);
      if((ntlm->identity.Domain = malloc(domlen + 1)) == NULL)
        return CURLE_OUT_OF_MEMORY;

      strncpy((char *)ntlm->identity.Domain, domain, domlen);
      ntlm->identity.Domain[domlen] = '\0';
      ntlm->identity.DomainLength = domlen;
      ntlm->identity.DomainLength =  (unsigned long)domlen;
      ntlm->identity.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
    }
    else
      ntlm->p_identity = NULL;

    status = s_pSecFn->AcquireCredentialsHandleA(NULL, (char *)"NTLM",
    status = s_pSecFn->AcquireCredentialsHandleA(NULL, (void *)"NTLM",
                                                 SECPKG_CRED_OUTBOUND, NULL,
                                                 ntlm->p_identity, NULL, NULL,
                                                 &ntlm->handle, &tsDummy);
@@ -1142,7 +1148,7 @@ CURLcode Curl_output_ntlm(struct connectdata *conn,
    buf.pvBuffer   = ntlmbuf;

    status = s_pSecFn->InitializeSecurityContextA(&ntlm->handle, NULL,
                                                 (char *) host,
                                                  (void *)dest,
                                                  ISC_REQ_CONFIDENTIALITY |
                                                  ISC_REQ_REPLAY_DETECT |
                                                  ISC_REQ_CONNECTION,
@@ -1163,22 +1169,15 @@ CURLcode Curl_output_ntlm(struct connectdata *conn,
    size = buf.cbBuffer;
  }
#else
    hostoff = 0;
    domoff = hostoff + hostlen; /* This is 0: remember that host and domain
                                   are empty */

    /* Create and send a type-1 message:

    Index Description          Content
    0     NTLMSSP Signature    Null-terminated ASCII "NTLMSSP"
                               (0x4e544c4d53535000)
    8     NTLM Message Type    long (0x01000000)
    12    Flags                long
    16    Supplied Domain      security buffer(*)
    24    Supplied Workstation security buffer(*)
    32    start of data block
  {
    char *host = "";                    /* empty */
    const char *domain = "";            /* empty */
    size_t hostlen = 0;
    size_t domlen = 0;
    size_t hostoff = 0;
    size_t domoff = hostoff + hostlen;  /* This is 0: remember that host and
                                           domain are empty */

    */
#if USE_NTLM2SESSION
#define NTLM2FLAG NTLMFLAG_NEGOTIATE_NTLM2_KEY
#else
@@ -1219,6 +1218,7 @@ CURLcode Curl_output_ntlm(struct connectdata *conn,

    /* initial packet length */
    size = 32 + hostlen + domlen;
  }
#endif

    DEBUG_OUT({
@@ -1279,13 +1279,14 @@ CURLcode Curl_output_ntlm(struct connectdata *conn,

  {
#ifdef USE_WINDOWS_SSPI
    const char *dest = "";
    SecBuffer type_2;
    SecBuffer type_3;
    SecBufferDesc type_2_desc;
    SecBufferDesc type_3_desc;
    SECURITY_STATUS status;
    ULONG attrs;
    TimeStamp tsDummy; /* For Windows 9x compatibility of SPPI calls */
    TimeStamp tsDummy; /* For Windows 9x compatibility of SSPI calls */

    type_2_desc.ulVersion = type_3_desc.ulVersion  = SECBUFFER_VERSION;
    type_2_desc.cBuffers  = type_3_desc.cBuffers   = 1;
@@ -1301,7 +1302,7 @@ CURLcode Curl_output_ntlm(struct connectdata *conn,

    status = s_pSecFn->InitializeSecurityContextA(&ntlm->handle,
                                                  &ntlm->c_handle,
                                                  (char *) host,
                                                  (void *)dest,
                                                  ISC_REQ_CONFIDENTIALITY |
                                                  ISC_REQ_REPLAY_DETECT |
                                                  ISC_REQ_CONNECTION,
@@ -1325,9 +1326,15 @@ CURLcode Curl_output_ntlm(struct connectdata *conn,
    unsigned char ntresp[24]; /* fixed-size */
#endif
    bool unicode = (ntlm->flags & NTLMFLAG_NEGOTIATE_UNICODE) ? TRUE : FALSE;
    size_t useroff;
    char host[HOSTNAME_MAX + 1] = "";
    const char *user;
    const char *domain = "";
    size_t hostoff = 0;
    size_t useroff = 0;
    size_t domoff = 0;
    size_t hostlen = 0;
    size_t userlen = 0;
    size_t domlen = 0;
    CURLcode res;

    user = strchr(userp, '\\');
@@ -1575,7 +1582,7 @@ CURLcode Curl_output_ntlm(struct connectdata *conn,
      fprintf(stderr, "\n****\n");
    });

    /* Make sure that the domain, user and host strings fit in the target
    /* Make sure that the domain, user and host strings fit in the
       buffer before we copy them there. */
    if(size + userlen + domlen + hostlen >= sizeof(ntlmbuf)) {
      failf(conn->data, "user + domain + host name too big");
+1 −1
Original line number Diff line number Diff line
@@ -360,7 +360,7 @@ struct ntlmdata {
  SEC_WINNT_AUTH_IDENTITY *p_identity;
  int has_handles;
  void *type_2;
  int n_type_2;
  unsigned long n_type_2;
#else
  unsigned int flags;
  unsigned char nonce[8];