s3: libsmb: add replace support to SMB2 rename
[sfrench/samba-autobuild/.git] / source3 / libsmb / cli_smb2_fnum.c
index 1be93812df9991905185e48e255b32e6551dc339..351fccfb6453d07ddd5420ee5c04628232d848a6 100644 (file)
@@ -886,6 +886,9 @@ NTSTATUS cli_smb2_list(struct cli_state *cli,
        if (fnum != 0xffff) {
                cli_smb2_close_fnum(cli, fnum);
        }
+
+       cli->raw_status = status;
+
        TALLOC_FREE(subframe);
        TALLOC_FREE(frame);
        return status;
@@ -957,7 +960,7 @@ NTSTATUS cli_smb2_qpathinfo_basic(struct cli_state *cli,
                return status;
        }
 
-       cli_smb2_close_fnum(cli, fnum);
+       status = cli_smb2_close_fnum(cli, fnum);
 
        ZERO_STRUCTP(sbuf);
 
@@ -967,7 +970,7 @@ NTSTATUS cli_smb2_qpathinfo_basic(struct cli_state *cli,
        sbuf->st_ex_size = cr.end_of_file;
        *attributes = cr.file_attributes;
 
-       return NT_STATUS_OK;
+       return status;
 }
 
 /***************************************************************
@@ -1133,6 +1136,9 @@ NTSTATUS cli_smb2_qpathinfo_alt_name(struct cli_state *cli,
        if (fnum != 0xffff) {
                cli_smb2_close_fnum(cli, fnum);
        }
+
+       cli->raw_status = status;
+
        TALLOC_FREE(frame);
        return status;
 }
@@ -1232,6 +1238,8 @@ NTSTATUS cli_smb2_qfileinfo_basic(struct cli_state *cli,
 
   fail:
 
+       cli->raw_status = status;
+
        TALLOC_FREE(frame);
        return status;
 }
@@ -1263,6 +1271,8 @@ NTSTATUS cli_smb2_getattrE(struct cli_state *cli,
                                        &change_time_ts,
                                         NULL);
 
+       cli->raw_status = status;
+
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -1340,6 +1350,8 @@ NTSTATUS cli_smb2_getatr(struct cli_state *cli,
                cli_smb2_close_fnum(cli, fnum);
        }
 
+       cli->raw_status = status;
+
        TALLOC_FREE(frame);
        return status;
 }
@@ -1410,6 +1422,8 @@ NTSTATUS cli_smb2_qpathinfo2(struct cli_state *cli,
                cli_smb2_close_fnum(cli, fnum);
        }
 
+       cli->raw_status = status;
+
        TALLOC_FREE(frame);
        return status;
 }
@@ -1498,6 +1512,8 @@ NTSTATUS cli_smb2_qpathinfo_streams(struct cli_state *cli,
                cli_smb2_close_fnum(cli, fnum);
        }
 
+       cli->raw_status = status;
+
        TALLOC_FREE(frame);
        return status;
 }
@@ -1580,6 +1596,8 @@ NTSTATUS cli_smb2_setatr(struct cli_state *cli,
                cli_smb2_close_fnum(cli, fnum);
        }
 
+       cli->raw_status = status;
+
        TALLOC_FREE(frame);
        return status;
 }
@@ -1636,7 +1654,7 @@ NTSTATUS cli_smb2_setattrE(struct cli_state *cli,
                put_long_date((char *)inbuf.data + 16, write_time);
        }
 
-       return smb2cli_set_info(cli->conn,
+       cli->raw_status = smb2cli_set_info(cli->conn,
                                cli->timeout,
                                cli->smb2.session,
                                cli->smb2.tcon,
@@ -1646,6 +1664,8 @@ NTSTATUS cli_smb2_setattrE(struct cli_state *cli,
                                0, /* in_additional_info */
                                ph->fid_persistent,
                                ph->fid_volatile);
+
+       return cli->raw_status;
 }
 
 /***************************************************************
@@ -1752,6 +1772,8 @@ NTSTATUS cli_smb2_dskattr(struct cli_state *cli, const char *path,
                cli_smb2_close_fnum(cli, fnum);
        }
 
+       cli->raw_status = status;
+
        TALLOC_FREE(frame);
        return status;
 }
@@ -1829,6 +1851,8 @@ fail:
                cli_smb2_close_fnum(cli, fnum);
        }
 
+       cli->raw_status = status;
+
        TALLOC_FREE(frame);
        return status;
 }
@@ -1909,6 +1933,8 @@ NTSTATUS cli_smb2_query_security_descriptor(struct cli_state *cli,
 
   fail:
 
+       cli->raw_status = status;
+
        TALLOC_FREE(frame);
        return status;
 }
@@ -1972,6 +1998,8 @@ NTSTATUS cli_smb2_set_security_descriptor(struct cli_state *cli,
 
   fail:
 
+       cli->raw_status = status;
+
        TALLOC_FREE(frame);
        return status;
 }
@@ -1982,8 +2010,9 @@ NTSTATUS cli_smb2_set_security_descriptor(struct cli_state *cli,
 ***************************************************************/
 
 NTSTATUS cli_smb2_rename(struct cli_state *cli,
-                       const char *fname_src,
-                       const char *fname_dst)
+                        const char *fname_src,
+                        const char *fname_dst,
+                        bool replace)
 {
        NTSTATUS status;
        DATA_BLOB inbuf = data_blob_null;
@@ -2063,6 +2092,10 @@ NTSTATUS cli_smb2_rename(struct cli_state *cli,
                goto fail;
        }
 
+       if (replace) {
+               SCVAL(inbuf.data, 0, 1);
+       }
+
        SIVAL(inbuf.data, 16, converted_size_bytes);
        memcpy(inbuf.data + 20, converted_str, converted_size_bytes);
 
@@ -2086,6 +2119,8 @@ NTSTATUS cli_smb2_rename(struct cli_state *cli,
                cli_smb2_close_fnum(cli, fnum);
        }
 
+       cli->raw_status = status;
+
        TALLOC_FREE(frame);
        return status;
 }
@@ -2179,6 +2214,8 @@ NTSTATUS cli_smb2_set_ea_fnum(struct cli_state *cli,
 
   fail:
 
+       cli->raw_status = status;
+
        TALLOC_FREE(frame);
        return status;
 }
@@ -2234,6 +2271,8 @@ NTSTATUS cli_smb2_set_ea_path(struct cli_state *cli,
                cli_smb2_close_fnum(cli, fnum);
        }
 
+       cli->raw_status = status;
+
        return status;
 }
 
@@ -2344,6 +2383,8 @@ NTSTATUS cli_smb2_get_ea_list_path(struct cli_state *cli,
                cli_smb2_close_fnum(cli, fnum);
        }
 
+       cli->raw_status = status;
+
        TALLOC_FREE(frame);
        return status;
 }
@@ -2429,6 +2470,8 @@ NTSTATUS cli_smb2_get_user_quota(struct cli_state *cli,
        }
 
 fail:
+       cli->raw_status = status;
+
        TALLOC_FREE(frame);
        return status;
 }
@@ -2502,6 +2545,8 @@ NTSTATUS cli_smb2_list_user_quota_step(struct cli_state *cli,
                                       pqt_list);
 
 cleanup:
+       cli->raw_status = status;
+
        TALLOC_FREE(frame);
        return status;
 }
@@ -2555,6 +2600,106 @@ NTSTATUS cli_smb2_get_fs_quota_info(struct cli_state *cli,
        status = parse_fs_quota_buffer(outbuf.data, outbuf.length, pqt);
 
 cleanup:
+       cli->raw_status = status;
+
+       TALLOC_FREE(frame);
+       return status;
+}
+
+/***************************************************************
+ Wrapper that allows SMB2 to set user quota.
+ Synchronous only.
+***************************************************************/
+
+NTSTATUS cli_smb2_set_user_quota(struct cli_state *cli,
+                                int quota_fnum,
+                                SMB_NTQUOTA_LIST *qtl)
+{
+       NTSTATUS status;
+       DATA_BLOB inbuf = data_blob_null;
+       struct smb2_hnd *ph = NULL;
+       TALLOC_CTX *frame = talloc_stackframe();
+
+       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 cleanup;
+       }
+
+       if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto cleanup;
+       }
+
+       status = map_fnum_to_smb2_handle(cli, quota_fnum, &ph);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto cleanup;
+       }
+
+       status = build_user_quota_buffer(qtl, 0, talloc_tos(), &inbuf, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto cleanup;
+       }
+
+       status = smb2cli_set_info(cli->conn, cli->timeout, cli->smb2.session,
+                                 cli->smb2.tcon, 4, /* in_info_type */
+                                 0,                 /* in_file_info_class */
+                                 &inbuf,           /* in_input_buffer */
+                                 0,                 /* in_additional_info */
+                                 ph->fid_persistent, ph->fid_volatile);
+cleanup:
+
+       cli->raw_status = status;
+
+       TALLOC_FREE(frame);
+
+       return status;
+}
+
+NTSTATUS cli_smb2_set_fs_quota_info(struct cli_state *cli,
+                                   int quota_fnum,
+                                   SMB_NTQUOTA_STRUCT *pqt)
+{
+       NTSTATUS status;
+       DATA_BLOB inbuf = data_blob_null;
+       struct smb2_hnd *ph = NULL;
+       TALLOC_CTX *frame = talloc_stackframe();
+
+       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 cleanup;
+       }
+
+       if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto cleanup;
+       }
+
+       status = map_fnum_to_smb2_handle(cli, quota_fnum, &ph);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto cleanup;
+       }
+
+       status = build_fs_quota_buffer(talloc_tos(), pqt, &inbuf, 0);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto cleanup;
+       }
+
+       status = smb2cli_set_info(
+           cli->conn, cli->timeout, cli->smb2.session, cli->smb2.tcon,
+           2,                               /* in_info_type */
+           SMB_FS_QUOTA_INFORMATION - 1000, /* in_file_info_class */
+           &inbuf,                          /* in_input_buffer */
+           0,                               /* in_additional_info */
+           ph->fid_persistent, ph->fid_volatile);
+cleanup:
+       cli->raw_status = status;
+
        TALLOC_FREE(frame);
        return status;
 }
@@ -3227,7 +3372,7 @@ static struct tevent_req *cli_smb2_shadow_copy_data_fnum_send(
                                        bool get_names)
 {
        struct tevent_req *req, *subreq;
-       struct cli_smb2_close_fnum_state *state;
+       struct cli_smb2_shadow_copy_data_fnum_state *state;
        NTSTATUS status;
 
        req = tevent_req_create(mem_ctx, &state,
@@ -3429,6 +3574,73 @@ NTSTATUS cli_smb2_shadow_copy_data(TALLOC_CTX *mem_ctx,
                                                pnames,
                                                pnum_names);
  fail:
+       cli->raw_status = status;
+
+       TALLOC_FREE(frame);
+       return status;
+}
+
+/***************************************************************
+ Wrapper that allows SMB2 to truncate a file.
+ Synchronous only.
+***************************************************************/
+
+NTSTATUS cli_smb2_ftruncate(struct cli_state *cli,
+                       uint16_t fnum,
+                       uint64_t newsize)
+{
+       NTSTATUS status;
+       DATA_BLOB inbuf = data_blob_null;
+       struct smb2_hnd *ph = NULL;
+       TALLOC_CTX *frame = talloc_stackframe();
+
+       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;
+       }
+
+       if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
+       }
+
+       status = map_fnum_to_smb2_handle(cli,
+                                       fnum,
+                                       &ph);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto fail;
+       }
+
+       inbuf = data_blob_talloc_zero(frame, 8);
+       if (inbuf.data == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       SBVAL(inbuf.data, 0, newsize);
+
+       /* setinfo on the handle with info_type SMB2_SETINFO_FILE (1),
+          level 20 (SMB_FILE_END_OF_FILE_INFORMATION - 1000). */
+
+       status = smb2cli_set_info(cli->conn,
+                               cli->timeout,
+                               cli->smb2.session,
+                               cli->smb2.tcon,
+                               1, /* in_info_type */
+                                       /* in_file_info_class */
+                               SMB_FILE_END_OF_FILE_INFORMATION - 1000,
+                               &inbuf, /* in_input_buffer */
+                               0, /* in_additional_info */
+                               ph->fid_persistent,
+                               ph->fid_volatile);
+
+  fail:
+
+       cli->raw_status = status;
+
        TALLOC_FREE(frame);
        return status;
 }