s4:librpc/rpc: make PyErr_SetDCERPCStatus() static
[Samba/gebeck_regimport.git] / source3 / libsmb / clifsinfo.c
blob3297ec76ca2342df8b410e02c5588f7ce698df6f
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 "ntlmssp.h"
25 /****************************************************************************
26 Get UNIX extensions version info.
27 ****************************************************************************/
29 struct cli_unix_extensions_version_state {
30 uint16_t setup[1];
31 uint8_t param[2];
32 uint16_t major, minor;
33 uint32_t caplow, caphigh;
36 static void cli_unix_extensions_version_done(struct tevent_req *subreq);
38 struct tevent_req *cli_unix_extensions_version_send(TALLOC_CTX *mem_ctx,
39 struct tevent_context *ev,
40 struct cli_state *cli)
42 struct tevent_req *req, *subreq;
43 struct cli_unix_extensions_version_state *state;
45 req = tevent_req_create(mem_ctx, &state,
46 struct cli_unix_extensions_version_state);
47 if (req == NULL) {
48 return NULL;
50 SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
51 SSVAL(state->param, 0, SMB_QUERY_CIFS_UNIX_INFO);
53 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
54 NULL, 0, 0, 0,
55 state->setup, 1, 0,
56 state->param, 2, 0,
57 NULL, 0, 560);
58 if (tevent_req_nomem(subreq, req)) {
59 return tevent_req_post(req, ev);
61 tevent_req_set_callback(subreq, cli_unix_extensions_version_done, req);
62 return req;
65 static void cli_unix_extensions_version_done(struct tevent_req *subreq)
67 struct tevent_req *req = tevent_req_callback_data(
68 subreq, struct tevent_req);
69 struct cli_unix_extensions_version_state *state = tevent_req_data(
70 req, struct cli_unix_extensions_version_state);
71 uint8_t *data;
72 uint32_t num_data;
73 NTSTATUS status;
75 status = cli_trans_recv(subreq, state, NULL, 0, NULL, NULL, 0, NULL,
76 &data, 12, &num_data);
77 TALLOC_FREE(subreq);
78 if (!NT_STATUS_IS_OK(status)) {
79 tevent_req_nterror(req, status);
80 return;
83 state->major = SVAL(data, 0);
84 state->minor = SVAL(data, 2);
85 state->caplow = IVAL(data, 4);
86 state->caphigh = IVAL(data, 8);
87 TALLOC_FREE(data);
88 tevent_req_done(req);
91 NTSTATUS cli_unix_extensions_version_recv(struct tevent_req *req,
92 uint16_t *pmajor, uint16_t *pminor,
93 uint32_t *pcaplow,
94 uint32_t *pcaphigh)
96 struct cli_unix_extensions_version_state *state = tevent_req_data(
97 req, struct cli_unix_extensions_version_state);
98 NTSTATUS status;
100 if (tevent_req_is_nterror(req, &status)) {
101 return status;
103 *pmajor = state->major;
104 *pminor = state->minor;
105 *pcaplow = state->caplow;
106 *pcaphigh = state->caphigh;
107 return NT_STATUS_OK;
110 NTSTATUS cli_unix_extensions_version(struct cli_state *cli, uint16 *pmajor,
111 uint16 *pminor, uint32 *pcaplow,
112 uint32 *pcaphigh)
114 TALLOC_CTX *frame = talloc_stackframe();
115 struct event_context *ev;
116 struct tevent_req *req;
117 NTSTATUS status = NT_STATUS_OK;
119 if (cli_has_async_calls(cli)) {
121 * Can't use sync call while an async call is in flight
123 status = NT_STATUS_INVALID_PARAMETER;
124 goto fail;
127 ev = event_context_init(frame);
128 if (ev == NULL) {
129 status = NT_STATUS_NO_MEMORY;
130 goto fail;
133 req = cli_unix_extensions_version_send(frame, ev, cli);
134 if (req == NULL) {
135 status = NT_STATUS_NO_MEMORY;
136 goto fail;
139 if (!tevent_req_poll(req, ev)) {
140 status = map_nt_error_from_unix(errno);
141 goto fail;
144 status = cli_unix_extensions_version_recv(req, pmajor, pminor, pcaplow,
145 pcaphigh);
146 if (NT_STATUS_IS_OK(status)) {
147 cli->posix_capabilities = *pcaplow;
149 fail:
150 TALLOC_FREE(frame);
151 if (!NT_STATUS_IS_OK(status)) {
152 cli_set_error(cli, status);
154 return status;
157 /****************************************************************************
158 Set UNIX extensions capabilities.
159 ****************************************************************************/
161 struct cli_set_unix_extensions_capabilities_state {
162 uint16_t setup[1];
163 uint8_t param[4];
164 uint8_t data[12];
167 static void cli_set_unix_extensions_capabilities_done(
168 struct tevent_req *subreq);
170 struct tevent_req *cli_set_unix_extensions_capabilities_send(
171 TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
172 uint16_t major, uint16_t minor, uint32_t caplow, uint32_t caphigh)
174 struct tevent_req *req, *subreq;
175 struct cli_set_unix_extensions_capabilities_state *state;
177 req = tevent_req_create(
178 mem_ctx, &state,
179 struct cli_set_unix_extensions_capabilities_state);
180 if (req == NULL) {
181 return NULL;
184 SSVAL(state->setup+0, 0, TRANSACT2_SETFSINFO);
186 SSVAL(state->param, 0, 0);
187 SSVAL(state->param, 2, SMB_SET_CIFS_UNIX_INFO);
189 SSVAL(state->data, 0, major);
190 SSVAL(state->data, 2, minor);
191 SIVAL(state->data, 4, caplow);
192 SIVAL(state->data, 8, caphigh);
194 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
195 NULL, 0, 0, 0,
196 state->setup, 1, 0,
197 state->param, 4, 0,
198 state->data, 12, 560);
199 if (tevent_req_nomem(subreq, req)) {
200 return tevent_req_post(req, ev);
202 tevent_req_set_callback(
203 subreq, cli_set_unix_extensions_capabilities_done, req);
204 return req;
207 static void cli_set_unix_extensions_capabilities_done(
208 struct tevent_req *subreq)
210 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, 0, NULL,
211 NULL, 0, NULL, NULL, 0, NULL);
212 tevent_req_simple_finish_ntstatus(subreq, status);
215 NTSTATUS cli_set_unix_extensions_capabilities_recv(struct tevent_req *req)
217 return tevent_req_simple_recv_ntstatus(req);
220 NTSTATUS cli_set_unix_extensions_capabilities(struct cli_state *cli,
221 uint16 major, uint16 minor,
222 uint32 caplow, uint32 caphigh)
224 struct tevent_context *ev;
225 struct tevent_req *req;
226 NTSTATUS status = NT_STATUS_NO_MEMORY;
228 if (cli_has_async_calls(cli)) {
229 return NT_STATUS_INVALID_PARAMETER;
231 ev = tevent_context_init(talloc_tos());
232 if (ev == NULL) {
233 goto fail;
235 req = cli_set_unix_extensions_capabilities_send(
236 ev, ev, cli, major, minor, caplow, caphigh);
237 if (req == NULL) {
238 goto fail;
240 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
241 goto fail;
243 status = cli_set_unix_extensions_capabilities_recv(req);
244 fail:
245 TALLOC_FREE(ev);
246 if (!NT_STATUS_IS_OK(status)) {
247 cli_set_error(cli, status);
249 return status;
252 struct cli_get_fs_attr_info_state {
253 uint16_t setup[1];
254 uint8_t param[2];
255 uint32_t fs_attr;
258 static void cli_get_fs_attr_info_done(struct tevent_req *subreq);
260 struct tevent_req *cli_get_fs_attr_info_send(TALLOC_CTX *mem_ctx,
261 struct tevent_context *ev,
262 struct cli_state *cli)
264 struct tevent_req *subreq, *req;
265 struct cli_get_fs_attr_info_state *state;
267 req = tevent_req_create(mem_ctx, &state,
268 struct cli_get_fs_attr_info_state);
269 if (req == NULL) {
270 return NULL;
272 SSVAL(state->setup+0, 0, TRANSACT2_QFSINFO);
273 SSVAL(state->param+0, 0, SMB_QUERY_FS_ATTRIBUTE_INFO);
275 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
276 NULL, 0, 0, 0,
277 state->setup, 1, 0,
278 state->param, 2, 0,
279 NULL, 0, 560);
280 if (tevent_req_nomem(subreq, req)) {
281 return tevent_req_post(req, ev);
283 tevent_req_set_callback(subreq, cli_get_fs_attr_info_done, req);
284 return req;
287 static void cli_get_fs_attr_info_done(struct tevent_req *subreq)
289 struct tevent_req *req = tevent_req_callback_data(
290 subreq, struct tevent_req);
291 struct cli_get_fs_attr_info_state *state = tevent_req_data(
292 req, struct cli_get_fs_attr_info_state);
293 uint8_t *data;
294 uint32_t num_data;
295 NTSTATUS status;
297 status = cli_trans_recv(subreq, talloc_tos(), NULL, 0, NULL,
298 NULL, 0, NULL, &data, 12, &num_data);
299 TALLOC_FREE(subreq);
300 if (!NT_STATUS_IS_OK(status)) {
301 tevent_req_nterror(req, status);
302 return;
304 state->fs_attr = IVAL(data, 0);
305 TALLOC_FREE(data);
306 tevent_req_done(req);
309 NTSTATUS cli_get_fs_attr_info_recv(struct tevent_req *req, uint32_t *fs_attr)
311 struct cli_get_fs_attr_info_state *state = tevent_req_data(
312 req, struct cli_get_fs_attr_info_state);
313 NTSTATUS status;
315 if (tevent_req_is_nterror(req, &status)) {
316 return status;
318 *fs_attr = state->fs_attr;
319 return NT_STATUS_OK;
322 NTSTATUS cli_get_fs_attr_info(struct cli_state *cli, uint32_t *fs_attr)
324 struct tevent_context *ev;
325 struct tevent_req *req;
326 NTSTATUS status = NT_STATUS_NO_MEMORY;
328 if (cli_has_async_calls(cli)) {
329 return NT_STATUS_INVALID_PARAMETER;
331 ev = tevent_context_init(talloc_tos());
332 if (ev == NULL) {
333 goto fail;
335 req = cli_get_fs_attr_info_send(ev, ev, cli);
336 if (req == NULL) {
337 goto fail;
339 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
340 goto fail;
342 status = cli_get_fs_attr_info_recv(req, fs_attr);
343 fail:
344 TALLOC_FREE(ev);
345 if (!NT_STATUS_IS_OK(status)) {
346 cli_set_error(cli, status);
348 return status;
351 NTSTATUS cli_get_fs_volume_info(struct cli_state *cli, fstring volume_name,
352 uint32 *pserial_number, time_t *pdate)
354 NTSTATUS status;
355 uint16 setup[1];
356 uint8_t param[2];
357 uint8_t *rdata;
358 uint32_t rdata_count;
359 unsigned int nlen;
361 SSVAL(setup, 0, TRANSACT2_QFSINFO);
362 SSVAL(param,0,SMB_QUERY_FS_VOLUME_INFO);
364 status = cli_trans(talloc_tos(), cli, SMBtrans2,
365 NULL, 0, 0, 0,
366 setup, 1, 0,
367 param, 2, 0,
368 NULL, 0, 560,
369 NULL, 0, NULL,
370 NULL, 0, NULL,
371 &rdata, 10, &rdata_count);
372 if (!NT_STATUS_IS_OK(status)) {
373 return status;
376 if (pdate) {
377 struct timespec ts;
378 ts = interpret_long_date((char *)rdata);
379 *pdate = ts.tv_sec;
381 if (pserial_number) {
382 *pserial_number = IVAL(rdata,8);
384 nlen = IVAL(rdata,12);
385 clistr_pull(cli->inbuf, volume_name, rdata + 18, sizeof(fstring),
386 nlen, STR_UNICODE);
388 /* todo: but not yet needed
389 * return the other stuff
392 TALLOC_FREE(rdata);
393 return NT_STATUS_OK;
396 bool cli_get_fs_full_size_info(struct cli_state *cli,
397 uint64_t *total_allocation_units,
398 uint64_t *caller_allocation_units,
399 uint64_t *actual_allocation_units,
400 uint64_t *sectors_per_allocation_unit,
401 uint64_t *bytes_per_sector)
403 bool ret = False;
404 uint16 setup;
405 char param[2];
406 char *rparam=NULL, *rdata=NULL;
407 unsigned int rparam_count=0, rdata_count=0;
409 setup = TRANSACT2_QFSINFO;
411 SSVAL(param,0,SMB_FS_FULL_SIZE_INFORMATION);
413 if (!cli_send_trans(cli, SMBtrans2,
414 NULL,
415 0, 0,
416 &setup, 1, 0,
417 param, 2, 0,
418 NULL, 0, 560)) {
419 goto cleanup;
422 if (!cli_receive_trans(cli, SMBtrans2,
423 &rparam, &rparam_count,
424 &rdata, &rdata_count)) {
425 goto cleanup;
428 if (cli_is_error(cli)) {
429 ret = False;
430 goto cleanup;
431 } else {
432 ret = True;
435 if (rdata_count != 32) {
436 goto cleanup;
439 if (total_allocation_units) {
440 *total_allocation_units = BIG_UINT(rdata, 0);
442 if (caller_allocation_units) {
443 *caller_allocation_units = BIG_UINT(rdata,8);
445 if (actual_allocation_units) {
446 *actual_allocation_units = BIG_UINT(rdata,16);
448 if (sectors_per_allocation_unit) {
449 *sectors_per_allocation_unit = IVAL(rdata,24);
451 if (bytes_per_sector) {
452 *bytes_per_sector = IVAL(rdata,28);
455 cleanup:
456 SAFE_FREE(rparam);
457 SAFE_FREE(rdata);
459 return ret;
462 bool cli_get_posix_fs_info(struct cli_state *cli,
463 uint32 *optimal_transfer_size,
464 uint32 *block_size,
465 uint64_t *total_blocks,
466 uint64_t *blocks_available,
467 uint64_t *user_blocks_available,
468 uint64_t *total_file_nodes,
469 uint64_t *free_file_nodes,
470 uint64_t *fs_identifier)
472 bool ret = False;
473 uint16 setup;
474 char param[2];
475 char *rparam=NULL, *rdata=NULL;
476 unsigned int rparam_count=0, rdata_count=0;
478 setup = TRANSACT2_QFSINFO;
480 SSVAL(param,0,SMB_QUERY_POSIX_FS_INFO);
482 if (!cli_send_trans(cli, SMBtrans2,
483 NULL,
484 0, 0,
485 &setup, 1, 0,
486 param, 2, 0,
487 NULL, 0, 560)) {
488 goto cleanup;
491 if (!cli_receive_trans(cli, SMBtrans2,
492 &rparam, &rparam_count,
493 &rdata, &rdata_count)) {
494 goto cleanup;
497 if (cli_is_error(cli)) {
498 ret = False;
499 goto cleanup;
500 } else {
501 ret = True;
504 if (rdata_count != 56) {
505 goto cleanup;
508 if (optimal_transfer_size) {
509 *optimal_transfer_size = IVAL(rdata, 0);
511 if (block_size) {
512 *block_size = IVAL(rdata,4);
514 if (total_blocks) {
515 *total_blocks = BIG_UINT(rdata,8);
517 if (blocks_available) {
518 *blocks_available = BIG_UINT(rdata,16);
520 if (user_blocks_available) {
521 *user_blocks_available = BIG_UINT(rdata,24);
523 if (total_file_nodes) {
524 *total_file_nodes = BIG_UINT(rdata,32);
526 if (free_file_nodes) {
527 *free_file_nodes = BIG_UINT(rdata,40);
529 if (fs_identifier) {
530 *fs_identifier = BIG_UINT(rdata,48);
533 cleanup:
534 SAFE_FREE(rparam);
535 SAFE_FREE(rdata);
537 return ret;
541 /******************************************************************************
542 Send/receive the request encryption blob.
543 ******************************************************************************/
545 static NTSTATUS enc_blob_send_receive(struct cli_state *cli, DATA_BLOB *in, DATA_BLOB *out, DATA_BLOB *param_out)
547 uint16 setup;
548 char param[4];
549 char *rparam=NULL, *rdata=NULL;
550 unsigned int rparam_count=0, rdata_count=0;
551 NTSTATUS status = NT_STATUS_OK;
553 setup = TRANSACT2_SETFSINFO;
555 SSVAL(param,0,0);
556 SSVAL(param,2,SMB_REQUEST_TRANSPORT_ENCRYPTION);
558 if (!cli_send_trans(cli, SMBtrans2,
559 NULL,
560 0, 0,
561 &setup, 1, 0,
562 param, 4, 0,
563 (char *)in->data, in->length, CLI_BUFFER_SIZE)) {
564 status = cli_nt_error(cli);
565 goto out;
568 if (!cli_receive_trans(cli, SMBtrans2,
569 &rparam, &rparam_count,
570 &rdata, &rdata_count)) {
571 status = cli_nt_error(cli);
572 goto out;
575 if (cli_is_error(cli)) {
576 status = cli_nt_error(cli);
577 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
578 goto out;
582 *out = data_blob(rdata, rdata_count);
583 *param_out = data_blob(rparam, rparam_count);
585 out:
587 SAFE_FREE(rparam);
588 SAFE_FREE(rdata);
589 return status;
592 /******************************************************************************
593 Make a client state struct.
594 ******************************************************************************/
596 static struct smb_trans_enc_state *make_cli_enc_state(enum smb_trans_enc_type smb_enc_type)
598 struct smb_trans_enc_state *es = NULL;
599 es = SMB_MALLOC_P(struct smb_trans_enc_state);
600 if (!es) {
601 return NULL;
603 ZERO_STRUCTP(es);
604 es->smb_enc_type = smb_enc_type;
606 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
607 if (smb_enc_type == SMB_TRANS_ENC_GSS) {
608 es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
609 if (!es->s.gss_state) {
610 SAFE_FREE(es);
611 return NULL;
613 ZERO_STRUCTP(es->s.gss_state);
615 #endif
616 return es;
619 /******************************************************************************
620 Start a raw ntlmssp encryption.
621 ******************************************************************************/
623 NTSTATUS cli_raw_ntlm_smb_encryption_start(struct cli_state *cli,
624 const char *user,
625 const char *pass,
626 const char *domain)
628 DATA_BLOB blob_in = data_blob_null;
629 DATA_BLOB blob_out = data_blob_null;
630 DATA_BLOB param_out = data_blob_null;
631 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
632 struct smb_trans_enc_state *es = make_cli_enc_state(SMB_TRANS_ENC_NTLM);
634 if (!es) {
635 return NT_STATUS_NO_MEMORY;
637 status = ntlmssp_client_start(NULL,
638 global_myname(),
639 lp_workgroup(),
640 lp_client_ntlmv2_auth(),
641 &es->s.ntlmssp_state);
642 if (!NT_STATUS_IS_OK(status)) {
643 goto fail;
646 ntlmssp_want_feature(es->s.ntlmssp_state, NTLMSSP_FEATURE_SESSION_KEY);
647 es->s.ntlmssp_state->neg_flags |= (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL);
649 if (!NT_STATUS_IS_OK(status = ntlmssp_set_username(es->s.ntlmssp_state, user))) {
650 goto fail;
652 if (!NT_STATUS_IS_OK(status = ntlmssp_set_domain(es->s.ntlmssp_state, domain))) {
653 goto fail;
655 if (!NT_STATUS_IS_OK(status = ntlmssp_set_password(es->s.ntlmssp_state, pass))) {
656 goto fail;
659 do {
660 status = ntlmssp_update(es->s.ntlmssp_state, blob_in, &blob_out);
661 data_blob_free(&blob_in);
662 data_blob_free(&param_out);
663 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) || NT_STATUS_IS_OK(status)) {
664 NTSTATUS trans_status = enc_blob_send_receive(cli,
665 &blob_out,
666 &blob_in,
667 &param_out);
668 if (!NT_STATUS_EQUAL(trans_status,
669 NT_STATUS_MORE_PROCESSING_REQUIRED) &&
670 !NT_STATUS_IS_OK(trans_status)) {
671 status = trans_status;
672 } else {
673 if (param_out.length == 2) {
674 es->enc_ctx_num = SVAL(param_out.data, 0);
678 data_blob_free(&blob_out);
679 } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
681 data_blob_free(&blob_in);
683 if (NT_STATUS_IS_OK(status)) {
684 /* Replace the old state, if any. */
685 if (cli->trans_enc_state) {
686 common_free_encryption_state(&cli->trans_enc_state);
688 cli->trans_enc_state = es;
689 cli->trans_enc_state->enc_on = True;
690 es = NULL;
693 fail:
695 common_free_encryption_state(&es);
696 return status;
699 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
701 #ifndef SMB_GSS_REQUIRED_FLAGS
702 #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)
703 #endif
705 /******************************************************************************
706 Get client gss blob to send to a server.
707 ******************************************************************************/
709 static NTSTATUS make_cli_gss_blob(struct smb_trans_enc_state *es,
710 const char *service,
711 const char *host,
712 NTSTATUS status_in,
713 DATA_BLOB spnego_blob_in,
714 DATA_BLOB *p_blob_out)
716 const char *krb_mechs[] = {OID_KERBEROS5, NULL};
717 OM_uint32 ret;
718 OM_uint32 min;
719 gss_name_t srv_name;
720 gss_buffer_desc input_name;
721 gss_buffer_desc *p_tok_in;
722 gss_buffer_desc tok_out, tok_in;
723 DATA_BLOB blob_out = data_blob_null;
724 DATA_BLOB blob_in = data_blob_null;
725 char *host_princ_s = NULL;
726 OM_uint32 ret_flags = 0;
727 NTSTATUS status = NT_STATUS_OK;
729 gss_OID_desc nt_hostbased_service =
730 {10, CONST_DISCARD(char *,"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
732 memset(&tok_out, '\0', sizeof(tok_out));
734 /* Get a ticket for the service@host */
735 if (asprintf(&host_princ_s, "%s@%s", service, host) == -1) {
736 return NT_STATUS_NO_MEMORY;
739 input_name.value = host_princ_s;
740 input_name.length = strlen(host_princ_s) + 1;
742 ret = gss_import_name(&min,
743 &input_name,
744 &nt_hostbased_service,
745 &srv_name);
747 if (ret != GSS_S_COMPLETE) {
748 SAFE_FREE(host_princ_s);
749 return map_nt_error_from_gss(ret, min);
752 if (spnego_blob_in.length == 0) {
753 p_tok_in = GSS_C_NO_BUFFER;
754 } else {
755 /* Remove the SPNEGO wrapper */
756 if (!spnego_parse_auth_response(spnego_blob_in, status_in, OID_KERBEROS5, &blob_in)) {
757 status = NT_STATUS_UNSUCCESSFUL;
758 goto fail;
760 tok_in.value = blob_in.data;
761 tok_in.length = blob_in.length;
762 p_tok_in = &tok_in;
765 ret = gss_init_sec_context(&min,
766 GSS_C_NO_CREDENTIAL, /* Use our default cred. */
767 &es->s.gss_state->gss_ctx,
768 srv_name,
769 GSS_C_NO_OID, /* default OID. */
770 GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG | GSS_C_DELEG_FLAG,
771 GSS_C_INDEFINITE, /* requested ticket lifetime. */
772 NULL, /* no channel bindings */
773 p_tok_in,
774 NULL, /* ignore mech type */
775 &tok_out,
776 &ret_flags,
777 NULL); /* ignore time_rec */
779 status = map_nt_error_from_gss(ret, min);
780 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
781 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
782 DEBUG(10,("make_cli_gss_blob: gss_init_sec_context failed with %s\n",
783 ads_errstr(adss)));
784 goto fail;
787 if ((ret_flags & SMB_GSS_REQUIRED_FLAGS) != SMB_GSS_REQUIRED_FLAGS) {
788 status = NT_STATUS_ACCESS_DENIED;
791 blob_out = data_blob(tok_out.value, tok_out.length);
793 /* Wrap in an SPNEGO wrapper */
794 *p_blob_out = gen_negTokenTarg(krb_mechs, blob_out);
796 fail:
798 data_blob_free(&blob_out);
799 data_blob_free(&blob_in);
800 SAFE_FREE(host_princ_s);
801 gss_release_name(&min, &srv_name);
802 if (tok_out.value) {
803 gss_release_buffer(&min, &tok_out);
805 return status;
808 /******************************************************************************
809 Start a SPNEGO gssapi encryption context.
810 ******************************************************************************/
812 NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
814 DATA_BLOB blob_recv = data_blob_null;
815 DATA_BLOB blob_send = data_blob_null;
816 DATA_BLOB param_out = data_blob_null;
817 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
818 fstring fqdn;
819 const char *servicename;
820 struct smb_trans_enc_state *es = make_cli_enc_state(SMB_TRANS_ENC_GSS);
822 if (!es) {
823 return NT_STATUS_NO_MEMORY;
826 name_to_fqdn(fqdn, cli->desthost);
827 strlower_m(fqdn);
829 servicename = "cifs";
830 status = make_cli_gss_blob(es, servicename, fqdn, NT_STATUS_OK, blob_recv, &blob_send);
831 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
832 servicename = "host";
833 status = make_cli_gss_blob(es, servicename, fqdn, NT_STATUS_OK, blob_recv, &blob_send);
834 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
835 goto fail;
839 do {
840 data_blob_free(&blob_recv);
841 status = enc_blob_send_receive(cli, &blob_send, &blob_recv, &param_out);
842 if (param_out.length == 2) {
843 es->enc_ctx_num = SVAL(param_out.data, 0);
845 data_blob_free(&blob_send);
846 status = make_cli_gss_blob(es, servicename, fqdn, status, blob_recv, &blob_send);
847 } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
848 data_blob_free(&blob_recv);
850 if (NT_STATUS_IS_OK(status)) {
851 /* Replace the old state, if any. */
852 if (cli->trans_enc_state) {
853 common_free_encryption_state(&cli->trans_enc_state);
855 cli->trans_enc_state = es;
856 cli->trans_enc_state->enc_on = True;
857 es = NULL;
860 fail:
862 common_free_encryption_state(&es);
863 return status;
865 #else
866 NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
868 return NT_STATUS_NOT_SUPPORTED;
870 #endif
872 /********************************************************************
873 Ensure a connection is encrypted.
874 ********************************************************************/
876 NTSTATUS cli_force_encryption(struct cli_state *c,
877 const char *username,
878 const char *password,
879 const char *domain)
881 uint16 major, minor;
882 uint32 caplow, caphigh;
883 NTSTATUS status;
885 if (!SERVER_HAS_UNIX_CIFS(c)) {
886 return NT_STATUS_NOT_SUPPORTED;
889 status = cli_unix_extensions_version(c, &major, &minor, &caplow,
890 &caphigh);
891 if (!NT_STATUS_IS_OK(status)) {
892 DEBUG(10, ("cli_force_encryption: cli_unix_extensions_version "
893 "returned %s\n", nt_errstr(status)));
894 return NT_STATUS_UNKNOWN_REVISION;
897 if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
898 return NT_STATUS_UNSUPPORTED_COMPRESSION;
901 if (c->use_kerberos) {
902 return cli_gss_smb_encryption_start(c);
904 return cli_raw_ntlm_smb_encryption_start(c,
905 username,
906 password,
907 domain);