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;
43 BOOL connect_as_printer = False;
44 BOOL connect_as_ipc = False;
49 extern pstring user_socket_options;
51 /* 30 second timeout on most commands */
52 #define CLIENT_TIMEOUT (30*1000)
53 #define SHORT_TIMEOUT (5*1000)
57 int max_protocol = PROTOCOL_NT1;
59 BOOL readbraw_supported = False;
60 BOOL writebraw_supported = False;
62 extern int DEBUGLEVEL;
70 int max_xmit = BUFFER_SIZE;
74 extern struct in_addr dest_ip;
81 /****************************************************************************
82 setup basics in a outgoing packet
83 ****************************************************************************/
84 void cli_setup_pkt(char *outbuf)
86 SSVAL(outbuf,smb_pid,pid);
87 SSVAL(outbuf,smb_uid,uid);
88 SSVAL(outbuf,smb_mid,mid);
89 if (Protocol > PROTOCOL_COREPLUS)
91 SCVAL(outbuf,smb_flg,0x8);
92 SSVAL(outbuf,smb_flg2,0x1);
96 /****************************************************************************
98 ****************************************************************************/
99 BOOL cli_call_api(char *pipe_name, int pipe_name_len,
100 int prcnt,int drcnt, int srcnt,
101 int mprcnt,int mdrcnt,
102 int *rprcnt,int *rdrcnt,
103 char *param,char *data, uint16 *setup,
104 char **rparam,char **rdata)
106 static char *inbuf=NULL;
107 static char *outbuf=NULL;
109 if (!inbuf) inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
110 if (!outbuf) outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
112 if (pipe_name_len == 0) pipe_name_len = strlen(pipe_name);
114 cli_send_trans_request(outbuf,SMBtrans,pipe_name, pipe_name_len, 0,0,
119 return (cli_receive_trans_response(inbuf,SMBtrans,
125 /****************************************************************************
126 receive a SMB trans or trans2 response allocating the necessary memory
127 ****************************************************************************/
128 BOOL cli_receive_trans_response(char *inbuf,int trans,
129 int *data_len,int *param_len,
130 char **data,char **param)
134 int this_data,this_param;
136 *data_len = *param_len = 0;
138 client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
142 if (CVAL(inbuf,smb_com) != trans)
144 DEBUG(0,("Expected %s response, got command 0x%02x\n",
145 trans==SMBtrans?"SMBtrans":"SMBtrans2", CVAL(inbuf,smb_com)));
148 if (CVAL(inbuf,smb_rcls) != 0)
151 /* parse out the lengths */
152 total_data = SVAL(inbuf,smb_tdrcnt);
153 total_param = SVAL(inbuf,smb_tprcnt);
156 *data = Realloc(*data,total_data);
157 *param = Realloc(*param,total_param);
161 this_data = SVAL(inbuf,smb_drcnt);
162 this_param = SVAL(inbuf,smb_prcnt);
164 memcpy(*data + SVAL(inbuf,smb_drdisp),
165 smb_base(inbuf) + SVAL(inbuf,smb_droff),
168 memcpy(*param + SVAL(inbuf,smb_prdisp),
169 smb_base(inbuf) + SVAL(inbuf,smb_proff),
171 *data_len += this_data;
172 *param_len += this_param;
174 /* parse out the total lengths again - they can shrink! */
175 total_data = SVAL(inbuf,smb_tdrcnt);
176 total_param = SVAL(inbuf,smb_tprcnt);
178 if (total_data <= *data_len && total_param <= *param_len)
181 client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
185 if (CVAL(inbuf,smb_com) != trans)
187 DEBUG(0,("Expected %s response, got command 0x%02x\n",
188 trans==SMBtrans?"SMBtrans":"SMBtrans2", CVAL(inbuf,smb_com)));
191 if (CVAL(inbuf,smb_rcls) != 0)
200 /****************************************************************************
201 send a SMB trans or trans2 request
202 ****************************************************************************/
203 BOOL cli_send_trans_request(char *outbuf,int trans,
204 char *name,int namelen, int fid,int flags,
205 char *data,char *param,uint16 *setup,
206 int ldata,int lparam,int lsetup,
207 int mdata,int mparam,int msetup)
210 int this_ldata,this_lparam;
211 int tot_data=0,tot_param=0;
212 char *outdata,*outparam;
216 this_lparam = MIN(lparam,max_xmit - (500+lsetup*SIZEOFWORD)); /* hack */
217 this_ldata = MIN(ldata,max_xmit - (500+lsetup*SIZEOFWORD+this_lparam));
219 bzero(outbuf,smb_size);
220 set_message(outbuf,14+lsetup,0,True);
221 CVAL(outbuf,smb_com) = trans;
222 SSVAL(outbuf,smb_tid,cnum);
223 cli_setup_pkt(outbuf);
225 outparam = smb_buf(outbuf)+(trans==SMBtrans ? namelen+1 : 3);
226 outdata = outparam+this_lparam;
228 /* primary request */
229 SSVAL(outbuf,smb_tpscnt,lparam); /* tpscnt */
230 SSVAL(outbuf,smb_tdscnt,ldata); /* tdscnt */
231 SSVAL(outbuf,smb_mprcnt,mparam); /* mprcnt */
232 SSVAL(outbuf,smb_mdrcnt,mdata); /* mdrcnt */
233 SCVAL(outbuf,smb_msrcnt,msetup); /* msrcnt */
234 SSVAL(outbuf,smb_flags,flags); /* flags */
235 SIVAL(outbuf,smb_timeout,0); /* timeout */
236 SSVAL(outbuf,smb_pscnt,this_lparam); /* pscnt */
237 SSVAL(outbuf,smb_psoff,smb_offset(outparam,outbuf)); /* psoff */
238 SSVAL(outbuf,smb_dscnt,this_ldata); /* dscnt */
239 SSVAL(outbuf,smb_dsoff,smb_offset(outdata,outbuf)); /* dsoff */
240 SCVAL(outbuf,smb_suwcnt,lsetup); /* suwcnt */
241 for (i=0;i<lsetup;i++) /* setup[] */
242 SSVAL(outbuf,smb_setup+i*SIZEOFWORD,setup[i]);
245 memcpy(p,name, namelen+1); /* name[] */
248 *p++ = 0; /* put in a null smb_name */
249 *p++ = 'D'; *p++ = ' '; /* this was added because OS/2 does it */
251 if (this_lparam) /* param[] */
252 memcpy(outparam,param,this_lparam);
253 if (this_ldata) /* data[] */
254 memcpy(outdata,data,this_ldata);
255 set_message(outbuf,14+lsetup, /* wcnt, bcc */
256 PTR_DIFF(outdata+this_ldata,smb_buf(outbuf)),False);
259 send_smb(Client,outbuf);
261 if (this_ldata < ldata || this_lparam < lparam)
263 /* receive interim response */
264 if (!client_receive_smb(Client,inbuf,SHORT_TIMEOUT) || CVAL(inbuf,smb_rcls) != 0)
266 DEBUG(0,("%s request failed (%s)\n",
267 trans==SMBtrans?"SMBtrans":"SMBtrans2", smb_errstr(inbuf)));
271 tot_data = this_ldata;
272 tot_param = this_lparam;
274 while (tot_data < ldata || tot_param < lparam)
276 this_lparam = MIN(lparam-tot_param,max_xmit - 500); /* hack */
277 this_ldata = MIN(ldata-tot_data,max_xmit - (500+this_lparam));
279 set_message(outbuf,trans==SMBtrans?8:9,0,True);
280 CVAL(outbuf,smb_com) = trans==SMBtrans ? SMBtranss : SMBtranss2;
282 outparam = smb_buf(outbuf);
283 outdata = outparam+this_lparam;
285 /* secondary request */
286 SSVAL(outbuf,smb_tpscnt,lparam); /* tpscnt */
287 SSVAL(outbuf,smb_tdscnt,ldata); /* tdscnt */
288 SSVAL(outbuf,smb_spscnt,this_lparam); /* pscnt */
289 SSVAL(outbuf,smb_spsoff,smb_offset(outparam,outbuf)); /* psoff */
290 SSVAL(outbuf,smb_spsdisp,tot_param); /* psdisp */
291 SSVAL(outbuf,smb_sdscnt,this_ldata); /* dscnt */
292 SSVAL(outbuf,smb_sdsoff,smb_offset(outdata,outbuf)); /* dsoff */
293 SSVAL(outbuf,smb_sdsdisp,tot_data); /* dsdisp */
294 if (trans==SMBtrans2)
295 SSVAL(outbuf,smb_sfid,fid); /* fid */
296 if (this_lparam) /* param[] */
297 memcpy(outparam,param,this_lparam);
298 if (this_ldata) /* data[] */
299 memcpy(outdata,data,this_ldata);
300 set_message(outbuf,trans==SMBtrans?8:9, /* wcnt, bcc */
301 PTR_DIFF(outdata+this_ldata,smb_buf(outbuf)),False);
304 send_smb(Client,outbuf);
306 tot_data += this_ldata;
307 tot_param += this_lparam;
315 /****************************************************************************
316 send a session request
317 ****************************************************************************/
318 BOOL cli_send_session_request(char *inbuf,char *outbuf)
323 /* send a session request (RFC 8002) */
325 strcpy(dest,desthost);
326 p = strchr(dest,'.');
329 /* put in the destination name */
331 name_mangle(dest,p,name_type); /* 0x20 is the SMB server NetBIOS type. */
336 name_mangle(myname,p,0);
339 /* setup the packet length */
340 _smb_setlen(outbuf,len);
341 CVAL(outbuf,0) = 0x81;
343 send_smb(Client,outbuf);
344 DEBUG(5,("Sent session request\n"));
346 client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
348 if (CVAL(inbuf,0) == 0x84) /* C. Hoch 9/14/95 Start */
350 /* For information, here is the response structure.
351 * We do the byte-twiddling to for portability.
352 struct RetargetResponse{
361 int port = (CVAL(inbuf,8)<<8)+CVAL(inbuf,9);
362 /* SESSION RETARGET */
363 putip((char *)&dest_ip,inbuf+4);
366 Client = open_socket_out(SOCK_STREAM, &dest_ip, port, LONG_CONNECT_TIMEOUT);
370 DEBUG(3,("Retargeted\n"));
372 set_socket_options(Client,user_socket_options);
375 return cli_send_session_request(inbuf,outbuf);
376 } /* C. Hoch 9/14/95 End */
379 if (CVAL(inbuf,0) != 0x82)
381 int ecode = CVAL(inbuf,4);
382 DEBUG(0,("Session request failed (%d,%d) with myname=%s destname=%s\n",
383 CVAL(inbuf,0),ecode,myname,desthost));
387 DEBUG(0,("Not listening on called name\n"));
388 DEBUG(0,("Try to connect to another name (instead of %s)\n",desthost));
389 DEBUG(0,("You may find the -I option useful for this\n"));
392 DEBUG(0,("Not listening for calling name\n"));
393 DEBUG(0,("Try to connect as another name (instead of %s)\n",myname));
394 DEBUG(0,("You may find the -n option useful for this\n"));
397 DEBUG(0,("Called name not present\n"));
398 DEBUG(0,("Try to connect to another name (instead of %s)\n",desthost));
399 DEBUG(0,("You may find the -I option useful for this\n"));
402 DEBUG(0,("Called name present, but insufficient resources\n"));
403 DEBUG(0,("Perhaps you should try again later?\n"));
406 DEBUG(0,("Unspecified error 0x%X\n",ecode));
407 DEBUG(0,("Your server software is being unfriendly\n"));
419 {PROTOCOL_CORE,"PC NETWORK PROGRAM 1.0"},
420 {PROTOCOL_COREPLUS,"MICROSOFT NETWORKS 1.03"},
421 {PROTOCOL_LANMAN1,"MICROSOFT NETWORKS 3.0"},
422 {PROTOCOL_LANMAN1,"LANMAN1.0"},
423 {PROTOCOL_LANMAN2,"LM1.2X002"},
424 {PROTOCOL_LANMAN2,"Samba"},
425 {PROTOCOL_NT1,"NT LM 0.12"},
426 {PROTOCOL_NT1,"NT LANMAN 1.0"},
431 /****************************************************************************
432 send a login command.
433 ****************************************************************************/
434 BOOL cli_send_login(char *inbuf,char *outbuf,BOOL start_session,BOOL use_setup, struct connection_options *options)
436 BOOL was_null = (!inbuf && !outbuf);
437 time_t servertime = 0;
438 extern int serverzone;
445 struct connection_options opt;
447 bzero(&opt, sizeof(opt));
451 inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
452 outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
456 if (strstr(service,"IPC$")) connect_as_ipc = True;
460 if (connect_as_printer)
466 if (start_session && !cli_send_session_request(inbuf,outbuf))
476 bzero(outbuf,smb_size);
478 /* setup the protocol strings */
482 for (plength=0,numprots=0;
483 prots[numprots].name && prots[numprots].prot<=max_protocol;
485 plength += strlen(prots[numprots].name)+2;
487 set_message(outbuf,0,plength,True);
491 prots[numprots].name && prots[numprots].prot<=max_protocol;
495 strcpy(p,prots[numprots].name);
500 CVAL(outbuf,smb_com) = SMBnegprot;
501 cli_setup_pkt(outbuf);
503 CVAL(smb_buf(outbuf),0) = 2;
505 send_smb(Client,outbuf);
506 client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
510 if (CVAL(inbuf,smb_rcls) != 0 || ((int)SVAL(inbuf,smb_vwv0) >= numprots))
512 DEBUG(0,("SMBnegprot failed. myname=%s destname=%s - %s \n",
513 myname,desthost,smb_errstr(inbuf)));
522 opt.protocol = Protocol = prots[SVAL(inbuf,smb_vwv0)].prot;
525 if (Protocol < PROTOCOL_LANMAN1) {
526 /* no extra params */
527 } else if (Protocol < PROTOCOL_NT1) {
528 opt.sec_mode = SVAL(inbuf,smb_vwv1);
529 opt.max_xmit = max_xmit = SVAL(inbuf,smb_vwv2);
530 opt.sesskey = IVAL(inbuf,smb_vwv6);
531 opt.serverzone = serverzone = SVALS(inbuf,smb_vwv10)*60;
532 /* this time is converted to GMT by make_unix_date */
533 servertime = make_unix_date(inbuf+smb_vwv8);
534 if (Protocol >= PROTOCOL_COREPLUS) {
535 opt.rawmode = SVAL(inbuf,smb_vwv5);
536 readbraw_supported = ((SVAL(inbuf,smb_vwv5) & 0x1) != 0);
537 writebraw_supported = ((SVAL(inbuf,smb_vwv5) & 0x2) != 0);
539 crypt_len = smb_buflen(inbuf);
540 memcpy(cryptkey,smb_buf(inbuf),8);
541 DEBUG(3,("max mux %d\n",SVAL(inbuf,smb_vwv3)));
542 opt.max_vcs = SVAL(inbuf,smb_vwv4);
543 DEBUG(3,("max vcs %d\n",opt.max_vcs));
544 DEBUG(3,("max blk %d\n",SVAL(inbuf,smb_vwv5)));
547 opt.sec_mode = CVAL(inbuf,smb_vwv1);
548 opt.max_xmit = max_xmit = IVAL(inbuf,smb_vwv3+1);
549 opt.sesskey = IVAL(inbuf,smb_vwv7+1);
550 opt.serverzone = SVALS(inbuf,smb_vwv15+1)*60;
551 /* this time arrives in real GMT */
552 servertime = interpret_long_date(inbuf+smb_vwv11+1);
553 crypt_len = CVAL(inbuf,smb_vwv16+1);
554 memcpy(cryptkey,smb_buf(inbuf),8);
555 if (IVAL(inbuf,smb_vwv9+1) & 1)
556 readbraw_supported = writebraw_supported = True;
557 DEBUG(3,("max mux %d\n",SVAL(inbuf,smb_vwv1+1)));
558 opt.max_vcs = SVAL(inbuf,smb_vwv2+1);
559 DEBUG(3,("max vcs %d\n",opt.max_vcs));
560 DEBUG(3,("max raw %d\n",IVAL(inbuf,smb_vwv5+1)));
561 DEBUG(3,("capabilities 0x%x\n",IVAL(inbuf,smb_vwv9+1)));
564 DEBUG(3,("Sec mode %d\n",SVAL(inbuf,smb_vwv1)));
565 DEBUG(3,("max xmt %d\n",max_xmit));
566 DEBUG(3,("Got %d byte crypt key\n",crypt_len));
567 DEBUG(3,("Chose protocol [%s]\n",prots[SVAL(inbuf,smb_vwv0)].name));
569 doencrypt = ((opt.sec_mode & 2) != 0);
572 static BOOL done_time = False;
574 DEBUG(1,("Server time is %sTimezone is UTC%+02.1f\n",
575 asctime(LocalTime(&servertime)),
576 -(double)(serverzone/3600.0)));
586 pass = (char *)getpass("Password: ");
588 pstrcpy(smb_login_passwd, pass);
590 /* use a blank username for the 2nd try with a blank password */
591 if (tries++ && !*pass)
594 if (Protocol >= PROTOCOL_LANMAN1 && use_setup)
597 int passlen = strlen(pass)+1;
600 if (doencrypt && *pass) {
601 DEBUG(3,("Using encrypted passwords\n"));
603 SMBencrypt((uchar *)pass,(uchar *)cryptkey,(uchar *)pword);
606 /* if in share level security then don't send a password now */
607 if (!(opt.sec_mode & 1)) {strcpy(pword, "");passlen=1;}
609 /* send a session setup command */
610 bzero(outbuf,smb_size);
612 if (Protocol < PROTOCOL_NT1) {
613 set_message(outbuf,10,1 + strlen(username) + passlen,True);
614 CVAL(outbuf,smb_com) = SMBsesssetupX;
615 cli_setup_pkt(outbuf);
617 CVAL(outbuf,smb_vwv0) = 0xFF;
618 SSVAL(outbuf,smb_vwv2,max_xmit);
619 SSVAL(outbuf,smb_vwv3,2);
620 SSVAL(outbuf,smb_vwv4,opt.max_vcs-1);
621 SIVAL(outbuf,smb_vwv5,opt.sesskey);
622 SSVAL(outbuf,smb_vwv7,passlen);
624 memcpy(p,pword,passlen);
628 if (!doencrypt) passlen--;
630 set_message(outbuf,13,0,True);
631 CVAL(outbuf,smb_com) = SMBsesssetupX;
632 cli_setup_pkt(outbuf);
634 CVAL(outbuf,smb_vwv0) = 0xFF;
635 SSVAL(outbuf,smb_vwv2,BUFFER_SIZE);
636 SSVAL(outbuf,smb_vwv3,2);
637 SSVAL(outbuf,smb_vwv4,getpid());
638 SIVAL(outbuf,smb_vwv5,opt.sesskey);
639 SSVAL(outbuf,smb_vwv7,passlen);
640 SSVAL(outbuf,smb_vwv8,0);
642 memcpy(p,pword,passlen); p += SVAL(outbuf,smb_vwv7);
643 strcpy(p,username);p = skip_string(p,1);
644 strcpy(p,workgroup);p = skip_string(p,1);
645 strcpy(p,"Unix");p = skip_string(p,1);
646 strcpy(p,"Samba");p = skip_string(p,1);
647 set_message(outbuf,13,PTR_DIFF(p,smb_buf(outbuf)),False);
650 send_smb(Client,outbuf);
651 client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
655 if (CVAL(inbuf,smb_rcls) != 0)
658 ((CVAL(inbuf,smb_rcls) == ERRDOS &&
659 SVAL(inbuf,smb_err) == ERRnoaccess) ||
660 (CVAL(inbuf,smb_rcls) == ERRSRV &&
661 SVAL(inbuf,smb_err) == ERRbadpw)))
664 DEBUG(3,("resending login\n"));
669 DEBUG(0,("Session setup failed for username=%s myname=%s destname=%s %s\n",
670 username,myname,desthost,smb_errstr(inbuf)));
671 DEBUG(0,("You might find the -U, -W or -n options useful\n"));
672 DEBUG(0,("Sometimes you have to use `-n USERNAME' (particularly with OS/2)\n"));
673 DEBUG(0,("Some servers also insist on uppercase-only passwords\n"));
682 if (Protocol >= PROTOCOL_NT1) {
683 char *domain,*os,*lanman;
686 lanman = skip_string(os,1);
687 domain = skip_string(lanman,1);
688 if (*domain || *os || *lanman)
689 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",domain,os,lanman));
692 /* use the returned uid from now on */
693 if (SVAL(inbuf,smb_uid) != uid)
694 DEBUG(3,("Server gave us a UID of %d. We gave %d\n",
695 SVAL(inbuf,smb_uid),uid));
696 opt.server_uid = uid = SVAL(inbuf,smb_uid);
699 if (opt.sec_mode & 1) {
700 if (SVAL(inbuf, smb_vwv2) & 1)
701 DEBUG(1,("connected as guest "));
702 DEBUG(1,("security=user\n"));
704 DEBUG(1,("security=share\n"));
707 /* now we've got a connection - send a tcon message */
708 bzero(outbuf,smb_size);
710 if (strncmp(service,"\\\\",2) != 0)
712 DEBUG(0,("\nWarning: Your service name doesn't start with \\\\. This is probably incorrect.\n"));
713 DEBUG(0,("Perhaps try replacing each \\ with \\\\ on the command line?\n\n"));
720 int passlen = strlen(pass)+1;
724 if (doencrypt && *pass) {
726 SMBencrypt((uchar *)pass,(uchar *)cryptkey,(uchar *)pword);
729 /* if in user level security then don't send a password now */
730 if ((opt.sec_mode & 1)) {
731 strcpy(pword, ""); passlen=1;
734 if (Protocol <= PROTOCOL_COREPLUS) {
735 set_message(outbuf,0,6 + strlen(service) + passlen + strlen(dev),True);
736 CVAL(outbuf,smb_com) = SMBtcon;
737 cli_setup_pkt(outbuf);
742 p = skip_string(p,1);
744 memcpy(p,pword,passlen);
750 set_message(outbuf,4,2 + strlen(service) + passlen + strlen(dev),True);
751 CVAL(outbuf,smb_com) = SMBtconX;
752 cli_setup_pkt(outbuf);
754 SSVAL(outbuf,smb_vwv0,0xFF);
755 SSVAL(outbuf,smb_vwv3,passlen);
758 memcpy(p,pword,passlen);
761 p = skip_string(p,1);
766 send_smb(Client,outbuf);
767 client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
769 /* trying again with a blank password */
770 if (CVAL(inbuf,smb_rcls) != 0 &&
771 (int)strlen(pass) > 0 &&
773 Protocol >= PROTOCOL_LANMAN1)
775 DEBUG(2,("first SMBtconX failed, trying again. %s\n",smb_errstr(inbuf)));
780 if (CVAL(inbuf,smb_rcls) != 0)
782 DEBUG(0,("SMBtconX failed. %s\n",smb_errstr(inbuf)));
783 DEBUG(0,("Perhaps you are using the wrong sharename, username or password?\n"));
784 DEBUG(0,("Some servers insist that these be in uppercase\n"));
794 if (Protocol <= PROTOCOL_COREPLUS) {
795 max_xmit = SVAL(inbuf,smb_vwv0);
797 cnum = SVAL(inbuf,smb_vwv1);
800 max_xmit = MIN(max_xmit,BUFFER_SIZE-4);
802 max_xmit = BUFFER_SIZE - 4;
804 cnum = SVAL(inbuf,smb_tid);
806 opt.max_xmit = max_xmit;
809 DEBUG(3,("Connected with cnum=%d max_xmit=%d\n",cnum,max_xmit));
826 /****************************************************************************
827 send a logout command
828 ****************************************************************************/
829 void cli_send_logout(void )
831 pstring inbuf,outbuf;
833 DEBUG(5,("cli_send_logout\n"));
835 bzero(outbuf,smb_size);
836 set_message(outbuf,0,0,True);
837 CVAL(outbuf,smb_com) = SMBtdis;
838 SSVAL(outbuf,smb_tid,cnum);
839 cli_setup_pkt(outbuf);
841 send_smb(Client,outbuf);
842 client_receive_smb(Client,inbuf,SHORT_TIMEOUT);
844 if (CVAL(inbuf,smb_rcls) != 0)
846 DEBUG(0,("SMBtdis failed %s\n",smb_errstr(inbuf)));
857 /****************************************************************************
858 open the client sockets
859 ****************************************************************************/
860 BOOL cli_open_sockets(int port )
862 static int last_port;
868 if (port == 0) port=last_port;
879 strcpy(service2,service);
880 host = strtok(service2,"\\/");
882 DEBUG(0,("Badly formed host name\n"));
885 strcpy(desthost,host);
889 get_myname(myname,NULL);
893 DEBUG(3,("Opening sockets\n"));
899 if ((hp = Get_Hostbyname(host)))
901 putip((char *)&dest_ip,(char *)hp->h_addr);
907 /* Try and resolve the name with the netbios server */
909 struct in_addr *ip_list;
911 if ((bcast = open_socket_in(SOCK_DGRAM, 0, 3,
912 interpret_addr(lp_socket_address()))) != -1) {
913 set_socket_options(bcast, "SO_BROADCAST");
915 if ((ip_list = name_query(bcast, host, name_type, True, True, *iface_bcast(dest_ip),
916 &count,0)) != NULL) {
917 dest_ip = ip_list[0];
925 DEBUG(0,("Get_Hostbyname: Unknown host %s.\n",host));
931 Client = open_socket_out(SOCK_STREAM, &dest_ip, port, LONG_CONNECT_TIMEOUT);
935 DEBUG(3,("Connected\n"));
937 set_socket_options(Client,user_socket_options);
942 /****************************************************************************
943 close and open the connection again
944 ****************************************************************************/
945 BOOL cli_reopen_connection(char *inbuf,char *outbuf)
947 static int open_count=0;
951 if (open_count>5) return(False);
953 DEBUG(1,("Trying to re-open connection\n"));
955 set_message(outbuf,0,0,True);
956 SCVAL(outbuf,smb_com,SMBtdis);
957 SSVAL(outbuf,smb_tid,cnum);
958 cli_setup_pkt(outbuf);
960 send_smb(Client,outbuf);
961 client_receive_smb(Client,inbuf,SHORT_TIMEOUT);
964 if (!cli_open_sockets(0)) return(False);
966 return(cli_send_login(inbuf,outbuf,True,True,NULL));