s3-libsmb: Support getting fs attributes via SMB2
[metze/samba/wip.git] / source3 / libsmb / clifsinfo.c
index 9e4d9fa8ad9240f52857a885415294b7e4fff0bf..31a2c0222b2d89dd8e446d7c13c8d2af3b85ea4b 100644 (file)
@@ -3,6 +3,7 @@
    FS info functions
    Copyright (C) Stefan (metze) Metzmacher     2003
    Copyright (C) Jeremy Allison 2007
+   Copyright (C) Andrew Bartlett 2011
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 */
 
 #include "includes.h"
-#include "../libcli/auth/spnego.h"
-#include "../libcli/auth/ntlmssp.h"
+#include "libsmb/libsmb.h"
+#include "../lib/util/tevent_ntstatus.h"
+#include "async_smb.h"
+#include "../libcli/smb/smb_seal.h"
+#include "trans2.h"
+#include "auth_generic.h"
+#include "auth/gensec/gensec.h"
+#include "../libcli/smb/smbXcli_base.h"
+#include "auth/credentials/credentials.h"
+#include "../librpc/gen_ndr/ndr_security.h"
 
 /****************************************************************************
  Get UNIX extensions version info.
@@ -52,7 +61,7 @@ struct tevent_req *cli_unix_extensions_version_send(TALLOC_CTX *mem_ctx,
        SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
        SSVAL(state->param, 0, SMB_QUERY_CIFS_UNIX_INFO);
 
-       subreq = cli_trans_send(state, ev, cli, SMBtrans2,
+       subreq = cli_trans_send(state, ev, cli, 0, SMBtrans2,
                                NULL, 0, 0, 0,
                                state->setup, 1, 0,
                                state->param, 2, 0,
@@ -74,8 +83,8 @@ static void cli_unix_extensions_version_done(struct tevent_req *subreq)
        uint32_t num_data;
        NTSTATUS status;
 
-       status = cli_trans_recv(subreq, state, NULL, 0, NULL, NULL, 0, NULL,
-                               &data, 12, &num_data);
+       status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
+                               NULL, 0, NULL, &data, 12, &num_data);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
                tevent_req_nterror(req, status);
@@ -110,16 +119,16 @@ NTSTATUS cli_unix_extensions_version_recv(struct tevent_req *req,
        return NT_STATUS_OK;
 }
 
-NTSTATUS cli_unix_extensions_version(struct cli_state *cli, uint16 *pmajor,
-                                    uint16 *pminor, uint32 *pcaplow,
-                                    uint32 *pcaphigh)
+NTSTATUS cli_unix_extensions_version(struct cli_state *cli, uint16_t *pmajor,
+                                    uint16_t *pminor, uint32_t *pcaplow,
+                                    uint32_t *pcaphigh)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -127,7 +136,7 @@ NTSTATUS cli_unix_extensions_version(struct cli_state *cli, uint16 *pmajor,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -139,8 +148,7 @@ NTSTATUS cli_unix_extensions_version(struct cli_state *cli, uint16 *pmajor,
                goto fail;
        }
 
-       if (!tevent_req_poll(req, ev)) {
-               status = map_nt_error_from_unix(errno);
+       if (!tevent_req_poll_ntstatus(req, ev, &status)) {
                goto fail;
        }
 
@@ -148,9 +156,6 @@ NTSTATUS cli_unix_extensions_version(struct cli_state *cli, uint16 *pmajor,
                                                  pcaphigh);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -193,7 +198,7 @@ struct tevent_req *cli_set_unix_extensions_capabilities_send(
        SIVAL(state->data, 4, caplow);
        SIVAL(state->data, 8, caphigh);
 
-       subreq = cli_trans_send(state, ev, cli, SMBtrans2,
+       subreq = cli_trans_send(state, ev, cli, 0, SMBtrans2,
                                NULL, 0, 0, 0,
                                state->setup, 1, 0,
                                state->param, 4, 0,
@@ -214,7 +219,7 @@ static void cli_set_unix_extensions_capabilities_done(
        struct cli_set_unix_extensions_capabilities_state *state = tevent_req_data(
                req, struct cli_set_unix_extensions_capabilities_state);
 
-       NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, 0, NULL,
+       NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
                                         NULL, 0, NULL, NULL, 0, NULL);
        if (NT_STATUS_IS_OK(status)) {
                state->cli->requested_posix_capabilities = IVAL(state->data, 4);
@@ -228,17 +233,17 @@ NTSTATUS cli_set_unix_extensions_capabilities_recv(struct tevent_req *req)
 }
 
 NTSTATUS cli_set_unix_extensions_capabilities(struct cli_state *cli,
-                                             uint16 major, uint16 minor,
-                                             uint32 caplow, uint32 caphigh)
+                                             uint16_t major, uint16_t minor,
+                                             uint32_t caplow, uint32_t caphigh)
 {
        struct tevent_context *ev;
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_NO_MEMORY;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                return NT_STATUS_INVALID_PARAMETER;
        }
-       ev = tevent_context_init(talloc_tos());
+       ev = samba_tevent_context_init(talloc_tos());
        if (ev == NULL) {
                goto fail;
        }
@@ -253,9 +258,6 @@ NTSTATUS cli_set_unix_extensions_capabilities(struct cli_state *cli,
        status = cli_set_unix_extensions_capabilities_recv(req);
 fail:
        TALLOC_FREE(ev);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -282,7 +284,7 @@ struct tevent_req *cli_get_fs_attr_info_send(TALLOC_CTX *mem_ctx,
        SSVAL(state->setup+0, 0, TRANSACT2_QFSINFO);
        SSVAL(state->param+0, 0, SMB_QUERY_FS_ATTRIBUTE_INFO);
 
-       subreq = cli_trans_send(state, ev, cli, SMBtrans2,
+       subreq = cli_trans_send(state, ev, cli, 0, SMBtrans2,
                                NULL, 0, 0, 0,
                                state->setup, 1, 0,
                                state->param, 2, 0,
@@ -304,7 +306,7 @@ static void cli_get_fs_attr_info_done(struct tevent_req *subreq)
        uint32_t num_data;
        NTSTATUS status;
 
-       status = cli_trans_recv(subreq, talloc_tos(), NULL, 0, NULL,
+       status = cli_trans_recv(subreq, talloc_tos(), NULL, NULL, 0, NULL,
                                NULL, 0, NULL, &data, 12, &num_data);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
@@ -335,10 +337,14 @@ NTSTATUS cli_get_fs_attr_info(struct cli_state *cli, uint32_t *fs_attr)
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_NO_MEMORY;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_get_fs_attr_info(cli, fs_attr);
+       }
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                return NT_STATUS_INVALID_PARAMETER;
        }
-       ev = tevent_context_init(talloc_tos());
+       ev = samba_tevent_context_init(talloc_tos());
        if (ev == NULL) {
                goto fail;
        }
@@ -352,21 +358,23 @@ NTSTATUS cli_get_fs_attr_info(struct cli_state *cli, uint32_t *fs_attr)
        status = cli_get_fs_attr_info_recv(req, fs_attr);
 fail:
        TALLOC_FREE(ev);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
-NTSTATUS cli_get_fs_volume_info(struct cli_state *cli, fstring volume_name,
-                               uint32 *pserial_number, time_t *pdate)
+NTSTATUS cli_get_fs_volume_info(struct cli_state *cli,
+                               TALLOC_CTX *mem_ctx,
+                               char **_volume_name,
+                               uint32_t *pserial_number,
+                               time_t *pdate)
 {
        NTSTATUS status;
-       uint16 setup[1];
+       uint16_t recv_flags2;
+       uint16_t setup[1];
        uint8_t param[2];
        uint8_t *rdata;
        uint32_t rdata_count;
        unsigned int nlen;
+       char *volume_name = NULL;
 
        SSVAL(setup, 0, TRANSACT2_QFSINFO);
        SSVAL(param,0,SMB_QUERY_FS_VOLUME_INFO);
@@ -376,9 +384,10 @@ NTSTATUS cli_get_fs_volume_info(struct cli_state *cli, fstring volume_name,
                           setup, 1, 0,
                           param, 2, 0,
                           NULL, 0, 560,
+                          &recv_flags2,
                           NULL, 0, NULL,
                           NULL, 0, NULL,
-                          &rdata, 10, &rdata_count);
+                          &rdata, 18, &rdata_count);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -392,58 +401,59 @@ NTSTATUS cli_get_fs_volume_info(struct cli_state *cli, fstring volume_name,
                *pserial_number = IVAL(rdata,8);
        }
        nlen = IVAL(rdata,12);
-       clistr_pull(cli->inbuf, volume_name, rdata + 18, sizeof(fstring),
-                   nlen, STR_UNICODE);
+       if (nlen > (rdata_count - 18)) {
+               TALLOC_FREE(rdata);
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       }
+
+       clistr_pull_talloc(mem_ctx,
+                          (const char *)rdata,
+                          recv_flags2,
+                          &volume_name,
+                          rdata + 18,
+                          nlen, STR_UNICODE);
+       if (volume_name == NULL) {
+               status = map_nt_error_from_unix(errno);
+               TALLOC_FREE(rdata);
+               return status;
+       }
 
        /* todo: but not yet needed
         *       return the other stuff
         */
 
+       *_volume_name = volume_name;
        TALLOC_FREE(rdata);
        return NT_STATUS_OK;
 }
 
-bool cli_get_fs_full_size_info(struct cli_state *cli,
-                               uint64_t *total_allocation_units,
-                               uint64_t *caller_allocation_units,
-                               uint64_t *actual_allocation_units,
-                               uint64_t *sectors_per_allocation_unit,
-                               uint64_t *bytes_per_sector)
+NTSTATUS cli_get_fs_full_size_info(struct cli_state *cli,
+                                  uint64_t *total_allocation_units,
+                                  uint64_t *caller_allocation_units,
+                                  uint64_t *actual_allocation_units,
+                                  uint64_t *sectors_per_allocation_unit,
+                                  uint64_t *bytes_per_sector)
 {
-       bool ret = False;
-       uint16 setup;
-       char param[2];
-       char *rparam=NULL, *rdata=NULL;
-       unsigned int rparam_count=0, rdata_count=0;
-
-       setup = TRANSACT2_QFSINFO;
-
-       SSVAL(param,0,SMB_FS_FULL_SIZE_INFORMATION);
-
-       if (!cli_send_trans(cli, SMBtrans2,
-                   NULL,
-                   0, 0,
-                   &setup, 1, 0,
-                   param, 2, 0,
-                   NULL, 0, 560)) {
-               goto cleanup;
-       }
-
-       if (!cli_receive_trans(cli, SMBtrans2,
-                              &rparam, &rparam_count,
-                              &rdata, &rdata_count)) {
-               goto cleanup;
-       }
+       uint16_t setup[1];
+       uint8_t param[2];
+       uint8_t *rdata = NULL;
+       uint32_t rdata_count;
+       NTSTATUS status;
 
-       if (cli_is_error(cli)) {
-               ret = False;
-               goto cleanup;
-       } else {
-               ret = True;
-       }
+       SSVAL(setup, 0, TRANSACT2_QFSINFO);
+       SSVAL(param, 0, SMB_FS_FULL_SIZE_INFORMATION);
 
-       if (rdata_count != 32) {
-               goto cleanup;
+       status = cli_trans(talloc_tos(), cli, SMBtrans2,
+                          NULL, 0, 0, 0,
+                          setup, 1, 0, /* setup */
+                          param, 2, 0,  /* param */
+                          NULL, 0, 560, /* data */
+                          NULL,
+                          NULL, 0, NULL, /* rsetup */
+                          NULL, 0, NULL, /* rparam */
+                          &rdata, 32, &rdata_count);  /* rdata */
+       if (!NT_STATUS_IS_OK(status)) {
+               goto fail;
        }
 
        if (total_allocation_units) {
@@ -462,57 +472,40 @@ bool cli_get_fs_full_size_info(struct cli_state *cli,
                *bytes_per_sector = IVAL(rdata,28);
        }
 
-cleanup:
-       SAFE_FREE(rparam);
-       SAFE_FREE(rdata);
-
-       return ret;
+fail:
+       TALLOC_FREE(rdata);
+       return status;
 }
 
-bool cli_get_posix_fs_info(struct cli_state *cli,
-                           uint32 *optimal_transfer_size,
-                           uint32 *block_size,
-                           uint64_t *total_blocks,
-                           uint64_t *blocks_available,
-                           uint64_t *user_blocks_available,
-                           uint64_t *total_file_nodes,
-                           uint64_t *free_file_nodes,
-                           uint64_t *fs_identifier)
+NTSTATUS cli_get_posix_fs_info(struct cli_state *cli,
+                              uint32_t *optimal_transfer_size,
+                              uint32_t *block_size,
+                              uint64_t *total_blocks,
+                              uint64_t *blocks_available,
+                              uint64_t *user_blocks_available,
+                              uint64_t *total_file_nodes,
+                              uint64_t *free_file_nodes,
+                              uint64_t *fs_identifier)
 {
-       bool ret = False;
-       uint16 setup;
-       char param[2];
-       char *rparam=NULL, *rdata=NULL;
-       unsigned int rparam_count=0, rdata_count=0;
-
-       setup = TRANSACT2_QFSINFO;
+       uint16_t setup[1];
+       uint8_t param[2];
+       uint8_t *rdata = NULL;
+       uint32_t rdata_count;
+       NTSTATUS status;
 
+       SSVAL(setup, 0, TRANSACT2_QFSINFO);
        SSVAL(param,0,SMB_QUERY_POSIX_FS_INFO);
 
-       if (!cli_send_trans(cli, SMBtrans2,
-                   NULL,
-                   0, 0,
-                   &setup, 1, 0,
-                   param, 2, 0,
-                   NULL, 0, 560)) {
-               goto cleanup;
-       }
-
-       if (!cli_receive_trans(cli, SMBtrans2,
-                              &rparam, &rparam_count,
-                              &rdata, &rdata_count)) {
-               goto cleanup;
-       }
-
-       if (cli_is_error(cli)) {
-               ret = False;
-               goto cleanup;
-       } else {
-               ret = True;
-       }
-
-       if (rdata_count != 56) {
-               goto cleanup;
+       status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, 0, 0, 0,
+                          setup, 1, 0,
+                          param, 2, 0,
+                          NULL, 0, 560,
+                          NULL,
+                          NULL, 0, NULL, /* rsetup */
+                          NULL, 0, NULL, /* rparam */
+                          &rdata, 56, &rdata_count);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
        if (optimal_transfer_size) {
@@ -539,12 +532,7 @@ bool cli_get_posix_fs_info(struct cli_state *cli,
        if (fs_identifier) {
                *fs_identifier = BIG_UINT(rdata,48);
        }
-
-cleanup:
-       SAFE_FREE(rparam);
-       SAFE_FREE(rdata);
-
-       return ret;
+       return NT_STATUS_OK;
 }
 
 
@@ -554,78 +542,38 @@ cleanup:
 
 static NTSTATUS enc_blob_send_receive(struct cli_state *cli, DATA_BLOB *in, DATA_BLOB *out, DATA_BLOB *param_out)
 {
-       uint16 setup;
-       char param[4];
-       char *rparam=NULL, *rdata=NULL;
-       unsigned int rparam_count=0, rdata_count=0;
-       NTSTATUS status = NT_STATUS_OK;
-
-       setup = TRANSACT2_SETFSINFO;
+       uint16_t setup[1];
+       uint8_t param[4];
+       uint8_t *rparam=NULL, *rdata=NULL;
+       uint32_t num_rparam, num_rdata;
+       NTSTATUS status;
 
+       SSVAL(setup+0, 0, TRANSACT2_SETFSINFO);
        SSVAL(param,0,0);
        SSVAL(param,2,SMB_REQUEST_TRANSPORT_ENCRYPTION);
 
-       if (!cli_send_trans(cli, SMBtrans2,
-                               NULL,
-                               0, 0,
-                               &setup, 1, 0,
-                               param, 4, 0,
-                               (char *)in->data, in->length, CLI_BUFFER_SIZE)) {
-               status = cli_nt_error(cli);
-               goto out;
-       }
-
-       if (!cli_receive_trans(cli, SMBtrans2,
-                               &rparam, &rparam_count,
-                               &rdata, &rdata_count)) {
-               status = cli_nt_error(cli);
-               goto out;
-       }
-
-       if (cli_is_error(cli)) {
-               status = cli_nt_error(cli);
-               if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
-                       goto out;
-               }
+       status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, 0, 0, 0,
+                          setup, 1, 0,
+                          param, 4, 2,
+                          (uint8_t *)in->data, in->length, CLI_BUFFER_SIZE,
+                          NULL,          /* recv_flags */
+                          NULL, 0, NULL, /* rsetup */
+                          &rparam, 0, &num_rparam,
+                          &rdata, 0, &num_rdata);
+
+       if (!NT_STATUS_IS_OK(status) &&
+           !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
+               return status;
        }
 
-       *out = data_blob(rdata, rdata_count);
-       *param_out = data_blob(rparam, rparam_count);
+       *out = data_blob(rdata, num_rdata);
+       *param_out = data_blob(rparam, num_rparam);
 
-  out:
-
-       SAFE_FREE(rparam);
-       SAFE_FREE(rdata);
+       TALLOC_FREE(rparam);
+       TALLOC_FREE(rdata);
        return status;
 }
 
-/******************************************************************************
- Make a client state struct.
-******************************************************************************/
-
-static struct smb_trans_enc_state *make_cli_enc_state(enum smb_trans_enc_type smb_enc_type)
-{
-       struct smb_trans_enc_state *es = NULL;
-       es = SMB_MALLOC_P(struct smb_trans_enc_state);
-       if (!es) {
-               return NULL;
-       }
-       ZERO_STRUCTP(es);
-       es->smb_enc_type = smb_enc_type;
-
-#if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
-       if (smb_enc_type == SMB_TRANS_ENC_GSS) {
-               es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
-               if (!es->s.gss_state) {
-                       SAFE_FREE(es);
-                       return NULL;
-               }
-               ZERO_STRUCTP(es->s.gss_state);
-       }
-#endif
-       return es;
-}
-
 /******************************************************************************
  Start a raw ntlmssp encryption.
 ******************************************************************************/
@@ -639,35 +587,37 @@ NTSTATUS cli_raw_ntlm_smb_encryption_start(struct cli_state *cli,
        DATA_BLOB blob_out = data_blob_null;
        DATA_BLOB param_out = data_blob_null;
        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
-       struct smb_trans_enc_state *es = make_cli_enc_state(SMB_TRANS_ENC_NTLM);
-
+       struct auth_generic_state *auth_generic_state;
+       struct smb_trans_enc_state *es = talloc_zero(NULL, struct smb_trans_enc_state);
        if (!es) {
                return NT_STATUS_NO_MEMORY;
        }
-       status = ntlmssp_client_start(NULL,
-                                     global_myname(),
-                                     lp_workgroup(),
-                                     lp_client_ntlmv2_auth(),
-                                     &es->s.ntlmssp_state);
+       status = auth_generic_client_prepare(es,
+                                            &auth_generic_state);
        if (!NT_STATUS_IS_OK(status)) {
                goto fail;
        }
 
-       ntlmssp_want_feature(es->s.ntlmssp_state, NTLMSSP_FEATURE_SESSION_KEY);
-       es->s.ntlmssp_state->neg_flags |= (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL);
+       gensec_want_feature(auth_generic_state->gensec_security, GENSEC_FEATURE_SESSION_KEY);
+       gensec_want_feature(auth_generic_state->gensec_security, GENSEC_FEATURE_SEAL);
 
-       if (!NT_STATUS_IS_OK(status = ntlmssp_set_username(es->s.ntlmssp_state, user))) {
+       if (!NT_STATUS_IS_OK(status = auth_generic_set_username(auth_generic_state, user))) {
                goto fail;
        }
-       if (!NT_STATUS_IS_OK(status = ntlmssp_set_domain(es->s.ntlmssp_state, domain))) {
+       if (!NT_STATUS_IS_OK(status = auth_generic_set_domain(auth_generic_state, domain))) {
                goto fail;
        }
-       if (!NT_STATUS_IS_OK(status = ntlmssp_set_password(es->s.ntlmssp_state, pass))) {
+       if (!NT_STATUS_IS_OK(status = auth_generic_set_password(auth_generic_state, pass))) {
+               goto fail;
+       }
+
+       if (!NT_STATUS_IS_OK(status = auth_generic_client_start(auth_generic_state, GENSEC_OID_NTLMSSP))) {
                goto fail;
        }
 
        do {
-               status = ntlmssp_update(es->s.ntlmssp_state, blob_in, &blob_out);
+               status = gensec_update(auth_generic_state->gensec_security, auth_generic_state,
+                                      blob_in, &blob_out);
                data_blob_free(&blob_in);
                data_blob_free(&param_out);
                if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) || NT_STATUS_IS_OK(status)) {
@@ -691,162 +641,69 @@ NTSTATUS cli_raw_ntlm_smb_encryption_start(struct cli_state *cli,
        data_blob_free(&blob_in);
 
        if (NT_STATUS_IS_OK(status)) {
+               es->enc_on = true;
                /* Replace the old state, if any. */
-               if (cli->trans_enc_state) {
-                       common_free_encryption_state(&cli->trans_enc_state);
-               }
-               cli->trans_enc_state = es;
-               cli->trans_enc_state->enc_on = True;
+               /* We only need the gensec_security part from here.
+                * es is a malloc()ed pointer, so we cannot make
+                * gensec_security a talloc child */
+               es->gensec_security = talloc_move(NULL,
+                                                 &auth_generic_state->gensec_security);
+               smb1cli_conn_set_encryption(cli->conn, es);
                es = NULL;
        }
 
   fail:
-
-       common_free_encryption_state(&es);
+       TALLOC_FREE(es);
        return status;
 }
 
-#if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
-
-#ifndef SMB_GSS_REQUIRED_FLAGS
-#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)
-#endif
-
 /******************************************************************************
Get client gss blob to send to a server.
Start a SPNEGO gssapi encryption context.
 ******************************************************************************/
 
-static NTSTATUS make_cli_gss_blob(TALLOC_CTX *ctx,
-                               struct smb_trans_enc_state *es,
-                               const char *service,
-                               const char *host,
-                               NTSTATUS status_in,
-                               DATA_BLOB spnego_blob_in,
-                               DATA_BLOB *p_blob_out)
+NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
 {
-       const char *krb_mechs[] = {OID_KERBEROS5, NULL};
-       OM_uint32 ret;
-       OM_uint32 min;
-       gss_name_t srv_name;
-       gss_buffer_desc input_name;
-       gss_buffer_desc *p_tok_in;
-       gss_buffer_desc tok_out, tok_in;
-       DATA_BLOB blob_out = data_blob_null;
-       DATA_BLOB blob_in = data_blob_null;
-       char *host_princ_s = NULL;
-       OM_uint32 ret_flags = 0;
-       NTSTATUS status = NT_STATUS_OK;
-
-       gss_OID_desc nt_hostbased_service =
-       {10, CONST_DISCARD(char *,"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
-
-       memset(&tok_out, '\0', sizeof(tok_out));
+       DATA_BLOB blob_recv = data_blob_null;
+       DATA_BLOB blob_send = data_blob_null;
+       DATA_BLOB param_out = data_blob_null;
+       NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
+       struct auth_generic_state *auth_generic_state;
+       struct smb_trans_enc_state *es = talloc_zero(NULL, struct smb_trans_enc_state);
 
-       /* Get a ticket for the service@host */
-       if (asprintf(&host_princ_s, "%s@%s", service, host) == -1) {
+       if (!es) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       input_name.value = host_princ_s;
-       input_name.length = strlen(host_princ_s) + 1;
-
-       ret = gss_import_name(&min,
-                               &input_name,
-                               &nt_hostbased_service,
-                               &srv_name);
-
-       if (ret != GSS_S_COMPLETE) {
-               SAFE_FREE(host_princ_s);
-               return map_nt_error_from_gss(ret, min);
-       }
-
-       if (spnego_blob_in.length == 0) {
-               p_tok_in = GSS_C_NO_BUFFER;
-       } else {
-               /* Remove the SPNEGO wrapper */
-               if (!spnego_parse_auth_response(ctx, spnego_blob_in, status_in, OID_KERBEROS5, &blob_in)) {
-                       status = NT_STATUS_UNSUCCESSFUL;
-                       goto fail;
-               }
-               tok_in.value = blob_in.data;
-               tok_in.length = blob_in.length;
-               p_tok_in = &tok_in;
-       }
-
-       ret = gss_init_sec_context(&min,
-                               GSS_C_NO_CREDENTIAL, /* Use our default cred. */
-                               &es->s.gss_state->gss_ctx,
-                               srv_name,
-                               GSS_C_NO_OID, /* default OID. */
-                               GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG | GSS_C_DELEG_FLAG,
-                               GSS_C_INDEFINITE,       /* requested ticket lifetime. */
-                               NULL,   /* no channel bindings */
-                               p_tok_in,
-                               NULL,   /* ignore mech type */
-                               &tok_out,
-                               &ret_flags,
-                               NULL);  /* ignore time_rec */
-
-       status = map_nt_error_from_gss(ret, min);
-       if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
-               ADS_STATUS adss = ADS_ERROR_GSS(ret, min);
-               DEBUG(10,("make_cli_gss_blob: gss_init_sec_context failed with %s\n",
-                       ads_errstr(adss)));
+       status = auth_generic_client_prepare(es,
+                                            &auth_generic_state);
+       if (!NT_STATUS_IS_OK(status)) {
                goto fail;
        }
 
-       if ((ret_flags & SMB_GSS_REQUIRED_FLAGS) != SMB_GSS_REQUIRED_FLAGS) {
-               status = NT_STATUS_ACCESS_DENIED;
-       }
-
-       blob_out = data_blob_talloc(ctx, tok_out.value, tok_out.length);
+       gensec_want_feature(auth_generic_state->gensec_security, GENSEC_FEATURE_SESSION_KEY);
+       gensec_want_feature(auth_generic_state->gensec_security, GENSEC_FEATURE_SEAL);
 
-       /* Wrap in an SPNEGO wrapper */
-       *p_blob_out = spnego_gen_negTokenInit(ctx, krb_mechs, &blob_out, NULL);
+       cli_credentials_set_kerberos_state(auth_generic_state->credentials, 
+                                          CRED_MUST_USE_KERBEROS);
 
-  fail:
-
-       data_blob_free(&blob_out);
-       data_blob_free(&blob_in);
-       SAFE_FREE(host_princ_s);
-       gss_release_name(&min, &srv_name);
-       if (tok_out.value) {
-               gss_release_buffer(&min, &tok_out);
+       status = gensec_set_target_service(auth_generic_state->gensec_security, "cifs");
+       if (!NT_STATUS_IS_OK(status)) {
+               goto fail;
        }
-       return status;
-}
 
-/******************************************************************************
- Start a SPNEGO gssapi encryption context.
-******************************************************************************/
-
-NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
-{
-       DATA_BLOB blob_recv = data_blob_null;
-       DATA_BLOB blob_send = data_blob_null;
-       DATA_BLOB param_out = data_blob_null;
-       NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
-       fstring fqdn;
-       const char *servicename;
-       struct smb_trans_enc_state *es = make_cli_enc_state(SMB_TRANS_ENC_GSS);
-
-       if (!es) {
-               return NT_STATUS_NO_MEMORY;
+       status = gensec_set_target_hostname(auth_generic_state->gensec_security, 
+                                           smbXcli_conn_remote_name(cli->conn));
+       if (!NT_STATUS_IS_OK(status)) {
+               goto fail;
        }
 
-       name_to_fqdn(fqdn, cli->desthost);
-       strlower_m(fqdn);
-
-       servicename = "cifs";
-       status = make_cli_gss_blob(talloc_tos(), es, servicename, fqdn, NT_STATUS_OK, blob_recv, &blob_send);
-       if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
-               servicename = "host";
-               status = make_cli_gss_blob(talloc_tos(), es, servicename, fqdn, NT_STATUS_OK, blob_recv, &blob_send);
-               if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED)) {
-                       goto fail;
-               }
+       if (!NT_STATUS_IS_OK(status = auth_generic_client_start(auth_generic_state, GENSEC_OID_SPNEGO))) {
+               goto fail;
        }
 
+       status = gensec_update(auth_generic_state->gensec_security, talloc_tos(),
+                              blob_recv, &blob_send);
+
        do {
                data_blob_free(&blob_recv);
                status = enc_blob_send_receive(cli, &blob_send, &blob_recv, &param_out);
@@ -854,31 +711,35 @@ NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
                        es->enc_ctx_num = SVAL(param_out.data, 0);
                }
                data_blob_free(&blob_send);
-               status = make_cli_gss_blob(talloc_tos(), es, servicename, fqdn, status, blob_recv, &blob_send);
+               status = gensec_update(auth_generic_state->gensec_security, talloc_tos(),
+                                      blob_recv, &blob_send);
        } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
        data_blob_free(&blob_recv);
 
        if (NT_STATUS_IS_OK(status)) {
-               /* Replace the old state, if any. */
-               if (cli->trans_enc_state) {
-                       common_free_encryption_state(&cli->trans_enc_state);
+               if (!gensec_have_feature(auth_generic_state->gensec_security, 
+                                        GENSEC_FEATURE_SIGN) ||
+                   !gensec_have_feature(auth_generic_state->gensec_security, 
+                                        GENSEC_FEATURE_SEAL)) {
+                       status = NT_STATUS_ACCESS_DENIED;
                }
-               cli->trans_enc_state = es;
-               cli->trans_enc_state->enc_on = True;
-               es = NULL;
        }
 
-  fail:
-
-       common_free_encryption_state(&es);
+       if (NT_STATUS_IS_OK(status)) {
+               es->enc_on = true;
+               /* Replace the old state, if any. */
+               /* We only need the gensec_security part from here.
+                * es is a malloc()ed pointer, so we cannot make
+                * gensec_security a talloc child */
+               es->gensec_security = talloc_move(es,
+                                                 &auth_generic_state->gensec_security);
+               smb1cli_conn_set_encryption(cli->conn, es);
+               es = NULL;
+       }
+fail:
+       TALLOC_FREE(es);
        return status;
 }
-#else
-NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
-{
-       return NT_STATUS_NOT_SUPPORTED;
-}
-#endif
 
 /********************************************************************
  Ensure a connection is encrypted.
@@ -889,8 +750,8 @@ NTSTATUS cli_force_encryption(struct cli_state *c,
                        const char *password,
                        const char *domain)
 {
-       uint16 major, minor;
-       uint32 caplow, caphigh;
+       uint16_t major, minor;
+       uint32_t caplow, caphigh;
        NTSTATUS status;
 
        if (!SERVER_HAS_UNIX_CIFS(c)) {
@@ -917,3 +778,263 @@ NTSTATUS cli_force_encryption(struct cli_state *c,
                                        password,
                                        domain);
 }
+
+/****************************************************************************
+ Do a UNIX extensions SMB_QUERY_POSIX_WHOAMI call.
+****************************************************************************/
+
+struct posix_whoami_state {
+       uint16_t setup[1];
+       uint8_t param[2];
+       uint32_t max_rdata;
+       bool guest;
+       uint64_t uid;
+       uint64_t gid;
+       uint32_t num_gids;
+       uint64_t *gids;
+       uint32_t num_sids;
+       struct dom_sid *sids;
+};
+
+static void cli_posix_whoami_done(struct tevent_req *subreq);
+
+struct tevent_req *cli_posix_whoami_send(TALLOC_CTX *mem_ctx,
+                                       struct tevent_context *ev,
+                                       struct cli_state *cli)
+{
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct posix_whoami_state *state = NULL;
+
+       req = tevent_req_create(mem_ctx, &state, struct posix_whoami_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       /* Setup setup word. */
+       SSVAL(state->setup, 0, TRANSACT2_QFSINFO);
+       SSVAL(state->param, 0, SMB_QUERY_POSIX_WHOAMI);
+
+       state->max_rdata = 62*1024;
+
+       subreq = cli_trans_send(state,                  /* mem ctx. */
+                               ev,                     /* event ctx. */
+                               cli,                    /* cli_state. */
+                               0,                      /* additional_flags2 */
+                               SMBtrans2,              /* cmd. */
+                               NULL,                   /* pipe name. */
+                               -1,                     /* fid. */
+                               0,                      /* function. */
+                               0,                      /* flags. */
+                               state->setup,           /* setup. */
+                               1,                      /* num setup uint16_t words. */
+                               0,                      /* max returned setup. */
+                               state->param,           /* param. */
+                               2,                      /* num param. */
+                               0,                      /* max returned param. */
+                               NULL,                   /* data. */
+                               0,                      /* num data. */
+                               state->max_rdata);      /* max returned data. */
+
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_posix_whoami_done, req);
+       return req;
+}
+
+static void cli_posix_whoami_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+                       subreq, struct tevent_req);
+       struct posix_whoami_state *state = tevent_req_data(
+                       req, struct posix_whoami_state);
+       uint8_t *rdata = NULL;
+       uint8_t *p = NULL;
+       uint32_t num_rdata = 0;
+       uint32_t i;
+       NTSTATUS status;
+
+       status = cli_trans_recv(subreq,
+                               state,
+                               NULL,
+                               NULL,
+                               0,
+                               NULL,
+                                NULL,
+                               0,
+                               NULL,
+                               &rdata,
+                               40,
+                               &num_rdata);
+       TALLOC_FREE(subreq);
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
+
+       /*
+        * Not strictly needed - cli_trans_recv()
+        * will ensure at least 40 bytes here. Added
+        * as more of a reminder to be careful when
+        * parsing network packets in C.
+        */
+
+       if (num_rdata < 40 || rdata + num_rdata < rdata) {
+               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+               return;
+       }
+
+       state->guest = (IVAL(rdata, 0) & SMB_WHOAMI_GUEST);
+       state->uid = BVAL(rdata, 8);
+       state->gid = BVAL(rdata, 16);
+       state->num_gids = IVAL(rdata, 24);
+       state->num_sids = IVAL(rdata, 28);
+
+       state->gids = talloc_array(state, uint64_t, state->num_gids);
+       if (tevent_req_nomem(state->gids, req)) {
+               return;
+       }
+       state->sids = talloc_array(state, struct dom_sid, state->num_sids);
+       if (tevent_req_nomem(state->sids, req)) {
+               return;
+       }
+
+       p = rdata + 40;
+
+       for (i = 0; i < state->num_gids; i++) {
+               if (p + 8 > rdata + num_rdata) {
+                       tevent_req_nterror(req,
+                               NT_STATUS_INVALID_NETWORK_RESPONSE);
+                       return;
+               }
+               state->gids[i] = BVAL(p, 0);
+               p += 8;
+       }
+
+       num_rdata -= (p - rdata);
+
+       for (i = 0; i < state->num_sids; i++) {
+               size_t sid_size;
+               DATA_BLOB in = data_blob_const(p, num_rdata);
+               enum ndr_err_code ndr_err;
+
+               ndr_err = ndr_pull_struct_blob(&in,
+                               state,
+                               &state->sids[i],
+                               (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
+               if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+                       tevent_req_nterror(req,
+                               NT_STATUS_INVALID_NETWORK_RESPONSE);
+                       return;
+               }
+
+               sid_size = ndr_size_dom_sid(&state->sids[i], 0);
+
+               if (sid_size > num_rdata) {
+                       tevent_req_nterror(req,
+                               NT_STATUS_INVALID_NETWORK_RESPONSE);
+                       return;
+               }
+
+               p += sid_size;
+               num_rdata -= sid_size;
+       }
+       tevent_req_done(req);
+}
+
+NTSTATUS cli_posix_whoami_recv(struct tevent_req *req,
+                       TALLOC_CTX *mem_ctx,
+                       uint64_t *puid,
+                       uint64_t *pgid,
+                       uint32_t *pnum_gids,
+                       uint64_t **pgids,
+                       uint32_t *pnum_sids,
+                       struct dom_sid **psids,
+                       bool *pguest)
+{
+       NTSTATUS status;
+       struct posix_whoami_state *state = tevent_req_data(
+                       req, struct posix_whoami_state);
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+
+       if (puid) {
+               *puid = state->uid;
+       }
+       if (pgid) {
+               *pgid = state->gid;
+       }
+       if (pnum_gids) {
+               *pnum_gids = state->num_gids;
+       }
+       if (pgids) {
+               *pgids = talloc_move(mem_ctx, &state->gids);
+       }
+       if (pnum_sids) {
+               *pnum_sids = state->num_sids;
+       }
+       if (psids) {
+               *psids = talloc_move(mem_ctx, &state->sids);
+       }
+       if (pguest) {
+               *pguest = state->guest;
+       }
+       return NT_STATUS_OK;
+}
+
+NTSTATUS cli_posix_whoami(struct cli_state *cli,
+                       TALLOC_CTX *mem_ctx,
+                       uint64_t *puid,
+                       uint64_t *pgid,
+                       uint32_t *num_gids,
+                       uint64_t **gids,
+                       uint32_t *num_sids,
+                       struct dom_sid **sids,
+                       bool *pguest)
+{
+        TALLOC_CTX *frame = talloc_stackframe();
+        struct tevent_context *ev = NULL;
+        struct tevent_req *req = NULL;
+        NTSTATUS status = NT_STATUS_OK;
+
+        if (smbXcli_conn_has_async_calls(cli->conn)) {
+                /*
+                 * Can't use sync call while an async call is in flight
+                 */
+                status = NT_STATUS_INVALID_PARAMETER;
+                goto fail;
+        }
+
+        ev = samba_tevent_context_init(frame);
+        if (ev == NULL) {
+                status = NT_STATUS_NO_MEMORY;
+                goto fail;
+        }
+
+        req = cli_posix_whoami_send(frame,
+                                ev,
+                                cli);
+        if (req == NULL) {
+                status = NT_STATUS_NO_MEMORY;
+                goto fail;
+        }
+
+        if (!tevent_req_poll_ntstatus(req, ev, &status)) {
+                goto fail;
+        }
+
+        status = cli_posix_whoami_recv(req,
+                       mem_ctx,
+                       puid,
+                       pgid,
+                       num_gids,
+                       gids,
+                       num_sids,
+                       sids,
+                       pguest);
+
+ fail:
+       TALLOC_FREE(frame);
+       return status;
+}