ctdb-server: Clean up connection tracking functions
[samba4-gss.git] / source3 / libsmb / clifsinfo.c
blob4b82a0bd1e971c3b4d8808cc473ab5fc8a08b2c9
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
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.
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.
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/>.
22 #include "includes.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "trans2.h"
27 #include "auth_generic.h"
28 #include "auth/gensec/gensec.h"
29 #include "../libcli/smb/smbXcli_base.h"
30 #include "auth/credentials/credentials.h"
31 #include "../librpc/gen_ndr/ndr_security.h"
33 /****************************************************************************
34 Get UNIX extensions version info.
35 ****************************************************************************/
37 struct cli_unix_extensions_version_state {
38 struct cli_state *cli;
39 uint16_t setup[1];
40 uint8_t param[2];
41 uint16_t major, minor;
42 uint32_t caplow, caphigh;
45 static void cli_unix_extensions_version_done(struct tevent_req *subreq);
47 struct tevent_req *cli_unix_extensions_version_send(TALLOC_CTX *mem_ctx,
48 struct tevent_context *ev,
49 struct cli_state *cli)
51 struct tevent_req *req, *subreq;
52 struct cli_unix_extensions_version_state *state;
54 req = tevent_req_create(mem_ctx, &state,
55 struct cli_unix_extensions_version_state);
56 if (req == NULL) {
57 return NULL;
59 state->cli = cli;
60 SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
61 SSVAL(state->param, 0, SMB_QUERY_CIFS_UNIX_INFO);
63 subreq = cli_trans_send(state, ev, cli, 0, SMBtrans2,
64 NULL, 0, 0, 0,
65 state->setup, 1, 0,
66 state->param, 2, 0,
67 NULL, 0, 560);
68 if (tevent_req_nomem(subreq, req)) {
69 return tevent_req_post(req, ev);
71 tevent_req_set_callback(subreq, cli_unix_extensions_version_done, req);
72 return req;
75 static void cli_unix_extensions_version_done(struct tevent_req *subreq)
77 struct tevent_req *req = tevent_req_callback_data(
78 subreq, struct tevent_req);
79 struct cli_unix_extensions_version_state *state = tevent_req_data(
80 req, struct cli_unix_extensions_version_state);
81 uint8_t *data;
82 uint32_t num_data;
83 NTSTATUS status;
85 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
86 NULL, 0, NULL, &data, 12, &num_data);
87 TALLOC_FREE(subreq);
88 if (tevent_req_nterror(req, status)) {
89 return;
92 state->major = SVAL(data, 0);
93 state->minor = SVAL(data, 2);
94 state->caplow = IVAL(data, 4);
95 state->caphigh = IVAL(data, 8);
96 TALLOC_FREE(data);
97 tevent_req_done(req);
100 NTSTATUS cli_unix_extensions_version_recv(struct tevent_req *req,
101 uint16_t *pmajor, uint16_t *pminor,
102 uint32_t *pcaplow,
103 uint32_t *pcaphigh)
105 struct cli_unix_extensions_version_state *state = tevent_req_data(
106 req, struct cli_unix_extensions_version_state);
107 NTSTATUS status;
109 if (tevent_req_is_nterror(req, &status)) {
110 return status;
112 *pmajor = state->major;
113 *pminor = state->minor;
114 *pcaplow = state->caplow;
115 *pcaphigh = state->caphigh;
116 state->cli->server_posix_capabilities = *pcaplow;
117 return NT_STATUS_OK;
120 NTSTATUS cli_unix_extensions_version(struct cli_state *cli, uint16_t *pmajor,
121 uint16_t *pminor, uint32_t *pcaplow,
122 uint32_t *pcaphigh)
124 TALLOC_CTX *frame = talloc_stackframe();
125 struct tevent_context *ev;
126 struct tevent_req *req;
127 NTSTATUS status = NT_STATUS_NO_MEMORY;
129 if (smbXcli_conn_has_async_calls(cli->conn)) {
131 * Can't use sync call while an async call is in flight
133 status = NT_STATUS_INVALID_PARAMETER;
134 goto fail;
136 ev = samba_tevent_context_init(frame);
137 if (ev == NULL) {
138 goto fail;
140 req = cli_unix_extensions_version_send(frame, ev, cli);
141 if (req == NULL) {
142 goto fail;
144 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
145 goto fail;
147 status = cli_unix_extensions_version_recv(req, pmajor, pminor, pcaplow,
148 pcaphigh);
149 fail:
150 TALLOC_FREE(frame);
151 return status;
154 /****************************************************************************
155 Set UNIX extensions capabilities.
156 ****************************************************************************/
158 struct cli_set_unix_extensions_capabilities_state {
159 struct cli_state *cli;
160 uint16_t setup[1];
161 uint8_t param[4];
162 uint8_t data[12];
165 static void cli_set_unix_extensions_capabilities_done(
166 struct tevent_req *subreq);
168 struct tevent_req *cli_set_unix_extensions_capabilities_send(
169 TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
170 uint16_t major, uint16_t minor, uint32_t caplow, uint32_t caphigh)
172 struct tevent_req *req, *subreq;
173 struct cli_set_unix_extensions_capabilities_state *state;
175 req = tevent_req_create(
176 mem_ctx, &state,
177 struct cli_set_unix_extensions_capabilities_state);
178 if (req == NULL) {
179 return NULL;
182 state->cli = cli;
183 SSVAL(state->setup+0, 0, TRANSACT2_SETFSINFO);
185 SSVAL(state->param, 0, 0);
186 SSVAL(state->param, 2, SMB_SET_CIFS_UNIX_INFO);
188 SSVAL(state->data, 0, major);
189 SSVAL(state->data, 2, minor);
190 SIVAL(state->data, 4, caplow);
191 SIVAL(state->data, 8, caphigh);
193 subreq = cli_trans_send(state, ev, cli, 0, SMBtrans2,
194 NULL, 0, 0, 0,
195 state->setup, 1, 0,
196 state->param, 4, 0,
197 state->data, 12, 560);
198 if (tevent_req_nomem(subreq, req)) {
199 return tevent_req_post(req, ev);
201 tevent_req_set_callback(
202 subreq, cli_set_unix_extensions_capabilities_done, req);
203 return req;
206 static void cli_set_unix_extensions_capabilities_done(
207 struct tevent_req *subreq)
209 struct tevent_req *req = tevent_req_callback_data(
210 subreq, struct tevent_req);
211 struct cli_set_unix_extensions_capabilities_state *state = tevent_req_data(
212 req, struct cli_set_unix_extensions_capabilities_state);
214 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
215 NULL, 0, NULL, NULL, 0, NULL);
216 if (NT_STATUS_IS_OK(status)) {
217 state->cli->requested_posix_capabilities = IVAL(state->data, 4);
219 tevent_req_simple_finish_ntstatus(subreq, status);
222 NTSTATUS cli_set_unix_extensions_capabilities_recv(struct tevent_req *req)
224 return tevent_req_simple_recv_ntstatus(req);
227 NTSTATUS cli_set_unix_extensions_capabilities(struct cli_state *cli,
228 uint16_t major, uint16_t minor,
229 uint32_t caplow, uint32_t caphigh)
231 struct tevent_context *ev;
232 struct tevent_req *req;
233 NTSTATUS status = NT_STATUS_NO_MEMORY;
235 if (smbXcli_conn_has_async_calls(cli->conn)) {
236 return NT_STATUS_INVALID_PARAMETER;
238 ev = samba_tevent_context_init(talloc_tos());
239 if (ev == NULL) {
240 goto fail;
242 req = cli_set_unix_extensions_capabilities_send(
243 ev, ev, cli, major, minor, caplow, caphigh);
244 if (req == NULL) {
245 goto fail;
247 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
248 goto fail;
250 status = cli_set_unix_extensions_capabilities_recv(req);
251 fail:
252 TALLOC_FREE(ev);
253 return status;
256 struct cli_get_fs_attr_info_state {
257 uint16_t setup[1];
258 uint8_t param[2];
259 uint32_t fs_attr;
262 static void cli_get_fs_attr_info_done(struct tevent_req *subreq);
264 struct tevent_req *cli_get_fs_attr_info_send(TALLOC_CTX *mem_ctx,
265 struct tevent_context *ev,
266 struct cli_state *cli)
268 struct tevent_req *subreq, *req;
269 struct cli_get_fs_attr_info_state *state;
271 req = tevent_req_create(mem_ctx, &state,
272 struct cli_get_fs_attr_info_state);
273 if (req == NULL) {
274 return NULL;
276 SSVAL(state->setup+0, 0, TRANSACT2_QFSINFO);
277 SSVAL(state->param+0, 0, SMB_QUERY_FS_ATTRIBUTE_INFO);
279 subreq = cli_trans_send(state, ev, cli, 0, SMBtrans2,
280 NULL, 0, 0, 0,
281 state->setup, 1, 0,
282 state->param, 2, 0,
283 NULL, 0, 560);
284 if (tevent_req_nomem(subreq, req)) {
285 return tevent_req_post(req, ev);
287 tevent_req_set_callback(subreq, cli_get_fs_attr_info_done, req);
288 return req;
291 static void cli_get_fs_attr_info_done(struct tevent_req *subreq)
293 struct tevent_req *req = tevent_req_callback_data(
294 subreq, struct tevent_req);
295 struct cli_get_fs_attr_info_state *state = tevent_req_data(
296 req, struct cli_get_fs_attr_info_state);
297 uint8_t *data;
298 uint32_t num_data;
299 NTSTATUS status;
301 status = cli_trans_recv(subreq, talloc_tos(), NULL, NULL, 0, NULL,
302 NULL, 0, NULL, &data, 12, &num_data);
303 TALLOC_FREE(subreq);
304 if (tevent_req_nterror(req, status)) {
305 return;
307 state->fs_attr = IVAL(data, 0);
308 TALLOC_FREE(data);
309 tevent_req_done(req);
312 NTSTATUS cli_get_fs_attr_info_recv(struct tevent_req *req, uint32_t *fs_attr)
314 struct cli_get_fs_attr_info_state *state = tevent_req_data(
315 req, struct cli_get_fs_attr_info_state);
316 NTSTATUS status;
318 if (tevent_req_is_nterror(req, &status)) {
319 return status;
321 *fs_attr = state->fs_attr;
322 return NT_STATUS_OK;
325 NTSTATUS cli_get_fs_attr_info(struct cli_state *cli, uint32_t *fs_attr)
327 struct tevent_context *ev;
328 struct tevent_req *req;
329 NTSTATUS status = NT_STATUS_NO_MEMORY;
331 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
332 return cli_smb2_get_fs_attr_info(cli, fs_attr);
335 if (smbXcli_conn_has_async_calls(cli->conn)) {
336 return NT_STATUS_INVALID_PARAMETER;
338 ev = samba_tevent_context_init(talloc_tos());
339 if (ev == NULL) {
340 goto fail;
342 req = cli_get_fs_attr_info_send(ev, ev, cli);
343 if (req == NULL) {
344 goto fail;
346 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
347 goto fail;
349 status = cli_get_fs_attr_info_recv(req, fs_attr);
350 fail:
351 TALLOC_FREE(ev);
352 return status;
355 NTSTATUS cli_get_fs_volume_info(struct cli_state *cli,
356 TALLOC_CTX *mem_ctx,
357 char **_volume_name,
358 uint32_t *pserial_number,
359 time_t *pdate)
361 NTSTATUS status;
362 uint16_t recv_flags2;
363 uint16_t setup[1];
364 uint8_t param[2];
365 uint8_t *rdata;
366 uint32_t rdata_count;
367 unsigned int nlen;
368 char *volume_name = NULL;
370 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
371 return cli_smb2_get_fs_volume_info(cli,
372 mem_ctx,
373 _volume_name,
374 pserial_number,
375 pdate);
378 SSVAL(setup, 0, TRANSACT2_QFSINFO);
379 SSVAL(param,0,SMB_QUERY_FS_VOLUME_INFO);
381 status = cli_trans(talloc_tos(), cli, SMBtrans2,
382 NULL, 0, 0, 0,
383 setup, 1, 0,
384 param, 2, 0,
385 NULL, 0, 560,
386 &recv_flags2,
387 NULL, 0, NULL,
388 NULL, 0, NULL,
389 &rdata, 18, &rdata_count);
390 if (!NT_STATUS_IS_OK(status)) {
391 return status;
394 if (pdate) {
395 struct timespec ts;
396 ts = interpret_long_date(BVAL(rdata, 0));
397 *pdate = ts.tv_sec;
399 if (pserial_number) {
400 *pserial_number = IVAL(rdata,8);
402 nlen = IVAL(rdata,12);
403 if (nlen > (rdata_count - 18)) {
404 TALLOC_FREE(rdata);
405 return NT_STATUS_INVALID_NETWORK_RESPONSE;
408 pull_string_talloc(mem_ctx,
409 (const char *)rdata,
410 recv_flags2,
411 &volume_name,
412 rdata + 18,
413 nlen, STR_UNICODE);
414 if (volume_name == NULL) {
415 status = map_nt_error_from_unix(errno);
416 TALLOC_FREE(rdata);
417 return status;
420 /* todo: but not yet needed
421 * return the other stuff
424 *_volume_name = volume_name;
425 TALLOC_FREE(rdata);
426 return NT_STATUS_OK;
429 NTSTATUS cli_get_fs_full_size_info(struct cli_state *cli,
430 uint64_t *total_allocation_units,
431 uint64_t *caller_allocation_units,
432 uint64_t *actual_allocation_units,
433 uint64_t *sectors_per_allocation_unit,
434 uint64_t *bytes_per_sector)
436 uint16_t setup[1];
437 uint8_t param[2];
438 uint8_t *rdata = NULL;
439 uint32_t rdata_count;
440 NTSTATUS status;
442 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
443 return cli_smb2_get_fs_full_size_info(cli,
444 total_allocation_units,
445 caller_allocation_units,
446 actual_allocation_units,
447 sectors_per_allocation_unit,
448 bytes_per_sector);
451 SSVAL(setup, 0, TRANSACT2_QFSINFO);
452 SSVAL(param, 0, SMB_FS_FULL_SIZE_INFORMATION);
454 status = cli_trans(talloc_tos(), cli, SMBtrans2,
455 NULL, 0, 0, 0,
456 setup, 1, 0, /* setup */
457 param, 2, 0, /* param */
458 NULL, 0, 560, /* data */
459 NULL,
460 NULL, 0, NULL, /* rsetup */
461 NULL, 0, NULL, /* rparam */
462 &rdata, 32, &rdata_count); /* rdata */
463 if (!NT_STATUS_IS_OK(status)) {
464 goto fail;
467 if (total_allocation_units) {
468 *total_allocation_units = BIG_UINT(rdata, 0);
470 if (caller_allocation_units) {
471 *caller_allocation_units = BIG_UINT(rdata,8);
473 if (actual_allocation_units) {
474 *actual_allocation_units = BIG_UINT(rdata,16);
476 if (sectors_per_allocation_unit) {
477 *sectors_per_allocation_unit = IVAL(rdata,24);
479 if (bytes_per_sector) {
480 *bytes_per_sector = IVAL(rdata,28);
483 fail:
484 TALLOC_FREE(rdata);
485 return status;
488 struct cli_get_posix_fs_info_state {
489 uint16_t setup[1];
490 uint8_t param[2];
491 uint32_t optimal_transfer_size;
492 uint32_t block_size;
493 uint64_t total_blocks;
494 uint64_t blocks_available;
495 uint64_t user_blocks_available;
496 uint64_t total_file_nodes;
497 uint64_t free_file_nodes;
498 uint64_t fs_identifier;
501 static void cli_get_posix_fs_info_done(struct tevent_req *subreq);
502 static void cli_get_posix_fs_info_done2(struct tevent_req *subreq);
504 struct tevent_req *cli_get_posix_fs_info_send(TALLOC_CTX *mem_ctx,
505 struct tevent_context *ev,
506 struct cli_state *cli)
508 struct tevent_req *req = NULL, *subreq = NULL;
509 struct cli_get_posix_fs_info_state *state = NULL;
511 req = tevent_req_create(mem_ctx, &state,
512 struct cli_get_posix_fs_info_state);
513 if (req == NULL) {
514 return NULL;
516 SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
517 SSVAL(state->param, 0, SMB_QUERY_POSIX_FS_INFO);
519 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
520 subreq = cli_smb2_get_posix_fs_info_send(mem_ctx, ev, cli);
521 if (tevent_req_nomem(subreq, req)) {
522 return tevent_req_post(req, ev);
524 tevent_req_set_callback(subreq, cli_get_posix_fs_info_done2, req);
525 return req;
528 subreq = cli_trans_send(talloc_tos(), /* mem ctx. */
529 ev, /* event ctx. */
530 cli, /* cli_state. */
531 0, /* additional_flags2 */
532 SMBtrans2, /* cmd. */
533 NULL, /* pipe name. */
534 0, /* fid. */
535 0, /* function. */
536 0, /* flags. */
537 state->setup, /* setup. */
538 1, /* num setup uint16_t words. */
539 0, /* max returned setup. */
540 state->param, /* param. */
541 2, /* num param. */
542 0, /* max returned param. */
543 NULL, /* data. */
544 0, /* num data. */
545 560); /* max returned data. */
547 if (tevent_req_nomem(subreq, req)) {
548 return tevent_req_post(req, ev);
550 tevent_req_set_callback(subreq, cli_get_posix_fs_info_done, req);
551 return req;
554 static void cli_get_posix_fs_info_done(struct tevent_req *subreq)
556 struct tevent_req *req = tevent_req_callback_data(
557 subreq, struct tevent_req);
558 struct cli_get_posix_fs_info_state *state = tevent_req_data(
559 req, struct cli_get_posix_fs_info_state);
560 uint8_t *data = NULL;
561 uint32_t num_data;
562 NTSTATUS status;
564 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
565 NULL, 0, NULL, &data, 56, &num_data);
566 TALLOC_FREE(subreq);
567 if (tevent_req_nterror(req, status)) {
568 return;
571 state->optimal_transfer_size = IVAL(data, 0);
572 state->block_size = IVAL(data,4);
573 state->total_blocks = BIG_UINT(data,8);
574 state->blocks_available = BIG_UINT(data,16);
575 state->user_blocks_available = BIG_UINT(data,24);
576 state->total_file_nodes = BIG_UINT(data,32);
577 state->free_file_nodes = BIG_UINT(data,40);
578 state->fs_identifier = BIG_UINT(data,48);
579 TALLOC_FREE(data);
580 tevent_req_done(req);
583 static void cli_get_posix_fs_info_done2(struct tevent_req *subreq)
585 struct tevent_req *req = tevent_req_callback_data(
586 subreq, struct tevent_req);
587 struct cli_get_posix_fs_info_state *state = tevent_req_data(
588 req, struct cli_get_posix_fs_info_state);
589 NTSTATUS status;
590 status = cli_smb2_get_posix_fs_info_recv(subreq,
591 &state->optimal_transfer_size,
592 &state->block_size,
593 &state->total_blocks,
594 &state->blocks_available,
595 &state->user_blocks_available,
596 &state->total_file_nodes,
597 &state->free_file_nodes,
598 &state->fs_identifier);
599 TALLOC_FREE(subreq);
600 if (tevent_req_nterror(req, status)) {
601 return;
604 tevent_req_done(req);
607 NTSTATUS cli_get_posix_fs_info_recv(struct tevent_req *req,
608 uint32_t *optimal_transfer_size,
609 uint32_t *block_size,
610 uint64_t *total_blocks,
611 uint64_t *blocks_available,
612 uint64_t *user_blocks_available,
613 uint64_t *total_file_nodes,
614 uint64_t *free_file_nodes,
615 uint64_t *fs_identifier)
617 struct cli_get_posix_fs_info_state *state = tevent_req_data(
618 req, struct cli_get_posix_fs_info_state);
619 NTSTATUS status;
621 if (tevent_req_is_nterror(req, &status)) {
622 tevent_req_received(req);
623 return status;
626 if (optimal_transfer_size) {
627 *optimal_transfer_size = state->optimal_transfer_size;
629 if (block_size) {
630 *block_size = state->block_size;
632 if (total_blocks) {
633 *total_blocks = state->total_blocks;
635 if (blocks_available) {
636 *blocks_available = state->blocks_available;
638 if (user_blocks_available) {
639 *user_blocks_available = state->user_blocks_available;
641 if (total_file_nodes) {
642 *total_file_nodes = state->total_file_nodes;
644 if (free_file_nodes) {
645 *free_file_nodes = state->free_file_nodes;
647 if (fs_identifier) {
648 *fs_identifier = state->fs_identifier;
650 tevent_req_received(req);
651 return NT_STATUS_OK;
654 NTSTATUS cli_get_posix_fs_info(struct cli_state *cli,
655 uint32_t *optimal_transfer_size,
656 uint32_t *block_size,
657 uint64_t *total_blocks,
658 uint64_t *blocks_available,
659 uint64_t *user_blocks_available,
660 uint64_t *total_file_nodes,
661 uint64_t *free_file_nodes,
662 uint64_t *fs_identifier)
664 TALLOC_CTX *frame = talloc_stackframe();
665 struct tevent_context *ev = NULL;
666 struct tevent_req *req = NULL;
667 NTSTATUS status = NT_STATUS_NO_MEMORY;
669 if (smbXcli_conn_has_async_calls(cli->conn)) {
671 * Can't use sync call while an async call is in flight
673 status = NT_STATUS_INVALID_PARAMETER;
674 goto fail;
677 ev = samba_tevent_context_init(frame);
678 if (ev == NULL) {
679 goto fail;
682 req = cli_get_posix_fs_info_send(frame, ev, cli);
683 if (req == NULL) {
684 goto fail;
686 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
687 goto fail;
690 status = cli_get_posix_fs_info_recv(req,
691 optimal_transfer_size,
692 block_size,
693 total_blocks,
694 blocks_available,
695 user_blocks_available,
696 total_file_nodes,
697 free_file_nodes,
698 fs_identifier);
700 fail:
701 TALLOC_FREE(frame);
702 return status;
705 /****************************************************************************
706 Do a UNIX extensions SMB_QUERY_POSIX_WHOAMI call.
707 ****************************************************************************/
709 struct posix_whoami_state {
710 uint16_t setup[1];
711 uint8_t param[2];
712 uint32_t max_rdata;
713 bool guest;
714 uint64_t uid;
715 uint64_t gid;
716 uint32_t num_gids;
717 uint64_t *gids;
718 uint32_t num_sids;
719 struct dom_sid *sids;
722 static void cli_posix_whoami_done(struct tevent_req *subreq);
724 static const uint32_t posix_whoami_max_rdata = 62*1024;
726 struct tevent_req *cli_posix_whoami_send(TALLOC_CTX *mem_ctx,
727 struct tevent_context *ev,
728 struct cli_state *cli)
730 struct tevent_req *req = NULL, *subreq = NULL;
731 struct posix_whoami_state *state = NULL;
733 req = tevent_req_create(mem_ctx, &state, struct posix_whoami_state);
734 if (req == NULL) {
735 return NULL;
738 /* Setup setup word. */
739 SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
740 SSVAL(state->param, 0, SMB_QUERY_POSIX_WHOAMI);
742 state->max_rdata = posix_whoami_max_rdata;
744 subreq = cli_trans_send(state, /* mem ctx. */
745 ev, /* event ctx. */
746 cli, /* cli_state. */
747 0, /* additional_flags2 */
748 SMBtrans2, /* cmd. */
749 NULL, /* pipe name. */
750 -1, /* fid. */
751 0, /* function. */
752 0, /* flags. */
753 state->setup, /* setup. */
754 1, /* num setup uint16_t words. */
755 0, /* max returned setup. */
756 state->param, /* param. */
757 2, /* num param. */
758 0, /* max returned param. */
759 NULL, /* data. */
760 0, /* num data. */
761 state->max_rdata); /* max returned data. */
763 if (tevent_req_nomem(subreq, req)) {
764 return tevent_req_post(req, ev);
766 tevent_req_set_callback(subreq, cli_posix_whoami_done, req);
767 return req;
770 static void cli_posix_whoami_done(struct tevent_req *subreq)
772 struct tevent_req *req = tevent_req_callback_data(
773 subreq, struct tevent_req);
774 struct posix_whoami_state *state = tevent_req_data(
775 req, struct posix_whoami_state);
776 uint8_t *rdata = NULL;
777 uint8_t *p = NULL;
778 uint32_t num_rdata = 0;
779 uint32_t i;
780 NTSTATUS status;
782 status = cli_trans_recv(subreq,
783 state,
784 NULL,
785 NULL,
787 NULL,
788 NULL,
790 NULL,
791 &rdata,
793 &num_rdata);
794 TALLOC_FREE(subreq);
795 if (tevent_req_nterror(req, status)) {
796 return;
800 * Not strictly needed - cli_trans_recv()
801 * will ensure at least 40 bytes here. Added
802 * as more of a reminder to be careful when
803 * parsing network packets in C.
806 if (num_rdata < 40 || num_rdata > posix_whoami_max_rdata) {
807 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
808 return;
811 state->guest = (IVAL(rdata, 0) & SMB_WHOAMI_GUEST);
812 state->uid = BVAL(rdata, 8);
813 state->gid = BVAL(rdata, 16);
814 state->num_gids = IVAL(rdata, 24);
815 state->num_sids = IVAL(rdata, 28);
817 /* Ensure the gid array doesn't overflow */
818 if (state->num_gids > (num_rdata - 40) / sizeof(uint64_t)) {
819 tevent_req_nterror(req,
820 NT_STATUS_INVALID_NETWORK_RESPONSE);
821 return;
824 state->gids = talloc_array(state, uint64_t, state->num_gids);
825 if (tevent_req_nomem(state->gids, req)) {
826 return;
828 state->sids = talloc_array(state, struct dom_sid, state->num_sids);
829 if (tevent_req_nomem(state->sids, req)) {
830 return;
833 p = rdata + 40;
835 for (i = 0; i < state->num_gids; i++) {
836 state->gids[i] = BVAL(p, 0);
837 p += 8;
840 num_rdata -= (p - rdata);
842 for (i = 0; i < state->num_sids; i++) {
843 size_t sid_size;
844 DATA_BLOB in = data_blob_const(p, num_rdata);
845 enum ndr_err_code ndr_err;
847 ndr_err = ndr_pull_struct_blob(&in,
848 state,
849 &state->sids[i],
850 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
851 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
852 tevent_req_nterror(req,
853 NT_STATUS_INVALID_NETWORK_RESPONSE);
854 return;
857 sid_size = ndr_size_dom_sid(&state->sids[i], 0);
859 if (sid_size > num_rdata) {
860 tevent_req_nterror(req,
861 NT_STATUS_INVALID_NETWORK_RESPONSE);
862 return;
865 p += sid_size;
866 num_rdata -= sid_size;
869 if (num_rdata != 0) {
870 tevent_req_nterror(req,
871 NT_STATUS_INVALID_NETWORK_RESPONSE);
872 return;
875 tevent_req_done(req);
878 NTSTATUS cli_posix_whoami_recv(struct tevent_req *req,
879 TALLOC_CTX *mem_ctx,
880 uint64_t *puid,
881 uint64_t *pgid,
882 uint32_t *pnum_gids,
883 uint64_t **pgids,
884 uint32_t *pnum_sids,
885 struct dom_sid **psids,
886 bool *pguest)
888 NTSTATUS status;
889 struct posix_whoami_state *state = tevent_req_data(
890 req, struct posix_whoami_state);
892 if (tevent_req_is_nterror(req, &status)) {
893 return status;
896 if (puid) {
897 *puid = state->uid;
899 if (pgid) {
900 *pgid = state->gid;
902 if (pnum_gids) {
903 *pnum_gids = state->num_gids;
905 if (pgids) {
906 *pgids = talloc_move(mem_ctx, &state->gids);
908 if (pnum_sids) {
909 *pnum_sids = state->num_sids;
911 if (psids) {
912 *psids = talloc_move(mem_ctx, &state->sids);
914 if (pguest) {
915 *pguest = state->guest;
917 return NT_STATUS_OK;
920 NTSTATUS cli_posix_whoami(struct cli_state *cli,
921 TALLOC_CTX *mem_ctx,
922 uint64_t *puid,
923 uint64_t *pgid,
924 uint32_t *num_gids,
925 uint64_t **gids,
926 uint32_t *num_sids,
927 struct dom_sid **sids,
928 bool *pguest)
930 TALLOC_CTX *frame = talloc_stackframe();
931 struct tevent_context *ev = NULL;
932 struct tevent_req *req = NULL;
933 NTSTATUS status = NT_STATUS_OK;
935 if (smbXcli_conn_has_async_calls(cli->conn)) {
937 * Can't use sync call while an async call is in flight
939 status = NT_STATUS_INVALID_PARAMETER;
940 goto fail;
943 ev = samba_tevent_context_init(frame);
944 if (ev == NULL) {
945 status = NT_STATUS_NO_MEMORY;
946 goto fail;
949 req = cli_posix_whoami_send(frame,
951 cli);
952 if (req == NULL) {
953 status = NT_STATUS_NO_MEMORY;
954 goto fail;
957 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
958 goto fail;
961 status = cli_posix_whoami_recv(req,
962 mem_ctx,
963 puid,
964 pgid,
965 num_gids,
966 gids,
967 num_sids,
968 sids,
969 pguest);
971 fail:
972 TALLOC_FREE(frame);
973 return status;