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