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.
26 #include <linux/version.h>
27 #define LVERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
28 #if LINUX_VERSION_CODE < LVERSION(2,1,70)
29 #error this code will only compile on versions of linux after 2.1.70
33 #include <linux/smb_fs.h>
34 static struct smb_conn_opt conn_options;
40 /* Uncomment this to allow debug the smbmount daemon */
43 pstring cur_dir = "\\";
45 extern pstring service;
46 extern pstring desthost;
47 extern pstring myname;
48 extern pstring myhostname;
49 extern pstring password;
50 extern pstring username;
51 extern pstring workgroup;
54 extern BOOL connect_as_printer;
55 extern BOOL connect_as_ipc;
56 extern struct in_addr ipzero;
58 extern BOOL doencrypt;
60 extern pstring user_socket_options;
62 /* 30 second timeout on most commands */
63 #define CLIENT_TIMEOUT (30*1000)
64 #define SHORT_TIMEOUT (5*1000)
66 /* value for unused fid field in trans2 secondary request */
67 #define FID_UNUSED (0xFFFF)
71 extern int max_protocol;
75 time_t newer_than = 0;
76 int archive_level = 0;
78 extern pstring debugf;
79 extern int DEBUGLEVEL;
81 BOOL translation = False;
93 /* clitar bits insert */
96 extern BOOL tar_reset;
102 extern pstring scope;
108 BOOL recurse = False;
109 BOOL lowercase = False;
111 struct in_addr dest_ip;
113 #define SEPARATORS " \t\n\r"
115 BOOL abort_mget = True;
119 extern BOOL readbraw_supported ;
120 extern BOOL writebraw_supported;
122 pstring fileselection = "";
124 extern file_info def_finfo;
127 int get_total_size = 0;
128 int get_total_time_ms = 0;
129 int put_total_size = 0;
130 int put_total_time_ms = 0;
139 #define CNV_LANG(s) dos_to_unix(s,False)
140 #define CNV_INPUT(s) unix_to_dos(s,True)
142 /****************************************************************************
143 check for existance of a dir
144 ****************************************************************************/
145 static BOOL chkpath(char *path,BOOL report)
148 pstring inbuf,outbuf;
152 trim_string(path2,NULL,"\\");
153 if (!*path2) *path2 = '\\';
155 bzero(outbuf,smb_size);
156 set_message(outbuf,0,4 + strlen(path2),True);
157 SCVAL(outbuf,smb_com,SMBchkpth);
158 SSVAL(outbuf,smb_tid,cnum);
159 cli_setup_pkt(outbuf);
167 /* this little bit of code can be used to extract NT error codes.
168 Just feed a bunch of "cd foo" commands to smbclient then watch
169 in netmon (tridge) */
171 SIVAL(outbuf, smb_rcls, code | 0xC0000000);
172 SSVAL(outbuf, smb_flg2, SVAL(outbuf, smb_flg2) | (1<<14));
177 send_smb(Client,outbuf);
178 client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
180 if (report && CVAL(inbuf,smb_rcls) != 0)
181 DEBUG(2,("chkpath: %s\n",smb_errstr(inbuf)));
183 return(CVAL(inbuf,smb_rcls) == 0);
190 if ((i = fork()) < 0)
192 DEBUG(0, ("could not fork\n"));
196 /* parent simply exits */
204 close_our_files(void)
207 for (i = 0; i < NR_OPEN; i++) {
222 * Send a login and store the connection options. This is a separate
223 * function to keep clientutil.c independent of linux kernel changes.
225 static BOOL mount_send_login(char *inbuf, char *outbuf)
227 struct connection_options opt;
228 int res = cli_send_login(inbuf, outbuf, True, True, &opt);
233 conn_options.protocol = opt.protocol;
234 conn_options.case_handling = CASE_LOWER;
235 conn_options.max_xmit = opt.max_xmit;
236 conn_options.server_uid = opt.server_uid;
237 conn_options.tid = opt.tid;
238 conn_options.secmode = opt.sec_mode;
239 conn_options.maxmux = opt.max_mux;
240 conn_options.maxvcs = opt.max_vcs;
241 conn_options.rawmode = opt.rawmode;
242 conn_options.sesskey = opt.sesskey;
243 conn_options.maxraw = opt.maxraw;
244 conn_options.capabilities = opt.capabilities;
245 conn_options.serverzone = opt.serverzone;
251 * Call the smbfs ioctl to install a connection socket,
252 * then wait for a signal to reconnect. Note that we do
253 * not exit after open_sockets() or send_login() errors,
254 * as the smbfs mount would then have no way to recover.
257 send_fs_socket(char *mount_point, char *inbuf, char *outbuf)
259 int fd, closed = 0, res = 1;
263 if ((fd = open(mount_point, O_RDONLY)) < 0)
265 DEBUG(0, ("smbmount: can't open %s\n", mount_point));
270 * Call the ioctl even if we couldn't get a socket ...
271 * there's no point in making smbfs wait for a timeout.
273 conn_options.fd = -1;
275 conn_options.fd = Client;
276 res = ioctl(fd, SMB_IOC_NEWCONN, &conn_options);
279 DEBUG(0, ("smbmount: ioctl failed, res=%d\n", res));
285 * Close all open files if we haven't done so yet.
296 * Wait for a signal from smbfs ...
298 signal(SIGUSR1, &usr1_handler);
300 DEBUG(0, ("smbmount: got signal, getting new socket\n"));
302 res = cli_open_sockets(port);
305 DEBUG(0, ("smbmount: can't open sockets\n"));
309 res = mount_send_login(inbuf, outbuf);
312 DEBUG(0, ("smbmount: login failed\n"));
315 DEBUG(0, ("smbmount: exit\n"));
319 /****************************************************************************
321 ****************************************************************************/
322 static void cmd_mount(char *inbuf,char *outbuf)
326 pstring mount_command;
329 char mount_point[MAXPATHLEN+1];
331 if (!next_token(NULL, mpoint, NULL))
333 DEBUG(0,("You must supply a mount point\n"));
337 memset(mount_point, 0, sizeof(mount_point));
339 if (realpath(mpoint, mount_point) == NULL)
341 DEBUG(0, ("Could not resolve mount point\n"));
346 * Build the service name to report on the Unix side,
347 * converting '\' to '/' and ' ' to '_'.
349 strcpy(share_name, service);
350 string_replace(share_name, '\\', '/');
351 string_replace(share_name, ' ', '_');
353 sprintf(mount_command, "smbmnt %s -s %s", mount_point, share_name);
355 while(next_token(NULL, buf, NULL))
357 strcat(mount_command, " ");
358 strcat(mount_command, buf);
361 DEBUG(3, ("mount command: %s\n", mount_command));
364 * Create the background process before trying the mount.
365 * (We delay closing files to allow diagnostic messages.)
369 /* The parent has exited here, the child handles the connection: */
370 if ((retval = system(mount_command)) != 0)
372 DEBUG(0,("mount failed\n"));
375 send_fs_socket(mount_point, inbuf, outbuf);
379 /* This defines the commands supported by this client */
387 {"mount", cmd_mount, "<mount-point options> mount an smbfs file system"},
392 /*******************************************************************
393 lookup a command string in the list of commands, including
395 ******************************************************************/
396 static int process_tok(fstring tok)
398 int i = 0, matches = 0;
400 int tok_len = strlen(tok);
402 while (commands[i].fn != NULL)
404 if (strequal(commands[i].name,tok))
410 else if (strnequal(commands[i].name, tok, tok_len))
420 else if (matches == 1)
426 /****************************************************************************
428 ****************************************************************************/
434 if (next_token(NULL,buf,NULL))
436 if ((i = process_tok(buf)) >= 0)
437 DEBUG(0,("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description));
440 while (commands[i].description)
442 for (j=0; commands[i].description && (j<5); j++) {
443 DEBUG(0,("%-15s",commands[i].name));
450 /****************************************************************************
451 wait for keyboard activity, swallowing network packets
452 ****************************************************************************/
454 static char wait_keyboard(char *buffer)
456 static void wait_keyboard(char *buffer)
461 struct timeval timeout;
473 FD_SET(fileno(stdin),&fds);
481 selrtn = sys_select(&fds,&timeout);
484 if (FD_ISSET(fileno(stdin),&fds))
491 set_blocking(fileno(stdin), False);
492 readret = read_data( fileno(stdin), &ch, 1);
493 set_blocking(fileno(stdin), True);
498 /* should crash here */
499 DEBUG(1,("readchar stdin failed\n"));
502 else if (readret != 0)
509 /* We deliberately use receive_smb instead of
510 client_receive_smb as we want to receive
511 session keepalives and then drop them here.
513 if (FD_ISSET(Client,&fds))
514 receive_smb(Client,buffer,0);
530 /****************************************************************************
531 process commands from the client
532 ****************************************************************************/
533 static BOOL process(char *base_directory)
539 char *InBuffer = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
540 char *OutBuffer = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
542 if ((InBuffer == NULL) || (OutBuffer == NULL))
545 bzero(OutBuffer,smb_size);
547 if (!mount_send_login(InBuffer,OutBuffer))
551 if (cmd[0] != '\0') while (cmd[0] != '\0')
557 if ((p = strchr(cmd, ';')) == 0)
559 strncpy(line, cmd, 999);
565 if (p - cmd > 999) p = cmd + 999;
566 strncpy(line, cmd, p - cmd);
567 line[p - cmd] = '\0';
571 /* input language code to internal one */
574 /* and get the first part of the command */
577 if (!next_token(&ptr,tok,NULL)) continue;
580 if ((i = process_tok(tok)) >= 0)
581 commands[i].fn(InBuffer,OutBuffer);
583 DEBUG(0,("%s: command abbreviation ambiguous\n",CNV_LANG(tok)));
585 DEBUG(0,("%s: command not found\n",CNV_LANG(tok)));
587 else while (!feof(stdin))
592 bzero(OutBuffer,smb_size);
594 /* display a prompt */
595 DEBUG(0,("smb: %s> ", CNV_LANG(cur_dir)));
599 line[0] = wait_keyboard(InBuffer);
600 /* this might not be such a good idea... */
604 wait_keyboard(InBuffer);
607 /* and get a response */
609 fgets( &line[1],999, stdin);
611 if (!fgets(line,1000,stdin))
615 /* input language code to internal one */
618 /* special case - first char is ! */
625 /* and get the first part of the command */
628 if (!next_token(&ptr,tok,NULL)) continue;
631 if ((i = process_tok(tok)) >= 0)
632 commands[i].fn(InBuffer,OutBuffer);
634 DEBUG(0,("%s: command abbreviation ambiguous\n",CNV_LANG(tok)));
636 DEBUG(0,("%s: command not found\n",CNV_LANG(tok)));
643 /****************************************************************************
645 ****************************************************************************/
646 static void usage(char *pname)
648 DEBUG(0,("Usage: %s service <password> [-p port] [-d debuglevel] [-l log] ",
651 DEBUG(0,("\nVersion %s\n",VERSION));
652 DEBUG(0,("\t-p port connect to the specified port\n"));
653 DEBUG(0,("\t-d debuglevel set the debuglevel\n"));
654 DEBUG(0,("\t-l log basename. Basename for log/debug files\n"));
655 DEBUG(0,("\t-n netbios name. Use this name as my netbios name\n"));
656 DEBUG(0,("\t-N don't ask for a password\n"));
657 DEBUG(0,("\t-m max protocol set the max protocol level\n"));
658 DEBUG(0,("\t-I dest IP use this IP to connect to\n"));
659 DEBUG(0,("\t-E write messages to stderr instead of stdout\n"));
660 DEBUG(0,("\t-U username set the network username\n"));
661 DEBUG(0,("\t-W workgroup set the workgroup name\n"));
662 DEBUG(0,("\t-c command string execute semicolon separated commands\n"));
663 DEBUG(0,("\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n"));
664 DEBUG(0,("\t-D directory start from directory\n"));
668 /****************************************************************************
670 ****************************************************************************/
671 int main(int argc,char *argv[])
673 fstring base_directory;
674 char *pname = argv[0];
680 BOOL nt_domain_logon = False;
681 static pstring servicesf = CONFIGFILE;
686 strcpy(term_code, KANJI);
696 setup_logging(pname,True);
699 charset_initialise();
710 strcpy(username,getenv("USER"));
712 /* modification to support userid%passwd syntax in the USER var
713 25.Aug.97, jdblair@uab.edu */
715 if ((p=strchr(username,'%')))
718 strcpy(password,p+1);
720 memset(strchr(getenv("USER"),'%')+1,'X',strlen(password));
725 /* modification to support PASSWD environmental var
726 25.Aug.97, jdblair@uab.edu */
728 if (getenv("PASSWD"))
729 strcpy(password,getenv("PASSWD"));
731 if (*username == 0 && getenv("LOGNAME"))
733 strcpy(username,getenv("LOGNAME"));
746 strcpy(service, argv[1]);
747 /* Convert any '/' characters in the service name to '\' characters */
748 string_replace( service, '/','\\');
752 if (count_chars(service,'\\') < 3)
755 printf("\n%s: Not enough '\\' characters in service\n",service);
759 if (argc > 1 && (*argv[1] != '-'))
762 strcpy(password,argv[1]);
763 memset(argv[1],'X',strlen(argv[1]));
770 getopt(argc, argv,"s:B:O:M:S:i:Nn:d:Pp:l:hI:EB:U:L:t:m:W:T:D:c:")) != EOF)
774 max_protocol = interpret_protocol(optarg,max_protocol);
777 strcpy(user_socket_options,optarg);
780 strcpy(desthost,optarg);
782 nt_domain_logon = True;
785 iface_set_default(NULL,optarg,NULL);
788 strcpy(base_directory,optarg);
791 strcpy(scope,optarg);
796 strcpy(username,optarg);
797 if ((lp=strchr(username,'%')))
800 strcpy(password,lp+1);
802 memset(strchr(optarg,'%')+1,'X',strlen(password));
808 strcpy(workgroup,optarg);
815 dest_ip = *interpret_addr2(optarg);
816 if (zero_ip(dest_ip)) exit(1);
821 strcpy(myname,optarg);
830 DEBUGLEVEL = atoi(optarg);
833 sprintf(debugf,"%s.client",optarg);
847 strcpy(servicesf, optarg);
850 strcpy(term_code, optarg);
857 if (!*query_host && !*service)
864 DEBUG(3,("%s client started (version %s)\n",timestring(),VERSION));
866 if(!get_myname(myhostname,NULL))
868 DEBUG(0,("Failed to get my hostname.\n"));
871 if (!lp_load(servicesf,True)) {
872 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
875 codepage_initialise(lp_client_code_page());
877 interpret_coding_system(term_code);
880 strcpy(workgroup,lp_workgroup());
883 get_myname((*myname)?NULL:myname,NULL);
891 sprintf(service,"\\\\%s\\IPC$",query_host);
893 connect_as_ipc = True;
895 DEBUG(5,("NT Domain Logon. Service: %s\n", service));
897 if (cli_open_sockets(port))
899 if (!cli_send_login(NULL,NULL,True,True,NULL)) return(1);
901 do_nt_login(desthost, myhostname, Client, cnum);
911 if (cli_open_sockets(port))
913 if (!process(base_directory))