1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2014, Bill Nagel <wnagel@tycoint.com>, Exacq Technologies
9 * Copyright (C) 2015, 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 http://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 #define getpid _getpid
45 #include "vtls/vtls.h"
46 #include "curl_ntlm_core.h"
47 #include "curl_memory.h"
49 #include "curl_endian.h"
51 /* The last #include file should be: */
54 /* Local API functions */
55 static CURLcode smb_setup_connection(struct connectdata *conn);
56 static CURLcode smb_connect(struct connectdata *conn, bool *done);
57 static CURLcode smb_connection_state(struct connectdata *conn, bool *done);
58 static CURLcode smb_request_state(struct connectdata *conn, bool *done);
59 static CURLcode smb_done(struct connectdata *conn, CURLcode status,
61 static CURLcode smb_disconnect(struct connectdata *conn, bool dead);
62 static int smb_getsock(struct connectdata *conn, curl_socket_t *socks,
64 static CURLcode smb_parse_url_path(struct connectdata *conn);
67 * SMB handler interface
69 const struct Curl_handler Curl_handler_smb = {
71 smb_setup_connection, /* setup_connection */
72 ZERO_NULL, /* do_it */
74 ZERO_NULL, /* do_more */
75 smb_connect, /* connect_it */
76 smb_connection_state, /* connecting */
77 smb_request_state, /* doing */
78 smb_getsock, /* proto_getsock */
79 smb_getsock, /* doing_getsock */
80 ZERO_NULL, /* domore_getsock */
81 ZERO_NULL, /* perform_getsock */
82 smb_disconnect, /* disconnect */
83 ZERO_NULL, /* readwrite */
84 PORT_SMB, /* defport */
85 CURLPROTO_SMB, /* protocol */
86 PROTOPT_NONE /* flags */
91 * SMBS handler interface
93 const struct Curl_handler Curl_handler_smbs = {
95 smb_setup_connection, /* setup_connection */
96 ZERO_NULL, /* do_it */
98 ZERO_NULL, /* do_more */
99 smb_connect, /* connect_it */
100 smb_connection_state, /* connecting */
101 smb_request_state, /* doing */
102 smb_getsock, /* proto_getsock */
103 smb_getsock, /* doing_getsock */
104 ZERO_NULL, /* domore_getsock */
105 ZERO_NULL, /* perform_getsock */
106 smb_disconnect, /* disconnect */
107 ZERO_NULL, /* readwrite */
108 PORT_SMBS, /* defport */
109 CURLPROTO_SMBS, /* protocol */
110 PROTOPT_SSL /* flags */
114 #define MAX_PAYLOAD_SIZE 0x8000
115 #define MAX_MESSAGE_SIZE (MAX_PAYLOAD_SIZE + 0x1000)
116 #define CLIENTNAME "curl"
117 #define SERVICENAME "?????"
119 /* Append a string to an SMB message */
120 #define MSGCAT(str) \
124 /* Append a null-terminated string to an SMB message */
125 #define MSGCATNULL(str) \
127 p += strlen(str) + 1;
129 /* SMB is mostly little endian */
130 #if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \
132 static unsigned short smb_swap16(unsigned short x)
134 return (x << 8) | ((x >> 8) & 0xff);
137 static unsigned int smb_swap32(unsigned int x)
139 return (x << 24) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) |
144 static unsigned long long smb_swap64(unsigned long long x)
146 return ((unsigned long long)smb_swap32(x) << 32) | smb_swap32(x >> 32);
149 static unsigned __int64 smb_swap64(unsigned __int64 x)
151 return ((unsigned __int64)smb_swap32(x) << 32) | smb_swap32(x >> 32);
155 # define smb_swap16(x) (x)
156 # define smb_swap32(x) (x)
157 # define smb_swap64(x) (x)
160 /* SMB request state */
172 /* SMB request data */
174 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 *smb = &conn->proto.smbc;
185 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
186 /* For debug purposes */
187 static const char * const names[] = {
196 if(smb->state != newstate)
197 infof(conn->data, "SMB conn %p state change from %s to %s\n",
198 (void *)smb, names[smb->state], names[newstate]);
201 smb->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 static CURLcode smb_setup_connection(struct connectdata *conn)
232 struct smb_request *req;
234 /* Initialize the request state */
235 conn->data->req.protop = req = calloc(1, sizeof(struct smb_request));
237 return CURLE_OUT_OF_MEMORY;
239 /* Parse the URL path */
240 return smb_parse_url_path(conn);
243 static CURLcode smb_connect(struct connectdata *conn, bool *done)
245 struct smb_conn *smbc = &conn->proto.smbc;
250 /* Check we have a username and password to authenticate with */
251 if(!conn->bits.user_passwd)
252 return CURLE_LOGIN_DENIED;
254 /* Initialize the connection state */
255 memset(smbc, 0, sizeof(*smbc));
256 smbc->state = SMB_CONNECTING;
257 smbc->recv_buf = malloc(MAX_MESSAGE_SIZE);
259 return CURLE_OUT_OF_MEMORY;
261 /* Multiple requests are allowed with this connection */
262 connkeep(conn, "SMB default");
264 /* Parse the username, domain, and password */
265 slash = strchr(conn->user, '/');
267 slash = strchr(conn->user, '\\');
270 smbc->user = slash + 1;
271 smbc->domain = strdup(conn->user);
273 return CURLE_OUT_OF_MEMORY;
274 smbc->domain[slash - conn->user] = 0;
277 smbc->user = conn->user;
278 smbc->domain = strdup(conn->host.name);
280 return CURLE_OUT_OF_MEMORY;
286 static CURLcode smb_recv_message(struct connectdata *conn, void **msg)
288 struct smb_conn *smbc = &conn->proto.smbc;
289 char *buf = smbc->recv_buf;
293 size_t len = MAX_MESSAGE_SIZE - smbc->got;
296 result = Curl_read(conn, FIRSTSOCKET, buf + smbc->got, len, &bytes_read);
303 smbc->got += bytes_read;
305 /* Check for a 32-bit nbt header */
306 if(smbc->got < sizeof(unsigned int))
309 nbt_size = Curl_read16_be((unsigned char *)(buf + sizeof(unsigned short))) +
310 sizeof(unsigned int);
311 if(smbc->got < nbt_size)
314 msg_size = sizeof(struct smb_header);
315 if(nbt_size >= msg_size + 1) {
316 /* Add the word count */
317 msg_size += 1 + ((unsigned char) buf[msg_size]) * sizeof(unsigned short);
318 if(nbt_size >= msg_size + sizeof(unsigned short)) {
319 /* Add the byte count */
320 msg_size += sizeof(unsigned short) +
321 Curl_read16_le((unsigned char *)&buf[msg_size]);
322 if(nbt_size < msg_size)
323 return CURLE_READ_ERROR;
332 static void smb_pop_message(struct connectdata *conn)
334 struct smb_conn *smbc = &conn->proto.smbc;
339 static void smb_format_message(struct connectdata *conn, struct smb_header *h,
340 unsigned char cmd, size_t len)
342 struct smb_conn *smbc = &conn->proto.smbc;
343 struct smb_request *req = conn->data->req.protop;
346 memset(h, 0, sizeof(*h));
347 h->nbt_length = htons((unsigned short) (sizeof(*h) - sizeof(unsigned int) +
349 memcpy((char *)h->magic, "\xffSMB", 4);
351 h->flags = SMB_FLAGS_CANONICAL_PATHNAMES | SMB_FLAGS_CASELESS_PATHNAMES;
352 h->flags2 = smb_swap16(SMB_FLAGS2_IS_LONG_NAME | SMB_FLAGS2_KNOWS_LONG_NAME);
353 h->uid = smb_swap16(smbc->uid);
354 h->tid = smb_swap16(req->tid);
356 h->pid_high = smb_swap16((unsigned short)(pid >> 16));
357 h->pid = smb_swap16((unsigned short) pid);
360 static CURLcode smb_send(struct connectdata *conn, ssize_t len,
363 struct smb_conn *smbc = &conn->proto.smbc;
364 ssize_t bytes_written;
367 result = Curl_write(conn, FIRSTSOCKET, conn->data->state.uploadbuffer,
368 len, &bytes_written);
372 if(bytes_written != len) {
373 smbc->send_size = len;
374 smbc->sent = bytes_written;
377 smbc->upload_size = upload_size;
382 static CURLcode smb_flush(struct connectdata *conn)
384 struct smb_conn *smbc = &conn->proto.smbc;
385 ssize_t bytes_written;
386 ssize_t len = smbc->send_size - smbc->sent;
392 result = Curl_write(conn, FIRSTSOCKET,
393 conn->data->state.uploadbuffer + smbc->sent,
394 len, &bytes_written);
398 if(bytes_written != len)
399 smbc->sent += bytes_written;
406 static CURLcode smb_send_message(struct connectdata *conn, unsigned char cmd,
407 const void *msg, size_t msg_len)
409 smb_format_message(conn, (struct smb_header *)conn->data->state.uploadbuffer,
411 memcpy(conn->data->state.uploadbuffer + sizeof(struct smb_header),
414 return smb_send(conn, sizeof(struct smb_header) + msg_len, 0);
417 static CURLcode smb_send_negotiate(struct connectdata *conn)
419 const char *msg = "\x00\x0c\x00\x02NT LM 0.12";
421 return smb_send_message(conn, SMB_COM_NEGOTIATE, msg, 15);
424 static CURLcode smb_send_setup(struct connectdata *conn)
426 struct smb_conn *smbc = &conn->proto.smbc;
427 struct smb_setup msg;
429 unsigned char lm_hash[21];
430 unsigned char lm[24];
431 unsigned char nt_hash[21];
432 unsigned char nt[24];
434 size_t byte_count = sizeof(lm) + sizeof(nt);
435 byte_count += strlen(smbc->user) + strlen(smbc->domain);
436 byte_count += strlen(OS) + strlen(CLIENTNAME) + 4; /* 4 null chars */
437 if(byte_count > sizeof(msg.bytes))
438 return CURLE_FILESIZE_EXCEEDED;
440 Curl_ntlm_core_mk_lm_hash(conn->data, conn->passwd, lm_hash);
441 Curl_ntlm_core_lm_resp(lm_hash, smbc->challenge, lm);
443 Curl_ntlm_core_mk_nt_hash(conn->data, conn->passwd, nt_hash);
444 Curl_ntlm_core_lm_resp(nt_hash, smbc->challenge, nt);
446 memset(nt, 0, sizeof(nt));
449 memset(&msg, 0, sizeof(msg));
450 msg.word_count = SMB_WC_SETUP_ANDX;
451 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
452 msg.max_buffer_size = smb_swap16(MAX_MESSAGE_SIZE);
453 msg.max_mpx_count = smb_swap16(1);
454 msg.vc_number = smb_swap16(1);
455 msg.session_key = smb_swap32(smbc->session_key);
456 msg.capabilities = smb_swap32(SMB_CAP_LARGE_FILES);
457 msg.lengths[0] = smb_swap16(sizeof(lm));
458 msg.lengths[1] = smb_swap16(sizeof(nt));
459 memcpy(p, lm, sizeof(lm));
461 memcpy(p, nt, sizeof(nt));
463 MSGCATNULL(smbc->user);
464 MSGCATNULL(smbc->domain);
466 MSGCATNULL(CLIENTNAME);
467 byte_count = p - msg.bytes;
468 msg.byte_count = smb_swap16((unsigned short)byte_count);
470 return smb_send_message(conn, SMB_COM_SETUP_ANDX, &msg,
471 sizeof(msg) - sizeof(msg.bytes) + byte_count);
474 static CURLcode smb_send_tree_connect(struct connectdata *conn)
476 struct smb_request *req = conn->data->req.protop;
477 struct smb_tree_connect msg;
480 size_t byte_count = strlen(conn->host.name) + strlen(req->share);
481 byte_count += strlen(SERVICENAME) + 5; /* 2 nulls and 3 backslashes */
482 if(byte_count > sizeof(msg.bytes))
483 return CURLE_FILESIZE_EXCEEDED;
485 memset(&msg, 0, sizeof(msg));
486 msg.word_count = SMB_WC_TREE_CONNECT_ANDX;
487 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
490 MSGCAT(conn->host.name);
492 MSGCATNULL(req->share);
493 MSGCATNULL(SERVICENAME); /* Match any type of service */
494 byte_count = p - msg.bytes;
495 msg.byte_count = smb_swap16((unsigned short)byte_count);
497 return smb_send_message(conn, SMB_COM_TREE_CONNECT_ANDX, &msg,
498 sizeof(msg) - sizeof(msg.bytes) + byte_count);
501 static CURLcode smb_send_open(struct connectdata *conn)
503 struct smb_request *req = conn->data->req.protop;
504 struct smb_nt_create msg;
507 if((strlen(req->path) + 1) > sizeof(msg.bytes))
508 return CURLE_FILESIZE_EXCEEDED;
510 memset(&msg, 0, sizeof(msg));
511 msg.word_count = SMB_WC_NT_CREATE_ANDX;
512 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
513 byte_count = strlen(req->path);
514 msg.name_length = smb_swap16((unsigned short)byte_count);
515 msg.share_access = smb_swap32(SMB_FILE_SHARE_ALL);
516 if(conn->data->set.upload) {
517 msg.access = smb_swap32(SMB_GENERIC_READ | SMB_GENERIC_WRITE);
518 msg.create_disposition = smb_swap32(SMB_FILE_OVERWRITE_IF);
521 msg.access = smb_swap32(SMB_GENERIC_READ);
522 msg.create_disposition = smb_swap32(SMB_FILE_OPEN);
524 msg.byte_count = smb_swap16((unsigned short) ++byte_count);
525 strcpy(msg.bytes, req->path);
527 return smb_send_message(conn, SMB_COM_NT_CREATE_ANDX, &msg,
528 sizeof(msg) - sizeof(msg.bytes) + byte_count);
531 static CURLcode smb_send_close(struct connectdata *conn)
533 struct smb_request *req = conn->data->req.protop;
534 struct smb_close msg;
536 memset(&msg, 0, sizeof(msg));
537 msg.word_count = SMB_WC_CLOSE;
538 msg.fid = smb_swap16(req->fid);
540 return smb_send_message(conn, SMB_COM_CLOSE, &msg, sizeof(msg));
543 static CURLcode smb_send_tree_disconnect(struct connectdata *conn)
545 struct smb_tree_disconnect msg;
547 memset(&msg, 0, sizeof(msg));
549 return smb_send_message(conn, SMB_COM_TREE_DISCONNECT, &msg, sizeof(msg));
552 static CURLcode smb_send_read(struct connectdata *conn)
554 struct smb_request *req = conn->data->req.protop;
555 curl_off_t offset = conn->data->req.offset;
558 memset(&msg, 0, sizeof(msg));
559 msg.word_count = SMB_WC_READ_ANDX;
560 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
561 msg.fid = smb_swap16(req->fid);
562 msg.offset = smb_swap32((unsigned int) offset);
563 msg.offset_high = smb_swap32((unsigned int) (offset >> 32));
564 msg.min_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
565 msg.max_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
567 return smb_send_message(conn, SMB_COM_READ_ANDX, &msg, sizeof(msg));
570 static CURLcode smb_send_write(struct connectdata *conn)
572 struct smb_write *msg = (struct smb_write *)conn->data->state.uploadbuffer;
573 struct smb_request *req = conn->data->req.protop;
574 curl_off_t offset = conn->data->req.offset;
576 curl_off_t upload_size = conn->data->req.size - conn->data->req.bytecount;
577 if(upload_size >= MAX_PAYLOAD_SIZE - 1) /* There is one byte of padding */
578 upload_size = MAX_PAYLOAD_SIZE - 1;
580 memset(msg, 0, sizeof(*msg));
581 msg->word_count = SMB_WC_WRITE_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->data_length = smb_swap16((unsigned short) upload_size);
587 msg->data_offset = smb_swap16(sizeof(*msg) - sizeof(unsigned int));
588 msg->byte_count = smb_swap16((unsigned short) (upload_size + 1));
590 smb_format_message(conn, &msg->h, SMB_COM_WRITE_ANDX,
591 sizeof(*msg) - sizeof(msg->h) + (size_t) upload_size);
593 return smb_send(conn, sizeof(*msg), (size_t) upload_size);
596 static CURLcode smb_send_and_recv(struct connectdata *conn, void **msg)
598 struct smb_conn *smbc = &conn->proto.smbc;
601 /* Check if there is data in the transfer buffer */
602 if(!smbc->send_size && smbc->upload_size) {
603 int nread = smbc->upload_size > BUFSIZE ? BUFSIZE :
604 (int) smbc->upload_size;
605 conn->data->req.upload_fromhere = conn->data->state.uploadbuffer;
606 result = Curl_fillreadbuffer(conn, nread, &nread);
607 if(result && result != CURLE_AGAIN)
612 smbc->upload_size -= nread;
613 smbc->send_size = nread;
617 /* Check if there is data to send */
618 if(smbc->send_size) {
619 result = smb_flush(conn);
624 /* Check if there is still data to be sent */
625 if(smbc->send_size || smbc->upload_size)
628 return smb_recv_message(conn, msg);
631 static CURLcode smb_connection_state(struct connectdata *conn, bool *done)
633 struct smb_conn *smbc = &conn->proto.smbc;
634 struct smb_negotiate_response *nrsp;
635 struct smb_header *h;
639 if(smbc->state == SMB_CONNECTING) {
641 if((conn->handler->flags & PROTOPT_SSL)) {
643 result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &ssl_done);
644 if(result && result != CURLE_AGAIN)
651 result = smb_send_negotiate(conn);
653 connclose(conn, "SMB: failed to send negotiate message");
657 conn_state(conn, SMB_NEGOTIATE);
660 /* Send the previous message and check for a response */
661 result = smb_send_and_recv(conn, &msg);
662 if(result && result != CURLE_AGAIN) {
663 connclose(conn, "SMB: failed to communicate");
672 switch(smbc->state) {
675 connclose(conn, "SMB: negotiation failed");
676 return CURLE_COULDNT_CONNECT;
679 memcpy(smbc->challenge, nrsp->bytes, sizeof(smbc->challenge));
680 smbc->session_key = smb_swap32(nrsp->session_key);
681 result = smb_send_setup(conn);
683 connclose(conn, "SMB: failed to send setup message");
686 conn_state(conn, SMB_SETUP);
691 connclose(conn, "SMB: authentication failed");
692 return CURLE_LOGIN_DENIED;
694 smbc->uid = smb_swap16(h->uid);
695 conn_state(conn, SMB_CONNECTED);
700 smb_pop_message(conn);
701 return CURLE_OK; /* ignore */
704 smb_pop_message(conn);
709 static CURLcode smb_request_state(struct connectdata *conn, bool *done)
711 struct smb_request *req = conn->data->req.protop;
712 struct smb_header *h;
713 enum smb_req_state next_state = SMB_DONE;
719 /* Start the request */
720 if(req->state == SMB_REQUESTING) {
721 result = smb_send_tree_connect(conn);
723 connclose(conn, "SMB: failed to send tree connect message");
727 request_state(conn, SMB_TREE_CONNECT);
730 /* Send the previous message and check for a response */
731 result = smb_send_and_recv(conn, &msg);
732 if(result && result != CURLE_AGAIN) {
733 connclose(conn, "SMB: failed to communicate");
743 case SMB_TREE_CONNECT:
745 req->result = CURLE_REMOTE_FILE_NOT_FOUND;
746 if(h->status == smb_swap32(SMB_ERR_NOACCESS))
747 req->result = CURLE_REMOTE_ACCESS_DENIED;
750 req->tid = smb_swap16(h->tid);
751 next_state = SMB_OPEN;
756 req->result = CURLE_REMOTE_FILE_NOT_FOUND;
757 next_state = SMB_TREE_DISCONNECT;
760 req->fid = smb_swap16(((struct smb_nt_create_response *)msg)->fid);
761 conn->data->req.offset = 0;
762 if(conn->data->set.upload) {
763 conn->data->req.size = conn->data->state.infilesize;
764 Curl_pgrsSetUploadSize(conn->data, conn->data->req.size);
765 next_state = SMB_UPLOAD;
768 conn->data->req.size =
769 smb_swap64(((struct smb_nt_create_response *)msg)->end_of_file);
770 Curl_pgrsSetDownloadSize(conn->data, conn->data->req.size);
771 next_state = SMB_DOWNLOAD;
777 req->result = CURLE_RECV_ERROR;
778 next_state = SMB_CLOSE;
781 len = Curl_read16_le(((unsigned char *) msg) +
782 sizeof(struct smb_header) + 11);
783 off = Curl_read16_le(((unsigned char *) msg) +
784 sizeof(struct smb_header) + 13);
786 struct smb_conn *smbc = &conn->proto.smbc;
787 if(off + sizeof(unsigned int) + len > smbc->got) {
788 failf(conn->data, "Invalid input packet");
789 result = CURLE_RECV_ERROR;
792 result = Curl_client_write(conn, CLIENTWRITE_BODY,
793 (char *)msg + off + sizeof(unsigned int),
796 req->result = result;
797 next_state = SMB_CLOSE;
801 conn->data->req.bytecount += len;
802 conn->data->req.offset += len;
803 Curl_pgrsSetDownloadCounter(conn->data, conn->data->req.bytecount);
804 next_state = (len < MAX_PAYLOAD_SIZE) ? SMB_CLOSE : SMB_DOWNLOAD;
809 req->result = CURLE_UPLOAD_FAILED;
810 next_state = SMB_CLOSE;
813 len = Curl_read16_le(((unsigned char *) msg) +
814 sizeof(struct smb_header) + 5);
815 conn->data->req.bytecount += len;
816 conn->data->req.offset += len;
817 Curl_pgrsSetUploadCounter(conn->data, conn->data->req.bytecount);
818 if(conn->data->req.bytecount >= conn->data->req.size)
819 next_state = SMB_CLOSE;
821 next_state = SMB_UPLOAD;
825 /* We don't care if the close failed, proceed to tree disconnect anyway */
826 next_state = SMB_TREE_DISCONNECT;
829 case SMB_TREE_DISCONNECT:
830 next_state = SMB_DONE;
834 smb_pop_message(conn);
835 return CURLE_OK; /* ignore */
838 smb_pop_message(conn);
842 result = smb_send_open(conn);
846 result = smb_send_read(conn);
850 result = smb_send_write(conn);
854 result = smb_send_close(conn);
857 case SMB_TREE_DISCONNECT:
858 result = smb_send_tree_disconnect(conn);
862 result = req->result;
871 connclose(conn, "SMB: failed to send message");
875 request_state(conn, next_state);
880 static CURLcode smb_done(struct connectdata *conn, CURLcode status,
883 struct smb_request *req = conn->data->req.protop;
887 Curl_safefree(req->share);
888 Curl_safefree(conn->data->req.protop);
893 static CURLcode smb_disconnect(struct connectdata *conn, bool dead)
895 struct smb_conn *smbc = &conn->proto.smbc;
896 struct smb_request *req = conn->data->req.protop;
900 Curl_safefree(smbc->domain);
901 Curl_safefree(smbc->recv_buf);
903 /* smb_done is not always called, so cleanup the request */
905 Curl_safefree(req->share);
906 Curl_safefree(conn->data->req.protop);
912 static int smb_getsock(struct connectdata *conn, curl_socket_t *socks,
915 struct smb_conn *smbc = &conn->proto.smbc;
918 return GETSOCK_BLANK;
920 socks[0] = conn->sock[FIRSTSOCKET];
922 if(smbc->send_size || smbc->upload_size)
923 return GETSOCK_WRITESOCK(0);
925 return GETSOCK_READSOCK(0);
928 static CURLcode smb_parse_url_path(struct connectdata *conn)
930 CURLcode result = CURLE_OK;
931 struct SessionHandle *data = conn->data;
932 struct smb_request *req = data->req.protop;
936 /* URL decode the path */
937 result = Curl_urldecode(data, data->state.path, 0, &path, NULL, TRUE);
941 /* Parse the path for the share */
942 req->share = strdup((*path == '/' || *path == '\\') ? path + 1 : path);
946 return CURLE_OUT_OF_MEMORY;
949 slash = strchr(req->share, '/');
951 slash = strchr(req->share, '\\');
953 /* The share must be present */
957 return CURLE_URL_MALFORMAT;
960 /* Parse the path for the file path converting any forward slashes into
964 for(; *slash; slash++) {
974 #endif /* !USE_WINDOWS_SSPI || USE_WIN32_CRYPTO */
976 #endif /* CURL_DISABLE_SMB && USE_NTLM && CURL_SIZEOF_CURL_OFF_T > 4 */