TODO: s3:libsmb: add cli_state_dup()
[metze/samba/wip.git] / source3 / libsmb / clientgen.c
index 6bc8d0ce70855120926289cbc39d6fa470d45ab0..b2eccfe9f55a622fd6286f2d4bc80623f2c283d3 100644 (file)
@@ -26,6 +26,7 @@
 #include "async_smb.h"
 #include "../libcli/smb/smbXcli_base.h"
 #include "../librpc/ndr/libndr.h"
+#include "../include/client.h"
 
 /*******************************************************************
  Setup the word count and byte count for a client smb message.
@@ -64,76 +65,16 @@ bool cli_set_backup_intent(struct cli_state *cli, bool flag)
        return old_state;
 }
 
-/****************************************************************************
- Initialize Domain, user or password.
-****************************************************************************/
-
-NTSTATUS cli_set_domain(struct cli_state *cli, const char *domain)
-{
-       TALLOC_FREE(cli->domain);
-       cli->domain = talloc_strdup(cli, domain ? domain : "");
-       if (cli->domain == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-       return NT_STATUS_OK;
-}
-
-NTSTATUS cli_set_username(struct cli_state *cli, const char *username)
-{
-       TALLOC_FREE(cli->user_name);
-       cli->user_name = talloc_strdup(cli, username ? username : "");
-       if (cli->user_name == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-       return NT_STATUS_OK;
-}
-
-NTSTATUS cli_set_password(struct cli_state *cli, const char *password)
-{
-       TALLOC_FREE(cli->password);
-
-       /* Password can be NULL. */
-       if (password) {
-               cli->password = talloc_strdup(cli, password);
-               if (cli->password == NULL) {
-                       return NT_STATUS_NO_MEMORY;
-               }
-       } else {
-               /* Use zero NTLMSSP hashes and session key. */
-               cli->password = NULL;
-       }
-
-       return NT_STATUS_OK;
-}
-
-/****************************************************************************
- Initialise credentials of a client structure.
-****************************************************************************/
-
-NTSTATUS cli_init_creds(struct cli_state *cli, const char *username, const char *domain, const char *password)
-{
-       NTSTATUS status = cli_set_username(cli, username);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
-       status = cli_set_domain(cli, domain);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
-       DEBUG(10,("cli_init_creds: user %s domain %s\n", cli->user_name, cli->domain));
-
-       return cli_set_password(cli, password);
-}
-
 /****************************************************************************
  Initialise a client structure. Always returns a talloc'ed struct.
  Set the signing state (used from the command line).
 ****************************************************************************/
 
+struct GUID cli_state_client_guid;
+
 struct cli_state *cli_state_create(TALLOC_CTX *mem_ctx,
                                   int fd,
                                   const char *remote_name,
-                                  const char *remote_realm,
                                   int signing_state, int flags)
 {
        struct cli_state *cli = NULL;
@@ -143,7 +84,13 @@ struct cli_state *cli_state_create(TALLOC_CTX *mem_ctx,
        bool use_level_II_oplocks = false;
        uint32_t smb1_capabilities = 0;
        uint32_t smb2_capabilities = 0;
-       struct GUID client_guid = GUID_random();
+       struct GUID client_guid;
+
+       if (!GUID_all_zero(&cli_state_client_guid)) {
+               client_guid = cli_state_client_guid;
+       } else {
+               client_guid = GUID_random();
+       }
 
        /* Check the effective uid - make sure we are not setuid */
        if (is_setuid_root()) {
@@ -175,8 +122,7 @@ struct cli_state *cli_state_create(TALLOC_CTX *mem_ctx,
        }
        cli->raw_status = NT_STATUS_INTERNAL_ERROR;
        cli->map_dos_errors = true; /* remove this */
-       cli->timeout = 20000; /* Timeout is in milliseconds. */
-       cli->case_sensitive = false;
+       cli->timeout = CLIENT_TIMEOUT;
 
        /* Set the CLI_FORCE_DOSERR environment variable to test
           client routines using DOS errors instead of STATUS32
@@ -223,6 +169,15 @@ struct cli_state *cli_state_create(TALLOC_CTX *mem_ctx,
                use_level_II_oplocks = true;
        }
 
+       if (signing_state == SMB_SIGNING_IPC_DEFAULT) {
+               /*
+                * Ensure for IPC/RPC the default is to require
+                * signing unless explicitly turned off by the
+                * administrator.
+                */
+               signing_state = lp_client_ipc_signing();
+       }
+
        if (signing_state == SMB_SIGNING_DEFAULT) {
                signing_state = lp_client_signing();
        }
@@ -253,13 +208,6 @@ struct cli_state *cli_state_create(TALLOC_CTX *mem_ctx,
 
        smb2_capabilities = SMB2_CAP_ALL;
 
-       if (remote_realm) {
-               cli->remote_realm = talloc_strdup(cli, remote_realm);
-               if (cli->remote_realm == NULL) {
-                       goto error;
-               }
-       }
-
        cli->conn = smbXcli_conn_create(cli, fd, remote_name,
                                        signing_state,
                                        smb1_capabilities,
@@ -269,13 +217,8 @@ struct cli_state *cli_state_create(TALLOC_CTX *mem_ctx,
                goto error;
        }
 
-       cli->smb1.pid = (uint16_t)getpid();
+       cli->smb1.pid = (uint32_t)getpid();
        cli->smb1.vc_num = cli->smb1.pid;
-       cli->smb1.tcon = smbXcli_tcon_create(cli);
-       if (cli->smb1.tcon == NULL) {
-               goto error;
-       }
-       smb1cli_tcon_set_id(cli->smb1.tcon, UINT16_MAX);
        cli->smb1.session = smbXcli_session_create(cli, cli->conn);
        if (cli->smb1.session == NULL) {
                goto error;
@@ -292,6 +235,68 @@ struct cli_state *cli_state_create(TALLOC_CTX *mem_ctx,
         return NULL;
 }
 
+struct cli_state *cli_state_dup(TALLOC_CTX *mem_ctx,
+                               struct cli_state *primary)
+{
+       struct cli_state *cli = NULL;
+
+       cli = talloc_zero(mem_ctx, struct cli_state);
+       if (!cli) {
+               return NULL;
+       }
+
+       cli->raw_status = NT_STATUS_INTERNAL_ERROR;
+       cli->map_dos_errors = true; /* remove this */
+       cli->timeout = CLIENT_TIMEOUT;
+
+       cli->server_domain = talloc_strdup(cli, primary->server_domain);
+       if (!cli->server_domain) {
+               goto error;
+       }
+       cli->server_os = talloc_strdup(cli, primary->server_os);
+       if (!cli->server_os) {
+               goto error;
+       }
+       cli->server_type = talloc_strdup(cli, primary->server_type);
+       if (!cli->server_type) {
+               goto error;
+       }
+
+       cli->dfs_mountpoint = talloc_strdup(cli, primary->dfs_mountpoint);
+       if (!cli->dfs_mountpoint) {
+               goto error;
+       }
+
+       cli->use_kerberos = primary->use_kerberos;
+       cli->fallback_after_kerberos = primary->fallback_after_kerberos;
+       cli->use_ccache = primary->use_ccache;
+       cli->pw_nt_hash = primary->pw_nt_hash;
+       cli->use_oplocks = primary->use_oplocks;
+
+       /*
+        * We reference the connection and session details from the primary
+        * connection.
+        */
+       cli->primary_state = primary;
+       cli->conn = primary->conn;
+       cli->smb1.pid = primary->smb1.pid;
+       cli->smb1.vc_num = primary->smb1.vc_num;
+       cli->smb1.session = primary->smb1.session;
+       cli->smb2.session = primary->smb2.session;
+
+       cli->initialised = 1;
+
+       DLIST_ADD_END(primary, cli);
+       return cli;
+
+       /* Clean up after malloc() error */
+
+ error:
+
+       TALLOC_FREE(cli);
+       return NULL;
+}
+
 /****************************************************************************
  Close all pipes open on this session.
 ****************************************************************************/
@@ -326,7 +331,12 @@ static void _cli_shutdown(struct cli_state *cli)
                cli_tdis(cli);
        }
 
-       smbXcli_conn_disconnect(cli->conn, NT_STATUS_OK);
+       if (cli->primary_state == NULL) {
+               /*
+                * We only disconnect if it's the primary cli_state.
+                */
+               smbXcli_conn_disconnect(cli->conn, NT_STATUS_OK);
+       }
 
        TALLOC_FREE(cli);
 }
@@ -338,7 +348,7 @@ void cli_shutdown(struct cli_state *cli)
                return;
        }
        DLIST_HEAD(cli, cli_head);
-       if (cli_head == cli) {
+       if (cli->primary_state == NULL && cli_head == cli) {
                /*
                 * head of a DFS list, shutdown all subsidiary DFS
                 * connections.
@@ -357,11 +367,6 @@ void cli_shutdown(struct cli_state *cli)
        _cli_shutdown(cli);
 }
 
-const char *cli_state_remote_realm(struct cli_state *cli)
-{
-       return cli->remote_realm;
-}
-
 uint16_t cli_state_get_vc_num(struct cli_state *cli)
 {
        return cli->smb1.vc_num;
@@ -371,41 +376,96 @@ uint16_t cli_state_get_vc_num(struct cli_state *cli)
  Set the PID to use for smb messages. Return the old pid.
 ****************************************************************************/
 
-uint16 cli_setpid(struct cli_state *cli, uint16 pid)
+uint32_t cli_setpid(struct cli_state *cli, uint32_t pid)
 {
-       uint16_t ret = cli->smb1.pid;
+       uint32_t ret = cli->smb1.pid;
        cli->smb1.pid = pid;
        return ret;
 }
 
-uint16_t cli_getpid(struct cli_state *cli)
+uint32_t cli_getpid(struct cli_state *cli)
 {
        return cli->smb1.pid;
 }
 
-bool cli_state_has_tcon(struct cli_state *cli)
+bool cli_state_is_encryption_on(struct cli_state *cli)
 {
-       uint16_t tid = cli_state_get_tid(cli);
+       if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
+               return smb1cli_conn_encryption_on(cli->conn);
+       }
 
-       if (tid == UINT16_MAX) {
+       if (cli->smb2.tcon == NULL) {
                return false;
        }
 
+       return smb2cli_tcon_is_encryption_on(cli->smb2.tcon);
+}
+
+bool cli_state_has_tcon(struct cli_state *cli)
+{
+       uint32_t tid;
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               if (cli->smb2.tcon == NULL) {
+                       return false;
+               }
+               tid = cli_state_get_tid(cli);
+               if (tid == UINT32_MAX) {
+                       return false;
+               }
+       } else {
+               if (cli->smb1.tcon == NULL) {
+                       return false;
+               }
+               tid = cli_state_get_tid(cli);
+               if (tid == UINT16_MAX) {
+                       return false;
+               }
+       }
        return true;
 }
 
-uint16_t cli_state_get_tid(struct cli_state *cli)
+uint32_t cli_state_get_tid(struct cli_state *cli)
 {
-       return smb1cli_tcon_current_id(cli->smb1.tcon);
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return smb2cli_tcon_current_id(cli->smb2.tcon);
+       } else {
+               return (uint32_t)smb1cli_tcon_current_id(cli->smb1.tcon);
+       }
 }
 
-uint16_t cli_state_set_tid(struct cli_state *cli, uint16_t tid)
+uint32_t cli_state_set_tid(struct cli_state *cli, uint32_t tid)
 {
-       uint16_t ret = smb1cli_tcon_current_id(cli->smb1.tcon);
-       smb1cli_tcon_set_id(cli->smb1.tcon, tid);
+       uint32_t ret;
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               ret = smb2cli_tcon_current_id(cli->smb2.tcon);
+               smb2cli_tcon_set_id(cli->smb2.tcon, tid);
+       } else {
+               ret = smb1cli_tcon_current_id(cli->smb1.tcon);
+               smb1cli_tcon_set_id(cli->smb1.tcon, tid);
+       }
        return ret;
 }
 
+struct smbXcli_tcon *cli_state_save_tcon(struct cli_state *cli)
+{
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return smbXcli_tcon_copy(cli, cli->smb2.tcon);
+       } else {
+               return smbXcli_tcon_copy(cli, cli->smb1.tcon);
+       }
+}
+
+void cli_state_restore_tcon(struct cli_state *cli, struct smbXcli_tcon *tcon)
+{
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               TALLOC_FREE(cli->smb2.tcon);
+               cli->smb2.tcon = tcon;
+       } else {
+               TALLOC_FREE(cli->smb1.tcon);
+               cli->smb1.tcon = tcon;
+       }
+}
+
 uint16_t cli_state_get_uid(struct cli_state *cli)
 {
        return smb1cli_session_current_id(cli->smb1.session);
@@ -424,8 +484,29 @@ uint16_t cli_state_set_uid(struct cli_state *cli, uint16_t uid)
 
 bool cli_set_case_sensitive(struct cli_state *cli, bool case_sensitive)
 {
-       bool ret = cli->case_sensitive;
-       cli->case_sensitive = case_sensitive;
+       bool ret;
+       uint32_t fs_attrs;
+       struct smbXcli_tcon *tcon;
+
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               tcon = cli->smb2.tcon;
+       } else {
+               tcon = cli->smb1.tcon;
+       }
+
+       fs_attrs = smbXcli_tcon_get_fs_attributes(tcon);
+       if (fs_attrs & FILE_CASE_SENSITIVE_SEARCH) {
+               ret = true;
+       } else {
+               ret = false;
+       }
+       if (case_sensitive) {
+               fs_attrs |= FILE_CASE_SENSITIVE_SEARCH;
+       } else {
+               fs_attrs &= ~FILE_CASE_SENSITIVE_SEARCH;
+       }
+       smbXcli_tcon_set_fs_attributes(tcon, fs_attrs);
+
        return ret;
 }
 
@@ -459,7 +540,7 @@ struct cli_echo_state {
 
 static void cli_echo_done(struct tevent_req *subreq);
 
-struct tevent_req *cli_echo_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
+struct tevent_req *cli_echo_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
                                 struct cli_state *cli, uint16_t num_echos,
                                 DATA_BLOB data)
 {
@@ -537,7 +618,7 @@ NTSTATUS cli_echo_recv(struct tevent_req *req)
 NTSTATUS cli_echo(struct cli_state *cli, uint16_t num_echos, DATA_BLOB data)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
@@ -549,7 +630,7 @@ NTSTATUS cli_echo(struct cli_state *cli, uint16_t num_echos, DATA_BLOB data)
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -561,8 +642,7 @@ NTSTATUS cli_echo(struct cli_state *cli, uint16_t num_echos, DATA_BLOB data)
                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;
        }
 
@@ -612,11 +692,11 @@ NTSTATUS cli_smb(TALLOC_CTX *mem_ctx, struct cli_state *cli,
         if (smbXcli_conn_has_async_calls(cli->conn)) {
                 return NT_STATUS_INVALID_PARAMETER;
         }
-        ev = tevent_context_init(mem_ctx);
+        ev = samba_tevent_context_init(mem_ctx);
         if (ev == NULL) {
                 goto fail;
         }
-        req = cli_smb_send(mem_ctx, ev, cli, smb_command, additional_flags,
+        req = cli_smb_send(mem_ctx, ev, cli, smb_command, additional_flags, 0,
                           wct, vwv, num_bytes, bytes);
         if (req == NULL) {
                 goto fail;