2 Unix SMB/CIFS implementation.
4 Copyright (C) Gregor Beck 2013
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "system/network.h"
22 #include "lib/util/tevent_ntstatus.h"
23 #include "smb_common.h"
24 #include "smbXcli_base.h"
26 struct smb1cli_writex_state
{
35 static void smb1cli_writex_done(struct tevent_req
*subreq
);
38 * Send an asynchrounus SMB_COM_WRITE_ANDX request.
39 * <a href="http://msdn.microsoft.com/en-us/library/ee441954.aspx">MS-CIFS 2.2.4.43.1</a>
40 * @see smb1cli_writex_recv(), smb1cli_writex()
42 * @param[in] mem_ctx The memory context for the result.
43 * @param[in] ev The event context to work on.
44 * @param[in] conn The smb connection.
45 * @param[in] timeout_msec If positiv a timeout for the request.
46 * @param[in] pid The process identifier
47 * @param[in] tcon The smb tree connect.
48 * @param[in] session The smb session.
49 * @param[in] fnum The file id of the file the data should be written to.
50 * @param[in] mode A bitfield containing the write mode.
51 * @param[in] buf The data to be written to the file.
52 * @param[in] offset The offset in bytes from the begin of file where to write.
53 * @param[in] size The number of bytes to write.
55 * @return a tevent_req or NULL
57 struct tevent_req
*smb1cli_writex_send(TALLOC_CTX
*mem_ctx
,
58 struct tevent_context
*ev
,
59 struct smbXcli_conn
*conn
,
60 uint32_t timeout_msec
,
62 struct smbXcli_tcon
*tcon
,
63 struct smbXcli_session
*session
,
70 struct tevent_req
*req
, *subreq
;
71 struct smb1cli_writex_state
*state
;
72 bool bigoffset
= ((smb1cli_conn_capabilities(conn
) & CAP_LARGE_FILES
) != 0);
73 uint8_t wct
= bigoffset
? 14 : 12;
75 uint16_t data_offset
=
76 smb1cli_req_wct_ofs(NULL
, 0) /* reqs_before */
77 + 1 /* the wct field */
79 + 2 /* num_bytes field */
83 req
= tevent_req_create(mem_ctx
, &state
, struct smb1cli_writex_state
);
92 SCVAL(vwv
+0, 0, 0xFF);
95 SSVAL(vwv
+2, 0, fnum
);
96 SIVAL(vwv
+3, 0, offset
);
98 SSVAL(vwv
+7, 0, mode
);
100 SSVAL(vwv
+9, 0, (state
->size
>>16));
101 SSVAL(vwv
+10, 0, state
->size
);
102 SSVAL(vwv
+11, 0, data_offset
);
105 SIVAL(vwv
+12, 0, (((uint64_t)offset
)>>32) & 0xffffffff);
109 state
->iov
[0].iov_base
= (void *)&state
->pad
;
110 state
->iov
[0].iov_len
= 1;
111 state
->iov
[1].iov_base
= discard_const_p(void, buf
);
112 state
->iov
[1].iov_len
= state
->size
;
114 subreq
= smb1cli_req_create(state
, ev
, conn
, SMBwriteX
,
117 timeout_msec
, pid
, tcon
, session
,
119 ARRAY_SIZE(state
->iov
), state
->iov
);
120 if (tevent_req_nomem(subreq
, req
)) {
121 return tevent_req_post(req
, ev
);
123 tevent_req_set_callback(subreq
, smb1cli_writex_done
, req
);
125 status
= smb1cli_req_chain_submit(&subreq
, 1);
126 if (tevent_req_nterror(req
, status
)) {
127 return tevent_req_post(req
, ev
);
133 static void smb1cli_writex_done(struct tevent_req
*subreq
)
135 struct tevent_req
*req
= tevent_req_callback_data(
136 subreq
, struct tevent_req
);
137 struct smb1cli_writex_state
*state
= tevent_req_data(
138 req
, struct smb1cli_writex_state
);
139 struct iovec
*recv_iov
= NULL
;
143 static const struct smb1cli_req_expected_response expected
[] = {
145 .status
= NT_STATUS_OK
,
150 status
= smb1cli_req_recv(subreq
, state
,
155 NULL
, /* pvwv_offset */
156 NULL
, /* num_bytes */
158 NULL
, /* pbytes_offset */
160 expected
, ARRAY_SIZE(expected
));
162 if (tevent_req_nterror(req
, status
)) {
166 state
->written
= SVAL(vwv
+2, 0);
167 if (state
->size
> UINT16_MAX
) {
169 * It is important that we only set the
170 * high bits only if we asked for a large write.
172 * OS/2 print shares get this wrong and may send
177 state
->written
|= SVAL(vwv
+4, 0)<<16;
179 state
->available
= SVAL(vwv
+3, 0);
181 tevent_req_done(req
);
185 * Receive the response to an asynchronous SMB_COM_WRITE_ANDX request.
186 * <a href="http://msdn.microsoft.com/en-us/library/ee441673.aspx">MS-CIFS:2.2.4.43.2</a>
189 * @param[in] req req A tevent request created with smb1cli_writex_send()
190 * @param[out] pwritten The number of bytes written to the file.
191 * @param[out] pavailable Valid if writing to a named pipe or IO device.
193 * @return NT_STATUS_OK on succsess.
195 NTSTATUS
smb1cli_writex_recv(struct tevent_req
*req
, uint32_t *pwritten
, uint16_t *pavailable
)
197 struct smb1cli_writex_state
*state
= tevent_req_data(
198 req
, struct smb1cli_writex_state
);
201 if (tevent_req_is_nterror(req
, &status
)) {
204 if (pwritten
!= NULL
) {
205 *pwritten
= state
->written
;
207 if (pavailable
!= NULL
) {
208 *pavailable
= state
->available
;
214 * Send an synchrounus SMB_COM_WRITE_ANDX request.
215 * <a href="http://msdn.microsoft.com/en-us/library/ee441848.aspx">MS-CIFS 2.2.4.43</a>
216 * @see smb1cli_writex_send(), smb1cli_writex_recv()
218 * @param[in] conn The smb connection.
219 * @param[in] timeout_msec If positiv a timeout for the request.
220 * @param[in] pid The process identifier
221 * @param[in] tcon The smb tree connect.
222 * @param[in] session The smb session.
223 * @param[in] fnum The file id of the file the data should be written to.
224 * @param[in] mode A bitfield containing the write mode.
225 * @param[in] buf The data to be written to the file.
226 * @param[in] offset The offset in bytes from the begin of file where to write.
227 * @param[in] size The number of bytes to write.
228 * @param[out] pwritten The number of bytes written to the file.
229 * @param[out] pavailable Valid if writing to a named pipe or IO device.
231 * @return NT_STATUS_OK on succsess.
233 NTSTATUS
smb1cli_writex(struct smbXcli_conn
*conn
,
234 uint32_t timeout_msec
,
236 struct smbXcli_tcon
*tcon
,
237 struct smbXcli_session
*session
,
244 uint16_t *pavailable
)
246 TALLOC_CTX
*frame
= NULL
;
247 struct tevent_context
*ev
;
248 struct tevent_req
*req
;
249 NTSTATUS status
= NT_STATUS_OK
;
251 frame
= talloc_stackframe();
253 if (smbXcli_conn_has_async_calls(conn
)) {
255 * Can't use sync call while an async call is in flight
257 status
= NT_STATUS_INVALID_PARAMETER
;
261 ev
= samba_tevent_context_init(frame
);
263 status
= NT_STATUS_NO_MEMORY
;
267 req
= smb1cli_writex_send(frame
, ev
, conn
,
270 fnum
, mode
, buf
, offset
, size
);
272 status
= NT_STATUS_NO_MEMORY
;
276 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
280 status
= smb1cli_writex_recv(req
, pwritten
, pavailable
);