WHATSNEW: Add changes since 3.6.15.
[Samba.git] / source3 / libsmb / clifsinfo.c
blob1d66eb4c6b8ba85802d9f9e872a06e9e63173907
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 "libsmb/libsmb.h"
23 #include "../libcli/auth/spnego.h"
24 #include "../libcli/auth/ntlmssp.h"
25 #include "../lib/util/tevent_ntstatus.h"
26 #include "async_smb.h"
27 #include "smb_crypt.h"
28 #include "trans2.h"
30 /****************************************************************************
31 Get UNIX extensions version info.
32 ****************************************************************************/
34 struct cli_unix_extensions_version_state {
35 struct cli_state *cli;
36 uint16_t setup[1];
37 uint8_t param[2];
38 uint16_t major, minor;
39 uint32_t caplow, caphigh;
42 static void cli_unix_extensions_version_done(struct tevent_req *subreq);
44 struct tevent_req *cli_unix_extensions_version_send(TALLOC_CTX *mem_ctx,
45 struct tevent_context *ev,
46 struct cli_state *cli)
48 struct tevent_req *req, *subreq;
49 struct cli_unix_extensions_version_state *state;
51 req = tevent_req_create(mem_ctx, &state,
52 struct cli_unix_extensions_version_state);
53 if (req == NULL) {
54 return NULL;
56 state->cli = cli;
57 SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
58 SSVAL(state->param, 0, SMB_QUERY_CIFS_UNIX_INFO);
60 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
61 NULL, 0, 0, 0,
62 state->setup, 1, 0,
63 state->param, 2, 0,
64 NULL, 0, 560);
65 if (tevent_req_nomem(subreq, req)) {
66 return tevent_req_post(req, ev);
68 tevent_req_set_callback(subreq, cli_unix_extensions_version_done, req);
69 return req;
72 static void cli_unix_extensions_version_done(struct tevent_req *subreq)
74 struct tevent_req *req = tevent_req_callback_data(
75 subreq, struct tevent_req);
76 struct cli_unix_extensions_version_state *state = tevent_req_data(
77 req, struct cli_unix_extensions_version_state);
78 uint8_t *data;
79 uint32_t num_data;
80 NTSTATUS status;
82 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
83 NULL, 0, NULL, &data, 12, &num_data);
84 TALLOC_FREE(subreq);
85 if (!NT_STATUS_IS_OK(status)) {
86 tevent_req_nterror(req, status);
87 return;
90 state->major = SVAL(data, 0);
91 state->minor = SVAL(data, 2);
92 state->caplow = IVAL(data, 4);
93 state->caphigh = IVAL(data, 8);
94 TALLOC_FREE(data);
95 tevent_req_done(req);
98 NTSTATUS cli_unix_extensions_version_recv(struct tevent_req *req,
99 uint16_t *pmajor, uint16_t *pminor,
100 uint32_t *pcaplow,
101 uint32_t *pcaphigh)
103 struct cli_unix_extensions_version_state *state = tevent_req_data(
104 req, struct cli_unix_extensions_version_state);
105 NTSTATUS status;
107 if (tevent_req_is_nterror(req, &status)) {
108 return status;
110 *pmajor = state->major;
111 *pminor = state->minor;
112 *pcaplow = state->caplow;
113 *pcaphigh = state->caphigh;
114 state->cli->server_posix_capabilities = *pcaplow;
115 return NT_STATUS_OK;
118 NTSTATUS cli_unix_extensions_version(struct cli_state *cli, uint16 *pmajor,
119 uint16 *pminor, uint32 *pcaplow,
120 uint32 *pcaphigh)
122 TALLOC_CTX *frame = talloc_stackframe();
123 struct event_context *ev;
124 struct tevent_req *req;
125 NTSTATUS status = NT_STATUS_OK;
127 if (cli_has_async_calls(cli)) {
129 * Can't use sync call while an async call is in flight
131 status = NT_STATUS_INVALID_PARAMETER;
132 goto fail;
135 ev = event_context_init(frame);
136 if (ev == NULL) {
137 status = NT_STATUS_NO_MEMORY;
138 goto fail;
141 req = cli_unix_extensions_version_send(frame, ev, cli);
142 if (req == NULL) {
143 status = NT_STATUS_NO_MEMORY;
144 goto fail;
147 if (!tevent_req_poll(req, ev)) {
148 status = map_nt_error_from_unix(errno);
149 goto fail;
152 status = cli_unix_extensions_version_recv(req, pmajor, pminor, pcaplow,
153 pcaphigh);
154 fail:
155 TALLOC_FREE(frame);
156 return status;
159 /****************************************************************************
160 Set UNIX extensions capabilities.
161 ****************************************************************************/
163 struct cli_set_unix_extensions_capabilities_state {
164 struct cli_state *cli;
165 uint16_t setup[1];
166 uint8_t param[4];
167 uint8_t data[12];
170 static void cli_set_unix_extensions_capabilities_done(
171 struct tevent_req *subreq);
173 struct tevent_req *cli_set_unix_extensions_capabilities_send(
174 TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
175 uint16_t major, uint16_t minor, uint32_t caplow, uint32_t caphigh)
177 struct tevent_req *req, *subreq;
178 struct cli_set_unix_extensions_capabilities_state *state;
180 req = tevent_req_create(
181 mem_ctx, &state,
182 struct cli_set_unix_extensions_capabilities_state);
183 if (req == NULL) {
184 return NULL;
187 state->cli = cli;
188 SSVAL(state->setup+0, 0, TRANSACT2_SETFSINFO);
190 SSVAL(state->param, 0, 0);
191 SSVAL(state->param, 2, SMB_SET_CIFS_UNIX_INFO);
193 SSVAL(state->data, 0, major);
194 SSVAL(state->data, 2, minor);
195 SIVAL(state->data, 4, caplow);
196 SIVAL(state->data, 8, caphigh);
198 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
199 NULL, 0, 0, 0,
200 state->setup, 1, 0,
201 state->param, 4, 0,
202 state->data, 12, 560);
203 if (tevent_req_nomem(subreq, req)) {
204 return tevent_req_post(req, ev);
206 tevent_req_set_callback(
207 subreq, cli_set_unix_extensions_capabilities_done, req);
208 return req;
211 static void cli_set_unix_extensions_capabilities_done(
212 struct tevent_req *subreq)
214 struct tevent_req *req = tevent_req_callback_data(
215 subreq, struct tevent_req);
216 struct cli_set_unix_extensions_capabilities_state *state = tevent_req_data(
217 req, struct cli_set_unix_extensions_capabilities_state);
219 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
220 NULL, 0, NULL, NULL, 0, NULL);
221 if (NT_STATUS_IS_OK(status)) {
222 state->cli->requested_posix_capabilities = IVAL(state->data, 4);
224 tevent_req_simple_finish_ntstatus(subreq, status);
227 NTSTATUS cli_set_unix_extensions_capabilities_recv(struct tevent_req *req)
229 return tevent_req_simple_recv_ntstatus(req);
232 NTSTATUS cli_set_unix_extensions_capabilities(struct cli_state *cli,
233 uint16 major, uint16 minor,
234 uint32 caplow, uint32 caphigh)
236 struct tevent_context *ev;
237 struct tevent_req *req;
238 NTSTATUS status = NT_STATUS_NO_MEMORY;
240 if (cli_has_async_calls(cli)) {
241 return NT_STATUS_INVALID_PARAMETER;
243 ev = tevent_context_init(talloc_tos());
244 if (ev == NULL) {
245 goto fail;
247 req = cli_set_unix_extensions_capabilities_send(
248 ev, ev, cli, major, minor, caplow, caphigh);
249 if (req == NULL) {
250 goto fail;
252 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
253 goto fail;
255 status = cli_set_unix_extensions_capabilities_recv(req);
256 fail:
257 TALLOC_FREE(ev);
258 return status;
261 struct cli_get_fs_attr_info_state {
262 uint16_t setup[1];
263 uint8_t param[2];
264 uint32_t fs_attr;
267 static void cli_get_fs_attr_info_done(struct tevent_req *subreq);
269 struct tevent_req *cli_get_fs_attr_info_send(TALLOC_CTX *mem_ctx,
270 struct tevent_context *ev,
271 struct cli_state *cli)
273 struct tevent_req *subreq, *req;
274 struct cli_get_fs_attr_info_state *state;
276 req = tevent_req_create(mem_ctx, &state,
277 struct cli_get_fs_attr_info_state);
278 if (req == NULL) {
279 return NULL;
281 SSVAL(state->setup+0, 0, TRANSACT2_QFSINFO);
282 SSVAL(state->param+0, 0, SMB_QUERY_FS_ATTRIBUTE_INFO);
284 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
285 NULL, 0, 0, 0,
286 state->setup, 1, 0,
287 state->param, 2, 0,
288 NULL, 0, 560);
289 if (tevent_req_nomem(subreq, req)) {
290 return tevent_req_post(req, ev);
292 tevent_req_set_callback(subreq, cli_get_fs_attr_info_done, req);
293 return req;
296 static void cli_get_fs_attr_info_done(struct tevent_req *subreq)
298 struct tevent_req *req = tevent_req_callback_data(
299 subreq, struct tevent_req);
300 struct cli_get_fs_attr_info_state *state = tevent_req_data(
301 req, struct cli_get_fs_attr_info_state);
302 uint8_t *data;
303 uint32_t num_data;
304 NTSTATUS status;
306 status = cli_trans_recv(subreq, talloc_tos(), NULL, NULL, 0, NULL,
307 NULL, 0, NULL, &data, 12, &num_data);
308 TALLOC_FREE(subreq);
309 if (!NT_STATUS_IS_OK(status)) {
310 tevent_req_nterror(req, status);
311 return;
313 state->fs_attr = IVAL(data, 0);
314 TALLOC_FREE(data);
315 tevent_req_done(req);
318 NTSTATUS cli_get_fs_attr_info_recv(struct tevent_req *req, uint32_t *fs_attr)
320 struct cli_get_fs_attr_info_state *state = tevent_req_data(
321 req, struct cli_get_fs_attr_info_state);
322 NTSTATUS status;
324 if (tevent_req_is_nterror(req, &status)) {
325 return status;
327 *fs_attr = state->fs_attr;
328 return NT_STATUS_OK;
331 NTSTATUS cli_get_fs_attr_info(struct cli_state *cli, uint32_t *fs_attr)
333 struct tevent_context *ev;
334 struct tevent_req *req;
335 NTSTATUS status = NT_STATUS_NO_MEMORY;
337 if (cli_has_async_calls(cli)) {
338 return NT_STATUS_INVALID_PARAMETER;
340 ev = tevent_context_init(talloc_tos());
341 if (ev == NULL) {
342 goto fail;
344 req = cli_get_fs_attr_info_send(ev, ev, cli);
345 if (req == NULL) {
346 goto fail;
348 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
349 goto fail;
351 status = cli_get_fs_attr_info_recv(req, fs_attr);
352 fail:
353 TALLOC_FREE(ev);
354 return status;
357 NTSTATUS cli_get_fs_volume_info(struct cli_state *cli, fstring volume_name,
358 uint32 *pserial_number, time_t *pdate)
360 NTSTATUS status;
361 uint16 setup[1];
362 uint8_t param[2];
363 uint8_t *rdata;
364 uint32_t rdata_count;
365 unsigned int nlen;
367 SSVAL(setup, 0, TRANSACT2_QFSINFO);
368 SSVAL(param,0,SMB_QUERY_FS_VOLUME_INFO);
370 status = cli_trans(talloc_tos(), cli, SMBtrans2,
371 NULL, 0, 0, 0,
372 setup, 1, 0,
373 param, 2, 0,
374 NULL, 0, 560,
375 NULL,
376 NULL, 0, NULL,
377 NULL, 0, NULL,
378 &rdata, 10, &rdata_count);
379 if (!NT_STATUS_IS_OK(status)) {
380 return status;
383 if (pdate) {
384 struct timespec ts;
385 ts = interpret_long_date((char *)rdata);
386 *pdate = ts.tv_sec;
388 if (pserial_number) {
389 *pserial_number = IVAL(rdata,8);
391 nlen = IVAL(rdata,12);
392 clistr_pull(cli->inbuf, volume_name, rdata + 18, sizeof(fstring),
393 nlen, STR_UNICODE);
395 /* todo: but not yet needed
396 * return the other stuff
399 TALLOC_FREE(rdata);
400 return NT_STATUS_OK;
403 NTSTATUS cli_get_fs_full_size_info(struct cli_state *cli,
404 uint64_t *total_allocation_units,
405 uint64_t *caller_allocation_units,
406 uint64_t *actual_allocation_units,
407 uint64_t *sectors_per_allocation_unit,
408 uint64_t *bytes_per_sector)
410 uint16 setup[1];
411 uint8_t param[2];
412 uint8_t *rdata = NULL;
413 uint32_t rdata_count;
414 NTSTATUS status;
416 SSVAL(setup, 0, TRANSACT2_QFSINFO);
417 SSVAL(param, 0, SMB_FS_FULL_SIZE_INFORMATION);
419 status = cli_trans(talloc_tos(), cli, SMBtrans2,
420 NULL, 0, 0, 0,
421 setup, 1, 0, /* setup */
422 param, 2, 0, /* param */
423 NULL, 0, 560, /* data */
424 NULL,
425 NULL, 0, NULL, /* rsetup */
426 NULL, 0, NULL, /* rparam */
427 &rdata, 32, &rdata_count); /* rdata */
428 if (!NT_STATUS_IS_OK(status)) {
429 goto fail;
432 if (total_allocation_units) {
433 *total_allocation_units = BIG_UINT(rdata, 0);
435 if (caller_allocation_units) {
436 *caller_allocation_units = BIG_UINT(rdata,8);
438 if (actual_allocation_units) {
439 *actual_allocation_units = BIG_UINT(rdata,16);
441 if (sectors_per_allocation_unit) {
442 *sectors_per_allocation_unit = IVAL(rdata,24);
444 if (bytes_per_sector) {
445 *bytes_per_sector = IVAL(rdata,28);
448 fail:
449 TALLOC_FREE(rdata);
450 return status;
453 NTSTATUS cli_get_posix_fs_info(struct cli_state *cli,
454 uint32 *optimal_transfer_size,
455 uint32 *block_size,
456 uint64_t *total_blocks,
457 uint64_t *blocks_available,
458 uint64_t *user_blocks_available,
459 uint64_t *total_file_nodes,
460 uint64_t *free_file_nodes,
461 uint64_t *fs_identifier)
463 uint16 setup[1];
464 uint8_t param[2];
465 uint8_t *rdata = NULL;
466 uint32_t rdata_count;
467 NTSTATUS status;
469 SSVAL(setup, 0, TRANSACT2_QFSINFO);
470 SSVAL(param,0,SMB_QUERY_POSIX_FS_INFO);
472 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, 0, 0, 0,
473 setup, 1, 0,
474 param, 2, 0,
475 NULL, 0, 560,
476 NULL,
477 NULL, 0, NULL, /* rsetup */
478 NULL, 0, NULL, /* rparam */
479 &rdata, 56, &rdata_count);
480 if (!NT_STATUS_IS_OK(status)) {
481 return status;
484 if (optimal_transfer_size) {
485 *optimal_transfer_size = IVAL(rdata, 0);
487 if (block_size) {
488 *block_size = IVAL(rdata,4);
490 if (total_blocks) {
491 *total_blocks = BIG_UINT(rdata,8);
493 if (blocks_available) {
494 *blocks_available = BIG_UINT(rdata,16);
496 if (user_blocks_available) {
497 *user_blocks_available = BIG_UINT(rdata,24);
499 if (total_file_nodes) {
500 *total_file_nodes = BIG_UINT(rdata,32);
502 if (free_file_nodes) {
503 *free_file_nodes = BIG_UINT(rdata,40);
505 if (fs_identifier) {
506 *fs_identifier = BIG_UINT(rdata,48);
508 return NT_STATUS_OK;
512 /******************************************************************************
513 Send/receive the request encryption blob.
514 ******************************************************************************/
516 static NTSTATUS enc_blob_send_receive(struct cli_state *cli, DATA_BLOB *in, DATA_BLOB *out, DATA_BLOB *param_out)
518 uint16_t setup[1];
519 uint8_t param[4];
520 uint8_t *rparam=NULL, *rdata=NULL;
521 uint32_t num_rparam, num_rdata;
522 NTSTATUS status;
524 SSVAL(setup+0, 0, TRANSACT2_SETFSINFO);
525 SSVAL(param,0,0);
526 SSVAL(param,2,SMB_REQUEST_TRANSPORT_ENCRYPTION);
528 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, 0, 0, 0,
529 setup, 1, 0,
530 param, 4, 2,
531 (uint8_t *)in->data, in->length, CLI_BUFFER_SIZE,
532 NULL, /* recv_flags */
533 NULL, 0, NULL, /* rsetup */
534 &rparam, 0, &num_rparam,
535 &rdata, 0, &num_rdata);
537 if (!NT_STATUS_IS_OK(status) &&
538 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
539 return status;
542 *out = data_blob(rdata, num_rdata);
543 *param_out = data_blob(rparam, num_rparam);
545 TALLOC_FREE(rparam);
546 TALLOC_FREE(rdata);
547 return status;
550 /******************************************************************************
551 Make a client state struct.
552 ******************************************************************************/
554 static struct smb_trans_enc_state *make_cli_enc_state(enum smb_trans_enc_type smb_enc_type)
556 struct smb_trans_enc_state *es = NULL;
557 es = SMB_MALLOC_P(struct smb_trans_enc_state);
558 if (!es) {
559 return NULL;
561 ZERO_STRUCTP(es);
562 es->smb_enc_type = smb_enc_type;
564 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
565 if (smb_enc_type == SMB_TRANS_ENC_GSS) {
566 es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
567 if (!es->s.gss_state) {
568 SAFE_FREE(es);
569 return NULL;
571 ZERO_STRUCTP(es->s.gss_state);
573 #endif
574 return es;
577 /******************************************************************************
578 Start a raw ntlmssp encryption.
579 ******************************************************************************/
581 NTSTATUS cli_raw_ntlm_smb_encryption_start(struct cli_state *cli,
582 const char *user,
583 const char *pass,
584 const char *domain)
586 DATA_BLOB blob_in = data_blob_null;
587 DATA_BLOB blob_out = data_blob_null;
588 DATA_BLOB param_out = data_blob_null;
589 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
590 struct smb_trans_enc_state *es = make_cli_enc_state(SMB_TRANS_ENC_NTLM);
592 if (!es) {
593 return NT_STATUS_NO_MEMORY;
595 status = ntlmssp_client_start(NULL,
596 global_myname(),
597 lp_workgroup(),
598 lp_client_ntlmv2_auth(),
599 &es->s.ntlmssp_state);
600 if (!NT_STATUS_IS_OK(status)) {
601 goto fail;
604 ntlmssp_want_feature(es->s.ntlmssp_state, NTLMSSP_FEATURE_SESSION_KEY);
605 es->s.ntlmssp_state->neg_flags |= (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL);
607 if (!NT_STATUS_IS_OK(status = ntlmssp_set_username(es->s.ntlmssp_state, user))) {
608 goto fail;
610 if (!NT_STATUS_IS_OK(status = ntlmssp_set_domain(es->s.ntlmssp_state, domain))) {
611 goto fail;
613 if (!NT_STATUS_IS_OK(status = ntlmssp_set_password(es->s.ntlmssp_state, pass))) {
614 goto fail;
617 do {
618 status = ntlmssp_update(es->s.ntlmssp_state, blob_in, &blob_out);
619 data_blob_free(&blob_in);
620 data_blob_free(&param_out);
621 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) || NT_STATUS_IS_OK(status)) {
622 NTSTATUS trans_status = enc_blob_send_receive(cli,
623 &blob_out,
624 &blob_in,
625 &param_out);
626 if (!NT_STATUS_EQUAL(trans_status,
627 NT_STATUS_MORE_PROCESSING_REQUIRED) &&
628 !NT_STATUS_IS_OK(trans_status)) {
629 status = trans_status;
630 } else {
631 if (param_out.length == 2) {
632 es->enc_ctx_num = SVAL(param_out.data, 0);
636 data_blob_free(&blob_out);
637 } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
639 data_blob_free(&blob_in);
641 if (NT_STATUS_IS_OK(status)) {
642 /* Replace the old state, if any. */
643 if (cli->trans_enc_state) {
644 common_free_encryption_state(&cli->trans_enc_state);
646 cli->trans_enc_state = es;
647 cli->trans_enc_state->enc_on = True;
648 es = NULL;
651 fail:
653 common_free_encryption_state(&es);
654 return status;
657 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
659 #ifndef SMB_GSS_REQUIRED_FLAGS
660 #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)
661 #endif
663 /******************************************************************************
664 Get client gss blob to send to a server.
665 ******************************************************************************/
667 static NTSTATUS make_cli_gss_blob(TALLOC_CTX *ctx,
668 struct smb_trans_enc_state *es,
669 const char *service,
670 const char *host,
671 NTSTATUS status_in,
672 DATA_BLOB spnego_blob_in,
673 DATA_BLOB *p_blob_out)
675 const char *krb_mechs[] = {OID_KERBEROS5, NULL};
676 OM_uint32 ret;
677 OM_uint32 min;
678 gss_name_t srv_name;
679 gss_buffer_desc input_name;
680 gss_buffer_desc *p_tok_in;
681 gss_buffer_desc tok_out, tok_in;
682 DATA_BLOB blob_out = data_blob_null;
683 DATA_BLOB blob_in = data_blob_null;
684 char *host_princ_s = NULL;
685 OM_uint32 ret_flags = 0;
686 NTSTATUS status = NT_STATUS_OK;
688 gss_OID_desc nt_hostbased_service =
689 {10, CONST_DISCARD(char *,"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
691 memset(&tok_out, '\0', sizeof(tok_out));
693 /* Get a ticket for the service@host */
694 if (asprintf(&host_princ_s, "%s@%s", service, host) == -1) {
695 return NT_STATUS_NO_MEMORY;
698 input_name.value = host_princ_s;
699 input_name.length = strlen(host_princ_s) + 1;
701 ret = gss_import_name(&min,
702 &input_name,
703 &nt_hostbased_service,
704 &srv_name);
706 if (ret != GSS_S_COMPLETE) {
707 SAFE_FREE(host_princ_s);
708 return map_nt_error_from_gss(ret, min);
711 if (spnego_blob_in.length == 0) {
712 p_tok_in = GSS_C_NO_BUFFER;
713 } else {
714 /* Remove the SPNEGO wrapper */
715 if (!spnego_parse_auth_response(ctx, spnego_blob_in, status_in, OID_KERBEROS5, &blob_in)) {
716 status = NT_STATUS_UNSUCCESSFUL;
717 goto fail;
719 tok_in.value = blob_in.data;
720 tok_in.length = blob_in.length;
721 p_tok_in = &tok_in;
724 ret = gss_init_sec_context(&min,
725 GSS_C_NO_CREDENTIAL, /* Use our default cred. */
726 &es->s.gss_state->gss_ctx,
727 srv_name,
728 GSS_C_NO_OID, /* default OID. */
729 GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG | GSS_C_DELEG_FLAG,
730 GSS_C_INDEFINITE, /* requested ticket lifetime. */
731 NULL, /* no channel bindings */
732 p_tok_in,
733 NULL, /* ignore mech type */
734 &tok_out,
735 &ret_flags,
736 NULL); /* ignore time_rec */
738 status = map_nt_error_from_gss(ret, min);
739 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
740 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
741 DEBUG(10,("make_cli_gss_blob: gss_init_sec_context failed with %s\n",
742 ads_errstr(adss)));
743 goto fail;
746 if ((ret_flags & SMB_GSS_REQUIRED_FLAGS) != SMB_GSS_REQUIRED_FLAGS) {
747 status = NT_STATUS_ACCESS_DENIED;
750 blob_out = data_blob_talloc(ctx, tok_out.value, tok_out.length);
752 /* Wrap in an SPNEGO wrapper */
753 *p_blob_out = spnego_gen_negTokenInit(ctx, krb_mechs, &blob_out, NULL);
755 fail:
757 data_blob_free(&blob_out);
758 data_blob_free(&blob_in);
759 SAFE_FREE(host_princ_s);
760 gss_release_name(&min, &srv_name);
761 if (tok_out.value) {
762 gss_release_buffer(&min, &tok_out);
764 return status;
767 /******************************************************************************
768 Start a SPNEGO gssapi encryption context.
769 ******************************************************************************/
771 NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
773 DATA_BLOB blob_recv = data_blob_null;
774 DATA_BLOB blob_send = data_blob_null;
775 DATA_BLOB param_out = data_blob_null;
776 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
777 fstring fqdn;
778 const char *servicename;
779 struct smb_trans_enc_state *es = make_cli_enc_state(SMB_TRANS_ENC_GSS);
781 if (!es) {
782 return NT_STATUS_NO_MEMORY;
785 name_to_fqdn(fqdn, cli->desthost);
786 strlower_m(fqdn);
788 servicename = "cifs";
789 status = make_cli_gss_blob(talloc_tos(), es, servicename, fqdn, NT_STATUS_OK, blob_recv, &blob_send);
790 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
791 servicename = "host";
792 status = make_cli_gss_blob(talloc_tos(), es, servicename, fqdn, NT_STATUS_OK, blob_recv, &blob_send);
793 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
794 goto fail;
798 do {
799 data_blob_free(&blob_recv);
800 status = enc_blob_send_receive(cli, &blob_send, &blob_recv, &param_out);
801 if (param_out.length == 2) {
802 es->enc_ctx_num = SVAL(param_out.data, 0);
804 data_blob_free(&blob_send);
805 status = make_cli_gss_blob(talloc_tos(), es, servicename, fqdn, status, blob_recv, &blob_send);
806 } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
807 data_blob_free(&blob_recv);
809 if (NT_STATUS_IS_OK(status)) {
810 /* Replace the old state, if any. */
811 if (cli->trans_enc_state) {
812 common_free_encryption_state(&cli->trans_enc_state);
814 cli->trans_enc_state = es;
815 cli->trans_enc_state->enc_on = True;
816 es = NULL;
819 fail:
821 common_free_encryption_state(&es);
822 return status;
824 #else
825 NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
827 return NT_STATUS_NOT_SUPPORTED;
829 #endif
831 /********************************************************************
832 Ensure a connection is encrypted.
833 ********************************************************************/
835 NTSTATUS cli_force_encryption(struct cli_state *c,
836 const char *username,
837 const char *password,
838 const char *domain)
840 uint16 major, minor;
841 uint32 caplow, caphigh;
842 NTSTATUS status;
844 if (!SERVER_HAS_UNIX_CIFS(c)) {
845 return NT_STATUS_NOT_SUPPORTED;
848 status = cli_unix_extensions_version(c, &major, &minor, &caplow,
849 &caphigh);
850 if (!NT_STATUS_IS_OK(status)) {
851 DEBUG(10, ("cli_force_encryption: cli_unix_extensions_version "
852 "returned %s\n", nt_errstr(status)));
853 return NT_STATUS_UNKNOWN_REVISION;
856 if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
857 return NT_STATUS_UNSUPPORTED_COMPRESSION;
860 if (c->use_kerberos) {
861 return cli_gss_smb_encryption_start(c);
863 return cli_raw_ntlm_smb_encryption_start(c,
864 username,
865 password,
866 domain);