ldb: version 1.1.27
[Samba.git] / source3 / libsmb / clifsinfo.c
blob795d12ba7410d9b0683acafb00d4581f96be8f40
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 "../libcli/smb/smb_seal.h"
27 #include "trans2.h"
28 #include "auth_generic.h"
29 #include "auth/gensec/gensec.h"
30 #include "../libcli/smb/smbXcli_base.h"
31 #include "auth/credentials/credentials.h"
32 #include "../librpc/gen_ndr/ndr_security.h"
34 /****************************************************************************
35 Get UNIX extensions version info.
36 ****************************************************************************/
38 struct cli_unix_extensions_version_state {
39 struct cli_state *cli;
40 uint16_t setup[1];
41 uint8_t param[2];
42 uint16_t major, minor;
43 uint32_t caplow, caphigh;
46 static void cli_unix_extensions_version_done(struct tevent_req *subreq);
48 struct tevent_req *cli_unix_extensions_version_send(TALLOC_CTX *mem_ctx,
49 struct tevent_context *ev,
50 struct cli_state *cli)
52 struct tevent_req *req, *subreq;
53 struct cli_unix_extensions_version_state *state;
55 req = tevent_req_create(mem_ctx, &state,
56 struct cli_unix_extensions_version_state);
57 if (req == NULL) {
58 return NULL;
60 state->cli = cli;
61 SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
62 SSVAL(state->param, 0, SMB_QUERY_CIFS_UNIX_INFO);
64 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
65 NULL, 0, 0, 0,
66 state->setup, 1, 0,
67 state->param, 2, 0,
68 NULL, 0, 560);
69 if (tevent_req_nomem(subreq, req)) {
70 return tevent_req_post(req, ev);
72 tevent_req_set_callback(subreq, cli_unix_extensions_version_done, req);
73 return req;
76 static void cli_unix_extensions_version_done(struct tevent_req *subreq)
78 struct tevent_req *req = tevent_req_callback_data(
79 subreq, struct tevent_req);
80 struct cli_unix_extensions_version_state *state = tevent_req_data(
81 req, struct cli_unix_extensions_version_state);
82 uint8_t *data;
83 uint32_t num_data;
84 NTSTATUS status;
86 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
87 NULL, 0, NULL, &data, 12, &num_data);
88 TALLOC_FREE(subreq);
89 if (!NT_STATUS_IS_OK(status)) {
90 tevent_req_nterror(req, status);
91 return;
94 state->major = SVAL(data, 0);
95 state->minor = SVAL(data, 2);
96 state->caplow = IVAL(data, 4);
97 state->caphigh = IVAL(data, 8);
98 TALLOC_FREE(data);
99 tevent_req_done(req);
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)
107 struct cli_unix_extensions_version_state *state = tevent_req_data(
108 req, struct cli_unix_extensions_version_state);
109 NTSTATUS status;
111 if (tevent_req_is_nterror(req, &status)) {
112 return status;
114 *pmajor = state->major;
115 *pminor = state->minor;
116 *pcaplow = state->caplow;
117 *pcaphigh = state->caphigh;
118 state->cli->server_posix_capabilities = *pcaplow;
119 return NT_STATUS_OK;
122 NTSTATUS cli_unix_extensions_version(struct cli_state *cli, uint16_t *pmajor,
123 uint16_t *pminor, uint32_t *pcaplow,
124 uint32_t *pcaphigh)
126 TALLOC_CTX *frame = talloc_stackframe();
127 struct tevent_context *ev;
128 struct tevent_req *req;
129 NTSTATUS status = NT_STATUS_OK;
131 if (smbXcli_conn_has_async_calls(cli->conn)) {
133 * Can't use sync call while an async call is in flight
135 status = NT_STATUS_INVALID_PARAMETER;
136 goto fail;
139 ev = samba_tevent_context_init(frame);
140 if (ev == NULL) {
141 status = NT_STATUS_NO_MEMORY;
142 goto fail;
145 req = cli_unix_extensions_version_send(frame, ev, cli);
146 if (req == NULL) {
147 status = NT_STATUS_NO_MEMORY;
148 goto fail;
151 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
152 goto fail;
155 status = cli_unix_extensions_version_recv(req, pmajor, pminor, pcaplow,
156 pcaphigh);
157 fail:
158 TALLOC_FREE(frame);
159 return status;
162 /****************************************************************************
163 Set UNIX extensions capabilities.
164 ****************************************************************************/
166 struct cli_set_unix_extensions_capabilities_state {
167 struct cli_state *cli;
168 uint16_t setup[1];
169 uint8_t param[4];
170 uint8_t data[12];
173 static void cli_set_unix_extensions_capabilities_done(
174 struct tevent_req *subreq);
176 struct tevent_req *cli_set_unix_extensions_capabilities_send(
177 TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
178 uint16_t major, uint16_t minor, uint32_t caplow, uint32_t caphigh)
180 struct tevent_req *req, *subreq;
181 struct cli_set_unix_extensions_capabilities_state *state;
183 req = tevent_req_create(
184 mem_ctx, &state,
185 struct cli_set_unix_extensions_capabilities_state);
186 if (req == NULL) {
187 return NULL;
190 state->cli = cli;
191 SSVAL(state->setup+0, 0, TRANSACT2_SETFSINFO);
193 SSVAL(state->param, 0, 0);
194 SSVAL(state->param, 2, SMB_SET_CIFS_UNIX_INFO);
196 SSVAL(state->data, 0, major);
197 SSVAL(state->data, 2, minor);
198 SIVAL(state->data, 4, caplow);
199 SIVAL(state->data, 8, caphigh);
201 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
202 NULL, 0, 0, 0,
203 state->setup, 1, 0,
204 state->param, 4, 0,
205 state->data, 12, 560);
206 if (tevent_req_nomem(subreq, req)) {
207 return tevent_req_post(req, ev);
209 tevent_req_set_callback(
210 subreq, cli_set_unix_extensions_capabilities_done, req);
211 return req;
214 static void cli_set_unix_extensions_capabilities_done(
215 struct tevent_req *subreq)
217 struct tevent_req *req = tevent_req_callback_data(
218 subreq, struct tevent_req);
219 struct cli_set_unix_extensions_capabilities_state *state = tevent_req_data(
220 req, struct cli_set_unix_extensions_capabilities_state);
222 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
223 NULL, 0, NULL, NULL, 0, NULL);
224 if (NT_STATUS_IS_OK(status)) {
225 state->cli->requested_posix_capabilities = IVAL(state->data, 4);
227 tevent_req_simple_finish_ntstatus(subreq, status);
230 NTSTATUS cli_set_unix_extensions_capabilities_recv(struct tevent_req *req)
232 return tevent_req_simple_recv_ntstatus(req);
235 NTSTATUS cli_set_unix_extensions_capabilities(struct cli_state *cli,
236 uint16_t major, uint16_t minor,
237 uint32_t caplow, uint32_t caphigh)
239 struct tevent_context *ev;
240 struct tevent_req *req;
241 NTSTATUS status = NT_STATUS_NO_MEMORY;
243 if (smbXcli_conn_has_async_calls(cli->conn)) {
244 return NT_STATUS_INVALID_PARAMETER;
246 ev = samba_tevent_context_init(talloc_tos());
247 if (ev == NULL) {
248 goto fail;
250 req = cli_set_unix_extensions_capabilities_send(
251 ev, ev, cli, major, minor, caplow, caphigh);
252 if (req == NULL) {
253 goto fail;
255 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
256 goto fail;
258 status = cli_set_unix_extensions_capabilities_recv(req);
259 fail:
260 TALLOC_FREE(ev);
261 return status;
264 struct cli_get_fs_attr_info_state {
265 uint16_t setup[1];
266 uint8_t param[2];
267 uint32_t fs_attr;
270 static void cli_get_fs_attr_info_done(struct tevent_req *subreq);
272 struct tevent_req *cli_get_fs_attr_info_send(TALLOC_CTX *mem_ctx,
273 struct tevent_context *ev,
274 struct cli_state *cli)
276 struct tevent_req *subreq, *req;
277 struct cli_get_fs_attr_info_state *state;
279 req = tevent_req_create(mem_ctx, &state,
280 struct cli_get_fs_attr_info_state);
281 if (req == NULL) {
282 return NULL;
284 SSVAL(state->setup+0, 0, TRANSACT2_QFSINFO);
285 SSVAL(state->param+0, 0, SMB_QUERY_FS_ATTRIBUTE_INFO);
287 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
288 NULL, 0, 0, 0,
289 state->setup, 1, 0,
290 state->param, 2, 0,
291 NULL, 0, 560);
292 if (tevent_req_nomem(subreq, req)) {
293 return tevent_req_post(req, ev);
295 tevent_req_set_callback(subreq, cli_get_fs_attr_info_done, req);
296 return req;
299 static void cli_get_fs_attr_info_done(struct tevent_req *subreq)
301 struct tevent_req *req = tevent_req_callback_data(
302 subreq, struct tevent_req);
303 struct cli_get_fs_attr_info_state *state = tevent_req_data(
304 req, struct cli_get_fs_attr_info_state);
305 uint8_t *data;
306 uint32_t num_data;
307 NTSTATUS status;
309 status = cli_trans_recv(subreq, talloc_tos(), NULL, NULL, 0, NULL,
310 NULL, 0, NULL, &data, 12, &num_data);
311 TALLOC_FREE(subreq);
312 if (!NT_STATUS_IS_OK(status)) {
313 tevent_req_nterror(req, status);
314 return;
316 state->fs_attr = IVAL(data, 0);
317 TALLOC_FREE(data);
318 tevent_req_done(req);
321 NTSTATUS cli_get_fs_attr_info_recv(struct tevent_req *req, uint32_t *fs_attr)
323 struct cli_get_fs_attr_info_state *state = tevent_req_data(
324 req, struct cli_get_fs_attr_info_state);
325 NTSTATUS status;
327 if (tevent_req_is_nterror(req, &status)) {
328 return status;
330 *fs_attr = state->fs_attr;
331 return NT_STATUS_OK;
334 NTSTATUS cli_get_fs_attr_info(struct cli_state *cli, uint32_t *fs_attr)
336 struct tevent_context *ev;
337 struct tevent_req *req;
338 NTSTATUS status = NT_STATUS_NO_MEMORY;
340 if (smbXcli_conn_has_async_calls(cli->conn)) {
341 return NT_STATUS_INVALID_PARAMETER;
343 ev = samba_tevent_context_init(talloc_tos());
344 if (ev == NULL) {
345 goto fail;
347 req = cli_get_fs_attr_info_send(ev, ev, cli);
348 if (req == NULL) {
349 goto fail;
351 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
352 goto fail;
354 status = cli_get_fs_attr_info_recv(req, fs_attr);
355 fail:
356 TALLOC_FREE(ev);
357 return status;
360 NTSTATUS cli_get_fs_volume_info(struct cli_state *cli,
361 TALLOC_CTX *mem_ctx,
362 char **_volume_name,
363 uint32_t *pserial_number,
364 time_t *pdate)
366 NTSTATUS status;
367 uint16_t recv_flags2;
368 uint16_t setup[1];
369 uint8_t param[2];
370 uint8_t *rdata;
371 uint32_t rdata_count;
372 unsigned int nlen;
373 char *volume_name = NULL;
375 SSVAL(setup, 0, TRANSACT2_QFSINFO);
376 SSVAL(param,0,SMB_QUERY_FS_VOLUME_INFO);
378 status = cli_trans(talloc_tos(), cli, SMBtrans2,
379 NULL, 0, 0, 0,
380 setup, 1, 0,
381 param, 2, 0,
382 NULL, 0, 560,
383 &recv_flags2,
384 NULL, 0, NULL,
385 NULL, 0, NULL,
386 &rdata, 18, &rdata_count);
387 if (!NT_STATUS_IS_OK(status)) {
388 return status;
391 if (pdate) {
392 struct timespec ts;
393 ts = interpret_long_date((char *)rdata);
394 *pdate = ts.tv_sec;
396 if (pserial_number) {
397 *pserial_number = IVAL(rdata,8);
399 nlen = IVAL(rdata,12);
400 if (nlen > (rdata_count - 18)) {
401 TALLOC_FREE(rdata);
402 return NT_STATUS_INVALID_NETWORK_RESPONSE;
405 clistr_pull_talloc(mem_ctx,
406 (const char *)rdata,
407 recv_flags2,
408 &volume_name,
409 rdata + 18,
410 nlen, STR_UNICODE);
411 if (volume_name == NULL) {
412 status = map_nt_error_from_unix(errno);
413 TALLOC_FREE(rdata);
414 return status;
417 /* todo: but not yet needed
418 * return the other stuff
421 *_volume_name = volume_name;
422 TALLOC_FREE(rdata);
423 return NT_STATUS_OK;
426 NTSTATUS cli_get_fs_full_size_info(struct cli_state *cli,
427 uint64_t *total_allocation_units,
428 uint64_t *caller_allocation_units,
429 uint64_t *actual_allocation_units,
430 uint64_t *sectors_per_allocation_unit,
431 uint64_t *bytes_per_sector)
433 uint16_t setup[1];
434 uint8_t param[2];
435 uint8_t *rdata = NULL;
436 uint32_t rdata_count;
437 NTSTATUS status;
439 SSVAL(setup, 0, TRANSACT2_QFSINFO);
440 SSVAL(param, 0, SMB_FS_FULL_SIZE_INFORMATION);
442 status = cli_trans(talloc_tos(), cli, SMBtrans2,
443 NULL, 0, 0, 0,
444 setup, 1, 0, /* setup */
445 param, 2, 0, /* param */
446 NULL, 0, 560, /* data */
447 NULL,
448 NULL, 0, NULL, /* rsetup */
449 NULL, 0, NULL, /* rparam */
450 &rdata, 32, &rdata_count); /* rdata */
451 if (!NT_STATUS_IS_OK(status)) {
452 goto fail;
455 if (total_allocation_units) {
456 *total_allocation_units = BIG_UINT(rdata, 0);
458 if (caller_allocation_units) {
459 *caller_allocation_units = BIG_UINT(rdata,8);
461 if (actual_allocation_units) {
462 *actual_allocation_units = BIG_UINT(rdata,16);
464 if (sectors_per_allocation_unit) {
465 *sectors_per_allocation_unit = IVAL(rdata,24);
467 if (bytes_per_sector) {
468 *bytes_per_sector = IVAL(rdata,28);
471 fail:
472 TALLOC_FREE(rdata);
473 return status;
476 NTSTATUS cli_get_posix_fs_info(struct cli_state *cli,
477 uint32_t *optimal_transfer_size,
478 uint32_t *block_size,
479 uint64_t *total_blocks,
480 uint64_t *blocks_available,
481 uint64_t *user_blocks_available,
482 uint64_t *total_file_nodes,
483 uint64_t *free_file_nodes,
484 uint64_t *fs_identifier)
486 uint16_t setup[1];
487 uint8_t param[2];
488 uint8_t *rdata = NULL;
489 uint32_t rdata_count;
490 NTSTATUS status;
492 SSVAL(setup, 0, TRANSACT2_QFSINFO);
493 SSVAL(param,0,SMB_QUERY_POSIX_FS_INFO);
495 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, 0, 0, 0,
496 setup, 1, 0,
497 param, 2, 0,
498 NULL, 0, 560,
499 NULL,
500 NULL, 0, NULL, /* rsetup */
501 NULL, 0, NULL, /* rparam */
502 &rdata, 56, &rdata_count);
503 if (!NT_STATUS_IS_OK(status)) {
504 return status;
507 if (optimal_transfer_size) {
508 *optimal_transfer_size = IVAL(rdata, 0);
510 if (block_size) {
511 *block_size = IVAL(rdata,4);
513 if (total_blocks) {
514 *total_blocks = BIG_UINT(rdata,8);
516 if (blocks_available) {
517 *blocks_available = BIG_UINT(rdata,16);
519 if (user_blocks_available) {
520 *user_blocks_available = BIG_UINT(rdata,24);
522 if (total_file_nodes) {
523 *total_file_nodes = BIG_UINT(rdata,32);
525 if (free_file_nodes) {
526 *free_file_nodes = BIG_UINT(rdata,40);
528 if (fs_identifier) {
529 *fs_identifier = BIG_UINT(rdata,48);
531 return NT_STATUS_OK;
535 /******************************************************************************
536 Send/receive the request encryption blob.
537 ******************************************************************************/
539 static NTSTATUS enc_blob_send_receive(struct cli_state *cli, DATA_BLOB *in, DATA_BLOB *out, DATA_BLOB *param_out)
541 uint16_t setup[1];
542 uint8_t param[4];
543 uint8_t *rparam=NULL, *rdata=NULL;
544 uint32_t num_rparam, num_rdata;
545 NTSTATUS status;
547 SSVAL(setup+0, 0, TRANSACT2_SETFSINFO);
548 SSVAL(param,0,0);
549 SSVAL(param,2,SMB_REQUEST_TRANSPORT_ENCRYPTION);
551 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, 0, 0, 0,
552 setup, 1, 0,
553 param, 4, 2,
554 (uint8_t *)in->data, in->length, CLI_BUFFER_SIZE,
555 NULL, /* recv_flags */
556 NULL, 0, NULL, /* rsetup */
557 &rparam, 0, &num_rparam,
558 &rdata, 0, &num_rdata);
560 if (!NT_STATUS_IS_OK(status) &&
561 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
562 return status;
565 *out = data_blob(rdata, num_rdata);
566 *param_out = data_blob(rparam, num_rparam);
568 TALLOC_FREE(rparam);
569 TALLOC_FREE(rdata);
570 return status;
573 /******************************************************************************
574 Start a raw ntlmssp encryption.
575 ******************************************************************************/
577 NTSTATUS cli_raw_ntlm_smb_encryption_start(struct cli_state *cli,
578 const char *user,
579 const char *pass,
580 const char *domain)
582 DATA_BLOB blob_in = data_blob_null;
583 DATA_BLOB blob_out = data_blob_null;
584 DATA_BLOB param_out = data_blob_null;
585 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
586 struct auth_generic_state *auth_generic_state;
587 struct smb_trans_enc_state *es = talloc_zero(NULL, struct smb_trans_enc_state);
588 if (!es) {
589 return NT_STATUS_NO_MEMORY;
591 status = auth_generic_client_prepare(es,
592 &auth_generic_state);
593 if (!NT_STATUS_IS_OK(status)) {
594 goto fail;
597 gensec_want_feature(auth_generic_state->gensec_security, GENSEC_FEATURE_SESSION_KEY);
598 gensec_want_feature(auth_generic_state->gensec_security, GENSEC_FEATURE_SEAL);
600 if (!NT_STATUS_IS_OK(status = auth_generic_set_username(auth_generic_state, user))) {
601 goto fail;
603 if (!NT_STATUS_IS_OK(status = auth_generic_set_domain(auth_generic_state, domain))) {
604 goto fail;
606 if (!NT_STATUS_IS_OK(status = auth_generic_set_password(auth_generic_state, pass))) {
607 goto fail;
610 if (!NT_STATUS_IS_OK(status = auth_generic_client_start(auth_generic_state, GENSEC_OID_NTLMSSP))) {
611 goto fail;
614 do {
615 status = gensec_update(auth_generic_state->gensec_security, auth_generic_state,
616 blob_in, &blob_out);
617 data_blob_free(&blob_in);
618 data_blob_free(&param_out);
619 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) || NT_STATUS_IS_OK(status)) {
620 NTSTATUS trans_status = enc_blob_send_receive(cli,
621 &blob_out,
622 &blob_in,
623 &param_out);
624 if (!NT_STATUS_EQUAL(trans_status,
625 NT_STATUS_MORE_PROCESSING_REQUIRED) &&
626 !NT_STATUS_IS_OK(trans_status)) {
627 status = trans_status;
628 } else {
629 if (param_out.length == 2) {
630 es->enc_ctx_num = SVAL(param_out.data, 0);
634 data_blob_free(&blob_out);
635 } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
637 data_blob_free(&blob_in);
639 if (NT_STATUS_IS_OK(status)) {
640 es->enc_on = true;
641 /* Replace the old state, if any. */
642 /* We only need the gensec_security part from here.
643 * es is a malloc()ed pointer, so we cannot make
644 * gensec_security a talloc child */
645 es->gensec_security = talloc_move(NULL,
646 &auth_generic_state->gensec_security);
647 smb1cli_conn_set_encryption(cli->conn, es);
648 es = NULL;
651 fail:
652 TALLOC_FREE(es);
653 return status;
656 /******************************************************************************
657 Start a SPNEGO gssapi encryption context.
658 ******************************************************************************/
660 NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
662 DATA_BLOB blob_recv = data_blob_null;
663 DATA_BLOB blob_send = data_blob_null;
664 DATA_BLOB param_out = data_blob_null;
665 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
666 struct auth_generic_state *auth_generic_state;
667 struct smb_trans_enc_state *es = talloc_zero(NULL, struct smb_trans_enc_state);
669 if (!es) {
670 return NT_STATUS_NO_MEMORY;
673 status = auth_generic_client_prepare(es,
674 &auth_generic_state);
675 if (!NT_STATUS_IS_OK(status)) {
676 goto fail;
679 gensec_want_feature(auth_generic_state->gensec_security, GENSEC_FEATURE_SESSION_KEY);
680 gensec_want_feature(auth_generic_state->gensec_security, GENSEC_FEATURE_SEAL);
682 cli_credentials_set_kerberos_state(auth_generic_state->credentials,
683 CRED_MUST_USE_KERBEROS);
685 status = gensec_set_target_service(auth_generic_state->gensec_security, "cifs");
686 if (!NT_STATUS_IS_OK(status)) {
687 goto fail;
690 status = gensec_set_target_hostname(auth_generic_state->gensec_security,
691 smbXcli_conn_remote_name(cli->conn));
692 if (!NT_STATUS_IS_OK(status)) {
693 goto fail;
696 if (!NT_STATUS_IS_OK(status = auth_generic_client_start(auth_generic_state, GENSEC_OID_SPNEGO))) {
697 goto fail;
700 status = gensec_update(auth_generic_state->gensec_security, talloc_tos(),
701 blob_recv, &blob_send);
703 do {
704 data_blob_free(&blob_recv);
705 status = enc_blob_send_receive(cli, &blob_send, &blob_recv, &param_out);
706 if (param_out.length == 2) {
707 es->enc_ctx_num = SVAL(param_out.data, 0);
709 data_blob_free(&blob_send);
710 status = gensec_update(auth_generic_state->gensec_security, talloc_tos(),
711 blob_recv, &blob_send);
712 } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
713 data_blob_free(&blob_recv);
715 if (NT_STATUS_IS_OK(status)) {
716 if (!gensec_have_feature(auth_generic_state->gensec_security,
717 GENSEC_FEATURE_SIGN) ||
718 !gensec_have_feature(auth_generic_state->gensec_security,
719 GENSEC_FEATURE_SEAL)) {
720 status = NT_STATUS_ACCESS_DENIED;
724 if (NT_STATUS_IS_OK(status)) {
725 es->enc_on = true;
726 /* Replace the old state, if any. */
727 /* We only need the gensec_security part from here.
728 * es is a malloc()ed pointer, so we cannot make
729 * gensec_security a talloc child */
730 es->gensec_security = talloc_move(es,
731 &auth_generic_state->gensec_security);
732 smb1cli_conn_set_encryption(cli->conn, es);
733 es = NULL;
735 fail:
736 TALLOC_FREE(es);
737 return status;
740 /********************************************************************
741 Ensure a connection is encrypted.
742 ********************************************************************/
744 NTSTATUS cli_force_encryption(struct cli_state *c,
745 const char *username,
746 const char *password,
747 const char *domain)
749 uint16_t major, minor;
750 uint32_t caplow, caphigh;
751 NTSTATUS status;
753 if (!SERVER_HAS_UNIX_CIFS(c)) {
754 return NT_STATUS_NOT_SUPPORTED;
757 status = cli_unix_extensions_version(c, &major, &minor, &caplow,
758 &caphigh);
759 if (!NT_STATUS_IS_OK(status)) {
760 DEBUG(10, ("cli_force_encryption: cli_unix_extensions_version "
761 "returned %s\n", nt_errstr(status)));
762 return NT_STATUS_UNKNOWN_REVISION;
765 if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
766 return NT_STATUS_UNSUPPORTED_COMPRESSION;
769 if (c->use_kerberos) {
770 return cli_gss_smb_encryption_start(c);
772 return cli_raw_ntlm_smb_encryption_start(c,
773 username,
774 password,
775 domain);
778 /****************************************************************************
779 Do a UNIX extensions SMB_QUERY_POSIX_WHOAMI call.
780 ****************************************************************************/
782 struct posix_whoami_state {
783 uint16_t setup[1];
784 uint8_t param[2];
785 uint32_t max_rdata;
786 bool guest;
787 uint64_t uid;
788 uint64_t gid;
789 uint32_t num_gids;
790 uint64_t *gids;
791 uint32_t num_sids;
792 struct dom_sid *sids;
795 static void cli_posix_whoami_done(struct tevent_req *subreq);
797 struct tevent_req *cli_posix_whoami_send(TALLOC_CTX *mem_ctx,
798 struct tevent_context *ev,
799 struct cli_state *cli)
801 struct tevent_req *req = NULL, *subreq = NULL;
802 struct posix_whoami_state *state = NULL;
804 req = tevent_req_create(mem_ctx, &state, struct posix_whoami_state);
805 if (req == NULL) {
806 return NULL;
809 /* Setup setup word. */
810 SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
811 SSVAL(state->param, 0, SMB_QUERY_POSIX_WHOAMI);
813 state->max_rdata = 62*1024;
815 subreq = cli_trans_send(state, /* mem ctx. */
816 ev, /* event ctx. */
817 cli, /* cli_state. */
818 SMBtrans2, /* cmd. */
819 NULL, /* pipe name. */
820 -1, /* fid. */
821 0, /* function. */
822 0, /* flags. */
823 state->setup, /* setup. */
824 1, /* num setup uint16_t words. */
825 0, /* max returned setup. */
826 state->param, /* param. */
827 2, /* num param. */
828 0, /* max returned param. */
829 NULL, /* data. */
830 0, /* num data. */
831 state->max_rdata); /* max returned data. */
833 if (tevent_req_nomem(subreq, req)) {
834 return tevent_req_post(req, ev);
836 tevent_req_set_callback(subreq, cli_posix_whoami_done, req);
837 return req;
840 static void cli_posix_whoami_done(struct tevent_req *subreq)
842 struct tevent_req *req = tevent_req_callback_data(
843 subreq, struct tevent_req);
844 struct posix_whoami_state *state = tevent_req_data(
845 req, struct posix_whoami_state);
846 uint8_t *rdata = NULL;
847 uint8_t *p = NULL;
848 uint32_t num_rdata = 0;
849 uint32_t i;
850 NTSTATUS status;
852 status = cli_trans_recv(subreq,
853 state,
854 NULL,
855 NULL,
857 NULL,
858 NULL,
860 NULL,
861 &rdata,
863 &num_rdata);
864 TALLOC_FREE(subreq);
865 if (tevent_req_nterror(req, status)) {
866 return;
870 * Not strictly needed - cli_trans_recv()
871 * will ensure at least 40 bytes here. Added
872 * as more of a reminder to be careful when
873 * parsing network packets in C.
876 if (num_rdata < 40 || rdata + num_rdata < rdata) {
877 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
878 return;
881 state->guest = (IVAL(rdata, 0) & SMB_WHOAMI_GUEST);
882 state->uid = BVAL(rdata, 8);
883 state->gid = BVAL(rdata, 16);
884 state->num_gids = IVAL(rdata, 24);
885 state->num_sids = IVAL(rdata, 28);
887 state->gids = talloc_array(state, uint64_t, state->num_gids);
888 if (tevent_req_nomem(state->gids, req)) {
889 return;
891 state->sids = talloc_array(state, struct dom_sid, state->num_sids);
892 if (tevent_req_nomem(state->sids, req)) {
893 return;
896 p = rdata + 40;
898 for (i = 0; i < state->num_gids; i++) {
899 if (p + 8 > rdata + num_rdata) {
900 tevent_req_nterror(req,
901 NT_STATUS_INVALID_NETWORK_RESPONSE);
902 return;
904 state->gids[i] = BVAL(p, 0);
905 p += 8;
908 num_rdata -= (p - rdata);
910 for (i = 0; i < state->num_sids; i++) {
911 size_t sid_size;
912 DATA_BLOB in = data_blob_const(p, num_rdata);
913 enum ndr_err_code ndr_err;
915 ndr_err = ndr_pull_struct_blob(&in,
916 state,
917 &state->sids[i],
918 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
919 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
920 tevent_req_nterror(req,
921 NT_STATUS_INVALID_NETWORK_RESPONSE);
922 return;
925 sid_size = ndr_size_dom_sid(&state->sids[i], 0);
927 if (sid_size > num_rdata) {
928 tevent_req_nterror(req,
929 NT_STATUS_INVALID_NETWORK_RESPONSE);
930 return;
933 p += sid_size;
934 num_rdata -= sid_size;
936 tevent_req_done(req);
939 NTSTATUS cli_posix_whoami_recv(struct tevent_req *req,
940 TALLOC_CTX *mem_ctx,
941 uint64_t *puid,
942 uint64_t *pgid,
943 uint32_t *pnum_gids,
944 uint64_t **pgids,
945 uint32_t *pnum_sids,
946 struct dom_sid **psids,
947 bool *pguest)
949 NTSTATUS status;
950 struct posix_whoami_state *state = tevent_req_data(
951 req, struct posix_whoami_state);
953 if (tevent_req_is_nterror(req, &status)) {
954 return status;
957 if (puid) {
958 *puid = state->uid;
960 if (pgid) {
961 *pgid = state->gid;
963 if (pnum_gids) {
964 *pnum_gids = state->num_gids;
966 if (pgids) {
967 *pgids = talloc_move(mem_ctx, &state->gids);
969 if (pnum_sids) {
970 *pnum_sids = state->num_sids;
972 if (psids) {
973 *psids = talloc_move(mem_ctx, &state->sids);
975 if (pguest) {
976 *pguest = state->guest;
978 return NT_STATUS_OK;
981 NTSTATUS cli_posix_whoami(struct cli_state *cli,
982 TALLOC_CTX *mem_ctx,
983 uint64_t *puid,
984 uint64_t *pgid,
985 uint32_t *num_gids,
986 uint64_t **gids,
987 uint32_t *num_sids,
988 struct dom_sid **sids,
989 bool *pguest)
991 TALLOC_CTX *frame = talloc_stackframe();
992 struct tevent_context *ev = NULL;
993 struct tevent_req *req = NULL;
994 NTSTATUS status = NT_STATUS_OK;
996 if (smbXcli_conn_has_async_calls(cli->conn)) {
998 * Can't use sync call while an async call is in flight
1000 status = NT_STATUS_INVALID_PARAMETER;
1001 goto fail;
1004 ev = samba_tevent_context_init(frame);
1005 if (ev == NULL) {
1006 status = NT_STATUS_NO_MEMORY;
1007 goto fail;
1010 req = cli_posix_whoami_send(frame,
1012 cli);
1013 if (req == NULL) {
1014 status = NT_STATUS_NO_MEMORY;
1015 goto fail;
1018 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1019 goto fail;
1022 status = cli_posix_whoami_recv(req,
1023 mem_ctx,
1024 puid,
1025 pgid,
1026 num_gids,
1027 gids,
1028 num_sids,
1029 sids,
1030 pguest);
1032 fail:
1033 TALLOC_FREE(frame);
1034 return status;