TODO: s3:libsmb: add cli_state_dup()
[metze/samba/wip.git] / source3 / libsmb / clientgen.c
index e6f372ff764b749334047819f3a58087f3cb56db..b2eccfe9f55a622fd6286f2d4bc80623f2c283d3 100644 (file)
@@ -75,7 +75,6 @@ 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;
@@ -209,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,
@@ -243,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.
 ****************************************************************************/
@@ -277,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);
 }
@@ -289,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.
@@ -329,6 +388,19 @@ uint32_t cli_getpid(struct cli_state *cli)
        return cli->smb1.pid;
 }
 
+bool cli_state_is_encryption_on(struct cli_state *cli)
+{
+       if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
+               return smb1cli_conn_encryption_on(cli->conn);
+       }
+
+       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;
@@ -366,7 +438,7 @@ uint32_t cli_state_set_tid(struct cli_state *cli, uint32_t 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->smb1.tcon, tid);
+               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);