some cleanups in the clientutil.c code.
[Samba.git] / source / client / clientutil.c
blob9dd1dc040e43aa00026ca44952f833a453aa505d
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 static 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);
94 /****************************************************************************
95 receive a SMB trans or trans2 response allocating the necessary memory
96 ****************************************************************************/
97 static BOOL cli_receive_trans_response(char *inbuf,int trans,int *data_len,
98 int *param_len, char **data,
99 char **param)
101 int total_data=0;
102 int total_param=0;
103 int this_data,this_param;
105 *data_len = *param_len = 0;
107 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
108 show_msg(inbuf);
110 /* sanity check */
111 if (CVAL(inbuf,smb_com) != trans)
113 DEBUG(0,("Expected %s response, got command 0x%02x\n",
114 trans==SMBtrans?"SMBtrans":"SMBtrans2", CVAL(inbuf,smb_com)));
115 return(False);
117 if (CVAL(inbuf,smb_rcls) != 0)
118 return(False);
120 /* parse out the lengths */
121 total_data = SVAL(inbuf,smb_tdrcnt);
122 total_param = SVAL(inbuf,smb_tprcnt);
124 /* allocate it */
125 *data = Realloc(*data,total_data);
126 *param = Realloc(*param,total_param);
128 while (1)
130 this_data = SVAL(inbuf,smb_drcnt);
131 this_param = SVAL(inbuf,smb_prcnt);
133 if (this_data + *data_len > total_data ||
134 this_param + *param_len > total_param) {
135 DEBUG(1,("Data overflow in cli_receive_trans_response\n"));
136 return False;
139 if (this_data)
140 memcpy(*data + SVAL(inbuf,smb_drdisp),
141 smb_base(inbuf) + SVAL(inbuf,smb_droff),
142 this_data);
143 if (this_param)
144 memcpy(*param + SVAL(inbuf,smb_prdisp),
145 smb_base(inbuf) + SVAL(inbuf,smb_proff),
146 this_param);
147 *data_len += this_data;
148 *param_len += this_param;
150 /* parse out the total lengths again - they can shrink! */
151 total_data = SVAL(inbuf,smb_tdrcnt);
152 total_param = SVAL(inbuf,smb_tprcnt);
154 if (total_data <= *data_len && total_param <= *param_len)
155 break;
157 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
158 show_msg(inbuf);
160 /* sanity check */
161 if (CVAL(inbuf,smb_com) != trans)
163 DEBUG(0,("Expected %s response, got command 0x%02x\n",
164 trans==SMBtrans?"SMBtrans":"SMBtrans2", CVAL(inbuf,smb_com)));
165 return(False);
167 if (CVAL(inbuf,smb_rcls) != 0)
168 return(False);
171 return(True);
174 /****************************************************************************
175 send a session request
176 ****************************************************************************/
177 static BOOL cli_send_session_request(char *inbuf, char *outbuf)
179 fstring dest;
180 char *p;
181 int len = 4;
182 /* send a session request (RFC 8002) */
184 fstrcpy(dest,desthost);
186 p = strchr(dest,'.');
187 if (p) *p = 0;
189 /* put in the destination name */
190 p = outbuf+len;
191 name_mangle(dest,p,name_type);
192 len += name_len(p);
194 /* and my name */
195 p = outbuf+len;
196 name_mangle(myname,p,0);
197 len += name_len(p);
199 /* setup the packet length */
200 _smb_setlen(outbuf,len);
201 CVAL(outbuf,0) = 0x81;
203 send_smb(Client,outbuf);
204 DEBUG(5,("Sent session request\n"));
206 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
208 if (CVAL(inbuf,0) == 0x84) /* C. Hoch 9/14/95 Start */
210 /* For information, here is the response structure.
211 * We do the byte-twiddling to for portability.
212 struct RetargetResponse{
213 unsigned char type;
214 unsigned char flags;
215 int16 length;
216 int32 ip_addr;
217 int16 port;
220 extern int Client;
221 int port = (CVAL(inbuf,8)<<8)+CVAL(inbuf,9);
222 /* SESSION RETARGET */
223 putip((char *)&dest_ip,inbuf+4);
225 close_sockets();
226 Client = open_socket_out(SOCK_STREAM, &dest_ip, port, SHORT_CONNECT_TIMEOUT);
227 if (Client == -1)
228 return False;
230 DEBUG(5,("Retargeted\n"));
232 set_socket_options(Client,user_socket_options);
234 /* Try again */
235 return cli_send_session_request(inbuf,outbuf);
236 } /* C. Hoch 9/14/95 End */
239 if (CVAL(inbuf,0) != 0x82)
241 int ecode = CVAL(inbuf,4);
242 DEBUG(0,("Session request failed (%d,%d) with myname=%s destname=%s\n",
243 CVAL(inbuf,0),ecode,myname,desthost));
244 switch (ecode)
246 case 0x80:
247 DEBUG(0,("Not listening on called name\n"));
248 DEBUG(0,("Try to connect to another name (instead of %s)\n",desthost));
249 DEBUG(0,("You may find the -I option useful for this\n"));
250 break;
251 case 0x81:
252 DEBUG(0,("Not listening for calling name\n"));
253 DEBUG(0,("Try to connect as another name (instead of %s)\n",myname));
254 DEBUG(0,("You may find the -n option useful for this\n"));
255 break;
256 case 0x82:
257 DEBUG(0,("Called name not present\n"));
258 DEBUG(0,("Try to connect to another name (instead of %s)\n",desthost));
259 DEBUG(0,("You may find the -I option useful for this\n"));
260 break;
261 case 0x83:
262 DEBUG(0,("Called name present, but insufficient resources\n"));
263 DEBUG(0,("Perhaps you should try again later?\n"));
264 break;
265 default:
266 DEBUG(0,("Unspecified error 0x%X\n",ecode));
267 DEBUG(0,("Your server software is being unfriendly\n"));
268 break;
270 return(False);
272 return(True);
276 static struct {
277 int prot;
278 char *name;
280 prots[] =
282 {PROTOCOL_CORE,"PC NETWORK PROGRAM 1.0"},
283 {PROTOCOL_COREPLUS,"MICROSOFT NETWORKS 1.03"},
284 {PROTOCOL_LANMAN1,"MICROSOFT NETWORKS 3.0"},
285 {PROTOCOL_LANMAN1,"LANMAN1.0"},
286 {PROTOCOL_LANMAN2,"LM1.2X002"},
287 {PROTOCOL_LANMAN2,"Samba"},
288 {PROTOCOL_NT1,"NT LM 0.12"},
289 {PROTOCOL_NT1,"NT LANMAN 1.0"},
290 {-1,NULL}
293 /****************************************************************************
294 send a login command
295 ****************************************************************************/
296 BOOL cli_send_login(char *inbuf, char *outbuf, BOOL start_session, BOOL use_setup)
298 BOOL was_null = (!inbuf && !outbuf);
299 int sesskey=0;
300 time_t servertime = 0;
301 extern int serverzone;
302 int sec_mode=0;
303 int crypt_len;
304 int max_vcs=0;
305 char *pass = NULL;
306 pstring dev;
307 char *p;
308 int numprots;
310 if (was_null)
312 inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
313 outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
316 pstrcpy(dev,"A:");
317 if (connect_as_printer)
318 pstrcpy(dev,"LPT1:");
319 if (connect_as_ipc)
320 pstrcpy(dev,"IPC");
323 if (start_session && !cli_send_session_request(inbuf,outbuf))
325 if (was_null)
327 free(inbuf);
328 free(outbuf);
330 return(False);
333 bzero(outbuf,smb_size);
335 /* setup the protocol strings */
337 int plength;
339 for (plength=0,numprots=0;
340 prots[numprots].name && prots[numprots].prot<=max_protocol;
341 numprots++)
342 plength += strlen(prots[numprots].name)+2;
344 set_message(outbuf,0,plength,True);
346 p = smb_buf(outbuf);
347 for (numprots=0;
348 prots[numprots].name && prots[numprots].prot<=max_protocol;
349 numprots++)
351 *p++ = 2;
352 strcpy(p,prots[numprots].name);
353 p += strlen(p) + 1;
357 CVAL(outbuf,smb_com) = SMBnegprot;
358 cli_setup_pkt(outbuf);
360 CVAL(smb_buf(outbuf),0) = 2;
362 send_smb(Client,outbuf);
363 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
365 show_msg(inbuf);
367 if (CVAL(inbuf,smb_rcls) != 0 || ((int)SVAL(inbuf,smb_vwv0) >= numprots))
369 DEBUG(0,("SMBnegprot failed. myname=%s destname=%s - %s \n",
370 myname,desthost,smb_errstr(inbuf)));
371 if (was_null)
373 free(inbuf);
374 free(outbuf);
376 return(False);
379 Protocol = prots[SVAL(inbuf,smb_vwv0)].prot;
382 if (Protocol < PROTOCOL_NT1) {
383 sec_mode = SVAL(inbuf,smb_vwv1);
384 max_xmit = SVAL(inbuf,smb_vwv2);
385 sesskey = IVAL(inbuf,smb_vwv6);
386 serverzone = SVALS(inbuf,smb_vwv10)*60;
387 /* this time is converted to GMT by make_unix_date */
388 servertime = make_unix_date(inbuf+smb_vwv8);
389 if (Protocol >= PROTOCOL_COREPLUS) {
390 readbraw_supported = ((SVAL(inbuf,smb_vwv5) & 0x1) != 0);
391 writebraw_supported = ((SVAL(inbuf,smb_vwv5) & 0x2) != 0);
393 crypt_len = smb_buflen(inbuf);
394 memcpy(cryptkey,smb_buf(inbuf),8);
395 DEBUG(5,("max mux %d\n",SVAL(inbuf,smb_vwv3)));
396 max_vcs = SVAL(inbuf,smb_vwv4);
397 DEBUG(5,("max vcs %d\n",max_vcs));
398 DEBUG(5,("max blk %d\n",SVAL(inbuf,smb_vwv5)));
399 } else {
400 /* NT protocol */
401 sec_mode = CVAL(inbuf,smb_vwv1);
402 max_xmit = IVAL(inbuf,smb_vwv3+1);
403 sesskey = IVAL(inbuf,smb_vwv7+1);
404 serverzone = SVALS(inbuf,smb_vwv15+1)*60;
405 /* this time arrives in real GMT */
406 servertime = interpret_long_date(inbuf+smb_vwv11+1);
407 crypt_len = CVAL(inbuf,smb_vwv16+1);
408 memcpy(cryptkey,smb_buf(inbuf),8);
409 if (IVAL(inbuf,smb_vwv9+1) & 1)
410 readbraw_supported = writebraw_supported = True;
411 DEBUG(5,("max mux %d\n",SVAL(inbuf,smb_vwv1+1)));
412 max_vcs = SVAL(inbuf,smb_vwv2+1);
413 DEBUG(5,("max vcs %d\n",max_vcs));
414 DEBUG(5,("max raw %d\n",IVAL(inbuf,smb_vwv5+1)));
415 DEBUG(5,("capabilities 0x%x\n",IVAL(inbuf,smb_vwv9+1)));
418 DEBUG(5,("Sec mode %d\n",SVAL(inbuf,smb_vwv1)));
419 DEBUG(5,("max xmt %d\n",max_xmit));
420 DEBUG(5,("Got %d byte crypt key\n",crypt_len));
421 DEBUG(5,("Chose protocol [%s]\n",prots[SVAL(inbuf,smb_vwv0)].name));
423 doencrypt = ((sec_mode & 2) != 0);
425 if (servertime) {
426 static BOOL done_time = False;
427 if (!done_time) {
428 DEBUG(1,("Server time is %sTimezone is UTC%+02.1f\n",
429 asctime(LocalTime(&servertime)),
430 -(double)(serverzone/3600.0)));
431 done_time = True;
435 get_pass:
437 if (got_pass)
438 pass = password;
439 else
440 pass = (char *)getpass("Password: ");
442 if(pass == NULL)
444 DEBUG(0, ("cli_send_login : no password available - logon failed.\n"));
445 return False;
448 if (Protocol >= PROTOCOL_LANMAN1 && use_setup)
450 fstring pword;
451 int passlen = strlen(pass)+1;
452 fstrcpy(pword,pass);
454 if (doencrypt && *pass) {
455 DEBUG(5,("Using encrypted passwords\n"));
456 passlen = 24;
457 SMBencrypt((uchar *)pass,(uchar *)cryptkey,(uchar *)pword);
460 /* if in share level security then don't send a password now */
461 if (!(sec_mode & 1)) {fstrcpy(pword, "");passlen=1;}
463 /* send a session setup command */
464 bzero(outbuf,smb_size);
466 if (passlen > MAX_PASS_LEN) {
467 DEBUG(1,("password too long %d\n", passlen));
468 return False;
471 if (Protocol < PROTOCOL_NT1) {
472 set_message(outbuf,10,1 + strlen(username) + passlen,True);
473 CVAL(outbuf,smb_com) = SMBsesssetupX;
474 cli_setup_pkt(outbuf);
476 CVAL(outbuf,smb_vwv0) = 0xFF;
477 SSVAL(outbuf,smb_vwv2,max_xmit);
478 SSVAL(outbuf,smb_vwv3,2);
479 SSVAL(outbuf,smb_vwv4,max_vcs-1);
480 SIVAL(outbuf,smb_vwv5,sesskey);
481 SSVAL(outbuf,smb_vwv7,passlen);
482 p = smb_buf(outbuf);
483 memcpy(p,pword,passlen);
484 p += passlen;
485 strcpy(p,username);
486 } else {
487 if (!doencrypt) passlen--;
488 /* for Win95 */
489 set_message(outbuf,13,0,True);
490 CVAL(outbuf,smb_com) = SMBsesssetupX;
491 cli_setup_pkt(outbuf);
493 CVAL(outbuf,smb_vwv0) = 0xFF;
494 SSVAL(outbuf,smb_vwv2,BUFFER_SIZE);
495 SSVAL(outbuf,smb_vwv3,2);
496 SSVAL(outbuf,smb_vwv4,getpid());
497 SIVAL(outbuf,smb_vwv5,sesskey);
498 SSVAL(outbuf,smb_vwv7,passlen);
499 SSVAL(outbuf,smb_vwv8,0);
500 p = smb_buf(outbuf);
501 memcpy(p,pword,passlen); p += SVAL(outbuf,smb_vwv7);
502 strcpy(p,username);p = skip_string(p,1);
503 strcpy(p,workgroup);p = skip_string(p,1);
504 strcpy(p,"Unix");p = skip_string(p,1);
505 strcpy(p,"Samba");p = skip_string(p,1);
506 set_message(outbuf,13,PTR_DIFF(p,smb_buf(outbuf)),False);
509 send_smb(Client,outbuf);
510 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
512 show_msg(inbuf);
514 if (CVAL(inbuf,smb_rcls) != 0)
516 if (! *pass &&
517 ((CVAL(inbuf,smb_rcls) == ERRDOS &&
518 SVAL(inbuf,smb_err) == ERRnoaccess) ||
519 (CVAL(inbuf,smb_rcls) == ERRSRV &&
520 SVAL(inbuf,smb_err) == ERRbadpw)))
522 got_pass = False;
523 DEBUG(5,("resending login\n"));
524 goto get_pass;
527 DEBUG(0,("Session setup failed for username=%s myname=%s destname=%s %s\n",
528 username,myname,desthost,smb_errstr(inbuf)));
529 DEBUG(0,("You might find the -U or -n options useful\n"));
530 DEBUG(0,("Sometimes you have to use `-n USERNAME' (particularly with OS/2)\n"));
531 DEBUG(0,("Some servers also insist on uppercase-only passwords\n"));
532 if (was_null)
534 free(inbuf);
535 free(outbuf);
537 return(False);
540 if (Protocol >= PROTOCOL_NT1) {
541 char *domain,*os,*lanman;
542 p = smb_buf(inbuf);
543 os = p;
544 lanman = skip_string(os,1);
545 domain = skip_string(lanman,1);
546 if (*domain || *os || *lanman)
547 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",domain,os,lanman));
550 /* use the returned uid from now on */
551 if (SVAL(inbuf,smb_uid) != uid)
552 DEBUG(5,("Server gave us a UID of %d. We gave %d\n",
553 SVAL(inbuf,smb_uid),uid));
554 uid = SVAL(inbuf,smb_uid);
557 /* now we've got a connection - send a tcon message */
558 bzero(outbuf,smb_size);
560 if (strncmp(service,"\\\\",2) != 0)
562 DEBUG(0,("\nWarning: Your service name doesn't start with \\\\. This is probably incorrect.\n"));
563 DEBUG(0,("Perhaps try replacing each \\ with \\\\ on the command line?\n\n"));
567 again2:
570 int passlen = strlen(pass)+1;
571 fstring pword;
572 fstrcpy(pword,pass);
574 if (doencrypt && *pass) {
575 passlen=24;
576 SMBencrypt((uchar *)pass,(uchar *)cryptkey,(uchar *)pword);
579 /* if in user level security then don't send a password now */
580 if ((sec_mode & 1)) {
581 fstrcpy(pword, ""); passlen=1;
584 set_message(outbuf,4,2 + strlen(service) + passlen + strlen(dev),True);
585 CVAL(outbuf,smb_com) = SMBtconX;
586 cli_setup_pkt(outbuf);
588 SSVAL(outbuf,smb_vwv0,0xFF);
589 SSVAL(outbuf,smb_vwv3,passlen);
591 p = smb_buf(outbuf);
592 memcpy(p,pword,passlen);
593 p += passlen;
594 strcpy(p,service);
595 p = skip_string(p,1);
596 strcpy(p,dev);
599 send_smb(Client,outbuf);
600 receive_smb(Client,inbuf,CLIENT_TIMEOUT);
602 /* trying again with a blank password */
603 if (CVAL(inbuf,smb_rcls) != 0 &&
604 (int)strlen(pass) > 0 &&
605 !doencrypt &&
606 Protocol >= PROTOCOL_LANMAN1)
608 DEBUG(2,("first SMBtconX failed, trying again. %s\n",smb_errstr(inbuf)));
609 strcpy(pass,"");
610 goto again2;
613 if (CVAL(inbuf,smb_rcls) != 0)
615 DEBUG(0,("SMBtconX failed. %s\n",smb_errstr(inbuf)));
616 DEBUG(0,("Perhaps you are using the wrong sharename, username or password?\n"));
617 DEBUG(0,("Some servers insist that these be in uppercase\n"));
618 if (was_null)
620 free(inbuf);
621 free(outbuf);
623 return(False);
627 max_xmit = MIN(max_xmit,BUFFER_SIZE-4);
628 if (max_xmit <= 0)
629 max_xmit = BUFFER_SIZE - 4;
631 cnum = SVAL(inbuf,smb_tid);
633 DEBUG(5,("Connected with cnum=%d max_xmit=%d\n",cnum,max_xmit));
635 if (was_null)
637 free(inbuf);
638 free(outbuf);
640 return True;
644 /****************************************************************************
645 send a logout command
646 ****************************************************************************/
647 void cli_send_logout(void)
649 pstring inbuf,outbuf;
651 bzero(outbuf,smb_size);
652 set_message(outbuf,0,0,True);
653 CVAL(outbuf,smb_com) = SMBtdis;
654 SSVAL(outbuf,smb_tid,cnum);
655 cli_setup_pkt(outbuf);
657 send_smb(Client,outbuf);
658 receive_smb(Client,inbuf,SHORT_TIMEOUT);
660 if (CVAL(inbuf,smb_rcls) != 0)
662 DEBUG(0,("SMBtdis failed %s\n",smb_errstr(inbuf)));
666 #ifdef STATS
667 stats_report();
668 #endif
669 exit(0);
673 /****************************************************************************
674 send a SMB trans or trans2 request
675 ****************************************************************************/
676 static BOOL cli_send_trans_request(char *outbuf, int trans, char *name, int fid, int flags,
677 char *data,char *param,uint16 *setup, int ldata,int lparam,
678 int lsetup,int mdata,int mparam,int msetup)
680 int i;
681 int this_ldata,this_lparam;
682 int tot_data=0,tot_param=0;
683 char *outdata,*outparam;
684 pstring inbuf;
685 char *p;
687 this_lparam = MIN(lparam,max_xmit - (500+lsetup*SIZEOFWORD)); /* hack */
688 this_ldata = MIN(ldata,max_xmit - (500+lsetup*SIZEOFWORD+this_lparam));
690 bzero(outbuf,smb_size);
691 set_message(outbuf,14+lsetup,0,True);
692 CVAL(outbuf,smb_com) = trans;
693 SSVAL(outbuf,smb_tid,cnum);
694 cli_setup_pkt(outbuf);
696 outparam = smb_buf(outbuf)+(trans==SMBtrans ? strlen(name)+1 : 3);
697 outdata = outparam+this_lparam;
699 /* primary request */
700 SSVAL(outbuf,smb_tpscnt,lparam); /* tpscnt */
701 SSVAL(outbuf,smb_tdscnt,ldata); /* tdscnt */
702 SSVAL(outbuf,smb_mprcnt,mparam); /* mprcnt */
703 SSVAL(outbuf,smb_mdrcnt,mdata); /* mdrcnt */
704 SCVAL(outbuf,smb_msrcnt,msetup); /* msrcnt */
705 SSVAL(outbuf,smb_flags,flags); /* flags */
706 SIVAL(outbuf,smb_timeout,0); /* timeout */
707 SSVAL(outbuf,smb_pscnt,this_lparam); /* pscnt */
708 SSVAL(outbuf,smb_psoff,smb_offset(outparam,outbuf)); /* psoff */
709 SSVAL(outbuf,smb_dscnt,this_ldata); /* dscnt */
710 SSVAL(outbuf,smb_dsoff,smb_offset(outdata,outbuf)); /* dsoff */
711 SCVAL(outbuf,smb_suwcnt,lsetup); /* suwcnt */
712 for (i=0;i<lsetup;i++) /* setup[] */
713 SSVAL(outbuf,smb_setup+i*SIZEOFWORD,setup[i]);
714 p = smb_buf(outbuf);
715 if (trans==SMBtrans)
716 strcpy(p,name); /* name[] */
717 else
719 *p++ = 0; /* put in a null smb_name */
720 *p++ = 'D'; *p++ = ' '; /* this was added because OS/2 does it */
722 if (this_lparam) /* param[] */
723 memcpy(outparam,param,this_lparam);
724 if (this_ldata) /* data[] */
725 memcpy(outdata,data,this_ldata);
726 set_message(outbuf,14+lsetup, /* wcnt, bcc */
727 PTR_DIFF(outdata+this_ldata,smb_buf(outbuf)),False);
729 show_msg(outbuf);
730 send_smb(Client,outbuf);
732 if (this_ldata < ldata || this_lparam < lparam)
734 /* receive interim response */
735 if (!receive_smb(Client,inbuf,SHORT_TIMEOUT) || CVAL(inbuf,smb_rcls) != 0)
737 DEBUG(0,("%s request failed (%s)\n",
738 trans==SMBtrans?"SMBtrans":"SMBtrans2", smb_errstr(inbuf)));
739 return(False);
742 tot_data = this_ldata;
743 tot_param = this_lparam;
745 while (tot_data < ldata || tot_param < lparam)
747 this_lparam = MIN(lparam-tot_param,max_xmit - 500); /* hack */
748 this_ldata = MIN(ldata-tot_data,max_xmit - (500+this_lparam));
750 set_message(outbuf,trans==SMBtrans?8:9,0,True);
751 CVAL(outbuf,smb_com) = trans==SMBtrans ? SMBtranss : SMBtranss2;
753 outparam = smb_buf(outbuf);
754 outdata = outparam+this_lparam;
756 /* secondary request */
757 SSVAL(outbuf,smb_tpscnt,lparam); /* tpscnt */
758 SSVAL(outbuf,smb_tdscnt,ldata); /* tdscnt */
759 SSVAL(outbuf,smb_spscnt,this_lparam); /* pscnt */
760 SSVAL(outbuf,smb_spsoff,smb_offset(outparam,outbuf)); /* psoff */
761 SSVAL(outbuf,smb_spsdisp,tot_param); /* psdisp */
762 SSVAL(outbuf,smb_sdscnt,this_ldata); /* dscnt */
763 SSVAL(outbuf,smb_sdsoff,smb_offset(outdata,outbuf)); /* dsoff */
764 SSVAL(outbuf,smb_sdsdisp,tot_data); /* dsdisp */
765 if (trans==SMBtrans2)
766 SSVAL(outbuf,smb_sfid,fid); /* fid */
767 if (this_lparam) /* param[] */
768 memcpy(outparam,param,this_lparam);
769 if (this_ldata) /* data[] */
770 memcpy(outdata,data,this_ldata);
771 set_message(outbuf,trans==SMBtrans?8:9, /* wcnt, bcc */
772 PTR_DIFF(outdata+this_ldata,smb_buf(outbuf)),False);
774 show_msg(outbuf);
775 send_smb(Client,outbuf);
777 tot_data += this_ldata;
778 tot_param += this_lparam;
782 return(True);
787 /****************************************************************************
788 call a remote api
789 ****************************************************************************/
790 BOOL cli_call_api(int prcnt,int drcnt,int mprcnt,int mdrcnt,int *rprcnt,
791 int *rdrcnt, char *param,char *data,
792 char **rparam, char **rdata)
794 static char *inbuf=NULL;
795 static char *outbuf=NULL;
797 if (!inbuf) inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
798 if (!outbuf) outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
800 cli_send_trans_request(outbuf,SMBtrans,"\\PIPE\\LANMAN",0,0,
801 data,param,NULL,
802 drcnt,prcnt,0,
803 mdrcnt,mprcnt,0);
805 return (cli_receive_trans_response(inbuf,SMBtrans,
806 rdrcnt,rprcnt,
807 rdata,rparam));
811 /****************************************************************************
812 open the client sockets
813 ****************************************************************************/
814 BOOL cli_open_sockets(int port)
816 static int last_port;
817 char *host;
818 pstring service2;
819 extern int Client;
821 if (port == 0) port=last_port;
822 last_port=port;
824 strupper(service);
826 if (*desthost)
828 host = desthost;
830 else
832 pstrcpy(service2,service);
833 host = strtok(service2,"\\/");
834 pstrcpy(desthost,host);
837 DEBUG(5,("Opening sockets\n"));
839 if (*myname == 0)
840 get_myname(myname,NULL);
841 strupper(myname);
843 if (!have_ip)
845 struct hostent *hp;
847 if ((hp = Get_Hostbyname(host)) == 0)
849 DEBUG(0,("Get_Hostbyname: Unknown host %s.\n",host));
850 return False;
853 putip((char *)&dest_ip,(char *)hp->h_addr);
856 Client = open_socket_out(SOCK_STREAM, &dest_ip, port, SHORT_CONNECT_TIMEOUT);
857 if (Client == -1)
858 return False;
860 DEBUG(5,("Connected\n"));
862 set_socket_options(Client,user_socket_options);
864 return True;
867 /****************************************************************************
868 close and open the connection again
869 ****************************************************************************/
870 BOOL cli_reopen_connection(char *inbuf,char *outbuf)
872 static int open_count=0;
874 open_count++;
876 if (open_count>5) return(False);
878 DEBUG(1,("Trying to re-open connection\n"));
880 set_message(outbuf,0,0,True);
881 SCVAL(outbuf,smb_com,SMBtdis);
882 SSVAL(outbuf,smb_tid,cnum);
883 cli_setup_pkt(outbuf);
885 send_smb(Client,outbuf);
886 receive_smb(Client,inbuf,SHORT_TIMEOUT);
888 close_sockets();
889 if (!cli_open_sockets(0)) return(False);
891 return(cli_send_login(inbuf,outbuf,True,True));
894 /* error code stuff - put together by Merik Karman
895 merik@blackadder.dsh.oz.au */
897 typedef struct
899 char *name;
900 int code;
901 char *message;
902 } err_code_struct;
904 /* Dos Error Messages */
905 err_code_struct dos_msgs[] = {
906 {"ERRbadfunc",1,"Invalid function."},
907 {"ERRbadfile",2,"File not found."},
908 {"ERRbadpath",3,"Directory invalid."},
909 {"ERRnofids",4,"No file descriptors available"},
910 {"ERRnoaccess",5,"Access denied."},
911 {"ERRbadfid",6,"Invalid file handle."},
912 {"ERRbadmcb",7,"Memory control blocks destroyed."},
913 {"ERRnomem",8,"Insufficient server memory to perform the requested function."},
914 {"ERRbadmem",9,"Invalid memory block address."},
915 {"ERRbadenv",10,"Invalid environment."},
916 {"ERRbadformat",11,"Invalid format."},
917 {"ERRbadaccess",12,"Invalid open mode."},
918 {"ERRbaddata",13,"Invalid data."},
919 {"ERR",14,"reserved."},
920 {"ERRbaddrive",15,"Invalid drive specified."},
921 {"ERRremcd",16,"A Delete Directory request attempted to remove the server's current directory."},
922 {"ERRdiffdevice",17,"Not same device."},
923 {"ERRnofiles",18,"A File Search command can find no more files matching the specified criteria."},
924 {"ERRbadshare",32,"The sharing mode specified for an Open conflicts with existing FIDs on the file."},
925 {"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."},
926 {"ERRfilexists",80,"The file named in a Create Directory, Make New File or Link request already exists."},
927 {"ERRbadpipe",230,"Pipe invalid."},
928 {"ERRpipebusy",231,"All instances of the requested pipe are busy."},
929 {"ERRpipeclosing",232,"Pipe close in progress."},
930 {"ERRnotconnected",233,"No process on other end of pipe."},
931 {"ERRmoredata",234,"There is more data to be returned."},
932 {NULL,-1,NULL}};
934 /* Server Error Messages */
935 err_code_struct server_msgs[] = {
936 {"ERRerror",1,"Non-specific error code."},
937 {"ERRbadpw",2,"Bad password - name/password pair in a Tree Connect or Session Setup are invalid."},
938 {"ERRbadtype",3,"reserved."},
939 {"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."},
940 {"ERRinvnid",5,"The tree ID (TID) specified in a command was invalid."},
941 {"ERRinvnetname",6,"Invalid network name in tree connect."},
942 {"ERRinvdevice",7,"Invalid device - printer request made to non-printer connection or non-printer request made to printer connection."},
943 {"ERRqfull",49,"Print queue full (files) -- returned by open print file."},
944 {"ERRqtoobig",50,"Print queue full -- no space."},
945 {"ERRqeof",51,"EOF on print queue dump."},
946 {"ERRinvpfid",52,"Invalid print file FID."},
947 {"ERRsmbcmd",64,"The server did not recognize the command received."},
948 {"ERRsrverror",65,"The server encountered an internal error, e.g., system file unavailable."},
949 {"ERRfilespecs",67,"The file handle (FID) and pathname parameters contained an invalid combination of values."},
950 {"ERRreserved",68,"reserved."},
951 {"ERRbadpermits",69,"The access permissions specified for a file or directory are not a valid combination. The server cannot set the requested attribute."},
952 {"ERRreserved",70,"reserved."},
953 {"ERRsetattrmode",71,"The attribute mode in the Set File Attribute request is invalid."},
954 {"ERRpaused",81,"Server is paused."},
955 {"ERRmsgoff",82,"Not receiving messages."},
956 {"ERRnoroom",83,"No room to buffer message."},
957 {"ERRrmuns",87,"Too many remote user names."},
958 {"ERRtimeout",88,"Operation timed out."},
959 {"ERRnoresource",89,"No resources currently available for request."},
960 {"ERRtoomanyuids",90,"Too many UIDs active on this session."},
961 {"ERRbaduid",91,"The UID is not known as a valid ID on this session."},
962 {"ERRusempx",250,"Temp unable to support Raw, use MPX mode."},
963 {"ERRusestd",251,"Temp unable to support Raw, use standard read/write."},
964 {"ERRcontmpx",252,"Continue in MPX mode."},
965 {"ERRreserved",253,"reserved."},
966 {"ERRreserved",254,"reserved."},
967 {"ERRnosupport",0xFFFF,"Function not supported."},
968 {NULL,-1,NULL}};
970 /* Hard Error Messages */
971 err_code_struct hard_msgs[] = {
972 {"ERRnowrite",19,"Attempt to write on write-protected diskette."},
973 {"ERRbadunit",20,"Unknown unit."},
974 {"ERRnotready",21,"Drive not ready."},
975 {"ERRbadcmd",22,"Unknown command."},
976 {"ERRdata",23,"Data error (CRC)."},
977 {"ERRbadreq",24,"Bad request structure length."},
978 {"ERRseek",25 ,"Seek error."},
979 {"ERRbadmedia",26,"Unknown media type."},
980 {"ERRbadsector",27,"Sector not found."},
981 {"ERRnopaper",28,"Printer out of paper."},
982 {"ERRwrite",29,"Write fault."},
983 {"ERRread",30,"Read fault."},
984 {"ERRgeneral",31,"General failure."},
985 {"ERRbadshare",32,"A open conflicts with an existing open."},
986 {"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."},
987 {"ERRwrongdisk",34,"The wrong disk was found in a drive."},
988 {"ERRFCBUnavail",35,"No FCBs are available to process request."},
989 {"ERRsharebufexc",36,"A sharing buffer has been exceeded."},
990 {NULL,-1,NULL}};
993 struct
995 int code;
996 char *class;
997 err_code_struct *err_msgs;
998 } err_classes[] = {
999 {0,"SUCCESS",NULL},
1000 {0x01,"ERRDOS",dos_msgs},
1001 {0x02,"ERRSRV",server_msgs},
1002 {0x03,"ERRHRD",hard_msgs},
1003 {0x04,"ERRXOS",NULL},
1004 {0xE1,"ERRRMX1",NULL},
1005 {0xE2,"ERRRMX2",NULL},
1006 {0xE3,"ERRRMX3",NULL},
1007 {0xFF,"ERRCMD",NULL},
1008 {-1,NULL,NULL}};
1011 /****************************************************************************
1012 return a SMB error string from a SMB buffer
1013 ****************************************************************************/
1014 char *smb_errstr(char *inbuf)
1016 static pstring ret;
1017 int class = CVAL(inbuf,smb_rcls);
1018 int num = SVAL(inbuf,smb_err);
1019 int i,j;
1021 for (i=0;err_classes[i].class;i++)
1022 if (err_classes[i].code == class)
1024 if (err_classes[i].err_msgs)
1026 err_code_struct *err = err_classes[i].err_msgs;
1027 for (j=0;err[j].name;j++)
1028 if (num == err[j].code)
1030 if (DEBUGLEVEL > 0)
1031 sprintf(ret,"%s - %s (%s)",err_classes[i].class,
1032 err[j].name,err[j].message);
1033 else
1034 sprintf(ret,"%s - %s",err_classes[i].class,err[j].name);
1035 return ret;
1039 sprintf(ret,"%s - %d",err_classes[i].class,num);
1040 return ret;
1043 sprintf(ret,"ERROR: Unknown error (%d,%d)",class,num);
1044 return(ret);