s3-tevent: only include ../lib/util/tevent wrappers where needed.
[Samba.git] / source3 / libsmb / clifsinfo.c
blob4b3383a1d2488b35bf70c38485e78f0fab966661
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 "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "smb_crypt.h"
27 #include "trans2.h"
29 /****************************************************************************
30 Get UNIX extensions version info.
31 ****************************************************************************/
33 struct cli_unix_extensions_version_state {
34 struct cli_state *cli;
35 uint16_t setup[1];
36 uint8_t param[2];
37 uint16_t major, minor;
38 uint32_t caplow, caphigh;
41 static void cli_unix_extensions_version_done(struct tevent_req *subreq);
43 struct tevent_req *cli_unix_extensions_version_send(TALLOC_CTX *mem_ctx,
44 struct tevent_context *ev,
45 struct cli_state *cli)
47 struct tevent_req *req, *subreq;
48 struct cli_unix_extensions_version_state *state;
50 req = tevent_req_create(mem_ctx, &state,
51 struct cli_unix_extensions_version_state);
52 if (req == NULL) {
53 return NULL;
55 state->cli = cli;
56 SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
57 SSVAL(state->param, 0, SMB_QUERY_CIFS_UNIX_INFO);
59 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
60 NULL, 0, 0, 0,
61 state->setup, 1, 0,
62 state->param, 2, 0,
63 NULL, 0, 560);
64 if (tevent_req_nomem(subreq, req)) {
65 return tevent_req_post(req, ev);
67 tevent_req_set_callback(subreq, cli_unix_extensions_version_done, req);
68 return req;
71 static void cli_unix_extensions_version_done(struct tevent_req *subreq)
73 struct tevent_req *req = tevent_req_callback_data(
74 subreq, struct tevent_req);
75 struct cli_unix_extensions_version_state *state = tevent_req_data(
76 req, struct cli_unix_extensions_version_state);
77 uint8_t *data;
78 uint32_t num_data;
79 NTSTATUS status;
81 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
82 NULL, 0, NULL, &data, 12, &num_data);
83 TALLOC_FREE(subreq);
84 if (!NT_STATUS_IS_OK(status)) {
85 tevent_req_nterror(req, status);
86 return;
89 state->major = SVAL(data, 0);
90 state->minor = SVAL(data, 2);
91 state->caplow = IVAL(data, 4);
92 state->caphigh = IVAL(data, 8);
93 TALLOC_FREE(data);
94 tevent_req_done(req);
97 NTSTATUS cli_unix_extensions_version_recv(struct tevent_req *req,
98 uint16_t *pmajor, uint16_t *pminor,
99 uint32_t *pcaplow,
100 uint32_t *pcaphigh)
102 struct cli_unix_extensions_version_state *state = tevent_req_data(
103 req, struct cli_unix_extensions_version_state);
104 NTSTATUS status;
106 if (tevent_req_is_nterror(req, &status)) {
107 return status;
109 *pmajor = state->major;
110 *pminor = state->minor;
111 *pcaplow = state->caplow;
112 *pcaphigh = state->caphigh;
113 state->cli->server_posix_capabilities = *pcaplow;
114 return NT_STATUS_OK;
117 NTSTATUS cli_unix_extensions_version(struct cli_state *cli, uint16 *pmajor,
118 uint16 *pminor, uint32 *pcaplow,
119 uint32 *pcaphigh)
121 TALLOC_CTX *frame = talloc_stackframe();
122 struct event_context *ev;
123 struct tevent_req *req;
124 NTSTATUS status = NT_STATUS_OK;
126 if (cli_has_async_calls(cli)) {
128 * Can't use sync call while an async call is in flight
130 status = NT_STATUS_INVALID_PARAMETER;
131 goto fail;
134 ev = event_context_init(frame);
135 if (ev == NULL) {
136 status = NT_STATUS_NO_MEMORY;
137 goto fail;
140 req = cli_unix_extensions_version_send(frame, ev, cli);
141 if (req == NULL) {
142 status = NT_STATUS_NO_MEMORY;
143 goto fail;
146 if (!tevent_req_poll(req, ev)) {
147 status = map_nt_error_from_unix(errno);
148 goto fail;
151 status = cli_unix_extensions_version_recv(req, pmajor, pminor, pcaplow,
152 pcaphigh);
153 fail:
154 TALLOC_FREE(frame);
155 if (!NT_STATUS_IS_OK(status)) {
156 cli_set_error(cli, status);
158 return status;
161 /****************************************************************************
162 Set UNIX extensions capabilities.
163 ****************************************************************************/
165 struct cli_set_unix_extensions_capabilities_state {
166 struct cli_state *cli;
167 uint16_t setup[1];
168 uint8_t param[4];
169 uint8_t data[12];
172 static void cli_set_unix_extensions_capabilities_done(
173 struct tevent_req *subreq);
175 struct tevent_req *cli_set_unix_extensions_capabilities_send(
176 TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
177 uint16_t major, uint16_t minor, uint32_t caplow, uint32_t caphigh)
179 struct tevent_req *req, *subreq;
180 struct cli_set_unix_extensions_capabilities_state *state;
182 req = tevent_req_create(
183 mem_ctx, &state,
184 struct cli_set_unix_extensions_capabilities_state);
185 if (req == NULL) {
186 return NULL;
189 state->cli = cli;
190 SSVAL(state->setup+0, 0, TRANSACT2_SETFSINFO);
192 SSVAL(state->param, 0, 0);
193 SSVAL(state->param, 2, SMB_SET_CIFS_UNIX_INFO);
195 SSVAL(state->data, 0, major);
196 SSVAL(state->data, 2, minor);
197 SIVAL(state->data, 4, caplow);
198 SIVAL(state->data, 8, caphigh);
200 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
201 NULL, 0, 0, 0,
202 state->setup, 1, 0,
203 state->param, 4, 0,
204 state->data, 12, 560);
205 if (tevent_req_nomem(subreq, req)) {
206 return tevent_req_post(req, ev);
208 tevent_req_set_callback(
209 subreq, cli_set_unix_extensions_capabilities_done, req);
210 return req;
213 static void cli_set_unix_extensions_capabilities_done(
214 struct tevent_req *subreq)
216 struct tevent_req *req = tevent_req_callback_data(
217 subreq, struct tevent_req);
218 struct cli_set_unix_extensions_capabilities_state *state = tevent_req_data(
219 req, struct cli_set_unix_extensions_capabilities_state);
221 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
222 NULL, 0, NULL, NULL, 0, NULL);
223 if (NT_STATUS_IS_OK(status)) {
224 state->cli->requested_posix_capabilities = IVAL(state->data, 4);
226 tevent_req_simple_finish_ntstatus(subreq, status);
229 NTSTATUS cli_set_unix_extensions_capabilities_recv(struct tevent_req *req)
231 return tevent_req_simple_recv_ntstatus(req);
234 NTSTATUS cli_set_unix_extensions_capabilities(struct cli_state *cli,
235 uint16 major, uint16 minor,
236 uint32 caplow, uint32 caphigh)
238 struct tevent_context *ev;
239 struct tevent_req *req;
240 NTSTATUS status = NT_STATUS_NO_MEMORY;
242 if (cli_has_async_calls(cli)) {
243 return NT_STATUS_INVALID_PARAMETER;
245 ev = tevent_context_init(talloc_tos());
246 if (ev == NULL) {
247 goto fail;
249 req = cli_set_unix_extensions_capabilities_send(
250 ev, ev, cli, major, minor, caplow, caphigh);
251 if (req == NULL) {
252 goto fail;
254 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
255 goto fail;
257 status = cli_set_unix_extensions_capabilities_recv(req);
258 fail:
259 TALLOC_FREE(ev);
260 if (!NT_STATUS_IS_OK(status)) {
261 cli_set_error(cli, status);
263 return status;
266 struct cli_get_fs_attr_info_state {
267 uint16_t setup[1];
268 uint8_t param[2];
269 uint32_t fs_attr;
272 static void cli_get_fs_attr_info_done(struct tevent_req *subreq);
274 struct tevent_req *cli_get_fs_attr_info_send(TALLOC_CTX *mem_ctx,
275 struct tevent_context *ev,
276 struct cli_state *cli)
278 struct tevent_req *subreq, *req;
279 struct cli_get_fs_attr_info_state *state;
281 req = tevent_req_create(mem_ctx, &state,
282 struct cli_get_fs_attr_info_state);
283 if (req == NULL) {
284 return NULL;
286 SSVAL(state->setup+0, 0, TRANSACT2_QFSINFO);
287 SSVAL(state->param+0, 0, SMB_QUERY_FS_ATTRIBUTE_INFO);
289 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
290 NULL, 0, 0, 0,
291 state->setup, 1, 0,
292 state->param, 2, 0,
293 NULL, 0, 560);
294 if (tevent_req_nomem(subreq, req)) {
295 return tevent_req_post(req, ev);
297 tevent_req_set_callback(subreq, cli_get_fs_attr_info_done, req);
298 return req;
301 static void cli_get_fs_attr_info_done(struct tevent_req *subreq)
303 struct tevent_req *req = tevent_req_callback_data(
304 subreq, struct tevent_req);
305 struct cli_get_fs_attr_info_state *state = tevent_req_data(
306 req, struct cli_get_fs_attr_info_state);
307 uint8_t *data;
308 uint32_t num_data;
309 NTSTATUS status;
311 status = cli_trans_recv(subreq, talloc_tos(), NULL, NULL, 0, NULL,
312 NULL, 0, NULL, &data, 12, &num_data);
313 TALLOC_FREE(subreq);
314 if (!NT_STATUS_IS_OK(status)) {
315 tevent_req_nterror(req, status);
316 return;
318 state->fs_attr = IVAL(data, 0);
319 TALLOC_FREE(data);
320 tevent_req_done(req);
323 NTSTATUS cli_get_fs_attr_info_recv(struct tevent_req *req, uint32_t *fs_attr)
325 struct cli_get_fs_attr_info_state *state = tevent_req_data(
326 req, struct cli_get_fs_attr_info_state);
327 NTSTATUS status;
329 if (tevent_req_is_nterror(req, &status)) {
330 return status;
332 *fs_attr = state->fs_attr;
333 return NT_STATUS_OK;
336 NTSTATUS cli_get_fs_attr_info(struct cli_state *cli, uint32_t *fs_attr)
338 struct tevent_context *ev;
339 struct tevent_req *req;
340 NTSTATUS status = NT_STATUS_NO_MEMORY;
342 if (cli_has_async_calls(cli)) {
343 return NT_STATUS_INVALID_PARAMETER;
345 ev = tevent_context_init(talloc_tos());
346 if (ev == NULL) {
347 goto fail;
349 req = cli_get_fs_attr_info_send(ev, ev, cli);
350 if (req == NULL) {
351 goto fail;
353 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
354 goto fail;
356 status = cli_get_fs_attr_info_recv(req, fs_attr);
357 fail:
358 TALLOC_FREE(ev);
359 if (!NT_STATUS_IS_OK(status)) {
360 cli_set_error(cli, status);
362 return status;
365 NTSTATUS cli_get_fs_volume_info(struct cli_state *cli, fstring volume_name,
366 uint32 *pserial_number, time_t *pdate)
368 NTSTATUS status;
369 uint16 setup[1];
370 uint8_t param[2];
371 uint8_t *rdata;
372 uint32_t rdata_count;
373 unsigned int nlen;
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 NULL,
384 NULL, 0, NULL,
385 NULL, 0, NULL,
386 &rdata, 10, &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 clistr_pull(cli->inbuf, volume_name, rdata + 18, sizeof(fstring),
401 nlen, STR_UNICODE);
403 /* todo: but not yet needed
404 * return the other stuff
407 TALLOC_FREE(rdata);
408 return NT_STATUS_OK;
411 NTSTATUS cli_get_fs_full_size_info(struct cli_state *cli,
412 uint64_t *total_allocation_units,
413 uint64_t *caller_allocation_units,
414 uint64_t *actual_allocation_units,
415 uint64_t *sectors_per_allocation_unit,
416 uint64_t *bytes_per_sector)
418 uint16 setup[1];
419 uint8_t param[2];
420 uint8_t *rdata = NULL;
421 uint32_t rdata_count;
422 NTSTATUS status;
424 SSVAL(setup, 0, TRANSACT2_QFSINFO);
425 SSVAL(param, 0, SMB_FS_FULL_SIZE_INFORMATION);
427 status = cli_trans(talloc_tos(), cli, SMBtrans2,
428 NULL, 0, 0, 0,
429 setup, 1, 0, /* setup */
430 param, 2, 0, /* param */
431 NULL, 0, 560, /* data */
432 NULL,
433 NULL, 0, NULL, /* rsetup */
434 NULL, 0, NULL, /* rparam */
435 &rdata, 32, &rdata_count); /* rdata */
436 if (!NT_STATUS_IS_OK(status)) {
437 goto fail;
440 if (total_allocation_units) {
441 *total_allocation_units = BIG_UINT(rdata, 0);
443 if (caller_allocation_units) {
444 *caller_allocation_units = BIG_UINT(rdata,8);
446 if (actual_allocation_units) {
447 *actual_allocation_units = BIG_UINT(rdata,16);
449 if (sectors_per_allocation_unit) {
450 *sectors_per_allocation_unit = IVAL(rdata,24);
452 if (bytes_per_sector) {
453 *bytes_per_sector = IVAL(rdata,28);
456 fail:
457 TALLOC_FREE(rdata);
458 return status;
461 NTSTATUS cli_get_posix_fs_info(struct cli_state *cli,
462 uint32 *optimal_transfer_size,
463 uint32 *block_size,
464 uint64_t *total_blocks,
465 uint64_t *blocks_available,
466 uint64_t *user_blocks_available,
467 uint64_t *total_file_nodes,
468 uint64_t *free_file_nodes,
469 uint64_t *fs_identifier)
471 uint16 setup[1];
472 uint8_t param[2];
473 uint8_t *rdata = NULL;
474 NTSTATUS status;
476 SSVAL(setup, 0, TRANSACT2_QFSINFO);
477 SSVAL(param,0,SMB_QUERY_POSIX_FS_INFO);
479 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, 0, 0, 0,
480 setup, 1, 0,
481 param, 2, 0,
482 NULL, 0, 560,
483 NULL,
484 NULL, 0, NULL, /* rsetup */
485 NULL, 0, NULL, /* rparam */
486 &rdata, 56, NULL);
487 if (!NT_STATUS_IS_OK(status)) {
488 return status;
491 if (optimal_transfer_size) {
492 *optimal_transfer_size = IVAL(rdata, 0);
494 if (block_size) {
495 *block_size = IVAL(rdata,4);
497 if (total_blocks) {
498 *total_blocks = BIG_UINT(rdata,8);
500 if (blocks_available) {
501 *blocks_available = BIG_UINT(rdata,16);
503 if (user_blocks_available) {
504 *user_blocks_available = BIG_UINT(rdata,24);
506 if (total_file_nodes) {
507 *total_file_nodes = BIG_UINT(rdata,32);
509 if (free_file_nodes) {
510 *free_file_nodes = BIG_UINT(rdata,40);
512 if (fs_identifier) {
513 *fs_identifier = BIG_UINT(rdata,48);
515 return NT_STATUS_OK;
519 /******************************************************************************
520 Send/receive the request encryption blob.
521 ******************************************************************************/
523 static NTSTATUS enc_blob_send_receive(struct cli_state *cli, DATA_BLOB *in, DATA_BLOB *out, DATA_BLOB *param_out)
525 uint16_t setup[1];
526 uint8_t param[4];
527 uint8_t *rparam=NULL, *rdata=NULL;
528 uint32_t num_rparam, num_rdata;
529 NTSTATUS status;
531 SSVAL(setup+0, 0, TRANSACT2_SETFSINFO);
532 SSVAL(param,0,0);
533 SSVAL(param,2,SMB_REQUEST_TRANSPORT_ENCRYPTION);
535 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, 0, 0, 0,
536 setup, 1, 0,
537 param, 4, 2,
538 (uint8_t *)in->data, in->length, CLI_BUFFER_SIZE,
539 NULL, /* recv_flags */
540 NULL, 0, NULL, /* rsetup */
541 &rparam, 0, &num_rparam,
542 &rdata, 0, &num_rdata);
544 if (!NT_STATUS_IS_OK(status) &&
545 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
546 return status;
549 *out = data_blob(rdata, num_rdata);
550 *param_out = data_blob(rparam, num_rparam);
552 TALLOC_FREE(rparam);
553 TALLOC_FREE(rdata);
554 return status;
557 /******************************************************************************
558 Make a client state struct.
559 ******************************************************************************/
561 static struct smb_trans_enc_state *make_cli_enc_state(enum smb_trans_enc_type smb_enc_type)
563 struct smb_trans_enc_state *es = NULL;
564 es = SMB_MALLOC_P(struct smb_trans_enc_state);
565 if (!es) {
566 return NULL;
568 ZERO_STRUCTP(es);
569 es->smb_enc_type = smb_enc_type;
571 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
572 if (smb_enc_type == SMB_TRANS_ENC_GSS) {
573 es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
574 if (!es->s.gss_state) {
575 SAFE_FREE(es);
576 return NULL;
578 ZERO_STRUCTP(es->s.gss_state);
580 #endif
581 return es;
584 /******************************************************************************
585 Start a raw ntlmssp encryption.
586 ******************************************************************************/
588 NTSTATUS cli_raw_ntlm_smb_encryption_start(struct cli_state *cli,
589 const char *user,
590 const char *pass,
591 const char *domain)
593 DATA_BLOB blob_in = data_blob_null;
594 DATA_BLOB blob_out = data_blob_null;
595 DATA_BLOB param_out = data_blob_null;
596 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
597 struct smb_trans_enc_state *es = make_cli_enc_state(SMB_TRANS_ENC_NTLM);
599 if (!es) {
600 return NT_STATUS_NO_MEMORY;
602 status = ntlmssp_client_start(NULL,
603 global_myname(),
604 lp_workgroup(),
605 lp_client_ntlmv2_auth(),
606 &es->s.ntlmssp_state);
607 if (!NT_STATUS_IS_OK(status)) {
608 goto fail;
611 ntlmssp_want_feature(es->s.ntlmssp_state, NTLMSSP_FEATURE_SESSION_KEY);
612 es->s.ntlmssp_state->neg_flags |= (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL);
614 if (!NT_STATUS_IS_OK(status = ntlmssp_set_username(es->s.ntlmssp_state, user))) {
615 goto fail;
617 if (!NT_STATUS_IS_OK(status = ntlmssp_set_domain(es->s.ntlmssp_state, domain))) {
618 goto fail;
620 if (!NT_STATUS_IS_OK(status = ntlmssp_set_password(es->s.ntlmssp_state, pass))) {
621 goto fail;
624 do {
625 status = ntlmssp_update(es->s.ntlmssp_state, blob_in, &blob_out);
626 data_blob_free(&blob_in);
627 data_blob_free(&param_out);
628 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) || NT_STATUS_IS_OK(status)) {
629 NTSTATUS trans_status = enc_blob_send_receive(cli,
630 &blob_out,
631 &blob_in,
632 &param_out);
633 if (!NT_STATUS_EQUAL(trans_status,
634 NT_STATUS_MORE_PROCESSING_REQUIRED) &&
635 !NT_STATUS_IS_OK(trans_status)) {
636 status = trans_status;
637 } else {
638 if (param_out.length == 2) {
639 es->enc_ctx_num = SVAL(param_out.data, 0);
643 data_blob_free(&blob_out);
644 } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
646 data_blob_free(&blob_in);
648 if (NT_STATUS_IS_OK(status)) {
649 /* Replace the old state, if any. */
650 if (cli->trans_enc_state) {
651 common_free_encryption_state(&cli->trans_enc_state);
653 cli->trans_enc_state = es;
654 cli->trans_enc_state->enc_on = True;
655 es = NULL;
658 fail:
660 common_free_encryption_state(&es);
661 return status;
664 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
666 #ifndef SMB_GSS_REQUIRED_FLAGS
667 #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)
668 #endif
670 /******************************************************************************
671 Get client gss blob to send to a server.
672 ******************************************************************************/
674 static NTSTATUS make_cli_gss_blob(TALLOC_CTX *ctx,
675 struct smb_trans_enc_state *es,
676 const char *service,
677 const char *host,
678 NTSTATUS status_in,
679 DATA_BLOB spnego_blob_in,
680 DATA_BLOB *p_blob_out)
682 const char *krb_mechs[] = {OID_KERBEROS5, NULL};
683 OM_uint32 ret;
684 OM_uint32 min;
685 gss_name_t srv_name;
686 gss_buffer_desc input_name;
687 gss_buffer_desc *p_tok_in;
688 gss_buffer_desc tok_out, tok_in;
689 DATA_BLOB blob_out = data_blob_null;
690 DATA_BLOB blob_in = data_blob_null;
691 char *host_princ_s = NULL;
692 OM_uint32 ret_flags = 0;
693 NTSTATUS status = NT_STATUS_OK;
695 gss_OID_desc nt_hostbased_service =
696 {10, CONST_DISCARD(char *,"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
698 memset(&tok_out, '\0', sizeof(tok_out));
700 /* Get a ticket for the service@host */
701 if (asprintf(&host_princ_s, "%s@%s", service, host) == -1) {
702 return NT_STATUS_NO_MEMORY;
705 input_name.value = host_princ_s;
706 input_name.length = strlen(host_princ_s) + 1;
708 ret = gss_import_name(&min,
709 &input_name,
710 &nt_hostbased_service,
711 &srv_name);
713 if (ret != GSS_S_COMPLETE) {
714 SAFE_FREE(host_princ_s);
715 return map_nt_error_from_gss(ret, min);
718 if (spnego_blob_in.length == 0) {
719 p_tok_in = GSS_C_NO_BUFFER;
720 } else {
721 /* Remove the SPNEGO wrapper */
722 if (!spnego_parse_auth_response(ctx, spnego_blob_in, status_in, OID_KERBEROS5, &blob_in)) {
723 status = NT_STATUS_UNSUCCESSFUL;
724 goto fail;
726 tok_in.value = blob_in.data;
727 tok_in.length = blob_in.length;
728 p_tok_in = &tok_in;
731 ret = gss_init_sec_context(&min,
732 GSS_C_NO_CREDENTIAL, /* Use our default cred. */
733 &es->s.gss_state->gss_ctx,
734 srv_name,
735 GSS_C_NO_OID, /* default OID. */
736 GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG | GSS_C_DELEG_FLAG,
737 GSS_C_INDEFINITE, /* requested ticket lifetime. */
738 NULL, /* no channel bindings */
739 p_tok_in,
740 NULL, /* ignore mech type */
741 &tok_out,
742 &ret_flags,
743 NULL); /* ignore time_rec */
745 status = map_nt_error_from_gss(ret, min);
746 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
747 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
748 DEBUG(10,("make_cli_gss_blob: gss_init_sec_context failed with %s\n",
749 ads_errstr(adss)));
750 goto fail;
753 if ((ret_flags & SMB_GSS_REQUIRED_FLAGS) != SMB_GSS_REQUIRED_FLAGS) {
754 status = NT_STATUS_ACCESS_DENIED;
757 blob_out = data_blob_talloc(ctx, tok_out.value, tok_out.length);
759 /* Wrap in an SPNEGO wrapper */
760 *p_blob_out = spnego_gen_negTokenInit(ctx, krb_mechs, &blob_out, NULL);
762 fail:
764 data_blob_free(&blob_out);
765 data_blob_free(&blob_in);
766 SAFE_FREE(host_princ_s);
767 gss_release_name(&min, &srv_name);
768 if (tok_out.value) {
769 gss_release_buffer(&min, &tok_out);
771 return status;
774 /******************************************************************************
775 Start a SPNEGO gssapi encryption context.
776 ******************************************************************************/
778 NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
780 DATA_BLOB blob_recv = data_blob_null;
781 DATA_BLOB blob_send = data_blob_null;
782 DATA_BLOB param_out = data_blob_null;
783 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
784 fstring fqdn;
785 const char *servicename;
786 struct smb_trans_enc_state *es = make_cli_enc_state(SMB_TRANS_ENC_GSS);
788 if (!es) {
789 return NT_STATUS_NO_MEMORY;
792 name_to_fqdn(fqdn, cli->desthost);
793 strlower_m(fqdn);
795 servicename = "cifs";
796 status = make_cli_gss_blob(talloc_tos(), es, servicename, fqdn, NT_STATUS_OK, blob_recv, &blob_send);
797 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
798 servicename = "host";
799 status = make_cli_gss_blob(talloc_tos(), es, servicename, fqdn, NT_STATUS_OK, blob_recv, &blob_send);
800 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
801 goto fail;
805 do {
806 data_blob_free(&blob_recv);
807 status = enc_blob_send_receive(cli, &blob_send, &blob_recv, &param_out);
808 if (param_out.length == 2) {
809 es->enc_ctx_num = SVAL(param_out.data, 0);
811 data_blob_free(&blob_send);
812 status = make_cli_gss_blob(talloc_tos(), es, servicename, fqdn, status, blob_recv, &blob_send);
813 } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
814 data_blob_free(&blob_recv);
816 if (NT_STATUS_IS_OK(status)) {
817 /* Replace the old state, if any. */
818 if (cli->trans_enc_state) {
819 common_free_encryption_state(&cli->trans_enc_state);
821 cli->trans_enc_state = es;
822 cli->trans_enc_state->enc_on = True;
823 es = NULL;
826 fail:
828 common_free_encryption_state(&es);
829 return status;
831 #else
832 NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
834 return NT_STATUS_NOT_SUPPORTED;
836 #endif
838 /********************************************************************
839 Ensure a connection is encrypted.
840 ********************************************************************/
842 NTSTATUS cli_force_encryption(struct cli_state *c,
843 const char *username,
844 const char *password,
845 const char *domain)
847 uint16 major, minor;
848 uint32 caplow, caphigh;
849 NTSTATUS status;
851 if (!SERVER_HAS_UNIX_CIFS(c)) {
852 return NT_STATUS_NOT_SUPPORTED;
855 status = cli_unix_extensions_version(c, &major, &minor, &caplow,
856 &caphigh);
857 if (!NT_STATUS_IS_OK(status)) {
858 DEBUG(10, ("cli_force_encryption: cli_unix_extensions_version "
859 "returned %s\n", nt_errstr(status)));
860 return NT_STATUS_UNKNOWN_REVISION;
863 if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
864 return NT_STATUS_UNSUPPORTED_COMPRESSION;
867 if (c->use_kerberos) {
868 return cli_gss_smb_encryption_start(c);
870 return cli_raw_ntlm_smb_encryption_start(c,
871 username,
872 password,
873 domain);