2 Unix SMB/Netbios implementation.
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.
36 extern pstring myname;
37 pstring password = "";
38 pstring smb_login_passwd = "";
40 pstring workgroup=WORKGROUP;
41 BOOL got_pass = False;
42 BOOL connect_as_printer = False;
43 BOOL connect_as_ipc = False;
48 extern pstring user_socket_options;
50 /* 30 second timeout on most commands */
51 #define CLIENT_TIMEOUT (30*1000)
52 #define SHORT_TIMEOUT (5*1000)
56 int max_protocol = PROTOCOL_NT1;
58 BOOL readbraw_supported = False;
59 BOOL writebraw_supported = False;
61 extern int DEBUGLEVEL;
69 int max_xmit = BUFFER_SIZE;
73 extern struct in_addr dest_ip;
80 /****************************************************************************
81 setup basics in a outgoing packet
82 ****************************************************************************/
83 void cli_setup_pkt(char *outbuf)
85 SSVAL(outbuf,smb_pid,pid);
86 SSVAL(outbuf,smb_uid,uid);
87 SSVAL(outbuf,smb_mid,mid);
88 if (Protocol > PROTOCOL_COREPLUS)
90 SCVAL(outbuf,smb_flg,0x8);
91 SSVAL(outbuf,smb_flg2,0x1);
95 /****************************************************************************
97 ****************************************************************************/
98 BOOL cli_call_api(char *pipe_name, int pipe_name_len,
99 int prcnt,int drcnt, int srcnt,
100 int mprcnt,int mdrcnt,
101 int *rprcnt,int *rdrcnt,
102 char *param,char *data, uint16 *setup,
103 char **rparam,char **rdata)
105 static char *inbuf=NULL;
106 static char *outbuf=NULL;
108 if (!inbuf) inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
109 if (!outbuf) outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
111 if (pipe_name_len == 0) pipe_name_len = strlen(pipe_name);
113 cli_send_trans_request(outbuf,SMBtrans,pipe_name, pipe_name_len, 0,0,
118 return (cli_receive_trans_response(inbuf,SMBtrans,
124 /****************************************************************************
125 receive a SMB trans or trans2 response allocating the necessary memory
126 ****************************************************************************/
127 BOOL cli_receive_trans_response(char *inbuf,int trans,
128 int *data_len,int *param_len,
129 char **data,char **param)
133 int this_data,this_param;
135 *data_len = *param_len = 0;
137 client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
141 if (CVAL(inbuf,smb_com) != trans)
143 DEBUG(0,("Expected %s response, got command 0x%02x\n",
144 trans==SMBtrans?"SMBtrans":"SMBtrans2", CVAL(inbuf,smb_com)));
147 if (CVAL(inbuf,smb_rcls) != 0)
150 /* parse out the lengths */
151 total_data = SVAL(inbuf,smb_tdrcnt);
152 total_param = SVAL(inbuf,smb_tprcnt);
155 *data = Realloc(*data,total_data);
156 *param = Realloc(*param,total_param);
160 this_data = SVAL(inbuf,smb_drcnt);
161 this_param = SVAL(inbuf,smb_prcnt);
163 memcpy(*data + SVAL(inbuf,smb_drdisp),
164 smb_base(inbuf) + SVAL(inbuf,smb_droff),
167 memcpy(*param + SVAL(inbuf,smb_prdisp),
168 smb_base(inbuf) + SVAL(inbuf,smb_proff),
170 *data_len += this_data;
171 *param_len += this_param;
173 /* parse out the total lengths again - they can shrink! */
174 total_data = SVAL(inbuf,smb_tdrcnt);
175 total_param = SVAL(inbuf,smb_tprcnt);
177 if (total_data <= *data_len && total_param <= *param_len)
180 client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
184 if (CVAL(inbuf,smb_com) != trans)
186 DEBUG(0,("Expected %s response, got command 0x%02x\n",
187 trans==SMBtrans?"SMBtrans":"SMBtrans2", CVAL(inbuf,smb_com)));
190 if (CVAL(inbuf,smb_rcls) != 0)
199 /****************************************************************************
200 send a SMB trans or trans2 request
201 ****************************************************************************/
202 BOOL cli_send_trans_request(char *outbuf,int trans,
203 char *name,int namelen, int fid,int flags,
204 char *data,char *param,uint16 *setup,
205 int ldata,int lparam,int lsetup,
206 int mdata,int mparam,int msetup)
209 int this_ldata,this_lparam;
210 int tot_data=0,tot_param=0;
211 char *outdata,*outparam;
215 this_lparam = MIN(lparam,max_xmit - (500+lsetup*SIZEOFWORD)); /* hack */
216 this_ldata = MIN(ldata,max_xmit - (500+lsetup*SIZEOFWORD+this_lparam));
218 bzero(outbuf,smb_size);
219 set_message(outbuf,14+lsetup,0,True);
220 CVAL(outbuf,smb_com) = trans;
221 SSVAL(outbuf,smb_tid,cnum);
222 cli_setup_pkt(outbuf);
224 outparam = smb_buf(outbuf)+(trans==SMBtrans ? namelen+1 : 3);
225 outdata = outparam+this_lparam;
227 /* primary request */
228 SSVAL(outbuf,smb_tpscnt,lparam); /* tpscnt */
229 SSVAL(outbuf,smb_tdscnt,ldata); /* tdscnt */
230 SSVAL(outbuf,smb_mprcnt,mparam); /* mprcnt */
231 SSVAL(outbuf,smb_mdrcnt,mdata); /* mdrcnt */
232 SCVAL(outbuf,smb_msrcnt,msetup); /* msrcnt */
233 SSVAL(outbuf,smb_flags,flags); /* flags */
234 SIVAL(outbuf,smb_timeout,0); /* timeout */
235 SSVAL(outbuf,smb_pscnt,this_lparam); /* pscnt */
236 SSVAL(outbuf,smb_psoff,smb_offset(outparam,outbuf)); /* psoff */
237 SSVAL(outbuf,smb_dscnt,this_ldata); /* dscnt */
238 SSVAL(outbuf,smb_dsoff,smb_offset(outdata,outbuf)); /* dsoff */
239 SCVAL(outbuf,smb_suwcnt,lsetup); /* suwcnt */
240 for (i=0;i<lsetup;i++) /* setup[] */
241 SSVAL(outbuf,smb_setup+i*SIZEOFWORD,setup[i]);
244 memcpy(p,name, namelen+1); /* name[] */
247 *p++ = 0; /* put in a null smb_name */
248 *p++ = 'D'; *p++ = ' '; /* this was added because OS/2 does it */
250 if (this_lparam) /* param[] */
251 memcpy(outparam,param,this_lparam);
252 if (this_ldata) /* data[] */
253 memcpy(outdata,data,this_ldata);
254 set_message(outbuf,14+lsetup, /* wcnt, bcc */
255 PTR_DIFF(outdata+this_ldata,smb_buf(outbuf)),False);
258 send_smb(Client,outbuf);
260 if (this_ldata < ldata || this_lparam < lparam)
262 /* receive interim response */
263 if (!client_receive_smb(Client,inbuf,SHORT_TIMEOUT) || CVAL(inbuf,smb_rcls) != 0)
265 DEBUG(0,("%s request failed (%s)\n",
266 trans==SMBtrans?"SMBtrans":"SMBtrans2", smb_errstr(inbuf)));
270 tot_data = this_ldata;
271 tot_param = this_lparam;
273 while (tot_data < ldata || tot_param < lparam)
275 this_lparam = MIN(lparam-tot_param,max_xmit - 500); /* hack */
276 this_ldata = MIN(ldata-tot_data,max_xmit - (500+this_lparam));
278 set_message(outbuf,trans==SMBtrans?8:9,0,True);
279 CVAL(outbuf,smb_com) = trans==SMBtrans ? SMBtranss : SMBtranss2;
281 outparam = smb_buf(outbuf);
282 outdata = outparam+this_lparam;
284 /* secondary request */
285 SSVAL(outbuf,smb_tpscnt,lparam); /* tpscnt */
286 SSVAL(outbuf,smb_tdscnt,ldata); /* tdscnt */
287 SSVAL(outbuf,smb_spscnt,this_lparam); /* pscnt */
288 SSVAL(outbuf,smb_spsoff,smb_offset(outparam,outbuf)); /* psoff */
289 SSVAL(outbuf,smb_spsdisp,tot_param); /* psdisp */
290 SSVAL(outbuf,smb_sdscnt,this_ldata); /* dscnt */
291 SSVAL(outbuf,smb_sdsoff,smb_offset(outdata,outbuf)); /* dsoff */
292 SSVAL(outbuf,smb_sdsdisp,tot_data); /* dsdisp */
293 if (trans==SMBtrans2)
294 SSVAL(outbuf,smb_sfid,fid); /* fid */
295 if (this_lparam) /* param[] */
296 memcpy(outparam,param,this_lparam);
297 if (this_ldata) /* data[] */
298 memcpy(outdata,data,this_ldata);
299 set_message(outbuf,trans==SMBtrans?8:9, /* wcnt, bcc */
300 PTR_DIFF(outdata+this_ldata,smb_buf(outbuf)),False);
303 send_smb(Client,outbuf);
305 tot_data += this_ldata;
306 tot_param += this_lparam;
314 /****************************************************************************
315 send a session request
316 ****************************************************************************/
317 BOOL cli_send_session_request(char *inbuf,char *outbuf)
322 /* send a session request (RFC 8002) */
324 strcpy(dest,desthost);
325 p = strchr(dest,'.');
328 /* put in the destination name */
330 name_mangle(dest,p,name_type); /* 0x20 is the SMB server NetBIOS type. */
335 name_mangle(myname,p,0);
338 /* setup the packet length */
339 _smb_setlen(outbuf,len);
340 CVAL(outbuf,0) = 0x81;
342 send_smb(Client,outbuf);
343 DEBUG(5,("Sent session request\n"));
345 client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
347 if (CVAL(inbuf,0) == 0x84) /* C. Hoch 9/14/95 Start */
349 /* For information, here is the response structure.
350 * We do the byte-twiddling to for portability.
351 struct RetargetResponse{
360 int port = (CVAL(inbuf,8)<<8)+CVAL(inbuf,9);
361 /* SESSION RETARGET */
362 putip((char *)&dest_ip,inbuf+4);
365 Client = open_socket_out(SOCK_STREAM, &dest_ip, port, LONG_CONNECT_TIMEOUT);
369 DEBUG(3,("Retargeted\n"));
371 set_socket_options(Client,user_socket_options);
374 return cli_send_session_request(inbuf,outbuf);
375 } /* C. Hoch 9/14/95 End */
378 if (CVAL(inbuf,0) != 0x82)
380 int ecode = CVAL(inbuf,4);
381 DEBUG(0,("Session request failed (%d,%d) with myname=%s destname=%s\n",
382 CVAL(inbuf,0),ecode,myname,desthost));
386 DEBUG(0,("Not listening on called name\n"));
387 DEBUG(0,("Try to connect to another name (instead of %s)\n",desthost));
388 DEBUG(0,("You may find the -I option useful for this\n"));
391 DEBUG(0,("Not listening for calling name\n"));
392 DEBUG(0,("Try to connect as another name (instead of %s)\n",myname));
393 DEBUG(0,("You may find the -n option useful for this\n"));
396 DEBUG(0,("Called name not present\n"));
397 DEBUG(0,("Try to connect to another name (instead of %s)\n",desthost));
398 DEBUG(0,("You may find the -I option useful for this\n"));
401 DEBUG(0,("Called name present, but insufficient resources\n"));
402 DEBUG(0,("Perhaps you should try again later?\n"));
405 DEBUG(0,("Unspecified error 0x%X\n",ecode));
406 DEBUG(0,("Your server software is being unfriendly\n"));
418 {PROTOCOL_CORE,"PC NETWORK PROGRAM 1.0"},
419 {PROTOCOL_COREPLUS,"MICROSOFT NETWORKS 1.03"},
420 {PROTOCOL_LANMAN1,"MICROSOFT NETWORKS 3.0"},
421 {PROTOCOL_LANMAN1,"LANMAN1.0"},
422 {PROTOCOL_LANMAN2,"LM1.2X002"},
423 {PROTOCOL_LANMAN2,"Samba"},
424 {PROTOCOL_NT1,"NT LM 0.12"},
425 {PROTOCOL_NT1,"NT LANMAN 1.0"},
430 /****************************************************************************
431 send a login command.
432 ****************************************************************************/
433 BOOL cli_send_login(char *inbuf,char *outbuf,BOOL start_session,BOOL use_setup, struct connection_options *options)
435 BOOL was_null = (!inbuf && !outbuf);
436 time_t servertime = 0;
437 extern int serverzone;
444 struct connection_options opt;
446 bzero(&opt, sizeof(opt));
450 inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
451 outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
455 if (strstr(service,"IPC$")) connect_as_ipc = True;
459 if (connect_as_printer)
465 if (start_session && !cli_send_session_request(inbuf,outbuf))
475 bzero(outbuf,smb_size);
477 /* setup the protocol strings */
481 for (plength=0,numprots=0;
482 prots[numprots].name && prots[numprots].prot<=max_protocol;
484 plength += strlen(prots[numprots].name)+2;
486 set_message(outbuf,0,plength,True);
490 prots[numprots].name && prots[numprots].prot<=max_protocol;
494 strcpy(p,prots[numprots].name);
499 CVAL(outbuf,smb_com) = SMBnegprot;
500 cli_setup_pkt(outbuf);
502 CVAL(smb_buf(outbuf),0) = 2;
504 send_smb(Client,outbuf);
505 client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
509 if (CVAL(inbuf,smb_rcls) != 0 || ((int)SVAL(inbuf,smb_vwv0) >= numprots))
511 DEBUG(0,("SMBnegprot failed. myname=%s destname=%s - %s \n",
512 myname,desthost,smb_errstr(inbuf)));
521 opt.protocol = Protocol = prots[SVAL(inbuf,smb_vwv0)].prot;
524 if (Protocol < PROTOCOL_LANMAN1) {
525 /* no extra params */
526 } else if (Protocol < PROTOCOL_NT1) {
527 opt.sec_mode = SVAL(inbuf,smb_vwv1);
528 opt.max_xmit = max_xmit = SVAL(inbuf,smb_vwv2);
529 opt.sesskey = IVAL(inbuf,smb_vwv6);
530 opt.serverzone = serverzone = SVALS(inbuf,smb_vwv10)*60;
531 /* this time is converted to GMT by make_unix_date */
532 servertime = make_unix_date(inbuf+smb_vwv8);
533 if (Protocol >= PROTOCOL_COREPLUS) {
534 opt.rawmode = SVAL(inbuf,smb_vwv5);
535 readbraw_supported = ((SVAL(inbuf,smb_vwv5) & 0x1) != 0);
536 writebraw_supported = ((SVAL(inbuf,smb_vwv5) & 0x2) != 0);
538 crypt_len = smb_buflen(inbuf);
539 memcpy(cryptkey,smb_buf(inbuf),8);
540 DEBUG(3,("max mux %d\n",SVAL(inbuf,smb_vwv3)));
541 opt.max_vcs = SVAL(inbuf,smb_vwv4);
542 DEBUG(3,("max vcs %d\n",opt.max_vcs));
543 DEBUG(3,("max blk %d\n",SVAL(inbuf,smb_vwv5)));
546 opt.sec_mode = CVAL(inbuf,smb_vwv1);
547 opt.max_xmit = max_xmit = IVAL(inbuf,smb_vwv3+1);
548 opt.sesskey = IVAL(inbuf,smb_vwv7+1);
549 opt.serverzone = SVALS(inbuf,smb_vwv15+1)*60;
550 /* this time arrives in real GMT */
551 servertime = interpret_long_date(inbuf+smb_vwv11+1);
552 crypt_len = CVAL(inbuf,smb_vwv16+1);
553 memcpy(cryptkey,smb_buf(inbuf),8);
554 if (IVAL(inbuf,smb_vwv9+1) & 1)
555 readbraw_supported = writebraw_supported = True;
556 DEBUG(3,("max mux %d\n",SVAL(inbuf,smb_vwv1+1)));
557 opt.max_vcs = SVAL(inbuf,smb_vwv2+1);
558 DEBUG(3,("max vcs %d\n",opt.max_vcs));
559 DEBUG(3,("max raw %d\n",IVAL(inbuf,smb_vwv5+1)));
560 DEBUG(3,("capabilities 0x%x\n",IVAL(inbuf,smb_vwv9+1)));
563 DEBUG(3,("Sec mode %d\n",SVAL(inbuf,smb_vwv1)));
564 DEBUG(3,("max xmt %d\n",max_xmit));
565 DEBUG(3,("Got %d byte crypt key\n",crypt_len));
566 DEBUG(3,("Chose protocol [%s]\n",prots[SVAL(inbuf,smb_vwv0)].name));
568 doencrypt = ((opt.sec_mode & 2) != 0);
571 static BOOL done_time = False;
573 DEBUG(1,("Server time is %sTimezone is UTC%+02.1f\n",
574 asctime(LocalTime(&servertime)),
575 -(double)(serverzone/3600.0)));
585 pass = (char *)getpass("Password: ");
587 pstrcpy(smb_login_passwd, pass);
589 /* use a blank username for the 2nd try with a blank password */
590 if (tries++ && !*pass)
593 if (Protocol >= PROTOCOL_LANMAN1 && use_setup)
596 int passlen = strlen(pass)+1;
599 if (doencrypt && *pass) {
600 DEBUG(3,("Using encrypted passwords\n"));
602 SMBencrypt((uchar *)pass,(uchar *)cryptkey,(uchar *)pword);
605 /* if in share level security then don't send a password now */
606 if (!(opt.sec_mode & 1)) {strcpy(pword, "");passlen=1;}
608 /* send a session setup command */
609 bzero(outbuf,smb_size);
611 if (Protocol < PROTOCOL_NT1) {
612 set_message(outbuf,10,1 + strlen(username) + passlen,True);
613 CVAL(outbuf,smb_com) = SMBsesssetupX;
614 cli_setup_pkt(outbuf);
616 CVAL(outbuf,smb_vwv0) = 0xFF;
617 SSVAL(outbuf,smb_vwv2,max_xmit);
618 SSVAL(outbuf,smb_vwv3,2);
619 SSVAL(outbuf,smb_vwv4,opt.max_vcs-1);
620 SIVAL(outbuf,smb_vwv5,opt.sesskey);
621 SSVAL(outbuf,smb_vwv7,passlen);
623 memcpy(p,pword,passlen);
627 if (!doencrypt) passlen--;
629 set_message(outbuf,13,0,True);
630 CVAL(outbuf,smb_com) = SMBsesssetupX;
631 cli_setup_pkt(outbuf);
633 CVAL(outbuf,smb_vwv0) = 0xFF;
634 SSVAL(outbuf,smb_vwv2,BUFFER_SIZE);
635 SSVAL(outbuf,smb_vwv3,2);
636 SSVAL(outbuf,smb_vwv4,getpid());
637 SIVAL(outbuf,smb_vwv5,opt.sesskey);
638 SSVAL(outbuf,smb_vwv7,passlen);
639 SSVAL(outbuf,smb_vwv8,0);
641 memcpy(p,pword,passlen); p += SVAL(outbuf,smb_vwv7);
642 strcpy(p,username);p = skip_string(p,1);
643 strcpy(p,workgroup);p = skip_string(p,1);
644 strcpy(p,"Unix");p = skip_string(p,1);
645 strcpy(p,"Samba");p = skip_string(p,1);
646 set_message(outbuf,13,PTR_DIFF(p,smb_buf(outbuf)),False);
649 send_smb(Client,outbuf);
650 client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
654 if (CVAL(inbuf,smb_rcls) != 0)
657 ((CVAL(inbuf,smb_rcls) == ERRDOS &&
658 SVAL(inbuf,smb_err) == ERRnoaccess) ||
659 (CVAL(inbuf,smb_rcls) == ERRSRV &&
660 SVAL(inbuf,smb_err) == ERRbadpw)))
663 DEBUG(3,("resending login\n"));
667 DEBUG(0,("Session setup failed for username=%s myname=%s destname=%s %s\n",
668 username,myname,desthost,smb_errstr(inbuf)));
669 DEBUG(0,("You might find the -U, -W or -n options useful\n"));
670 DEBUG(0,("Sometimes you have to use `-n USERNAME' (particularly with OS/2)\n"));
671 DEBUG(0,("Some servers also insist on uppercase-only passwords\n"));
680 if (Protocol >= PROTOCOL_NT1) {
681 char *domain,*os,*lanman;
684 lanman = skip_string(os,1);
685 domain = skip_string(lanman,1);
686 if (*domain || *os || *lanman)
687 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",domain,os,lanman));
690 /* use the returned uid from now on */
691 if (SVAL(inbuf,smb_uid) != uid)
692 DEBUG(3,("Server gave us a UID of %d. We gave %d\n",
693 SVAL(inbuf,smb_uid),uid));
694 opt.server_uid = uid = SVAL(inbuf,smb_uid);
697 if (opt.sec_mode & 1) {
698 if (SVAL(inbuf, smb_vwv2) & 1)
699 DEBUG(1,("connected as guest "));
700 DEBUG(1,("security=user\n"));
702 DEBUG(1,("security=share\n"));
705 /* now we've got a connection - send a tcon message */
706 bzero(outbuf,smb_size);
708 if (strncmp(service,"\\\\",2) != 0)
710 DEBUG(0,("\nWarning: Your service name doesn't start with \\\\. This is probably incorrect.\n"));
711 DEBUG(0,("Perhaps try replacing each \\ with \\\\ on the command line?\n\n"));
718 int passlen = strlen(pass)+1;
722 if (doencrypt && *pass) {
724 SMBencrypt((uchar *)pass,(uchar *)cryptkey,(uchar *)pword);
727 /* if in user level security then don't send a password now */
728 if ((opt.sec_mode & 1)) {
729 strcpy(pword, ""); passlen=1;
732 if (Protocol <= PROTOCOL_COREPLUS) {
733 set_message(outbuf,0,6 + strlen(service) + passlen + strlen(dev),True);
734 CVAL(outbuf,smb_com) = SMBtcon;
735 cli_setup_pkt(outbuf);
740 p = skip_string(p,1);
742 memcpy(p,pword,passlen);
748 set_message(outbuf,4,2 + strlen(service) + passlen + strlen(dev),True);
749 CVAL(outbuf,smb_com) = SMBtconX;
750 cli_setup_pkt(outbuf);
752 SSVAL(outbuf,smb_vwv0,0xFF);
753 SSVAL(outbuf,smb_vwv3,passlen);
756 memcpy(p,pword,passlen);
759 p = skip_string(p,1);
764 send_smb(Client,outbuf);
765 client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
767 /* trying again with a blank password */
768 if (CVAL(inbuf,smb_rcls) != 0 &&
769 (int)strlen(pass) > 0 &&
771 Protocol >= PROTOCOL_LANMAN1)
773 DEBUG(2,("first SMBtconX failed, trying again. %s\n",smb_errstr(inbuf)));
778 if (CVAL(inbuf,smb_rcls) != 0)
780 DEBUG(0,("SMBtconX failed. %s\n",smb_errstr(inbuf)));
781 DEBUG(0,("Perhaps you are using the wrong sharename, username or password?\n"));
782 DEBUG(0,("Some servers insist that these be in uppercase\n"));
792 if (Protocol <= PROTOCOL_COREPLUS) {
793 max_xmit = SVAL(inbuf,smb_vwv0);
795 cnum = SVAL(inbuf,smb_vwv1);
798 max_xmit = MIN(max_xmit,BUFFER_SIZE-4);
800 max_xmit = BUFFER_SIZE - 4;
802 cnum = SVAL(inbuf,smb_tid);
804 opt.max_xmit = max_xmit;
807 DEBUG(3,("Connected with cnum=%d max_xmit=%d\n",cnum,max_xmit));
824 /****************************************************************************
825 send a logout command
826 ****************************************************************************/
827 void cli_send_logout(void )
829 pstring inbuf,outbuf;
831 DEBUG(5,("cli_send_logout\n"));
833 bzero(outbuf,smb_size);
834 set_message(outbuf,0,0,True);
835 CVAL(outbuf,smb_com) = SMBtdis;
836 SSVAL(outbuf,smb_tid,cnum);
837 cli_setup_pkt(outbuf);
839 send_smb(Client,outbuf);
840 client_receive_smb(Client,inbuf,SHORT_TIMEOUT);
842 if (CVAL(inbuf,smb_rcls) != 0)
844 DEBUG(0,("SMBtdis failed %s\n",smb_errstr(inbuf)));
855 /****************************************************************************
856 open the client sockets
857 ****************************************************************************/
858 BOOL cli_open_sockets(int port )
860 static int last_port;
866 if (port == 0) port=last_port;
877 strcpy(service2,service);
878 host = strtok(service2,"\\/");
880 DEBUG(0,("Badly formed host name\n"));
883 strcpy(desthost,host);
887 get_myname(myname,NULL);
891 DEBUG(3,("Opening sockets\n"));
897 if ((hp = Get_Hostbyname(host)))
899 putip((char *)&dest_ip,(char *)hp->h_addr);
905 /* Try and resolve the name with the netbios server */
907 struct in_addr *ip_list;
909 if ((bcast = open_socket_in(SOCK_DGRAM, 0, 3,
910 interpret_addr(lp_socket_address()))) != -1) {
911 set_socket_options(bcast, "SO_BROADCAST");
913 if ((ip_list = name_query(bcast, host, name_type, True, True, *iface_bcast(dest_ip),
914 &count,0)) != NULL) {
915 dest_ip = ip_list[0];
923 DEBUG(0,("Get_Hostbyname: Unknown host %s.\n",host));
929 Client = open_socket_out(SOCK_STREAM, &dest_ip, port, LONG_CONNECT_TIMEOUT);
933 DEBUG(3,("Connected\n"));
935 set_socket_options(Client,user_socket_options);
940 /****************************************************************************
941 close and open the connection again
942 ****************************************************************************/
943 BOOL cli_reopen_connection(char *inbuf,char *outbuf)
945 static int open_count=0;
949 if (open_count>5) return(False);
951 DEBUG(1,("Trying to re-open connection\n"));
953 set_message(outbuf,0,0,True);
954 SCVAL(outbuf,smb_com,SMBtdis);
955 SSVAL(outbuf,smb_tid,cnum);
956 cli_setup_pkt(outbuf);
958 send_smb(Client,outbuf);
959 client_receive_smb(Client,inbuf,SHORT_TIMEOUT);
962 if (!cli_open_sockets(0)) return(False);
964 return(cli_send_login(inbuf,outbuf,True,True,NULL));