2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1999
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <asm/types.h>
27 #include <linux/smb_fs.h>
29 extern BOOL in_client
;
30 extern pstring user_socket_options
;
32 static pstring credentials
;
33 static pstring my_netbios_name
;
34 static pstring password
;
35 static pstring username
;
36 static pstring workgroup
;
37 static pstring mpoint
;
38 static pstring service
;
39 static pstring options
;
41 static struct in_addr dest_ip
;
43 static int smb_port
= 0;
46 static uid_t mount_uid
;
47 static gid_t mount_gid
;
49 static unsigned mount_fmask
;
50 static unsigned mount_dmask
;
51 static BOOL use_kerberos
;
52 /* TODO: Add code to detect smbfs version in kernel */
53 static BOOL status32_smbfs
= False
;
54 static BOOL smbfs_has_unicode
= False
;
55 static BOOL smbfs_has_lfs
= False
;
57 static void usage(void);
59 static void exit_parent(int sig
)
61 /* parent simply exits when child says go... */
65 static void daemonize(void)
70 signal( SIGTERM
, exit_parent
);
72 if ((child_pid
= sys_fork()) < 0) {
73 DEBUG(0,("could not fork\n"));
78 j
= waitpid( child_pid
, &status
, 0 );
80 if( EINTR
== errno
) {
88 /* If we get here - the child exited with some error status */
89 if (WIFSIGNALED(status
))
90 exit(128 + WTERMSIG(status
));
92 exit(WEXITSTATUS(status
));
95 signal( SIGTERM
, SIG_DFL
);
99 static void close_our_files(int client_fd
)
102 struct rlimit limits
;
104 getrlimit(RLIMIT_NOFILE
,&limits
);
105 for (i
= 0; i
< limits
.rlim_max
; i
++) {
112 static void usr1_handler(int x
)
118 /*****************************************************
119 return a connection to a server
120 *******************************************************/
121 static struct cli_state
*do_connection(char *the_service
)
124 struct nmb_name called
, calling
;
130 if (the_service
[0] != '\\' || the_service
[1] != '\\') {
135 pstrcpy(server
, the_service
+2);
136 share
= strchr_m(server
,'\\');
146 make_nmb_name(&calling
, my_netbios_name
, 0x0);
147 make_nmb_name(&called
, server
, 0x20);
151 if (have_ip
) ip
= dest_ip
;
153 /* have to open a new connection */
154 if (!(c
=cli_initialise(NULL
)) || (cli_set_port(c
, smb_port
) != smb_port
) ||
155 !cli_connect(c
, server_n
, &ip
)) {
156 DEBUG(0,("%d: Connection to %s failed\n", sys_getpid(), server_n
));
163 /* SPNEGO doesn't work till we get NTSTATUS error support */
164 /* But it is REQUIRED for kerberos authentication */
165 if(!use_kerberos
) c
->use_spnego
= False
;
167 /* The kernel doesn't yet know how to sign it's packets */
168 c
->sign_info
.allow_smb_signing
= False
;
170 /* Use kerberos authentication if specified */
171 c
->use_kerberos
= use_kerberos
;
173 if (!cli_session_request(c
, &calling
, &called
)) {
175 DEBUG(0,("%d: session request to %s failed (%s)\n",
176 sys_getpid(), called
.name
, cli_errstr(c
)));
178 if ((p
=strchr_m(called
.name
, '.'))) {
182 if (strcmp(called
.name
, "*SMBSERVER")) {
183 make_nmb_name(&called
, "*SMBSERVER", 0x20);
189 DEBUG(4,("%d: session request ok\n", sys_getpid()));
191 if (!cli_negprot(c
)) {
192 DEBUG(0,("%d: protocol negotiation failed\n", sys_getpid()));
198 char *pass
= getpass("Password: ");
200 pstrcpy(password
, pass
);
204 /* This should be right for current smbfs. Future versions will support
205 large files as well as unicode and oplocks. */
206 c
->capabilities
&= ~(CAP_NT_SMBS
| CAP_NT_FIND
| CAP_LEVEL_II_OPLOCKS
);
208 c
->capabilities
&= ~CAP_LARGE_FILES
;
209 if (!smbfs_has_unicode
)
210 c
->capabilities
&= ~CAP_UNICODE
;
211 if (!status32_smbfs
) {
212 c
->capabilities
&= ~CAP_STATUS32
;
213 c
->force_dos_errors
= True
;
216 if (!cli_session_setup(c
, username
,
217 password
, strlen(password
),
218 password
, strlen(password
),
220 /* if a password was not supplied then try again with a
222 if (password
[0] || !username
[0] ||
223 !cli_session_setup(c
, "", "", 0, "", 0, workgroup
)) {
224 DEBUG(0,("%d: session setup failed: %s\n",
225 sys_getpid(), cli_errstr(c
)));
229 DEBUG(0,("Anonymous login successful\n"));
232 DEBUG(4,("%d: session setup ok\n", sys_getpid()));
234 if (!cli_send_tconX(c
, share
, "?????",
235 password
, strlen(password
)+1)) {
236 DEBUG(0,("%d: tree connect failed: %s\n",
237 sys_getpid(), cli_errstr(c
)));
242 DEBUG(4,("%d: tconx ok\n", sys_getpid()));
250 /****************************************************************************
251 unmount smbfs (this is a bailout routine to clean up if a reconnect fails)
252 Code blatently stolen from smbumount.c
254 ****************************************************************************/
255 static void smb_umount(char *mount_point
)
263 This routine only gets called to the scene of a disaster
264 to shoot the survivors... A connection that was working
265 has now apparently failed. We have an active mount point
266 (presumably) that we need to dump. If we get errors along
267 the way - make some noise, but we are already turning out
268 the lights to exit anyways...
270 if (umount(mount_point
) != 0) {
271 DEBUG(0,("%d: Could not umount %s: %s\n",
272 sys_getpid(), mount_point
, strerror(errno
)));
276 if ((fd
= open(MOUNTED
"~", O_RDWR
|O_CREAT
|O_EXCL
, 0600)) == -1) {
277 DEBUG(0,("%d: Can't get "MOUNTED
"~ lock file", sys_getpid()));
283 if ((mtab
= setmntent(MOUNTED
, "r")) == NULL
) {
284 DEBUG(0,("%d: Can't open " MOUNTED
": %s\n",
285 sys_getpid(), strerror(errno
)));
289 #define MOUNTED_TMP MOUNTED".tmp"
291 if ((new_mtab
= setmntent(MOUNTED_TMP
, "w")) == NULL
) {
292 DEBUG(0,("%d: Can't open " MOUNTED_TMP
": %s\n",
293 sys_getpid(), strerror(errno
)));
298 while ((mnt
= getmntent(mtab
)) != NULL
) {
299 if (strcmp(mnt
->mnt_dir
, mount_point
) != 0) {
300 addmntent(new_mtab
, mnt
);
306 if (fchmod (fileno (new_mtab
), S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
) < 0) {
307 DEBUG(0,("%d: Error changing mode of %s: %s\n",
308 sys_getpid(), MOUNTED_TMP
, strerror(errno
)));
314 if (rename(MOUNTED_TMP
, MOUNTED
) < 0) {
315 DEBUG(0,("%d: Cannot rename %s to %s: %s\n",
316 sys_getpid(), MOUNTED
, MOUNTED_TMP
, strerror(errno
)));
320 if (unlink(MOUNTED
"~") == -1) {
321 DEBUG(0,("%d: Can't remove "MOUNTED
"~", sys_getpid()));
328 * Call the smbfs ioctl to install a connection socket,
329 * then wait for a signal to reconnect. Note that we do
330 * not exit after open_sockets() or send_login() errors,
331 * as the smbfs mount would then have no way to recover.
333 static void send_fs_socket(char *the_service
, char *mount_point
, struct cli_state
*c
)
335 int fd
, closed
= 0, res
= 1;
336 pid_t parentpid
= getppid();
337 struct smb_conn_opt conn_options
;
339 memset(&conn_options
, 0, sizeof(conn_options
));
342 if ((fd
= open(mount_point
, O_RDONLY
)) < 0) {
343 DEBUG(0,("mount.smbfs[%d]: can't open %s\n",
344 sys_getpid(), mount_point
));
348 conn_options
.fd
= c
->fd
;
349 conn_options
.protocol
= c
->protocol
;
350 conn_options
.case_handling
= SMB_CASE_DEFAULT
;
351 conn_options
.max_xmit
= c
->max_xmit
;
352 conn_options
.server_uid
= c
->vuid
;
353 conn_options
.tid
= c
->cnum
;
354 conn_options
.secmode
= c
->sec_mode
;
355 conn_options
.rawmode
= 0;
356 conn_options
.sesskey
= c
->sesskey
;
357 conn_options
.maxraw
= 0;
358 conn_options
.capabilities
= c
->capabilities
;
359 conn_options
.serverzone
= c
->serverzone
/60;
361 res
= ioctl(fd
, SMB_IOC_NEWCONN
, &conn_options
);
363 DEBUG(0,("mount.smbfs[%d]: ioctl failed, res=%d\n",
370 /* Ok... We are going to kill the parent. Now
371 is the time to break the process group... */
373 /* Send a signal to the parent to terminate */
374 kill(parentpid
, SIGTERM
);
380 /* This looks wierd but we are only closing the userspace
381 side, the connection has already been passed to smbfs and
382 it has increased the usage count on the socket.
384 If we don't do this we will "leak" sockets and memory on
385 each reconnection we have to make. */
390 /* redirect stdout & stderr since we can't know that
391 the library functions we use are using DEBUG. */
392 if ( (fd
= open("/dev/null", O_WRONLY
)) < 0)
393 DEBUG(2,("mount.smbfs: can't open /dev/null\n"));
396 dup2(fd
, STDOUT_FILENO
);
397 dup2(fd
, STDERR_FILENO
);
401 /* here we are no longer interactive */
402 set_remote_machine_name("smbmount", False
); /* sneaky ... */
403 setup_logging("mount.smbfs", False
);
405 DEBUG(0, ("mount.smbfs: entering daemon mode for service %s, pid=%d\n", the_service
, sys_getpid()));
410 /* Wait for a signal from smbfs ... but don't continue
411 until we actually get a new connection. */
413 CatchSignal(SIGUSR1
, &usr1_handler
);
415 DEBUG(2,("mount.smbfs[%d]: got signal, getting new socket\n", sys_getpid()));
416 c
= do_connection(the_service
);
420 smb_umount(mount_point
);
421 DEBUG(2,("mount.smbfs[%d]: exit\n", sys_getpid()));
429 static void init_mount(void)
431 char mount_point
[PATH_MAX
+1];
438 if (realpath(mpoint
, mount_point
) == NULL
) {
439 fprintf(stderr
, "Could not resolve mount point %s\n", mpoint
);
444 c
= do_connection(service
);
446 fprintf(stderr
,"SMB connection failed\n");
451 Set up to return as a daemon child and wait in the parent
452 until the child say it's ready...
456 pstrcpy(svc2
, service
);
457 string_replace(svc2
, '\\','/');
458 string_replace(svc2
, ' ','_');
460 memset(args
, 0, sizeof(args
[0])*20);
463 args
[i
++] = "smbmnt";
465 args
[i
++] = mount_point
;
473 slprintf(tmp
, sizeof(tmp
)-1, "%d", mount_uid
);
475 args
[i
++] = smb_xstrdup(tmp
);
478 slprintf(tmp
, sizeof(tmp
)-1, "%d", mount_gid
);
480 args
[i
++] = smb_xstrdup(tmp
);
483 slprintf(tmp
, sizeof(tmp
)-1, "0%o", mount_fmask
);
485 args
[i
++] = smb_xstrdup(tmp
);
488 slprintf(tmp
, sizeof(tmp
)-1, "0%o", mount_dmask
);
490 args
[i
++] = smb_xstrdup(tmp
);
497 if (sys_fork() == 0) {
500 asprintf(&smbmnt_path
, "%s/smbmnt", dyn_BINDIR
);
502 if (file_exist(smbmnt_path
, NULL
)) {
503 execv(smbmnt_path
, args
);
505 "smbfs/init_mount: execv of %s failed. Error was %s.",
506 smbmnt_path
, strerror(errno
));
508 execvp("smbmnt", args
);
510 "smbfs/init_mount: execv of %s failed. Error was %s.",
511 "smbmnt", strerror(errno
));
517 if (waitpid(-1, &status
, 0) == -1) {
518 fprintf(stderr
,"waitpid failed: Error was %s", strerror(errno
) );
519 /* FIXME: do some proper error handling */
523 if (WIFEXITED(status
) && WEXITSTATUS(status
) != 0) {
524 fprintf(stderr
,"smbmnt failed: %d\n", WEXITSTATUS(status
));
525 /* FIXME: do some proper error handling */
527 } else if (WIFSIGNALED(status
)) {
528 fprintf(stderr
, "smbmnt killed by signal %d\n", WTERMSIG(status
));
532 /* Ok... This is the rubicon for that mount point... At any point
533 after this, if the connections fail and can not be reconstructed
534 for any reason, we will have to unmount the mount point. There
535 is no exit from the next call...
537 send_fs_socket(service
, mount_point
, c
);
541 /****************************************************************************
542 get a password from a a file or file descriptor
543 exit on failure (from smbclient, move to libsmb or shared .c file?)
544 ****************************************************************************/
545 static void get_password_file(void)
549 BOOL close_it
= False
;
553 if ((p
= getenv("PASSWD_FD")) != NULL
) {
554 pstrcpy(spec
, "descriptor ");
556 sscanf(p
, "%d", &fd
);
558 } else if ((p
= getenv("PASSWD_FILE")) != NULL
) {
559 fd
= sys_open(p
, O_RDONLY
, 0);
562 fprintf(stderr
, "Error opening PASSWD_FILE %s: %s\n",
563 spec
, strerror(errno
));
569 for(p
= pass
, *p
= '\0'; /* ensure that pass is null-terminated */
570 p
&& p
- pass
< sizeof(pass
);) {
571 switch (read(fd
, p
, 1)) {
573 if (*p
!= '\n' && *p
!= '\0') {
574 *++p
= '\0'; /* advance p, and null-terminate pass */
579 *p
= '\0'; /* null-terminate it, just in case... */
580 p
= NULL
; /* then force the loop condition to become false */
583 fprintf(stderr
, "Error reading password from file %s: %s\n",
584 spec
, "empty password\n");
589 fprintf(stderr
, "Error reading password from file %s: %s\n",
590 spec
, strerror(errno
));
594 pstrcpy(password
, pass
);
599 /****************************************************************************
600 get username and password from a credentials file
601 exit on failure (from smbclient, move to libsmb or shared .c file?)
602 ****************************************************************************/
603 static void read_credentials_file(char *filename
)
608 char *ptr
, *val
, *param
;
610 if ((auth
=sys_fopen(filename
, "r")) == NULL
)
612 /* fail if we can't open the credentials file */
613 DEBUG(0,("ERROR: Unable to open credentials file!\n"));
619 /* get a line from the file */
620 if (!fgets (buf
, sizeof(buf
), auth
))
624 if ((len
) && (buf
[len
-1]=='\n'))
632 /* break up the line into parameter & value.
633 will need to eat a little whitespace possibly */
635 if (!(ptr
= strchr (buf
, '=')))
640 /* eat leading white space */
641 while ((*val
!='\0') && ((*val
==' ') || (*val
=='\t')))
644 if (strwicmp("password", param
) == 0)
646 pstrcpy(password
, val
);
649 else if (strwicmp("username", param
) == 0) {
650 pstrcpy(username
, val
);
653 memset(buf
, 0, sizeof(buf
));
659 /****************************************************************************
661 ****************************************************************************/
662 static void usage(void)
664 printf("Usage: mount.smbfs service mountpoint [-o options,...]\n");
666 printf("Version %s\n\n",SAMBA_VERSION_STRING
);
670 username=<arg> SMB username\n\
671 password=<arg> SMB password\n\
672 credentials=<filename> file with username/password\n\
673 krb use kerberos (active directory)\n\
674 netbiosname=<arg> source NetBIOS name\n\
675 uid=<arg> mount uid or username\n\
676 gid=<arg> mount gid or groupname\n\
677 port=<arg> remote SMB port number\n\
678 fmask=<arg> file umask\n\
679 dmask=<arg> directory umask\n\
680 debug=<arg> debug level\n\
681 ip=<arg> destination host or IP address\n\
682 workgroup=<arg> workgroup on destination\n\
683 sockopt=<arg> TCP socket options\n\
684 scope=<arg> NetBIOS scope\n\
685 iocharset=<arg> Linux charset (iso8859-1, utf8)\n\
686 codepage=<arg> server codepage (cp850)\n\
687 unicode use unicode when communicating with server\n\
688 lfs large file system support\n\
689 ttl=<arg> dircache time to live\n\
690 guest don't prompt for a password\n\
691 ro mount read-only\n\
692 rw mount read-write\n\
694 This command is designed to be run from within /bin/mount by giving\n\
695 the option '-t smbfs'. For example:\n\
696 mount -t smbfs -o username=tridge,password=foobar //fjall/test /data/test\n\
701 /****************************************************************************
702 Argument parsing for mount.smbfs interface
703 mount will call us like this:
704 mount.smbfs device mountpoint -o <options>
706 <options> is never empty, containing at least rw or ro
707 ****************************************************************************/
708 static void parse_mount_smb(int argc
, char **argv
)
717 /* FIXME: This function can silently fail if the arguments are
718 * not in the expected order.
720 > The arguments syntax of smbmount 2.2.3a (smbfs of Debian stable)
721 > requires that one gives "-o" before further options like username=...
722 > . Without -o, the username=.. setting is *silently* ignored. I've
723 > spent about an hour trying to find out why I couldn't log in now..
728 if (argc
< 2 || argv
[1][0] == '-') {
733 pstrcpy(service
, argv
[1]);
734 pstrcpy(mpoint
, argv
[2]);
736 /* Convert any '/' characters in the service name to
738 string_replace(service
, '/','\\');
742 opt
= getopt(argc
, argv
, "o:");
751 * option parsing from nfsmount.c (util-linux-2.9u)
753 for (opts
= strtok(optarg
, ","); opts
; opts
= strtok(NULL
, ",")) {
754 DEBUG(3, ("opts: %s\n", opts
));
755 if ((opteq
= strchr_m(opts
, '='))) {
756 val
= atoi(opteq
+ 1);
759 if (!strcmp(opts
, "username") ||
760 !strcmp(opts
, "logon")) {
763 pstrcpy(username
,opteq
+1);
764 if ((lp
=strchr_m(username
,'%'))) {
766 pstrcpy(password
,lp
+1);
768 memset(strchr_m(opteq
+1,'%')+1,'X',strlen(password
));
770 if ((lp
=strchr_m(username
,'/'))) {
772 pstrcpy(workgroup
,lp
+1);
774 } else if(!strcmp(opts
, "passwd") ||
775 !strcmp(opts
, "password")) {
776 pstrcpy(password
,opteq
+1);
778 memset(opteq
+1,'X',strlen(password
));
779 } else if(!strcmp(opts
, "credentials")) {
780 pstrcpy(credentials
,opteq
+1);
781 } else if(!strcmp(opts
, "netbiosname")) {
782 pstrcpy(my_netbios_name
,opteq
+1);
783 } else if(!strcmp(opts
, "uid")) {
784 mount_uid
= nametouid(opteq
+1);
785 } else if(!strcmp(opts
, "gid")) {
786 mount_gid
= nametogid(opteq
+1);
787 } else if(!strcmp(opts
, "port")) {
789 } else if(!strcmp(opts
, "fmask")) {
790 mount_fmask
= strtol(opteq
+1, NULL
, 8);
791 } else if(!strcmp(opts
, "dmask")) {
792 mount_dmask
= strtol(opteq
+1, NULL
, 8);
793 } else if(!strcmp(opts
, "debug")) {
795 } else if(!strcmp(opts
, "ip")) {
796 dest_ip
= *interpret_addr2(opteq
+1);
797 if (is_zero_ip(dest_ip
)) {
798 fprintf(stderr
,"Can't resolve address %s\n", opteq
+1);
802 } else if(!strcmp(opts
, "workgroup")) {
803 pstrcpy(workgroup
,opteq
+1);
804 } else if(!strcmp(opts
, "sockopt")) {
805 pstrcpy(user_socket_options
,opteq
+1);
806 } else if(!strcmp(opts
, "scope")) {
807 set_global_scope(opteq
+1);
809 slprintf(p
, sizeof(pstring
) - (p
- options
) - 1, "%s=%s,", opts
, opteq
+1);
814 if(!strcmp(opts
, "nocaps")) {
815 fprintf(stderr
, "Unhandled option: %s\n", opteq
+1);
817 } else if(!strcmp(opts
, "guest")) {
820 } else if(!strcmp(opts
, "krb")) {
825 fprintf(stderr
, "Warning: kerberos support will only work for samba servers\n");
827 fprintf(stderr
,"No kerberos support compiled in\n");
830 } else if(!strcmp(opts
, "rw")) {
832 } else if(!strcmp(opts
, "ro")) {
834 } else if(!strcmp(opts
, "unicode")) {
835 smbfs_has_unicode
= True
;
836 } else if(!strcmp(opts
, "lfs")) {
837 smbfs_has_lfs
= True
;
839 strncpy(p
, opts
, sizeof(pstring
) - (p
- options
) - 1);
853 *(p
-1) = 0; /* remove trailing , */
854 DEBUG(3,("passthrough options '%s'\n", options
));
858 /****************************************************************************
860 ****************************************************************************/
861 int main(int argc
,char *argv
[])
869 /* here we are interactive, even if run from autofs */
870 setup_logging("mount.smbfs",True
);
872 #if 0 /* JRA - Urban says not needed ? */
873 /* CLI_FORCE_ASCII=false makes smbmount negotiate unicode. The default
874 is to not announce any unicode capabilities as current smbfs does
876 p
= getenv("CLI_FORCE_ASCII");
877 if (p
&& !strcmp(p
, "false"))
878 unsetenv("CLI_FORCE_ASCII");
880 setenv("CLI_FORCE_ASCII", "true", 1);
883 in_client
= True
; /* Make sure that we tell lp_load we are */
885 if (getenv("USER")) {
886 pstrcpy(username
,getenv("USER"));
888 if ((p
=strchr_m(username
,'%'))) {
890 pstrcpy(password
,p
+1);
892 memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(password
));
894 strupper_m(username
);
897 if (getenv("PASSWD")) {
898 pstrcpy(password
,getenv("PASSWD"));
902 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
907 if (*username
== 0 && getenv("LOGNAME")) {
908 pstrcpy(username
,getenv("LOGNAME"));
911 if (!lp_load(dyn_CONFIGFILE
,True
,False
,False
)) {
912 fprintf(stderr
, "Can't load %s - run testparm to debug it\n",
916 parse_mount_smb(argc
, argv
);
918 if (use_kerberos
&& !got_user
) {
922 if (*credentials
!= 0) {
923 read_credentials_file(credentials
);
926 DEBUG(3,("mount.smbfs started (version %s)\n", SAMBA_VERSION_STRING
));
928 if (*workgroup
== 0) {
929 pstrcpy(workgroup
,lp_workgroup());
933 if (!*my_netbios_name
) {
934 pstrcpy(my_netbios_name
, myhostname());
936 strupper_m(my_netbios_name
);