midltests: move the current implementation to midltests_simple.exe
[Samba.git] / source3 / libsmb / clifsinfo.c
blob5ca76ac16a8410c09b6997695524c6e331e4cb10
1 /*
2 Unix SMB/CIFS implementation.
3 FS info functions
4 Copyright (C) Stefan (metze) Metzmacher 2003
5 Copyright (C) Jeremy Allison 2007
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.
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.
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/>.
21 #include "includes.h"
22 #include "../libcli/auth/spnego.h"
23 #include "../libcli/auth/ntlmssp.h"
24 #include "async_smb.h"
26 /****************************************************************************
27 Get UNIX extensions version info.
28 ****************************************************************************/
30 struct cli_unix_extensions_version_state {
31 struct cli_state *cli;
32 uint16_t setup[1];
33 uint8_t param[2];
34 uint16_t major, minor;
35 uint32_t caplow, caphigh;
38 static void cli_unix_extensions_version_done(struct tevent_req *subreq);
40 struct tevent_req *cli_unix_extensions_version_send(TALLOC_CTX *mem_ctx,
41 struct tevent_context *ev,
42 struct cli_state *cli)
44 struct tevent_req *req, *subreq;
45 struct cli_unix_extensions_version_state *state;
47 req = tevent_req_create(mem_ctx, &state,
48 struct cli_unix_extensions_version_state);
49 if (req == NULL) {
50 return NULL;
52 state->cli = cli;
53 SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
54 SSVAL(state->param, 0, SMB_QUERY_CIFS_UNIX_INFO);
56 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
57 NULL, 0, 0, 0,
58 state->setup, 1, 0,
59 state->param, 2, 0,
60 NULL, 0, 560);
61 if (tevent_req_nomem(subreq, req)) {
62 return tevent_req_post(req, ev);
64 tevent_req_set_callback(subreq, cli_unix_extensions_version_done, req);
65 return req;
68 static void cli_unix_extensions_version_done(struct tevent_req *subreq)
70 struct tevent_req *req = tevent_req_callback_data(
71 subreq, struct tevent_req);
72 struct cli_unix_extensions_version_state *state = tevent_req_data(
73 req, struct cli_unix_extensions_version_state);
74 uint8_t *data;
75 uint32_t num_data;
76 NTSTATUS status;
78 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
79 NULL, 0, NULL, &data, 12, &num_data);
80 TALLOC_FREE(subreq);
81 if (!NT_STATUS_IS_OK(status)) {
82 tevent_req_nterror(req, status);
83 return;
86 state->major = SVAL(data, 0);
87 state->minor = SVAL(data, 2);
88 state->caplow = IVAL(data, 4);
89 state->caphigh = IVAL(data, 8);
90 TALLOC_FREE(data);
91 tevent_req_done(req);
94 NTSTATUS cli_unix_extensions_version_recv(struct tevent_req *req,
95 uint16_t *pmajor, uint16_t *pminor,
96 uint32_t *pcaplow,
97 uint32_t *pcaphigh)
99 struct cli_unix_extensions_version_state *state = tevent_req_data(
100 req, struct cli_unix_extensions_version_state);
101 NTSTATUS status;
103 if (tevent_req_is_nterror(req, &status)) {
104 return status;
106 *pmajor = state->major;
107 *pminor = state->minor;
108 *pcaplow = state->caplow;
109 *pcaphigh = state->caphigh;
110 state->cli->server_posix_capabilities = *pcaplow;
111 return NT_STATUS_OK;
114 NTSTATUS cli_unix_extensions_version(struct cli_state *cli, uint16 *pmajor,
115 uint16 *pminor, uint32 *pcaplow,
116 uint32 *pcaphigh)
118 TALLOC_CTX *frame = talloc_stackframe();
119 struct event_context *ev;
120 struct tevent_req *req;
121 NTSTATUS status = NT_STATUS_OK;
123 if (cli_has_async_calls(cli)) {
125 * Can't use sync call while an async call is in flight
127 status = NT_STATUS_INVALID_PARAMETER;
128 goto fail;
131 ev = event_context_init(frame);
132 if (ev == NULL) {
133 status = NT_STATUS_NO_MEMORY;
134 goto fail;
137 req = cli_unix_extensions_version_send(frame, ev, cli);
138 if (req == NULL) {
139 status = NT_STATUS_NO_MEMORY;
140 goto fail;
143 if (!tevent_req_poll(req, ev)) {
144 status = map_nt_error_from_unix(errno);
145 goto fail;
148 status = cli_unix_extensions_version_recv(req, pmajor, pminor, pcaplow,
149 pcaphigh);
150 fail:
151 TALLOC_FREE(frame);
152 if (!NT_STATUS_IS_OK(status)) {
153 cli_set_error(cli, status);
155 return status;
158 /****************************************************************************
159 Set UNIX extensions capabilities.
160 ****************************************************************************/
162 struct cli_set_unix_extensions_capabilities_state {
163 struct cli_state *cli;
164 uint16_t setup[1];
165 uint8_t param[4];
166 uint8_t data[12];
169 static void cli_set_unix_extensions_capabilities_done(
170 struct tevent_req *subreq);
172 struct tevent_req *cli_set_unix_extensions_capabilities_send(
173 TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
174 uint16_t major, uint16_t minor, uint32_t caplow, uint32_t caphigh)
176 struct tevent_req *req, *subreq;
177 struct cli_set_unix_extensions_capabilities_state *state;
179 req = tevent_req_create(
180 mem_ctx, &state,
181 struct cli_set_unix_extensions_capabilities_state);
182 if (req == NULL) {
183 return NULL;
186 state->cli = cli;
187 SSVAL(state->setup+0, 0, TRANSACT2_SETFSINFO);
189 SSVAL(state->param, 0, 0);
190 SSVAL(state->param, 2, SMB_SET_CIFS_UNIX_INFO);
192 SSVAL(state->data, 0, major);
193 SSVAL(state->data, 2, minor);
194 SIVAL(state->data, 4, caplow);
195 SIVAL(state->data, 8, caphigh);
197 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
198 NULL, 0, 0, 0,
199 state->setup, 1, 0,
200 state->param, 4, 0,
201 state->data, 12, 560);
202 if (tevent_req_nomem(subreq, req)) {
203 return tevent_req_post(req, ev);
205 tevent_req_set_callback(
206 subreq, cli_set_unix_extensions_capabilities_done, req);
207 return req;
210 static void cli_set_unix_extensions_capabilities_done(
211 struct tevent_req *subreq)
213 struct tevent_req *req = tevent_req_callback_data(
214 subreq, struct tevent_req);
215 struct cli_set_unix_extensions_capabilities_state *state = tevent_req_data(
216 req, struct cli_set_unix_extensions_capabilities_state);
218 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
219 NULL, 0, NULL, NULL, 0, NULL);
220 if (NT_STATUS_IS_OK(status)) {
221 state->cli->requested_posix_capabilities = IVAL(state->data, 4);
223 tevent_req_simple_finish_ntstatus(subreq, status);
226 NTSTATUS cli_set_unix_extensions_capabilities_recv(struct tevent_req *req)
228 return tevent_req_simple_recv_ntstatus(req);
231 NTSTATUS cli_set_unix_extensions_capabilities(struct cli_state *cli,
232 uint16 major, uint16 minor,
233 uint32 caplow, uint32 caphigh)
235 struct tevent_context *ev;
236 struct tevent_req *req;
237 NTSTATUS status = NT_STATUS_NO_MEMORY;
239 if (cli_has_async_calls(cli)) {
240 return NT_STATUS_INVALID_PARAMETER;
242 ev = tevent_context_init(talloc_tos());
243 if (ev == NULL) {
244 goto fail;
246 req = cli_set_unix_extensions_capabilities_send(
247 ev, ev, cli, major, minor, caplow, caphigh);
248 if (req == NULL) {
249 goto fail;
251 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
252 goto fail;
254 status = cli_set_unix_extensions_capabilities_recv(req);
255 fail:
256 TALLOC_FREE(ev);
257 if (!NT_STATUS_IS_OK(status)) {
258 cli_set_error(cli, status);
260 return status;
263 struct cli_get_fs_attr_info_state {
264 uint16_t setup[1];
265 uint8_t param[2];
266 uint32_t fs_attr;
269 static void cli_get_fs_attr_info_done(struct tevent_req *subreq);
271 struct tevent_req *cli_get_fs_attr_info_send(TALLOC_CTX *mem_ctx,
272 struct tevent_context *ev,
273 struct cli_state *cli)
275 struct tevent_req *subreq, *req;
276 struct cli_get_fs_attr_info_state *state;
278 req = tevent_req_create(mem_ctx, &state,
279 struct cli_get_fs_attr_info_state);
280 if (req == NULL) {
281 return NULL;
283 SSVAL(state->setup+0, 0, TRANSACT2_QFSINFO);
284 SSVAL(state->param+0, 0, SMB_QUERY_FS_ATTRIBUTE_INFO);
286 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
287 NULL, 0, 0, 0,
288 state->setup, 1, 0,
289 state->param, 2, 0,
290 NULL, 0, 560);
291 if (tevent_req_nomem(subreq, req)) {
292 return tevent_req_post(req, ev);
294 tevent_req_set_callback(subreq, cli_get_fs_attr_info_done, req);
295 return req;
298 static void cli_get_fs_attr_info_done(struct tevent_req *subreq)
300 struct tevent_req *req = tevent_req_callback_data(
301 subreq, struct tevent_req);
302 struct cli_get_fs_attr_info_state *state = tevent_req_data(
303 req, struct cli_get_fs_attr_info_state);
304 uint8_t *data;
305 uint32_t num_data;
306 NTSTATUS status;
308 status = cli_trans_recv(subreq, talloc_tos(), NULL, NULL, 0, NULL,
309 NULL, 0, NULL, &data, 12, &num_data);
310 TALLOC_FREE(subreq);
311 if (!NT_STATUS_IS_OK(status)) {
312 tevent_req_nterror(req, status);
313 return;
315 state->fs_attr = IVAL(data, 0);
316 TALLOC_FREE(data);
317 tevent_req_done(req);
320 NTSTATUS cli_get_fs_attr_info_recv(struct tevent_req *req, uint32_t *fs_attr)
322 struct cli_get_fs_attr_info_state *state = tevent_req_data(
323 req, struct cli_get_fs_attr_info_state);
324 NTSTATUS status;
326 if (tevent_req_is_nterror(req, &status)) {
327 return status;
329 *fs_attr = state->fs_attr;
330 return NT_STATUS_OK;
333 NTSTATUS cli_get_fs_attr_info(struct cli_state *cli, uint32_t *fs_attr)
335 struct tevent_context *ev;
336 struct tevent_req *req;
337 NTSTATUS status = NT_STATUS_NO_MEMORY;
339 if (cli_has_async_calls(cli)) {
340 return NT_STATUS_INVALID_PARAMETER;
342 ev = tevent_context_init(talloc_tos());
343 if (ev == NULL) {
344 goto fail;
346 req = cli_get_fs_attr_info_send(ev, ev, cli);
347 if (req == NULL) {
348 goto fail;
350 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
351 goto fail;
353 status = cli_get_fs_attr_info_recv(req, fs_attr);
354 fail:
355 TALLOC_FREE(ev);
356 if (!NT_STATUS_IS_OK(status)) {
357 cli_set_error(cli, status);
359 return status;
362 NTSTATUS cli_get_fs_volume_info(struct cli_state *cli, fstring volume_name,
363 uint32 *pserial_number, time_t *pdate)
365 NTSTATUS status;
366 uint16 setup[1];
367 uint8_t param[2];
368 uint8_t *rdata;
369 uint32_t rdata_count;
370 unsigned int nlen;
372 SSVAL(setup, 0, TRANSACT2_QFSINFO);
373 SSVAL(param,0,SMB_QUERY_FS_VOLUME_INFO);
375 status = cli_trans(talloc_tos(), cli, SMBtrans2,
376 NULL, 0, 0, 0,
377 setup, 1, 0,
378 param, 2, 0,
379 NULL, 0, 560,
380 NULL,
381 NULL, 0, NULL,
382 NULL, 0, NULL,
383 &rdata, 10, &rdata_count);
384 if (!NT_STATUS_IS_OK(status)) {
385 return status;
388 if (pdate) {
389 struct timespec ts;
390 ts = interpret_long_date((char *)rdata);
391 *pdate = ts.tv_sec;
393 if (pserial_number) {
394 *pserial_number = IVAL(rdata,8);
396 nlen = IVAL(rdata,12);
397 clistr_pull(cli->inbuf, volume_name, rdata + 18, sizeof(fstring),
398 nlen, STR_UNICODE);
400 /* todo: but not yet needed
401 * return the other stuff
404 TALLOC_FREE(rdata);
405 return NT_STATUS_OK;
408 bool cli_get_fs_full_size_info(struct cli_state *cli,
409 uint64_t *total_allocation_units,
410 uint64_t *caller_allocation_units,
411 uint64_t *actual_allocation_units,
412 uint64_t *sectors_per_allocation_unit,
413 uint64_t *bytes_per_sector)
415 bool ret = False;
416 uint16 setup;
417 char param[2];
418 char *rparam=NULL, *rdata=NULL;
419 unsigned int rparam_count=0, rdata_count=0;
421 setup = TRANSACT2_QFSINFO;
423 SSVAL(param,0,SMB_FS_FULL_SIZE_INFORMATION);
425 if (!cli_send_trans(cli, SMBtrans2,
426 NULL,
427 0, 0,
428 &setup, 1, 0,
429 param, 2, 0,
430 NULL, 0, 560)) {
431 goto cleanup;
434 if (!cli_receive_trans(cli, SMBtrans2,
435 &rparam, &rparam_count,
436 &rdata, &rdata_count)) {
437 goto cleanup;
440 if (cli_is_error(cli)) {
441 ret = False;
442 goto cleanup;
443 } else {
444 ret = True;
447 if (rdata_count != 32) {
448 goto cleanup;
451 if (total_allocation_units) {
452 *total_allocation_units = BIG_UINT(rdata, 0);
454 if (caller_allocation_units) {
455 *caller_allocation_units = BIG_UINT(rdata,8);
457 if (actual_allocation_units) {
458 *actual_allocation_units = BIG_UINT(rdata,16);
460 if (sectors_per_allocation_unit) {
461 *sectors_per_allocation_unit = IVAL(rdata,24);
463 if (bytes_per_sector) {
464 *bytes_per_sector = IVAL(rdata,28);
467 cleanup:
468 SAFE_FREE(rparam);
469 SAFE_FREE(rdata);
471 return ret;
474 bool cli_get_posix_fs_info(struct cli_state *cli,
475 uint32 *optimal_transfer_size,
476 uint32 *block_size,
477 uint64_t *total_blocks,
478 uint64_t *blocks_available,
479 uint64_t *user_blocks_available,
480 uint64_t *total_file_nodes,
481 uint64_t *free_file_nodes,
482 uint64_t *fs_identifier)
484 bool ret = False;
485 uint16 setup;
486 char param[2];
487 char *rparam=NULL, *rdata=NULL;
488 unsigned int rparam_count=0, rdata_count=0;
490 setup = TRANSACT2_QFSINFO;
492 SSVAL(param,0,SMB_QUERY_POSIX_FS_INFO);
494 if (!cli_send_trans(cli, SMBtrans2,
495 NULL,
496 0, 0,
497 &setup, 1, 0,
498 param, 2, 0,
499 NULL, 0, 560)) {
500 goto cleanup;
503 if (!cli_receive_trans(cli, SMBtrans2,
504 &rparam, &rparam_count,
505 &rdata, &rdata_count)) {
506 goto cleanup;
509 if (cli_is_error(cli)) {
510 ret = False;
511 goto cleanup;
512 } else {
513 ret = True;
516 if (rdata_count != 56) {
517 goto cleanup;
520 if (optimal_transfer_size) {
521 *optimal_transfer_size = IVAL(rdata, 0);
523 if (block_size) {
524 *block_size = IVAL(rdata,4);
526 if (total_blocks) {
527 *total_blocks = BIG_UINT(rdata,8);
529 if (blocks_available) {
530 *blocks_available = BIG_UINT(rdata,16);
532 if (user_blocks_available) {
533 *user_blocks_available = BIG_UINT(rdata,24);
535 if (total_file_nodes) {
536 *total_file_nodes = BIG_UINT(rdata,32);
538 if (free_file_nodes) {
539 *free_file_nodes = BIG_UINT(rdata,40);
541 if (fs_identifier) {
542 *fs_identifier = BIG_UINT(rdata,48);
545 cleanup:
546 SAFE_FREE(rparam);
547 SAFE_FREE(rdata);
549 return ret;
553 /******************************************************************************
554 Send/receive the request encryption blob.
555 ******************************************************************************/
557 static NTSTATUS enc_blob_send_receive(struct cli_state *cli, DATA_BLOB *in, DATA_BLOB *out, DATA_BLOB *param_out)
559 uint16 setup;
560 char param[4];
561 char *rparam=NULL, *rdata=NULL;
562 unsigned int rparam_count=0, rdata_count=0;
563 NTSTATUS status = NT_STATUS_OK;
565 setup = TRANSACT2_SETFSINFO;
567 SSVAL(param,0,0);
568 SSVAL(param,2,SMB_REQUEST_TRANSPORT_ENCRYPTION);
570 if (!cli_send_trans(cli, SMBtrans2,
571 NULL,
572 0, 0,
573 &setup, 1, 0,
574 param, 4, 0,
575 (char *)in->data, in->length, CLI_BUFFER_SIZE)) {
576 status = cli_nt_error(cli);
577 goto out;
580 if (!cli_receive_trans(cli, SMBtrans2,
581 &rparam, &rparam_count,
582 &rdata, &rdata_count)) {
583 status = cli_nt_error(cli);
584 goto out;
587 if (cli_is_error(cli)) {
588 status = cli_nt_error(cli);
589 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
590 goto out;
594 *out = data_blob(rdata, rdata_count);
595 *param_out = data_blob(rparam, rparam_count);
597 out:
599 SAFE_FREE(rparam);
600 SAFE_FREE(rdata);
601 return status;
604 /******************************************************************************
605 Make a client state struct.
606 ******************************************************************************/
608 static struct smb_trans_enc_state *make_cli_enc_state(enum smb_trans_enc_type smb_enc_type)
610 struct smb_trans_enc_state *es = NULL;
611 es = SMB_MALLOC_P(struct smb_trans_enc_state);
612 if (!es) {
613 return NULL;
615 ZERO_STRUCTP(es);
616 es->smb_enc_type = smb_enc_type;
618 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
619 if (smb_enc_type == SMB_TRANS_ENC_GSS) {
620 es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
621 if (!es->s.gss_state) {
622 SAFE_FREE(es);
623 return NULL;
625 ZERO_STRUCTP(es->s.gss_state);
627 #endif
628 return es;
631 /******************************************************************************
632 Start a raw ntlmssp encryption.
633 ******************************************************************************/
635 NTSTATUS cli_raw_ntlm_smb_encryption_start(struct cli_state *cli,
636 const char *user,
637 const char *pass,
638 const char *domain)
640 DATA_BLOB blob_in = data_blob_null;
641 DATA_BLOB blob_out = data_blob_null;
642 DATA_BLOB param_out = data_blob_null;
643 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
644 struct smb_trans_enc_state *es = make_cli_enc_state(SMB_TRANS_ENC_NTLM);
646 if (!es) {
647 return NT_STATUS_NO_MEMORY;
649 status = ntlmssp_client_start(NULL,
650 global_myname(),
651 lp_workgroup(),
652 lp_client_ntlmv2_auth(),
653 &es->s.ntlmssp_state);
654 if (!NT_STATUS_IS_OK(status)) {
655 goto fail;
658 ntlmssp_want_feature(es->s.ntlmssp_state, NTLMSSP_FEATURE_SESSION_KEY);
659 es->s.ntlmssp_state->neg_flags |= (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL);
661 if (!NT_STATUS_IS_OK(status = ntlmssp_set_username(es->s.ntlmssp_state, user))) {
662 goto fail;
664 if (!NT_STATUS_IS_OK(status = ntlmssp_set_domain(es->s.ntlmssp_state, domain))) {
665 goto fail;
667 if (!NT_STATUS_IS_OK(status = ntlmssp_set_password(es->s.ntlmssp_state, pass))) {
668 goto fail;
671 do {
672 status = ntlmssp_update(es->s.ntlmssp_state, blob_in, &blob_out);
673 data_blob_free(&blob_in);
674 data_blob_free(&param_out);
675 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) || NT_STATUS_IS_OK(status)) {
676 NTSTATUS trans_status = enc_blob_send_receive(cli,
677 &blob_out,
678 &blob_in,
679 &param_out);
680 if (!NT_STATUS_EQUAL(trans_status,
681 NT_STATUS_MORE_PROCESSING_REQUIRED) &&
682 !NT_STATUS_IS_OK(trans_status)) {
683 status = trans_status;
684 } else {
685 if (param_out.length == 2) {
686 es->enc_ctx_num = SVAL(param_out.data, 0);
690 data_blob_free(&blob_out);
691 } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
693 data_blob_free(&blob_in);
695 if (NT_STATUS_IS_OK(status)) {
696 /* Replace the old state, if any. */
697 if (cli->trans_enc_state) {
698 common_free_encryption_state(&cli->trans_enc_state);
700 cli->trans_enc_state = es;
701 cli->trans_enc_state->enc_on = True;
702 es = NULL;
705 fail:
707 common_free_encryption_state(&es);
708 return status;
711 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
713 #ifndef SMB_GSS_REQUIRED_FLAGS
714 #define SMB_GSS_REQUIRED_FLAGS (GSS_C_CONF_FLAG|GSS_C_INTEG_FLAG|GSS_C_MUTUAL_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)
715 #endif
717 /******************************************************************************
718 Get client gss blob to send to a server.
719 ******************************************************************************/
721 static NTSTATUS make_cli_gss_blob(TALLOC_CTX *ctx,
722 struct smb_trans_enc_state *es,
723 const char *service,
724 const char *host,
725 NTSTATUS status_in,
726 DATA_BLOB spnego_blob_in,
727 DATA_BLOB *p_blob_out)
729 const char *krb_mechs[] = {OID_KERBEROS5, NULL};
730 OM_uint32 ret;
731 OM_uint32 min;
732 gss_name_t srv_name;
733 gss_buffer_desc input_name;
734 gss_buffer_desc *p_tok_in;
735 gss_buffer_desc tok_out, tok_in;
736 DATA_BLOB blob_out = data_blob_null;
737 DATA_BLOB blob_in = data_blob_null;
738 char *host_princ_s = NULL;
739 OM_uint32 ret_flags = 0;
740 NTSTATUS status = NT_STATUS_OK;
742 gss_OID_desc nt_hostbased_service =
743 {10, CONST_DISCARD(char *,"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
745 memset(&tok_out, '\0', sizeof(tok_out));
747 /* Get a ticket for the service@host */
748 if (asprintf(&host_princ_s, "%s@%s", service, host) == -1) {
749 return NT_STATUS_NO_MEMORY;
752 input_name.value = host_princ_s;
753 input_name.length = strlen(host_princ_s) + 1;
755 ret = gss_import_name(&min,
756 &input_name,
757 &nt_hostbased_service,
758 &srv_name);
760 if (ret != GSS_S_COMPLETE) {
761 SAFE_FREE(host_princ_s);
762 return map_nt_error_from_gss(ret, min);
765 if (spnego_blob_in.length == 0) {
766 p_tok_in = GSS_C_NO_BUFFER;
767 } else {
768 /* Remove the SPNEGO wrapper */
769 if (!spnego_parse_auth_response(ctx, spnego_blob_in, status_in, OID_KERBEROS5, &blob_in)) {
770 status = NT_STATUS_UNSUCCESSFUL;
771 goto fail;
773 tok_in.value = blob_in.data;
774 tok_in.length = blob_in.length;
775 p_tok_in = &tok_in;
778 ret = gss_init_sec_context(&min,
779 GSS_C_NO_CREDENTIAL, /* Use our default cred. */
780 &es->s.gss_state->gss_ctx,
781 srv_name,
782 GSS_C_NO_OID, /* default OID. */
783 GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG | GSS_C_DELEG_FLAG,
784 GSS_C_INDEFINITE, /* requested ticket lifetime. */
785 NULL, /* no channel bindings */
786 p_tok_in,
787 NULL, /* ignore mech type */
788 &tok_out,
789 &ret_flags,
790 NULL); /* ignore time_rec */
792 status = map_nt_error_from_gss(ret, min);
793 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
794 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
795 DEBUG(10,("make_cli_gss_blob: gss_init_sec_context failed with %s\n",
796 ads_errstr(adss)));
797 goto fail;
800 if ((ret_flags & SMB_GSS_REQUIRED_FLAGS) != SMB_GSS_REQUIRED_FLAGS) {
801 status = NT_STATUS_ACCESS_DENIED;
804 blob_out = data_blob_talloc(ctx, tok_out.value, tok_out.length);
806 /* Wrap in an SPNEGO wrapper */
807 *p_blob_out = spnego_gen_negTokenInit(ctx, krb_mechs, &blob_out, NULL);
809 fail:
811 data_blob_free(&blob_out);
812 data_blob_free(&blob_in);
813 SAFE_FREE(host_princ_s);
814 gss_release_name(&min, &srv_name);
815 if (tok_out.value) {
816 gss_release_buffer(&min, &tok_out);
818 return status;
821 /******************************************************************************
822 Start a SPNEGO gssapi encryption context.
823 ******************************************************************************/
825 NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
827 DATA_BLOB blob_recv = data_blob_null;
828 DATA_BLOB blob_send = data_blob_null;
829 DATA_BLOB param_out = data_blob_null;
830 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
831 fstring fqdn;
832 const char *servicename;
833 struct smb_trans_enc_state *es = make_cli_enc_state(SMB_TRANS_ENC_GSS);
835 if (!es) {
836 return NT_STATUS_NO_MEMORY;
839 name_to_fqdn(fqdn, cli->desthost);
840 strlower_m(fqdn);
842 servicename = "cifs";
843 status = make_cli_gss_blob(talloc_tos(), es, servicename, fqdn, NT_STATUS_OK, blob_recv, &blob_send);
844 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
845 servicename = "host";
846 status = make_cli_gss_blob(talloc_tos(), es, servicename, fqdn, NT_STATUS_OK, blob_recv, &blob_send);
847 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
848 goto fail;
852 do {
853 data_blob_free(&blob_recv);
854 status = enc_blob_send_receive(cli, &blob_send, &blob_recv, &param_out);
855 if (param_out.length == 2) {
856 es->enc_ctx_num = SVAL(param_out.data, 0);
858 data_blob_free(&blob_send);
859 status = make_cli_gss_blob(talloc_tos(), es, servicename, fqdn, status, blob_recv, &blob_send);
860 } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
861 data_blob_free(&blob_recv);
863 if (NT_STATUS_IS_OK(status)) {
864 /* Replace the old state, if any. */
865 if (cli->trans_enc_state) {
866 common_free_encryption_state(&cli->trans_enc_state);
868 cli->trans_enc_state = es;
869 cli->trans_enc_state->enc_on = True;
870 es = NULL;
873 fail:
875 common_free_encryption_state(&es);
876 return status;
878 #else
879 NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
881 return NT_STATUS_NOT_SUPPORTED;
883 #endif
885 /********************************************************************
886 Ensure a connection is encrypted.
887 ********************************************************************/
889 NTSTATUS cli_force_encryption(struct cli_state *c,
890 const char *username,
891 const char *password,
892 const char *domain)
894 uint16 major, minor;
895 uint32 caplow, caphigh;
896 NTSTATUS status;
898 if (!SERVER_HAS_UNIX_CIFS(c)) {
899 return NT_STATUS_NOT_SUPPORTED;
902 status = cli_unix_extensions_version(c, &major, &minor, &caplow,
903 &caphigh);
904 if (!NT_STATUS_IS_OK(status)) {
905 DEBUG(10, ("cli_force_encryption: cli_unix_extensions_version "
906 "returned %s\n", nt_errstr(status)));
907 return NT_STATUS_UNKNOWN_REVISION;
910 if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
911 return NT_STATUS_UNSUPPORTED_COMPRESSION;
914 if (c->use_kerberos) {
915 return cli_gss_smb_encryption_start(c);
917 return cli_raw_ntlm_smb_encryption_start(c,
918 username,
919 password,
920 domain);