r21899: At least we're getting to stage 2 of the blob
[tprouty/samba.git] / source3 / libsmb / clitrans.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client transaction calls
4    Copyright (C) Andrew Tridgell 1994-1998
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22
23
24 /****************************************************************************
25  Send a SMB trans or trans2 request.
26 ****************************************************************************/
27
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)
34 {
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;
42
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));
45
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);
51
52         /*
53          * Save the mid we're using. We need this for finding
54          * signing replies.
55          */
56
57         mid = cli->mid;
58
59         if (pipe_name) {
60                 pipe_name_len = clistr_push(cli, smb_buf(cli->outbuf), pipe_name, -1, STR_TERMINATE);
61         }
62
63         outparam = smb_buf(cli->outbuf)+(trans==SMBtrans ? pipe_name_len : 3);
64         outdata = outparam+this_lparam;
65
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 */
85         }
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);
91
92         show_msg(cli->outbuf);
93
94         if (!cli_send_smb(cli)) {
95                 return False;
96         }
97
98         /* Note we're in a trans state. Save the sequence
99          * numbers for replies. */
100         client_set_trans_sign_state_on(cli, mid);
101
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);
107                 }
108
109                 tot_data = this_ldata;
110                 tot_param = this_lparam;
111                 
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));
115
116                         set_message(cli->outbuf,trans==SMBtrans?8:9,0,True);
117                         SCVAL(cli->outbuf,smb_com,(trans==SMBtrans ? SMBtranss : SMBtranss2));
118                         
119                         outparam = smb_buf(cli->outbuf);
120                         outdata = outparam+this_lparam;
121                         
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);
138                         
139                         /*
140                          * Save the mid we're using. We need this for finding
141                          * signing replies.
142                          */
143                         mid = cli->mid;
144
145                         show_msg(cli->outbuf);
146                         if (!cli_send_smb(cli)) {
147                                 client_set_trans_sign_state_off(cli, mid);
148                                 return False;
149                         }
150
151                         /* Ensure we use the same mid for the secondaries. */
152                         cli->mid = mid;
153                         
154                         tot_data += this_ldata;
155                         tot_param += this_lparam;
156                 }
157         }
158
159         return(True);
160 }
161
162 /****************************************************************************
163  Receive a SMB trans or trans2 response allocating the necessary memory.
164 ****************************************************************************/
165
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)
169 {
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;
175
176         *data_len = *param_len = 0;
177
178         if (!cli_receive_smb(cli)) {
179                 return False;
180         }
181
182         show_msg(cli->inbuf);
183         
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;
190         }
191
192         /*
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.
197          * When setting up an encrypted transport we can also
198          * see NT_STATUS_MORE_PROCESSING_REQUIRED here.
199          */
200         status = cli_nt_error(cli);
201         
202         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
203                 if (NT_STATUS_IS_ERR(status) || NT_STATUS_EQUAL(status,STATUS_NO_MORE_FILES)) {
204                         goto out;
205                 }
206         }
207
208         /* parse out the lengths */
209         total_data = SVAL(cli->inbuf,smb_tdrcnt);
210         total_param = SVAL(cli->inbuf,smb_tprcnt);
211
212         /* allocate it */
213         if (total_data!=0) {
214                 *data = (char *)SMB_REALLOC(*data,total_data);
215                 if (!(*data)) {
216                         DEBUG(0,("cli_receive_trans: failed to enlarge data buffer\n"));
217                         goto out;
218                 }
219         }
220
221         if (total_param!=0) {
222                 *param = (char *)SMB_REALLOC(*param,total_param);
223                 if (!(*param)) {
224                         DEBUG(0,("cli_receive_trans: failed to enlarge param buffer\n"));
225                         goto out;
226                 }
227         }
228
229         for (;;)  {
230                 this_data = SVAL(cli->inbuf,smb_drcnt);
231                 this_param = SVAL(cli->inbuf,smb_prcnt);
232
233                 if (this_data + *data_len > total_data ||
234                     this_param + *param_len > total_param) {
235                         DEBUG(1,("Data overflow in cli_receive_trans\n"));
236                         goto out;
237                 }
238
239                 if (this_data + *data_len < this_data ||
240                                 this_data + *data_len < *data_len ||
241                                 this_param + *param_len < this_param ||
242                                 this_param + *param_len < *param_len) {
243                         DEBUG(1,("Data overflow in cli_receive_trans\n"));
244                         goto out;
245                 }
246
247                 if (this_data) {
248                         unsigned int data_offset_out = SVAL(cli->inbuf,smb_drdisp);
249                         unsigned int data_offset_in = SVAL(cli->inbuf,smb_droff);
250
251                         if (data_offset_out > total_data ||
252                                         data_offset_out + this_data > total_data ||
253                                         data_offset_out + this_data < data_offset_out ||
254                                         data_offset_out + this_data < this_data) {
255                                 DEBUG(1,("Data overflow in cli_receive_trans\n"));
256                                 goto out;
257                         }
258                         if (data_offset_in > cli->bufsize ||
259                                         data_offset_in + this_data >  cli->bufsize ||
260                                         data_offset_in + this_data < data_offset_in ||
261                                         data_offset_in + this_data < this_data) {
262                                 DEBUG(1,("Data overflow in cli_receive_trans\n"));
263                                 goto out;
264                         }
265
266                         memcpy(*data + data_offset_out, smb_base(cli->inbuf) + data_offset_in, this_data);
267                 }
268                 if (this_param) {
269                         unsigned int param_offset_out = SVAL(cli->inbuf,smb_prdisp);
270                         unsigned int param_offset_in = SVAL(cli->inbuf,smb_proff);
271
272                         if (param_offset_out > total_param ||
273                                         param_offset_out + this_param > total_param ||
274                                         param_offset_out + this_param < param_offset_out ||
275                                         param_offset_out + this_param < this_param) {
276                                 DEBUG(1,("Param overflow in cli_receive_trans\n"));
277                                 goto out;
278                         }
279                         if (param_offset_in > cli->bufsize ||
280                                         param_offset_in + this_param >  cli->bufsize ||
281                                         param_offset_in + this_param < param_offset_in ||
282                                         param_offset_in + this_param < this_param) {
283                                 DEBUG(1,("Param overflow in cli_receive_trans\n"));
284                                 goto out;
285                         }
286
287                         memcpy(*param + param_offset_out, smb_base(cli->inbuf) + param_offset_in, this_param);
288                 }
289                 *data_len += this_data;
290                 *param_len += this_param;
291
292                 if (total_data <= *data_len && total_param <= *param_len) {
293                         ret = True;
294                         break;
295                 }
296                 
297                 if (!cli_receive_smb(cli)) {
298                         goto out;
299                 }
300
301                 show_msg(cli->inbuf);
302                 
303                 /* sanity check */
304                 if (CVAL(cli->inbuf,smb_com) != trans) {
305                         DEBUG(0,("Expected %s response, got command 0x%02x\n",
306                                  trans==SMBtrans?"SMBtrans":"SMBtrans2", 
307                                  CVAL(cli->inbuf,smb_com)));
308                         goto out;
309                 }
310                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
311                         if (NT_STATUS_IS_ERR(cli_nt_error(cli))) {
312                                 goto out;
313                         }
314                 }
315
316                 /* parse out the total lengths again - they can shrink! */
317                 if (SVAL(cli->inbuf,smb_tdrcnt) < total_data)
318                         total_data = SVAL(cli->inbuf,smb_tdrcnt);
319                 if (SVAL(cli->inbuf,smb_tprcnt) < total_param)
320                         total_param = SVAL(cli->inbuf,smb_tprcnt);
321                 
322                 if (total_data <= *data_len && total_param <= *param_len) {
323                         ret = True;
324                         break;
325                 }
326         }
327
328   out:
329
330         client_set_trans_sign_state_off(cli, SVAL(cli->inbuf,smb_mid));
331         return ret;
332 }
333
334 /****************************************************************************
335  Send a SMB nttrans request.
336 ****************************************************************************/
337
338 BOOL cli_send_nt_trans(struct cli_state *cli, 
339                        int function, 
340                        int flags,
341                        uint16 *setup, unsigned int lsetup, unsigned int msetup,
342                        char *param, unsigned int lparam, unsigned int mparam,
343                        char *data, unsigned int ldata, unsigned int mdata)
344 {
345         unsigned int i;
346         unsigned int this_ldata,this_lparam;
347         unsigned int tot_data=0,tot_param=0;
348         uint16 mid;
349         char *outdata,*outparam;
350
351         this_lparam = MIN(lparam,cli->max_xmit - (500+lsetup*2)); /* hack */
352         this_ldata = MIN(ldata,cli->max_xmit - (500+lsetup*2+this_lparam));
353
354         memset(cli->outbuf,'\0',smb_size);
355         set_message(cli->outbuf,19+lsetup,0,True);
356         SCVAL(cli->outbuf,smb_com,SMBnttrans);
357         SSVAL(cli->outbuf,smb_tid, cli->cnum);
358         cli_setup_packet(cli);
359
360         /*
361          * Save the mid we're using. We need this for finding
362          * signing replies.
363          */
364
365         mid = cli->mid;
366
367         outparam = smb_buf(cli->outbuf)+3;
368         outdata = outparam+this_lparam;
369
370         /* primary request */
371         SCVAL(cli->outbuf,smb_nt_MaxSetupCount,msetup);
372         SCVAL(cli->outbuf,smb_nt_Flags,flags);
373         SIVAL(cli->outbuf,smb_nt_TotalParameterCount, lparam);
374         SIVAL(cli->outbuf,smb_nt_TotalDataCount, ldata);
375         SIVAL(cli->outbuf,smb_nt_MaxParameterCount, mparam);
376         SIVAL(cli->outbuf,smb_nt_MaxDataCount, mdata);
377         SIVAL(cli->outbuf,smb_nt_ParameterCount, this_lparam);
378         SIVAL(cli->outbuf,smb_nt_ParameterOffset, smb_offset(outparam,cli->outbuf));
379         SIVAL(cli->outbuf,smb_nt_DataCount, this_ldata);
380         SIVAL(cli->outbuf,smb_nt_DataOffset, smb_offset(outdata,cli->outbuf));
381         SIVAL(cli->outbuf,smb_nt_SetupCount, lsetup);
382         SIVAL(cli->outbuf,smb_nt_Function, function);
383         for (i=0;i<lsetup;i++)          /* setup[] */
384                 SSVAL(cli->outbuf,smb_nt_SetupStart+i*2,setup[i]);
385         
386         if (this_lparam)                        /* param[] */
387                 memcpy(outparam,param,this_lparam);
388         if (this_ldata)                 /* data[] */
389                 memcpy(outdata,data,this_ldata);
390
391         cli_setup_bcc(cli, outdata+this_ldata);
392
393         show_msg(cli->outbuf);
394         if (!cli_send_smb(cli)) {
395                 return False;
396         }       
397
398         /* Note we're in a trans state. Save the sequence
399          * numbers for replies. */
400         client_set_trans_sign_state_on(cli, mid);
401
402         if (this_ldata < ldata || this_lparam < lparam) {
403                 /* receive interim response */
404                 if (!cli_receive_smb(cli) || cli_is_error(cli)) {
405                         client_set_trans_sign_state_off(cli, mid);
406                         return(False);
407                 }
408
409                 tot_data = this_ldata;
410                 tot_param = this_lparam;
411                 
412                 while (tot_data < ldata || tot_param < lparam)  {
413                         this_lparam = MIN(lparam-tot_param,cli->max_xmit - 500); /* hack */
414                         this_ldata = MIN(ldata-tot_data,cli->max_xmit - (500+this_lparam));
415
416                         set_message(cli->outbuf,18,0,True);
417                         SCVAL(cli->outbuf,smb_com,SMBnttranss);
418
419                         /* XXX - these should probably be aligned */
420                         outparam = smb_buf(cli->outbuf);
421                         outdata = outparam+this_lparam;
422                         
423                         /* secondary request */
424                         SIVAL(cli->outbuf,smb_nts_TotalParameterCount,lparam);
425                         SIVAL(cli->outbuf,smb_nts_TotalDataCount,ldata);
426                         SIVAL(cli->outbuf,smb_nts_ParameterCount,this_lparam);
427                         SIVAL(cli->outbuf,smb_nts_ParameterOffset,smb_offset(outparam,cli->outbuf));
428                         SIVAL(cli->outbuf,smb_nts_ParameterDisplacement,tot_param);
429                         SIVAL(cli->outbuf,smb_nts_DataCount,this_ldata);
430                         SIVAL(cli->outbuf,smb_nts_DataOffset,smb_offset(outdata,cli->outbuf));
431                         SIVAL(cli->outbuf,smb_nts_DataDisplacement,tot_data);
432                         if (this_lparam)                        /* param[] */
433                                 memcpy(outparam,param+tot_param,this_lparam);
434                         if (this_ldata)                 /* data[] */
435                                 memcpy(outdata,data+tot_data,this_ldata);
436                         cli_setup_bcc(cli, outdata+this_ldata);
437                         
438                         /*
439                          * Save the mid we're using. We need this for finding
440                          * signing replies.
441                          */
442                         mid = cli->mid;
443
444                         show_msg(cli->outbuf);
445
446                         if (!cli_send_smb(cli)) {
447                                 client_set_trans_sign_state_off(cli, mid);
448                                 return False;
449                         }
450                         
451                         /* Ensure we use the same mid for the secondaries. */
452                         cli->mid = mid;
453                         
454                         tot_data += this_ldata;
455                         tot_param += this_lparam;
456                 }
457         }
458
459         return(True);
460 }
461
462 /****************************************************************************
463  Receive a SMB nttrans response allocating the necessary memory.
464 ****************************************************************************/
465
466 BOOL cli_receive_nt_trans(struct cli_state *cli,
467                           char **param, unsigned int *param_len,
468                           char **data, unsigned int *data_len)
469 {
470         unsigned int total_data=0;
471         unsigned int total_param=0;
472         unsigned int this_data,this_param;
473         uint8 eclass;
474         uint32 ecode;
475         BOOL ret = False;
476
477         *data_len = *param_len = 0;
478
479         if (!cli_receive_smb(cli)) {
480                 return False;
481         }
482
483         show_msg(cli->inbuf);
484         
485         /* sanity check */
486         if (CVAL(cli->inbuf,smb_com) != SMBnttrans) {
487                 DEBUG(0,("Expected SMBnttrans response, got command 0x%02x\n",
488                          CVAL(cli->inbuf,smb_com)));
489                 return(False);
490         }
491
492         /*
493          * An NT RPC pipe call can return ERRDOS, ERRmoredata
494          * to a trans call. This is not an error and should not
495          * be treated as such.
496          */
497         if (cli_is_dos_error(cli)) {
498                 cli_dos_error(cli, &eclass, &ecode);
499                 if (!(eclass == ERRDOS && ecode == ERRmoredata)) {
500                         goto out;
501                 }
502         }
503
504         /*
505          * Likewise for NT_STATUS_BUFFER_TOO_SMALL
506          */
507         if (cli_is_nt_error(cli)) {
508                 if (!NT_STATUS_EQUAL(cli_nt_error(cli),
509                                      NT_STATUS_BUFFER_TOO_SMALL)) {
510                         goto out;
511                 }
512         }
513
514         /* parse out the lengths */
515         total_data = SVAL(cli->inbuf,smb_ntr_TotalDataCount);
516         total_param = SVAL(cli->inbuf,smb_ntr_TotalParameterCount);
517
518         /* allocate it */
519         if (total_data) {
520                 *data = (char *)SMB_REALLOC(*data,total_data);
521                 if (!(*data)) {
522                         DEBUG(0,("cli_receive_nt_trans: failed to enlarge data buffer to %d\n",total_data));
523                         goto out;
524                 }
525         }
526
527         if (total_param) {
528                 *param = (char *)SMB_REALLOC(*param,total_param);
529                 if (!(*param)) {
530                         DEBUG(0,("cli_receive_nt_trans: failed to enlarge param buffer to %d\n", total_param));
531                         goto out;
532                 }
533         }
534
535         while (1)  {
536                 this_data = SVAL(cli->inbuf,smb_ntr_DataCount);
537                 this_param = SVAL(cli->inbuf,smb_ntr_ParameterCount);
538
539                 if (this_data + *data_len > total_data ||
540                     this_param + *param_len > total_param) {
541                         DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
542                         goto out;
543                 }
544
545                 if (this_data + *data_len < this_data ||
546                                 this_data + *data_len < *data_len ||
547                                 this_param + *param_len < this_param ||
548                                 this_param + *param_len < *param_len) {
549                         DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
550                         goto out;
551                 }
552
553                 if (this_data) {
554                         unsigned int data_offset_out = SVAL(cli->inbuf,smb_ntr_DataDisplacement);
555                         unsigned int data_offset_in = SVAL(cli->inbuf,smb_ntr_DataOffset);
556
557                         if (data_offset_out > total_data ||
558                                         data_offset_out + this_data > total_data ||
559                                         data_offset_out + this_data < data_offset_out ||
560                                         data_offset_out + this_data < this_data) {
561                                 DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
562                                 goto out;
563                         }
564                         if (data_offset_in > cli->bufsize ||
565                                         data_offset_in + this_data >  cli->bufsize ||
566                                         data_offset_in + this_data < data_offset_in ||
567                                         data_offset_in + this_data < this_data) {
568                                 DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
569                                 goto out;
570                         }
571
572                         memcpy(*data + data_offset_out, smb_base(cli->inbuf) + data_offset_in, this_data);
573                 }
574
575                 if (this_param) {
576                         unsigned int param_offset_out = SVAL(cli->inbuf,smb_ntr_ParameterDisplacement);
577                         unsigned int param_offset_in = SVAL(cli->inbuf,smb_ntr_ParameterOffset);
578
579                         if (param_offset_out > total_param ||
580                                         param_offset_out + this_param > total_param ||
581                                         param_offset_out + this_param < param_offset_out ||
582                                         param_offset_out + this_param < this_param) {
583                                 DEBUG(1,("Param overflow in cli_receive_nt_trans\n"));
584                                 goto out;
585                         }
586                         if (param_offset_in > cli->bufsize ||
587                                         param_offset_in + this_param >  cli->bufsize ||
588                                         param_offset_in + this_param < param_offset_in ||
589                                         param_offset_in + this_param < this_param) {
590                                 DEBUG(1,("Param overflow in cli_receive_nt_trans\n"));
591                                 goto out;
592                         }
593
594                         memcpy(*param + param_offset_out, smb_base(cli->inbuf) + param_offset_in, this_param);
595                 }
596
597                 *data_len += this_data;
598                 *param_len += this_param;
599
600                 if (total_data <= *data_len && total_param <= *param_len) {
601                         ret = True;
602                         break;
603                 }
604                 
605                 if (!cli_receive_smb(cli)) {
606                         goto out;
607                 }
608
609                 show_msg(cli->inbuf);
610                 
611                 /* sanity check */
612                 if (CVAL(cli->inbuf,smb_com) != SMBnttrans) {
613                         DEBUG(0,("Expected SMBnttrans response, got command 0x%02x\n",
614                                  CVAL(cli->inbuf,smb_com)));
615                         goto out;
616                 }
617                 if (cli_is_dos_error(cli)) {
618                         cli_dos_error(cli, &eclass, &ecode);
619                         if(!(eclass == ERRDOS && ecode == ERRmoredata)) {
620                                 goto out;
621                         }
622                 }
623                 /*
624                  * Likewise for NT_STATUS_BUFFER_TOO_SMALL
625                  */
626                 if (cli_is_nt_error(cli)) {
627                         if (!NT_STATUS_EQUAL(cli_nt_error(cli),
628                                              NT_STATUS_BUFFER_TOO_SMALL)) {
629                                 goto out;
630                         }
631                 }
632
633                 /* parse out the total lengths again - they can shrink! */
634                 if (SVAL(cli->inbuf,smb_ntr_TotalDataCount) < total_data)
635                         total_data = SVAL(cli->inbuf,smb_ntr_TotalDataCount);
636                 if (SVAL(cli->inbuf,smb_ntr_TotalParameterCount) < total_param)
637                         total_param = SVAL(cli->inbuf,smb_ntr_TotalParameterCount);
638                 
639                 if (total_data <= *data_len && total_param <= *param_len) {
640                         ret = True;
641                         break;
642                 }
643         }
644
645   out:
646
647         client_set_trans_sign_state_off(cli, SVAL(cli->inbuf,smb_mid));
648         return ret;
649 }