first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[samba.git] / source3 / libsmb / clientgen.c
index d454cbdd3cf16db97f15c58e8e1bb8d31c51b849..4f620bc5f4197e495865b0275f597349afc45b96 100644 (file)
 
 
 extern int DEBUGLEVEL;
+extern pstring user_socket_options;
+extern pstring scope;
+
+static void cli_process_oplock(struct cli_state *cli);
+
+/*
+ * Change the port number used to call on 
+ */
+int cli_set_port(struct cli_state *cli, int port)
+{
+       if (port > 0)
+         cli->port = port;
+
+       return cli->port;
+}
+
+/****************************************************************************
+recv an smb
+****************************************************************************/
+static BOOL cli_receive_smb(struct cli_state *cli)
+{
+       BOOL ret;
+ again:
+       ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
+       
+       if (ret) {
+               /* it might be an oplock break request */
+               if (CVAL(cli->inbuf,smb_com) == SMBlockingX &&
+                   SVAL(cli->inbuf,smb_vwv6) == 0 &&
+                   SVAL(cli->inbuf,smb_vwv7) == 0) {
+                       if (cli->use_oplocks) cli_process_oplock(cli);
+                       /* try to prevent loops */
+                       CVAL(cli->inbuf,smb_com) = 0xFF;
+                       goto again;
+               }
+       }
+
+       return ret;
+}
+
+/****************************************************************************
+  send an smb to a fd and re-establish if necessary
+****************************************************************************/
+static BOOL cli_send_smb(struct cli_state *cli)
+{
+       size_t len;
+       size_t nwritten=0;
+       ssize_t ret;
+       BOOL reestablished=False;
+
+       len = smb_len(cli->outbuf) + 4;
+
+       while (nwritten < len) {
+               ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
+               if (ret <= 0 && errno == EPIPE && !reestablished) {
+                       if (cli_reestablish_connection(cli)) {
+                               reestablished = True;
+                               nwritten=0;
+                               continue;
+                       }
+               }
+               if (ret <= 0) {
+                       DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",
+                                (int)len,(int)ret));
+                       close_sockets();
+                       exit(1);
+               }
+               nwritten += ret;
+       }
+       
+       return True;
+}
+
+/****************************************************************************
+setup basics in a outgoing packet
+****************************************************************************/
+static void cli_setup_packet(struct cli_state *cli)
+{
+        cli->rap_error = 0;
+        cli->nt_error = 0;
+       SSVAL(cli->outbuf,smb_pid,cli->pid);
+       SSVAL(cli->outbuf,smb_uid,cli->vuid);
+       SSVAL(cli->outbuf,smb_mid,cli->mid);
+       if (cli->protocol > PROTOCOL_CORE) {
+               SCVAL(cli->outbuf,smb_flg,0x8);
+               SSVAL(cli->outbuf,smb_flg2,0x1);
+       }
+}
+
+
+
+/****************************************************************************
+process an oplock break request from the server
+****************************************************************************/
+static void cli_process_oplock(struct cli_state *cli)
+{
+       char *oldbuf = cli->outbuf;
+       pstring buf;
+       int fnum;
+
+       fnum = SVAL(cli->inbuf,smb_vwv2);
+
+       /* damn, we really need to keep a record of open files so we
+          can detect a oplock break and a close crossing on the
+          wire. for now this swallows the errors */
+       if (fnum == 0) return;
+
+       cli->outbuf = buf;
+
+        memset(buf,'\0',smb_size);
+        set_message(buf,8,0,True);
+
+        CVAL(buf,smb_com) = SMBlockingX;
+       SSVAL(buf,smb_tid, cli->cnum);
+        cli_setup_packet(cli);
+       SSVAL(buf,smb_vwv0,0xFF);
+       SSVAL(buf,smb_vwv1,0);
+       SSVAL(buf,smb_vwv2,fnum);
+       SSVAL(buf,smb_vwv3,2); /* oplock break ack */
+       SIVAL(buf,smb_vwv4,0); /* timoeut */
+       SSVAL(buf,smb_vwv6,0); /* unlockcount */
+       SSVAL(buf,smb_vwv7,0); /* lockcount */
+
+        cli_send_smb(cli);     
+
+       cli->outbuf = oldbuf;
+}
+
 
 /*****************************************************
  RAP error codes - a small start but will be extended.
@@ -45,6 +173,13 @@ struct
   {2244, "This password cannot be used now (password history conflict)." },
   {2245, "The password is shorter than required." },
   {2246, "The password of this user is too recent to change."},
+
+  /* these really shouldn't be here ... */
+  {0x80, "Not listening on called name"},
+  {0x81, "Not listening for calling name"},
+  {0x82, "Called name not present"},
+  {0x83, "Called name present, but insufficient resources"},
+
   {0, NULL}
 };  
 
@@ -66,6 +201,7 @@ char *cli_errstr(struct cli_state *cli)
        static fstring error_message;
        uint8 errclass;
        uint32 errnum;
+       uint32 nt_rpc_error;
        int i;      
 
        /*  
@@ -75,7 +211,7 @@ char *cli_errstr(struct cli_state *cli)
         * errors, whose error code is in cli.rap_error.
         */ 
 
-       cli_error(cli, &errclass, &errnum);
+       cli_error(cli, &errclass, &errnum, &nt_rpc_error);
 
        if (errclass != 0)
        {
@@ -86,13 +222,13 @@ char *cli_errstr(struct cli_state *cli)
         * Was it an NT error ?
         */
 
-       if (cli->nt_error)
+       if (nt_rpc_error)
        {
-               char *nt_msg = get_nt_error_msg(cli->nt_error);
+               char *nt_msg = get_nt_error_msg(nt_rpc_error);
 
                if (nt_msg == NULL)
                {
-                       slprintf(error_message, sizeof(fstring) - 1, "NT code %d", cli->nt_error);
+                       slprintf(error_message, sizeof(fstring) - 1, "NT code %d", nt_rpc_error);
                }
                else
                {
@@ -120,23 +256,6 @@ char *cli_errstr(struct cli_state *cli)
        return error_message;
 }
 
-/****************************************************************************
-setup basics in a outgoing packet
-****************************************************************************/
-static void cli_setup_packet(struct cli_state *cli)
-{
-        cli->rap_error = 0;
-        cli->nt_error = 0;
-       SSVAL(cli->outbuf,smb_pid,cli->pid);
-       SSVAL(cli->outbuf,smb_uid,cli->vuid);
-       SSVAL(cli->outbuf,smb_mid,cli->mid);
-       if (cli->protocol > PROTOCOL_CORE) {
-               SCVAL(cli->outbuf,smb_flg,0x8);
-               SSVAL(cli->outbuf,smb_flg2,0x1);
-       }
-}
-
-
 /*****************************************************************************
  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 
@@ -179,7 +298,7 @@ static BOOL cli_send_trans(struct cli_state *cli, int trans,
        this_lparam = MIN(lparam,cli->max_xmit - (500+lsetup*2)); /* hack */
        this_ldata = MIN(ldata,cli->max_xmit - (500+lsetup*2+this_lparam));
 
-       bzero(cli->outbuf,smb_size);
+       memset(cli->outbuf,'\0',smb_size);
        set_message(cli->outbuf,14+lsetup,0,True);
        CVAL(cli->outbuf,smb_com) = trans;
        SSVAL(cli->outbuf,smb_tid, cli->cnum);
@@ -218,11 +337,11 @@ static BOOL cli_send_trans(struct cli_state *cli, int trans,
                    PTR_DIFF(outdata+this_ldata,smb_buf(cli->outbuf)),False);
 
        show_msg(cli->outbuf);
-       send_smb(cli->fd,cli->outbuf);
+       cli_send_smb(cli);
 
        if (this_ldata < ldata || this_lparam < lparam) {
                /* receive interim response */
-               if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout) || 
+               if (!cli_receive_smb(cli) || 
                    CVAL(cli->inbuf,smb_rcls) != 0) {
                        return(False);
                }      
@@ -252,14 +371,14 @@ static BOOL cli_send_trans(struct cli_state *cli, int trans,
                        if (trans==SMBtrans2)
                                SSVALS(cli->outbuf,smb_sfid,fid);               /* fid */
                        if (this_lparam)                        /* param[] */
-                               memcpy(outparam,param,this_lparam);
+                               memcpy(outparam,param+tot_param,this_lparam);
                        if (this_ldata)                 /* data[] */
-                               memcpy(outdata,data,this_ldata);
+                               memcpy(outdata,data+tot_data,this_ldata);
                        set_message(cli->outbuf,trans==SMBtrans?8:9, /* wcnt, bcc */
                                    PTR_DIFF(outdata+this_ldata,smb_buf(cli->outbuf)),False);
                        
                        show_msg(cli->outbuf);
-                       send_smb(cli->fd,cli->outbuf);
+                       cli_send_smb(cli);
                        
                        tot_data += this_ldata;
                        tot_param += this_lparam;
@@ -280,10 +399,12 @@ static BOOL cli_receive_trans(struct cli_state *cli,int trans,
        int total_data=0;
        int total_param=0;
        int this_data,this_param;
-       
+       uint8 eclass;
+       uint32 ecode;
+
        *data_len = *param_len = 0;
 
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
+       if (!cli_receive_smb(cli))
                return False;
 
        show_msg(cli->inbuf);
@@ -296,9 +417,16 @@ static BOOL cli_receive_trans(struct cli_state *cli,int trans,
                return(False);
        }
 
-       if (cli_error(cli, NULL, NULL))
+       /*
+        * An NT RPC pipe call can return ERRDOS, ERRmoredata
+        * to a trans call. This is not an error and should not
+        * be treated as such.
+        */
+
+       if (cli_error(cli, &eclass, &ecode, NULL))
        {
-               return(False);
+        if(cli->nt_pipe_fnum == 0 || !(eclass == ERRDOS && ecode == ERRmoredata))
+                       return(False);
        }
 
        /* parse out the lengths */
@@ -337,7 +465,7 @@ static BOOL cli_receive_trans(struct cli_state *cli,int trans,
                if (total_data <= *data_len && total_param <= *param_len)
                        break;
                
-               if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
+               if (!cli_receive_smb(cli))
                        return False;
 
                show_msg(cli->inbuf);
@@ -349,9 +477,10 @@ static BOOL cli_receive_trans(struct cli_state *cli,int trans,
                                 CVAL(cli->inbuf,smb_com)));
                        return(False);
                }
-               if (cli_error(cli, NULL, NULL))
+               if (cli_error(cli, &eclass, &ecode, NULL))
                {
-                       return(False);
+               if(cli->nt_pipe_fnum == 0 || !(eclass == ERRDOS && ecode == ERRmoredata))
+                               return(False);
                }
        }
        
@@ -432,9 +561,9 @@ BOOL cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
        pstrcpy(p,user);
        strupper(p);
        p += 21;
-    p++;
-    p += 15;
-    p++; 
+       p++;
+       p += 15;
+       p++; 
        pstrcpy(p, workstation); 
        strupper(p);
        p += 16;
@@ -454,7 +583,7 @@ BOOL cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
                
                if (cli->rap_error == 0) {
                        DEBUG(4,("NetWkstaUserLogon success\n"));
-                       cli->privilages = SVAL(p, 24);
+                       cli->privileges = SVAL(p, 24);
                        fstrcpy(cli->eff_name,p+2);
                } else {
                        DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli->rap_error));
@@ -489,12 +618,12 @@ BOOL cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32, c
   pstrcpy(p,"B13BWz");
   p = skip_string(p,1);
   SSVAL(p,0,1);
-  SSVAL(p,2,CLI_BUFFER_SIZE);
+  SSVAL(p,2,0xFFFF);
   p += 4;
 
   if (cli_api(cli, 
               param, PTR_DIFF(p,param), 1024,  /* Param, length, maxlen */
-              NULL, 0, CLI_BUFFER_SIZE,            /* data, length, maxlen */
+              NULL, 0, 0xFFFF,            /* data, length, maxlen */
               &rparam, &rprcnt,                /* return params, length */
               &rdata, &rdrcnt))                /* return data, length */
     {
@@ -502,20 +631,24 @@ BOOL cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32, c
       int converter=SVAL(rparam,2);
       int i;
       
-      if (res == 0)
-       {
-         count=SVAL(rparam,4);
-         p = rdata;
-
-         for (i=0;i<count;i++,p+=20)
-           {
-             char *sname = p;
-             int type = SVAL(p,14);
-             int comment_offset = IVAL(p,16) & 0xFFFF;
-             char *cmnt = comment_offset?(rdata+comment_offset-converter):"";
-             fn(sname, type, cmnt);
-           }
-       }
+      if (res == 0 || res == ERRmoredata) {
+             count=SVAL(rparam,4);
+             p = rdata;
+
+             for (i=0;i<count;i++,p+=20) {
+                     char *sname = p;
+                     int type = SVAL(p,14);
+                     int comment_offset = IVAL(p,16) & 0xFFFF;
+                     char *cmnt = comment_offset?(rdata+comment_offset-converter):"";
+                         dos_to_unix(sname,True);
+                         dos_to_unix(cmnt,True);
+                     fn(sname, type, cmnt);
+             }
+      } else {
+             DEBUG(4,("NetShareEnum res=%d\n", res));
+      }      
+    } else {
+             DEBUG(4,("NetShareEnum failed\n"));
     }
   
   if (rparam)
@@ -574,7 +707,7 @@ BOOL cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
                int converter=SVAL(rparam,2);
                int i;
                        
-               if (res == 0) {
+               if (res == 0 || res == ERRmoredata) {
                        count=SVAL(rparam,4);
                        p = rdata;
                                        
@@ -586,6 +719,8 @@ BOOL cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
 
                                stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
 
+                               dos_to_unix(sname, True);
+                               dos_to_unix(cmnt, True);
                                fn(sname, stype, cmnt);
                        }
                }
@@ -614,15 +749,18 @@ prots[] =
       {PROTOCOL_LANMAN1,"LANMAN1.0"},
       {PROTOCOL_LANMAN2,"LM1.2X002"},
       {PROTOCOL_LANMAN2,"Samba"},
-      {PROTOCOL_NT1,"NT LM 0.12"},
       {PROTOCOL_NT1,"NT LANMAN 1.0"},
+      {PROTOCOL_NT1,"NT LM 0.12"},
       {-1,NULL}
     };
 
 
 /****************************************************************************
-send a session setup 
+ Send a session setup. The username is in UNIX character format and must be
+ converted to DOS codepage format before sending. If the password is in
+ plaintext, the same should be done.
 ****************************************************************************/
+
 BOOL cli_session_setup(struct cli_state *cli, 
                       char *user, 
                       char *pass, int passlen,
@@ -639,22 +777,44 @@ BOOL cli_session_setup(struct cli_state *cli,
                return False;
        }
 
-        if (((passlen == 0) || (passlen == 1)) && (pass[0] == '\0')) {
-          /* Null session connect. */
-          pword[0] = '\0';
-          ntpword[0] = '\0';
-        } else {
-          if ((cli->sec_mode & 2) && passlen != 24) {
-            passlen = 24;
-            ntpasslen = 24;
-            SMBencrypt((uchar *)pass,(uchar *)cli->cryptkey,(uchar *)pword);
-            SMBNTencrypt((uchar *)ntpass,(uchar *)cli->cryptkey,(uchar *)ntpword);
-          } else {
-                 fstrcpy(pword, pass);
-                 fstrcpy(ntpword, "");
-                 ntpasslen = 0;
-          }
-        }
+       if (((passlen == 0) || (passlen == 1)) && (pass[0] == '\0')) {
+               /* Null session connect. */
+               pword[0] = '\0';
+               ntpword[0] = '\0';
+       } else {
+               if ((cli->sec_mode & 2) && passlen != 24) {
+                       /*
+                        * Encrypted mode needed, and non encrypted password supplied.
+                        */
+                       passlen = 24;
+                       ntpasslen = 24;
+                       fstrcpy(pword, pass);
+                       unix_to_dos(pword,True);
+                       fstrcpy(ntpword, ntpass);;
+                       unix_to_dos(ntpword,True);
+                       SMBencrypt((uchar *)pword,(uchar *)cli->cryptkey,(uchar *)pword);
+                       SMBNTencrypt((uchar *)ntpword,(uchar *)cli->cryptkey,(uchar *)ntpword);
+               } else if ((cli->sec_mode & 2) && passlen == 24) {
+                       /*
+                        * Encrypted mode needed, and encrypted password supplied.
+                        */
+                       memcpy(pword, pass, passlen);
+                       if(ntpasslen == 24) {
+                               memcpy(ntpword, ntpass, ntpasslen);
+                       } else {
+                               fstrcpy(ntpword, "");
+                               ntpasslen = 0;
+                       }
+               } else {
+                       /*
+                        * Plaintext mode needed, assume plaintext supplied.
+                        */
+                       fstrcpy(pword, pass);
+                       unix_to_dos(pword,True);
+                       fstrcpy(ntpword, "");
+                       ntpasslen = 0;
+               }
+       }
 
        /* if in share level security then don't send a password now */
        if (!(cli->sec_mode & 1)) {
@@ -665,7 +825,7 @@ BOOL cli_session_setup(struct cli_state *cli,
        } 
 
        /* send a session setup command */
-       bzero(cli->outbuf,smb_size);
+       memset(cli->outbuf,'\0',smb_size);
 
        if (cli->protocol < PROTOCOL_NT1)
        {
@@ -683,6 +843,7 @@ BOOL cli_session_setup(struct cli_state *cli,
                memcpy(p,pword,passlen);
                p += passlen;
                pstrcpy(p,user);
+               unix_to_dos(p,True);
                strupper(p);
        }
        else
@@ -698,13 +859,14 @@ BOOL cli_session_setup(struct cli_state *cli,
                SIVAL(cli->outbuf,smb_vwv5,cli->sesskey);
                SSVAL(cli->outbuf,smb_vwv7,passlen);
                SSVAL(cli->outbuf,smb_vwv8,ntpasslen);
-               SSVAL(cli->outbuf,smb_vwv11,CAP_STATUS32);
+               SSVAL(cli->outbuf,smb_vwv11,0);
                p = smb_buf(cli->outbuf);
                memcpy(p,pword,passlen); 
                p += SVAL(cli->outbuf,smb_vwv7);
                memcpy(p,ntpword,ntpasslen); 
                p += SVAL(cli->outbuf,smb_vwv8);
                pstrcpy(p,user);
+               unix_to_dos(p,True);
                strupper(p);
                p = skip_string(p,1);
                pstrcpy(p,workgroup);
@@ -715,8 +877,8 @@ BOOL cli_session_setup(struct cli_state *cli,
                set_message(cli->outbuf,13,PTR_DIFF(p,smb_buf(cli->outbuf)),False);
        }
 
-      send_smb(cli->fd,cli->outbuf);
-      if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
+      cli_send_smb(cli);
+      if (!cli_receive_smb(cli))
              return False;
 
       show_msg(cli->inbuf);
@@ -728,6 +890,26 @@ BOOL cli_session_setup(struct cli_state *cli,
       /* use the returned vuid from now on */
       cli->vuid = SVAL(cli->inbuf,smb_uid);
 
+      if (cli->protocol >= PROTOCOL_NT1) {
+        /*
+         * Save off some of the connected server
+         * info.
+         */
+        char *server_domain,*server_os,*server_type;
+        server_os = smb_buf(cli->inbuf);
+        server_type = skip_string(server_os,1);
+        server_domain = skip_string(server_type,1);
+        fstrcpy(cli->server_os, server_os);
+               dos_to_unix(cli->server_os, True);
+        fstrcpy(cli->server_type, server_type);
+               dos_to_unix(cli->server_type, True);
+        fstrcpy(cli->server_domain, server_domain);
+               dos_to_unix(cli->server_domain, True);
+      }
+
+      fstrcpy(cli->user_name, user);
+      dos_to_unix(cli->user_name, True);
+
       return True;
 }
 
@@ -737,15 +919,15 @@ BOOL cli_session_setup(struct cli_state *cli,
 
 BOOL cli_ulogoff(struct cli_state *cli)
 {
-        bzero(cli->outbuf,smb_size);
+        memset(cli->outbuf,'\0',smb_size);
         set_message(cli->outbuf,2,0,True);
         CVAL(cli->outbuf,smb_com) = SMBulogoffX;
         cli_setup_packet(cli);
        SSVAL(cli->outbuf,smb_vwv0,0xFF);
        SSVAL(cli->outbuf,smb_vwv2,0);  /* no additional info */
 
-        send_smb(cli->fd,cli->outbuf);
-        if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
+        cli_send_smb(cli);
+        if (!cli_receive_smb(cli))
                 return False;
 
         return CVAL(cli->inbuf,smb_rcls) == 0;
@@ -757,10 +939,10 @@ send a tconX
 BOOL cli_send_tconX(struct cli_state *cli, 
                    char *share, char *dev, char *pass, int passlen)
 {
-       fstring fullshare, pword;
+       fstring fullshare, pword, dos_pword;
        char *p;
-       bzero(cli->outbuf,smb_size);
-       bzero(cli->inbuf,smb_size);
+       memset(cli->outbuf,'\0',smb_size);
+       memset(cli->inbuf,'\0',smb_size);
 
        fstrcpy(cli->share, share);
 
@@ -771,14 +953,29 @@ BOOL cli_send_tconX(struct cli_state *cli,
        }
 
        if ((cli->sec_mode & 2) && *pass && passlen != 24) {
+               /*
+                * Non-encrypted passwords - convert to DOS codepage before encryption.
+                */
                passlen = 24;
-               SMBencrypt((uchar *)pass,(uchar *)cli->cryptkey,(uchar *)pword);
+               fstrcpy(dos_pword,pass);
+               unix_to_dos(dos_pword,True);
+               SMBencrypt((uchar *)dos_pword,(uchar *)cli->cryptkey,(uchar *)pword);
        } else {
-               memcpy(pword, pass, passlen);
+               if(!(cli->sec_mode & 2)) {
+                       /*
+                        * Non-encrypted passwords - convert to DOS codepage before using.
+                        */
+                       fstrcpy(pword,pass);
+                       unix_to_dos(pword,True);
+               } else {
+                       memcpy(pword, pass, passlen);
+               }
        }
 
        slprintf(fullshare, sizeof(fullshare)-1,
                 "\\\\%s\\%s", cli->desthost, share);
+       unix_to_dos(fullshare, True);
+       strupper(fullshare);
 
        set_message(cli->outbuf,4,
                    2 + strlen(fullshare) + passlen + strlen(dev),True);
@@ -794,19 +991,29 @@ BOOL cli_send_tconX(struct cli_state *cli,
        fstrcpy(p,fullshare);
        p = skip_string(p,1);
        pstrcpy(p,dev);
+       unix_to_dos(p,True);
 
        SCVAL(cli->inbuf,smb_rcls, 1);
 
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
+       cli_send_smb(cli);
+       if (!cli_receive_smb(cli))
                return False;
 
        if (CVAL(cli->inbuf,smb_rcls) != 0) {
                return False;
        }
 
-       fstrcpy(cli->dev, smb_buf(cli->inbuf));
+       fstrcpy(cli->dev, "A:");
+
+       if (cli->protocol >= PROTOCOL_NT1) {
+               fstrcpy(cli->dev, smb_buf(cli->inbuf));
+       }
+
+       if (strcasecmp(share,"IPC$")==0) {
+               fstrcpy(cli->dev, "IPC");
+       }
 
+       /* only grab the device if we have a recent protocol level */
        if (cli->protocol >= PROTOCOL_NT1 &&
            smb_buflen(cli->inbuf) == 3) {
                /* almost certainly win95 - enable bug fixes */
@@ -823,14 +1030,14 @@ send a tree disconnect
 ****************************************************************************/
 BOOL cli_tdis(struct cli_state *cli)
 {
-       bzero(cli->outbuf,smb_size);
+       memset(cli->outbuf,'\0',smb_size);
        set_message(cli->outbuf,0,0,True);
        CVAL(cli->outbuf,smb_com) = SMBtdis;
        SSVAL(cli->outbuf,smb_tid,cli->cnum);
        cli_setup_packet(cli);
        
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
+       cli_send_smb(cli);
+       if (!cli_receive_smb(cli))
                return False;
        
        return CVAL(cli->inbuf,smb_rcls) == 0;
@@ -843,8 +1050,8 @@ BOOL cli_rename(struct cli_state *cli, char *fname_src, char *fname_dst)
 {
         char *p;
 
-        bzero(cli->outbuf,smb_size);
-        bzero(cli->inbuf,smb_size);
+        memset(cli->outbuf,'\0',smb_size);
+        memset(cli->inbuf,'\0',smb_size);
 
         set_message(cli->outbuf,1, 4 + strlen(fname_src) + strlen(fname_dst), True);
 
@@ -857,12 +1064,14 @@ BOOL cli_rename(struct cli_state *cli, char *fname_src, char *fname_dst)
         p = smb_buf(cli->outbuf);
         *p++ = 4;
         pstrcpy(p,fname_src);
+        unix_to_dos(p,True);
         p = skip_string(p,1);
         *p++ = 4;
         pstrcpy(p,fname_dst);
+        unix_to_dos(p,True);
 
-        send_smb(cli->fd,cli->outbuf);
-        if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+        cli_send_smb(cli);
+        if (!cli_receive_smb(cli)) {
                 return False;
         }
 
@@ -880,8 +1089,8 @@ BOOL cli_unlink(struct cli_state *cli, char *fname)
 {
        char *p;
 
-       bzero(cli->outbuf,smb_size);
-       bzero(cli->inbuf,smb_size);
+       memset(cli->outbuf,'\0',smb_size);
+       memset(cli->inbuf,'\0',smb_size);
 
        set_message(cli->outbuf,1, 2 + strlen(fname),True);
 
@@ -894,9 +1103,10 @@ BOOL cli_unlink(struct cli_state *cli, char *fname)
        p = smb_buf(cli->outbuf);
        *p++ = 4;      
        pstrcpy(p,fname);
+    unix_to_dos(p,True);
 
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+       cli_send_smb(cli);
+       if (!cli_receive_smb(cli)) {
                return False;
        }
 
@@ -914,8 +1124,8 @@ BOOL cli_mkdir(struct cli_state *cli, char *dname)
 {
        char *p;
 
-       bzero(cli->outbuf,smb_size);
-       bzero(cli->inbuf,smb_size);
+       memset(cli->outbuf,'\0',smb_size);
+       memset(cli->inbuf,'\0',smb_size);
 
        set_message(cli->outbuf,0, 2 + strlen(dname),True);
 
@@ -926,9 +1136,10 @@ BOOL cli_mkdir(struct cli_state *cli, char *dname)
        p = smb_buf(cli->outbuf);
        *p++ = 4;      
        pstrcpy(p,dname);
+    unix_to_dos(p,True);
 
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+       cli_send_smb(cli);
+       if (!cli_receive_smb(cli)) {
                return False;
        }
 
@@ -946,8 +1157,8 @@ BOOL cli_rmdir(struct cli_state *cli, char *dname)
 {
        char *p;
 
-       bzero(cli->outbuf,smb_size);
-       bzero(cli->inbuf,smb_size);
+       memset(cli->outbuf,'\0',smb_size);
+       memset(cli->inbuf,'\0',smb_size);
 
        set_message(cli->outbuf,0, 2 + strlen(dname),True);
 
@@ -958,9 +1169,10 @@ BOOL cli_rmdir(struct cli_state *cli, char *dname)
        p = smb_buf(cli->outbuf);
        *p++ = 4;      
        pstrcpy(p,dname);
+    unix_to_dos(p,True);
 
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+       cli_send_smb(cli);
+       if (!cli_receive_smb(cli)) {
                return False;
        }
 
@@ -973,6 +1185,51 @@ BOOL cli_rmdir(struct cli_state *cli, char *dname)
 
 
 
+/****************************************************************************
+open a file
+****************************************************************************/
+int cli_nt_create(struct cli_state *cli, char *fname)
+{
+       char *p;
+
+       memset(cli->outbuf,'\0',smb_size);
+       memset(cli->inbuf,'\0',smb_size);
+
+       set_message(cli->outbuf,24,1 + strlen(fname),True);
+
+       CVAL(cli->outbuf,smb_com) = SMBntcreateX;
+       SSVAL(cli->outbuf,smb_tid,cli->cnum);
+       cli_setup_packet(cli);
+
+       SSVAL(cli->outbuf,smb_vwv0,0xFF);
+       SIVAL(cli->outbuf,smb_ntcreate_Flags, 0x06);
+       SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
+       SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, 0x2019f);
+       SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, 0x0);
+       SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, 0x03);
+       SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, 0x01);
+       SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, 0x0);
+       SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
+       SSVAL(cli->outbuf,smb_ntcreate_NameLength, strlen(fname));
+
+       p = smb_buf(cli->outbuf);
+       pstrcpy(p,fname);
+    unix_to_dos(p,True);
+       p = skip_string(p,1);
+
+       cli_send_smb(cli);
+       if (!cli_receive_smb(cli)) {
+               return -1;
+       }
+
+       if (CVAL(cli->inbuf,smb_rcls) != 0) {
+               return -1;
+       }
+
+       return SVAL(cli->inbuf,smb_vwv2 + 1);
+}
+
+
 /****************************************************************************
 open a file
 ****************************************************************************/
@@ -984,7 +1241,7 @@ int cli_open(struct cli_state *cli, char *fname, int flags, int share_mode)
 
        /* you must open for RW not just write - otherwise getattrE doesn't
           work! */
-       if ((flags & O_ACCMODE) == O_WRONLY) {
+       if ((flags & O_ACCMODE) == O_WRONLY && strncmp(cli->dev, "LPT", 3)) {
                flags = (flags & ~O_ACCMODE) | O_RDWR;
        }
 
@@ -1011,8 +1268,8 @@ int cli_open(struct cli_state *cli, char *fname, int flags, int share_mode)
        }
 #endif /* O_SYNC */
 
-       bzero(cli->outbuf,smb_size);
-       bzero(cli->inbuf,smb_size);
+       memset(cli->outbuf,'\0',smb_size);
+       memset(cli->inbuf,'\0',smb_size);
 
        set_message(cli->outbuf,15,1 + strlen(fname),True);
 
@@ -1026,13 +1283,22 @@ int cli_open(struct cli_state *cli, char *fname, int flags, int share_mode)
        SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
        SSVAL(cli->outbuf,smb_vwv5,0);
        SSVAL(cli->outbuf,smb_vwv8,openfn);
+
+       if (cli->use_oplocks) {
+               /* if using oplocks then ask for a batch oplock via
+                   core and extended methods */
+               CVAL(cli->outbuf,smb_flg) |= 
+                       FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
+               SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
+       }
   
        p = smb_buf(cli->outbuf);
        pstrcpy(p,fname);
+    unix_to_dos(p,True);
        p = skip_string(p,1);
 
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+       cli_send_smb(cli);
+       if (!cli_receive_smb(cli)) {
                return -1;
        }
 
@@ -1051,8 +1317,8 @@ int cli_open(struct cli_state *cli, char *fname, int flags, int share_mode)
 ****************************************************************************/
 BOOL cli_close(struct cli_state *cli, int fnum)
 {
-       bzero(cli->outbuf,smb_size);
-       bzero(cli->inbuf,smb_size);
+       memset(cli->outbuf,'\0',smb_size);
+       memset(cli->inbuf,'\0',smb_size);
 
        set_message(cli->outbuf,3,0,True);
 
@@ -1063,8 +1329,8 @@ BOOL cli_close(struct cli_state *cli, int fnum)
        SSVAL(cli->outbuf,smb_vwv0,fnum);
        SIVALS(cli->outbuf,smb_vwv1,-1);
 
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+       cli_send_smb(cli);
+       if (!cli_receive_smb(cli)) {
                return False;
        }
 
@@ -1082,9 +1348,10 @@ BOOL cli_close(struct cli_state *cli, int fnum)
 BOOL cli_lock(struct cli_state *cli, int fnum, uint32 offset, uint32 len, int timeout)
 {
        char *p;
+        int saved_timeout = cli->timeout;
 
-       bzero(cli->outbuf,smb_size);
-       bzero(cli->inbuf,smb_size);
+       memset(cli->outbuf,'\0',smb_size);
+       memset(cli->inbuf,'\0', smb_size);
 
        set_message(cli->outbuf,8,10,True);
 
@@ -1103,12 +1370,17 @@ BOOL cli_lock(struct cli_state *cli, int fnum, uint32 offset, uint32 len, int ti
        SSVAL(p, 0, cli->pid);
        SIVAL(p, 2, offset);
        SIVAL(p, 6, len);
+       cli_send_smb(cli);
+
+        cli->timeout = (timeout == -1) ? 0x7FFFFFFF : timeout;
 
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+       if (!cli_receive_smb(cli)) {
+                cli->timeout = saved_timeout;
                return False;
        }
 
+       cli->timeout = saved_timeout;
+
        if (CVAL(cli->inbuf,smb_rcls) != 0) {
                return False;
        }
@@ -1123,8 +1395,8 @@ BOOL cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len, int
 {
        char *p;
 
-       bzero(cli->outbuf,smb_size);
-       bzero(cli->inbuf,smb_size);
+       memset(cli->outbuf,'\0',smb_size);
+       memset(cli->inbuf,'\0',smb_size);
 
        set_message(cli->outbuf,8,10,True);
 
@@ -1144,8 +1416,8 @@ BOOL cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len, int
        SIVAL(p, 2, offset);
        SIVAL(p, 6, len);
 
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+       cli_send_smb(cli);
+       if (!cli_receive_smb(cli)) {
                return False;
        }
 
@@ -1164,8 +1436,8 @@ issue a single SMBread and don't wait for a reply
 static void cli_issue_read(struct cli_state *cli, int fnum, off_t offset, 
                           size_t size, int i)
 {
-       bzero(cli->outbuf,smb_size);
-       bzero(cli->inbuf,smb_size);
+       memset(cli->outbuf,'\0',smb_size);
+       memset(cli->inbuf,'\0',smb_size);
 
        set_message(cli->outbuf,10,0,True);
                
@@ -1180,7 +1452,7 @@ static void cli_issue_read(struct cli_state *cli, int fnum, off_t offset,
        SSVAL(cli->outbuf,smb_vwv6,size);
        SSVAL(cli->outbuf,smb_mid,cli->mid + i);
 
-       send_smb(cli->fd,cli->outbuf);
+       cli_send_smb(cli);
 }
 
 /****************************************************************************
@@ -1192,7 +1464,17 @@ size_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t
        int total = -1;
        int issued=0;
        int received=0;
-       int mpx = MAX(cli->max_mux-1, 1);
+/*
+ * There is a problem in this code when mpx is more than one.
+ * for some reason files can get corrupted when being read.
+ * Until we understand this fully I am serializing reads (one
+ * read/one reply) for now. JRA.
+ */
+#if 0
+       int mpx = MAX(cli->max_mux-1, 1); 
+#else
+       int mpx = 1;
+#endif
        int block = (cli->max_xmit - (smb_size+32)) & ~1023;
        int mid;
        int blocks = (size + (block-1)) / block;
@@ -1208,7 +1490,7 @@ size_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t
                        issued++;
                }
 
-               if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+               if (!cli_receive_smb(cli)) {
                        return total;
                }
 
@@ -1244,7 +1526,7 @@ size_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t
        }
 
        while (received < issued) {
-               client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
+               cli_receive_smb(cli);
                received++;
        }
        
@@ -1255,13 +1537,13 @@ size_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t
 /****************************************************************************
 issue a single SMBwrite and don't wait for a reply
 ****************************************************************************/
-static void cli_issue_write(struct cli_state *cli, int fnum, off_t offset, char *buf,
+static void cli_issue_write(struct cli_state *cli, int fnum, off_t offset, uint16 mode, char *buf,
                            size_t size, int i)
 {
        char *p;
 
-       bzero(cli->outbuf,smb_size);
-       bzero(cli->inbuf,smb_size);
+       memset(cli->outbuf,'\0',smb_size);
+       memset(cli->inbuf,'\0',smb_size);
 
        set_message(cli->outbuf,12,size,True);
        
@@ -1271,8 +1553,12 @@ static void cli_issue_write(struct cli_state *cli, int fnum, off_t offset, char
        
        CVAL(cli->outbuf,smb_vwv0) = 0xFF;
        SSVAL(cli->outbuf,smb_vwv2,fnum);
+
        SIVAL(cli->outbuf,smb_vwv3,offset);
-       
+       SIVAL(cli->outbuf,smb_vwv5,IS_BITS_SET_ALL(mode, 0x0008) ? 0xFFFFFFFF : 0);
+       SSVAL(cli->outbuf,smb_vwv7,mode);
+
+       SSVAL(cli->outbuf,smb_vwv8,IS_BITS_SET_ALL(mode, 0x0008) ? size : 0);
        SSVAL(cli->outbuf,smb_vwv10,size);
        SSVAL(cli->outbuf,smb_vwv11,
              smb_buf(cli->outbuf) - smb_base(cli->outbuf));
@@ -1282,64 +1568,113 @@ static void cli_issue_write(struct cli_state *cli, int fnum, off_t offset, char
 
        SSVAL(cli->outbuf,smb_mid,cli->mid + i);
        
-       send_smb(cli->fd,cli->outbuf);
+       show_msg(cli->outbuf);
+       cli_send_smb(cli);
 }
 
 /****************************************************************************
   write to a file
+  write_mode: 0x0001 disallow write cacheing
+              0x0002 return bytes remaining
+              0x0004 use raw named pipe protocol
+              0x0008 start of message mode named pipe protocol
 ****************************************************************************/
-size_t cli_write(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
+ssize_t cli_write(struct cli_state *cli,
+                 int fnum, uint16 write_mode,
+                 char *buf, off_t offset, size_t size)
 {
-       int total = -1;
-       int issued=0;
-       int received=0;
+       int bwritten = 0;
+       int issued = 0;
+       int received = 0;
        int mpx = MAX(cli->max_mux-1, 1);
        int block = (cli->max_xmit - (smb_size+32)) & ~1023;
-       int mid;
        int blocks = (size + (block-1)) / block;
 
-       if (size == 0) return 0;
-
        while (received < blocks) {
-               int size2;
 
-               while (issued - received < mpx && issued < blocks) {
-                       int size1 = MIN(block, size-issued*block);
-                       cli_issue_write(cli, fnum, offset+issued*block, buf + issued*block,
+               while ((issued - received < mpx) && (issued < blocks))
+               {
+                       int bsent = issued * block;
+                       int size1 = MIN(block, size - bsent);
+
+                       cli_issue_write(cli, fnum, offset + bsent,
+                                       write_mode,
+                                       buf + bsent,
                                        size1, issued);
                        issued++;
                }
 
-               if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
-                       return total;
+               if (!cli_receive_smb(cli))
+               {
+                       return bwritten;
                }
 
                received++;
-               mid = SVAL(cli->inbuf, smb_mid) - cli->mid;
-               size2 = SVAL(cli->inbuf, smb_vwv2);
-
-               if (CVAL(cli->inbuf,smb_rcls) != 0) {
-                       blocks = MIN(blocks, mid-1);
-                       continue;
-               }
 
-               if (size2 <= 0) {
-                       blocks = MIN(blocks, mid-1);
-                       /* this distinguishes EOF from an error */
-                       total = MAX(total, 0);
-                       continue;
+               if (CVAL(cli->inbuf,smb_rcls) != 0)
+               {
+                       break;
                }
 
-               total += size2;
-
-               total = MAX(total, mid*block + size2);
+               bwritten += SVAL(cli->inbuf, smb_vwv2);
        }
 
-       while (received < issued) {
-               client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
+       while (received < issued && cli_receive_smb(cli))
+       {
                received++;
        }
        
+       return bwritten;
+}
+
+
+/****************************************************************************
+  write to a file using a SMBwrite and not bypassing 0 byte writes
+****************************************************************************/
+ssize_t cli_smbwrite(struct cli_state *cli,
+                    int fnum, char *buf, off_t offset, size_t size1)
+{
+       char *p;
+       ssize_t total = 0;
+
+       do {
+               size_t size = MIN(size1, cli->max_xmit - 48);
+               
+               memset(cli->outbuf,'\0',smb_size);
+               memset(cli->inbuf,'\0',smb_size);
+
+               set_message(cli->outbuf,5, 3 + size,True);
+
+               CVAL(cli->outbuf,smb_com) = SMBwrite;
+               SSVAL(cli->outbuf,smb_tid,cli->cnum);
+               cli_setup_packet(cli);
+               
+               SSVAL(cli->outbuf,smb_vwv0,fnum);
+               SSVAL(cli->outbuf,smb_vwv1,size);
+               SIVAL(cli->outbuf,smb_vwv2,offset);
+               SSVAL(cli->outbuf,smb_vwv4,0);
+               
+               p = smb_buf(cli->outbuf);
+               *p++ = 1;
+               SSVAL(p, 0, size);
+               memcpy(p+2, buf, size);
+               
+               cli_send_smb(cli);
+               if (!cli_receive_smb(cli)) {
+                       return -1;
+               }
+               
+               if (CVAL(cli->inbuf,smb_rcls) != 0) {
+                       return -1;
+               }
+
+               size = SVAL(cli->inbuf,smb_vwv0);
+               if (size == 0) break;
+
+               size1 -= size;
+               total += size;
+       } while (size1);
+
        return total;
 }
 
@@ -1348,13 +1683,13 @@ size_t cli_write(struct cli_state *cli, int fnum, char *buf, off_t offset, size_
 do a SMBgetattrE call
 ****************************************************************************/
 BOOL cli_getattrE(struct cli_state *cli, int fd, 
-                 uint32 *attr, size_t *size, 
+                 uint16 *attr, size_t *size, 
                  time_t *c_time, time_t *a_time, time_t *m_time)
 {
-       bzero(cli->outbuf,smb_size);
-       bzero(cli->inbuf,smb_size);
+       memset(cli->outbuf,'\0',smb_size);
+       memset(cli->inbuf,'\0',smb_size);
 
-       set_message(cli->outbuf,2,0,True);
+       set_message(cli->outbuf,1,0,True);
 
        CVAL(cli->outbuf,smb_com) = SMBgetattrE;
        SSVAL(cli->outbuf,smb_tid,cli->cnum);
@@ -1362,8 +1697,8 @@ BOOL cli_getattrE(struct cli_state *cli, int fd,
 
        SSVAL(cli->outbuf,smb_vwv0,fd);
 
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+       cli_send_smb(cli);
+       if (!cli_receive_smb(cli)) {
                return False;
        }
        
@@ -1399,12 +1734,12 @@ BOOL cli_getattrE(struct cli_state *cli, int fd,
 do a SMBgetatr call
 ****************************************************************************/
 BOOL cli_getatr(struct cli_state *cli, char *fname, 
-               uint32 *attr, size_t *size, time_t *t)
+               uint16 *attr, size_t *size, time_t *t)
 {
        char *p;
 
-       bzero(cli->outbuf,smb_size);
-       bzero(cli->inbuf,smb_size);
+       memset(cli->outbuf,'\0',smb_size);
+       memset(cli->inbuf,'\0',smb_size);
 
        set_message(cli->outbuf,0,strlen(fname)+2,True);
 
@@ -1415,9 +1750,10 @@ BOOL cli_getatr(struct cli_state *cli, char *fname,
        p = smb_buf(cli->outbuf);
        *p = 4;
        pstrcpy(p+1, fname);
+    unix_to_dos(p+1,True);
 
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+       cli_send_smb(cli);
+       if (!cli_receive_smb(cli)) {
                return False;
        }
        
@@ -1445,12 +1781,12 @@ BOOL cli_getatr(struct cli_state *cli, char *fname,
 /****************************************************************************
 do a SMBsetatr call
 ****************************************************************************/
-BOOL cli_setatr(struct cli_state *cli, char *fname, int attr, time_t t)
+BOOL cli_setatr(struct cli_state *cli, char *fname, uint16 attr, time_t t)
 {
        char *p;
 
-       bzero(cli->outbuf,smb_size);
-       bzero(cli->inbuf,smb_size);
+       memset(cli->outbuf,'\0',smb_size);
+       memset(cli->inbuf,'\0',smb_size);
 
        set_message(cli->outbuf,8,strlen(fname)+4,True);
 
@@ -1464,11 +1800,12 @@ BOOL cli_setatr(struct cli_state *cli, char *fname, int attr, time_t t)
        p = smb_buf(cli->outbuf);
        *p = 4;
        pstrcpy(p+1, fname);
+    unix_to_dos(p+1,True);
        p = skip_string(p,1);
        *p = 4;
 
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+       cli_send_smb(cli);
+       if (!cli_receive_smb(cli)) {
                return False;
        }
        
@@ -1484,7 +1821,7 @@ send a qpathinfo call
 ****************************************************************************/
 BOOL cli_qpathinfo(struct cli_state *cli, const char *fname, 
                   time_t *c_time, time_t *a_time, time_t *m_time, 
-                  size_t *size, uint32 *mode)
+                  size_t *size, uint16 *mode)
 {
        int data_len = 0;
        int param_len = 0;
@@ -1500,6 +1837,7 @@ BOOL cli_qpathinfo(struct cli_state *cli, const char *fname,
        memset(param, 0, param_len);
        SSVAL(param, 0, SMB_INFO_STANDARD);
        pstrcpy(&param[6], fname);
+    unix_to_dos(&param[6],True);
 
        do {
                ret = (cli_send_trans(cli, SMBtrans2, 
@@ -1517,7 +1855,7 @@ BOOL cli_qpathinfo(struct cli_state *cli, const char *fname,
                           it gives ERRSRV/ERRerror temprarily */
                        uint8 eclass;
                        uint32 ecode;
-                       cli_error(cli, &eclass, &ecode);
+                       cli_error(cli, &eclass, &ecode, NULL);
                        if (eclass != ERRSRV || ecode != ERRerror) break;
                        msleep(100);
                }
@@ -1559,7 +1897,8 @@ send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level
 ****************************************************************************/
 BOOL cli_qpathinfo2(struct cli_state *cli, const char *fname, 
                    time_t *c_time, time_t *a_time, time_t *m_time, 
-                   time_t *w_time, size_t *size, uint32 *mode)
+                   time_t *w_time, size_t *size, uint16 *mode,
+                   SMB_INO_T *ino)
 {
        int data_len = 0;
        int param_len = 0;
@@ -1572,6 +1911,7 @@ BOOL cli_qpathinfo2(struct cli_state *cli, const char *fname,
        memset(param, 0, param_len);
        SSVAL(param, 0, SMB_QUERY_FILE_ALL_INFO);
        pstrcpy(&param[6], fname);
+    unix_to_dos(&param[6],True);
 
        if (!cli_send_trans(cli, SMBtrans2, 
                             NULL, 0,                      /* name, length */
@@ -1605,11 +1945,14 @@ BOOL cli_qpathinfo2(struct cli_state *cli, const char *fname,
        if (w_time) {
                *w_time = interpret_long_date(rdata+24) - cli->serverzone;
        }
+       if (mode) {
+               *mode = SVAL(rdata, 32);
+       }
        if (size) {
-               *size = IVAL(rdata, 40);
+               *size = IVAL(rdata, 48);
        }
-       if (mode) {
-               *mode = IVAL(rdata, 32);
+       if (ino) {
+               *ino = IVAL(rdata, 64);
        }
 
        if (rdata) free(rdata);
@@ -1622,8 +1965,9 @@ BOOL cli_qpathinfo2(struct cli_state *cli, const char *fname,
 send a qfileinfo call
 ****************************************************************************/
 BOOL cli_qfileinfo(struct cli_state *cli, int fnum, 
-                  uint32 *mode, size_t *size,
-                  time_t *c_time, time_t *a_time, time_t *m_time)
+                  uint16 *mode, size_t *size,
+                  time_t *c_time, time_t *a_time, time_t *m_time, 
+                  time_t *w_time, SMB_INO_T *ino)
 {
        int data_len = 0;
        int param_len = 0;
@@ -1631,11 +1975,15 @@ BOOL cli_qfileinfo(struct cli_state *cli, int fnum,
        pstring param;
        char *rparam=NULL, *rdata=NULL;
 
+       /* if its a win95 server then fail this - win95 totally screws it
+          up */
+       if (cli->win95) return False;
+
        param_len = 4;
 
        memset(param, 0, param_len);
        SSVAL(param, 0, fnum);
-       SSVAL(param, 2, SMB_INFO_STANDARD);
+       SSVAL(param, 2, SMB_QUERY_FILE_ALL_INFO);
 
        if (!cli_send_trans(cli, SMBtrans2, 
                             NULL, 0,                        /* name, length */
@@ -1653,24 +2001,30 @@ BOOL cli_qfileinfo(struct cli_state *cli, int fnum,
                return False;
        }
 
-       if (!rdata || data_len < 22) {
+       if (!rdata || data_len < 68) {
                return False;
        }
 
        if (c_time) {
-               *c_time = make_unix_date2(rdata+0);
+               *c_time = interpret_long_date(rdata+0) - cli->serverzone;
        }
        if (a_time) {
-               *a_time = make_unix_date2(rdata+4);
+               *a_time = interpret_long_date(rdata+8) - cli->serverzone;
        }
        if (m_time) {
-               *m_time = make_unix_date2(rdata+8);
+               *m_time = interpret_long_date(rdata+16) - cli->serverzone;
        }
-       if (size) {
-               *size = IVAL(rdata, 12);
+       if (w_time) {
+               *w_time = interpret_long_date(rdata+24) - cli->serverzone;
        }
        if (mode) {
-               *mode = SVAL(rdata,l1_attrFile);
+               *mode = SVAL(rdata, 32);
+       }
+       if (size) {
+               *size = IVAL(rdata, 48);
+       }
+       if (ino) {
+               *ino = IVAL(rdata, 64);
        }
 
        if (rdata) free(rdata);
@@ -1703,6 +2057,7 @@ static int interpret_long_filename(int level,char *p,file_info *finfo)
                                finfo->size = IVAL(p,16);
                                finfo->mode = CVAL(p,24);
                                pstrcpy(finfo->name,p+27);
+                               dos_to_unix(finfo->name,True);
                        }
                        return(28 + CVAL(p,26));
 
@@ -1715,6 +2070,7 @@ static int interpret_long_filename(int level,char *p,file_info *finfo)
                                finfo->size = IVAL(p,16);
                                finfo->mode = CVAL(p,24);
                                pstrcpy(finfo->name,p+31);
+                               dos_to_unix(finfo->name,True);
                        }
                        return(32 + CVAL(p,30));
 
@@ -1728,6 +2084,7 @@ static int interpret_long_filename(int level,char *p,file_info *finfo)
                                finfo->size = IVAL(p,20);
                                finfo->mode = CVAL(p,28);
                                pstrcpy(finfo->name,p+33);
+                               dos_to_unix(finfo->name,True);
                        }
                        return(SVAL(p,4)+4);
                        
@@ -1740,6 +2097,7 @@ static int interpret_long_filename(int level,char *p,file_info *finfo)
                                finfo->size = IVAL(p,20);
                                finfo->mode = CVAL(p,28);
                                pstrcpy(finfo->name,p+37);
+                               dos_to_unix(finfo->name,True);
                        }
                        return(SVAL(p,4)+4);
                        
@@ -1775,7 +2133,8 @@ static int interpret_long_filename(int level,char *p,file_info *finfo)
                                p += 4; /* EA size */
                                p += 2; /* short name len? */
                                p += 24; /* short name? */        
-                               StrnCpy(finfo->name,p,namelen);
+                               StrnCpy(finfo->name,p,MIN(sizeof(finfo->name)-1,namelen));
+                               dos_to_unix(finfo->name,True);
                                return(ret);
                        }
                        return(SVAL(p,0));
@@ -1789,7 +2148,8 @@ static int interpret_long_filename(int level,char *p,file_info *finfo)
 /****************************************************************************
   do a directory listing, calling fn on each file found
   ****************************************************************************/
-int cli_list(struct cli_state *cli,char *Mask,int attribute,void (*fn)(file_info *))
+int cli_list(struct cli_state *cli,const char *Mask,uint16 attribute, 
+            void (*fn)(file_info *, const char *))
 {
        int max_matches = 512;
        /* NT uses 260, OS/2 uses 2. Both accept 1. */
@@ -1802,7 +2162,6 @@ int cli_list(struct cli_state *cli,char *Mask,int attribute,void (*fn)(file_info
        int dirlist_len = 0;
        int total_received = -1;
        BOOL First = True;
-       int ff_resume_key = 0;
        int ff_searchcount=0;
        int ff_eos=0;
        int ff_lastname=0;
@@ -1815,6 +2174,7 @@ int cli_list(struct cli_state *cli,char *Mask,int attribute,void (*fn)(file_info
        pstring param;
        
        pstrcpy(mask,Mask);
+       unix_to_dos(mask,True);
        
        while (ff_eos == 0) {
                loop_count++;
@@ -1838,12 +2198,12 @@ int cli_list(struct cli_state *cli,char *Mask,int attribute,void (*fn)(file_info
                        SSVAL(param,0,ff_dir_handle);
                        SSVAL(param,2,max_matches); /* max count */
                        SSVAL(param,4,info_level); 
-                       SIVAL(param,6,ff_resume_key); /* ff_resume_key */
+                       SIVAL(param,6,0); /* ff_resume_key */
                        SSVAL(param,10,8+4+2);  /* resume required + close on end + continue */
                        pstrcpy(param+12,mask);
 
-                       DEBUG(5,("hand=0x%X resume=%d ff_lastname=%d mask=%s\n",
-                                ff_dir_handle,ff_resume_key,ff_lastname,mask));
+                       DEBUG(5,("hand=0x%X ff_lastname=%d mask=%s\n",
+                                ff_dir_handle,ff_lastname,mask));
                }
 
                if (!cli_send_trans(cli, SMBtrans2, 
@@ -1864,7 +2224,7 @@ int cli_list(struct cli_state *cli,char *Mask,int attribute,void (*fn)(file_info
                           it gives ERRSRV/ERRerror temprarily */
                        uint8 eclass;
                        uint32 ecode;
-                       cli_error(cli, &eclass, &ecode);
+                       cli_error(cli, &eclass, &ecode, NULL);
                        if (eclass != ERRSRV || ecode != ERRerror) break;
                        msleep(100);
                        continue;
@@ -1896,19 +2256,19 @@ int cli_list(struct cli_state *cli,char *Mask,int attribute,void (*fn)(file_info
                        switch(info_level)
                                {
                                case 260:
-                                       ff_resume_key =0;
                                        StrnCpy(mask,p+ff_lastname,
-                                               data_len-ff_lastname);
+                                               MIN(sizeof(mask)-1,data_len-ff_lastname));
                                        break;
                                case 1:
                                        pstrcpy(mask,p + ff_lastname + 1);
-                                       ff_resume_key = 0;
                                        break;
                                }
                } else {
                        pstrcpy(mask,"");
                }
-  
+               dos_to_unix(mask, True);
                /* and add them to the dirlist pool */
                dirlist = Realloc(dirlist,dirlist_len + data_len);
 
@@ -1932,15 +2292,17 @@ int cli_list(struct cli_state *cli,char *Mask,int attribute,void (*fn)(file_info
                if (rdata) free(rdata); rdata = NULL;
                if (rparam) free(rparam); rparam = NULL;
                
-               DEBUG(3,("received %d entries (eos=%d resume=%d)\n",
-                        ff_searchcount,ff_eos,ff_resume_key));
+               DEBUG(3,("received %d entries (eos=%d)\n",
+                        ff_searchcount,ff_eos));
+
+               if (ff_searchcount > 0) loop_count = 0;
 
                First = False;
        }
 
        for (p=dirlist,i=0;i<total_received;i++) {
                p += interpret_long_filename(info_level,p,&finfo);
-               fn(&finfo);
+               fn(&finfo, Mask);
        }
 
        /* free up the dirlist buffer */
@@ -1953,8 +2315,8 @@ int cli_list(struct cli_state *cli,char *Mask,int attribute,void (*fn)(file_info
 Send a SamOEMChangePassword command
 ****************************************************************************/
 
-BOOL cli_oem_change_password(struct cli_state *cli, char *user, char *new_password,
-                             char *old_password)
+BOOL cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
+                             const char *old_password)
 {
   char param[16+sizeof(fstring)];
   char data[532];
@@ -1965,21 +2327,16 @@ BOOL cli_oem_change_password(struct cli_state *cli, char *user, char *new_passwo
   unsigned char new_pw_hash[16];
   int data_len;
   int param_len = 0;
-  int new_pw_len = strlen(new_password);
   char *rparam = NULL;
   char *rdata = NULL;
   int rprcnt, rdrcnt;
+  pstring dos_new_password;
 
   if (strlen(user) >= sizeof(fstring)-1) {
     DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
     return False;
   }
 
-  if (new_pw_len > 512) {
-    DEBUG(0,("cli_oem_change_password: new password for user %s is too long.\n", user));
-    return False;
-  }
-
   SSVAL(p,0,214); /* SamOEMChangePassword command. */
   p += 2;
   pstrcpy(p, "zsT");
@@ -1993,32 +2350,28 @@ BOOL cli_oem_change_password(struct cli_state *cli, char *user, char *new_passwo
 
   param_len = PTR_DIFF(p,param);
 
-  /*
-   * Now setup the data area.
-   * We need to generate a random fill
-   * for this area to make it harder to
-   * decrypt. JRA.
-   */
-  generate_random_buffer((unsigned char *)data, sizeof(data), False);
-  fstrcpy( &data[512 - new_pw_len], new_password);
-  SIVAL(data, 512, new_pw_len);
-
   /*
    * Get the Lanman hash of the old password, we
-   * use this as the key to SamOEMHash().
+   * use this as the key to make_oem_passwd_hash().
    */
   memset(upper_case_old_pw, '\0', sizeof(upper_case_old_pw));
   fstrcpy(upper_case_old_pw, old_password);
+  unix_to_dos(upper_case_old_pw,True);
   strupper(upper_case_old_pw);
   E_P16((uchar *)upper_case_old_pw, old_pw_hash);
 
-  SamOEMhash( (unsigned char *)data, (unsigned char *)old_pw_hash, True);
+  pstrcpy(dos_new_password, new_password);
+  unix_to_dos(dos_new_password, True);
+
+  if (!make_oem_passwd_hash( data, dos_new_password, old_pw_hash, False))
+    return False;
 
   /* 
    * Now place the old password hash in the data.
    */
   memset(upper_case_new_pw, '\0', sizeof(upper_case_new_pw));
   fstrcpy(upper_case_new_pw, new_password);
+  unix_to_dos(upper_case_new_pw,True);
   strupper(upper_case_new_pw);
 
   E_P16((uchar *)upper_case_new_pw, new_pw_hash);
@@ -2063,7 +2416,7 @@ BOOL cli_negprot(struct cli_state *cli)
        int numprots;
        int plength;
 
-       bzero(cli->outbuf,smb_size);
+       memset(cli->outbuf,'\0',smb_size);
 
        /* setup the protocol strings */
        for (plength=0,numprots=0;
@@ -2079,6 +2432,7 @@ BOOL cli_negprot(struct cli_state *cli)
             numprots++) {
                *p++ = 2;
                pstrcpy(p,prots[numprots].name);
+               unix_to_dos(p,True);
                p += strlen(p) + 1;
        }
 
@@ -2087,8 +2441,8 @@ BOOL cli_negprot(struct cli_state *cli)
 
        CVAL(smb_buf(cli->outbuf),0) = 2;
 
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
+       cli_send_smb(cli);
+       if (!cli_receive_smb(cli))
                return False;
 
        show_msg(cli->inbuf);
@@ -2107,7 +2461,8 @@ BOOL cli_negprot(struct cli_state *cli)
                cli->max_mux = SVAL(cli->inbuf, smb_vwv1+1);
                cli->max_xmit = IVAL(cli->inbuf,smb_vwv3+1);
                cli->sesskey = IVAL(cli->inbuf,smb_vwv7+1);
-               cli->serverzone = SVALS(cli->inbuf,smb_vwv15+1)*60;
+               cli->serverzone = SVALS(cli->inbuf,smb_vwv15+1);
+               cli->serverzone *= 60;
                /* this time arrives in real GMT */
                cli->servertime = interpret_long_date(cli->inbuf+smb_vwv11+1);
                memcpy(cli->cryptkey,smb_buf(cli->inbuf),8);
@@ -2120,7 +2475,8 @@ BOOL cli_negprot(struct cli_state *cli)
                cli->sec_mode = SVAL(cli->inbuf,smb_vwv1);
                cli->max_xmit = SVAL(cli->inbuf,smb_vwv2);
                cli->sesskey = IVAL(cli->inbuf,smb_vwv6);
-               cli->serverzone = SVALS(cli->inbuf,smb_vwv10)*60;
+               cli->serverzone = SVALS(cli->inbuf,smb_vwv10);
+               cli->serverzone *= 60;
                /* this time is converted to GMT by make_unix_date */
                cli->servertime = make_unix_date(cli->inbuf+smb_vwv8);
                cli->readbraw_supported = ((SVAL(cli->inbuf,smb_vwv5) & 0x1) != 0);
@@ -2169,12 +2525,41 @@ BOOL cli_session_request(struct cli_state *cli,
 retry:
 #endif /* WITH_SSL */
 
-       send_smb(cli->fd,cli->outbuf);
+       cli_send_smb(cli);
        DEBUG(5,("Sent session request\n"));
 
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
+       if (!cli_receive_smb(cli))
                return False;
 
+       if (CVAL(cli->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;
+               };
+               */
+               int port = (CVAL(cli->inbuf,8)<<8)+CVAL(cli->inbuf,9);
+               /* SESSION RETARGET */
+               putip((char *)&cli->dest_ip,cli->inbuf+4);
+
+               close_sockets();
+               cli->fd = open_socket_out(SOCK_STREAM, &cli->dest_ip, port, LONG_CONNECT_TIMEOUT);
+               if (cli->fd == -1)
+                       return False;
+
+               DEBUG(3,("Retargeted\n"));
+
+               set_socket_options(cli->fd,user_socket_options);
+
+               /* Try again */
+               return cli_session_request(cli, calling, called);
+       } /* C. Hoch 9/14/95 End */
+
 #ifdef WITH_SSL
     if (CVAL(cli->inbuf,0) == 0x83 && CVAL(cli->inbuf,4) == 0x8e){ /* use ssl */
         if (!sslutil_fd_is_ssl(cli->fd)){
@@ -2186,7 +2571,7 @@ retry:
 
        if (CVAL(cli->inbuf,0) != 0x82) {
                 /* This is the wrong place to put the error... JRA. */
-               cli->rap_error = CVAL(cli->inbuf,0);
+               cli->rap_error = CVAL(cli->inbuf,4);
                return False;
        }
        return(True);
@@ -2196,26 +2581,30 @@ retry:
 /****************************************************************************
 open the client sockets
 ****************************************************************************/
-BOOL cli_connect(struct cli_state *cli, char *host, struct in_addr *ip)
+BOOL cli_connect(struct cli_state *cli, const char *host, struct in_addr *ip)
 {
-       struct in_addr dest_ip;
        extern struct in_addr ipzero;
 
        fstrcpy(cli->desthost, host);
        
        if (!ip || ip_equal(*ip, ipzero)) {
-                if (!resolve_name( cli->desthost, &dest_ip, 0x20)) {
+                if (!resolve_name( cli->desthost, &cli->dest_ip, 0x20)) {
                         return False;
                 }
+               if (ip) *ip = cli->dest_ip;
        } else {
-               dest_ip = *ip;
+               cli->dest_ip = *ip;
        }
 
+        if (cli->port == 0) cli->port = 139;  /* Set to default */
 
-       cli->fd = open_socket_out(SOCK_STREAM, &dest_ip, 139, cli->timeout);
+       cli->fd = open_socket_out(SOCK_STREAM, &cli->dest_ip, 
+                                 cli->port, cli->timeout);
        if (cli->fd == -1)
                return False;
 
+       set_socket_options(cli->fd,user_socket_options);
+
        return True;
 }
 
@@ -2223,27 +2612,44 @@ BOOL cli_connect(struct cli_state *cli, char *host, struct in_addr *ip)
 /****************************************************************************
 initialise a client structure
 ****************************************************************************/
-BOOL cli_initialise(struct cli_state *cli)
+struct cli_state *cli_initialise(struct cli_state *cli)
 {
-       if (cli->initialised)
-      cli_shutdown(cli);
+       if (!cli) {
+               cli = (struct cli_state *)malloc(sizeof(*cli));
+               if (!cli)
+                       return NULL;
+               ZERO_STRUCTP(cli);
+       }
 
-       memset(cli, 0, sizeof(*cli));
+       if (cli->initialised) {
+               cli_shutdown(cli);
+       }
+
+       ZERO_STRUCTP(cli);
+
+       cli->port = 0;
        cli->fd = -1;
        cli->cnum = -1;
        cli->pid = (uint16)getpid();
        cli->mid = 1;
        cli->vuid = UID_FIELD_INVALID;
        cli->protocol = PROTOCOL_NT1;
-       cli->timeout = 20000;
+       cli->timeout = 20000; /* Timeout is in milliseconds. */
        cli->bufsize = CLI_BUFFER_SIZE+4;
        cli->max_xmit = cli->bufsize;
        cli->outbuf = (char *)malloc(cli->bufsize);
        cli->inbuf = (char *)malloc(cli->bufsize);
        if (!cli->outbuf || !cli->inbuf)
-      return False;
+       {
+               return False;
+       }
+
+       memset(cli->outbuf, '\0', cli->bufsize);
+       memset(cli->inbuf, '\0', cli->bufsize);
+
        cli->initialised = 1;
-       return True;
+
+       return cli;
 }
 
 /****************************************************************************
@@ -2252,9 +2658,13 @@ shutdown a client structure
 void cli_shutdown(struct cli_state *cli)
 {
        if (cli->outbuf)
-      free(cli->outbuf);
+       {
+               free(cli->outbuf);
+       }
        if (cli->inbuf)
-      free(cli->inbuf);
+       {
+               free(cli->inbuf);
+       }
 #ifdef WITH_SSL
     if (cli->fd != -1)
       sslutil_disconnect(cli->fd);
@@ -2273,7 +2683,7 @@ void cli_shutdown(struct cli_state *cli)
   for 32 bit "warnings", a return code of 0 is expected.
 
 ****************************************************************************/
-int cli_error(struct cli_state *cli, uint8 *eclass, uint32 *num)
+int cli_error(struct cli_state *cli, uint8 *eclass, uint32 *num, uint32 *nt_rpc_error)
 {
        int  flgs2 = SVAL(cli->inbuf,smb_flg2);
        char rcls;
@@ -2281,6 +2691,7 @@ int cli_error(struct cli_state *cli, uint8 *eclass, uint32 *num)
 
        if (eclass) *eclass = 0;
        if (num   ) *num = 0;
+       if (nt_rpc_error) *nt_rpc_error = cli->nt_error;
 
        if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
                /* 32 bit error codes detected */
@@ -2320,11 +2731,17 @@ int cli_error(struct cli_state *cli, uint8 *eclass, uint32 *num)
                case ERRnoaccess: return EACCES;
                case ERRfilexists: return EEXIST;
                case ERRrename: return EEXIST;
+               case ERRbadshare: return EBUSY;
+               case ERRlock: return EBUSY;
                }
        }
        if (rcls == ERRSRV) {
                switch (code) {
                case ERRbadpw: return EPERM;
+               case ERRaccess: return EACCES;
+               case ERRnoresource: return ENOMEM;
+               case ERRinvdevice: return ENODEV;
+               case ERRinvnetname: return ENODEV;
                }
        }
        /* for other cases */
@@ -2350,17 +2767,17 @@ uint16 cli_setpid(struct cli_state *cli, uint16 pid)
 }
 
 /****************************************************************************
-establishes a connection right up to doing tconX, reading in a password.
+re-establishes a connection
 ****************************************************************************/
 BOOL cli_reestablish_connection(struct cli_state *cli)
 {
        struct nmb_name calling;
        struct nmb_name called;
        fstring dest_host;
-       struct in_addr dest_ip;
        fstring share;
        fstring dev;
        BOOL do_tcon = False;
+       int oldfd = cli->fd;
 
        if (!cli->initialised || cli->fd == -1)
        {
@@ -2380,16 +2797,26 @@ BOOL cli_reestablish_connection(struct cli_state *cli)
        memcpy(&called , &(cli->called ), sizeof(called ));
        memcpy(&calling, &(cli->calling), sizeof(calling));
        fstrcpy(dest_host, cli->full_dest_host_name);
-       dest_ip = cli->dest_ip;
 
        DEBUG(5,("cli_reestablish_connection: %s connecting to %s (ip %s) - %s [%s]\n",
-                         namestr(&calling), namestr(&called), inet_ntoa(dest_ip),
-                     cli->user_name, cli->domain));
+                nmb_namestr(&calling), nmb_namestr(&called), 
+                inet_ntoa(cli->dest_ip),
+                cli->user_name, cli->domain));
+
+       cli->fd = -1;
 
-       return cli_establish_connection(cli,
-                                       dest_host, &dest_ip,
-                                       &calling, &called,
-                                       share, dev, False, do_tcon);
+       if (cli_establish_connection(cli,
+                                    dest_host, &cli->dest_ip,
+                                    &calling, &called,
+                                    share, dev, False, do_tcon)) {
+               if (cli->fd != oldfd) {
+                       if (dup2(cli->fd, oldfd) == oldfd) {
+                               close(cli->fd);
+                       }
+               }
+               return True;
+       }
+       return False;
 }
 
 /****************************************************************************
@@ -2402,7 +2829,7 @@ BOOL cli_establish_connection(struct cli_state *cli,
                                BOOL do_shutdown, BOOL do_tcon)
 {
        DEBUG(5,("cli_establish_connection: %s connecting to %s (%s) - %s [%s]\n",
-                         namestr(calling), namestr(called), inet_ntoa(*dest_ip),
+                         nmb_namestr(calling), nmb_namestr(called), inet_ntoa(*dest_ip),
                      cli->user_name, cli->domain));
 
        /* establish connection */
@@ -2417,7 +2844,7 @@ BOOL cli_establish_connection(struct cli_state *cli,
                if (!cli_connect(cli, dest_host, dest_ip))
                {
                        DEBUG(1,("cli_establish_connection: failed to connect to %s (%s)\n",
-                                         namestr(calling), inet_ntoa(*dest_ip)));
+                                         nmb_namestr(calling), inet_ntoa(*dest_ip)));
                        return False;
                }
        }
@@ -2440,21 +2867,33 @@ BOOL cli_establish_connection(struct cli_state *cli,
 
        if (cli->pwd.cleartext || cli->pwd.null_pwd)
        {
-               /* attempt clear-text session */
-
                fstring passwd;
+               int pass_len;
 
-               pwd_get_cleartext(&(cli->pwd), passwd);
+               if (cli->pwd.null_pwd)
+               {
+                       /* attempt null session */
+                       passwd[0] = 0;
+                       pass_len = 1;
+               }
+               else
+               {
+                       /* attempt clear-text session */
+                       pwd_get_cleartext(&(cli->pwd), passwd);
+                       pass_len = strlen(passwd);
+               }
 
                /* attempt clear-text session */
                if (!cli_session_setup(cli, cli->user_name,
-                              passwd, strlen(passwd),
+                              passwd, pass_len,
                               NULL, 0,
                               cli->domain))
                {
                        DEBUG(1,("failed session setup\n"));
                        if (do_shutdown)
-              cli_shutdown(cli);
+                       {
+                               cli_shutdown(cli);
+                       }
                        return False;
                }
                if (do_tcon)
@@ -2464,7 +2903,9 @@ BOOL cli_establish_connection(struct cli_state *cli,
                        {
                                DEBUG(1,("failed tcon_X\n"));
                                if (do_shutdown)
-                  cli_shutdown(cli);
+                               {
+                                       cli_shutdown(cli);
+                               }
                                return False;
                        }
                }
@@ -2522,7 +2963,7 @@ int cli_printjob_del(struct cli_state *cli, int job)
        int rdrcnt,rprcnt, ret = -1;
        pstring param;
 
-       bzero(param,sizeof(param));
+       memset(param,'\0',sizeof(param));
 
        p = param;
        SSVAL(p,0,81);          /* DosPrintJobDel() */
@@ -2563,7 +3004,7 @@ int cli_print_queue(struct cli_state *cli,
        int result_code=0;
        int i = -1;
        
-       bzero(param,sizeof(param));
+       memset(param,'\0',sizeof(param));
 
        p = param;
        SSVAL(p,0,76);         /* API function number 76 (DosPrintJobEnum) */
@@ -2619,3 +3060,203 @@ int cli_print_queue(struct cli_state *cli,
 
        return i;
 }
+
+/****************************************************************************
+check for existance of a dir
+****************************************************************************/
+BOOL cli_chkpath(struct cli_state *cli, char *path)
+{
+       pstring path2;
+       char *p;
+       
+       safe_strcpy(path2,path,sizeof(pstring));
+       trim_string(path2,NULL,"\\");
+       if (!*path2) *path2 = '\\';
+       
+       memset(cli->outbuf,'\0',smb_size);
+       set_message(cli->outbuf,0,4 + strlen(path2),True);
+       SCVAL(cli->outbuf,smb_com,SMBchkpth);
+       SSVAL(cli->outbuf,smb_tid,cli->cnum);
+       cli_setup_packet(cli);
+       p = smb_buf(cli->outbuf);
+       *p++ = 4;
+       safe_strcpy(p,path2,strlen(path2));
+       unix_to_dos(p,True);
+
+       cli_send_smb(cli);
+       if (!cli_receive_smb(cli)) {
+               return False;
+       }
+
+       if (cli_error(cli, NULL, NULL, NULL)) return False;
+
+       return True;
+}
+
+
+/****************************************************************************
+start a message sequence
+****************************************************************************/
+BOOL cli_message_start(struct cli_state *cli, char *host, char *username, 
+                             int *grp)
+{
+       char *p;
+
+       /* send a SMBsendstrt command */
+       memset(cli->outbuf,'\0',smb_size);
+       set_message(cli->outbuf,0,0,True);
+       CVAL(cli->outbuf,smb_com) = SMBsendstrt;
+       SSVAL(cli->outbuf,smb_tid,cli->cnum);
+       cli_setup_packet(cli);
+       
+       p = smb_buf(cli->outbuf);
+       *p++ = 4;
+       pstrcpy(p,username);
+       unix_to_dos(p,True);
+       p = skip_string(p,1);
+       *p++ = 4;
+       pstrcpy(p,host);
+       unix_to_dos(p,True);
+       p = skip_string(p,1);
+       
+       set_message(cli->outbuf,0,PTR_DIFF(p,smb_buf(cli->outbuf)),False);
+       
+       cli_send_smb(cli);      
+       
+       if (!cli_receive_smb(cli)) {
+               return False;
+       }
+
+       if (cli_error(cli, NULL, NULL, NULL)) return False;
+
+       *grp = SVAL(cli->inbuf,smb_vwv0);
+
+       return True;
+}
+
+
+/****************************************************************************
+send a message 
+****************************************************************************/
+BOOL cli_message_text(struct cli_state *cli, char *msg, int len, int grp)
+{
+       char *p;
+
+       memset(cli->outbuf,'\0',smb_size);
+       set_message(cli->outbuf,1,len+3,True);
+       CVAL(cli->outbuf,smb_com) = SMBsendtxt;
+       SSVAL(cli->outbuf,smb_tid,cli->cnum);
+       cli_setup_packet(cli);
+
+       SSVAL(cli->outbuf,smb_vwv0,grp);
+       
+       p = smb_buf(cli->outbuf);
+       *p = 1;
+       SSVAL(p,1,len);
+       memcpy(p+3,msg,len);
+       cli_send_smb(cli);
+
+       if (!cli_receive_smb(cli)) {
+               return False;
+       }
+
+       if (cli_error(cli, NULL, NULL, NULL)) return False;
+
+       return True;
+}      
+
+/****************************************************************************
+end a message 
+****************************************************************************/
+BOOL cli_message_end(struct cli_state *cli, int grp)
+{
+       memset(cli->outbuf,'\0',smb_size);
+       set_message(cli->outbuf,1,0,True);
+       CVAL(cli->outbuf,smb_com) = SMBsendend;
+       SSVAL(cli->outbuf,smb_tid,cli->cnum);
+
+       SSVAL(cli->outbuf,smb_vwv0,grp);
+
+       cli_setup_packet(cli);
+       
+       cli_send_smb(cli);
+
+       if (!cli_receive_smb(cli)) {
+               return False;
+       }
+
+       if (cli_error(cli, NULL, NULL, NULL)) return False;
+
+       return True;
+}      
+
+
+/****************************************************************************
+query disk space
+****************************************************************************/
+BOOL cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
+{
+       memset(cli->outbuf,'\0',smb_size);
+       set_message(cli->outbuf,0,0,True);
+       CVAL(cli->outbuf,smb_com) = SMBdskattr;
+       SSVAL(cli->outbuf,smb_tid,cli->cnum);
+       cli_setup_packet(cli);
+
+       cli_send_smb(cli);
+       if (!cli_receive_smb(cli)) {
+               return False;
+       }
+
+       *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
+       *total = SVAL(cli->inbuf,smb_vwv0);
+       *avail = SVAL(cli->inbuf,smb_vwv3);
+       
+       return True;
+}
+
+/****************************************************************************
+ Attempt a NetBIOS session request, falling back to *SMBSERVER if needed.
+****************************************************************************/
+
+BOOL attempt_netbios_session_request(struct cli_state *cli, char *srchost, char *desthost,
+                                            struct in_addr *pdest_ip)
+{
+  struct nmb_name calling, called;
+
+  make_nmb_name(&calling, srchost, 0x0, scope);
+
+  /*
+   * If the called name is an IP address
+   * then use *SMBSERVER immediately.
+   */
+
+  if(is_ipaddress(desthost))
+    make_nmb_name(&called, "*SMBSERVER", 0x20, scope);
+  else
+    make_nmb_name(&called, desthost, 0x20, scope);
+
+  if (!cli_session_request(cli, &calling, &called)) {
+    struct nmb_name smbservername;
+
+    /*
+     * If the name wasn't *SMBSERVER then
+     * try with *SMBSERVER if the first name fails.
+     */
+
+    cli_shutdown(cli);
+
+    make_nmb_name(&smbservername , "*SMBSERVER", 0x20, scope);
+
+    if (!nmb_name_equal(&called, &smbservername) ||
+        !cli_initialise(cli) ||
+        !cli_connect(cli, desthost, pdest_ip) ||
+        !cli_session_request(cli, &calling, &smbservername)) {
+          DEBUG(0,("attempt_netbios_session_request: %s rejected the session for name *SMBSERVER.\n",
+                desthost));
+          cli_shutdown(cli);
+          return False;
+    }
+  }
+
+  return True;
+}