Coverage Report

Created: 2025-08-26 07:08

/src/PROJ/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
0
#define CURL_META_SMB_EASY   "meta:proto:smb:easy"
53
/* meta key for storing protocol meta at connection */
54
0
#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
0
#define SMB_COM_CLOSE                 0x04
116
0
#define SMB_COM_READ_ANDX             0x2e
117
0
#define SMB_COM_WRITE_ANDX            0x2f
118
0
#define SMB_COM_TREE_DISCONNECT       0x71
119
0
#define SMB_COM_NEGOTIATE             0x72
120
0
#define SMB_COM_SETUP_ANDX            0x73
121
0
#define SMB_COM_TREE_CONNECT_ANDX     0x75
122
0
#define SMB_COM_NT_CREATE_ANDX        0xa2
123
0
#define SMB_COM_NO_ANDX_COMMAND       0xff
124
125
0
#define SMB_WC_CLOSE                  0x03
126
0
#define SMB_WC_READ_ANDX              0x0c
127
0
#define SMB_WC_WRITE_ANDX             0x0e
128
0
#define SMB_WC_SETUP_ANDX             0x0d
129
0
#define SMB_WC_TREE_CONNECT_ANDX      0x04
130
0
#define SMB_WC_NT_CREATE_ANDX         0x18
131
132
0
#define SMB_FLAGS_CANONICAL_PATHNAMES 0x10
133
0
#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
0
#define MAX_PAYLOAD_SIZE  0x8000
367
0
#define MAX_MESSAGE_SIZE  (MAX_PAYLOAD_SIZE + 0x1000)
368
0
#define CLIENTNAME        "curl"
369
0
#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
0
#  define smb_swap16(x) (x)
393
0
#  define smb_swap32(x) (x)
394
0
#  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
0
{
400
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
401
  /* For debug purposes */
402
  static const char * const names[] = {
403
    "SMB_NOT_CONNECTED",
404
    "SMB_CONNECTING",
405
    "SMB_NEGOTIATE",
406
    "SMB_SETUP",
407
    "SMB_CONNECTED",
408
    /* LAST */
409
  };
410
411
  if(smbc->state != newstate)
412
    infof(data, "SMB conn %p state change from %s to %s",
413
          (void *)smbc, names[smbc->state], names[newstate]);
414
#endif
415
0
  (void)data;
416
0
  smbc->state = newstate;
417
0
}
418
419
static void request_state(struct Curl_easy *data,
420
                          enum smb_req_state newstate)
421
0
{
422
0
  struct smb_request *req = Curl_meta_get(data, CURL_META_SMB_EASY);
423
0
  if(req) {
424
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
425
    /* For debug purposes */
426
    static const char * const names[] = {
427
      "SMB_REQUESTING",
428
      "SMB_TREE_CONNECT",
429
      "SMB_OPEN",
430
      "SMB_DOWNLOAD",
431
      "SMB_UPLOAD",
432
      "SMB_CLOSE",
433
      "SMB_TREE_DISCONNECT",
434
      "SMB_DONE",
435
      /* LAST */
436
    };
437
438
    if(req->state != newstate)
439
      infof(data, "SMB request %p state change from %s to %s",
440
            (void *)req, names[req->state], names[newstate]);
441
#endif
442
443
0
    req->state = newstate;
444
0
  }
445
0
}
446
447
static void smb_easy_dtor(void *key, size_t klen, void *entry)
448
0
{
449
0
  struct smb_request *req = entry;
450
0
  (void)key;
451
0
  (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
0
  free(req);
456
0
}
457
458
static void smb_conn_dtor(void *key, size_t klen, void *entry)
459
0
{
460
0
  struct smb_conn *smbc = entry;
461
0
  (void)key;
462
0
  (void)klen;
463
0
  Curl_safefree(smbc->share);
464
0
  Curl_safefree(smbc->domain);
465
0
  Curl_safefree(smbc->recv_buf);
466
0
  Curl_safefree(smbc->send_buf);
467
0
  free(smbc);
468
0
}
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
0
{
475
0
  struct smb_conn *smbc;
476
0
  struct smb_request *req;
477
478
  /* Initialize the connection state */
479
0
  smbc = calloc(1, sizeof(*smbc));
480
0
  if(!smbc ||
481
0
     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
0
  req = calloc(1, sizeof(*req));
486
0
  if(!req ||
487
0
     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
0
  return smb_parse_url_path(data, smbc, req);
492
0
}
493
494
static CURLcode smb_connect(struct Curl_easy *data, bool *done)
495
0
{
496
0
  struct connectdata *conn = data->conn;
497
0
  struct smb_conn *smbc = Curl_conn_meta_get(conn, CURL_META_SMB_CONN);
498
0
  char *slash;
499
500
0
  (void)done;
501
0
  if(!smbc)
502
0
    return CURLE_FAILED_INIT;
503
504
  /* Check we have a username and password to authenticate with */
505
0
  if(!data->state.aptr.user)
506
0
    return CURLE_LOGIN_DENIED;
507
508
  /* Initialize the connection state */
509
0
  smbc->state = SMB_CONNECTING;
510
0
  smbc->recv_buf = malloc(MAX_MESSAGE_SIZE);
511
0
  if(!smbc->recv_buf)
512
0
    return CURLE_OUT_OF_MEMORY;
513
0
  smbc->send_buf = malloc(MAX_MESSAGE_SIZE);
514
0
  if(!smbc->send_buf)
515
0
    return CURLE_OUT_OF_MEMORY;
516
517
  /* Multiple requests are allowed with this connection */
518
0
  connkeep(conn, "SMB default");
519
520
  /* Parse the username, domain, and password */
521
0
  slash = strchr(conn->user, '/');
522
0
  if(!slash)
523
0
    slash = strchr(conn->user, '\\');
524
525
0
  if(slash) {
526
0
    smbc->user = slash + 1;
527
0
    smbc->domain = strdup(conn->user);
528
0
    if(!smbc->domain)
529
0
      return CURLE_OUT_OF_MEMORY;
530
0
    smbc->domain[slash - conn->user] = 0;
531
0
  }
532
0
  else {
533
0
    smbc->user = conn->user;
534
0
    smbc->domain = strdup(conn->host.name);
535
0
    if(!smbc->domain)
536
0
      return CURLE_OUT_OF_MEMORY;
537
0
  }
538
539
0
  return CURLE_OK;
540
0
}
541
542
static CURLcode smb_recv_message(struct Curl_easy *data,
543
                                 struct smb_conn *smbc,
544
                                 void **msg)
545
0
{
546
0
  char *buf = smbc->recv_buf;
547
0
  size_t bytes_read;
548
0
  size_t nbt_size;
549
0
  size_t msg_size;
550
0
  size_t len = MAX_MESSAGE_SIZE - smbc->got;
551
0
  CURLcode result;
552
553
0
  result = Curl_xfer_recv(data, buf + smbc->got, len, &bytes_read);
554
0
  if(result)
555
0
    return result;
556
557
0
  if(!bytes_read)
558
0
    return CURLE_OK;
559
560
0
  smbc->got += bytes_read;
561
562
  /* Check for a 32-bit nbt header */
563
0
  if(smbc->got < sizeof(unsigned int))
564
0
    return CURLE_OK;
565
566
0
  nbt_size = Curl_read16_be((const unsigned char *)
567
0
                            (buf + sizeof(unsigned short))) +
568
0
    sizeof(unsigned int);
569
0
  if(smbc->got < nbt_size)
570
0
    return CURLE_OK;
571
572
0
  msg_size = sizeof(struct smb_header);
573
0
  if(nbt_size >= msg_size + 1) {
574
    /* Add the word count */
575
0
    msg_size += 1 + ((unsigned char) buf[msg_size]) * sizeof(unsigned short);
576
0
    if(nbt_size >= msg_size + sizeof(unsigned short)) {
577
      /* Add the byte count */
578
0
      msg_size += sizeof(unsigned short) +
579
0
        Curl_read16_le((const unsigned char *)&buf[msg_size]);
580
0
      if(nbt_size < msg_size)
581
0
        return CURLE_READ_ERROR;
582
0
    }
583
0
  }
584
585
0
  *msg = buf;
586
587
0
  return CURLE_OK;
588
0
}
589
590
static void smb_pop_message(struct smb_conn *smbc)
591
0
{
592
0
  smbc->got = 0;
593
0
}
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
0
{
600
0
  const unsigned int pid = 0xbad71d; /* made up */
601
602
0
  memset(h, 0, sizeof(*h));
603
0
  h->nbt_length = htons((unsigned short) (sizeof(*h) - sizeof(unsigned int) +
604
0
                                          len));
605
0
  memcpy((char *)h->magic, "\xffSMB", 4);
606
0
  h->command = cmd;
607
0
  h->flags = SMB_FLAGS_CANONICAL_PATHNAMES | SMB_FLAGS_CASELESS_PATHNAMES;
608
0
  h->flags2 = smb_swap16(SMB_FLAGS2_IS_LONG_NAME | SMB_FLAGS2_KNOWS_LONG_NAME);
609
0
  h->uid = smb_swap16(smbc->uid);
610
0
  h->tid = smb_swap16(req->tid);
611
0
  h->pid_high = smb_swap16((unsigned short)(pid >> 16));
612
0
  h->pid = smb_swap16((unsigned short) pid);
613
0
}
614
615
static CURLcode smb_send(struct Curl_easy *data, struct smb_conn *smbc,
616
                         size_t len, size_t upload_size)
617
0
{
618
0
  size_t bytes_written;
619
0
  CURLcode result;
620
621
0
  result = Curl_xfer_send(data, smbc->send_buf, len, FALSE, &bytes_written);
622
0
  if(result)
623
0
    return result;
624
625
0
  if(bytes_written != len) {
626
0
    smbc->send_size = len;
627
0
    smbc->sent = bytes_written;
628
0
  }
629
630
0
  smbc->upload_size = upload_size;
631
632
0
  return CURLE_OK;
633
0
}
634
635
static CURLcode smb_flush(struct Curl_easy *data, struct smb_conn *smbc)
636
0
{
637
0
  size_t bytes_written;
638
0
  size_t len = smbc->send_size - smbc->sent;
639
0
  CURLcode result;
640
641
0
  if(!smbc->send_size)
642
0
    return CURLE_OK;
643
644
0
  result = Curl_xfer_send(data, smbc->send_buf + smbc->sent, len, FALSE,
645
0
                          &bytes_written);
646
0
  if(result)
647
0
    return result;
648
649
0
  if(bytes_written != len)
650
0
    smbc->sent += bytes_written;
651
0
  else
652
0
    smbc->send_size = 0;
653
654
0
  return CURLE_OK;
655
0
}
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
0
{
663
0
  smb_format_message(smbc, req, (struct smb_header *)smbc->send_buf,
664
0
                     cmd, msg_len);
665
0
  DEBUGASSERT((sizeof(struct smb_header) + msg_len) <= MAX_MESSAGE_SIZE);
666
0
  memcpy(smbc->send_buf + sizeof(struct smb_header), msg, msg_len);
667
668
0
  return smb_send(data, smbc, sizeof(struct smb_header) + msg_len, 0);
669
0
}
670
671
static CURLcode smb_send_negotiate(struct Curl_easy *data,
672
                                   struct smb_conn *smbc,
673
                                   struct smb_request *req)
674
0
{
675
0
  const char *msg = "\x00\x0c\x00\x02NT LM 0.12";
676
677
0
  return smb_send_message(data, smbc, req, SMB_COM_NEGOTIATE, msg, 15);
678
0
}
679
680
static CURLcode smb_send_setup(struct Curl_easy *data)
681
0
{
682
0
  struct connectdata *conn = data->conn;
683
0
  struct smb_conn *smbc = Curl_conn_meta_get(conn, CURL_META_SMB_CONN);
684
0
  struct smb_request *req = Curl_meta_get(data, CURL_META_SMB_EASY);
685
0
  struct smb_setup msg;
686
0
  char *p = msg.bytes;
687
0
  unsigned char lm_hash[21];
688
0
  unsigned char lm[24];
689
0
  unsigned char nt_hash[21];
690
0
  unsigned char nt[24];
691
0
  size_t byte_count;
692
693
0
  if(!smbc || !req)
694
0
    return CURLE_FAILED_INIT;
695
696
0
  byte_count = sizeof(lm) + sizeof(nt) +
697
0
    strlen(smbc->user) + strlen(smbc->domain) +
698
0
    strlen(CURL_OS) + strlen(CLIENTNAME) + 4; /* 4 null chars */
699
0
  if(byte_count > sizeof(msg.bytes))
700
0
    return CURLE_FILESIZE_EXCEEDED;
701
702
0
  Curl_ntlm_core_mk_lm_hash(conn->passwd, lm_hash);
703
0
  Curl_ntlm_core_lm_resp(lm_hash, smbc->challenge, lm);
704
0
  Curl_ntlm_core_mk_nt_hash(conn->passwd, nt_hash);
705
0
  Curl_ntlm_core_lm_resp(nt_hash, smbc->challenge, nt);
706
707
0
  memset(&msg, 0, sizeof(msg) - sizeof(msg.bytes));
708
0
  msg.word_count = SMB_WC_SETUP_ANDX;
709
0
  msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
710
0
  msg.max_buffer_size = smb_swap16(MAX_MESSAGE_SIZE);
711
0
  msg.max_mpx_count = smb_swap16(1);
712
0
  msg.vc_number = smb_swap16(1);
713
0
  msg.session_key = smb_swap32(smbc->session_key);
714
0
  msg.capabilities = smb_swap32(SMB_CAP_LARGE_FILES);
715
0
  msg.lengths[0] = smb_swap16(sizeof(lm));
716
0
  msg.lengths[1] = smb_swap16(sizeof(nt));
717
0
  memcpy(p, lm, sizeof(lm));
718
0
  p += sizeof(lm);
719
0
  memcpy(p, nt, sizeof(nt));
720
0
  p += sizeof(nt);
721
0
  p += msnprintf(p, byte_count - sizeof(nt) - sizeof(lm),
722
0
                 "%s%c"  /* user */
723
0
                 "%s%c"  /* domain */
724
0
                 "%s%c"  /* OS */
725
0
                 "%s", /* client name */
726
0
                 smbc->user, 0, smbc->domain, 0, CURL_OS, 0, CLIENTNAME);
727
0
  p++; /* count the final null-termination */
728
0
  DEBUGASSERT(byte_count == (size_t)(p - msg.bytes));
729
0
  msg.byte_count = smb_swap16((unsigned short)byte_count);
730
731
0
  return smb_send_message(data, smbc, req, SMB_COM_SETUP_ANDX, &msg,
732
0
                          sizeof(msg) - sizeof(msg.bytes) + byte_count);
733
0
}
734
735
static CURLcode smb_send_tree_connect(struct Curl_easy *data,
736
                                      struct smb_conn *smbc,
737
                                      struct smb_request *req)
738
0
{
739
0
  struct smb_tree_connect msg;
740
0
  struct connectdata *conn = data->conn;
741
0
  char *p = msg.bytes;
742
0
  const size_t byte_count = strlen(conn->host.name) + strlen(smbc->share) +
743
0
    strlen(SERVICENAME) + 5; /* 2 nulls and 3 backslashes */
744
745
0
  if(byte_count > sizeof(msg.bytes))
746
0
    return CURLE_FILESIZE_EXCEEDED;
747
748
0
  memset(&msg, 0, sizeof(msg) - sizeof(msg.bytes));
749
0
  msg.word_count = SMB_WC_TREE_CONNECT_ANDX;
750
0
  msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
751
0
  msg.pw_len = 0;
752
753
0
  p += msnprintf(p, byte_count,
754
0
                 "\\\\%s\\"  /* hostname */
755
0
                 "%s%c"      /* share */
756
0
                 "%s",       /* service */
757
0
                 conn->host.name, smbc->share, 0, SERVICENAME);
758
0
  p++; /* count the final null-termination */
759
0
  DEBUGASSERT(byte_count == (size_t)(p - msg.bytes));
760
0
  msg.byte_count = smb_swap16((unsigned short)byte_count);
761
762
0
  return smb_send_message(data, smbc, req, SMB_COM_TREE_CONNECT_ANDX, &msg,
763
0
                          sizeof(msg) - sizeof(msg.bytes) + byte_count);
764
0
}
765
766
static CURLcode smb_send_open(struct Curl_easy *data,
767
                              struct smb_conn *smbc,
768
                              struct smb_request *req)
769
0
{
770
0
  struct smb_nt_create msg;
771
0
  const size_t byte_count = strlen(req->path) + 1;
772
773
0
  if(byte_count > sizeof(msg.bytes))
774
0
    return CURLE_FILESIZE_EXCEEDED;
775
776
0
  memset(&msg, 0, sizeof(msg) - sizeof(msg.bytes));
777
0
  msg.word_count = SMB_WC_NT_CREATE_ANDX;
778
0
  msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
779
0
  msg.name_length = smb_swap16((unsigned short)(byte_count - 1));
780
0
  msg.share_access = smb_swap32(SMB_FILE_SHARE_ALL);
781
0
  if(data->state.upload) {
782
0
    msg.access = smb_swap32(SMB_GENERIC_READ | SMB_GENERIC_WRITE);
783
0
    msg.create_disposition = smb_swap32(SMB_FILE_OVERWRITE_IF);
784
0
  }
785
0
  else {
786
0
    msg.access = smb_swap32(SMB_GENERIC_READ);
787
0
    msg.create_disposition = smb_swap32(SMB_FILE_OPEN);
788
0
  }
789
0
  msg.byte_count = smb_swap16((unsigned short) byte_count);
790
0
  strcpy(msg.bytes, req->path);
791
792
0
  return smb_send_message(data, smbc, req, SMB_COM_NT_CREATE_ANDX, &msg,
793
0
                          sizeof(msg) - sizeof(msg.bytes) + byte_count);
794
0
}
795
796
static CURLcode smb_send_close(struct Curl_easy *data,
797
                               struct smb_conn *smbc,
798
                               struct smb_request *req)
799
0
{
800
0
  struct smb_close msg;
801
802
0
  memset(&msg, 0, sizeof(msg));
803
0
  msg.word_count = SMB_WC_CLOSE;
804
0
  msg.fid = smb_swap16(req->fid);
805
806
0
  return smb_send_message(data, smbc, req, SMB_COM_CLOSE, &msg, sizeof(msg));
807
0
}
808
809
static CURLcode smb_send_tree_disconnect(struct Curl_easy *data,
810
                                         struct smb_conn *smbc,
811
                                         struct smb_request *req)
812
0
{
813
0
  struct smb_tree_disconnect msg;
814
0
  memset(&msg, 0, sizeof(msg));
815
0
  return smb_send_message(data, smbc, req, SMB_COM_TREE_DISCONNECT,
816
0
                          &msg, sizeof(msg));
817
0
}
818
819
static CURLcode smb_send_read(struct Curl_easy *data,
820
                              struct smb_conn *smbc,
821
                              struct smb_request *req)
822
0
{
823
0
  curl_off_t offset = data->req.offset;
824
0
  struct smb_read msg;
825
826
0
  memset(&msg, 0, sizeof(msg));
827
0
  msg.word_count = SMB_WC_READ_ANDX;
828
0
  msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
829
0
  msg.fid = smb_swap16(req->fid);
830
0
  msg.offset = smb_swap32((unsigned int) offset);
831
0
  msg.offset_high = smb_swap32((unsigned int) (offset >> 32));
832
0
  msg.min_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
833
0
  msg.max_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
834
835
0
  return smb_send_message(data, smbc, req, SMB_COM_READ_ANDX,
836
0
                          &msg, sizeof(msg));
837
0
}
838
839
static CURLcode smb_send_write(struct Curl_easy *data,
840
                               struct smb_conn *smbc,
841
                               struct smb_request *req)
842
0
{
843
0
  struct smb_write *msg;
844
0
  curl_off_t offset = data->req.offset;
845
0
  curl_off_t upload_size = data->req.size - data->req.bytecount;
846
847
0
  msg = (struct smb_write *)smbc->send_buf;
848
0
  if(upload_size >= MAX_PAYLOAD_SIZE - 1) /* There is one byte of padding */
849
0
    upload_size = MAX_PAYLOAD_SIZE - 1;
850
851
0
  memset(msg, 0, sizeof(*msg));
852
0
  msg->word_count = SMB_WC_WRITE_ANDX;
853
0
  msg->andx.command = SMB_COM_NO_ANDX_COMMAND;
854
0
  msg->fid = smb_swap16(req->fid);
855
0
  msg->offset = smb_swap32((unsigned int) offset);
856
0
  msg->offset_high = smb_swap32((unsigned int) (offset >> 32));
857
0
  msg->data_length = smb_swap16((unsigned short) upload_size);
858
0
  msg->data_offset = smb_swap16(sizeof(*msg) - sizeof(unsigned int));
859
0
  msg->byte_count = smb_swap16((unsigned short) (upload_size + 1));
860
861
0
  smb_format_message(smbc, req, &msg->h, SMB_COM_WRITE_ANDX,
862
0
                     sizeof(*msg) - sizeof(msg->h) + (size_t) upload_size);
863
864
0
  return smb_send(data, smbc, sizeof(*msg), (size_t) upload_size);
865
0
}
866
867
static CURLcode smb_send_and_recv(struct Curl_easy *data,
868
                                  struct smb_conn *smbc, void **msg)
869
0
{
870
0
  CURLcode result;
871
0
  *msg = NULL; /* if it returns early */
872
873
  /* Check if there is data in the transfer buffer */
874
0
  if(!smbc->send_size && smbc->upload_size) {
875
0
    size_t nread = smbc->upload_size > (size_t)MAX_MESSAGE_SIZE ?
876
0
      (size_t)MAX_MESSAGE_SIZE : smbc->upload_size;
877
0
    bool eos;
878
879
0
    result = Curl_client_read(data, smbc->send_buf, nread, &nread, &eos);
880
0
    if(result && result != CURLE_AGAIN)
881
0
      return result;
882
0
    if(!nread)
883
0
      return CURLE_OK;
884
885
0
    smbc->upload_size -= nread;
886
0
    smbc->send_size = nread;
887
0
    smbc->sent = 0;
888
0
  }
889
890
  /* Check if there is data to send */
891
0
  if(smbc->send_size) {
892
0
    result = smb_flush(data, smbc);
893
0
    if(result)
894
0
      return result;
895
0
  }
896
897
  /* Check if there is still data to be sent */
898
0
  if(smbc->send_size || smbc->upload_size)
899
0
    return CURLE_AGAIN;
900
901
0
  return smb_recv_message(data, smbc, msg);
902
0
}
903
904
static CURLcode smb_connection_state(struct Curl_easy *data, bool *done)
905
0
{
906
0
  struct connectdata *conn = data->conn;
907
0
  struct smb_conn *smbc = Curl_conn_meta_get(conn, CURL_META_SMB_CONN);
908
0
  struct smb_request *req = Curl_meta_get(data, CURL_META_SMB_EASY);
909
0
  struct smb_negotiate_response *nrsp;
910
0
  struct smb_header *h;
911
0
  CURLcode result;
912
0
  void *msg = NULL;
913
914
0
  if(!smbc || !req)
915
0
    return CURLE_FAILED_INIT;
916
917
0
  if(smbc->state == SMB_CONNECTING) {
918
0
#ifdef USE_SSL
919
0
    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
0
#endif
928
929
0
    result = smb_send_negotiate(data, smbc, req);
930
0
    if(result) {
931
0
      connclose(conn, "SMB: failed to send negotiate message");
932
0
      return result;
933
0
    }
934
935
0
    conn_state(data, smbc, SMB_NEGOTIATE);
936
0
  }
937
938
  /* Send the previous message and check for a response */
939
0
  result = smb_send_and_recv(data, smbc, &msg);
940
0
  if(result && result != CURLE_AGAIN) {
941
0
    connclose(conn, "SMB: failed to communicate");
942
0
    return result;
943
0
  }
944
945
0
  if(!msg)
946
0
    return CURLE_OK;
947
948
0
  h = msg;
949
950
0
  switch(smbc->state) {
951
0
  case SMB_NEGOTIATE:
952
0
    if((smbc->got < sizeof(*nrsp) + sizeof(smbc->challenge) - 1) ||
953
0
       h->status) {
954
0
      connclose(conn, "SMB: negotiation failed");
955
0
      return CURLE_COULDNT_CONNECT;
956
0
    }
957
0
    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
0
    memcpy(smbc->challenge, nrsp->bytes, sizeof(smbc->challenge));
965
#if defined(__GNUC__) && __GNUC__ >= 13
966
#pragma GCC diagnostic pop
967
#endif
968
0
    smbc->session_key = smb_swap32(nrsp->session_key);
969
0
    result = smb_send_setup(data);
970
0
    if(result) {
971
0
      connclose(conn, "SMB: failed to send setup message");
972
0
      return result;
973
0
    }
974
0
    conn_state(data, smbc, SMB_SETUP);
975
0
    break;
976
977
0
  case SMB_SETUP:
978
0
    if(h->status) {
979
0
      connclose(conn, "SMB: authentication failed");
980
0
      return CURLE_LOGIN_DENIED;
981
0
    }
982
0
    smbc->uid = smb_swap16(h->uid);
983
0
    conn_state(data, smbc, SMB_CONNECTED);
984
0
    *done = TRUE;
985
0
    break;
986
987
0
  default:
988
0
    smb_pop_message(smbc);
989
0
    return CURLE_OK; /* ignore */
990
0
  }
991
992
0
  smb_pop_message(smbc);
993
994
0
  return CURLE_OK;
995
0
}
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
0
{
1003
0
  if(timestamp >= (curl_off_t)116444736000000000) {
1004
0
    timestamp -= (curl_off_t)116444736000000000;
1005
0
    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
0
      *out = (time_t) timestamp;
1014
0
  }
1015
0
  else
1016
0
    *out = 0;
1017
0
}
1018
1019
static CURLcode smb_request_state(struct Curl_easy *data, bool *done)
1020
0
{
1021
0
  struct connectdata *conn = data->conn;
1022
0
  struct smb_conn *smbc = Curl_conn_meta_get(conn, CURL_META_SMB_CONN);
1023
0
  struct smb_request *req = Curl_meta_get(data, CURL_META_SMB_EASY);
1024
0
  struct smb_header *h;
1025
0
  enum smb_req_state next_state = SMB_DONE;
1026
0
  unsigned short len;
1027
0
  unsigned short off;
1028
0
  CURLcode result;
1029
0
  void *msg = NULL;
1030
0
  const struct smb_nt_create_response *smb_m;
1031
1032
0
  if(!smbc || !req)
1033
0
    return CURLE_FAILED_INIT;
1034
1035
0
  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
0
  if(req->state == SMB_REQUESTING) {
1042
0
    result = smb_send_tree_connect(data, smbc, req);
1043
0
    if(result) {
1044
0
      connclose(conn, "SMB: failed to send tree connect message");
1045
0
      return result;
1046
0
    }
1047
1048
0
    request_state(data, SMB_TREE_CONNECT);
1049
0
  }
1050
1051
  /* Send the previous message and check for a response */
1052
0
  result = smb_send_and_recv(data, smbc, &msg);
1053
0
  if(result && result != CURLE_AGAIN) {
1054
0
    connclose(conn, "SMB: failed to communicate");
1055
0
    return result;
1056
0
  }
1057
1058
0
  if(!msg)
1059
0
    return CURLE_OK;
1060
1061
0
  h = msg;
1062
1063
0
  switch(req->state) {
1064
0
  case SMB_TREE_CONNECT:
1065
0
    if(h->status) {
1066
0
      req->result = CURLE_REMOTE_FILE_NOT_FOUND;
1067
0
      if(h->status == smb_swap32(SMB_ERR_NOACCESS))
1068
0
        req->result = CURLE_REMOTE_ACCESS_DENIED;
1069
0
      break;
1070
0
    }
1071
0
    req->tid = smb_swap16(h->tid);
1072
0
    next_state = SMB_OPEN;
1073
0
    break;
1074
1075
0
  case SMB_OPEN:
1076
0
    if(h->status || smbc->got < sizeof(struct smb_nt_create_response)) {
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
      next_state = SMB_TREE_DISCONNECT;
1081
0
      break;
1082
0
    }
1083
0
    smb_m = (const struct smb_nt_create_response*) msg;
1084
0
    req->fid = smb_swap16(smb_m->fid);
1085
0
    data->req.offset = 0;
1086
0
    if(data->state.upload) {
1087
0
      data->req.size = data->state.infilesize;
1088
0
      Curl_pgrsSetUploadSize(data, data->req.size);
1089
0
      next_state = SMB_UPLOAD;
1090
0
    }
1091
0
    else {
1092
0
      data->req.size = smb_swap64(smb_m->end_of_file);
1093
0
      if(data->req.size < 0) {
1094
0
        req->result = CURLE_WEIRD_SERVER_REPLY;
1095
0
        next_state = SMB_CLOSE;
1096
0
      }
1097
0
      else {
1098
0
        Curl_pgrsSetDownloadSize(data, data->req.size);
1099
0
        if(data->set.get_filetime)
1100
0
          get_posix_time(&data->info.filetime, smb_m->last_change_time);
1101
0
        next_state = SMB_DOWNLOAD;
1102
0
      }
1103
0
    }
1104
0
    break;
1105
1106
0
  case SMB_DOWNLOAD:
1107
0
    if(h->status || smbc->got < sizeof(struct smb_header) + 14) {
1108
0
      req->result = CURLE_RECV_ERROR;
1109
0
      next_state = SMB_CLOSE;
1110
0
      break;
1111
0
    }
1112
0
    len = Curl_read16_le(((const unsigned char *) msg) +
1113
0
                         sizeof(struct smb_header) + 11);
1114
0
    off = Curl_read16_le(((const unsigned char *) msg) +
1115
0
                         sizeof(struct smb_header) + 13);
1116
0
    if(len > 0) {
1117
0
      if(off + sizeof(unsigned int) + len > smbc->got) {
1118
0
        failf(data, "Invalid input packet");
1119
0
        result = CURLE_RECV_ERROR;
1120
0
      }
1121
0
      else
1122
0
        result = Curl_client_write(data, CLIENTWRITE_BODY,
1123
0
                                   (char *)msg + off + sizeof(unsigned int),
1124
0
                                   len);
1125
0
      if(result) {
1126
0
        req->result = result;
1127
0
        next_state = SMB_CLOSE;
1128
0
        break;
1129
0
      }
1130
0
    }
1131
0
    data->req.offset += len;
1132
0
    next_state = (len < MAX_PAYLOAD_SIZE) ? SMB_CLOSE : SMB_DOWNLOAD;
1133
0
    break;
1134
1135
0
  case SMB_UPLOAD:
1136
0
    if(h->status || smbc->got < sizeof(struct smb_header) + 6) {
1137
0
      req->result = CURLE_UPLOAD_FAILED;
1138
0
      next_state = SMB_CLOSE;
1139
0
      break;
1140
0
    }
1141
0
    len = Curl_read16_le(((const unsigned char *) msg) +
1142
0
                         sizeof(struct smb_header) + 5);
1143
0
    data->req.bytecount += len;
1144
0
    data->req.offset += len;
1145
0
    Curl_pgrsSetUploadCounter(data, data->req.bytecount);
1146
0
    if(data->req.bytecount >= data->req.size)
1147
0
      next_state = SMB_CLOSE;
1148
0
    else
1149
0
      next_state = SMB_UPLOAD;
1150
0
    break;
1151
1152
0
  case SMB_CLOSE:
1153
    /* We do not care if the close failed, proceed to tree disconnect anyway */
1154
0
    next_state = SMB_TREE_DISCONNECT;
1155
0
    break;
1156
1157
0
  case SMB_TREE_DISCONNECT:
1158
0
    next_state = SMB_DONE;
1159
0
    break;
1160
1161
0
  default:
1162
0
    smb_pop_message(smbc);
1163
0
    return CURLE_OK; /* ignore */
1164
0
  }
1165
1166
0
  smb_pop_message(smbc);
1167
1168
0
  switch(next_state) {
1169
0
  case SMB_OPEN:
1170
0
    result = smb_send_open(data, smbc, req);
1171
0
    break;
1172
1173
0
  case SMB_DOWNLOAD:
1174
0
    result = smb_send_read(data, smbc, req);
1175
0
    break;
1176
1177
0
  case SMB_UPLOAD:
1178
0
    result = smb_send_write(data, smbc, req);
1179
0
    break;
1180
1181
0
  case SMB_CLOSE:
1182
0
    result = smb_send_close(data, smbc, req);
1183
0
    break;
1184
1185
0
  case SMB_TREE_DISCONNECT:
1186
0
    result = smb_send_tree_disconnect(data, smbc, req);
1187
0
    break;
1188
1189
0
  case SMB_DONE:
1190
0
    result = req->result;
1191
0
    *done = TRUE;
1192
0
    break;
1193
1194
0
  default:
1195
0
    break;
1196
0
  }
1197
1198
0
  if(result) {
1199
0
    connclose(conn, "SMB: failed to send message");
1200
0
    return result;
1201
0
  }
1202
1203
0
  request_state(data, next_state);
1204
1205
0
  return CURLE_OK;
1206
0
}
1207
1208
static CURLcode smb_pollset(struct Curl_easy *data,
1209
                            struct easy_pollset *ps)
1210
0
{
1211
0
  return Curl_pollset_add_inout(data, ps, data->conn->sock[FIRSTSOCKET]);
1212
0
}
1213
1214
static CURLcode smb_do(struct Curl_easy *data, bool *done)
1215
0
{
1216
0
  struct connectdata *conn = data->conn;
1217
0
  struct smb_conn *smbc = Curl_conn_meta_get(conn, CURL_META_SMB_CONN);
1218
1219
0
  *done = FALSE;
1220
0
  if(!smbc)
1221
0
    return CURLE_FAILED_INIT;
1222
0
  if(smbc->share)
1223
0
    return CURLE_OK;
1224
0
  return CURLE_URL_MALFORMAT;
1225
0
}
1226
1227
static CURLcode smb_parse_url_path(struct Curl_easy *data,
1228
                                   struct smb_conn *smbc,
1229
                                   struct smb_request *req)
1230
0
{
1231
0
  char *path;
1232
0
  char *slash;
1233
0
  CURLcode result;
1234
1235
  /* URL decode the path */
1236
0
  result = Curl_urldecode(data->state.up.path, 0, &path, NULL, REJECT_CTRL);
1237
0
  if(result)
1238
0
    return result;
1239
1240
  /* Parse the path for the share */
1241
0
  smbc->share = strdup((*path == '/' || *path == '\\') ? path + 1 : path);
1242
0
  free(path);
1243
0
  if(!smbc->share)
1244
0
    return CURLE_OUT_OF_MEMORY;
1245
1246
0
  slash = strchr(smbc->share, '/');
1247
0
  if(!slash)
1248
0
    slash = strchr(smbc->share, '\\');
1249
1250
  /* The share must be present */
1251
0
  if(!slash) {
1252
0
    Curl_safefree(smbc->share);
1253
0
    failf(data, "missing share in URL path for SMB");
1254
0
    return CURLE_URL_MALFORMAT;
1255
0
  }
1256
1257
  /* Parse the path for the file path converting any forward slashes into
1258
     backslashes */
1259
0
  *slash++ = 0;
1260
0
  req->path = slash;
1261
1262
0
  for(; *slash; slash++) {
1263
0
    if(*slash == '/')
1264
0
      *slash = '\\';
1265
0
  }
1266
0
  return CURLE_OK;
1267
0
}
1268
1269
#endif /* CURL_DISABLE_SMB && USE_CURL_NTLM_CORE && SIZEOF_CURL_OFF_T > 4 */