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