Coverage Report

Created: 2025-10-13 06:43

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