2 Unix SMB/CIFS implementation.
3 client transaction calls
4 Copyright (C) Andrew Tridgell 1994-1998
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 "../lib/util/tevent_ntstatus.h"
22 #include "async_smb.h"
24 struct trans_recvblob
{
26 uint32_t max
, total
, received
;
29 struct cli_trans_state
{
30 struct cli_state
*cli
;
31 struct event_context
*ev
;
35 const char *pipe_name
;
36 uint8_t *pipe_name_conv
;
37 size_t pipe_name_conv_len
;
42 uint8_t num_setup
, max_setup
;
44 uint32_t num_param
, param_sent
;
46 uint32_t num_data
, data_sent
;
50 struct trans_recvblob rparam
;
51 struct trans_recvblob rdata
;
54 TALLOC_CTX
*secondary_request_ctx
;
61 static NTSTATUS
cli_pull_trans(uint8_t *inbuf
,
62 uint8_t wct
, uint16_t *vwv
,
63 uint16_t num_bytes
, uint8_t *bytes
,
64 uint8_t smb_cmd
, bool expect_first_reply
,
65 uint8_t *pnum_setup
, uint16_t **psetup
,
66 uint32_t *ptotal_param
, uint32_t *pnum_param
,
67 uint32_t *pparam_disp
, uint8_t **pparam
,
68 uint32_t *ptotal_data
, uint32_t *pnum_data
,
69 uint32_t *pdata_disp
, uint8_t **pdata
)
71 uint32_t param_ofs
, data_ofs
;
73 if (expect_first_reply
) {
74 if ((wct
!= 0) || (num_bytes
!= 0)) {
75 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
84 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
86 *ptotal_param
= SVAL(vwv
+ 0, 0);
87 *ptotal_data
= SVAL(vwv
+ 1, 0);
88 *pnum_param
= SVAL(vwv
+ 3, 0);
89 param_ofs
= SVAL(vwv
+ 4, 0);
90 *pparam_disp
= SVAL(vwv
+ 5, 0);
91 *pnum_data
= SVAL(vwv
+ 6, 0);
92 data_ofs
= SVAL(vwv
+ 7, 0);
93 *pdata_disp
= SVAL(vwv
+ 8, 0);
94 *pnum_setup
= CVAL(vwv
+ 9, 0);
95 if (wct
< 10 + (*pnum_setup
)) {
96 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
103 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
105 *ptotal_param
= IVAL(vwv
, 3);
106 *ptotal_data
= IVAL(vwv
, 7);
107 *pnum_param
= IVAL(vwv
, 11);
108 param_ofs
= IVAL(vwv
, 15);
109 *pparam_disp
= IVAL(vwv
, 19);
110 *pnum_data
= IVAL(vwv
, 23);
111 data_ofs
= IVAL(vwv
, 27);
112 *pdata_disp
= IVAL(vwv
, 31);
113 *pnum_setup
= CVAL(vwv
, 35);
118 return NT_STATUS_INTERNAL_ERROR
;
122 * Check for buffer overflows. data_ofs needs to be checked against
123 * the incoming buffer length, data_disp against the total
124 * length. Likewise for param_ofs/param_disp.
127 if (trans_oob(smb_len(inbuf
), param_ofs
, *pnum_param
)
128 || trans_oob(*ptotal_param
, *pparam_disp
, *pnum_param
)
129 || trans_oob(smb_len(inbuf
), data_ofs
, *pnum_data
)
130 || trans_oob(*ptotal_data
, *pdata_disp
, *pnum_data
)) {
131 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
134 *pparam
= (uint8_t *)inbuf
+ 4 + param_ofs
;
135 *pdata
= (uint8_t *)inbuf
+ 4 + data_ofs
;
140 static NTSTATUS
cli_trans_pull_blob(TALLOC_CTX
*mem_ctx
,
141 struct trans_recvblob
*blob
,
142 uint32_t total
, uint32_t thistime
,
143 uint8_t *buf
, uint32_t displacement
)
145 if (blob
->data
== NULL
) {
146 if (total
> blob
->max
) {
147 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
150 blob
->data
= TALLOC_ARRAY(mem_ctx
, uint8_t, total
);
151 if (blob
->data
== NULL
) {
152 return NT_STATUS_NO_MEMORY
;
156 if (total
> blob
->total
) {
157 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
161 memcpy(blob
->data
+ displacement
, buf
, thistime
);
162 blob
->received
+= thistime
;
168 static void cli_trans_format(struct cli_trans_state
*state
, uint8_t *pwct
,
172 struct iovec
*iov
= state
->iov
;
173 uint8_t *pad
= state
->pad
;
174 uint16_t *vwv
= state
->vwv
;
175 uint16_t param_offset
;
176 uint16_t this_param
= 0;
177 uint16_t this_data
= 0;
178 uint32_t useable_space
;
183 if ((state
->param_sent
!= 0) || (state
->data_sent
!= 0)) {
184 /* The secondary commands are one after the primary ones */
188 param_offset
= smb_size
- 4;
193 iov
[0].iov_base
= (void *)pad
;
195 iov
[1].iov_base
= (void *)state
->pipe_name_conv
;
196 iov
[1].iov_len
= state
->pipe_name_conv_len
;
197 wct
= 14 + state
->num_setup
;
198 param_offset
+= iov
[0].iov_len
+ iov
[1].iov_len
;
203 pad
[1] = 'D'; /* Copy this from "old" 3.0 behaviour */
205 iov
[0].iov_base
= (void *)pad
;
207 wct
= 14 + state
->num_setup
;
218 wct
= 19 + state
->num_setup
;
225 useable_space
= state
->cli
->max_xmit
- smb_size
- sizeof(uint16_t)*wct
;
227 if (state
->param_sent
< state
->num_param
) {
228 this_param
= MIN(state
->num_param
- state
->param_sent
,
230 iov
[0].iov_base
= (void *)(state
->param
+ state
->param_sent
);
231 iov
[0].iov_len
= this_param
;
235 if (state
->data_sent
< state
->num_data
) {
236 this_data
= MIN(state
->num_data
- state
->data_sent
,
237 useable_space
- this_param
);
238 iov
[0].iov_base
= (void *)(state
->data
+ state
->data_sent
);
239 iov
[0].iov_len
= this_data
;
243 param_offset
+= wct
* sizeof(uint16_t);
245 DEBUG(10, ("num_setup=%u, max_setup=%u, "
246 "param_total=%u, this_param=%u, max_param=%u, "
247 "data_total=%u, this_data=%u, max_data=%u, "
248 "param_offset=%u, param_disp=%u, data_disp=%u\n",
249 (unsigned)state
->num_setup
, (unsigned)state
->max_setup
,
250 (unsigned)state
->num_param
, (unsigned)this_param
,
251 (unsigned)state
->rparam
.max
,
252 (unsigned)state
->num_data
, (unsigned)this_data
,
253 (unsigned)state
->rdata
.max
,
254 (unsigned)param_offset
,
255 (unsigned)state
->param_sent
, (unsigned)state
->data_sent
));
260 SSVAL(vwv
+ 0, 0, state
->num_param
);
261 SSVAL(vwv
+ 1, 0, state
->num_data
);
262 SSVAL(vwv
+ 2, 0, state
->rparam
.max
);
263 SSVAL(vwv
+ 3, 0, state
->rdata
.max
);
264 SCVAL(vwv
+ 4, 0, state
->max_setup
);
265 SCVAL(vwv
+ 4, 1, 0); /* reserved */
266 SSVAL(vwv
+ 5, 0, state
->flags
);
267 SIVAL(vwv
+ 6, 0, 0); /* timeout */
268 SSVAL(vwv
+ 8, 0, 0); /* reserved */
269 SSVAL(vwv
+ 9, 0, this_param
);
270 SSVAL(vwv
+10, 0, param_offset
);
271 SSVAL(vwv
+11, 0, this_data
);
272 SSVAL(vwv
+12, 0, param_offset
+ this_param
);
273 SCVAL(vwv
+13, 0, state
->num_setup
);
274 SCVAL(vwv
+13, 1, 0); /* reserved */
275 memcpy(vwv
+ 14, state
->setup
,
276 sizeof(uint16_t) * state
->num_setup
);
280 SSVAL(vwv
+ 0, 0, state
->num_param
);
281 SSVAL(vwv
+ 1, 0, state
->num_data
);
282 SSVAL(vwv
+ 2, 0, this_param
);
283 SSVAL(vwv
+ 3, 0, param_offset
);
284 SSVAL(vwv
+ 4, 0, state
->param_sent
);
285 SSVAL(vwv
+ 5, 0, this_data
);
286 SSVAL(vwv
+ 6, 0, param_offset
+ this_param
);
287 SSVAL(vwv
+ 7, 0, state
->data_sent
);
288 if (cmd
== SMBtranss2
) {
289 SSVAL(vwv
+ 8, 0, state
->fid
);
293 SCVAL(vwv
, 0, state
->max_setup
);
294 SSVAL(vwv
, 1, 0); /* reserved */
295 SIVAL(vwv
, 3, state
->num_param
);
296 SIVAL(vwv
, 7, state
->num_data
);
297 SIVAL(vwv
, 11, state
->rparam
.max
);
298 SIVAL(vwv
, 15, state
->rdata
.max
);
299 SIVAL(vwv
, 19, this_param
);
300 SIVAL(vwv
, 23, param_offset
);
301 SIVAL(vwv
, 27, this_data
);
302 SIVAL(vwv
, 31, param_offset
+ this_param
);
303 SCVAL(vwv
, 35, state
->num_setup
);
304 SSVAL(vwv
, 36, state
->function
);
305 memcpy(vwv
+ 19, state
->setup
,
306 sizeof(uint16_t) * state
->num_setup
);
309 SSVAL(vwv
, 0, 0); /* reserved */
310 SCVAL(vwv
, 2, 0); /* reserved */
311 SIVAL(vwv
, 3, state
->num_param
);
312 SIVAL(vwv
, 7, state
->num_data
);
313 SIVAL(vwv
, 11, this_param
);
314 SIVAL(vwv
, 15, param_offset
);
315 SIVAL(vwv
, 19, state
->param_sent
);
316 SIVAL(vwv
, 23, this_data
);
317 SIVAL(vwv
, 27, param_offset
+ this_param
);
318 SIVAL(vwv
, 31, state
->data_sent
);
319 SCVAL(vwv
, 35, 0); /* reserved */
323 state
->param_sent
+= this_param
;
324 state
->data_sent
+= this_data
;
327 *piov_count
= iov
- state
->iov
;
330 static void cli_trans_done(struct tevent_req
*subreq
);
332 struct tevent_req
*cli_trans_send(
333 TALLOC_CTX
*mem_ctx
, struct event_context
*ev
,
334 struct cli_state
*cli
, uint8_t cmd
,
335 const char *pipe_name
, uint16_t fid
, uint16_t function
, int flags
,
336 uint16_t *setup
, uint8_t num_setup
, uint8_t max_setup
,
337 uint8_t *param
, uint32_t num_param
, uint32_t max_param
,
338 uint8_t *data
, uint32_t num_data
, uint32_t max_data
)
340 struct tevent_req
*req
, *subreq
;
341 struct cli_trans_state
*state
;
346 req
= tevent_req_create(mem_ctx
, &state
, struct cli_trans_state
);
351 if ((cmd
== SMBtrans
) || (cmd
== SMBtrans2
)) {
352 if ((num_param
> 0xffff) || (max_param
> 0xffff)
353 || (num_data
> 0xffff) || (max_data
> 0xffff)) {
354 DEBUG(3, ("Attempt to send invalid trans2 request "
355 "(setup %u, params %u/%u, data %u/%u)\n",
357 (unsigned)num_param
, (unsigned)max_param
,
358 (unsigned)num_data
, (unsigned)max_data
));
359 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
360 return tevent_req_post(req
, ev
);
365 * The largest wct will be for nttrans (19+num_setup). Make sure we
366 * don't overflow state->vwv in cli_trans_format.
369 if ((num_setup
+ 19) > ARRAY_SIZE(state
->vwv
)) {
370 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
371 return tevent_req_post(req
, ev
);
377 state
->flags
= flags
;
378 state
->num_rsetup
= 0;
379 state
->rsetup
= NULL
;
380 ZERO_STRUCT(state
->rparam
);
381 ZERO_STRUCT(state
->rdata
);
383 if ((pipe_name
!= NULL
)
384 && (!convert_string_talloc(state
, CH_UNIX
,
385 cli_ucs2(cli
) ? CH_UTF16LE
: CH_DOS
,
386 pipe_name
, strlen(pipe_name
) + 1,
387 &state
->pipe_name_conv
,
388 &state
->pipe_name_conv_len
, true))) {
389 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
390 return tevent_req_post(req
, ev
);
392 state
->fid
= fid
; /* trans2 */
393 state
->function
= function
; /* nttrans */
395 state
->setup
= setup
;
396 state
->num_setup
= num_setup
;
397 state
->max_setup
= max_setup
;
399 state
->param
= param
;
400 state
->num_param
= num_param
;
401 state
->param_sent
= 0;
402 state
->rparam
.max
= max_param
;
405 state
->num_data
= num_data
;
406 state
->data_sent
= 0;
407 state
->rdata
.max
= max_data
;
409 cli_trans_format(state
, &wct
, &iov_count
);
411 subreq
= cli_smb_req_create(state
, ev
, cli
, cmd
, 0, wct
, state
->vwv
,
412 iov_count
, state
->iov
);
413 if (tevent_req_nomem(subreq
, req
)) {
414 return tevent_req_post(req
, ev
);
416 state
->mid
= cli_smb_req_mid(subreq
);
417 status
= cli_smb_req_send(subreq
);
418 if (!NT_STATUS_IS_OK(status
)) {
419 tevent_req_nterror(req
, status
);
420 return tevent_req_post(req
, state
->ev
);
422 cli_state_seqnum_persistent(cli
, state
->mid
);
423 tevent_req_set_callback(subreq
, cli_trans_done
, req
);
427 static void cli_trans_done(struct tevent_req
*subreq
)
429 struct tevent_req
*req
= tevent_req_callback_data(
430 subreq
, struct tevent_req
);
431 struct cli_trans_state
*state
= tevent_req_data(
432 req
, struct cli_trans_state
);
440 uint8_t num_setup
= 0;
441 uint16_t *setup
= NULL
;
442 uint32_t total_param
= 0;
443 uint32_t num_param
= 0;
444 uint32_t param_disp
= 0;
445 uint32_t total_data
= 0;
446 uint32_t num_data
= 0;
447 uint32_t data_disp
= 0;
448 uint8_t *param
= NULL
;
449 uint8_t *data
= NULL
;
451 status
= cli_smb_recv(subreq
, state
, &inbuf
, 0, &wct
, &vwv
,
454 * Do not TALLOC_FREE(subreq) here, we might receive more than
455 * one response for the same mid.
459 * We can receive something like STATUS_MORE_ENTRIES, so don't use
460 * !NT_STATUS_IS_OK(status) here.
463 if (NT_STATUS_IS_ERR(status
)) {
467 sent_all
= ((state
->param_sent
== state
->num_param
)
468 && (state
->data_sent
== state
->num_data
));
470 status
= cli_pull_trans(
471 inbuf
, wct
, vwv
, num_bytes
, bytes
,
472 state
->cmd
, !sent_all
, &num_setup
, &setup
,
473 &total_param
, &num_param
, ¶m_disp
, ¶m
,
474 &total_data
, &num_data
, &data_disp
, &data
);
476 if (!NT_STATUS_IS_OK(status
)) {
485 cli_trans_format(state
, &wct
, &iov_count
);
487 subreq
= cli_smb_req_create(state
, state
->ev
, state
->cli
,
488 state
->cmd
+ 1, 0, wct
, state
->vwv
,
489 iov_count
, state
->iov
);
490 if (tevent_req_nomem(subreq
, req
)) {
493 cli_smb_req_set_mid(subreq
, state
->mid
);
495 status
= cli_smb_req_send(subreq
);
497 if (!NT_STATUS_IS_OK(status
)) {
500 tevent_req_set_callback(subreq
, cli_trans_done
, req
);
504 status
= cli_trans_pull_blob(
505 state
, &state
->rparam
, total_param
, num_param
, param
,
508 if (!NT_STATUS_IS_OK(status
)) {
509 DEBUG(10, ("Pulling params failed: %s\n", nt_errstr(status
)));
513 status
= cli_trans_pull_blob(
514 state
, &state
->rdata
, total_data
, num_data
, data
,
517 if (!NT_STATUS_IS_OK(status
)) {
518 DEBUG(10, ("Pulling data failed: %s\n", nt_errstr(status
)));
522 if ((state
->rparam
.total
== state
->rparam
.received
)
523 && (state
->rdata
.total
== state
->rdata
.received
)) {
524 state
->recv_flags2
= SVAL(inbuf
, smb_flg2
);
526 cli_state_seqnum_remove(state
->cli
, state
->mid
);
527 tevent_req_done(req
);
533 if (!cli_smb_req_set_pending(subreq
)) {
534 status
= NT_STATUS_NO_MEMORY
;
540 cli_state_seqnum_remove(state
->cli
, state
->mid
);
542 tevent_req_nterror(req
, status
);
545 NTSTATUS
cli_trans_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
546 uint16_t *recv_flags2
,
547 uint16_t **setup
, uint8_t min_setup
,
549 uint8_t **param
, uint32_t min_param
,
551 uint8_t **data
, uint32_t min_data
,
554 struct cli_trans_state
*state
= tevent_req_data(
555 req
, struct cli_trans_state
);
558 if (tevent_req_is_nterror(req
, &status
)) {
562 if ((state
->num_rsetup
< min_setup
)
563 || (state
->rparam
.total
< min_param
)
564 || (state
->rdata
.total
< min_data
)) {
565 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
568 if (recv_flags2
!= NULL
) {
569 *recv_flags2
= state
->recv_flags2
;
573 *setup
= talloc_move(mem_ctx
, &state
->rsetup
);
574 *num_setup
= state
->num_rsetup
;
576 TALLOC_FREE(state
->rsetup
);
580 *param
= talloc_move(mem_ctx
, &state
->rparam
.data
);
581 *num_param
= state
->rparam
.total
;
583 TALLOC_FREE(state
->rparam
.data
);
587 *data
= talloc_move(mem_ctx
, &state
->rdata
.data
);
588 *num_data
= state
->rdata
.total
;
590 TALLOC_FREE(state
->rdata
.data
);
596 NTSTATUS
cli_trans(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
598 const char *pipe_name
, uint16_t fid
, uint16_t function
,
600 uint16_t *setup
, uint8_t num_setup
, uint8_t max_setup
,
601 uint8_t *param
, uint32_t num_param
, uint32_t max_param
,
602 uint8_t *data
, uint32_t num_data
, uint32_t max_data
,
603 uint16_t *recv_flags2
,
604 uint16_t **rsetup
, uint8_t min_rsetup
, uint8_t *num_rsetup
,
605 uint8_t **rparam
, uint32_t min_rparam
, uint32_t *num_rparam
,
606 uint8_t **rdata
, uint32_t min_rdata
, uint32_t *num_rdata
)
608 TALLOC_CTX
*frame
= talloc_stackframe();
609 struct event_context
*ev
;
610 struct tevent_req
*req
;
611 NTSTATUS status
= NT_STATUS_OK
;
613 if (cli_has_async_calls(cli
)) {
615 * Can't use sync call while an async call is in flight
617 status
= NT_STATUS_INVALID_PARAMETER
;
621 ev
= event_context_init(frame
);
623 status
= NT_STATUS_NO_MEMORY
;
627 req
= cli_trans_send(frame
, ev
, cli
, trans_cmd
,
628 pipe_name
, fid
, function
, flags
,
629 setup
, num_setup
, max_setup
,
630 param
, num_param
, max_param
,
631 data
, num_data
, max_data
);
633 status
= NT_STATUS_NO_MEMORY
;
637 if (!tevent_req_poll(req
, ev
)) {
638 status
= map_nt_error_from_unix(errno
);
642 status
= cli_trans_recv(req
, mem_ctx
, recv_flags2
,
643 rsetup
, min_rsetup
, num_rsetup
,
644 rparam
, min_rparam
, num_rparam
,
645 rdata
, min_rdata
, num_rdata
);
648 if (!NT_STATUS_IS_OK(status
)) {
649 cli_set_error(cli
, status
);