r5917: First step in using the new cli_credentials structure. This patch
[bbaumbach/samba-autobuild/.git] / source4 / client / client.c
index d9a8aa72295f1efb90432275beefc9e7d7330d6e..fd5be736079f2f222f3ac669a69cd5a154604a79 100644 (file)
@@ -3,7 +3,7 @@
    SMB client
    Copyright (C) Andrew Tridgell 1994-1998
    Copyright (C) Simo Sorce 2001-2002
-   Copyright (C) Jelmer Vernooij 2003
+   Copyright (C) Jelmer Vernooij 2003-2004
    Copyright (C) James J Myers   2003 <myersjj@samba.org>
    
    This program is free software; you can redistribute it and/or modify
 */
 
 #include "includes.h"
+#include "version.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
 
-struct cli_state *cli;
+struct smbcli_state *cli;
 extern BOOL in_client;
 static int port = 0;
 pstring cur_dir = "\\";
 static pstring cd_path = "";
 static pstring service;
 static pstring desthost;
-static pstring username;
-static pstring password;
-static BOOL use_kerberos;
-static BOOL got_pass;
 static char *cmdstr = NULL;
 
 static int io_bufsize = 64512;
@@ -44,7 +52,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)
@@ -58,8 +66,6 @@ static int archive_level = 0;
 
 static BOOL translation = False;
 
-static BOOL have_ip;
-
 /* clitar bits insert */
 extern int blocksize;
 extern BOOL tar_inc;
@@ -74,7 +80,7 @@ static int printmode = 1;
 static BOOL recurse = False;
 BOOL lowercase = False;
 
-static struct in_addr dest_ip;
+static const char *dest_ip;
 
 #define SEPARATORS " \t\n\r"
 
@@ -83,10 +89,10 @@ static BOOL abort_mget = True;
 static pstring fileselection = "";
 
 /* timing globals */
-SMB_BIG_UINT get_total_size = 0;
-unsigned int get_total_time_ms = 0;
-static SMB_BIG_UINT put_total_size = 0;
-static unsigned int put_total_time_ms = 0;
+uint64_t get_total_size = 0;
+uint_t get_total_time_ms = 0;
+static uint64_t put_total_size = 0;
+static uint_t put_total_time_ms = 0;
 
 /* totals globals */
 static double dir_total;
@@ -94,7 +100,7 @@ static double dir_total;
 #define USENMB
 
 /* some forward declarations */
-static struct cli_state *do_connect(const char *server, const char *share);
+static struct smbcli_state *do_connect(const char *server, const char *share, struct cli_credentials *cred);
 
 
 /*******************************************************************
@@ -131,8 +137,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) {
@@ -158,8 +165,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;
 
@@ -167,7 +175,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;
                }
@@ -191,8 +199,8 @@ static void send_message(void)
        int total_len = 0;
        int grp_id;
 
-       if (!cli_message_start(cli->tree, desthost, username, &grp_id)) {
-               d_printf("message start: %s\n", cli_errstr(cli->tree));
+       if (!smbcli_message_start(cli->tree, desthost, cli_credentials_get_username(cmdline_credentials), &grp_id)) {
+               d_printf("message start: %s\n", smbcli_errstr(cli->tree));
                return;
        }
 
@@ -205,7 +213,7 @@ static void send_message(void)
                int l=0;
                int c;
 
-               ZERO_ARRAY(msg);
+               ZERO_STRUCT(msg);
 
                for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
                        if (c == '\n')
@@ -213,8 +221,8 @@ static void send_message(void)
                        msg[l] = c;   
                }
 
-               if (!cli_message_text(cli->tree, msg, l, grp_id)) {
-                       d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli->tree));
+               if (!smbcli_message_text(cli->tree, msg, l, grp_id)) {
+                       d_printf("SMBsendtxt failed (%s)\n",smbcli_errstr(cli->tree));
                        return;
                }      
                
@@ -226,8 +234,8 @@ static void send_message(void)
        else
                d_printf("sent %d bytes\n",total_len);
 
-       if (!cli_message_end(cli->tree, grp_id)) {
-               d_printf("SMBsendend failed (%s)\n",cli_errstr(cli->tree));
+       if (!smbcli_message_end(cli->tree, grp_id)) {
+               d_printf("SMBsendend failed (%s)\n",smbcli_errstr(cli->tree));
                return;
        }      
 }
@@ -241,8 +249,8 @@ static int do_dskattr(void)
 {
        int total, bsize, avail;
 
-       if (NT_STATUS_IS_ERR(cli_dskattr(cli->tree, &bsize, &total, &avail))) {
-               d_printf("Error in dskattr: %s\n",cli_errstr(cli->tree)); 
+       if (NT_STATUS_IS_ERR(smbcli_dskattr(cli->tree, &bsize, &total, &avail))) {
+               d_printf("Error in dskattr: %s\n",smbcli_errstr(cli->tree)); 
                return 1;
        }
 
@@ -255,13 +263,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
@@ -290,8 +305,8 @@ static int do_cd(char *newdir)
        dos_clean_name(cur_dir);
        
        if (!strequal(cur_dir,"\\")) {
-               if (NT_STATUS_IS_ERR(cli_chkpath(cli->tree, dname))) {
-                       d_printf("cd %s: %s\n", dname, cli_errstr(cli->tree));
+               if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, dname))) {
+                       d_printf("cd %s: %s\n", dname, smbcli_errstr(cli->tree));
                        pstrcpy(cur_dir,saved_dir);
                }
        }
@@ -304,12 +319,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);
@@ -318,12 +333,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)) {
@@ -336,7 +374,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);
        }
@@ -344,57 +382,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 mode)
-{
-       static fstring attrstr;
-       int i, len;
-       const struct {
-               char c;
-               uint16 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);
        }
 }
 
@@ -402,7 +404,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;
@@ -415,7 +417,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
@@ -487,7 +489,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);
@@ -534,9 +536,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,9 +566,10 @@ static void do_list_helper(file_info *f, const char *mask, void *state)
 
 
 /****************************************************************************
-a wrapper around cli_list that adds recursion
+a wrapper around smbcli_list that adds recursion
   ****************************************************************************/
-void do_list(const char *mask,uint16 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;
 
@@ -591,14 +594,14 @@ void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec,
                {
                        /*
                         * Need to copy head so that it doesn't become
-                        * invalid inside the call to cli_list.  This
+                        * invalid inside the call to smbcli_list.  This
                         * would happen if the list were expanded
                         * during the call.
                         * Fix from E. Jay Berkenbilt (ejb@ql.org)
                         */
                        pstring head;
                        pstrcpy(head, do_list_queue_head());
-                       cli_list(cli->tree, head, attribute, do_list_helper, NULL);
+                       smbcli_list(cli->tree, head, attribute, do_list_helper, NULL);
                        remove_do_list_queue_head();
                        if ((! do_list_queue_empty()) && (fn == display_finfo))
                        {
@@ -622,9 +625,9 @@ void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec,
        }
        else
        {
-               if (cli_list(cli->tree, mask, attribute, do_list_helper, NULL) == -1)
+               if (smbcli_list(cli->tree, mask, attribute, do_list_helper, NULL) == -1)
                {
-                       d_printf("%s listing %s\n", cli_errstr(cli->tree), mask);
+                       d_printf("%s listing %s\n", smbcli_errstr(cli->tree), mask);
                }
        }
 
@@ -635,9 +638,9 @@ void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec,
 /****************************************************************************
   get a directory listing
   ****************************************************************************/
-static int cmd_dir(void)
+static int cmd_dir(const char **cmd_ptr)
 {
-       uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
+       uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
        pstring mask;
        fstring buf;
        char *p=buf;
@@ -648,7 +651,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);
@@ -656,7 +659,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);
@@ -672,9 +680,9 @@ static int cmd_dir(void)
 /****************************************************************************
   get a directory listing
   ****************************************************************************/
-static int cmd_du(void)
+static int cmd_du(const char **cmd_ptr)
 {
-       uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
+       uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
        pstring mask;
        fstring buf;
        char *p=buf;
@@ -685,14 +693,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);
@@ -712,10 +720,10 @@ 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 attr;
+       uint16_t attr;
        size_t size;
        off_t start = 0;
        off_t nread = 0;
@@ -724,13 +732,13 @@ 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 = cli_open(cli->tree, rname, O_RDONLY, DENY_NONE);
+       fnum = smbcli_open(cli->tree, rname, O_RDONLY, DENY_NONE);
 
        if (fnum == -1) {
-               d_printf("%s opening remote file %s\n",cli_errstr(cli->tree),rname);
+               d_printf("%s opening remote file %s\n",smbcli_errstr(cli->tree),rname);
                return 1;
        }
 
@@ -738,16 +746,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;
        }
@@ -757,25 +765,25 @@ static int do_get(char *rname, const char *lname, BOOL reget)
        }
 
 
-       if (NT_STATUS_IS_ERR(cli_qfileinfo(cli->tree, fnum, 
+       if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum, 
                           &attr, &size, NULL, NULL, NULL, NULL, NULL)) &&
-           NT_STATUS_IS_ERR(cli_getattrE(cli->tree, fnum, 
+           NT_STATUS_IS_ERR(smbcli_getattrE(cli->tree, fnum, 
                          &attr, &size, NULL, NULL, NULL))) {
-               d_printf("getattrib: %s\n",cli_errstr(cli->tree));
+               d_printf("getattrib: %s\n",smbcli_errstr(cli->tree));
                return 1;
        }
 
        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);
-               cli_close(cli->tree, fnum);
+               smbcli_close(cli->tree, fnum);
                return 1;
        }
 
        while (1) {
-               int n = cli_read(cli->tree, fnum, data, nread + start, read_size);
+               int n = smbcli_read(cli->tree, fnum, data, nread + start, read_size);
 
                if (n <= 0) break;
  
@@ -797,8 +805,8 @@ static int do_get(char *rname, const char *lname, BOOL reget)
 
        SAFE_FREE(data);
        
-       if (NT_STATUS_IS_ERR(cli_close(cli->tree, fnum))) {
-               d_printf("Error %s closing remote file\n",cli_errstr(cli->tree));
+       if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
+               d_printf("Error %s closing remote file\n",smbcli_errstr(cli->tree));
                rc = 1;
        }
 
@@ -807,7 +815,7 @@ static int do_get(char *rname, const char *lname, BOOL reget)
        }
 
        if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
-               cli_setatr(cli->tree, rname, attr & ~(uint16)FILE_ATTRIBUTE_ARCHIVE, 0);
+               smbcli_setatr(cli->tree, rname, attr & ~(uint16_t)FILE_ATTRIBUTE_ARCHIVE, 0);
        }
 
        {
@@ -833,7 +841,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;
@@ -844,23 +852,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;
@@ -875,7 +899,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
@@ -884,7 +908,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);
@@ -897,11 +921,12 @@ 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) && 
+       if (!directory_exist(finfo->name) && 
            mkdir(finfo->name,0777) != 0) {
                d_printf("failed to create directory %s\n",finfo->name);
                pstrcpy(cur_dir,saved_curdir);
@@ -926,7 +951,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;
@@ -944,7 +969,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;
@@ -968,9 +993,9 @@ static int cmd_more(void)
 /****************************************************************************
 do a mget command
 ****************************************************************************/
-static int cmd_mget(void)
+static int cmd_mget(const char **cmd_ptr)
 {
-       uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
+       uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
        pstring mget_mask;
        fstring buf;
        char *p=buf;
@@ -982,7 +1007,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,"\\");
@@ -1013,9 +1038,9 @@ static NTSTATUS do_mkdir(char *name)
 {
        NTSTATUS status;
 
-       if (NT_STATUS_IS_ERR(status = cli_mkdir(cli->tree, name))) {
+       if (NT_STATUS_IS_ERR(status = smbcli_mkdir(cli->tree, name))) {
                d_printf("%s making remote directory %s\n",
-                        cli_errstr(cli->tree),name);
+                        smbcli_errstr(cli->tree),name);
                return status;
        }
 
@@ -1028,9 +1053,9 @@ show 8.3 name of a file
 static BOOL do_altname(char *name)
 {
        const char *altname;
-       if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli->tree, name, &altname))) {
+       if (!NT_STATUS_IS_OK(smbcli_qpathinfo_alt_name(cli->tree, name, &altname))) {
                d_printf("%s getting alt name for %s\n",
-                        cli_errstr(cli->tree),name);
+                        smbcli_errstr(cli->tree),name);
                return(False);
        }
        d_printf("%s\n", altname);
@@ -1042,9 +1067,9 @@ static BOOL do_altname(char *name)
 /****************************************************************************
  Exit client.
 ****************************************************************************/
-static int cmd_quit(void)
+static int cmd_quit(const char **cmd_ptr)
 {
-       cli_shutdown(cli);
+       smbcli_shutdown(cli);
        exit(0);
        /* NOTREACHED */
        return 0;
@@ -1054,7 +1079,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;
@@ -1062,7 +1087,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;
@@ -1079,7 +1104,7 @@ static int cmd_mkdir(void)
                p = strtok(ddir,"/\\");
                while (p) {
                        pstrcat(ddir2,p);
-                       if (NT_STATUS_IS_ERR(cli_chkpath(cli->tree, ddir2))) { 
+                       if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, ddir2))) { 
                                do_mkdir(ddir2);
                        }
                        pstrcat(ddir2,"\\");
@@ -1096,7 +1121,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;
@@ -1104,7 +1129,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;
        }
@@ -1125,7 +1150,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;
        
@@ -1133,21 +1158,21 @@ static int do_put(char *rname, char *lname, BOOL reput)
        GetTimeOfDay(&tp_start);
 
        if (reput) {
-               fnum = cli_open(cli->tree, rname, O_RDWR|O_CREAT, DENY_NONE);
+               fnum = smbcli_open(cli->tree, rname, O_RDWR|O_CREAT, DENY_NONE);
                if (fnum >= 0) {
-                       if (NT_STATUS_IS_ERR(cli_qfileinfo(cli->tree, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL)) &&
-                           NT_STATUS_IS_ERR(cli_getattrE(cli->tree, fnum, NULL, &start, NULL, NULL, NULL))) {
-                               d_printf("getattrib: %s\n",cli_errstr(cli->tree));
+                       if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL)) &&
+                           NT_STATUS_IS_ERR(smbcli_getattrE(cli->tree, fnum, NULL, &start, NULL, NULL, NULL))) {
+                               d_printf("getattrib: %s\n",smbcli_errstr(cli->tree));
                                return 1;
                        }
                }
        } else {
-               fnum = cli_open(cli->tree, rname, O_RDWR|O_CREAT|O_TRUNC, 
+               fnum = smbcli_open(cli->tree, rname, O_RDWR|O_CREAT|O_TRUNC, 
                                DENY_NONE);
        }
   
        if (fnum == -1) {
-               d_printf("%s opening remote file %s\n",cli_errstr(cli->tree),rname);
+               d_printf("%s opening remote file %s\n",smbcli_errstr(cli->tree),rname);
                return 1;
        }
 
@@ -1178,7 +1203,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;
@@ -1196,10 +1221,10 @@ static int do_put(char *rname, char *lname, BOOL reput)
                        break;
                }
 
-               ret = cli_write(cli->tree, fnum, 0, buf, nread + start, n);
+               ret = smbcli_write(cli->tree, fnum, 0, buf, nread + start, n);
 
                if (n != ret) {
-                       d_printf("Error writing file: %s\n", cli_errstr(cli->tree));
+                       d_printf("Error writing file: %s\n", smbcli_errstr(cli->tree));
                        rc = 1;
                        break;
                } 
@@ -1207,8 +1232,8 @@ static int do_put(char *rname, char *lname, BOOL reput)
                nread += n;
        }
 
-       if (NT_STATUS_IS_ERR(cli_close(cli->tree, fnum))) {
-               d_printf("%s closing remote file %s\n",cli_errstr(cli->tree),rname);
+       if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
+               d_printf("%s closing remote file %s\n",smbcli_errstr(cli->tree),rname);
                x_fclose(f);
                SAFE_FREE(buf);
                return 1;
@@ -1238,7 +1263,7 @@ static int do_put(char *rname, char *lname, BOOL reput)
        }
 
        if (f == x_stdin) {
-               cli_shutdown(cli);
+               smbcli_shutdown(cli);
                exit(0);
        }
        
@@ -1250,7 +1275,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;
@@ -1260,28 +1285,24 @@ 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);
        
        dos_clean_name(rname);
 
-       {
-               SMB_STRUCT_STAT st;
-               /* allow '-' to represent stdin
-                  jdblair, 24.jun.98 */
-               if (!file_exist(lname,&st) &&
-                   (strcmp(lname,"-"))) {
-                       d_printf("%s does not exist\n",lname);
-                       return 1;
-               }
+       /* allow '-' to represent stdin
+          jdblair, 24.jun.98 */
+       if (!file_exist(lname) && (strcmp(lname,"-"))) {
+               d_printf("%s does not exist\n",lname);
+               return 1;
        }
 
        return do_put(rname, lname, False);
@@ -1334,14 +1355,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
@@ -1387,7 +1446,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);
@@ -1408,12 +1467,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;
@@ -1453,7 +1512,7 @@ static int cmd_mput(void)
                                        SAFE_FREE(rname);
                                        if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
                                        dos_format(rname);
-                                       if (NT_STATUS_IS_ERR(cli_chkpath(cli->tree, rname)) && 
+                                       if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, rname)) && 
                                            NT_STATUS_IS_ERR(do_mkdir(rname))) {
                                                DEBUG (0, ("Unable to make dir, skipping..."));
                                                /* Skip the directory */
@@ -1501,19 +1560,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;
 }
@@ -1522,13 +1581,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;
        }
@@ -1550,7 +1609,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");
        
@@ -1560,42 +1619,26 @@ 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(cli_unlink(cli->tree, mask))) {
-               d_printf("%s deleting remote file %s\n",cli_errstr(cli->tree),mask);
-       }
-}
-
-/****************************************************************************
-delete some files
-****************************************************************************/
-static int cmd_del(void)
+static int cmd_del(const char **cmd_ptr)
 {
        pstring mask;
        fstring buf;
-       uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
+       uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
 
        if (recurse)
                attribute |= FILE_ATTRIBUTE_DIRECTORY;
        
        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;
 }
@@ -1604,7 +1647,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;
@@ -1612,16 +1655,16 @@ 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;
        }
        pstrcat(dname,buf);
 
-       ret = cli_deltree(cli->tree, dname);
+       ret = smbcli_deltree(cli->tree, dname);
 
        if (ret == -1) {
-               printf("Failed to delete tree %s - %s\n", dname, cli_errstr(cli->tree));
+               printf("Failed to delete tree %s - %s\n", dname, smbcli_errstr(cli->tree));
                return -1;
        }
 
@@ -1634,7 +1677,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;
@@ -1645,13 +1688,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;
@@ -1663,10 +1706,10 @@ static int cmd_allinfo(void)
                goto done;
        }
 
-       d_printf("\tcreate_time:    %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.create_time));
-       d_printf("\taccess_time:    %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.access_time));
-       d_printf("\twrite_time:     %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.write_time));
-       d_printf("\tchange_time:    %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.change_time));
+       d_printf("\tcreate_time:    %s\n", nt_time_string(mem_ctx, finfo.all_info.out.create_time));
+       d_printf("\taccess_time:    %s\n", nt_time_string(mem_ctx, finfo.all_info.out.access_time));
+       d_printf("\twrite_time:     %s\n", nt_time_string(mem_ctx, finfo.all_info.out.write_time));
+       d_printf("\tchange_time:    %s\n", nt_time_string(mem_ctx, finfo.all_info.out.change_time));
        d_printf("\tattrib:         0x%x\n", finfo.all_info.out.attrib);
        d_printf("\talloc_size:     %lu\n", (unsigned long)finfo.all_info.out.alloc_size);
        d_printf("\tsize:           %lu\n", (unsigned long)finfo.all_info.out.size);
@@ -1697,12 +1740,10 @@ static int cmd_allinfo(void)
        if (NT_STATUS_IS_OK(status)) {
                int i;
                for (i=0;i<finfo.all_eas.out.num_eas;i++) {
-                       d_printf("\tEA[%d] flags=%d %s=%*.*s\n", i,
+                       d_printf("\tEA[%d] flags=%d len=%d '%s'\n", i,
                                 finfo.all_eas.out.eas[i].flags,
-                                finfo.all_eas.out.eas[i].name.s,
-                                finfo.all_eas.out.eas[i].value.length,
                                 finfo.all_eas.out.eas[i].value.length,
-                                finfo.all_eas.out.eas[i].value.data);
+                                finfo.all_eas.out.eas[i].name.s);
                }
        }
 
@@ -1732,75 +1773,328 @@ static int cmd_allinfo(void)
                d_printf("\tcluster_shift   %ld\n", (long)finfo.compression_info.out.cluster_shift);
        }
 
-       talloc_destroy(mem_ctx);
+       talloc_free(mem_ctx);
 
 done:
        return ret;
 }
 
 
+/****************************************************************************
+shows EA contents
+****************************************************************************/
+static int cmd_eainfo(const char **cmd_ptr)
+{
+       pstring fname;
+       fstring buf;
+       int ret = 0;
+       TALLOC_CTX *mem_ctx;
+       union smb_fileinfo finfo;
+       NTSTATUS status;
+       int i;
+
+       pstrcpy(fname,cur_dir);
+       
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
+               d_printf("eainfo <filename>\n");
+               return 1;
+       }
+       pstrcat(fname,buf);
+
+       mem_ctx = talloc_init("%s", fname);
+
+       finfo.generic.in.fname = fname;
+       finfo.generic.level = RAW_FILEINFO_ALL_EAS;
+       status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
+       
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("RAW_FILEINFO_ALL_EAS - %s\n", nt_errstr(status));
+               talloc_free(mem_ctx);
+               return 1;
+       }
+
+       d_printf("%s has %d EAs\n", fname, finfo.all_eas.out.num_eas);
+
+       for (i=0;i<finfo.all_eas.out.num_eas;i++) {
+               d_printf("\tEA[%d] flags=%d len=%d '%s'\n", i,
+                        finfo.all_eas.out.eas[i].flags,
+                        finfo.all_eas.out.eas[i].value.length,
+                        finfo.all_eas.out.eas[i].name.s);
+               fflush(stdout);
+               dump_data(0, 
+                         finfo.all_eas.out.eas[i].value.data,
+                         finfo.all_eas.out.eas[i].value.length);
+       }
+
+       talloc_free(mem_ctx);
+
+       return ret;
+}
+
+
 /****************************************************************************
 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;
        }
        pstrcat(fname,buf);
 
-       fnum = cli_open(cli->tree, fname, O_RDONLY, DENY_NONE);
+       fnum = smbcli_nt_create_full(cli->tree, fname, 0, 
+                                    SEC_STD_READ_CONTROL,
+                                    0,
+                                    NTCREATEX_SHARE_ACCESS_DELETE|
+                                    NTCREATEX_SHARE_ACCESS_READ|
+                                    NTCREATEX_SHARE_ACCESS_WRITE, 
+                                    NTCREATEX_DISP_OPEN,
+                                    0, 0);
        if (fnum == -1) {
-               d_printf("%s - %s\n", fname, cli_errstr(cli->tree));
+               d_printf("%s - %s\n", fname, smbcli_errstr(cli->tree));
                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;
        }
 
-       talloc_destroy(mem_ctx);
+       NDR_PRINT_DEBUG(security_descriptor, query.query_secdesc.out.sd);
+
+       talloc_free(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_new(NULL);
+       NTSTATUS status;
+       struct dom_sid *sid;
+
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
+               d_printf("lookup <sid|name>\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_new(NULL);
+       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_new(NULL);
+       NTSTATUS status;
+       struct dom_sid *sid;
+       struct lsa_RightSet rights;
+
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
+               d_printf("addprivileges <sid|name> <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(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_new(NULL);
+       NTSTATUS status;
+       struct dom_sid *sid;
+       struct lsa_RightSet rights;
+
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
+               d_printf("delprivileges <sid|name> <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(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;
        }
        pstrcat(mask,buf);
 
-       cli_open(cli->tree, mask, O_RDWR, DENY_ALL);
+       smbcli_open(cli->tree, mask, O_RDWR, DENY_ALL);
 
        return 0;
 }
@@ -1809,22 +2103,22 @@ 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;
        }
        pstrcat(mask,buf);
 
-       if (NT_STATUS_IS_ERR(cli_rmdir(cli->tree, mask))) {
+       if (NT_STATUS_IS_ERR(smbcli_rmdir(cli->tree, mask))) {
                d_printf("%s removing remote directory file %s\n",
-                        cli_errstr(cli->tree),mask);
+                        smbcli_errstr(cli->tree),mask);
        }
        
        return 0;
@@ -1833,7 +2127,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;
@@ -1846,8 +2140,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;
        }
@@ -1855,8 +2149,8 @@ static int cmd_link(void)
        pstrcat(src,buf);
        pstrcat(dest,buf2);
 
-       if (NT_STATUS_IS_ERR(cli_unix_hardlink(cli->tree, src, dest))) {
-               d_printf("%s linking files (%s -> %s)\n", cli_errstr(cli->tree), src, dest);
+       if (NT_STATUS_IS_ERR(smbcli_unix_hardlink(cli->tree, src, dest))) {
+               d_printf("%s linking files (%s -> %s)\n", smbcli_errstr(cli->tree), src, dest);
                return 1;
        }  
 
@@ -1867,7 +2161,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;
@@ -1880,8 +2174,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;
        }
@@ -1889,9 +2183,9 @@ static int cmd_symlink(void)
        pstrcat(src,buf);
        pstrcat(dest,buf2);
 
-       if (NT_STATUS_IS_ERR(cli_unix_symlink(cli->tree, src, dest))) {
+       if (NT_STATUS_IS_ERR(smbcli_unix_symlink(cli->tree, src, dest))) {
                d_printf("%s symlinking files (%s -> %s)\n",
-                       cli_errstr(cli->tree), src, dest);
+                       smbcli_errstr(cli->tree), src, dest);
                return 1;
        } 
 
@@ -1902,7 +2196,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;
@@ -1915,8 +2209,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;
        }
@@ -1924,9 +2218,9 @@ static int cmd_chmod(void)
        mode = (mode_t)strtol(buf, NULL, 8);
        pstrcat(src,buf2);
 
-       if (NT_STATUS_IS_ERR(cli_unix_chmod(cli->tree, src, mode))) {
+       if (NT_STATUS_IS_ERR(smbcli_unix_chmod(cli->tree, src, mode))) {
                d_printf("%s chmod file %s 0%o\n",
-                       cli_errstr(cli->tree), src, (unsigned int)mode);
+                       smbcli_errstr(cli->tree), src, (uint_t)mode);
                return 1;
        } 
 
@@ -1937,7 +2231,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;
@@ -1951,9 +2245,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;
        }
@@ -1962,9 +2256,9 @@ static int cmd_chown(void)
        gid = (gid_t)atoi(buf2);
        pstrcat(src,buf3);
 
-       if (NT_STATUS_IS_ERR(cli_unix_chown(cli->tree, src, uid, gid))) {
+       if (NT_STATUS_IS_ERR(smbcli_unix_chown(cli->tree, src, uid, gid))) {
                d_printf("%s chown file %s uid=%d, gid=%d\n",
-                       cli_errstr(cli->tree), src, (int)uid, (int)gid);
+                       smbcli_errstr(cli->tree), src, (int)uid, (int)gid);
                return 1;
        } 
 
@@ -1974,7 +2268,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;
@@ -1982,8 +2276,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;
        }
@@ -1991,8 +2285,8 @@ static int cmd_rename(void)
        pstrcat(src,buf);
        pstrcat(dest,buf2);
 
-       if (NT_STATUS_IS_ERR(cli_rename(cli->tree, src, dest))) {
-               d_printf("%s renaming files\n",cli_errstr(cli->tree));
+       if (NT_STATUS_IS_ERR(smbcli_rename(cli->tree, src, dest))) {
+               d_printf("%s renaming files\n",smbcli_errstr(cli->tree));
                return 1;
        }
        
@@ -2003,7 +2297,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"));
@@ -2015,14 +2309,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))));
@@ -2041,11 +2335,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);
@@ -2056,7 +2350,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"));
@@ -2070,7 +2364,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"));
@@ -2081,7 +2375,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",
@@ -2094,12 +2388,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 {
@@ -2131,12 +2425,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)));
 
@@ -2146,7 +2440,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;
@@ -2157,14 +2451,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);
 }
@@ -2172,29 +2466,28 @@ 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;
        
        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;
        }
        pstrcpy(local_name, p);
   
-       if (!file_exist(local_name, &st)) {
+       if (!file_exist(local_name)) {
                d_printf("%s does not exist\n", local_name);
                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);
@@ -2208,7 +2501,7 @@ static int cmd_reput(void)
 /*
   return a string representing a share type
 */
-static const char *share_type_str(uint32 type)
+static const char *share_type_str(uint32_t type)
 {
        switch (type & 0xF) {
        case STYPE_DISKTREE: 
@@ -2253,7 +2546,7 @@ static BOOL browse_host(const char *query_host)
        char *binding;
        NTSTATUS status;
        struct srvsvc_NetShareEnumAll r;
-       uint32 resume_handle = 0;
+       uint32_t resume_handle = 0;
        TALLOC_CTX *mem_ctx = talloc_init("browse_host");
        struct srvsvc_NetShareCtr1 ctr1;
 
@@ -2262,25 +2555,26 @@ static BOOL browse_host(const char *query_host)
        status = dcerpc_pipe_connect(&p, binding, 
                                     DCERPC_SRVSVC_UUID, 
                                     DCERPC_SRVSVC_VERSION,
-                                    lp_workgroup(), 
-                                    username, password);
+                                    lp_netbios_name(),
+                                    cli_credentials_get_domain(cmdline_credentials), 
+                                    cli_credentials_get_username(cmdline_credentials), 
+                                        cli_credentials_get_password(cmdline_credentials));
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Failed to connect to %s - %s\n", 
                         binding, nt_errstr(status));
-               talloc_destroy(mem_ctx);
+               talloc_free(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;
        r.in.ctr.ctr1 = &ctr1;
        r.in.max_buffer = ~0;
        r.in.resume_handle = &resume_handle;
-       r.out.resume_handle = &resume_handle;
-       r.out.ctr.ctr1 = &ctr1;
 
-       d_printf("\n\tSharename       Type      Comment\n");
-       d_printf("\t---------       ----      -------\n");
+       d_printf("\n\tSharename       Type       Comment\n");
+       d_printf("\t---------       ----       -------\n");
 
        do {
                ZERO_STRUCT(ctr1);
@@ -2295,10 +2589,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_free(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;
        }
 
@@ -2308,7 +2603,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;
@@ -2328,31 +2623,34 @@ 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|name> <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|name> <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}},
+  {"eainfo",cmd_eainfo,"<file> show EA contents for a file",{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}},
+  {"lookup",cmd_lookup,"<sid|name> 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}},
@@ -2363,6 +2661,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}},  
@@ -2378,10 +2677,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. */
@@ -2405,7 +2701,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;
                }
@@ -2423,12 +2719,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 {
@@ -2455,7 +2751,7 @@ static int process_command_string(char *cmd)
        /* establish the connection if not already */
        
        if (!cli) {
-               cli = do_connect(desthost, service);
+               cli = do_connect(desthost, service, cmdline_credentials);
                if (!cli)
                        return 0;
        }
@@ -2478,10 +2774,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 {
@@ -2502,12 +2798,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;
@@ -2517,13 +2813,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)
@@ -2548,7 +2844,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;
 
@@ -2565,7 +2861,7 @@ static char **remote_completion(const char *text, int len)
        } else
                snprintf(dirmask, sizeof(dirmask), "%s*", cur_dir);
 
-       if (cli_list(cli->tree, dirmask, 
+       if (smbcli_list(cli->tree, dirmask, 
                     FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN, 
                     completion_remote_filter, &info) < 0)
                goto cleanup;
@@ -2626,9 +2922,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;
 
@@ -2679,8 +2975,6 @@ make sure we swallow keepalives during idle time
 ****************************************************************************/
 static void readline_callback(void)
 {
-       fd_set fds;
-       struct timeval timeout;
        static time_t last_t;
        time_t t;
 
@@ -2690,31 +2984,10 @@ static void readline_callback(void)
 
        last_t = t;
 
- again:
-       if (cli->transport->socket->fd == -1)
-               return;
-
-       FD_ZERO(&fds);
-       FD_SET(cli->transport->socket->fd, &fds);
-
-       timeout.tv_sec = 0;
-       timeout.tv_usec = 0;
-       sys_select_intr(cli->transport->socket->fd+1,&fds,NULL,NULL,&timeout);
-               
-       /* We deliberately use cli_swallow_keepalives instead of
-          client_receive_smb as we want to receive
-          session keepalives and then drop them here.
-       */
-       if (FD_ISSET(cli->transport->socket->fd, &fds)) {
-               if (!cli_request_receive_next(cli->transport)) {
-                       d_printf("Lost connection to server\n");
-                       exit(1);
-               }
-               goto again;
-       }
+       smbcli_transport_process(cli->transport);
 
        if (cli->tree) {
-               cli_chkpath(cli->tree, "\\");
+               smbcli_chkpath(cli->tree, "\\");
        }
 }
 
@@ -2724,13 +2997,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 */
@@ -2749,10 +3021,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 {
@@ -2765,101 +3037,26 @@ static void process_stdin(void)
 /***************************************************** 
 return a connection to a server
 *******************************************************/
-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;
-       fstring servicename;
-       char *sharename;
+static struct smbcli_state *do_connect(const char *server, const char *share, struct cli_credentials *cred)
+{
+       struct smbcli_state *c;
        NTSTATUS status;
-       
-       /* make a copy so we don't modify the global string 'service' */
-       fstrcpy(servicename, share);
-       sharename = servicename;
-       if (*sharename == '\\') {
-               server = sharename+2;
-               sharename = strchr_m(server,'\\');
-               if (!sharename) return NULL;
-               *sharename = 0;
-               sharename++;
-       }
-
-       asprintf(&sharename, "\\\\%s\\%s", server, sharename);
-
-       server_n = server;
-       
-       zero_ip(&ip);
-
-       make_nmb_name(&calling, lp_netbios_name(), 0x0);
-       make_nmb_name(&called , server, name_type);
-
- again:
-       zero_ip(&ip);
-       if (have_ip) ip = dest_ip;
 
-       /* have to open a new connection */
-       if (!(c=cli_state_init()) || !cli_socket_connect(c, server_n, &ip)) {
-               d_printf("Connection to %s failed\n", server_n);
-               return NULL;
+       if (strncmp(share, "\\\\", 2) == 0 ||
+           strncmp(share, "//", 2) == 0) {
+               smbcli_parse_unc(share, NULL, &server, &share);
        }
-
-       if (!cli_transport_establish(c, &calling, &called)) {
-               char *p;
-               d_printf("session request to %s failed (%s)\n", 
-                        called.name, cli_errstr(c->tree));
-               cli_shutdown(c);
-               if ((p=strchr_m(called.name, '.'))) {
-                       *p = 0;
-                       goto again;
-               }
-               if (strcmp(called.name, "*SMBSERVER")) {
-                       make_nmb_name(&called , "*SMBSERVER", 0x20);
-                       goto again;
-               }
-               return NULL;
-       }
-
-       DEBUG(4,(" session request ok\n"));
-
-       if (NT_STATUS_IS_ERR(cli_negprot(c))) {
-               d_printf("protocol negotiation failed\n");
-               cli_shutdown(c);
-               return NULL;
-       }
-
-       if (!got_pass) {
-               const char *pass = getpass("Password: ");
-               if (pass) {
-                       pstrcpy(password, pass);
-               }
-       }
-
-       status = cli_session_setup(c, username, password, lp_workgroup());
-       if (NT_STATUS_IS_ERR(status)) {
-               /* if a password was not supplied then try again with a null username */
-               if (password[0] || !username[0] || use_kerberos) {
-                       status = cli_session_setup(c, "", "", lp_workgroup());
-               }
-               if (NT_STATUS_IS_ERR(status)) {
-                       d_printf("session setup failed: %s\n", nt_errstr(status));
-                       cli_shutdown(c);
-                       return NULL;
-               }
-               d_printf("Anonymous login successful\n");
-       }
-
-       DEBUG(4,(" session setup ok\n"));
-
-       if (NT_STATUS_IS_ERR(cli_send_tconX(c, sharename, "?????", password))) {
-               d_printf("tree connect failed: %s\n", cli_errstr(c->tree));
-               cli_shutdown(c);
+       
+       status = smbcli_full_connection(NULL, &c, lp_netbios_name(), server,
+                                       share, NULL, cli_credentials_get_username(cred), 
+                                                                cli_credentials_get_domain(cred), 
+                                                                cli_credentials_get_password(cred));
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("Connection to \\\\%s\\%s failed - %s\n", 
+                        server, share, nt_errstr(status));
                return NULL;
        }
 
-       DEBUG(4,(" tconx ok\n"));
-
        return c;
 }
 
@@ -2871,7 +3068,7 @@ static int process(char *base_directory)
 {
        int rc = 0;
 
-       cli = do_connect(desthost, service);
+       cli = do_connect(desthost, service, cmdline_credentials);
        if (!cli) {
                return 1;
        }
@@ -2884,7 +3081,7 @@ static int process(char *base_directory)
                process_stdin();
        }
   
-       cli_shutdown(cli);
+       smbcli_shutdown(cli);
        return rc;
 }
 
@@ -2893,77 +3090,41 @@ handle a -L query
 ****************************************************************************/
 static int do_host_query(char *query_host)
 {
-       cli = do_connect(query_host, "IPC$");
-       if (!cli)
-               return 1;
-
        browse_host(query_host);
        list_servers(lp_workgroup());
-
-       cli_shutdown(cli);
-       
        return(0);
 }
 
 
-/****************************************************************************
-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();
-
-       cli_shutdown(cli);
-
-       return(ret);
-}
-
 /****************************************************************************
 handle a message operation
 ****************************************************************************/
 static int do_message_op(void)
 {
-       struct in_addr ip;
-       struct nmb_name called, calling;
-       fstring server_name;
-       char name_type_hex[10];
+       struct nbt_name called, calling;
+       const char *server_name;
 
-       make_nmb_name(&calling, lp_netbios_name(), 0x0);
-       make_nmb_name(&called , desthost, name_type);
+       calling.name = lp_netbios_name();
+       calling.type = NBT_NAME_CLIENT;
+       calling.scope = NULL;
 
-       fstrcpy(server_name, desthost);
-       snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
-       fstrcat(server_name, name_type_hex);
+       nbt_choose_called_name(NULL, &called, desthost, name_type);
 
-       zero_ip(&ip);
-       if (have_ip) ip = dest_ip;
+       server_name = dest_ip ? dest_ip : desthost;
 
-       if (!(cli=cli_state_init()) || !cli_socket_connect(cli, server_name, &ip)) {
-               d_printf("Connection to %s failed\n", desthost);
+       if (!(cli=smbcli_state_init(NULL)) || !smbcli_socket_connect(cli, server_name)) {
+               d_printf("Connection to %s failed\n", server_name);
                return 1;
        }
 
-       if (!cli_transport_establish(cli, &calling, &called)) {
+       if (!smbcli_transport_establish(cli, &calling, &called)) {
                d_printf("session request failed\n");
-               cli_shutdown(cli);
+               smbcli_shutdown(cli);
                return 1;
        }
 
        send_message();
-       cli_shutdown(cli);
+       smbcli_shutdown(cli);
 
        return 0;
 }
@@ -2999,7 +3160,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;
@@ -3008,14 +3168,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" },
@@ -3023,10 +3180,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 */
@@ -3048,9 +3205,8 @@ static void remember_query_host(const char *arg,
                        argv[0], dyn_CONFIGFILE);
        }
        
-       pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 
-                               POPT_CONTEXT_KEEP_FIRST);
-       poptSetOtherOptionHelp(pc, "service <password>");
+       pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 0);
+       poptSetOtherOptionHelp(pc, "[OPTIONS] service <password>");
 
        in_client = True;   /* Make sure that we tell lp_load we are */
 
@@ -3067,12 +3223,7 @@ static void remember_query_host(const char *arg,
                        message = True;
                        break;
                case 'I':
-                       {
-                               dest_ip = *interpret_addr2(mem_ctx, poptGetOptArg(pc));
-                               if (is_zero_ip(dest_ip))
-                                       exit(1);
-                               have_ip = True;
-                       }
+                       dest_ip = poptGetOptArg(pc);
                        break;
                case 'E':
                        setup_logging("client", DEBUG_STDERR);
@@ -3084,18 +3235,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;
@@ -3105,10 +3244,10 @@ static void remember_query_host(const char *arg,
                }
        }
 
-       poptGetArg(pc);
-       
        load_interfaces();
 
+       smbclient_init_subsystems;
+
        if(poptPeekArg(pc)) {
                pstrcpy(service,poptGetArg(pc));  
                /* Convert any '/' characters in the service name to '\' characters */
@@ -3122,32 +3261,21 @@ static void remember_query_host(const char *arg,
        }
 
        if (poptPeekArg(pc)) { 
-               cmdline_auth_info.got_pass = True;
-               pstrcpy(cmdline_auth_info.password,poptGetArg(pc));  
+               cli_credentials_set_password(cmdline_credentials, poptGetArg(pc), CRED_SPECIFIED);
        }
 
        /*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;
-
        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);
-       }
+       talloc_free(mem_ctx);
 
        if ((p=strchr_m(query_host,'#'))) {
                *p = 0;