r4207: remove "lookupname" and "lookupsid", and instead have a single "lookup" comman...
[samba.git] / source4 / client / client.c
index 928a331b0c58858a0ed87c22727d99e05acfdd48..a83ff6d29ec0c28300c3fa1f99d884e654aef5b5 100644 (file)
 */
 
 #include "includes.h"
+#include "dynconfig.h"
+#include "clilist.h"
+#include "lib/cmdline/popt_common.h"
+#include "librpc/gen_ndr/ndr_srvsvc.h"
+#include "librpc/gen_ndr/ndr_lsa.h"
+#include "libcli/raw/libcliraw.h"
+#include "system/time.h"
+#include "system/dir.h"
+#include "system/filesys.h"
+#include "dlinklist.h"
+
 #ifndef REGISTER
 #define REGISTER 0
 #endif
@@ -34,9 +45,8 @@ static pstring cd_path = "";
 static pstring service;
 static pstring desthost;
 static pstring username;
+static pstring domain;
 static pstring password;
-static BOOL use_kerberos;
-static BOOL got_pass;
 static char *cmdstr = NULL;
 
 static int io_bufsize = 64512;
@@ -44,7 +54,7 @@ static int io_bufsize = 64512;
 static int name_type = 0x20;
 
 static int process_tok(fstring tok);
-static int cmd_help(void);
+static int cmd_help(const char **cmd_ptr);
 
 /* 30 second timeout on most commands */
 #define CLIENT_TIMEOUT (30*1000)
@@ -129,8 +139,9 @@ void dos_clean_name(char *s)
 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.
 ****************************************************************************/
-static int writefile(int f, char *b, int n)
+static int writefile(int f, const void *_b, int n)
 {
+       const uint8_t *b = _b;
        int i;
 
        if (!translation) {
@@ -156,8 +167,9 @@ static int writefile(int f, char *b, int n)
   read from a file with LF->CR/LF translation if appropriate. return the 
   number read. read approx n bytes.
 ****************************************************************************/
-static int readfile(char *b, int n, XFILE *f)
+static int readfile(void *_b, int n, XFILE *f)
 {
+       uint8_t *b = _b;
        int i;
        int c;
 
@@ -165,7 +177,7 @@ static int readfile(char *b, int n, XFILE *f)
                return x_fread(b,1,n,f);
   
        i = 0;
-       while (i < (n - 1) && (i < CLI_BUFFER_SIZE)) {
+       while (i < (n - 1)) {
                if ((c = x_getc(f)) == EOF) {
                        break;
                }
@@ -253,13 +265,20 @@ static int do_dskattr(void)
 /****************************************************************************
 show cd/pwd
 ****************************************************************************/
-static int cmd_pwd(void)
+static int cmd_pwd(const char **cmd_ptr)
 {
        d_printf("Current directory is %s",service);
        d_printf("%s\n",cur_dir);
        return 0;
 }
 
+/*
+  convert a string to dos format
+*/
+static void dos_format(char *s)
+{
+       string_replace(s, '/', '\\');
+}
 
 /****************************************************************************
 change directory - inner section
@@ -302,12 +321,12 @@ static int do_cd(char *newdir)
 /****************************************************************************
 change directory
 ****************************************************************************/
-static int cmd_cd(void)
+static int cmd_cd(const char **cmd_ptr)
 {
        fstring buf;
        int rc = 0;
 
-       if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
+       if (next_token(cmd_ptr,buf,NULL,sizeof(buf)))
                rc = do_cd(buf);
        else
                d_printf("Current directory is %s\n",cur_dir);
@@ -316,12 +335,35 @@ static int cmd_cd(void)
 }
 
 
+BOOL mask_match(struct smbcli_state *c, const char *string, char *pattern, 
+               BOOL is_case_sensitive)
+{
+       fstring p2, s2;
+
+       if (strcmp(string,"..") == 0)
+               string = ".";
+       if (strcmp(pattern,".") == 0)
+               return False;
+       
+       if (is_case_sensitive)
+               return ms_fnmatch(pattern, string, 
+                                 c->transport->negotiate.protocol) == 0;
+
+       fstrcpy(p2, pattern);
+       fstrcpy(s2, string);
+       strlower(p2); 
+       strlower(s2);
+       return ms_fnmatch(p2, s2, c->transport->negotiate.protocol) == 0;
+}
+
+
+
 /*******************************************************************
   decide if a file should be operated on
   ********************************************************************/
-static BOOL do_this_one(file_info *finfo)
+static BOOL do_this_one(struct clilist_file_info *finfo)
 {
-       if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) return(True);
+       if (finfo->attrib & FILE_ATTRIBUTE_DIRECTORY) return(True);
 
        if (*fileselection && 
            !mask_match(cli, finfo->name,fileselection,False)) {
@@ -334,7 +376,7 @@ static BOOL do_this_one(file_info *finfo)
                return(False);
        }
 
-       if ((archive_level==1 || archive_level==2) && !(finfo->mode & FILE_ATTRIBUTE_ARCHIVE)) {
+       if ((archive_level==1 || archive_level==2) && !(finfo->attrib & FILE_ATTRIBUTE_ARCHIVE)) {
                DEBUG(3,("archive %s failed\n", finfo->name));
                return(False);
        }
@@ -342,57 +384,21 @@ static BOOL do_this_one(file_info *finfo)
        return(True);
 }
 
-/*******************************************************************
- Return a string representing an attribute for a file.
-********************************************************************/
-static const char *attrib_string(uint16_t mode)
-{
-       static fstring attrstr;
-       int i, len;
-       const struct {
-               char c;
-               uint16_t attr;
-       } attr_strs[] = {
-               {'V', FILE_ATTRIBUTE_VOLUME},
-               {'D', FILE_ATTRIBUTE_DIRECTORY},
-               {'A', FILE_ATTRIBUTE_ARCHIVE},
-               {'H', FILE_ATTRIBUTE_HIDDEN},
-               {'S', FILE_ATTRIBUTE_SYSTEM},
-               {'R', FILE_ATTRIBUTE_READONLY},
-               {'d', FILE_ATTRIBUTE_DEVICE},
-               {'t', FILE_ATTRIBUTE_TEMPORARY},
-               {'s', FILE_ATTRIBUTE_SPARSE},
-               {'r', FILE_ATTRIBUTE_REPARSE_POINT},
-               {'c', FILE_ATTRIBUTE_COMPRESSED},
-               {'o', FILE_ATTRIBUTE_OFFLINE},
-               {'n', FILE_ATTRIBUTE_NONINDEXED},
-               {'e', FILE_ATTRIBUTE_ENCRYPTED}
-       };
-
-       for (len=i=0; i<ARRAY_SIZE(attr_strs); i++) {
-               if (mode & attr_strs[i].attr) {
-                       attrstr[len++] = attr_strs[i].c;
-               }
-       }
-
-       attrstr[len] = 0;
-
-       return(attrstr);
-}
-
 /****************************************************************************
   display info about a file
   ****************************************************************************/
-static void display_finfo(file_info *finfo)
+static void display_finfo(struct clilist_file_info *finfo)
 {
        if (do_this_one(finfo)) {
                time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
+               char *astr = attrib_string(NULL, finfo->attrib);
                d_printf("  %-30s%7.7s %8.0f  %s",
                         finfo->name,
-                        attrib_string(finfo->mode),
+                        astr,
                         (double)finfo->size,
                         asctime(localtime(&t)));
                dir_total += finfo->size;
+               talloc_free(astr);
        }
 }
 
@@ -400,7 +406,7 @@ static void display_finfo(file_info *finfo)
 /****************************************************************************
    accumulate size of a file
   ****************************************************************************/
-static void do_du(file_info *finfo)
+static void do_du(struct clilist_file_info *finfo)
 {
        if (do_this_one(finfo)) {
                dir_total += finfo->size;
@@ -413,7 +419,7 @@ static char *do_list_queue = 0;
 static long do_list_queue_size = 0;
 static long do_list_queue_start = 0;
 static long do_list_queue_end = 0;
-static void (*do_list_fn)(file_info *);
+static void (*do_list_fn)(struct clilist_file_info *);
 
 /****************************************************************************
 functions for do_list_queue
@@ -485,7 +491,7 @@ static void add_to_do_list_queue(const char* entry)
                do_list_queue_size *= 2;
                DEBUG(4,("enlarging do_list_queue to %d\n",
                         (int)do_list_queue_size));
-               dlq = Realloc(do_list_queue, do_list_queue_size);
+               dlq = realloc_p(do_list_queue, char, do_list_queue_size);
                if (! dlq) {
                        d_printf("failure enlarging do_list_queue to %d bytes\n",
                                 (int)do_list_queue_size);
@@ -532,9 +538,9 @@ static int do_list_queue_empty(void)
 /****************************************************************************
 a helper for do_list
   ****************************************************************************/
-static void do_list_helper(file_info *f, const char *mask, void *state)
+static void do_list_helper(struct clilist_file_info *f, const char *mask, void *state)
 {
-       if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
+       if (f->attrib & FILE_ATTRIBUTE_DIRECTORY) {
                if (do_list_dirs && do_this_one(f)) {
                        do_list_fn(f);
                }
@@ -564,7 +570,8 @@ static void do_list_helper(file_info *f, const char *mask, void *state)
 /****************************************************************************
 a wrapper around smbcli_list that adds recursion
   ****************************************************************************/
-void do_list(const char *mask,uint16_t attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
+void do_list(const char *mask,uint16_t attribute,
+            void (*fn)(struct clilist_file_info *),BOOL rec, BOOL dirs)
 {
        static int in_do_list = 0;
 
@@ -633,7 +640,7 @@ void do_list(const char *mask,uint16_t attribute,void (*fn)(file_info *),BOOL re
 /****************************************************************************
   get a directory listing
   ****************************************************************************/
-static int cmd_dir(void)
+static int cmd_dir(const char **cmd_ptr)
 {
        uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
        pstring mask;
@@ -646,7 +653,7 @@ static int cmd_dir(void)
        if(mask[strlen(mask)-1]!='\\')
                pstrcat(mask,"\\");
        
-       if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                dos_format(p);
                if (*p == '\\')
                        pstrcpy(mask,p);
@@ -654,7 +661,12 @@ static int cmd_dir(void)
                        pstrcat(mask,p);
        }
        else {
-               pstrcat(mask,"*");
+               if (cli->tree->session->transport->negotiate.protocol <= 
+                   PROTOCOL_LANMAN1) { 
+                       pstrcat(mask,"*.*");
+               } else {
+                       pstrcat(mask,"*");
+               }
        }
 
        do_list(mask, attribute, display_finfo, recurse, True);
@@ -670,7 +682,7 @@ static int cmd_dir(void)
 /****************************************************************************
   get a directory listing
   ****************************************************************************/
-static int cmd_du(void)
+static int cmd_du(const char **cmd_ptr)
 {
        uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
        pstring mask;
@@ -683,14 +695,14 @@ static int cmd_du(void)
        if(mask[strlen(mask)-1]!='\\')
                pstrcat(mask,"\\");
        
-       if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                dos_format(p);
                if (*p == '\\')
                        pstrcpy(mask,p);
                else
                        pstrcat(mask,p);
        } else {
-               pstrcat(mask,"*");
+               pstrcat(mask,"\\*");
        }
 
        do_list(mask, attribute, do_du, recurse, True);
@@ -710,7 +722,7 @@ static int do_get(char *rname, const char *lname, BOOL reget)
 {  
        int handle = 0, fnum;
        BOOL newhandle = False;
-       char *data;
+       uint8_t *data;
        struct timeval tp_start;
        int read_size = io_bufsize;
        uint16_t attr;
@@ -722,7 +734,7 @@ static int do_get(char *rname, const char *lname, BOOL reget)
        GetTimeOfDay(&tp_start);
 
        if (lowercase) {
-               strlower(lname);
+               strlower(discard_const_p(char, lname));
        }
 
        fnum = smbcli_open(cli->tree, rname, O_RDONLY, DENY_NONE);
@@ -736,16 +748,16 @@ static int do_get(char *rname, const char *lname, BOOL reget)
                handle = fileno(stdout);
        } else {
                if (reget) {
-                       handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
+                       handle = open(lname, O_WRONLY|O_CREAT, 0644);
                        if (handle >= 0) {
-                               start = sys_lseek(handle, 0, SEEK_END);
+                               start = 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);
+                       handle = open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
                }
                newhandle = True;
        }
@@ -766,7 +778,7 @@ static int do_get(char *rname, const char *lname, BOOL reget)
        DEBUG(2,("getting file %s of size %.0f as %s ", 
                 rname, (double)size, lname));
 
-       if(!(data = (char *)malloc(read_size))) { 
+       if(!(data = (uint8_t *)malloc(read_size))) { 
                d_printf("malloc fail for size %d\n", read_size);
                smbcli_close(cli->tree, fnum);
                return 1;
@@ -831,7 +843,7 @@ static int do_get(char *rname, const char *lname, BOOL reget)
 /****************************************************************************
   get a file
   ****************************************************************************/
-static int cmd_get(void)
+static int cmd_get(const char **cmd_ptr)
 {
        pstring lname;
        pstring rname;
@@ -842,23 +854,39 @@ static int cmd_get(void)
        
        p = rname + strlen(rname);
        
-       if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
+       if (!next_token(cmd_ptr,p,NULL,sizeof(rname)-strlen(rname))) {
                d_printf("get <filename>\n");
                return 1;
        }
        pstrcpy(lname,p);
        dos_clean_name(rname);
        
-       next_token_nr(NULL,lname,NULL,sizeof(lname));
+       next_token(cmd_ptr,lname,NULL,sizeof(lname));
        
        return do_get(rname, lname, False);
 }
 
+/****************************************************************************
+ Put up a yes/no prompt.
+****************************************************************************/
+static BOOL yesno(char *p)
+{
+       pstring ans;
+       printf("%s",p);
+
+       if (!fgets(ans,sizeof(ans)-1,stdin))
+               return(False);
+
+       if (*ans == 'y' || *ans == 'Y')
+               return(True);
+
+       return(False);
+}
 
 /****************************************************************************
   do a mget operation on one file
   ****************************************************************************/
-static void do_mget(file_info *finfo)
+static void do_mget(struct clilist_file_info *finfo)
 {
        pstring rname;
        pstring quest;
@@ -873,7 +901,7 @@ static void do_mget(file_info *finfo)
                return;
        }
 
-       if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY)
+       if (finfo->attrib & FILE_ATTRIBUTE_DIRECTORY)
                slprintf(quest,sizeof(pstring)-1,
                         "Get directory %s? ",finfo->name);
        else
@@ -882,7 +910,7 @@ static void do_mget(file_info *finfo)
 
        if (prompt && !yesno(quest)) return;
 
-       if (!(finfo->mode & FILE_ATTRIBUTE_DIRECTORY)) {
+       if (!(finfo->attrib & FILE_ATTRIBUTE_DIRECTORY)) {
                pstrcpy(rname,cur_dir);
                pstrcat(rname,finfo->name);
                do_get(rname, finfo->name, False);
@@ -895,9 +923,10 @@ static void do_mget(file_info *finfo)
        pstrcat(cur_dir,finfo->name);
        pstrcat(cur_dir,"\\");
 
-       unix_format(finfo->name);
-       if (lowercase)
-               strlower(finfo->name);
+       string_replace(discard_const_p(char, finfo->name), '\\', '/');
+       if (lowercase) {
+               strlower(discard_const_p(char, finfo->name));
+       }
        
        if (!directory_exist(finfo->name,NULL) && 
            mkdir(finfo->name,0777) != 0) {
@@ -924,7 +953,7 @@ static void do_mget(file_info *finfo)
 /****************************************************************************
 view the file using the pager
 ****************************************************************************/
-static int cmd_more(void)
+static int cmd_more(const char **cmd_ptr)
 {
        fstring rname,lname,pager_cmd;
        char *pager;
@@ -942,7 +971,7 @@ static int cmd_more(void)
        }
        close(fd);
 
-       if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
+       if (!next_token(cmd_ptr,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
                d_printf("more <filename>\n");
                unlink(lname);
                return 1;
@@ -966,7 +995,7 @@ static int cmd_more(void)
 /****************************************************************************
 do a mget command
 ****************************************************************************/
-static int cmd_mget(void)
+static int cmd_mget(const char **cmd_ptr)
 {
        uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
        pstring mget_mask;
@@ -980,7 +1009,7 @@ static int cmd_mget(void)
        
        abort_mget = False;
 
-       while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
+       while (next_token(cmd_ptr,p,NULL,sizeof(buf))) {
                pstrcpy(mget_mask,cur_dir);
                if(mget_mask[strlen(mget_mask)-1]!='\\')
                        pstrcat(mget_mask,"\\");
@@ -1040,7 +1069,7 @@ static BOOL do_altname(char *name)
 /****************************************************************************
  Exit client.
 ****************************************************************************/
-static int cmd_quit(void)
+static int cmd_quit(const char **cmd_ptr)
 {
        smbcli_shutdown(cli);
        exit(0);
@@ -1052,7 +1081,7 @@ static int cmd_quit(void)
 /****************************************************************************
   make a directory
   ****************************************************************************/
-static int cmd_mkdir(void)
+static int cmd_mkdir(const char **cmd_ptr)
 {
        pstring mask;
        fstring buf;
@@ -1060,7 +1089,7 @@ static int cmd_mkdir(void)
   
        pstrcpy(mask,cur_dir);
 
-       if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,p,NULL,sizeof(buf))) {
                if (!recurse)
                        d_printf("mkdir <dirname>\n");
                return 1;
@@ -1094,7 +1123,7 @@ static int cmd_mkdir(void)
 /****************************************************************************
   show alt name
   ****************************************************************************/
-static int cmd_altname(void)
+static int cmd_altname(const char **cmd_ptr)
 {
        pstring name;
        fstring buf;
@@ -1102,7 +1131,7 @@ static int cmd_altname(void)
   
        pstrcpy(name,cur_dir);
 
-       if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,p,NULL,sizeof(buf))) {
                d_printf("altname <file>\n");
                return 1;
        }
@@ -1123,7 +1152,7 @@ static int do_put(char *rname, char *lname, BOOL reput)
        XFILE *f;
        size_t start = 0;
        off_t nread = 0;
-       char *buf = NULL;
+       uint8_t *buf = NULL;
        int maxwrite = io_bufsize;
        int rc = 0;
        
@@ -1176,7 +1205,7 @@ static int do_put(char *rname, char *lname, BOOL reput)
        DEBUG(1,("putting file %s as %s ",lname,
                 rname));
   
-       buf = (char *)malloc(maxwrite);
+       buf = (uint8_t *)malloc(maxwrite);
        if (!buf) {
                d_printf("ERROR: Not enough memory!\n");
                return 1;
@@ -1248,7 +1277,7 @@ static int do_put(char *rname, char *lname, BOOL reput)
 /****************************************************************************
   put a file
   ****************************************************************************/
-static int cmd_put(void)
+static int cmd_put(const char **cmd_ptr)
 {
        pstring lname;
        pstring rname;
@@ -1258,13 +1287,13 @@ static int cmd_put(void)
        pstrcpy(rname,cur_dir);
        pstrcat(rname,"\\");
   
-       if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,p,NULL,sizeof(buf))) {
                d_printf("put <filename>\n");
                return 1;
        }
        pstrcpy(lname,p);
   
-       if (next_token_nr(NULL,p,NULL,sizeof(buf)))
+       if (next_token(cmd_ptr,p,NULL,sizeof(buf)))
                pstrcat(rname,p);      
        else
                pstrcat(rname,lname);
@@ -1272,7 +1301,7 @@ static int cmd_put(void)
        dos_clean_name(rname);
 
        {
-               SMB_STRUCT_STAT st;
+               struct stat st;
                /* allow '-' to represent stdin
                   jdblair, 24.jun.98 */
                if (!file_exist(lname,&st) &&
@@ -1332,14 +1361,52 @@ static BOOL seek_list(struct file_list *list, char *name)
 /****************************************************************************
   set the file selection mask
   ****************************************************************************/
-static int cmd_select(void)
+static int cmd_select(const char **cmd_ptr)
 {
        pstrcpy(fileselection,"");
-       next_token_nr(NULL,fileselection,NULL,sizeof(fileselection));
+       next_token(cmd_ptr,fileselection,NULL,sizeof(fileselection));
 
        return 0;
 }
 
+/*******************************************************************
+  A readdir wrapper which just returns the file name.
+ ********************************************************************/
+static const char *readdirname(DIR *p)
+{
+       struct dirent *ptr;
+       char *dname;
+
+       if (!p)
+               return(NULL);
+  
+       ptr = (struct dirent *)readdir(p);
+       if (!ptr)
+               return(NULL);
+
+       dname = ptr->d_name;
+
+#ifdef NEXT2
+       if (telldir(p) < 0)
+               return(NULL);
+#endif
+
+#ifdef HAVE_BROKEN_READDIR
+       /* using /usr/ucb/cc is BAD */
+       dname = dname - 2;
+#endif
+
+       {
+               static pstring buf;
+               int len = NAMLEN(ptr);
+               memcpy(buf, dname, len);
+               buf[len] = 0;
+               dname = buf;
+       }
+
+       return(dname);
+}
+
 /****************************************************************************
   Recursive file matching function act as find
   match must be always set to True when calling this function
@@ -1385,7 +1452,7 @@ static int file_find(struct file_list **list, const char *directory,
                                        return -1;
                                }
                        }
-                       entry = (struct file_list *) malloc(sizeof (struct file_list));
+                       entry = malloc_p(struct file_list);
                        if (!entry) {
                                d_printf("Out of memory in file_find\n");
                                closedir(dir);
@@ -1406,12 +1473,12 @@ static int file_find(struct file_list **list, const char *directory,
 /****************************************************************************
   mput some files
   ****************************************************************************/
-static int cmd_mput(void)
+static int cmd_mput(const char **cmd_ptr)
 {
        fstring buf;
        char *p=buf;
        
-       while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
+       while (next_token(cmd_ptr,p,NULL,sizeof(buf))) {
                int ret;
                struct file_list *temp_list;
                char *quest, *lname, *rname;
@@ -1499,19 +1566,19 @@ static int do_cancel(int job)
 /****************************************************************************
   cancel a print job
   ****************************************************************************/
-static int cmd_cancel(void)
+static int cmd_cancel(const char **cmd_ptr)
 {
        fstring buf;
        int job; 
 
-       if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                d_printf("cancel <jobid> ...\n");
                return 1;
        }
        do {
                job = atoi(buf);
                do_cancel(job);
-       } while (next_token_nr(NULL,buf,NULL,sizeof(buf)));
+       } while (next_token(cmd_ptr,buf,NULL,sizeof(buf)));
        
        return 0;
 }
@@ -1520,13 +1587,13 @@ static int cmd_cancel(void)
 /****************************************************************************
   print a file
   ****************************************************************************/
-static int cmd_print(void)
+static int cmd_print(const char **cmd_ptr)
 {
        pstring lname;
        pstring rname;
        char *p;
 
-       if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
+       if (!next_token(cmd_ptr,lname,NULL, sizeof(lname))) {
                d_printf("print <filename>\n");
                return 1;
        }
@@ -1548,7 +1615,7 @@ static int cmd_print(void)
 /****************************************************************************
  show a print queue
 ****************************************************************************/
-static int cmd_queue(void)
+static int cmd_queue(const char **cmd_ptr)
 {
        d_printf("REWRITE: print job queue not implemented\n");
        
@@ -1558,25 +1625,7 @@ static int cmd_queue(void)
 /****************************************************************************
 delete some files
 ****************************************************************************/
-static void do_del(file_info *finfo)
-{
-       pstring mask;
-
-       pstrcpy(mask,cur_dir);
-       pstrcat(mask,finfo->name);
-
-       if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) 
-               return;
-
-       if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, mask))) {
-               d_printf("%s deleting remote file %s\n",smbcli_errstr(cli->tree),mask);
-       }
-}
-
-/****************************************************************************
-delete some files
-****************************************************************************/
-static int cmd_del(void)
+static int cmd_del(const char **cmd_ptr)
 {
        pstring mask;
        fstring buf;
@@ -1587,13 +1636,15 @@ static int cmd_del(void)
        
        pstrcpy(mask,cur_dir);
        
-       if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                d_printf("del <filename>\n");
                return 1;
        }
        pstrcat(mask,buf);
 
-       do_list(mask, attribute,do_del,False,False);
+       if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, mask))) {
+               d_printf("%s deleting remote file %s\n",smbcli_errstr(cli->tree),mask);
+       }
        
        return 0;
 }
@@ -1602,7 +1653,7 @@ static int cmd_del(void)
 /****************************************************************************
 delete a whole directory tree
 ****************************************************************************/
-static int cmd_deltree(void)
+static int cmd_deltree(const char **cmd_ptr)
 {
        pstring dname;
        fstring buf;
@@ -1610,7 +1661,7 @@ static int cmd_deltree(void)
 
        pstrcpy(dname,cur_dir);
        
-       if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                d_printf("deltree <dirname>\n");
                return 1;
        }
@@ -1632,7 +1683,7 @@ static int cmd_deltree(void)
 /****************************************************************************
 show as much information as possible about a file
 ****************************************************************************/
-static int cmd_allinfo(void)
+static int cmd_allinfo(const char **cmd_ptr)
 {
        pstring fname;
        fstring buf;
@@ -1643,13 +1694,13 @@ static int cmd_allinfo(void)
 
        pstrcpy(fname,cur_dir);
        
-       if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                d_printf("allinfo <filename>\n");
                return 1;
        }
        pstrcat(fname,buf);
 
-       mem_ctx = talloc_init(fname);
+       mem_ctx = talloc_init("%s", fname);
 
        /* first a ALL_INFO QPATHINFO */
        finfo.generic.level = RAW_FILEINFO_ALL_INFO;
@@ -1740,19 +1791,19 @@ done:
 /****************************************************************************
 show any ACL on a file
 ****************************************************************************/
-static int cmd_acl(void)
+static int cmd_acl(const char **cmd_ptr)
 {
        pstring fname;
        fstring buf;
        int ret = 0;
        TALLOC_CTX *mem_ctx;
-       struct smb_query_secdesc query;
+       union smb_fileinfo query;
        NTSTATUS status;
        int fnum;
 
        pstrcpy(fname,cur_dir);
        
-       if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                d_printf("acl <filename>\n");
                return 1;
        }
@@ -1764,35 +1815,229 @@ static int cmd_acl(void)
                return -1;
        }
 
-       mem_ctx = talloc_init(fname);
+       mem_ctx = talloc_init("%s", fname);
 
-       query.in.fnum = fnum;
-       query.in.secinfo_flags = 0x7;
+       query.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
+       query.query_secdesc.in.fnum = fnum;
+       query.query_secdesc.in.secinfo_flags = 0x7;
 
-       status = smb_raw_query_secdesc(cli->tree, mem_ctx, &query);
+       status = smb_raw_fileinfo(cli->tree, mem_ctx, &query);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("%s - %s\n", fname, nt_errstr(status));
                ret = 1;
                goto done;
        }
 
+       NDR_PRINT_DEBUG(security_descriptor, query.query_secdesc.out.sd);
+
        talloc_destroy(mem_ctx);
 
 done:
        return ret;
 }
 
+/****************************************************************************
+lookup a name or sid
+****************************************************************************/
+static int cmd_lookup(const char **cmd_ptr)
+{
+       fstring buf;
+       TALLOC_CTX *mem_ctx = talloc(NULL, 0);
+       NTSTATUS status;
+       struct dom_sid *sid;
+
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
+               d_printf("lookup <name|sid>\n");
+               talloc_free(mem_ctx);
+               return 1;
+       }
+
+       sid = dom_sid_parse_talloc(mem_ctx, buf);
+       if (sid == NULL) {
+               const char *sidstr;
+               status = smblsa_lookup_name(cli, buf, mem_ctx, &sidstr);
+               if (!NT_STATUS_IS_OK(status)) {
+                       d_printf("lsa_LookupNames - %s\n", nt_errstr(status));
+                       talloc_free(mem_ctx);
+                       return 1;
+               }
+
+               d_printf("%s\n", sidstr);
+       } else {
+               const char *name;
+               status = smblsa_lookup_sid(cli, buf, mem_ctx, &name);
+               if (!NT_STATUS_IS_OK(status)) {
+                       d_printf("lsa_LookupSids - %s\n", nt_errstr(status));
+                       talloc_free(mem_ctx);
+                       return 1;
+               }
+
+               d_printf("%s\n", name);
+       }
+
+       talloc_free(mem_ctx);
+
+       return 0;
+}
 
 /****************************************************************************
+show privileges for a user
 ****************************************************************************/
-static int cmd_open(void)
+static int cmd_privileges(const char **cmd_ptr)
+{
+       fstring buf;
+       TALLOC_CTX *mem_ctx = talloc(NULL, 0);
+       NTSTATUS status;
+       struct dom_sid *sid;
+       struct lsa_RightSet rights;
+       unsigned i;
+
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
+               d_printf("privileges <sid|name>\n");
+               talloc_free(mem_ctx);
+               return 1;
+       }
+
+       sid = dom_sid_parse_talloc(mem_ctx, buf);
+       if (sid == NULL) {
+               const char *sid_str;
+               status = smblsa_lookup_name(cli, buf, mem_ctx, &sid_str);
+               if (!NT_STATUS_IS_OK(status)) {
+                       d_printf("lsa_LookupNames - %s\n", nt_errstr(status));
+                       talloc_free(mem_ctx);
+                       return 1;
+               }
+               sid = dom_sid_parse_talloc(mem_ctx, sid_str);
+       }
+
+       status = smblsa_sid_privileges(cli, sid, mem_ctx, &rights);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("lsa_EnumAccountRights - %s\n", nt_errstr(status));
+               talloc_free(mem_ctx);
+               return 1;
+       }
+
+       for (i=0;i<rights.count;i++) {
+               d_printf("\t%s\n", rights.names[i].string);
+       }
+
+       talloc_free(mem_ctx);
+
+       return 0;
+}
+
+
+/****************************************************************************
+add privileges for a user
+****************************************************************************/
+static int cmd_addprivileges(const char **cmd_ptr)
+{
+       fstring buf;
+       TALLOC_CTX *mem_ctx = talloc(NULL, 0);
+       NTSTATUS status;
+       struct dom_sid *sid;
+       struct lsa_RightSet rights;
+
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
+               d_printf("addprivileges <sid> <privilege...>\n");
+               talloc_free(mem_ctx);
+               return 1;
+       }
+
+       sid = dom_sid_parse_talloc(mem_ctx, buf);
+       if (sid == NULL) {
+               const char *sid_str;
+               status = smblsa_lookup_name(cli, buf, mem_ctx, &sid_str);
+               if (!NT_STATUS_IS_OK(status)) {
+                       d_printf("lsa_LookupNames - %s\n", nt_errstr(status));
+                       talloc_free(mem_ctx);
+                       return 1;
+               }
+               sid = dom_sid_parse_talloc(mem_ctx, sid_str);
+       }
+
+       ZERO_STRUCT(rights);
+       while (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
+               rights.names = talloc_realloc_p(mem_ctx, rights.names, 
+                                               struct lsa_String, rights.count+1);
+               rights.names[rights.count].string = talloc_strdup(mem_ctx, buf);
+               rights.count++;
+       }
+
+
+       status = smblsa_sid_add_privileges(cli, sid, mem_ctx, &rights);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("lsa_AddAccountRights - %s\n", nt_errstr(status));
+               talloc_free(mem_ctx);
+               return 1;
+       }
+
+       talloc_free(mem_ctx);
+
+       return 0;
+}
+
+/****************************************************************************
+delete privileges for a user
+****************************************************************************/
+static int cmd_delprivileges(const char **cmd_ptr)
+{
+       fstring buf;
+       TALLOC_CTX *mem_ctx = talloc(NULL, 0);
+       NTSTATUS status;
+       struct dom_sid *sid;
+       struct lsa_RightSet rights;
+
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
+               d_printf("delprivileges <sid> <privilege...>\n");
+               talloc_free(mem_ctx);
+               return 1;
+       }
+
+       sid = dom_sid_parse_talloc(mem_ctx, buf);
+       if (sid == NULL) {
+               const char *sid_str;
+               status = smblsa_lookup_name(cli, buf, mem_ctx, &sid_str);
+               if (!NT_STATUS_IS_OK(status)) {
+                       d_printf("lsa_LookupNames - %s\n", nt_errstr(status));
+                       talloc_free(mem_ctx);
+                       return 1;
+               }
+               sid = dom_sid_parse_talloc(mem_ctx, sid_str);
+       }
+
+       ZERO_STRUCT(rights);
+       while (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
+               rights.names = talloc_realloc_p(mem_ctx, rights.names, 
+                                               struct lsa_String, rights.count+1);
+               rights.names[rights.count].string = talloc_strdup(mem_ctx, buf);
+               rights.count++;
+       }
+
+
+       status = smblsa_sid_del_privileges(cli, sid, mem_ctx, &rights);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("lsa_RemoveAccountRights - %s\n", nt_errstr(status));
+               talloc_free(mem_ctx);
+               return 1;
+       }
+
+       talloc_free(mem_ctx);
+
+       return 0;
+}
+
+
+/****************************************************************************
+****************************************************************************/
+static int cmd_open(const char **cmd_ptr)
 {
        pstring mask;
        fstring buf;
        
        pstrcpy(mask,cur_dir);
        
-       if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                d_printf("open <filename>\n");
                return 1;
        }
@@ -1807,14 +2052,14 @@ static int cmd_open(void)
 /****************************************************************************
 remove a directory
 ****************************************************************************/
-static int cmd_rmdir(void)
+static int cmd_rmdir(const char **cmd_ptr)
 {
        pstring mask;
        fstring buf;
   
        pstrcpy(mask,cur_dir);
        
-       if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                d_printf("rmdir <dirname>\n");
                return 1;
        }
@@ -1831,7 +2076,7 @@ static int cmd_rmdir(void)
 /****************************************************************************
  UNIX hardlink.
 ****************************************************************************/
-static int cmd_link(void)
+static int cmd_link(const char **cmd_ptr)
 {
        pstring src,dest;
        fstring buf,buf2;
@@ -1844,8 +2089,8 @@ static int cmd_link(void)
        pstrcpy(src,cur_dir);
        pstrcpy(dest,cur_dir);
   
-       if (!next_token(NULL,buf,NULL,sizeof(buf)) || 
-           !next_token(NULL,buf2,NULL, sizeof(buf2))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) || 
+           !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
                d_printf("link <src> <dest>\n");
                return 1;
        }
@@ -1865,7 +2110,7 @@ static int cmd_link(void)
  UNIX symlink.
 ****************************************************************************/
 
-static int cmd_symlink(void)
+static int cmd_symlink(const char **cmd_ptr)
 {
        pstring src,dest;
        fstring buf,buf2;
@@ -1878,8 +2123,8 @@ static int cmd_symlink(void)
        pstrcpy(src,cur_dir);
        pstrcpy(dest,cur_dir);
        
-       if (!next_token(NULL,buf,NULL,sizeof(buf)) || 
-           !next_token(NULL,buf2,NULL, sizeof(buf2))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) || 
+           !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
                d_printf("symlink <src> <dest>\n");
                return 1;
        }
@@ -1900,7 +2145,7 @@ static int cmd_symlink(void)
  UNIX chmod.
 ****************************************************************************/
 
-static int cmd_chmod(void)
+static int cmd_chmod(const char **cmd_ptr)
 {
        pstring src;
        mode_t mode;
@@ -1913,8 +2158,8 @@ static int cmd_chmod(void)
 
        pstrcpy(src,cur_dir);
        
-       if (!next_token(NULL,buf,NULL,sizeof(buf)) || 
-           !next_token(NULL,buf2,NULL, sizeof(buf2))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) || 
+           !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
                d_printf("chmod mode file\n");
                return 1;
        }
@@ -1935,7 +2180,7 @@ static int cmd_chmod(void)
  UNIX chown.
 ****************************************************************************/
 
-static int cmd_chown(void)
+static int cmd_chown(const char **cmd_ptr)
 {
        pstring src;
        uid_t uid;
@@ -1949,9 +2194,9 @@ static int cmd_chown(void)
 
        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))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) || 
+           !next_token(cmd_ptr,buf2,NULL, sizeof(buf2)) ||
+           !next_token(cmd_ptr,buf3,NULL, sizeof(buf3))) {
                d_printf("chown uid gid file\n");
                return 1;
        }
@@ -1972,7 +2217,7 @@ static int cmd_chown(void)
 /****************************************************************************
 rename some files
 ****************************************************************************/
-static int cmd_rename(void)
+static int cmd_rename(const char **cmd_ptr)
 {
        pstring src,dest;
        fstring buf,buf2;
@@ -1980,8 +2225,8 @@ static int cmd_rename(void)
        pstrcpy(src,cur_dir);
        pstrcpy(dest,cur_dir);
        
-       if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) || 
-           !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) || 
+           !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
                d_printf("rename <src> <dest>\n");
                return 1;
        }
@@ -2001,7 +2246,7 @@ static int cmd_rename(void)
 /****************************************************************************
 toggle the prompt flag
 ****************************************************************************/
-static int cmd_prompt(void)
+static int cmd_prompt(const char **cmd_ptr)
 {
        prompt = !prompt;
        DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
@@ -2013,14 +2258,14 @@ static int cmd_prompt(void)
 /****************************************************************************
 set the newer than time
 ****************************************************************************/
-static int cmd_newer(void)
+static int cmd_newer(const char **cmd_ptr)
 {
        fstring buf;
        BOOL ok;
-       SMB_STRUCT_STAT sbuf;
+       struct stat sbuf;
 
-       ok = next_token_nr(NULL,buf,NULL,sizeof(buf));
-       if (ok && (sys_stat(buf,&sbuf) == 0)) {
+       ok = next_token(cmd_ptr,buf,NULL,sizeof(buf));
+       if (ok && (stat(buf,&sbuf) == 0)) {
                newer_than = sbuf.st_mtime;
                DEBUG(1,("Getting files newer than %s",
                         asctime(localtime(&newer_than))));
@@ -2039,11 +2284,11 @@ static int cmd_newer(void)
 /****************************************************************************
 set the archive level
 ****************************************************************************/
-static int cmd_archive(void)
+static int cmd_archive(const char **cmd_ptr)
 {
        fstring buf;
 
-       if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                archive_level = atoi(buf);
        } else
                d_printf("Archive level is %d\n",archive_level);
@@ -2054,7 +2299,7 @@ static int cmd_archive(void)
 /****************************************************************************
 toggle the lowercaseflag
 ****************************************************************************/
-static int cmd_lowercase(void)
+static int cmd_lowercase(const char **cmd_ptr)
 {
        lowercase = !lowercase;
        DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
@@ -2068,7 +2313,7 @@ static int cmd_lowercase(void)
 /****************************************************************************
 toggle the recurse flag
 ****************************************************************************/
-static int cmd_recurse(void)
+static int cmd_recurse(const char **cmd_ptr)
 {
        recurse = !recurse;
        DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
@@ -2079,7 +2324,7 @@ static int cmd_recurse(void)
 /****************************************************************************
 toggle the translate flag
 ****************************************************************************/
-static int cmd_translate(void)
+static int cmd_translate(const char **cmd_ptr)
 {
        translation = !translation;
        DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
@@ -2092,12 +2337,12 @@ static int cmd_translate(void)
 /****************************************************************************
 do a printmode command
 ****************************************************************************/
-static int cmd_printmode(void)
+static int cmd_printmode(const char **cmd_ptr)
 {
        fstring buf;
        fstring mode;
 
-       if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                if (strequal(buf,"text")) {
                        printmode = 0;      
                } else {
@@ -2129,12 +2374,12 @@ static int cmd_printmode(void)
 /****************************************************************************
  do the lcd command
  ****************************************************************************/
-static int cmd_lcd(void)
+static int cmd_lcd(const char **cmd_ptr)
 {
        fstring buf;
        pstring d;
        
-       if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
+       if (next_token(cmd_ptr,buf,NULL,sizeof(buf)))
                chdir(buf);
        DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
 
@@ -2144,7 +2389,7 @@ static int cmd_lcd(void)
 /****************************************************************************
  get a file restarting at end of local file
  ****************************************************************************/
-static int cmd_reget(void)
+static int cmd_reget(const char **cmd_ptr)
 {
        pstring local_name;
        pstring remote_name;
@@ -2155,14 +2400,14 @@ static int cmd_reget(void)
        
        p = remote_name + strlen(remote_name);
        
-       if (!next_token_nr(NULL, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
+       if (!next_token(cmd_ptr, 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));
+       next_token(cmd_ptr, local_name, NULL, sizeof(local_name));
        
        return do_get(remote_name, local_name, True);
 }
@@ -2170,18 +2415,18 @@ static int cmd_reget(void)
 /****************************************************************************
  put a file restarting at end of local file
  ****************************************************************************/
-static int cmd_reput(void)
+static int cmd_reput(const char **cmd_ptr)
 {
        pstring local_name;
        pstring remote_name;
        fstring buf;
        char *p = buf;
-       SMB_STRUCT_STAT st;
+       struct stat st;
        
        pstrcpy(remote_name, cur_dir);
        pstrcat(remote_name, "\\");
   
-       if (!next_token_nr(NULL, p, NULL, sizeof(buf))) {
+       if (!next_token(cmd_ptr, p, NULL, sizeof(buf))) {
                d_printf("reput <filename>\n");
                return 1;
        }
@@ -2192,7 +2437,7 @@ static int cmd_reput(void)
                return 1;
        }
 
-       if (next_token_nr(NULL, p, NULL, sizeof(buf)))
+       if (next_token(cmd_ptr, p, NULL, sizeof(buf)))
                pstrcat(remote_name, p);
        else
                pstrcat(remote_name, local_name);
@@ -2260,7 +2505,7 @@ static BOOL browse_host(const char *query_host)
        status = dcerpc_pipe_connect(&p, binding, 
                                     DCERPC_SRVSVC_UUID, 
                                     DCERPC_SRVSVC_VERSION,
-                                    lp_workgroup()
+                                    domain
                                     username, password);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Failed to connect to %s - %s\n", 
@@ -2268,6 +2513,7 @@ static BOOL browse_host(const char *query_host)
                talloc_destroy(mem_ctx);
                return False;
        }
+       talloc_steal(mem_ctx, p);
 
        r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
        r.in.level = 1;
@@ -2291,10 +2537,11 @@ static BOOL browse_host(const char *query_host)
                }
        } while (NT_STATUS_IS_OK(status) && W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA));
 
+       talloc_destroy(mem_ctx);
+
        if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
                d_printf("Failed NetShareEnumAll %s - %s/%s\n", 
                         binding, nt_errstr(status), win_errstr(r.out.result));
-               talloc_destroy(mem_ctx);
                return False;
        }
 
@@ -2304,7 +2551,7 @@ static BOOL browse_host(const char *query_host)
 /****************************************************************************
 try and browse available connections on a host
 ****************************************************************************/
-static BOOL list_servers(char *wk_grp)
+static BOOL list_servers(const char *wk_grp)
 {
        d_printf("REWRITE: list servers not implemented\n");
        return False;
@@ -2324,22 +2571,23 @@ static BOOL list_servers(char *wk_grp)
 static struct
 {
   const char *name;
-  int (*fn)(void);
+  int (*fn)(const char **cmd_ptr);
   const char *description;
   char compl_args[2];      /* Completion argument info */
 } commands[] = 
 {
   {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
+  {"addprivileges",cmd_addprivileges,"<sid|user> <privilege...> add privileges for a user",{COMPL_NONE,COMPL_NONE}},
   {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
   {"acl",cmd_acl,"<file> show file ACL",{COMPL_NONE,COMPL_NONE}},
   {"allinfo",cmd_allinfo,"<file> show all possible info about a file",{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}},
+  {"delprivileges",cmd_delprivileges,"<sid|user> <privilege...> remove privileges for a user",{COMPL_NONE,COMPL_NONE}},
   {"deltree",cmd_deltree,"<dir> delete a whole directory tree",{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}},
@@ -2349,6 +2597,7 @@ static struct
   {"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}},
+  {"lookup",cmd_lookup,"<name|sid> show SID for name or name for SID",{COMPL_NONE,COMPL_NONE}},
   {"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}},
@@ -2359,6 +2608,7 @@ static struct
   {"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}},
+  {"privileges",cmd_privileges,"<user> show privileges for a user",{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}},  
@@ -2374,10 +2624,7 @@ static struct
   {"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}},
   {"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. */
@@ -2401,7 +2648,7 @@ static int process_tok(fstring tok)
                        matches = 1;
                        cmd = i;
                        break;
-               } else if (strnequal(commands[i].name, tok, tok_len)) {
+               } else if (strncasecmp(commands[i].name, tok, tok_len) == 0) {
                        matches++;
                        cmd = i;
                }
@@ -2419,12 +2666,12 @@ static int process_tok(fstring tok)
 /****************************************************************************
 help
 ****************************************************************************/
-static int cmd_help(void)
+static int cmd_help(const char **cmd_ptr)
 {
        int i=0,j;
        fstring buf;
        
-       if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                if ((i = process_tok(buf)) >= 0)
                        d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
        } else {
@@ -2474,10 +2721,10 @@ static int process_command_string(char *cmd)
                
                /* and get the first part of the command */
                ptr = line;
-               if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
+               if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
                
                if ((i = process_tok(tok)) >= 0) {
-                       rc = commands[i].fn();
+                       rc = commands[i].fn(&ptr);
                } else if (i == -2) {
                        d_printf("%s: command abbreviation ambiguous\n",tok);
                } else {
@@ -2498,12 +2745,12 @@ typedef struct {
        int len;
 } completion_remote_t;
 
-static void completion_remote_filter(file_info *f, const char *mask, void *state)
+static void completion_remote_filter(struct clilist_file_info *f, const char *mask, void *state)
 {
        completion_remote_t *info = (completion_remote_t *)state;
 
        if ((info->count < MAX_COMPLETIONS - 1) && (strncmp(info->text, f->name, info->len) == 0) && (strcmp(f->name, ".") != 0) && (strcmp(f->name, "..") != 0)) {
-               if ((info->dirmask[0] == 0) && !(f->mode & FILE_ATTRIBUTE_DIRECTORY))
+               if ((info->dirmask[0] == 0) && !(f->attrib & FILE_ATTRIBUTE_DIRECTORY))
                        info->matches[info->count] = strdup(f->name);
                else {
                        pstring tmp;
@@ -2513,13 +2760,13 @@ static void completion_remote_filter(file_info *f, const char *mask, void *state
                        else
                                tmp[0] = 0;
                        pstrcat(tmp, f->name);
-                       if (f->mode & FILE_ATTRIBUTE_DIRECTORY)
+                       if (f->attrib & FILE_ATTRIBUTE_DIRECTORY)
                                pstrcat(tmp, "/");
                        info->matches[info->count] = strdup(tmp);
                }
                if (info->matches[info->count] == NULL)
                        return;
-               if (f->mode & FILE_ATTRIBUTE_DIRECTORY)
+               if (f->attrib & FILE_ATTRIBUTE_DIRECTORY)
                        smb_readline_ca_char(0);
 
                if (info->count == 1)
@@ -2544,7 +2791,7 @@ static char **remote_completion(const char *text, int len)
        if (len >= PATH_MAX)
                return(NULL);
 
-       info.matches = (char **)malloc(sizeof(info.matches[0])*MAX_COMPLETIONS);
+       info.matches = malloc_array_p(char *, MAX_COMPLETIONS);
        if (!info.matches) return NULL;
        info.matches[0] = NULL;
 
@@ -2622,9 +2869,9 @@ static char **completion_fn(const char *text, int start, int end)
                        return NULL;
        } else {
                char **matches;
-               int i, len, samelen, count=1;
+               int i, len, samelen = 0, count=1;
 
-               matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
+               matches = malloc_array_p(char *, MAX_COMPLETIONS);
                if (!matches) return NULL;
                matches[0] = NULL;
 
@@ -2697,13 +2944,12 @@ process commands on stdin
 ****************************************************************************/
 static void process_stdin(void)
 {
-       const char *ptr;
-
        while (1) {
                fstring tok;
                fstring the_prompt;
                char *cline;
                pstring line;
+               const char *ptr;
                int i;
                
                /* display a prompt */
@@ -2722,10 +2968,10 @@ static void process_stdin(void)
       
                /* and get the first part of the command */
                ptr = line;
-               if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
+               if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
 
                if ((i = process_tok(tok)) >= 0) {
-                       commands[i].fn();
+                       commands[i].fn(&ptr);
                } else if (i == -2) {
                        d_printf("%s: command abbreviation ambiguous\n",tok);
                } else {
@@ -2767,7 +3013,7 @@ static struct smbcli_state *do_connect(const char *server, const char *share)
 
  again:
        /* have to open a new connection */
-       if (!(c=smbcli_state_init()) || !smbcli_socket_connect(c, server_n)) {
+       if (!(c=smbcli_state_init(NULL)) || !smbcli_socket_connect(c, server_n)) {
                d_printf("Connection to %s failed\n", server_n);
                return NULL;
        }
@@ -2796,18 +3042,11 @@ static struct smbcli_state *do_connect(const char *server, const char *share)
                return NULL;
        }
 
-       if (!got_pass) {
-               const char *pass = getpass("Password: ");
-               if (pass) {
-                       pstrcpy(password, pass);
-               }
-       }
-
-       status = smbcli_session_setup(c, username, password, lp_workgroup());
+       status = smbcli_session_setup(c, username, password, domain);
        if (NT_STATUS_IS_ERR(status)) {
                d_printf("authenticated session setup failed: %s\n", nt_errstr(status));
                /* if a password was not supplied then try again with a null username */
-               if (password[0] || !username[0] || use_kerberos) {
+               if (password[0] || !username[0]) {
                        status = smbcli_session_setup(c, "", "", lp_workgroup());
                }
                if (NT_STATUS_IS_ERR(status)) {
@@ -2867,31 +3106,6 @@ static int do_host_query(char *query_host)
 }
 
 
-/****************************************************************************
-handle a tar operation
-****************************************************************************/
-static int do_tar_op(char *base_directory)
-{
-       int ret;
-
-       /* do we already have a connection? */
-       if (!cli) {
-               cli = do_connect(desthost, service);    
-               if (!cli)
-                       return 1;
-       }
-
-       recurse=True;
-
-       if (*base_directory) do_cd(base_directory);
-       
-       ret=process_tar();
-
-       smbcli_shutdown(cli);
-
-       return(ret);
-}
-
 /****************************************************************************
 handle a message operation
 ****************************************************************************/
@@ -2905,7 +3119,7 @@ static int do_message_op(void)
 
        server_name = dest_ip ? dest_ip : desthost;
 
-       if (!(cli=smbcli_state_init()) || !smbcli_socket_connect(cli, server_name)) {
+       if (!(cli=smbcli_state_init(NULL)) || !smbcli_socket_connect(cli, server_name)) {
                d_printf("Connection to %s failed\n", server_name);
                return 1;
        }
@@ -2953,7 +3167,6 @@ static void remember_query_host(const char *arg,
        int opt;
        pstring query_host;
        BOOL message = False;
-       extern char tar_type;
        pstring term_code;
        poptContext pc;
        char *p;
@@ -2962,14 +3175,11 @@ static void remember_query_host(const char *arg,
        struct poptOption long_options[] = {
                POPT_AUTOHELP
 
-               { "name-resolve", 'R', POPT_ARG_STRING, NULL, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
                { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
                { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
                { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
                { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
                { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
-               { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
-               { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
                { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
                { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" }, 
                { "send-buffer", 'b', POPT_ARG_INT, NULL, 'b', "Changes the transmit/send buffer", "BYTES" },
@@ -2977,10 +3187,10 @@ static void remember_query_host(const char *arg,
                POPT_COMMON_SAMBA
                POPT_COMMON_CONNECTION
                POPT_COMMON_CREDENTIALS
+               POPT_COMMON_VERSION
                POPT_TABLEEND
        };
        
-
 #ifdef KANJI
        pstrcpy(term_code, KANJI);
 #else /* KANJI */
@@ -3032,18 +3242,6 @@ static void remember_query_host(const char *arg,
                case 't':
                        pstrcpy(term_code, poptGetOptArg(pc));
                        break;
-               case 'm':
-                       lp_set_cmdline("max protocol", poptGetOptArg(pc));
-                       break;
-               case 'R':
-                       lp_set_cmdline("name resolve order", poptGetOptArg(pc));
-                       break;
-               case 'T':
-                       if (!tar_parseargs(argc, argv, poptGetOptArg(pc), optind)) {
-                               poptPrintUsage(pc, stderr, 0);
-                               exit(1);
-                       }
-                       break;
                case 'D':
                        fstrcpy(base_directory,poptGetOptArg(pc));
                        break;
@@ -3055,6 +3253,8 @@ static void remember_query_host(const char *arg,
 
        load_interfaces();
 
+       smbclient_init_subsystems;
+
        if(poptPeekArg(pc)) {
                pstrcpy(service,poptGetArg(pc));  
                /* Convert any '/' characters in the service name to '\' characters */
@@ -3068,32 +3268,25 @@ static void remember_query_host(const char *arg,
        }
 
        if (poptPeekArg(pc)) { 
-               cmdline_auth_info.got_pass = True;
-               pstrcpy(cmdline_auth_info.password,poptGetArg(pc));  
+               cmdline_set_userpassword(poptGetArg(pc));
        }
 
        /*init_names(); */
 
-       if (!tar_type && !*query_host && !*service && !message) {
+       if (!*query_host && !*service && !message) {
                poptPrintUsage(pc, stderr, 0);
                exit(1);
        }
 
        poptFreeContext(pc);
 
-       pstrcpy(username, cmdline_auth_info.username);
-       pstrcpy(password, cmdline_auth_info.password);
-       use_kerberos = cmdline_auth_info.use_kerberos;
-       got_pass = cmdline_auth_info.got_pass;
+       pstrcpy(username, cmdline_get_username());
+       pstrcpy(domain, cmdline_get_userdomain());
+       pstrcpy(password, cmdline_get_userpassword());
 
        DEBUG( 3, ( "Client started (version %s).\n", SAMBA_VERSION_STRING ) );
 
        talloc_destroy(mem_ctx);
-       if (tar_type) {
-               if (cmdstr)
-                       process_command_string(cmdstr);
-               return do_tar_op(base_directory);
-       }
 
        if ((p=strchr_m(query_host,'#'))) {
                *p = 0;