r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[tprouty/samba.git] / source / client / smbmount.c
index 573b2fe2f6f561eda3b31ff8e4f465367c83b2f8..b68dbfe346299365c0541f4f8432587e90bbc528 100644 (file)
@@ -5,7 +5,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#define NO_SYSLOG
-
 #include "includes.h"
 
 #include <mntent.h>
@@ -28,6 +25,8 @@
 
 extern BOOL in_client;
 extern pstring user_socket_options;
+extern char *optarg;
+extern int optind;
 
 static pstring credentials;
 static pstring my_netbios_name;
@@ -41,12 +40,18 @@ static pstring options;
 static struct in_addr dest_ip;
 static BOOL have_ip;
 static int smb_port = 0;
+static BOOL got_user;
 static BOOL got_pass;
 static uid_t mount_uid;
 static gid_t mount_gid;
 static int mount_ro;
 static unsigned mount_fmask;
 static unsigned mount_dmask;
+static BOOL use_kerberos;
+/* TODO: Add code to detect smbfs version in kernel */
+static BOOL status32_smbfs = False;
+static BOOL smbfs_has_unicode = False;
+static BOOL smbfs_has_lfs = False;
 
 static void usage(void);
 
@@ -145,8 +150,8 @@ static struct cli_state *do_connection(char *the_service)
        if (have_ip) ip = dest_ip;
 
        /* have to open a new connection */
-       if (!(c=cli_initialise(NULL)) || (cli_set_port(c, smb_port) != smb_port) ||
-           !cli_connect(c, server_n, &ip)) {
+       if (!(c=cli_initialise()) || (cli_set_port(c, smb_port) != smb_port) ||
+           !NT_STATUS_IS_OK(cli_connect(c, server_n, &ip))) {
                DEBUG(0,("%d: Connection to %s failed\n", sys_getpid(), server_n));
                if (c) {
                        cli_shutdown(c);
@@ -155,10 +160,14 @@ static struct cli_state *do_connection(char *the_service)
        }
 
        /* SPNEGO doesn't work till we get NTSTATUS error support */
-       c->use_spnego = False;
+       /* But it is REQUIRED for kerberos authentication */
+       if(!use_kerberos) c->use_spnego = False;
 
        /* The kernel doesn't yet know how to sign it's packets */
-       c->sign_info->allow_smb_signing = False;
+       c->sign_info.allow_smb_signing = False;
+
+       /* Use kerberos authentication if specified */
+       c->use_kerberos = use_kerberos;
 
        if (!cli_session_request(c, &calling, &called)) {
                char *p;
@@ -193,17 +202,24 @@ static struct cli_state *do_connection(char *the_service)
 
        /* This should be right for current smbfs. Future versions will support
          large files as well as unicode and oplocks. */
-       c->capabilities &= ~(CAP_UNICODE | CAP_LARGE_FILES | CAP_NT_SMBS |
-                               CAP_NT_FIND | CAP_STATUS32 | CAP_LEVEL_II_OPLOCKS);
-       c->force_dos_errors = True;
-       if (!cli_session_setup(c, username, 
-                              password, strlen(password),
-                              password, strlen(password),
-                              workgroup)) {
+       c->capabilities &= ~(CAP_NT_SMBS | CAP_NT_FIND | CAP_LEVEL_II_OPLOCKS);
+       if (!smbfs_has_lfs)
+               c->capabilities &= ~CAP_LARGE_FILES;
+       if (!smbfs_has_unicode)
+               c->capabilities &= ~CAP_UNICODE;
+       if (!status32_smbfs) {
+               c->capabilities &= ~CAP_STATUS32;
+               c->force_dos_errors = True;
+       }
+
+       if (!NT_STATUS_IS_OK(cli_session_setup(c, username, 
+                                              password, strlen(password),
+                                              password, strlen(password),
+                                              workgroup))) {
                /* if a password was not supplied then try again with a
                        null username */
                if (password[0] || !username[0] ||
-                               !cli_session_setup(c, "", "", 0, "", 0, workgroup)) {
+                   !NT_STATUS_IS_OK(cli_session_setup(c, "", "", 0, "", 0, workgroup))) {
                        DEBUG(0,("%d: session setup failed: %s\n",
                                sys_getpid(), cli_errstr(c)));
                        cli_shutdown(c);
@@ -366,10 +382,14 @@ static void send_fs_socket(char *the_service, char *mount_point, struct cli_stat
 
                   If we don't do this we will "leak" sockets and memory on
                   each reconnection we have to make. */
+               c->smb_rw_error = DO_NOT_DO_TDIS;
                cli_shutdown(c);
                c = NULL;
 
                if (!closed) {
+                       /* close the name cache so that close_our_files() doesn't steal its FD */
+                       namecache_shutdown();
+
                        /* redirect stdout & stderr since we can't know that
                           the library functions we use are using DEBUG. */
                        if ( (fd = open("/dev/null", O_WRONLY)) < 0)
@@ -382,7 +402,7 @@ static void send_fs_socket(char *the_service, char *mount_point, struct cli_stat
                        }
 
                        /* here we are no longer interactive */
-                       set_remote_machine_name("smbmount");    /* sneaky ... */
+                       set_remote_machine_name("smbmount", False);     /* sneaky ... */
                        setup_logging("mount.smbfs", False);
                        reopen_logs();
                        DEBUG(0, ("mount.smbfs: entering daemon mode for service %s, pid=%d\n", the_service, sys_getpid()));
@@ -411,7 +431,7 @@ static void send_fs_socket(char *the_service, char *mount_point, struct cli_stat
  **/
 static void init_mount(void)
 {
-       char mount_point[MAXPATHLEN+1];
+       char mount_point[PATH_MAX+1];
        pstring tmp;
        pstring svc2;
        struct cli_state *c;
@@ -507,6 +527,9 @@ static void init_mount(void)
                fprintf(stderr,"smbmnt failed: %d\n", WEXITSTATUS(status));
                /* FIXME: do some proper error handling */
                exit(1);
+       } else if (WIFSIGNALED(status)) {
+               fprintf(stderr, "smbmnt killed by signal %d\n", WTERMSIG(status));
+               exit(1);
        }
 
        /* Ok...  This is the rubicon for that mount point...  At any point
@@ -626,8 +649,9 @@ static void read_credentials_file(char *filename)
                        pstrcpy(password, val);
                        got_pass = True;
                }
-               else if (strwicmp("username", param) == 0)
+               else if (strwicmp("username", param) == 0) {
                        pstrcpy(username, val);
+               }
 
                memset(buf, 0, sizeof(buf));
        }
@@ -642,13 +666,17 @@ static void usage(void)
 {
        printf("Usage: mount.smbfs service mountpoint [-o options,...]\n");
 
-       printf("Version %s\n\n",VERSION);
+       printf("Version %s\n\n",SAMBA_VERSION_STRING);
+
+       printf("Please be aware that smbfs is deprecated in favor of "
+              "cifs\n\n");
 
        printf(
 "Options:\n\
       username=<arg>                  SMB username\n\
       password=<arg>                  SMB password\n\
       credentials=<filename>          file with username/password\n\
+      krb                             use kerberos (active directory)\n\
       netbiosname=<arg>               source NetBIOS name\n\
       uid=<arg>                       mount uid or username\n\
       gid=<arg>                       mount gid or groupname\n\
@@ -662,6 +690,8 @@ static void usage(void)
       scope=<arg>                     NetBIOS scope\n\
       iocharset=<arg>                 Linux charset (iso8859-1, utf8)\n\
       codepage=<arg>                  server codepage (cp850)\n\
+      unicode                         use unicode when communicating with server\n\
+      lfs                             large file system support\n\
       ttl=<arg>                       dircache time to live\n\
       guest                           don't prompt for a password\n\
       ro                              mount read-only\n\
@@ -686,7 +716,6 @@ static void parse_mount_smb(int argc, char **argv)
        int opt;
        char *opts;
        char *opteq;
-       extern char *optarg;
        int val;
        char *p;
 
@@ -735,6 +764,7 @@ static void parse_mount_smb(int argc, char **argv)
                         if (!strcmp(opts, "username") || 
                            !strcmp(opts, "logon")) {
                                char *lp;
+                               got_user = True;
                                pstrcpy(username,opteq+1);
                                if ((lp=strchr_m(username,'%'))) {
                                        *lp = 0;
@@ -792,10 +822,24 @@ static void parse_mount_smb(int argc, char **argv)
                        } else if(!strcmp(opts, "guest")) {
                                *password = '\0';
                                got_pass = True;
+                       } else if(!strcmp(opts, "krb")) {
+#ifdef HAVE_KRB5
+
+                               use_kerberos = True;
+                               if(!status32_smbfs)
+                                       fprintf(stderr, "Warning: kerberos support will only work for samba servers\n");
+#else
+                               fprintf(stderr,"No kerberos support compiled in\n");
+                               exit(1);
+#endif
                        } else if(!strcmp(opts, "rw")) {
                                mount_ro = 0;
                        } else if(!strcmp(opts, "ro")) {
                                mount_ro = 1;
+                       } else if(!strcmp(opts, "unicode")) {
+                               smbfs_has_unicode = True;
+                       } else if(!strcmp(opts, "lfs")) {
+                               smbfs_has_lfs = True;
                        } else {
                                strncpy(p, opts, sizeof(pstring) - (p - options) - 1);
                                p += strlen(opts);
@@ -821,12 +865,12 @@ static void parse_mount_smb(int argc, char **argv)
 ****************************************************************************/
  int main(int argc,char *argv[])
 {
-       extern char *optarg;
-       extern int optind;
        char *p;
 
        DEBUGLEVEL = 1;
 
+       load_case_tables();
+
        /* here we are interactive, even if run from autofs */
        setup_logging("mount.smbfs",True);
 
@@ -852,7 +896,7 @@ static void parse_mount_smb(int argc, char **argv)
                        got_pass = True;
                        memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(password));
                }
-               strupper(username);
+               strupper_m(username);
        }
 
        if (getenv("PASSWD")) {
@@ -869,18 +913,22 @@ static void parse_mount_smb(int argc, char **argv)
                pstrcpy(username,getenv("LOGNAME"));
        }
 
-       if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
+       if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
                fprintf(stderr, "Can't load %s - run testparm to debug it\n", 
                        dyn_CONFIGFILE);
        }
 
        parse_mount_smb(argc, argv);
 
+       if (use_kerberos && !got_user) {
+               got_pass = True;
+       }
+
        if (*credentials != 0) {
                read_credentials_file(credentials);
        }
 
-       DEBUG(3,("mount.smbfs started (version %s)\n", VERSION));
+       DEBUG(3,("mount.smbfs started (version %s)\n", SAMBA_VERSION_STRING));
 
        if (*workgroup == 0) {
                pstrcpy(workgroup,lp_workgroup());
@@ -890,7 +938,7 @@ static void parse_mount_smb(int argc, char **argv)
        if (!*my_netbios_name) {
                pstrcpy(my_netbios_name, myhostname());
        }
-       strupper(my_netbios_name);
+       strupper_m(my_netbios_name);
 
        init_mount();
        return 0;