1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2014, Bill Nagel <wnagel@tycoint.com>, Exacq Technologies
9 * Copyright (C) 2016-2018, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.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_NTLM) && \
27 (CURL_SIZEOF_CURL_OFF_T > 4)
29 #if !defined(USE_WINDOWS_SSPI) || defined(USE_WIN32_CRYPTO)
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 connectdata *conn);
60 static CURLcode smb_connect(struct connectdata *conn, bool *done);
61 static CURLcode smb_connection_state(struct connectdata *conn, bool *done);
62 static CURLcode smb_request_state(struct connectdata *conn, bool *done);
63 static CURLcode smb_done(struct connectdata *conn, CURLcode status,
65 static CURLcode smb_disconnect(struct connectdata *conn, bool dead);
66 static int smb_getsock(struct connectdata *conn, curl_socket_t *socks,
68 static CURLcode smb_parse_url_path(struct connectdata *conn);
71 * SMB handler interface
73 const struct Curl_handler Curl_handler_smb = {
75 smb_setup_connection, /* setup_connection */
76 ZERO_NULL, /* do_it */
78 ZERO_NULL, /* do_more */
79 smb_connect, /* connect_it */
80 smb_connection_state, /* connecting */
81 smb_request_state, /* doing */
82 smb_getsock, /* proto_getsock */
83 smb_getsock, /* doing_getsock */
84 ZERO_NULL, /* domore_getsock */
85 ZERO_NULL, /* perform_getsock */
86 smb_disconnect, /* disconnect */
87 ZERO_NULL, /* readwrite */
88 ZERO_NULL, /* connection_check */
89 PORT_SMB, /* defport */
90 CURLPROTO_SMB, /* protocol */
91 PROTOPT_NONE /* flags */
96 * SMBS handler interface
98 const struct Curl_handler Curl_handler_smbs = {
100 smb_setup_connection, /* setup_connection */
101 ZERO_NULL, /* do_it */
103 ZERO_NULL, /* do_more */
104 smb_connect, /* connect_it */
105 smb_connection_state, /* connecting */
106 smb_request_state, /* doing */
107 smb_getsock, /* proto_getsock */
108 smb_getsock, /* doing_getsock */
109 ZERO_NULL, /* domore_getsock */
110 ZERO_NULL, /* perform_getsock */
111 smb_disconnect, /* disconnect */
112 ZERO_NULL, /* readwrite */
113 ZERO_NULL, /* connection_check */
114 PORT_SMBS, /* defport */
115 CURLPROTO_SMBS, /* protocol */
116 PROTOPT_SSL /* flags */
120 #define MAX_PAYLOAD_SIZE 0x8000
121 #define MAX_MESSAGE_SIZE (MAX_PAYLOAD_SIZE + 0x1000)
122 #define CLIENTNAME "curl"
123 #define SERVICENAME "?????"
125 /* Append a string to an SMB message */
126 #define MSGCAT(str) \
130 /* Append a null-terminated string to an SMB message */
131 #define MSGCATNULL(str) \
133 p += strlen(str) + 1;
135 /* SMB is mostly little endian */
136 #if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \
138 static unsigned short smb_swap16(unsigned short x)
140 return (unsigned short) ((x << 8) | ((x >> 8) & 0xff));
143 static unsigned int smb_swap32(unsigned int x)
145 return (x << 24) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) |
149 static curl_off_t smb_swap64(curl_off_t x)
151 return ((curl_off_t) smb_swap32((unsigned int) x) << 32) |
152 smb_swap32((unsigned int) (x >> 32));
156 # define smb_swap16(x) (x)
157 # define smb_swap32(x) (x)
158 # define smb_swap64(x) (x)
161 /* SMB request state */
173 /* SMB request data */
175 enum smb_req_state state;
178 unsigned short tid; /* Even if we connect to the same tree as another */
179 unsigned short fid; /* request, the tid will be different */
183 static void conn_state(struct connectdata *conn, enum smb_conn_state newstate)
185 struct smb_conn *smb = &conn->proto.smbc;
186 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
187 /* For debug purposes */
188 static const char * const names[] = {
197 if(smb->state != newstate)
198 infof(conn->data, "SMB conn %p state change from %s to %s\n",
199 (void *)smb, names[smb->state], names[newstate]);
202 smb->state = newstate;
205 static void request_state(struct connectdata *conn,
206 enum smb_req_state newstate)
208 struct smb_request *req = conn->data->req.protop;
209 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
210 /* For debug purposes */
211 static const char * const names[] = {
218 "SMB_TREE_DISCONNECT",
223 if(req->state != newstate)
224 infof(conn->data, "SMB request %p state change from %s to %s\n",
225 (void *)req, names[req->state], names[newstate]);
228 req->state = newstate;
231 static CURLcode smb_setup_connection(struct connectdata *conn)
233 struct smb_request *req;
235 /* Initialize the request state */
236 conn->data->req.protop = req = calloc(1, sizeof(struct smb_request));
238 return CURLE_OUT_OF_MEMORY;
240 /* Parse the URL path */
241 return smb_parse_url_path(conn);
244 static CURLcode smb_connect(struct connectdata *conn, bool *done)
246 struct smb_conn *smbc = &conn->proto.smbc;
251 /* Check we have a username and password to authenticate with */
252 if(!conn->bits.user_passwd)
253 return CURLE_LOGIN_DENIED;
255 /* Initialize the connection state */
256 memset(smbc, 0, sizeof(*smbc));
257 smbc->state = SMB_CONNECTING;
258 smbc->recv_buf = malloc(MAX_MESSAGE_SIZE);
260 return CURLE_OUT_OF_MEMORY;
262 /* Multiple requests are allowed with this connection */
263 connkeep(conn, "SMB default");
265 /* Parse the username, domain, and password */
266 slash = strchr(conn->user, '/');
268 slash = strchr(conn->user, '\\');
271 smbc->user = slash + 1;
272 smbc->domain = strdup(conn->user);
274 return CURLE_OUT_OF_MEMORY;
275 smbc->domain[slash - conn->user] = 0;
278 smbc->user = conn->user;
279 smbc->domain = strdup(conn->host.name);
281 return CURLE_OUT_OF_MEMORY;
287 static CURLcode smb_recv_message(struct connectdata *conn, void **msg)
289 struct smb_conn *smbc = &conn->proto.smbc;
290 char *buf = smbc->recv_buf;
294 size_t len = MAX_MESSAGE_SIZE - smbc->got;
297 result = Curl_read(conn, FIRSTSOCKET, buf + smbc->got, len, &bytes_read);
304 smbc->got += bytes_read;
306 /* Check for a 32-bit nbt header */
307 if(smbc->got < sizeof(unsigned int))
310 nbt_size = Curl_read16_be((const unsigned char *)
311 (buf + sizeof(unsigned short))) +
312 sizeof(unsigned int);
313 if(smbc->got < nbt_size)
316 msg_size = sizeof(struct smb_header);
317 if(nbt_size >= msg_size + 1) {
318 /* Add the word count */
319 msg_size += 1 + ((unsigned char) buf[msg_size]) * sizeof(unsigned short);
320 if(nbt_size >= msg_size + sizeof(unsigned short)) {
321 /* Add the byte count */
322 msg_size += sizeof(unsigned short) +
323 Curl_read16_le((const unsigned char *)&buf[msg_size]);
324 if(nbt_size < msg_size)
325 return CURLE_READ_ERROR;
334 static void smb_pop_message(struct connectdata *conn)
336 struct smb_conn *smbc = &conn->proto.smbc;
341 static void smb_format_message(struct connectdata *conn, struct smb_header *h,
342 unsigned char cmd, size_t len)
344 struct smb_conn *smbc = &conn->proto.smbc;
345 struct smb_request *req = conn->data->req.protop;
348 memset(h, 0, sizeof(*h));
349 h->nbt_length = htons((unsigned short) (sizeof(*h) - sizeof(unsigned int) +
351 memcpy((char *)h->magic, "\xffSMB", 4);
353 h->flags = SMB_FLAGS_CANONICAL_PATHNAMES | SMB_FLAGS_CASELESS_PATHNAMES;
354 h->flags2 = smb_swap16(SMB_FLAGS2_IS_LONG_NAME | SMB_FLAGS2_KNOWS_LONG_NAME);
355 h->uid = smb_swap16(smbc->uid);
356 h->tid = smb_swap16(req->tid);
358 h->pid_high = smb_swap16((unsigned short)(pid >> 16));
359 h->pid = smb_swap16((unsigned short) pid);
362 static CURLcode smb_send(struct connectdata *conn, ssize_t len,
365 struct smb_conn *smbc = &conn->proto.smbc;
366 ssize_t bytes_written;
369 result = Curl_write(conn, FIRSTSOCKET, conn->data->state.uploadbuffer,
370 len, &bytes_written);
374 if(bytes_written != len) {
375 smbc->send_size = len;
376 smbc->sent = bytes_written;
379 smbc->upload_size = upload_size;
384 static CURLcode smb_flush(struct connectdata *conn)
386 struct smb_conn *smbc = &conn->proto.smbc;
387 ssize_t bytes_written;
388 ssize_t len = smbc->send_size - smbc->sent;
394 result = Curl_write(conn, FIRSTSOCKET,
395 conn->data->state.uploadbuffer + smbc->sent,
396 len, &bytes_written);
400 if(bytes_written != len)
401 smbc->sent += bytes_written;
408 static CURLcode smb_send_message(struct connectdata *conn, unsigned char cmd,
409 const void *msg, size_t msg_len)
411 smb_format_message(conn, (struct smb_header *)conn->data->state.uploadbuffer,
413 memcpy(conn->data->state.uploadbuffer + sizeof(struct smb_header),
416 return smb_send(conn, sizeof(struct smb_header) + msg_len, 0);
419 static CURLcode smb_send_negotiate(struct connectdata *conn)
421 const char *msg = "\x00\x0c\x00\x02NT LM 0.12";
423 return smb_send_message(conn, SMB_COM_NEGOTIATE, msg, 15);
426 static CURLcode smb_send_setup(struct connectdata *conn)
428 struct smb_conn *smbc = &conn->proto.smbc;
429 struct smb_setup msg;
431 unsigned char lm_hash[21];
432 unsigned char lm[24];
433 unsigned char nt_hash[21];
434 unsigned char nt[24];
436 size_t byte_count = sizeof(lm) + sizeof(nt);
437 byte_count += strlen(smbc->user) + strlen(smbc->domain);
438 byte_count += strlen(OS) + strlen(CLIENTNAME) + 4; /* 4 null chars */
439 if(byte_count > sizeof(msg.bytes))
440 return CURLE_FILESIZE_EXCEEDED;
442 Curl_ntlm_core_mk_lm_hash(conn->data, conn->passwd, lm_hash);
443 Curl_ntlm_core_lm_resp(lm_hash, smbc->challenge, lm);
444 #ifdef USE_NTRESPONSES
445 Curl_ntlm_core_mk_nt_hash(conn->data, conn->passwd, nt_hash);
446 Curl_ntlm_core_lm_resp(nt_hash, smbc->challenge, nt);
448 memset(nt, 0, sizeof(nt));
451 memset(&msg, 0, sizeof(msg));
452 msg.word_count = SMB_WC_SETUP_ANDX;
453 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
454 msg.max_buffer_size = smb_swap16(MAX_MESSAGE_SIZE);
455 msg.max_mpx_count = smb_swap16(1);
456 msg.vc_number = smb_swap16(1);
457 msg.session_key = smb_swap32(smbc->session_key);
458 msg.capabilities = smb_swap32(SMB_CAP_LARGE_FILES);
459 msg.lengths[0] = smb_swap16(sizeof(lm));
460 msg.lengths[1] = smb_swap16(sizeof(nt));
461 memcpy(p, lm, sizeof(lm));
463 memcpy(p, nt, sizeof(nt));
465 MSGCATNULL(smbc->user);
466 MSGCATNULL(smbc->domain);
468 MSGCATNULL(CLIENTNAME);
469 byte_count = p - msg.bytes;
470 msg.byte_count = smb_swap16((unsigned short)byte_count);
472 return smb_send_message(conn, SMB_COM_SETUP_ANDX, &msg,
473 sizeof(msg) - sizeof(msg.bytes) + byte_count);
476 static CURLcode smb_send_tree_connect(struct connectdata *conn)
478 struct smb_request *req = conn->data->req.protop;
479 struct smb_tree_connect msg;
482 size_t byte_count = strlen(conn->host.name) + strlen(req->share);
483 byte_count += strlen(SERVICENAME) + 5; /* 2 nulls and 3 backslashes */
484 if(byte_count > sizeof(msg.bytes))
485 return CURLE_FILESIZE_EXCEEDED;
487 memset(&msg, 0, sizeof(msg));
488 msg.word_count = SMB_WC_TREE_CONNECT_ANDX;
489 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
492 MSGCAT(conn->host.name);
494 MSGCATNULL(req->share);
495 MSGCATNULL(SERVICENAME); /* Match any type of service */
496 byte_count = p - msg.bytes;
497 msg.byte_count = smb_swap16((unsigned short)byte_count);
499 return smb_send_message(conn, SMB_COM_TREE_CONNECT_ANDX, &msg,
500 sizeof(msg) - sizeof(msg.bytes) + byte_count);
503 static CURLcode smb_send_open(struct connectdata *conn)
505 struct smb_request *req = conn->data->req.protop;
506 struct smb_nt_create msg;
509 if((strlen(req->path) + 1) > sizeof(msg.bytes))
510 return CURLE_FILESIZE_EXCEEDED;
512 memset(&msg, 0, sizeof(msg));
513 msg.word_count = SMB_WC_NT_CREATE_ANDX;
514 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
515 byte_count = strlen(req->path);
516 msg.name_length = smb_swap16((unsigned short)byte_count);
517 msg.share_access = smb_swap32(SMB_FILE_SHARE_ALL);
518 if(conn->data->set.upload) {
519 msg.access = smb_swap32(SMB_GENERIC_READ | SMB_GENERIC_WRITE);
520 msg.create_disposition = smb_swap32(SMB_FILE_OVERWRITE_IF);
523 msg.access = smb_swap32(SMB_GENERIC_READ);
524 msg.create_disposition = smb_swap32(SMB_FILE_OPEN);
526 msg.byte_count = smb_swap16((unsigned short) ++byte_count);
527 strcpy(msg.bytes, req->path);
529 return smb_send_message(conn, SMB_COM_NT_CREATE_ANDX, &msg,
530 sizeof(msg) - sizeof(msg.bytes) + byte_count);
533 static CURLcode smb_send_close(struct connectdata *conn)
535 struct smb_request *req = conn->data->req.protop;
536 struct smb_close msg;
538 memset(&msg, 0, sizeof(msg));
539 msg.word_count = SMB_WC_CLOSE;
540 msg.fid = smb_swap16(req->fid);
542 return smb_send_message(conn, SMB_COM_CLOSE, &msg, sizeof(msg));
545 static CURLcode smb_send_tree_disconnect(struct connectdata *conn)
547 struct smb_tree_disconnect msg;
549 memset(&msg, 0, sizeof(msg));
551 return smb_send_message(conn, SMB_COM_TREE_DISCONNECT, &msg, sizeof(msg));
554 static CURLcode smb_send_read(struct connectdata *conn)
556 struct smb_request *req = conn->data->req.protop;
557 curl_off_t offset = conn->data->req.offset;
560 memset(&msg, 0, sizeof(msg));
561 msg.word_count = SMB_WC_READ_ANDX;
562 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
563 msg.fid = smb_swap16(req->fid);
564 msg.offset = smb_swap32((unsigned int) offset);
565 msg.offset_high = smb_swap32((unsigned int) (offset >> 32));
566 msg.min_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
567 msg.max_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
569 return smb_send_message(conn, SMB_COM_READ_ANDX, &msg, sizeof(msg));
572 static CURLcode smb_send_write(struct connectdata *conn)
574 struct smb_write *msg = (struct smb_write *)conn->data->state.uploadbuffer;
575 struct smb_request *req = conn->data->req.protop;
576 curl_off_t offset = conn->data->req.offset;
578 curl_off_t upload_size = conn->data->req.size - conn->data->req.bytecount;
579 if(upload_size >= MAX_PAYLOAD_SIZE - 1) /* There is one byte of padding */
580 upload_size = MAX_PAYLOAD_SIZE - 1;
582 memset(msg, 0, sizeof(*msg));
583 msg->word_count = SMB_WC_WRITE_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->data_length = smb_swap16((unsigned short) upload_size);
589 msg->data_offset = smb_swap16(sizeof(*msg) - sizeof(unsigned int));
590 msg->byte_count = smb_swap16((unsigned short) (upload_size + 1));
592 smb_format_message(conn, &msg->h, SMB_COM_WRITE_ANDX,
593 sizeof(*msg) - sizeof(msg->h) + (size_t) upload_size);
595 return smb_send(conn, sizeof(*msg), (size_t) upload_size);
598 static CURLcode smb_send_and_recv(struct connectdata *conn, void **msg)
600 struct smb_conn *smbc = &conn->proto.smbc;
603 /* Check if there is data in the transfer buffer */
604 if(!smbc->send_size && smbc->upload_size) {
605 int nread = smbc->upload_size > UPLOAD_BUFSIZE ? UPLOAD_BUFSIZE :
606 (int) smbc->upload_size;
607 conn->data->req.upload_fromhere = conn->data->state.uploadbuffer;
608 result = Curl_fillreadbuffer(conn, nread, &nread);
609 if(result && result != CURLE_AGAIN)
614 smbc->upload_size -= nread;
615 smbc->send_size = nread;
619 /* Check if there is data to send */
620 if(smbc->send_size) {
621 result = smb_flush(conn);
626 /* Check if there is still data to be sent */
627 if(smbc->send_size || smbc->upload_size)
630 return smb_recv_message(conn, msg);
633 static CURLcode smb_connection_state(struct connectdata *conn, bool *done)
635 struct smb_conn *smbc = &conn->proto.smbc;
636 struct smb_negotiate_response *nrsp;
637 struct smb_header *h;
641 if(smbc->state == SMB_CONNECTING) {
643 if((conn->handler->flags & PROTOPT_SSL)) {
644 bool ssl_done = FALSE;
645 result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &ssl_done);
646 if(result && result != CURLE_AGAIN)
653 result = smb_send_negotiate(conn);
655 connclose(conn, "SMB: failed to send negotiate message");
659 conn_state(conn, SMB_NEGOTIATE);
662 /* Send the previous message and check for a response */
663 result = smb_send_and_recv(conn, &msg);
664 if(result && result != CURLE_AGAIN) {
665 connclose(conn, "SMB: failed to communicate");
674 switch(smbc->state) {
676 if(h->status || smbc->got < sizeof(*nrsp) + sizeof(smbc->challenge) - 1) {
677 connclose(conn, "SMB: negotiation failed");
678 return CURLE_COULDNT_CONNECT;
681 memcpy(smbc->challenge, nrsp->bytes, sizeof(smbc->challenge));
682 smbc->session_key = smb_swap32(nrsp->session_key);
683 result = smb_send_setup(conn);
685 connclose(conn, "SMB: failed to send setup message");
688 conn_state(conn, SMB_SETUP);
693 connclose(conn, "SMB: authentication failed");
694 return CURLE_LOGIN_DENIED;
696 smbc->uid = smb_swap16(h->uid);
697 conn_state(conn, SMB_CONNECTED);
702 smb_pop_message(conn);
703 return CURLE_OK; /* ignore */
706 smb_pop_message(conn);
712 * Convert a timestamp from the Windows world (100 nsec units from 1 Jan 1601)
713 * to Posix time. Cap the output to fit within a time_t.
715 static void get_posix_time(time_t *out, curl_off_t timestamp)
717 timestamp -= 116444736000000000;
718 timestamp /= 10000000;
719 #if SIZEOF_TIME_T < SIZEOF_CURL_OFF_T
720 if(timestamp > TIME_T_MAX)
722 else if(timestamp < TIME_T_MIN)
726 *out = (time_t) timestamp;
729 static CURLcode smb_request_state(struct connectdata *conn, bool *done)
731 struct smb_request *req = conn->data->req.protop;
732 struct smb_header *h;
733 struct smb_conn *smbc = &conn->proto.smbc;
734 enum smb_req_state next_state = SMB_DONE;
739 const struct smb_nt_create_response *smb_m;
741 /* Start the request */
742 if(req->state == SMB_REQUESTING) {
743 result = smb_send_tree_connect(conn);
745 connclose(conn, "SMB: failed to send tree connect message");
749 request_state(conn, SMB_TREE_CONNECT);
752 /* Send the previous message and check for a response */
753 result = smb_send_and_recv(conn, &msg);
754 if(result && result != CURLE_AGAIN) {
755 connclose(conn, "SMB: failed to communicate");
765 case SMB_TREE_CONNECT:
767 req->result = CURLE_REMOTE_FILE_NOT_FOUND;
768 if(h->status == smb_swap32(SMB_ERR_NOACCESS))
769 req->result = CURLE_REMOTE_ACCESS_DENIED;
772 req->tid = smb_swap16(h->tid);
773 next_state = SMB_OPEN;
777 if(h->status || smbc->got < sizeof(struct smb_nt_create_response)) {
778 req->result = CURLE_REMOTE_FILE_NOT_FOUND;
779 next_state = SMB_TREE_DISCONNECT;
782 smb_m = (const struct smb_nt_create_response*) msg;
783 req->fid = smb_swap16(smb_m->fid);
784 conn->data->req.offset = 0;
785 if(conn->data->set.upload) {
786 conn->data->req.size = conn->data->state.infilesize;
787 Curl_pgrsSetUploadSize(conn->data, conn->data->req.size);
788 next_state = SMB_UPLOAD;
791 smb_m = (const struct smb_nt_create_response*) msg;
792 conn->data->req.size = smb_swap64(smb_m->end_of_file);
793 Curl_pgrsSetDownloadSize(conn->data, conn->data->req.size);
794 if(conn->data->set.get_filetime)
795 get_posix_time(&conn->data->info.filetime, smb_m->last_change_time);
796 next_state = SMB_DOWNLOAD;
801 if(h->status || smbc->got < sizeof(struct smb_header) + 14) {
802 req->result = CURLE_RECV_ERROR;
803 next_state = SMB_CLOSE;
806 len = Curl_read16_le(((const unsigned char *) msg) +
807 sizeof(struct smb_header) + 11);
808 off = Curl_read16_le(((const unsigned char *) msg) +
809 sizeof(struct smb_header) + 13);
811 if(off + sizeof(unsigned int) + len > smbc->got) {
812 failf(conn->data, "Invalid input packet");
813 result = CURLE_RECV_ERROR;
816 result = Curl_client_write(conn, CLIENTWRITE_BODY,
817 (char *)msg + off + sizeof(unsigned int),
820 req->result = result;
821 next_state = SMB_CLOSE;
825 conn->data->req.bytecount += len;
826 conn->data->req.offset += len;
827 Curl_pgrsSetDownloadCounter(conn->data, conn->data->req.bytecount);
828 next_state = (len < MAX_PAYLOAD_SIZE) ? SMB_CLOSE : SMB_DOWNLOAD;
832 if(h->status || smbc->got < sizeof(struct smb_header) + 6) {
833 req->result = CURLE_UPLOAD_FAILED;
834 next_state = SMB_CLOSE;
837 len = Curl_read16_le(((const unsigned char *) msg) +
838 sizeof(struct smb_header) + 5);
839 conn->data->req.bytecount += len;
840 conn->data->req.offset += len;
841 Curl_pgrsSetUploadCounter(conn->data, conn->data->req.bytecount);
842 if(conn->data->req.bytecount >= conn->data->req.size)
843 next_state = SMB_CLOSE;
845 next_state = SMB_UPLOAD;
849 /* We don't care if the close failed, proceed to tree disconnect anyway */
850 next_state = SMB_TREE_DISCONNECT;
853 case SMB_TREE_DISCONNECT:
854 next_state = SMB_DONE;
858 smb_pop_message(conn);
859 return CURLE_OK; /* ignore */
862 smb_pop_message(conn);
866 result = smb_send_open(conn);
870 result = smb_send_read(conn);
874 result = smb_send_write(conn);
878 result = smb_send_close(conn);
881 case SMB_TREE_DISCONNECT:
882 result = smb_send_tree_disconnect(conn);
886 result = req->result;
895 connclose(conn, "SMB: failed to send message");
899 request_state(conn, next_state);
904 static CURLcode smb_done(struct connectdata *conn, CURLcode status,
907 struct smb_request *req = conn->data->req.protop;
911 Curl_safefree(req->share);
912 Curl_safefree(conn->data->req.protop);
917 static CURLcode smb_disconnect(struct connectdata *conn, bool dead)
919 struct smb_conn *smbc = &conn->proto.smbc;
920 struct smb_request *req = conn->data->req.protop;
924 Curl_safefree(smbc->domain);
925 Curl_safefree(smbc->recv_buf);
927 /* smb_done is not always called, so cleanup the request */
929 Curl_safefree(req->share);
935 static int smb_getsock(struct connectdata *conn, curl_socket_t *socks,
938 struct smb_conn *smbc = &conn->proto.smbc;
941 return GETSOCK_BLANK;
943 socks[0] = conn->sock[FIRSTSOCKET];
945 if(smbc->send_size || smbc->upload_size)
946 return GETSOCK_WRITESOCK(0);
948 return GETSOCK_READSOCK(0);
951 static CURLcode smb_parse_url_path(struct connectdata *conn)
953 CURLcode result = CURLE_OK;
954 struct Curl_easy *data = conn->data;
955 struct smb_request *req = data->req.protop;
959 /* URL decode the path */
960 result = Curl_urldecode(data, data->state.path, 0, &path, NULL, TRUE);
964 /* Parse the path for the share */
965 req->share = strdup((*path == '/' || *path == '\\') ? path + 1 : path);
969 return CURLE_OUT_OF_MEMORY;
972 slash = strchr(req->share, '/');
974 slash = strchr(req->share, '\\');
976 /* The share must be present */
980 return CURLE_URL_MALFORMAT;
983 /* Parse the path for the file path converting any forward slashes into
987 for(; *slash; slash++) {
997 #endif /* !USE_WINDOWS_SSPI || USE_WIN32_CRYPTO */
999 #endif /* CURL_DISABLE_SMB && USE_NTLM && CURL_SIZEOF_CURL_OFF_T > 4 */