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