1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2016 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
9 * Copyright (C) 2014, Bill Nagel <wnagel@tycoint.com>, Exacq Technologies
11 * This software is licensed as described in the file COPYING, which
12 * you should have received as part of this distribution. The terms
13 * are also available at https://curl.se/docs/copyright.html.
15 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
16 * copies of the Software, and permit persons to whom the Software is
17 * furnished to do so, under the terms of the COPYING file.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ***************************************************************************/
24 #include "curl_setup.h"
26 #if !defined(CURL_DISABLE_SMB) && defined(USE_CURL_NTLM_CORE) && \
27 (SIZEOF_CURL_OFF_T > 4)
29 #define BUILDING_CURL_SMB_C
33 #ifdef CURL_WINDOWS_APP
34 #define getpid GetCurrentProcessId
36 #define getpid _getpid
47 #include "vtls/vtls.h"
48 #include "curl_ntlm_core.h"
50 #include "curl_endian.h"
52 /* The last #include files should be: */
53 #include "curl_memory.h"
56 /* Local API functions */
57 static CURLcode smb_setup_connection(struct Curl_easy *data,
58 struct connectdata *conn);
59 static CURLcode smb_connect(struct Curl_easy *data, bool *done);
60 static CURLcode smb_connection_state(struct Curl_easy *data, bool *done);
61 static CURLcode smb_do(struct Curl_easy *data, bool *done);
62 static CURLcode smb_request_state(struct Curl_easy *data, bool *done);
63 static CURLcode smb_done(struct Curl_easy *data, CURLcode status,
65 static CURLcode smb_disconnect(struct Curl_easy *data,
66 struct connectdata *conn, bool dead);
67 static int smb_getsock(struct Curl_easy *data, struct connectdata *conn,
68 curl_socket_t *socks);
69 static CURLcode smb_parse_url_path(struct Curl_easy *data,
70 struct connectdata *conn);
73 * SMB handler interface
75 const struct Curl_handler Curl_handler_smb = {
77 smb_setup_connection, /* setup_connection */
80 ZERO_NULL, /* do_more */
81 smb_connect, /* connect_it */
82 smb_connection_state, /* connecting */
83 smb_request_state, /* doing */
84 smb_getsock, /* proto_getsock */
85 smb_getsock, /* doing_getsock */
86 ZERO_NULL, /* domore_getsock */
87 ZERO_NULL, /* perform_getsock */
88 smb_disconnect, /* disconnect */
89 ZERO_NULL, /* readwrite */
90 ZERO_NULL, /* connection_check */
91 ZERO_NULL, /* attach connection */
92 PORT_SMB, /* defport */
93 CURLPROTO_SMB, /* protocol */
94 CURLPROTO_SMB, /* family */
95 PROTOPT_NONE /* flags */
100 * SMBS handler interface
102 const struct Curl_handler Curl_handler_smbs = {
104 smb_setup_connection, /* setup_connection */
107 ZERO_NULL, /* do_more */
108 smb_connect, /* connect_it */
109 smb_connection_state, /* connecting */
110 smb_request_state, /* doing */
111 smb_getsock, /* proto_getsock */
112 smb_getsock, /* doing_getsock */
113 ZERO_NULL, /* domore_getsock */
114 ZERO_NULL, /* perform_getsock */
115 smb_disconnect, /* disconnect */
116 ZERO_NULL, /* readwrite */
117 ZERO_NULL, /* connection_check */
118 ZERO_NULL, /* attach connection */
119 PORT_SMBS, /* defport */
120 CURLPROTO_SMBS, /* protocol */
121 CURLPROTO_SMB, /* family */
122 PROTOPT_SSL /* flags */
126 #define MAX_PAYLOAD_SIZE 0x8000
127 #define MAX_MESSAGE_SIZE (MAX_PAYLOAD_SIZE + 0x1000)
128 #define CLIENTNAME "curl"
129 #define SERVICENAME "?????"
131 /* Append a string to an SMB message */
132 #define MSGCAT(str) \
138 /* Append a null-terminated string to an SMB message */
139 #define MSGCATNULL(str) \
142 p += strlen(str) + 1; \
145 /* SMB is mostly little endian */
146 #if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \
148 static unsigned short smb_swap16(unsigned short x)
150 return (unsigned short) ((x << 8) | ((x >> 8) & 0xff));
153 static unsigned int smb_swap32(unsigned int x)
155 return (x << 24) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) |
159 static curl_off_t smb_swap64(curl_off_t x)
161 return ((curl_off_t) smb_swap32((unsigned int) x) << 32) |
162 smb_swap32((unsigned int) (x >> 32));
166 # define smb_swap16(x) (x)
167 # define smb_swap32(x) (x)
168 # define smb_swap64(x) (x)
171 /* SMB request state */
183 /* SMB request data */
185 enum smb_req_state state;
187 unsigned short tid; /* Even if we connect to the same tree as another */
188 unsigned short fid; /* request, the tid will be different */
192 static void conn_state(struct Curl_easy *data, enum smb_conn_state newstate)
194 struct smb_conn *smbc = &data->conn->proto.smbc;
195 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
196 /* For debug purposes */
197 static const char * const names[] = {
206 if(smbc->state != newstate)
207 infof(data, "SMB conn %p state change from %s to %s",
208 (void *)smbc, names[smbc->state], names[newstate]);
211 smbc->state = newstate;
214 static void request_state(struct Curl_easy *data,
215 enum smb_req_state newstate)
217 struct smb_request *req = data->req.p.smb;
218 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
219 /* For debug purposes */
220 static const char * const names[] = {
227 "SMB_TREE_DISCONNECT",
232 if(req->state != newstate)
233 infof(data, "SMB request %p state change from %s to %s",
234 (void *)req, names[req->state], names[newstate]);
237 req->state = newstate;
240 /* this should setup things in the connection, not in the easy
242 static CURLcode smb_setup_connection(struct Curl_easy *data,
243 struct connectdata *conn)
245 struct smb_request *req;
247 /* Initialize the request state */
248 data->req.p.smb = req = calloc(1, sizeof(struct smb_request));
250 return CURLE_OUT_OF_MEMORY;
252 /* Parse the URL path */
253 return smb_parse_url_path(data, conn);
256 static CURLcode smb_connect(struct Curl_easy *data, bool *done)
258 struct connectdata *conn = data->conn;
259 struct smb_conn *smbc = &conn->proto.smbc;
264 /* Check we have a username and password to authenticate with */
265 if(!data->state.aptr.user)
266 return CURLE_LOGIN_DENIED;
268 /* Initialize the connection state */
269 smbc->state = SMB_CONNECTING;
270 smbc->recv_buf = malloc(MAX_MESSAGE_SIZE);
272 return CURLE_OUT_OF_MEMORY;
274 /* Multiple requests are allowed with this connection */
275 connkeep(conn, "SMB default");
277 /* Parse the username, domain, and password */
278 slash = strchr(conn->user, '/');
280 slash = strchr(conn->user, '\\');
283 smbc->user = slash + 1;
284 smbc->domain = strdup(conn->user);
286 return CURLE_OUT_OF_MEMORY;
287 smbc->domain[slash - conn->user] = 0;
290 smbc->user = conn->user;
291 smbc->domain = strdup(conn->host.name);
293 return CURLE_OUT_OF_MEMORY;
299 static CURLcode smb_recv_message(struct Curl_easy *data, void **msg)
301 struct connectdata *conn = data->conn;
302 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
303 struct smb_conn *smbc = &conn->proto.smbc;
304 char *buf = smbc->recv_buf;
308 size_t len = MAX_MESSAGE_SIZE - smbc->got;
311 result = Curl_read(data, sockfd, buf + smbc->got, len, &bytes_read);
318 smbc->got += bytes_read;
320 /* Check for a 32-bit nbt header */
321 if(smbc->got < sizeof(unsigned int))
324 nbt_size = Curl_read16_be((const unsigned char *)
325 (buf + sizeof(unsigned short))) +
326 sizeof(unsigned int);
327 if(smbc->got < nbt_size)
330 msg_size = sizeof(struct smb_header);
331 if(nbt_size >= msg_size + 1) {
332 /* Add the word count */
333 msg_size += 1 + ((unsigned char) buf[msg_size]) * sizeof(unsigned short);
334 if(nbt_size >= msg_size + sizeof(unsigned short)) {
335 /* Add the byte count */
336 msg_size += sizeof(unsigned short) +
337 Curl_read16_le((const unsigned char *)&buf[msg_size]);
338 if(nbt_size < msg_size)
339 return CURLE_READ_ERROR;
348 static void smb_pop_message(struct connectdata *conn)
350 struct smb_conn *smbc = &conn->proto.smbc;
355 static void smb_format_message(struct Curl_easy *data, struct smb_header *h,
356 unsigned char cmd, size_t len)
358 struct connectdata *conn = data->conn;
359 struct smb_conn *smbc = &conn->proto.smbc;
360 struct smb_request *req = data->req.p.smb;
363 memset(h, 0, sizeof(*h));
364 h->nbt_length = htons((unsigned short) (sizeof(*h) - sizeof(unsigned int) +
366 memcpy((char *)h->magic, "\xffSMB", 4);
368 h->flags = SMB_FLAGS_CANONICAL_PATHNAMES | SMB_FLAGS_CASELESS_PATHNAMES;
369 h->flags2 = smb_swap16(SMB_FLAGS2_IS_LONG_NAME | SMB_FLAGS2_KNOWS_LONG_NAME);
370 h->uid = smb_swap16(smbc->uid);
371 h->tid = smb_swap16(req->tid);
373 h->pid_high = smb_swap16((unsigned short)(pid >> 16));
374 h->pid = smb_swap16((unsigned short) pid);
377 static CURLcode smb_send(struct Curl_easy *data, ssize_t len,
380 struct connectdata *conn = data->conn;
381 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
382 struct smb_conn *smbc = &conn->proto.smbc;
383 ssize_t bytes_written;
386 result = Curl_write(data, sockfd, data->state.ulbuf,
387 len, &bytes_written);
391 if(bytes_written != len) {
392 smbc->send_size = len;
393 smbc->sent = bytes_written;
396 smbc->upload_size = upload_size;
401 static CURLcode smb_flush(struct Curl_easy *data)
403 struct connectdata *conn = data->conn;
404 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
405 struct smb_conn *smbc = &conn->proto.smbc;
406 ssize_t bytes_written;
407 ssize_t len = smbc->send_size - smbc->sent;
413 result = Curl_write(data, sockfd,
414 data->state.ulbuf + smbc->sent,
415 len, &bytes_written);
419 if(bytes_written != len)
420 smbc->sent += bytes_written;
427 static CURLcode smb_send_message(struct Curl_easy *data, unsigned char cmd,
428 const void *msg, size_t msg_len)
430 CURLcode result = Curl_get_upload_buffer(data);
433 smb_format_message(data, (struct smb_header *)data->state.ulbuf,
435 memcpy(data->state.ulbuf + sizeof(struct smb_header),
438 return smb_send(data, sizeof(struct smb_header) + msg_len, 0);
441 static CURLcode smb_send_negotiate(struct Curl_easy *data)
443 const char *msg = "\x00\x0c\x00\x02NT LM 0.12";
445 return smb_send_message(data, SMB_COM_NEGOTIATE, msg, 15);
448 static CURLcode smb_send_setup(struct Curl_easy *data)
450 struct connectdata *conn = data->conn;
451 struct smb_conn *smbc = &conn->proto.smbc;
452 struct smb_setup msg;
454 unsigned char lm_hash[21];
455 unsigned char lm[24];
456 unsigned char nt_hash[21];
457 unsigned char nt[24];
459 size_t byte_count = sizeof(lm) + sizeof(nt);
460 byte_count += strlen(smbc->user) + strlen(smbc->domain);
461 byte_count += strlen(OS) + strlen(CLIENTNAME) + 4; /* 4 null chars */
462 if(byte_count > sizeof(msg.bytes))
463 return CURLE_FILESIZE_EXCEEDED;
465 Curl_ntlm_core_mk_lm_hash(conn->passwd, lm_hash);
466 Curl_ntlm_core_lm_resp(lm_hash, smbc->challenge, lm);
467 Curl_ntlm_core_mk_nt_hash(conn->passwd, nt_hash);
468 Curl_ntlm_core_lm_resp(nt_hash, smbc->challenge, nt);
470 memset(&msg, 0, sizeof(msg));
471 msg.word_count = SMB_WC_SETUP_ANDX;
472 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
473 msg.max_buffer_size = smb_swap16(MAX_MESSAGE_SIZE);
474 msg.max_mpx_count = smb_swap16(1);
475 msg.vc_number = smb_swap16(1);
476 msg.session_key = smb_swap32(smbc->session_key);
477 msg.capabilities = smb_swap32(SMB_CAP_LARGE_FILES);
478 msg.lengths[0] = smb_swap16(sizeof(lm));
479 msg.lengths[1] = smb_swap16(sizeof(nt));
480 memcpy(p, lm, sizeof(lm));
482 memcpy(p, nt, sizeof(nt));
484 MSGCATNULL(smbc->user);
485 MSGCATNULL(smbc->domain);
487 MSGCATNULL(CLIENTNAME);
488 byte_count = p - msg.bytes;
489 msg.byte_count = smb_swap16((unsigned short)byte_count);
491 return smb_send_message(data, SMB_COM_SETUP_ANDX, &msg,
492 sizeof(msg) - sizeof(msg.bytes) + byte_count);
495 static CURLcode smb_send_tree_connect(struct Curl_easy *data)
497 struct smb_tree_connect msg;
498 struct connectdata *conn = data->conn;
499 struct smb_conn *smbc = &conn->proto.smbc;
502 size_t byte_count = strlen(conn->host.name) + strlen(smbc->share);
503 byte_count += strlen(SERVICENAME) + 5; /* 2 nulls and 3 backslashes */
504 if(byte_count > sizeof(msg.bytes))
505 return CURLE_FILESIZE_EXCEEDED;
507 memset(&msg, 0, sizeof(msg));
508 msg.word_count = SMB_WC_TREE_CONNECT_ANDX;
509 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
512 MSGCAT(conn->host.name);
514 MSGCATNULL(smbc->share);
515 MSGCATNULL(SERVICENAME); /* Match any type of service */
516 byte_count = p - msg.bytes;
517 msg.byte_count = smb_swap16((unsigned short)byte_count);
519 return smb_send_message(data, SMB_COM_TREE_CONNECT_ANDX, &msg,
520 sizeof(msg) - sizeof(msg.bytes) + byte_count);
523 static CURLcode smb_send_open(struct Curl_easy *data)
525 struct smb_request *req = data->req.p.smb;
526 struct smb_nt_create msg;
529 if((strlen(req->path) + 1) > sizeof(msg.bytes))
530 return CURLE_FILESIZE_EXCEEDED;
532 memset(&msg, 0, sizeof(msg));
533 msg.word_count = SMB_WC_NT_CREATE_ANDX;
534 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
535 byte_count = strlen(req->path);
536 msg.name_length = smb_swap16((unsigned short)byte_count);
537 msg.share_access = smb_swap32(SMB_FILE_SHARE_ALL);
538 if(data->set.upload) {
539 msg.access = smb_swap32(SMB_GENERIC_READ | SMB_GENERIC_WRITE);
540 msg.create_disposition = smb_swap32(SMB_FILE_OVERWRITE_IF);
543 msg.access = smb_swap32(SMB_GENERIC_READ);
544 msg.create_disposition = smb_swap32(SMB_FILE_OPEN);
546 msg.byte_count = smb_swap16((unsigned short) ++byte_count);
547 strcpy(msg.bytes, req->path);
549 return smb_send_message(data, SMB_COM_NT_CREATE_ANDX, &msg,
550 sizeof(msg) - sizeof(msg.bytes) + byte_count);
553 static CURLcode smb_send_close(struct Curl_easy *data)
555 struct smb_request *req = data->req.p.smb;
556 struct smb_close msg;
558 memset(&msg, 0, sizeof(msg));
559 msg.word_count = SMB_WC_CLOSE;
560 msg.fid = smb_swap16(req->fid);
562 return smb_send_message(data, SMB_COM_CLOSE, &msg, sizeof(msg));
565 static CURLcode smb_send_tree_disconnect(struct Curl_easy *data)
567 struct smb_tree_disconnect msg;
569 memset(&msg, 0, sizeof(msg));
571 return smb_send_message(data, SMB_COM_TREE_DISCONNECT, &msg, sizeof(msg));
574 static CURLcode smb_send_read(struct Curl_easy *data)
576 struct smb_request *req = data->req.p.smb;
577 curl_off_t offset = data->req.offset;
580 memset(&msg, 0, sizeof(msg));
581 msg.word_count = SMB_WC_READ_ANDX;
582 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
583 msg.fid = smb_swap16(req->fid);
584 msg.offset = smb_swap32((unsigned int) offset);
585 msg.offset_high = smb_swap32((unsigned int) (offset >> 32));
586 msg.min_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
587 msg.max_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
589 return smb_send_message(data, SMB_COM_READ_ANDX, &msg, sizeof(msg));
592 static CURLcode smb_send_write(struct Curl_easy *data)
594 struct smb_write *msg;
595 struct smb_request *req = data->req.p.smb;
596 curl_off_t offset = data->req.offset;
597 curl_off_t upload_size = data->req.size - data->req.bytecount;
598 CURLcode result = Curl_get_upload_buffer(data);
601 msg = (struct smb_write *)data->state.ulbuf;
603 if(upload_size >= MAX_PAYLOAD_SIZE - 1) /* There is one byte of padding */
604 upload_size = MAX_PAYLOAD_SIZE - 1;
606 memset(msg, 0, sizeof(*msg));
607 msg->word_count = SMB_WC_WRITE_ANDX;
608 msg->andx.command = SMB_COM_NO_ANDX_COMMAND;
609 msg->fid = smb_swap16(req->fid);
610 msg->offset = smb_swap32((unsigned int) offset);
611 msg->offset_high = smb_swap32((unsigned int) (offset >> 32));
612 msg->data_length = smb_swap16((unsigned short) upload_size);
613 msg->data_offset = smb_swap16(sizeof(*msg) - sizeof(unsigned int));
614 msg->byte_count = smb_swap16((unsigned short) (upload_size + 1));
616 smb_format_message(data, &msg->h, SMB_COM_WRITE_ANDX,
617 sizeof(*msg) - sizeof(msg->h) + (size_t) upload_size);
619 return smb_send(data, sizeof(*msg), (size_t) upload_size);
622 static CURLcode smb_send_and_recv(struct Curl_easy *data, void **msg)
624 struct connectdata *conn = data->conn;
625 struct smb_conn *smbc = &conn->proto.smbc;
627 *msg = NULL; /* if it returns early */
629 /* Check if there is data in the transfer buffer */
630 if(!smbc->send_size && smbc->upload_size) {
631 size_t nread = smbc->upload_size > (size_t)data->set.upload_buffer_size ?
632 (size_t)data->set.upload_buffer_size : smbc->upload_size;
633 data->req.upload_fromhere = data->state.ulbuf;
634 result = Curl_fillreadbuffer(data, nread, &nread);
635 if(result && result != CURLE_AGAIN)
640 smbc->upload_size -= nread;
641 smbc->send_size = nread;
645 /* Check if there is data to send */
646 if(smbc->send_size) {
647 result = smb_flush(data);
652 /* Check if there is still data to be sent */
653 if(smbc->send_size || smbc->upload_size)
656 return smb_recv_message(data, msg);
659 static CURLcode smb_connection_state(struct Curl_easy *data, bool *done)
661 struct connectdata *conn = data->conn;
662 struct smb_conn *smbc = &conn->proto.smbc;
663 struct smb_negotiate_response *nrsp;
664 struct smb_header *h;
668 if(smbc->state == SMB_CONNECTING) {
670 if((conn->handler->flags & PROTOPT_SSL)) {
671 bool ssl_done = FALSE;
672 result = Curl_ssl_connect_nonblocking(data, conn, FALSE,
673 FIRSTSOCKET, &ssl_done);
674 if(result && result != CURLE_AGAIN)
681 result = smb_send_negotiate(data);
683 connclose(conn, "SMB: failed to send negotiate message");
687 conn_state(data, SMB_NEGOTIATE);
690 /* Send the previous message and check for a response */
691 result = smb_send_and_recv(data, &msg);
692 if(result && result != CURLE_AGAIN) {
693 connclose(conn, "SMB: failed to communicate");
702 switch(smbc->state) {
704 if((smbc->got < sizeof(*nrsp) + sizeof(smbc->challenge) - 1) ||
706 connclose(conn, "SMB: negotiation failed");
707 return CURLE_COULDNT_CONNECT;
710 memcpy(smbc->challenge, nrsp->bytes, sizeof(smbc->challenge));
711 smbc->session_key = smb_swap32(nrsp->session_key);
712 result = smb_send_setup(data);
714 connclose(conn, "SMB: failed to send setup message");
717 conn_state(data, SMB_SETUP);
722 connclose(conn, "SMB: authentication failed");
723 return CURLE_LOGIN_DENIED;
725 smbc->uid = smb_swap16(h->uid);
726 conn_state(data, SMB_CONNECTED);
731 smb_pop_message(conn);
732 return CURLE_OK; /* ignore */
735 smb_pop_message(conn);
741 * Convert a timestamp from the Windows world (100 nsec units from 1 Jan 1601)
742 * to Posix time. Cap the output to fit within a time_t.
744 static void get_posix_time(time_t *out, curl_off_t timestamp)
746 timestamp -= 116444736000000000;
747 timestamp /= 10000000;
748 #if SIZEOF_TIME_T < SIZEOF_CURL_OFF_T
749 if(timestamp > TIME_T_MAX)
751 else if(timestamp < TIME_T_MIN)
755 *out = (time_t) timestamp;
758 static CURLcode smb_request_state(struct Curl_easy *data, bool *done)
760 struct connectdata *conn = data->conn;
761 struct smb_request *req = data->req.p.smb;
762 struct smb_header *h;
763 struct smb_conn *smbc = &conn->proto.smbc;
764 enum smb_req_state next_state = SMB_DONE;
769 const struct smb_nt_create_response *smb_m;
771 /* Start the request */
772 if(req->state == SMB_REQUESTING) {
773 result = smb_send_tree_connect(data);
775 connclose(conn, "SMB: failed to send tree connect message");
779 request_state(data, SMB_TREE_CONNECT);
782 /* Send the previous message and check for a response */
783 result = smb_send_and_recv(data, &msg);
784 if(result && result != CURLE_AGAIN) {
785 connclose(conn, "SMB: failed to communicate");
795 case SMB_TREE_CONNECT:
797 req->result = CURLE_REMOTE_FILE_NOT_FOUND;
798 if(h->status == smb_swap32(SMB_ERR_NOACCESS))
799 req->result = CURLE_REMOTE_ACCESS_DENIED;
802 req->tid = smb_swap16(h->tid);
803 next_state = SMB_OPEN;
807 if(h->status || smbc->got < sizeof(struct smb_nt_create_response)) {
808 req->result = CURLE_REMOTE_FILE_NOT_FOUND;
809 if(h->status == smb_swap32(SMB_ERR_NOACCESS))
810 req->result = CURLE_REMOTE_ACCESS_DENIED;
811 next_state = SMB_TREE_DISCONNECT;
814 smb_m = (const struct smb_nt_create_response*) msg;
815 req->fid = smb_swap16(smb_m->fid);
816 data->req.offset = 0;
817 if(data->set.upload) {
818 data->req.size = data->state.infilesize;
819 Curl_pgrsSetUploadSize(data, data->req.size);
820 next_state = SMB_UPLOAD;
823 smb_m = (const struct smb_nt_create_response*) msg;
824 data->req.size = smb_swap64(smb_m->end_of_file);
825 if(data->req.size < 0) {
826 req->result = CURLE_WEIRD_SERVER_REPLY;
827 next_state = SMB_CLOSE;
830 Curl_pgrsSetDownloadSize(data, data->req.size);
831 if(data->set.get_filetime)
832 get_posix_time(&data->info.filetime, smb_m->last_change_time);
833 next_state = SMB_DOWNLOAD;
839 if(h->status || smbc->got < sizeof(struct smb_header) + 14) {
840 req->result = CURLE_RECV_ERROR;
841 next_state = SMB_CLOSE;
844 len = Curl_read16_le(((const unsigned char *) msg) +
845 sizeof(struct smb_header) + 11);
846 off = Curl_read16_le(((const unsigned char *) msg) +
847 sizeof(struct smb_header) + 13);
849 if(off + sizeof(unsigned int) + len > smbc->got) {
850 failf(data, "Invalid input packet");
851 result = CURLE_RECV_ERROR;
854 result = Curl_client_write(data, CLIENTWRITE_BODY,
855 (char *)msg + off + sizeof(unsigned int),
858 req->result = result;
859 next_state = SMB_CLOSE;
863 data->req.bytecount += len;
864 data->req.offset += len;
865 Curl_pgrsSetDownloadCounter(data, data->req.bytecount);
866 next_state = (len < MAX_PAYLOAD_SIZE) ? SMB_CLOSE : SMB_DOWNLOAD;
870 if(h->status || smbc->got < sizeof(struct smb_header) + 6) {
871 req->result = CURLE_UPLOAD_FAILED;
872 next_state = SMB_CLOSE;
875 len = Curl_read16_le(((const unsigned char *) msg) +
876 sizeof(struct smb_header) + 5);
877 data->req.bytecount += len;
878 data->req.offset += len;
879 Curl_pgrsSetUploadCounter(data, data->req.bytecount);
880 if(data->req.bytecount >= data->req.size)
881 next_state = SMB_CLOSE;
883 next_state = SMB_UPLOAD;
887 /* We don't care if the close failed, proceed to tree disconnect anyway */
888 next_state = SMB_TREE_DISCONNECT;
891 case SMB_TREE_DISCONNECT:
892 next_state = SMB_DONE;
896 smb_pop_message(conn);
897 return CURLE_OK; /* ignore */
900 smb_pop_message(conn);
904 result = smb_send_open(data);
908 result = smb_send_read(data);
912 result = smb_send_write(data);
916 result = smb_send_close(data);
919 case SMB_TREE_DISCONNECT:
920 result = smb_send_tree_disconnect(data);
924 result = req->result;
933 connclose(conn, "SMB: failed to send message");
937 request_state(data, next_state);
942 static CURLcode smb_done(struct Curl_easy *data, CURLcode status,
946 Curl_safefree(data->req.p.smb);
950 static CURLcode smb_disconnect(struct Curl_easy *data,
951 struct connectdata *conn, bool dead)
953 struct smb_conn *smbc = &conn->proto.smbc;
956 Curl_safefree(smbc->share);
957 Curl_safefree(smbc->domain);
958 Curl_safefree(smbc->recv_buf);
962 static int smb_getsock(struct Curl_easy *data,
963 struct connectdata *conn, curl_socket_t *socks)
966 socks[0] = conn->sock[FIRSTSOCKET];
967 return GETSOCK_READSOCK(0) | GETSOCK_WRITESOCK(0);
970 static CURLcode smb_do(struct Curl_easy *data, bool *done)
972 struct connectdata *conn = data->conn;
973 struct smb_conn *smbc = &conn->proto.smbc;
979 return CURLE_URL_MALFORMAT;
982 static CURLcode smb_parse_url_path(struct Curl_easy *data,
983 struct connectdata *conn)
985 struct smb_request *req = data->req.p.smb;
986 struct smb_conn *smbc = &conn->proto.smbc;
990 /* URL decode the path */
991 CURLcode result = Curl_urldecode(data->state.up.path, 0, &path, NULL,
996 /* Parse the path for the share */
997 smbc->share = strdup((*path == '/' || *path == '\\') ? path + 1 : path);
1000 return CURLE_OUT_OF_MEMORY;
1002 slash = strchr(smbc->share, '/');
1004 slash = strchr(smbc->share, '\\');
1006 /* The share must be present */
1008 Curl_safefree(smbc->share);
1009 return CURLE_URL_MALFORMAT;
1012 /* Parse the path for the file path converting any forward slashes into
1017 for(; *slash; slash++) {
1024 #endif /* CURL_DISABLE_SMB && USE_CURL_NTLM_CORE &&
1025 SIZEOF_CURL_OFF_T > 4 */