1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2014, Bill Nagel <wnagel@tycoint.com>, Exacq Technologies
9 * Copyright (C) 2016-2020, 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_CURL_NTLM_CORE) && \
27 (CURL_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 connectdata *conn);
58 static CURLcode smb_connect(struct connectdata *conn, bool *done);
59 static CURLcode smb_connection_state(struct connectdata *conn, bool *done);
60 static CURLcode smb_do(struct connectdata *conn, bool *done);
61 static CURLcode smb_request_state(struct connectdata *conn, bool *done);
62 static CURLcode smb_done(struct connectdata *conn, CURLcode status,
64 static CURLcode smb_disconnect(struct connectdata *conn, bool dead);
65 static int smb_getsock(struct connectdata *conn, curl_socket_t *socks);
66 static CURLcode smb_parse_url_path(struct connectdata *conn);
69 * SMB handler interface
71 const struct Curl_handler Curl_handler_smb = {
73 smb_setup_connection, /* setup_connection */
76 ZERO_NULL, /* do_more */
77 smb_connect, /* connect_it */
78 smb_connection_state, /* connecting */
79 smb_request_state, /* doing */
80 smb_getsock, /* proto_getsock */
81 smb_getsock, /* doing_getsock */
82 ZERO_NULL, /* domore_getsock */
83 ZERO_NULL, /* perform_getsock */
84 smb_disconnect, /* disconnect */
85 ZERO_NULL, /* readwrite */
86 ZERO_NULL, /* connection_check */
87 PORT_SMB, /* defport */
88 CURLPROTO_SMB, /* protocol */
89 CURLPROTO_SMB, /* family */
90 PROTOPT_NONE /* flags */
95 * SMBS handler interface
97 const struct Curl_handler Curl_handler_smbs = {
99 smb_setup_connection, /* setup_connection */
102 ZERO_NULL, /* do_more */
103 smb_connect, /* connect_it */
104 smb_connection_state, /* connecting */
105 smb_request_state, /* doing */
106 smb_getsock, /* proto_getsock */
107 smb_getsock, /* doing_getsock */
108 ZERO_NULL, /* domore_getsock */
109 ZERO_NULL, /* perform_getsock */
110 smb_disconnect, /* disconnect */
111 ZERO_NULL, /* readwrite */
112 ZERO_NULL, /* connection_check */
113 PORT_SMBS, /* defport */
114 CURLPROTO_SMBS, /* protocol */
115 CURLPROTO_SMB, /* family */
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;
177 unsigned short tid; /* Even if we connect to the same tree as another */
178 unsigned short fid; /* request, the tid will be different */
182 static void conn_state(struct connectdata *conn, enum smb_conn_state newstate)
184 struct smb_conn *smbc = &conn->proto.smbc;
185 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
186 /* For debug purposes */
187 static const char * const names[] = {
196 if(smbc->state != newstate)
197 infof(conn->data, "SMB conn %p state change from %s to %s\n",
198 (void *)smbc, names[smbc->state], names[newstate]);
201 smbc->state = newstate;
204 static void request_state(struct connectdata *conn,
205 enum smb_req_state newstate)
207 struct smb_request *req = conn->data->req.protop;
208 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
209 /* For debug purposes */
210 static const char * const names[] = {
217 "SMB_TREE_DISCONNECT",
222 if(req->state != newstate)
223 infof(conn->data, "SMB request %p state change from %s to %s\n",
224 (void *)req, names[req->state], names[newstate]);
227 req->state = newstate;
230 /* this should setup things in the connection, not in the easy
232 static CURLcode smb_setup_connection(struct connectdata *conn)
234 struct smb_request *req;
236 /* Initialize the request state */
237 conn->data->req.protop = req = calloc(1, sizeof(struct smb_request));
239 return CURLE_OUT_OF_MEMORY;
241 /* Parse the URL path */
242 return smb_parse_url_path(conn);
245 static CURLcode smb_connect(struct connectdata *conn, bool *done)
247 struct smb_conn *smbc = &conn->proto.smbc;
252 /* Check we have a username and password to authenticate with */
253 if(!conn->bits.user_passwd)
254 return CURLE_LOGIN_DENIED;
256 /* Initialize the connection state */
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.ulbuf,
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.ulbuf + 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 CURLcode result = Curl_get_upload_buffer(conn->data);
414 smb_format_message(conn, (struct smb_header *)conn->data->state.ulbuf,
416 memcpy(conn->data->state.ulbuf + sizeof(struct smb_header),
419 return smb_send(conn, sizeof(struct smb_header) + msg_len, 0);
422 static CURLcode smb_send_negotiate(struct connectdata *conn)
424 const char *msg = "\x00\x0c\x00\x02NT LM 0.12";
426 return smb_send_message(conn, SMB_COM_NEGOTIATE, msg, 15);
429 static CURLcode smb_send_setup(struct connectdata *conn)
431 struct smb_conn *smbc = &conn->proto.smbc;
432 struct smb_setup msg;
434 unsigned char lm_hash[21];
435 unsigned char lm[24];
436 unsigned char nt_hash[21];
437 unsigned char nt[24];
439 size_t byte_count = sizeof(lm) + sizeof(nt);
440 byte_count += strlen(smbc->user) + strlen(smbc->domain);
441 byte_count += strlen(OS) + strlen(CLIENTNAME) + 4; /* 4 null chars */
442 if(byte_count > sizeof(msg.bytes))
443 return CURLE_FILESIZE_EXCEEDED;
445 Curl_ntlm_core_mk_lm_hash(conn->data, conn->passwd, lm_hash);
446 Curl_ntlm_core_lm_resp(lm_hash, smbc->challenge, lm);
447 #ifdef USE_NTRESPONSES
448 Curl_ntlm_core_mk_nt_hash(conn->data, conn->passwd, nt_hash);
449 Curl_ntlm_core_lm_resp(nt_hash, smbc->challenge, nt);
451 memset(nt, 0, sizeof(nt));
454 memset(&msg, 0, sizeof(msg));
455 msg.word_count = SMB_WC_SETUP_ANDX;
456 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
457 msg.max_buffer_size = smb_swap16(MAX_MESSAGE_SIZE);
458 msg.max_mpx_count = smb_swap16(1);
459 msg.vc_number = smb_swap16(1);
460 msg.session_key = smb_swap32(smbc->session_key);
461 msg.capabilities = smb_swap32(SMB_CAP_LARGE_FILES);
462 msg.lengths[0] = smb_swap16(sizeof(lm));
463 msg.lengths[1] = smb_swap16(sizeof(nt));
464 memcpy(p, lm, sizeof(lm));
466 memcpy(p, nt, sizeof(nt));
468 MSGCATNULL(smbc->user);
469 MSGCATNULL(smbc->domain);
471 MSGCATNULL(CLIENTNAME);
472 byte_count = p - msg.bytes;
473 msg.byte_count = smb_swap16((unsigned short)byte_count);
475 return smb_send_message(conn, SMB_COM_SETUP_ANDX, &msg,
476 sizeof(msg) - sizeof(msg.bytes) + byte_count);
479 static CURLcode smb_send_tree_connect(struct connectdata *conn)
481 struct smb_tree_connect msg;
482 struct smb_conn *smbc = &conn->proto.smbc;
485 size_t byte_count = strlen(conn->host.name) + strlen(smbc->share);
486 byte_count += strlen(SERVICENAME) + 5; /* 2 nulls and 3 backslashes */
487 if(byte_count > sizeof(msg.bytes))
488 return CURLE_FILESIZE_EXCEEDED;
490 memset(&msg, 0, sizeof(msg));
491 msg.word_count = SMB_WC_TREE_CONNECT_ANDX;
492 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
495 MSGCAT(conn->host.name);
497 MSGCATNULL(smbc->share);
498 MSGCATNULL(SERVICENAME); /* Match any type of service */
499 byte_count = p - msg.bytes;
500 msg.byte_count = smb_swap16((unsigned short)byte_count);
502 return smb_send_message(conn, SMB_COM_TREE_CONNECT_ANDX, &msg,
503 sizeof(msg) - sizeof(msg.bytes) + byte_count);
506 static CURLcode smb_send_open(struct connectdata *conn)
508 struct smb_request *req = conn->data->req.protop;
509 struct smb_nt_create msg;
512 if((strlen(req->path) + 1) > sizeof(msg.bytes))
513 return CURLE_FILESIZE_EXCEEDED;
515 memset(&msg, 0, sizeof(msg));
516 msg.word_count = SMB_WC_NT_CREATE_ANDX;
517 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
518 byte_count = strlen(req->path);
519 msg.name_length = smb_swap16((unsigned short)byte_count);
520 msg.share_access = smb_swap32(SMB_FILE_SHARE_ALL);
521 if(conn->data->set.upload) {
522 msg.access = smb_swap32(SMB_GENERIC_READ | SMB_GENERIC_WRITE);
523 msg.create_disposition = smb_swap32(SMB_FILE_OVERWRITE_IF);
526 msg.access = smb_swap32(SMB_GENERIC_READ);
527 msg.create_disposition = smb_swap32(SMB_FILE_OPEN);
529 msg.byte_count = smb_swap16((unsigned short) ++byte_count);
530 strcpy(msg.bytes, req->path);
532 return smb_send_message(conn, SMB_COM_NT_CREATE_ANDX, &msg,
533 sizeof(msg) - sizeof(msg.bytes) + byte_count);
536 static CURLcode smb_send_close(struct connectdata *conn)
538 struct smb_request *req = conn->data->req.protop;
539 struct smb_close msg;
541 memset(&msg, 0, sizeof(msg));
542 msg.word_count = SMB_WC_CLOSE;
543 msg.fid = smb_swap16(req->fid);
545 return smb_send_message(conn, SMB_COM_CLOSE, &msg, sizeof(msg));
548 static CURLcode smb_send_tree_disconnect(struct connectdata *conn)
550 struct smb_tree_disconnect msg;
552 memset(&msg, 0, sizeof(msg));
554 return smb_send_message(conn, SMB_COM_TREE_DISCONNECT, &msg, sizeof(msg));
557 static CURLcode smb_send_read(struct connectdata *conn)
559 struct smb_request *req = conn->data->req.protop;
560 curl_off_t offset = conn->data->req.offset;
563 memset(&msg, 0, sizeof(msg));
564 msg.word_count = SMB_WC_READ_ANDX;
565 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
566 msg.fid = smb_swap16(req->fid);
567 msg.offset = smb_swap32((unsigned int) offset);
568 msg.offset_high = smb_swap32((unsigned int) (offset >> 32));
569 msg.min_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
570 msg.max_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
572 return smb_send_message(conn, SMB_COM_READ_ANDX, &msg, sizeof(msg));
575 static CURLcode smb_send_write(struct connectdata *conn)
577 struct smb_write *msg;
578 struct smb_request *req = conn->data->req.protop;
579 curl_off_t offset = conn->data->req.offset;
580 curl_off_t upload_size = conn->data->req.size - conn->data->req.bytecount;
581 CURLcode result = Curl_get_upload_buffer(conn->data);
584 msg = (struct smb_write *)conn->data->state.ulbuf;
586 if(upload_size >= MAX_PAYLOAD_SIZE - 1) /* There is one byte of padding */
587 upload_size = MAX_PAYLOAD_SIZE - 1;
589 memset(msg, 0, sizeof(*msg));
590 msg->word_count = SMB_WC_WRITE_ANDX;
591 msg->andx.command = SMB_COM_NO_ANDX_COMMAND;
592 msg->fid = smb_swap16(req->fid);
593 msg->offset = smb_swap32((unsigned int) offset);
594 msg->offset_high = smb_swap32((unsigned int) (offset >> 32));
595 msg->data_length = smb_swap16((unsigned short) upload_size);
596 msg->data_offset = smb_swap16(sizeof(*msg) - sizeof(unsigned int));
597 msg->byte_count = smb_swap16((unsigned short) (upload_size + 1));
599 smb_format_message(conn, &msg->h, SMB_COM_WRITE_ANDX,
600 sizeof(*msg) - sizeof(msg->h) + (size_t) upload_size);
602 return smb_send(conn, sizeof(*msg), (size_t) upload_size);
605 static CURLcode smb_send_and_recv(struct connectdata *conn, void **msg)
607 struct smb_conn *smbc = &conn->proto.smbc;
609 *msg = NULL; /* if it returns early */
611 /* Check if there is data in the transfer buffer */
612 if(!smbc->send_size && smbc->upload_size) {
613 size_t nread = smbc->upload_size > conn->data->set.upload_buffer_size ?
614 conn->data->set.upload_buffer_size :
616 conn->data->req.upload_fromhere = conn->data->state.ulbuf;
617 result = Curl_fillreadbuffer(conn, nread, &nread);
618 if(result && result != CURLE_AGAIN)
623 smbc->upload_size -= nread;
624 smbc->send_size = nread;
628 /* Check if there is data to send */
629 if(smbc->send_size) {
630 result = smb_flush(conn);
635 /* Check if there is still data to be sent */
636 if(smbc->send_size || smbc->upload_size)
639 return smb_recv_message(conn, msg);
642 static CURLcode smb_connection_state(struct connectdata *conn, bool *done)
644 struct smb_conn *smbc = &conn->proto.smbc;
645 struct smb_negotiate_response *nrsp;
646 struct smb_header *h;
650 if(smbc->state == SMB_CONNECTING) {
652 if((conn->handler->flags & PROTOPT_SSL)) {
653 bool ssl_done = FALSE;
654 result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &ssl_done);
655 if(result && result != CURLE_AGAIN)
662 result = smb_send_negotiate(conn);
664 connclose(conn, "SMB: failed to send negotiate message");
668 conn_state(conn, SMB_NEGOTIATE);
671 /* Send the previous message and check for a response */
672 result = smb_send_and_recv(conn, &msg);
673 if(result && result != CURLE_AGAIN) {
674 connclose(conn, "SMB: failed to communicate");
683 switch(smbc->state) {
685 if((smbc->got < sizeof(*nrsp) + sizeof(smbc->challenge) - 1) ||
687 connclose(conn, "SMB: negotiation failed");
688 return CURLE_COULDNT_CONNECT;
691 memcpy(smbc->challenge, nrsp->bytes, sizeof(smbc->challenge));
692 smbc->session_key = smb_swap32(nrsp->session_key);
693 result = smb_send_setup(conn);
695 connclose(conn, "SMB: failed to send setup message");
698 conn_state(conn, SMB_SETUP);
703 connclose(conn, "SMB: authentication failed");
704 return CURLE_LOGIN_DENIED;
706 smbc->uid = smb_swap16(h->uid);
707 conn_state(conn, SMB_CONNECTED);
712 smb_pop_message(conn);
713 return CURLE_OK; /* ignore */
716 smb_pop_message(conn);
722 * Convert a timestamp from the Windows world (100 nsec units from 1 Jan 1601)
723 * to Posix time. Cap the output to fit within a time_t.
725 static void get_posix_time(time_t *out, curl_off_t timestamp)
727 timestamp -= 116444736000000000;
728 timestamp /= 10000000;
729 #if SIZEOF_TIME_T < SIZEOF_CURL_OFF_T
730 if(timestamp > TIME_T_MAX)
732 else if(timestamp < TIME_T_MIN)
736 *out = (time_t) timestamp;
739 static CURLcode smb_request_state(struct connectdata *conn, bool *done)
741 struct smb_request *req = conn->data->req.protop;
742 struct smb_header *h;
743 struct smb_conn *smbc = &conn->proto.smbc;
744 enum smb_req_state next_state = SMB_DONE;
749 const struct smb_nt_create_response *smb_m;
751 /* Start the request */
752 if(req->state == SMB_REQUESTING) {
753 result = smb_send_tree_connect(conn);
755 connclose(conn, "SMB: failed to send tree connect message");
759 request_state(conn, SMB_TREE_CONNECT);
762 /* Send the previous message and check for a response */
763 result = smb_send_and_recv(conn, &msg);
764 if(result && result != CURLE_AGAIN) {
765 connclose(conn, "SMB: failed to communicate");
775 case SMB_TREE_CONNECT:
777 req->result = CURLE_REMOTE_FILE_NOT_FOUND;
778 if(h->status == smb_swap32(SMB_ERR_NOACCESS))
779 req->result = CURLE_REMOTE_ACCESS_DENIED;
782 req->tid = smb_swap16(h->tid);
783 next_state = SMB_OPEN;
787 if(h->status || smbc->got < sizeof(struct smb_nt_create_response)) {
788 req->result = CURLE_REMOTE_FILE_NOT_FOUND;
789 if(h->status == smb_swap32(SMB_ERR_NOACCESS))
790 req->result = CURLE_REMOTE_ACCESS_DENIED;
791 next_state = SMB_TREE_DISCONNECT;
794 smb_m = (const struct smb_nt_create_response*) msg;
795 req->fid = smb_swap16(smb_m->fid);
796 conn->data->req.offset = 0;
797 if(conn->data->set.upload) {
798 conn->data->req.size = conn->data->state.infilesize;
799 Curl_pgrsSetUploadSize(conn->data, conn->data->req.size);
800 next_state = SMB_UPLOAD;
803 smb_m = (const struct smb_nt_create_response*) msg;
804 conn->data->req.size = smb_swap64(smb_m->end_of_file);
805 if(conn->data->req.size < 0) {
806 req->result = CURLE_WEIRD_SERVER_REPLY;
807 next_state = SMB_CLOSE;
810 Curl_pgrsSetDownloadSize(conn->data, conn->data->req.size);
811 if(conn->data->set.get_filetime)
812 get_posix_time(&conn->data->info.filetime, smb_m->last_change_time);
813 next_state = SMB_DOWNLOAD;
819 if(h->status || smbc->got < sizeof(struct smb_header) + 14) {
820 req->result = CURLE_RECV_ERROR;
821 next_state = SMB_CLOSE;
824 len = Curl_read16_le(((const unsigned char *) msg) +
825 sizeof(struct smb_header) + 11);
826 off = Curl_read16_le(((const unsigned char *) msg) +
827 sizeof(struct smb_header) + 13);
829 if(off + sizeof(unsigned int) + len > smbc->got) {
830 failf(conn->data, "Invalid input packet");
831 result = CURLE_RECV_ERROR;
834 result = Curl_client_write(conn, CLIENTWRITE_BODY,
835 (char *)msg + off + sizeof(unsigned int),
838 req->result = result;
839 next_state = SMB_CLOSE;
843 conn->data->req.bytecount += len;
844 conn->data->req.offset += len;
845 Curl_pgrsSetDownloadCounter(conn->data, conn->data->req.bytecount);
846 next_state = (len < MAX_PAYLOAD_SIZE) ? SMB_CLOSE : SMB_DOWNLOAD;
850 if(h->status || smbc->got < sizeof(struct smb_header) + 6) {
851 req->result = CURLE_UPLOAD_FAILED;
852 next_state = SMB_CLOSE;
855 len = Curl_read16_le(((const unsigned char *) msg) +
856 sizeof(struct smb_header) + 5);
857 conn->data->req.bytecount += len;
858 conn->data->req.offset += len;
859 Curl_pgrsSetUploadCounter(conn->data, conn->data->req.bytecount);
860 if(conn->data->req.bytecount >= conn->data->req.size)
861 next_state = SMB_CLOSE;
863 next_state = SMB_UPLOAD;
867 /* We don't care if the close failed, proceed to tree disconnect anyway */
868 next_state = SMB_TREE_DISCONNECT;
871 case SMB_TREE_DISCONNECT:
872 next_state = SMB_DONE;
876 smb_pop_message(conn);
877 return CURLE_OK; /* ignore */
880 smb_pop_message(conn);
884 result = smb_send_open(conn);
888 result = smb_send_read(conn);
892 result = smb_send_write(conn);
896 result = smb_send_close(conn);
899 case SMB_TREE_DISCONNECT:
900 result = smb_send_tree_disconnect(conn);
904 result = req->result;
913 connclose(conn, "SMB: failed to send message");
917 request_state(conn, next_state);
922 static CURLcode smb_done(struct connectdata *conn, CURLcode status,
926 Curl_safefree(conn->data->req.protop);
930 static CURLcode smb_disconnect(struct connectdata *conn, bool dead)
932 struct smb_conn *smbc = &conn->proto.smbc;
934 Curl_safefree(smbc->share);
935 Curl_safefree(smbc->domain);
936 Curl_safefree(smbc->recv_buf);
940 static int smb_getsock(struct connectdata *conn, curl_socket_t *socks)
942 socks[0] = conn->sock[FIRSTSOCKET];
943 return GETSOCK_READSOCK(0) | GETSOCK_WRITESOCK(0);
946 static CURLcode smb_do(struct connectdata *conn, bool *done)
948 struct smb_conn *smbc = &conn->proto.smbc;
954 return CURLE_URL_MALFORMAT;
957 static CURLcode smb_parse_url_path(struct connectdata *conn)
959 struct Curl_easy *data = conn->data;
960 struct smb_request *req = data->req.protop;
961 struct smb_conn *smbc = &conn->proto.smbc;
965 /* URL decode the path */
966 CURLcode result = Curl_urldecode(data, data->state.up.path, 0, &path, NULL,
971 /* Parse the path for the share */
972 smbc->share = strdup((*path == '/' || *path == '\\') ? path + 1 : path);
975 return CURLE_OUT_OF_MEMORY;
977 slash = strchr(smbc->share, '/');
979 slash = strchr(smbc->share, '\\');
981 /* The share must be present */
983 Curl_safefree(smbc->share);
984 return CURLE_URL_MALFORMAT;
987 /* Parse the path for the file path converting any forward slashes into
992 for(; *slash; slash++) {
999 #endif /* CURL_DISABLE_SMB && USE_CURL_NTLM_CORE &&
1000 CURL_SIZEOF_CURL_OFF_T > 4 */