s3:smb2_flush: make use of file_fsp_smb2()
[ddiss/samba.git] / source3 / smbd / smb2_flush.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../lib/util/tevent_ntstatus.h"
26
27 static struct tevent_req *smbd_smb2_flush_send(TALLOC_CTX *mem_ctx,
28                                                struct tevent_context *ev,
29                                                struct smbd_smb2_request *smb2req,
30                                                struct files_struct *fsp);
31 static NTSTATUS smbd_smb2_flush_recv(struct tevent_req *req);
32
33 static void smbd_smb2_request_flush_done(struct tevent_req *subreq);
34 NTSTATUS smbd_smb2_request_process_flush(struct smbd_smb2_request *req)
35 {
36         NTSTATUS status;
37         const uint8_t *inbody;
38         int i = req->current_idx;
39         uint64_t in_file_id_persistent;
40         uint64_t in_file_id_volatile;
41         struct files_struct *in_fsp;
42         struct tevent_req *subreq;
43
44         status = smbd_smb2_request_verify_sizes(req, 0x18);
45         if (!NT_STATUS_IS_OK(status)) {
46                 return smbd_smb2_request_error(req, status);
47         }
48         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
49
50         in_file_id_persistent   = BVAL(inbody, 0x08);
51         in_file_id_volatile     = BVAL(inbody, 0x10);
52
53         in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
54         if (in_fsp == NULL) {
55                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
56         }
57
58         subreq = smbd_smb2_flush_send(req, req->sconn->smb2.event_ctx,
59                                       req, in_fsp);
60         if (subreq == NULL) {
61                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
62         }
63         tevent_req_set_callback(subreq, smbd_smb2_request_flush_done, req);
64
65         return smbd_smb2_request_pending_queue(req, subreq);
66 }
67
68 static void smbd_smb2_request_flush_done(struct tevent_req *subreq)
69 {
70         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
71                                         struct smbd_smb2_request);
72         DATA_BLOB outbody;
73         NTSTATUS status;
74         NTSTATUS error; /* transport error */
75
76         status = smbd_smb2_flush_recv(subreq);
77         TALLOC_FREE(subreq);
78         if (!NT_STATUS_IS_OK(status)) {
79                 error = smbd_smb2_request_error(req, status);
80                 if (!NT_STATUS_IS_OK(error)) {
81                         smbd_server_connection_terminate(req->sconn,
82                                                          nt_errstr(error));
83                         return;
84                 }
85                 return;
86         }
87
88         outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
89         if (outbody.data == NULL) {
90                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
91                 if (!NT_STATUS_IS_OK(error)) {
92                         smbd_server_connection_terminate(req->sconn,
93                                                          nt_errstr(error));
94                         return;
95                 }
96                 return;
97         }
98
99         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
100         SSVAL(outbody.data, 0x02, 0);           /* reserved */
101
102         error = smbd_smb2_request_done(req, outbody, NULL);
103         if (!NT_STATUS_IS_OK(error)) {
104                 smbd_server_connection_terminate(req->sconn,
105                                                  nt_errstr(error));
106                 return;
107         }
108 }
109
110 struct smbd_smb2_flush_state {
111         struct smbd_smb2_request *smb2req;
112 };
113
114 static struct tevent_req *smbd_smb2_flush_send(TALLOC_CTX *mem_ctx,
115                                                struct tevent_context *ev,
116                                                struct smbd_smb2_request *smb2req,
117                                                struct files_struct *fsp)
118 {
119         struct tevent_req *req;
120         struct smbd_smb2_flush_state *state;
121         NTSTATUS status;
122         struct smb_request *smbreq;
123
124         req = tevent_req_create(mem_ctx, &state,
125                                 struct smbd_smb2_flush_state);
126         if (req == NULL) {
127                 return NULL;
128         }
129         state->smb2req = smb2req;
130
131         DEBUG(10,("smbd_smb2_flush: %s - fnum[%d]\n",
132                   fsp_str_dbg(fsp), fsp->fnum));
133
134         smbreq = smbd_smb2_fake_smb_request(smb2req);
135         if (tevent_req_nomem(smbreq, req)) {
136                 return tevent_req_post(req, ev);
137         }
138
139         if (IS_IPC(smbreq->conn)) {
140                 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
141                 return tevent_req_post(req, ev);
142         }
143
144         if (!CHECK_WRITE(fsp)) {
145                 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
146                 return tevent_req_post(req, ev);
147         }
148
149         status = sync_file(smbreq->conn, fsp, true);
150         if (!NT_STATUS_IS_OK(status)) {
151                 DEBUG(5,("smbd_smb2_flush: sync_file for %s returned %s\n",
152                          fsp_str_dbg(fsp), nt_errstr(status)));
153                 tevent_req_nterror(req, status);
154                 return tevent_req_post(req, ev);
155         }
156
157         tevent_req_done(req);
158         return tevent_req_post(req, ev);
159 }
160
161 static NTSTATUS smbd_smb2_flush_recv(struct tevent_req *req)
162 {
163         NTSTATUS status;
164
165         if (tevent_req_is_nterror(req, &status)) {
166                 tevent_req_received(req);
167                 return status;
168         }
169
170         tevent_req_received(req);
171         return NT_STATUS_OK;
172 }