Coverage Report

Created: 2025-12-31 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/samba/source3/libsmb/clientgen.c
Line
Count
Source
1
/*
2
   Unix SMB/CIFS implementation.
3
   SMB client generic functions
4
   Copyright (C) Andrew Tridgell 1994-1998
5
   Copyright (C) Jeremy Allison 2007.
6
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
*/
20
21
#include "includes.h"
22
#include "source3/include/client.h"
23
#include "source3/libsmb/proto.h"
24
#include "../lib/util/tevent_ntstatus.h"
25
#include "../libcli/smb/smb_signing.h"
26
#include "../libcli/smb/smb_seal.h"
27
#include "async_smb.h"
28
#include "../libcli/smb/smbXcli_base.h"
29
#include "../libcli/smb/smb2_negotiate_context.h"
30
#include "../librpc/ndr/libndr.h"
31
#include "../include/client.h"
32
33
/****************************************************************************
34
 Change the timeout (in milliseconds).
35
****************************************************************************/
36
37
unsigned int cli_set_timeout(struct cli_state *cli, unsigned int timeout)
38
0
{
39
0
  unsigned int old_timeout = cli->timeout;
40
0
  DBG_DEBUG("Changing connection timeout for server '%s' from %d (ms) to "
41
0
      "%d (ms).\n",
42
0
      smbXcli_conn_remote_name(cli->conn),
43
0
      cli->timeout,
44
0
      timeout);
45
0
  cli->timeout = timeout;
46
0
  return old_timeout;
47
0
}
48
49
/****************************************************************************
50
 Set the 'backup_intent' flag.
51
****************************************************************************/
52
53
bool cli_set_backup_intent(struct cli_state *cli, bool flag)
54
0
{
55
0
  bool old_state = cli->backup_intent;
56
0
  cli->backup_intent = flag;
57
0
  return old_state;
58
0
}
59
60
/****************************************************************************
61
 Initialise a client structure. Always returns a talloc'ed struct.
62
 Set the signing state (used from the command line).
63
****************************************************************************/
64
65
struct GUID cli_state_client_guid;
66
67
struct cli_state *cli_state_create(TALLOC_CTX *mem_ctx,
68
           struct smbXcli_transport **ptransport,
69
           const char *remote_name,
70
           enum smb_signing_setting signing_state,
71
           int flags)
72
0
{
73
0
  struct cli_state *cli = NULL;
74
0
  bool use_spnego = lp_client_use_spnego();
75
0
  bool force_dos_errors = false;
76
0
  bool force_ascii = false;
77
0
  bool use_level_II_oplocks = false;
78
0
  uint32_t smb1_capabilities = 0;
79
0
  uint32_t smb2_capabilities = 0;
80
0
  struct smb311_capabilities smb3_capabilities =
81
0
    smb311_capabilities_parse(
82
0
      "client",
83
0
      lp_client_smb3_signing_algorithms(),
84
0
      lp_client_smb3_encryption_algorithms(),
85
0
      lp_client_smb_encryption_over_quic());
86
0
  struct GUID client_guid;
87
88
0
  if (!GUID_all_zero(&cli_state_client_guid)) {
89
0
    client_guid = cli_state_client_guid;
90
0
  } else {
91
0
    const char *str = NULL;
92
93
0
    str = lp_parm_const_string(-1, "libsmb", "client_guid", NULL);
94
0
    if (str != NULL) {
95
0
      GUID_from_string(str, &client_guid);
96
0
    } else {
97
0
      client_guid = GUID_random();
98
0
    }
99
0
  }
100
101
  /* Check the effective uid - make sure we are not setuid */
102
0
  if (is_setuid_root()) {
103
0
    DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
104
0
    return NULL;
105
0
  }
106
107
0
  cli = talloc_zero(mem_ctx, struct cli_state);
108
0
  if (!cli) {
109
0
    return NULL;
110
0
  }
111
112
0
  cli->server_domain = talloc_strdup(cli, "");
113
0
  if (!cli->server_domain) {
114
0
    goto error;
115
0
  }
116
0
  cli->server_os = talloc_strdup(cli, "");
117
0
  if (!cli->server_os) {
118
0
    goto error;
119
0
  }
120
0
  cli->server_type = talloc_strdup(cli, "");
121
0
  if (!cli->server_type) {
122
0
    goto error;
123
0
  }
124
125
0
  cli->map_dos_errors = true; /* remove this */
126
0
  cli->timeout = CLIENT_TIMEOUT;
127
128
  /* Set the CLI_FORCE_DOSERR environment variable to test
129
     client routines using DOS errors instead of STATUS32
130
     ones.  This intended only as a temporary hack. */
131
0
  if (getenv("CLI_FORCE_DOSERR")) {
132
0
    force_dos_errors = true;
133
0
  }
134
0
  if (flags & CLI_FULL_CONNECTION_FORCE_DOS_ERRORS) {
135
0
    force_dos_errors = true;
136
0
  }
137
138
0
  if (getenv("CLI_FORCE_ASCII")) {
139
0
    force_ascii = true;
140
0
  }
141
0
  if (!lp_unicode()) {
142
0
    force_ascii = true;
143
0
  }
144
0
  if (flags & CLI_FULL_CONNECTION_FORCE_ASCII) {
145
0
    force_ascii = true;
146
0
  }
147
148
0
  if (flags & CLI_FULL_CONNECTION_DONT_SPNEGO) {
149
0
    use_spnego = false;
150
0
  }
151
152
0
  if (flags & CLI_FULL_CONNECTION_OPLOCKS) {
153
0
    cli->use_oplocks = true;
154
0
  }
155
0
  if (flags & CLI_FULL_CONNECTION_LEVEL_II_OPLOCKS) {
156
0
    use_level_II_oplocks = true;
157
0
  }
158
159
0
  if (signing_state == SMB_SIGNING_IPC_DEFAULT) {
160
    /*
161
     * Ensure for IPC/RPC the default is to require
162
     * signing unless explicitly turned off by the
163
     * administrator.
164
     */
165
0
    signing_state = lp_client_ipc_signing();
166
0
  }
167
168
0
  if (signing_state == SMB_SIGNING_DEFAULT) {
169
0
    signing_state = lp_client_signing();
170
0
  }
171
172
0
  smb1_capabilities = 0;
173
0
  smb1_capabilities |= CAP_LARGE_FILES;
174
0
  smb1_capabilities |= CAP_NT_SMBS | CAP_RPC_REMOTE_APIS;
175
0
  smb1_capabilities |= CAP_LOCK_AND_READ | CAP_NT_FIND;
176
0
  smb1_capabilities |= CAP_DFS | CAP_W2K_SMBS;
177
0
  smb1_capabilities |= CAP_LARGE_READX|CAP_LARGE_WRITEX;
178
0
  smb1_capabilities |= CAP_LWIO;
179
180
0
  if (!force_dos_errors) {
181
0
    smb1_capabilities |= CAP_STATUS32;
182
0
  }
183
184
0
  if (!force_ascii) {
185
0
    smb1_capabilities |= CAP_UNICODE;
186
0
  }
187
188
0
  if (use_spnego) {
189
0
    smb1_capabilities |= CAP_EXTENDED_SECURITY;
190
0
  }
191
192
0
  if (use_level_II_oplocks) {
193
0
    smb1_capabilities |= CAP_LEVEL_II_OPLOCKS;
194
0
  }
195
196
0
  smb2_capabilities = SMB2_CAP_ALL;
197
198
0
  cli->conn = smbXcli_conn_create(cli,
199
0
          ptransport,
200
0
          remote_name,
201
0
          signing_state,
202
0
          smb1_capabilities,
203
0
          &client_guid,
204
0
          smb2_capabilities,
205
0
          &smb3_capabilities);
206
0
  if (cli->conn == NULL) {
207
0
    goto error;
208
0
  }
209
210
0
  cli->smb1.pid = (uint32_t)getpid();
211
0
  cli->smb1.vc_num = cli->smb1.pid;
212
0
  cli->smb1.session = smbXcli_session_create(cli, cli->conn);
213
0
  if (cli->smb1.session == NULL) {
214
0
    goto error;
215
0
  }
216
217
0
  cli->initialised = 1;
218
0
  return cli;
219
220
        /* Clean up after malloc() error */
221
222
0
 error:
223
224
0
  TALLOC_FREE(cli);
225
0
        return NULL;
226
0
}
227
228
/****************************************************************************
229
 Close all pipes open on this session.
230
****************************************************************************/
231
232
static void cli_nt_pipes_close(struct cli_state *cli)
233
0
{
234
0
  while (cli->pipe_list != NULL) {
235
    /*
236
     * No TALLOC_FREE here!
237
     */
238
0
    talloc_free(cli->pipe_list);
239
0
  }
240
0
}
241
242
/****************************************************************************
243
 Shutdown a client structure.
244
****************************************************************************/
245
246
static void _cli_shutdown(struct cli_state *cli)
247
0
{
248
0
  cli_nt_pipes_close(cli);
249
250
  /*
251
   * tell our peer to free his resources.  Without this, when an
252
   * application attempts to do a graceful shutdown and calls
253
   * smbc_free_context() to clean up all connections, some connections
254
   * can remain active on the peer end, until some (long) timeout period
255
   * later.  This tree disconnect forces the peer to clean up, since the
256
   * connection will be going away.
257
   */
258
0
  if (cli_state_has_tcon(cli)) {
259
0
    cli_tdis(cli);
260
0
  }
261
262
0
  smbXcli_conn_disconnect(cli->conn, NT_STATUS_OK);
263
264
0
  TALLOC_FREE(cli);
265
0
}
266
267
void cli_shutdown(struct cli_state *cli)
268
0
{
269
0
  struct cli_state *cli_head;
270
0
  if (cli == NULL) {
271
0
    return;
272
0
  }
273
0
  DLIST_HEAD(cli, cli_head);
274
0
  if (cli_head == cli) {
275
    /*
276
     * head of a DFS list, shutdown all subsidiary DFS
277
     * connections.
278
     */
279
0
    struct cli_state *p, *next;
280
281
0
    for (p = cli_head->next; p; p = next) {
282
0
      next = p->next;
283
0
      DLIST_REMOVE(cli_head, p);
284
0
      _cli_shutdown(p);
285
0
    }
286
0
  } else {
287
0
    DLIST_REMOVE(cli_head, cli);
288
0
  }
289
290
0
  _cli_shutdown(cli);
291
0
}
292
293
uint16_t cli_state_get_vc_num(struct cli_state *cli)
294
0
{
295
0
  return cli->smb1.vc_num;
296
0
}
297
298
/****************************************************************************
299
 Set the PID to use for smb messages. Return the old pid.
300
****************************************************************************/
301
302
uint32_t cli_setpid(struct cli_state *cli, uint32_t pid)
303
0
{
304
0
  uint32_t ret = cli->smb1.pid;
305
0
  cli->smb1.pid = pid;
306
0
  return ret;
307
0
}
308
309
uint32_t cli_getpid(struct cli_state *cli)
310
0
{
311
0
  return cli->smb1.pid;
312
0
}
313
314
bool cli_state_is_encryption_on(struct cli_state *cli)
315
0
{
316
0
  if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
317
0
    return smb1cli_conn_encryption_on(cli->conn);
318
0
  }
319
320
0
  if (cli->smb2.tcon == NULL) {
321
0
    return false;
322
0
  }
323
324
0
  return smb2cli_tcon_is_encryption_on(cli->smb2.tcon);
325
0
}
326
327
bool cli_state_has_tcon(struct cli_state *cli)
328
0
{
329
0
  uint32_t tid;
330
0
  if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
331
0
    if (cli->smb2.tcon == NULL) {
332
0
      return false;
333
0
    }
334
0
    tid = cli_state_get_tid(cli);
335
0
    if (tid == UINT32_MAX) {
336
0
      return false;
337
0
    }
338
0
  } else {
339
0
    if (cli->smb1.tcon == NULL) {
340
0
      return false;
341
0
    }
342
0
    tid = cli_state_get_tid(cli);
343
0
    if (tid == UINT16_MAX) {
344
0
      return false;
345
0
    }
346
0
  }
347
0
  return true;
348
0
}
349
350
uint32_t cli_state_get_tid(struct cli_state *cli)
351
0
{
352
0
  if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
353
0
    return smb2cli_tcon_current_id(cli->smb2.tcon);
354
0
  } else {
355
0
    return (uint32_t)smb1cli_tcon_current_id(cli->smb1.tcon);
356
0
  }
357
0
}
358
359
uint32_t cli_state_set_tid(struct cli_state *cli, uint32_t tid)
360
0
{
361
0
  uint32_t ret;
362
0
  if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
363
0
    ret = smb2cli_tcon_current_id(cli->smb2.tcon);
364
0
    smb2cli_tcon_set_id(cli->smb2.tcon, tid);
365
0
  } else {
366
0
    ret = smb1cli_tcon_current_id(cli->smb1.tcon);
367
0
    smb1cli_tcon_set_id(cli->smb1.tcon, tid);
368
0
  }
369
0
  return ret;
370
0
}
371
372
static struct smbXcli_tcon *cli_state_save_tcon(struct cli_state *cli)
373
0
{
374
  /*
375
   * Note. This used to make a deep copy of either
376
   * cli->smb2.tcon or cli->smb1.tcon, but this leaves
377
   * the original pointer in place which will then get
378
   * TALLOC_FREE()'d when the new connection is made on
379
   * this cli_state.
380
   *
381
   * As there may be pipes open on the old connection with
382
   * talloc'ed state allocated using the tcon pointer as a
383
   * parent we can't deep copy and then free this as that
384
   * closes the open pipes.
385
   *
386
   * This call is used to temporarily swap out a tcon pointer
387
   * to allow a new tcon on the same cli_state.
388
   *
389
   * Just return the raw pointer and set the old value to NULL.
390
   * We know we MUST be calling cli_state_restore_tcon() below
391
   * to restore before closing the session.
392
   *
393
   * See BUG: https://bugzilla.samba.org/show_bug.cgi?id=13992
394
   */
395
0
  struct smbXcli_tcon *tcon_ret = NULL;
396
397
0
  if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
398
0
    tcon_ret = cli->smb2.tcon;
399
0
    cli->smb2.tcon = NULL; /* *Not* TALLOC_FREE(). */
400
0
  } else {
401
0
    tcon_ret = cli->smb1.tcon;
402
0
    cli->smb1.tcon = NULL; /* *Not* TALLOC_FREE(). */
403
0
  }
404
0
  return tcon_ret;
405
0
}
406
407
void cli_state_save_tcon_share(struct cli_state *cli,
408
             struct smbXcli_tcon **_tcon_ret,
409
             char **_sharename_ret)
410
0
{
411
0
  *_tcon_ret = cli_state_save_tcon(cli);
412
  /*
413
   * No talloc_copy as cli->share is already
414
   * allocated off cli.
415
   */
416
0
  *_sharename_ret = cli->share;
417
0
  cli->share = NULL;
418
0
}
419
420
static void cli_state_restore_tcon(struct cli_state *cli,
421
           struct smbXcli_tcon *tcon)
422
0
{
423
0
  if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
424
0
    TALLOC_FREE(cli->smb2.tcon);
425
0
    cli->smb2.tcon = tcon;
426
0
  } else {
427
0
    TALLOC_FREE(cli->smb1.tcon);
428
0
    cli->smb1.tcon = tcon;
429
0
  }
430
0
}
431
432
void cli_state_restore_tcon_share(struct cli_state *cli,
433
          struct smbXcli_tcon *tcon,
434
          char *share)
435
0
{
436
  /* cli->share will have been replaced by a cli_tree_connect() call. */
437
0
  TALLOC_FREE(cli->share);
438
0
  cli->share = share;
439
0
  cli_state_restore_tcon(cli, tcon);
440
0
}
441
442
uint16_t cli_state_get_uid(struct cli_state *cli)
443
0
{
444
0
  return smb1cli_session_current_id(cli->smb1.session);
445
0
}
446
447
uint16_t cli_state_set_uid(struct cli_state *cli, uint16_t uid)
448
0
{
449
0
  uint16_t ret = smb1cli_session_current_id(cli->smb1.session);
450
0
  smb1cli_session_set_id(cli->smb1.session, uid);
451
0
  return ret;
452
0
}
453
454
/****************************************************************************
455
 Set the case sensitivity flag on the packets. Returns old state.
456
****************************************************************************/
457
458
bool cli_set_case_sensitive(struct cli_state *cli, bool case_sensitive)
459
0
{
460
0
  bool ret;
461
0
  uint32_t fs_attrs;
462
0
  struct smbXcli_tcon *tcon;
463
464
0
  if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
465
0
    tcon = cli->smb2.tcon;
466
0
  } else {
467
0
    tcon = cli->smb1.tcon;
468
0
  }
469
470
0
  fs_attrs = smbXcli_tcon_get_fs_attributes(tcon);
471
0
  if (fs_attrs & FILE_CASE_SENSITIVE_SEARCH) {
472
0
    ret = true;
473
0
  } else {
474
0
    ret = false;
475
0
  }
476
0
  if (case_sensitive) {
477
0
    fs_attrs |= FILE_CASE_SENSITIVE_SEARCH;
478
0
  } else {
479
0
    fs_attrs &= ~FILE_CASE_SENSITIVE_SEARCH;
480
0
  }
481
0
  smbXcli_tcon_set_fs_attributes(tcon, fs_attrs);
482
483
0
  return ret;
484
0
}
485
486
uint32_t cli_state_available_size(struct cli_state *cli, uint32_t ofs)
487
0
{
488
0
  uint32_t ret = smb1cli_conn_max_xmit(cli->conn);
489
490
0
  if (ofs >= ret) {
491
0
    return 0;
492
0
  }
493
494
0
  ret -= ofs;
495
496
0
  return ret;
497
0
}
498
499
time_t cli_state_server_time(struct cli_state *cli)
500
0
{
501
0
  NTTIME nt;
502
0
  time_t t;
503
504
0
  nt = smbXcli_conn_server_system_time(cli->conn);
505
0
  t = nt_time_to_unix(nt);
506
507
0
  return t;
508
0
}
509
510
struct cli_echo_state {
511
  uint8_t dummy;
512
};
513
514
static void cli_echo_done1(struct tevent_req *subreq);
515
static void cli_echo_done2(struct tevent_req *subreq);
516
517
struct tevent_req *cli_echo_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
518
         struct cli_state *cli, uint16_t num_echos,
519
         DATA_BLOB data)
520
0
{
521
0
  struct tevent_req *req, *subreq;
522
0
  struct cli_echo_state *state;
523
524
0
  req = tevent_req_create(mem_ctx, &state, struct cli_echo_state);
525
0
  if (req == NULL) {
526
0
    return NULL;
527
0
  }
528
529
0
  if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
530
0
    subreq = smb2cli_echo_send(
531
0
      state, ev, cli->conn, cli->timeout);
532
0
    if (tevent_req_nomem(subreq, req)) {
533
0
      return tevent_req_post(req, ev);
534
0
    }
535
0
    tevent_req_set_callback(subreq, cli_echo_done2, req);
536
0
    return req;
537
0
  }
538
539
0
  subreq = smb1cli_echo_send(
540
0
    state, ev, cli->conn, cli->timeout, num_echos, data);
541
0
  if (tevent_req_nomem(subreq, req)) {
542
0
    return tevent_req_post(req, ev);
543
0
  }
544
0
  tevent_req_set_callback(subreq, cli_echo_done1, req);
545
546
0
  return req;
547
0
}
548
549
static void cli_echo_done1(struct tevent_req *subreq)
550
0
{
551
0
  NTSTATUS status = smb1cli_echo_recv(subreq);
552
0
  return tevent_req_simple_finish_ntstatus(subreq, status);
553
0
}
554
555
static void cli_echo_done2(struct tevent_req *subreq)
556
0
{
557
0
  NTSTATUS status = smb2cli_echo_recv(subreq);
558
0
  return tevent_req_simple_finish_ntstatus(subreq, status);
559
0
}
560
561
/**
562
 * Get the result out from an echo request
563
 * @param[in] req The async_req from cli_echo_send
564
 * @retval Did the server reply correctly?
565
 */
566
567
NTSTATUS cli_echo_recv(struct tevent_req *req)
568
0
{
569
0
  return tevent_req_simple_recv_ntstatus(req);
570
0
}
571
572
/**
573
 * @brief Send/Receive SMBEcho requests
574
 * @param[in] mem_ctx The memory context to put the async_req on
575
 * @param[in] ev  The event context that will call us back
576
 * @param[in] cli The connection to send the echo to
577
 * @param[in] num_echos How many times do we want to get the reply?
578
 * @param[in] data  The data we want to get back
579
 * @retval Did the server reply correctly?
580
 */
581
582
NTSTATUS cli_echo(struct cli_state *cli, uint16_t num_echos, DATA_BLOB data)
583
0
{
584
0
  TALLOC_CTX *frame = talloc_stackframe();
585
0
  struct tevent_context *ev;
586
0
  struct tevent_req *req;
587
0
  NTSTATUS status = NT_STATUS_OK;
588
589
0
  if (smbXcli_conn_has_async_calls(cli->conn)) {
590
    /*
591
     * Can't use sync call while an async call is in flight
592
     */
593
0
    status = NT_STATUS_INVALID_PARAMETER;
594
0
    goto fail;
595
0
  }
596
597
0
  ev = samba_tevent_context_init(frame);
598
0
  if (ev == NULL) {
599
0
    status = NT_STATUS_NO_MEMORY;
600
0
    goto fail;
601
0
  }
602
603
0
  req = cli_echo_send(frame, ev, cli, num_echos, data);
604
0
  if (req == NULL) {
605
0
    status = NT_STATUS_NO_MEMORY;
606
0
    goto fail;
607
0
  }
608
609
0
  if (!tevent_req_poll_ntstatus(req, ev, &status)) {
610
0
    goto fail;
611
0
  }
612
613
0
  status = cli_echo_recv(req);
614
0
 fail:
615
  TALLOC_FREE(frame);
616
0
  return status;
617
0
}