syncing up with HEAD. Seems to be a lot of differences creeping in
[vlendec/samba-autobuild/.git] / source3 / client / client.c
index c9842335a9f98fa17bc82eecd7579932e80fc52a..f25ed1623b0d96c865620a5ffecf90b9f2f86856 100644 (file)
@@ -1,8 +1,8 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 1.9.
+   Unix SMB/CIFS implementation.
    SMB client
    Copyright (C) Andrew Tridgell 1994-1998
+   Copyright (C) Simo Sorce 2001-2002
    
    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
 #define NO_SYSLOG
 
 #include "includes.h"
-
+#include "../client/client_proto.h"
 #ifndef REGISTER
 #define REGISTER 0
 #endif
 
+const char prog_name[] = "smbclient";
+
 struct cli_state *cli;
 extern BOOL in_client;
+extern BOOL AllowDebugChange;
 static int port = 0;
 pstring cur_dir = "\\";
-pstring cd_path = "";
+static pstring cd_path = "";
 static pstring service;
 static pstring desthost;
 extern pstring global_myname;
@@ -41,14 +44,14 @@ static pstring workgroup;
 static char *cmdstr;
 static BOOL got_pass;
 static int io_bufsize = 64512;
-extern struct in_addr ipzero;
+static BOOL use_kerberos;
 
 static int name_type = 0x20;
 static int max_protocol = PROTOCOL_NT1;
 extern pstring user_socket_options;
 
 static int process_tok(fstring tok);
-static NTSTATUS cmd_help(void);
+static int cmd_help(void);
 
 /* 30 second timeout on most commands */
 #define CLIENT_TIMEOUT (30*1000)
@@ -58,12 +61,9 @@ static NTSTATUS cmd_help(void);
 #define FID_UNUSED (0xFFFF)
 
 time_t newer_than = 0;
-int archive_level = 0;
-
-extern pstring debugf;
-extern int DEBUGLEVEL;
+static int archive_level = 0;
 
-BOOL translation = False;
+static BOOL translation = False;
 
 static BOOL have_ip;
 
@@ -74,36 +74,39 @@ extern BOOL tar_reset;
 /* clitar bits end */
  
 
-mode_t myumask = 0755;
+static mode_t myumask = 0755;
 
-BOOL prompt = True;
+static BOOL prompt = True;
 
-int printmode = 1;
+static int printmode = 1;
 
 static BOOL recurse = False;
 BOOL lowercase = False;
 
-struct in_addr dest_ip;
+static struct in_addr dest_ip;
 
 #define SEPARATORS " \t\n\r"
 
-BOOL abort_mget = True;
+static BOOL abort_mget = True;
 
-pstring fileselection = "";
+static pstring fileselection = "";
 
 extern file_info def_finfo;
 
 /* timing globals */
 int get_total_size = 0;
 int get_total_time_ms = 0;
-int put_total_size = 0;
-int put_total_time_ms = 0;
+static int put_total_size = 0;
+static int put_total_time_ms = 0;
 
 /* totals globals */
 static double dir_total;
 
 #define USENMB
 
+/* some forward declarations */
+static struct cli_state *do_connect(const char *server, const char *share);
+
 /****************************************************************************
 write to a local file with CR/LF->LF translation if appropriate. return the 
 number taken from the buffer. This may not equal the number written.
@@ -214,33 +217,36 @@ static void send_message(void)
 /****************************************************************************
 check the space on a device
 ****************************************************************************/
-static void do_dskattr(void)
+static int do_dskattr(void)
 {
        int total, bsize, avail;
 
        if (!cli_dskattr(cli, &bsize, &total, &avail)) {
                d_printf("Error in dskattr: %s\n",cli_errstr(cli)); 
-               return;
+               return 1;
        }
 
        d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
                 total, bsize, avail);
+
+       return 0;
 }
 
 /****************************************************************************
 show cd/pwd
 ****************************************************************************/
-static void cmd_pwd(void)
+static int cmd_pwd(void)
 {
        d_printf("Current directory is %s",service);
        d_printf("%s\n",cur_dir);
+       return 0;
 }
 
 
 /****************************************************************************
 change directory - inner section
 ****************************************************************************/
-static void do_cd(char *newdir)
+static int do_cd(char *newdir)
 {
        char *p = newdir;
        pstring saved_dir;
@@ -271,19 +277,24 @@ static void do_cd(char *newdir)
        }
        
        pstrcpy(cd_path,cur_dir);
+
+       return 0;
 }
 
 /****************************************************************************
 change directory
 ****************************************************************************/
-static void cmd_cd(void)
+static int cmd_cd(void)
 {
        fstring buf;
+       int rc = 0;
 
        if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
-               do_cd(buf);
+               rc = do_cd(buf);
        else
                d_printf("Current directory is %s\n",cur_dir);
+
+       return rc;
 }
 
 
@@ -296,7 +307,7 @@ static BOOL do_this_one(file_info *finfo)
 
        if (*fileselection && 
            !mask_match(finfo->name,fileselection,False)) {
-               DEBUG(3,("match_match %s failed\n", finfo->name));
+               DEBUG(3,("mask_match %s failed\n", finfo->name));
                return False;
        }
 
@@ -365,11 +376,7 @@ functions for do_list_queue
  */
 static void reset_do_list_queue(void)
 {
-       if (do_list_queue)
-       {
-               free(do_list_queue);
-       }
-       do_list_queue = 0;
+       SAFE_FREE(do_list_queue);
        do_list_queue_size = 0;
        do_list_queue_start = 0;
        do_list_queue_end = 0;
@@ -569,12 +576,13 @@ void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec,
 /****************************************************************************
   get a directory listing
   ****************************************************************************/
-static void cmd_dir(void)
+static int cmd_dir(void)
 {
        uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
        pstring mask;
        fstring buf;
        char *p=buf;
+       int rc;
        
        dir_total = 0;
        pstrcpy(mask,cur_dir);
@@ -594,21 +602,24 @@ static void cmd_dir(void)
 
        do_list(mask, attribute, display_finfo, recurse, True);
 
-       do_dskattr();
+       rc = do_dskattr();
 
        DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
+
+       return rc;
 }
 
 
 /****************************************************************************
   get a directory listing
   ****************************************************************************/
-static void cmd_du(void)
+static int cmd_du(void)
 {
        uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
        pstring mask;
        fstring buf;
        char *p=buf;
+       int rc;
        
        dir_total = 0;
        pstrcpy(mask,cur_dir);
@@ -627,25 +638,29 @@ static void cmd_du(void)
 
        do_list(mask, attribute, do_du, recurse, True);
 
-       do_dskattr();
+       rc = do_dskattr();
 
        d_printf("Total number of bytes: %.0f\n", dir_total);
+
+       return rc;
 }
 
 
 /****************************************************************************
   get a file from rname to lname
   ****************************************************************************/
-static void do_get(char *rname,char *lname)
+static int do_get(char *rname, char *lname, BOOL reget)
 {  
-       int handle=0,fnum;
+       int handle = 0, fnum;
        BOOL newhandle = False;
        char *data;
        struct timeval tp_start;
        int read_size = io_bufsize;
        uint16 attr;
        size_t size;
+       off_t start = 0;
        off_t nread = 0;
+       int rc = 0;
 
        GetTimeOfDay(&tp_start);
 
@@ -657,18 +672,29 @@ static void do_get(char *rname,char *lname)
 
        if (fnum == -1) {
                d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
-               return;
+               return 1;
        }
 
        if(!strcmp(lname,"-")) {
                handle = fileno(stdout);
        } else {
-               handle = sys_open(lname,O_WRONLY|O_CREAT|O_TRUNC,0644);
+               if (reget) {
+                       handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
+                       if (handle >= 0) {
+                               start = sys_lseek(handle, 0, SEEK_END);
+                               if (start == -1) {
+                                       d_printf("Error seeking local file\n");
+                                       return 1;
+                               }
+                       }
+               } else {
+                       handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
+               }
                newhandle = True;
        }
        if (handle < 0) {
                d_printf("Error opening local file %s\n",lname);
-               return;
+               return 1;
        }
 
 
@@ -677,40 +703,44 @@ static void do_get(char *rname,char *lname)
            !cli_getattrE(cli, fnum, 
                          &attr, &size, NULL, NULL, NULL)) {
                d_printf("getattrib: %s\n",cli_errstr(cli));
-               return;
+               return 1;
        }
 
        DEBUG(2,("getting file %s of size %.0f as %s ", 
-                lname, (double)size, lname));
+                rname, (double)size, lname));
 
        if(!(data = (char *)malloc(read_size))) { 
                d_printf("malloc fail for size %d\n", read_size);
                cli_close(cli, fnum);
-               return;
+               return 1;
        }
 
        while (1) {
-               int n = cli_read(cli, fnum, data, nread, read_size);
+               int n = cli_read(cli, fnum, data, nread + start, read_size);
 
                if (n <= 0) break;
  
                if (writefile(handle,data, n) != n) {
                        d_printf("Error writing local file\n");
+                       rc = 1;
                        break;
                }
       
                nread += n;
        }
 
-       if (nread < size) {
+       if (nread + start < size) {
                DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
-               rname, (long)nread));
+                           rname, (long)nread));
+
+               rc = 1;
        }
 
-       free(data);
+       SAFE_FREE(data);
        
        if (!cli_close(cli, fnum)) {
                d_printf("Error %s closing remote file\n",cli_errstr(cli));
+               rc = 1;
        }
 
        if (newhandle) {
@@ -736,13 +766,15 @@ static void do_get(char *rname,char *lname)
                         nread / (1.024*this_time + 1.0e-4),
                         get_total_size / (1.024*get_total_time_ms)));
        }
+       
+       return rc;
 }
 
 
 /****************************************************************************
   get a file
   ****************************************************************************/
-static void cmd_get(void)
+static int cmd_get(void)
 {
        pstring lname;
        pstring rname;
@@ -755,14 +787,14 @@ static void cmd_get(void)
        
        if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
                d_printf("get <filename>\n");
-               return;
+               return 1;
        }
        pstrcpy(lname,p);
        dos_clean_name(rname);
        
        next_token_nr(NULL,lname,NULL,sizeof(lname));
        
-       do_get(rname, lname);
+       return do_get(rname, lname, False);
 }
 
 
@@ -796,7 +828,7 @@ static void do_mget(file_info *finfo)
        if (!(finfo->mode & aDIR)) {
                pstrcpy(rname,cur_dir);
                pstrcat(rname,finfo->name);
-               do_get(rname,finfo->name);
+               do_get(rname, finfo->name, False);
                return;
        }
 
@@ -835,11 +867,12 @@ static void do_mget(file_info *finfo)
 /****************************************************************************
 view the file using the pager
 ****************************************************************************/
-static void cmd_more(void)
+static int cmd_more(void)
 {
        fstring rname,lname,pager_cmd;
        char *pager;
        int fd;
+       int rc = 0;
 
        fstrcpy(rname,cur_dir);
        fstrcat(rname,"\\");
@@ -848,18 +881,18 @@ static void cmd_more(void)
        fd = smb_mkstemp(lname);
        if (fd == -1) {
                d_printf("failed to create temporary file for more\n");
-               return;
+               return 1;
        }
        close(fd);
 
        if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
                d_printf("more <filename>\n");
                unlink(lname);
-               return;
+               return 1;
        }
        dos_clean_name(rname);
 
-       do_get(rname,lname);
+       rc = do_get(rname, lname, False);
 
        pager=getenv("PAGER");
 
@@ -867,6 +900,8 @@ static void cmd_more(void)
                 "%s %s",(pager? pager:PAGER), lname);
        system(pager_cmd);
        unlink(lname);
+       
+       return rc;
 }
 
 
@@ -874,7 +909,7 @@ static void cmd_more(void)
 /****************************************************************************
 do a mget command
 ****************************************************************************/
-static void cmd_mget(void)
+static int cmd_mget(void)
 {
        uint16 attribute = aSYSTEM | aHIDDEN;
        pstring mget_mask;
@@ -907,6 +942,8 @@ static void cmd_mget(void)
                pstrcat(mget_mask,"*");
                do_list(mget_mask, attribute,do_mget,False,True);
        }
+       
+       return 0;
 }
 
 
@@ -924,21 +961,39 @@ static BOOL do_mkdir(char *name)
        return(True);
 }
 
+/****************************************************************************
+show 8.3 name of a file
+****************************************************************************/
+static BOOL do_altname(char *name)
+{
+       fstring altname;
+       if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, altname))) {
+               d_printf("%s getting alt name for %s\n",
+                        cli_errstr(cli),name);
+               return(False);
+       }
+       d_printf("%s\n", altname);
+
+       return(True);
+}
+
 
 /****************************************************************************
  Exit client.
 ****************************************************************************/
-static void cmd_quit(void)
+static int cmd_quit(void)
 {
        cli_shutdown(cli);
        exit(0);
+       /* NOTREACHED */
+       return 0;
 }
 
 
 /****************************************************************************
   make a directory
   ****************************************************************************/
-static void cmd_mkdir(void)
+static int cmd_mkdir(void)
 {
        pstring mask;
        fstring buf;
@@ -949,7 +1004,7 @@ static void cmd_mkdir(void)
        if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
                if (!recurse)
                        d_printf("mkdir <dirname>\n");
-               return;
+               return 1;
        }
        pstrcat(mask,p);
 
@@ -972,42 +1027,89 @@ static void cmd_mkdir(void)
        } else {
                do_mkdir(mask);
        }
+       
+       return 0;
+}
+
+
+/****************************************************************************
+  show alt name
+  ****************************************************************************/
+static int cmd_altname(void)
+{
+       pstring name;
+       fstring buf;
+       char *p=buf;
+  
+       pstrcpy(name,cur_dir);
+
+       if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
+               d_printf("altname <file>\n");
+               return 1;
+       }
+       pstrcat(name,p);
+
+       do_altname(name);
+
+       return 0;
 }
 
 
 /****************************************************************************
   put a single file
   ****************************************************************************/
-static void do_put(char *rname,char *lname)
+static int do_put(char *rname, char *lname, BOOL reput)
 {
        int fnum;
        XFILE *f;
-       int nread=0;
-       char *buf=NULL;
-       int maxwrite=io_bufsize;
+       int start = 0;
+       int nread = 0;
+       char *buf = NULL;
+       int maxwrite = io_bufsize;
+       int rc = 0;
        
        struct timeval tp_start;
        GetTimeOfDay(&tp_start);
 
-       fnum = cli_open(cli, rname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
+       if (reput) {
+               fnum = cli_open(cli, rname, O_RDWR|O_CREAT, DENY_NONE);
+               if (fnum >= 0) {
+                       if (!cli_qfileinfo(cli, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL) &&
+                           !cli_getattrE(cli, fnum, NULL, &start, NULL, NULL, NULL)) {
+                               d_printf("getattrib: %s\n",cli_errstr(cli));
+                               return 1;
+                       }
+               }
+       } else {
+               fnum = cli_open(cli, rname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
+       }
   
        if (fnum == -1) {
                d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
-               return;
+               return 1;
        }
 
        /* allow files to be piped into smbclient
-          jdblair 24.jun.98 */
+          jdblair 24.jun.98
+
+          Note that in this case this function will exit(0) rather
+          than returning. */
        if (!strcmp(lname, "-")) {
                f = x_stdin;
                /* size of file is not known */
        } else {
                f = x_fopen(lname,O_RDONLY, 0);
+               if (f && reput) {
+                       if (x_tseek(f, start, SEEK_SET) == -1) {
+                               d_printf("Error seeking local file\n");
+                               return 1;
+                       }
+               }
        }
 
        if (!f) {
                d_printf("Error opening local file %s\n",lname);
-               return;
+               return 1;
        }
 
   
@@ -1017,7 +1119,7 @@ static void do_put(char *rname,char *lname)
        buf = (char *)malloc(maxwrite);
        if (!buf) {
                d_printf("ERROR: Not enough memory!\n");
-               return;
+               return 1;
        }
        while (!x_feof(f)) {
                int n = maxwrite;
@@ -1028,13 +1130,15 @@ static void do_put(char *rname,char *lname)
                                break; /* Empty local file. */
 
                        d_printf("Error reading local file: %s\n", strerror(errno));
+                       rc = 1;
                        break;
                }
 
-               ret = cli_write(cli, fnum, 0, buf, nread, n);
+               ret = cli_write(cli, fnum, 0, buf, nread + start, n);
 
                if (n != ret) {
                        d_printf("Error writing file: %s\n", cli_errstr(cli));
+                       rc = 1;
                        break;
                } 
 
@@ -1044,13 +1148,16 @@ static void do_put(char *rname,char *lname)
        if (!cli_close(cli, fnum)) {
                d_printf("%s closing remote file %s\n",cli_errstr(cli),rname);
                x_fclose(f);
-               if (buf) free(buf);
-               return;
+               SAFE_FREE(buf);
+               return 1;
        }
 
        
-       x_fclose(f);
-       if (buf) free(buf);
+       if (f != x_stdin) {
+               x_fclose(f);
+       }
+
+       SAFE_FREE(buf);
 
        {
                struct timeval tp_end;
@@ -1072,6 +1179,8 @@ static void do_put(char *rname,char *lname)
                cli_shutdown(cli);
                exit(0);
        }
+       
+       return rc;
 }
 
  
@@ -1079,7 +1188,7 @@ static void do_put(char *rname,char *lname)
 /****************************************************************************
   put a file
   ****************************************************************************/
-static void cmd_put(void)
+static int cmd_put(void)
 {
        pstring lname;
        pstring rname;
@@ -1091,7 +1200,7 @@ static void cmd_put(void)
   
        if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
                d_printf("put <filename>\n");
-               return;
+               return 1;
        }
        pstrcpy(lname,p);
   
@@ -1109,11 +1218,11 @@ static void cmd_put(void)
                if (!file_exist(lname,&st) &&
                    (strcmp(lname,"-"))) {
                        d_printf("%s does not exist\n",lname);
-                       return;
+                       return 1;
                }
        }
 
-       do_put(rname,lname);
+       return do_put(rname, lname, False);
 }
 
 /*************************************
@@ -1138,8 +1247,8 @@ static void free_file_list (struct file_list * list)
        {
                tmp = list;
                DLIST_REMOVE(list, list);
-               if (tmp->file_path) free(tmp->file_path);
-               free(tmp);
+               SAFE_FREE(tmp->file_path);
+               SAFE_FREE(tmp);
        }
 }
 
@@ -1163,10 +1272,12 @@ static BOOL seek_list(struct file_list *list, char *name)
 /****************************************************************************
   set the file selection mask
   ****************************************************************************/
-static void cmd_select(void)
+static int cmd_select(void)
 {
        pstrcpy(fileselection,"");
        next_token_nr(NULL,fileselection,NULL,sizeof(fileselection));
+
+       return 0;
 }
 
 /****************************************************************************
@@ -1209,7 +1320,7 @@ static int file_find(struct file_list **list, const char *directory,
                                }
                                
                                if (ret == -1) {
-                                       free(path);
+                                       SAFE_FREE(path);
                                        closedir(dir);
                                        return -1;
                                }
@@ -1224,7 +1335,7 @@ static int file_find(struct file_list **list, const char *directory,
                        entry->isdir = isdir;
                         DLIST_ADD(*list, entry);
                } else {
-                       free(path);
+                       SAFE_FREE(path);
                }
         }
 
@@ -1235,7 +1346,7 @@ static int file_find(struct file_list **list, const char *directory,
 /****************************************************************************
   mput some files
   ****************************************************************************/
-static void cmd_mput(void)
+static int cmd_mput(void)
 {
        fstring buf;
        char *p=buf;
@@ -1260,7 +1371,7 @@ static void cmd_mput(void)
                for (temp_list = file_list; temp_list; 
                     temp_list = temp_list->next) {
 
-                       if (lname) free(lname);
+                       SAFE_FREE(lname);
                        if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
                                continue;
                        trim_string(lname, "./", "/");
@@ -1269,16 +1380,16 @@ static void cmd_mput(void)
                        if (temp_list->isdir) {
                                /* if (!recurse) continue; */
                                
-                               if (quest) free(quest);
-                               asprintf(&quest, "Put directory %s? ", lname);
+                               SAFE_FREE(quest);
+                               if (asprintf(&quest, "Put directory %s? ", lname) < 0) break;
                                if (prompt && !yesno(quest)) { /* No */
                                        /* Skip the directory */
                                        lname[strlen(lname)-1] = '/';
                                        if (!seek_list(temp_list, lname))
                                                break;              
                                } else { /* Yes */
-                                       if (rname) free(rname);
-                                       asprintf(&rname, "%s%s", cur_dir, lname);
+                                       SAFE_FREE(rname);
+                                       if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
                                        dos_format(rname);
                                        if (!cli_chkpath(cli, rname) && 
                                            !do_mkdir(rname)) {
@@ -1291,37 +1402,41 @@ static void cmd_mput(void)
                                }
                                continue;
                        } else {
-                               if (quest) free(quest);
-                               asprintf(&quest,"Put file %s? ", lname);
+                               SAFE_FREE(quest);
+                               if (asprintf(&quest,"Put file %s? ", lname) < 0) break;
                                if (prompt && !yesno(quest)) /* No */
                                        continue;
                                
                                /* Yes */
-                               if (rname) free(rname);
-                               asprintf(&rname, "%s%s", cur_dir, lname);
+                               SAFE_FREE(rname);
+                               if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
                        }
 
                        dos_format(rname);
 
-                       do_put(rname, lname);
+                       do_put(rname, lname, False);
                }
                free_file_list(file_list);
-               if (quest) free(quest);
-               if (lname) free(lname);
-               if (rname) free(rname);
+               SAFE_FREE(quest);
+               SAFE_FREE(lname);
+               SAFE_FREE(rname);
        }
+
+       return 0;
 }
 
 
 /****************************************************************************
   cancel a print job
   ****************************************************************************/
-static void do_cancel(int job)
+static int do_cancel(int job)
 {
        if (cli_printjob_del(cli, job)) {
                d_printf("Job %d cancelled\n",job);
+               return 0;
        } else {
                d_printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
+               return 1;
        }
 }
 
@@ -1329,26 +1444,28 @@ static void do_cancel(int job)
 /****************************************************************************
   cancel a print job
   ****************************************************************************/
-static void cmd_cancel(void)
+static int cmd_cancel(void)
 {
        fstring buf;
        int job; 
 
        if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
                d_printf("cancel <jobid> ...\n");
-               return;
+               return 1;
        }
        do {
                job = atoi(buf);
                do_cancel(job);
        } while (next_token_nr(NULL,buf,NULL,sizeof(buf)));
+       
+       return 0;
 }
 
 
 /****************************************************************************
   print a file
   ****************************************************************************/
-static void cmd_print(void)
+static int cmd_print(void)
 {
        pstring lname;
        pstring rname;
@@ -1356,7 +1473,7 @@ static void cmd_print(void)
 
        if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
                d_printf("print <filename>\n");
-               return;
+               return 1;
        }
 
        pstrcpy(rname,lname);
@@ -1369,7 +1486,7 @@ static void cmd_print(void)
                slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)sys_getpid());
        }
 
-       do_put(rname, lname);
+       return do_put(rname, lname, False);
 }
 
 
@@ -1384,9 +1501,11 @@ static void queue_fn(struct print_job_info *p)
 /****************************************************************************
  show a print queue
 ****************************************************************************/
-static void cmd_queue(void)
+static int cmd_queue(void)
 {
-       cli_print_queue(cli, queue_fn);  
+       cli_print_queue(cli, queue_fn);
+       
+       return 0;
 }
 
 /****************************************************************************
@@ -1410,7 +1529,7 @@ static void do_del(file_info *finfo)
 /****************************************************************************
 delete some files
 ****************************************************************************/
-static void cmd_del(void)
+static int cmd_del(void)
 {
        pstring mask;
        fstring buf;
@@ -1423,16 +1542,18 @@ static void cmd_del(void)
        
        if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
                d_printf("del <filename>\n");
-               return;
+               return 1;
        }
        pstrcat(mask,buf);
 
        do_list(mask, attribute,do_del,False,False);
+       
+       return 0;
 }
 
 /****************************************************************************
 ****************************************************************************/
-static void cmd_open(void)
+static int cmd_open(void)
 {
        pstring mask;
        fstring buf;
@@ -1440,19 +1561,21 @@ static void cmd_open(void)
        pstrcpy(mask,cur_dir);
        
        if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
-               d_printf("del <filename>\n");
-               return;
+               d_printf("open <filename>\n");
+               return 1;
        }
        pstrcat(mask,buf);
 
        cli_open(cli, mask, O_RDWR, DENY_ALL);
+
+       return 0;
 }
 
 
 /****************************************************************************
 remove a directory
 ****************************************************************************/
-static void cmd_rmdir(void)
+static int cmd_rmdir(void)
 {
        pstring mask;
        fstring buf;
@@ -1461,20 +1584,164 @@ static void cmd_rmdir(void)
        
        if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
                d_printf("rmdir <dirname>\n");
-               return;
+               return 1;
        }
        pstrcat(mask,buf);
 
        if (!cli_rmdir(cli, mask)) {
                d_printf("%s removing remote directory file %s\n",
                         cli_errstr(cli),mask);
+       }
+       
+       return 0;
+}
+
+/****************************************************************************
+ UNIX hardlink.
+****************************************************************************/
+
+static int cmd_link(void)
+{
+       pstring src,dest;
+       fstring buf,buf2;
+  
+       if (!SERVER_HAS_UNIX_CIFS(cli)) {
+               d_printf("Server doesn't support UNIX CIFS calls.\n");
+               return 1;
+       }
+
+       pstrcpy(src,cur_dir);
+       pstrcpy(dest,cur_dir);
+  
+       if (!next_token(NULL,buf,NULL,sizeof(buf)) || 
+           !next_token(NULL,buf2,NULL, sizeof(buf2))) {
+               d_printf("link <src> <dest>\n");
+               return 1;
+       }
+
+       pstrcat(src,buf);
+       pstrcat(dest,buf2);
+
+       if (!cli_unix_hardlink(cli, src, dest)) {
+               d_printf("%s linking files (%s -> %s)\n", cli_errstr(cli), src, dest);
+               return 1;
        }  
+
+       return 0;
+}
+
+/****************************************************************************
+ UNIX symlink.
+****************************************************************************/
+
+static int cmd_symlink(void)
+{
+       pstring src,dest;
+       fstring buf,buf2;
+  
+       if (!SERVER_HAS_UNIX_CIFS(cli)) {
+               d_printf("Server doesn't support UNIX CIFS calls.\n");
+               return 1;
+       }
+
+       pstrcpy(src,cur_dir);
+       pstrcpy(dest,cur_dir);
+       
+       if (!next_token(NULL,buf,NULL,sizeof(buf)) || 
+           !next_token(NULL,buf2,NULL, sizeof(buf2))) {
+               d_printf("symlink <src> <dest>\n");
+               return 1;
+       }
+
+       pstrcat(src,buf);
+       pstrcat(dest,buf2);
+
+       if (!cli_unix_symlink(cli, src, dest)) {
+               d_printf("%s symlinking files (%s -> %s)\n",
+                       cli_errstr(cli), src, dest);
+               return 1;
+       } 
+
+       return 0;
+}
+
+/****************************************************************************
+ UNIX chmod.
+****************************************************************************/
+
+static int cmd_chmod(void)
+{
+       pstring src;
+       mode_t mode;
+       fstring buf, buf2;
+  
+       if (!SERVER_HAS_UNIX_CIFS(cli)) {
+               d_printf("Server doesn't support UNIX CIFS calls.\n");
+               return 1;
+       }
+
+       pstrcpy(src,cur_dir);
+       
+       if (!next_token(NULL,buf,NULL,sizeof(buf)) || 
+           !next_token(NULL,buf2,NULL, sizeof(buf2))) {
+               d_printf("chmod mode file\n");
+               return 1;
+       }
+
+       mode = (mode_t)strtol(buf, NULL, 8);
+       pstrcat(src,buf2);
+
+       if (!cli_unix_chmod(cli, src, mode)) {
+               d_printf("%s chmod file %s 0%o\n",
+                       cli_errstr(cli), src, (unsigned int)mode);
+               return 1;
+       } 
+
+       return 0;
+}
+
+/****************************************************************************
+ UNIX chown.
+****************************************************************************/
+
+static int cmd_chown(void)
+{
+       pstring src;
+       uid_t uid;
+       gid_t gid;
+       fstring buf, buf2, buf3;
+  
+       if (!SERVER_HAS_UNIX_CIFS(cli)) {
+               d_printf("Server doesn't support UNIX CIFS calls.\n");
+               return 1;
+       }
+
+       pstrcpy(src,cur_dir);
+       
+       if (!next_token(NULL,buf,NULL,sizeof(buf)) || 
+           !next_token(NULL,buf2,NULL, sizeof(buf2)) ||
+           !next_token(NULL,buf3,NULL, sizeof(buf3))) {
+               d_printf("chown uid gid file\n");
+               return 1;
+       }
+
+       uid = (uid_t)atoi(buf);
+       gid = (gid_t)atoi(buf2);
+       pstrcat(src,buf3);
+
+       if (!cli_unix_chown(cli, src, uid, gid)) {
+               d_printf("%s chown file %s uid=%d, gid=%d\n",
+                       cli_errstr(cli), src, (int)uid, (int)gid);
+               return 1;
+       } 
+
+       return 0;
 }
 
 /****************************************************************************
 rename some files
 ****************************************************************************/
-static void cmd_rename(void)
+static int cmd_rename(void)
 {
        pstring src,dest;
        fstring buf,buf2;
@@ -1485,7 +1752,7 @@ static void cmd_rename(void)
        if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) || 
            !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
                d_printf("rename <src> <dest>\n");
-               return;
+               return 1;
        }
 
        pstrcat(src,buf);
@@ -1493,25 +1760,29 @@ static void cmd_rename(void)
 
        if (!cli_rename(cli, src, dest)) {
                d_printf("%s renaming files\n",cli_errstr(cli));
-               return;
-       }  
+               return 1;
+       }
+       
+       return 0;
 }
 
 
 /****************************************************************************
 toggle the prompt flag
 ****************************************************************************/
-static void cmd_prompt(void)
+static int cmd_prompt(void)
 {
        prompt = !prompt;
        DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
+       
+       return 1;
 }
 
 
 /****************************************************************************
 set the newer than time
 ****************************************************************************/
-static void cmd_newer(void)
+static int cmd_newer(void)
 {
        fstring buf;
        BOOL ok;
@@ -1526,14 +1797,18 @@ static void cmd_newer(void)
                newer_than = 0;
        }
 
-       if (ok && newer_than == 0)
+       if (ok && newer_than == 0) {
                d_printf("Error setting newer-than time\n");
+               return 1;
+       }
+
+       return 0;
 }
 
 /****************************************************************************
 set the archive level
 ****************************************************************************/
-static void cmd_archive(void)
+static int cmd_archive(void)
 {
        fstring buf;
 
@@ -1541,15 +1816,19 @@ static void cmd_archive(void)
                archive_level = atoi(buf);
        } else
                d_printf("Archive level is %d\n",archive_level);
+
+       return 0;
 }
 
 /****************************************************************************
 toggle the lowercaseflag
 ****************************************************************************/
-static void cmd_lowercase(void)
+static int cmd_lowercase(void)
 {
        lowercase = !lowercase;
        DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
+
+       return 0;
 }
 
 
@@ -1558,27 +1837,31 @@ static void cmd_lowercase(void)
 /****************************************************************************
 toggle the recurse flag
 ****************************************************************************/
-static void cmd_recurse(void)
+static int cmd_recurse(void)
 {
        recurse = !recurse;
        DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
+
+       return 0;
 }
 
 /****************************************************************************
 toggle the translate flag
 ****************************************************************************/
-static void cmd_translate(void)
+static int cmd_translate(void)
 {
        translation = !translation;
        DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
                 translation?"on":"off"));
+
+       return 0;
 }
 
 
 /****************************************************************************
 do a printmode command
 ****************************************************************************/
-static void cmd_printmode(void)
+static int cmd_printmode(void)
 {
        fstring buf;
        fstring mode;
@@ -1608,12 +1891,14 @@ static void cmd_printmode(void)
                }
        
        DEBUG(2,("the printmode is now %s\n",mode));
+
+       return 0;
 }
 
 /****************************************************************************
-do the lcd command
-****************************************************************************/
-static void cmd_lcd(void)
+ do the lcd command
+ ****************************************************************************/
+static int cmd_lcd(void)
 {
        fstring buf;
        pstring d;
@@ -1621,11 +1906,75 @@ static void cmd_lcd(void)
        if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
                chdir(buf);
        DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
+
+       return 0;
 }
 
 /****************************************************************************
-list a share name
-****************************************************************************/
+ get a file restarting at end of local file
+ ****************************************************************************/
+static int cmd_reget(void)
+{
+       pstring local_name;
+       pstring remote_name;
+       char *p;
+
+       pstrcpy(remote_name, cur_dir);
+       pstrcat(remote_name, "\\");
+       
+       p = remote_name + strlen(remote_name);
+       
+       if (!next_token_nr(NULL, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
+               d_printf("reget <filename>\n");
+               return 1;
+       }
+       pstrcpy(local_name, p);
+       dos_clean_name(remote_name);
+       
+       next_token_nr(NULL, local_name, NULL, sizeof(local_name));
+       
+       return do_get(remote_name, local_name, True);
+}
+
+/****************************************************************************
+ put a file restarting at end of local file
+ ****************************************************************************/
+static int cmd_reput(void)
+{
+       pstring local_name;
+       pstring remote_name;
+       fstring buf;
+       char *p = buf;
+       SMB_STRUCT_STAT st;
+       
+       pstrcpy(remote_name, cur_dir);
+       pstrcat(remote_name, "\\");
+  
+       if (!next_token_nr(NULL, p, NULL, sizeof(buf))) {
+               d_printf("reput <filename>\n");
+               return 1;
+       }
+       pstrcpy(local_name, p);
+  
+       if (!file_exist(local_name, &st)) {
+               d_printf("%s does not exist\n", local_name);
+               return 1;
+       }
+
+       if (next_token_nr(NULL, p, NULL, sizeof(buf)))
+               pstrcat(remote_name, p);
+       else
+               pstrcat(remote_name, local_name);
+       
+       dos_clean_name(remote_name);
+
+       return do_put(remote_name, local_name, True);
+}
+
+
+/****************************************************************************
+ list a share name
+ ****************************************************************************/
 static void browse_fn(const char *name, uint32 m, 
                       const char *comment, void *state)
 {
@@ -1644,6 +1993,9 @@ static void browse_fn(const char *name, uint32 m,
           case STYPE_IPC:
             fstrcpy(typestr,"IPC"); break;
         }
+       /* FIXME: If the remote machine returns non-ascii characters
+          in any of these fields, they can corrupt the output.  We
+          should remove them. */
        d_printf("\t%-15.15s%-10.10s%s\n",
                name,typestr,comment);
 }
@@ -1699,56 +2051,68 @@ static BOOL list_servers(char *wk_grp)
 #define COMPL_REMOTE      1          /* Complete remote filename */
 #define COMPL_LOCAL       2          /* Complete local filename */
 
-/* This defines the commands supported by this client */
-struct
+/* This defines the commands supported by this client.
+ * NOTE: The "!" must be the last one in the list because it's fn pointer
+ *       field is NULL, and NULL in that field is used in process_tok()
+ *       (below) to indicate the end of the list.  crh
+ */
+static struct
 {
   char *name;
-  void (*fn)(void);
+  int (*fn)(void);
   char *description;
   char compl_args[2];      /* Completion argument info */
 } commands[] = 
 {
-  {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
+  {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
+  {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
+  {"archive",cmd_archive,"<level>\n0=ignore archive bit\n1=only get archive files\n2=only get archive files and reset archive bit\n3=get all files and reset archive bit",{COMPL_NONE,COMPL_NONE}},
+  {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
+  {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
+  {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
+  {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
+  {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
+  {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
   {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
   {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
-  {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
-  {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
-  {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
+  {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
   {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
+  {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
+  {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
+  {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
+  {"link",cmd_link,"<src> <dest> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
+  {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},  
+  {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
+  {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
+  {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
   {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
-  {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
-  {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
-  {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
+  {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
   {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},  
-  {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
-  {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
+  {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
+  {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
   {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
-  {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
-  {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
-  {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
-  {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
-  {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
-  {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},  
-  {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},  
-  {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},  
-  {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},  
   {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
   {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
+  {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},  
+  {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
+  {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
+  {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
   {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
-  {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
   {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
-  {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
-  {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
-  {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
-  {"archive",cmd_archive,"<level>\n0=ignore archive bit\n1=only get archive files\n2=only get archive files and reset archive bit\n3=get all files and reset archive bit",{COMPL_NONE,COMPL_NONE}},
-  {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
-  {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
-  {"tarmode",cmd_tarmode,
-     "<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
+  {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
+  {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},  
+  {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
+  {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
+  {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
+  {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
+  {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
   {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
-  {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
-  {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
-  {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
+  {"symlink",cmd_symlink,"<src> <dest> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
+  {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
+  {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
+  {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
+  
+  /* Yes, this must be here, see crh's comment above. */
   {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
   {"",NULL,NULL,{COMPL_NONE,COMPL_NONE}}
 };
@@ -1787,7 +2151,7 @@ static int process_tok(fstring tok)
 /****************************************************************************
 help
 ****************************************************************************/
-static NTSTATUS cmd_help(void)
+static int cmd_help(void)
 {
        int i=0,j;
        fstring buf;
@@ -1804,17 +2168,26 @@ static NTSTATUS cmd_help(void)
                        d_printf("\n");
                }
        }
-       return NT_STATUS_OK;
+       return 0;
 }
 
 /****************************************************************************
 process a -c command string
 ****************************************************************************/
-static void process_command_string(char *cmd)
+static int process_command_string(char *cmd)
 {
        pstring line;
        char *ptr;
+       int rc = 0;
 
+       /* establish the connection if not already */
+       
+       if (!cli) {
+               cli = do_connect(desthost, service);
+               if (!cli)
+                       return 0;
+       }
+       
        while (cmd[0] != '\0')    {
                char *p;
                fstring tok;
@@ -1836,13 +2209,15 @@ static void process_command_string(char *cmd)
                if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
                
                if ((i = process_tok(tok)) >= 0) {
-                       commands[i].fn();
+                       rc = commands[i].fn();
                } else if (i == -2) {
                        d_printf("%s: command abbreviation ambiguous\n",tok);
                } else {
                        d_printf("%s: command not found\n",tok);
                }
        }
+       
+       return rc;
 }      
 
 /****************************************************************************
@@ -1872,7 +2247,7 @@ static char **completion_fn(char *text, int start, int end)
        }
 
        if (count == 2) {
-               free(matches[0]);
+               SAFE_FREE(matches[0]);
                matches[0] = strdup(matches[1]);
        }
        matches[count] = NULL;
@@ -1902,7 +2277,7 @@ static void readline_callback(void)
 
        timeout.tv_sec = 0;
        timeout.tv_usec = 0;
-       sys_select_intr(cli->fd+1,&fds,&timeout);
+       sys_select_intr(cli->fd+1,&fds,NULL,NULL,&timeout);
                
        /* We deliberately use receive_smb instead of
           client_receive_smb as we want to receive
@@ -1963,13 +2338,12 @@ static void process_stdin(void)
 /***************************************************** 
 return a connection to a server
 *******************************************************/
-struct cli_state *do_connect(const char *server, const char *share)
+static struct cli_state *do_connect(const char *server, const char *share)
 {
        struct cli_state *c;
        struct nmb_name called, calling;
        const char *server_n;
        struct in_addr ip;
-       extern struct in_addr ipzero;
        fstring servicename;
        char *sharename;
        
@@ -1986,13 +2360,13 @@ struct cli_state *do_connect(const char *server, const char *share)
 
        server_n = server;
        
-       ip = ipzero;
+       zero_ip(&ip);
 
        make_nmb_name(&calling, global_myname, 0x0);
        make_nmb_name(&called , server, name_type);
 
  again:
-       ip = ipzero;
+       zero_ip(&ip);
        if (have_ip) ip = dest_ip;
 
        /* have to open a new connection */
@@ -2003,13 +2377,13 @@ struct cli_state *do_connect(const char *server, const char *share)
        }
 
        c->protocol = max_protocol;
+       c->use_kerberos = use_kerberos;
 
        if (!cli_session_request(c, &calling, &called)) {
                char *p;
                d_printf("session request to %s failed (%s)\n", 
                         called.name, cli_errstr(c));
                cli_shutdown(c);
-               free(c);
                if ((p=strchr_m(called.name, '.'))) {
                        *p = 0;
                        goto again;
@@ -2026,7 +2400,6 @@ struct cli_state *do_connect(const char *server, const char *share)
        if (!cli_negprot(c)) {
                d_printf("protocol negotiation failed\n");
                cli_shutdown(c);
-               free(c);
                return NULL;
        }
 
@@ -2042,37 +2415,45 @@ struct cli_state *do_connect(const char *server, const char *share)
                               password, strlen(password),
                               workgroup)) {
                /* if a password was not supplied then try again with a null username */
-               if (password[0] || !username[0] || 
+               if (password[0] || !username[0] || use_kerberos ||
                    !cli_session_setup(c, "", "", 0, "", 0, workgroup)) { 
                        d_printf("session setup failed: %s\n", cli_errstr(c));
                        cli_shutdown(c);
-                       free(c);
                        return NULL;
                }
                d_printf("Anonymous login successful\n");
        }
 
-       /*
-        * These next two lines are needed to emulate
-        * old client behaviour for people who have
-        * scripts based on client output.
-        * QUESTION ? Do we want to have a 'client compatibility
-        * mode to turn these on/off ? JRA.
-        */
-
-       if (*c->server_domain || *c->server_os || *c->server_type){
+       if (*c->server_domain) {
                DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
                        c->server_domain,c->server_os,c->server_type));
+       } else if (*c->server_os || *c->server_type){
+               DEBUG(1,("OS=[%s] Server=[%s]\n",
+                        c->server_os,c->server_type));
        }               
        
        DEBUG(4,(" session setup ok\n"));
 
        if (!cli_send_tconX(c, sharename, "?????",
                            password, strlen(password)+1)) {
-               d_printf("tree connect failed: %s\n", cli_errstr(c));
-               cli_shutdown(c);
-               free(c);
-               return NULL;
+               pstring full_share;
+
+               /*
+                * Some servers require \\server\share for the share
+                * while others are happy with share as we gave above
+                * Lets see if we give it the long form if it works
+                */
+               pstrcpy(full_share, "\\\\");
+               pstrcat(full_share, server);
+               pstrcat(full_share, "\\");
+               pstrcat(full_share, sharename);
+               if (!cli_send_tconX(c, full_share, "?????", password,
+                                       strlen(password) + 1)) {
+
+                       d_printf("tree connect failed: %s\n", cli_errstr(c));
+                       cli_shutdown(c);
+                       return NULL;
+               }
        }
 
        DEBUG(4,(" tconx ok\n"));
@@ -2084,23 +2465,25 @@ struct cli_state *do_connect(const char *server, const char *share)
 /****************************************************************************
   process commands from the client
 ****************************************************************************/
-static BOOL process(char *base_directory)
+static int process(char *base_directory)
 {
+       int rc = 0;
+
        cli = do_connect(desthost, service);
        if (!cli) {
-               return(False);
+               return 1;
        }
 
        if (*base_directory) do_cd(base_directory);
        
        if (cmdstr) {
-               process_command_string(cmdstr);
+               rc = process_command_string(cmdstr);
        } else {
                process_stdin();
        }
   
        cli_shutdown(cli);
-       return(True);
+       return rc;
 }
 
 /****************************************************************************
@@ -2125,6 +2508,7 @@ static void usage(char *pname)
   d_printf("\t-h                    Print this help message.\n");
   d_printf("\t-I dest IP            use this IP to connect to\n");
   d_printf("\t-E                    write messages to stderr instead of stdout\n");
+  d_printf("\t-k                    use kerberos (active directory) authentication\n");
   d_printf("\t-U username           set the network username\n");
   d_printf("\t-L host               get a list of shares available on a host\n");
   d_printf("\t-t terminal code      terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n");
@@ -2223,9 +2607,13 @@ handle a tar operation
 static int do_tar_op(char *base_directory)
 {
        int ret;
-       cli = do_connect(desthost, service);
-       if (!cli)
-               return 1;
+
+       /* do we already have a connection? */
+       if (!cli) {
+               cli = do_connect(desthost, service);    
+               if (!cli)
+                       return 1;
+       }
 
        recurse=True;
 
@@ -2245,16 +2633,21 @@ static int do_message_op(void)
 {
        struct in_addr ip;
        struct nmb_name called, calling;
-
-       ip = ipzero;
+       fstring server_name;
+       char name_type_hex[10];
 
        make_nmb_name(&calling, global_myname, 0x0);
        make_nmb_name(&called , desthost, name_type);
 
-       ip = ipzero;
+       safe_strcpy(server_name, desthost, sizeof(server_name));
+       snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
+       safe_strcat(server_name, name_type_hex, sizeof(server_name));
+
+        zero_ip(&ip);
        if (have_ip) ip = dest_ip;
 
-       if (!(cli=cli_initialise(NULL)) || (cli_set_port(cli, port) != port) || !cli_connect(cli, desthost, &ip)) {
+       if (!(cli=cli_initialise(NULL)) || (cli_set_port(cli, port) != port) ||
+           !cli_connect(cli, server_name, &ip)) {
                d_printf("Connection to %s failed\n", desthost);
                return 1;
        }
@@ -2272,6 +2665,27 @@ static int do_message_op(void)
 }
 
 
+/**
+ * Process "-L hostname" option.
+ *
+ * We don't actually do anything yet -- we just stash the name in a
+ * global variable and do the query when all options have been read.
+ **/
+static void remember_query_host(const char *arg,
+                               pstring query_host)
+{
+       char *slash;
+       
+       while (*arg == '\\' || *arg == '/')
+               arg++;
+       pstrcpy(query_host, arg);
+       if ((slash = strchr(query_host, '/'))
+           || (slash = strchr(query_host, '\\'))) {
+               *slash = 0;
+       }
+}
+
+
 /****************************************************************************
   main program
 ****************************************************************************/
@@ -2280,17 +2694,17 @@ static int do_message_op(void)
        fstring base_directory;
        char *pname = argv[0];
        int opt;
-       extern XFILE *dbf;
        extern char *optarg;
        extern int optind;
        int old_debug;
        pstring query_host;
        BOOL message = False;
        extern char tar_type;
-       static pstring servicesf = CONFIGFILE;
        pstring term_code;
        pstring new_name_resolve_order;
+       pstring logfile;
        char *p;
+       int rc = 0;
 
 #ifdef KANJI
        pstrcpy(term_code, KANJI);
@@ -2304,6 +2718,7 @@ static int do_message_op(void)
        *new_name_resolve_order = 0;
 
        DEBUGLEVEL = 2;
+       AllowDebugChange = False;
  
        setup_logging(pname,True);
 
@@ -2318,12 +2733,12 @@ static int do_message_op(void)
                        dbf = x_stderr;
                else if(strncmp(argv[opt], "-s", 2) == 0) {
                        if(argv[opt][2] != '\0')
-                               pstrcpy(servicesf, &argv[opt][2]);
+                               pstrcpy(dyn_CONFIGFILE, &argv[opt][2]);
                        else if(argv[opt+1] != NULL) {
                                /*
                                 * At least one more arg left.
                                 */
-                               pstrcpy(servicesf, argv[opt+1]);
+                               pstrcpy(dyn_CONFIGFILE, argv[opt+1]);
                        } else {
                                usage(pname);
                                exit(1);
@@ -2331,20 +2746,15 @@ static int do_message_op(void)
                }
        }
 
-       TimeInit();
-
        in_client = True;   /* Make sure that we tell lp_load we are */
 
        old_debug = DEBUGLEVEL;
-       if (!lp_load(servicesf,True,False,False)) {
-               fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
+       if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
+               fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
+                       prog_name, dyn_CONFIGFILE);
        }
        DEBUGLEVEL = old_debug;
        
-#ifdef WITH_SSL
-       sslutil_init(0);
-#endif
-
        pstrcpy(workgroup,lp_workgroup());
 
        load_interfaces();
@@ -2363,7 +2773,6 @@ static int do_message_op(void)
                        got_pass = True;
                        memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(password));
                }
-               strupper(username);
        }
 
        /* modification to support PASSWD environmental var
@@ -2380,7 +2789,6 @@ static int do_message_op(void)
 
        if (*username == 0 && getenv("LOGNAME")) {
                pstrcpy(username,getenv("LOGNAME"));
-               strupper(username);
        }
 
        if (*username == 0) {
@@ -2391,7 +2799,13 @@ static int do_message_op(void)
                usage(pname);
                exit(1);
        }
-  
+
+       /* FIXME: At the moment, if the user should happen to give the
+        * options ahead of the service name (in standard Unix
+        * fashion) then smbclient just spits out the usage message
+        * with no explanation of what in particular was wrong.  Is
+        * there any reason we can't just parse out the service name
+        * and password after running getopt?? -- mbp */
        if (*argv[1] != '-') {
                pstrcpy(service,argv[1]);  
                /* Convert any '/' characters in the service name to '\' characters */
@@ -2415,10 +2829,10 @@ static int do_message_op(void)
        }
 
        while ((opt = 
-               getopt(argc, argv,"s:O:R:M:i:Nn:d:Pp:l:hI:EU:L:t:m:W:T:D:c:b:A:")) != EOF) {
+               getopt(argc, argv,"s:O:R:M:i:Nn:d:Pp:l:hI:EU:L:t:m:W:T:D:c:b:A:k")) != EOF) {
                switch (opt) {
                case 's':
-                       pstrcpy(servicesf, optarg);
+                       pstrcpy(dyn_CONFIGFILE, optarg);
                        break;
                case 'O':
                        pstrcpy(user_socket_options,optarg);
@@ -2457,7 +2871,8 @@ static int do_message_op(void)
                        port = atoi(optarg);
                        break;
                case 'l':
-                       slprintf(debugf,sizeof(debugf)-1, "%s.client",optarg);
+                       slprintf(logfile,sizeof(logfile)-1, "%s.client",optarg);
+                       lp_set_logfile(logfile);
                        break;
                case 'h':
                        usage(pname);
@@ -2466,7 +2881,7 @@ static int do_message_op(void)
                case 'I':
                        {
                                dest_ip = *interpret_addr2(optarg);
-                               if (zero_ip(dest_ip))
+                               if (is_zero_ip(dest_ip))
                                        exit(1);
                                have_ip = True;
                        }
@@ -2536,7 +2951,8 @@ static int do_message_op(void)
                                        }
                                        else if (strwicmp("username", param) == 0)
                                                pstrcpy(username, val);
-                                               
+                                       else if (strwicmp("domain", param) == 0)
+                                               pstrcpy(workgroup,val);
                                        memset(buf, 0, sizeof(buf));
                                }
                                x_fclose(auth);
@@ -2544,10 +2960,7 @@ static int do_message_op(void)
                        break;
 
                case 'L':
-                       p = optarg;
-                       while(*p == '\\' || *p == '/')
-                               p++;
-                       pstrcpy(query_host,p);
+                       remember_query_host(optarg, query_host);
                        break;
                case 't':
                        pstrcpy(term_code, optarg);
@@ -2573,6 +2986,15 @@ static int do_message_op(void)
                case 'b':
                        io_bufsize = MAX(1, atoi(optarg));
                        break;
+               case 'k':
+#ifdef HAVE_KRB5
+                       use_kerberos = True;
+                       got_pass = True;
+#else
+                       d_printf("No kerberos support compiled in\n");
+                       exit(1);
+#endif
+                       break;
                default:
                        usage(pname);
                        exit(1);
@@ -2611,9 +3033,9 @@ static int do_message_op(void)
                return do_message_op();
        }
        
-       if (!process(base_directory)) {
-               return(1);
+       if (process(base_directory)) {
+               return 1;
        }
 
-       return(0);
+       return rc;
 }