Split out the client unix capabilities to those the server offered, and those the...
[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 "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         } else {
259                 cli->requested_posix_capabilities = caplow;
260         }
261         return status;
262 }
263
264 struct cli_get_fs_attr_info_state {
265         uint16_t setup[1];
266         uint8_t param[2];
267         uint32_t fs_attr;
268 };
269
270 static void cli_get_fs_attr_info_done(struct tevent_req *subreq);
271
272 struct tevent_req *cli_get_fs_attr_info_send(TALLOC_CTX *mem_ctx,
273                                              struct tevent_context *ev,
274                                              struct cli_state *cli)
275 {
276         struct tevent_req *subreq, *req;
277         struct cli_get_fs_attr_info_state *state;
278
279         req = tevent_req_create(mem_ctx, &state,
280                                 struct cli_get_fs_attr_info_state);
281         if (req == NULL) {
282                 return NULL;
283         }
284         SSVAL(state->setup+0, 0, TRANSACT2_QFSINFO);
285         SSVAL(state->param+0, 0, SMB_QUERY_FS_ATTRIBUTE_INFO);
286
287         subreq = cli_trans_send(state, ev, cli, SMBtrans2,
288                                 NULL, 0, 0, 0,
289                                 state->setup, 1, 0,
290                                 state->param, 2, 0,
291                                 NULL, 0, 560);
292         if (tevent_req_nomem(subreq, req)) {
293                 return tevent_req_post(req, ev);
294         }
295         tevent_req_set_callback(subreq, cli_get_fs_attr_info_done, req);
296         return req;
297 }
298
299 static void cli_get_fs_attr_info_done(struct tevent_req *subreq)
300 {
301         struct tevent_req *req = tevent_req_callback_data(
302                 subreq, struct tevent_req);
303         struct cli_get_fs_attr_info_state *state = tevent_req_data(
304                 req, struct cli_get_fs_attr_info_state);
305         uint8_t *data;
306         uint32_t num_data;
307         NTSTATUS status;
308
309         status = cli_trans_recv(subreq, talloc_tos(), NULL, 0, NULL,
310                                 NULL, 0, NULL, &data, 12, &num_data);
311         TALLOC_FREE(subreq);
312         if (!NT_STATUS_IS_OK(status)) {
313                 tevent_req_nterror(req, status);
314                 return;
315         }
316         state->fs_attr = IVAL(data, 0);
317         TALLOC_FREE(data);
318         tevent_req_done(req);
319 }
320
321 NTSTATUS cli_get_fs_attr_info_recv(struct tevent_req *req, uint32_t *fs_attr)
322 {
323         struct cli_get_fs_attr_info_state *state = tevent_req_data(
324                 req, struct cli_get_fs_attr_info_state);
325         NTSTATUS status;
326
327         if (tevent_req_is_nterror(req, &status)) {
328                 return status;
329         }
330         *fs_attr = state->fs_attr;
331         return NT_STATUS_OK;
332 }
333
334 NTSTATUS cli_get_fs_attr_info(struct cli_state *cli, uint32_t *fs_attr)
335 {
336         struct tevent_context *ev;
337         struct tevent_req *req;
338         NTSTATUS status = NT_STATUS_NO_MEMORY;
339
340         if (cli_has_async_calls(cli)) {
341                 return NT_STATUS_INVALID_PARAMETER;
342         }
343         ev = tevent_context_init(talloc_tos());
344         if (ev == NULL) {
345                 goto fail;
346         }
347         req = cli_get_fs_attr_info_send(ev, ev, cli);
348         if (req == NULL) {
349                 goto fail;
350         }
351         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
352                 goto fail;
353         }
354         status = cli_get_fs_attr_info_recv(req, fs_attr);
355 fail:
356         TALLOC_FREE(ev);
357         if (!NT_STATUS_IS_OK(status)) {
358                 cli_set_error(cli, status);
359         }
360         return status;
361 }
362
363 NTSTATUS cli_get_fs_volume_info(struct cli_state *cli, fstring volume_name,
364                                 uint32 *pserial_number, time_t *pdate)
365 {
366         NTSTATUS status;
367         uint16 setup[1];
368         uint8_t param[2];
369         uint8_t *rdata;
370         uint32_t rdata_count;
371         unsigned int nlen;
372
373         SSVAL(setup, 0, TRANSACT2_QFSINFO);
374         SSVAL(param,0,SMB_QUERY_FS_VOLUME_INFO);
375
376         status = cli_trans(talloc_tos(), cli, SMBtrans2,
377                            NULL, 0, 0, 0,
378                            setup, 1, 0,
379                            param, 2, 0,
380                            NULL, 0, 560,
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 bool 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         bool ret = False;
416         uint16 setup;
417         char param[2];
418         char *rparam=NULL, *rdata=NULL;
419         unsigned int rparam_count=0, rdata_count=0;
420
421         setup = TRANSACT2_QFSINFO;
422
423         SSVAL(param,0,SMB_FS_FULL_SIZE_INFORMATION);
424
425         if (!cli_send_trans(cli, SMBtrans2,
426                     NULL,
427                     0, 0,
428                     &setup, 1, 0,
429                     param, 2, 0,
430                     NULL, 0, 560)) {
431                 goto cleanup;
432         }
433
434         if (!cli_receive_trans(cli, SMBtrans2,
435                               &rparam, &rparam_count,
436                               &rdata, &rdata_count)) {
437                 goto cleanup;
438         }
439
440         if (cli_is_error(cli)) {
441                 ret = False;
442                 goto cleanup;
443         } else {
444                 ret = True;
445         }
446
447         if (rdata_count != 32) {
448                 goto cleanup;
449         }
450
451         if (total_allocation_units) {
452                 *total_allocation_units = BIG_UINT(rdata, 0);
453         }
454         if (caller_allocation_units) {
455                 *caller_allocation_units = BIG_UINT(rdata,8);
456         }
457         if (actual_allocation_units) {
458                 *actual_allocation_units = BIG_UINT(rdata,16);
459         }
460         if (sectors_per_allocation_unit) {
461                 *sectors_per_allocation_unit = IVAL(rdata,24);
462         }
463         if (bytes_per_sector) {
464                 *bytes_per_sector = IVAL(rdata,28);
465         }
466
467 cleanup:
468         SAFE_FREE(rparam);
469         SAFE_FREE(rdata);
470
471         return ret;
472 }
473
474 bool cli_get_posix_fs_info(struct cli_state *cli,
475                            uint32 *optimal_transfer_size,
476                            uint32 *block_size,
477                            uint64_t *total_blocks,
478                            uint64_t *blocks_available,
479                            uint64_t *user_blocks_available,
480                            uint64_t *total_file_nodes,
481                            uint64_t *free_file_nodes,
482                            uint64_t *fs_identifier)
483 {
484         bool ret = False;
485         uint16 setup;
486         char param[2];
487         char *rparam=NULL, *rdata=NULL;
488         unsigned int rparam_count=0, rdata_count=0;
489
490         setup = TRANSACT2_QFSINFO;
491
492         SSVAL(param,0,SMB_QUERY_POSIX_FS_INFO);
493
494         if (!cli_send_trans(cli, SMBtrans2,
495                     NULL,
496                     0, 0,
497                     &setup, 1, 0,
498                     param, 2, 0,
499                     NULL, 0, 560)) {
500                 goto cleanup;
501         }
502
503         if (!cli_receive_trans(cli, SMBtrans2,
504                               &rparam, &rparam_count,
505                               &rdata, &rdata_count)) {
506                 goto cleanup;
507         }
508
509         if (cli_is_error(cli)) {
510                 ret = False;
511                 goto cleanup;
512         } else {
513                 ret = True;
514         }
515
516         if (rdata_count != 56) {
517                 goto cleanup;
518         }
519
520         if (optimal_transfer_size) {
521                 *optimal_transfer_size = IVAL(rdata, 0);
522         }
523         if (block_size) {
524                 *block_size = IVAL(rdata,4);
525         }
526         if (total_blocks) {
527                 *total_blocks = BIG_UINT(rdata,8);
528         }
529         if (blocks_available) {
530                 *blocks_available = BIG_UINT(rdata,16);
531         }
532         if (user_blocks_available) {
533                 *user_blocks_available = BIG_UINT(rdata,24);
534         }
535         if (total_file_nodes) {
536                 *total_file_nodes = BIG_UINT(rdata,32);
537         }
538         if (free_file_nodes) {
539                 *free_file_nodes = BIG_UINT(rdata,40);
540         }
541         if (fs_identifier) {
542                 *fs_identifier = BIG_UINT(rdata,48);
543         }
544
545 cleanup:
546         SAFE_FREE(rparam);
547         SAFE_FREE(rdata);
548
549         return ret;
550 }
551
552
553 /******************************************************************************
554  Send/receive the request encryption blob.
555 ******************************************************************************/
556
557 static NTSTATUS enc_blob_send_receive(struct cli_state *cli, DATA_BLOB *in, DATA_BLOB *out, DATA_BLOB *param_out)
558 {
559         uint16 setup;
560         char param[4];
561         char *rparam=NULL, *rdata=NULL;
562         unsigned int rparam_count=0, rdata_count=0;
563         NTSTATUS status = NT_STATUS_OK;
564
565         setup = TRANSACT2_SETFSINFO;
566
567         SSVAL(param,0,0);
568         SSVAL(param,2,SMB_REQUEST_TRANSPORT_ENCRYPTION);
569
570         if (!cli_send_trans(cli, SMBtrans2,
571                                 NULL,
572                                 0, 0,
573                                 &setup, 1, 0,
574                                 param, 4, 0,
575                                 (char *)in->data, in->length, CLI_BUFFER_SIZE)) {
576                 status = cli_nt_error(cli);
577                 goto out;
578         }
579
580         if (!cli_receive_trans(cli, SMBtrans2,
581                                 &rparam, &rparam_count,
582                                 &rdata, &rdata_count)) {
583                 status = cli_nt_error(cli);
584                 goto out;
585         }
586
587         if (cli_is_error(cli)) {
588                 status = cli_nt_error(cli);
589                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
590                         goto out;
591                 }
592         }
593
594         *out = data_blob(rdata, rdata_count);
595         *param_out = data_blob(rparam, rparam_count);
596
597   out:
598
599         SAFE_FREE(rparam);
600         SAFE_FREE(rdata);
601         return status;
602 }
603
604 /******************************************************************************
605  Make a client state struct.
606 ******************************************************************************/
607
608 static struct smb_trans_enc_state *make_cli_enc_state(enum smb_trans_enc_type smb_enc_type)
609 {
610         struct smb_trans_enc_state *es = NULL;
611         es = SMB_MALLOC_P(struct smb_trans_enc_state);
612         if (!es) {
613                 return NULL;
614         }
615         ZERO_STRUCTP(es);
616         es->smb_enc_type = smb_enc_type;
617
618 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
619         if (smb_enc_type == SMB_TRANS_ENC_GSS) {
620                 es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
621                 if (!es->s.gss_state) {
622                         SAFE_FREE(es);
623                         return NULL;
624                 }
625                 ZERO_STRUCTP(es->s.gss_state);
626         }
627 #endif
628         return es;
629 }
630
631 /******************************************************************************
632  Start a raw ntlmssp encryption.
633 ******************************************************************************/
634
635 NTSTATUS cli_raw_ntlm_smb_encryption_start(struct cli_state *cli, 
636                                 const char *user,
637                                 const char *pass,
638                                 const char *domain)
639 {
640         DATA_BLOB blob_in = data_blob_null;
641         DATA_BLOB blob_out = data_blob_null;
642         DATA_BLOB param_out = data_blob_null;
643         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
644         struct smb_trans_enc_state *es = make_cli_enc_state(SMB_TRANS_ENC_NTLM);
645
646         if (!es) {
647                 return NT_STATUS_NO_MEMORY;
648         }
649         status = ntlmssp_client_start(NULL,
650                                       global_myname(),
651                                       lp_workgroup(),
652                                       lp_client_ntlmv2_auth(),
653                                       &es->s.ntlmssp_state);
654         if (!NT_STATUS_IS_OK(status)) {
655                 goto fail;
656         }
657
658         ntlmssp_want_feature(es->s.ntlmssp_state, NTLMSSP_FEATURE_SESSION_KEY);
659         es->s.ntlmssp_state->neg_flags |= (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL);
660
661         if (!NT_STATUS_IS_OK(status = ntlmssp_set_username(es->s.ntlmssp_state, user))) {
662                 goto fail;
663         }
664         if (!NT_STATUS_IS_OK(status = ntlmssp_set_domain(es->s.ntlmssp_state, domain))) {
665                 goto fail;
666         }
667         if (!NT_STATUS_IS_OK(status = ntlmssp_set_password(es->s.ntlmssp_state, pass))) {
668                 goto fail;
669         }
670
671         do {
672                 status = ntlmssp_update(es->s.ntlmssp_state, blob_in, &blob_out);
673                 data_blob_free(&blob_in);
674                 data_blob_free(&param_out);
675                 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) || NT_STATUS_IS_OK(status)) {
676                         NTSTATUS trans_status = enc_blob_send_receive(cli,
677                                                                         &blob_out,
678                                                                         &blob_in,
679                                                                         &param_out);
680                         if (!NT_STATUS_EQUAL(trans_status,
681                                         NT_STATUS_MORE_PROCESSING_REQUIRED) &&
682                                         !NT_STATUS_IS_OK(trans_status)) {
683                                 status = trans_status;
684                         } else {
685                                 if (param_out.length == 2) {
686                                         es->enc_ctx_num = SVAL(param_out.data, 0);
687                                 }
688                         }
689                 }
690                 data_blob_free(&blob_out);
691         } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
692
693         data_blob_free(&blob_in);
694
695         if (NT_STATUS_IS_OK(status)) {
696                 /* Replace the old state, if any. */
697                 if (cli->trans_enc_state) {
698                         common_free_encryption_state(&cli->trans_enc_state);
699                 }
700                 cli->trans_enc_state = es;
701                 cli->trans_enc_state->enc_on = True;
702                 es = NULL;
703         }
704
705   fail:
706
707         common_free_encryption_state(&es);
708         return status;
709 }
710
711 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
712
713 #ifndef SMB_GSS_REQUIRED_FLAGS
714 #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)
715 #endif
716
717 /******************************************************************************
718  Get client gss blob to send to a server.
719 ******************************************************************************/
720
721 static NTSTATUS make_cli_gss_blob(struct smb_trans_enc_state *es,
722                                 const char *service,
723                                 const char *host,
724                                 NTSTATUS status_in,
725                                 DATA_BLOB spnego_blob_in,
726                                 DATA_BLOB *p_blob_out)
727 {
728         const char *krb_mechs[] = {OID_KERBEROS5, NULL};
729         OM_uint32 ret;
730         OM_uint32 min;
731         gss_name_t srv_name;
732         gss_buffer_desc input_name;
733         gss_buffer_desc *p_tok_in;
734         gss_buffer_desc tok_out, tok_in;
735         DATA_BLOB blob_out = data_blob_null;
736         DATA_BLOB blob_in = data_blob_null;
737         char *host_princ_s = NULL;
738         OM_uint32 ret_flags = 0;
739         NTSTATUS status = NT_STATUS_OK;
740
741         gss_OID_desc nt_hostbased_service =
742         {10, CONST_DISCARD(char *,"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
743
744         memset(&tok_out, '\0', sizeof(tok_out));
745
746         /* Get a ticket for the service@host */
747         if (asprintf(&host_princ_s, "%s@%s", service, host) == -1) {
748                 return NT_STATUS_NO_MEMORY;
749         }
750
751         input_name.value = host_princ_s;
752         input_name.length = strlen(host_princ_s) + 1;
753
754         ret = gss_import_name(&min,
755                                 &input_name,
756                                 &nt_hostbased_service,
757                                 &srv_name);
758
759         if (ret != GSS_S_COMPLETE) {
760                 SAFE_FREE(host_princ_s);
761                 return map_nt_error_from_gss(ret, min);
762         }
763
764         if (spnego_blob_in.length == 0) {
765                 p_tok_in = GSS_C_NO_BUFFER;
766         } else {
767                 /* Remove the SPNEGO wrapper */
768                 if (!spnego_parse_auth_response(spnego_blob_in, status_in, OID_KERBEROS5, &blob_in)) {
769                         status = NT_STATUS_UNSUCCESSFUL;
770                         goto fail;
771                 }
772                 tok_in.value = blob_in.data;
773                 tok_in.length = blob_in.length;
774                 p_tok_in = &tok_in;
775         }
776
777         ret = gss_init_sec_context(&min,
778                                 GSS_C_NO_CREDENTIAL, /* Use our default cred. */
779                                 &es->s.gss_state->gss_ctx,
780                                 srv_name,
781                                 GSS_C_NO_OID, /* default OID. */
782                                 GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG | GSS_C_DELEG_FLAG,
783                                 GSS_C_INDEFINITE,       /* requested ticket lifetime. */
784                                 NULL,   /* no channel bindings */
785                                 p_tok_in,
786                                 NULL,   /* ignore mech type */
787                                 &tok_out,
788                                 &ret_flags,
789                                 NULL);  /* ignore time_rec */
790
791         status = map_nt_error_from_gss(ret, min);
792         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
793                 ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
794                 DEBUG(10,("make_cli_gss_blob: gss_init_sec_context failed with %s\n",
795                         ads_errstr(adss)));
796                 goto fail;
797         }
798
799         if ((ret_flags & SMB_GSS_REQUIRED_FLAGS) != SMB_GSS_REQUIRED_FLAGS) {
800                 status = NT_STATUS_ACCESS_DENIED;
801         }
802
803         blob_out = data_blob(tok_out.value, tok_out.length);
804
805         /* Wrap in an SPNEGO wrapper */
806         *p_blob_out = gen_negTokenTarg(krb_mechs, blob_out);
807
808   fail:
809
810         data_blob_free(&blob_out);
811         data_blob_free(&blob_in);
812         SAFE_FREE(host_princ_s);
813         gss_release_name(&min, &srv_name);
814         if (tok_out.value) {
815                 gss_release_buffer(&min, &tok_out);
816         }
817         return status;
818 }
819
820 /******************************************************************************
821  Start a SPNEGO gssapi encryption context.
822 ******************************************************************************/
823
824 NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
825 {
826         DATA_BLOB blob_recv = data_blob_null;
827         DATA_BLOB blob_send = data_blob_null;
828         DATA_BLOB param_out = data_blob_null;
829         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
830         fstring fqdn;
831         const char *servicename;
832         struct smb_trans_enc_state *es = make_cli_enc_state(SMB_TRANS_ENC_GSS);
833
834         if (!es) {
835                 return NT_STATUS_NO_MEMORY;
836         }
837
838         name_to_fqdn(fqdn, cli->desthost);
839         strlower_m(fqdn);
840
841         servicename = "cifs";
842         status = make_cli_gss_blob(es, servicename, fqdn, NT_STATUS_OK, blob_recv, &blob_send);
843         if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
844                 servicename = "host";
845                 status = make_cli_gss_blob(es, servicename, fqdn, NT_STATUS_OK, blob_recv, &blob_send);
846                 if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
847                         goto fail;
848                 }
849         }
850
851         do {
852                 data_blob_free(&blob_recv);
853                 status = enc_blob_send_receive(cli, &blob_send, &blob_recv, &param_out);
854                 if (param_out.length == 2) {
855                         es->enc_ctx_num = SVAL(param_out.data, 0);
856                 }
857                 data_blob_free(&blob_send);
858                 status = make_cli_gss_blob(es, servicename, fqdn, status, blob_recv, &blob_send);
859         } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
860         data_blob_free(&blob_recv);
861
862         if (NT_STATUS_IS_OK(status)) {
863                 /* Replace the old state, if any. */
864                 if (cli->trans_enc_state) {
865                         common_free_encryption_state(&cli->trans_enc_state);
866                 }
867                 cli->trans_enc_state = es;
868                 cli->trans_enc_state->enc_on = True;
869                 es = NULL;
870         }
871
872   fail:
873
874         common_free_encryption_state(&es);
875         return status;
876 }
877 #else
878 NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
879 {
880         return NT_STATUS_NOT_SUPPORTED;
881 }
882 #endif
883
884 /********************************************************************
885  Ensure a connection is encrypted.
886 ********************************************************************/
887
888 NTSTATUS cli_force_encryption(struct cli_state *c,
889                         const char *username,
890                         const char *password,
891                         const char *domain)
892 {
893         uint16 major, minor;
894         uint32 caplow, caphigh;
895         NTSTATUS status;
896
897         if (!SERVER_HAS_UNIX_CIFS(c)) {
898                 return NT_STATUS_NOT_SUPPORTED;
899         }
900
901         status = cli_unix_extensions_version(c, &major, &minor, &caplow,
902                                              &caphigh);
903         if (!NT_STATUS_IS_OK(status)) {
904                 DEBUG(10, ("cli_force_encryption: cli_unix_extensions_version "
905                            "returned %s\n", nt_errstr(status)));
906                 return NT_STATUS_UNKNOWN_REVISION;
907         }
908
909         if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
910                 return NT_STATUS_UNSUPPORTED_COMPRESSION;
911         }
912
913         if (c->use_kerberos) {
914                 return cli_gss_smb_encryption_start(c);
915         }
916         return cli_raw_ntlm_smb_encryption_start(c,
917                                         username,
918                                         password,
919                                         domain);
920 }