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