s4:libcli/util/nterr: NO_S4U_PROT_SUPPORT and CROSSREALM_DELEGATION_FAILURE
[Samba.git] / source3 / libsmb / clitrans.c
blob7bc4a75fe3ee33667811f8314f43d51ec56997be
1 /*
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/>.
20 #include "includes.h"
21 #include "../lib/util/tevent_ntstatus.h"
22 #include "async_smb.h"
24 struct trans_recvblob {
25 uint8_t *data;
26 uint32_t max, total, received;
29 struct cli_trans_state {
30 struct cli_state *cli;
31 struct event_context *ev;
32 uint8_t cmd;
33 uint16_t mid;
34 uint32_t seqnum;
35 const char *pipe_name;
36 uint8_t *pipe_name_conv;
37 size_t pipe_name_conv_len;
38 uint16_t fid;
39 uint16_t function;
40 int flags;
41 uint16_t *setup;
42 uint8_t num_setup, max_setup;
43 uint8_t *param;
44 uint32_t num_param, param_sent;
45 uint8_t *data;
46 uint32_t num_data, data_sent;
48 uint8_t num_rsetup;
49 uint16_t *rsetup;
50 struct trans_recvblob rparam;
51 struct trans_recvblob rdata;
52 uint16_t recv_flags2;
54 TALLOC_CTX *secondary_request_ctx;
56 struct iovec iov[4];
57 uint8_t pad[4];
58 uint16_t vwv[32];
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;
77 return NT_STATUS_OK;
80 switch (smb_cmd) {
81 case SMBtrans:
82 case SMBtrans2:
83 if (wct < 10) {
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;
98 *psetup = vwv + 10;
100 break;
101 case SMBnttrans:
102 if (wct < 18) {
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);
114 *psetup = vwv + 18;
115 break;
117 default:
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;
137 return NT_STATUS_OK;
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;
149 blob->total = total;
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;
160 if (thistime) {
161 memcpy(blob->data + displacement, buf, thistime);
162 blob->received += thistime;
165 return NT_STATUS_OK;
168 static void cli_trans_format(struct cli_trans_state *state, uint8_t *pwct,
169 int *piov_count)
171 uint8_t wct = 0;
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;
179 uint8_t cmd;
181 cmd = state->cmd;
183 if ((state->param_sent != 0) || (state->data_sent != 0)) {
184 /* The secondary commands are one after the primary ones */
185 cmd += 1;
188 param_offset = smb_size - 4;
190 switch (cmd) {
191 case SMBtrans:
192 pad[0] = 0;
193 iov[0].iov_base = (void *)pad;
194 iov[0].iov_len = 1;
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;
199 iov += 2;
200 break;
201 case SMBtrans2:
202 pad[0] = 0;
203 pad[1] = 'D'; /* Copy this from "old" 3.0 behaviour */
204 pad[2] = ' ';
205 iov[0].iov_base = (void *)pad;
206 iov[0].iov_len = 3;
207 wct = 14 + state->num_setup;
208 param_offset += 3;
209 iov += 1;
210 break;
211 case SMBtranss:
212 wct = 8;
213 break;
214 case SMBtranss2:
215 wct = 9;
216 break;
217 case SMBnttrans:
218 wct = 19 + state->num_setup;
219 break;
220 case SMBnttranss:
221 wct = 18;
222 break;
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,
229 useable_space);
230 iov[0].iov_base = (void *)(state->param + state->param_sent);
231 iov[0].iov_len = this_param;
232 iov += 1;
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;
240 iov += 1;
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));
257 switch (cmd) {
258 case SMBtrans:
259 case SMBtrans2:
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);
277 break;
278 case SMBtranss:
279 case SMBtranss2:
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);
291 break;
292 case SMBnttrans:
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);
307 break;
308 case SMBnttranss:
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 */
320 break;
323 state->param_sent += this_param;
324 state->data_sent += this_data;
326 *pwct = wct;
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;
342 int iov_count;
343 uint8_t wct;
344 NTSTATUS status;
346 req = tevent_req_create(mem_ctx, &state, struct cli_trans_state);
347 if (req == NULL) {
348 return NULL;
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",
356 (unsigned)num_setup,
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);
374 state->cli = cli;
375 state->ev = ev;
376 state->cmd = cmd;
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;
404 state->data = data;
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);
424 return 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);
433 NTSTATUS status;
434 bool sent_all;
435 uint8_t wct;
436 uint16_t *vwv;
437 uint32_t num_bytes;
438 uint8_t *bytes;
439 uint8_t *inbuf;
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,
452 &num_bytes, &bytes);
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)) {
464 goto fail;
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, &param_disp, &param,
474 &total_data, &num_data, &data_disp, &data);
476 if (!NT_STATUS_IS_OK(status)) {
477 goto fail;
480 if (!sent_all) {
481 int iov_count;
483 TALLOC_FREE(subreq);
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)) {
491 return;
493 cli_smb_req_set_mid(subreq, state->mid);
495 status = cli_smb_req_send(subreq);
497 if (!NT_STATUS_IS_OK(status)) {
498 goto fail;
500 tevent_req_set_callback(subreq, cli_trans_done, req);
501 return;
504 status = cli_trans_pull_blob(
505 state, &state->rparam, total_param, num_param, param,
506 param_disp);
508 if (!NT_STATUS_IS_OK(status)) {
509 DEBUG(10, ("Pulling params failed: %s\n", nt_errstr(status)));
510 goto fail;
513 status = cli_trans_pull_blob(
514 state, &state->rdata, total_data, num_data, data,
515 data_disp);
517 if (!NT_STATUS_IS_OK(status)) {
518 DEBUG(10, ("Pulling data failed: %s\n", nt_errstr(status)));
519 goto fail;
522 if ((state->rparam.total == state->rparam.received)
523 && (state->rdata.total == state->rdata.received)) {
524 state->recv_flags2 = SVAL(inbuf, smb_flg2);
525 TALLOC_FREE(subreq);
526 cli_state_seqnum_remove(state->cli, state->mid);
527 tevent_req_done(req);
528 return;
531 TALLOC_FREE(inbuf);
533 if (!cli_smb_req_set_pending(subreq)) {
534 status = NT_STATUS_NO_MEMORY;
535 goto fail;
537 return;
539 fail:
540 cli_state_seqnum_remove(state->cli, state->mid);
541 TALLOC_FREE(subreq);
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,
548 uint8_t *num_setup,
549 uint8_t **param, uint32_t min_param,
550 uint32_t *num_param,
551 uint8_t **data, uint32_t min_data,
552 uint32_t *num_data)
554 struct cli_trans_state *state = tevent_req_data(
555 req, struct cli_trans_state);
556 NTSTATUS status;
558 if (tevent_req_is_nterror(req, &status)) {
559 return 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;
572 if (setup != NULL) {
573 *setup = talloc_move(mem_ctx, &state->rsetup);
574 *num_setup = state->num_rsetup;
575 } else {
576 TALLOC_FREE(state->rsetup);
579 if (param != NULL) {
580 *param = talloc_move(mem_ctx, &state->rparam.data);
581 *num_param = state->rparam.total;
582 } else {
583 TALLOC_FREE(state->rparam.data);
586 if (data != NULL) {
587 *data = talloc_move(mem_ctx, &state->rdata.data);
588 *num_data = state->rdata.total;
589 } else {
590 TALLOC_FREE(state->rdata.data);
593 return NT_STATUS_OK;
596 NTSTATUS cli_trans(TALLOC_CTX *mem_ctx, struct cli_state *cli,
597 uint8_t trans_cmd,
598 const char *pipe_name, uint16_t fid, uint16_t function,
599 int flags,
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;
618 goto fail;
621 ev = event_context_init(frame);
622 if (ev == NULL) {
623 status = NT_STATUS_NO_MEMORY;
624 goto fail;
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);
632 if (req == NULL) {
633 status = NT_STATUS_NO_MEMORY;
634 goto fail;
637 if (!tevent_req_poll(req, ev)) {
638 status = map_nt_error_from_unix(errno);
639 goto fail;
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);
646 fail:
647 TALLOC_FREE(frame);
648 if (!NT_STATUS_IS_OK(status)) {
649 cli_set_error(cli, status);
651 return status;