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 | | |
27 | | #if !defined(CURL_DISABLE_SMB) && defined(USE_CURL_NTLM_CORE) |
28 | | |
29 | | #include "smb.h" |
30 | | #include "urldata.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(CURL_DISABLE_VERBOSE_STRINGS) |
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 | 0 | #endif |
337 | 0 | (void)data; |
338 | 0 | smbc->state = newstate; |
339 | 0 | } |
340 | | |
341 | | static void request_state(struct Curl_easy *data, |
342 | | enum smb_req_state newstate) |
343 | 0 | { |
344 | 0 | struct smb_request *req = Curl_meta_get(data, CURL_META_SMB_EASY); |
345 | 0 | if(req) { |
346 | 0 | #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS) |
347 | | /* For debug purposes */ |
348 | 0 | static const char * const names[] = { |
349 | 0 | "SMB_REQUESTING", |
350 | 0 | "SMB_TREE_CONNECT", |
351 | 0 | "SMB_OPEN", |
352 | 0 | "SMB_DOWNLOAD", |
353 | 0 | "SMB_UPLOAD", |
354 | 0 | "SMB_CLOSE", |
355 | 0 | "SMB_TREE_DISCONNECT", |
356 | 0 | "SMB_DONE", |
357 | | /* LAST */ |
358 | 0 | }; |
359 | |
|
360 | 0 | if(req->state != newstate) |
361 | 0 | infof(data, "SMB request %p state change from %s to %s", |
362 | 0 | (void *)req, names[req->state], names[newstate]); |
363 | 0 | #endif |
364 | |
|
365 | 0 | req->state = newstate; |
366 | 0 | } |
367 | 0 | } |
368 | | |
369 | | static void smb_easy_dtor(void *key, size_t klen, void *entry) |
370 | 0 | { |
371 | 0 | struct smb_request *req = entry; |
372 | 0 | (void)key; |
373 | 0 | (void)klen; |
374 | | /* `req->path` points to somewhere in `struct smb_conn` which is |
375 | | * kept at the connection meta. If the connection is destroyed first, |
376 | | * req->path points to free'd memory. */ |
377 | 0 | curlx_free(req); |
378 | 0 | } |
379 | | |
380 | | static void smb_conn_dtor(void *key, size_t klen, void *entry) |
381 | 0 | { |
382 | 0 | struct smb_conn *smbc = entry; |
383 | 0 | (void)key; |
384 | 0 | (void)klen; |
385 | 0 | Curl_safefree(smbc->share); |
386 | 0 | Curl_safefree(smbc->domain); |
387 | 0 | Curl_safefree(smbc->recv_buf); |
388 | 0 | Curl_safefree(smbc->send_buf); |
389 | 0 | curlx_free(smbc); |
390 | 0 | } |
391 | | |
392 | | static CURLcode smb_parse_url_path(struct Curl_easy *data, |
393 | | struct smb_conn *smbc, |
394 | | struct smb_request *req) |
395 | 0 | { |
396 | 0 | char *path; |
397 | 0 | char *slash; |
398 | 0 | CURLcode result; |
399 | | |
400 | | /* URL decode the path */ |
401 | 0 | result = Curl_urldecode(data->state.up.path, 0, &path, NULL, REJECT_CTRL); |
402 | 0 | if(result) |
403 | 0 | return result; |
404 | | |
405 | | /* Parse the path for the share */ |
406 | 0 | smbc->share = curlx_strdup((*path == '/' || *path == '\\') |
407 | 0 | ? path + 1 : path); |
408 | 0 | curlx_free(path); |
409 | 0 | if(!smbc->share) |
410 | 0 | return CURLE_OUT_OF_MEMORY; |
411 | | |
412 | 0 | slash = strchr(smbc->share, '/'); |
413 | 0 | if(!slash) |
414 | 0 | slash = strchr(smbc->share, '\\'); |
415 | | |
416 | | /* The share must be present */ |
417 | 0 | if(!slash) { |
418 | 0 | Curl_safefree(smbc->share); |
419 | 0 | failf(data, "missing share in URL path for SMB"); |
420 | 0 | return CURLE_URL_MALFORMAT; |
421 | 0 | } |
422 | | |
423 | | /* Parse the path for the file path converting any forward slashes into |
424 | | backslashes */ |
425 | 0 | *slash++ = 0; |
426 | 0 | req->path = slash; |
427 | |
|
428 | 0 | for(; *slash; slash++) { |
429 | 0 | if(*slash == '/') |
430 | 0 | *slash = '\\'; |
431 | 0 | } |
432 | 0 | return CURLE_OK; |
433 | 0 | } |
434 | | |
435 | | /* this should setup things in the connection, not in the easy |
436 | | handle */ |
437 | | static CURLcode smb_setup_connection(struct Curl_easy *data, |
438 | | struct connectdata *conn) |
439 | 0 | { |
440 | 0 | struct smb_conn *smbc; |
441 | 0 | struct smb_request *req; |
442 | | |
443 | | /* Initialize the connection state */ |
444 | 0 | smbc = curlx_calloc(1, sizeof(*smbc)); |
445 | 0 | if(!smbc || |
446 | 0 | Curl_conn_meta_set(conn, CURL_META_SMB_CONN, smbc, smb_conn_dtor)) |
447 | 0 | return CURLE_OUT_OF_MEMORY; |
448 | | |
449 | | /* Initialize the request state */ |
450 | 0 | req = curlx_calloc(1, sizeof(*req)); |
451 | 0 | if(!req || |
452 | 0 | Curl_meta_set(data, CURL_META_SMB_EASY, req, smb_easy_dtor)) |
453 | 0 | return CURLE_OUT_OF_MEMORY; |
454 | | |
455 | | /* Parse the URL path */ |
456 | 0 | return smb_parse_url_path(data, smbc, req); |
457 | 0 | } |
458 | | |
459 | | static CURLcode smb_connect(struct Curl_easy *data, bool *done) |
460 | 0 | { |
461 | 0 | struct connectdata *conn = data->conn; |
462 | 0 | struct smb_conn *smbc = Curl_conn_meta_get(conn, CURL_META_SMB_CONN); |
463 | 0 | char *slash; |
464 | |
|
465 | 0 | (void)done; |
466 | 0 | if(!smbc) |
467 | 0 | return CURLE_FAILED_INIT; |
468 | | |
469 | | /* Check we have a username and password to authenticate with */ |
470 | 0 | if(!data->state.aptr.user) |
471 | 0 | return CURLE_LOGIN_DENIED; |
472 | | |
473 | | /* Initialize the connection state */ |
474 | 0 | smbc->state = SMB_CONNECTING; |
475 | 0 | smbc->recv_buf = curlx_malloc(MAX_MESSAGE_SIZE); |
476 | 0 | if(!smbc->recv_buf) |
477 | 0 | return CURLE_OUT_OF_MEMORY; |
478 | 0 | smbc->send_buf = curlx_malloc(MAX_MESSAGE_SIZE); |
479 | 0 | if(!smbc->send_buf) |
480 | 0 | return CURLE_OUT_OF_MEMORY; |
481 | | |
482 | | /* Parse the username, domain, and password */ |
483 | 0 | slash = strchr(conn->user, '/'); |
484 | 0 | if(!slash) |
485 | 0 | slash = strchr(conn->user, '\\'); |
486 | |
|
487 | 0 | if(slash) { |
488 | 0 | smbc->user = slash + 1; |
489 | 0 | smbc->domain = curlx_strdup(conn->user); |
490 | 0 | if(!smbc->domain) |
491 | 0 | return CURLE_OUT_OF_MEMORY; |
492 | 0 | smbc->domain[slash - conn->user] = 0; |
493 | 0 | } |
494 | 0 | else { |
495 | 0 | smbc->user = conn->user; |
496 | 0 | smbc->domain = curlx_strdup(conn->host.name); |
497 | 0 | if(!smbc->domain) |
498 | 0 | return CURLE_OUT_OF_MEMORY; |
499 | 0 | } |
500 | | |
501 | 0 | return CURLE_OK; |
502 | 0 | } |
503 | | |
504 | | static CURLcode smb_recv_message(struct Curl_easy *data, |
505 | | struct smb_conn *smbc, |
506 | | void **msg) |
507 | 0 | { |
508 | 0 | char *buf = smbc->recv_buf; |
509 | 0 | size_t bytes_read; |
510 | 0 | size_t nbt_size; |
511 | 0 | size_t msg_size = sizeof(struct smb_header); |
512 | 0 | size_t len = MAX_MESSAGE_SIZE - smbc->got; |
513 | 0 | CURLcode result; |
514 | |
|
515 | 0 | result = Curl_xfer_recv(data, buf + smbc->got, len, &bytes_read); |
516 | 0 | if(result) |
517 | 0 | return result; |
518 | | |
519 | 0 | if(!bytes_read) |
520 | 0 | return CURLE_OK; |
521 | | |
522 | 0 | smbc->got += bytes_read; |
523 | | |
524 | | /* Check for a 32-bit nbt header */ |
525 | 0 | if(smbc->got < sizeof(unsigned int)) |
526 | 0 | return CURLE_OK; |
527 | | |
528 | 0 | nbt_size = Curl_read16_be((const unsigned char *) |
529 | 0 | (buf + sizeof(unsigned short))) + |
530 | 0 | sizeof(unsigned int); |
531 | 0 | if(nbt_size > MAX_MESSAGE_SIZE) { |
532 | 0 | failf(data, "too large NetBIOS frame size %zu", nbt_size); |
533 | 0 | return CURLE_RECV_ERROR; |
534 | 0 | } |
535 | 0 | else if(nbt_size < msg_size) { |
536 | | /* Each SMB message must be at least this large, e.g. 32 bytes */ |
537 | 0 | failf(data, "too small NetBIOS frame size %zu", nbt_size); |
538 | 0 | return CURLE_RECV_ERROR; |
539 | 0 | } |
540 | | |
541 | 0 | if(smbc->got < nbt_size) |
542 | 0 | return CURLE_OK; |
543 | | |
544 | 0 | if(nbt_size >= msg_size + 1) { |
545 | | /* Add the word count */ |
546 | 0 | msg_size += 1 + ((unsigned char)buf[msg_size]) * sizeof(unsigned short); |
547 | 0 | if(nbt_size >= msg_size + sizeof(unsigned short)) { |
548 | | /* Add the byte count */ |
549 | 0 | msg_size += sizeof(unsigned short) + |
550 | 0 | Curl_read16_le((const unsigned char *)&buf[msg_size]); |
551 | 0 | if(nbt_size < msg_size) |
552 | 0 | return CURLE_RECV_ERROR; |
553 | 0 | } |
554 | 0 | } |
555 | | |
556 | 0 | *msg = buf; |
557 | |
|
558 | 0 | return CURLE_OK; |
559 | 0 | } |
560 | | |
561 | | static void smb_pop_message(struct smb_conn *smbc) |
562 | 0 | { |
563 | 0 | smbc->got = 0; |
564 | 0 | } |
565 | | |
566 | | static void smb_format_message(struct smb_conn *smbc, |
567 | | struct smb_request *req, |
568 | | struct smb_header *h, |
569 | | unsigned char cmd, size_t len) |
570 | 0 | { |
571 | 0 | const unsigned int pid = 0xbad71d; /* made up */ |
572 | |
|
573 | 0 | memset(h, 0, sizeof(*h)); |
574 | 0 | h->nbt_length = htons((unsigned short)(sizeof(*h) - sizeof(unsigned int) + |
575 | 0 | len)); |
576 | 0 | memcpy((char *)h->magic, "\xffSMB", 4); |
577 | 0 | h->command = cmd; |
578 | 0 | h->flags = SMB_FLAGS_CANONICAL_PATHNAMES | SMB_FLAGS_CASELESS_PATHNAMES; |
579 | 0 | h->flags2 = smb_swap16(SMB_FLAGS2_IS_LONG_NAME | SMB_FLAGS2_KNOWS_LONG_NAME); |
580 | 0 | h->uid = smb_swap16(smbc->uid); |
581 | 0 | h->tid = smb_swap16(req->tid); |
582 | 0 | h->pid_high = smb_swap16((unsigned short)(pid >> 16)); |
583 | 0 | h->pid = smb_swap16((unsigned short)pid); |
584 | 0 | } |
585 | | |
586 | | static CURLcode smb_send(struct Curl_easy *data, struct smb_conn *smbc, |
587 | | size_t len, size_t upload_size) |
588 | 0 | { |
589 | 0 | size_t bytes_written; |
590 | 0 | CURLcode result; |
591 | |
|
592 | 0 | result = Curl_xfer_send(data, smbc->send_buf, len, FALSE, &bytes_written); |
593 | 0 | if(result) |
594 | 0 | return result; |
595 | | |
596 | 0 | if(bytes_written != len) { |
597 | 0 | smbc->send_size = len; |
598 | 0 | smbc->sent = bytes_written; |
599 | 0 | } |
600 | |
|
601 | 0 | smbc->upload_size = upload_size; |
602 | |
|
603 | 0 | return CURLE_OK; |
604 | 0 | } |
605 | | |
606 | | static CURLcode smb_flush(struct Curl_easy *data, struct smb_conn *smbc) |
607 | 0 | { |
608 | 0 | size_t bytes_written; |
609 | 0 | size_t len = smbc->send_size - smbc->sent; |
610 | 0 | CURLcode result; |
611 | |
|
612 | 0 | if(!smbc->send_size) |
613 | 0 | return CURLE_OK; |
614 | | |
615 | 0 | result = Curl_xfer_send(data, smbc->send_buf + smbc->sent, len, FALSE, |
616 | 0 | &bytes_written); |
617 | 0 | if(result) |
618 | 0 | return result; |
619 | | |
620 | 0 | if(bytes_written != len) |
621 | 0 | smbc->sent += bytes_written; |
622 | 0 | else |
623 | 0 | smbc->send_size = 0; |
624 | |
|
625 | 0 | return CURLE_OK; |
626 | 0 | } |
627 | | |
628 | | static CURLcode smb_send_message(struct Curl_easy *data, |
629 | | struct smb_conn *smbc, |
630 | | struct smb_request *req, |
631 | | unsigned char cmd, |
632 | | const void *msg, size_t msg_len) |
633 | 0 | { |
634 | 0 | if((MAX_MESSAGE_SIZE - sizeof(struct smb_header)) < msg_len) { |
635 | 0 | DEBUGASSERT(0); |
636 | 0 | return CURLE_SEND_ERROR; |
637 | 0 | } |
638 | 0 | smb_format_message(smbc, req, (struct smb_header *)smbc->send_buf, |
639 | 0 | cmd, msg_len); |
640 | 0 | memcpy(smbc->send_buf + sizeof(struct smb_header), msg, msg_len); |
641 | |
|
642 | 0 | return smb_send(data, smbc, sizeof(struct smb_header) + msg_len, 0); |
643 | 0 | } |
644 | | |
645 | | static CURLcode smb_send_negotiate(struct Curl_easy *data, |
646 | | struct smb_conn *smbc, |
647 | | struct smb_request *req) |
648 | 0 | { |
649 | 0 | const char *msg = "\x00\x0c\x00\x02NT LM 0.12"; |
650 | |
|
651 | 0 | return smb_send_message(data, smbc, req, SMB_COM_NEGOTIATE, msg, 15); |
652 | 0 | } |
653 | | |
654 | | static CURLcode smb_send_setup(struct Curl_easy *data) |
655 | 0 | { |
656 | 0 | struct connectdata *conn = data->conn; |
657 | 0 | struct smb_conn *smbc = Curl_conn_meta_get(conn, CURL_META_SMB_CONN); |
658 | 0 | struct smb_request *req = Curl_meta_get(data, CURL_META_SMB_EASY); |
659 | 0 | struct smb_setup msg; |
660 | 0 | char *p = msg.bytes; |
661 | 0 | unsigned char lm_hash[21]; |
662 | 0 | unsigned char lm[24]; |
663 | 0 | unsigned char nt_hash[21]; |
664 | 0 | unsigned char nt[24]; |
665 | 0 | size_t byte_count; |
666 | |
|
667 | 0 | if(!smbc || !req) |
668 | 0 | return CURLE_FAILED_INIT; |
669 | | |
670 | 0 | byte_count = sizeof(lm) + sizeof(nt) + |
671 | 0 | strlen(smbc->user) + strlen(smbc->domain) + |
672 | 0 | strlen(CURL_OS) + strlen(CLIENTNAME) + 4; /* 4 null chars */ |
673 | 0 | if(byte_count > sizeof(msg.bytes)) |
674 | 0 | return CURLE_FILESIZE_EXCEEDED; |
675 | | |
676 | 0 | Curl_ntlm_core_mk_lm_hash(conn->passwd, lm_hash); |
677 | 0 | Curl_ntlm_core_lm_resp(lm_hash, smbc->challenge, lm); |
678 | 0 | Curl_ntlm_core_mk_nt_hash(conn->passwd, nt_hash); |
679 | 0 | Curl_ntlm_core_lm_resp(nt_hash, smbc->challenge, nt); |
680 | |
|
681 | 0 | memset(&msg, 0, sizeof(msg) - sizeof(msg.bytes)); |
682 | 0 | msg.word_count = SMB_WC_SETUP_ANDX; |
683 | 0 | msg.andx.command = SMB_COM_NO_ANDX_COMMAND; |
684 | 0 | msg.max_buffer_size = smb_swap16(MAX_MESSAGE_SIZE); |
685 | 0 | msg.max_mpx_count = smb_swap16(1); |
686 | 0 | msg.vc_number = smb_swap16(1); |
687 | 0 | msg.session_key = smb_swap32(smbc->session_key); |
688 | 0 | msg.capabilities = smb_swap32(SMB_CAP_LARGE_FILES); |
689 | 0 | msg.lengths[0] = smb_swap16(sizeof(lm)); |
690 | 0 | msg.lengths[1] = smb_swap16(sizeof(nt)); |
691 | 0 | memcpy(p, lm, sizeof(lm)); |
692 | 0 | p += sizeof(lm); |
693 | 0 | memcpy(p, nt, sizeof(nt)); |
694 | 0 | p += sizeof(nt); |
695 | 0 | p += curl_msnprintf(p, byte_count - sizeof(nt) - sizeof(lm), |
696 | 0 | "%s%c" /* user */ |
697 | 0 | "%s%c" /* domain */ |
698 | 0 | "%s%c" /* OS */ |
699 | 0 | "%s", /* client name */ |
700 | 0 | smbc->user, 0, smbc->domain, 0, CURL_OS, 0, CLIENTNAME); |
701 | 0 | p++; /* count the final null-termination */ |
702 | 0 | DEBUGASSERT(byte_count == (size_t)(p - msg.bytes)); |
703 | 0 | msg.byte_count = smb_swap16((unsigned short)byte_count); |
704 | |
|
705 | 0 | return smb_send_message(data, smbc, req, SMB_COM_SETUP_ANDX, &msg, |
706 | 0 | sizeof(msg) - sizeof(msg.bytes) + byte_count); |
707 | 0 | } |
708 | | |
709 | | static CURLcode smb_send_tree_connect(struct Curl_easy *data, |
710 | | struct smb_conn *smbc, |
711 | | struct smb_request *req) |
712 | 0 | { |
713 | 0 | struct smb_tree_connect msg; |
714 | 0 | struct connectdata *conn = data->conn; |
715 | 0 | char *p = msg.bytes; |
716 | 0 | const size_t byte_count = strlen(conn->host.name) + strlen(smbc->share) + |
717 | 0 | strlen(SERVICENAME) + 5; /* 2 nulls and 3 backslashes */ |
718 | |
|
719 | 0 | if(byte_count > sizeof(msg.bytes)) |
720 | 0 | return CURLE_FILESIZE_EXCEEDED; |
721 | | |
722 | 0 | memset(&msg, 0, sizeof(msg) - sizeof(msg.bytes)); |
723 | 0 | msg.word_count = SMB_WC_TREE_CONNECT_ANDX; |
724 | 0 | msg.andx.command = SMB_COM_NO_ANDX_COMMAND; |
725 | 0 | msg.pw_len = 0; |
726 | |
|
727 | 0 | p += curl_msnprintf(p, byte_count, |
728 | 0 | "\\\\%s\\" /* hostname */ |
729 | 0 | "%s%c" /* share */ |
730 | 0 | "%s", /* service */ |
731 | 0 | conn->host.name, smbc->share, 0, SERVICENAME); |
732 | 0 | p++; /* count the final null-termination */ |
733 | 0 | DEBUGASSERT(byte_count == (size_t)(p - msg.bytes)); |
734 | 0 | msg.byte_count = smb_swap16((unsigned short)byte_count); |
735 | |
|
736 | 0 | return smb_send_message(data, smbc, req, SMB_COM_TREE_CONNECT_ANDX, &msg, |
737 | 0 | sizeof(msg) - sizeof(msg.bytes) + byte_count); |
738 | 0 | } |
739 | | |
740 | | static CURLcode smb_send_open(struct Curl_easy *data, |
741 | | struct smb_conn *smbc, |
742 | | struct smb_request *req) |
743 | 0 | { |
744 | 0 | struct smb_nt_create msg; |
745 | 0 | const size_t byte_count = strlen(req->path) + 1; |
746 | |
|
747 | 0 | if(byte_count > sizeof(msg.bytes)) |
748 | 0 | return CURLE_FILESIZE_EXCEEDED; |
749 | | |
750 | 0 | memset(&msg, 0, sizeof(msg) - sizeof(msg.bytes)); |
751 | 0 | msg.word_count = SMB_WC_NT_CREATE_ANDX; |
752 | 0 | msg.andx.command = SMB_COM_NO_ANDX_COMMAND; |
753 | 0 | msg.name_length = smb_swap16((unsigned short)(byte_count - 1)); |
754 | 0 | msg.share_access = smb_swap32(SMB_FILE_SHARE_ALL); |
755 | 0 | if(data->state.upload) { |
756 | 0 | msg.access = smb_swap32(SMB_GENERIC_READ | SMB_GENERIC_WRITE); |
757 | 0 | msg.create_disposition = smb_swap32(SMB_FILE_OVERWRITE_IF); |
758 | 0 | } |
759 | 0 | else { |
760 | 0 | msg.access = smb_swap32(SMB_GENERIC_READ); |
761 | 0 | msg.create_disposition = smb_swap32(SMB_FILE_OPEN); |
762 | 0 | } |
763 | 0 | msg.byte_count = smb_swap16((unsigned short)byte_count); |
764 | 0 | curlx_strcopy(msg.bytes, sizeof(msg.bytes), req->path, byte_count - 1); |
765 | |
|
766 | 0 | return smb_send_message(data, smbc, req, SMB_COM_NT_CREATE_ANDX, &msg, |
767 | 0 | sizeof(msg) - sizeof(msg.bytes) + byte_count); |
768 | 0 | } |
769 | | |
770 | | static CURLcode smb_send_close(struct Curl_easy *data, |
771 | | struct smb_conn *smbc, |
772 | | struct smb_request *req) |
773 | 0 | { |
774 | 0 | struct smb_close msg; |
775 | |
|
776 | 0 | memset(&msg, 0, sizeof(msg)); |
777 | 0 | msg.word_count = SMB_WC_CLOSE; |
778 | 0 | msg.fid = smb_swap16(req->fid); |
779 | |
|
780 | 0 | return smb_send_message(data, smbc, req, SMB_COM_CLOSE, &msg, sizeof(msg)); |
781 | 0 | } |
782 | | |
783 | | static CURLcode smb_send_tree_disconnect(struct Curl_easy *data, |
784 | | struct smb_conn *smbc, |
785 | | struct smb_request *req) |
786 | 0 | { |
787 | 0 | struct smb_tree_disconnect msg; |
788 | 0 | memset(&msg, 0, sizeof(msg)); |
789 | 0 | return smb_send_message(data, smbc, req, SMB_COM_TREE_DISCONNECT, |
790 | 0 | &msg, sizeof(msg)); |
791 | 0 | } |
792 | | |
793 | | static CURLcode smb_send_read(struct Curl_easy *data, |
794 | | struct smb_conn *smbc, |
795 | | struct smb_request *req) |
796 | 0 | { |
797 | 0 | curl_off_t offset = data->req.offset; |
798 | 0 | struct smb_read msg; |
799 | |
|
800 | 0 | memset(&msg, 0, sizeof(msg)); |
801 | 0 | msg.word_count = SMB_WC_READ_ANDX; |
802 | 0 | msg.andx.command = SMB_COM_NO_ANDX_COMMAND; |
803 | 0 | msg.fid = smb_swap16(req->fid); |
804 | 0 | msg.offset = smb_swap32((unsigned int)offset); |
805 | 0 | msg.offset_high = smb_swap32((unsigned int)(offset >> 32)); |
806 | 0 | msg.min_bytes = smb_swap16(MAX_PAYLOAD_SIZE); |
807 | 0 | msg.max_bytes = smb_swap16(MAX_PAYLOAD_SIZE); |
808 | |
|
809 | 0 | return smb_send_message(data, smbc, req, SMB_COM_READ_ANDX, |
810 | 0 | &msg, sizeof(msg)); |
811 | 0 | } |
812 | | |
813 | | static CURLcode smb_send_write(struct Curl_easy *data, |
814 | | struct smb_conn *smbc, |
815 | | struct smb_request *req) |
816 | 0 | { |
817 | 0 | struct smb_write *msg; |
818 | 0 | curl_off_t offset = data->req.offset; |
819 | 0 | curl_off_t upload_size = data->req.size - data->req.bytecount; |
820 | |
|
821 | 0 | msg = (struct smb_write *)smbc->send_buf; |
822 | 0 | if(upload_size >= MAX_PAYLOAD_SIZE - 1) /* There is one byte of padding */ |
823 | 0 | upload_size = MAX_PAYLOAD_SIZE - 1; |
824 | |
|
825 | 0 | memset(msg, 0, sizeof(*msg)); |
826 | 0 | msg->word_count = SMB_WC_WRITE_ANDX; |
827 | 0 | msg->andx.command = SMB_COM_NO_ANDX_COMMAND; |
828 | 0 | msg->fid = smb_swap16(req->fid); |
829 | 0 | msg->offset = smb_swap32((unsigned int)offset); |
830 | 0 | msg->offset_high = smb_swap32((unsigned int)(offset >> 32)); |
831 | 0 | msg->data_length = smb_swap16((unsigned short)upload_size); |
832 | 0 | msg->data_offset = smb_swap16(sizeof(*msg) - sizeof(unsigned int)); |
833 | 0 | msg->byte_count = smb_swap16((unsigned short)(upload_size + 1)); |
834 | |
|
835 | 0 | smb_format_message(smbc, req, &msg->h, SMB_COM_WRITE_ANDX, |
836 | 0 | sizeof(*msg) - sizeof(msg->h) + (size_t)upload_size); |
837 | |
|
838 | 0 | return smb_send(data, smbc, sizeof(*msg), (size_t)upload_size); |
839 | 0 | } |
840 | | |
841 | | static CURLcode smb_send_and_recv(struct Curl_easy *data, |
842 | | struct smb_conn *smbc, void **msg) |
843 | 0 | { |
844 | 0 | CURLcode result; |
845 | 0 | *msg = NULL; /* if it returns early */ |
846 | | |
847 | | /* Check if there is data in the transfer buffer */ |
848 | 0 | if(!smbc->send_size && smbc->upload_size) { |
849 | 0 | size_t nread = smbc->upload_size > (size_t)MAX_MESSAGE_SIZE ? |
850 | 0 | (size_t)MAX_MESSAGE_SIZE : smbc->upload_size; |
851 | 0 | bool eos; |
852 | |
|
853 | 0 | result = Curl_client_read(data, smbc->send_buf, nread, &nread, &eos); |
854 | 0 | if(result && result != CURLE_AGAIN) |
855 | 0 | return result; |
856 | 0 | if(!nread) |
857 | 0 | return CURLE_OK; |
858 | | |
859 | 0 | smbc->upload_size -= nread; |
860 | 0 | smbc->send_size = nread; |
861 | 0 | smbc->sent = 0; |
862 | 0 | } |
863 | | |
864 | | /* Check if there is data to send */ |
865 | 0 | if(smbc->send_size) { |
866 | 0 | result = smb_flush(data, smbc); |
867 | 0 | if(result) |
868 | 0 | return result; |
869 | 0 | } |
870 | | |
871 | | /* Check if there is still data to be sent */ |
872 | 0 | if(smbc->send_size || smbc->upload_size) |
873 | 0 | return CURLE_AGAIN; |
874 | | |
875 | 0 | return smb_recv_message(data, smbc, msg); |
876 | 0 | } |
877 | | |
878 | | static CURLcode smb_connection_state(struct Curl_easy *data, bool *done) |
879 | 0 | { |
880 | 0 | struct connectdata *conn = data->conn; |
881 | 0 | struct smb_conn *smbc = Curl_conn_meta_get(conn, CURL_META_SMB_CONN); |
882 | 0 | struct smb_request *req = Curl_meta_get(data, CURL_META_SMB_EASY); |
883 | 0 | struct smb_negotiate_response *nrsp; |
884 | 0 | struct smb_header *h; |
885 | 0 | CURLcode result; |
886 | 0 | void *msg = NULL; |
887 | |
|
888 | 0 | if(!smbc || !req) |
889 | 0 | return CURLE_FAILED_INIT; |
890 | | |
891 | 0 | if(smbc->state == SMB_CONNECTING) { |
892 | 0 | #ifdef USE_SSL |
893 | 0 | if(Curl_conn_is_ssl(conn, FIRSTSOCKET)) { |
894 | 0 | bool ssl_done = FALSE; |
895 | 0 | result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssl_done); |
896 | 0 | if(result && result != CURLE_AGAIN) |
897 | 0 | return result; |
898 | 0 | if(!ssl_done) |
899 | 0 | return CURLE_OK; |
900 | 0 | } |
901 | 0 | #endif |
902 | | |
903 | 0 | result = smb_send_negotiate(data, smbc, req); |
904 | 0 | if(result) { |
905 | 0 | connclose(conn, "SMB: failed to send negotiate message"); |
906 | 0 | return result; |
907 | 0 | } |
908 | | |
909 | 0 | conn_state(data, smbc, SMB_NEGOTIATE); |
910 | 0 | } |
911 | | |
912 | | /* Send the previous message and check for a response */ |
913 | 0 | result = smb_send_and_recv(data, smbc, &msg); |
914 | 0 | if(result && result != CURLE_AGAIN) { |
915 | 0 | connclose(conn, "SMB: failed to communicate"); |
916 | 0 | return result; |
917 | 0 | } |
918 | | |
919 | 0 | if(!msg) |
920 | 0 | return CURLE_OK; |
921 | | |
922 | 0 | h = msg; |
923 | |
|
924 | 0 | switch(smbc->state) { |
925 | 0 | case SMB_NEGOTIATE: |
926 | 0 | if((smbc->got < sizeof(*nrsp) + sizeof(smbc->challenge) - 1) || |
927 | 0 | h->status) { |
928 | 0 | connclose(conn, "SMB: negotiation failed"); |
929 | 0 | return CURLE_COULDNT_CONNECT; |
930 | 0 | } |
931 | 0 | nrsp = msg; |
932 | | #if defined(__GNUC__) && __GNUC__ >= 13 |
933 | | #pragma GCC diagnostic push |
934 | | /* error: 'memcpy' offset [74, 80] from the object at '<unknown>' is out of |
935 | | the bounds of referenced subobject 'bytes' with type 'char[1]' */ |
936 | | #pragma GCC diagnostic ignored "-Warray-bounds" |
937 | | #endif |
938 | 0 | memcpy(smbc->challenge, nrsp->bytes, sizeof(smbc->challenge)); |
939 | | #if defined(__GNUC__) && __GNUC__ >= 13 |
940 | | #pragma GCC diagnostic pop |
941 | | #endif |
942 | 0 | smbc->session_key = smb_swap32(nrsp->session_key); |
943 | 0 | result = smb_send_setup(data); |
944 | 0 | if(result) { |
945 | 0 | connclose(conn, "SMB: failed to send setup message"); |
946 | 0 | return result; |
947 | 0 | } |
948 | 0 | conn_state(data, smbc, SMB_SETUP); |
949 | 0 | break; |
950 | | |
951 | 0 | case SMB_SETUP: |
952 | 0 | if(h->status) { |
953 | 0 | connclose(conn, "SMB: authentication failed"); |
954 | 0 | return CURLE_LOGIN_DENIED; |
955 | 0 | } |
956 | 0 | smbc->uid = smb_swap16(h->uid); |
957 | 0 | conn_state(data, smbc, SMB_CONNECTED); |
958 | 0 | *done = TRUE; |
959 | 0 | break; |
960 | | |
961 | 0 | default: |
962 | 0 | smb_pop_message(smbc); |
963 | 0 | return CURLE_OK; /* ignore */ |
964 | 0 | } |
965 | | |
966 | 0 | smb_pop_message(smbc); |
967 | |
|
968 | 0 | return CURLE_OK; |
969 | 0 | } |
970 | | |
971 | | /* |
972 | | * Convert a timestamp from the Windows world (100 nsec units from 1 Jan 1601) |
973 | | * to POSIX time. Cap the output to fit within a time_t. |
974 | | */ |
975 | | static void get_posix_time(time_t *out, curl_off_t timestamp) |
976 | 0 | { |
977 | 0 | if(timestamp >= (curl_off_t)116444736000000000) { |
978 | 0 | timestamp -= (curl_off_t)116444736000000000; |
979 | 0 | timestamp /= 10000000; |
980 | | #if SIZEOF_TIME_T < SIZEOF_CURL_OFF_T |
981 | | if(timestamp > TIME_T_MAX) |
982 | | *out = TIME_T_MAX; |
983 | | else if(timestamp < TIME_T_MIN) |
984 | | *out = TIME_T_MIN; |
985 | | else |
986 | | #endif |
987 | 0 | *out = (time_t)timestamp; |
988 | 0 | } |
989 | 0 | else |
990 | 0 | *out = 0; |
991 | 0 | } |
992 | | |
993 | | static CURLcode smb_request_state(struct Curl_easy *data, bool *done) |
994 | 0 | { |
995 | 0 | struct connectdata *conn = data->conn; |
996 | 0 | struct smb_conn *smbc = Curl_conn_meta_get(conn, CURL_META_SMB_CONN); |
997 | 0 | struct smb_request *req = Curl_meta_get(data, CURL_META_SMB_EASY); |
998 | 0 | struct smb_header *h; |
999 | 0 | enum smb_req_state next_state = SMB_DONE; |
1000 | 0 | unsigned short len; |
1001 | 0 | unsigned short off; |
1002 | 0 | CURLcode result; |
1003 | 0 | void *msg = NULL; |
1004 | 0 | const struct smb_nt_create_response *smb_m; |
1005 | |
|
1006 | 0 | if(!smbc || !req) |
1007 | 0 | return CURLE_FAILED_INIT; |
1008 | | |
1009 | 0 | if(data->state.upload && (data->state.infilesize < 0)) { |
1010 | 0 | failf(data, "SMB upload needs to know the size up front"); |
1011 | 0 | return CURLE_SEND_ERROR; |
1012 | 0 | } |
1013 | | |
1014 | | /* Start the request */ |
1015 | 0 | if(req->state == SMB_REQUESTING) { |
1016 | 0 | result = smb_send_tree_connect(data, smbc, req); |
1017 | 0 | if(result) { |
1018 | 0 | connclose(conn, "SMB: failed to send tree connect message"); |
1019 | 0 | return result; |
1020 | 0 | } |
1021 | | |
1022 | 0 | request_state(data, SMB_TREE_CONNECT); |
1023 | 0 | } |
1024 | | |
1025 | | /* Send the previous message and check for a response */ |
1026 | 0 | result = smb_send_and_recv(data, smbc, &msg); |
1027 | 0 | if(result && result != CURLE_AGAIN) { |
1028 | 0 | connclose(conn, "SMB: failed to communicate"); |
1029 | 0 | return result; |
1030 | 0 | } |
1031 | | |
1032 | 0 | if(!msg) |
1033 | 0 | return CURLE_OK; |
1034 | | |
1035 | 0 | h = msg; |
1036 | |
|
1037 | 0 | switch(req->state) { |
1038 | 0 | case SMB_TREE_CONNECT: |
1039 | 0 | if(h->status) { |
1040 | 0 | req->result = CURLE_REMOTE_FILE_NOT_FOUND; |
1041 | 0 | if(h->status == smb_swap32(SMB_ERR_NOACCESS)) |
1042 | 0 | req->result = CURLE_REMOTE_ACCESS_DENIED; |
1043 | 0 | break; |
1044 | 0 | } |
1045 | 0 | req->tid = smb_swap16(h->tid); |
1046 | 0 | next_state = SMB_OPEN; |
1047 | 0 | break; |
1048 | | |
1049 | 0 | case SMB_OPEN: |
1050 | 0 | if(h->status || smbc->got < sizeof(struct smb_nt_create_response)) { |
1051 | 0 | req->result = CURLE_REMOTE_FILE_NOT_FOUND; |
1052 | 0 | if(h->status == smb_swap32(SMB_ERR_NOACCESS)) |
1053 | 0 | req->result = CURLE_REMOTE_ACCESS_DENIED; |
1054 | 0 | next_state = SMB_TREE_DISCONNECT; |
1055 | 0 | break; |
1056 | 0 | } |
1057 | 0 | smb_m = (const struct smb_nt_create_response *)msg; |
1058 | 0 | req->fid = smb_swap16(smb_m->fid); |
1059 | 0 | data->req.offset = 0; |
1060 | 0 | if(data->state.upload) { |
1061 | 0 | data->req.size = data->state.infilesize; |
1062 | 0 | Curl_pgrsSetUploadSize(data, data->req.size); |
1063 | 0 | next_state = SMB_UPLOAD; |
1064 | 0 | } |
1065 | 0 | else { |
1066 | 0 | data->req.size = smb_swap64(smb_m->end_of_file); |
1067 | 0 | if(data->req.size < 0) { |
1068 | 0 | req->result = CURLE_WEIRD_SERVER_REPLY; |
1069 | 0 | next_state = SMB_CLOSE; |
1070 | 0 | } |
1071 | 0 | else { |
1072 | 0 | Curl_pgrsSetDownloadSize(data, data->req.size); |
1073 | 0 | if(data->set.get_filetime) |
1074 | 0 | get_posix_time(&data->info.filetime, smb_m->last_change_time); |
1075 | 0 | next_state = SMB_DOWNLOAD; |
1076 | 0 | } |
1077 | 0 | } |
1078 | 0 | break; |
1079 | | |
1080 | 0 | case SMB_DOWNLOAD: |
1081 | 0 | if(h->status || smbc->got < sizeof(struct smb_header) + 15) { |
1082 | 0 | req->result = CURLE_RECV_ERROR; |
1083 | 0 | next_state = SMB_CLOSE; |
1084 | 0 | break; |
1085 | 0 | } |
1086 | 0 | len = Curl_read16_le((const unsigned char *)msg + |
1087 | 0 | sizeof(struct smb_header) + 11); |
1088 | 0 | off = Curl_read16_le((const unsigned char *)msg + |
1089 | 0 | sizeof(struct smb_header) + 13); |
1090 | 0 | if(len > 0) { |
1091 | 0 | if(off + sizeof(unsigned int) + len > smbc->got) { |
1092 | 0 | failf(data, "Invalid input packet"); |
1093 | 0 | result = CURLE_RECV_ERROR; |
1094 | 0 | } |
1095 | 0 | else |
1096 | 0 | result = Curl_client_write(data, CLIENTWRITE_BODY, |
1097 | 0 | (char *)msg + off + sizeof(unsigned int), |
1098 | 0 | len); |
1099 | 0 | if(result) { |
1100 | 0 | req->result = result; |
1101 | 0 | next_state = SMB_CLOSE; |
1102 | 0 | break; |
1103 | 0 | } |
1104 | 0 | } |
1105 | 0 | data->req.offset += len; |
1106 | 0 | next_state = (len < MAX_PAYLOAD_SIZE) ? SMB_CLOSE : SMB_DOWNLOAD; |
1107 | 0 | break; |
1108 | | |
1109 | 0 | case SMB_UPLOAD: |
1110 | 0 | if(h->status || smbc->got < sizeof(struct smb_header) + 7) { |
1111 | 0 | req->result = CURLE_UPLOAD_FAILED; |
1112 | 0 | next_state = SMB_CLOSE; |
1113 | 0 | break; |
1114 | 0 | } |
1115 | 0 | len = Curl_read16_le((const unsigned char *)msg + |
1116 | 0 | sizeof(struct smb_header) + 5); |
1117 | 0 | data->req.bytecount += len; |
1118 | 0 | data->req.offset += len; |
1119 | 0 | Curl_pgrs_upload_inc(data, len); |
1120 | 0 | if(data->req.bytecount >= data->req.size) |
1121 | 0 | next_state = SMB_CLOSE; |
1122 | 0 | else |
1123 | 0 | next_state = SMB_UPLOAD; |
1124 | 0 | break; |
1125 | | |
1126 | 0 | case SMB_CLOSE: |
1127 | | /* We do not care if the close failed, proceed to tree disconnect anyway */ |
1128 | 0 | next_state = SMB_TREE_DISCONNECT; |
1129 | 0 | break; |
1130 | | |
1131 | 0 | case SMB_TREE_DISCONNECT: |
1132 | 0 | next_state = SMB_DONE; |
1133 | 0 | break; |
1134 | | |
1135 | 0 | default: |
1136 | 0 | smb_pop_message(smbc); |
1137 | 0 | return CURLE_OK; /* ignore */ |
1138 | 0 | } |
1139 | | |
1140 | 0 | smb_pop_message(smbc); |
1141 | |
|
1142 | 0 | switch(next_state) { |
1143 | 0 | case SMB_OPEN: |
1144 | 0 | result = smb_send_open(data, smbc, req); |
1145 | 0 | break; |
1146 | | |
1147 | 0 | case SMB_DOWNLOAD: |
1148 | 0 | result = smb_send_read(data, smbc, req); |
1149 | 0 | break; |
1150 | | |
1151 | 0 | case SMB_UPLOAD: |
1152 | 0 | result = smb_send_write(data, smbc, req); |
1153 | 0 | break; |
1154 | | |
1155 | 0 | case SMB_CLOSE: |
1156 | 0 | result = smb_send_close(data, smbc, req); |
1157 | 0 | break; |
1158 | | |
1159 | 0 | case SMB_TREE_DISCONNECT: |
1160 | 0 | result = smb_send_tree_disconnect(data, smbc, req); |
1161 | 0 | break; |
1162 | | |
1163 | 0 | case SMB_DONE: |
1164 | 0 | result = req->result; |
1165 | 0 | *done = TRUE; |
1166 | 0 | break; |
1167 | | |
1168 | 0 | default: |
1169 | 0 | break; |
1170 | 0 | } |
1171 | | |
1172 | 0 | if(result) { |
1173 | 0 | connclose(conn, "SMB: failed to send message"); |
1174 | 0 | return result; |
1175 | 0 | } |
1176 | | |
1177 | 0 | request_state(data, next_state); |
1178 | |
|
1179 | 0 | return CURLE_OK; |
1180 | 0 | } |
1181 | | |
1182 | | static CURLcode smb_pollset(struct Curl_easy *data, |
1183 | | struct easy_pollset *ps) |
1184 | 0 | { |
1185 | 0 | return Curl_pollset_add_inout(data, ps, data->conn->sock[FIRSTSOCKET]); |
1186 | 0 | } |
1187 | | |
1188 | | static CURLcode smb_do(struct Curl_easy *data, bool *done) |
1189 | 0 | { |
1190 | 0 | struct connectdata *conn = data->conn; |
1191 | 0 | struct smb_conn *smbc = Curl_conn_meta_get(conn, CURL_META_SMB_CONN); |
1192 | |
|
1193 | 0 | *done = FALSE; |
1194 | 0 | if(!smbc) |
1195 | 0 | return CURLE_FAILED_INIT; |
1196 | 0 | if(smbc->share) |
1197 | 0 | return CURLE_OK; |
1198 | 0 | return CURLE_URL_MALFORMAT; |
1199 | 0 | } |
1200 | | |
1201 | | /* |
1202 | | * SMB handler interface |
1203 | | */ |
1204 | | const struct Curl_handler Curl_handler_smb = { |
1205 | | "smb", /* scheme */ |
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 | | PORT_SMB, /* defport */ |
1224 | | CURLPROTO_SMB, /* protocol */ |
1225 | | CURLPROTO_SMB, /* family */ |
1226 | | PROTOPT_CONN_REUSE /* flags */ |
1227 | | }; |
1228 | | |
1229 | | #ifdef USE_SSL |
1230 | | /* |
1231 | | * SMBS handler interface |
1232 | | */ |
1233 | | const struct Curl_handler Curl_handler_smbs = { |
1234 | | "smbs", /* scheme */ |
1235 | | smb_setup_connection, /* setup_connection */ |
1236 | | smb_do, /* do_it */ |
1237 | | ZERO_NULL, /* done */ |
1238 | | ZERO_NULL, /* do_more */ |
1239 | | smb_connect, /* connect_it */ |
1240 | | smb_connection_state, /* connecting */ |
1241 | | smb_request_state, /* doing */ |
1242 | | smb_pollset, /* proto_pollset */ |
1243 | | smb_pollset, /* doing_pollset */ |
1244 | | ZERO_NULL, /* domore_pollset */ |
1245 | | ZERO_NULL, /* perform_pollset */ |
1246 | | ZERO_NULL, /* disconnect */ |
1247 | | ZERO_NULL, /* write_resp */ |
1248 | | ZERO_NULL, /* write_resp_hd */ |
1249 | | ZERO_NULL, /* connection_check */ |
1250 | | ZERO_NULL, /* attach connection */ |
1251 | | ZERO_NULL, /* follow */ |
1252 | | PORT_SMBS, /* defport */ |
1253 | | CURLPROTO_SMBS, /* protocol */ |
1254 | | CURLPROTO_SMB, /* family */ |
1255 | | PROTOPT_SSL | PROTOPT_CONN_REUSE /* flags */ |
1256 | | }; |
1257 | | #endif |
1258 | | |
1259 | | #endif /* CURL_DISABLE_SMB && USE_CURL_NTLM_CORE && SIZEOF_CURL_OFF_T > 4 */ |