s3:smbd/connection: use smbXsrv_tcon_traverse to count connections
[kai/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         uint64_t in_file_id_persistent;
39         uint64_t in_file_id_volatile;
40         struct files_struct *in_fsp;
41         struct tevent_req *subreq;
42
43         status = smbd_smb2_request_verify_sizes(req, 0x18);
44         if (!NT_STATUS_IS_OK(status)) {
45                 return smbd_smb2_request_error(req, status);
46         }
47         inbody = SMBD_SMB2_IN_BODY_PTR(req);
48
49         in_file_id_persistent   = BVAL(inbody, 0x08);
50         in_file_id_volatile     = BVAL(inbody, 0x10);
51
52         in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
53         if (in_fsp == NULL) {
54                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
55         }
56
57         subreq = smbd_smb2_flush_send(req, req->sconn->ev_ctx,
58                                       req, in_fsp);
59         if (subreq == NULL) {
60                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
61         }
62         tevent_req_set_callback(subreq, smbd_smb2_request_flush_done, req);
63
64         return smbd_smb2_request_pending_queue(req, subreq, 500);
65 }
66
67 static void smbd_smb2_request_flush_done(struct tevent_req *subreq)
68 {
69         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
70                                         struct smbd_smb2_request);
71         DATA_BLOB outbody;
72         NTSTATUS status;
73         NTSTATUS error; /* transport error */
74
75         status = smbd_smb2_flush_recv(subreq);
76         TALLOC_FREE(subreq);
77         if (!NT_STATUS_IS_OK(status)) {
78                 error = smbd_smb2_request_error(req, status);
79                 if (!NT_STATUS_IS_OK(error)) {
80                         smbd_server_connection_terminate(req->sconn,
81                                                          nt_errstr(error));
82                         return;
83                 }
84                 return;
85         }
86
87         outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
88         if (outbody.data == NULL) {
89                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
90                 if (!NT_STATUS_IS_OK(error)) {
91                         smbd_server_connection_terminate(req->sconn,
92                                                          nt_errstr(error));
93                         return;
94                 }
95                 return;
96         }
97
98         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
99         SSVAL(outbody.data, 0x02, 0);           /* reserved */
100
101         error = smbd_smb2_request_done(req, outbody, NULL);
102         if (!NT_STATUS_IS_OK(error)) {
103                 smbd_server_connection_terminate(req->sconn,
104                                                  nt_errstr(error));
105                 return;
106         }
107 }
108
109 struct smbd_smb2_flush_state {
110         struct smbd_smb2_request *smb2req;
111 };
112
113 static struct tevent_req *smbd_smb2_flush_send(TALLOC_CTX *mem_ctx,
114                                                struct tevent_context *ev,
115                                                struct smbd_smb2_request *smb2req,
116                                                struct files_struct *fsp)
117 {
118         struct tevent_req *req;
119         struct smbd_smb2_flush_state *state;
120         NTSTATUS status;
121         struct smb_request *smbreq;
122
123         req = tevent_req_create(mem_ctx, &state,
124                                 struct smbd_smb2_flush_state);
125         if (req == NULL) {
126                 return NULL;
127         }
128         state->smb2req = smb2req;
129
130         DEBUG(10,("smbd_smb2_flush: %s - %s\n",
131                   fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
132
133         smbreq = smbd_smb2_fake_smb_request(smb2req);
134         if (tevent_req_nomem(smbreq, req)) {
135                 return tevent_req_post(req, ev);
136         }
137
138         if (IS_IPC(smbreq->conn)) {
139                 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
140                 return tevent_req_post(req, ev);
141         }
142
143         if (!CHECK_WRITE(fsp)) {
144                 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
145                 return tevent_req_post(req, ev);
146         }
147
148         status = sync_file(smbreq->conn, fsp, true);
149         if (!NT_STATUS_IS_OK(status)) {
150                 DEBUG(5,("smbd_smb2_flush: sync_file for %s returned %s\n",
151                          fsp_str_dbg(fsp), nt_errstr(status)));
152                 tevent_req_nterror(req, status);
153                 return tevent_req_post(req, ev);
154         }
155
156         tevent_req_done(req);
157         return tevent_req_post(req, ev);
158 }
159
160 static NTSTATUS smbd_smb2_flush_recv(struct tevent_req *req)
161 {
162         NTSTATUS status;
163
164         if (tevent_req_is_nterror(req, &status)) {
165                 tevent_req_received(req);
166                 return status;
167         }
168
169         tevent_req_received(req);
170         return NT_STATUS_OK;
171 }