VirtualBox

source: vbox/trunk/src/libs/curl-8.11.1/lib/smb.c@ 108333

最後變更 在這個檔案從108333是 108048,由 vboxsync 提交於 7 週 前

curl-8.11.1: Applied and adjusted our curl changes to 8.7.1. jiraref:VBP-1535

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

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