Adding the same changes to HEAD as were added to BRANCH_1_9_18.
[samba.git] / source3 / client / client.c
index 4718db84ac02587f72371db6252844374551167c..3ce635fbeb2fab8a68d4d19cff1e1ce62f12134a 100644 (file)
@@ -2,7 +2,7 @@
    Unix SMB/Netbios implementation.
    Version 1.9.
    SMB client
-   Copyright (C) Andrew Tridgell 1994-1997
+   Copyright (C) Andrew Tridgell 1994-1998
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 pstring cur_dir = "\\";
 pstring cd_path = "";
-pstring service="";
-pstring desthost="";
+extern pstring service;
+extern pstring desthost;
 extern pstring myname;
 extern pstring myhostname;
-pstring password = "";
-pstring username="";
-pstring workgroup="";
+extern pstring password;
+extern pstring username;
+extern pstring workgroup;
 char *cmdstr="";
-BOOL got_pass = False;
-BOOL connect_as_printer = False;
-BOOL connect_as_ipc = False;
+extern BOOL got_pass;
+extern BOOL no_pass;
+extern BOOL connect_as_printer;
+extern BOOL connect_as_ipc;
 extern struct in_addr ipzero;
 
-char cryptkey[8];
-BOOL doencrypt=False;
+extern BOOL doencrypt;
 
 extern pstring user_socket_options;
 
@@ -56,9 +56,9 @@ extern pstring user_socket_options;
 /* value for unused fid field in trans2 secondary request */
 #define FID_UNUSED (0xFFFF)
 
-int name_type = 0x20;
+extern int name_type;
 
-int max_protocol = PROTOCOL_NT1;
+extern int max_protocol;
 
 
 time_t newer_than = 0;
@@ -69,23 +69,19 @@ extern int DEBUGLEVEL;
 
 BOOL translation = False;
 
+extern int cnum;
+extern int mid;
+extern int pid;
+extern int tid;
+extern int gid;
+extern int uid;
+
+extern BOOL have_ip;
+extern int max_xmit;
 
-static BOOL send_trans_request(char *outbuf,int trans,
-                              char *name,int fid,int flags,
-                              char *data,char *param,uint16 *setup,
-                              int ldata,int lparam,int lsetup,
-                              int mdata,int mparam,int msetup);
-static BOOL receive_trans_response(char *inbuf,int trans,
-                                   int *data_len,int *param_len,
-                                  char **data,char **param);
 static int interpret_long_filename(int level,char *p,file_info *finfo);
 static void dir_action(char *inbuf,char *outbuf,int attribute,file_info *finfo,BOOL recurse_dir,void (*fn)(),BOOL longdir);
 static int interpret_short_filename(char *p,file_info *finfo);
-static BOOL call_api(int prcnt,int drcnt,
-                    int mprcnt,int mdrcnt,
-                    int *rprcnt,int *rdrcnt,
-                    char *param,char *data,
-                    char **rparam,char **rdata);
 static BOOL do_this_one(file_info *finfo);
 
 /* clitar bits insert */
@@ -95,15 +91,8 @@ extern BOOL tar_reset;
 /* clitar bits end */
  
 
-int cnum = 0;
-int pid = 0;
-int gid = 0;
-int uid = 0;
-int mid = 0;
 int myumask = 0755;
 
-int max_xmit = BUFFER_SIZE;
-
 extern pstring scope;
 
 BOOL prompt = True;
@@ -113,8 +102,6 @@ int printmode = 1;
 BOOL recurse = False;
 BOOL lowercase = False;
 
-BOOL have_ip = False;
-
 struct in_addr dest_ip;
 
 #define SEPARATORS " \t\n\r"
@@ -123,8 +110,8 @@ BOOL abort_mget = True;
 
 extern int Protocol;
 
-BOOL readbraw_supported = False;
-BOOL writebraw_supported = False;
+extern BOOL readbraw_supported ;
+extern BOOL writebraw_supported;
 
 pstring fileselection = "";
 
@@ -143,39 +130,25 @@ extern int Client;
 
 #define USENMB
 
-#ifdef KANJI
-extern int coding_system;
-#define CNV_LANG(s) (coding_system == DOSV_CODE?s:dos_to_unix(s, False))
-#define CNV_INPUT(s) (coding_system == DOSV_CODE?s:unix_to_dos(s, True))
-static BOOL
-setup_term_code (char *code)
-{
-    int new;
-    new = interpret_coding_system (code, UNKNOWN_CODE);
-    if (new != UNKNOWN_CODE) {
-       coding_system = new;
-       return True;
-    }
-    return False;
-}
-#else
-#define CNV_LANG(s) dos2unix_format(s,False)
-#define CNV_INPUT(s) unix2dos_format(s,True)
-#endif
+#define CNV_LANG(s) dos_to_unix(s,False)
+#define CNV_INPUT(s) unix_to_dos(s,True)
 
 /****************************************************************************
-setup basics in a outgoing packet
+send an SMBclose on an SMB file handle
 ****************************************************************************/
-void setup_pkt(char *outbuf)
+void cli_smb_close(char *inbuf, char *outbuf, int clnt_fd, int c_num, int f_num)
 {
-  SSVAL(outbuf,smb_pid,pid);
-  SSVAL(outbuf,smb_uid,uid);
-  SSVAL(outbuf,smb_mid,mid);
-  if (Protocol > PROTOCOL_COREPLUS)
-    {
-      SCVAL(outbuf,smb_flg,0x8);
-      SSVAL(outbuf,smb_flg2,0x1);
-    }
+  bzero(outbuf,smb_size);
+  set_message(outbuf,3,0,True);
+
+  CVAL (outbuf,smb_com) = SMBclose;
+  SSVAL(outbuf,smb_tid,c_num);
+  cli_setup_pkt(outbuf);
+  SSVAL (outbuf,smb_vwv0, f_num);
+  SIVALS(outbuf,smb_vwv1, -1);
+  
+  send_smb(clnt_fd, outbuf);
+  client_receive_smb(clnt_fd,inbuf,CLIENT_TIMEOUT);
 }
 
 /****************************************************************************
@@ -275,14 +248,26 @@ static BOOL chkpath(char *path,BOOL report)
   set_message(outbuf,0,4 + strlen(path2),True);
   SCVAL(outbuf,smb_com,SMBchkpth);
   SSVAL(outbuf,smb_tid,cnum);
-  setup_pkt(outbuf);
+  cli_setup_pkt(outbuf);
 
   p = smb_buf(outbuf);
   *p++ = 4;
   strcpy(p,path2);
 
+#if 0
+  {
+         /* this little bit of code can be used to extract NT error codes.
+            Just feed a bunch of "cd foo" commands to smbclient then watch
+            in netmon (tridge) */
+         static int code=0;
+         SIVAL(outbuf, smb_rcls, code | 0xC0000000);
+         SSVAL(outbuf, smb_flg2, SVAL(outbuf, smb_flg2) | (1<<14));
+         code++;
+  }
+#endif
+
   send_smb(Client,outbuf);
-  receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
 
   if (report && CVAL(inbuf,smb_rcls) != 0)
     DEBUG(2,("chkpath: %s\n",smb_errstr(inbuf)));
@@ -320,7 +305,7 @@ static void send_message(char *inbuf,char *outbuf)
   send_smb(Client,outbuf);
   
 
-  if (!receive_smb(Client,inbuf,SHORT_TIMEOUT) || CVAL(inbuf,smb_rcls) != 0)
+  if (!client_receive_smb(Client,inbuf,SHORT_TIMEOUT) || CVAL(inbuf,smb_rcls) != 0)
     {
       printf("SMBsendstrt failed. (%s)\n",smb_errstr(inbuf));
       return;
@@ -360,7 +345,7 @@ static void send_message(char *inbuf,char *outbuf)
       send_smb(Client,outbuf);
       
 
-      if (!receive_smb(Client,inbuf,SHORT_TIMEOUT) || CVAL(inbuf,smb_rcls) != 0)
+      if (!client_receive_smb(Client,inbuf,SHORT_TIMEOUT) || CVAL(inbuf,smb_rcls) != 0)
        {
          printf("SMBsendtxt failed (%s)\n",smb_errstr(inbuf));
          return;
@@ -383,7 +368,7 @@ static void send_message(char *inbuf,char *outbuf)
   send_smb(Client,outbuf);
   
 
-  if (!receive_smb(Client,inbuf,SHORT_TIMEOUT) || CVAL(inbuf,smb_rcls) != 0)
+  if (!client_receive_smb(Client,inbuf,SHORT_TIMEOUT) || CVAL(inbuf,smb_rcls) != 0)
     {
       printf("SMBsendend failed (%s)\n",smb_errstr(inbuf));
       return;
@@ -403,10 +388,10 @@ static void do_dskattr(void)
   set_message(outbuf,0,0,True);
   CVAL(outbuf,smb_com) = SMBdskattr;
   SSVAL(outbuf,smb_tid,cnum);
-  setup_pkt(outbuf);
+  cli_setup_pkt(outbuf);
 
   send_smb(Client,outbuf);
-  receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
 
   if (CVAL(inbuf,smb_rcls) != 0) 
     DEBUG(0,("Error in dskattr: %s\n",smb_errstr(inbuf)));      
@@ -556,12 +541,12 @@ static int do_long_dir(char *inbuf,char *outbuf,char *Mask,int attribute,void (*
        }
       /* ??? original code added 1 pad byte after param */
 
-      send_trans_request(outbuf,SMBtrans2,NULL,FID_UNUSED,0,
+      cli_send_trans_request(outbuf,SMBtrans2,NULL,0,FID_UNUSED,0,
                         NULL,param,&setup,
                         0,12+strlen(mask)+1,1,
                         BUFFER_SIZE,10,0);
 
-      if (!receive_trans_response(inbuf,SMBtrans2,
+      if (!cli_receive_trans_response(inbuf,SMBtrans2,
                              &resp_data_len,&resp_param_len,
                                  &resp_data,&resp_param))
        {
@@ -700,7 +685,7 @@ static int do_short_dir(char *inbuf,char *outbuf,char *Mask,int attribute,void (
        CVAL(outbuf,smb_com) = SMBsearch;
 
       SSVAL(outbuf,smb_tid,cnum);
-      setup_pkt(outbuf);
+      cli_setup_pkt(outbuf);
 
       SSVAL(outbuf,smb_vwv0,num_asked);
       SSVAL(outbuf,smb_vwv1,attribute);
@@ -725,7 +710,7 @@ static int do_short_dir(char *inbuf,char *outbuf,char *Mask,int attribute,void (
        }
 
       send_smb(Client,outbuf);
-      receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+      client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
 
       received = SVAL(inbuf,smb_vwv0);
 
@@ -761,7 +746,7 @@ static int do_short_dir(char *inbuf,char *outbuf,char *Mask,int attribute,void (
       CVAL(outbuf,smb_com) = SMBfclose;
 
       SSVAL(outbuf,smb_tid,cnum);
-      setup_pkt(outbuf);
+      cli_setup_pkt(outbuf);
 
       p = smb_buf(outbuf);
       *p++ = 4;
@@ -775,7 +760,7 @@ static int do_short_dir(char *inbuf,char *outbuf,char *Mask,int attribute,void (
       memcpy(p,status,21);
 
       send_smb(Client,outbuf);
-      receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+      client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
 
       if (CVAL(inbuf,smb_rcls) != 0) 
        DEBUG(0,("Error closing search: %s\n",smb_errstr(inbuf)));      
@@ -836,7 +821,7 @@ static BOOL do_this_one(file_info *finfo)
 
 
 /*****************************************************************************
- Convert a character pointer in a call_api() response to a form we can use.
+ Convert a character pointer in a cli_call_api() response to a form we can use.
  This function contains code to prevent core dumps if the server returns 
  invalid data.
 *****************************************************************************/
@@ -1027,80 +1012,6 @@ static void dir_action(char *inbuf,char *outbuf,int attribute,file_info *finfo,B
 }
 
 
-/****************************************************************************
-  receive a SMB trans or trans2 response allocating the necessary memory
-  ****************************************************************************/
-static BOOL receive_trans_response(char *inbuf,int trans,
-                                   int *data_len,int *param_len,
-                                  char **data,char **param)
-{
-  int total_data=0;
-  int total_param=0;
-  int this_data,this_param;
-
-  *data_len = *param_len = 0;
-
-  receive_smb(Client,inbuf,CLIENT_TIMEOUT);
-  show_msg(inbuf);
-
-  /* sanity check */
-  if (CVAL(inbuf,smb_com) != trans)
-    {
-      DEBUG(0,("Expected %s response, got command 0x%02x\n",
-              trans==SMBtrans?"SMBtrans":"SMBtrans2", CVAL(inbuf,smb_com)));
-      return(False);
-    }
-  if (CVAL(inbuf,smb_rcls) != 0)
-    return(False);
-
-  /* parse out the lengths */
-  total_data = SVAL(inbuf,smb_tdrcnt);
-  total_param = SVAL(inbuf,smb_tprcnt);
-
-  /* allocate it */
-  *data = Realloc(*data,total_data);
-  *param = Realloc(*param,total_param);
-
-  while (1)
-    {
-      this_data = SVAL(inbuf,smb_drcnt);
-      this_param = SVAL(inbuf,smb_prcnt);
-      if (this_data)
-       memcpy(*data + SVAL(inbuf,smb_drdisp),
-              smb_base(inbuf) + SVAL(inbuf,smb_droff),
-              this_data);
-      if (this_param)
-       memcpy(*param + SVAL(inbuf,smb_prdisp),
-              smb_base(inbuf) + SVAL(inbuf,smb_proff),
-              this_param);
-      *data_len += this_data;
-      *param_len += this_param;
-
-      /* parse out the total lengths again - they can shrink! */
-      total_data = SVAL(inbuf,smb_tdrcnt);
-      total_param = SVAL(inbuf,smb_tprcnt);
-
-      if (total_data <= *data_len && total_param <= *param_len)
-       break;
-
-      receive_smb(Client,inbuf,CLIENT_TIMEOUT);
-      show_msg(inbuf);
-
-      /* sanity check */
-      if (CVAL(inbuf,smb_com) != trans)
-       {
-         DEBUG(0,("Expected %s response, got command 0x%02x\n",
-                  trans==SMBtrans?"SMBtrans":"SMBtrans2", CVAL(inbuf,smb_com)));
-         return(False);
-       }
-      if (CVAL(inbuf,smb_rcls) != 0)
-         return(False);
-    }
-  
-  return(True);
-}
-
-
 /****************************************************************************
   get a directory listing
   ****************************************************************************/
@@ -1178,14 +1089,16 @@ static void do_get(char *rname,char *lname,file_info *finfo1)
 
   CVAL(outbuf,smb_com) = SMBopenX;
   SSVAL(outbuf,smb_tid,cnum);
-  setup_pkt(outbuf);
+  cli_setup_pkt(outbuf);
 
   SSVAL(outbuf,smb_vwv0,0xFF);
-  SSVAL(outbuf,smb_vwv2,1);
+  SSVAL(outbuf,smb_vwv2,1); /* return additional info */
   SSVAL(outbuf,smb_vwv3,(DENY_NONE<<4));
   SSVAL(outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
   SSVAL(outbuf,smb_vwv5,aSYSTEM | aHIDDEN);
   SSVAL(outbuf,smb_vwv8,1);
+  SSVAL(outbuf,smb_vwv11,0xffff);
+  SSVAL(outbuf,smb_vwv12,0xffff);
   
   p = smb_buf(outbuf);
   strcpy(p,rname);
@@ -1223,13 +1136,13 @@ static void do_get(char *rname,char *lname,file_info *finfo1)
     }
 
   send_smb(Client,outbuf);
-  receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
 
   if (CVAL(inbuf,smb_rcls) != 0)
     {
       if (CVAL(inbuf,smb_rcls) == ERRSRV &&
          SVAL(inbuf,smb_err) == ERRnoresource &&
-         reopen_connection(inbuf,outbuf))
+         cli_reopen_connection(inbuf,outbuf))
        {
          do_get(rname,lname,finfo1);
          return;
@@ -1319,7 +1232,7 @@ static void do_get(char *rname,char *lname,file_info *finfo1)
          set_message(outbuf,10,0,True);
          CVAL(outbuf,smb_com) = SMBreadX;
          SSVAL(outbuf,smb_tid,cnum);
-         setup_pkt(outbuf);
+         cli_setup_pkt(outbuf);
          
          if (close_done)
            {
@@ -1350,7 +1263,7 @@ static void do_get(char *rname,char *lname,file_info *finfo1)
            }
          
          send_smb(Client,outbuf);
-         receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+         client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
          
          if (CVAL(inbuf,smb_rcls) != 0)
            {
@@ -1383,7 +1296,7 @@ static void do_get(char *rname,char *lname,file_info *finfo1)
            set_message(outbuf,8,0,True);
            CVAL(outbuf,smb_com) = SMBreadbraw;
            SSVAL(outbuf,smb_tid,cnum);
-           setup_pkt(outbuf);
+           cli_setup_pkt(outbuf);
            SSVAL(outbuf,smb_vwv0,fnum);
            SIVAL(outbuf,smb_vwv1,nread);
            SSVAL(outbuf,smb_vwv3,MIN(finfo.size-nread,readbraw_size));
@@ -1435,7 +1348,7 @@ static void do_get(char *rname,char *lname,file_info *finfo1)
          set_message(outbuf,5,0,True);
          CVAL(outbuf,smb_com) = SMBread;
          SSVAL(outbuf,smb_tid,cnum);
-         setup_pkt(outbuf);
+         cli_setup_pkt(outbuf);
 
          SSVAL(outbuf,smb_vwv0,fnum);
          SSVAL(outbuf,smb_vwv1,MIN(max_xmit-200,finfo.size - nread));
@@ -1443,7 +1356,7 @@ static void do_get(char *rname,char *lname,file_info *finfo1)
          SSVAL(outbuf,smb_vwv4,finfo.size - nread);
 
          send_smb(Client,outbuf);
-         receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+         client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
 
          if (CVAL(inbuf,smb_rcls) != 0)
            {
@@ -1477,17 +1390,7 @@ static void do_get(char *rname,char *lname,file_info *finfo1)
 
   if (!close_done)
     {
-      bzero(outbuf,smb_size);
-      set_message(outbuf,3,0,True);
-      CVAL(outbuf,smb_com) = SMBclose;
-      SSVAL(outbuf,smb_tid,cnum);
-      setup_pkt(outbuf);
-      
-      SSVAL(outbuf,smb_vwv0,fnum);
-      SIVALS(outbuf,smb_vwv1,-1);
-      
-      send_smb(Client,outbuf);
-      receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+      cli_smb_close(inbuf, outbuf, Client, cnum, fnum);
       
       if (!ignore_close_error && CVAL(inbuf,smb_rcls) != 0)
        {
@@ -1507,7 +1410,7 @@ static void do_get(char *rname,char *lname,file_info *finfo1)
     set_message(outbuf,8,strlen(rname)+4,True);
     CVAL(outbuf,smb_com) = SMBsetatr;
     SSVAL(outbuf,smb_tid,cnum);
-    setup_pkt(outbuf);
+    cli_setup_pkt(outbuf);
     SSVAL(outbuf,smb_vwv0,finfo.mode & ~(aARCH));
     SIVALS(outbuf,smb_vwv1,0);
     p = smb_buf(outbuf);
@@ -1517,7 +1420,7 @@ static void do_get(char *rname,char *lname,file_info *finfo1)
     *p++ = 4;
     *p = 0;
     send_smb(Client,outbuf);
-    receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+    client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
   }
 
   {
@@ -1742,7 +1645,7 @@ static BOOL do_mkdir(char *name)
   
   CVAL(outbuf,smb_com) = SMBmkdir;
   SSVAL(outbuf,smb_tid,cnum);
-  setup_pkt(outbuf);
+  cli_setup_pkt(outbuf);
 
   
   p = smb_buf(outbuf);
@@ -1750,7 +1653,7 @@ static BOOL do_mkdir(char *name)
   strcpy(p,name);
   
   send_smb(Client,outbuf);
-  receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
   
   if (CVAL(inbuf,smb_rcls) != 0)
     {
@@ -1824,7 +1727,7 @@ static int smb_writeraw(char *outbuf,int fnum,int pos,char *buf,int n)
 
   CVAL(outbuf,smb_com) = SMBwritebraw;
   SSVAL(outbuf,smb_tid,cnum);
-  setup_pkt(outbuf);
+  cli_setup_pkt(outbuf);
 
   SSVAL(outbuf,smb_vwv0,fnum);
   SSVAL(outbuf,smb_vwv1,n);
@@ -1833,7 +1736,7 @@ static int smb_writeraw(char *outbuf,int fnum,int pos,char *buf,int n)
 
   send_smb(Client,outbuf);
   
-  if (!receive_smb(Client,inbuf,CLIENT_TIMEOUT) || CVAL(inbuf,smb_rcls) != 0)
+  if (!client_receive_smb(Client,inbuf,CLIENT_TIMEOUT) || CVAL(inbuf,smb_rcls) != 0)
     return(0);
 
   _smb_setlen(buf-4,n);                /* HACK! XXXX */
@@ -1841,7 +1744,7 @@ static int smb_writeraw(char *outbuf,int fnum,int pos,char *buf,int n)
   if (write_socket(Client,buf-4,n+4) != n+4)
     return(0);
 
-  if (!receive_smb(Client,inbuf,CLIENT_TIMEOUT) || CVAL(inbuf,smb_rcls) != 0) {
+  if (!client_receive_smb(Client,inbuf,CLIENT_TIMEOUT) || CVAL(inbuf,smb_rcls) != 0) {
     DEBUG(0,("Error writing remote file (2)\n"));
     return(0);
   }
@@ -1866,7 +1769,7 @@ static int smb_writefile(char *outbuf,int fnum,int pos,char *buf,int n)
 
   CVAL(outbuf,smb_com) = SMBwrite;
   SSVAL(outbuf,smb_tid,cnum);
-  setup_pkt(outbuf);
+  cli_setup_pkt(outbuf);
 
   SSVAL(outbuf,smb_vwv0,fnum);
   SSVAL(outbuf,smb_vwv1,n);
@@ -1878,7 +1781,7 @@ static int smb_writefile(char *outbuf,int fnum,int pos,char *buf,int n)
   memcpy(smb_buf(outbuf)+3,buf,n);
 
   send_smb(Client,outbuf);
-  receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
 
   if (CVAL(inbuf,smb_rcls) != 0) {
     DEBUG(0,("%s writing remote file\n",smb_errstr(inbuf)));
@@ -1923,7 +1826,7 @@ static void do_put(char *rname,char *lname,file_info *finfo)
 
   CVAL(outbuf,smb_com) = SMBcreate;
   SSVAL(outbuf,smb_tid,cnum);
-  setup_pkt(outbuf);
+  cli_setup_pkt(outbuf);
 
   SSVAL(outbuf,smb_vwv0,finfo->mode);
   put_dos_date3(outbuf,smb_vwv1,finfo->mtime);
@@ -1933,7 +1836,7 @@ static void do_put(char *rname,char *lname,file_info *finfo)
   strcpy(p,rname);
   
   send_smb(Client,outbuf);
-  receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
   
   if (CVAL(inbuf,smb_rcls) != 0)
     {
@@ -1999,13 +1902,13 @@ static void do_put(char *rname,char *lname,file_info *finfo)
   set_message(outbuf,3,0,True);
   CVAL(outbuf,smb_com) = SMBclose;
   SSVAL(outbuf,smb_tid,cnum);
-  setup_pkt(outbuf);
+  cli_setup_pkt(outbuf);
 
   SSVAL(outbuf,smb_vwv0,fnum);  
   put_dos_date3(outbuf,smb_vwv1,close_time);
 
   send_smb(Client,outbuf);
-  receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
   
   if (CVAL(inbuf,smb_rcls) != 0)
     {
@@ -2224,10 +2127,10 @@ static void do_cancel(int job)
   SSVAL(p,0,job);     
   p += 2;
 
-  if (call_api(PTR_DIFF(p,param),0,
-              6,1000,
+  if (cli_call_api(PIPE_LANMAN, 0,PTR_DIFF(p,param),0, 0,
+           6, 1000,
               &rprcnt,&rdrcnt,
-              param,NULL,
+              param,NULL, NULL,
               &rparam,&rdata))
     {
       int res = SVAL(rparam,0);
@@ -2273,43 +2176,6 @@ static void cmd_cancel(char *inbuf,char *outbuf )
 }
 
 
-/****************************************************************************
-  get info on a file
-  ****************************************************************************/
-static void cmd_stat(char *inbuf,char *outbuf)
-{
-  fstring buf;
-  pstring param;
-  char *resp_data=NULL;
-  char *resp_param=NULL;
-  int resp_data_len = 0;
-  int resp_param_len=0;
-  char *p;
-  uint16 setup = TRANSACT2_QPATHINFO;
-
-  if (!next_token(NULL,buf,NULL)) {
-    printf("stat <file>\n");
-    return;
-  }
-
-  bzero(param,6);
-  SSVAL(param,0,4); /* level */
-  p = param+6;
-  strcpy(p,cur_dir);
-  strcat(p,buf);
-
-  send_trans_request(outbuf,SMBtrans2,NULL,FID_UNUSED,0,
-                    NULL,param,&setup,
-                    0,6 + strlen(p)+1,1,
-                    BUFFER_SIZE,2,0);
-
-  receive_trans_response(inbuf,SMBtrans2,
-                         &resp_data_len,&resp_param_len,
-                         &resp_data,&resp_param);
-
-  if (resp_data) free(resp_data); resp_data = NULL;
-  if (resp_param) free(resp_param); resp_param = NULL;
-}
 
 
 /****************************************************************************
@@ -2361,7 +2227,7 @@ static void cmd_print(char *inbuf,char *outbuf )
   
   CVAL(outbuf,smb_com) = SMBsplopen;
   SSVAL(outbuf,smb_tid,cnum);
-  setup_pkt(outbuf);
+  cli_setup_pkt(outbuf);
 
   SSVAL(outbuf,smb_vwv0,0);
   SSVAL(outbuf,smb_vwv1,printmode);
@@ -2371,7 +2237,7 @@ static void cmd_print(char *inbuf,char *outbuf )
   strcpy(p,rname);
   
   send_smb(Client,outbuf);
-  receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
   
   if (CVAL(inbuf,smb_rcls) != 0)
     {
@@ -2417,7 +2283,7 @@ static void cmd_print(char *inbuf,char *outbuf )
 
       CVAL(outbuf,smb_com) = SMBsplwr;
       SSVAL(outbuf,smb_tid,cnum);
-      setup_pkt(outbuf);
+      cli_setup_pkt(outbuf);
 
       SSVAL(outbuf,smb_vwv0,fnum);
       SSVAL(outbuf,smb_vwv1,n+3);
@@ -2425,7 +2291,7 @@ static void cmd_print(char *inbuf,char *outbuf )
       SSVAL(smb_buf(outbuf),1,n);
 
       send_smb(Client,outbuf);
-      receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+      client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
 
       if (CVAL(inbuf,smb_rcls) != 0)
        {
@@ -2442,12 +2308,12 @@ static void cmd_print(char *inbuf,char *outbuf )
   set_message(outbuf,1,0,True);
   CVAL(outbuf,smb_com) = SMBsplclose;
   SSVAL(outbuf,smb_tid,cnum);
-  setup_pkt(outbuf);
+  cli_setup_pkt(outbuf);
 
   SSVAL(outbuf,smb_vwv0,fnum);
 
   send_smb(Client,outbuf);
-  receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
   
   if (CVAL(inbuf,smb_rcls) != 0)
     {
@@ -2475,13 +2341,13 @@ static void cmd_queue(char *inbuf,char *outbuf )
   
   CVAL(outbuf,smb_com) = SMBsplretq;
   SSVAL(outbuf,smb_tid,cnum);
-  setup_pkt(outbuf);
+  cli_setup_pkt(outbuf);
 
   SSVAL(outbuf,smb_vwv0,32); /* a max of 20 entries is to be shown */
   SSVAL(outbuf,smb_vwv1,0); /* the index into the queue */
   
   send_smb(Client,outbuf);
-  receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
   
   if (CVAL(inbuf,smb_rcls) != 0)
     {
@@ -2560,10 +2426,10 @@ static void cmd_p_queue_4(char *inbuf,char *outbuf )
   p = skip_string(p,1);
 
   DEBUG(1,("Calling DosPrintJobEnum()...\n"));
-  if( call_api(PTR_DIFF(p,param), 0,
+  if( cli_call_api(PIPE_LANMAN, 0,PTR_DIFF(p,param), 0, 0,
                10, 4096,
                &rprcnt, &rdrcnt,
-               param, NULL,
+               param, NULL, NULL,
                &rparam, &rdata) )
     {
       int converter;
@@ -2625,7 +2491,7 @@ static void cmd_p_queue_4(char *inbuf,char *outbuf )
             }
         }
     }
-  else                  /* call_api() failed */
+  else                  /* cli_call_api() failed */
     {
       printf("Failed, error = %d\n", result_code);
     }
@@ -2667,10 +2533,10 @@ static void cmd_qinfo(char *inbuf,char *outbuf )
   p = skip_string(p,1);
 
   DEBUG(1,("Calling DosPrintQueueGetInfo()...\n"));
-  if( call_api(PTR_DIFF(p,param), 0,
-              10, 4096,
+  if( cli_call_api(PIPE_LANMAN, 0,PTR_DIFF(p,param), 0, 0,
+           10, 4096,
               &rprcnt, &rdrcnt,
-              param, NULL,
+              param, NULL, NULL,
               &rparam, &rdata) )
        {
        int converter;
@@ -2728,7 +2594,7 @@ static void cmd_qinfo(char *inbuf,char *outbuf )
            
            }
        }
-  else                 /* call_api() failed */
+  else                 /* cli_call_api() failed */
        {
        printf("Failed, error = %d\n", result_code);
        }
@@ -2769,7 +2635,7 @@ static void do_del(file_info *finfo)
   
   CVAL(outbuf,smb_com) = SMBunlink;
   SSVAL(outbuf,smb_tid,cnum);
-  setup_pkt(outbuf);
+  cli_setup_pkt(outbuf);
 
   SSVAL(outbuf,smb_vwv0,0);
   
@@ -2778,7 +2644,7 @@ static void do_del(file_info *finfo)
   strcpy(p,mask);
   
   send_smb(Client,outbuf);
-  receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
   
   if (CVAL(inbuf,smb_rcls) != 0)
     DEBUG(0,("%s deleting remote file %s\n",smb_errstr(inbuf),CNV_LANG(mask)));
@@ -2835,7 +2701,7 @@ static void cmd_rmdir(char *inbuf,char *outbuf )
   
   CVAL(outbuf,smb_com) = SMBrmdir;
   SSVAL(outbuf,smb_tid,cnum);
-  setup_pkt(outbuf);
+  cli_setup_pkt(outbuf);
 
   
   p = smb_buf(outbuf);
@@ -2843,7 +2709,7 @@ static void cmd_rmdir(char *inbuf,char *outbuf )
   strcpy(p,mask);
   
   send_smb(Client,outbuf);
-  receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
   
   if (CVAL(inbuf,smb_rcls) != 0)
     {
@@ -2879,7 +2745,7 @@ static void cmd_rename(char *inbuf,char *outbuf )
   CVAL(outbuf,smb_com) = SMBmv;
   SSVAL(outbuf,smb_tid,cnum);
   SSVAL(outbuf,smb_vwv0,aHIDDEN | aDIR | aSYSTEM);
-  setup_pkt(outbuf);
+  cli_setup_pkt(outbuf);
   
   p = smb_buf(outbuf);
   *p++ = 4;      
@@ -2889,7 +2755,7 @@ static void cmd_rename(char *inbuf,char *outbuf )
   strcpy(p,dest);
   
   send_smb(Client,outbuf);
-  receive_smb(Client,inbuf,CLIENT_TIMEOUT);
+  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
   
   if (CVAL(inbuf,smb_rcls) != 0)
     {
@@ -3029,670 +2895,6 @@ static void cmd_lcd(void)
 }
 
 
-/****************************************************************************
-send a session request
-****************************************************************************/
-static BOOL send_session_request(char *inbuf,char *outbuf)
-{
-  fstring dest;
-  char *p;
-  int len = 4;
-  /* send a session request (RFC 8002) */
-
-  strcpy(dest,desthost);
-  p = strchr(dest,'.');
-  if (p) *p = 0;
-
-  /* put in the destination name */
-  p = outbuf+len;
-  name_mangle(dest,p,name_type); /* 0x20 is the SMB server NetBIOS type. */
-  len += name_len(p);
-
-  /* and my name */
-  p = outbuf+len;
-  name_mangle(myname,p,0);
-  len += name_len(p);
-
-  /* setup the packet length */
-  _smb_setlen(outbuf,len);
-  CVAL(outbuf,0) = 0x81;
-
-  send_smb(Client,outbuf);
-  DEBUG(5,("Sent session request\n"));
-
-  receive_smb(Client,inbuf,CLIENT_TIMEOUT);
-
-  if (CVAL(inbuf,0) == 0x84) /* C. Hoch  9/14/95 Start */
-    {
-      /* For information, here is the response structure.
-       * We do the byte-twiddling to for portability.
-       struct RetargetResponse{
-       unsigned char type;
-       unsigned char flags;
-       int16 length;
-       int32 ip_addr;
-       int16 port;
-       };
-       */
-      extern int Client;
-      int port = (CVAL(inbuf,8)<<8)+CVAL(inbuf,9);
-      /* SESSION RETARGET */
-      putip((char *)&dest_ip,inbuf+4);
-
-      close_sockets();
-      Client = open_socket_out(SOCK_STREAM, &dest_ip, port, LONG_CONNECT_TIMEOUT);
-      if (Client == -1)
-        return False;
-
-      DEBUG(3,("Retargeted\n"));
-
-      set_socket_options(Client,user_socket_options);
-
-      /* Try again */
-      return send_session_request(inbuf,outbuf);
-    } /* C. Hoch 9/14/95 End */
-
-
-  if (CVAL(inbuf,0) != 0x82)
-    {
-      int ecode = CVAL(inbuf,4);
-      DEBUG(0,("Session request failed (%d,%d) with myname=%s destname=%s\n",
-              CVAL(inbuf,0),ecode,myname,desthost));
-      switch (ecode)
-       {
-       case 0x80: 
-         DEBUG(0,("Not listening on called name\n")); 
-         DEBUG(0,("Try to connect to another name (instead of %s)\n",desthost));
-         DEBUG(0,("You may find the -I option useful for this\n"));
-         break;
-       case 0x81: 
-         DEBUG(0,("Not listening for calling name\n")); 
-         DEBUG(0,("Try to connect as another name (instead of %s)\n",myname));
-         DEBUG(0,("You may find the -n option useful for this\n"));
-         break;
-       case 0x82: 
-         DEBUG(0,("Called name not present\n")); 
-         DEBUG(0,("Try to connect to another name (instead of %s)\n",desthost));
-         DEBUG(0,("You may find the -I option useful for this\n"));
-         break;
-       case 0x83: 
-         DEBUG(0,("Called name present, but insufficient resources\n")); 
-         DEBUG(0,("Perhaps you should try again later?\n")); 
-         break;
-       default:
-         DEBUG(0,("Unspecified error 0x%X\n",ecode)); 
-         DEBUG(0,("Your server software is being unfriendly\n"));
-         break;          
-       }
-      return(False);
-    }
-  return(True);
-}
-
-static struct {
-  int prot;
-  char *name;
-} prots[] = {
-  {PROTOCOL_CORE,"PC NETWORK PROGRAM 1.0"},
-  {PROTOCOL_COREPLUS,"MICROSOFT NETWORKS 1.03"},
-  {PROTOCOL_LANMAN1,"MICROSOFT NETWORKS 3.0"},
-  {PROTOCOL_LANMAN1,"LANMAN1.0"},
-  {PROTOCOL_LANMAN2,"LM1.2X002"},
-  {PROTOCOL_LANMAN2,"Samba"},
-  {PROTOCOL_NT1,"NT LM 0.12"},
-  {PROTOCOL_NT1,"NT LANMAN 1.0"},
-  {-1,NULL}
-};
-
-
-/****************************************************************************
-send a login command
-****************************************************************************/
-static BOOL send_login(char *inbuf,char *outbuf,BOOL start_session,BOOL use_setup)
-{
-  BOOL was_null = (!inbuf && !outbuf);
-  int sesskey=0;
-  time_t servertime = 0;
-  extern int serverzone;
-  int sec_mode=0;
-  int crypt_len;
-  int max_vcs=0;
-  char *pass = NULL;  
-  pstring dev;
-  char *p;
-  int numprots;
-  int tries=0;
-
-  if (was_null)
-    {
-      inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
-      outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
-    }
-
-#if AJT
-  if (strstr(service,"IPC$")) connect_as_ipc = True;
-#endif
-
-  strcpy(dev,"A:");
-  if (connect_as_printer)
-    strcpy(dev,"LPT1:");
-  if (connect_as_ipc)
-    strcpy(dev,"IPC");
-
-
-  if (start_session && !send_session_request(inbuf,outbuf))
-    {
-      if (was_null)
-       {
-         free(inbuf);
-         free(outbuf);
-       }      
-      return(False);
-    }
-
-  bzero(outbuf,smb_size);
-
-  /* setup the protocol strings */
-  {
-    int plength;
-
-    for (plength=0,numprots=0;
-        prots[numprots].name && prots[numprots].prot<=max_protocol;
-        numprots++)
-      plength += strlen(prots[numprots].name)+2;
-    
-    set_message(outbuf,0,plength,True);
-
-    p = smb_buf(outbuf);
-    for (numprots=0;
-        prots[numprots].name && prots[numprots].prot<=max_protocol;
-        numprots++)
-      {
-       *p++ = 2;
-       strcpy(p,prots[numprots].name);
-       p += strlen(p) + 1;
-      }
-  }
-
-  CVAL(outbuf,smb_com) = SMBnegprot;
-  setup_pkt(outbuf);
-
-  CVAL(smb_buf(outbuf),0) = 2;
-
-  send_smb(Client,outbuf);
-  receive_smb(Client,inbuf,CLIENT_TIMEOUT);
-
-  show_msg(inbuf);
-
-  if (CVAL(inbuf,smb_rcls) != 0 || ((int)SVAL(inbuf,smb_vwv0) >= numprots))
-    {
-      DEBUG(0,("SMBnegprot failed. myname=%s destname=%s - %s \n",
-           myname,desthost,smb_errstr(inbuf)));
-      if (was_null)
-       {
-         free(inbuf);
-         free(outbuf);
-       }
-      return(False);
-    }
-
-  Protocol = prots[SVAL(inbuf,smb_vwv0)].prot;
-
-
-  if (Protocol < PROTOCOL_NT1) {    
-    sec_mode = SVAL(inbuf,smb_vwv1);
-    max_xmit = SVAL(inbuf,smb_vwv2);
-    sesskey = IVAL(inbuf,smb_vwv6);
-    serverzone = SVALS(inbuf,smb_vwv10)*60;
-    /* this time is converted to GMT by make_unix_date */
-    servertime = make_unix_date(inbuf+smb_vwv8);
-    if (Protocol >= PROTOCOL_COREPLUS) {
-      readbraw_supported = ((SVAL(inbuf,smb_vwv5) & 0x1) != 0);
-      writebraw_supported = ((SVAL(inbuf,smb_vwv5) & 0x2) != 0);
-    }
-    crypt_len = smb_buflen(inbuf);
-    memcpy(cryptkey,smb_buf(inbuf),8);
-    DEBUG(3,("max mux %d\n",SVAL(inbuf,smb_vwv3)));
-    max_vcs = SVAL(inbuf,smb_vwv4); 
-    DEBUG(3,("max vcs %d\n",max_vcs)); 
-    DEBUG(3,("max blk %d\n",SVAL(inbuf,smb_vwv5)));
-  } else {
-    /* NT protocol */
-    sec_mode = CVAL(inbuf,smb_vwv1);
-    max_xmit = IVAL(inbuf,smb_vwv3+1);
-    sesskey = IVAL(inbuf,smb_vwv7+1);
-    serverzone = SVALS(inbuf,smb_vwv15+1)*60;
-    /* this time arrives in real GMT */
-    servertime = interpret_long_date(inbuf+smb_vwv11+1);
-    crypt_len = CVAL(inbuf,smb_vwv16+1);
-    memcpy(cryptkey,smb_buf(inbuf),8);
-    if (IVAL(inbuf,smb_vwv9+1) & 1)
-      readbraw_supported = writebraw_supported = True;      
-    DEBUG(3,("max mux %d\n",SVAL(inbuf,smb_vwv1+1)));
-    max_vcs = SVAL(inbuf,smb_vwv2+1); 
-    DEBUG(3,("max vcs %d\n",max_vcs));
-    DEBUG(3,("max raw %d\n",IVAL(inbuf,smb_vwv5+1)));
-    DEBUG(3,("capabilities 0x%x\n",IVAL(inbuf,smb_vwv9+1)));
-  }
-
-  DEBUG(3,("Sec mode %d\n",SVAL(inbuf,smb_vwv1)));
-  DEBUG(3,("max xmt %d\n",max_xmit));
-  DEBUG(3,("Got %d byte crypt key\n",crypt_len));
-  DEBUG(3,("Chose protocol [%s]\n",prots[SVAL(inbuf,smb_vwv0)].name));
-
-  doencrypt = ((sec_mode & 2) != 0);
-
-  if (servertime) {
-    static BOOL done_time = False;
-    if (!done_time) {
-      DEBUG(1,("Server time is %sTimezone is UTC%+02.1f\n",
-              asctime(LocalTime(&servertime)),
-              -(double)(serverzone/3600.0)));
-      done_time = True;
-    }
-  }
-
- get_pass:
-
-  if (got_pass)
-    pass = password;
-  else
-    pass = (char *)getpass("Password: ");
-
-  /* use a blank username for the 2nd try with a blank password */
-  if (tries++ && !*pass)
-    *username = 0;
-
-  if (Protocol >= PROTOCOL_LANMAN1 && use_setup)
-    {
-      fstring pword;
-      int passlen = strlen(pass)+1;
-      strcpy(pword,pass);      
-
-#ifdef SMB_PASSWD
-      if (doencrypt && *pass) {
-       DEBUG(3,("Using encrypted passwords\n"));
-       passlen = 24;
-       SMBencrypt((uchar *)pass,(uchar *)cryptkey,(uchar *)pword);
-      }
-#else
-      doencrypt = False;
-#endif
-
-      /* if in share level security then don't send a password now */
-      if (!(sec_mode & 1)) {strcpy(pword, "");passlen=1;} 
-
-      /* send a session setup command */
-      bzero(outbuf,smb_size);
-
-      if (Protocol < PROTOCOL_NT1) {
-       set_message(outbuf,10,1 + strlen(username) + passlen,True);
-       CVAL(outbuf,smb_com) = SMBsesssetupX;
-       setup_pkt(outbuf);
-
-       CVAL(outbuf,smb_vwv0) = 0xFF;
-       SSVAL(outbuf,smb_vwv2,max_xmit);
-       SSVAL(outbuf,smb_vwv3,2);
-       SSVAL(outbuf,smb_vwv4,max_vcs-1);
-       SIVAL(outbuf,smb_vwv5,sesskey);
-       SSVAL(outbuf,smb_vwv7,passlen);
-       p = smb_buf(outbuf);
-       memcpy(p,pword,passlen);
-       p += passlen;
-       strcpy(p,username);
-      } else {
-       if (!doencrypt) passlen--;
-       /* for Win95 */
-       set_message(outbuf,13,0,True);
-       CVAL(outbuf,smb_com) = SMBsesssetupX;
-       setup_pkt(outbuf);
-
-       CVAL(outbuf,smb_vwv0) = 0xFF;
-       SSVAL(outbuf,smb_vwv2,BUFFER_SIZE);
-       SSVAL(outbuf,smb_vwv3,2);
-       SSVAL(outbuf,smb_vwv4,getpid());
-       SIVAL(outbuf,smb_vwv5,sesskey);
-       SSVAL(outbuf,smb_vwv7,passlen);
-       SSVAL(outbuf,smb_vwv8,0);
-       p = smb_buf(outbuf);
-       memcpy(p,pword,passlen); p += SVAL(outbuf,smb_vwv7);
-       strcpy(p,username);p = skip_string(p,1);
-       strcpy(p,workgroup);p = skip_string(p,1);
-       strcpy(p,"Unix");p = skip_string(p,1);
-       strcpy(p,"Samba");p = skip_string(p,1);
-       set_message(outbuf,13,PTR_DIFF(p,smb_buf(outbuf)),False);
-      }
-
-      send_smb(Client,outbuf);
-      receive_smb(Client,inbuf,CLIENT_TIMEOUT);
-
-      show_msg(inbuf);
-
-      if (CVAL(inbuf,smb_rcls) != 0)
-       {
-         if (! *pass &&
-             ((CVAL(inbuf,smb_rcls) == ERRDOS && 
-               SVAL(inbuf,smb_err) == ERRnoaccess) ||
-              (CVAL(inbuf,smb_rcls) == ERRSRV && 
-               SVAL(inbuf,smb_err) == ERRbadpw)))
-           {
-             got_pass = False;
-             DEBUG(3,("resending login\n"));
-             goto get_pass;
-           }
-             
-         DEBUG(0,("Session setup failed for username=%s myname=%s destname=%s   %s\n",
-               username,myname,desthost,smb_errstr(inbuf)));
-         DEBUG(0,("You might find the -U, -W or -n options useful\n"));
-         DEBUG(0,("Sometimes you have to use `-n USERNAME' (particularly with OS/2)\n"));
-         DEBUG(0,("Some servers also insist on uppercase-only passwords\n"));
-         if (was_null)
-           {
-             free(inbuf);
-             free(outbuf);
-           }
-         return(False);
-       }
-
-      if (Protocol >= PROTOCOL_NT1) {
-       char *domain,*os,*lanman;
-       p = smb_buf(inbuf);
-       os = p;
-       lanman = skip_string(os,1);
-       domain = skip_string(lanman,1);
-       if (*domain || *os || *lanman)
-         DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",domain,os,lanman));
-      }
-
-      /* use the returned uid from now on */
-      if (SVAL(inbuf,smb_uid) != uid)
-       DEBUG(3,("Server gave us a UID of %d. We gave %d\n",
-             SVAL(inbuf,smb_uid),uid));
-      uid = SVAL(inbuf,smb_uid);
-    }
-
-  /* now we've got a connection - send a tcon message */
-  bzero(outbuf,smb_size);
-
-  if (strncmp(service,"\\\\",2) != 0)
-    {
-      DEBUG(0,("\nWarning: Your service name doesn't start with \\\\. This is probably incorrect.\n"));
-      DEBUG(0,("Perhaps try replacing each \\ with \\\\ on the command line?\n\n"));
-    }
-
-
- again2:
-
-  {
-    int passlen = strlen(pass)+1;
-    fstring pword;
-    strcpy(pword,pass);
-
-#ifdef SMB_PASSWD
-    if (doencrypt && *pass) {
-      passlen=24;
-      SMBencrypt((uchar *)pass,(uchar *)cryptkey,(uchar *)pword);      
-    }
-#endif
-
-    /* if in user level security then don't send a password now */
-    if ((sec_mode & 1)) {
-      strcpy(pword, ""); passlen=1; 
-    }
-
-    if (Protocol <= PROTOCOL_COREPLUS) {
-      set_message(outbuf,0,6 + strlen(service) + passlen + strlen(dev),True);
-      CVAL(outbuf,smb_com) = SMBtcon;
-      setup_pkt(outbuf);
-
-      p = smb_buf(outbuf);
-      *p++ = 0x04;
-      strcpy(p, service);
-      p = skip_string(p,1);
-      *p++ = 0x04;
-      memcpy(p,pword,passlen);
-      p += passlen;
-      *p++ = 0x04;
-      strcpy(p, dev);
-    }
-    else {
-      set_message(outbuf,4,2 + strlen(service) + passlen + strlen(dev),True);
-      CVAL(outbuf,smb_com) = SMBtconX;
-      setup_pkt(outbuf);
-  
-      SSVAL(outbuf,smb_vwv0,0xFF);
-      SSVAL(outbuf,smb_vwv3,passlen);
-  
-      p = smb_buf(outbuf);
-      memcpy(p,pword,passlen);
-      p += passlen;
-      strcpy(p,service);
-      p = skip_string(p,1);
-      strcpy(p,dev);
-    }
-  }
-
-  send_smb(Client,outbuf);
-  receive_smb(Client,inbuf,CLIENT_TIMEOUT);
-
-  /* trying again with a blank password */
-  if (CVAL(inbuf,smb_rcls) != 0 && 
-      (int)strlen(pass) > 0 && 
-      !doencrypt &&
-      Protocol >= PROTOCOL_LANMAN1)
-    {
-      DEBUG(2,("first SMBtconX failed, trying again. %s\n",smb_errstr(inbuf)));
-      strcpy(pass,"");
-      goto again2;
-    }  
-
-  if (CVAL(inbuf,smb_rcls) != 0)
-    {
-      DEBUG(0,("SMBtconX failed. %s\n",smb_errstr(inbuf)));
-      DEBUG(0,("Perhaps you are using the wrong sharename, username or password?\n"));
-      DEBUG(0,("Some servers insist that these be in uppercase\n"));
-      if (was_null)
-       {
-         free(inbuf);
-         free(outbuf);
-       }
-      return(False);
-    }
-  
-
-  if (Protocol <= PROTOCOL_COREPLUS) {
-    max_xmit = SVAL(inbuf,smb_vwv0);
-
-    cnum = SVAL(inbuf,smb_vwv1);
-  }
-  else {
-    max_xmit = MIN(max_xmit,BUFFER_SIZE-4);
-    if (max_xmit <= 0)
-      max_xmit = BUFFER_SIZE - 4;
-
-    cnum = SVAL(inbuf,smb_tid);
-  }
-
-  DEBUG(3,("Connected with cnum=%d max_xmit=%d\n",cnum,max_xmit));
-
-  if (was_null)
-    {
-      free(inbuf);
-      free(outbuf);
-    }
-  return True;
-}
-
-
-/****************************************************************************
-send a logout command
-****************************************************************************/
-static void send_logout(void )
-{
-  pstring inbuf,outbuf;
-
-  bzero(outbuf,smb_size);
-  set_message(outbuf,0,0,True);
-  CVAL(outbuf,smb_com) = SMBtdis;
-  SSVAL(outbuf,smb_tid,cnum);
-  setup_pkt(outbuf);
-
-  send_smb(Client,outbuf);
-  receive_smb(Client,inbuf,SHORT_TIMEOUT);
-
-  if (CVAL(inbuf,smb_rcls) != 0)
-    {
-      DEBUG(0,("SMBtdis failed %s\n",smb_errstr(inbuf)));
-    }
-
-  
-#ifdef STATS
-  stats_report();
-#endif
-  exit(0);
-}
-
-
-
-/****************************************************************************
-call a remote api
-****************************************************************************/
-static BOOL call_api(int prcnt,int drcnt,
-                    int mprcnt,int mdrcnt,
-                    int *rprcnt,int *rdrcnt,
-                    char *param,char *data,
-                    char **rparam,char **rdata)
-{
-  static char *inbuf=NULL;
-  static char *outbuf=NULL;
-
-  if (!inbuf) inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
-  if (!outbuf) outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
-
-  send_trans_request(outbuf,SMBtrans,"\\PIPE\\LANMAN",0,0,
-                    data,param,NULL,
-                    drcnt,prcnt,0,
-                    mdrcnt,mprcnt,0);
-
-  return (receive_trans_response(inbuf,SMBtrans,
-                                 rdrcnt,rprcnt,
-                                 rdata,rparam));
-}
-
-/****************************************************************************
-  send a SMB trans or trans2 request
-  ****************************************************************************/
-static BOOL send_trans_request(char *outbuf,int trans,
-                              char *name,int fid,int flags,
-                              char *data,char *param,uint16 *setup,
-                              int ldata,int lparam,int lsetup,
-                              int mdata,int mparam,int msetup)
-{
-  int i;
-  int this_ldata,this_lparam;
-  int tot_data=0,tot_param=0;
-  char *outdata,*outparam;
-  pstring inbuf;
-  char *p;
-
-  this_lparam = MIN(lparam,max_xmit - (500+lsetup*SIZEOFWORD)); /* hack */
-  this_ldata = MIN(ldata,max_xmit - (500+lsetup*SIZEOFWORD+this_lparam));
-
-  bzero(outbuf,smb_size);
-  set_message(outbuf,14+lsetup,0,True);
-  CVAL(outbuf,smb_com) = trans;
-  SSVAL(outbuf,smb_tid,cnum);
-  setup_pkt(outbuf);
-
-  outparam = smb_buf(outbuf)+(trans==SMBtrans ? strlen(name)+1 : 3);
-  outdata = outparam+this_lparam;
-
-  /* primary request */
-  SSVAL(outbuf,smb_tpscnt,lparam);     /* tpscnt */
-  SSVAL(outbuf,smb_tdscnt,ldata);      /* tdscnt */
-  SSVAL(outbuf,smb_mprcnt,mparam);     /* mprcnt */
-  SSVAL(outbuf,smb_mdrcnt,mdata);      /* mdrcnt */
-  SCVAL(outbuf,smb_msrcnt,msetup);     /* msrcnt */
-  SSVAL(outbuf,smb_flags,flags);       /* flags */
-  SIVAL(outbuf,smb_timeout,0);         /* timeout */
-  SSVAL(outbuf,smb_pscnt,this_lparam); /* pscnt */
-  SSVAL(outbuf,smb_psoff,smb_offset(outparam,outbuf)); /* psoff */
-  SSVAL(outbuf,smb_dscnt,this_ldata);  /* dscnt */
-  SSVAL(outbuf,smb_dsoff,smb_offset(outdata,outbuf)); /* dsoff */
-  SCVAL(outbuf,smb_suwcnt,lsetup);     /* suwcnt */
-  for (i=0;i<lsetup;i++)               /* setup[] */
-    SSVAL(outbuf,smb_setup+i*SIZEOFWORD,setup[i]);
-  p = smb_buf(outbuf);
-  if (trans==SMBtrans)
-    strcpy(p,name);                    /* name[] */
-  else
-    {
-      *p++ = 0;                                /* put in a null smb_name */
-      *p++ = 'D'; *p++ = ' ';          /* this was added because OS/2 does it */
-    }
-  if (this_lparam)                     /* param[] */
-    memcpy(outparam,param,this_lparam);
-  if (this_ldata)                      /* data[] */
-    memcpy(outdata,data,this_ldata);
-  set_message(outbuf,14+lsetup,                /* wcnt, bcc */
-             PTR_DIFF(outdata+this_ldata,smb_buf(outbuf)),False);
-
-  show_msg(outbuf);
-  send_smb(Client,outbuf);
-
-  if (this_ldata < ldata || this_lparam < lparam)
-    {
-      /* receive interim response */
-      if (!receive_smb(Client,inbuf,SHORT_TIMEOUT) || CVAL(inbuf,smb_rcls) != 0)
-       {
-         DEBUG(0,("%s request failed (%s)\n",
-                  trans==SMBtrans?"SMBtrans":"SMBtrans2", smb_errstr(inbuf)));
-         return(False);
-       }      
-
-      tot_data = this_ldata;
-      tot_param = this_lparam;
-
-      while (tot_data < ldata || tot_param < lparam)
-    {
-         this_lparam = MIN(lparam-tot_param,max_xmit - 500); /* hack */
-         this_ldata = MIN(ldata-tot_data,max_xmit - (500+this_lparam));
-
-         set_message(outbuf,trans==SMBtrans?8:9,0,True);
-         CVAL(outbuf,smb_com) = trans==SMBtrans ? SMBtranss : SMBtranss2;
-
-         outparam = smb_buf(outbuf);
-         outdata = outparam+this_lparam;
-
-         /* secondary request */
-         SSVAL(outbuf,smb_tpscnt,lparam);      /* tpscnt */
-         SSVAL(outbuf,smb_tdscnt,ldata);       /* tdscnt */
-         SSVAL(outbuf,smb_spscnt,this_lparam); /* pscnt */
-         SSVAL(outbuf,smb_spsoff,smb_offset(outparam,outbuf)); /* psoff */
-         SSVAL(outbuf,smb_spsdisp,tot_param);  /* psdisp */
-         SSVAL(outbuf,smb_sdscnt,this_ldata);  /* dscnt */
-         SSVAL(outbuf,smb_sdsoff,smb_offset(outdata,outbuf)); /* dsoff */
-         SSVAL(outbuf,smb_sdsdisp,tot_data);   /* dsdisp */
-         if (trans==SMBtrans2)
-           SSVAL(outbuf,smb_sfid,fid);         /* fid */
-         if (this_lparam)                      /* param[] */
-           memcpy(outparam,param,this_lparam);
-         if (this_ldata)                       /* data[] */
-           memcpy(outdata,data,this_ldata);
-         set_message(outbuf,trans==SMBtrans?8:9, /* wcnt, bcc */
-                     PTR_DIFF(outdata+this_ldata,smb_buf(outbuf)),False);
-
-         show_msg(outbuf);
-         send_smb(Client,outbuf);
-
-         tot_data += this_ldata;
-         tot_param += this_lparam;
-       }
-    }
-
-    return(True);
-}
-
 /****************************************************************************
 try and browse available connections on a host
 ****************************************************************************/
@@ -3727,10 +2929,10 @@ static BOOL browse_host(BOOL sort)
   SSVAL(p,2,BUFFER_SIZE);
   p += 4;
 
-  if (call_api(PTR_DIFF(p,param),0,
-              1024,BUFFER_SIZE,
+  if (cli_call_api(PIPE_LANMAN, 0,PTR_DIFF(p,param),0, 0,
+             1024, BUFFER_SIZE,
                &rprcnt,&rdrcnt,
-              param,NULL,
+              param,NULL, NULL,
               &rparam,&rdata))
     {
       int res = SVAL(rparam,0);
@@ -3819,10 +3021,10 @@ static void server_info()
   SSVAL(p,2,1000);
   p += 6;
 
-  if (call_api(PTR_DIFF(p,param),0,
-              6,1000,
+  if (cli_call_api(PIPE_LANMAN, 0,PTR_DIFF(p,param),0, 0,
+           6, 1000,
               &rprcnt,&rdrcnt,
-              param,NULL,
+              param,NULL, NULL,
               &rparam,&rdata))
     {
       int res = SVAL(rparam,0);
@@ -3894,10 +3096,10 @@ static BOOL list_servers(char *wk_grp)
   /* first ask for a list of servers in this workgroup */
   SIVAL(svtype_p,0,SV_TYPE_ALL);
 
-  if (call_api(PTR_DIFF(p+4,param),0,
-              8,BUFFER_SIZE - SAFETY_MARGIN,
+  if (cli_call_api(PIPE_LANMAN, 0,PTR_DIFF(p+4,param),0, 0,
+           8, BUFFER_SIZE - SAFETY_MARGIN,
               &rprcnt,&rdrcnt,
-              param,NULL,
+              param,NULL, NULL,
               &rparam,&rdata))
     {
       int res = SVAL(rparam,0);
@@ -3933,10 +3135,10 @@ static BOOL list_servers(char *wk_grp)
   /* now ask for a list of workgroups */
   SIVAL(svtype_p,0,SV_TYPE_DOMAIN_ENUM);
 
-  if (call_api(PTR_DIFF(p+4,param),0,
-              8,BUFFER_SIZE - SAFETY_MARGIN,
+  if (cli_call_api(PIPE_LANMAN, 0,PTR_DIFF(p+4,param),0, 0,
+           8, BUFFER_SIZE - SAFETY_MARGIN,
               &rprcnt,&rdrcnt,
-              param,NULL,
+              param,NULL, NULL,
               &rparam,&rdata))
     {
       int res = SVAL(rparam,0);
@@ -4009,10 +3211,9 @@ struct
   {"queue",cmd_queue,"show the print queue"},
   {"qinfo",cmd_qinfo,"show print queue information"},
   {"cancel",cmd_cancel,"<jobid> cancel a print queue entry"},
-  {"stat",cmd_stat,"<file> get info on a file (experimental!)"},
-  {"quit",send_logout,"logoff the server"},
-  {"q",send_logout,"logoff the server"},
-  {"exit",send_logout,"logoff the server"},
+  {"quit",cli_send_logout,"logoff the server"},
+  {"q",cli_send_logout,"logoff the server"},
+  {"exit",cli_send_logout,"logoff the server"},
   {"newer",cmd_newer,"<file> only mget files newer than the specified local file"},
   {"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"},
   {"tar",cmd_tar,"tar <c|x>[IXbgNa] current directory to/from <file name>" },
@@ -4045,7 +3246,7 @@ static int process_tok(fstring tok)
          cmd = i;
          break;
        }
-      else if (strnequal(commands[i].name, tok, tok_len+1))
+      else if (strnequal(commands[i].name, tok, tok_len))
        {
          matches++;
          cmd = i;
@@ -4085,87 +3286,6 @@ void cmd_help(void)
       }
 }
 
-/****************************************************************************
-open the client sockets
-****************************************************************************/
-static BOOL open_sockets(int port )
-{
-  static int last_port;
-  char *host;
-  pstring service2;
-  extern int Client;
-#ifdef USENMB
-  BOOL failed = True;
-#endif
-
-  if (port == 0) port=last_port;
-  last_port=port;
-
-  strupper(service);
-
-  if (*desthost)
-    {
-      host = desthost;
-    }
-  else
-    {
-      strcpy(service2,service);
-      host = strtok(service2,"\\/");
-      if (!host) {
-       DEBUG(0,("Badly formed host name\n"));
-       return(False);
-      }
-      strcpy(desthost,host);
-    }
-
-  if (*myname == 0) {
-      get_myname(myname,NULL);
-  }
-  strupper(myname);
-
-  DEBUG(3,("Opening sockets\n"));
-
-  if (!have_ip)
-    {
-      struct hostent *hp;
-
-      if ((hp = Get_Hostbyname(host))) {
-       putip((char *)&dest_ip,(char *)hp->h_addr);
-       failed = False;
-      } else {
-#ifdef USENMB
-       /* Try and resolve the name with the netbios server */
-       int             bcast;
-
-       if ((bcast = open_socket_in(SOCK_DGRAM, 0, 3,
-                                   interpret_addr(lp_socket_address()))) != -1) {
-         set_socket_options(bcast, "SO_BROADCAST");
-
-         if (name_query(bcast, host, name_type, True, True, *iface_bcast(dest_ip),
-                        &dest_ip,0)) {
-           failed = False;
-         }
-         close (bcast);
-       }
-#endif
-       if (failed) {
-         DEBUG(0,("Get_Hostbyname: Unknown host %s.\n",host));
-         return False;
-       }
-      }
-    }
-
-  Client = open_socket_out(SOCK_STREAM, &dest_ip, port, LONG_CONNECT_TIMEOUT);
-  if (Client == -1)
-    return False;
-
-  DEBUG(3,("Connected\n"));
-  
-  set_socket_options(Client,user_socket_options);  
-  
-  return True;
-}
-
 /****************************************************************************
 wait for keyboard activity, swallowing network packets
 ****************************************************************************/
@@ -4224,6 +3344,11 @@ static void wait_keyboard(char *buffer)
          }
       }
 #endif
+
+      /* We deliberately use receive_smb instead of
+         client_receive_smb as we want to receive
+         session keepalives and then drop them here.
+       */
       if (FD_ISSET(Client,&fds))
        receive_smb(Client,buffer,0);
       
@@ -4241,33 +3366,6 @@ static void wait_keyboard(char *buffer)
 }
 
 
-/****************************************************************************
-close and open the connection again
-****************************************************************************/
-BOOL reopen_connection(char *inbuf,char *outbuf)
-{
-  static int open_count=0;
-
-  open_count++;
-
-  if (open_count>5) return(False);
-
-  DEBUG(1,("Trying to re-open connection\n"));
-
-  set_message(outbuf,0,0,True);
-  SCVAL(outbuf,smb_com,SMBtdis);
-  SSVAL(outbuf,smb_tid,cnum);
-  setup_pkt(outbuf);
-
-  send_smb(Client,outbuf);
-  receive_smb(Client,inbuf,SHORT_TIMEOUT);
-
-  close_sockets();
-  if (!open_sockets(0)) return(False);
-
-  return(send_login(inbuf,outbuf,True,True));
-}
-
 /****************************************************************************
   process commands from the client
 ****************************************************************************/
@@ -4285,7 +3383,7 @@ static BOOL process(char *base_directory)
   
   bzero(OutBuffer,smb_size);
 
-  if (!send_login(InBuffer,OutBuffer,True,True))
+  if (!cli_send_login(InBuffer,OutBuffer,True,True,NULL))
     return(False);
 
   if (*base_directory) do_cd(base_directory);
@@ -4379,11 +3477,10 @@ static BOOL process(char *base_directory)
        DEBUG(0,("%s: command not found\n",CNV_LANG(tok)));
     }
   
-  send_logout();
+  cli_send_logout();
   return(True);
 }
 
-
 /****************************************************************************
 usage on the program
 ****************************************************************************/
@@ -4392,12 +3489,8 @@ static void usage(char *pname)
   DEBUG(0,("Usage: %s service <password> [-p port] [-d debuglevel] [-l log] ",
           pname));
 
-#ifdef KANJI
-  DEBUG(0,("[-t termcode] "));
-#endif /* KANJI */
-
   DEBUG(0,("\nVersion %s\n",VERSION));
-  DEBUG(0,("\t-p port               listen on the specified port\n"));
+  DEBUG(0,("\t-p port               connect to the specified port\n"));
   DEBUG(0,("\t-d debuglevel         set the debuglevel\n"));
   DEBUG(0,("\t-l log basename.      Basename for log/debug files\n"));
   DEBUG(0,("\t-n netbios name.      Use this name as my netbios name\n"));
@@ -4407,13 +3500,12 @@ static void usage(char *pname)
   DEBUG(0,("\t-m max protocol       set the max protocol level\n"));
   DEBUG(0,("\t-L host               get a list of shares available on a host\n"));
   DEBUG(0,("\t-I dest IP            use this IP to connect to\n"));
+  DEBUG(0,("\t-R name resolve order use these name resolution services only\n"));
   DEBUG(0,("\t-E                    write messages to stderr instead of stdout\n"));
   DEBUG(0,("\t-U username           set the network username\n"));
   DEBUG(0,("\t-W workgroup          set the workgroup name\n"));
   DEBUG(0,("\t-c command string     execute semicolon separated commands\n"));
-#ifdef KANJI
   DEBUG(0,("\t-t terminal code      terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n"));
-#endif /* KANJI */
   DEBUG(0,("\t-T<c|x>IXgbNa          command line tar\n"));
   DEBUG(0,("\t-D directory          start from directory\n"));
   DEBUG(0,("\n"));
@@ -4433,12 +3525,24 @@ static void usage(char *pname)
   extern int optind;
   pstring query_host;
   BOOL message = False;
+  BOOL nt_domain_logon = False;
   extern char tar_type;
   static pstring servicesf = CONFIGFILE;
+  pstring term_code;
+  pstring new_name_resolve_order;
+  char *p;
+
+#ifdef KANJI
+  strcpy(term_code, KANJI);
+#else /* KANJI */
+  *term_code = 0;
+#endif /* KANJI */
 
   *query_host = 0;
   *base_directory = 0;
 
+  *new_name_resolve_order = 0;
+
   DEBUGLEVEL = 2;
 
   setup_logging(pname,True);
@@ -4454,10 +3558,27 @@ static void usage(char *pname)
   umask(myumask);
 
   if (getenv("USER"))
+  {
+    strcpy(username,getenv("USER"));
+
+    /* modification to support userid%passwd syntax in the USER var
+       25.Aug.97, jdblair@uab.edu */
+
+    if ((p=strchr(username,'%')))
     {
-      strcpy(username,getenv("USER"));
-      strupper(username);
+      *p = 0;
+      strcpy(password,p+1);
+      got_pass = True;
+      memset(strchr(getenv("USER"),'%')+1,'X',strlen(password));
     }
+    strupper(username);
+  }
+
+ /* modification to support PASSWD environmental var
+  25.Aug.97, jdblair@uab.edu */
+
+  if (getenv("PASSWD"))
+    strcpy(password,getenv("PASSWD"));
 
   if (*username == 0 && getenv("LOGNAME"))
     {
@@ -4506,11 +3627,8 @@ static void usage(char *pname)
        }
     }
 
-#ifdef KANJI
-  setup_term_code (KANJI);
-#endif
   while ((opt = 
-         getopt(argc, argv,"s:B:O:M:i:Nn:d:Pp:l:hI:EB:U:L:t:m:W:T:D:c:")) != EOF)
+         getopt(argc, argv,"s:B:O:R:M:S:i:Nn:d:Pp:l:hI:EB:U:L:t:m:W:T:D:c:")) != EOF)
     switch (opt)
       {
       case 'm':
@@ -4519,6 +3637,14 @@ static void usage(char *pname)
       case 'O':
        strcpy(user_socket_options,optarg);
        break;  
+      case 'R':
+        pstrcpy(new_name_resolve_order, optarg);
+        break;
+      case 'S':
+       strcpy(desthost,optarg);
+       strupper(desthost);
+       nt_domain_logon = True;
+       break;
       case 'M':
        name_type = 0x03; /* messages are sent to NetBIOS name type 0x3 */
        strcpy(desthost,optarg);
@@ -4546,12 +3672,12 @@ static void usage(char *pname)
        break;
       case 'U':
        {
-         char *p;
+         char *lp;
        strcpy(username,optarg);
-       if ((p=strchr(username,'%')))
+       if ((lp=strchr(username,'%')))
          {
-           *p = 0;
-           strcpy(password,p+1);
+           *lp = 0;
+           strcpy(password,lp+1);
            got_pass = True;
            memset(strchr(optarg,'%')+1,'X',strlen(password));
          }
@@ -4576,6 +3702,7 @@ static void usage(char *pname)
        break;
       case 'N':
        got_pass = True;
+       no_pass = True;
        break;
       case 'P':
        connect_as_printer = True;
@@ -4604,13 +3731,7 @@ static void usage(char *pname)
        strcpy(servicesf, optarg);
        break;
       case 't':
-#ifdef KANJI
-       if (!setup_term_code (optarg)) {
-           DEBUG(0, ("%s: unknown terminal code name\n", optarg));
-           usage (pname);
-           exit (1);
-       }
-#endif
+        strcpy(term_code, optarg);
        break;
       default:
        usage(pname);
@@ -4631,21 +3752,28 @@ static void usage(char *pname)
     DEBUG(0,("Failed to get my hostname.\n"));
   }
 
-  if (!lp_load(servicesf,True)) {
+  if (!lp_load(servicesf,True,False,False)) {
     fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
   }
 
+  codepage_initialise(lp_client_code_page());
+
+  interpret_coding_system(term_code);
+
   if (*workgroup == 0)
     strcpy(workgroup,lp_workgroup());
 
   load_interfaces();
-  get_myname(*myname?NULL:myname,NULL);  
+  get_myname((*myname)?NULL:myname,NULL);  
   strupper(myname);
 
+  if(*new_name_resolve_order)
+    lp_set_name_resolve_order(new_name_resolve_order);
+
   if (tar_type) {
     recurse=True;
 
-    if (open_sockets(port)) {
+    if (cli_open_sockets(port)) {
         char *InBuffer = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
        char *OutBuffer = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
        int ret;
@@ -4654,32 +3782,32 @@ static void usage(char *pname)
          return(1);
 
        bzero(OutBuffer,smb_size);
-       if (!send_login(InBuffer,OutBuffer,True,True))
+       if (!cli_send_login(InBuffer,OutBuffer,True,True,NULL))
          return(False);
 
        if (*base_directory) do_cd(base_directory);
 
        ret=process_tar(InBuffer, OutBuffer);
 
-       send_logout();
+       cli_send_logout();
        close_sockets();
        return(ret);
     } else
       return(1);
   }
   
-  if (*query_host)
+  if (*query_host && !nt_domain_logon)
     {
       int ret = 0;
       sprintf(service,"\\\\%s\\IPC$",query_host);
       strupper(service);
       connect_as_ipc = True;
-      if (open_sockets(port))
+      if (cli_open_sockets(port))
        {
 #if 0
          *username = 0;
 #endif
-         if (!send_login(NULL,NULL,True,True))
+         if (!cli_send_login(NULL,NULL,True,True,NULL))
            return(1);
 
          server_info();
@@ -4692,7 +3820,7 @@ static void usage(char *pname)
            list_servers(workgroup);
          }
 
-         send_logout();
+         cli_send_logout();
          close_sockets();
        }
 
@@ -4702,11 +3830,11 @@ static void usage(char *pname)
   if (message)
     {
       int ret = 0;
-      if (open_sockets(port))
+      if (cli_open_sockets(port))
        {
          pstring inbuf,outbuf;
          bzero(outbuf,smb_size);
-         if (!send_session_request(inbuf,outbuf))
+         if (!cli_send_session_request(inbuf,outbuf))
            return(1);
 
          send_message(inbuf,outbuf);
@@ -4717,7 +3845,7 @@ static void usage(char *pname)
       return(ret);
     }
 
-  if (open_sockets(port))
+  if (cli_open_sockets(port))
     {
       if (!process(base_directory))
        {
@@ -4733,156 +3861,3 @@ static void usage(char *pname)
 }
 
 
-/* error code stuff - put together by Merik Karman
-   merik@blackadder.dsh.oz.au */
-
-typedef struct
-{
-  char *name;
-  int code;
-  char *message;
-} err_code_struct;
-
-/* Dos Error Messages */
-err_code_struct dos_msgs[] = {
-  {"ERRbadfunc",1,"Invalid function."},
-  {"ERRbadfile",2,"File not found."},
-  {"ERRbadpath",3,"Directory invalid."},
-  {"ERRnofids",4,"No file descriptors available"},
-  {"ERRnoaccess",5,"Access denied."},
-  {"ERRbadfid",6,"Invalid file handle."},
-  {"ERRbadmcb",7,"Memory control blocks destroyed."},
-  {"ERRnomem",8,"Insufficient server memory to perform the requested function."},
-  {"ERRbadmem",9,"Invalid memory block address."},
-  {"ERRbadenv",10,"Invalid environment."},
-  {"ERRbadformat",11,"Invalid format."},
-  {"ERRbadaccess",12,"Invalid open mode."},
-  {"ERRbaddata",13,"Invalid data."},
-  {"ERR",14,"reserved."},
-  {"ERRbaddrive",15,"Invalid drive specified."},
-  {"ERRremcd",16,"A Delete Directory request attempted  to  remove  the  server's  current directory."},
-  {"ERRdiffdevice",17,"Not same device."},
-  {"ERRnofiles",18,"A File Search command can find no more files matching the specified criteria."},
-  {"ERRbadshare",32,"The sharing mode specified for an Open conflicts with existing  FIDs  on the file."},
-  {"ERRlock",33,"A Lock request conflicted with an existing lock or specified an  invalid mode,  or an Unlock requested attempted to remove a lock held by another process."},
-  {"ERRfilexists",80,"The file named in a Create Directory, Make  New  File  or  Link  request already exists."},
-  {"ERRbadpipe",230,"Pipe invalid."},
-  {"ERRpipebusy",231,"All instances of the requested pipe are busy."},
-  {"ERRpipeclosing",232,"Pipe close in progress."},
-  {"ERRnotconnected",233,"No process on other end of pipe."},
-  {"ERRmoredata",234,"There is more data to be returned."},
-  {"ERRinvgroup",2455,"Invalid workgroup (try the -W option)"},
-  {NULL,-1,NULL}};
-
-/* Server Error Messages */
-err_code_struct server_msgs[] = {
-  {"ERRerror",1,"Non-specific error code."},
-  {"ERRbadpw",2,"Bad password - name/password pair in a Tree Connect or Session Setup are invalid."},
-  {"ERRbadtype",3,"reserved."},
-  {"ERRaccess",4,"The requester does not have  the  necessary  access  rights  within  the specified  context for the requested function. The context is defined by the TID or the UID."},
-  {"ERRinvnid",5,"The tree ID (TID) specified in a command was invalid."},
-  {"ERRinvnetname",6,"Invalid network name in tree connect."},
-  {"ERRinvdevice",7,"Invalid device - printer request made to non-printer connection or  non-printer request made to printer connection."},
-  {"ERRqfull",49,"Print queue full (files) -- returned by open print file."},
-  {"ERRqtoobig",50,"Print queue full -- no space."},
-  {"ERRqeof",51,"EOF on print queue dump."},
-  {"ERRinvpfid",52,"Invalid print file FID."},
-  {"ERRsmbcmd",64,"The server did not recognize the command received."},
-  {"ERRsrverror",65,"The server encountered an internal error, e.g., system file unavailable."},
-  {"ERRfilespecs",67,"The file handle (FID) and pathname parameters contained an invalid  combination of values."},
-  {"ERRreserved",68,"reserved."},
-  {"ERRbadpermits",69,"The access permissions specified for a file or directory are not a valid combination.  The server cannot set the requested attribute."},
-  {"ERRreserved",70,"reserved."},
-  {"ERRsetattrmode",71,"The attribute mode in the Set File Attribute request is invalid."},
-  {"ERRpaused",81,"Server is paused."},
-  {"ERRmsgoff",82,"Not receiving messages."},
-  {"ERRnoroom",83,"No room to buffer message."},
-  {"ERRrmuns",87,"Too many remote user names."},
-  {"ERRtimeout",88,"Operation timed out."},
-  {"ERRnoresource",89,"No resources currently available for request."},
-  {"ERRtoomanyuids",90,"Too many UIDs active on this session."},
-  {"ERRbaduid",91,"The UID is not known as a valid ID on this session."},
-  {"ERRusempx",250,"Temp unable to support Raw, use MPX mode."},
-  {"ERRusestd",251,"Temp unable to support Raw, use standard read/write."},
-  {"ERRcontmpx",252,"Continue in MPX mode."},
-  {"ERRreserved",253,"reserved."},
-  {"ERRreserved",254,"reserved."},
-  {"ERRnosupport",0xFFFF,"Function not supported."},
-  {NULL,-1,NULL}};
-
-/* Hard Error Messages */
-err_code_struct hard_msgs[] = {
-  {"ERRnowrite",19,"Attempt to write on write-protected diskette."},
-  {"ERRbadunit",20,"Unknown unit."},
-  {"ERRnotready",21,"Drive not ready."},
-  {"ERRbadcmd",22,"Unknown command."},
-  {"ERRdata",23,"Data error (CRC)."},
-  {"ERRbadreq",24,"Bad request structure length."},
-  {"ERRseek",25 ,"Seek error."},
-  {"ERRbadmedia",26,"Unknown media type."},
-  {"ERRbadsector",27,"Sector not found."},
-  {"ERRnopaper",28,"Printer out of paper."},
-  {"ERRwrite",29,"Write fault."},
-  {"ERRread",30,"Read fault."},
-  {"ERRgeneral",31,"General failure."},
-  {"ERRbadshare",32,"An open conflicts with an existing open."},
-  {"ERRlock",33,"A Lock request conflicted with an existing lock or specified an invalid mode, or an Unlock requested attempted to remove a lock held by another process."},
-  {"ERRwrongdisk",34,"The wrong disk was found in a drive."},
-  {"ERRFCBUnavail",35,"No FCBs are available to process request."},
-  {"ERRsharebufexc",36,"A sharing buffer has been exceeded."},
-  {NULL,-1,NULL}};
-
-
-struct
-{
-  int code;
-  char *class;
-  err_code_struct *err_msgs;
-} err_classes[] = { 
-  {0,"SUCCESS",NULL},
-  {0x01,"ERRDOS",dos_msgs},
-  {0x02,"ERRSRV",server_msgs},
-  {0x03,"ERRHRD",hard_msgs},
-  {0x04,"ERRXOS",NULL},
-  {0xE1,"ERRRMX1",NULL},
-  {0xE2,"ERRRMX2",NULL},
-  {0xE3,"ERRRMX3",NULL},
-  {0xFF,"ERRCMD",NULL},
-  {-1,NULL,NULL}};
-
-
-/****************************************************************************
-return a SMB error string from a SMB buffer
-****************************************************************************/
-char *smb_errstr(char *inbuf)
-{
-  static pstring ret;
-  int class = CVAL(inbuf,smb_rcls);
-  int num = SVAL(inbuf,smb_err);
-  int i,j;
-
-  for (i=0;err_classes[i].class;i++)
-    if (err_classes[i].code == class)
-      {
-       if (err_classes[i].err_msgs)
-         {
-           err_code_struct *err = err_classes[i].err_msgs;
-           for (j=0;err[j].name;j++)
-             if (num == err[j].code)
-               {
-                 if (DEBUGLEVEL > 0)
-                   sprintf(ret,"%s - %s (%s)",err_classes[i].class,
-                           err[j].name,err[j].message);
-                 else
-                   sprintf(ret,"%s - %s",err_classes[i].class,err[j].name);
-                 return ret;
-               }
-         }
-
-       sprintf(ret,"%s - %d",err_classes[i].class,num);
-       return ret;
-      }
-  
-  sprintf(ret,"Error: Unknown error (%d,%d)",class,num);
-  return(ret);
-}