s3: include smbd/smbd.h where needed.
[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
26 static struct tevent_req *smbd_smb2_flush_send(TALLOC_CTX *mem_ctx,
27                                                struct tevent_context *ev,
28                                                struct smbd_smb2_request *smb2req,
29                                                uint64_t in_file_id_volatile);
30 static NTSTATUS smbd_smb2_flush_recv(struct tevent_req *req);
31
32 static void smbd_smb2_request_flush_done(struct tevent_req *subreq);
33 NTSTATUS smbd_smb2_request_process_flush(struct smbd_smb2_request *req)
34 {
35         const uint8_t *inhdr;
36         const uint8_t *inbody;
37         int i = req->current_idx;
38         size_t expected_body_size = 0x18;
39         size_t body_size;
40         uint64_t in_file_id_persistent;
41         uint64_t in_file_id_volatile;
42         struct tevent_req *subreq;
43
44         inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
45         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
46                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
47         }
48
49         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
50
51         body_size = SVAL(inbody, 0x00);
52         if (body_size != expected_body_size) {
53                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
54         }
55
56         in_file_id_persistent   = BVAL(inbody, 0x08);
57         in_file_id_volatile     = BVAL(inbody, 0x10);
58
59         if (req->compat_chain_fsp) {
60                 /* skip check */
61         } else if (in_file_id_persistent != in_file_id_volatile) {
62                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
63         }
64
65         subreq = smbd_smb2_flush_send(req,
66                                       req->sconn->smb2.event_ctx,
67                                       req,
68                                       in_file_id_volatile);
69         if (subreq == NULL) {
70                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
71         }
72         tevent_req_set_callback(subreq, smbd_smb2_request_flush_done, req);
73
74         return smbd_smb2_request_pending_queue(req, subreq);
75 }
76
77 static void smbd_smb2_request_flush_done(struct tevent_req *subreq)
78 {
79         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
80                                         struct smbd_smb2_request);
81         DATA_BLOB outbody;
82         NTSTATUS status;
83         NTSTATUS error; /* transport error */
84
85         status = smbd_smb2_flush_recv(subreq);
86         TALLOC_FREE(subreq);
87         if (!NT_STATUS_IS_OK(status)) {
88                 error = smbd_smb2_request_error(req, status);
89                 if (!NT_STATUS_IS_OK(error)) {
90                         smbd_server_connection_terminate(req->sconn,
91                                                          nt_errstr(error));
92                         return;
93                 }
94                 return;
95         }
96
97         outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
98         if (outbody.data == NULL) {
99                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
100                 if (!NT_STATUS_IS_OK(error)) {
101                         smbd_server_connection_terminate(req->sconn,
102                                                          nt_errstr(error));
103                         return;
104                 }
105                 return;
106         }
107
108         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
109         SSVAL(outbody.data, 0x02, 0);           /* reserved */
110
111         error = smbd_smb2_request_done(req, outbody, NULL);
112         if (!NT_STATUS_IS_OK(error)) {
113                 smbd_server_connection_terminate(req->sconn,
114                                                  nt_errstr(error));
115                 return;
116         }
117 }
118
119 struct smbd_smb2_flush_state {
120         struct smbd_smb2_request *smb2req;
121 };
122
123 static struct tevent_req *smbd_smb2_flush_send(TALLOC_CTX *mem_ctx,
124                                                struct tevent_context *ev,
125                                                struct smbd_smb2_request *smb2req,
126                                                uint64_t in_file_id_volatile)
127 {
128         struct tevent_req *req;
129         struct smbd_smb2_flush_state *state;
130         NTSTATUS status;
131         struct smb_request *smbreq;
132         files_struct *fsp;
133
134         req = tevent_req_create(mem_ctx, &state,
135                                 struct smbd_smb2_flush_state);
136         if (req == NULL) {
137                 return NULL;
138         }
139         state->smb2req = smb2req;
140
141         DEBUG(10,("smbd_smb2_flush: file_id[0x%016llX]\n",
142                   (unsigned long long)in_file_id_volatile));
143
144         smbreq = smbd_smb2_fake_smb_request(smb2req);
145         if (tevent_req_nomem(smbreq, req)) {
146                 return tevent_req_post(req, ev);
147         }
148
149         if (IS_IPC(smbreq->conn)) {
150                 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
151                 return tevent_req_post(req, ev);
152         }
153
154         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
155         if (fsp == NULL) {
156                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
157                 return tevent_req_post(req, ev);
158         }
159         if (smbreq->conn != fsp->conn) {
160                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
161                 return tevent_req_post(req, ev);
162         }
163         if (smb2req->session->vuid != fsp->vuid) {
164                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
165                 return tevent_req_post(req, ev);
166         }
167
168         if (!CHECK_WRITE(fsp)) {
169                 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
170                 return tevent_req_post(req, ev);
171         }
172
173         status = sync_file(smbreq->conn, fsp, true);
174         if (!NT_STATUS_IS_OK(status)) {
175                 DEBUG(5,("smbd_smb2_flush: sync_file for %s returned %s\n",
176                          fsp_str_dbg(fsp), nt_errstr(status)));
177                 tevent_req_nterror(req, status);
178                 return tevent_req_post(req, ev);
179         }
180
181         tevent_req_done(req);
182         return tevent_req_post(req, ev);
183 }
184
185 static NTSTATUS smbd_smb2_flush_recv(struct tevent_req *req)
186 {
187         NTSTATUS status;
188
189         if (tevent_req_is_nterror(req, &status)) {
190                 tevent_req_received(req);
191                 return status;
192         }
193
194         tevent_req_received(req);
195         return NT_STATUS_OK;
196 }