Coverage Report

Created: 2025-10-13 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PROJ/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
0
#define CURL_META_SMB_EASY   "meta:proto:smb:easy"
52
/* meta key for storing protocol meta at connection */
53
0
#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
0
#define SMB_COM_NEGOTIATE             0x72
119
0
#define SMB_COM_SETUP_ANDX            0x73
120
0
#define SMB_COM_TREE_CONNECT_ANDX     0x75
121
0
#define SMB_COM_NT_CREATE_ANDX        0xa2
122
0
#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
0
#define SMB_WC_SETUP_ANDX             0x0d
128
0
#define SMB_WC_TREE_CONNECT_ANDX      0x04
129
0
#define SMB_WC_NT_CREATE_ANDX         0x18
130
131
0
#define SMB_FLAGS_CANONICAL_PATHNAMES 0x10
132
0
#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
0
#define MAX_PAYLOAD_SIZE  0x8000
366
0
#define MAX_MESSAGE_SIZE  (MAX_PAYLOAD_SIZE + 0x1000)
367
0
#define CLIENTNAME        "curl"
368
0
#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
0
#  define smb_swap16(x) (x)
392
0
#  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
0
{
399
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
400
  /* For debug purposes */
401
  static const char * const names[] = {
402
    "SMB_NOT_CONNECTED",
403
    "SMB_CONNECTING",
404
    "SMB_NEGOTIATE",
405
    "SMB_SETUP",
406
    "SMB_CONNECTED",
407
    /* LAST */
408
  };
409
410
  if(smbc->state != newstate)
411
    infof(data, "SMB conn %p state change from %s to %s",
412
          (void *)smbc, names[smbc->state], names[newstate]);
413
#endif
414
0
  (void)data;
415
0
  smbc->state = newstate;
416
0
}
417
418
static void request_state(struct Curl_easy *data,
419
                          enum smb_req_state newstate)
420
0
{
421
0
  struct smb_request *req = Curl_meta_get(data, CURL_META_SMB_EASY);
422
0
  if(req) {
423
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
424
    /* For debug purposes */
425
    static const char * const names[] = {
426
      "SMB_REQUESTING",
427
      "SMB_TREE_CONNECT",
428
      "SMB_OPEN",
429
      "SMB_DOWNLOAD",
430
      "SMB_UPLOAD",
431
      "SMB_CLOSE",
432
      "SMB_TREE_DISCONNECT",
433
      "SMB_DONE",
434
      /* LAST */
435
    };
436
437
    if(req->state != newstate)
438
      infof(data, "SMB request %p state change from %s to %s",
439
            (void *)req, names[req->state], names[newstate]);
440
#endif
441
442
0
    req->state = newstate;
443
0
  }
444
0
}
445
446
static void smb_easy_dtor(void *key, size_t klen, void *entry)
447
0
{
448
0
  struct smb_request *req = entry;
449
0
  (void)key;
450
0
  (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
0
  free(req);
455
0
}
456
457
static void smb_conn_dtor(void *key, size_t klen, void *entry)
458
0
{
459
0
  struct smb_conn *smbc = entry;
460
0
  (void)key;
461
0
  (void)klen;
462
0
  Curl_safefree(smbc->share);
463
0
  Curl_safefree(smbc->domain);
464
0
  Curl_safefree(smbc->recv_buf);
465
0
  Curl_safefree(smbc->send_buf);
466
0
  free(smbc);
467
0
}
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
0
{
474
0
  struct smb_conn *smbc;
475
0
  struct smb_request *req;
476
477
  /* Initialize the connection state */
478
0
  smbc = calloc(1, sizeof(*smbc));
479
0
  if(!smbc ||
480
0
     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
0
  req = calloc(1, sizeof(*req));
485
0
  if(!req ||
486
0
     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
0
  return smb_parse_url_path(data, smbc, req);
491
0
}
492
493
static CURLcode smb_connect(struct Curl_easy *data, bool *done)
494
0
{
495
0
  struct connectdata *conn = data->conn;
496
0
  struct smb_conn *smbc = Curl_conn_meta_get(conn, CURL_META_SMB_CONN);
497
0
  char *slash;
498
499
0
  (void)done;
500
0
  if(!smbc)
501
0
    return CURLE_FAILED_INIT;
502
503
  /* Check we have a username and password to authenticate with */
504
0
  if(!data->state.aptr.user)
505
0
    return CURLE_LOGIN_DENIED;
506
507
  /* Initialize the connection state */
508
0
  smbc->state = SMB_CONNECTING;
509
0
  smbc->recv_buf = malloc(MAX_MESSAGE_SIZE);
510
0
  if(!smbc->recv_buf)
511
0
    return CURLE_OUT_OF_MEMORY;
512
0
  smbc->send_buf = malloc(MAX_MESSAGE_SIZE);
513
0
  if(!smbc->send_buf)
514
0
    return CURLE_OUT_OF_MEMORY;
515
516
  /* Multiple requests are allowed with this connection */
517
0
  connkeep(conn, "SMB default");
518
519
  /* Parse the username, domain, and password */
520
0
  slash = strchr(conn->user, '/');
521
0
  if(!slash)
522
0
    slash = strchr(conn->user, '\\');
523
524
0
  if(slash) {
525
0
    smbc->user = slash + 1;
526
0
    smbc->domain = strdup(conn->user);
527
0
    if(!smbc->domain)
528
0
      return CURLE_OUT_OF_MEMORY;
529
0
    smbc->domain[slash - conn->user] = 0;
530
0
  }
531
0
  else {
532
0
    smbc->user = conn->user;
533
0
    smbc->domain = strdup(conn->host.name);
534
0
    if(!smbc->domain)
535
0
      return CURLE_OUT_OF_MEMORY;
536
0
  }
537
538
0
  return CURLE_OK;
539
0
}
540
541
static CURLcode smb_recv_message(struct Curl_easy *data,
542
                                 struct smb_conn *smbc,
543
                                 void **msg)
544
0
{
545
0
  char *buf = smbc->recv_buf;
546
0
  size_t bytes_read;
547
0
  size_t nbt_size;
548
0
  size_t msg_size = sizeof(struct smb_header);
549
0
  size_t len = MAX_MESSAGE_SIZE - smbc->got;
550
0
  CURLcode result;
551
552
0
  result = Curl_xfer_recv(data, buf + smbc->got, len, &bytes_read);
553
0
  if(result)
554
0
    return result;
555
556
0
  if(!bytes_read)
557
0
    return CURLE_OK;
558
559
0
  smbc->got += bytes_read;
560
561
  /* Check for a 32-bit nbt header */
562
0
  if(smbc->got < sizeof(unsigned int))
563
0
    return CURLE_OK;
564
565
0
  nbt_size = Curl_read16_be((const unsigned char *)
566
0
                            (buf + sizeof(unsigned short))) +
567
0
    sizeof(unsigned int);
568
0
  if(nbt_size > MAX_MESSAGE_SIZE) {
569
0
    failf(data, "too large NetBIOS frame size %zu", nbt_size);
570
0
    return CURLE_RECV_ERROR;
571
0
  }
572
0
  else if(nbt_size < msg_size) {
573
    /* Each SMB message must be at least this large, e.g. 32 bytes */
574
0
    failf(data, "too small NetBIOS frame size %zu", nbt_size);
575
0
    return CURLE_RECV_ERROR;
576
0
  }
577
578
0
  if(smbc->got < nbt_size)
579
0
    return CURLE_OK;
580
581
0
  if(nbt_size >= msg_size + 1) {
582
    /* Add the word count */
583
0
    msg_size += 1 + ((unsigned char) buf[msg_size]) * sizeof(unsigned short);
584
0
    if(nbt_size >= msg_size + sizeof(unsigned short)) {
585
      /* Add the byte count */
586
0
      msg_size += sizeof(unsigned short) +
587
0
        Curl_read16_le((const unsigned char *)&buf[msg_size]);
588
0
      if(nbt_size < msg_size)
589
0
        return CURLE_RECV_ERROR;
590
0
    }
591
0
  }
592
593
0
  *msg = buf;
594
595
0
  return CURLE_OK;
596
0
}
597
598
static void smb_pop_message(struct smb_conn *smbc)
599
0
{
600
0
  smbc->got = 0;
601
0
}
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
0
{
608
0
  const unsigned int pid = 0xbad71d; /* made up */
609
610
0
  memset(h, 0, sizeof(*h));
611
0
  h->nbt_length = htons((unsigned short) (sizeof(*h) - sizeof(unsigned int) +
612
0
                                          len));
613
0
  memcpy((char *)h->magic, "\xffSMB", 4);
614
0
  h->command = cmd;
615
0
  h->flags = SMB_FLAGS_CANONICAL_PATHNAMES | SMB_FLAGS_CASELESS_PATHNAMES;
616
0
  h->flags2 = smb_swap16(SMB_FLAGS2_IS_LONG_NAME | SMB_FLAGS2_KNOWS_LONG_NAME);
617
0
  h->uid = smb_swap16(smbc->uid);
618
0
  h->tid = smb_swap16(req->tid);
619
0
  h->pid_high = smb_swap16((unsigned short)(pid >> 16));
620
0
  h->pid = smb_swap16((unsigned short) pid);
621
0
}
622
623
static CURLcode smb_send(struct Curl_easy *data, struct smb_conn *smbc,
624
                         size_t len, size_t upload_size)
625
0
{
626
0
  size_t bytes_written;
627
0
  CURLcode result;
628
629
0
  result = Curl_xfer_send(data, smbc->send_buf, len, FALSE, &bytes_written);
630
0
  if(result)
631
0
    return result;
632
633
0
  if(bytes_written != len) {
634
0
    smbc->send_size = len;
635
0
    smbc->sent = bytes_written;
636
0
  }
637
638
0
  smbc->upload_size = upload_size;
639
640
0
  return CURLE_OK;
641
0
}
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
0
{
671
0
  smb_format_message(smbc, req, (struct smb_header *)smbc->send_buf,
672
0
                     cmd, msg_len);
673
0
  if((sizeof(struct smb_header) + msg_len) > MAX_MESSAGE_SIZE) {
674
0
    DEBUGASSERT(0);
675
0
    return CURLE_SEND_ERROR;
676
0
  }
677
0
  memcpy(smbc->send_buf + sizeof(struct smb_header), msg, msg_len);
678
679
0
  return smb_send(data, smbc, sizeof(struct smb_header) + msg_len, 0);
680
0
}
681
682
static CURLcode smb_send_negotiate(struct Curl_easy *data,
683
                                   struct smb_conn *smbc,
684
                                   struct smb_request *req)
685
0
{
686
0
  const char *msg = "\x00\x0c\x00\x02NT LM 0.12";
687
688
0
  return smb_send_message(data, smbc, req, SMB_COM_NEGOTIATE, msg, 15);
689
0
}
690
691
static CURLcode smb_send_setup(struct Curl_easy *data)
692
0
{
693
0
  struct connectdata *conn = data->conn;
694
0
  struct smb_conn *smbc = Curl_conn_meta_get(conn, CURL_META_SMB_CONN);
695
0
  struct smb_request *req = Curl_meta_get(data, CURL_META_SMB_EASY);
696
0
  struct smb_setup msg;
697
0
  char *p = msg.bytes;
698
0
  unsigned char lm_hash[21];
699
0
  unsigned char lm[24];
700
0
  unsigned char nt_hash[21];
701
0
  unsigned char nt[24];
702
0
  size_t byte_count;
703
704
0
  if(!smbc || !req)
705
0
    return CURLE_FAILED_INIT;
706
707
0
  byte_count = sizeof(lm) + sizeof(nt) +
708
0
    strlen(smbc->user) + strlen(smbc->domain) +
709
0
    strlen(CURL_OS) + strlen(CLIENTNAME) + 4; /* 4 null chars */
710
0
  if(byte_count > sizeof(msg.bytes))
711
0
    return CURLE_FILESIZE_EXCEEDED;
712
713
0
  Curl_ntlm_core_mk_lm_hash(conn->passwd, lm_hash);
714
0
  Curl_ntlm_core_lm_resp(lm_hash, smbc->challenge, lm);
715
0
  Curl_ntlm_core_mk_nt_hash(conn->passwd, nt_hash);
716
0
  Curl_ntlm_core_lm_resp(nt_hash, smbc->challenge, nt);
717
718
0
  memset(&msg, 0, sizeof(msg) - sizeof(msg.bytes));
719
0
  msg.word_count = SMB_WC_SETUP_ANDX;
720
0
  msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
721
0
  msg.max_buffer_size = smb_swap16(MAX_MESSAGE_SIZE);
722
0
  msg.max_mpx_count = smb_swap16(1);
723
0
  msg.vc_number = smb_swap16(1);
724
0
  msg.session_key = smb_swap32(smbc->session_key);
725
0
  msg.capabilities = smb_swap32(SMB_CAP_LARGE_FILES);
726
0
  msg.lengths[0] = smb_swap16(sizeof(lm));
727
0
  msg.lengths[1] = smb_swap16(sizeof(nt));
728
0
  memcpy(p, lm, sizeof(lm));
729
0
  p += sizeof(lm);
730
0
  memcpy(p, nt, sizeof(nt));
731
0
  p += sizeof(nt);
732
0
  p += curl_msnprintf(p, byte_count - sizeof(nt) - sizeof(lm),
733
0
                      "%s%c"  /* user */
734
0
                      "%s%c"  /* domain */
735
0
                      "%s%c"  /* OS */
736
0
                      "%s", /* client name */
737
0
                      smbc->user, 0, smbc->domain, 0, CURL_OS, 0, CLIENTNAME);
738
0
  p++; /* count the final null-termination */
739
0
  DEBUGASSERT(byte_count == (size_t)(p - msg.bytes));
740
0
  msg.byte_count = smb_swap16((unsigned short)byte_count);
741
742
0
  return smb_send_message(data, smbc, req, SMB_COM_SETUP_ANDX, &msg,
743
0
                          sizeof(msg) - sizeof(msg.bytes) + byte_count);
744
0
}
745
746
static CURLcode smb_send_tree_connect(struct Curl_easy *data,
747
                                      struct smb_conn *smbc,
748
                                      struct smb_request *req)
749
0
{
750
0
  struct smb_tree_connect msg;
751
0
  struct connectdata *conn = data->conn;
752
0
  char *p = msg.bytes;
753
0
  const size_t byte_count = strlen(conn->host.name) + strlen(smbc->share) +
754
0
    strlen(SERVICENAME) + 5; /* 2 nulls and 3 backslashes */
755
756
0
  if(byte_count > sizeof(msg.bytes))
757
0
    return CURLE_FILESIZE_EXCEEDED;
758
759
0
  memset(&msg, 0, sizeof(msg) - sizeof(msg.bytes));
760
0
  msg.word_count = SMB_WC_TREE_CONNECT_ANDX;
761
0
  msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
762
0
  msg.pw_len = 0;
763
764
0
  p += curl_msnprintf(p, byte_count,
765
0
                      "\\\\%s\\"  /* hostname */
766
0
                      "%s%c"      /* share */
767
0
                      "%s",       /* service */
768
0
                      conn->host.name, smbc->share, 0, SERVICENAME);
769
0
  p++; /* count the final null-termination */
770
0
  DEBUGASSERT(byte_count == (size_t)(p - msg.bytes));
771
0
  msg.byte_count = smb_swap16((unsigned short)byte_count);
772
773
0
  return smb_send_message(data, smbc, req, SMB_COM_TREE_CONNECT_ANDX, &msg,
774
0
                          sizeof(msg) - sizeof(msg.bytes) + byte_count);
775
0
}
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
0
{
881
0
  CURLcode result;
882
0
  *msg = NULL; /* if it returns early */
883
884
  /* Check if there is data in the transfer buffer */
885
0
  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
0
  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
0
  if(smbc->send_size || smbc->upload_size)
910
0
    return CURLE_AGAIN;
911
912
0
  return smb_recv_message(data, smbc, msg);
913
0
}
914
915
static CURLcode smb_connection_state(struct Curl_easy *data, bool *done)
916
0
{
917
0
  struct connectdata *conn = data->conn;
918
0
  struct smb_conn *smbc = Curl_conn_meta_get(conn, CURL_META_SMB_CONN);
919
0
  struct smb_request *req = Curl_meta_get(data, CURL_META_SMB_EASY);
920
0
  struct smb_negotiate_response *nrsp;
921
0
  struct smb_header *h;
922
0
  CURLcode result;
923
0
  void *msg = NULL;
924
925
0
  if(!smbc || !req)
926
0
    return CURLE_FAILED_INIT;
927
928
0
  if(smbc->state == SMB_CONNECTING) {
929
0
#ifdef USE_SSL
930
0
    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
0
#endif
939
940
0
    result = smb_send_negotiate(data, smbc, req);
941
0
    if(result) {
942
0
      connclose(conn, "SMB: failed to send negotiate message");
943
0
      return result;
944
0
    }
945
946
0
    conn_state(data, smbc, SMB_NEGOTIATE);
947
0
  }
948
949
  /* Send the previous message and check for a response */
950
0
  result = smb_send_and_recv(data, smbc, &msg);
951
0
  if(result && result != CURLE_AGAIN) {
952
0
    connclose(conn, "SMB: failed to communicate");
953
0
    return result;
954
0
  }
955
956
0
  if(!msg)
957
0
    return CURLE_OK;
958
959
0
  h = msg;
960
961
0
  switch(smbc->state) {
962
0
  case SMB_NEGOTIATE:
963
0
    if((smbc->got < sizeof(*nrsp) + sizeof(smbc->challenge) - 1) ||
964
0
       h->status) {
965
0
      connclose(conn, "SMB: negotiation failed");
966
0
      return CURLE_COULDNT_CONNECT;
967
0
    }
968
0
    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
0
    memcpy(smbc->challenge, nrsp->bytes, sizeof(smbc->challenge));
976
#if defined(__GNUC__) && __GNUC__ >= 13
977
#pragma GCC diagnostic pop
978
#endif
979
0
    smbc->session_key = smb_swap32(nrsp->session_key);
980
0
    result = smb_send_setup(data);
981
0
    if(result) {
982
0
      connclose(conn, "SMB: failed to send setup message");
983
0
      return result;
984
0
    }
985
0
    conn_state(data, smbc, SMB_SETUP);
986
0
    break;
987
988
0
  case SMB_SETUP:
989
0
    if(h->status) {
990
0
      connclose(conn, "SMB: authentication failed");
991
0
      return CURLE_LOGIN_DENIED;
992
0
    }
993
0
    smbc->uid = smb_swap16(h->uid);
994
0
    conn_state(data, smbc, SMB_CONNECTED);
995
0
    *done = TRUE;
996
0
    break;
997
998
0
  default:
999
0
    smb_pop_message(smbc);
1000
0
    return CURLE_OK; /* ignore */
1001
0
  }
1002
1003
0
  smb_pop_message(smbc);
1004
1005
0
  return CURLE_OK;
1006
0
}
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
0
{
1032
0
  struct connectdata *conn = data->conn;
1033
0
  struct smb_conn *smbc = Curl_conn_meta_get(conn, CURL_META_SMB_CONN);
1034
0
  struct smb_request *req = Curl_meta_get(data, CURL_META_SMB_EASY);
1035
0
  struct smb_header *h;
1036
0
  enum smb_req_state next_state = SMB_DONE;
1037
0
  unsigned short len;
1038
0
  unsigned short off;
1039
0
  CURLcode result;
1040
0
  void *msg = NULL;
1041
0
  const struct smb_nt_create_response *smb_m;
1042
1043
0
  if(!smbc || !req)
1044
0
    return CURLE_FAILED_INIT;
1045
1046
0
  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
0
  if(req->state == SMB_REQUESTING) {
1053
0
    result = smb_send_tree_connect(data, smbc, req);
1054
0
    if(result) {
1055
0
      connclose(conn, "SMB: failed to send tree connect message");
1056
0
      return result;
1057
0
    }
1058
1059
0
    request_state(data, SMB_TREE_CONNECT);
1060
0
  }
1061
1062
  /* Send the previous message and check for a response */
1063
0
  result = smb_send_and_recv(data, smbc, &msg);
1064
0
  if(result && result != CURLE_AGAIN) {
1065
0
    connclose(conn, "SMB: failed to communicate");
1066
0
    return result;
1067
0
  }
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
0
{
1222
0
  return Curl_pollset_add_inout(data, ps, data->conn->sock[FIRSTSOCKET]);
1223
0
}
1224
1225
static CURLcode smb_do(struct Curl_easy *data, bool *done)
1226
0
{
1227
0
  struct connectdata *conn = data->conn;
1228
0
  struct smb_conn *smbc = Curl_conn_meta_get(conn, CURL_META_SMB_CONN);
1229
1230
0
  *done = FALSE;
1231
0
  if(!smbc)
1232
0
    return CURLE_FAILED_INIT;
1233
0
  if(smbc->share)
1234
0
    return CURLE_OK;
1235
0
  return CURLE_URL_MALFORMAT;
1236
0
}
1237
1238
static CURLcode smb_parse_url_path(struct Curl_easy *data,
1239
                                   struct smb_conn *smbc,
1240
                                   struct smb_request *req)
1241
0
{
1242
0
  char *path;
1243
0
  char *slash;
1244
0
  CURLcode result;
1245
1246
  /* URL decode the path */
1247
0
  result = Curl_urldecode(data->state.up.path, 0, &path, NULL, REJECT_CTRL);
1248
0
  if(result)
1249
0
    return result;
1250
1251
  /* Parse the path for the share */
1252
0
  smbc->share = strdup((*path == '/' || *path == '\\') ? path + 1 : path);
1253
0
  free(path);
1254
0
  if(!smbc->share)
1255
0
    return CURLE_OUT_OF_MEMORY;
1256
1257
0
  slash = strchr(smbc->share, '/');
1258
0
  if(!slash)
1259
0
    slash = strchr(smbc->share, '\\');
1260
1261
  /* The share must be present */
1262
0
  if(!slash) {
1263
0
    Curl_safefree(smbc->share);
1264
0
    failf(data, "missing share in URL path for SMB");
1265
0
    return CURLE_URL_MALFORMAT;
1266
0
  }
1267
1268
  /* Parse the path for the file path converting any forward slashes into
1269
     backslashes */
1270
0
  *slash++ = 0;
1271
0
  req->path = slash;
1272
1273
0
  for(; *slash; slash++) {
1274
0
    if(*slash == '/')
1275
0
      *slash = '\\';
1276
0
  }
1277
0
  return CURLE_OK;
1278
0
}
1279
1280
#endif /* CURL_DISABLE_SMB && USE_CURL_NTLM_CORE && SIZEOF_CURL_OFF_T > 4 */