libcli/smb: move smb_seal.c to the toplevel
[rusty/samba.git] / source3 / libsmb / clifsinfo.c
index 5ca76ac16a8410c09b6997695524c6e331e4cb10..a5b7838b2a2620878832b462c8f2024187a9f6ac 100644 (file)
 */
 
 #include "includes.h"
+#include "libsmb/libsmb.h"
 #include "../libcli/auth/spnego.h"
-#include "../libcli/auth/ntlmssp.h"
+#include "../auth/ntlmssp/ntlmssp.h"
+#include "../lib/util/tevent_ntstatus.h"
 #include "async_smb.h"
+#include "../libcli/smb/smb_seal.h"
+#include "trans2.h"
+#include "ntlmssp_wrap.h"
+#include "auth/gensec/gensec.h"
 
 /****************************************************************************
  Get UNIX extensions version info.
@@ -149,9 +155,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;
 }
 
@@ -254,9 +257,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;
 }
 
@@ -353,21 +353,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);
@@ -377,10 +379,10 @@ NTSTATUS cli_get_fs_volume_info(struct cli_state *cli, fstring volume_name,
                           setup, 1, 0,
                           param, 2, 0,
                           NULL, 0, 560,
-                          NULL,
+                          &recv_flags2,
                           NULL, 0, NULL,
                           NULL, 0, NULL,
-                          &rdata, 10, &rdata_count);
+                          &rdata, 18, &rdata_count);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -394,58 +396,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 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) {
@@ -464,57 +467,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 *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)
 {
-       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 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) {
@@ -541,12 +527,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;
 }
 
 
@@ -556,48 +537,35 @@ 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;
 }
 
@@ -641,35 +609,38 @@ 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 auth_ntlmssp_state *auth_ntlmssp_state;
        struct smb_trans_enc_state *es = make_cli_enc_state(SMB_TRANS_ENC_NTLM);
 
        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_ntlmssp_client_prepare(NULL,
+                                            &auth_ntlmssp_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_ntlmssp_state->gensec_security, GENSEC_FEATURE_SESSION_KEY);
+       gensec_want_feature(auth_ntlmssp_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_ntlmssp_set_username(auth_ntlmssp_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_ntlmssp_set_domain(auth_ntlmssp_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_ntlmssp_set_password(auth_ntlmssp_state, pass))) {
+               goto fail;
+       }
+
+       if (!NT_STATUS_IS_OK(status = auth_ntlmssp_client_start(auth_ntlmssp_state))) {
                goto fail;
        }
 
        do {
-               status = ntlmssp_update(es->s.ntlmssp_state, blob_in, &blob_out);
+               status = gensec_update(auth_ntlmssp_state->gensec_security, auth_ntlmssp_state,
+                                      NULL, 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)) {
@@ -697,13 +668,18 @@ NTSTATUS cli_raw_ntlm_smb_encryption_start(struct cli_state *cli,
                if (cli->trans_enc_state) {
                        common_free_encryption_state(&cli->trans_enc_state);
                }
+               /* 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->s.gensec_security = talloc_move(NULL,
+                                       &auth_ntlmssp_state->gensec_security);
                cli->trans_enc_state = es;
                cli->trans_enc_state->enc_on = True;
                es = NULL;
        }
 
   fail:
-
+       TALLOC_FREE(auth_ntlmssp_state);
        common_free_encryption_state(&es);
        return status;
 }
@@ -740,7 +716,7 @@ static NTSTATUS make_cli_gss_blob(TALLOC_CTX *ctx,
        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")};
+       {10, discard_const_p(char, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
 
        memset(&tok_out, '\0', sizeof(tok_out));
 
@@ -836,7 +812,7 @@ NTSTATUS cli_gss_smb_encryption_start(struct cli_state *cli)
                return NT_STATUS_NO_MEMORY;
        }
 
-       name_to_fqdn(fqdn, cli->desthost);
+       name_to_fqdn(fqdn, cli_state_remote_name(cli));
        strlower_m(fqdn);
 
        servicename = "cifs";