r17333: Some C++ warnings
[Samba.git] / source / libsmb / clitrans.c
blob27207e72e2f53058fd846ea9d9735c0160f05188
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) || NT_STATUS_EQUAL(status,STATUS_NO_MORE_FILES)) {
201 goto out;
204 /* parse out the lengths */
205 total_data = SVAL(cli->inbuf,smb_tdrcnt);
206 total_param = SVAL(cli->inbuf,smb_tprcnt);
208 /* allocate it */
209 if (total_data!=0) {
210 *data = (char *)SMB_REALLOC(*data,total_data);
211 if (!(*data)) {
212 DEBUG(0,("cli_receive_trans: failed to enlarge data buffer\n"));
213 goto out;
217 if (total_param!=0) {
218 *param = (char *)SMB_REALLOC(*param,total_param);
219 if (!(*param)) {
220 DEBUG(0,("cli_receive_trans: failed to enlarge param buffer\n"));
221 goto out;
225 for (;;) {
226 this_data = SVAL(cli->inbuf,smb_drcnt);
227 this_param = SVAL(cli->inbuf,smb_prcnt);
229 if (this_data + *data_len > total_data ||
230 this_param + *param_len > total_param) {
231 DEBUG(1,("Data overflow in cli_receive_trans\n"));
232 goto out;
235 if (this_data + *data_len < this_data ||
236 this_data + *data_len < *data_len ||
237 this_param + *param_len < this_param ||
238 this_param + *param_len < *param_len) {
239 DEBUG(1,("Data overflow in cli_receive_trans\n"));
240 goto out;
243 if (this_data) {
244 unsigned int data_offset_out = SVAL(cli->inbuf,smb_drdisp);
245 unsigned int data_offset_in = SVAL(cli->inbuf,smb_droff);
247 if (data_offset_out > total_data ||
248 data_offset_out + this_data > total_data ||
249 data_offset_out + this_data < data_offset_out ||
250 data_offset_out + this_data < this_data) {
251 DEBUG(1,("Data overflow in cli_receive_trans\n"));
252 goto out;
254 if (data_offset_in > cli->bufsize ||
255 data_offset_in + this_data > cli->bufsize ||
256 data_offset_in + this_data < data_offset_in ||
257 data_offset_in + this_data < this_data) {
258 DEBUG(1,("Data overflow in cli_receive_trans\n"));
259 goto out;
262 memcpy(*data + data_offset_out, smb_base(cli->inbuf) + data_offset_in, this_data);
264 if (this_param) {
265 unsigned int param_offset_out = SVAL(cli->inbuf,smb_prdisp);
266 unsigned int param_offset_in = SVAL(cli->inbuf,smb_proff);
268 if (param_offset_out > total_param ||
269 param_offset_out + this_param > total_param ||
270 param_offset_out + this_param < param_offset_out ||
271 param_offset_out + this_param < this_param) {
272 DEBUG(1,("Param overflow in cli_receive_trans\n"));
273 goto out;
275 if (param_offset_in > cli->bufsize ||
276 param_offset_in + this_param > cli->bufsize ||
277 param_offset_in + this_param < param_offset_in ||
278 param_offset_in + this_param < this_param) {
279 DEBUG(1,("Param overflow in cli_receive_trans\n"));
280 goto out;
283 memcpy(*param + param_offset_out, smb_base(cli->inbuf) + param_offset_in, this_param);
285 *data_len += this_data;
286 *param_len += this_param;
288 if (total_data <= *data_len && total_param <= *param_len) {
289 ret = True;
290 break;
293 if (!cli_receive_smb(cli)) {
294 goto out;
297 show_msg(cli->inbuf);
299 /* sanity check */
300 if (CVAL(cli->inbuf,smb_com) != trans) {
301 DEBUG(0,("Expected %s response, got command 0x%02x\n",
302 trans==SMBtrans?"SMBtrans":"SMBtrans2",
303 CVAL(cli->inbuf,smb_com)));
304 goto out;
306 if (NT_STATUS_IS_ERR(cli_nt_error(cli))) {
307 goto out;
310 /* parse out the total lengths again - they can shrink! */
311 if (SVAL(cli->inbuf,smb_tdrcnt) < total_data)
312 total_data = SVAL(cli->inbuf,smb_tdrcnt);
313 if (SVAL(cli->inbuf,smb_tprcnt) < total_param)
314 total_param = SVAL(cli->inbuf,smb_tprcnt);
316 if (total_data <= *data_len && total_param <= *param_len) {
317 ret = True;
318 break;
322 out:
324 client_set_trans_sign_state_off(cli, SVAL(cli->inbuf,smb_mid));
325 return ret;
328 /****************************************************************************
329 Send a SMB nttrans request.
330 ****************************************************************************/
332 BOOL cli_send_nt_trans(struct cli_state *cli,
333 int function,
334 int flags,
335 uint16 *setup, unsigned int lsetup, unsigned int msetup,
336 char *param, unsigned int lparam, unsigned int mparam,
337 char *data, unsigned int ldata, unsigned int mdata)
339 unsigned int i;
340 unsigned int this_ldata,this_lparam;
341 unsigned int tot_data=0,tot_param=0;
342 uint16 mid;
343 char *outdata,*outparam;
345 this_lparam = MIN(lparam,cli->max_xmit - (500+lsetup*2)); /* hack */
346 this_ldata = MIN(ldata,cli->max_xmit - (500+lsetup*2+this_lparam));
348 memset(cli->outbuf,'\0',smb_size);
349 set_message(cli->outbuf,19+lsetup,0,True);
350 SCVAL(cli->outbuf,smb_com,SMBnttrans);
351 SSVAL(cli->outbuf,smb_tid, cli->cnum);
352 cli_setup_packet(cli);
355 * Save the mid we're using. We need this for finding
356 * signing replies.
359 mid = cli->mid;
361 outparam = smb_buf(cli->outbuf)+3;
362 outdata = outparam+this_lparam;
364 /* primary request */
365 SCVAL(cli->outbuf,smb_nt_MaxSetupCount,msetup);
366 SCVAL(cli->outbuf,smb_nt_Flags,flags);
367 SIVAL(cli->outbuf,smb_nt_TotalParameterCount, lparam);
368 SIVAL(cli->outbuf,smb_nt_TotalDataCount, ldata);
369 SIVAL(cli->outbuf,smb_nt_MaxParameterCount, mparam);
370 SIVAL(cli->outbuf,smb_nt_MaxDataCount, mdata);
371 SIVAL(cli->outbuf,smb_nt_ParameterCount, this_lparam);
372 SIVAL(cli->outbuf,smb_nt_ParameterOffset, smb_offset(outparam,cli->outbuf));
373 SIVAL(cli->outbuf,smb_nt_DataCount, this_ldata);
374 SIVAL(cli->outbuf,smb_nt_DataOffset, smb_offset(outdata,cli->outbuf));
375 SIVAL(cli->outbuf,smb_nt_SetupCount, lsetup);
376 SIVAL(cli->outbuf,smb_nt_Function, function);
377 for (i=0;i<lsetup;i++) /* setup[] */
378 SSVAL(cli->outbuf,smb_nt_SetupStart+i*2,setup[i]);
380 if (this_lparam) /* param[] */
381 memcpy(outparam,param,this_lparam);
382 if (this_ldata) /* data[] */
383 memcpy(outdata,data,this_ldata);
385 cli_setup_bcc(cli, outdata+this_ldata);
387 show_msg(cli->outbuf);
388 if (!cli_send_smb(cli)) {
389 return False;
392 /* Note we're in a trans state. Save the sequence
393 * numbers for replies. */
394 client_set_trans_sign_state_on(cli, mid);
396 if (this_ldata < ldata || this_lparam < lparam) {
397 /* receive interim response */
398 if (!cli_receive_smb(cli) || cli_is_error(cli)) {
399 client_set_trans_sign_state_off(cli, mid);
400 return(False);
403 tot_data = this_ldata;
404 tot_param = this_lparam;
406 while (tot_data < ldata || tot_param < lparam) {
407 this_lparam = MIN(lparam-tot_param,cli->max_xmit - 500); /* hack */
408 this_ldata = MIN(ldata-tot_data,cli->max_xmit - (500+this_lparam));
410 set_message(cli->outbuf,18,0,True);
411 SCVAL(cli->outbuf,smb_com,SMBnttranss);
413 /* XXX - these should probably be aligned */
414 outparam = smb_buf(cli->outbuf);
415 outdata = outparam+this_lparam;
417 /* secondary request */
418 SIVAL(cli->outbuf,smb_nts_TotalParameterCount,lparam);
419 SIVAL(cli->outbuf,smb_nts_TotalDataCount,ldata);
420 SIVAL(cli->outbuf,smb_nts_ParameterCount,this_lparam);
421 SIVAL(cli->outbuf,smb_nts_ParameterOffset,smb_offset(outparam,cli->outbuf));
422 SIVAL(cli->outbuf,smb_nts_ParameterDisplacement,tot_param);
423 SIVAL(cli->outbuf,smb_nts_DataCount,this_ldata);
424 SIVAL(cli->outbuf,smb_nts_DataOffset,smb_offset(outdata,cli->outbuf));
425 SIVAL(cli->outbuf,smb_nts_DataDisplacement,tot_data);
426 if (this_lparam) /* param[] */
427 memcpy(outparam,param+tot_param,this_lparam);
428 if (this_ldata) /* data[] */
429 memcpy(outdata,data+tot_data,this_ldata);
430 cli_setup_bcc(cli, outdata+this_ldata);
433 * Save the mid we're using. We need this for finding
434 * signing replies.
436 mid = cli->mid;
438 show_msg(cli->outbuf);
440 if (!cli_send_smb(cli)) {
441 client_set_trans_sign_state_off(cli, mid);
442 return False;
445 /* Ensure we use the same mid for the secondaries. */
446 cli->mid = mid;
448 tot_data += this_ldata;
449 tot_param += this_lparam;
453 return(True);
456 /****************************************************************************
457 Receive a SMB nttrans response allocating the necessary memory.
458 ****************************************************************************/
460 BOOL cli_receive_nt_trans(struct cli_state *cli,
461 char **param, unsigned int *param_len,
462 char **data, unsigned int *data_len)
464 unsigned int total_data=0;
465 unsigned int total_param=0;
466 unsigned int this_data,this_param;
467 uint8 eclass;
468 uint32 ecode;
469 BOOL ret = False;
471 *data_len = *param_len = 0;
473 if (!cli_receive_smb(cli)) {
474 return False;
477 show_msg(cli->inbuf);
479 /* sanity check */
480 if (CVAL(cli->inbuf,smb_com) != SMBnttrans) {
481 DEBUG(0,("Expected SMBnttrans response, got command 0x%02x\n",
482 CVAL(cli->inbuf,smb_com)));
483 return(False);
487 * An NT RPC pipe call can return ERRDOS, ERRmoredata
488 * to a trans call. This is not an error and should not
489 * be treated as such.
491 if (cli_is_dos_error(cli)) {
492 cli_dos_error(cli, &eclass, &ecode);
493 if (!(eclass == ERRDOS && ecode == ERRmoredata)) {
494 goto out;
499 * Likewise for NT_STATUS_BUFFER_TOO_SMALL
501 if (cli_is_nt_error(cli)) {
502 if (!NT_STATUS_EQUAL(cli_nt_error(cli),
503 NT_STATUS_BUFFER_TOO_SMALL)) {
504 goto out;
508 /* parse out the lengths */
509 total_data = SVAL(cli->inbuf,smb_ntr_TotalDataCount);
510 total_param = SVAL(cli->inbuf,smb_ntr_TotalParameterCount);
512 /* allocate it */
513 if (total_data) {
514 *data = (char *)SMB_REALLOC(*data,total_data);
515 if (!(*data)) {
516 DEBUG(0,("cli_receive_nt_trans: failed to enlarge data buffer to %d\n",total_data));
517 goto out;
521 if (total_param) {
522 *param = (char *)SMB_REALLOC(*param,total_param);
523 if (!(*param)) {
524 DEBUG(0,("cli_receive_nt_trans: failed to enlarge param buffer to %d\n", total_param));
525 goto out;
529 while (1) {
530 this_data = SVAL(cli->inbuf,smb_ntr_DataCount);
531 this_param = SVAL(cli->inbuf,smb_ntr_ParameterCount);
533 if (this_data + *data_len > total_data ||
534 this_param + *param_len > total_param) {
535 DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
536 goto out;
539 if (this_data + *data_len < this_data ||
540 this_data + *data_len < *data_len ||
541 this_param + *param_len < this_param ||
542 this_param + *param_len < *param_len) {
543 DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
544 goto out;
547 if (this_data) {
548 unsigned int data_offset_out = SVAL(cli->inbuf,smb_ntr_DataDisplacement);
549 unsigned int data_offset_in = SVAL(cli->inbuf,smb_ntr_DataOffset);
551 if (data_offset_out > total_data ||
552 data_offset_out + this_data > total_data ||
553 data_offset_out + this_data < data_offset_out ||
554 data_offset_out + this_data < this_data) {
555 DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
556 goto out;
558 if (data_offset_in > cli->bufsize ||
559 data_offset_in + this_data > cli->bufsize ||
560 data_offset_in + this_data < data_offset_in ||
561 data_offset_in + this_data < this_data) {
562 DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
563 goto out;
566 memcpy(*data + data_offset_out, smb_base(cli->inbuf) + data_offset_in, this_data);
569 if (this_param) {
570 unsigned int param_offset_out = SVAL(cli->inbuf,smb_ntr_ParameterDisplacement);
571 unsigned int param_offset_in = SVAL(cli->inbuf,smb_ntr_ParameterOffset);
573 if (param_offset_out > total_param ||
574 param_offset_out + this_param > total_param ||
575 param_offset_out + this_param < param_offset_out ||
576 param_offset_out + this_param < this_param) {
577 DEBUG(1,("Param overflow in cli_receive_nt_trans\n"));
578 goto out;
580 if (param_offset_in > cli->bufsize ||
581 param_offset_in + this_param > cli->bufsize ||
582 param_offset_in + this_param < param_offset_in ||
583 param_offset_in + this_param < this_param) {
584 DEBUG(1,("Param overflow in cli_receive_nt_trans\n"));
585 goto out;
588 memcpy(*param + param_offset_out, smb_base(cli->inbuf) + param_offset_in, this_param);
591 *data_len += this_data;
592 *param_len += this_param;
594 if (total_data <= *data_len && total_param <= *param_len) {
595 ret = True;
596 break;
599 if (!cli_receive_smb(cli)) {
600 goto out;
603 show_msg(cli->inbuf);
605 /* sanity check */
606 if (CVAL(cli->inbuf,smb_com) != SMBnttrans) {
607 DEBUG(0,("Expected SMBnttrans response, got command 0x%02x\n",
608 CVAL(cli->inbuf,smb_com)));
609 goto out;
611 if (cli_is_dos_error(cli)) {
612 cli_dos_error(cli, &eclass, &ecode);
613 if(!(eclass == ERRDOS && ecode == ERRmoredata)) {
614 goto out;
618 * Likewise for NT_STATUS_BUFFER_TOO_SMALL
620 if (cli_is_nt_error(cli)) {
621 if (!NT_STATUS_EQUAL(cli_nt_error(cli),
622 NT_STATUS_BUFFER_TOO_SMALL)) {
623 goto out;
627 /* parse out the total lengths again - they can shrink! */
628 if (SVAL(cli->inbuf,smb_ntr_TotalDataCount) < total_data)
629 total_data = SVAL(cli->inbuf,smb_ntr_TotalDataCount);
630 if (SVAL(cli->inbuf,smb_ntr_TotalParameterCount) < total_param)
631 total_param = SVAL(cli->inbuf,smb_ntr_TotalParameterCount);
633 if (total_data <= *data_len && total_param <= *param_len) {
634 ret = True;
635 break;
639 out:
641 client_set_trans_sign_state_off(cli, SVAL(cli->inbuf,smb_mid));
642 return ret;