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/>.
23 /****************************************************************************
24 Send a SMB trans or trans2 request.
25 ****************************************************************************/
27 BOOL cli_send_trans(struct cli_state *cli, int trans,
28 const char *pipe_name,
30 uint16 *setup, unsigned int lsetup, unsigned int msetup,
31 const char *param, unsigned int lparam, unsigned int mparam,
32 const char *data, unsigned int ldata, unsigned int mdata)
35 unsigned int this_ldata,this_lparam;
36 unsigned int tot_data=0,tot_param=0;
37 char *outdata,*outparam;
42 this_lparam = MIN(lparam,cli->max_xmit - (500+lsetup*2)); /* hack */
43 this_ldata = MIN(ldata,cli->max_xmit - (500+lsetup*2+this_lparam));
45 memset(cli->outbuf,'\0',smb_size);
46 set_message(NULL,cli->outbuf,14+lsetup,0,True);
47 SCVAL(cli->outbuf,smb_com,trans);
48 SSVAL(cli->outbuf,smb_tid, cli->cnum);
49 cli_setup_packet(cli);
52 * Save the mid we're using. We need this for finding
59 pipe_name_len = clistr_push(cli, smb_buf(cli->outbuf), pipe_name, -1, STR_TERMINATE);
62 outparam = smb_buf(cli->outbuf)+(trans==SMBtrans ? pipe_name_len : 3);
63 outdata = outparam+this_lparam;
66 SSVAL(cli->outbuf,smb_tpscnt,lparam); /* tpscnt */
67 SSVAL(cli->outbuf,smb_tdscnt,ldata); /* tdscnt */
68 SSVAL(cli->outbuf,smb_mprcnt,mparam); /* mprcnt */
69 SSVAL(cli->outbuf,smb_mdrcnt,mdata); /* mdrcnt */
70 SCVAL(cli->outbuf,smb_msrcnt,msetup); /* msrcnt */
71 SSVAL(cli->outbuf,smb_flags,flags); /* flags */
72 SIVAL(cli->outbuf,smb_timeout,0); /* timeout */
73 SSVAL(cli->outbuf,smb_pscnt,this_lparam); /* pscnt */
74 SSVAL(cli->outbuf,smb_psoff,smb_offset(outparam,cli->outbuf)); /* psoff */
75 SSVAL(cli->outbuf,smb_dscnt,this_ldata); /* dscnt */
76 SSVAL(cli->outbuf,smb_dsoff,smb_offset(outdata,cli->outbuf)); /* dsoff */
77 SCVAL(cli->outbuf,smb_suwcnt,lsetup); /* suwcnt */
78 for (i=0;i<lsetup;i++) /* setup[] */
79 SSVAL(cli->outbuf,smb_setup+i*2,setup[i]);
80 p = smb_buf(cli->outbuf);
81 if (trans != SMBtrans) {
82 *p++ = 0; /* put in a null smb_name */
83 *p++ = 'D'; *p++ = ' '; /* observed in OS/2 */
85 if (this_lparam) /* param[] */
86 memcpy(outparam,param,this_lparam);
87 if (this_ldata) /* data[] */
88 memcpy(outdata,data,this_ldata);
89 cli_setup_bcc(cli, outdata+this_ldata);
91 show_msg(cli->outbuf);
93 if (!cli_send_smb(cli)) {
97 /* Note we're in a trans state. Save the sequence
98 * numbers for replies. */
99 client_set_trans_sign_state_on(cli, mid);
101 if (this_ldata < ldata || this_lparam < lparam) {
102 /* receive interim response */
103 if (!cli_receive_smb(cli) || cli_is_error(cli)) {
104 client_set_trans_sign_state_off(cli, mid);
108 tot_data = this_ldata;
109 tot_param = this_lparam;
111 while (tot_data < ldata || tot_param < lparam) {
112 this_lparam = MIN(lparam-tot_param,cli->max_xmit - 500); /* hack */
113 this_ldata = MIN(ldata-tot_data,cli->max_xmit - (500+this_lparam));
115 set_message(NULL,cli->outbuf,trans==SMBtrans?8:9,0,True);
116 SCVAL(cli->outbuf,smb_com,(trans==SMBtrans ? SMBtranss : SMBtranss2));
118 outparam = smb_buf(cli->outbuf);
119 outdata = outparam+this_lparam;
121 /* secondary request */
122 SSVAL(cli->outbuf,smb_tpscnt,lparam); /* tpscnt */
123 SSVAL(cli->outbuf,smb_tdscnt,ldata); /* tdscnt */
124 SSVAL(cli->outbuf,smb_spscnt,this_lparam); /* pscnt */
125 SSVAL(cli->outbuf,smb_spsoff,smb_offset(outparam,cli->outbuf)); /* psoff */
126 SSVAL(cli->outbuf,smb_spsdisp,tot_param); /* psdisp */
127 SSVAL(cli->outbuf,smb_sdscnt,this_ldata); /* dscnt */
128 SSVAL(cli->outbuf,smb_sdsoff,smb_offset(outdata,cli->outbuf)); /* dsoff */
129 SSVAL(cli->outbuf,smb_sdsdisp,tot_data); /* dsdisp */
130 if (trans==SMBtrans2)
131 SSVALS(cli->outbuf,smb_sfid,fid); /* fid */
132 if (this_lparam) /* param[] */
133 memcpy(outparam,param+tot_param,this_lparam);
134 if (this_ldata) /* data[] */
135 memcpy(outdata,data+tot_data,this_ldata);
136 cli_setup_bcc(cli, outdata+this_ldata);
139 * Save the mid we're using. We need this for finding
144 show_msg(cli->outbuf);
145 if (!cli_send_smb(cli)) {
146 client_set_trans_sign_state_off(cli, mid);
150 /* Ensure we use the same mid for the secondaries. */
153 tot_data += this_ldata;
154 tot_param += this_lparam;
161 /****************************************************************************
162 Receive a SMB trans or trans2 response allocating the necessary memory.
163 ****************************************************************************/
165 BOOL cli_receive_trans(struct cli_state *cli,int trans,
166 char **param, unsigned int *param_len,
167 char **data, unsigned int *data_len)
169 unsigned int total_data=0;
170 unsigned int total_param=0;
171 unsigned int this_data,this_param;
175 *data_len = *param_len = 0;
177 if (!cli_receive_smb(cli)) {
181 show_msg(cli->inbuf);
184 if (CVAL(cli->inbuf,smb_com) != trans) {
185 DEBUG(0,("Expected %s response, got command 0x%02x\n",
186 trans==SMBtrans?"SMBtrans":"SMBtrans2",
187 CVAL(cli->inbuf,smb_com)));
192 * An NT RPC pipe call can return ERRDOS, ERRmoredata
193 * to a trans call. This is not an error and should not
194 * be treated as such. Note that STATUS_NO_MORE_FILES is
195 * returned when a trans2 findfirst/next finishes.
196 * When setting up an encrypted transport we can also
197 * see NT_STATUS_MORE_PROCESSING_REQUIRED here.
199 * Vista returns NT_STATUS_INACCESSIBLE_SYSTEM_SHORTCUT if the folder
200 * "<share>/Users/All Users" is enumerated. This is a special pseudo
201 * folder, and the response does not have parameters (nor a parameter
204 status = cli_nt_error(cli);
206 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
207 if (NT_STATUS_IS_ERR(status) ||
208 NT_STATUS_EQUAL(status,STATUS_NO_MORE_FILES) ||
209 NT_STATUS_EQUAL(status,NT_STATUS_INACCESSIBLE_SYSTEM_SHORTCUT)) {
214 /* parse out the lengths */
215 total_data = SVAL(cli->inbuf,smb_tdrcnt);
216 total_param = SVAL(cli->inbuf,smb_tprcnt);
220 *data = (char *)SMB_REALLOC(*data,total_data);
222 DEBUG(0,("cli_receive_trans: failed to enlarge data buffer\n"));
227 if (total_param!=0) {
228 *param = (char *)SMB_REALLOC(*param,total_param);
230 DEBUG(0,("cli_receive_trans: failed to enlarge param buffer\n"));
236 this_data = SVAL(cli->inbuf,smb_drcnt);
237 this_param = SVAL(cli->inbuf,smb_prcnt);
239 if (this_data + *data_len > total_data ||
240 this_param + *param_len > total_param) {
241 DEBUG(1,("Data overflow in cli_receive_trans\n"));
245 if (this_data + *data_len < this_data ||
246 this_data + *data_len < *data_len ||
247 this_param + *param_len < this_param ||
248 this_param + *param_len < *param_len) {
249 DEBUG(1,("Data overflow in cli_receive_trans\n"));
254 unsigned int data_offset_out = SVAL(cli->inbuf,smb_drdisp);
255 unsigned int data_offset_in = SVAL(cli->inbuf,smb_droff);
257 if (data_offset_out > total_data ||
258 data_offset_out + this_data > total_data ||
259 data_offset_out + this_data < data_offset_out ||
260 data_offset_out + this_data < this_data) {
261 DEBUG(1,("Data overflow in cli_receive_trans\n"));
264 if (data_offset_in > cli->bufsize ||
265 data_offset_in + this_data > cli->bufsize ||
266 data_offset_in + this_data < data_offset_in ||
267 data_offset_in + this_data < this_data) {
268 DEBUG(1,("Data overflow in cli_receive_trans\n"));
272 memcpy(*data + data_offset_out, smb_base(cli->inbuf) + data_offset_in, this_data);
275 unsigned int param_offset_out = SVAL(cli->inbuf,smb_prdisp);
276 unsigned int param_offset_in = SVAL(cli->inbuf,smb_proff);
278 if (param_offset_out > total_param ||
279 param_offset_out + this_param > total_param ||
280 param_offset_out + this_param < param_offset_out ||
281 param_offset_out + this_param < this_param) {
282 DEBUG(1,("Param overflow in cli_receive_trans\n"));
285 if (param_offset_in > cli->bufsize ||
286 param_offset_in + this_param > cli->bufsize ||
287 param_offset_in + this_param < param_offset_in ||
288 param_offset_in + this_param < this_param) {
289 DEBUG(1,("Param overflow in cli_receive_trans\n"));
293 memcpy(*param + param_offset_out, smb_base(cli->inbuf) + param_offset_in, this_param);
295 *data_len += this_data;
296 *param_len += this_param;
298 if (total_data <= *data_len && total_param <= *param_len) {
303 if (!cli_receive_smb(cli)) {
307 show_msg(cli->inbuf);
310 if (CVAL(cli->inbuf,smb_com) != trans) {
311 DEBUG(0,("Expected %s response, got command 0x%02x\n",
312 trans==SMBtrans?"SMBtrans":"SMBtrans2",
313 CVAL(cli->inbuf,smb_com)));
316 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
317 if (NT_STATUS_IS_ERR(cli_nt_error(cli))) {
322 /* parse out the total lengths again - they can shrink! */
323 if (SVAL(cli->inbuf,smb_tdrcnt) < total_data)
324 total_data = SVAL(cli->inbuf,smb_tdrcnt);
325 if (SVAL(cli->inbuf,smb_tprcnt) < total_param)
326 total_param = SVAL(cli->inbuf,smb_tprcnt);
328 if (total_data <= *data_len && total_param <= *param_len) {
336 client_set_trans_sign_state_off(cli, SVAL(cli->inbuf,smb_mid));
340 /****************************************************************************
341 Send a SMB nttrans request.
342 ****************************************************************************/
344 BOOL cli_send_nt_trans(struct cli_state *cli,
347 uint16 *setup, unsigned int lsetup, unsigned int msetup,
348 char *param, unsigned int lparam, unsigned int mparam,
349 char *data, unsigned int ldata, unsigned int mdata)
352 unsigned int this_ldata,this_lparam;
353 unsigned int tot_data=0,tot_param=0;
355 char *outdata,*outparam;
357 this_lparam = MIN(lparam,cli->max_xmit - (500+lsetup*2)); /* hack */
358 this_ldata = MIN(ldata,cli->max_xmit - (500+lsetup*2+this_lparam));
360 memset(cli->outbuf,'\0',smb_size);
361 set_message(NULL,cli->outbuf,19+lsetup,0,True);
362 SCVAL(cli->outbuf,smb_com,SMBnttrans);
363 SSVAL(cli->outbuf,smb_tid, cli->cnum);
364 cli_setup_packet(cli);
367 * Save the mid we're using. We need this for finding
373 outparam = smb_buf(cli->outbuf)+3;
374 outdata = outparam+this_lparam;
376 /* primary request */
377 SCVAL(cli->outbuf,smb_nt_MaxSetupCount,msetup);
378 SCVAL(cli->outbuf,smb_nt_Flags,flags);
379 SIVAL(cli->outbuf,smb_nt_TotalParameterCount, lparam);
380 SIVAL(cli->outbuf,smb_nt_TotalDataCount, ldata);
381 SIVAL(cli->outbuf,smb_nt_MaxParameterCount, mparam);
382 SIVAL(cli->outbuf,smb_nt_MaxDataCount, mdata);
383 SIVAL(cli->outbuf,smb_nt_ParameterCount, this_lparam);
384 SIVAL(cli->outbuf,smb_nt_ParameterOffset, smb_offset(outparam,cli->outbuf));
385 SIVAL(cli->outbuf,smb_nt_DataCount, this_ldata);
386 SIVAL(cli->outbuf,smb_nt_DataOffset, smb_offset(outdata,cli->outbuf));
387 SIVAL(cli->outbuf,smb_nt_SetupCount, lsetup);
388 SIVAL(cli->outbuf,smb_nt_Function, function);
389 for (i=0;i<lsetup;i++) /* setup[] */
390 SSVAL(cli->outbuf,smb_nt_SetupStart+i*2,setup[i]);
392 if (this_lparam) /* param[] */
393 memcpy(outparam,param,this_lparam);
394 if (this_ldata) /* data[] */
395 memcpy(outdata,data,this_ldata);
397 cli_setup_bcc(cli, outdata+this_ldata);
399 show_msg(cli->outbuf);
400 if (!cli_send_smb(cli)) {
404 /* Note we're in a trans state. Save the sequence
405 * numbers for replies. */
406 client_set_trans_sign_state_on(cli, mid);
408 if (this_ldata < ldata || this_lparam < lparam) {
409 /* receive interim response */
410 if (!cli_receive_smb(cli) || cli_is_error(cli)) {
411 client_set_trans_sign_state_off(cli, mid);
415 tot_data = this_ldata;
416 tot_param = this_lparam;
418 while (tot_data < ldata || tot_param < lparam) {
419 this_lparam = MIN(lparam-tot_param,cli->max_xmit - 500); /* hack */
420 this_ldata = MIN(ldata-tot_data,cli->max_xmit - (500+this_lparam));
422 set_message(NULL,cli->outbuf,18,0,True);
423 SCVAL(cli->outbuf,smb_com,SMBnttranss);
425 /* XXX - these should probably be aligned */
426 outparam = smb_buf(cli->outbuf);
427 outdata = outparam+this_lparam;
429 /* secondary request */
430 SIVAL(cli->outbuf,smb_nts_TotalParameterCount,lparam);
431 SIVAL(cli->outbuf,smb_nts_TotalDataCount,ldata);
432 SIVAL(cli->outbuf,smb_nts_ParameterCount,this_lparam);
433 SIVAL(cli->outbuf,smb_nts_ParameterOffset,smb_offset(outparam,cli->outbuf));
434 SIVAL(cli->outbuf,smb_nts_ParameterDisplacement,tot_param);
435 SIVAL(cli->outbuf,smb_nts_DataCount,this_ldata);
436 SIVAL(cli->outbuf,smb_nts_DataOffset,smb_offset(outdata,cli->outbuf));
437 SIVAL(cli->outbuf,smb_nts_DataDisplacement,tot_data);
438 if (this_lparam) /* param[] */
439 memcpy(outparam,param+tot_param,this_lparam);
440 if (this_ldata) /* data[] */
441 memcpy(outdata,data+tot_data,this_ldata);
442 cli_setup_bcc(cli, outdata+this_ldata);
445 * Save the mid we're using. We need this for finding
450 show_msg(cli->outbuf);
452 if (!cli_send_smb(cli)) {
453 client_set_trans_sign_state_off(cli, mid);
457 /* Ensure we use the same mid for the secondaries. */
460 tot_data += this_ldata;
461 tot_param += this_lparam;
468 /****************************************************************************
469 Receive a SMB nttrans response allocating the necessary memory.
470 ****************************************************************************/
472 BOOL cli_receive_nt_trans(struct cli_state *cli,
473 char **param, unsigned int *param_len,
474 char **data, unsigned int *data_len)
476 unsigned int total_data=0;
477 unsigned int total_param=0;
478 unsigned int this_data,this_param;
483 *data_len = *param_len = 0;
485 if (!cli_receive_smb(cli)) {
489 show_msg(cli->inbuf);
492 if (CVAL(cli->inbuf,smb_com) != SMBnttrans) {
493 DEBUG(0,("Expected SMBnttrans response, got command 0x%02x\n",
494 CVAL(cli->inbuf,smb_com)));
499 * An NT RPC pipe call can return ERRDOS, ERRmoredata
500 * to a trans call. This is not an error and should not
501 * be treated as such.
503 if (cli_is_dos_error(cli)) {
504 cli_dos_error(cli, &eclass, &ecode);
505 if (!(eclass == ERRDOS && ecode == ERRmoredata)) {
511 * Likewise for NT_STATUS_BUFFER_TOO_SMALL
513 if (cli_is_nt_error(cli)) {
514 if (!NT_STATUS_EQUAL(cli_nt_error(cli),
515 NT_STATUS_BUFFER_TOO_SMALL)) {
520 /* parse out the lengths */
521 total_data = SVAL(cli->inbuf,smb_ntr_TotalDataCount);
522 total_param = SVAL(cli->inbuf,smb_ntr_TotalParameterCount);
526 *data = (char *)SMB_REALLOC(*data,total_data);
528 DEBUG(0,("cli_receive_nt_trans: failed to enlarge data buffer to %d\n",total_data));
534 *param = (char *)SMB_REALLOC(*param,total_param);
536 DEBUG(0,("cli_receive_nt_trans: failed to enlarge param buffer to %d\n", total_param));
542 this_data = SVAL(cli->inbuf,smb_ntr_DataCount);
543 this_param = SVAL(cli->inbuf,smb_ntr_ParameterCount);
545 if (this_data + *data_len > total_data ||
546 this_param + *param_len > total_param) {
547 DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
551 if (this_data + *data_len < this_data ||
552 this_data + *data_len < *data_len ||
553 this_param + *param_len < this_param ||
554 this_param + *param_len < *param_len) {
555 DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
560 unsigned int data_offset_out = SVAL(cli->inbuf,smb_ntr_DataDisplacement);
561 unsigned int data_offset_in = SVAL(cli->inbuf,smb_ntr_DataOffset);
563 if (data_offset_out > total_data ||
564 data_offset_out + this_data > total_data ||
565 data_offset_out + this_data < data_offset_out ||
566 data_offset_out + this_data < this_data) {
567 DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
570 if (data_offset_in > cli->bufsize ||
571 data_offset_in + this_data > cli->bufsize ||
572 data_offset_in + this_data < data_offset_in ||
573 data_offset_in + this_data < this_data) {
574 DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
578 memcpy(*data + data_offset_out, smb_base(cli->inbuf) + data_offset_in, this_data);
582 unsigned int param_offset_out = SVAL(cli->inbuf,smb_ntr_ParameterDisplacement);
583 unsigned int param_offset_in = SVAL(cli->inbuf,smb_ntr_ParameterOffset);
585 if (param_offset_out > total_param ||
586 param_offset_out + this_param > total_param ||
587 param_offset_out + this_param < param_offset_out ||
588 param_offset_out + this_param < this_param) {
589 DEBUG(1,("Param overflow in cli_receive_nt_trans\n"));
592 if (param_offset_in > cli->bufsize ||
593 param_offset_in + this_param > cli->bufsize ||
594 param_offset_in + this_param < param_offset_in ||
595 param_offset_in + this_param < this_param) {
596 DEBUG(1,("Param overflow in cli_receive_nt_trans\n"));
600 memcpy(*param + param_offset_out, smb_base(cli->inbuf) + param_offset_in, this_param);
603 *data_len += this_data;
604 *param_len += this_param;
606 if (total_data <= *data_len && total_param <= *param_len) {
611 if (!cli_receive_smb(cli)) {
615 show_msg(cli->inbuf);
618 if (CVAL(cli->inbuf,smb_com) != SMBnttrans) {
619 DEBUG(0,("Expected SMBnttrans response, got command 0x%02x\n",
620 CVAL(cli->inbuf,smb_com)));
623 if (cli_is_dos_error(cli)) {
624 cli_dos_error(cli, &eclass, &ecode);
625 if(!(eclass == ERRDOS && ecode == ERRmoredata)) {
630 * Likewise for NT_STATUS_BUFFER_TOO_SMALL
632 if (cli_is_nt_error(cli)) {
633 if (!NT_STATUS_EQUAL(cli_nt_error(cli),
634 NT_STATUS_BUFFER_TOO_SMALL)) {
639 /* parse out the total lengths again - they can shrink! */
640 if (SVAL(cli->inbuf,smb_ntr_TotalDataCount) < total_data)
641 total_data = SVAL(cli->inbuf,smb_ntr_TotalDataCount);
642 if (SVAL(cli->inbuf,smb_ntr_TotalParameterCount) < total_param)
643 total_param = SVAL(cli->inbuf,smb_ntr_TotalParameterCount);
645 if (total_data <= *data_len && total_param <= *param_len) {
653 client_set_trans_sign_state_off(cli, SVAL(cli->inbuf,smb_mid));