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 * SPDX-License-Identifier: curl
24 ***************************************************************************/
26 #include "curl_setup.h"
28 #if !defined(CURL_DISABLE_SMB) && defined(USE_CURL_NTLM_CORE) && \
29 (SIZEOF_CURL_OFF_T > 4)
31 #define BUILDING_CURL_SMB_C
35 #ifdef CURL_WINDOWS_APP
36 #define getpid GetCurrentProcessId
38 #define getpid _getpid
49 #include "vtls/vtls.h"
50 #include "curl_ntlm_core.h"
52 #include "curl_endian.h"
54 /* The last #include files should be: */
55 #include "curl_memory.h"
58 /* Local API functions */
59 static CURLcode smb_setup_connection(struct Curl_easy *data,
60 struct connectdata *conn);
61 static CURLcode smb_connect(struct Curl_easy *data, bool *done);
62 static CURLcode smb_connection_state(struct Curl_easy *data, bool *done);
63 static CURLcode smb_do(struct Curl_easy *data, bool *done);
64 static CURLcode smb_request_state(struct Curl_easy *data, bool *done);
65 static CURLcode smb_done(struct Curl_easy *data, CURLcode status,
67 static CURLcode smb_disconnect(struct Curl_easy *data,
68 struct connectdata *conn, bool dead);
69 static int smb_getsock(struct Curl_easy *data, struct connectdata *conn,
70 curl_socket_t *socks);
71 static CURLcode smb_parse_url_path(struct Curl_easy *data,
72 struct connectdata *conn);
75 * SMB handler interface
77 const struct Curl_handler Curl_handler_smb = {
79 smb_setup_connection, /* setup_connection */
82 ZERO_NULL, /* do_more */
83 smb_connect, /* connect_it */
84 smb_connection_state, /* connecting */
85 smb_request_state, /* doing */
86 smb_getsock, /* proto_getsock */
87 smb_getsock, /* doing_getsock */
88 ZERO_NULL, /* domore_getsock */
89 ZERO_NULL, /* perform_getsock */
90 smb_disconnect, /* disconnect */
91 ZERO_NULL, /* readwrite */
92 ZERO_NULL, /* connection_check */
93 ZERO_NULL, /* attach connection */
94 PORT_SMB, /* defport */
95 CURLPROTO_SMB, /* protocol */
96 CURLPROTO_SMB, /* family */
97 PROTOPT_NONE /* flags */
102 * SMBS handler interface
104 const struct Curl_handler Curl_handler_smbs = {
106 smb_setup_connection, /* setup_connection */
109 ZERO_NULL, /* do_more */
110 smb_connect, /* connect_it */
111 smb_connection_state, /* connecting */
112 smb_request_state, /* doing */
113 smb_getsock, /* proto_getsock */
114 smb_getsock, /* doing_getsock */
115 ZERO_NULL, /* domore_getsock */
116 ZERO_NULL, /* perform_getsock */
117 smb_disconnect, /* disconnect */
118 ZERO_NULL, /* readwrite */
119 ZERO_NULL, /* connection_check */
120 ZERO_NULL, /* attach connection */
121 PORT_SMBS, /* defport */
122 CURLPROTO_SMBS, /* protocol */
123 CURLPROTO_SMB, /* family */
124 PROTOPT_SSL /* flags */
128 #define MAX_PAYLOAD_SIZE 0x8000
129 #define MAX_MESSAGE_SIZE (MAX_PAYLOAD_SIZE + 0x1000)
130 #define CLIENTNAME "curl"
131 #define SERVICENAME "?????"
133 /* Append a string to an SMB message */
134 #define MSGCAT(str) \
140 /* Append a null-terminated string to an SMB message */
141 #define MSGCATNULL(str) \
144 p += strlen(str) + 1; \
147 /* SMB is mostly little endian */
148 #if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \
150 static unsigned short smb_swap16(unsigned short x)
152 return (unsigned short) ((x << 8) | ((x >> 8) & 0xff));
155 static unsigned int smb_swap32(unsigned int x)
157 return (x << 24) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) |
161 static curl_off_t smb_swap64(curl_off_t x)
163 return ((curl_off_t) smb_swap32((unsigned int) x) << 32) |
164 smb_swap32((unsigned int) (x >> 32));
168 # define smb_swap16(x) (x)
169 # define smb_swap32(x) (x)
170 # define smb_swap64(x) (x)
173 /* SMB request state */
185 /* SMB request data */
187 enum smb_req_state state;
189 unsigned short tid; /* Even if we connect to the same tree as another */
190 unsigned short fid; /* request, the tid will be different */
194 static void conn_state(struct Curl_easy *data, enum smb_conn_state newstate)
196 struct smb_conn *smbc = &data->conn->proto.smbc;
197 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
198 /* For debug purposes */
199 static const char * const names[] = {
208 if(smbc->state != newstate)
209 infof(data, "SMB conn %p state change from %s to %s",
210 (void *)smbc, names[smbc->state], names[newstate]);
213 smbc->state = newstate;
216 static void request_state(struct Curl_easy *data,
217 enum smb_req_state newstate)
219 struct smb_request *req = data->req.p.smb;
220 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
221 /* For debug purposes */
222 static const char * const names[] = {
229 "SMB_TREE_DISCONNECT",
234 if(req->state != newstate)
235 infof(data, "SMB request %p state change from %s to %s",
236 (void *)req, names[req->state], names[newstate]);
239 req->state = newstate;
242 /* this should setup things in the connection, not in the easy
244 static CURLcode smb_setup_connection(struct Curl_easy *data,
245 struct connectdata *conn)
247 struct smb_request *req;
249 /* Initialize the request state */
250 data->req.p.smb = req = calloc(1, sizeof(struct smb_request));
252 return CURLE_OUT_OF_MEMORY;
254 /* Parse the URL path */
255 return smb_parse_url_path(data, conn);
258 static CURLcode smb_connect(struct Curl_easy *data, bool *done)
260 struct connectdata *conn = data->conn;
261 struct smb_conn *smbc = &conn->proto.smbc;
266 /* Check we have a username and password to authenticate with */
267 if(!data->state.aptr.user)
268 return CURLE_LOGIN_DENIED;
270 /* Initialize the connection state */
271 smbc->state = SMB_CONNECTING;
272 smbc->recv_buf = malloc(MAX_MESSAGE_SIZE);
274 return CURLE_OUT_OF_MEMORY;
276 /* Multiple requests are allowed with this connection */
277 connkeep(conn, "SMB default");
279 /* Parse the username, domain, and password */
280 slash = strchr(conn->user, '/');
282 slash = strchr(conn->user, '\\');
285 smbc->user = slash + 1;
286 smbc->domain = strdup(conn->user);
288 return CURLE_OUT_OF_MEMORY;
289 smbc->domain[slash - conn->user] = 0;
292 smbc->user = conn->user;
293 smbc->domain = strdup(conn->host.name);
295 return CURLE_OUT_OF_MEMORY;
301 static CURLcode smb_recv_message(struct Curl_easy *data, void **msg)
303 struct connectdata *conn = data->conn;
304 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
305 struct smb_conn *smbc = &conn->proto.smbc;
306 char *buf = smbc->recv_buf;
310 size_t len = MAX_MESSAGE_SIZE - smbc->got;
313 result = Curl_read(data, sockfd, buf + smbc->got, len, &bytes_read);
320 smbc->got += bytes_read;
322 /* Check for a 32-bit nbt header */
323 if(smbc->got < sizeof(unsigned int))
326 nbt_size = Curl_read16_be((const unsigned char *)
327 (buf + sizeof(unsigned short))) +
328 sizeof(unsigned int);
329 if(smbc->got < nbt_size)
332 msg_size = sizeof(struct smb_header);
333 if(nbt_size >= msg_size + 1) {
334 /* Add the word count */
335 msg_size += 1 + ((unsigned char) buf[msg_size]) * sizeof(unsigned short);
336 if(nbt_size >= msg_size + sizeof(unsigned short)) {
337 /* Add the byte count */
338 msg_size += sizeof(unsigned short) +
339 Curl_read16_le((const unsigned char *)&buf[msg_size]);
340 if(nbt_size < msg_size)
341 return CURLE_READ_ERROR;
350 static void smb_pop_message(struct connectdata *conn)
352 struct smb_conn *smbc = &conn->proto.smbc;
357 static void smb_format_message(struct Curl_easy *data, struct smb_header *h,
358 unsigned char cmd, size_t len)
360 struct connectdata *conn = data->conn;
361 struct smb_conn *smbc = &conn->proto.smbc;
362 struct smb_request *req = data->req.p.smb;
365 memset(h, 0, sizeof(*h));
366 h->nbt_length = htons((unsigned short) (sizeof(*h) - sizeof(unsigned int) +
368 memcpy((char *)h->magic, "\xffSMB", 4);
370 h->flags = SMB_FLAGS_CANONICAL_PATHNAMES | SMB_FLAGS_CASELESS_PATHNAMES;
371 h->flags2 = smb_swap16(SMB_FLAGS2_IS_LONG_NAME | SMB_FLAGS2_KNOWS_LONG_NAME);
372 h->uid = smb_swap16(smbc->uid);
373 h->tid = smb_swap16(req->tid);
375 h->pid_high = smb_swap16((unsigned short)(pid >> 16));
376 h->pid = smb_swap16((unsigned short) pid);
379 static CURLcode smb_send(struct Curl_easy *data, ssize_t len,
382 struct connectdata *conn = data->conn;
383 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
384 struct smb_conn *smbc = &conn->proto.smbc;
385 ssize_t bytes_written;
388 result = Curl_write(data, sockfd, data->state.ulbuf,
389 len, &bytes_written);
393 if(bytes_written != len) {
394 smbc->send_size = len;
395 smbc->sent = bytes_written;
398 smbc->upload_size = upload_size;
403 static CURLcode smb_flush(struct Curl_easy *data)
405 struct connectdata *conn = data->conn;
406 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
407 struct smb_conn *smbc = &conn->proto.smbc;
408 ssize_t bytes_written;
409 ssize_t len = smbc->send_size - smbc->sent;
415 result = Curl_write(data, sockfd,
416 data->state.ulbuf + smbc->sent,
417 len, &bytes_written);
421 if(bytes_written != len)
422 smbc->sent += bytes_written;
429 static CURLcode smb_send_message(struct Curl_easy *data, unsigned char cmd,
430 const void *msg, size_t msg_len)
432 CURLcode result = Curl_get_upload_buffer(data);
435 smb_format_message(data, (struct smb_header *)data->state.ulbuf,
437 memcpy(data->state.ulbuf + sizeof(struct smb_header),
440 return smb_send(data, sizeof(struct smb_header) + msg_len, 0);
443 static CURLcode smb_send_negotiate(struct Curl_easy *data)
445 const char *msg = "\x00\x0c\x00\x02NT LM 0.12";
447 return smb_send_message(data, SMB_COM_NEGOTIATE, msg, 15);
450 static CURLcode smb_send_setup(struct Curl_easy *data)
452 struct connectdata *conn = data->conn;
453 struct smb_conn *smbc = &conn->proto.smbc;
454 struct smb_setup msg;
456 unsigned char lm_hash[21];
457 unsigned char lm[24];
458 unsigned char nt_hash[21];
459 unsigned char nt[24];
461 size_t byte_count = sizeof(lm) + sizeof(nt);
462 byte_count += strlen(smbc->user) + strlen(smbc->domain);
463 byte_count += strlen(OS) + strlen(CLIENTNAME) + 4; /* 4 null chars */
464 if(byte_count > sizeof(msg.bytes))
465 return CURLE_FILESIZE_EXCEEDED;
467 Curl_ntlm_core_mk_lm_hash(conn->passwd, lm_hash);
468 Curl_ntlm_core_lm_resp(lm_hash, smbc->challenge, lm);
469 Curl_ntlm_core_mk_nt_hash(conn->passwd, nt_hash);
470 Curl_ntlm_core_lm_resp(nt_hash, smbc->challenge, nt);
472 memset(&msg, 0, sizeof(msg));
473 msg.word_count = SMB_WC_SETUP_ANDX;
474 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
475 msg.max_buffer_size = smb_swap16(MAX_MESSAGE_SIZE);
476 msg.max_mpx_count = smb_swap16(1);
477 msg.vc_number = smb_swap16(1);
478 msg.session_key = smb_swap32(smbc->session_key);
479 msg.capabilities = smb_swap32(SMB_CAP_LARGE_FILES);
480 msg.lengths[0] = smb_swap16(sizeof(lm));
481 msg.lengths[1] = smb_swap16(sizeof(nt));
482 memcpy(p, lm, sizeof(lm));
484 memcpy(p, nt, sizeof(nt));
486 MSGCATNULL(smbc->user);
487 MSGCATNULL(smbc->domain);
489 MSGCATNULL(CLIENTNAME);
490 byte_count = p - msg.bytes;
491 msg.byte_count = smb_swap16((unsigned short)byte_count);
493 return smb_send_message(data, SMB_COM_SETUP_ANDX, &msg,
494 sizeof(msg) - sizeof(msg.bytes) + byte_count);
497 static CURLcode smb_send_tree_connect(struct Curl_easy *data)
499 struct smb_tree_connect msg;
500 struct connectdata *conn = data->conn;
501 struct smb_conn *smbc = &conn->proto.smbc;
504 size_t byte_count = strlen(conn->host.name) + strlen(smbc->share);
505 byte_count += strlen(SERVICENAME) + 5; /* 2 nulls and 3 backslashes */
506 if(byte_count > sizeof(msg.bytes))
507 return CURLE_FILESIZE_EXCEEDED;
509 memset(&msg, 0, sizeof(msg));
510 msg.word_count = SMB_WC_TREE_CONNECT_ANDX;
511 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
514 MSGCAT(conn->host.name);
516 MSGCATNULL(smbc->share);
517 MSGCATNULL(SERVICENAME); /* Match any type of service */
518 byte_count = p - msg.bytes;
519 msg.byte_count = smb_swap16((unsigned short)byte_count);
521 return smb_send_message(data, SMB_COM_TREE_CONNECT_ANDX, &msg,
522 sizeof(msg) - sizeof(msg.bytes) + byte_count);
525 static CURLcode smb_send_open(struct Curl_easy *data)
527 struct smb_request *req = data->req.p.smb;
528 struct smb_nt_create msg;
531 if((strlen(req->path) + 1) > sizeof(msg.bytes))
532 return CURLE_FILESIZE_EXCEEDED;
534 memset(&msg, 0, sizeof(msg));
535 msg.word_count = SMB_WC_NT_CREATE_ANDX;
536 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
537 byte_count = strlen(req->path);
538 msg.name_length = smb_swap16((unsigned short)byte_count);
539 msg.share_access = smb_swap32(SMB_FILE_SHARE_ALL);
540 if(data->set.upload) {
541 msg.access = smb_swap32(SMB_GENERIC_READ | SMB_GENERIC_WRITE);
542 msg.create_disposition = smb_swap32(SMB_FILE_OVERWRITE_IF);
545 msg.access = smb_swap32(SMB_GENERIC_READ);
546 msg.create_disposition = smb_swap32(SMB_FILE_OPEN);
548 msg.byte_count = smb_swap16((unsigned short) ++byte_count);
549 strcpy(msg.bytes, req->path);
551 return smb_send_message(data, SMB_COM_NT_CREATE_ANDX, &msg,
552 sizeof(msg) - sizeof(msg.bytes) + byte_count);
555 static CURLcode smb_send_close(struct Curl_easy *data)
557 struct smb_request *req = data->req.p.smb;
558 struct smb_close msg;
560 memset(&msg, 0, sizeof(msg));
561 msg.word_count = SMB_WC_CLOSE;
562 msg.fid = smb_swap16(req->fid);
564 return smb_send_message(data, SMB_COM_CLOSE, &msg, sizeof(msg));
567 static CURLcode smb_send_tree_disconnect(struct Curl_easy *data)
569 struct smb_tree_disconnect msg;
571 memset(&msg, 0, sizeof(msg));
573 return smb_send_message(data, SMB_COM_TREE_DISCONNECT, &msg, sizeof(msg));
576 static CURLcode smb_send_read(struct Curl_easy *data)
578 struct smb_request *req = data->req.p.smb;
579 curl_off_t offset = data->req.offset;
582 memset(&msg, 0, sizeof(msg));
583 msg.word_count = SMB_WC_READ_ANDX;
584 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
585 msg.fid = smb_swap16(req->fid);
586 msg.offset = smb_swap32((unsigned int) offset);
587 msg.offset_high = smb_swap32((unsigned int) (offset >> 32));
588 msg.min_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
589 msg.max_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
591 return smb_send_message(data, SMB_COM_READ_ANDX, &msg, sizeof(msg));
594 static CURLcode smb_send_write(struct Curl_easy *data)
596 struct smb_write *msg;
597 struct smb_request *req = data->req.p.smb;
598 curl_off_t offset = data->req.offset;
599 curl_off_t upload_size = data->req.size - data->req.bytecount;
600 CURLcode result = Curl_get_upload_buffer(data);
603 msg = (struct smb_write *)data->state.ulbuf;
605 if(upload_size >= MAX_PAYLOAD_SIZE - 1) /* There is one byte of padding */
606 upload_size = MAX_PAYLOAD_SIZE - 1;
608 memset(msg, 0, sizeof(*msg));
609 msg->word_count = SMB_WC_WRITE_ANDX;
610 msg->andx.command = SMB_COM_NO_ANDX_COMMAND;
611 msg->fid = smb_swap16(req->fid);
612 msg->offset = smb_swap32((unsigned int) offset);
613 msg->offset_high = smb_swap32((unsigned int) (offset >> 32));
614 msg->data_length = smb_swap16((unsigned short) upload_size);
615 msg->data_offset = smb_swap16(sizeof(*msg) - sizeof(unsigned int));
616 msg->byte_count = smb_swap16((unsigned short) (upload_size + 1));
618 smb_format_message(data, &msg->h, SMB_COM_WRITE_ANDX,
619 sizeof(*msg) - sizeof(msg->h) + (size_t) upload_size);
621 return smb_send(data, sizeof(*msg), (size_t) upload_size);
624 static CURLcode smb_send_and_recv(struct Curl_easy *data, void **msg)
626 struct connectdata *conn = data->conn;
627 struct smb_conn *smbc = &conn->proto.smbc;
629 *msg = NULL; /* if it returns early */
631 /* Check if there is data in the transfer buffer */
632 if(!smbc->send_size && smbc->upload_size) {
633 size_t nread = smbc->upload_size > (size_t)data->set.upload_buffer_size ?
634 (size_t)data->set.upload_buffer_size : smbc->upload_size;
635 data->req.upload_fromhere = data->state.ulbuf;
636 result = Curl_fillreadbuffer(data, nread, &nread);
637 if(result && result != CURLE_AGAIN)
642 smbc->upload_size -= nread;
643 smbc->send_size = nread;
647 /* Check if there is data to send */
648 if(smbc->send_size) {
649 result = smb_flush(data);
654 /* Check if there is still data to be sent */
655 if(smbc->send_size || smbc->upload_size)
658 return smb_recv_message(data, msg);
661 static CURLcode smb_connection_state(struct Curl_easy *data, bool *done)
663 struct connectdata *conn = data->conn;
664 struct smb_conn *smbc = &conn->proto.smbc;
665 struct smb_negotiate_response *nrsp;
666 struct smb_header *h;
670 if(smbc->state == SMB_CONNECTING) {
672 if((conn->handler->flags & PROTOPT_SSL)) {
673 bool ssl_done = FALSE;
674 result = Curl_ssl_connect_nonblocking(data, conn, FALSE,
675 FIRSTSOCKET, &ssl_done);
676 if(result && result != CURLE_AGAIN)
683 result = smb_send_negotiate(data);
685 connclose(conn, "SMB: failed to send negotiate message");
689 conn_state(data, SMB_NEGOTIATE);
692 /* Send the previous message and check for a response */
693 result = smb_send_and_recv(data, &msg);
694 if(result && result != CURLE_AGAIN) {
695 connclose(conn, "SMB: failed to communicate");
704 switch(smbc->state) {
706 if((smbc->got < sizeof(*nrsp) + sizeof(smbc->challenge) - 1) ||
708 connclose(conn, "SMB: negotiation failed");
709 return CURLE_COULDNT_CONNECT;
712 memcpy(smbc->challenge, nrsp->bytes, sizeof(smbc->challenge));
713 smbc->session_key = smb_swap32(nrsp->session_key);
714 result = smb_send_setup(data);
716 connclose(conn, "SMB: failed to send setup message");
719 conn_state(data, SMB_SETUP);
724 connclose(conn, "SMB: authentication failed");
725 return CURLE_LOGIN_DENIED;
727 smbc->uid = smb_swap16(h->uid);
728 conn_state(data, SMB_CONNECTED);
733 smb_pop_message(conn);
734 return CURLE_OK; /* ignore */
737 smb_pop_message(conn);
743 * Convert a timestamp from the Windows world (100 nsec units from 1 Jan 1601)
744 * to Posix time. Cap the output to fit within a time_t.
746 static void get_posix_time(time_t *out, curl_off_t timestamp)
748 timestamp -= 116444736000000000;
749 timestamp /= 10000000;
750 #if SIZEOF_TIME_T < SIZEOF_CURL_OFF_T
751 if(timestamp > TIME_T_MAX)
753 else if(timestamp < TIME_T_MIN)
757 *out = (time_t) timestamp;
760 static CURLcode smb_request_state(struct Curl_easy *data, bool *done)
762 struct connectdata *conn = data->conn;
763 struct smb_request *req = data->req.p.smb;
764 struct smb_header *h;
765 struct smb_conn *smbc = &conn->proto.smbc;
766 enum smb_req_state next_state = SMB_DONE;
771 const struct smb_nt_create_response *smb_m;
773 /* Start the request */
774 if(req->state == SMB_REQUESTING) {
775 result = smb_send_tree_connect(data);
777 connclose(conn, "SMB: failed to send tree connect message");
781 request_state(data, SMB_TREE_CONNECT);
784 /* Send the previous message and check for a response */
785 result = smb_send_and_recv(data, &msg);
786 if(result && result != CURLE_AGAIN) {
787 connclose(conn, "SMB: failed to communicate");
797 case SMB_TREE_CONNECT:
799 req->result = CURLE_REMOTE_FILE_NOT_FOUND;
800 if(h->status == smb_swap32(SMB_ERR_NOACCESS))
801 req->result = CURLE_REMOTE_ACCESS_DENIED;
804 req->tid = smb_swap16(h->tid);
805 next_state = SMB_OPEN;
809 if(h->status || smbc->got < sizeof(struct smb_nt_create_response)) {
810 req->result = CURLE_REMOTE_FILE_NOT_FOUND;
811 if(h->status == smb_swap32(SMB_ERR_NOACCESS))
812 req->result = CURLE_REMOTE_ACCESS_DENIED;
813 next_state = SMB_TREE_DISCONNECT;
816 smb_m = (const struct smb_nt_create_response*) msg;
817 req->fid = smb_swap16(smb_m->fid);
818 data->req.offset = 0;
819 if(data->set.upload) {
820 data->req.size = data->state.infilesize;
821 Curl_pgrsSetUploadSize(data, data->req.size);
822 next_state = SMB_UPLOAD;
825 smb_m = (const struct smb_nt_create_response*) msg;
826 data->req.size = smb_swap64(smb_m->end_of_file);
827 if(data->req.size < 0) {
828 req->result = CURLE_WEIRD_SERVER_REPLY;
829 next_state = SMB_CLOSE;
832 Curl_pgrsSetDownloadSize(data, data->req.size);
833 if(data->set.get_filetime)
834 get_posix_time(&data->info.filetime, smb_m->last_change_time);
835 next_state = SMB_DOWNLOAD;
841 if(h->status || smbc->got < sizeof(struct smb_header) + 14) {
842 req->result = CURLE_RECV_ERROR;
843 next_state = SMB_CLOSE;
846 len = Curl_read16_le(((const unsigned char *) msg) +
847 sizeof(struct smb_header) + 11);
848 off = Curl_read16_le(((const unsigned char *) msg) +
849 sizeof(struct smb_header) + 13);
851 if(off + sizeof(unsigned int) + len > smbc->got) {
852 failf(data, "Invalid input packet");
853 result = CURLE_RECV_ERROR;
856 result = Curl_client_write(data, CLIENTWRITE_BODY,
857 (char *)msg + off + sizeof(unsigned int),
860 req->result = result;
861 next_state = SMB_CLOSE;
865 data->req.bytecount += len;
866 data->req.offset += len;
867 Curl_pgrsSetDownloadCounter(data, data->req.bytecount);
868 next_state = (len < MAX_PAYLOAD_SIZE) ? SMB_CLOSE : SMB_DOWNLOAD;
872 if(h->status || smbc->got < sizeof(struct smb_header) + 6) {
873 req->result = CURLE_UPLOAD_FAILED;
874 next_state = SMB_CLOSE;
877 len = Curl_read16_le(((const unsigned char *) msg) +
878 sizeof(struct smb_header) + 5);
879 data->req.bytecount += len;
880 data->req.offset += len;
881 Curl_pgrsSetUploadCounter(data, data->req.bytecount);
882 if(data->req.bytecount >= data->req.size)
883 next_state = SMB_CLOSE;
885 next_state = SMB_UPLOAD;
889 /* We don't care if the close failed, proceed to tree disconnect anyway */
890 next_state = SMB_TREE_DISCONNECT;
893 case SMB_TREE_DISCONNECT:
894 next_state = SMB_DONE;
898 smb_pop_message(conn);
899 return CURLE_OK; /* ignore */
902 smb_pop_message(conn);
906 result = smb_send_open(data);
910 result = smb_send_read(data);
914 result = smb_send_write(data);
918 result = smb_send_close(data);
921 case SMB_TREE_DISCONNECT:
922 result = smb_send_tree_disconnect(data);
926 result = req->result;
935 connclose(conn, "SMB: failed to send message");
939 request_state(data, next_state);
944 static CURLcode smb_done(struct Curl_easy *data, CURLcode status,
948 Curl_safefree(data->req.p.smb);
952 static CURLcode smb_disconnect(struct Curl_easy *data,
953 struct connectdata *conn, bool dead)
955 struct smb_conn *smbc = &conn->proto.smbc;
958 Curl_safefree(smbc->share);
959 Curl_safefree(smbc->domain);
960 Curl_safefree(smbc->recv_buf);
964 static int smb_getsock(struct Curl_easy *data,
965 struct connectdata *conn, curl_socket_t *socks)
968 socks[0] = conn->sock[FIRSTSOCKET];
969 return GETSOCK_READSOCK(0) | GETSOCK_WRITESOCK(0);
972 static CURLcode smb_do(struct Curl_easy *data, bool *done)
974 struct connectdata *conn = data->conn;
975 struct smb_conn *smbc = &conn->proto.smbc;
981 return CURLE_URL_MALFORMAT;
984 static CURLcode smb_parse_url_path(struct Curl_easy *data,
985 struct connectdata *conn)
987 struct smb_request *req = data->req.p.smb;
988 struct smb_conn *smbc = &conn->proto.smbc;
992 /* URL decode the path */
993 CURLcode result = Curl_urldecode(data->state.up.path, 0, &path, NULL,
998 /* Parse the path for the share */
999 smbc->share = strdup((*path == '/' || *path == '\\') ? path + 1 : path);
1002 return CURLE_OUT_OF_MEMORY;
1004 slash = strchr(smbc->share, '/');
1006 slash = strchr(smbc->share, '\\');
1008 /* The share must be present */
1010 Curl_safefree(smbc->share);
1011 return CURLE_URL_MALFORMAT;
1014 /* Parse the path for the file path converting any forward slashes into
1019 for(; *slash; slash++) {
1026 #endif /* CURL_DISABLE_SMB && USE_CURL_NTLM_CORE &&
1027 SIZEOF_CURL_OFF_T > 4 */