Coverage Report

Created: 2025-08-26 07:17

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