Coverage Report

Created: 2026-04-01 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/samba/source3/libsmb/clifsinfo.c
Line
Count
Source
1
/*
2
   Unix SMB/CIFS implementation.
3
   FS info functions
4
   Copyright (C) Stefan (metze) Metzmacher  2003
5
   Copyright (C) Jeremy Allison 2007
6
   Copyright (C) Andrew Bartlett 2011
7
8
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 3 of the License, or
11
   (at your option) any later version.
12
13
   This program is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
18
   You should have received a copy of the GNU General Public License
19
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
*/
21
22
#include "includes.h"
23
#include "source3/include/client.h"
24
#include "source3/libsmb/proto.h"
25
#include "source3/libsmb/cli_smb2_fnum.h"
26
#include "../lib/util/tevent_ntstatus.h"
27
#include "async_smb.h"
28
#include "trans2.h"
29
#include "auth_generic.h"
30
#include "auth/gensec/gensec.h"
31
#include "../libcli/smb/smbXcli_base.h"
32
#include "auth/credentials/credentials.h"
33
#include "../librpc/gen_ndr/ndr_security.h"
34
35
/****************************************************************************
36
 Get UNIX extensions version info.
37
****************************************************************************/
38
39
struct cli_unix_extensions_version_state {
40
  struct cli_state *cli;
41
  uint16_t setup[1];
42
  uint8_t param[2];
43
  uint16_t major, minor;
44
  uint32_t caplow, caphigh;
45
};
46
47
static void cli_unix_extensions_version_done(struct tevent_req *subreq);
48
49
struct tevent_req *cli_unix_extensions_version_send(TALLOC_CTX *mem_ctx,
50
                struct tevent_context *ev,
51
                struct cli_state *cli)
52
0
{
53
0
  struct tevent_req *req, *subreq;
54
0
  struct cli_unix_extensions_version_state *state;
55
56
0
  req = tevent_req_create(mem_ctx, &state,
57
0
        struct cli_unix_extensions_version_state);
58
0
  if (req == NULL) {
59
0
    return NULL;
60
0
  }
61
0
  state->cli = cli;
62
0
  SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
63
0
  SSVAL(state->param, 0, SMB_QUERY_CIFS_UNIX_INFO);
64
65
0
  subreq = cli_trans_send(state, ev, cli, 0, SMBtrans2,
66
0
        NULL, 0, 0, 0,
67
0
        state->setup, 1, 0,
68
0
        state->param, 2, 0,
69
0
        NULL, 0, 560);
70
0
  if (tevent_req_nomem(subreq, req)) {
71
0
    return tevent_req_post(req, ev);
72
0
  }
73
0
  tevent_req_set_callback(subreq, cli_unix_extensions_version_done, req);
74
0
  return req;
75
0
}
76
77
static void cli_unix_extensions_version_done(struct tevent_req *subreq)
78
0
{
79
0
  struct tevent_req *req = tevent_req_callback_data(
80
0
    subreq, struct tevent_req);
81
0
  struct cli_unix_extensions_version_state *state = tevent_req_data(
82
0
    req, struct cli_unix_extensions_version_state);
83
0
  uint8_t *data;
84
0
  uint32_t num_data;
85
0
  NTSTATUS status;
86
87
0
  status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
88
0
        NULL, 0, NULL, &data, 12, &num_data);
89
0
  TALLOC_FREE(subreq);
90
0
  if (tevent_req_nterror(req, status)) {
91
0
    return;
92
0
  }
93
94
0
  state->major = SVAL(data, 0);
95
0
  state->minor = SVAL(data, 2);
96
0
  state->caplow = IVAL(data, 4);
97
0
  state->caphigh = IVAL(data, 8);
98
0
  TALLOC_FREE(data);
99
0
  tevent_req_done(req);
100
0
}
101
102
NTSTATUS cli_unix_extensions_version_recv(struct tevent_req *req,
103
            uint16_t *pmajor, uint16_t *pminor,
104
            uint32_t *pcaplow,
105
            uint32_t *pcaphigh)
106
0
{
107
0
  struct cli_unix_extensions_version_state *state = tevent_req_data(
108
0
    req, struct cli_unix_extensions_version_state);
109
0
  NTSTATUS status;
110
111
0
  if (tevent_req_is_nterror(req, &status)) {
112
0
    return status;
113
0
  }
114
0
  *pmajor = state->major;
115
0
  *pminor = state->minor;
116
0
  *pcaplow = state->caplow;
117
0
  *pcaphigh = state->caphigh;
118
0
  state->cli->server_posix_capabilities = *pcaplow;
119
0
  return NT_STATUS_OK;
120
0
}
121
122
NTSTATUS cli_unix_extensions_version(struct cli_state *cli, uint16_t *pmajor,
123
             uint16_t *pminor, uint32_t *pcaplow,
124
             uint32_t *pcaphigh)
125
0
{
126
0
  TALLOC_CTX *frame = talloc_stackframe();
127
0
  struct tevent_context *ev;
128
0
  struct tevent_req *req;
129
0
  NTSTATUS status = NT_STATUS_NO_MEMORY;
130
131
0
  if (smbXcli_conn_has_async_calls(cli->conn)) {
132
    /*
133
     * Can't use sync call while an async call is in flight
134
     */
135
0
    status = NT_STATUS_INVALID_PARAMETER;
136
0
    goto fail;
137
0
  }
138
0
  ev = samba_tevent_context_init(frame);
139
0
  if (ev == NULL) {
140
0
    goto fail;
141
0
  }
142
0
  req = cli_unix_extensions_version_send(frame, ev, cli);
143
0
  if (req == NULL) {
144
0
    goto fail;
145
0
  }
146
0
  if (!tevent_req_poll_ntstatus(req, ev, &status)) {
147
0
    goto fail;
148
0
  }
149
0
  status = cli_unix_extensions_version_recv(req, pmajor, pminor, pcaplow,
150
0
              pcaphigh);
151
0
 fail:
152
0
  TALLOC_FREE(frame);
153
0
  return status;
154
0
}
155
156
/****************************************************************************
157
 Set UNIX extensions capabilities.
158
****************************************************************************/
159
160
struct cli_set_unix_extensions_capabilities_state {
161
  struct cli_state *cli;
162
  uint16_t setup[1];
163
  uint8_t param[4];
164
  uint8_t data[12];
165
};
166
167
static void cli_set_unix_extensions_capabilities_done(
168
  struct tevent_req *subreq);
169
170
struct tevent_req *cli_set_unix_extensions_capabilities_send(
171
  TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
172
  uint16_t major, uint16_t minor, uint32_t caplow, uint32_t caphigh)
173
0
{
174
0
  struct tevent_req *req, *subreq;
175
0
  struct cli_set_unix_extensions_capabilities_state *state;
176
177
0
  req = tevent_req_create(
178
0
    mem_ctx, &state,
179
0
    struct cli_set_unix_extensions_capabilities_state);
180
0
  if (req == NULL) {
181
0
    return NULL;
182
0
  }
183
184
0
  state->cli = cli;
185
0
  SSVAL(state->setup+0, 0, TRANSACT2_SETFSINFO);
186
187
0
  SSVAL(state->param, 0, 0);
188
0
  SSVAL(state->param, 2, SMB_SET_CIFS_UNIX_INFO);
189
190
0
  SSVAL(state->data, 0, major);
191
0
  SSVAL(state->data, 2, minor);
192
0
  SIVAL(state->data, 4, caplow);
193
0
  SIVAL(state->data, 8, caphigh);
194
195
0
  subreq = cli_trans_send(state, ev, cli, 0, SMBtrans2,
196
0
        NULL, 0, 0, 0,
197
0
        state->setup, 1, 0,
198
0
        state->param, 4, 0,
199
0
        state->data, 12, 560);
200
0
  if (tevent_req_nomem(subreq, req)) {
201
0
    return tevent_req_post(req, ev);
202
0
  }
203
0
  tevent_req_set_callback(
204
0
    subreq, cli_set_unix_extensions_capabilities_done, req);
205
0
  return req;
206
0
}
207
208
static void cli_set_unix_extensions_capabilities_done(
209
  struct tevent_req *subreq)
210
0
{
211
0
  struct tevent_req *req = tevent_req_callback_data(
212
0
    subreq, struct tevent_req);
213
0
  struct cli_set_unix_extensions_capabilities_state *state = tevent_req_data(
214
0
    req, struct cli_set_unix_extensions_capabilities_state);
215
216
0
  NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
217
0
           NULL, 0, NULL, NULL, 0, NULL);
218
0
  if (NT_STATUS_IS_OK(status)) {
219
0
    state->cli->requested_posix_capabilities = IVAL(state->data, 4);
220
0
  }
221
0
  tevent_req_simple_finish_ntstatus(subreq, status);
222
0
}
223
224
NTSTATUS cli_set_unix_extensions_capabilities_recv(struct tevent_req *req)
225
0
{
226
0
  return tevent_req_simple_recv_ntstatus(req);
227
0
}
228
229
NTSTATUS cli_set_unix_extensions_capabilities(struct cli_state *cli,
230
                uint16_t major, uint16_t minor,
231
                uint32_t caplow, uint32_t caphigh)
232
0
{
233
0
  struct tevent_context *ev;
234
0
  struct tevent_req *req;
235
0
  NTSTATUS status = NT_STATUS_NO_MEMORY;
236
237
0
  if (smbXcli_conn_has_async_calls(cli->conn)) {
238
0
    return NT_STATUS_INVALID_PARAMETER;
239
0
  }
240
0
  ev = samba_tevent_context_init(talloc_tos());
241
0
  if (ev == NULL) {
242
0
    goto fail;
243
0
  }
244
0
  req = cli_set_unix_extensions_capabilities_send(
245
0
    ev, ev, cli, major, minor, caplow, caphigh);
246
0
  if (req == NULL) {
247
0
    goto fail;
248
0
  }
249
0
  if (!tevent_req_poll_ntstatus(req, ev, &status)) {
250
0
    goto fail;
251
0
  }
252
0
  status = cli_set_unix_extensions_capabilities_recv(req);
253
0
fail:
254
0
  TALLOC_FREE(ev);
255
0
  return status;
256
0
}
257
258
struct cli_get_fs_attr_info_state {
259
  uint16_t setup[1];
260
  uint8_t param[2];
261
  uint32_t fs_attr;
262
};
263
264
static void cli_get_fs_attr_info_done(struct tevent_req *subreq);
265
266
struct tevent_req *cli_get_fs_attr_info_send(TALLOC_CTX *mem_ctx,
267
               struct tevent_context *ev,
268
               struct cli_state *cli)
269
0
{
270
0
  struct tevent_req *subreq, *req;
271
0
  struct cli_get_fs_attr_info_state *state;
272
273
0
  req = tevent_req_create(mem_ctx, &state,
274
0
        struct cli_get_fs_attr_info_state);
275
0
  if (req == NULL) {
276
0
    return NULL;
277
0
  }
278
0
  SSVAL(state->setup+0, 0, TRANSACT2_QFSINFO);
279
0
  SSVAL(state->param+0, 0, SMB_QUERY_FS_ATTRIBUTE_INFO);
280
281
0
  subreq = cli_trans_send(state, ev, cli, 0, SMBtrans2,
282
0
        NULL, 0, 0, 0,
283
0
        state->setup, 1, 0,
284
0
        state->param, 2, 0,
285
0
        NULL, 0, 560);
286
0
  if (tevent_req_nomem(subreq, req)) {
287
0
    return tevent_req_post(req, ev);
288
0
  }
289
0
  tevent_req_set_callback(subreq, cli_get_fs_attr_info_done, req);
290
0
  return req;
291
0
}
292
293
static void cli_get_fs_attr_info_done(struct tevent_req *subreq)
294
0
{
295
0
  struct tevent_req *req = tevent_req_callback_data(
296
0
    subreq, struct tevent_req);
297
0
  struct cli_get_fs_attr_info_state *state = tevent_req_data(
298
0
    req, struct cli_get_fs_attr_info_state);
299
0
  uint8_t *data;
300
0
  uint32_t num_data;
301
0
  NTSTATUS status;
302
303
0
  status = cli_trans_recv(subreq, talloc_tos(), NULL, NULL, 0, NULL,
304
0
        NULL, 0, NULL, &data, 12, &num_data);
305
0
  TALLOC_FREE(subreq);
306
0
  if (tevent_req_nterror(req, status)) {
307
0
    return;
308
0
  }
309
0
  state->fs_attr = IVAL(data, 0);
310
0
  TALLOC_FREE(data);
311
0
  tevent_req_done(req);
312
0
}
313
314
NTSTATUS cli_get_fs_attr_info_recv(struct tevent_req *req, uint32_t *fs_attr)
315
0
{
316
0
  struct cli_get_fs_attr_info_state *state = tevent_req_data(
317
0
    req, struct cli_get_fs_attr_info_state);
318
0
  NTSTATUS status;
319
320
0
  if (tevent_req_is_nterror(req, &status)) {
321
0
    return status;
322
0
  }
323
0
  *fs_attr = state->fs_attr;
324
0
  return NT_STATUS_OK;
325
0
}
326
327
NTSTATUS cli_get_fs_attr_info(struct cli_state *cli, uint32_t *fs_attr)
328
0
{
329
0
  struct tevent_context *ev;
330
0
  struct tevent_req *req;
331
0
  NTSTATUS status = NT_STATUS_NO_MEMORY;
332
333
0
  if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
334
0
    return cli_smb2_get_fs_attr_info(cli, fs_attr);
335
0
  }
336
337
0
  if (smbXcli_conn_has_async_calls(cli->conn)) {
338
0
    return NT_STATUS_INVALID_PARAMETER;
339
0
  }
340
0
  ev = samba_tevent_context_init(talloc_tos());
341
0
  if (ev == NULL) {
342
0
    goto fail;
343
0
  }
344
0
  req = cli_get_fs_attr_info_send(ev, ev, cli);
345
0
  if (req == NULL) {
346
0
    goto fail;
347
0
  }
348
0
  if (!tevent_req_poll_ntstatus(req, ev, &status)) {
349
0
    goto fail;
350
0
  }
351
0
  status = cli_get_fs_attr_info_recv(req, fs_attr);
352
0
fail:
353
0
  TALLOC_FREE(ev);
354
0
  return status;
355
0
}
356
357
NTSTATUS cli_get_fs_volume_info(struct cli_state *cli,
358
        TALLOC_CTX *mem_ctx,
359
        char **_volume_name,
360
        uint32_t *pserial_number,
361
        time_t *pdate)
362
0
{
363
0
  NTSTATUS status;
364
0
  uint16_t recv_flags2;
365
0
  uint16_t setup[1];
366
0
  uint8_t param[2];
367
0
  uint8_t *rdata;
368
0
  uint32_t rdata_count;
369
0
  unsigned int nlen;
370
0
  char *volume_name = NULL;
371
372
0
  if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
373
0
    return cli_smb2_get_fs_volume_info(cli,
374
0
            mem_ctx,
375
0
            _volume_name,
376
0
            pserial_number,
377
0
            pdate);
378
0
  }
379
380
0
  SSVAL(setup, 0, TRANSACT2_QFSINFO);
381
0
  SSVAL(param,0,SMB_QUERY_FS_VOLUME_INFO);
382
383
0
  status = cli_trans(talloc_tos(), cli, SMBtrans2,
384
0
         NULL, 0, 0, 0,
385
0
         setup, 1, 0,
386
0
         param, 2, 0,
387
0
         NULL, 0, 560,
388
0
         &recv_flags2,
389
0
         NULL, 0, NULL,
390
0
         NULL, 0, NULL,
391
0
         &rdata, 18, &rdata_count);
392
0
  if (!NT_STATUS_IS_OK(status)) {
393
0
    return status;
394
0
  }
395
396
0
  if (pdate) {
397
0
    struct timespec ts;
398
0
    ts = interpret_long_date(BVAL(rdata, 0));
399
0
    *pdate = ts.tv_sec;
400
0
  }
401
0
  if (pserial_number) {
402
0
    *pserial_number = IVAL(rdata,8);
403
0
  }
404
0
  nlen = IVAL(rdata,12);
405
0
  if (nlen > (rdata_count - 18)) {
406
0
    TALLOC_FREE(rdata);
407
0
    return NT_STATUS_INVALID_NETWORK_RESPONSE;
408
0
  }
409
410
0
  pull_string_talloc(mem_ctx,
411
0
         (const char *)rdata,
412
0
         recv_flags2,
413
0
         &volume_name,
414
0
         rdata + 18,
415
0
         nlen, STR_UNICODE);
416
0
  if (volume_name == NULL) {
417
0
    status = map_nt_error_from_unix(errno);
418
0
    TALLOC_FREE(rdata);
419
0
    return status;
420
0
  }
421
422
  /* todo: but not yet needed
423
   *       return the other stuff
424
   */
425
426
0
  *_volume_name = volume_name;
427
0
  TALLOC_FREE(rdata);
428
0
  return NT_STATUS_OK;
429
0
}
430
431
NTSTATUS cli_get_fs_full_size_info(struct cli_state *cli,
432
           uint64_t *total_allocation_units,
433
           uint64_t *caller_allocation_units,
434
           uint64_t *actual_allocation_units,
435
           uint64_t *sectors_per_allocation_unit,
436
           uint64_t *bytes_per_sector)
437
0
{
438
0
  uint16_t setup[1];
439
0
  uint8_t param[2];
440
0
  uint8_t *rdata = NULL;
441
0
  uint32_t rdata_count;
442
0
  NTSTATUS status;
443
444
0
  if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
445
0
    return cli_smb2_get_fs_full_size_info(cli,
446
0
            total_allocation_units,
447
0
            caller_allocation_units,
448
0
            actual_allocation_units,
449
0
            sectors_per_allocation_unit,
450
0
            bytes_per_sector);
451
0
  }
452
453
0
  SSVAL(setup, 0, TRANSACT2_QFSINFO);
454
0
  SSVAL(param, 0, SMB_FS_FULL_SIZE_INFORMATION);
455
456
0
  status = cli_trans(talloc_tos(), cli, SMBtrans2,
457
0
         NULL, 0, 0, 0,
458
0
         setup, 1, 0, /* setup */
459
0
         param, 2, 0,  /* param */
460
0
         NULL, 0, 560, /* data */
461
0
         NULL,
462
0
         NULL, 0, NULL, /* rsetup */
463
0
         NULL, 0, NULL, /* rparam */
464
0
         &rdata, 32, &rdata_count);  /* rdata */
465
0
  if (!NT_STATUS_IS_OK(status)) {
466
0
    goto fail;
467
0
  }
468
469
0
  if (total_allocation_units) {
470
0
                *total_allocation_units = PULL_LE_U64(rdata, 0);
471
0
  }
472
0
  if (caller_allocation_units) {
473
0
    *caller_allocation_units = PULL_LE_U64(rdata, 8);
474
0
  }
475
0
  if (actual_allocation_units) {
476
0
    *actual_allocation_units = PULL_LE_U64(rdata, 16);
477
0
  }
478
0
  if (sectors_per_allocation_unit) {
479
0
    *sectors_per_allocation_unit = IVAL(rdata,24);
480
0
  }
481
0
  if (bytes_per_sector) {
482
0
    *bytes_per_sector = IVAL(rdata,28);
483
0
  }
484
485
0
fail:
486
0
  TALLOC_FREE(rdata);
487
0
  return status;
488
0
}
489
490
struct cli_get_posix_fs_info_state {
491
  uint16_t setup[1];
492
  uint8_t param[2];
493
  uint32_t optimal_transfer_size;
494
  uint32_t block_size;
495
  uint64_t total_blocks;
496
  uint64_t blocks_available;
497
  uint64_t user_blocks_available;
498
  uint64_t total_file_nodes;
499
  uint64_t free_file_nodes;
500
  uint64_t fs_identifier;
501
};
502
503
static void cli_get_posix_fs_info_done(struct tevent_req *subreq);
504
static void cli_get_posix_fs_info_done2(struct tevent_req *subreq);
505
506
struct tevent_req *cli_get_posix_fs_info_send(TALLOC_CTX *mem_ctx,
507
                struct tevent_context *ev,
508
                struct cli_state *cli)
509
0
{
510
0
  struct tevent_req *req = NULL, *subreq = NULL;
511
0
  struct cli_get_posix_fs_info_state *state = NULL;
512
513
0
  req = tevent_req_create(mem_ctx, &state,
514
0
        struct cli_get_posix_fs_info_state);
515
0
  if (req == NULL) {
516
0
    return NULL;
517
0
  }
518
0
  SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
519
0
  SSVAL(state->param, 0, SMB_QUERY_POSIX_FS_INFO);
520
521
0
  if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
522
0
    subreq = cli_smb2_get_posix_fs_info_send(mem_ctx, ev, cli);
523
0
    if (tevent_req_nomem(subreq, req)) {
524
0
      return tevent_req_post(req, ev);
525
0
    }
526
0
    tevent_req_set_callback(subreq, cli_get_posix_fs_info_done2, req);
527
0
    return req;
528
0
  }
529
530
0
  subreq = cli_trans_send(talloc_tos(),           /* mem ctx. */
531
0
        ev,                     /* event ctx. */
532
0
        cli,                    /* cli_state. */
533
0
        0,      /* additional_flags2 */
534
0
        SMBtrans2,              /* cmd. */
535
0
        NULL,                   /* pipe name. */
536
0
        0,                      /* fid. */
537
0
        0,                      /* function. */
538
0
        0,                      /* flags. */
539
0
        state->setup,           /* setup. */
540
0
        1,                      /* num setup uint16_t words. */
541
0
        0,                      /* max returned setup. */
542
0
        state->param,           /* param. */
543
0
        2,                      /* num param. */
544
0
        0,                      /* max returned param. */
545
0
        NULL,                 /* data. */
546
0
        0,                      /* num data. */
547
0
        560);                   /* max returned data. */
548
549
0
  if (tevent_req_nomem(subreq, req)) {
550
0
    return tevent_req_post(req, ev);
551
0
  }
552
0
  tevent_req_set_callback(subreq, cli_get_posix_fs_info_done, req);
553
0
  return req;
554
0
}
555
556
static void cli_get_posix_fs_info_done(struct tevent_req *subreq)
557
0
{
558
0
  struct tevent_req *req = tevent_req_callback_data(
559
0
    subreq, struct tevent_req);
560
0
  struct cli_get_posix_fs_info_state *state = tevent_req_data(
561
0
    req, struct cli_get_posix_fs_info_state);
562
0
  uint8_t *data = NULL;
563
0
  uint32_t num_data;
564
0
  NTSTATUS status;
565
566
0
  status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
567
0
        NULL, 0, NULL, &data, 56, &num_data);
568
0
  TALLOC_FREE(subreq);
569
0
  if (tevent_req_nterror(req, status)) {
570
0
    return;
571
0
  }
572
573
0
  state->optimal_transfer_size = IVAL(data, 0);
574
0
  state->block_size = IVAL(data,4);
575
0
  state->total_blocks = PULL_LE_U64(data, 8);
576
0
  state->blocks_available = PULL_LE_U64(data, 16);
577
0
  state->user_blocks_available = PULL_LE_U64(data, 24);
578
0
  state->total_file_nodes = PULL_LE_U64(data, 32);
579
0
  state->free_file_nodes = PULL_LE_U64(data, 40);
580
0
  state->fs_identifier = PULL_LE_U64(data, 48);
581
0
  TALLOC_FREE(data);
582
0
  tevent_req_done(req);
583
0
}
584
585
static void cli_get_posix_fs_info_done2(struct tevent_req *subreq)
586
0
{
587
0
  struct tevent_req *req = tevent_req_callback_data(
588
0
    subreq, struct tevent_req);
589
0
  struct cli_get_posix_fs_info_state *state = tevent_req_data(
590
0
    req, struct cli_get_posix_fs_info_state);
591
0
  NTSTATUS status;
592
0
  status = cli_smb2_get_posix_fs_info_recv(subreq,
593
0
             &state->optimal_transfer_size,
594
0
             &state->block_size,
595
0
             &state->total_blocks,
596
0
             &state->blocks_available,
597
0
             &state->user_blocks_available,
598
0
             &state->total_file_nodes,
599
0
             &state->free_file_nodes,
600
0
             &state->fs_identifier);
601
0
  TALLOC_FREE(subreq);
602
0
  if (tevent_req_nterror(req, status)) {
603
0
    return;
604
0
  }
605
606
0
  tevent_req_done(req);
607
0
}
608
609
NTSTATUS cli_get_posix_fs_info_recv(struct tevent_req *req,
610
                  uint32_t *optimal_transfer_size,
611
                  uint32_t *block_size,
612
                  uint64_t *total_blocks,
613
                  uint64_t *blocks_available,
614
                  uint64_t *user_blocks_available,
615
                  uint64_t *total_file_nodes,
616
                  uint64_t *free_file_nodes,
617
                  uint64_t *fs_identifier)
618
0
{
619
0
  struct cli_get_posix_fs_info_state *state = tevent_req_data(
620
0
      req, struct cli_get_posix_fs_info_state);
621
0
  NTSTATUS status;
622
623
0
  if (tevent_req_is_nterror(req, &status)) {
624
0
    tevent_req_received(req);
625
0
    return status;
626
0
  }
627
628
0
  if (optimal_transfer_size) {
629
0
    *optimal_transfer_size = state->optimal_transfer_size;
630
0
  }
631
0
  if (block_size) {
632
0
    *block_size = state->block_size;
633
0
  }
634
0
  if (total_blocks) {
635
0
    *total_blocks = state->total_blocks;
636
0
  }
637
0
  if (blocks_available) {
638
0
    *blocks_available = state->blocks_available;
639
0
  }
640
0
  if (user_blocks_available) {
641
0
    *user_blocks_available = state->user_blocks_available;
642
0
  }
643
0
  if (total_file_nodes) {
644
0
    *total_file_nodes = state->total_file_nodes;
645
0
  }
646
0
  if (free_file_nodes) {
647
0
    *free_file_nodes = state->free_file_nodes;
648
0
  }
649
0
  if (fs_identifier) {
650
0
    *fs_identifier = state->fs_identifier;
651
0
  }
652
0
  tevent_req_received(req);
653
0
  return NT_STATUS_OK;
654
0
}
655
656
NTSTATUS cli_get_posix_fs_info(struct cli_state *cli,
657
             uint32_t *optimal_transfer_size,
658
             uint32_t *block_size,
659
             uint64_t *total_blocks,
660
             uint64_t *blocks_available,
661
             uint64_t *user_blocks_available,
662
             uint64_t *total_file_nodes,
663
             uint64_t *free_file_nodes,
664
             uint64_t *fs_identifier)
665
0
{
666
0
  TALLOC_CTX *frame = talloc_stackframe();
667
0
  struct tevent_context *ev = NULL;
668
0
  struct tevent_req *req = NULL;
669
0
  NTSTATUS status = NT_STATUS_NO_MEMORY;
670
671
0
  if (smbXcli_conn_has_async_calls(cli->conn)) {
672
    /*
673
    * Can't use sync call while an async call is in flight
674
    */
675
0
    status = NT_STATUS_INVALID_PARAMETER;
676
0
    goto fail;
677
0
  }
678
679
0
  ev = samba_tevent_context_init(frame);
680
0
  if (ev == NULL) {
681
0
    goto fail;
682
0
  }
683
684
0
  req = cli_get_posix_fs_info_send(frame, ev, cli);
685
0
  if (req == NULL) {
686
0
    goto fail;
687
0
  }
688
0
  if (!tevent_req_poll_ntstatus(req, ev, &status)) {
689
0
    goto fail;
690
0
  }
691
692
0
  status = cli_get_posix_fs_info_recv(req,
693
0
              optimal_transfer_size,
694
0
              block_size,
695
0
              total_blocks,
696
0
              blocks_available,
697
0
              user_blocks_available,
698
0
              total_file_nodes,
699
0
              free_file_nodes,
700
0
              fs_identifier);
701
702
0
 fail:
703
0
  TALLOC_FREE(frame);
704
0
  return status;
705
0
}
706
707
/****************************************************************************
708
 Do a UNIX extensions SMB_QUERY_POSIX_WHOAMI call.
709
****************************************************************************/
710
711
struct posix_whoami_state {
712
  uint16_t setup[1];
713
  uint8_t param[2];
714
  uint32_t max_rdata;
715
  bool guest;
716
  uint64_t uid;
717
  uint64_t gid;
718
  uint32_t num_gids;
719
  uint64_t *gids;
720
  uint32_t num_sids;
721
  struct dom_sid *sids;
722
};
723
724
static void cli_posix_whoami_done(struct tevent_req *subreq);
725
726
static const uint32_t posix_whoami_max_rdata = 62*1024;
727
728
struct tevent_req *cli_posix_whoami_send(TALLOC_CTX *mem_ctx,
729
          struct tevent_context *ev,
730
          struct cli_state *cli)
731
0
{
732
0
  struct tevent_req *req = NULL, *subreq = NULL;
733
0
  struct posix_whoami_state *state = NULL;
734
735
0
  req = tevent_req_create(mem_ctx, &state, struct posix_whoami_state);
736
0
  if (req == NULL) {
737
0
    return NULL;
738
0
  }
739
740
  /* Setup setup word. */
741
0
  SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
742
0
  SSVAL(state->param, 0, SMB_QUERY_POSIX_WHOAMI);
743
744
0
  state->max_rdata = posix_whoami_max_rdata;
745
746
0
  subreq = cli_trans_send(state,                  /* mem ctx. */
747
0
        ev,                     /* event ctx. */
748
0
        cli,                    /* cli_state. */
749
0
        0,      /* additional_flags2 */
750
0
        SMBtrans2,              /* cmd. */
751
0
        NULL,                   /* pipe name. */
752
0
        -1,                     /* fid. */
753
0
        0,                      /* function. */
754
0
        0,                      /* flags. */
755
0
        state->setup,           /* setup. */
756
0
        1,                      /* num setup uint16_t words. */
757
0
        0,                      /* max returned setup. */
758
0
        state->param,           /* param. */
759
0
        2,                      /* num param. */
760
0
        0,                      /* max returned param. */
761
0
        NULL,                 /* data. */
762
0
        0,                      /* num data. */
763
0
        state->max_rdata);      /* max returned data. */
764
765
0
  if (tevent_req_nomem(subreq, req)) {
766
0
    return tevent_req_post(req, ev);
767
0
  }
768
0
  tevent_req_set_callback(subreq, cli_posix_whoami_done, req);
769
0
  return req;
770
0
}
771
772
static void cli_posix_whoami_done(struct tevent_req *subreq)
773
0
{
774
0
  struct tevent_req *req = tevent_req_callback_data(
775
0
      subreq, struct tevent_req);
776
0
  struct posix_whoami_state *state = tevent_req_data(
777
0
      req, struct posix_whoami_state);
778
0
  uint8_t *rdata = NULL;
779
0
  uint8_t *p = NULL;
780
0
  uint32_t num_rdata = 0;
781
0
  uint32_t i;
782
0
  NTSTATUS status;
783
784
0
  status = cli_trans_recv(subreq,
785
0
        state,
786
0
        NULL,
787
0
        NULL,
788
0
        0,
789
0
        NULL,
790
0
                                NULL,
791
0
        0,
792
0
        NULL,
793
0
        &rdata,
794
0
        40,
795
0
        &num_rdata);
796
0
  TALLOC_FREE(subreq);
797
0
  if (tevent_req_nterror(req, status)) {
798
0
    return;
799
0
  }
800
801
  /*
802
   * Not strictly needed - cli_trans_recv()
803
   * will ensure at least 40 bytes here. Added
804
   * as more of a reminder to be careful when
805
   * parsing network packets in C.
806
   */
807
808
0
  if (num_rdata < 40 || num_rdata > posix_whoami_max_rdata) {
809
0
    tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
810
0
    return;
811
0
  }
812
813
0
  state->guest = (IVAL(rdata, 0) & SMB_WHOAMI_GUEST);
814
0
  state->uid = BVAL(rdata, 8);
815
0
  state->gid = BVAL(rdata, 16);
816
0
  state->num_gids = IVAL(rdata, 24);
817
0
  state->num_sids = IVAL(rdata, 28);
818
819
  /* Ensure the gid array doesn't overflow */
820
0
  if (state->num_gids > (num_rdata - 40) / sizeof(uint64_t)) {
821
0
    tevent_req_nterror(req,
822
0
      NT_STATUS_INVALID_NETWORK_RESPONSE);
823
0
    return;
824
0
  }
825
826
0
  state->gids = talloc_array(state, uint64_t, state->num_gids);
827
0
  if (tevent_req_nomem(state->gids, req)) {
828
0
    return;
829
0
  }
830
0
  state->sids = talloc_array(state, struct dom_sid, state->num_sids);
831
0
  if (tevent_req_nomem(state->sids, req)) {
832
0
    return;
833
0
  }
834
835
0
  p = rdata + 40;
836
837
0
  for (i = 0; i < state->num_gids; i++) {
838
0
    state->gids[i] = BVAL(p, 0);
839
0
    p += 8;
840
0
  }
841
842
0
  num_rdata -= (p - rdata);
843
844
0
  for (i = 0; i < state->num_sids; i++) {
845
0
    size_t sid_size;
846
0
    DATA_BLOB in = data_blob_const(p, num_rdata);
847
0
    enum ndr_err_code ndr_err;
848
849
0
    ndr_err = ndr_pull_struct_blob(&in,
850
0
        state,
851
0
        &state->sids[i],
852
0
        (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
853
0
    if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
854
0
      tevent_req_nterror(req,
855
0
        NT_STATUS_INVALID_NETWORK_RESPONSE);
856
0
      return;
857
0
    }
858
859
0
    sid_size = ndr_size_dom_sid(&state->sids[i], 0);
860
861
0
    if (sid_size > num_rdata) {
862
0
      tevent_req_nterror(req,
863
0
        NT_STATUS_INVALID_NETWORK_RESPONSE);
864
0
      return;
865
0
    }
866
867
0
    p += sid_size;
868
0
    num_rdata -= sid_size;
869
0
  }
870
871
0
  if (num_rdata != 0) {
872
0
    tevent_req_nterror(req,
873
0
      NT_STATUS_INVALID_NETWORK_RESPONSE);
874
0
    return;
875
0
  }
876
877
0
  tevent_req_done(req);
878
0
}
879
880
NTSTATUS cli_posix_whoami_recv(struct tevent_req *req,
881
      TALLOC_CTX *mem_ctx,
882
      uint64_t *puid,
883
      uint64_t *pgid,
884
      uint32_t *pnum_gids,
885
      uint64_t **pgids,
886
      uint32_t *pnum_sids,
887
      struct dom_sid **psids,
888
      bool *pguest)
889
0
{
890
0
  NTSTATUS status;
891
0
  struct posix_whoami_state *state = tevent_req_data(
892
0
      req, struct posix_whoami_state);
893
894
0
  if (tevent_req_is_nterror(req, &status)) {
895
0
    return status;
896
0
  }
897
898
0
  if (puid) {
899
0
    *puid = state->uid;
900
0
  }
901
0
  if (pgid) {
902
0
    *pgid = state->gid;
903
0
  }
904
0
  if (pnum_gids) {
905
0
    *pnum_gids = state->num_gids;
906
0
  }
907
0
  if (pgids) {
908
0
    *pgids = talloc_move(mem_ctx, &state->gids);
909
0
  }
910
0
  if (pnum_sids) {
911
0
    *pnum_sids = state->num_sids;
912
0
  }
913
0
  if (psids) {
914
0
    *psids = talloc_move(mem_ctx, &state->sids);
915
0
  }
916
0
  if (pguest) {
917
0
    *pguest = state->guest;
918
0
  }
919
0
  return NT_STATUS_OK;
920
0
}
921
922
NTSTATUS cli_posix_whoami(struct cli_state *cli,
923
      TALLOC_CTX *mem_ctx,
924
      uint64_t *puid,
925
      uint64_t *pgid,
926
      uint32_t *num_gids,
927
      uint64_t **gids,
928
      uint32_t *num_sids,
929
      struct dom_sid **sids,
930
      bool *pguest)
931
0
{
932
0
        TALLOC_CTX *frame = talloc_stackframe();
933
0
        struct tevent_context *ev = NULL;
934
0
        struct tevent_req *req = NULL;
935
0
        NTSTATUS status = NT_STATUS_OK;
936
937
0
        if (smbXcli_conn_has_async_calls(cli->conn)) {
938
                /*
939
                 * Can't use sync call while an async call is in flight
940
                 */
941
0
                status = NT_STATUS_INVALID_PARAMETER;
942
0
                goto fail;
943
0
        }
944
945
0
        ev = samba_tevent_context_init(frame);
946
0
        if (ev == NULL) {
947
0
                status = NT_STATUS_NO_MEMORY;
948
0
                goto fail;
949
0
        }
950
951
0
        req = cli_posix_whoami_send(frame,
952
0
                                ev,
953
0
                                cli);
954
0
        if (req == NULL) {
955
0
                status = NT_STATUS_NO_MEMORY;
956
0
                goto fail;
957
0
        }
958
959
0
        if (!tevent_req_poll_ntstatus(req, ev, &status)) {
960
0
                goto fail;
961
0
        }
962
963
0
        status = cli_posix_whoami_recv(req,
964
0
      mem_ctx,
965
0
      puid,
966
0
      pgid,
967
0
      num_gids,
968
0
      gids,
969
0
      num_sids,
970
0
      sids,
971
0
      pguest);
972
973
0
 fail:
974
  TALLOC_FREE(frame);
975
0
  return status;
976
0
}