48c378259d3582837e094ba88680a0e01e83e007
[metze/samba/wip.git] / source3 / libsmb / clifsinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3    FS info functions
4    Copyright (C) Stefan (metze) Metzmacher      2003
5    Copyright (C) Jeremy Allison 2007
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
22 #include "../libcli/auth/spnego.h"
23 #include "ntlmssp.h"
24
25 /****************************************************************************
26  Get UNIX extensions version info.
27 ****************************************************************************/
28
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;
34 };
35
36 static void cli_unix_extensions_version_done(struct tevent_req *subreq);
37
38 struct tevent_req *cli_unix_extensions_version_send(TALLOC_CTX *mem_ctx,
39                                                     struct tevent_context *ev,
40                                                     struct cli_state *cli)
41 {
42         struct tevent_req *req, *subreq;
43         struct cli_unix_extensions_version_state *state;
44
45         req = tevent_req_create(mem_ctx, &state,
46                                 struct cli_unix_extensions_version_state);
47         if (req == NULL) {
48                 return NULL;
49         }
50         SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
51         SSVAL(state->param, 0, SMB_QUERY_CIFS_UNIX_INFO);
52
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);
60         }
61         tevent_req_set_callback(subreq, cli_unix_extensions_version_done, req);
62         return req;
63 }
64
65 static void cli_unix_extensions_version_done(struct tevent_req *subreq)
66 {
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;
74
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;
81         }
82
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);
89 }
90
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)
95 {
96         struct cli_unix_extensions_version_state *state = tevent_req_data(
97                 req, struct cli_unix_extensions_version_state);
98         NTSTATUS status;
99
100         if (tevent_req_is_nterror(req, &status)) {
101                 return status;
102         }
103         *pmajor = state->major;
104         *pminor = state->minor;
105         *pcaplow = state->caplow;
106         *pcaphigh = state->caphigh;
107         return NT_STATUS_OK;
108 }
109
110 NTSTATUS cli_unix_extensions_version(struct cli_state *cli, uint16 *pmajor,
111                                      uint16 *pminor, uint32 *pcaplow,
112                                      uint32 *pcaphigh)
113 {
114         TALLOC_CTX *frame = talloc_stackframe();
115         struct event_context *ev;
116         struct tevent_req *req;
117         NTSTATUS status = NT_STATUS_OK;
118
119         if (cli_has_async_calls(cli)) {
120                 /*
121                  * Can't use sync call while an async call is in flight
122                  */
123                 status = NT_STATUS_INVALID_PARAMETER;
124                 goto fail;
125         }
126
127         ev = event_context_init(frame);
128         if (ev == NULL) {
129                 status = NT_STATUS_NO_MEMORY;
130                 goto fail;
131         }
132
133         req = cli_unix_extensions_version_send(frame, ev, cli);
134         if (req == NULL) {
135                 status = NT_STATUS_NO_MEMORY;
136                 goto fail;
137         }
138
139         if (!tevent_req_poll(req, ev)) {
140                 status = map_nt_error_from_unix(errno);
141                 goto fail;
142         }
143
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;
148         }
149  fail:
150         TALLOC_FREE(frame);
151         if (!NT_STATUS_IS_OK(status)) {
152                 cli_set_error(cli, status);
153         }
154         return status;
155 }
156
157 /****************************************************************************
158  Set UNIX extensions capabilities.
159 ****************************************************************************/
160
161 struct cli_set_unix_extensions_capabilities_state {
162         uint16_t setup[1];
163         uint8_t param[4];
164         uint8_t data[12];
165 };
166
167 static void cli_set_unix_extensions_capabilities_done(
168         struct tevent_req *subreq);
169
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)
173 {
174         struct tevent_req *req, *subreq;
175         struct cli_set_unix_extensions_capabilities_state *state;
176
177         req = tevent_req_create(
178                 mem_ctx, &state,
179                 struct cli_set_unix_extensions_capabilities_state);
180         if (req == NULL) {
181                 return NULL;
182         }
183
184         SSVAL(state->setup+0, 0, TRANSACT2_SETFSINFO);
185
186         SSVAL(state->param, 0, 0);
187         SSVAL(state->param, 2, SMB_SET_CIFS_UNIX_INFO);
188
189         SSVAL(state->data, 0, major);
190         SSVAL(state->data, 2, minor);
191         SIVAL(state->data, 4, caplow);
192         SIVAL(state->data, 8, caphigh);
193
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);
201         }
202         tevent_req_set_callback(
203                 subreq, cli_set_unix_extensions_capabilities_done, req);
204         return req;
205 }
206
207 static void cli_set_unix_extensions_capabilities_done(
208         struct tevent_req *subreq)
209 {
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);
213 }
214
215 NTSTATUS cli_set_unix_extensions_capabilities_recv(struct tevent_req *req)
216 {
217         return tevent_req_simple_recv_ntstatus(req);
218 }
219
220 NTSTATUS cli_set_unix_extensions_capabilities(struct cli_state *cli,
221                                               uint16 major, uint16 minor,
222                                               uint32 caplow, uint32 caphigh)
223 {
224         struct tevent_context *ev;
225         struct tevent_req *req;
226         NTSTATUS status = NT_STATUS_NO_MEMORY;
227
228         if (cli_has_async_calls(cli)) {
229                 return NT_STATUS_INVALID_PARAMETER;
230         }
231         ev = tevent_context_init(talloc_tos());
232         if (ev == NULL) {
233                 goto fail;
234         }
235         req = cli_set_unix_extensions_capabilities_send(
236                 ev, ev, cli, major, minor, caplow, caphigh);
237         if (req == NULL) {
238                 goto fail;
239         }
240         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
241                 goto fail;
242         }
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);
248         }
249         return status;
250 }
251
252 struct cli_get_fs_attr_info_state {
253         uint16_t setup[1];
254         uint8_t param[2];
255         uint32_t fs_attr;
256 };
257
258 static void cli_get_fs_attr_info_done(struct tevent_req *subreq);
259
260 struct tevent_req *cli_get_fs_attr_info_send(TALLOC_CTX *mem_ctx,
261                                              struct tevent_context *ev,
262                                              struct cli_state *cli)
263 {
264         struct tevent_req *subreq, *req;
265         struct cli_get_fs_attr_info_state *state;
266
267         req = tevent_req_create(mem_ctx, &state,
268                                 struct cli_get_fs_attr_info_state);
269         if (req == NULL) {
270                 return NULL;
271         }
272         SSVAL(state->setup+0, 0, TRANSACT2_QFSINFO);
273         SSVAL(state->param+0, 0, SMB_QUERY_FS_ATTRIBUTE_INFO);
274
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);
282         }
283         tevent_req_set_callback(subreq, cli_get_fs_attr_info_done, req);
284         return req;
285 }
286
287 static void cli_get_fs_attr_info_done(struct tevent_req *subreq)
288 {
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;
296
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;
303         }
304         state->fs_attr = IVAL(data, 0);
305         TALLOC_FREE(data);
306         tevent_req_done(req);
307 }
308
309 NTSTATUS cli_get_fs_attr_info_recv(struct tevent_req *req, uint32_t *fs_attr)
310 {
311         struct cli_get_fs_attr_info_state *state = tevent_req_data(
312                 req, struct cli_get_fs_attr_info_state);
313         NTSTATUS status;
314
315         if (tevent_req_is_nterror(req, &status)) {
316                 return status;
317         }
318         *fs_attr = state->fs_attr;
319         return NT_STATUS_OK;
320 }
321
322 NTSTATUS cli_get_fs_attr_info(struct cli_state *cli, uint32_t *fs_attr)
323 {
324         struct tevent_context *ev;
325         struct tevent_req *req;
326         NTSTATUS status = NT_STATUS_NO_MEMORY;
327
328         if (cli_has_async_calls(cli)) {
329                 return NT_STATUS_INVALID_PARAMETER;
330         }
331         ev = tevent_context_init(talloc_tos());
332         if (ev == NULL) {
333                 goto fail;
334         }
335         req = cli_get_fs_attr_info_send(ev, ev, cli);
336         if (req == NULL) {
337                 goto fail;
338         }
339         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
340                 goto fail;
341         }
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);
347         }
348         return status;
349 }
350
351 bool cli_get_fs_volume_info_old(struct cli_state *cli, fstring volume_name, uint32 *pserial_number)
352 {
353         bool ret = False;
354         uint16 setup;
355         char param[2];
356         char *rparam=NULL, *rdata=NULL;
357         unsigned int rparam_count=0, rdata_count=0;
358         unsigned char nlen;
359
360         setup = TRANSACT2_QFSINFO;
361
362         SSVAL(param,0,SMB_INFO_VOLUME);
363
364         if (!cli_send_trans(cli, SMBtrans2,
365                     NULL,
366                     0, 0,
367                     &setup, 1, 0,
368                     param, 2, 0,
369                     NULL, 0, 560)) {
370                 goto cleanup;
371         }
372
373         if (!cli_receive_trans(cli, SMBtrans2,
374                               &rparam, &rparam_count,
375                               &rdata, &rdata_count)) {
376                 goto cleanup;
377         }
378
379         if (cli_is_error(cli)) {
380                 ret = False;
381                 goto cleanup;
382         } else {
383                 ret = True;
384         }
385
386         if (rdata_count < 5) {
387                 goto cleanup;
388         }
389
390         if (pserial_number) {
391                 *pserial_number = IVAL(rdata,0);
392         }
393         nlen = CVAL(rdata,l2_vol_cch);
394         clistr_pull(cli->inbuf, volume_name, rdata + l2_vol_szVolLabel,
395                     sizeof(fstring), nlen, STR_NOALIGN);
396
397         /* todo: but not yet needed
398          *       return the other stuff
399          */
400
401 cleanup:
402         SAFE_FREE(rparam);
403         SAFE_FREE(rdata);
404
405         return ret;
406 }
407
408 bool cli_get_fs_volume_info(struct cli_state *cli, fstring volume_name, uint32 *pserial_number, time_t *pdate)
409 {
410         bool ret = False;
411         uint16 setup;
412         char param[2];
413         char *rparam=NULL, *rdata=NULL;
414         unsigned int rparam_count=0, rdata_count=0;
415         unsigned int nlen;
416
417         setup = TRANSACT2_QFSINFO;
418
419         SSVAL(param,0,SMB_QUERY_FS_VOLUME_INFO);
420
421         if (!cli_send_trans(cli, SMBtrans2,
422                     NULL,
423                     0, 0,
424                     &setup, 1, 0,
425                     param, 2, 0,
426                     NULL, 0, 560)) {
427                 goto cleanup;
428         }
429
430         if (!cli_receive_trans(cli, SMBtrans2,
431                               &rparam, &rparam_count,
432                               &rdata, &rdata_count)) {
433                 goto cleanup;
434         }
435
436         if (cli_is_error(cli)) {
437                 ret = False;
438                 goto cleanup;
439         } else {
440                 ret = True;
441         }
442
443         if (rdata_count < 19) {
444                 goto cleanup;
445         }
446
447         if (pdate) {
448                 struct timespec ts;
449                 ts = interpret_long_date(rdata);
450                 *pdate = ts.tv_sec;
451         }
452         if (pserial_number) {
453                 *pserial_number = IVAL(rdata,8);
454         }
455         nlen = IVAL(rdata,12);
456         clistr_pull(cli->inbuf, volume_name, rdata + 18, sizeof(fstring),
457                     nlen, STR_UNICODE);
458
459         /* todo: but not yet needed
460          *       return the other stuff
461          */
462
463 cleanup:
464         SAFE_FREE(rparam);
465         SAFE_FREE(rdata);
466
467         return ret;
468 }
469
470 bool cli_get_fs_full_size_info(struct cli_state *cli,
471                                uint64_t *total_allocation_units,
472                                uint64_t *caller_allocation_units,
473                                uint64_t *actual_allocation_units,
474                                uint64_t *sectors_per_allocation_unit,
475                                uint64_t *bytes_per_sector)
476 {
477         bool ret = False;
478         uint16 setup;
479         char param[2];
480         char *rparam=NULL, *rdata=NULL;
481         unsigned int rparam_count=0, rdata_count=0;
482
483         setup = TRANSACT2_QFSINFO;
484
485         SSVAL(param,0,SMB_FS_FULL_SIZE_INFORMATION);
486
487         if (!cli_send_trans(cli, SMBtrans2,
488                     NULL,
489                     0, 0,
490                     &setup, 1, 0,
491                     param, 2, 0,
492                     NULL, 0, 560)) {
493                 goto cleanup;
494         }
495
496         if (!cli_receive_trans(cli, SMBtrans2,
497                               &rparam, &rparam_count,
498                               &rdata, &rdata_count)) {
499                 goto cleanup;
500         }
501
502         if (cli_is_error(cli)) {
503                 ret = False;
504                 goto cleanup;
505         } else {
506                 ret = True;
507         }
508
509         if (rdata_count != 32) {
510                 goto cleanup;
511         }
512
513         if (total_allocation_units) {
514                 *total_allocation_units = BIG_UINT(rdata, 0);
515         }
516         if (caller_allocation_units) {
517                 *caller_allocation_units = BIG_UINT(rdata,8);
518         }
519         if (actual_allocation_units) {
520                 *actual_allocation_units = BIG_UINT(rdata,16);
521         }
522         if (sectors_per_allocation_unit) {
523                 *sectors_per_allocation_unit = IVAL(rdata,24);
524         }
525         if (bytes_per_sector) {
526                 *bytes_per_sector = IVAL(rdata,28);
527         }
528
529 cleanup:
530         SAFE_FREE(rparam);
531         SAFE_FREE(rdata);
532
533         return ret;
534 }
535
536 bool cli_get_posix_fs_info(struct cli_state *cli,
537                            uint32 *optimal_transfer_size,
538                            uint32 *block_size,
539                            uint64_t *total_blocks,
540                            uint64_t *blocks_available,
541                            uint64_t *user_blocks_available,
542                            uint64_t *total_file_nodes,
543                            uint64_t *free_file_nodes,
544                            uint64_t *fs_identifier)
545 {
546         bool ret = False;
547         uint16 setup;
548         char param[2];
549         char *rparam=NULL, *rdata=NULL;
550         unsigned int rparam_count=0, rdata_count=0;
551
552         setup = TRANSACT2_QFSINFO;
553
554         SSVAL(param,0,SMB_QUERY_POSIX_FS_INFO);
555
556         if (!cli_send_trans(cli, SMBtrans2,
557                     NULL,
558                     0, 0,
559                     &setup, 1, 0,
560                     param, 2, 0,
561                     NULL, 0, 560)) {
562                 goto cleanup;
563         }
564
565         if (!cli_receive_trans(cli, SMBtrans2,
566                               &rparam, &rparam_count,
567                               &rdata, &rdata_count)) {
568                 goto cleanup;
569         }
570
571         if (cli_is_error(cli)) {
572                 ret = False;
573                 goto cleanup;
574         } else {
575                 ret = True;
576         }
577
578         if (rdata_count != 56) {
579                 goto cleanup;
580         }
581
582         if (optimal_transfer_size) {
583                 *optimal_transfer_size = IVAL(rdata, 0);
584         }
585         if (block_size) {
586                 *block_size = IVAL(rdata,4);
587         }
588         if (total_blocks) {
589                 *total_blocks = BIG_UINT(rdata,8);
590         }
591         if (blocks_available) {
592                 *blocks_available = BIG_UINT(rdata,16);
593         }
594         if (user_blocks_available) {
595                 *user_blocks_available = BIG_UINT(rdata,24);
596         }
597         if (total_file_nodes) {
598                 *total_file_nodes = BIG_UINT(rdata,32);
599         }
600         if (free_file_nodes) {
601                 *free_file_nodes = BIG_UINT(rdata,40);
602         }
603         if (fs_identifier) {
604                 *fs_identifier = BIG_UINT(rdata,48);
605         }
606
607 cleanup:
608         SAFE_FREE(rparam);
609         SAFE_FREE(rdata);
610
611         return ret;
612 }
613
614
615 /******************************************************************************
616  Send/receive the request encryption blob.
617 ******************************************************************************/
618
619 static NTSTATUS enc_blob_send_receive(struct cli_state *cli, DATA_BLOB *in, DATA_BLOB *out, DATA_BLOB *param_out)
620 {
621         uint16 setup;
622         char param[4];
623         char *rparam=NULL, *rdata=NULL;
624         unsigned int rparam_count=0, rdata_count=0;
625         NTSTATUS status = NT_STATUS_OK;
626
627         setup = TRANSACT2_SETFSINFO;
628
629         SSVAL(param,0,0);
630         SSVAL(param,2,SMB_REQUEST_TRANSPORT_ENCRYPTION);
631
632         if (!cli_send_trans(cli, SMBtrans2,
633                                 NULL,
634                                 0, 0,
635                                 &setup, 1, 0,
636                                 param, 4, 0,
637                                 (char *)in->data, in->length, CLI_BUFFER_SIZE)) {
638                 status = cli_nt_error(cli);
639                 goto out;
640         }
641
642         if (!cli_receive_trans(cli, SMBtrans2,
643                                 &rparam, &rparam_count,
644                                 &rdata, &rdata_count)) {
645                 status = cli_nt_error(cli);
646                 goto out;
647         }
648
649         if (cli_is_error(cli)) {
650                 status = cli_nt_error(cli);
651                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
652                         goto out;
653                 }
654         }
655
656         *out = data_blob(rdata, rdata_count);
657         *param_out = data_blob(rparam, rparam_count);
658
659   out:
660
661         SAFE_FREE(rparam);
662         SAFE_FREE(rdata);
663         return status;
664 }
665
666 /******************************************************************************
667  Make a client state struct.
668 ******************************************************************************/
669
670 static struct smb_trans_enc_state *make_cli_enc_state(enum smb_trans_enc_type smb_enc_type)
671 {
672         struct smb_trans_enc_state *es = NULL;
673         es = SMB_MALLOC_P(struct smb_trans_enc_state);
674         if (!es) {
675                 return NULL;
676         }
677         ZERO_STRUCTP(es);
678         es->smb_enc_type = smb_enc_type;
679
680 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
681         if (smb_enc_type == SMB_TRANS_ENC_GSS) {
682                 es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
683                 if (!es->s.gss_state) {
684                         SAFE_FREE(es);
685                         return NULL;
686                 }
687                 ZERO_STRUCTP(es->s.gss_state);
688         }
689 #endif
690         return es;
691 }
692
693 /******************************************************************************
694  Start a raw ntlmssp encryption.
695 ******************************************************************************/
696
697 NTSTATUS cli_raw_ntlm_smb_encryption_start(struct cli_state *cli, 
698                                 const char *user,
699                                 const char *pass,
700                                 const char *domain)
701 {
702         DATA_BLOB blob_in = data_blob_null;
703         DATA_BLOB blob_out = data_blob_null;
704         DATA_BLOB param_out = data_blob_null;
705         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
706         struct smb_trans_enc_state *es = make_cli_enc_state(SMB_TRANS_ENC_NTLM);
707
708         if (!es) {
709                 return NT_STATUS_NO_MEMORY;
710         }
711         status = ntlmssp_client_start(&es->s.ntlmssp_state);
712         if (!NT_STATUS_IS_OK(status)) {
713                 goto fail;
714         }
715
716         ntlmssp_want_feature(es->s.ntlmssp_state, NTLMSSP_FEATURE_SESSION_KEY);
717         es->s.ntlmssp_state->neg_flags |= (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL);
718
719         if (!NT_STATUS_IS_OK(status = ntlmssp_set_username(es->s.ntlmssp_state, user))) {
720                 goto fail;
721         }
722         if (!NT_STATUS_IS_OK(status = ntlmssp_set_domain(es->s.ntlmssp_state, domain))) {
723                 goto fail;
724         }
725         if (!NT_STATUS_IS_OK(status = ntlmssp_set_password(es->s.ntlmssp_state, pass))) {
726                 goto fail;
727         }
728
729         do {
730                 status = ntlmssp_update(es->s.ntlmssp_state, blob_in, &blob_out);
731                 data_blob_free(&blob_in);
732                 data_blob_free(&param_out);
733                 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) || NT_STATUS_IS_OK(status)) {
734                         NTSTATUS trans_status = enc_blob_send_receive(cli,
735                                                                         &blob_out,
736                                                                         &blob_in,
737                                                                         &param_out);
738                         if (!NT_STATUS_EQUAL(trans_status,
739                                         NT_STATUS_MORE_PROCESSING_REQUIRED) &&
740                                         !NT_STATUS_IS_OK(trans_status)) {
741                                 status = trans_status;
742                         } else {
743                                 if (param_out.length == 2) {
744                                         es->enc_ctx_num = SVAL(param_out.data, 0);
745                                 }
746                         }
747                 }
748                 data_blob_free(&blob_out);
749         } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
750
751         data_blob_free(&blob_in);
752
753         if (NT_STATUS_IS_OK(status)) {
754                 /* Replace the old state, if any. */
755                 if (cli->trans_enc_state) {
756                         common_free_encryption_state(&cli->trans_enc_state);
757                 }
758                 cli->trans_enc_state = es;
759                 cli->trans_enc_state->enc_on = True;
760                 es = NULL;
761         }
762
763   fail:
764
765         common_free_encryption_state(&es);
766         return status;
767 }
768
769 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
770
771 #ifndef SMB_GSS_REQUIRED_FLAGS
772 #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)
773 #endif
774
775 /******************************************************************************
776  Get client gss blob to send to a server.
777 ******************************************************************************/
778
779 static NTSTATUS make_cli_gss_blob(struct smb_trans_enc_state *es,
780                                 const char *service,
781                                 const char *host,
782                                 NTSTATUS status_in,
783                                 DATA_BLOB spnego_blob_in,
784                                 DATA_BLOB *p_blob_out)
785 {
786         const char *krb_mechs[] = {OID_KERBEROS5, NULL};
787         OM_uint32 ret;
788         OM_uint32 min;
789         gss_name_t srv_name;
790         gss_buffer_desc input_name;
791         gss_buffer_desc *p_tok_in;
792         gss_buffer_desc tok_out, tok_in;
793         DATA_BLOB blob_out = data_blob_null;
794         DATA_BLOB blob_in = data_blob_null;
795         char *host_princ_s = NULL;
796         OM_uint32 ret_flags = 0;
797         NTSTATUS status = NT_STATUS_OK;
798
799         gss_OID_desc nt_hostbased_service =
800         {10, CONST_DISCARD(char *,"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
801
802         memset(&tok_out, '\0', sizeof(tok_out));
803
804         /* Get a ticket for the service@host */
805         if (asprintf(&host_princ_s, "%s@%s", service, host) == -1) {
806                 return NT_STATUS_NO_MEMORY;
807         }
808
809         input_name.value = host_princ_s;
810         input_name.length = strlen(host_princ_s) + 1;
811
812         ret = gss_import_name(&min,
813                                 &input_name,
814                                 &nt_hostbased_service,
815                                 &srv_name);
816
817         if (ret != GSS_S_COMPLETE) {
818                 SAFE_FREE(host_princ_s);
819                 return map_nt_error_from_gss(ret, min);
820         }
821
822         if (spnego_blob_in.length == 0) {
823                 p_tok_in = GSS_C_NO_BUFFER;
824         } else {
825                 /* Remove the SPNEGO wrapper */
826                 if (!spnego_parse_auth_response(spnego_blob_in, status_in, OID_KERBEROS5, &blob_in)) {
827                         status = NT_STATUS_UNSUCCESSFUL;
828                         goto fail;
829                 }
830                 tok_in.value = blob_in.data;
831                 tok_in.length = blob_in.length;
832                 p_tok_in = &tok_in;
833         }
834
835         ret = gss_init_sec_context(&min,
836                                 GSS_C_NO_CREDENTIAL, /* Use our default cred. */
837                                 &es->s.gss_state->gss_ctx,
838                                 srv_name,
839                                 GSS_C_NO_OID, /* default OID. */
840                                 GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG | GSS_C_DELEG_FLAG,
841                                 GSS_C_INDEFINITE,       /* requested ticket lifetime. */
842                                 NULL,   /* no channel bindings */
843                                 p_tok_in,
844                                 NULL,   /* ignore mech type */
845                                 &tok_out,
846                                 &ret_flags,
847                                 NULL);  /* ignore time_rec */
848
849         status = map_nt_error_from_gss(ret, min);
850         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
851                 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
852                 DEBUG(10,("make_cli_gss_blob: gss_init_sec_context failed with %s\n",
853                         ads_errstr(adss)));
854                 goto fail;
855         }
856
857         if ((ret_flags & SMB_GSS_REQUIRED_FLAGS) != SMB_GSS_REQUIRED_FLAGS) {
858                 status = NT_STATUS_ACCESS_DENIED;
859         }
860
861         blob_out = data_blob(tok_out.value, tok_out.length);
862
863         /* Wrap in an SPNEGO wrapper */
864         *p_blob_out = gen_negTokenTarg(krb_mechs, blob_out);
865
866   fail:
867
868         data_blob_free(&blob_out);
869         data_blob_free(&blob_in);
870         SAFE_FREE(host_princ_s);
871         gss_release_name(&min, &srv_name);
872         if (tok_out.value) {
873                 gss_release_buffer(&min, &tok_out);
874         }
875         return status;
876 }
877
878 /******************************************************************************
879  Start a SPNEGO gssapi encryption context.
880 ******************************************************************************/
881
882 NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
883 {
884         DATA_BLOB blob_recv = data_blob_null;
885         DATA_BLOB blob_send = data_blob_null;
886         DATA_BLOB param_out = data_blob_null;
887         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
888         fstring fqdn;
889         const char *servicename;
890         struct smb_trans_enc_state *es = make_cli_enc_state(SMB_TRANS_ENC_GSS);
891
892         if (!es) {
893                 return NT_STATUS_NO_MEMORY;
894         }
895
896         name_to_fqdn(fqdn, cli->desthost);
897         strlower_m(fqdn);
898
899         servicename = "cifs";
900         status = make_cli_gss_blob(es, servicename, fqdn, NT_STATUS_OK, blob_recv, &blob_send);
901         if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
902                 servicename = "host";
903                 status = make_cli_gss_blob(es, servicename, fqdn, NT_STATUS_OK, blob_recv, &blob_send);
904                 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
905                         goto fail;
906                 }
907         }
908
909         do {
910                 data_blob_free(&blob_recv);
911                 status = enc_blob_send_receive(cli, &blob_send, &blob_recv, &param_out);
912                 if (param_out.length == 2) {
913                         es->enc_ctx_num = SVAL(param_out.data, 0);
914                 }
915                 data_blob_free(&blob_send);
916                 status = make_cli_gss_blob(es, servicename, fqdn, status, blob_recv, &blob_send);
917         } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
918         data_blob_free(&blob_recv);
919
920         if (NT_STATUS_IS_OK(status)) {
921                 /* Replace the old state, if any. */
922                 if (cli->trans_enc_state) {
923                         common_free_encryption_state(&cli->trans_enc_state);
924                 }
925                 cli->trans_enc_state = es;
926                 cli->trans_enc_state->enc_on = True;
927                 es = NULL;
928         }
929
930   fail:
931
932         common_free_encryption_state(&es);
933         return status;
934 }
935 #else
936 NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
937 {
938         return NT_STATUS_NOT_SUPPORTED;
939 }
940 #endif
941
942 /********************************************************************
943  Ensure a connection is encrypted.
944 ********************************************************************/
945
946 NTSTATUS cli_force_encryption(struct cli_state *c,
947                         const char *username,
948                         const char *password,
949                         const char *domain)
950 {
951         uint16 major, minor;
952         uint32 caplow, caphigh;
953         NTSTATUS status;
954
955         if (!SERVER_HAS_UNIX_CIFS(c)) {
956                 return NT_STATUS_NOT_SUPPORTED;
957         }
958
959         status = cli_unix_extensions_version(c, &major, &minor, &caplow,
960                                              &caphigh);
961         if (!NT_STATUS_IS_OK(status)) {
962                 DEBUG(10, ("cli_force_encryption: cli_unix_extensions_version "
963                            "returned %s\n", nt_errstr(status)));
964                 return NT_STATUS_UNKNOWN_REVISION;
965         }
966
967         if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
968                 return NT_STATUS_UNSUPPORTED_COMPRESSION;
969         }
970
971         if (c->use_kerberos) {
972                 return cli_gss_smb_encryption_start(c);
973         }
974         return cli_raw_ntlm_smb_encryption_start(c,
975                                         username,
976                                         password,
977                                         domain);
978 }