2014-11-29 21:12:44 +03:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 15:51:48 +03:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
|
|
|
* Copyright (C) Bill Nagel <wnagel@tycoint.com>, Exacq Technologies
|
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
|
2020-11-04 16:02:01 +03:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2014-11-29 21:12:44 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 12:16:50 +03:00
|
|
|
*
|
2014-11-29 21:12:44 +03:00
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "curl_setup.h"
|
|
|
|
|
2023-02-24 02:45:16 +03:00
|
|
|
#if !defined(CURL_DISABLE_SMB) && defined(USE_CURL_NTLM_CORE)
|
2014-11-29 21:12:44 +03:00
|
|
|
|
2023-11-21 19:54:49 +03:00
|
|
|
#ifdef _WIN32
|
2017-02-20 12:27:51 +03:00
|
|
|
#define getpid GetCurrentProcessId
|
|
|
|
#endif
|
2014-11-30 19:09:41 +03:00
|
|
|
|
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"
|
2022-11-11 13:45:34 +03:00
|
|
|
#include "cfilters.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"
|
|
|
|
|
2023-05-09 13:10:40 +03:00
|
|
|
/*
|
|
|
|
* Definitions for SMB protocol data structures
|
|
|
|
*/
|
|
|
|
#if defined(_MSC_VER) || defined(__ILEC400__)
|
|
|
|
# define PACK
|
|
|
|
# pragma pack(push)
|
|
|
|
# pragma pack(1)
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
# define PACK __attribute__((packed))
|
|
|
|
#else
|
|
|
|
# define PACK
|
|
|
|
#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_WC_CLOSE 0x03
|
|
|
|
#define SMB_WC_READ_ANDX 0x0c
|
|
|
|
#define SMB_WC_WRITE_ANDX 0x0e
|
|
|
|
#define SMB_WC_SETUP_ANDX 0x0d
|
|
|
|
#define SMB_WC_TREE_CONNECT_ANDX 0x04
|
|
|
|
#define SMB_WC_NT_CREATE_ANDX 0x18
|
|
|
|
|
|
|
|
#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_CAP_LARGE_FILES 0x08
|
|
|
|
#define SMB_GENERIC_WRITE 0x40000000
|
|
|
|
#define SMB_GENERIC_READ 0x80000000
|
|
|
|
#define SMB_FILE_SHARE_ALL 0x07
|
|
|
|
#define SMB_FILE_OPEN 0x01
|
|
|
|
#define SMB_FILE_OVERWRITE_IF 0x05
|
|
|
|
|
|
|
|
#define SMB_ERR_NOACCESS 0x00050001
|
|
|
|
|
|
|
|
struct smb_header {
|
|
|
|
unsigned char nbt_type;
|
|
|
|
unsigned char nbt_flags;
|
|
|
|
unsigned short nbt_length;
|
|
|
|
unsigned char magic[4];
|
|
|
|
unsigned char command;
|
|
|
|
unsigned int status;
|
|
|
|
unsigned char flags;
|
|
|
|
unsigned short flags2;
|
|
|
|
unsigned short pid_high;
|
|
|
|
unsigned char signature[8];
|
|
|
|
unsigned short pad;
|
|
|
|
unsigned short tid;
|
|
|
|
unsigned short pid;
|
|
|
|
unsigned short uid;
|
|
|
|
unsigned short mid;
|
|
|
|
} PACK;
|
|
|
|
|
|
|
|
struct smb_negotiate_response {
|
|
|
|
struct smb_header h;
|
|
|
|
unsigned char word_count;
|
|
|
|
unsigned short dialect_index;
|
|
|
|
unsigned char security_mode;
|
|
|
|
unsigned short max_mpx_count;
|
|
|
|
unsigned short max_number_vcs;
|
|
|
|
unsigned int max_buffer_size;
|
|
|
|
unsigned int max_raw_size;
|
|
|
|
unsigned int session_key;
|
|
|
|
unsigned int capabilities;
|
|
|
|
unsigned int system_time_low;
|
|
|
|
unsigned int system_time_high;
|
|
|
|
unsigned short server_time_zone;
|
|
|
|
unsigned char encryption_key_length;
|
|
|
|
unsigned short byte_count;
|
|
|
|
char bytes[1];
|
|
|
|
} PACK;
|
|
|
|
|
|
|
|
struct andx {
|
|
|
|
unsigned char command;
|
|
|
|
unsigned char pad;
|
|
|
|
unsigned short offset;
|
|
|
|
} PACK;
|
|
|
|
|
|
|
|
struct smb_setup {
|
|
|
|
unsigned char word_count;
|
|
|
|
struct andx andx;
|
|
|
|
unsigned short max_buffer_size;
|
|
|
|
unsigned short max_mpx_count;
|
|
|
|
unsigned short vc_number;
|
|
|
|
unsigned int session_key;
|
|
|
|
unsigned short lengths[2];
|
|
|
|
unsigned int pad;
|
|
|
|
unsigned int capabilities;
|
|
|
|
unsigned short byte_count;
|
|
|
|
char bytes[1024];
|
|
|
|
} PACK;
|
|
|
|
|
|
|
|
struct smb_tree_connect {
|
|
|
|
unsigned char word_count;
|
|
|
|
struct andx andx;
|
|
|
|
unsigned short flags;
|
|
|
|
unsigned short pw_len;
|
|
|
|
unsigned short byte_count;
|
|
|
|
char bytes[1024];
|
|
|
|
} PACK;
|
|
|
|
|
|
|
|
struct smb_nt_create {
|
|
|
|
unsigned char word_count;
|
|
|
|
struct andx andx;
|
|
|
|
unsigned char pad;
|
|
|
|
unsigned short name_length;
|
|
|
|
unsigned int flags;
|
|
|
|
unsigned int root_fid;
|
|
|
|
unsigned int access;
|
|
|
|
curl_off_t allocation_size;
|
|
|
|
unsigned int ext_file_attributes;
|
|
|
|
unsigned int share_access;
|
|
|
|
unsigned int create_disposition;
|
|
|
|
unsigned int create_options;
|
|
|
|
unsigned int impersonation_level;
|
|
|
|
unsigned char security_flags;
|
|
|
|
unsigned short byte_count;
|
|
|
|
char bytes[1024];
|
|
|
|
} PACK;
|
|
|
|
|
|
|
|
struct smb_nt_create_response {
|
|
|
|
struct smb_header h;
|
|
|
|
unsigned char word_count;
|
|
|
|
struct andx andx;
|
|
|
|
unsigned char op_lock_level;
|
|
|
|
unsigned short fid;
|
|
|
|
unsigned int create_disposition;
|
|
|
|
|
|
|
|
curl_off_t create_time;
|
|
|
|
curl_off_t last_access_time;
|
|
|
|
curl_off_t last_write_time;
|
|
|
|
curl_off_t last_change_time;
|
|
|
|
unsigned int ext_file_attributes;
|
|
|
|
curl_off_t allocation_size;
|
|
|
|
curl_off_t end_of_file;
|
|
|
|
} PACK;
|
|
|
|
|
|
|
|
struct smb_read {
|
|
|
|
unsigned char word_count;
|
|
|
|
struct andx andx;
|
|
|
|
unsigned short fid;
|
|
|
|
unsigned int offset;
|
|
|
|
unsigned short max_bytes;
|
|
|
|
unsigned short min_bytes;
|
|
|
|
unsigned int timeout;
|
|
|
|
unsigned short remaining;
|
|
|
|
unsigned int offset_high;
|
|
|
|
unsigned short byte_count;
|
|
|
|
} PACK;
|
|
|
|
|
|
|
|
struct smb_write {
|
|
|
|
struct smb_header h;
|
|
|
|
unsigned char word_count;
|
|
|
|
struct andx andx;
|
|
|
|
unsigned short fid;
|
|
|
|
unsigned int offset;
|
|
|
|
unsigned int timeout;
|
|
|
|
unsigned short write_mode;
|
|
|
|
unsigned short remaining;
|
|
|
|
unsigned short pad;
|
|
|
|
unsigned short data_length;
|
|
|
|
unsigned short data_offset;
|
|
|
|
unsigned int offset_high;
|
|
|
|
unsigned short byte_count;
|
|
|
|
unsigned char pad2;
|
|
|
|
} PACK;
|
|
|
|
|
|
|
|
struct smb_close {
|
|
|
|
unsigned char word_count;
|
|
|
|
unsigned short fid;
|
|
|
|
unsigned int last_mtime;
|
|
|
|
unsigned short byte_count;
|
|
|
|
} PACK;
|
|
|
|
|
|
|
|
struct smb_tree_disconnect {
|
|
|
|
unsigned char word_count;
|
|
|
|
unsigned short byte_count;
|
|
|
|
} PACK;
|
|
|
|
|
|
|
|
#if defined(_MSC_VER) || defined(__ILEC400__)
|
|
|
|
# pragma pack(pop)
|
|
|
|
#endif
|
|
|
|
|
2014-11-30 18:39:14 +03:00
|
|
|
/* Local API functions */
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_setup_connection(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn);
|
|
|
|
static CURLcode smb_connect(struct Curl_easy *data, bool *done);
|
|
|
|
static CURLcode smb_connection_state(struct Curl_easy *data, bool *done);
|
|
|
|
static CURLcode smb_do(struct Curl_easy *data, bool *done);
|
|
|
|
static CURLcode smb_request_state(struct Curl_easy *data, bool *done);
|
|
|
|
static CURLcode smb_disconnect(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn, bool dead);
|
|
|
|
static int smb_getsock(struct Curl_easy *data, struct connectdata *conn,
|
|
|
|
curl_socket_t *socks);
|
|
|
|
static CURLcode smb_parse_url_path(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn);
|
2014-11-30 18:39:14 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SMB handler interface
|
|
|
|
*/
|
|
|
|
const struct Curl_handler Curl_handler_smb = {
|
2024-05-07 17:55:23 +03:00
|
|
|
"smb", /* scheme */
|
2014-12-24 20:10:28 +03:00
|
|
|
smb_setup_connection, /* setup_connection */
|
2018-07-29 18:58:10 +03:00
|
|
|
smb_do, /* do_it */
|
2022-12-19 10:38:37 +03:00
|
|
|
ZERO_NULL, /* done */
|
2014-11-30 18:39:14 +03:00
|
|
|
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 */
|
lib: replace readwrite with write_resp
This clarifies the handling of server responses by folding the code for
the complicated protocols into their protocol handlers. This concerns
mainly HTTP and its bastard sibling RTSP.
The terms "read" and "write" are often used without clear context if
they refer to the connect or the client/application side of a
transfer. This PR uses "read/write" for operations on the client side
and "send/receive" for the connection, e.g. server side. If this is
considered useful, we can revisit renaming of further methods in another
PR.
Curl's protocol handler `readwrite()` method been changed:
```diff
- CURLcode (*readwrite)(struct Curl_easy *data, struct connectdata *conn,
- const char *buf, size_t blen,
- size_t *pconsumed, bool *readmore);
+ CURLcode (*write_resp)(struct Curl_easy *data, const char *buf, size_t blen,
+ bool is_eos, bool *done);
```
The name was changed to clarify that this writes reponse data to the
client side. The parameter changes are:
* `conn` removed as it always operates on `data->conn`
* `pconsumed` removed as the method needs to handle all data on success
* `readmore` removed as no longer necessary
* `is_eos` as indicator that this is the last call for the transfer
response (end-of-stream).
* `done` TRUE on return iff the transfer response is to be treated as
finished
This change affects many files only because of updated comments in
handlers that provide no implementation. The real change is that the
HTTP protocol handlers now provide an implementation.
The HTTP protocol handlers `write_resp()` implementation will get passed
**all** raw data of a server response for the transfer. The HTTP/1.x
formatted status and headers, as well as the undecoded response
body. `Curl_http_write_resp_hds()` is used internally to parse the
response headers and pass them on. This method is public as the RTSP
protocol handler also uses it.
HTTP/1.1 "chunked" transport encoding is now part of the general
*content encoding* writer stack, just like other encodings. A new flag
`CLIENTWRITE_EOS` was added for the last client write. This allows
writers to verify that they are in a valid end state. The chunked
decoder will check if it indeed has seen the last chunk.
The general response handling in `transfer.c:466` happens in function
`readwrite_data()`. This mainly operates now like:
```
static CURLcode readwrite_data(data, ...)
{
do {
Curl_xfer_recv_resp(data, buf)
...
Curl_xfer_write_resp(data, buf)
...
} while(interested);
...
}
```
All the response data handling is implemented in
`Curl_xfer_write_resp()`. It calls the protocol handler's `write_resp()`
implementation if available, or does the default behaviour.
All raw response data needs to pass through this function. Which also
means that anyone in possession of such data may call
`Curl_xfer_write_resp()`.
Closes #12480
2023-12-01 15:50:32 +03:00
|
|
|
ZERO_NULL, /* write_resp */
|
2024-03-21 14:15:59 +03:00
|
|
|
ZERO_NULL, /* write_resp_hd */
|
2017-05-31 14:09:56 +03:00
|
|
|
ZERO_NULL, /* connection_check */
|
2021-05-17 09:54:00 +03:00
|
|
|
ZERO_NULL, /* attach connection */
|
2014-11-30 18:39:14 +03:00
|
|
|
PORT_SMB, /* defport */
|
|
|
|
CURLPROTO_SMB, /* protocol */
|
2020-09-21 14:45:24 +03:00
|
|
|
CURLPROTO_SMB, /* family */
|
2014-11-30 18:39:14 +03:00
|
|
|
PROTOPT_NONE /* flags */
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef USE_SSL
|
|
|
|
/*
|
|
|
|
* SMBS handler interface
|
|
|
|
*/
|
|
|
|
const struct Curl_handler Curl_handler_smbs = {
|
2024-05-07 17:55:23 +03:00
|
|
|
"smbs", /* scheme */
|
2014-12-24 20:10:28 +03:00
|
|
|
smb_setup_connection, /* setup_connection */
|
2018-07-29 18:58:10 +03:00
|
|
|
smb_do, /* do_it */
|
2022-12-19 10:38:37 +03:00
|
|
|
ZERO_NULL, /* done */
|
2014-11-30 18:39:14 +03:00
|
|
|
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 */
|
lib: replace readwrite with write_resp
This clarifies the handling of server responses by folding the code for
the complicated protocols into their protocol handlers. This concerns
mainly HTTP and its bastard sibling RTSP.
The terms "read" and "write" are often used without clear context if
they refer to the connect or the client/application side of a
transfer. This PR uses "read/write" for operations on the client side
and "send/receive" for the connection, e.g. server side. If this is
considered useful, we can revisit renaming of further methods in another
PR.
Curl's protocol handler `readwrite()` method been changed:
```diff
- CURLcode (*readwrite)(struct Curl_easy *data, struct connectdata *conn,
- const char *buf, size_t blen,
- size_t *pconsumed, bool *readmore);
+ CURLcode (*write_resp)(struct Curl_easy *data, const char *buf, size_t blen,
+ bool is_eos, bool *done);
```
The name was changed to clarify that this writes reponse data to the
client side. The parameter changes are:
* `conn` removed as it always operates on `data->conn`
* `pconsumed` removed as the method needs to handle all data on success
* `readmore` removed as no longer necessary
* `is_eos` as indicator that this is the last call for the transfer
response (end-of-stream).
* `done` TRUE on return iff the transfer response is to be treated as
finished
This change affects many files only because of updated comments in
handlers that provide no implementation. The real change is that the
HTTP protocol handlers now provide an implementation.
The HTTP protocol handlers `write_resp()` implementation will get passed
**all** raw data of a server response for the transfer. The HTTP/1.x
formatted status and headers, as well as the undecoded response
body. `Curl_http_write_resp_hds()` is used internally to parse the
response headers and pass them on. This method is public as the RTSP
protocol handler also uses it.
HTTP/1.1 "chunked" transport encoding is now part of the general
*content encoding* writer stack, just like other encodings. A new flag
`CLIENTWRITE_EOS` was added for the last client write. This allows
writers to verify that they are in a valid end state. The chunked
decoder will check if it indeed has seen the last chunk.
The general response handling in `transfer.c:466` happens in function
`readwrite_data()`. This mainly operates now like:
```
static CURLcode readwrite_data(data, ...)
{
do {
Curl_xfer_recv_resp(data, buf)
...
Curl_xfer_write_resp(data, buf)
...
} while(interested);
...
}
```
All the response data handling is implemented in
`Curl_xfer_write_resp()`. It calls the protocol handler's `write_resp()`
implementation if available, or does the default behaviour.
All raw response data needs to pass through this function. Which also
means that anyone in possession of such data may call
`Curl_xfer_write_resp()`.
Closes #12480
2023-12-01 15:50:32 +03:00
|
|
|
ZERO_NULL, /* write_resp */
|
2024-03-21 14:15:59 +03:00
|
|
|
ZERO_NULL, /* write_resp_hd */
|
2017-05-31 14:09:56 +03:00
|
|
|
ZERO_NULL, /* connection_check */
|
2021-05-17 09:54:00 +03:00
|
|
|
ZERO_NULL, /* attach connection */
|
2014-11-30 18:39:14 +03:00
|
|
|
PORT_SMBS, /* defport */
|
|
|
|
CURLPROTO_SMBS, /* protocol */
|
2020-09-21 14:45:24 +03:00
|
|
|
CURLPROTO_SMB, /* family */
|
2014-11-30 18:39:14 +03:00
|
|
|
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 */
|
2017-02-14 17:10:31 +03:00
|
|
|
#define MSGCAT(str) \
|
2020-12-26 17:43:25 +03:00
|
|
|
do { \
|
|
|
|
strcpy(p, (str)); \
|
|
|
|
p += strlen(str); \
|
|
|
|
} while(0)
|
2014-11-30 19:09:41 +03:00
|
|
|
|
|
|
|
/* Append a null-terminated string to an SMB message */
|
2017-02-14 17:10:31 +03:00
|
|
|
#define MSGCATNULL(str) \
|
2020-12-26 17:43:25 +03:00
|
|
|
do { \
|
|
|
|
strcpy(p, (str)); \
|
|
|
|
p += strlen(str) + 1; \
|
|
|
|
} while(0)
|
2014-11-30 19:09:41 +03:00
|
|
|
|
|
|
|
/* SMB is mostly little endian */
|
2014-12-05 20:37:35 +03:00
|
|
|
#if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \
|
2017-02-14 17:10:31 +03:00
|
|
|
defined(__OS400__)
|
2014-11-30 19:09:41 +03:00
|
|
|
static unsigned short smb_swap16(unsigned short x)
|
|
|
|
{
|
2015-11-21 14:41:20 +03:00
|
|
|
return (unsigned short) ((x << 8) | ((x >> 8) & 0xff));
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int smb_swap32(unsigned int x)
|
|
|
|
{
|
|
|
|
return (x << 24) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) |
|
2017-02-14 17:10:31 +03:00
|
|
|
((x >> 24) & 0xff);
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
2018-01-15 22:43:34 +03:00
|
|
|
static curl_off_t smb_swap64(curl_off_t x)
|
2014-11-30 19:09:41 +03:00
|
|
|
{
|
2018-01-15 22:43:34 +03:00
|
|
|
return ((curl_off_t) smb_swap32((unsigned int) x) << 32) |
|
2017-02-14 17:10:31 +03:00
|
|
|
smb_swap32((unsigned int) (x >> 32));
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
2018-01-15 22:43:34 +03:00
|
|
|
|
2014-11-30 19:09:41 +03:00
|
|
|
#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 *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;
|
|
|
|
};
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static void conn_state(struct Curl_easy *data, enum smb_conn_state newstate)
|
2014-12-13 15:12:45 +03:00
|
|
|
{
|
2021-01-08 19:58:15 +03:00
|
|
|
struct smb_conn *smbc = &data->conn->proto.smbc;
|
2014-12-13 15:12:45 +03:00
|
|
|
#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 */
|
|
|
|
};
|
|
|
|
|
2018-07-29 18:58:10 +03:00
|
|
|
if(smbc->state != newstate)
|
2021-07-06 18:05:17 +03:00
|
|
|
infof(data, "SMB conn %p state change from %s to %s",
|
2018-07-29 18:58:10 +03:00
|
|
|
(void *)smbc, names[smbc->state], names[newstate]);
|
2014-12-13 15:12:45 +03:00
|
|
|
#endif
|
|
|
|
|
2018-07-29 18:58:10 +03:00
|
|
|
smbc->state = newstate;
|
2014-12-13 15:12:45 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static void request_state(struct Curl_easy *data,
|
2014-12-13 16:06:56 +03:00
|
|
|
enum smb_req_state newstate)
|
2014-12-13 15:12:45 +03:00
|
|
|
{
|
2021-01-08 19:58:15 +03:00
|
|
|
struct smb_request *req = data->req.p.smb;
|
2014-12-13 15:12:45 +03:00
|
|
|
#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)
|
2021-07-06 18:05:17 +03:00
|
|
|
infof(data, "SMB request %p state change from %s to %s",
|
2017-02-14 17:10:31 +03:00
|
|
|
(void *)req, names[req->state], names[newstate]);
|
2014-12-13 15:12:45 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
req->state = newstate;
|
|
|
|
}
|
|
|
|
|
2018-07-29 18:58:10 +03:00
|
|
|
/* this should setup things in the connection, not in the easy
|
|
|
|
handle */
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_setup_connection(struct Curl_easy *data,
|
|
|
|
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 */
|
2021-01-08 19:58:15 +03:00
|
|
|
data->req.p.smb = req = calloc(1, sizeof(struct smb_request));
|
2014-11-30 19:09:41 +03:00
|
|
|
if(!req)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2014-12-07 00:02:06 +03:00
|
|
|
/* Parse the URL path */
|
2021-01-08 19:58:15 +03:00
|
|
|
return smb_parse_url_path(data, conn);
|
2014-11-30 18:39:14 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_connect(struct Curl_easy *data, bool *done)
|
2014-11-30 18:39:14 +03:00
|
|
|
{
|
2021-01-08 19:58:15 +03:00
|
|
|
struct connectdata *conn = data->conn;
|
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 */
|
2022-02-14 19:33:50 +03:00
|
|
|
if(!data->state.aptr.user)
|
2014-12-03 00:04:23 +03:00
|
|
|
return CURLE_LOGIN_DENIED;
|
|
|
|
|
2014-11-30 19:09:41 +03:00
|
|
|
/* Initialize the connection state */
|
|
|
|
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;
|
2024-02-28 16:51:53 +03:00
|
|
|
smbc->send_buf = malloc(MAX_MESSAGE_SIZE);
|
|
|
|
if(!smbc->send_buf)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2014-11-30 19:09:41 +03:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_recv_message(struct Curl_easy *data, void **msg)
|
2014-11-30 19:09:41 +03:00
|
|
|
{
|
2021-01-08 19:58:15 +03:00
|
|
|
struct connectdata *conn = data->conn;
|
2014-11-30 19:09:41 +03:00
|
|
|
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;
|
|
|
|
|
2024-02-14 14:09:32 +03:00
|
|
|
result = Curl_xfer_recv(data, buf + smbc->got, len, &bytes_read);
|
2014-11-30 19:09:41 +03:00
|
|
|
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;
|
|
|
|
|
2017-02-14 17:10:31 +03:00
|
|
|
nbt_size = Curl_read16_be((const 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) +
|
2017-02-14 17:10:31 +03:00
|
|
|
Curl_read16_le((const 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;
|
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static void smb_format_message(struct Curl_easy *data, 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
|
|
|
{
|
2021-01-08 19:58:15 +03:00
|
|
|
struct connectdata *conn = data->conn;
|
2014-11-30 19:09:41 +03:00
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
2021-01-08 19:58:15 +03:00
|
|
|
struct smb_request *req = data->req.p.smb;
|
2014-11-30 19:09:41 +03:00
|
|
|
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);
|
2024-06-02 23:30:52 +03:00
|
|
|
pid = (unsigned int)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
|
|
|
}
|
|
|
|
|
lib: Curl_read/Curl_write clarifications
- replace `Curl_read()`, `Curl_write()` and `Curl_nwrite()` to
clarify when and at what level they operate
- send/recv of transfer related data is now done via
`Curl_xfer_send()/Curl_xfer_recv()` which no longer has
socket/socketindex as parameter. It decides on the transfer
setup of `conn->sockfd` and `conn->writesockfd` on which
connection filter chain to operate.
- send/recv on a specific connection filter chain is done via
`Curl_conn_send()/Curl_conn_recv()` which get the socket index
as parameter.
- rename `Curl_setup_transfer()` to `Curl_xfer_setup()` for
naming consistency
- clarify that the special CURLE_AGAIN hangling to return
`CURLE_OK` with length 0 only applies to `Curl_xfer_send()`
and CURLE_AGAIN is returned by all other send() variants.
- fix a bug in websocket `curl_ws_recv()` that mixed up data
when it arrived in more than a single chunk
The method for sending not just raw bytes, but bytes that are either
"headers" or "body". The send abstraction stack, to to bottom, now is:
* `Curl_req_send()`: has parameter to indicate amount of header bytes,
buffers all data.
* `Curl_xfer_send()`: knows on which socket index to send, returns
amount of bytes sent.
* `Curl_conn_send()`: called with socket index, returns amount of bytes
sent.
In addition there is `Curl_req_flush()` for writing out all buffered
bytes.
`Curl_req_send()` is active for requests without body,
`Curl_buffer_send()` still being used for others. This is because the
special quirks need to be addressed in future parts:
* `expect-100` handling
* `Curl_fillreadbuffer()` needs to add directly to the new
`data->req.sendbuf`
* special body handlings, like `chunked` encodings and line end
conversions will be moved into something like a Client Reader.
In functions of the pattern `CURLcode xxx_send(..., ssize_t *written)`,
replace the `ssize_t` with a `size_t`. It makes no sense to allow for negative
values as the returned `CURLcode` already specifies error conditions. This
allows easier handling of lengths without casting.
Closes #12964
2024-02-15 18:22:53 +03:00
|
|
|
static CURLcode smb_send(struct Curl_easy *data, size_t len,
|
2014-12-30 16:09:31 +03:00
|
|
|
size_t upload_size)
|
2014-11-30 19:09:41 +03:00
|
|
|
{
|
2021-01-08 19:58:15 +03:00
|
|
|
struct connectdata *conn = data->conn;
|
2014-11-30 19:09:41 +03:00
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
lib: Curl_read/Curl_write clarifications
- replace `Curl_read()`, `Curl_write()` and `Curl_nwrite()` to
clarify when and at what level they operate
- send/recv of transfer related data is now done via
`Curl_xfer_send()/Curl_xfer_recv()` which no longer has
socket/socketindex as parameter. It decides on the transfer
setup of `conn->sockfd` and `conn->writesockfd` on which
connection filter chain to operate.
- send/recv on a specific connection filter chain is done via
`Curl_conn_send()/Curl_conn_recv()` which get the socket index
as parameter.
- rename `Curl_setup_transfer()` to `Curl_xfer_setup()` for
naming consistency
- clarify that the special CURLE_AGAIN hangling to return
`CURLE_OK` with length 0 only applies to `Curl_xfer_send()`
and CURLE_AGAIN is returned by all other send() variants.
- fix a bug in websocket `curl_ws_recv()` that mixed up data
when it arrived in more than a single chunk
The method for sending not just raw bytes, but bytes that are either
"headers" or "body". The send abstraction stack, to to bottom, now is:
* `Curl_req_send()`: has parameter to indicate amount of header bytes,
buffers all data.
* `Curl_xfer_send()`: knows on which socket index to send, returns
amount of bytes sent.
* `Curl_conn_send()`: called with socket index, returns amount of bytes
sent.
In addition there is `Curl_req_flush()` for writing out all buffered
bytes.
`Curl_req_send()` is active for requests without body,
`Curl_buffer_send()` still being used for others. This is because the
special quirks need to be addressed in future parts:
* `expect-100` handling
* `Curl_fillreadbuffer()` needs to add directly to the new
`data->req.sendbuf`
* special body handlings, like `chunked` encodings and line end
conversions will be moved into something like a Client Reader.
In functions of the pattern `CURLcode xxx_send(..., ssize_t *written)`,
replace the `ssize_t` with a `size_t`. It makes no sense to allow for negative
values as the returned `CURLcode` already specifies error conditions. This
allows easier handling of lengths without casting.
Closes #12964
2024-02-15 18:22:53 +03:00
|
|
|
size_t bytes_written;
|
2014-11-30 19:09:41 +03:00
|
|
|
CURLcode result;
|
|
|
|
|
2024-07-18 12:29:37 +03:00
|
|
|
result = Curl_xfer_send(data, smbc->send_buf, len, FALSE, &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;
|
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_flush(struct Curl_easy *data)
|
2014-11-30 19:09:41 +03:00
|
|
|
{
|
2021-01-08 19:58:15 +03:00
|
|
|
struct connectdata *conn = data->conn;
|
2014-11-30 19:09:41 +03:00
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
lib: Curl_read/Curl_write clarifications
- replace `Curl_read()`, `Curl_write()` and `Curl_nwrite()` to
clarify when and at what level they operate
- send/recv of transfer related data is now done via
`Curl_xfer_send()/Curl_xfer_recv()` which no longer has
socket/socketindex as parameter. It decides on the transfer
setup of `conn->sockfd` and `conn->writesockfd` on which
connection filter chain to operate.
- send/recv on a specific connection filter chain is done via
`Curl_conn_send()/Curl_conn_recv()` which get the socket index
as parameter.
- rename `Curl_setup_transfer()` to `Curl_xfer_setup()` for
naming consistency
- clarify that the special CURLE_AGAIN hangling to return
`CURLE_OK` with length 0 only applies to `Curl_xfer_send()`
and CURLE_AGAIN is returned by all other send() variants.
- fix a bug in websocket `curl_ws_recv()` that mixed up data
when it arrived in more than a single chunk
The method for sending not just raw bytes, but bytes that are either
"headers" or "body". The send abstraction stack, to to bottom, now is:
* `Curl_req_send()`: has parameter to indicate amount of header bytes,
buffers all data.
* `Curl_xfer_send()`: knows on which socket index to send, returns
amount of bytes sent.
* `Curl_conn_send()`: called with socket index, returns amount of bytes
sent.
In addition there is `Curl_req_flush()` for writing out all buffered
bytes.
`Curl_req_send()` is active for requests without body,
`Curl_buffer_send()` still being used for others. This is because the
special quirks need to be addressed in future parts:
* `expect-100` handling
* `Curl_fillreadbuffer()` needs to add directly to the new
`data->req.sendbuf`
* special body handlings, like `chunked` encodings and line end
conversions will be moved into something like a Client Reader.
In functions of the pattern `CURLcode xxx_send(..., ssize_t *written)`,
replace the `ssize_t` with a `size_t`. It makes no sense to allow for negative
values as the returned `CURLcode` already specifies error conditions. This
allows easier handling of lengths without casting.
Closes #12964
2024-02-15 18:22:53 +03:00
|
|
|
size_t bytes_written;
|
|
|
|
size_t len = smbc->send_size - smbc->sent;
|
2014-11-30 19:09:41 +03:00
|
|
|
CURLcode result;
|
|
|
|
|
|
|
|
if(!smbc->send_size)
|
|
|
|
return CURLE_OK;
|
|
|
|
|
2024-07-18 12:29:37 +03:00
|
|
|
result = Curl_xfer_send(data, smbc->send_buf + smbc->sent, len, FALSE,
|
2024-02-14 14:09:32 +03:00
|
|
|
&bytes_written);
|
2014-11-30 19:09:41 +03:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
if(bytes_written != len)
|
|
|
|
smbc->sent += bytes_written;
|
|
|
|
else
|
|
|
|
smbc->send_size = 0;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_send_message(struct Curl_easy *data, 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
|
|
|
{
|
2024-02-28 16:51:53 +03:00
|
|
|
struct connectdata *conn = data->conn;
|
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
|
|
|
|
|
|
|
smb_format_message(data, (struct smb_header *)smbc->send_buf,
|
2014-12-30 16:09:31 +03:00
|
|
|
cmd, msg_len);
|
2024-02-28 16:51:53 +03:00
|
|
|
DEBUGASSERT((sizeof(struct smb_header) + msg_len) <= MAX_MESSAGE_SIZE);
|
|
|
|
memcpy(smbc->send_buf + sizeof(struct smb_header), msg, msg_len);
|
2014-11-30 19:09:41 +03:00
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
return smb_send(data, sizeof(struct smb_header) + msg_len, 0);
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_send_negotiate(struct Curl_easy *data)
|
2014-11-30 19:09:41 +03:00
|
|
|
{
|
|
|
|
const char *msg = "\x00\x0c\x00\x02NT LM 0.12";
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
return smb_send_message(data, SMB_COM_NEGOTIATE, msg, 15);
|
2014-11-30 21:29:23 +03:00
|
|
|
}
|
2014-11-30 19:09:41 +03:00
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_send_setup(struct Curl_easy *data)
|
2014-11-30 19:09:41 +03:00
|
|
|
{
|
2021-01-08 19:58:15 +03:00
|
|
|
struct connectdata *conn = data->conn;
|
2014-11-30 19:09:41 +03:00
|
|
|
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;
|
|
|
|
|
2022-02-03 15:04:30 +03:00
|
|
|
Curl_ntlm_core_mk_lm_hash(conn->passwd, lm_hash);
|
2014-11-30 19:09:41 +03:00
|
|
|
Curl_ntlm_core_lm_resp(lm_hash, smbc->challenge, lm);
|
2022-02-03 15:04:30 +03:00
|
|
|
Curl_ntlm_core_mk_nt_hash(conn->passwd, nt_hash);
|
2014-11-30 19:09:41 +03:00
|
|
|
Curl_ntlm_core_lm_resp(nt_hash, smbc->challenge, nt);
|
|
|
|
|
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
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
return smb_send_message(data, 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
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_send_tree_connect(struct Curl_easy *data)
|
2014-11-30 19:09:41 +03:00
|
|
|
{
|
2014-11-30 21:56:36 +03:00
|
|
|
struct smb_tree_connect msg;
|
2021-01-08 19:58:15 +03:00
|
|
|
struct connectdata *conn = data->conn;
|
2018-07-29 18:58:10 +03:00
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
2014-11-30 21:56:36 +03:00
|
|
|
char *p = msg.bytes;
|
2014-11-30 19:09:41 +03:00
|
|
|
|
2018-07-29 18:58:10 +03:00
|
|
|
size_t byte_count = strlen(conn->host.name) + strlen(smbc->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("\\");
|
2018-07-29 18:58:10 +03:00
|
|
|
MSGCATNULL(smbc->share);
|
2014-11-30 19:09:41 +03:00
|
|
|
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
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
return smb_send_message(data, 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
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_send_open(struct Curl_easy *data)
|
2014-11-30 19:09:41 +03:00
|
|
|
{
|
2021-01-08 19:58:15 +03:00
|
|
|
struct smb_request *req = data->req.p.smb;
|
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);
|
2023-04-25 09:28:01 +03:00
|
|
|
if(data->state.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
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
return smb_send_message(data, 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
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_send_close(struct Curl_easy *data)
|
2014-11-30 19:09:41 +03:00
|
|
|
{
|
2021-01-08 19:58:15 +03:00
|
|
|
struct smb_request *req = data->req.p.smb;
|
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
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
return smb_send_message(data, SMB_COM_CLOSE, &msg, sizeof(msg));
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_send_tree_disconnect(struct Curl_easy *data)
|
2014-11-30 19:09:41 +03:00
|
|
|
{
|
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
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
return smb_send_message(data, SMB_COM_TREE_DISCONNECT, &msg, sizeof(msg));
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_send_read(struct Curl_easy *data)
|
2014-11-30 19:09:41 +03:00
|
|
|
{
|
2021-01-08 19:58:15 +03:00
|
|
|
struct smb_request *req = data->req.p.smb;
|
|
|
|
curl_off_t offset = 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);
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
return smb_send_message(data, SMB_COM_READ_ANDX, &msg, sizeof(msg));
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_send_write(struct Curl_easy *data)
|
2014-11-30 19:09:41 +03:00
|
|
|
{
|
2024-02-28 16:51:53 +03:00
|
|
|
struct connectdata *conn = data->conn;
|
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
2018-08-17 01:49:37 +03:00
|
|
|
struct smb_write *msg;
|
2021-01-08 19:58:15 +03:00
|
|
|
struct smb_request *req = data->req.p.smb;
|
|
|
|
curl_off_t offset = data->req.offset;
|
|
|
|
curl_off_t upload_size = data->req.size - data->req.bytecount;
|
2018-08-17 01:49:37 +03:00
|
|
|
|
2024-02-28 16:51:53 +03:00
|
|
|
msg = (struct smb_write *)smbc->send_buf;
|
2014-12-30 16:09:31 +03:00
|
|
|
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
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
smb_format_message(data, &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
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
return smb_send(data, sizeof(*msg), (size_t) upload_size);
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_send_and_recv(struct Curl_easy *data, void **msg)
|
2014-11-30 19:09:41 +03:00
|
|
|
{
|
2021-01-08 19:58:15 +03:00
|
|
|
struct connectdata *conn = data->conn;
|
2014-11-30 19:09:41 +03:00
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
|
|
|
CURLcode result;
|
2019-09-02 16:28:30 +03:00
|
|
|
*msg = NULL; /* if it returns early */
|
2014-11-30 19:09:41 +03:00
|
|
|
|
2014-12-30 16:09:31 +03:00
|
|
|
/* Check if there is data in the transfer buffer */
|
|
|
|
if(!smbc->send_size && smbc->upload_size) {
|
2024-02-28 16:51:53 +03:00
|
|
|
size_t nread = smbc->upload_size > (size_t)MAX_MESSAGE_SIZE ?
|
|
|
|
(size_t)MAX_MESSAGE_SIZE : smbc->upload_size;
|
2024-02-15 18:22:53 +03:00
|
|
|
bool eos;
|
|
|
|
|
2024-02-28 16:51:53 +03:00
|
|
|
result = Curl_client_read(data, smbc->send_buf, nread, &nread, &eos);
|
2014-12-30 16:09:31 +03:00
|
|
|
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) {
|
2021-01-08 19:58:15 +03:00
|
|
|
result = smb_flush(data);
|
2014-11-30 19:09:41 +03:00
|
|
|
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;
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
return smb_recv_message(data, msg);
|
2014-11-30 18:39:14 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_connection_state(struct Curl_easy *data, bool *done)
|
2014-11-30 18:39:14 +03:00
|
|
|
{
|
2021-01-08 19:58:15 +03:00
|
|
|
struct connectdata *conn = data->conn;
|
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)) {
|
2017-11-13 10:26:54 +03:00
|
|
|
bool ssl_done = FALSE;
|
2022-11-22 11:55:41 +03:00
|
|
|
result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssl_done);
|
2014-11-30 19:09:41 +03:00
|
|
|
if(result && result != CURLE_AGAIN)
|
|
|
|
return result;
|
|
|
|
if(!ssl_done)
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
result = smb_send_negotiate(data);
|
2014-11-30 19:09:41 +03:00
|
|
|
if(result) {
|
|
|
|
connclose(conn, "SMB: failed to send negotiate message");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
conn_state(data, SMB_NEGOTIATE);
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the previous message and check for a response */
|
2021-01-08 19:58:15 +03:00
|
|
|
result = smb_send_and_recv(data, &msg);
|
2014-11-30 19:09:41 +03:00
|
|
|
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:
|
2019-09-16 11:15:05 +03:00
|
|
|
if((smbc->got < sizeof(*nrsp) + sizeof(smbc->challenge) - 1) ||
|
|
|
|
h->status) {
|
2014-11-30 19:09:41 +03:00
|
|
|
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);
|
2021-01-08 19:58:15 +03:00
|
|
|
result = smb_send_setup(data);
|
2014-11-30 19:09:41 +03:00
|
|
|
if(result) {
|
|
|
|
connclose(conn, "SMB: failed to send setup message");
|
|
|
|
return result;
|
|
|
|
}
|
2021-01-08 19:58:15 +03:00
|
|
|
conn_state(data, 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);
|
2021-01-08 19:58:15 +03:00
|
|
|
conn_state(data, 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
|
|
|
}
|
|
|
|
|
2017-07-04 16:51:45 +03:00
|
|
|
/*
|
2018-01-26 01:05:24 +03:00
|
|
|
* Convert a timestamp from the Windows world (100 nsec units from 1 Jan 1601)
|
tidy-up: OS names
Use these words and casing more consistently across text, comments and
one curl tool output:
AIX, ALPN, ANSI, BSD, Cygwin, Darwin, FreeBSD, GitHub, HP-UX, Linux,
macOS, MS-DOS, MSYS, MinGW, NTLM, POSIX, Solaris, UNIX, Unix, Unicode,
WINE, WebDAV, Win32, winbind, WinIDN, Windows, Windows CE, Winsock.
Mostly OS names and a few more.
Also a couple of other minor text fixups.
Closes #14360
2024-08-03 02:09:57 +03:00
|
|
|
* to POSIX time. Cap the output to fit within a time_t.
|
2017-07-04 16:51:45 +03:00
|
|
|
*/
|
2018-01-26 01:05:24 +03:00
|
|
|
static void get_posix_time(time_t *out, curl_off_t timestamp)
|
2017-07-04 16:51:45 +03:00
|
|
|
{
|
2018-01-15 22:43:34 +03:00
|
|
|
timestamp -= 116444736000000000;
|
2017-07-09 00:19:36 +03:00
|
|
|
timestamp /= 10000000;
|
2018-02-09 16:29:03 +03:00
|
|
|
#if SIZEOF_TIME_T < SIZEOF_CURL_OFF_T
|
2018-01-26 01:05:24 +03:00
|
|
|
if(timestamp > TIME_T_MAX)
|
|
|
|
*out = TIME_T_MAX;
|
|
|
|
else if(timestamp < TIME_T_MIN)
|
|
|
|
*out = TIME_T_MIN;
|
|
|
|
else
|
2018-02-09 16:29:03 +03:00
|
|
|
#endif
|
2018-01-26 01:05:24 +03:00
|
|
|
*out = (time_t) timestamp;
|
2017-07-04 16:51:45 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_request_state(struct Curl_easy *data, bool *done)
|
2014-11-30 18:39:14 +03:00
|
|
|
{
|
2021-01-08 19:58:15 +03:00
|
|
|
struct connectdata *conn = data->conn;
|
|
|
|
struct smb_request *req = data->req.p.smb;
|
2014-11-30 19:09:41 +03:00
|
|
|
struct smb_header *h;
|
2016-10-03 22:48:31 +03:00
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
2014-11-30 19:09:41 +03:00
|
|
|
enum smb_req_state next_state = SMB_DONE;
|
|
|
|
unsigned short len;
|
|
|
|
unsigned short off;
|
|
|
|
CURLcode result;
|
|
|
|
void *msg = NULL;
|
2017-07-04 16:51:45 +03:00
|
|
|
const struct smb_nt_create_response *smb_m;
|
2014-11-30 19:09:41 +03:00
|
|
|
|
2023-04-25 09:28:01 +03:00
|
|
|
if(data->state.upload && (data->state.infilesize < 0)) {
|
2023-02-12 15:24:08 +03:00
|
|
|
failf(data, "SMB upload needs to know the size up front");
|
|
|
|
return CURLE_SEND_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-11-30 19:09:41 +03:00
|
|
|
/* Start the request */
|
|
|
|
if(req->state == SMB_REQUESTING) {
|
2021-01-08 19:58:15 +03:00
|
|
|
result = smb_send_tree_connect(data);
|
2014-11-30 19:09:41 +03:00
|
|
|
if(result) {
|
|
|
|
connclose(conn, "SMB: failed to send tree connect message");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
request_state(data, SMB_TREE_CONNECT);
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the previous message and check for a response */
|
2021-01-08 19:58:15 +03:00
|
|
|
result = smb_send_and_recv(data, &msg);
|
2014-11-30 19:09:41 +03:00
|
|
|
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:
|
2016-10-03 22:48:31 +03:00
|
|
|
if(h->status || smbc->got < sizeof(struct smb_nt_create_response)) {
|
2014-11-30 19:09:41 +03:00
|
|
|
req->result = CURLE_REMOTE_FILE_NOT_FOUND;
|
2019-07-07 18:10:24 +03:00
|
|
|
if(h->status == smb_swap32(SMB_ERR_NOACCESS))
|
|
|
|
req->result = CURLE_REMOTE_ACCESS_DENIED;
|
2014-11-30 19:09:41 +03:00
|
|
|
next_state = SMB_TREE_DISCONNECT;
|
|
|
|
break;
|
|
|
|
}
|
2017-07-04 16:51:45 +03:00
|
|
|
smb_m = (const struct smb_nt_create_response*) msg;
|
|
|
|
req->fid = smb_swap16(smb_m->fid);
|
2021-01-08 19:58:15 +03:00
|
|
|
data->req.offset = 0;
|
2023-04-25 09:28:01 +03:00
|
|
|
if(data->state.upload) {
|
2021-01-08 19:58:15 +03:00
|
|
|
data->req.size = data->state.infilesize;
|
|
|
|
Curl_pgrsSetUploadSize(data, data->req.size);
|
2014-11-30 19:09:41 +03:00
|
|
|
next_state = SMB_UPLOAD;
|
|
|
|
}
|
|
|
|
else {
|
2021-01-08 19:58:15 +03:00
|
|
|
data->req.size = smb_swap64(smb_m->end_of_file);
|
|
|
|
if(data->req.size < 0) {
|
2018-05-12 23:24:25 +03:00
|
|
|
req->result = CURLE_WEIRD_SERVER_REPLY;
|
|
|
|
next_state = SMB_CLOSE;
|
|
|
|
}
|
|
|
|
else {
|
2021-01-08 19:58:15 +03:00
|
|
|
Curl_pgrsSetDownloadSize(data, data->req.size);
|
|
|
|
if(data->set.get_filetime)
|
|
|
|
get_posix_time(&data->info.filetime, smb_m->last_change_time);
|
2018-05-12 23:24:25 +03:00
|
|
|
next_state = SMB_DOWNLOAD;
|
|
|
|
}
|
2014-11-30 19:09:41 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SMB_DOWNLOAD:
|
2016-10-03 22:48:31 +03:00
|
|
|
if(h->status || smbc->got < sizeof(struct smb_header) + 14) {
|
2014-11-30 19:09:41 +03:00
|
|
|
req->result = CURLE_RECV_ERROR;
|
|
|
|
next_state = SMB_CLOSE;
|
|
|
|
break;
|
|
|
|
}
|
2016-11-24 18:14:21 +03:00
|
|
|
len = Curl_read16_le(((const unsigned char *) msg) +
|
2015-01-01 07:56:16 +03:00
|
|
|
sizeof(struct smb_header) + 11);
|
2016-11-24 18:14:21 +03:00
|
|
|
off = Curl_read16_le(((const unsigned char *) msg) +
|
2015-01-01 07:56:16 +03:00
|
|
|
sizeof(struct smb_header) + 13);
|
2014-12-09 15:38:55 +03:00
|
|
|
if(len > 0) {
|
2015-05-22 11:28:21 +03:00
|
|
|
if(off + sizeof(unsigned int) + len > smbc->got) {
|
2021-01-08 19:58:15 +03:00
|
|
|
failf(data, "Invalid input packet");
|
2015-05-22 11:28:21 +03:00
|
|
|
result = CURLE_RECV_ERROR;
|
|
|
|
}
|
|
|
|
else
|
2021-01-08 19:58:15 +03:00
|
|
|
result = Curl_client_write(data, CLIENTWRITE_BODY,
|
2015-05-22 11:28:21 +03:00
|
|
|
(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
|
|
|
}
|
2021-01-08 19:58:15 +03:00
|
|
|
data->req.offset += len;
|
2014-11-30 19:09:41 +03:00
|
|
|
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:
|
2016-10-03 22:48:31 +03:00
|
|
|
if(h->status || smbc->got < sizeof(struct smb_header) + 6) {
|
2014-11-30 19:09:41 +03:00
|
|
|
req->result = CURLE_UPLOAD_FAILED;
|
|
|
|
next_state = SMB_CLOSE;
|
|
|
|
break;
|
|
|
|
}
|
2016-11-24 18:14:21 +03:00
|
|
|
len = Curl_read16_le(((const unsigned char *) msg) +
|
2015-01-01 07:56:16 +03:00
|
|
|
sizeof(struct smb_header) + 5);
|
2021-01-08 19:58:15 +03:00
|
|
|
data->req.bytecount += len;
|
|
|
|
data->req.offset += len;
|
|
|
|
Curl_pgrsSetUploadCounter(data, data->req.bytecount);
|
|
|
|
if(data->req.bytecount >= data->req.size)
|
2014-11-30 19:09:41 +03:00
|
|
|
next_state = SMB_CLOSE;
|
|
|
|
else
|
|
|
|
next_state = SMB_UPLOAD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SMB_CLOSE:
|
2024-07-01 17:47:21 +03:00
|
|
|
/* We do not care if the close failed, proceed to tree disconnect anyway */
|
2014-11-30 19:09:41 +03:00
|
|
|
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:
|
2021-01-08 19:58:15 +03:00
|
|
|
result = smb_send_open(data);
|
2014-11-30 19:09:41 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SMB_DOWNLOAD:
|
2021-01-08 19:58:15 +03:00
|
|
|
result = smb_send_read(data);
|
2014-11-30 19:09:41 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SMB_UPLOAD:
|
2021-01-08 19:58:15 +03:00
|
|
|
result = smb_send_write(data);
|
2014-11-30 19:09:41 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SMB_CLOSE:
|
2021-01-08 19:58:15 +03:00
|
|
|
result = smb_send_close(data);
|
2014-11-30 19:09:41 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SMB_TREE_DISCONNECT:
|
2021-01-08 19:58:15 +03:00
|
|
|
result = smb_send_tree_disconnect(data);
|
2014-11-30 19:09:41 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SMB_DONE:
|
|
|
|
result = req->result;
|
|
|
|
*done = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(result) {
|
|
|
|
connclose(conn, "SMB: failed to send message");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
request_state(data, next_state);
|
2014-11-30 19:09:41 +03:00
|
|
|
|
|
|
|
return CURLE_OK;
|
2014-11-30 18:39:14 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_disconnect(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn, bool dead)
|
2014-11-30 18:39:14 +03:00
|
|
|
{
|
2014-11-30 19:09:41 +03:00
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
2014-11-30 21:29:23 +03:00
|
|
|
(void) dead;
|
2021-01-08 19:58:15 +03:00
|
|
|
(void) data;
|
2018-07-29 18:58:10 +03:00
|
|
|
Curl_safefree(smbc->share);
|
2014-11-30 19:09:41 +03:00
|
|
|
Curl_safefree(smbc->domain);
|
|
|
|
Curl_safefree(smbc->recv_buf);
|
2024-02-28 16:51:53 +03:00
|
|
|
Curl_safefree(smbc->send_buf);
|
2014-11-30 19:09:41 +03:00
|
|
|
return CURLE_OK;
|
2014-11-30 18:39:14 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static int smb_getsock(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn, curl_socket_t *socks)
|
2014-11-30 18:39:14 +03:00
|
|
|
{
|
2021-01-08 19:58:15 +03:00
|
|
|
(void)data;
|
2014-11-30 19:09:41 +03:00
|
|
|
socks[0] = conn->sock[FIRSTSOCKET];
|
2018-07-20 01:57:28 +03:00
|
|
|
return GETSOCK_READSOCK(0) | GETSOCK_WRITESOCK(0);
|
2014-11-30 18:39:14 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_do(struct Curl_easy *data, bool *done)
|
2018-07-29 18:58:10 +03:00
|
|
|
{
|
2021-01-08 19:58:15 +03:00
|
|
|
struct connectdata *conn = data->conn;
|
2018-07-29 18:58:10 +03:00
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
|
|
|
|
2018-08-01 14:06:21 +03:00
|
|
|
*done = FALSE;
|
2018-07-29 18:58:10 +03:00
|
|
|
if(smbc->share) {
|
2018-12-19 00:28:20 +03:00
|
|
|
return CURLE_OK;
|
2018-07-29 18:58:10 +03:00
|
|
|
}
|
|
|
|
return CURLE_URL_MALFORMAT;
|
|
|
|
}
|
|
|
|
|
2021-01-08 19:58:15 +03:00
|
|
|
static CURLcode smb_parse_url_path(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn)
|
2014-12-07 00:02:06 +03:00
|
|
|
{
|
2020-11-23 10:32:41 +03:00
|
|
|
struct smb_request *req = data->req.p.smb;
|
2018-07-29 18:58:10 +03:00
|
|
|
struct smb_conn *smbc = &conn->proto.smbc;
|
2014-12-07 00:02:06 +03:00
|
|
|
char *path;
|
|
|
|
char *slash;
|
|
|
|
|
|
|
|
/* URL decode the path */
|
2022-02-03 15:04:30 +03:00
|
|
|
CURLcode result = Curl_urldecode(data->state.up.path, 0, &path, NULL,
|
2020-06-23 17:13:50 +03:00
|
|
|
REJECT_CTRL);
|
2014-12-07 00:02:06 +03:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
/* Parse the path for the share */
|
2018-07-29 18:58:10 +03:00
|
|
|
smbc->share = strdup((*path == '/' || *path == '\\') ? path + 1 : path);
|
2018-07-12 12:44:57 +03:00
|
|
|
free(path);
|
2018-07-29 18:58:10 +03:00
|
|
|
if(!smbc->share)
|
2014-12-07 00:02:06 +03:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2018-07-29 18:58:10 +03:00
|
|
|
slash = strchr(smbc->share, '/');
|
2014-12-07 00:02:06 +03:00
|
|
|
if(!slash)
|
2018-07-29 18:58:10 +03:00
|
|
|
slash = strchr(smbc->share, '\\');
|
2014-12-07 00:02:06 +03:00
|
|
|
|
|
|
|
/* The share must be present */
|
|
|
|
if(!slash) {
|
2018-07-29 18:58:10 +03:00
|
|
|
Curl_safefree(smbc->share);
|
2023-02-12 15:24:08 +03:00
|
|
|
failf(data, "missing share in URL path for SMB");
|
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;
|
2018-12-19 00:28:20 +03:00
|
|
|
req->path = slash;
|
2018-07-29 18:58:10 +03:00
|
|
|
|
2014-12-07 00:02:06 +03:00
|
|
|
for(; *slash; slash++) {
|
|
|
|
if(*slash == '/')
|
|
|
|
*slash = '\\';
|
|
|
|
}
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2020-08-03 11:33:17 +03:00
|
|
|
#endif /* CURL_DISABLE_SMB && USE_CURL_NTLM_CORE &&
|
2021-03-08 14:30:56 +03:00
|
|
|
SIZEOF_CURL_OFF_T > 4 */
|