This is the ubiqx binary tree and linked list library.
[Samba.git] / source / client / clientutil.c
blob01b4a093214fd150b275ddcb8bbb82aebb457095
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 SMB client
5 Copyright (C) Andrew Tridgell 1994-1997
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.
22 #ifdef SYSLOG
23 #undef SYSLOG
24 #endif
26 #include "includes.h"
28 #ifndef REGISTER
29 #define REGISTER 0
30 #endif
32 pstring service="";
33 pstring desthost="";
34 extern pstring myname;
35 pstring password = "";
36 pstring username="";
37 pstring workgroup=WORKGROUP;
38 BOOL got_pass = False;
39 BOOL connect_as_printer = False;
40 BOOL connect_as_ipc = False;
42 char cryptkey[8];
43 BOOL doencrypt=False;
45 extern pstring user_socket_options;
47 /* 30 second timeout on most commands */
48 #define CLIENT_TIMEOUT (30*1000)
49 #define SHORT_TIMEOUT (5*1000)
51 int name_type = 0x20;
53 int max_protocol = PROTOCOL_NT1;
55 BOOL readbraw_supported = False;
56 BOOL writebraw_supported = False;
58 extern int DEBUGLEVEL;
60 int cnum = 0;
61 int pid = 0;
62 int gid = 0;
63 int uid = 0;
64 int mid = 0;
66 int max_xmit = BUFFER_SIZE;
68 BOOL have_ip = False;
70 struct in_addr dest_ip;
72 extern int Protocol;
74 extern int Client;
77 /****************************************************************************
78 setup basics in a outgoing packet
79 ****************************************************************************/
80 void cli_setup_pkt(char *outbuf)
82 SSVAL(outbuf,smb_pid,pid);
83 SSVAL(outbuf,smb_uid,uid);
84 SSVAL(outbuf,smb_mid,mid);
85 if (Protocol > PROTOCOL_CORE)
87 SCVAL(outbuf,smb_flg,0x8);
88 SSVAL(outbuf,smb_flg2,0x1);
92 /****************************************************************************
93 receive a SMB trans or trans2 response allocating the necessary memory
94 ****************************************************************************/
95 BOOL cli_receive_trans_response(char *inbuf,int trans,int *data_len,
96 int *param_len, char **data,char **param)
98 int total_data=0;
99 int total_param=0;
100 int this_data,this_param;
102 *data_len = *param_len = 0;
104 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
105 show_msg(inbuf);
107 /* sanity check */
108 if (CVAL(inbuf,smb_com) != trans)
110 DEBUG(0,("Expected %s response, got command 0x%02x\n",
111 trans==SMBtrans?"SMBtrans":"SMBtrans2", CVAL(inbuf,smb_com)));
112 return(False);
114 if (CVAL(inbuf,smb_rcls) != 0)
115 return(False);
117 /* parse out the lengths */
118 total_data = SVAL(inbuf,smb_tdrcnt);
119 total_param = SVAL(inbuf,smb_tprcnt);
121 /* allocate it */
122 *data = Realloc(*data,total_data);
123 *param = Realloc(*param,total_param);
125 while (1)
127 this_data = SVAL(inbuf,smb_drcnt);
128 this_param = SVAL(inbuf,smb_prcnt);
130 if (this_data + *data_len > total_data ||
131 this_param + *param_len > total_param) {
132 DEBUG(1,("Data overflow in cli_receive_trans_response\n"));
133 return False;
136 if (this_data)
137 memcpy(*data + SVAL(inbuf,smb_drdisp),
138 smb_base(inbuf) + SVAL(inbuf,smb_droff),
139 this_data);
140 if (this_param)
141 memcpy(*param + SVAL(inbuf,smb_prdisp),
142 smb_base(inbuf) + SVAL(inbuf,smb_proff),
143 this_param);
144 *data_len += this_data;
145 *param_len += this_param;
147 /* parse out the total lengths again - they can shrink! */
148 total_data = SVAL(inbuf,smb_tdrcnt);
149 total_param = SVAL(inbuf,smb_tprcnt);
151 if (total_data <= *data_len && total_param <= *param_len)
152 break;
154 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
155 show_msg(inbuf);
157 /* sanity check */
158 if (CVAL(inbuf,smb_com) != trans)
160 DEBUG(0,("Expected %s response, got command 0x%02x\n",
161 trans==SMBtrans?"SMBtrans":"SMBtrans2", CVAL(inbuf,smb_com)));
162 return(False);
164 if (CVAL(inbuf,smb_rcls) != 0)
165 return(False);
168 return(True);
171 /****************************************************************************
172 send a session request
173 ****************************************************************************/
174 BOOL cli_send_session_request(char *inbuf, char *outbuf)
176 fstring dest;
177 char *p;
178 int len = 4;
179 /* send a session request (RFC 8002) */
181 fstrcpy(dest,desthost);
183 p = strchr(dest,'.');
184 if (p) *p = 0;
186 /* put in the destination name */
187 p = outbuf+len;
188 name_mangle(dest,p,name_type);
189 len += name_len(p);
191 /* and my name */
192 p = outbuf+len;
193 name_mangle(myname,p,0);
194 len += name_len(p);
196 /* setup the packet length */
197 _smb_setlen(outbuf,len);
198 CVAL(outbuf,0) = 0x81;
200 send_smb(Client,outbuf);
201 DEBUG(5,("Sent session request\n"));
203 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
205 if (CVAL(inbuf,0) == 0x84) /* C. Hoch 9/14/95 Start */
207 /* For information, here is the response structure.
208 * We do the byte-twiddling to for portability.
209 struct RetargetResponse{
210 unsigned char type;
211 unsigned char flags;
212 int16 length;
213 int32 ip_addr;
214 int16 port;
217 extern int Client;
218 int port = (CVAL(inbuf,8)<<8)+CVAL(inbuf,9);
219 /* SESSION RETARGET */
220 putip((char *)&dest_ip,inbuf+4);
222 close_sockets();
223 Client = open_socket_out(SOCK_STREAM, &dest_ip, port, SHORT_CONNECT_TIMEOUT);
224 if (Client == -1)
225 return False;
227 DEBUG(5,("Retargeted\n"));
229 set_socket_options(Client,user_socket_options);
231 /* Try again */
232 return cli_send_session_request(inbuf,outbuf);
233 } /* C. Hoch 9/14/95 End */
236 if (CVAL(inbuf,0) != 0x82)
238 int ecode = CVAL(inbuf,4);
239 DEBUG(0,("Session request failed (%d,%d) with myname=%s destname=%s\n",
240 CVAL(inbuf,0),ecode,myname,desthost));
241 switch (ecode)
243 case 0x80:
244 DEBUG(0,("Not listening on called name\n"));
245 DEBUG(0,("Try to connect to another name (instead of %s)\n",desthost));
246 DEBUG(0,("You may find the -I option useful for this\n"));
247 break;
248 case 0x81:
249 DEBUG(0,("Not listening for calling name\n"));
250 DEBUG(0,("Try to connect as another name (instead of %s)\n",myname));
251 DEBUG(0,("You may find the -n option useful for this\n"));
252 break;
253 case 0x82:
254 DEBUG(0,("Called name not present\n"));
255 DEBUG(0,("Try to connect to another name (instead of %s)\n",desthost));
256 DEBUG(0,("You may find the -I option useful for this\n"));
257 break;
258 case 0x83:
259 DEBUG(0,("Called name present, but insufficient resources\n"));
260 DEBUG(0,("Perhaps you should try again later?\n"));
261 break;
262 default:
263 DEBUG(0,("Unspecified error 0x%X\n",ecode));
264 DEBUG(0,("Your server software is being unfriendly\n"));
265 break;
267 return(False);
269 return(True);
273 static struct {
274 int prot;
275 char *name;
277 prots[] =
279 {PROTOCOL_CORE,"PC NETWORK PROGRAM 1.0"},
280 {PROTOCOL_COREPLUS,"MICROSOFT NETWORKS 1.03"},
281 {PROTOCOL_LANMAN1,"MICROSOFT NETWORKS 3.0"},
282 {PROTOCOL_LANMAN1,"LANMAN1.0"},
283 {PROTOCOL_LANMAN2,"LM1.2X002"},
284 {PROTOCOL_LANMAN2,"Samba"},
285 {PROTOCOL_NT1,"NT LM 0.12"},
286 {PROTOCOL_NT1,"NT LANMAN 1.0"},
287 {-1,NULL}
290 /****************************************************************************
291 send a login command
292 ****************************************************************************/
293 BOOL cli_send_login(char *inbuf, char *outbuf, BOOL start_session, BOOL use_setup)
295 BOOL was_null = (!inbuf && !outbuf);
296 int sesskey=0;
297 time_t servertime = 0;
298 extern int serverzone;
299 int sec_mode=0;
300 int crypt_len;
301 int max_vcs=0;
302 char *pass = NULL;
303 pstring dev;
304 char *p;
305 int numprots;
307 if (was_null)
309 inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
310 outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
313 pstrcpy(dev,"A:");
314 if (connect_as_printer)
315 pstrcpy(dev,"LPT1:");
316 if (connect_as_ipc)
317 pstrcpy(dev,"IPC");
320 if (start_session && !cli_send_session_request(inbuf,outbuf))
322 if (was_null)
324 free(inbuf);
325 free(outbuf);
327 return(False);
330 bzero(outbuf,smb_size);
332 /* setup the protocol strings */
334 int plength;
336 for (plength=0,numprots=0;
337 prots[numprots].name && prots[numprots].prot<=max_protocol;
338 numprots++)
339 plength += strlen(prots[numprots].name)+2;
341 set_message(outbuf,0,plength,True);
343 p = smb_buf(outbuf);
344 for (numprots=0;
345 prots[numprots].name && prots[numprots].prot<=max_protocol;
346 numprots++)
348 *p++ = 2;
349 strcpy(p,prots[numprots].name);
350 p += strlen(p) + 1;
354 CVAL(outbuf,smb_com) = SMBnegprot;
355 cli_setup_pkt(outbuf);
357 CVAL(smb_buf(outbuf),0) = 2;
359 send_smb(Client,outbuf);
360 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
362 show_msg(inbuf);
364 if (CVAL(inbuf,smb_rcls) != 0 || ((int)SVAL(inbuf,smb_vwv0) >= numprots))
366 DEBUG(0,("SMBnegprot failed. myname=%s destname=%s - %s \n",
367 myname,desthost,smb_errstr(inbuf)));
368 if (was_null)
370 free(inbuf);
371 free(outbuf);
373 return(False);
376 Protocol = prots[SVAL(inbuf,smb_vwv0)].prot;
379 if (Protocol < PROTOCOL_NT1) {
380 sec_mode = SVAL(inbuf,smb_vwv1);
381 max_xmit = SVAL(inbuf,smb_vwv2);
382 sesskey = IVAL(inbuf,smb_vwv6);
383 serverzone = SVALS(inbuf,smb_vwv10)*60;
384 /* this time is converted to GMT by make_unix_date */
385 servertime = make_unix_date(inbuf+smb_vwv8);
386 if (Protocol >= PROTOCOL_COREPLUS) {
387 readbraw_supported = ((SVAL(inbuf,smb_vwv5) & 0x1) != 0);
388 writebraw_supported = ((SVAL(inbuf,smb_vwv5) & 0x2) != 0);
390 crypt_len = smb_buflen(inbuf);
391 memcpy(cryptkey,smb_buf(inbuf),8);
392 DEBUG(5,("max mux %d\n",SVAL(inbuf,smb_vwv3)));
393 max_vcs = SVAL(inbuf,smb_vwv4);
394 DEBUG(5,("max vcs %d\n",max_vcs));
395 DEBUG(5,("max blk %d\n",SVAL(inbuf,smb_vwv5)));
396 } else {
397 /* NT protocol */
398 sec_mode = CVAL(inbuf,smb_vwv1);
399 max_xmit = IVAL(inbuf,smb_vwv3+1);
400 sesskey = IVAL(inbuf,smb_vwv7+1);
401 serverzone = SVALS(inbuf,smb_vwv15+1)*60;
402 /* this time arrives in real GMT */
403 servertime = interpret_long_date(inbuf+smb_vwv11+1);
404 crypt_len = CVAL(inbuf,smb_vwv16+1);
405 memcpy(cryptkey,smb_buf(inbuf),8);
406 if (IVAL(inbuf,smb_vwv9+1) & 1)
407 readbraw_supported = writebraw_supported = True;
408 DEBUG(5,("max mux %d\n",SVAL(inbuf,smb_vwv1+1)));
409 max_vcs = SVAL(inbuf,smb_vwv2+1);
410 DEBUG(5,("max vcs %d\n",max_vcs));
411 DEBUG(5,("max raw %d\n",IVAL(inbuf,smb_vwv5+1)));
412 DEBUG(5,("capabilities 0x%x\n",IVAL(inbuf,smb_vwv9+1)));
415 DEBUG(5,("Sec mode %d\n",SVAL(inbuf,smb_vwv1)));
416 DEBUG(5,("max xmt %d\n",max_xmit));
417 DEBUG(5,("Got %d byte crypt key\n",crypt_len));
418 DEBUG(5,("Chose protocol [%s]\n",prots[SVAL(inbuf,smb_vwv0)].name));
420 doencrypt = ((sec_mode & 2) != 0);
422 if (servertime) {
423 static BOOL done_time = False;
424 if (!done_time) {
425 DEBUG(1,("Server time is %sTimezone is UTC%+02.1f\n",
426 asctime(LocalTime(&servertime)),
427 -(double)(serverzone/3600.0)));
428 done_time = True;
432 get_pass:
434 if (got_pass)
435 pass = password;
436 else
437 pass = (char *)getpass("Password: ");
439 if(pass == NULL)
441 DEBUG(0, ("cli_send_login : no password available - logon failed.\n"));
442 return False;
445 if (Protocol >= PROTOCOL_LANMAN1 && use_setup)
447 fstring pword;
448 int passlen = strlen(pass)+1;
449 fstrcpy(pword,pass);
451 if (doencrypt && *pass) {
452 DEBUG(5,("Using encrypted passwords\n"));
453 passlen = 24;
454 SMBencrypt((uchar *)pass,(uchar *)cryptkey,(uchar *)pword);
457 /* if in share level security then don't send a password now */
458 if (!(sec_mode & 1)) {fstrcpy(pword, "");passlen=1;}
460 /* send a session setup command */
461 bzero(outbuf,smb_size);
463 if (passlen > MAX_PASS_LEN) {
464 DEBUG(1,("password too long %d\n", passlen));
465 return False;
468 if (Protocol < PROTOCOL_NT1) {
469 set_message(outbuf,10,1 + strlen(username) + passlen,True);
470 CVAL(outbuf,smb_com) = SMBsesssetupX;
471 cli_setup_pkt(outbuf);
473 CVAL(outbuf,smb_vwv0) = 0xFF;
474 SSVAL(outbuf,smb_vwv2,max_xmit);
475 SSVAL(outbuf,smb_vwv3,2);
476 SSVAL(outbuf,smb_vwv4,max_vcs-1);
477 SIVAL(outbuf,smb_vwv5,sesskey);
478 SSVAL(outbuf,smb_vwv7,passlen);
479 p = smb_buf(outbuf);
480 memcpy(p,pword,passlen);
481 p += passlen;
482 strcpy(p,username);
483 } else {
484 if (!doencrypt) passlen--;
485 /* for Win95 */
486 set_message(outbuf,13,0,True);
487 CVAL(outbuf,smb_com) = SMBsesssetupX;
488 cli_setup_pkt(outbuf);
490 CVAL(outbuf,smb_vwv0) = 0xFF;
491 SSVAL(outbuf,smb_vwv2,BUFFER_SIZE);
492 SSVAL(outbuf,smb_vwv3,2);
493 SSVAL(outbuf,smb_vwv4,getpid());
494 SIVAL(outbuf,smb_vwv5,sesskey);
495 SSVAL(outbuf,smb_vwv7,passlen);
496 SSVAL(outbuf,smb_vwv8,0);
497 p = smb_buf(outbuf);
498 memcpy(p,pword,passlen); p += SVAL(outbuf,smb_vwv7);
499 strcpy(p,username);p = skip_string(p,1);
500 strcpy(p,workgroup);p = skip_string(p,1);
501 strcpy(p,"Unix");p = skip_string(p,1);
502 strcpy(p,"Samba");p = skip_string(p,1);
503 set_message(outbuf,13,PTR_DIFF(p,smb_buf(outbuf)),False);
506 send_smb(Client,outbuf);
507 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
509 show_msg(inbuf);
511 if (CVAL(inbuf,smb_rcls) != 0)
513 if (! *pass &&
514 ((CVAL(inbuf,smb_rcls) == ERRDOS &&
515 SVAL(inbuf,smb_err) == ERRnoaccess) ||
516 (CVAL(inbuf,smb_rcls) == ERRSRV &&
517 SVAL(inbuf,smb_err) == ERRbadpw)))
519 got_pass = False;
520 DEBUG(5,("resending login\n"));
521 goto get_pass;
524 DEBUG(0,("Session setup failed for username=%s myname=%s destname=%s %s\n",
525 username,myname,desthost,smb_errstr(inbuf)));
526 DEBUG(0,("You might find the -U or -n options useful\n"));
527 DEBUG(0,("Sometimes you have to use `-n USERNAME' (particularly with OS/2)\n"));
528 DEBUG(0,("Some servers also insist on uppercase-only passwords\n"));
529 if (was_null)
531 free(inbuf);
532 free(outbuf);
534 return(False);
537 if (Protocol >= PROTOCOL_NT1) {
538 char *domain,*os,*lanman;
539 p = smb_buf(inbuf);
540 os = p;
541 lanman = skip_string(os,1);
542 domain = skip_string(lanman,1);
543 if (*domain || *os || *lanman)
544 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",domain,os,lanman));
547 /* use the returned uid from now on */
548 if (SVAL(inbuf,smb_uid) != uid)
549 DEBUG(5,("Server gave us a UID of %d. We gave %d\n",
550 SVAL(inbuf,smb_uid),uid));
551 uid = SVAL(inbuf,smb_uid);
554 /* now we've got a connection - send a tcon message */
555 bzero(outbuf,smb_size);
557 if (strncmp(service,"\\\\",2) != 0)
559 DEBUG(0,("\nWarning: Your service name doesn't start with \\\\. This is probably incorrect.\n"));
560 DEBUG(0,("Perhaps try replacing each \\ with \\\\ on the command line?\n\n"));
564 again2:
567 int passlen = strlen(pass)+1;
568 fstring pword;
569 fstrcpy(pword,pass);
571 if (doencrypt && *pass) {
572 passlen=24;
573 SMBencrypt((uchar *)pass,(uchar *)cryptkey,(uchar *)pword);
576 /* if in user level security then don't send a password now */
577 if ((sec_mode & 1)) {
578 fstrcpy(pword, ""); passlen=1;
581 set_message(outbuf,4,2 + strlen(service) + passlen + strlen(dev),True);
582 CVAL(outbuf,smb_com) = SMBtconX;
583 cli_setup_pkt(outbuf);
585 SSVAL(outbuf,smb_vwv0,0xFF);
586 SSVAL(outbuf,smb_vwv3,passlen);
588 p = smb_buf(outbuf);
589 memcpy(p,pword,passlen);
590 p += passlen;
591 strcpy(p,service);
592 p = skip_string(p,1);
593 strcpy(p,dev);
596 send_smb(Client,outbuf);
597 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
599 /* trying again with a blank password */
600 if (CVAL(inbuf,smb_rcls) != 0 &&
601 (int)strlen(pass) > 0 &&
602 !doencrypt &&
603 Protocol >= PROTOCOL_LANMAN1)
605 DEBUG(2,("first SMBtconX failed, trying again. %s\n",smb_errstr(inbuf)));
606 strcpy(pass,"");
607 goto again2;
610 if (CVAL(inbuf,smb_rcls) != 0)
612 DEBUG(0,("SMBtconX failed. %s\n",smb_errstr(inbuf)));
613 DEBUG(0,("Perhaps you are using the wrong sharename, username or password?\n"));
614 DEBUG(0,("Some servers insist that these be in uppercase\n"));
615 if (was_null)
617 free(inbuf);
618 free(outbuf);
620 return(False);
624 max_xmit = MIN(max_xmit,BUFFER_SIZE-4);
625 if (max_xmit <= 0)
626 max_xmit = BUFFER_SIZE - 4;
628 cnum = SVAL(inbuf,smb_tid);
630 DEBUG(5,("Connected with cnum=%d max_xmit=%d\n",cnum,max_xmit));
632 if (was_null)
634 free(inbuf);
635 free(outbuf);
637 return True;
641 /****************************************************************************
642 send a logout command
643 ****************************************************************************/
644 void cli_send_logout(void)
646 pstring inbuf,outbuf;
648 bzero(outbuf,smb_size);
649 set_message(outbuf,0,0,True);
650 CVAL(outbuf,smb_com) = SMBtdis;
651 SSVAL(outbuf,smb_tid,cnum);
652 cli_setup_pkt(outbuf);
654 send_smb(Client,outbuf);
655 receive_smb(Client,inbuf,SHORT_TIMEOUT);
657 if (CVAL(inbuf,smb_rcls) != 0)
659 DEBUG(0,("SMBtdis failed %s\n",smb_errstr(inbuf)));
663 #ifdef STATS
664 stats_report();
665 #endif
666 exit(0);
671 /****************************************************************************
672 call a remote api
673 ****************************************************************************/
674 BOOL cli_call_api(int prcnt,int drcnt,int mprcnt,int mdrcnt,int *rprcnt,
675 int *rdrcnt, char *param,char *data, char **rparam,char **rdata)
677 static char *inbuf=NULL;
678 static char *outbuf=NULL;
680 if (!inbuf) inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
681 if (!outbuf) outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
683 cli_send_trans_request(outbuf,SMBtrans,"\\PIPE\\LANMAN",0,0,
684 data,param,NULL,
685 drcnt,prcnt,0,
686 mdrcnt,mprcnt,0);
688 return (cli_receive_trans_response(inbuf,SMBtrans,
689 rdrcnt,rprcnt,
690 rdata,rparam));
693 /****************************************************************************
694 send a SMB trans or trans2 request
695 ****************************************************************************/
696 BOOL cli_send_trans_request(char *outbuf, int trans, char *name, int fid, int flags,
697 char *data,char *param,uint16 *setup, int ldata,int lparam,
698 int lsetup,int mdata,int mparam,int msetup)
700 int i;
701 int this_ldata,this_lparam;
702 int tot_data=0,tot_param=0;
703 char *outdata,*outparam;
704 pstring inbuf;
705 char *p;
707 this_lparam = MIN(lparam,max_xmit - (500+lsetup*SIZEOFWORD)); /* hack */
708 this_ldata = MIN(ldata,max_xmit - (500+lsetup*SIZEOFWORD+this_lparam));
710 bzero(outbuf,smb_size);
711 set_message(outbuf,14+lsetup,0,True);
712 CVAL(outbuf,smb_com) = trans;
713 SSVAL(outbuf,smb_tid,cnum);
714 cli_setup_pkt(outbuf);
716 outparam = smb_buf(outbuf)+(trans==SMBtrans ? strlen(name)+1 : 3);
717 outdata = outparam+this_lparam;
719 /* primary request */
720 SSVAL(outbuf,smb_tpscnt,lparam); /* tpscnt */
721 SSVAL(outbuf,smb_tdscnt,ldata); /* tdscnt */
722 SSVAL(outbuf,smb_mprcnt,mparam); /* mprcnt */
723 SSVAL(outbuf,smb_mdrcnt,mdata); /* mdrcnt */
724 SCVAL(outbuf,smb_msrcnt,msetup); /* msrcnt */
725 SSVAL(outbuf,smb_flags,flags); /* flags */
726 SIVAL(outbuf,smb_timeout,0); /* timeout */
727 SSVAL(outbuf,smb_pscnt,this_lparam); /* pscnt */
728 SSVAL(outbuf,smb_psoff,smb_offset(outparam,outbuf)); /* psoff */
729 SSVAL(outbuf,smb_dscnt,this_ldata); /* dscnt */
730 SSVAL(outbuf,smb_dsoff,smb_offset(outdata,outbuf)); /* dsoff */
731 SCVAL(outbuf,smb_suwcnt,lsetup); /* suwcnt */
732 for (i=0;i<lsetup;i++) /* setup[] */
733 SSVAL(outbuf,smb_setup+i*SIZEOFWORD,setup[i]);
734 p = smb_buf(outbuf);
735 if (trans==SMBtrans)
736 strcpy(p,name); /* name[] */
737 else
739 *p++ = 0; /* put in a null smb_name */
740 *p++ = 'D'; *p++ = ' '; /* this was added because OS/2 does it */
742 if (this_lparam) /* param[] */
743 memcpy(outparam,param,this_lparam);
744 if (this_ldata) /* data[] */
745 memcpy(outdata,data,this_ldata);
746 set_message(outbuf,14+lsetup, /* wcnt, bcc */
747 PTR_DIFF(outdata+this_ldata,smb_buf(outbuf)),False);
749 show_msg(outbuf);
750 send_smb(Client,outbuf);
752 if (this_ldata < ldata || this_lparam < lparam)
754 /* receive interim response */
755 if (!receive_smb(Client,inbuf,SHORT_TIMEOUT) || CVAL(inbuf,smb_rcls) != 0)
757 DEBUG(0,("%s request failed (%s)\n",
758 trans==SMBtrans?"SMBtrans":"SMBtrans2", smb_errstr(inbuf)));
759 return(False);
762 tot_data = this_ldata;
763 tot_param = this_lparam;
765 while (tot_data < ldata || tot_param < lparam)
767 this_lparam = MIN(lparam-tot_param,max_xmit - 500); /* hack */
768 this_ldata = MIN(ldata-tot_data,max_xmit - (500+this_lparam));
770 set_message(outbuf,trans==SMBtrans?8:9,0,True);
771 CVAL(outbuf,smb_com) = trans==SMBtrans ? SMBtranss : SMBtranss2;
773 outparam = smb_buf(outbuf);
774 outdata = outparam+this_lparam;
776 /* secondary request */
777 SSVAL(outbuf,smb_tpscnt,lparam); /* tpscnt */
778 SSVAL(outbuf,smb_tdscnt,ldata); /* tdscnt */
779 SSVAL(outbuf,smb_spscnt,this_lparam); /* pscnt */
780 SSVAL(outbuf,smb_spsoff,smb_offset(outparam,outbuf)); /* psoff */
781 SSVAL(outbuf,smb_spsdisp,tot_param); /* psdisp */
782 SSVAL(outbuf,smb_sdscnt,this_ldata); /* dscnt */
783 SSVAL(outbuf,smb_sdsoff,smb_offset(outdata,outbuf)); /* dsoff */
784 SSVAL(outbuf,smb_sdsdisp,tot_data); /* dsdisp */
785 if (trans==SMBtrans2)
786 SSVAL(outbuf,smb_sfid,fid); /* fid */
787 if (this_lparam) /* param[] */
788 memcpy(outparam,param,this_lparam);
789 if (this_ldata) /* data[] */
790 memcpy(outdata,data,this_ldata);
791 set_message(outbuf,trans==SMBtrans?8:9, /* wcnt, bcc */
792 PTR_DIFF(outdata+this_ldata,smb_buf(outbuf)),False);
794 show_msg(outbuf);
795 send_smb(Client,outbuf);
797 tot_data += this_ldata;
798 tot_param += this_lparam;
802 return(True);
806 /****************************************************************************
807 open the client sockets
808 ****************************************************************************/
809 BOOL cli_open_sockets(int port)
811 static int last_port;
812 char *host;
813 pstring service2;
814 extern int Client;
816 if (port == 0) port=last_port;
817 last_port=port;
819 strupper(service);
821 if (*desthost)
823 host = desthost;
825 else
827 pstrcpy(service2,service);
828 host = strtok(service2,"\\/");
829 pstrcpy(desthost,host);
832 DEBUG(5,("Opening sockets\n"));
834 if (*myname == 0)
835 get_myname(myname,NULL);
836 strupper(myname);
838 if (!have_ip)
840 struct hostent *hp;
842 if ((hp = Get_Hostbyname(host)) == 0)
844 DEBUG(0,("Get_Hostbyname: Unknown host %s.\n",host));
845 return False;
848 putip((char *)&dest_ip,(char *)hp->h_addr);
851 Client = open_socket_out(SOCK_STREAM, &dest_ip, port, SHORT_CONNECT_TIMEOUT);
852 if (Client == -1)
853 return False;
855 DEBUG(5,("Connected\n"));
857 set_socket_options(Client,user_socket_options);
859 return True;
862 /****************************************************************************
863 close and open the connection again
864 ****************************************************************************/
865 BOOL cli_reopen_connection(char *inbuf,char *outbuf)
867 static int open_count=0;
869 open_count++;
871 if (open_count>5) return(False);
873 DEBUG(1,("Trying to re-open connection\n"));
875 set_message(outbuf,0,0,True);
876 SCVAL(outbuf,smb_com,SMBtdis);
877 SSVAL(outbuf,smb_tid,cnum);
878 cli_setup_pkt(outbuf);
880 send_smb(Client,outbuf);
881 receive_smb(Client,inbuf,SHORT_TIMEOUT);
883 close_sockets();
884 if (!cli_open_sockets(0)) return(False);
886 return(cli_send_login(inbuf,outbuf,True,True));
889 /* error code stuff - put together by Merik Karman
890 merik@blackadder.dsh.oz.au */
892 typedef struct
894 char *name;
895 int code;
896 char *message;
897 } err_code_struct;
899 /* Dos Error Messages */
900 err_code_struct dos_msgs[] = {
901 {"ERRbadfunc",1,"Invalid function."},
902 {"ERRbadfile",2,"File not found."},
903 {"ERRbadpath",3,"Directory invalid."},
904 {"ERRnofids",4,"No file descriptors available"},
905 {"ERRnoaccess",5,"Access denied."},
906 {"ERRbadfid",6,"Invalid file handle."},
907 {"ERRbadmcb",7,"Memory control blocks destroyed."},
908 {"ERRnomem",8,"Insufficient server memory to perform the requested function."},
909 {"ERRbadmem",9,"Invalid memory block address."},
910 {"ERRbadenv",10,"Invalid environment."},
911 {"ERRbadformat",11,"Invalid format."},
912 {"ERRbadaccess",12,"Invalid open mode."},
913 {"ERRbaddata",13,"Invalid data."},
914 {"ERR",14,"reserved."},
915 {"ERRbaddrive",15,"Invalid drive specified."},
916 {"ERRremcd",16,"A Delete Directory request attempted to remove the server's current directory."},
917 {"ERRdiffdevice",17,"Not same device."},
918 {"ERRnofiles",18,"A File Search command can find no more files matching the specified criteria."},
919 {"ERRbadshare",32,"The sharing mode specified for an Open conflicts with existing FIDs on the file."},
920 {"ERRlock",33,"A Lock request conflicted with an existing lock or specified an invalid mode, or an Unlock requested attempted to remove a lock held by another process."},
921 {"ERRfilexists",80,"The file named in a Create Directory, Make New File or Link request already exists."},
922 {"ERRbadpipe",230,"Pipe invalid."},
923 {"ERRpipebusy",231,"All instances of the requested pipe are busy."},
924 {"ERRpipeclosing",232,"Pipe close in progress."},
925 {"ERRnotconnected",233,"No process on other end of pipe."},
926 {"ERRmoredata",234,"There is more data to be returned."},
927 {NULL,-1,NULL}};
929 /* Server Error Messages */
930 err_code_struct server_msgs[] = {
931 {"ERRerror",1,"Non-specific error code."},
932 {"ERRbadpw",2,"Bad password - name/password pair in a Tree Connect or Session Setup are invalid."},
933 {"ERRbadtype",3,"reserved."},
934 {"ERRaccess",4,"The requester does not have the necessary access rights within the specified context for the requested function. The context is defined by the TID or the UID."},
935 {"ERRinvnid",5,"The tree ID (TID) specified in a command was invalid."},
936 {"ERRinvnetname",6,"Invalid network name in tree connect."},
937 {"ERRinvdevice",7,"Invalid device - printer request made to non-printer connection or non-printer request made to printer connection."},
938 {"ERRqfull",49,"Print queue full (files) -- returned by open print file."},
939 {"ERRqtoobig",50,"Print queue full -- no space."},
940 {"ERRqeof",51,"EOF on print queue dump."},
941 {"ERRinvpfid",52,"Invalid print file FID."},
942 {"ERRsmbcmd",64,"The server did not recognize the command received."},
943 {"ERRsrverror",65,"The server encountered an internal error, e.g., system file unavailable."},
944 {"ERRfilespecs",67,"The file handle (FID) and pathname parameters contained an invalid combination of values."},
945 {"ERRreserved",68,"reserved."},
946 {"ERRbadpermits",69,"The access permissions specified for a file or directory are not a valid combination. The server cannot set the requested attribute."},
947 {"ERRreserved",70,"reserved."},
948 {"ERRsetattrmode",71,"The attribute mode in the Set File Attribute request is invalid."},
949 {"ERRpaused",81,"Server is paused."},
950 {"ERRmsgoff",82,"Not receiving messages."},
951 {"ERRnoroom",83,"No room to buffer message."},
952 {"ERRrmuns",87,"Too many remote user names."},
953 {"ERRtimeout",88,"Operation timed out."},
954 {"ERRnoresource",89,"No resources currently available for request."},
955 {"ERRtoomanyuids",90,"Too many UIDs active on this session."},
956 {"ERRbaduid",91,"The UID is not known as a valid ID on this session."},
957 {"ERRusempx",250,"Temp unable to support Raw, use MPX mode."},
958 {"ERRusestd",251,"Temp unable to support Raw, use standard read/write."},
959 {"ERRcontmpx",252,"Continue in MPX mode."},
960 {"ERRreserved",253,"reserved."},
961 {"ERRreserved",254,"reserved."},
962 {"ERRnosupport",0xFFFF,"Function not supported."},
963 {NULL,-1,NULL}};
965 /* Hard Error Messages */
966 err_code_struct hard_msgs[] = {
967 {"ERRnowrite",19,"Attempt to write on write-protected diskette."},
968 {"ERRbadunit",20,"Unknown unit."},
969 {"ERRnotready",21,"Drive not ready."},
970 {"ERRbadcmd",22,"Unknown command."},
971 {"ERRdata",23,"Data error (CRC)."},
972 {"ERRbadreq",24,"Bad request structure length."},
973 {"ERRseek",25 ,"Seek error."},
974 {"ERRbadmedia",26,"Unknown media type."},
975 {"ERRbadsector",27,"Sector not found."},
976 {"ERRnopaper",28,"Printer out of paper."},
977 {"ERRwrite",29,"Write fault."},
978 {"ERRread",30,"Read fault."},
979 {"ERRgeneral",31,"General failure."},
980 {"ERRbadshare",32,"A open conflicts with an existing open."},
981 {"ERRlock",33,"A Lock request conflicted with an existing lock or specified an invalid mode, or an Unlock requested attempted to remove a lock held by another process."},
982 {"ERRwrongdisk",34,"The wrong disk was found in a drive."},
983 {"ERRFCBUnavail",35,"No FCBs are available to process request."},
984 {"ERRsharebufexc",36,"A sharing buffer has been exceeded."},
985 {NULL,-1,NULL}};
988 struct
990 int code;
991 char *class;
992 err_code_struct *err_msgs;
993 } err_classes[] = {
994 {0,"SUCCESS",NULL},
995 {0x01,"ERRDOS",dos_msgs},
996 {0x02,"ERRSRV",server_msgs},
997 {0x03,"ERRHRD",hard_msgs},
998 {0x04,"ERRXOS",NULL},
999 {0xE1,"ERRRMX1",NULL},
1000 {0xE2,"ERRRMX2",NULL},
1001 {0xE3,"ERRRMX3",NULL},
1002 {0xFF,"ERRCMD",NULL},
1003 {-1,NULL,NULL}};
1006 /****************************************************************************
1007 return a SMB error string from a SMB buffer
1008 ****************************************************************************/
1009 char *smb_errstr(char *inbuf)
1011 static pstring ret;
1012 int class = CVAL(inbuf,smb_rcls);
1013 int num = SVAL(inbuf,smb_err);
1014 int i,j;
1016 for (i=0;err_classes[i].class;i++)
1017 if (err_classes[i].code == class)
1019 if (err_classes[i].err_msgs)
1021 err_code_struct *err = err_classes[i].err_msgs;
1022 for (j=0;err[j].name;j++)
1023 if (num == err[j].code)
1025 if (DEBUGLEVEL > 0)
1026 sprintf(ret,"%s - %s (%s)",err_classes[i].class,
1027 err[j].name,err[j].message);
1028 else
1029 sprintf(ret,"%s - %s",err_classes[i].class,err[j].name);
1030 return ret;
1034 sprintf(ret,"%s - %d",err_classes[i].class,num);
1035 return ret;
1038 sprintf(ret,"ERROR: Unknown error (%d,%d)",class,num);
1039 return(ret);