2 Unix SMB/Netbios implementation.
4 client transaction calls
5 Copyright (C) Andrew Tridgell 1994-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 /****************************************************************************
28 send a SMB trans or trans2 request
29 ****************************************************************************/
30 BOOL cli_send_trans(struct cli_state *cli, int trans,
31 const char *pipe_name,
33 uint16 *setup, int lsetup, int msetup,
34 char *param, int lparam, int mparam,
35 char *data, int ldata, int mdata)
38 int this_ldata,this_lparam;
39 int tot_data=0,tot_param=0;
40 char *outdata,*outparam;
44 this_lparam = MIN(lparam,cli->max_xmit - (500+lsetup*2)); /* hack */
45 this_ldata = MIN(ldata,cli->max_xmit - (500+lsetup*2+this_lparam));
47 memset(cli->outbuf,'\0',smb_size);
48 set_message(cli->outbuf,14+lsetup,0,True);
49 CVAL(cli->outbuf,smb_com) = trans;
50 SSVAL(cli->outbuf,smb_tid, cli->cnum);
51 cli_setup_packet(cli);
54 pipe_name_len = clistr_push(cli, smb_buf(cli->outbuf), pipe_name, -1, STR_TERMINATE);
57 outparam = smb_buf(cli->outbuf)+(trans==SMBtrans ? pipe_name_len : 3);
58 outdata = outparam+this_lparam;
61 SSVAL(cli->outbuf,smb_tpscnt,lparam); /* tpscnt */
62 SSVAL(cli->outbuf,smb_tdscnt,ldata); /* tdscnt */
63 SSVAL(cli->outbuf,smb_mprcnt,mparam); /* mprcnt */
64 SSVAL(cli->outbuf,smb_mdrcnt,mdata); /* mdrcnt */
65 SCVAL(cli->outbuf,smb_msrcnt,msetup); /* msrcnt */
66 SSVAL(cli->outbuf,smb_flags,flags); /* flags */
67 SIVAL(cli->outbuf,smb_timeout,0); /* timeout */
68 SSVAL(cli->outbuf,smb_pscnt,this_lparam); /* pscnt */
69 SSVAL(cli->outbuf,smb_psoff,smb_offset(outparam,cli->outbuf)); /* psoff */
70 SSVAL(cli->outbuf,smb_dscnt,this_ldata); /* dscnt */
71 SSVAL(cli->outbuf,smb_dsoff,smb_offset(outdata,cli->outbuf)); /* dsoff */
72 SCVAL(cli->outbuf,smb_suwcnt,lsetup); /* suwcnt */
73 for (i=0;i<lsetup;i++) /* setup[] */
74 SSVAL(cli->outbuf,smb_setup+i*2,setup[i]);
75 p = smb_buf(cli->outbuf);
76 if (trans != SMBtrans) {
77 *p++ = 0; /* put in a null smb_name */
78 *p++ = 'D'; *p++ = ' '; /* observed in OS/2 */
80 if (this_lparam) /* param[] */
81 memcpy(outparam,param,this_lparam);
82 if (this_ldata) /* data[] */
83 memcpy(outdata,data,this_ldata);
84 cli_setup_bcc(cli, outdata+this_ldata);
86 show_msg(cli->outbuf);
89 if (this_ldata < ldata || this_lparam < lparam) {
90 /* receive interim response */
91 if (!cli_receive_smb(cli) ||
92 CVAL(cli->inbuf,smb_rcls) != 0) {
96 tot_data = this_ldata;
97 tot_param = this_lparam;
99 while (tot_data < ldata || tot_param < lparam) {
100 this_lparam = MIN(lparam-tot_param,cli->max_xmit - 500); /* hack */
101 this_ldata = MIN(ldata-tot_data,cli->max_xmit - (500+this_lparam));
103 set_message(cli->outbuf,trans==SMBtrans?8:9,0,True);
104 CVAL(cli->outbuf,smb_com) = trans==SMBtrans ? SMBtranss : SMBtranss2;
106 outparam = smb_buf(cli->outbuf);
107 outdata = outparam+this_lparam;
109 /* secondary request */
110 SSVAL(cli->outbuf,smb_tpscnt,lparam); /* tpscnt */
111 SSVAL(cli->outbuf,smb_tdscnt,ldata); /* tdscnt */
112 SSVAL(cli->outbuf,smb_spscnt,this_lparam); /* pscnt */
113 SSVAL(cli->outbuf,smb_spsoff,smb_offset(outparam,cli->outbuf)); /* psoff */
114 SSVAL(cli->outbuf,smb_spsdisp,tot_param); /* psdisp */
115 SSVAL(cli->outbuf,smb_sdscnt,this_ldata); /* dscnt */
116 SSVAL(cli->outbuf,smb_sdsoff,smb_offset(outdata,cli->outbuf)); /* dsoff */
117 SSVAL(cli->outbuf,smb_sdsdisp,tot_data); /* dsdisp */
118 if (trans==SMBtrans2)
119 SSVALS(cli->outbuf,smb_sfid,fid); /* fid */
120 if (this_lparam) /* param[] */
121 memcpy(outparam,param+tot_param,this_lparam);
122 if (this_ldata) /* data[] */
123 memcpy(outdata,data+tot_data,this_ldata);
124 cli_setup_bcc(cli, outdata+this_ldata);
126 show_msg(cli->outbuf);
129 tot_data += this_ldata;
130 tot_param += this_lparam;
138 /****************************************************************************
139 receive a SMB trans or trans2 response allocating the necessary memory
140 ****************************************************************************/
141 BOOL cli_receive_trans(struct cli_state *cli,int trans,
142 char **param, int *param_len,
143 char **data, int *data_len)
147 int this_data,this_param;
152 *data_len = *param_len = 0;
154 if (!cli_receive_smb(cli))
157 show_msg(cli->inbuf);
160 if (CVAL(cli->inbuf,smb_com) != trans) {
161 DEBUG(0,("Expected %s response, got command 0x%02x\n",
162 trans==SMBtrans?"SMBtrans":"SMBtrans2",
163 CVAL(cli->inbuf,smb_com)));
168 * An NT RPC pipe call can return ERRDOS, ERRmoredata
169 * to a trans call. This is not an error and should not
170 * be treated as such.
173 if (cli_is_dos_error(cli))
175 cli_dos_error(cli, &eclass, &ecode);
177 if(cli->nt_pipe_fnum == 0)
180 if(!(eclass == ERRDOS && ecode == ERRmoredata)) {
181 if (eclass != 0 && (ecode != (0x80000000 | STATUS_BUFFER_OVERFLOW)))
186 /* parse out the lengths */
187 total_data = SVAL(cli->inbuf,smb_tdrcnt);
188 total_param = SVAL(cli->inbuf,smb_tprcnt);
192 tdata = Realloc(*data,total_data);
194 DEBUG(0,("cli_receive_trans: failed to enlarge data buffer\n"));
201 if (total_param!=0) {
202 tdata = Realloc(*param,total_param);
203 if (!tdata && total_param!=0) {
204 DEBUG(0,("cli_receive_trans: failed to enlarge param buffer\n"));
212 this_data = SVAL(cli->inbuf,smb_drcnt);
213 this_param = SVAL(cli->inbuf,smb_prcnt);
215 if (this_data + *data_len > total_data ||
216 this_param + *param_len > total_param) {
217 DEBUG(1,("Data overflow in cli_receive_trans\n"));
222 memcpy(*data + SVAL(cli->inbuf,smb_drdisp),
223 smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_droff),
226 memcpy(*param + SVAL(cli->inbuf,smb_prdisp),
227 smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_proff),
229 *data_len += this_data;
230 *param_len += this_param;
232 /* parse out the total lengths again - they can shrink! */
233 total_data = SVAL(cli->inbuf,smb_tdrcnt);
234 total_param = SVAL(cli->inbuf,smb_tprcnt);
236 if (total_data <= *data_len && total_param <= *param_len)
239 if (!cli_receive_smb(cli))
242 show_msg(cli->inbuf);
245 if (CVAL(cli->inbuf,smb_com) != trans) {
246 DEBUG(0,("Expected %s response, got command 0x%02x\n",
247 trans==SMBtrans?"SMBtrans":"SMBtrans2",
248 CVAL(cli->inbuf,smb_com)));
251 if (cli_is_dos_error(cli)) {
252 cli_dos_error(cli, &eclass, &ecode);
253 if(cli->nt_pipe_fnum == 0 ||
254 !(eclass == ERRDOS && ecode == ERRmoredata))
265 /****************************************************************************
266 send a SMB nttrans request
267 ****************************************************************************/
268 BOOL cli_send_nt_trans(struct cli_state *cli,
271 uint16 *setup, int lsetup, int msetup,
272 char *param, int lparam, int mparam,
273 char *data, int ldata, int mdata)
276 int this_ldata,this_lparam;
277 int tot_data=0,tot_param=0;
278 char *outdata,*outparam;
280 this_lparam = MIN(lparam,cli->max_xmit - (500+lsetup*2)); /* hack */
281 this_ldata = MIN(ldata,cli->max_xmit - (500+lsetup*2+this_lparam));
283 memset(cli->outbuf,'\0',smb_size);
284 set_message(cli->outbuf,19+lsetup,0,True);
285 CVAL(cli->outbuf,smb_com) = SMBnttrans;
286 SSVAL(cli->outbuf,smb_tid, cli->cnum);
287 cli_setup_packet(cli);
289 outparam = smb_buf(cli->outbuf)+3;
290 outdata = outparam+this_lparam;
292 /* primary request */
293 SCVAL(cli->outbuf,smb_nt_MaxSetupCount,msetup);
294 SCVAL(cli->outbuf,smb_nt_Flags,flags);
295 SIVAL(cli->outbuf,smb_nt_TotalParameterCount, lparam);
296 SIVAL(cli->outbuf,smb_nt_TotalDataCount, ldata);
297 SIVAL(cli->outbuf,smb_nt_MaxParameterCount, mparam);
298 SIVAL(cli->outbuf,smb_nt_MaxDataCount, mdata);
299 SIVAL(cli->outbuf,smb_nt_ParameterCount, this_lparam);
300 SIVAL(cli->outbuf,smb_nt_ParameterOffset, smb_offset(outparam,cli->outbuf));
301 SIVAL(cli->outbuf,smb_nt_DataCount, this_ldata);
302 SIVAL(cli->outbuf,smb_nt_DataOffset, smb_offset(outdata,cli->outbuf));
303 SIVAL(cli->outbuf,smb_nt_SetupCount, lsetup);
304 SIVAL(cli->outbuf,smb_nt_Function, function);
305 for (i=0;i<lsetup;i++) /* setup[] */
306 SSVAL(cli->outbuf,smb_nt_SetupStart+i*2,setup[i]);
308 if (this_lparam) /* param[] */
309 memcpy(outparam,param,this_lparam);
310 if (this_ldata) /* data[] */
311 memcpy(outdata,data,this_ldata);
313 cli_setup_bcc(cli, outdata+this_ldata);
315 show_msg(cli->outbuf);
318 if (this_ldata < ldata || this_lparam < lparam) {
319 /* receive interim response */
320 if (!cli_receive_smb(cli) ||
321 CVAL(cli->inbuf,smb_rcls) != 0) {
325 tot_data = this_ldata;
326 tot_param = this_lparam;
328 while (tot_data < ldata || tot_param < lparam) {
329 this_lparam = MIN(lparam-tot_param,cli->max_xmit - 500); /* hack */
330 this_ldata = MIN(ldata-tot_data,cli->max_xmit - (500+this_lparam));
332 set_message(cli->outbuf,18,0,True);
333 CVAL(cli->outbuf,smb_com) = SMBnttranss;
335 /* XXX - these should probably be aligned */
336 outparam = smb_buf(cli->outbuf);
337 outdata = outparam+this_lparam;
339 /* secondary request */
340 SIVAL(cli->outbuf,smb_nts_TotalParameterCount,lparam);
341 SIVAL(cli->outbuf,smb_nts_TotalDataCount,ldata);
342 SIVAL(cli->outbuf,smb_nts_ParameterCount,this_lparam);
343 SIVAL(cli->outbuf,smb_nts_ParameterOffset,smb_offset(outparam,cli->outbuf));
344 SIVAL(cli->outbuf,smb_nts_ParameterDisplacement,tot_param);
345 SIVAL(cli->outbuf,smb_nts_DataCount,this_ldata);
346 SIVAL(cli->outbuf,smb_nts_DataOffset,smb_offset(outdata,cli->outbuf));
347 SIVAL(cli->outbuf,smb_nts_DataDisplacement,tot_data);
348 if (this_lparam) /* param[] */
349 memcpy(outparam,param+tot_param,this_lparam);
350 if (this_ldata) /* data[] */
351 memcpy(outdata,data+tot_data,this_ldata);
352 cli_setup_bcc(cli, outdata+this_ldata);
354 show_msg(cli->outbuf);
357 tot_data += this_ldata;
358 tot_param += this_lparam;
367 /****************************************************************************
368 receive a SMB nttrans response allocating the necessary memory
369 ****************************************************************************/
370 BOOL cli_receive_nt_trans(struct cli_state *cli,
371 char **param, int *param_len,
372 char **data, int *data_len)
376 int this_data,this_param;
381 *data_len = *param_len = 0;
383 if (!cli_receive_smb(cli))
386 show_msg(cli->inbuf);
389 if (CVAL(cli->inbuf,smb_com) != SMBnttrans) {
390 DEBUG(0,("Expected SMBnttrans response, got command 0x%02x\n",
391 CVAL(cli->inbuf,smb_com)));
396 * An NT RPC pipe call can return ERRDOS, ERRmoredata
397 * to a trans call. This is not an error and should not
398 * be treated as such.
400 if (cli_is_dos_error(cli)) {
401 cli_dos_error(cli, &eclass, &ecode);
402 if (cli->nt_pipe_fnum == 0 || !(eclass == ERRDOS && ecode == ERRmoredata))
406 /* parse out the lengths */
407 total_data = SVAL(cli->inbuf,smb_ntr_TotalDataCount);
408 total_param = SVAL(cli->inbuf,smb_ntr_TotalParameterCount);
411 tdata = Realloc(*data,total_data);
413 DEBUG(0,("cli_receive_nt_trans: failed to enlarge buffer"));
417 tdata = Realloc(*param,total_param);
419 DEBUG(0,("cli_receive_nt_trans: failed to enlarge buffer"));
425 this_data = SVAL(cli->inbuf,smb_ntr_DataCount);
426 this_param = SVAL(cli->inbuf,smb_ntr_ParameterCount);
428 if (this_data + *data_len > total_data ||
429 this_param + *param_len > total_param) {
430 DEBUG(1,("Data overflow in cli_receive_trans\n"));
435 memcpy(*data + SVAL(cli->inbuf,smb_ntr_DataDisplacement),
436 smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_ntr_DataOffset),
439 memcpy(*param + SVAL(cli->inbuf,smb_ntr_ParameterDisplacement),
440 smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_ntr_ParameterOffset),
442 *data_len += this_data;
443 *param_len += this_param;
445 /* parse out the total lengths again - they can shrink! */
446 total_data = SVAL(cli->inbuf,smb_ntr_TotalDataCount);
447 total_param = SVAL(cli->inbuf,smb_ntr_TotalParameterCount);
449 if (total_data <= *data_len && total_param <= *param_len)
452 if (!cli_receive_smb(cli))
455 show_msg(cli->inbuf);
458 if (CVAL(cli->inbuf,smb_com) != SMBnttrans) {
459 DEBUG(0,("Expected SMBnttrans response, got command 0x%02x\n",
460 CVAL(cli->inbuf,smb_com)));
463 if (cli_is_dos_error(cli)) {
464 cli_dos_error(cli, &eclass, &ecode);
465 if(cli->nt_pipe_fnum == 0 ||
466 !(eclass == ERRDOS && ecode == ERRmoredata))