s3:libsmb: fix memory leak in cli_raw_ntlm_smb_encryption_start()
[metze/samba/wip.git] / source3 / libsmb / clifsinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3    FS info functions
4    Copyright (C) Stefan (metze) Metzmacher      2003
5    Copyright (C) Jeremy Allison 2007
6    Copyright (C) Andrew Bartlett 2011
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "../libcli/smb/smb_seal.h"
27 #include "trans2.h"
28 #include "auth_generic.h"
29 #include "auth/gensec/gensec.h"
30 #include "../libcli/smb/smbXcli_base.h"
31 #include "auth/credentials/credentials.h"
32 #include "../librpc/gen_ndr/ndr_security.h"
33
34 /****************************************************************************
35  Get UNIX extensions version info.
36 ****************************************************************************/
37
38 struct cli_unix_extensions_version_state {
39         struct cli_state *cli;
40         uint16_t setup[1];
41         uint8_t param[2];
42         uint16_t major, minor;
43         uint32_t caplow, caphigh;
44 };
45
46 static void cli_unix_extensions_version_done(struct tevent_req *subreq);
47
48 struct tevent_req *cli_unix_extensions_version_send(TALLOC_CTX *mem_ctx,
49                                                     struct tevent_context *ev,
50                                                     struct cli_state *cli)
51 {
52         struct tevent_req *req, *subreq;
53         struct cli_unix_extensions_version_state *state;
54
55         req = tevent_req_create(mem_ctx, &state,
56                                 struct cli_unix_extensions_version_state);
57         if (req == NULL) {
58                 return NULL;
59         }
60         state->cli = cli;
61         SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
62         SSVAL(state->param, 0, SMB_QUERY_CIFS_UNIX_INFO);
63
64         subreq = cli_trans_send(state, ev, cli, 0, SMBtrans2,
65                                 NULL, 0, 0, 0,
66                                 state->setup, 1, 0,
67                                 state->param, 2, 0,
68                                 NULL, 0, 560);
69         if (tevent_req_nomem(subreq, req)) {
70                 return tevent_req_post(req, ev);
71         }
72         tevent_req_set_callback(subreq, cli_unix_extensions_version_done, req);
73         return req;
74 }
75
76 static void cli_unix_extensions_version_done(struct tevent_req *subreq)
77 {
78         struct tevent_req *req = tevent_req_callback_data(
79                 subreq, struct tevent_req);
80         struct cli_unix_extensions_version_state *state = tevent_req_data(
81                 req, struct cli_unix_extensions_version_state);
82         uint8_t *data;
83         uint32_t num_data;
84         NTSTATUS status;
85
86         status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
87                                 NULL, 0, NULL, &data, 12, &num_data);
88         TALLOC_FREE(subreq);
89         if (!NT_STATUS_IS_OK(status)) {
90                 tevent_req_nterror(req, status);
91                 return;
92         }
93
94         state->major = SVAL(data, 0);
95         state->minor = SVAL(data, 2);
96         state->caplow = IVAL(data, 4);
97         state->caphigh = IVAL(data, 8);
98         TALLOC_FREE(data);
99         tevent_req_done(req);
100 }
101
102 NTSTATUS cli_unix_extensions_version_recv(struct tevent_req *req,
103                                           uint16_t *pmajor, uint16_t *pminor,
104                                           uint32_t *pcaplow,
105                                           uint32_t *pcaphigh)
106 {
107         struct cli_unix_extensions_version_state *state = tevent_req_data(
108                 req, struct cli_unix_extensions_version_state);
109         NTSTATUS status;
110
111         if (tevent_req_is_nterror(req, &status)) {
112                 return status;
113         }
114         *pmajor = state->major;
115         *pminor = state->minor;
116         *pcaplow = state->caplow;
117         *pcaphigh = state->caphigh;
118         state->cli->server_posix_capabilities = *pcaplow;
119         return NT_STATUS_OK;
120 }
121
122 NTSTATUS cli_unix_extensions_version(struct cli_state *cli, uint16_t *pmajor,
123                                      uint16_t *pminor, uint32_t *pcaplow,
124                                      uint32_t *pcaphigh)
125 {
126         TALLOC_CTX *frame = talloc_stackframe();
127         struct tevent_context *ev;
128         struct tevent_req *req;
129         NTSTATUS status = NT_STATUS_OK;
130
131         if (smbXcli_conn_has_async_calls(cli->conn)) {
132                 /*
133                  * Can't use sync call while an async call is in flight
134                  */
135                 status = NT_STATUS_INVALID_PARAMETER;
136                 goto fail;
137         }
138
139         ev = samba_tevent_context_init(frame);
140         if (ev == NULL) {
141                 status = NT_STATUS_NO_MEMORY;
142                 goto fail;
143         }
144
145         req = cli_unix_extensions_version_send(frame, ev, cli);
146         if (req == NULL) {
147                 status = NT_STATUS_NO_MEMORY;
148                 goto fail;
149         }
150
151         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
152                 goto fail;
153         }
154
155         status = cli_unix_extensions_version_recv(req, pmajor, pminor, pcaplow,
156                                                   pcaphigh);
157  fail:
158         TALLOC_FREE(frame);
159         return status;
160 }
161
162 /****************************************************************************
163  Set UNIX extensions capabilities.
164 ****************************************************************************/
165
166 struct cli_set_unix_extensions_capabilities_state {
167         struct cli_state *cli;
168         uint16_t setup[1];
169         uint8_t param[4];
170         uint8_t data[12];
171 };
172
173 static void cli_set_unix_extensions_capabilities_done(
174         struct tevent_req *subreq);
175
176 struct tevent_req *cli_set_unix_extensions_capabilities_send(
177         TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
178         uint16_t major, uint16_t minor, uint32_t caplow, uint32_t caphigh)
179 {
180         struct tevent_req *req, *subreq;
181         struct cli_set_unix_extensions_capabilities_state *state;
182
183         req = tevent_req_create(
184                 mem_ctx, &state,
185                 struct cli_set_unix_extensions_capabilities_state);
186         if (req == NULL) {
187                 return NULL;
188         }
189
190         state->cli = cli;
191         SSVAL(state->setup+0, 0, TRANSACT2_SETFSINFO);
192
193         SSVAL(state->param, 0, 0);
194         SSVAL(state->param, 2, SMB_SET_CIFS_UNIX_INFO);
195
196         SSVAL(state->data, 0, major);
197         SSVAL(state->data, 2, minor);
198         SIVAL(state->data, 4, caplow);
199         SIVAL(state->data, 8, caphigh);
200
201         subreq = cli_trans_send(state, ev, cli, 0, SMBtrans2,
202                                 NULL, 0, 0, 0,
203                                 state->setup, 1, 0,
204                                 state->param, 4, 0,
205                                 state->data, 12, 560);
206         if (tevent_req_nomem(subreq, req)) {
207                 return tevent_req_post(req, ev);
208         }
209         tevent_req_set_callback(
210                 subreq, cli_set_unix_extensions_capabilities_done, req);
211         return req;
212 }
213
214 static void cli_set_unix_extensions_capabilities_done(
215         struct tevent_req *subreq)
216 {
217         struct tevent_req *req = tevent_req_callback_data(
218                 subreq, struct tevent_req);
219         struct cli_set_unix_extensions_capabilities_state *state = tevent_req_data(
220                 req, struct cli_set_unix_extensions_capabilities_state);
221
222         NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
223                                          NULL, 0, NULL, NULL, 0, NULL);
224         if (NT_STATUS_IS_OK(status)) {
225                 state->cli->requested_posix_capabilities = IVAL(state->data, 4);
226         }
227         tevent_req_simple_finish_ntstatus(subreq, status);
228 }
229
230 NTSTATUS cli_set_unix_extensions_capabilities_recv(struct tevent_req *req)
231 {
232         return tevent_req_simple_recv_ntstatus(req);
233 }
234
235 NTSTATUS cli_set_unix_extensions_capabilities(struct cli_state *cli,
236                                               uint16_t major, uint16_t minor,
237                                               uint32_t caplow, uint32_t caphigh)
238 {
239         struct tevent_context *ev;
240         struct tevent_req *req;
241         NTSTATUS status = NT_STATUS_NO_MEMORY;
242
243         if (smbXcli_conn_has_async_calls(cli->conn)) {
244                 return NT_STATUS_INVALID_PARAMETER;
245         }
246         ev = samba_tevent_context_init(talloc_tos());
247         if (ev == NULL) {
248                 goto fail;
249         }
250         req = cli_set_unix_extensions_capabilities_send(
251                 ev, ev, cli, major, minor, caplow, caphigh);
252         if (req == NULL) {
253                 goto fail;
254         }
255         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
256                 goto fail;
257         }
258         status = cli_set_unix_extensions_capabilities_recv(req);
259 fail:
260         TALLOC_FREE(ev);
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, 0, 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, 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 (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
341                 return cli_smb2_get_fs_attr_info(cli, fs_attr);
342         }
343
344         if (smbXcli_conn_has_async_calls(cli->conn)) {
345                 return NT_STATUS_INVALID_PARAMETER;
346         }
347         ev = samba_tevent_context_init(talloc_tos());
348         if (ev == NULL) {
349                 goto fail;
350         }
351         req = cli_get_fs_attr_info_send(ev, ev, cli);
352         if (req == NULL) {
353                 goto fail;
354         }
355         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
356                 goto fail;
357         }
358         status = cli_get_fs_attr_info_recv(req, fs_attr);
359 fail:
360         TALLOC_FREE(ev);
361         return status;
362 }
363
364 NTSTATUS cli_get_fs_volume_info(struct cli_state *cli,
365                                 TALLOC_CTX *mem_ctx,
366                                 char **_volume_name,
367                                 uint32_t *pserial_number,
368                                 time_t *pdate)
369 {
370         NTSTATUS status;
371         uint16_t recv_flags2;
372         uint16_t setup[1];
373         uint8_t param[2];
374         uint8_t *rdata;
375         uint32_t rdata_count;
376         unsigned int nlen;
377         char *volume_name = NULL;
378
379         SSVAL(setup, 0, TRANSACT2_QFSINFO);
380         SSVAL(param,0,SMB_QUERY_FS_VOLUME_INFO);
381
382         status = cli_trans(talloc_tos(), cli, SMBtrans2,
383                            NULL, 0, 0, 0,
384                            setup, 1, 0,
385                            param, 2, 0,
386                            NULL, 0, 560,
387                            &recv_flags2,
388                            NULL, 0, NULL,
389                            NULL, 0, NULL,
390                            &rdata, 18, &rdata_count);
391         if (!NT_STATUS_IS_OK(status)) {
392                 return status;
393         }
394
395         if (pdate) {
396                 struct timespec ts;
397                 ts = interpret_long_date((char *)rdata);
398                 *pdate = ts.tv_sec;
399         }
400         if (pserial_number) {
401                 *pserial_number = IVAL(rdata,8);
402         }
403         nlen = IVAL(rdata,12);
404         if (nlen > (rdata_count - 18)) {
405                 TALLOC_FREE(rdata);
406                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
407         }
408
409         clistr_pull_talloc(mem_ctx,
410                            (const char *)rdata,
411                            recv_flags2,
412                            &volume_name,
413                            rdata + 18,
414                            nlen, STR_UNICODE);
415         if (volume_name == NULL) {
416                 status = map_nt_error_from_unix(errno);
417                 TALLOC_FREE(rdata);
418                 return status;
419         }
420
421         /* todo: but not yet needed
422          *       return the other stuff
423          */
424
425         *_volume_name = volume_name;
426         TALLOC_FREE(rdata);
427         return NT_STATUS_OK;
428 }
429
430 NTSTATUS cli_get_fs_full_size_info(struct cli_state *cli,
431                                    uint64_t *total_allocation_units,
432                                    uint64_t *caller_allocation_units,
433                                    uint64_t *actual_allocation_units,
434                                    uint64_t *sectors_per_allocation_unit,
435                                    uint64_t *bytes_per_sector)
436 {
437         uint16_t setup[1];
438         uint8_t param[2];
439         uint8_t *rdata = NULL;
440         uint32_t rdata_count;
441         NTSTATUS status;
442
443         SSVAL(setup, 0, TRANSACT2_QFSINFO);
444         SSVAL(param, 0, SMB_FS_FULL_SIZE_INFORMATION);
445
446         status = cli_trans(talloc_tos(), cli, SMBtrans2,
447                            NULL, 0, 0, 0,
448                            setup, 1, 0, /* setup */
449                            param, 2, 0,  /* param */
450                            NULL, 0, 560, /* data */
451                            NULL,
452                            NULL, 0, NULL, /* rsetup */
453                            NULL, 0, NULL, /* rparam */
454                            &rdata, 32, &rdata_count);  /* rdata */
455         if (!NT_STATUS_IS_OK(status)) {
456                 goto fail;
457         }
458
459         if (total_allocation_units) {
460                 *total_allocation_units = BIG_UINT(rdata, 0);
461         }
462         if (caller_allocation_units) {
463                 *caller_allocation_units = BIG_UINT(rdata,8);
464         }
465         if (actual_allocation_units) {
466                 *actual_allocation_units = BIG_UINT(rdata,16);
467         }
468         if (sectors_per_allocation_unit) {
469                 *sectors_per_allocation_unit = IVAL(rdata,24);
470         }
471         if (bytes_per_sector) {
472                 *bytes_per_sector = IVAL(rdata,28);
473         }
474
475 fail:
476         TALLOC_FREE(rdata);
477         return status;
478 }
479
480 NTSTATUS cli_get_posix_fs_info(struct cli_state *cli,
481                                uint32_t *optimal_transfer_size,
482                                uint32_t *block_size,
483                                uint64_t *total_blocks,
484                                uint64_t *blocks_available,
485                                uint64_t *user_blocks_available,
486                                uint64_t *total_file_nodes,
487                                uint64_t *free_file_nodes,
488                                uint64_t *fs_identifier)
489 {
490         uint16_t setup[1];
491         uint8_t param[2];
492         uint8_t *rdata = NULL;
493         uint32_t rdata_count;
494         NTSTATUS status;
495
496         SSVAL(setup, 0, TRANSACT2_QFSINFO);
497         SSVAL(param,0,SMB_QUERY_POSIX_FS_INFO);
498
499         status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, 0, 0, 0,
500                            setup, 1, 0,
501                            param, 2, 0,
502                            NULL, 0, 560,
503                            NULL,
504                            NULL, 0, NULL, /* rsetup */
505                            NULL, 0, NULL, /* rparam */
506                            &rdata, 56, &rdata_count);
507         if (!NT_STATUS_IS_OK(status)) {
508                 return status;
509         }
510
511         if (optimal_transfer_size) {
512                 *optimal_transfer_size = IVAL(rdata, 0);
513         }
514         if (block_size) {
515                 *block_size = IVAL(rdata,4);
516         }
517         if (total_blocks) {
518                 *total_blocks = BIG_UINT(rdata,8);
519         }
520         if (blocks_available) {
521                 *blocks_available = BIG_UINT(rdata,16);
522         }
523         if (user_blocks_available) {
524                 *user_blocks_available = BIG_UINT(rdata,24);
525         }
526         if (total_file_nodes) {
527                 *total_file_nodes = BIG_UINT(rdata,32);
528         }
529         if (free_file_nodes) {
530                 *free_file_nodes = BIG_UINT(rdata,40);
531         }
532         if (fs_identifier) {
533                 *fs_identifier = BIG_UINT(rdata,48);
534         }
535         return NT_STATUS_OK;
536 }
537
538
539 /******************************************************************************
540  Send/receive the request encryption blob.
541 ******************************************************************************/
542
543 static NTSTATUS enc_blob_send_receive(struct cli_state *cli, DATA_BLOB *in, DATA_BLOB *out, DATA_BLOB *param_out)
544 {
545         uint16_t setup[1];
546         uint8_t param[4];
547         uint8_t *rparam=NULL, *rdata=NULL;
548         uint32_t num_rparam, num_rdata;
549         NTSTATUS status;
550
551         SSVAL(setup+0, 0, TRANSACT2_SETFSINFO);
552         SSVAL(param,0,0);
553         SSVAL(param,2,SMB_REQUEST_TRANSPORT_ENCRYPTION);
554
555         status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, 0, 0, 0,
556                            setup, 1, 0,
557                            param, 4, 2,
558                            (uint8_t *)in->data, in->length, CLI_BUFFER_SIZE,
559                            NULL,          /* recv_flags */
560                            NULL, 0, NULL, /* rsetup */
561                            &rparam, 0, &num_rparam,
562                            &rdata, 0, &num_rdata);
563
564         if (!NT_STATUS_IS_OK(status) &&
565             !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
566                 return status;
567         }
568
569         *out = data_blob(rdata, num_rdata);
570         *param_out = data_blob(rparam, num_rparam);
571
572         TALLOC_FREE(rparam);
573         TALLOC_FREE(rdata);
574         return status;
575 }
576
577 /******************************************************************************
578  Start a raw ntlmssp encryption.
579 ******************************************************************************/
580
581 NTSTATUS cli_raw_ntlm_smb_encryption_start(struct cli_state *cli, 
582                                 const char *user,
583                                 const char *pass,
584                                 const char *domain)
585 {
586         DATA_BLOB blob_in = data_blob_null;
587         DATA_BLOB blob_out = data_blob_null;
588         DATA_BLOB param_out = data_blob_null;
589         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
590         struct auth_generic_state *auth_generic_state;
591         struct smb_trans_enc_state *es = talloc_zero(NULL, struct smb_trans_enc_state);
592         if (!es) {
593                 return NT_STATUS_NO_MEMORY;
594         }
595         status = auth_generic_client_prepare(es,
596                                              &auth_generic_state);
597         if (!NT_STATUS_IS_OK(status)) {
598                 goto fail;
599         }
600
601         gensec_want_feature(auth_generic_state->gensec_security, GENSEC_FEATURE_SESSION_KEY);
602         gensec_want_feature(auth_generic_state->gensec_security, GENSEC_FEATURE_SEAL);
603
604         if (!NT_STATUS_IS_OK(status = auth_generic_set_username(auth_generic_state, user))) {
605                 goto fail;
606         }
607         if (!NT_STATUS_IS_OK(status = auth_generic_set_domain(auth_generic_state, domain))) {
608                 goto fail;
609         }
610         if (!NT_STATUS_IS_OK(status = auth_generic_set_password(auth_generic_state, pass))) {
611                 goto fail;
612         }
613
614         if (!NT_STATUS_IS_OK(status = auth_generic_client_start(auth_generic_state, GENSEC_OID_NTLMSSP))) {
615                 goto fail;
616         }
617
618         do {
619                 status = gensec_update(auth_generic_state->gensec_security, auth_generic_state,
620                                        blob_in, &blob_out);
621                 data_blob_free(&blob_in);
622                 data_blob_free(&param_out);
623                 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) || NT_STATUS_IS_OK(status)) {
624                         NTSTATUS trans_status = enc_blob_send_receive(cli,
625                                                                         &blob_out,
626                                                                         &blob_in,
627                                                                         &param_out);
628                         if (!NT_STATUS_EQUAL(trans_status,
629                                         NT_STATUS_MORE_PROCESSING_REQUIRED) &&
630                                         !NT_STATUS_IS_OK(trans_status)) {
631                                 status = trans_status;
632                         } else {
633                                 if (param_out.length == 2) {
634                                         es->enc_ctx_num = SVAL(param_out.data, 0);
635                                 }
636                         }
637                 }
638                 data_blob_free(&blob_out);
639         } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
640
641         data_blob_free(&blob_in);
642
643         if (NT_STATUS_IS_OK(status)) {
644                 es->enc_on = true;
645                 /*
646                  * Replace the old state, if any.
647                  * We only need the gensec_security part from here.
648                  */
649                 es->gensec_security = talloc_move(es,
650                                                   &auth_generic_state->gensec_security);
651                 smb1cli_conn_set_encryption(cli->conn, es);
652                 es = NULL;
653         }
654
655   fail:
656         TALLOC_FREE(es);
657         return status;
658 }
659
660 /******************************************************************************
661  Start a SPNEGO gssapi encryption context.
662 ******************************************************************************/
663
664 NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
665 {
666         DATA_BLOB blob_recv = data_blob_null;
667         DATA_BLOB blob_send = data_blob_null;
668         DATA_BLOB param_out = data_blob_null;
669         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
670         struct auth_generic_state *auth_generic_state;
671         struct smb_trans_enc_state *es = talloc_zero(NULL, struct smb_trans_enc_state);
672
673         if (!es) {
674                 return NT_STATUS_NO_MEMORY;
675         }
676
677         status = auth_generic_client_prepare(es,
678                                              &auth_generic_state);
679         if (!NT_STATUS_IS_OK(status)) {
680                 goto fail;
681         }
682
683         gensec_want_feature(auth_generic_state->gensec_security, GENSEC_FEATURE_SESSION_KEY);
684         gensec_want_feature(auth_generic_state->gensec_security, GENSEC_FEATURE_SEAL);
685
686         cli_credentials_set_kerberos_state(auth_generic_state->credentials, 
687                                            CRED_MUST_USE_KERBEROS);
688
689         status = gensec_set_target_service(auth_generic_state->gensec_security, "cifs");
690         if (!NT_STATUS_IS_OK(status)) {
691                 goto fail;
692         }
693
694         status = gensec_set_target_hostname(auth_generic_state->gensec_security, 
695                                             smbXcli_conn_remote_name(cli->conn));
696         if (!NT_STATUS_IS_OK(status)) {
697                 goto fail;
698         }
699
700         if (!NT_STATUS_IS_OK(status = auth_generic_client_start(auth_generic_state, GENSEC_OID_SPNEGO))) {
701                 goto fail;
702         }
703
704         status = gensec_update(auth_generic_state->gensec_security, talloc_tos(),
705                                blob_recv, &blob_send);
706
707         do {
708                 data_blob_free(&blob_recv);
709                 status = enc_blob_send_receive(cli, &blob_send, &blob_recv, &param_out);
710                 if (param_out.length == 2) {
711                         es->enc_ctx_num = SVAL(param_out.data, 0);
712                 }
713                 data_blob_free(&blob_send);
714                 status = gensec_update(auth_generic_state->gensec_security, talloc_tos(),
715                                        blob_recv, &blob_send);
716         } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
717         data_blob_free(&blob_recv);
718
719         if (NT_STATUS_IS_OK(status)) {
720                 if (!gensec_have_feature(auth_generic_state->gensec_security, 
721                                          GENSEC_FEATURE_SIGN) ||
722                     !gensec_have_feature(auth_generic_state->gensec_security, 
723                                          GENSEC_FEATURE_SEAL)) {
724                         status = NT_STATUS_ACCESS_DENIED;
725                 }
726         }
727
728         if (NT_STATUS_IS_OK(status)) {
729                 es->enc_on = true;
730                 /*
731                  * Replace the old state, if any.
732                  * We only need the gensec_security part from here.
733                  */
734                 es->gensec_security = talloc_move(es,
735                                                   &auth_generic_state->gensec_security);
736                 smb1cli_conn_set_encryption(cli->conn, es);
737                 es = NULL;
738         }
739 fail:
740         TALLOC_FREE(es);
741         return status;
742 }
743
744 /********************************************************************
745  Ensure a connection is encrypted.
746 ********************************************************************/
747
748 NTSTATUS cli_force_encryption(struct cli_state *c,
749                         const char *username,
750                         const char *password,
751                         const char *domain)
752 {
753         uint16_t major, minor;
754         uint32_t caplow, caphigh;
755         NTSTATUS status;
756
757         if (!SERVER_HAS_UNIX_CIFS(c)) {
758                 return NT_STATUS_NOT_SUPPORTED;
759         }
760
761         status = cli_unix_extensions_version(c, &major, &minor, &caplow,
762                                              &caphigh);
763         if (!NT_STATUS_IS_OK(status)) {
764                 DEBUG(10, ("cli_force_encryption: cli_unix_extensions_version "
765                            "returned %s\n", nt_errstr(status)));
766                 return NT_STATUS_UNKNOWN_REVISION;
767         }
768
769         if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
770                 return NT_STATUS_UNSUPPORTED_COMPRESSION;
771         }
772
773         if (c->use_kerberos) {
774                 return cli_gss_smb_encryption_start(c);
775         }
776         return cli_raw_ntlm_smb_encryption_start(c,
777                                         username,
778                                         password,
779                                         domain);
780 }
781
782 /****************************************************************************
783  Do a UNIX extensions SMB_QUERY_POSIX_WHOAMI call.
784 ****************************************************************************/
785
786 struct posix_whoami_state {
787         uint16_t setup[1];
788         uint8_t param[2];
789         uint32_t max_rdata;
790         bool guest;
791         uint64_t uid;
792         uint64_t gid;
793         uint32_t num_gids;
794         uint64_t *gids;
795         uint32_t num_sids;
796         struct dom_sid *sids;
797 };
798
799 static void cli_posix_whoami_done(struct tevent_req *subreq);
800
801 struct tevent_req *cli_posix_whoami_send(TALLOC_CTX *mem_ctx,
802                                         struct tevent_context *ev,
803                                         struct cli_state *cli)
804 {
805         struct tevent_req *req = NULL, *subreq = NULL;
806         struct posix_whoami_state *state = NULL;
807
808         req = tevent_req_create(mem_ctx, &state, struct posix_whoami_state);
809         if (req == NULL) {
810                 return NULL;
811         }
812
813         /* Setup setup word. */
814         SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
815         SSVAL(state->param, 0, SMB_QUERY_POSIX_WHOAMI);
816
817         state->max_rdata = 62*1024;
818
819         subreq = cli_trans_send(state,                  /* mem ctx. */
820                                 ev,                     /* event ctx. */
821                                 cli,                    /* cli_state. */
822                                 0,                      /* additional_flags2 */
823                                 SMBtrans2,              /* cmd. */
824                                 NULL,                   /* pipe name. */
825                                 -1,                     /* fid. */
826                                 0,                      /* function. */
827                                 0,                      /* flags. */
828                                 state->setup,           /* setup. */
829                                 1,                      /* num setup uint16_t words. */
830                                 0,                      /* max returned setup. */
831                                 state->param,           /* param. */
832                                 2,                      /* num param. */
833                                 0,                      /* max returned param. */
834                                 NULL,                   /* data. */
835                                 0,                      /* num data. */
836                                 state->max_rdata);      /* max returned data. */
837
838         if (tevent_req_nomem(subreq, req)) {
839                 return tevent_req_post(req, ev);
840         }
841         tevent_req_set_callback(subreq, cli_posix_whoami_done, req);
842         return req;
843 }
844
845 static void cli_posix_whoami_done(struct tevent_req *subreq)
846 {
847         struct tevent_req *req = tevent_req_callback_data(
848                         subreq, struct tevent_req);
849         struct posix_whoami_state *state = tevent_req_data(
850                         req, struct posix_whoami_state);
851         uint8_t *rdata = NULL;
852         uint8_t *p = NULL;
853         uint32_t num_rdata = 0;
854         uint32_t i;
855         NTSTATUS status;
856
857         status = cli_trans_recv(subreq,
858                                 state,
859                                 NULL,
860                                 NULL,
861                                 0,
862                                 NULL,
863                                 NULL,
864                                 0,
865                                 NULL,
866                                 &rdata,
867                                 40,
868                                 &num_rdata);
869         TALLOC_FREE(subreq);
870         if (tevent_req_nterror(req, status)) {
871                 return;
872         }
873
874         /*
875          * Not strictly needed - cli_trans_recv()
876          * will ensure at least 40 bytes here. Added
877          * as more of a reminder to be careful when
878          * parsing network packets in C.
879          */
880
881         if (num_rdata < 40 || rdata + num_rdata < rdata) {
882                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
883                 return;
884         }
885
886         state->guest = (IVAL(rdata, 0) & SMB_WHOAMI_GUEST);
887         state->uid = BVAL(rdata, 8);
888         state->gid = BVAL(rdata, 16);
889         state->num_gids = IVAL(rdata, 24);
890         state->num_sids = IVAL(rdata, 28);
891
892         state->gids = talloc_array(state, uint64_t, state->num_gids);
893         if (tevent_req_nomem(state->gids, req)) {
894                 return;
895         }
896         state->sids = talloc_array(state, struct dom_sid, state->num_sids);
897         if (tevent_req_nomem(state->sids, req)) {
898                 return;
899         }
900
901         p = rdata + 40;
902
903         for (i = 0; i < state->num_gids; i++) {
904                 if (p + 8 > rdata + num_rdata) {
905                         tevent_req_nterror(req,
906                                 NT_STATUS_INVALID_NETWORK_RESPONSE);
907                         return;
908                 }
909                 state->gids[i] = BVAL(p, 0);
910                 p += 8;
911         }
912
913         num_rdata -= (p - rdata);
914
915         for (i = 0; i < state->num_sids; i++) {
916                 size_t sid_size;
917                 DATA_BLOB in = data_blob_const(p, num_rdata);
918                 enum ndr_err_code ndr_err;
919
920                 ndr_err = ndr_pull_struct_blob(&in,
921                                 state,
922                                 &state->sids[i],
923                                 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
924                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
925                         tevent_req_nterror(req,
926                                 NT_STATUS_INVALID_NETWORK_RESPONSE);
927                         return;
928                 }
929
930                 sid_size = ndr_size_dom_sid(&state->sids[i], 0);
931
932                 if (sid_size > num_rdata) {
933                         tevent_req_nterror(req,
934                                 NT_STATUS_INVALID_NETWORK_RESPONSE);
935                         return;
936                 }
937
938                 p += sid_size;
939                 num_rdata -= sid_size;
940         }
941         tevent_req_done(req);
942 }
943
944 NTSTATUS cli_posix_whoami_recv(struct tevent_req *req,
945                         TALLOC_CTX *mem_ctx,
946                         uint64_t *puid,
947                         uint64_t *pgid,
948                         uint32_t *pnum_gids,
949                         uint64_t **pgids,
950                         uint32_t *pnum_sids,
951                         struct dom_sid **psids,
952                         bool *pguest)
953 {
954         NTSTATUS status;
955         struct posix_whoami_state *state = tevent_req_data(
956                         req, struct posix_whoami_state);
957
958         if (tevent_req_is_nterror(req, &status)) {
959                 return status;
960         }
961
962         if (puid) {
963                 *puid = state->uid;
964         }
965         if (pgid) {
966                 *pgid = state->gid;
967         }
968         if (pnum_gids) {
969                 *pnum_gids = state->num_gids;
970         }
971         if (pgids) {
972                 *pgids = talloc_move(mem_ctx, &state->gids);
973         }
974         if (pnum_sids) {
975                 *pnum_sids = state->num_sids;
976         }
977         if (psids) {
978                 *psids = talloc_move(mem_ctx, &state->sids);
979         }
980         if (pguest) {
981                 *pguest = state->guest;
982         }
983         return NT_STATUS_OK;
984 }
985
986 NTSTATUS cli_posix_whoami(struct cli_state *cli,
987                         TALLOC_CTX *mem_ctx,
988                         uint64_t *puid,
989                         uint64_t *pgid,
990                         uint32_t *num_gids,
991                         uint64_t **gids,
992                         uint32_t *num_sids,
993                         struct dom_sid **sids,
994                         bool *pguest)
995 {
996         TALLOC_CTX *frame = talloc_stackframe();
997         struct tevent_context *ev = NULL;
998         struct tevent_req *req = NULL;
999         NTSTATUS status = NT_STATUS_OK;
1000
1001         if (smbXcli_conn_has_async_calls(cli->conn)) {
1002                 /*
1003                  * Can't use sync call while an async call is in flight
1004                  */
1005                 status = NT_STATUS_INVALID_PARAMETER;
1006                 goto fail;
1007         }
1008
1009         ev = samba_tevent_context_init(frame);
1010         if (ev == NULL) {
1011                 status = NT_STATUS_NO_MEMORY;
1012                 goto fail;
1013         }
1014
1015         req = cli_posix_whoami_send(frame,
1016                                 ev,
1017                                 cli);
1018         if (req == NULL) {
1019                 status = NT_STATUS_NO_MEMORY;
1020                 goto fail;
1021         }
1022
1023         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1024                 goto fail;
1025         }
1026
1027         status = cli_posix_whoami_recv(req,
1028                         mem_ctx,
1029                         puid,
1030                         pgid,
1031                         num_gids,
1032                         gids,
1033                         num_sids,
1034                         sids,
1035                         pguest);
1036
1037  fail:
1038         TALLOC_FREE(frame);
1039         return status;
1040 }