________________________________

This e-mail contains privileged and confidential information intended for the 
use of the addressees named above. If you are not the intended recipient of 
this e-mail, you are hereby notified that you must not disseminate, copy or 
take any action in respect of any information contained in it. If you have 
received this e-mail in error, please notify the sender immediately by e-mail 
and immediately destroy this e-mail and its attachments.
From 2446a6e014f7cdee720b613d87acfa38faf7be44 Mon Sep 17 00:00:00 2001
From: Bill Nagel <wna...@tycoint.com>
Date: Mon, 6 Oct 2014 17:07:19 -0400
Subject: [PATCH 2/4] smb: initial smb implementation

Initial implementation of the SMB/CIFS protocol.
---
 lib/smb.c      |  751 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/smb.h      |   52 ++++
 lib/smb_data.h |  225 +++++++++++++++++
 3 files changed, 1028 insertions(+)
 create mode 100644 lib/smb.c
 create mode 100644 lib/smb.h
 create mode 100644 lib/smb_data.h

diff --git a/lib/smb.c b/lib/smb.c
new file mode 100644
index 0000000..2e2bcd8
--- /dev/null
+++ b/lib/smb.c
@@ -0,0 +1,751 @@
+/***************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 2014, Bill Nagel <wna...@tycoint.com>, Exacq Technologies
+ * Copyright (C) 2014, Daniel Stenberg, <dan...@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+
+#include "curl_setup.h"
+
+#include "smb.h"
+#include "smb_data.h"
+#include "urldata.h"
+#include "sendf.h"
+#include "multiif.h"
+#include "connect.h"
+#include "progress.h"
+#include "transfer.h"
+#include "vtls/vtls.h"
+#include "curl_ntlm_core.h"
+#include "curl_memory.h"
+/* The last #include file should be: */
+#include "memdebug.h"
+
+/* Local API functions */
+static CURLcode smb_setup(struct connectdata *conn);
+static CURLcode smb_connect(struct connectdata *conn, bool *done);
+static CURLcode smb_connection_state(struct connectdata *conn, bool *done);
+static CURLcode smb_request_state(struct connectdata *conn, bool *done);
+static CURLcode smb_done(struct connectdata *conn, CURLcode status,
+                         bool premature);
+static CURLcode smb_disconnect(struct connectdata *conn, bool dead);
+static int smb_getsock(struct connectdata *conn, curl_socket_t *socks,
+                       int numsocks);
+
+/*
+ * SMB handler interface
+ */
+const struct Curl_handler Curl_handler_smb = {
+  "SMB",                                /* scheme */
+  smb_setup,                            /* setup_connection */
+  ZERO_NULL,                            /* do_it */
+  smb_done,                             /* done */
+  ZERO_NULL,                            /* do_more */
+  smb_connect,                          /* connect_it */
+  smb_connection_state,                 /* connecting */
+  smb_request_state,                    /* doing */
+  smb_getsock,                          /* proto_getsock */
+  smb_getsock,                          /* doing_getsock */
+  ZERO_NULL,                            /* domore_getsock */
+  ZERO_NULL,                            /* perform_getsock */
+  smb_disconnect,                       /* disconnect */
+  ZERO_NULL,                            /* readwrite */
+  PORT_SMB,                             /* defport */
+  CURLPROTO_SMB,                        /* protocol */
+  PROTOPT_NONE                          /* flags */
+};
+
+#ifdef USE_SSL
+/*
+ * SMBS handler interface
+ */
+const struct Curl_handler Curl_handler_smbs = {
+  "SMBS",                               /* scheme */
+  smb_setup,                            /* setup_connection */
+  ZERO_NULL,                            /* do_it */
+  smb_done,                             /* done */
+  ZERO_NULL,                            /* do_more */
+  smb_connect,                          /* connect_it */
+  smb_connection_state,                 /* connecting */
+  smb_request_state,                    /* doing */
+  smb_getsock,                          /* proto_getsock */
+  smb_getsock,                          /* doing_getsock */
+  ZERO_NULL,                            /* domore_getsock */
+  ZERO_NULL,                            /* perform_getsock */
+  smb_disconnect,                       /* disconnect */
+  ZERO_NULL,                            /* readwrite */
+  PORT_SMBS,                            /* defport */
+  CURLPROTO_SMBS,                       /* protocol */
+  PROTOPT_SSL                           /* flags */
+};
+#endif
+
+#define MAX_PAYLOAD_SIZE  0x8000
+#define MAX_MESSAGE_SIZE  (MAX_PAYLOAD_SIZE + 0x1000)
+#define CLIENTNAME        "curl"
+
+/* Append a string to an SMB message */
+#define MSGCAT(str) \
+  strcpy(p, (str)); \
+  p += strlen(str);
+
+/* Append a null-terminated string to an SMB message */
+#define MSGCATNULL(str) \
+  strcpy(p, (str)); \
+  p += strlen(str) + 1;
+
+/* SMB request state */
+enum smb_req_state {
+  SMB_REQUESTING,
+  SMB_TREE_CONNECT,
+  SMB_OPEN,
+  SMB_DOWNLOAD,
+  SMB_UPLOAD,
+  SMB_CLOSE,
+  SMB_TREE_DISCONNECT,
+  SMB_DONE,
+};
+
+/* SMB request data */
+struct smb_request {
+  enum smb_req_state state;
+  char *share;
+  char *path;
+  uint16_t tid; /* even if we connect to same tree as another request, */
+  uint16_t fid; /* tid will be different */
+  CURLcode result;
+};
+
+static CURLcode smb_setup(struct connectdata *conn)
+{
+  struct smb_request *req;
+  char *slash, *path;
+
+  /* initialize the request state */
+  conn->data->req.protop = req = calloc(1, sizeof(struct smb_request));
+  if(!req)
+    return CURLE_OUT_OF_MEMORY;
+
+  req->state = SMB_REQUESTING;
+  req->result = CURLE_OK;
+
+  /* parse the share and path */
+  path = conn->data->state.path;
+  req->share = strdup((*path == '/' || *path == '\\') ? path + 1 : path);
+  if(!req->share)
+    return CURLE_OUT_OF_MEMORY;
+
+  slash = strchr(req->share, '/');
+  if(!slash)
+    slash = strchr(req->share, '\\');
+
+  if(!slash)
+    return CURLE_URL_MALFORMAT;
+
+  *slash++ = 0;
+  req->path = slash;
+  for(; *slash; slash++) {
+    if(*slash == '/')
+      *slash = '\\';
+  }
+
+  return CURLE_OK;
+}
+
+static CURLcode smb_connect(struct connectdata *conn, bool *done)
+{
+  struct smb_conn *smbc = &conn->proto.smbc;
+  char *slash;
+
+  /* initialize connection state */
+  memset(smbc, 0, sizeof(*smbc));
+  smbc->state = SMB_CONNECTING;
+  smbc->send_buf = malloc(MAX_MESSAGE_SIZE);
+  smbc->recv_buf = malloc(MAX_MESSAGE_SIZE);
+  if(!smbc->send_buf || !smbc->recv_buf)
+    return CURLE_OUT_OF_MEMORY;
+
+  /* multiple requests are allow with this connection */
+  connkeep(conn, "SMB default");
+
+  /* parse the username, domain, and password */
+  slash = strchr(conn->user, '/');
+  if(!slash)
+    slash = strchr(conn->user, '\\');
+
+  if(slash) {
+    smbc->user = slash + 1;
+    smbc->domain = strdup(conn->user);
+    if(!smbc->domain)
+      return CURLE_OUT_OF_MEMORY;
+    smbc->domain[slash - conn->user] = 0;
+  }
+  else {
+    smbc->user = conn->user;
+    smbc->domain = strdup(conn->host.name);
+    if(!smbc->domain)
+      return CURLE_OUT_OF_MEMORY;
+  }
+
+  return CURLE_OK;
+}
+
+static CURLcode smb_recv_message(struct connectdata *conn, void **msg)
+{
+  struct smb_conn *smbc = &conn->proto.smbc;
+  char *buf = smbc->recv_buf;
+  ssize_t bytes_read, nbt_size, msg_size;
+  ssize_t len = MAX_MESSAGE_SIZE - smbc->got;
+  CURLcode result;
+
+  result = Curl_read(conn, FIRSTSOCKET, buf + smbc->got, len, &bytes_read);
+  if(result)
+    return result;
+
+  if(bytes_read == 0)
+    return CURLE_OK;
+
+  smbc->got += bytes_read;
+  if(smbc->got < 4)
+    return CURLE_OK;
+
+  nbt_size = ntohs(*(uint16_t*)(buf + 2)) + 4;
+  if(smbc->got < nbt_size)
+    return CURLE_OK;
+
+  msg_size = sizeof(struct smb_header);
+  if(nbt_size >= msg_size + 1) {
+    /* add word count */
+    msg_size += 1 + buf[msg_size] * 2;
+    if(nbt_size >= msg_size + 2) {
+      /* add byte count */
+      msg_size += 2 + buf[msg_size] + (buf[msg_size + 1] << 8);
+      if(nbt_size < msg_size)
+        return CURLE_READ_ERROR;
+    }
+  }
+  *msg = buf;
+  return CURLE_OK;
+}
+
+static void smb_pop_message(struct connectdata *conn)
+{
+  struct smb_conn *smbc = &conn->proto.smbc;
+  smbc->got = 0;
+}
+
+static void smb_format_message(struct connectdata *conn, struct smb_header *h,
+                               uint8_t cmd, int len)
+{
+  struct smb_conn *smbc = &conn->proto.smbc;
+  struct smb_request *req = conn->data->req.protop;
+  uint32_t pid;
+
+  memset(h, 0, sizeof(*h));
+  h->nbt_length = htons(sizeof(*h) - 4 + len);
+  strncpy((char*)h->magic, "\xffSMB", 4);
+  h->command = cmd;
+  h->flags = SMB_FLAGS_CANONICAL_PATHNAMES | SMB_FLAGS_CASELESS_PATHNAMES;
+  h->flags2 = SMB_FLAGS2_IS_LONG_NAME | SMB_FLAGS2_KNOWS_LONG_NAME;
+  h->uid = smbc->uid;
+  h->tid = req->tid;
+  pid = getpid();
+  h->pid_high = pid >> 16;
+  h->pid = pid;
+}
+
+static CURLcode smb_send(struct connectdata *conn, ssize_t len)
+{
+  struct smb_conn *smbc = &conn->proto.smbc;
+  ssize_t bytes_written;
+  CURLcode result;
+
+  result = Curl_write(conn, FIRSTSOCKET, smbc->send_buf, len, &bytes_written);
+  if(result)
+    return result;
+
+  if(bytes_written != len) {
+    smbc->send_size = len;
+    smbc->sent = bytes_written;
+  }
+
+  return CURLE_OK;
+}
+
+static CURLcode smb_flush(struct connectdata *conn)
+{
+  struct smb_conn *smbc = &conn->proto.smbc;
+  ssize_t bytes_written;
+  ssize_t len = smbc->send_size - smbc->sent;
+  CURLcode result;
+
+  if(!smbc->send_size)
+    return CURLE_OK;
+
+  result = Curl_write(conn, FIRSTSOCKET, smbc->send_buf + smbc->sent,
+                      len, &bytes_written);
+  if(result)
+    return result;
+
+  if(bytes_written != len)
+    smbc->sent += bytes_written;
+  else
+    smbc->send_size = 0;
+
+  return CURLE_OK;
+}
+
+static CURLcode smb_send_message(struct connectdata *conn, uint8_t cmd,
+                                 const void *msg, int msg_len)
+{
+  struct smb_conn *smbc = &conn->proto.smbc;
+  smb_format_message(conn, (struct smb_header*)smbc->send_buf, cmd, msg_len);
+  memcpy(smbc->send_buf + sizeof(struct smb_header), msg, msg_len);
+  return smb_send(conn, sizeof(struct smb_header) + msg_len);
+}
+
+static CURLcode smb_send_negotiate(struct connectdata *conn)
+{
+  const char *msg = "\x00\x0c\x00\x02NT LM 0.12";
+  return smb_send_message(conn, SMB_COM_NEGOTIATE, msg, 15);
+};
+
+static CURLcode smb_send_setup(struct connectdata *conn)
+{
+  struct smb_conn *smbc = &conn->proto.smbc;
+  struct smb_setup setup;
+  char *p = setup.bytes;
+  unsigned char lm_hash[21], lm[24];
+  unsigned char nt_hash[21], nt[24];
+
+  ssize_t byte_count = strlen(smbc->user) + strlen(smbc->domain) + 2;
+  byte_count += sizeof(lm) + sizeof(nt);
+  byte_count += strlen(OS) + 6; /* os and client name sizes */
+  if(byte_count > sizeof(setup.bytes))
+    return CURLE_FILESIZE_EXCEEDED;
+
+  Curl_ntlm_core_mk_lm_hash(conn->data, conn->passwd, lm_hash);
+  Curl_ntlm_core_lm_resp(lm_hash, smbc->challenge, lm);
+#if USE_NTRESPONSES
+  Curl_ntlm_core_mk_nt_hash(conn->data, conn->passwd, nt_hash);
+  Curl_ntlm_core_lm_resp(nt_hash, smbc->challenge, nt);
+#else
+  memset(nt, 0, sizeof(nt));
+#endif
+
+  memset(&setup, 0, sizeof(setup));
+  setup.word_count = 13;
+  setup.andx.command = SMB_COM_NO_ANDX_COMMAND;
+  setup.max_buffer_size = MAX_MESSAGE_SIZE;
+  setup.max_mpx_count = 1;
+  setup.vc_number = 1;
+  setup.session_key = smbc->session_key;
+  setup.capabilities = 0x18; /* large file support */
+  setup.lengths[0] = sizeof(lm);
+  setup.lengths[1] = sizeof(nt);
+  memcpy(p, lm, sizeof(lm));
+  p += sizeof(lm);
+  memcpy(p, nt, sizeof(nt));
+  p += sizeof(nt);
+  MSGCATNULL(smbc->user);
+  MSGCATNULL(smbc->domain);
+  MSGCATNULL(OS);
+  MSGCATNULL(CLIENTNAME);
+  setup.byte_count = p - setup.bytes;
+  return smb_send_message(conn, SMB_COM_SETUP_ANDX, &setup,
+                          sizeof(setup) - sizeof(setup.bytes) +
+                          setup.byte_count);
+}
+
+static CURLcode smb_send_tree_connect(struct connectdata *conn)
+{
+  struct smb_request *req = conn->data->req.protop;
+  struct smb_tree_connect tree;
+  char *p = tree.bytes;
+
+  ssize_t byte_count = strlen(conn->host.name) + strlen(req->share) + 4;
+  byte_count += 6; /* service string */
+  if(byte_count > sizeof(tree.bytes))
+    return CURLE_FILESIZE_EXCEEDED;
+
+  memset(&tree, 0, sizeof(tree));
+  tree.word_count = 4;
+  tree.andx.command = SMB_COM_NO_ANDX_COMMAND;
+  tree.pw_len = 0;
+  MSGCAT("\\\\");
+  MSGCAT(conn->host.name);
+  MSGCAT("\\");
+  MSGCATNULL(req->share);
+  MSGCATNULL("?????"); /* match any type of service */
+  tree.byte_count = p - tree.bytes;
+  return smb_send_message(conn, SMB_COM_TREE_CONNECT_ANDX, &tree,
+                          sizeof(tree) - sizeof(tree.bytes) + tree.byte_count);
+}
+
+static CURLcode smb_send_open(struct connectdata *conn)
+{
+  struct smb_request *req = conn->data->req.protop;
+  struct smb_nt_create open;
+
+  if((strlen(req->path) + 1) > sizeof(open.bytes))
+    return CURLE_FILESIZE_EXCEEDED;
+
+  memset(&open, 0, sizeof(open));
+  open.word_count = 0x18;
+  open.andx.command = SMB_COM_NO_ANDX_COMMAND;
+  open.name_length = strlen(req->path);
+  open.share_access = 7;
+  if(conn->data->set.upload) {
+    open.access = 0xc0000000;    /* read/write */
+    open.create_disposition = 5; /* create or truncate */
+  }
+  else {
+    open.access = 0x80000000;    /* read */
+    open.create_disposition = 1; /* open if exists */
+  }
+  open.byte_count = open.name_length + 1;
+  strcpy(open.bytes, req->path);
+  return smb_send_message(conn, SMB_COM_NT_CREATE_ANDX, &open,
+                          sizeof(open) - sizeof(open.bytes) + open.byte_count);
+}
+
+static CURLcode smb_send_close(struct connectdata *conn)
+{
+  struct smb_request *req = conn->data->req.protop;
+  struct smb_close close;
+
+  memset(&close, 0, sizeof(close));
+  close.word_count = 3;
+  close.fid = req->fid;
+  return smb_send_message(conn, SMB_COM_CLOSE, &close, sizeof(close));
+}
+
+static CURLcode smb_send_tree_disconnect(struct connectdata *conn)
+{
+  struct smb_tree_disconnect tree;
+
+  memset(&tree, 0, sizeof(tree));
+  return smb_send_message(conn, SMB_COM_TREE_DISCONNECT, &tree, sizeof(tree));
+}
+
+static CURLcode smb_send_read(struct connectdata *conn)
+{
+  struct smb_request *req = conn->data->req.protop;
+  curl_off_t offset = conn->data->req.offset;
+  struct smb_read read;
+
+  memset(&read, 0, sizeof(read));
+  read.word_count = 0xc;
+  read.andx.command = SMB_COM_NO_ANDX_COMMAND;
+  read.fid = req->fid;
+  read.offset = offset;
+  read.offset_high = offset >> 32;
+  read.min_bytes = MAX_PAYLOAD_SIZE;
+  read.max_bytes = MAX_PAYLOAD_SIZE;
+  return smb_send_message(conn, SMB_COM_READ_ANDX, &read, sizeof(read));
+}
+
+static CURLcode smb_send_write(struct connectdata *conn)
+{
+  struct smb_conn *smbc = &conn->proto.smbc;
+  struct smb_write *write = (struct smb_write*)smbc->send_buf;
+  struct smb_request *req = conn->data->req.protop;
+  curl_off_t offset = conn->data->req.offset;
+  CURLcode result;
+  int nread;
+
+  conn->data->req.upload_fromhere = smbc->send_buf + sizeof(*write);
+  result = Curl_fillreadbuffer(conn, MAX_PAYLOAD_SIZE, &nread);
+  if(result && result != CURLE_AGAIN)
+    return result;
+  if(nread == 0)
+    return CURLE_OK;
+
+  memset(write, 0, sizeof(*write));
+  write->word_count = 0xe;
+  write->andx.command = SMB_COM_NO_ANDX_COMMAND;
+  write->fid = req->fid;
+  write->offset = offset;
+  write->offset_high = offset >> 32;
+  write->data_length = nread;
+  write->data_offset = sizeof(*write) - 4;
+  smb_format_message(conn, &write->h, SMB_COM_WRITE_ANDX,
+                     sizeof(*write) - sizeof(write->h) + nread);
+  return smb_send(conn, sizeof(*write) + nread);
+}
+
+static CURLcode smb_send_and_recv(struct connectdata *conn, void **msg)
+{
+  struct smb_conn *smbc = &conn->proto.smbc;
+  CURLcode result;
+
+  /* check if there is still data to send */
+  if(smbc->send_size) {
+    result = smb_flush(conn);
+    if(result)
+      return result;
+  }
+
+  /* some data was sent, but not all */
+  if(smbc->send_size)
+    return CURLE_AGAIN;
+
+  return smb_recv_message(conn, msg);
+}
+
+static CURLcode smb_connection_state(struct connectdata *conn, bool *done)
+{
+  struct smb_conn *smbc = &conn->proto.smbc;
+  struct smb_negotiate_response *nrsp;
+  struct smb_header *h;
+  CURLcode result;
+  void *msg = NULL;
+
+  if(smbc->state == SMB_CONNECTING) {
+#ifdef USE_SSL
+    if((conn->handler->flags & PROTOPT_SSL)) {
+      bool ssl_done;
+      result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &ssl_done);
+      if(result && result != CURLE_AGAIN)
+        return result;
+      if(!ssl_done)
+        return CURLE_OK;
+    }
+#endif
+    result = smb_send_negotiate(conn);
+    if(result) {
+      connclose(conn, "SMB: failed to send negotiate message");
+      return result;
+    }
+    smbc->state = SMB_NEGOTIATE;
+  }
+
+  /* send previous message and check for response */
+  result = smb_send_and_recv(conn, &msg);
+  if(result && result != CURLE_AGAIN) {
+    connclose(conn, "SMB: failed to communicate");
+    return result;
+  }
+  if(!msg)
+    return CURLE_OK;
+  h = msg;
+
+  switch(smbc->state) {
+  case SMB_NEGOTIATE:
+    if(h->status) {
+      connclose(conn, "SMB: negotiation failed");
+      return CURLE_COULDNT_CONNECT;
+    }
+    nrsp = msg;
+    memcpy(smbc->challenge, nrsp->bytes, sizeof(smbc->challenge));
+    smbc->session_key = nrsp->session_key;
+    result = smb_send_setup(conn);
+    if(result) {
+      connclose(conn, "SMB: failed to send setup message");
+      return result;
+    }
+    smbc->state = SMB_SETUP;
+    break;
+  case SMB_SETUP:
+    if(h->status) {
+      connclose(conn, "SMB: authentication failed");
+      return CURLE_LOGIN_DENIED;
+    }
+    smbc->uid = h->uid;
+    smbc->state = SMB_CONNECTED;
+    *done = true;
+    break;
+  default:
+    smb_pop_message(conn);
+    return CURLE_OK; /* ignore */
+  }
+  smb_pop_message(conn);
+  return CURLE_OK;
+}
+
+static CURLcode smb_request_state(struct connectdata *conn, bool *done)
+{
+  struct smb_request *req = conn->data->req.protop;
+  struct smb_header *h;
+  enum smb_req_state next_state = SMB_DONE;
+  uint16_t len, off;
+  CURLcode result;
+  void *msg = NULL;
+
+  /* start request */
+  if(req->state == SMB_REQUESTING) {
+    result = smb_send_tree_connect(conn);
+    if(result) {
+      connclose(conn, "SMB: failed to send tree connect message");
+      return result;
+    }
+    req->state = SMB_TREE_CONNECT;
+  }
+
+  /* send previous message and check for response */
+  result = smb_send_and_recv(conn, &msg);
+  if(result && result != CURLE_AGAIN) {
+    connclose(conn, "SMB: failed to communicate");
+    return result;
+  }
+  if(!msg)
+    return CURLE_OK;
+  h = msg;
+
+  switch(req->state) {
+  case SMB_TREE_CONNECT:
+    if(h->status) {
+      req->result = CURLE_REMOTE_FILE_NOT_FOUND;
+      if(h->status == SMB_ERR_NOACCESS)
+        req->result = CURLE_REMOTE_ACCESS_DENIED;
+      break;
+    }
+    req->tid = h->tid;
+    next_state = SMB_OPEN;
+    break;
+  case SMB_OPEN:
+    if(h->status) {
+      req->result = CURLE_REMOTE_FILE_NOT_FOUND;
+      next_state = SMB_TREE_DISCONNECT;
+      break;
+    }
+    req->fid = ((struct smb_nt_create_response*)msg)->fid;
+    conn->data->req.offset = 0;
+    if(conn->data->set.upload) {
+      conn->data->req.size = conn->data->state.infilesize;
+      Curl_pgrsSetUploadSize(conn->data, conn->data->req.size);
+      next_state = SMB_UPLOAD;
+    }
+    else {
+      conn->data->req.size =
+        ((struct smb_nt_create_response*)msg)->end_of_file;
+      Curl_pgrsSetDownloadSize(conn->data, conn->data->req.size);
+      next_state = SMB_DOWNLOAD;
+    }
+    break;
+  case SMB_DOWNLOAD:
+    if(h->status) {
+      req->result = CURLE_RECV_ERROR;
+      next_state = SMB_CLOSE;
+      break;
+    }
+    len = *(uint16_t*)((char*)msg + sizeof(struct smb_header) + 11);
+    off = *(uint16_t*)((char*)msg + sizeof(struct smb_header) + 13);
+    if(len > 0)
+      Curl_client_write(conn, CLIENTWRITE_BODY, (char*)msg + off + 4, len);
+    conn->data->req.bytecount += len;
+    conn->data->req.offset += len;
+    Curl_pgrsSetDownloadCounter(conn->data, conn->data->req.bytecount);
+    next_state = (len < MAX_PAYLOAD_SIZE) ? SMB_CLOSE : SMB_DOWNLOAD;
+    break;
+  case SMB_UPLOAD:
+    if(h->status) {
+      req->result = CURLE_UPLOAD_FAILED;
+      next_state = SMB_CLOSE;
+      break;
+    }
+    len = *(uint16_t*)((char*)msg + sizeof(struct smb_header) + 5);
+    conn->data->req.bytecount += len;
+    conn->data->req.offset += len;
+    Curl_pgrsSetUploadCounter(conn->data, conn->data->req.bytecount);
+    if(conn->data->req.bytecount >= conn->data->req.size)
+      next_state = SMB_CLOSE;
+    else
+      next_state = SMB_UPLOAD;
+    break;
+  case SMB_CLOSE:
+    /* don't care if close failed, proceed to tree disconnect anyway */
+    next_state = SMB_TREE_DISCONNECT;
+    break;
+  case SMB_TREE_DISCONNECT:
+    next_state = SMB_DONE;
+    break;
+  default:
+    smb_pop_message(conn);
+    return CURLE_OK; /* ignore */
+  }
+
+  smb_pop_message(conn);
+  switch(next_state) {
+  case SMB_OPEN:
+    result = smb_send_open(conn);
+    break;
+  case SMB_DOWNLOAD:
+    result = smb_send_read(conn);
+    break;
+  case SMB_UPLOAD:
+    result = smb_send_write(conn);
+    break;
+  case SMB_CLOSE:
+    result = smb_send_close(conn);
+    break;
+  case SMB_TREE_DISCONNECT:
+    result = smb_send_tree_disconnect(conn);
+    break;
+  case SMB_DONE:
+    result = req->result;
+    *done = true;
+    break;
+  default:
+    break;
+  }
+
+  if(result) {
+    connclose(conn, "SMB: failed to send message");
+    return result;
+  }
+  req->state = next_state;
+  return CURLE_OK;
+}
+
+static CURLcode smb_done(struct connectdata *conn, CURLcode status,
+                         bool premature)
+{
+  struct smb_request *req = conn->data->req.protop;
+  Curl_safefree(req->share);
+  Curl_safefree(conn->data->req.protop);
+  return status;
+}
+
+static CURLcode smb_disconnect(struct connectdata *conn, bool dead)
+{
+  struct smb_conn *smbc = &conn->proto.smbc;
+  struct smb_request *req = conn->data->req.protop;
+  Curl_safefree(smbc->domain);
+  Curl_safefree(smbc->send_buf);
+  Curl_safefree(smbc->recv_buf);
+  /* smb_done is not always called, so cleanup the request */
+  if(req) {
+    Curl_safefree(req->share);
+    Curl_safefree(conn->data->req.protop);
+  }
+  return CURLE_OK;
+}
+
+static int smb_getsock(struct connectdata *conn, curl_socket_t *socks,
+                       int numsocks)
+{
+  struct smb_conn *smbc = &conn->proto.smbc;
+  if(!numsocks)
+    return GETSOCK_BLANK;
+  socks[0] = conn->sock[FIRSTSOCKET];
+  if(smbc->send_size)
+    return GETSOCK_WRITESOCK(0);
+  return GETSOCK_READSOCK(0);
+}
diff --git a/lib/smb.h b/lib/smb.h
new file mode 100644
index 0000000..9c10f49
--- /dev/null
+++ b/lib/smb.h
@@ -0,0 +1,52 @@
+#ifndef HEADER_CURL_SMB_H
+#define HEADER_CURL_SMB_H
+/***************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 2014, Bill Nagel <wna...@tycoint.com>, Exacq Technologies
+ * Copyright (C) 2014, Daniel Stenberg, <dan...@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+
+enum smb_conn_state {
+  SMB_NOT_CONNECTED = 0,
+  SMB_CONNECTING,
+  SMB_NEGOTIATE,
+  SMB_SETUP,
+  SMB_CONNECTED,
+};
+
+struct smb_conn
+{
+  enum smb_conn_state state;
+  char *user;
+  char *domain;
+  unsigned char challenge[8];
+  unsigned int session_key;
+  unsigned short uid;
+  char *send_buf;
+  char *recv_buf;
+  size_t send_size;
+  size_t sent;
+  size_t got;
+};
+
+extern const struct Curl_handler Curl_handler_smb;
+extern const struct Curl_handler Curl_handler_smbs;
+
+#endif /* HEADER_CURL_SMB_H */
diff --git a/lib/smb_data.h b/lib/smb_data.h
new file mode 100644
index 0000000..168a117
--- /dev/null
+++ b/lib/smb_data.h
@@ -0,0 +1,225 @@
+#ifndef HEADER_CURL_SMB_DATA_H
+#define HEADER_CURL_SMB_DATA_H
+/***************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 2014, Bill Nagel <wna...@tycoint.com>, Exacq Technologies
+ * Copyright (C) 2014, Daniel Stenberg, <dan...@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+
+#include "curl_setup.h"
+
+/*
+ * Definitions for SMB protocol data structures
+ */
+
+#ifdef _MSC_VER
+#  define PACK
+#  pragma pack(push)
+#  pragma pack(1)
+#elif defined(__GNUC__)
+#  define PACK __attribute__((packed))
+#else
+#  define PACK
+#endif
+
+#ifdef HAVE_STDINT_H
+# include <stdint.h>
+#else
+typedef char               int8_t;
+typedef short              int16_t;
+typedef int                int32_t;
+typedef unsigned char      uint8_t;
+typedef unsigned short     uint16_t;
+typedef unsigned int       uint32_t;
+# ifdef HAVE_LONGLONG
+typedef long long          int64_t;
+typedef unsigned long long uint64_t;
+# else
+typedef __int64            int64_t;
+typedef unsigned __int64   uint64_t;
+# endif
+#endif
+
+#define SMB_COM_CLOSE              0x04
+#define SMB_COM_READ_ANDX          0x2e
+#define SMB_COM_WRITE_ANDX         0x2f
+#define SMB_COM_TREE_DISCONNECT    0x71
+#define SMB_COM_NEGOTIATE          0x72
+#define SMB_COM_SETUP_ANDX         0x73
+#define SMB_COM_TREE_CONNECT_ANDX  0x75
+#define SMB_COM_NT_CREATE_ANDX     0xa2
+#define SMB_COM_NO_ANDX_COMMAND    0xff
+
+#define SMB_FLAGS_CANONICAL_PATHNAMES 0x10
+#define SMB_FLAGS_CASELESS_PATHNAMES  0x08
+#define SMB_FLAGS2_UNICODE_STRINGS    0x8000
+#define SMB_FLAGS2_IS_LONG_NAME       0x0040
+#define SMB_FLAGS2_KNOWS_LONG_NAME    0x0001
+
+#define SMB_ERR_NOACCESS              0x00050001
+
+struct smb_header {
+  uint8_t nbt_type;
+  uint8_t nbt_flags;
+  uint16_t nbt_length;
+  uint8_t magic[4];
+  uint8_t command;
+  uint32_t status;
+  uint8_t flags;
+  uint16_t flags2;
+  uint16_t pid_high;
+  uint8_t signature[8];
+  uint16_t pad;
+  uint16_t tid;
+  uint16_t pid;
+  uint16_t uid;
+  uint16_t mid;
+} PACK;
+
+struct smb_negotiate_response {
+  struct smb_header h;
+  uint8_t word_count;
+  uint16_t dialect_index;
+  uint8_t security_mode;
+  uint16_t max_mpx_count;
+  uint16_t max_number_vcs;
+  uint32_t max_buffer_size;
+  uint32_t max_raw_size;
+  uint32_t session_key;
+  uint32_t capabilities;
+  uint32_t system_time_low;
+  uint32_t system_time_high;
+  uint16_t server_time_zone;
+  uint8_t encryption_key_length;
+  uint16_t byte_count;
+  char bytes[1];
+} PACK;
+
+struct andx {
+  uint8_t command;
+  uint8_t pad;
+  uint16_t offset;
+} PACK;
+
+struct smb_setup {
+  uint8_t word_count;
+  struct andx andx;
+  uint16_t max_buffer_size;
+  uint16_t max_mpx_count;
+  uint16_t vc_number;
+  uint32_t session_key;
+  uint16_t lengths[2];
+  uint32_t pad;
+  uint32_t capabilities;
+  uint16_t byte_count;
+  char bytes[1024];
+} PACK;
+
+struct smb_tree_connect {
+  uint8_t word_count;
+  struct andx andx;
+  uint16_t flags;
+  uint16_t pw_len;
+  uint16_t byte_count;
+  char bytes[1024];
+} PACK;
+
+struct smb_nt_create {
+  uint8_t word_count;
+  struct andx andx;
+  uint8_t pad;
+  uint16_t name_length;
+  uint32_t flags;
+  uint32_t root_fid;
+  uint32_t access;
+  uint64_t allocation_size;
+  uint32_t ext_file_attributes;
+  uint32_t share_access;
+  uint32_t create_disposition;
+  uint32_t create_options;
+  uint32_t impersonation_level;
+  uint8_t security_flags;
+  uint16_t byte_count;
+  char bytes[1024];
+} PACK;
+
+struct smb_nt_create_response {
+  struct smb_header h;
+  uint8_t word_count;
+  struct andx andx;
+  uint8_t op_lock_level;
+  uint16_t fid;
+  uint32_t create_disposition;
+  uint64_t create_time;
+  uint64_t last_access_time;
+  uint64_t last_write_time;
+  uint64_t last_change_time;
+  uint32_t ext_file_attributes;
+  uint64_t allocation_size;
+  uint64_t end_of_file;
+} PACK;
+
+struct smb_read {
+  uint8_t word_count;
+  struct andx andx;
+  uint16_t fid;
+  uint32_t offset;
+  uint16_t max_bytes;
+  uint16_t min_bytes;
+  uint32_t timeout;
+  uint16_t remaining;
+  uint32_t offset_high;
+  uint16_t byte_count;
+} PACK;
+
+struct smb_write {
+  struct smb_header h;
+  uint8_t word_count;
+  struct andx andx;
+  uint16_t fid;
+  uint32_t offset;
+  uint32_t timeout;
+  uint16_t write_mode;
+  uint16_t remaining;
+  uint16_t pad;
+  uint16_t data_length;
+  uint16_t data_offset;
+  uint32_t offset_high;
+  uint16_t byte_count;
+  uint8_t pad2;
+} PACK;
+
+struct smb_close {
+  uint8_t word_count;
+  uint16_t fid;
+  uint32_t last_mtime;
+  uint16_t byte_count;
+} PACK;
+
+struct smb_tree_disconnect {
+  uint8_t word_count;
+  uint16_t byte_count;
+} PACK;
+
+#ifdef _MSC_VER
+#  pragma pack(pop)
+#endif
+
+#endif /* HEADER_CURL_SMB_DATA_H */
-- 
1.7.9.5

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html

Reply via email to