VirtualBox

source: vbox/trunk/src/libs/curl-7.83.1/lib/smb.c@ 97623

Last change on this file since 97623 was 95312, checked in by vboxsync, 3 years ago

libs/{curl,libxml2}: OSE export fixes, bugref:8515

  • Property svn:eol-style set to native
File size: 29.3 KB
Line 
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 2016 - 2022, Daniel Stenberg, <[email protected]>, et al.
9 * Copyright (C) 2014, Bill Nagel <[email protected]>, Exacq Technologies
10 *
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.
14 *
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.
18 *
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
21 *
22 ***************************************************************************/
23
24#include "curl_setup.h"
25
26#if !defined(CURL_DISABLE_SMB) && defined(USE_CURL_NTLM_CORE) && \
27 (SIZEOF_CURL_OFF_T > 4)
28
29#define BUILDING_CURL_SMB_C
30
31#ifdef HAVE_PROCESS_H
32#include <process.h>
33#ifdef CURL_WINDOWS_APP
34#define getpid GetCurrentProcessId
35#elif !defined(MSDOS)
36#define getpid _getpid
37#endif
38#endif
39
40#include "smb.h"
41#include "urldata.h"
42#include "sendf.h"
43#include "multiif.h"
44#include "connect.h"
45#include "progress.h"
46#include "transfer.h"
47#include "vtls/vtls.h"
48#include "curl_ntlm_core.h"
49#include "escape.h"
50#include "curl_endian.h"
51
52/* The last #include files should be: */
53#include "curl_memory.h"
54#include "memdebug.h"
55
56/* Local API functions */
57static CURLcode smb_setup_connection(struct Curl_easy *data,
58 struct connectdata *conn);
59static CURLcode smb_connect(struct Curl_easy *data, bool *done);
60static CURLcode smb_connection_state(struct Curl_easy *data, bool *done);
61static CURLcode smb_do(struct Curl_easy *data, bool *done);
62static CURLcode smb_request_state(struct Curl_easy *data, bool *done);
63static CURLcode smb_done(struct Curl_easy *data, CURLcode status,
64 bool premature);
65static CURLcode smb_disconnect(struct Curl_easy *data,
66 struct connectdata *conn, bool dead);
67static int smb_getsock(struct Curl_easy *data, struct connectdata *conn,
68 curl_socket_t *socks);
69static CURLcode smb_parse_url_path(struct Curl_easy *data,
70 struct connectdata *conn);
71
72/*
73 * SMB handler interface
74 */
75const struct Curl_handler Curl_handler_smb = {
76 "SMB", /* scheme */
77 smb_setup_connection, /* setup_connection */
78 smb_do, /* do_it */
79 smb_done, /* done */
80 ZERO_NULL, /* do_more */
81 smb_connect, /* connect_it */
82 smb_connection_state, /* connecting */
83 smb_request_state, /* doing */
84 smb_getsock, /* proto_getsock */
85 smb_getsock, /* doing_getsock */
86 ZERO_NULL, /* domore_getsock */
87 ZERO_NULL, /* perform_getsock */
88 smb_disconnect, /* disconnect */
89 ZERO_NULL, /* readwrite */
90 ZERO_NULL, /* connection_check */
91 ZERO_NULL, /* attach connection */
92 PORT_SMB, /* defport */
93 CURLPROTO_SMB, /* protocol */
94 CURLPROTO_SMB, /* family */
95 PROTOPT_NONE /* flags */
96};
97
98#ifdef USE_SSL
99/*
100 * SMBS handler interface
101 */
102const struct Curl_handler Curl_handler_smbs = {
103 "SMBS", /* scheme */
104 smb_setup_connection, /* setup_connection */
105 smb_do, /* do_it */
106 smb_done, /* done */
107 ZERO_NULL, /* do_more */
108 smb_connect, /* connect_it */
109 smb_connection_state, /* connecting */
110 smb_request_state, /* doing */
111 smb_getsock, /* proto_getsock */
112 smb_getsock, /* doing_getsock */
113 ZERO_NULL, /* domore_getsock */
114 ZERO_NULL, /* perform_getsock */
115 smb_disconnect, /* disconnect */
116 ZERO_NULL, /* readwrite */
117 ZERO_NULL, /* connection_check */
118 ZERO_NULL, /* attach connection */
119 PORT_SMBS, /* defport */
120 CURLPROTO_SMBS, /* protocol */
121 CURLPROTO_SMB, /* family */
122 PROTOPT_SSL /* flags */
123};
124#endif
125
126#define MAX_PAYLOAD_SIZE 0x8000
127#define MAX_MESSAGE_SIZE (MAX_PAYLOAD_SIZE + 0x1000)
128#define CLIENTNAME "curl"
129#define SERVICENAME "?????"
130
131/* Append a string to an SMB message */
132#define MSGCAT(str) \
133 do { \
134 strcpy(p, (str)); \
135 p += strlen(str); \
136 } while(0)
137
138/* Append a null-terminated string to an SMB message */
139#define MSGCATNULL(str) \
140 do { \
141 strcpy(p, (str)); \
142 p += strlen(str) + 1; \
143 } while(0)
144
145/* SMB is mostly little endian */
146#if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \
147 defined(__OS400__)
148static unsigned short smb_swap16(unsigned short x)
149{
150 return (unsigned short) ((x << 8) | ((x >> 8) & 0xff));
151}
152
153static unsigned int smb_swap32(unsigned int x)
154{
155 return (x << 24) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) |
156 ((x >> 24) & 0xff);
157}
158
159static curl_off_t smb_swap64(curl_off_t x)
160{
161 return ((curl_off_t) smb_swap32((unsigned int) x) << 32) |
162 smb_swap32((unsigned int) (x >> 32));
163}
164
165#else
166# define smb_swap16(x) (x)
167# define smb_swap32(x) (x)
168# define smb_swap64(x) (x)
169#endif
170
171/* SMB request state */
172enum smb_req_state {
173 SMB_REQUESTING,
174 SMB_TREE_CONNECT,
175 SMB_OPEN,
176 SMB_DOWNLOAD,
177 SMB_UPLOAD,
178 SMB_CLOSE,
179 SMB_TREE_DISCONNECT,
180 SMB_DONE
181};
182
183/* SMB request data */
184struct smb_request {
185 enum smb_req_state state;
186 char *path;
187 unsigned short tid; /* Even if we connect to the same tree as another */
188 unsigned short fid; /* request, the tid will be different */
189 CURLcode result;
190};
191
192static void conn_state(struct Curl_easy *data, enum smb_conn_state newstate)
193{
194 struct smb_conn *smbc = &data->conn->proto.smbc;
195#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
196 /* For debug purposes */
197 static const char * const names[] = {
198 "SMB_NOT_CONNECTED",
199 "SMB_CONNECTING",
200 "SMB_NEGOTIATE",
201 "SMB_SETUP",
202 "SMB_CONNECTED",
203 /* LAST */
204 };
205
206 if(smbc->state != newstate)
207 infof(data, "SMB conn %p state change from %s to %s",
208 (void *)smbc, names[smbc->state], names[newstate]);
209#endif
210
211 smbc->state = newstate;
212}
213
214static void request_state(struct Curl_easy *data,
215 enum smb_req_state newstate)
216{
217 struct smb_request *req = data->req.p.smb;
218#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
219 /* For debug purposes */
220 static const char * const names[] = {
221 "SMB_REQUESTING",
222 "SMB_TREE_CONNECT",
223 "SMB_OPEN",
224 "SMB_DOWNLOAD",
225 "SMB_UPLOAD",
226 "SMB_CLOSE",
227 "SMB_TREE_DISCONNECT",
228 "SMB_DONE",
229 /* LAST */
230 };
231
232 if(req->state != newstate)
233 infof(data, "SMB request %p state change from %s to %s",
234 (void *)req, names[req->state], names[newstate]);
235#endif
236
237 req->state = newstate;
238}
239
240/* this should setup things in the connection, not in the easy
241 handle */
242static CURLcode smb_setup_connection(struct Curl_easy *data,
243 struct connectdata *conn)
244{
245 struct smb_request *req;
246
247 /* Initialize the request state */
248 data->req.p.smb = req = calloc(1, sizeof(struct smb_request));
249 if(!req)
250 return CURLE_OUT_OF_MEMORY;
251
252 /* Parse the URL path */
253 return smb_parse_url_path(data, conn);
254}
255
256static CURLcode smb_connect(struct Curl_easy *data, bool *done)
257{
258 struct connectdata *conn = data->conn;
259 struct smb_conn *smbc = &conn->proto.smbc;
260 char *slash;
261
262 (void) done;
263
264 /* Check we have a username and password to authenticate with */
265 if(!data->state.aptr.user)
266 return CURLE_LOGIN_DENIED;
267
268 /* Initialize the connection state */
269 smbc->state = SMB_CONNECTING;
270 smbc->recv_buf = malloc(MAX_MESSAGE_SIZE);
271 if(!smbc->recv_buf)
272 return CURLE_OUT_OF_MEMORY;
273
274 /* Multiple requests are allowed with this connection */
275 connkeep(conn, "SMB default");
276
277 /* Parse the username, domain, and password */
278 slash = strchr(conn->user, '/');
279 if(!slash)
280 slash = strchr(conn->user, '\\');
281
282 if(slash) {
283 smbc->user = slash + 1;
284 smbc->domain = strdup(conn->user);
285 if(!smbc->domain)
286 return CURLE_OUT_OF_MEMORY;
287 smbc->domain[slash - conn->user] = 0;
288 }
289 else {
290 smbc->user = conn->user;
291 smbc->domain = strdup(conn->host.name);
292 if(!smbc->domain)
293 return CURLE_OUT_OF_MEMORY;
294 }
295
296 return CURLE_OK;
297}
298
299static CURLcode smb_recv_message(struct Curl_easy *data, void **msg)
300{
301 struct connectdata *conn = data->conn;
302 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
303 struct smb_conn *smbc = &conn->proto.smbc;
304 char *buf = smbc->recv_buf;
305 ssize_t bytes_read;
306 size_t nbt_size;
307 size_t msg_size;
308 size_t len = MAX_MESSAGE_SIZE - smbc->got;
309 CURLcode result;
310
311 result = Curl_read(data, sockfd, buf + smbc->got, len, &bytes_read);
312 if(result)
313 return result;
314
315 if(!bytes_read)
316 return CURLE_OK;
317
318 smbc->got += bytes_read;
319
320 /* Check for a 32-bit nbt header */
321 if(smbc->got < sizeof(unsigned int))
322 return CURLE_OK;
323
324 nbt_size = Curl_read16_be((const unsigned char *)
325 (buf + sizeof(unsigned short))) +
326 sizeof(unsigned int);
327 if(smbc->got < nbt_size)
328 return CURLE_OK;
329
330 msg_size = sizeof(struct smb_header);
331 if(nbt_size >= msg_size + 1) {
332 /* Add the word count */
333 msg_size += 1 + ((unsigned char) buf[msg_size]) * sizeof(unsigned short);
334 if(nbt_size >= msg_size + sizeof(unsigned short)) {
335 /* Add the byte count */
336 msg_size += sizeof(unsigned short) +
337 Curl_read16_le((const unsigned char *)&buf[msg_size]);
338 if(nbt_size < msg_size)
339 return CURLE_READ_ERROR;
340 }
341 }
342
343 *msg = buf;
344
345 return CURLE_OK;
346}
347
348static void smb_pop_message(struct connectdata *conn)
349{
350 struct smb_conn *smbc = &conn->proto.smbc;
351
352 smbc->got = 0;
353}
354
355static void smb_format_message(struct Curl_easy *data, struct smb_header *h,
356 unsigned char cmd, size_t len)
357{
358 struct connectdata *conn = data->conn;
359 struct smb_conn *smbc = &conn->proto.smbc;
360 struct smb_request *req = data->req.p.smb;
361 unsigned int pid;
362
363 memset(h, 0, sizeof(*h));
364 h->nbt_length = htons((unsigned short) (sizeof(*h) - sizeof(unsigned int) +
365 len));
366 memcpy((char *)h->magic, "\xffSMB", 4);
367 h->command = cmd;
368 h->flags = SMB_FLAGS_CANONICAL_PATHNAMES | SMB_FLAGS_CASELESS_PATHNAMES;
369 h->flags2 = smb_swap16(SMB_FLAGS2_IS_LONG_NAME | SMB_FLAGS2_KNOWS_LONG_NAME);
370 h->uid = smb_swap16(smbc->uid);
371 h->tid = smb_swap16(req->tid);
372 pid = getpid();
373 h->pid_high = smb_swap16((unsigned short)(pid >> 16));
374 h->pid = smb_swap16((unsigned short) pid);
375}
376
377static CURLcode smb_send(struct Curl_easy *data, ssize_t len,
378 size_t upload_size)
379{
380 struct connectdata *conn = data->conn;
381 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
382 struct smb_conn *smbc = &conn->proto.smbc;
383 ssize_t bytes_written;
384 CURLcode result;
385
386 result = Curl_write(data, sockfd, data->state.ulbuf,
387 len, &bytes_written);
388 if(result)
389 return result;
390
391 if(bytes_written != len) {
392 smbc->send_size = len;
393 smbc->sent = bytes_written;
394 }
395
396 smbc->upload_size = upload_size;
397
398 return CURLE_OK;
399}
400
401static CURLcode smb_flush(struct Curl_easy *data)
402{
403 struct connectdata *conn = data->conn;
404 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
405 struct smb_conn *smbc = &conn->proto.smbc;
406 ssize_t bytes_written;
407 ssize_t len = smbc->send_size - smbc->sent;
408 CURLcode result;
409
410 if(!smbc->send_size)
411 return CURLE_OK;
412
413 result = Curl_write(data, sockfd,
414 data->state.ulbuf + smbc->sent,
415 len, &bytes_written);
416 if(result)
417 return result;
418
419 if(bytes_written != len)
420 smbc->sent += bytes_written;
421 else
422 smbc->send_size = 0;
423
424 return CURLE_OK;
425}
426
427static CURLcode smb_send_message(struct Curl_easy *data, unsigned char cmd,
428 const void *msg, size_t msg_len)
429{
430 CURLcode result = Curl_get_upload_buffer(data);
431 if(result)
432 return result;
433 smb_format_message(data, (struct smb_header *)data->state.ulbuf,
434 cmd, msg_len);
435 memcpy(data->state.ulbuf + sizeof(struct smb_header),
436 msg, msg_len);
437
438 return smb_send(data, sizeof(struct smb_header) + msg_len, 0);
439}
440
441static CURLcode smb_send_negotiate(struct Curl_easy *data)
442{
443 const char *msg = "\x00\x0c\x00\x02NT LM 0.12";
444
445 return smb_send_message(data, SMB_COM_NEGOTIATE, msg, 15);
446}
447
448static CURLcode smb_send_setup(struct Curl_easy *data)
449{
450 struct connectdata *conn = data->conn;
451 struct smb_conn *smbc = &conn->proto.smbc;
452 struct smb_setup msg;
453 char *p = msg.bytes;
454 unsigned char lm_hash[21];
455 unsigned char lm[24];
456 unsigned char nt_hash[21];
457 unsigned char nt[24];
458
459 size_t byte_count = sizeof(lm) + sizeof(nt);
460 byte_count += strlen(smbc->user) + strlen(smbc->domain);
461 byte_count += strlen(OS) + strlen(CLIENTNAME) + 4; /* 4 null chars */
462 if(byte_count > sizeof(msg.bytes))
463 return CURLE_FILESIZE_EXCEEDED;
464
465 Curl_ntlm_core_mk_lm_hash(conn->passwd, lm_hash);
466 Curl_ntlm_core_lm_resp(lm_hash, smbc->challenge, lm);
467 Curl_ntlm_core_mk_nt_hash(conn->passwd, nt_hash);
468 Curl_ntlm_core_lm_resp(nt_hash, smbc->challenge, nt);
469
470 memset(&msg, 0, sizeof(msg));
471 msg.word_count = SMB_WC_SETUP_ANDX;
472 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
473 msg.max_buffer_size = smb_swap16(MAX_MESSAGE_SIZE);
474 msg.max_mpx_count = smb_swap16(1);
475 msg.vc_number = smb_swap16(1);
476 msg.session_key = smb_swap32(smbc->session_key);
477 msg.capabilities = smb_swap32(SMB_CAP_LARGE_FILES);
478 msg.lengths[0] = smb_swap16(sizeof(lm));
479 msg.lengths[1] = smb_swap16(sizeof(nt));
480 memcpy(p, lm, sizeof(lm));
481 p += sizeof(lm);
482 memcpy(p, nt, sizeof(nt));
483 p += sizeof(nt);
484 MSGCATNULL(smbc->user);
485 MSGCATNULL(smbc->domain);
486 MSGCATNULL(OS);
487 MSGCATNULL(CLIENTNAME);
488 byte_count = p - msg.bytes;
489 msg.byte_count = smb_swap16((unsigned short)byte_count);
490
491 return smb_send_message(data, SMB_COM_SETUP_ANDX, &msg,
492 sizeof(msg) - sizeof(msg.bytes) + byte_count);
493}
494
495static CURLcode smb_send_tree_connect(struct Curl_easy *data)
496{
497 struct smb_tree_connect msg;
498 struct connectdata *conn = data->conn;
499 struct smb_conn *smbc = &conn->proto.smbc;
500 char *p = msg.bytes;
501
502 size_t byte_count = strlen(conn->host.name) + strlen(smbc->share);
503 byte_count += strlen(SERVICENAME) + 5; /* 2 nulls and 3 backslashes */
504 if(byte_count > sizeof(msg.bytes))
505 return CURLE_FILESIZE_EXCEEDED;
506
507 memset(&msg, 0, sizeof(msg));
508 msg.word_count = SMB_WC_TREE_CONNECT_ANDX;
509 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
510 msg.pw_len = 0;
511 MSGCAT("\\\\");
512 MSGCAT(conn->host.name);
513 MSGCAT("\\");
514 MSGCATNULL(smbc->share);
515 MSGCATNULL(SERVICENAME); /* Match any type of service */
516 byte_count = p - msg.bytes;
517 msg.byte_count = smb_swap16((unsigned short)byte_count);
518
519 return smb_send_message(data, SMB_COM_TREE_CONNECT_ANDX, &msg,
520 sizeof(msg) - sizeof(msg.bytes) + byte_count);
521}
522
523static CURLcode smb_send_open(struct Curl_easy *data)
524{
525 struct smb_request *req = data->req.p.smb;
526 struct smb_nt_create msg;
527 size_t byte_count;
528
529 if((strlen(req->path) + 1) > sizeof(msg.bytes))
530 return CURLE_FILESIZE_EXCEEDED;
531
532 memset(&msg, 0, sizeof(msg));
533 msg.word_count = SMB_WC_NT_CREATE_ANDX;
534 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
535 byte_count = strlen(req->path);
536 msg.name_length = smb_swap16((unsigned short)byte_count);
537 msg.share_access = smb_swap32(SMB_FILE_SHARE_ALL);
538 if(data->set.upload) {
539 msg.access = smb_swap32(SMB_GENERIC_READ | SMB_GENERIC_WRITE);
540 msg.create_disposition = smb_swap32(SMB_FILE_OVERWRITE_IF);
541 }
542 else {
543 msg.access = smb_swap32(SMB_GENERIC_READ);
544 msg.create_disposition = smb_swap32(SMB_FILE_OPEN);
545 }
546 msg.byte_count = smb_swap16((unsigned short) ++byte_count);
547 strcpy(msg.bytes, req->path);
548
549 return smb_send_message(data, SMB_COM_NT_CREATE_ANDX, &msg,
550 sizeof(msg) - sizeof(msg.bytes) + byte_count);
551}
552
553static CURLcode smb_send_close(struct Curl_easy *data)
554{
555 struct smb_request *req = data->req.p.smb;
556 struct smb_close msg;
557
558 memset(&msg, 0, sizeof(msg));
559 msg.word_count = SMB_WC_CLOSE;
560 msg.fid = smb_swap16(req->fid);
561
562 return smb_send_message(data, SMB_COM_CLOSE, &msg, sizeof(msg));
563}
564
565static CURLcode smb_send_tree_disconnect(struct Curl_easy *data)
566{
567 struct smb_tree_disconnect msg;
568
569 memset(&msg, 0, sizeof(msg));
570
571 return smb_send_message(data, SMB_COM_TREE_DISCONNECT, &msg, sizeof(msg));
572}
573
574static CURLcode smb_send_read(struct Curl_easy *data)
575{
576 struct smb_request *req = data->req.p.smb;
577 curl_off_t offset = data->req.offset;
578 struct smb_read msg;
579
580 memset(&msg, 0, sizeof(msg));
581 msg.word_count = SMB_WC_READ_ANDX;
582 msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
583 msg.fid = smb_swap16(req->fid);
584 msg.offset = smb_swap32((unsigned int) offset);
585 msg.offset_high = smb_swap32((unsigned int) (offset >> 32));
586 msg.min_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
587 msg.max_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
588
589 return smb_send_message(data, SMB_COM_READ_ANDX, &msg, sizeof(msg));
590}
591
592static CURLcode smb_send_write(struct Curl_easy *data)
593{
594 struct smb_write *msg;
595 struct smb_request *req = data->req.p.smb;
596 curl_off_t offset = data->req.offset;
597 curl_off_t upload_size = data->req.size - data->req.bytecount;
598 CURLcode result = Curl_get_upload_buffer(data);
599 if(result)
600 return result;
601 msg = (struct smb_write *)data->state.ulbuf;
602
603 if(upload_size >= MAX_PAYLOAD_SIZE - 1) /* There is one byte of padding */
604 upload_size = MAX_PAYLOAD_SIZE - 1;
605
606 memset(msg, 0, sizeof(*msg));
607 msg->word_count = SMB_WC_WRITE_ANDX;
608 msg->andx.command = SMB_COM_NO_ANDX_COMMAND;
609 msg->fid = smb_swap16(req->fid);
610 msg->offset = smb_swap32((unsigned int) offset);
611 msg->offset_high = smb_swap32((unsigned int) (offset >> 32));
612 msg->data_length = smb_swap16((unsigned short) upload_size);
613 msg->data_offset = smb_swap16(sizeof(*msg) - sizeof(unsigned int));
614 msg->byte_count = smb_swap16((unsigned short) (upload_size + 1));
615
616 smb_format_message(data, &msg->h, SMB_COM_WRITE_ANDX,
617 sizeof(*msg) - sizeof(msg->h) + (size_t) upload_size);
618
619 return smb_send(data, sizeof(*msg), (size_t) upload_size);
620}
621
622static CURLcode smb_send_and_recv(struct Curl_easy *data, void **msg)
623{
624 struct connectdata *conn = data->conn;
625 struct smb_conn *smbc = &conn->proto.smbc;
626 CURLcode result;
627 *msg = NULL; /* if it returns early */
628
629 /* Check if there is data in the transfer buffer */
630 if(!smbc->send_size && smbc->upload_size) {
631 size_t nread = smbc->upload_size > (size_t)data->set.upload_buffer_size ?
632 (size_t)data->set.upload_buffer_size : smbc->upload_size;
633 data->req.upload_fromhere = data->state.ulbuf;
634 result = Curl_fillreadbuffer(data, nread, &nread);
635 if(result && result != CURLE_AGAIN)
636 return result;
637 if(!nread)
638 return CURLE_OK;
639
640 smbc->upload_size -= nread;
641 smbc->send_size = nread;
642 smbc->sent = 0;
643 }
644
645 /* Check if there is data to send */
646 if(smbc->send_size) {
647 result = smb_flush(data);
648 if(result)
649 return result;
650 }
651
652 /* Check if there is still data to be sent */
653 if(smbc->send_size || smbc->upload_size)
654 return CURLE_AGAIN;
655
656 return smb_recv_message(data, msg);
657}
658
659static CURLcode smb_connection_state(struct Curl_easy *data, bool *done)
660{
661 struct connectdata *conn = data->conn;
662 struct smb_conn *smbc = &conn->proto.smbc;
663 struct smb_negotiate_response *nrsp;
664 struct smb_header *h;
665 CURLcode result;
666 void *msg = NULL;
667
668 if(smbc->state == SMB_CONNECTING) {
669#ifdef USE_SSL
670 if((conn->handler->flags & PROTOPT_SSL)) {
671 bool ssl_done = FALSE;
672 result = Curl_ssl_connect_nonblocking(data, conn, FALSE,
673 FIRSTSOCKET, &ssl_done);
674 if(result && result != CURLE_AGAIN)
675 return result;
676 if(!ssl_done)
677 return CURLE_OK;
678 }
679#endif
680
681 result = smb_send_negotiate(data);
682 if(result) {
683 connclose(conn, "SMB: failed to send negotiate message");
684 return result;
685 }
686
687 conn_state(data, SMB_NEGOTIATE);
688 }
689
690 /* Send the previous message and check for a response */
691 result = smb_send_and_recv(data, &msg);
692 if(result && result != CURLE_AGAIN) {
693 connclose(conn, "SMB: failed to communicate");
694 return result;
695 }
696
697 if(!msg)
698 return CURLE_OK;
699
700 h = msg;
701
702 switch(smbc->state) {
703 case SMB_NEGOTIATE:
704 if((smbc->got < sizeof(*nrsp) + sizeof(smbc->challenge) - 1) ||
705 h->status) {
706 connclose(conn, "SMB: negotiation failed");
707 return CURLE_COULDNT_CONNECT;
708 }
709 nrsp = msg;
710 memcpy(smbc->challenge, nrsp->bytes, sizeof(smbc->challenge));
711 smbc->session_key = smb_swap32(nrsp->session_key);
712 result = smb_send_setup(data);
713 if(result) {
714 connclose(conn, "SMB: failed to send setup message");
715 return result;
716 }
717 conn_state(data, SMB_SETUP);
718 break;
719
720 case SMB_SETUP:
721 if(h->status) {
722 connclose(conn, "SMB: authentication failed");
723 return CURLE_LOGIN_DENIED;
724 }
725 smbc->uid = smb_swap16(h->uid);
726 conn_state(data, SMB_CONNECTED);
727 *done = true;
728 break;
729
730 default:
731 smb_pop_message(conn);
732 return CURLE_OK; /* ignore */
733 }
734
735 smb_pop_message(conn);
736
737 return CURLE_OK;
738}
739
740/*
741 * Convert a timestamp from the Windows world (100 nsec units from 1 Jan 1601)
742 * to Posix time. Cap the output to fit within a time_t.
743 */
744static void get_posix_time(time_t *out, curl_off_t timestamp)
745{
746 timestamp -= 116444736000000000;
747 timestamp /= 10000000;
748#if SIZEOF_TIME_T < SIZEOF_CURL_OFF_T
749 if(timestamp > TIME_T_MAX)
750 *out = TIME_T_MAX;
751 else if(timestamp < TIME_T_MIN)
752 *out = TIME_T_MIN;
753 else
754#endif
755 *out = (time_t) timestamp;
756}
757
758static CURLcode smb_request_state(struct Curl_easy *data, bool *done)
759{
760 struct connectdata *conn = data->conn;
761 struct smb_request *req = data->req.p.smb;
762 struct smb_header *h;
763 struct smb_conn *smbc = &conn->proto.smbc;
764 enum smb_req_state next_state = SMB_DONE;
765 unsigned short len;
766 unsigned short off;
767 CURLcode result;
768 void *msg = NULL;
769 const struct smb_nt_create_response *smb_m;
770
771 /* Start the request */
772 if(req->state == SMB_REQUESTING) {
773 result = smb_send_tree_connect(data);
774 if(result) {
775 connclose(conn, "SMB: failed to send tree connect message");
776 return result;
777 }
778
779 request_state(data, SMB_TREE_CONNECT);
780 }
781
782 /* Send the previous message and check for a response */
783 result = smb_send_and_recv(data, &msg);
784 if(result && result != CURLE_AGAIN) {
785 connclose(conn, "SMB: failed to communicate");
786 return result;
787 }
788
789 if(!msg)
790 return CURLE_OK;
791
792 h = msg;
793
794 switch(req->state) {
795 case SMB_TREE_CONNECT:
796 if(h->status) {
797 req->result = CURLE_REMOTE_FILE_NOT_FOUND;
798 if(h->status == smb_swap32(SMB_ERR_NOACCESS))
799 req->result = CURLE_REMOTE_ACCESS_DENIED;
800 break;
801 }
802 req->tid = smb_swap16(h->tid);
803 next_state = SMB_OPEN;
804 break;
805
806 case SMB_OPEN:
807 if(h->status || smbc->got < sizeof(struct smb_nt_create_response)) {
808 req->result = CURLE_REMOTE_FILE_NOT_FOUND;
809 if(h->status == smb_swap32(SMB_ERR_NOACCESS))
810 req->result = CURLE_REMOTE_ACCESS_DENIED;
811 next_state = SMB_TREE_DISCONNECT;
812 break;
813 }
814 smb_m = (const struct smb_nt_create_response*) msg;
815 req->fid = smb_swap16(smb_m->fid);
816 data->req.offset = 0;
817 if(data->set.upload) {
818 data->req.size = data->state.infilesize;
819 Curl_pgrsSetUploadSize(data, data->req.size);
820 next_state = SMB_UPLOAD;
821 }
822 else {
823 smb_m = (const struct smb_nt_create_response*) msg;
824 data->req.size = smb_swap64(smb_m->end_of_file);
825 if(data->req.size < 0) {
826 req->result = CURLE_WEIRD_SERVER_REPLY;
827 next_state = SMB_CLOSE;
828 }
829 else {
830 Curl_pgrsSetDownloadSize(data, data->req.size);
831 if(data->set.get_filetime)
832 get_posix_time(&data->info.filetime, smb_m->last_change_time);
833 next_state = SMB_DOWNLOAD;
834 }
835 }
836 break;
837
838 case SMB_DOWNLOAD:
839 if(h->status || smbc->got < sizeof(struct smb_header) + 14) {
840 req->result = CURLE_RECV_ERROR;
841 next_state = SMB_CLOSE;
842 break;
843 }
844 len = Curl_read16_le(((const unsigned char *) msg) +
845 sizeof(struct smb_header) + 11);
846 off = Curl_read16_le(((const unsigned char *) msg) +
847 sizeof(struct smb_header) + 13);
848 if(len > 0) {
849 if(off + sizeof(unsigned int) + len > smbc->got) {
850 failf(data, "Invalid input packet");
851 result = CURLE_RECV_ERROR;
852 }
853 else
854 result = Curl_client_write(data, CLIENTWRITE_BODY,
855 (char *)msg + off + sizeof(unsigned int),
856 len);
857 if(result) {
858 req->result = result;
859 next_state = SMB_CLOSE;
860 break;
861 }
862 }
863 data->req.bytecount += len;
864 data->req.offset += len;
865 Curl_pgrsSetDownloadCounter(data, data->req.bytecount);
866 next_state = (len < MAX_PAYLOAD_SIZE) ? SMB_CLOSE : SMB_DOWNLOAD;
867 break;
868
869 case SMB_UPLOAD:
870 if(h->status || smbc->got < sizeof(struct smb_header) + 6) {
871 req->result = CURLE_UPLOAD_FAILED;
872 next_state = SMB_CLOSE;
873 break;
874 }
875 len = Curl_read16_le(((const unsigned char *) msg) +
876 sizeof(struct smb_header) + 5);
877 data->req.bytecount += len;
878 data->req.offset += len;
879 Curl_pgrsSetUploadCounter(data, data->req.bytecount);
880 if(data->req.bytecount >= data->req.size)
881 next_state = SMB_CLOSE;
882 else
883 next_state = SMB_UPLOAD;
884 break;
885
886 case SMB_CLOSE:
887 /* We don't care if the close failed, proceed to tree disconnect anyway */
888 next_state = SMB_TREE_DISCONNECT;
889 break;
890
891 case SMB_TREE_DISCONNECT:
892 next_state = SMB_DONE;
893 break;
894
895 default:
896 smb_pop_message(conn);
897 return CURLE_OK; /* ignore */
898 }
899
900 smb_pop_message(conn);
901
902 switch(next_state) {
903 case SMB_OPEN:
904 result = smb_send_open(data);
905 break;
906
907 case SMB_DOWNLOAD:
908 result = smb_send_read(data);
909 break;
910
911 case SMB_UPLOAD:
912 result = smb_send_write(data);
913 break;
914
915 case SMB_CLOSE:
916 result = smb_send_close(data);
917 break;
918
919 case SMB_TREE_DISCONNECT:
920 result = smb_send_tree_disconnect(data);
921 break;
922
923 case SMB_DONE:
924 result = req->result;
925 *done = true;
926 break;
927
928 default:
929 break;
930 }
931
932 if(result) {
933 connclose(conn, "SMB: failed to send message");
934 return result;
935 }
936
937 request_state(data, next_state);
938
939 return CURLE_OK;
940}
941
942static CURLcode smb_done(struct Curl_easy *data, CURLcode status,
943 bool premature)
944{
945 (void) premature;
946 Curl_safefree(data->req.p.smb);
947 return status;
948}
949
950static CURLcode smb_disconnect(struct Curl_easy *data,
951 struct connectdata *conn, bool dead)
952{
953 struct smb_conn *smbc = &conn->proto.smbc;
954 (void) dead;
955 (void) data;
956 Curl_safefree(smbc->share);
957 Curl_safefree(smbc->domain);
958 Curl_safefree(smbc->recv_buf);
959 return CURLE_OK;
960}
961
962static int smb_getsock(struct Curl_easy *data,
963 struct connectdata *conn, curl_socket_t *socks)
964{
965 (void)data;
966 socks[0] = conn->sock[FIRSTSOCKET];
967 return GETSOCK_READSOCK(0) | GETSOCK_WRITESOCK(0);
968}
969
970static CURLcode smb_do(struct Curl_easy *data, bool *done)
971{
972 struct connectdata *conn = data->conn;
973 struct smb_conn *smbc = &conn->proto.smbc;
974
975 *done = FALSE;
976 if(smbc->share) {
977 return CURLE_OK;
978 }
979 return CURLE_URL_MALFORMAT;
980}
981
982static CURLcode smb_parse_url_path(struct Curl_easy *data,
983 struct connectdata *conn)
984{
985 struct smb_request *req = data->req.p.smb;
986 struct smb_conn *smbc = &conn->proto.smbc;
987 char *path;
988 char *slash;
989
990 /* URL decode the path */
991 CURLcode result = Curl_urldecode(data->state.up.path, 0, &path, NULL,
992 REJECT_CTRL);
993 if(result)
994 return result;
995
996 /* Parse the path for the share */
997 smbc->share = strdup((*path == '/' || *path == '\\') ? path + 1 : path);
998 free(path);
999 if(!smbc->share)
1000 return CURLE_OUT_OF_MEMORY;
1001
1002 slash = strchr(smbc->share, '/');
1003 if(!slash)
1004 slash = strchr(smbc->share, '\\');
1005
1006 /* The share must be present */
1007 if(!slash) {
1008 Curl_safefree(smbc->share);
1009 return CURLE_URL_MALFORMAT;
1010 }
1011
1012 /* Parse the path for the file path converting any forward slashes into
1013 backslashes */
1014 *slash++ = 0;
1015 req->path = slash;
1016
1017 for(; *slash; slash++) {
1018 if(*slash == '/')
1019 *slash = '\\';
1020 }
1021 return CURLE_OK;
1022}
1023
1024#endif /* CURL_DISABLE_SMB && USE_CURL_NTLM_CORE &&
1025 SIZEOF_CURL_OFF_T > 4 */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette