2014-11-29 21:12:44 +03:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014, Bill Nagel <wnagel@tycoint.com>, Exacq Technologies
|
2015-01-01 07:56:16 +03:00
|
|
|
* Copyright (C) 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2014-11-29 21:12:44 +03:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2014-12-13 01:08:03 +03:00
|
|
|
#if !defined(CURL_DISABLE_SMB) && defined(USE_NTLM) && \
|
|
|
|
(CURL_SIZEOF_CURL_OFF_T > 4)
|
2014-11-29 21:12:44 +03:00
|
|
|
|
2014-12-07 23:44:57 +03:00
|
|
|
#if !defined(USE_WINDOWS_SSPI) || defined(USE_WIN32_CRYPTO)
|
|
|
|
|
2014-11-29 22:03:53 +03:00
|
|
|
#define BUILDING_CURL_SMB_C
|
|
|
|
|
2014-12-14 19:42:08 +03:00
|
|
|
#ifdef HAVE_PROCESS_H
|
2014-11-30 19:09:41 +03:00
|
|
|
#include <process.h>
|
|
|
|
#define getpid _getpid
|
|
|
|
#endif
|
|
|
|
|
2014-11-29 21:12:44 +03:00
|
|
|
#include "smb.h"
|
2014-11-30 18:39:14 +03:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "multiif.h"
|
2014-11-30 19:09:41 +03:00
|
|
|
#include "connect.h"
|
|
|
|
#include "progress.h"
|
|
|
|
#include "transfer.h"
|
|
|
|
#include "vtls/vtls.h"
|
|
|
|
#include "curl_ntlm_core.h"
|
2014-12-06 23:46:54 +03:00
|
|
|
#include "escape.h"
|
2014-12-31 19:07:32 +03:00
|
|
|
#include "curl_endian.h"
|
2014-11-29 21:12:44 +03:00
|
|
|
|
2015-03-25 01:12:03 +03:00
|
|
|
/* The last #include files should be: */
|
|
|
|
#include "curl_memory.h"
|
2014-11-29 21:12:44 +03:00
|
|
|
#include "memdebug.h"
|
|
|
|
|
2014-11-30 18:39:14 +03:00
|
|
|
/* Local API functions */
|
2014-12-24 20:10:28 +03:00
|
|
|
static CURLcode smb_setup_connection(struct connectdata *conn);
|
2014-11-30 18:39:14 +03:00
|
|
|
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);
|
2014-12-07 00:02:06 +03:00
|
|
|
static CURLcode smb_parse_url_path(struct connectdata *conn);
|
2014-11-30 18:39:14 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SMB handler interface
|
|
|
|
*/
|
|
|
|
const struct Curl_handler Curl_handler_smb = {
|
|
|
|
"SMB", /* scheme */
|
2014-12-24 20:10:28 +03:00
|
|
|
smb_setup_connection, /* setup_connection */
|
2014-11-30 18:39:14 +03:00
|
|
|
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 */
|
2014-12-24 20:10:28 +03:00
|
|
|
smb_setup_connection, /* setup_connection */
|
2014-11-30 18:39:14 +03:00
|
|
|
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
|
|
|
|
|
2014-11-30 19:09:41 +03:00
|
|
|
#define MAX_PAYLOAD_SIZE 0x8000
|
|
|
|
#define MAX_MESSAGE_SIZE (MAX_PAYLOAD_SIZE + 0x1000)
|
|
|
|
#define CLIENTNAME "curl"
|
|
|
|
#define SERVICENAME "?????"
|
|
|
|
|
|
|
|
/* 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 is mostly little endian */
|
2014-12-05 20:37:35 +03:00
|
|
|
#if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \
|
|
|
|
defined(__OS400__)
|
2014-11-30 19:09:41 +03:00
|
|
|
static unsigned short smb_swap16(unsigned short x)
|
|
|
|
{
|
|
|
|
return (x << 8) | ((x >> 8) & 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int smb_swap32(unsigned int x)
|
|
|
|
{
|
|
|
|
return (x << 24) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) |
|
|
|
|
((x >> 24) & 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_LONGLONG
|
|
|
|
static unsigned long long smb_swap64(unsigned long long x)
|
|
|
|
{
|
|
|
|
return ((unsigned long long)smb_swap32(x) << 32) | smb_swap32(x >> 32);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static unsigned __int64 smb_swap64(unsigned __int64 x)
|
|
|
|
{
|
|
|
|
return ((unsigned __int64)smb_swap32(x) << 32) | smb_swap32(x >> 32);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
# define smb_swap16(x) (x)
|
|
|
|
# define smb_swap32(x) (x)
|
|
|
|
# define smb_swap64(x) (x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* SMB request state */
|
|
|
|
enum smb_req_state {
|
|
|
|
SMB_REQUESTING,
|
|
|
|
SMB_TREE_CONNECT,
|
|
|
|
SMB_OPEN,
|
|
|
|
SMB_DOWNLOAD,
|
|
|
|
SMB_UPLOAD,
|
|
|
|
SMB_CLOSE,
|
|
|
|
SMB_TREE_DISCONNECT,
|
2014-11-30 21:07:24 +03:00
|
|
|
SMB_DONE
|
2014-11-30 19:09:41 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/* SMB request data */
|
|
|
|
struct smb_request {
|
|
|
|
enum smb_req_state state;
|
|
|
|
char *share;
|
|
|
|
char *path;
|
|
|
|
unsigned short tid; /* Even if we connect to the same tree as another */
|
|
|
|
unsigned short fid; /* request, the tid will be different */
|
|
|
|
CURLcode result;
|
|
|
|
};
|
|
|
|
|
2014-12-13 15:12:45 +03:00
|
|
|
static void conn_state(struct connectdata *conn, enum smb_conn_state newstate)
|
|
|
|
{
|
|
|
|
struct smb_conn *smb = &conn->proto.smbc;
|
|
|
|
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
|
|
|
/* For debug purposes */
|
|
|
|
static const char * const names[] = {
|
|
|
|
"SMB_NOT_CONNECTED",
|
|
|
|
"SMB_CONNECTING",
|
|
|
|
"SMB_NEGOTIATE",
|
|
|
|
"SMB_SETUP",
|
|
|
|
"SMB_CONNECTED",
|
|
|
|
/* LAST */
|
|
|
|
};
|
|
|
|
|
|
|
|
if(smb->state != newstate)
|
|
|
|
infof(conn->data, "SMB conn %p state change from %s to %s\n",
|
|
|
|
(void *)smb, names[smb->state], names[newstate]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
smb->state = newstate;
|
|
|
|
}
|
|
|
|
|
2014-12-13 16:06:56 +03:00
|
|
|
static void request_state(struct connectdata *conn,
|
|
|
|
enum smb_req_state newstate)
|
2014-12-13 15:12:45 +03:00
|
|
|
{
|
|
|
|
struct smb_request *req = conn->data->req.protop;
|
|
|
|
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
|
|
|
/* For debug purposes */
|
|
|
|
static const char * const names[] = {
|
|
|
|
"SMB_REQUESTING",
|
|
|
|
"SMB_TREE_CONNECT",
|
|
|
|
"SMB_OPEN",
|
|
|
|
"SMB_DOWNLOAD",
|
|
|
|
"SMB_UPLOAD",
|
|
|
|
"SMB_CLOSE",
|
|
|
|
"SMB_TREE_DISCONNECT",
|
|
|
|
"SMB_DONE",
|
|
|
|
/* LAST */
|
|
|
|
};
|
|
|
|
|
|
|
|
if(req->state != newstate)
|
|
|
|
infof(conn->data, "SMB request %p state change from %s to %s\n",
|
|
|
|
(void *)req, names[req->state], names[newstate]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
req->state = newstate;
|
|
|
|
}
|
|
|
|
|
2014-12-24 20:10:28 +03:00
|
|
|
static CURLcode smb_setup_connection(struct connectdata *conn)
|
2014-11-30 18:39:14 +03:00
|
|
|
{
|
2014-11-30 19:09:41 +03:00
|
|
|
struct smb_request *req;
|
|
|
|
|
|
|
|
/* Initialize the request state */
|
|
|
|
conn->data->req.protop = req = calloc(1, sizeof(struct smb_request));
|
|
|
|
if(!req)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2014-12-07 00:02:06 +03:00
|
|
|
/* Parse the URL path */
|
|
|
|
return smb_parse_url_path(conn);
|
2014-11-30 18:39:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode smb_connect(struct connectdata *conn, bool *done)
|
|
|
|
{
|
2014-11-30 19:09:41 +03:00
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
|
|
|
char *slash;
|
|
|
|
|
2014-11-30 21:29:23 +03:00
|
|
|
(void) done;
|
|
|
|
|
2014-12-03 00:04:23 +03:00
|
|
|
/* Check we have a username and password to authenticate with */
|
|
|
|
if(!conn->bits.user_passwd)
|
|
|
|
return CURLE_LOGIN_DENIED;
|
|
|
|
|
2014-11-30 19:09:41 +03:00
|
|
|
/* Initialize the connection state */
|
|
|
|
memset(smbc, 0, sizeof(*smbc));
|
|
|
|
smbc->state = SMB_CONNECTING;
|
|
|
|
smbc->recv_buf = malloc(MAX_MESSAGE_SIZE);
|
2014-12-30 16:09:31 +03:00
|
|
|
if(!smbc->recv_buf)
|
2014-11-30 19:09:41 +03:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* Multiple requests are allowed 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;
|
|
|
|
size_t nbt_size;
|
|
|
|
size_t msg_size;
|
|
|
|
size_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)
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
smbc->got += bytes_read;
|
|
|
|
|
|
|
|
/* Check for a 32-bit nbt header */
|
|
|
|
if(smbc->got < sizeof(unsigned int))
|
|
|
|
return CURLE_OK;
|
|
|
|
|
2015-01-01 23:48:05 +03:00
|
|
|
nbt_size = Curl_read16_be((unsigned char *)(buf + sizeof(unsigned short))) +
|
|
|
|
sizeof(unsigned int);
|
2014-11-30 19:09:41 +03:00
|
|
|
if(smbc->got < nbt_size)
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
msg_size = sizeof(struct smb_header);
|
|
|
|
if(nbt_size >= msg_size + 1) {
|
|
|
|
/* Add the word count */
|
2014-12-07 00:44:00 +03:00
|
|
|
msg_size += 1 + ((unsigned char) buf[msg_size]) * sizeof(unsigned short);
|
2014-11-30 19:09:41 +03:00
|
|
|
if(nbt_size >= msg_size + sizeof(unsigned short)) {
|
|
|
|
/* Add the byte count */
|
2015-01-01 23:48:05 +03:00
|
|
|
msg_size += sizeof(unsigned short) +
|
|
|
|
Curl_read16_le((unsigned char *)&buf[msg_size]);
|
2014-11-30 19:09:41 +03:00
|
|
|
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,
|
2014-12-01 01:11:44 +03:00
|
|
|
unsigned char cmd, size_t len)
|
2014-11-30 19:09:41 +03:00
|
|
|
{
|
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
|
|
|
struct smb_request *req = conn->data->req.protop;
|
|
|
|
unsigned int pid;
|
|
|
|
|
|
|
|
memset(h, 0, sizeof(*h));
|
2014-11-30 21:11:16 +03:00
|
|
|
h->nbt_length = htons((unsigned short) (sizeof(*h) - sizeof(unsigned int) +
|
|
|
|
len));
|
2014-12-22 01:21:16 +03:00
|
|
|
memcpy((char *)h->magic, "\xffSMB", 4);
|
2014-11-30 19:09:41 +03:00
|
|
|
h->command = cmd;
|
|
|
|
h->flags = SMB_FLAGS_CANONICAL_PATHNAMES | SMB_FLAGS_CASELESS_PATHNAMES;
|
|
|
|
h->flags2 = smb_swap16(SMB_FLAGS2_IS_LONG_NAME | SMB_FLAGS2_KNOWS_LONG_NAME);
|
|
|
|
h->uid = smb_swap16(smbc->uid);
|
|
|
|
h->tid = smb_swap16(req->tid);
|
|
|
|
pid = getpid();
|
2014-11-30 23:10:23 +03:00
|
|
|
h->pid_high = smb_swap16((unsigned short)(pid >> 16));
|
|
|
|
h->pid = smb_swap16((unsigned short) pid);
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
2014-12-30 16:09:31 +03:00
|
|
|
static CURLcode smb_send(struct connectdata *conn, ssize_t len,
|
|
|
|
size_t upload_size)
|
2014-11-30 19:09:41 +03:00
|
|
|
{
|
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
|
|
|
ssize_t bytes_written;
|
|
|
|
CURLcode result;
|
|
|
|
|
2014-12-30 16:09:31 +03:00
|
|
|
result = Curl_write(conn, FIRSTSOCKET, conn->data->state.uploadbuffer,
|
|
|
|
len, &bytes_written);
|
2014-11-30 19:09:41 +03:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
if(bytes_written != len) {
|
|
|
|
smbc->send_size = len;
|
|
|
|
smbc->sent = bytes_written;
|
|
|
|
}
|
|
|
|
|
2014-12-30 16:09:31 +03:00
|
|
|
smbc->upload_size = upload_size;
|
|
|
|
|
2014-11-30 19:09:41 +03:00
|
|
|
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;
|
|
|
|
|
2014-12-30 16:09:31 +03:00
|
|
|
result = Curl_write(conn, FIRSTSOCKET,
|
|
|
|
conn->data->state.uploadbuffer + smbc->sent,
|
2014-11-30 19:09:41 +03:00
|
|
|
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, unsigned char cmd,
|
2014-12-01 01:11:44 +03:00
|
|
|
const void *msg, size_t msg_len)
|
2014-11-30 19:09:41 +03:00
|
|
|
{
|
2014-12-30 16:09:31 +03:00
|
|
|
smb_format_message(conn, (struct smb_header *)conn->data->state.uploadbuffer,
|
|
|
|
cmd, msg_len);
|
|
|
|
memcpy(conn->data->state.uploadbuffer + sizeof(struct smb_header),
|
|
|
|
msg, msg_len);
|
2014-11-30 19:09:41 +03:00
|
|
|
|
2014-12-30 16:09:31 +03:00
|
|
|
return smb_send(conn, sizeof(struct smb_header) + msg_len, 0);
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-11-30 21:29:23 +03:00
|
|
|
}
|
2014-11-30 19:09:41 +03:00
|
|
|
|
|
|
|
static CURLcode smb_send_setup(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
2014-11-30 21:56:36 +03:00
|
|
|
struct smb_setup msg;
|
|
|
|
char *p = msg.bytes;
|
2014-11-30 19:09:41 +03:00
|
|
|
unsigned char lm_hash[21];
|
|
|
|
unsigned char lm[24];
|
|
|
|
unsigned char nt_hash[21];
|
|
|
|
unsigned char nt[24];
|
|
|
|
|
2014-12-01 00:14:42 +03:00
|
|
|
size_t byte_count = sizeof(lm) + sizeof(nt);
|
2014-11-30 19:09:41 +03:00
|
|
|
byte_count += strlen(smbc->user) + strlen(smbc->domain);
|
|
|
|
byte_count += strlen(OS) + strlen(CLIENTNAME) + 4; /* 4 null chars */
|
2014-11-30 21:56:36 +03:00
|
|
|
if(byte_count > sizeof(msg.bytes))
|
2014-11-30 19:09:41 +03:00
|
|
|
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
|
|
|
|
|
2014-11-30 21:56:36 +03:00
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.word_count = SMB_WC_SETUP_ANDX;
|
|
|
|
msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
|
2014-12-08 17:30:11 +03:00
|
|
|
msg.max_buffer_size = smb_swap16(MAX_MESSAGE_SIZE);
|
2014-11-30 21:56:36 +03:00
|
|
|
msg.max_mpx_count = smb_swap16(1);
|
|
|
|
msg.vc_number = smb_swap16(1);
|
|
|
|
msg.session_key = smb_swap32(smbc->session_key);
|
|
|
|
msg.capabilities = smb_swap32(SMB_CAP_LARGE_FILES);
|
|
|
|
msg.lengths[0] = smb_swap16(sizeof(lm));
|
|
|
|
msg.lengths[1] = smb_swap16(sizeof(nt));
|
2014-11-30 19:09:41 +03:00
|
|
|
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);
|
2014-12-05 20:37:35 +03:00
|
|
|
byte_count = p - msg.bytes;
|
|
|
|
msg.byte_count = smb_swap16((unsigned short)byte_count);
|
2014-11-30 18:39:14 +03:00
|
|
|
|
2014-11-30 21:56:36 +03:00
|
|
|
return smb_send_message(conn, SMB_COM_SETUP_ANDX, &msg,
|
2014-12-05 20:37:35 +03:00
|
|
|
sizeof(msg) - sizeof(msg.bytes) + byte_count);
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode smb_send_tree_connect(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
struct smb_request *req = conn->data->req.protop;
|
2014-11-30 21:56:36 +03:00
|
|
|
struct smb_tree_connect msg;
|
|
|
|
char *p = msg.bytes;
|
2014-11-30 19:09:41 +03:00
|
|
|
|
2014-12-01 00:14:42 +03:00
|
|
|
size_t byte_count = strlen(conn->host.name) + strlen(req->share);
|
2014-11-30 19:09:41 +03:00
|
|
|
byte_count += strlen(SERVICENAME) + 5; /* 2 nulls and 3 backslashes */
|
2014-11-30 21:56:36 +03:00
|
|
|
if(byte_count > sizeof(msg.bytes))
|
2014-11-30 19:09:41 +03:00
|
|
|
return CURLE_FILESIZE_EXCEEDED;
|
|
|
|
|
2014-11-30 21:56:36 +03:00
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.word_count = SMB_WC_TREE_CONNECT_ANDX;
|
|
|
|
msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
|
|
|
|
msg.pw_len = 0;
|
2014-11-30 19:09:41 +03:00
|
|
|
MSGCAT("\\\\");
|
|
|
|
MSGCAT(conn->host.name);
|
|
|
|
MSGCAT("\\");
|
|
|
|
MSGCATNULL(req->share);
|
|
|
|
MSGCATNULL(SERVICENAME); /* Match any type of service */
|
2014-12-05 20:37:35 +03:00
|
|
|
byte_count = p - msg.bytes;
|
|
|
|
msg.byte_count = smb_swap16((unsigned short)byte_count);
|
2014-11-30 19:09:41 +03:00
|
|
|
|
2014-11-30 21:56:36 +03:00
|
|
|
return smb_send_message(conn, SMB_COM_TREE_CONNECT_ANDX, &msg,
|
2014-12-05 20:37:35 +03:00
|
|
|
sizeof(msg) - sizeof(msg.bytes) + byte_count);
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode smb_send_open(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
struct smb_request *req = conn->data->req.protop;
|
2014-11-30 21:56:36 +03:00
|
|
|
struct smb_nt_create msg;
|
2014-12-05 20:37:35 +03:00
|
|
|
size_t byte_count;
|
2014-11-30 19:09:41 +03:00
|
|
|
|
2014-11-30 21:56:36 +03:00
|
|
|
if((strlen(req->path) + 1) > sizeof(msg.bytes))
|
2014-11-30 19:09:41 +03:00
|
|
|
return CURLE_FILESIZE_EXCEEDED;
|
|
|
|
|
2014-11-30 21:56:36 +03:00
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.word_count = SMB_WC_NT_CREATE_ANDX;
|
|
|
|
msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
|
2014-12-05 20:37:35 +03:00
|
|
|
byte_count = strlen(req->path);
|
|
|
|
msg.name_length = smb_swap16((unsigned short)byte_count);
|
2014-11-30 21:56:36 +03:00
|
|
|
msg.share_access = smb_swap32(SMB_FILE_SHARE_ALL);
|
2014-11-30 19:09:41 +03:00
|
|
|
if(conn->data->set.upload) {
|
2014-11-30 21:56:36 +03:00
|
|
|
msg.access = smb_swap32(SMB_GENERIC_READ | SMB_GENERIC_WRITE);
|
|
|
|
msg.create_disposition = smb_swap32(SMB_FILE_OVERWRITE_IF);
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
else {
|
2014-11-30 21:56:36 +03:00
|
|
|
msg.access = smb_swap32(SMB_GENERIC_READ);
|
|
|
|
msg.create_disposition = smb_swap32(SMB_FILE_OPEN);
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
2014-12-05 20:37:35 +03:00
|
|
|
msg.byte_count = smb_swap16((unsigned short) ++byte_count);
|
2014-11-30 21:56:36 +03:00
|
|
|
strcpy(msg.bytes, req->path);
|
2014-11-30 19:09:41 +03:00
|
|
|
|
2014-11-30 21:56:36 +03:00
|
|
|
return smb_send_message(conn, SMB_COM_NT_CREATE_ANDX, &msg,
|
2014-12-05 20:37:35 +03:00
|
|
|
sizeof(msg) - sizeof(msg.bytes) + byte_count);
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode smb_send_close(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
struct smb_request *req = conn->data->req.protop;
|
2014-11-30 21:56:36 +03:00
|
|
|
struct smb_close msg;
|
2014-11-30 19:09:41 +03:00
|
|
|
|
2014-11-30 21:56:36 +03:00
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.word_count = SMB_WC_CLOSE;
|
|
|
|
msg.fid = smb_swap16(req->fid);
|
2014-11-30 19:09:41 +03:00
|
|
|
|
2014-11-30 21:56:36 +03:00
|
|
|
return smb_send_message(conn, SMB_COM_CLOSE, &msg, sizeof(msg));
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode smb_send_tree_disconnect(struct connectdata *conn)
|
|
|
|
{
|
2014-11-30 21:56:36 +03:00
|
|
|
struct smb_tree_disconnect msg;
|
2014-11-30 19:09:41 +03:00
|
|
|
|
2014-11-30 21:56:36 +03:00
|
|
|
memset(&msg, 0, sizeof(msg));
|
2014-11-30 19:09:41 +03:00
|
|
|
|
2014-11-30 21:56:36 +03:00
|
|
|
return smb_send_message(conn, SMB_COM_TREE_DISCONNECT, &msg, sizeof(msg));
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode smb_send_read(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
struct smb_request *req = conn->data->req.protop;
|
|
|
|
curl_off_t offset = conn->data->req.offset;
|
2014-11-30 21:56:36 +03:00
|
|
|
struct smb_read msg;
|
|
|
|
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.word_count = SMB_WC_READ_ANDX;
|
|
|
|
msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
|
|
|
|
msg.fid = smb_swap16(req->fid);
|
2014-11-30 23:10:23 +03:00
|
|
|
msg.offset = smb_swap32((unsigned int) offset);
|
|
|
|
msg.offset_high = smb_swap32((unsigned int) (offset >> 32));
|
2014-11-30 21:56:36 +03:00
|
|
|
msg.min_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
|
|
|
|
msg.max_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
|
|
|
|
|
|
|
|
return smb_send_message(conn, SMB_COM_READ_ANDX, &msg, sizeof(msg));
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode smb_send_write(struct connectdata *conn)
|
|
|
|
{
|
2014-12-30 16:09:31 +03:00
|
|
|
struct smb_write *msg = (struct smb_write *)conn->data->state.uploadbuffer;
|
2014-11-30 19:09:41 +03:00
|
|
|
struct smb_request *req = conn->data->req.protop;
|
|
|
|
curl_off_t offset = conn->data->req.offset;
|
|
|
|
|
2014-12-30 16:09:31 +03:00
|
|
|
curl_off_t upload_size = conn->data->req.size - conn->data->req.bytecount;
|
|
|
|
if(upload_size >= MAX_PAYLOAD_SIZE - 1) /* There is one byte of padding */
|
|
|
|
upload_size = MAX_PAYLOAD_SIZE - 1;
|
2014-11-30 19:09:41 +03:00
|
|
|
|
2014-11-30 21:56:36 +03:00
|
|
|
memset(msg, 0, sizeof(*msg));
|
|
|
|
msg->word_count = SMB_WC_WRITE_ANDX;
|
|
|
|
msg->andx.command = SMB_COM_NO_ANDX_COMMAND;
|
|
|
|
msg->fid = smb_swap16(req->fid);
|
2014-11-30 23:10:23 +03:00
|
|
|
msg->offset = smb_swap32((unsigned int) offset);
|
|
|
|
msg->offset_high = smb_swap32((unsigned int) (offset >> 32));
|
2014-12-30 16:09:31 +03:00
|
|
|
msg->data_length = smb_swap16((unsigned short) upload_size);
|
2014-11-30 21:56:36 +03:00
|
|
|
msg->data_offset = smb_swap16(sizeof(*msg) - sizeof(unsigned int));
|
2014-12-30 18:42:17 +03:00
|
|
|
msg->byte_count = smb_swap16((unsigned short) (upload_size + 1));
|
2014-11-30 19:09:41 +03:00
|
|
|
|
2014-11-30 21:56:36 +03:00
|
|
|
smb_format_message(conn, &msg->h, SMB_COM_WRITE_ANDX,
|
2014-12-30 16:09:31 +03:00
|
|
|
sizeof(*msg) - sizeof(msg->h) + (size_t) upload_size);
|
2014-11-30 19:09:41 +03:00
|
|
|
|
2014-12-30 16:09:31 +03:00
|
|
|
return smb_send(conn, sizeof(*msg), (size_t) upload_size);
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode smb_send_and_recv(struct connectdata *conn, void **msg)
|
|
|
|
{
|
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
|
|
|
CURLcode result;
|
|
|
|
|
2014-12-30 16:09:31 +03:00
|
|
|
/* Check if there is data in the transfer buffer */
|
|
|
|
if(!smbc->send_size && smbc->upload_size) {
|
|
|
|
int nread = smbc->upload_size > BUFSIZE ? BUFSIZE :
|
|
|
|
(int) smbc->upload_size;
|
|
|
|
conn->data->req.upload_fromhere = conn->data->state.uploadbuffer;
|
|
|
|
result = Curl_fillreadbuffer(conn, nread, &nread);
|
|
|
|
if(result && result != CURLE_AGAIN)
|
|
|
|
return result;
|
|
|
|
if(!nread)
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
smbc->upload_size -= nread;
|
|
|
|
smbc->send_size = nread;
|
|
|
|
smbc->sent = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if there is data to send */
|
2014-11-30 19:09:41 +03:00
|
|
|
if(smbc->send_size) {
|
|
|
|
result = smb_flush(conn);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-12-30 16:09:31 +03:00
|
|
|
/* Check if there is still data to be sent */
|
|
|
|
if(smbc->send_size || smbc->upload_size)
|
2014-11-30 19:09:41 +03:00
|
|
|
return CURLE_AGAIN;
|
|
|
|
|
|
|
|
return smb_recv_message(conn, msg);
|
2014-11-30 18:39:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode smb_connection_state(struct connectdata *conn, bool *done)
|
|
|
|
{
|
2014-11-30 19:09:41 +03:00
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
|
|
|
struct smb_negotiate_response *nrsp;
|
|
|
|
struct smb_header *h;
|
|
|
|
CURLcode result;
|
|
|
|
void *msg = NULL;
|
2014-11-30 18:39:14 +03:00
|
|
|
|
2014-11-30 19:09:41 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-12-13 15:12:45 +03:00
|
|
|
conn_state(conn, SMB_NEGOTIATE);
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the previous message and check for a 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 = smb_swap32(nrsp->session_key);
|
|
|
|
result = smb_send_setup(conn);
|
|
|
|
if(result) {
|
|
|
|
connclose(conn, "SMB: failed to send setup message");
|
|
|
|
return result;
|
|
|
|
}
|
2014-12-13 15:12:45 +03:00
|
|
|
conn_state(conn, SMB_SETUP);
|
2014-11-30 19:09:41 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SMB_SETUP:
|
|
|
|
if(h->status) {
|
|
|
|
connclose(conn, "SMB: authentication failed");
|
|
|
|
return CURLE_LOGIN_DENIED;
|
|
|
|
}
|
|
|
|
smbc->uid = smb_swap16(h->uid);
|
2014-12-13 15:12:45 +03:00
|
|
|
conn_state(conn, SMB_CONNECTED);
|
2014-11-30 19:09:41 +03:00
|
|
|
*done = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
smb_pop_message(conn);
|
|
|
|
return CURLE_OK; /* ignore */
|
|
|
|
}
|
|
|
|
|
|
|
|
smb_pop_message(conn);
|
|
|
|
|
|
|
|
return CURLE_OK;
|
2014-11-30 18:39:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode smb_request_state(struct connectdata *conn, bool *done)
|
|
|
|
{
|
2014-11-30 19:09:41 +03:00
|
|
|
struct smb_request *req = conn->data->req.protop;
|
|
|
|
struct smb_header *h;
|
|
|
|
enum smb_req_state next_state = SMB_DONE;
|
|
|
|
unsigned short len;
|
|
|
|
unsigned short off;
|
|
|
|
CURLcode result;
|
|
|
|
void *msg = NULL;
|
|
|
|
|
|
|
|
/* Start the 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;
|
|
|
|
}
|
|
|
|
|
2014-12-13 15:12:45 +03:00
|
|
|
request_state(conn, SMB_TREE_CONNECT);
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the previous message and check for a 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_swap32(SMB_ERR_NOACCESS))
|
|
|
|
req->result = CURLE_REMOTE_ACCESS_DENIED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
req->tid = smb_swap16(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;
|
|
|
|
}
|
2014-12-02 23:50:49 +03:00
|
|
|
req->fid = smb_swap16(((struct smb_nt_create_response *)msg)->fid);
|
2014-11-30 19:09:41 +03:00
|
|
|
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 =
|
2014-12-02 23:50:49 +03:00
|
|
|
smb_swap64(((struct smb_nt_create_response *)msg)->end_of_file);
|
2014-11-30 19:09:41 +03:00
|
|
|
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;
|
|
|
|
}
|
2015-01-01 07:56:16 +03:00
|
|
|
len = Curl_read16_le(((unsigned char *) msg) +
|
|
|
|
sizeof(struct smb_header) + 11);
|
|
|
|
off = Curl_read16_le(((unsigned char *) msg) +
|
|
|
|
sizeof(struct smb_header) + 13);
|
2014-12-09 15:38:55 +03:00
|
|
|
if(len > 0) {
|
|
|
|
result = Curl_client_write(conn, CLIENTWRITE_BODY,
|
|
|
|
(char *)msg + off + sizeof(unsigned int),
|
|
|
|
len);
|
2014-12-30 00:04:04 +03:00
|
|
|
if(result) {
|
|
|
|
req->result = result;
|
|
|
|
next_state = SMB_CLOSE;
|
|
|
|
break;
|
|
|
|
}
|
2014-12-09 15:38:55 +03:00
|
|
|
}
|
2014-11-30 19:09:41 +03:00
|
|
|
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;
|
2014-11-30 18:39:14 +03:00
|
|
|
|
2014-11-30 19:09:41 +03:00
|
|
|
case SMB_UPLOAD:
|
|
|
|
if(h->status) {
|
|
|
|
req->result = CURLE_UPLOAD_FAILED;
|
|
|
|
next_state = SMB_CLOSE;
|
|
|
|
break;
|
|
|
|
}
|
2015-01-01 07:56:16 +03:00
|
|
|
len = Curl_read16_le(((unsigned char *) msg) +
|
|
|
|
sizeof(struct smb_header) + 5);
|
2014-11-30 19:09:41 +03:00
|
|
|
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:
|
|
|
|
/* We don't care if the 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;
|
|
|
|
}
|
|
|
|
|
2014-12-13 15:12:45 +03:00
|
|
|
request_state(conn, next_state);
|
2014-11-30 19:09:41 +03:00
|
|
|
|
|
|
|
return CURLE_OK;
|
2014-11-30 18:39:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode smb_done(struct connectdata *conn, CURLcode status,
|
|
|
|
bool premature)
|
|
|
|
{
|
2014-11-30 19:09:41 +03:00
|
|
|
struct smb_request *req = conn->data->req.protop;
|
|
|
|
|
2014-11-30 21:29:23 +03:00
|
|
|
(void) premature;
|
|
|
|
|
2014-11-30 19:09:41 +03:00
|
|
|
Curl_safefree(req->share);
|
|
|
|
Curl_safefree(conn->data->req.protop);
|
2014-11-30 18:39:14 +03:00
|
|
|
|
2014-11-30 19:09:41 +03:00
|
|
|
return status;
|
2014-11-30 18:39:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode smb_disconnect(struct connectdata *conn, bool dead)
|
|
|
|
{
|
2014-11-30 19:09:41 +03:00
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
|
|
|
struct smb_request *req = conn->data->req.protop;
|
2014-11-30 18:39:14 +03:00
|
|
|
|
2014-11-30 21:29:23 +03:00
|
|
|
(void) dead;
|
|
|
|
|
2014-11-30 19:09:41 +03:00
|
|
|
Curl_safefree(smbc->domain);
|
|
|
|
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;
|
2014-11-30 18:39:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int smb_getsock(struct connectdata *conn, curl_socket_t *socks,
|
|
|
|
int numsocks)
|
|
|
|
{
|
2014-11-30 19:09:41 +03:00
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
|
|
|
|
|
|
|
if(!numsocks)
|
|
|
|
return GETSOCK_BLANK;
|
|
|
|
|
|
|
|
socks[0] = conn->sock[FIRSTSOCKET];
|
|
|
|
|
2014-12-30 16:09:31 +03:00
|
|
|
if(smbc->send_size || smbc->upload_size)
|
2014-11-30 19:09:41 +03:00
|
|
|
return GETSOCK_WRITESOCK(0);
|
2014-11-30 18:39:14 +03:00
|
|
|
|
2014-11-30 19:09:41 +03:00
|
|
|
return GETSOCK_READSOCK(0);
|
2014-11-30 18:39:14 +03:00
|
|
|
}
|
|
|
|
|
2014-12-07 00:02:06 +03:00
|
|
|
static CURLcode smb_parse_url_path(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
struct smb_request *req = data->req.protop;
|
|
|
|
char *path;
|
|
|
|
char *slash;
|
|
|
|
|
|
|
|
/* URL decode the path */
|
|
|
|
result = Curl_urldecode(data, data->state.path, 0, &path, NULL, TRUE);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
/* Parse the path for the share */
|
|
|
|
req->share = strdup((*path == '/' || *path == '\\') ? path + 1 : path);
|
|
|
|
if(!req->share) {
|
2015-03-16 17:01:15 +03:00
|
|
|
free(path);
|
2014-12-07 00:02:06 +03:00
|
|
|
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
slash = strchr(req->share, '/');
|
|
|
|
if(!slash)
|
|
|
|
slash = strchr(req->share, '\\');
|
|
|
|
|
|
|
|
/* The share must be present */
|
|
|
|
if(!slash) {
|
2015-03-16 17:01:15 +03:00
|
|
|
free(path);
|
2014-12-07 00:02:06 +03:00
|
|
|
|
|
|
|
return CURLE_URL_MALFORMAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the path for the file path converting any forward slashes into
|
|
|
|
backslashes */
|
|
|
|
*slash++ = 0;
|
|
|
|
req->path = slash;
|
|
|
|
for(; *slash; slash++) {
|
|
|
|
if(*slash == '/')
|
|
|
|
*slash = '\\';
|
|
|
|
}
|
|
|
|
|
2015-03-16 17:01:15 +03:00
|
|
|
free(path);
|
2014-12-07 00:02:06 +03:00
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2014-12-07 23:44:57 +03:00
|
|
|
#endif /* !USE_WINDOWS_SSPI || USE_WIN32_CRYPTO */
|
|
|
|
|
2014-12-13 01:08:03 +03:00
|
|
|
#endif /* CURL_DISABLE_SMB && USE_NTLM && CURL_SIZEOF_CURL_OFF_T > 4 */
|