[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / libsmb / clitrans.c
blobf212f499940620110264aa97c6ebd4b1da57a0e5
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
24 /****************************************************************************
25 Send a SMB trans or trans2 request.
26 ****************************************************************************/
28 BOOL cli_send_trans(struct cli_state *cli, int trans,
29 const char *pipe_name,
30 int fid, int flags,
31 uint16 *setup, unsigned int lsetup, unsigned int msetup,
32 const char *param, unsigned int lparam, unsigned int mparam,
33 const char *data, unsigned int ldata, unsigned int mdata)
35 unsigned int i;
36 unsigned int this_ldata,this_lparam;
37 unsigned int tot_data=0,tot_param=0;
38 char *outdata,*outparam;
39 char *p;
40 int pipe_name_len=0;
41 uint16 mid;
43 this_lparam = MIN(lparam,cli->max_xmit - (500+lsetup*2)); /* hack */
44 this_ldata = MIN(ldata,cli->max_xmit - (500+lsetup*2+this_lparam));
46 memset(cli->outbuf,'\0',smb_size);
47 set_message(cli->outbuf,14+lsetup,0,True);
48 SCVAL(cli->outbuf,smb_com,trans);
49 SSVAL(cli->outbuf,smb_tid, cli->cnum);
50 cli_setup_packet(cli);
53 * Save the mid we're using. We need this for finding
54 * signing replies.
57 mid = cli->mid;
59 if (pipe_name) {
60 pipe_name_len = clistr_push(cli, smb_buf(cli->outbuf), pipe_name, -1, STR_TERMINATE);
63 outparam = smb_buf(cli->outbuf)+(trans==SMBtrans ? pipe_name_len : 3);
64 outdata = outparam+this_lparam;
66 /* primary request */
67 SSVAL(cli->outbuf,smb_tpscnt,lparam); /* tpscnt */
68 SSVAL(cli->outbuf,smb_tdscnt,ldata); /* tdscnt */
69 SSVAL(cli->outbuf,smb_mprcnt,mparam); /* mprcnt */
70 SSVAL(cli->outbuf,smb_mdrcnt,mdata); /* mdrcnt */
71 SCVAL(cli->outbuf,smb_msrcnt,msetup); /* msrcnt */
72 SSVAL(cli->outbuf,smb_flags,flags); /* flags */
73 SIVAL(cli->outbuf,smb_timeout,0); /* timeout */
74 SSVAL(cli->outbuf,smb_pscnt,this_lparam); /* pscnt */
75 SSVAL(cli->outbuf,smb_psoff,smb_offset(outparam,cli->outbuf)); /* psoff */
76 SSVAL(cli->outbuf,smb_dscnt,this_ldata); /* dscnt */
77 SSVAL(cli->outbuf,smb_dsoff,smb_offset(outdata,cli->outbuf)); /* dsoff */
78 SCVAL(cli->outbuf,smb_suwcnt,lsetup); /* suwcnt */
79 for (i=0;i<lsetup;i++) /* setup[] */
80 SSVAL(cli->outbuf,smb_setup+i*2,setup[i]);
81 p = smb_buf(cli->outbuf);
82 if (trans != SMBtrans) {
83 *p++ = 0; /* put in a null smb_name */
84 *p++ = 'D'; *p++ = ' '; /* observed in OS/2 */
86 if (this_lparam) /* param[] */
87 memcpy(outparam,param,this_lparam);
88 if (this_ldata) /* data[] */
89 memcpy(outdata,data,this_ldata);
90 cli_setup_bcc(cli, outdata+this_ldata);
92 show_msg(cli->outbuf);
94 if (!cli_send_smb(cli)) {
95 return False;
98 /* Note we're in a trans state. Save the sequence
99 * numbers for replies. */
100 client_set_trans_sign_state_on(cli, mid);
102 if (this_ldata < ldata || this_lparam < lparam) {
103 /* receive interim response */
104 if (!cli_receive_smb(cli) || cli_is_error(cli)) {
105 client_set_trans_sign_state_off(cli, mid);
106 return(False);
109 tot_data = this_ldata;
110 tot_param = this_lparam;
112 while (tot_data < ldata || tot_param < lparam) {
113 this_lparam = MIN(lparam-tot_param,cli->max_xmit - 500); /* hack */
114 this_ldata = MIN(ldata-tot_data,cli->max_xmit - (500+this_lparam));
116 set_message(cli->outbuf,trans==SMBtrans?8:9,0,True);
117 SCVAL(cli->outbuf,smb_com,(trans==SMBtrans ? SMBtranss : SMBtranss2));
119 outparam = smb_buf(cli->outbuf);
120 outdata = outparam+this_lparam;
122 /* secondary request */
123 SSVAL(cli->outbuf,smb_tpscnt,lparam); /* tpscnt */
124 SSVAL(cli->outbuf,smb_tdscnt,ldata); /* tdscnt */
125 SSVAL(cli->outbuf,smb_spscnt,this_lparam); /* pscnt */
126 SSVAL(cli->outbuf,smb_spsoff,smb_offset(outparam,cli->outbuf)); /* psoff */
127 SSVAL(cli->outbuf,smb_spsdisp,tot_param); /* psdisp */
128 SSVAL(cli->outbuf,smb_sdscnt,this_ldata); /* dscnt */
129 SSVAL(cli->outbuf,smb_sdsoff,smb_offset(outdata,cli->outbuf)); /* dsoff */
130 SSVAL(cli->outbuf,smb_sdsdisp,tot_data); /* dsdisp */
131 if (trans==SMBtrans2)
132 SSVALS(cli->outbuf,smb_sfid,fid); /* fid */
133 if (this_lparam) /* param[] */
134 memcpy(outparam,param+tot_param,this_lparam);
135 if (this_ldata) /* data[] */
136 memcpy(outdata,data+tot_data,this_ldata);
137 cli_setup_bcc(cli, outdata+this_ldata);
140 * Save the mid we're using. We need this for finding
141 * signing replies.
143 mid = cli->mid;
145 show_msg(cli->outbuf);
146 if (!cli_send_smb(cli)) {
147 client_set_trans_sign_state_off(cli, mid);
148 return False;
151 /* Ensure we use the same mid for the secondaries. */
152 cli->mid = mid;
154 tot_data += this_ldata;
155 tot_param += this_lparam;
159 return(True);
162 /****************************************************************************
163 Receive a SMB trans or trans2 response allocating the necessary memory.
164 ****************************************************************************/
166 BOOL cli_receive_trans(struct cli_state *cli,int trans,
167 char **param, unsigned int *param_len,
168 char **data, unsigned int *data_len)
170 unsigned int total_data=0;
171 unsigned int total_param=0;
172 unsigned int this_data,this_param;
173 NTSTATUS status;
174 BOOL ret = False;
176 *data_len = *param_len = 0;
178 if (!cli_receive_smb(cli)) {
179 return False;
182 show_msg(cli->inbuf);
184 /* sanity check */
185 if (CVAL(cli->inbuf,smb_com) != trans) {
186 DEBUG(0,("Expected %s response, got command 0x%02x\n",
187 trans==SMBtrans?"SMBtrans":"SMBtrans2",
188 CVAL(cli->inbuf,smb_com)));
189 return False;
193 * An NT RPC pipe call can return ERRDOS, ERRmoredata
194 * to a trans call. This is not an error and should not
195 * be treated as such. Note that STATUS_NO_MORE_FILES is
196 * returned when a trans2 findfirst/next finishes.
198 status = cli_nt_error(cli);
200 if (NT_STATUS_IS_ERR(status) ||
201 NT_STATUS_EQUAL(status,STATUS_NO_MORE_FILES) ||
202 NT_STATUS_EQUAL(status,STATUS_INACCESSIBLE_SYSTEM_SHORTCUT)) {
203 goto out;
206 /* parse out the lengths */
207 total_data = SVAL(cli->inbuf,smb_tdrcnt);
208 total_param = SVAL(cli->inbuf,smb_tprcnt);
210 /* allocate it */
211 if (total_data!=0) {
212 *data = (char *)SMB_REALLOC(*data,total_data);
213 if (!(*data)) {
214 DEBUG(0,("cli_receive_trans: failed to enlarge data buffer\n"));
215 goto out;
219 if (total_param!=0) {
220 *param = (char *)SMB_REALLOC(*param,total_param);
221 if (!(*param)) {
222 DEBUG(0,("cli_receive_trans: failed to enlarge param buffer\n"));
223 goto out;
227 for (;;) {
228 this_data = SVAL(cli->inbuf,smb_drcnt);
229 this_param = SVAL(cli->inbuf,smb_prcnt);
231 if (this_data + *data_len > total_data ||
232 this_param + *param_len > total_param) {
233 DEBUG(1,("Data overflow in cli_receive_trans\n"));
234 goto out;
237 if (this_data + *data_len < this_data ||
238 this_data + *data_len < *data_len ||
239 this_param + *param_len < this_param ||
240 this_param + *param_len < *param_len) {
241 DEBUG(1,("Data overflow in cli_receive_trans\n"));
242 goto out;
245 if (this_data) {
246 unsigned int data_offset_out = SVAL(cli->inbuf,smb_drdisp);
247 unsigned int data_offset_in = SVAL(cli->inbuf,smb_droff);
249 if (data_offset_out > total_data ||
250 data_offset_out + this_data > total_data ||
251 data_offset_out + this_data < data_offset_out ||
252 data_offset_out + this_data < this_data) {
253 DEBUG(1,("Data overflow in cli_receive_trans\n"));
254 goto out;
256 if (data_offset_in > cli->bufsize ||
257 data_offset_in + this_data > cli->bufsize ||
258 data_offset_in + this_data < data_offset_in ||
259 data_offset_in + this_data < this_data) {
260 DEBUG(1,("Data overflow in cli_receive_trans\n"));
261 goto out;
264 memcpy(*data + data_offset_out, smb_base(cli->inbuf) + data_offset_in, this_data);
266 if (this_param) {
267 unsigned int param_offset_out = SVAL(cli->inbuf,smb_prdisp);
268 unsigned int param_offset_in = SVAL(cli->inbuf,smb_proff);
270 if (param_offset_out > total_param ||
271 param_offset_out + this_param > total_param ||
272 param_offset_out + this_param < param_offset_out ||
273 param_offset_out + this_param < this_param) {
274 DEBUG(1,("Param overflow in cli_receive_trans\n"));
275 goto out;
277 if (param_offset_in > cli->bufsize ||
278 param_offset_in + this_param > cli->bufsize ||
279 param_offset_in + this_param < param_offset_in ||
280 param_offset_in + this_param < this_param) {
281 DEBUG(1,("Param overflow in cli_receive_trans\n"));
282 goto out;
285 memcpy(*param + param_offset_out, smb_base(cli->inbuf) + param_offset_in, this_param);
287 *data_len += this_data;
288 *param_len += this_param;
290 if (total_data <= *data_len && total_param <= *param_len) {
291 ret = True;
292 break;
295 if (!cli_receive_smb(cli)) {
296 goto out;
299 show_msg(cli->inbuf);
301 /* sanity check */
302 if (CVAL(cli->inbuf,smb_com) != trans) {
303 DEBUG(0,("Expected %s response, got command 0x%02x\n",
304 trans==SMBtrans?"SMBtrans":"SMBtrans2",
305 CVAL(cli->inbuf,smb_com)));
306 goto out;
308 if (NT_STATUS_IS_ERR(cli_nt_error(cli))) {
309 goto out;
312 /* parse out the total lengths again - they can shrink! */
313 if (SVAL(cli->inbuf,smb_tdrcnt) < total_data)
314 total_data = SVAL(cli->inbuf,smb_tdrcnt);
315 if (SVAL(cli->inbuf,smb_tprcnt) < total_param)
316 total_param = SVAL(cli->inbuf,smb_tprcnt);
318 if (total_data <= *data_len && total_param <= *param_len) {
319 ret = True;
320 break;
324 out:
326 client_set_trans_sign_state_off(cli, SVAL(cli->inbuf,smb_mid));
327 return ret;
330 /****************************************************************************
331 Send a SMB nttrans request.
332 ****************************************************************************/
334 BOOL cli_send_nt_trans(struct cli_state *cli,
335 int function,
336 int flags,
337 uint16 *setup, unsigned int lsetup, unsigned int msetup,
338 char *param, unsigned int lparam, unsigned int mparam,
339 char *data, unsigned int ldata, unsigned int mdata)
341 unsigned int i;
342 unsigned int this_ldata,this_lparam;
343 unsigned int tot_data=0,tot_param=0;
344 uint16 mid;
345 char *outdata,*outparam;
347 this_lparam = MIN(lparam,cli->max_xmit - (500+lsetup*2)); /* hack */
348 this_ldata = MIN(ldata,cli->max_xmit - (500+lsetup*2+this_lparam));
350 memset(cli->outbuf,'\0',smb_size);
351 set_message(cli->outbuf,19+lsetup,0,True);
352 SCVAL(cli->outbuf,smb_com,SMBnttrans);
353 SSVAL(cli->outbuf,smb_tid, cli->cnum);
354 cli_setup_packet(cli);
357 * Save the mid we're using. We need this for finding
358 * signing replies.
361 mid = cli->mid;
363 outparam = smb_buf(cli->outbuf)+3;
364 outdata = outparam+this_lparam;
366 /* primary request */
367 SCVAL(cli->outbuf,smb_nt_MaxSetupCount,msetup);
368 SCVAL(cli->outbuf,smb_nt_Flags,flags);
369 SIVAL(cli->outbuf,smb_nt_TotalParameterCount, lparam);
370 SIVAL(cli->outbuf,smb_nt_TotalDataCount, ldata);
371 SIVAL(cli->outbuf,smb_nt_MaxParameterCount, mparam);
372 SIVAL(cli->outbuf,smb_nt_MaxDataCount, mdata);
373 SIVAL(cli->outbuf,smb_nt_ParameterCount, this_lparam);
374 SIVAL(cli->outbuf,smb_nt_ParameterOffset, smb_offset(outparam,cli->outbuf));
375 SIVAL(cli->outbuf,smb_nt_DataCount, this_ldata);
376 SIVAL(cli->outbuf,smb_nt_DataOffset, smb_offset(outdata,cli->outbuf));
377 SIVAL(cli->outbuf,smb_nt_SetupCount, lsetup);
378 SIVAL(cli->outbuf,smb_nt_Function, function);
379 for (i=0;i<lsetup;i++) /* setup[] */
380 SSVAL(cli->outbuf,smb_nt_SetupStart+i*2,setup[i]);
382 if (this_lparam) /* param[] */
383 memcpy(outparam,param,this_lparam);
384 if (this_ldata) /* data[] */
385 memcpy(outdata,data,this_ldata);
387 cli_setup_bcc(cli, outdata+this_ldata);
389 show_msg(cli->outbuf);
390 if (!cli_send_smb(cli)) {
391 return False;
394 /* Note we're in a trans state. Save the sequence
395 * numbers for replies. */
396 client_set_trans_sign_state_on(cli, mid);
398 if (this_ldata < ldata || this_lparam < lparam) {
399 /* receive interim response */
400 if (!cli_receive_smb(cli) || cli_is_error(cli)) {
401 client_set_trans_sign_state_off(cli, mid);
402 return(False);
405 tot_data = this_ldata;
406 tot_param = this_lparam;
408 while (tot_data < ldata || tot_param < lparam) {
409 this_lparam = MIN(lparam-tot_param,cli->max_xmit - 500); /* hack */
410 this_ldata = MIN(ldata-tot_data,cli->max_xmit - (500+this_lparam));
412 set_message(cli->outbuf,18,0,True);
413 SCVAL(cli->outbuf,smb_com,SMBnttranss);
415 /* XXX - these should probably be aligned */
416 outparam = smb_buf(cli->outbuf);
417 outdata = outparam+this_lparam;
419 /* secondary request */
420 SIVAL(cli->outbuf,smb_nts_TotalParameterCount,lparam);
421 SIVAL(cli->outbuf,smb_nts_TotalDataCount,ldata);
422 SIVAL(cli->outbuf,smb_nts_ParameterCount,this_lparam);
423 SIVAL(cli->outbuf,smb_nts_ParameterOffset,smb_offset(outparam,cli->outbuf));
424 SIVAL(cli->outbuf,smb_nts_ParameterDisplacement,tot_param);
425 SIVAL(cli->outbuf,smb_nts_DataCount,this_ldata);
426 SIVAL(cli->outbuf,smb_nts_DataOffset,smb_offset(outdata,cli->outbuf));
427 SIVAL(cli->outbuf,smb_nts_DataDisplacement,tot_data);
428 if (this_lparam) /* param[] */
429 memcpy(outparam,param+tot_param,this_lparam);
430 if (this_ldata) /* data[] */
431 memcpy(outdata,data+tot_data,this_ldata);
432 cli_setup_bcc(cli, outdata+this_ldata);
435 * Save the mid we're using. We need this for finding
436 * signing replies.
438 mid = cli->mid;
440 show_msg(cli->outbuf);
442 if (!cli_send_smb(cli)) {
443 client_set_trans_sign_state_off(cli, mid);
444 return False;
447 /* Ensure we use the same mid for the secondaries. */
448 cli->mid = mid;
450 tot_data += this_ldata;
451 tot_param += this_lparam;
455 return(True);
458 /****************************************************************************
459 Receive a SMB nttrans response allocating the necessary memory.
460 ****************************************************************************/
462 BOOL cli_receive_nt_trans(struct cli_state *cli,
463 char **param, unsigned int *param_len,
464 char **data, unsigned int *data_len)
466 unsigned int total_data=0;
467 unsigned int total_param=0;
468 unsigned int this_data,this_param;
469 uint8 eclass;
470 uint32 ecode;
471 BOOL ret = False;
473 *data_len = *param_len = 0;
475 if (!cli_receive_smb(cli)) {
476 return False;
479 show_msg(cli->inbuf);
481 /* sanity check */
482 if (CVAL(cli->inbuf,smb_com) != SMBnttrans) {
483 DEBUG(0,("Expected SMBnttrans response, got command 0x%02x\n",
484 CVAL(cli->inbuf,smb_com)));
485 return(False);
489 * An NT RPC pipe call can return ERRDOS, ERRmoredata
490 * to a trans call. This is not an error and should not
491 * be treated as such.
493 if (cli_is_dos_error(cli)) {
494 cli_dos_error(cli, &eclass, &ecode);
495 if (!(eclass == ERRDOS && ecode == ERRmoredata)) {
496 goto out;
501 * Likewise for NT_STATUS_BUFFER_TOO_SMALL
503 if (cli_is_nt_error(cli)) {
504 if (!NT_STATUS_EQUAL(cli_nt_error(cli),
505 NT_STATUS_BUFFER_TOO_SMALL)) {
506 goto out;
510 /* parse out the lengths */
511 total_data = SVAL(cli->inbuf,smb_ntr_TotalDataCount);
512 total_param = SVAL(cli->inbuf,smb_ntr_TotalParameterCount);
514 /* allocate it */
515 if (total_data) {
516 *data = (char *)SMB_REALLOC(*data,total_data);
517 if (!(*data)) {
518 DEBUG(0,("cli_receive_nt_trans: failed to enlarge data buffer to %d\n",total_data));
519 goto out;
523 if (total_param) {
524 *param = (char *)SMB_REALLOC(*param,total_param);
525 if (!(*param)) {
526 DEBUG(0,("cli_receive_nt_trans: failed to enlarge param buffer to %d\n", total_param));
527 goto out;
531 while (1) {
532 this_data = SVAL(cli->inbuf,smb_ntr_DataCount);
533 this_param = SVAL(cli->inbuf,smb_ntr_ParameterCount);
535 if (this_data + *data_len > total_data ||
536 this_param + *param_len > total_param) {
537 DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
538 goto out;
541 if (this_data + *data_len < this_data ||
542 this_data + *data_len < *data_len ||
543 this_param + *param_len < this_param ||
544 this_param + *param_len < *param_len) {
545 DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
546 goto out;
549 if (this_data) {
550 unsigned int data_offset_out = SVAL(cli->inbuf,smb_ntr_DataDisplacement);
551 unsigned int data_offset_in = SVAL(cli->inbuf,smb_ntr_DataOffset);
553 if (data_offset_out > total_data ||
554 data_offset_out + this_data > total_data ||
555 data_offset_out + this_data < data_offset_out ||
556 data_offset_out + this_data < this_data) {
557 DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
558 goto out;
560 if (data_offset_in > cli->bufsize ||
561 data_offset_in + this_data > cli->bufsize ||
562 data_offset_in + this_data < data_offset_in ||
563 data_offset_in + this_data < this_data) {
564 DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
565 goto out;
568 memcpy(*data + data_offset_out, smb_base(cli->inbuf) + data_offset_in, this_data);
571 if (this_param) {
572 unsigned int param_offset_out = SVAL(cli->inbuf,smb_ntr_ParameterDisplacement);
573 unsigned int param_offset_in = SVAL(cli->inbuf,smb_ntr_ParameterOffset);
575 if (param_offset_out > total_param ||
576 param_offset_out + this_param > total_param ||
577 param_offset_out + this_param < param_offset_out ||
578 param_offset_out + this_param < this_param) {
579 DEBUG(1,("Param overflow in cli_receive_nt_trans\n"));
580 goto out;
582 if (param_offset_in > cli->bufsize ||
583 param_offset_in + this_param > cli->bufsize ||
584 param_offset_in + this_param < param_offset_in ||
585 param_offset_in + this_param < this_param) {
586 DEBUG(1,("Param overflow in cli_receive_nt_trans\n"));
587 goto out;
590 memcpy(*param + param_offset_out, smb_base(cli->inbuf) + param_offset_in, this_param);
593 *data_len += this_data;
594 *param_len += this_param;
596 if (total_data <= *data_len && total_param <= *param_len) {
597 ret = True;
598 break;
601 if (!cli_receive_smb(cli)) {
602 goto out;
605 show_msg(cli->inbuf);
607 /* sanity check */
608 if (CVAL(cli->inbuf,smb_com) != SMBnttrans) {
609 DEBUG(0,("Expected SMBnttrans response, got command 0x%02x\n",
610 CVAL(cli->inbuf,smb_com)));
611 goto out;
613 if (cli_is_dos_error(cli)) {
614 cli_dos_error(cli, &eclass, &ecode);
615 if(!(eclass == ERRDOS && ecode == ERRmoredata)) {
616 goto out;
620 * Likewise for NT_STATUS_BUFFER_TOO_SMALL
622 if (cli_is_nt_error(cli)) {
623 if (!NT_STATUS_EQUAL(cli_nt_error(cli),
624 NT_STATUS_BUFFER_TOO_SMALL)) {
625 goto out;
629 /* parse out the total lengths again - they can shrink! */
630 if (SVAL(cli->inbuf,smb_ntr_TotalDataCount) < total_data)
631 total_data = SVAL(cli->inbuf,smb_ntr_TotalDataCount);
632 if (SVAL(cli->inbuf,smb_ntr_TotalParameterCount) < total_param)
633 total_param = SVAL(cli->inbuf,smb_ntr_TotalParameterCount);
635 if (total_data <= *data_len && total_param <= *param_len) {
636 ret = True;
637 break;
641 out:
643 client_set_trans_sign_state_off(cli, SVAL(cli->inbuf,smb_mid));
644 return ret;