s3: Fix Coverity ID 986, BUFFER_SIZE_WARNING
[Samba.git] / source3 / libsmb / clifsinfo.c
blobebd04e6d2b672effa59767527e43a6616d01427d
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"
25 #include "smb_crypt.h"
26 #include "trans2.h"
28 /****************************************************************************
29 Get UNIX extensions version info.
30 ****************************************************************************/
32 struct cli_unix_extensions_version_state {
33 struct cli_state *cli;
34 uint16_t setup[1];
35 uint8_t param[2];
36 uint16_t major, minor;
37 uint32_t caplow, caphigh;
40 static void cli_unix_extensions_version_done(struct tevent_req *subreq);
42 struct tevent_req *cli_unix_extensions_version_send(TALLOC_CTX *mem_ctx,
43 struct tevent_context *ev,
44 struct cli_state *cli)
46 struct tevent_req *req, *subreq;
47 struct cli_unix_extensions_version_state *state;
49 req = tevent_req_create(mem_ctx, &state,
50 struct cli_unix_extensions_version_state);
51 if (req == NULL) {
52 return NULL;
54 state->cli = cli;
55 SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
56 SSVAL(state->param, 0, SMB_QUERY_CIFS_UNIX_INFO);
58 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
59 NULL, 0, 0, 0,
60 state->setup, 1, 0,
61 state->param, 2, 0,
62 NULL, 0, 560);
63 if (tevent_req_nomem(subreq, req)) {
64 return tevent_req_post(req, ev);
66 tevent_req_set_callback(subreq, cli_unix_extensions_version_done, req);
67 return req;
70 static void cli_unix_extensions_version_done(struct tevent_req *subreq)
72 struct tevent_req *req = tevent_req_callback_data(
73 subreq, struct tevent_req);
74 struct cli_unix_extensions_version_state *state = tevent_req_data(
75 req, struct cli_unix_extensions_version_state);
76 uint8_t *data;
77 uint32_t num_data;
78 NTSTATUS status;
80 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
81 NULL, 0, NULL, &data, 12, &num_data);
82 TALLOC_FREE(subreq);
83 if (!NT_STATUS_IS_OK(status)) {
84 tevent_req_nterror(req, status);
85 return;
88 state->major = SVAL(data, 0);
89 state->minor = SVAL(data, 2);
90 state->caplow = IVAL(data, 4);
91 state->caphigh = IVAL(data, 8);
92 TALLOC_FREE(data);
93 tevent_req_done(req);
96 NTSTATUS cli_unix_extensions_version_recv(struct tevent_req *req,
97 uint16_t *pmajor, uint16_t *pminor,
98 uint32_t *pcaplow,
99 uint32_t *pcaphigh)
101 struct cli_unix_extensions_version_state *state = tevent_req_data(
102 req, struct cli_unix_extensions_version_state);
103 NTSTATUS status;
105 if (tevent_req_is_nterror(req, &status)) {
106 return status;
108 *pmajor = state->major;
109 *pminor = state->minor;
110 *pcaplow = state->caplow;
111 *pcaphigh = state->caphigh;
112 state->cli->server_posix_capabilities = *pcaplow;
113 return NT_STATUS_OK;
116 NTSTATUS cli_unix_extensions_version(struct cli_state *cli, uint16 *pmajor,
117 uint16 *pminor, uint32 *pcaplow,
118 uint32 *pcaphigh)
120 TALLOC_CTX *frame = talloc_stackframe();
121 struct event_context *ev;
122 struct tevent_req *req;
123 NTSTATUS status = NT_STATUS_OK;
125 if (cli_has_async_calls(cli)) {
127 * Can't use sync call while an async call is in flight
129 status = NT_STATUS_INVALID_PARAMETER;
130 goto fail;
133 ev = event_context_init(frame);
134 if (ev == NULL) {
135 status = NT_STATUS_NO_MEMORY;
136 goto fail;
139 req = cli_unix_extensions_version_send(frame, ev, cli);
140 if (req == NULL) {
141 status = NT_STATUS_NO_MEMORY;
142 goto fail;
145 if (!tevent_req_poll(req, ev)) {
146 status = map_nt_error_from_unix(errno);
147 goto fail;
150 status = cli_unix_extensions_version_recv(req, pmajor, pminor, pcaplow,
151 pcaphigh);
152 fail:
153 TALLOC_FREE(frame);
154 if (!NT_STATUS_IS_OK(status)) {
155 cli_set_error(cli, status);
157 return status;
160 /****************************************************************************
161 Set UNIX extensions capabilities.
162 ****************************************************************************/
164 struct cli_set_unix_extensions_capabilities_state {
165 struct cli_state *cli;
166 uint16_t setup[1];
167 uint8_t param[4];
168 uint8_t data[12];
171 static void cli_set_unix_extensions_capabilities_done(
172 struct tevent_req *subreq);
174 struct tevent_req *cli_set_unix_extensions_capabilities_send(
175 TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
176 uint16_t major, uint16_t minor, uint32_t caplow, uint32_t caphigh)
178 struct tevent_req *req, *subreq;
179 struct cli_set_unix_extensions_capabilities_state *state;
181 req = tevent_req_create(
182 mem_ctx, &state,
183 struct cli_set_unix_extensions_capabilities_state);
184 if (req == NULL) {
185 return NULL;
188 state->cli = cli;
189 SSVAL(state->setup+0, 0, TRANSACT2_SETFSINFO);
191 SSVAL(state->param, 0, 0);
192 SSVAL(state->param, 2, SMB_SET_CIFS_UNIX_INFO);
194 SSVAL(state->data, 0, major);
195 SSVAL(state->data, 2, minor);
196 SIVAL(state->data, 4, caplow);
197 SIVAL(state->data, 8, caphigh);
199 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
200 NULL, 0, 0, 0,
201 state->setup, 1, 0,
202 state->param, 4, 0,
203 state->data, 12, 560);
204 if (tevent_req_nomem(subreq, req)) {
205 return tevent_req_post(req, ev);
207 tevent_req_set_callback(
208 subreq, cli_set_unix_extensions_capabilities_done, req);
209 return req;
212 static void cli_set_unix_extensions_capabilities_done(
213 struct tevent_req *subreq)
215 struct tevent_req *req = tevent_req_callback_data(
216 subreq, struct tevent_req);
217 struct cli_set_unix_extensions_capabilities_state *state = tevent_req_data(
218 req, struct cli_set_unix_extensions_capabilities_state);
220 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
221 NULL, 0, NULL, NULL, 0, NULL);
222 if (NT_STATUS_IS_OK(status)) {
223 state->cli->requested_posix_capabilities = IVAL(state->data, 4);
225 tevent_req_simple_finish_ntstatus(subreq, status);
228 NTSTATUS cli_set_unix_extensions_capabilities_recv(struct tevent_req *req)
230 return tevent_req_simple_recv_ntstatus(req);
233 NTSTATUS cli_set_unix_extensions_capabilities(struct cli_state *cli,
234 uint16 major, uint16 minor,
235 uint32 caplow, uint32 caphigh)
237 struct tevent_context *ev;
238 struct tevent_req *req;
239 NTSTATUS status = NT_STATUS_NO_MEMORY;
241 if (cli_has_async_calls(cli)) {
242 return NT_STATUS_INVALID_PARAMETER;
244 ev = tevent_context_init(talloc_tos());
245 if (ev == NULL) {
246 goto fail;
248 req = cli_set_unix_extensions_capabilities_send(
249 ev, ev, cli, major, minor, caplow, caphigh);
250 if (req == NULL) {
251 goto fail;
253 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
254 goto fail;
256 status = cli_set_unix_extensions_capabilities_recv(req);
257 fail:
258 TALLOC_FREE(ev);
259 if (!NT_STATUS_IS_OK(status)) {
260 cli_set_error(cli, status);
262 return status;
265 struct cli_get_fs_attr_info_state {
266 uint16_t setup[1];
267 uint8_t param[2];
268 uint32_t fs_attr;
271 static void cli_get_fs_attr_info_done(struct tevent_req *subreq);
273 struct tevent_req *cli_get_fs_attr_info_send(TALLOC_CTX *mem_ctx,
274 struct tevent_context *ev,
275 struct cli_state *cli)
277 struct tevent_req *subreq, *req;
278 struct cli_get_fs_attr_info_state *state;
280 req = tevent_req_create(mem_ctx, &state,
281 struct cli_get_fs_attr_info_state);
282 if (req == NULL) {
283 return NULL;
285 SSVAL(state->setup+0, 0, TRANSACT2_QFSINFO);
286 SSVAL(state->param+0, 0, SMB_QUERY_FS_ATTRIBUTE_INFO);
288 subreq = cli_trans_send(state, ev, cli, SMBtrans2,
289 NULL, 0, 0, 0,
290 state->setup, 1, 0,
291 state->param, 2, 0,
292 NULL, 0, 560);
293 if (tevent_req_nomem(subreq, req)) {
294 return tevent_req_post(req, ev);
296 tevent_req_set_callback(subreq, cli_get_fs_attr_info_done, req);
297 return req;
300 static void cli_get_fs_attr_info_done(struct tevent_req *subreq)
302 struct tevent_req *req = tevent_req_callback_data(
303 subreq, struct tevent_req);
304 struct cli_get_fs_attr_info_state *state = tevent_req_data(
305 req, struct cli_get_fs_attr_info_state);
306 uint8_t *data;
307 uint32_t num_data;
308 NTSTATUS status;
310 status = cli_trans_recv(subreq, talloc_tos(), NULL, NULL, 0, NULL,
311 NULL, 0, NULL, &data, 12, &num_data);
312 TALLOC_FREE(subreq);
313 if (!NT_STATUS_IS_OK(status)) {
314 tevent_req_nterror(req, status);
315 return;
317 state->fs_attr = IVAL(data, 0);
318 TALLOC_FREE(data);
319 tevent_req_done(req);
322 NTSTATUS cli_get_fs_attr_info_recv(struct tevent_req *req, uint32_t *fs_attr)
324 struct cli_get_fs_attr_info_state *state = tevent_req_data(
325 req, struct cli_get_fs_attr_info_state);
326 NTSTATUS status;
328 if (tevent_req_is_nterror(req, &status)) {
329 return status;
331 *fs_attr = state->fs_attr;
332 return NT_STATUS_OK;
335 NTSTATUS cli_get_fs_attr_info(struct cli_state *cli, uint32_t *fs_attr)
337 struct tevent_context *ev;
338 struct tevent_req *req;
339 NTSTATUS status = NT_STATUS_NO_MEMORY;
341 if (cli_has_async_calls(cli)) {
342 return NT_STATUS_INVALID_PARAMETER;
344 ev = tevent_context_init(talloc_tos());
345 if (ev == NULL) {
346 goto fail;
348 req = cli_get_fs_attr_info_send(ev, ev, cli);
349 if (req == NULL) {
350 goto fail;
352 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
353 goto fail;
355 status = cli_get_fs_attr_info_recv(req, fs_attr);
356 fail:
357 TALLOC_FREE(ev);
358 if (!NT_STATUS_IS_OK(status)) {
359 cli_set_error(cli, status);
361 return status;
364 NTSTATUS cli_get_fs_volume_info(struct cli_state *cli, fstring volume_name,
365 uint32 *pserial_number, time_t *pdate)
367 NTSTATUS status;
368 uint16 setup[1];
369 uint8_t param[2];
370 uint8_t *rdata;
371 uint32_t rdata_count;
372 unsigned int nlen;
374 SSVAL(setup, 0, TRANSACT2_QFSINFO);
375 SSVAL(param,0,SMB_QUERY_FS_VOLUME_INFO);
377 status = cli_trans(talloc_tos(), cli, SMBtrans2,
378 NULL, 0, 0, 0,
379 setup, 1, 0,
380 param, 2, 0,
381 NULL, 0, 560,
382 NULL,
383 NULL, 0, NULL,
384 NULL, 0, NULL,
385 &rdata, 10, &rdata_count);
386 if (!NT_STATUS_IS_OK(status)) {
387 return status;
390 if (pdate) {
391 struct timespec ts;
392 ts = interpret_long_date((char *)rdata);
393 *pdate = ts.tv_sec;
395 if (pserial_number) {
396 *pserial_number = IVAL(rdata,8);
398 nlen = IVAL(rdata,12);
399 clistr_pull(cli->inbuf, volume_name, rdata + 18, sizeof(fstring),
400 nlen, STR_UNICODE);
402 /* todo: but not yet needed
403 * return the other stuff
406 TALLOC_FREE(rdata);
407 return NT_STATUS_OK;
410 NTSTATUS cli_get_fs_full_size_info(struct cli_state *cli,
411 uint64_t *total_allocation_units,
412 uint64_t *caller_allocation_units,
413 uint64_t *actual_allocation_units,
414 uint64_t *sectors_per_allocation_unit,
415 uint64_t *bytes_per_sector)
417 uint16 setup[1];
418 uint8_t param[2];
419 uint8_t *rdata = NULL;
420 uint32_t rdata_count;
421 NTSTATUS status;
423 SSVAL(setup, 0, TRANSACT2_QFSINFO);
424 SSVAL(param, 0, SMB_FS_FULL_SIZE_INFORMATION);
426 status = cli_trans(talloc_tos(), cli, SMBtrans2,
427 NULL, 0, 0, 0,
428 setup, 1, 0, /* setup */
429 param, 2, 0, /* param */
430 NULL, 0, 560, /* data */
431 NULL,
432 NULL, 0, NULL, /* rsetup */
433 NULL, 0, NULL, /* rparam */
434 &rdata, 32, &rdata_count); /* rdata */
435 if (!NT_STATUS_IS_OK(status)) {
436 goto fail;
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 fail:
456 TALLOC_FREE(rdata);
457 return status;
460 NTSTATUS cli_get_posix_fs_info(struct cli_state *cli,
461 uint32 *optimal_transfer_size,
462 uint32 *block_size,
463 uint64_t *total_blocks,
464 uint64_t *blocks_available,
465 uint64_t *user_blocks_available,
466 uint64_t *total_file_nodes,
467 uint64_t *free_file_nodes,
468 uint64_t *fs_identifier)
470 uint16 setup[1];
471 uint8_t param[2];
472 uint8_t *rdata = NULL;
473 NTSTATUS status;
475 SSVAL(setup, 0, TRANSACT2_QFSINFO);
476 SSVAL(param,0,SMB_QUERY_POSIX_FS_INFO);
478 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, 0, 0, 0,
479 setup, 1, 0,
480 param, 2, 0,
481 NULL, 0, 560,
482 NULL,
483 NULL, 0, NULL, /* rsetup */
484 NULL, 0, NULL, /* rparam */
485 &rdata, 56, NULL);
486 if (!NT_STATUS_IS_OK(status)) {
487 return status;
490 if (optimal_transfer_size) {
491 *optimal_transfer_size = IVAL(rdata, 0);
493 if (block_size) {
494 *block_size = IVAL(rdata,4);
496 if (total_blocks) {
497 *total_blocks = BIG_UINT(rdata,8);
499 if (blocks_available) {
500 *blocks_available = BIG_UINT(rdata,16);
502 if (user_blocks_available) {
503 *user_blocks_available = BIG_UINT(rdata,24);
505 if (total_file_nodes) {
506 *total_file_nodes = BIG_UINT(rdata,32);
508 if (free_file_nodes) {
509 *free_file_nodes = BIG_UINT(rdata,40);
511 if (fs_identifier) {
512 *fs_identifier = BIG_UINT(rdata,48);
514 return NT_STATUS_OK;
518 /******************************************************************************
519 Send/receive the request encryption blob.
520 ******************************************************************************/
522 static NTSTATUS enc_blob_send_receive(struct cli_state *cli, DATA_BLOB *in, DATA_BLOB *out, DATA_BLOB *param_out)
524 uint16_t setup[1];
525 uint8_t param[4];
526 uint8_t *rparam=NULL, *rdata=NULL;
527 uint32_t num_rparam, num_rdata;
528 NTSTATUS status;
530 SSVAL(setup+0, 0, TRANSACT2_SETFSINFO);
531 SSVAL(param,0,0);
532 SSVAL(param,2,SMB_REQUEST_TRANSPORT_ENCRYPTION);
534 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, 0, 0, 0,
535 setup, 1, 0,
536 param, 4, 2,
537 (uint8_t *)in->data, in->length, CLI_BUFFER_SIZE,
538 NULL, /* recv_flags */
539 NULL, 0, NULL, /* rsetup */
540 &rparam, 0, &num_rparam,
541 &rdata, 0, &num_rdata);
543 if (!NT_STATUS_IS_OK(status) &&
544 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
545 return status;
548 *out = data_blob(rdata, num_rdata);
549 *param_out = data_blob(rparam, num_rparam);
551 TALLOC_FREE(rparam);
552 TALLOC_FREE(rdata);
553 return status;
556 /******************************************************************************
557 Make a client state struct.
558 ******************************************************************************/
560 static struct smb_trans_enc_state *make_cli_enc_state(enum smb_trans_enc_type smb_enc_type)
562 struct smb_trans_enc_state *es = NULL;
563 es = SMB_MALLOC_P(struct smb_trans_enc_state);
564 if (!es) {
565 return NULL;
567 ZERO_STRUCTP(es);
568 es->smb_enc_type = smb_enc_type;
570 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
571 if (smb_enc_type == SMB_TRANS_ENC_GSS) {
572 es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
573 if (!es->s.gss_state) {
574 SAFE_FREE(es);
575 return NULL;
577 ZERO_STRUCTP(es->s.gss_state);
579 #endif
580 return es;
583 /******************************************************************************
584 Start a raw ntlmssp encryption.
585 ******************************************************************************/
587 NTSTATUS cli_raw_ntlm_smb_encryption_start(struct cli_state *cli,
588 const char *user,
589 const char *pass,
590 const char *domain)
592 DATA_BLOB blob_in = data_blob_null;
593 DATA_BLOB blob_out = data_blob_null;
594 DATA_BLOB param_out = data_blob_null;
595 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
596 struct smb_trans_enc_state *es = make_cli_enc_state(SMB_TRANS_ENC_NTLM);
598 if (!es) {
599 return NT_STATUS_NO_MEMORY;
601 status = ntlmssp_client_start(NULL,
602 global_myname(),
603 lp_workgroup(),
604 lp_client_ntlmv2_auth(),
605 &es->s.ntlmssp_state);
606 if (!NT_STATUS_IS_OK(status)) {
607 goto fail;
610 ntlmssp_want_feature(es->s.ntlmssp_state, NTLMSSP_FEATURE_SESSION_KEY);
611 es->s.ntlmssp_state->neg_flags |= (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL);
613 if (!NT_STATUS_IS_OK(status = ntlmssp_set_username(es->s.ntlmssp_state, user))) {
614 goto fail;
616 if (!NT_STATUS_IS_OK(status = ntlmssp_set_domain(es->s.ntlmssp_state, domain))) {
617 goto fail;
619 if (!NT_STATUS_IS_OK(status = ntlmssp_set_password(es->s.ntlmssp_state, pass))) {
620 goto fail;
623 do {
624 status = ntlmssp_update(es->s.ntlmssp_state, blob_in, &blob_out);
625 data_blob_free(&blob_in);
626 data_blob_free(&param_out);
627 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) || NT_STATUS_IS_OK(status)) {
628 NTSTATUS trans_status = enc_blob_send_receive(cli,
629 &blob_out,
630 &blob_in,
631 &param_out);
632 if (!NT_STATUS_EQUAL(trans_status,
633 NT_STATUS_MORE_PROCESSING_REQUIRED) &&
634 !NT_STATUS_IS_OK(trans_status)) {
635 status = trans_status;
636 } else {
637 if (param_out.length == 2) {
638 es->enc_ctx_num = SVAL(param_out.data, 0);
642 data_blob_free(&blob_out);
643 } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
645 data_blob_free(&blob_in);
647 if (NT_STATUS_IS_OK(status)) {
648 /* Replace the old state, if any. */
649 if (cli->trans_enc_state) {
650 common_free_encryption_state(&cli->trans_enc_state);
652 cli->trans_enc_state = es;
653 cli->trans_enc_state->enc_on = True;
654 es = NULL;
657 fail:
659 common_free_encryption_state(&es);
660 return status;
663 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
665 #ifndef SMB_GSS_REQUIRED_FLAGS
666 #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)
667 #endif
669 /******************************************************************************
670 Get client gss blob to send to a server.
671 ******************************************************************************/
673 static NTSTATUS make_cli_gss_blob(TALLOC_CTX *ctx,
674 struct smb_trans_enc_state *es,
675 const char *service,
676 const char *host,
677 NTSTATUS status_in,
678 DATA_BLOB spnego_blob_in,
679 DATA_BLOB *p_blob_out)
681 const char *krb_mechs[] = {OID_KERBEROS5, NULL};
682 OM_uint32 ret;
683 OM_uint32 min;
684 gss_name_t srv_name;
685 gss_buffer_desc input_name;
686 gss_buffer_desc *p_tok_in;
687 gss_buffer_desc tok_out, tok_in;
688 DATA_BLOB blob_out = data_blob_null;
689 DATA_BLOB blob_in = data_blob_null;
690 char *host_princ_s = NULL;
691 OM_uint32 ret_flags = 0;
692 NTSTATUS status = NT_STATUS_OK;
694 gss_OID_desc nt_hostbased_service =
695 {10, CONST_DISCARD(char *,"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
697 memset(&tok_out, '\0', sizeof(tok_out));
699 /* Get a ticket for the service@host */
700 if (asprintf(&host_princ_s, "%s@%s", service, host) == -1) {
701 return NT_STATUS_NO_MEMORY;
704 input_name.value = host_princ_s;
705 input_name.length = strlen(host_princ_s) + 1;
707 ret = gss_import_name(&min,
708 &input_name,
709 &nt_hostbased_service,
710 &srv_name);
712 if (ret != GSS_S_COMPLETE) {
713 SAFE_FREE(host_princ_s);
714 return map_nt_error_from_gss(ret, min);
717 if (spnego_blob_in.length == 0) {
718 p_tok_in = GSS_C_NO_BUFFER;
719 } else {
720 /* Remove the SPNEGO wrapper */
721 if (!spnego_parse_auth_response(ctx, spnego_blob_in, status_in, OID_KERBEROS5, &blob_in)) {
722 status = NT_STATUS_UNSUCCESSFUL;
723 goto fail;
725 tok_in.value = blob_in.data;
726 tok_in.length = blob_in.length;
727 p_tok_in = &tok_in;
730 ret = gss_init_sec_context(&min,
731 GSS_C_NO_CREDENTIAL, /* Use our default cred. */
732 &es->s.gss_state->gss_ctx,
733 srv_name,
734 GSS_C_NO_OID, /* default OID. */
735 GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG | GSS_C_DELEG_FLAG,
736 GSS_C_INDEFINITE, /* requested ticket lifetime. */
737 NULL, /* no channel bindings */
738 p_tok_in,
739 NULL, /* ignore mech type */
740 &tok_out,
741 &ret_flags,
742 NULL); /* ignore time_rec */
744 status = map_nt_error_from_gss(ret, min);
745 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
746 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
747 DEBUG(10,("make_cli_gss_blob: gss_init_sec_context failed with %s\n",
748 ads_errstr(adss)));
749 goto fail;
752 if ((ret_flags & SMB_GSS_REQUIRED_FLAGS) != SMB_GSS_REQUIRED_FLAGS) {
753 status = NT_STATUS_ACCESS_DENIED;
756 blob_out = data_blob_talloc(ctx, tok_out.value, tok_out.length);
758 /* Wrap in an SPNEGO wrapper */
759 *p_blob_out = spnego_gen_negTokenInit(ctx, krb_mechs, &blob_out, NULL);
761 fail:
763 data_blob_free(&blob_out);
764 data_blob_free(&blob_in);
765 SAFE_FREE(host_princ_s);
766 gss_release_name(&min, &srv_name);
767 if (tok_out.value) {
768 gss_release_buffer(&min, &tok_out);
770 return status;
773 /******************************************************************************
774 Start a SPNEGO gssapi encryption context.
775 ******************************************************************************/
777 NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
779 DATA_BLOB blob_recv = data_blob_null;
780 DATA_BLOB blob_send = data_blob_null;
781 DATA_BLOB param_out = data_blob_null;
782 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
783 fstring fqdn;
784 const char *servicename;
785 struct smb_trans_enc_state *es = make_cli_enc_state(SMB_TRANS_ENC_GSS);
787 if (!es) {
788 return NT_STATUS_NO_MEMORY;
791 name_to_fqdn(fqdn, cli->desthost);
792 strlower_m(fqdn);
794 servicename = "cifs";
795 status = make_cli_gss_blob(talloc_tos(), es, servicename, fqdn, NT_STATUS_OK, blob_recv, &blob_send);
796 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
797 servicename = "host";
798 status = make_cli_gss_blob(talloc_tos(), es, servicename, fqdn, NT_STATUS_OK, blob_recv, &blob_send);
799 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
800 goto fail;
804 do {
805 data_blob_free(&blob_recv);
806 status = enc_blob_send_receive(cli, &blob_send, &blob_recv, &param_out);
807 if (param_out.length == 2) {
808 es->enc_ctx_num = SVAL(param_out.data, 0);
810 data_blob_free(&blob_send);
811 status = make_cli_gss_blob(talloc_tos(), es, servicename, fqdn, status, blob_recv, &blob_send);
812 } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
813 data_blob_free(&blob_recv);
815 if (NT_STATUS_IS_OK(status)) {
816 /* Replace the old state, if any. */
817 if (cli->trans_enc_state) {
818 common_free_encryption_state(&cli->trans_enc_state);
820 cli->trans_enc_state = es;
821 cli->trans_enc_state->enc_on = True;
822 es = NULL;
825 fail:
827 common_free_encryption_state(&es);
828 return status;
830 #else
831 NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
833 return NT_STATUS_NOT_SUPPORTED;
835 #endif
837 /********************************************************************
838 Ensure a connection is encrypted.
839 ********************************************************************/
841 NTSTATUS cli_force_encryption(struct cli_state *c,
842 const char *username,
843 const char *password,
844 const char *domain)
846 uint16 major, minor;
847 uint32 caplow, caphigh;
848 NTSTATUS status;
850 if (!SERVER_HAS_UNIX_CIFS(c)) {
851 return NT_STATUS_NOT_SUPPORTED;
854 status = cli_unix_extensions_version(c, &major, &minor, &caplow,
855 &caphigh);
856 if (!NT_STATUS_IS_OK(status)) {
857 DEBUG(10, ("cli_force_encryption: cli_unix_extensions_version "
858 "returned %s\n", nt_errstr(status)));
859 return NT_STATUS_UNKNOWN_REVISION;
862 if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
863 return NT_STATUS_UNSUPPORTED_COMPRESSION;
866 if (c->use_kerberos) {
867 return cli_gss_smb_encryption_start(c);
869 return cli_raw_ntlm_smb_encryption_start(c,
870 username,
871 password,
872 domain);