eclass != ERRDOS && num != ERRmoredata
[samba.git] / source / libsmb / clientgen.c
index 2d491446957e89a84034f7a15257593b6d2714f8..688764fa7320f6c938c91de89a75a2c0bb0d8dc2 100644 (file)
 
 extern int DEBUGLEVEL;
 
+
+/****************************************************************************
+recv an smb
+****************************************************************************/
+static BOOL cli_receive_smb(struct cli_state *cli)
+{
+       return client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
+}
+
+/****************************************************************************
+  send an smb to a fd and re-establish if necessary
+****************************************************************************/
+static BOOL cli_send_smb(struct cli_state *cli, BOOL show)
+{
+       size_t len;
+       size_t nwritten=0;
+       ssize_t ret;
+       BOOL reestablished=False;
+
+       if (show)
+       {
+               show_msg(cli->outbuf);
+       }
+
+       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)
+               {
+                       DEBUG(5,("cli_send_smb: write error (%s) - reconnecting\n",
+                                 strerror(errno)));
+       
+                       if (cli_reestablish_connection(cli)) {
+                               reestablished = True;
+                               nwritten=0;
+                               continue;
+                       }
+               }
+               if (ret <= 0) {
+                       DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",
+                                len,ret));
+                       return False;
+               }
+               nwritten += ret;
+       }
+       
+       return True;
+}
+
 /*****************************************************
  RAP error codes - a small start but will be extended.
 *******************************************************/
@@ -217,12 +267,11 @@ static BOOL cli_send_trans(struct cli_state *cli, int trans,
        set_message(cli->outbuf,14+lsetup,              /* 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, True);
 
        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);
                }      
@@ -258,8 +307,7 @@ static BOOL cli_send_trans(struct cli_state *cli, int trans,
                        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, True);
                        
                        tot_data += this_ldata;
                        tot_param += this_lparam;
@@ -280,14 +328,14 @@ 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 num;
        
        *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);
-       
        /* sanity check */
        if (CVAL(cli->inbuf,smb_com) != trans) {
                DEBUG(0,("Expected %s response, got command 0x%02x\n",
@@ -296,7 +344,8 @@ static BOOL cli_receive_trans(struct cli_state *cli,int trans,
                return(False);
        }
 
-       if (cli_error(cli, NULL, NULL))
+       /* DOS error "more data" is an acceptable error code */
+       if (cli_error(cli, &eclass, &num) && !(eclass == ERRDOS && num == ERRmoredata))
        {
                return(False);
        }
@@ -337,11 +386,9 @@ 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);
-               
                /* sanity check */
                if (CVAL(cli->inbuf,smb_com) != trans) {
                        DEBUG(0,("Expected %s response, got command 0x%02x\n",
@@ -349,7 +396,8 @@ static BOOL cli_receive_trans(struct cli_state *cli,int trans,
                                 CVAL(cli->inbuf,smb_com)));
                        return(False);
                }
-               if (cli_error(cli, NULL, NULL))
+               /* DOS error "more data" is an acceptable error code */
+               if (cli_error(cli, &eclass, &num) && eclass != ERRDOS && num != ERRmoredata)
                {
                        return(False);
                }
@@ -432,9 +480,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;
@@ -489,12 +537,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 +550,22 @@ 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):"";
+                     fn(sname, type, cmnt);
+             }
+      } else {
+             DEBUG(4,("NetShareEnum res=%d\n", res));
+      }      
+    } else {
+             DEBUG(4,("NetShareEnum failed\n"));
     }
   
   if (rparam)
@@ -574,7 +624,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;
                                        
@@ -614,14 +664,14 @@ 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 
 ****************************************************************************/
 BOOL cli_session_setup(struct cli_state *cli, 
                       char *user, 
@@ -698,7 +748,7 @@ 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);
@@ -715,11 +765,12 @@ 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, True);
+      if (!cli_receive_smb(cli))
+       {
+               DEBUG(10,("cli_session_setup: receive smb failed\n"));
              return False;
-
-      show_msg(cli->inbuf);
+       }
 
       if (CVAL(cli->inbuf,smb_rcls) != 0) {
              return False;
@@ -728,6 +779,22 @@ 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);
+        fstrcpy(cli->server_type, server_type);
+        fstrcpy(cli->server_domain, server_domain);
+      }
+
+      fstrcpy(cli->user_name, user);
+
       return True;
 }
 
@@ -744,8 +811,8 @@ BOOL cli_ulogoff(struct cli_state *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, True);
+        if (!cli_receive_smb(cli))
                 return False;
 
         return CVAL(cli->inbuf,smb_rcls) == 0;
@@ -764,6 +831,7 @@ BOOL cli_send_tconX(struct cli_state *cli,
 
        fstrcpy(cli->share, share);
 
+       /* in user level security don't send a password now */
        if (cli->sec_mode & 1) {
                passlen = 1;
                pass = "";
@@ -778,6 +846,7 @@ BOOL cli_send_tconX(struct cli_state *cli,
 
        slprintf(fullshare, sizeof(fullshare)-1,
                 "\\\\%s\\%s", cli->desthost, share);
+       strupper(fullshare);
 
        set_message(cli->outbuf,4,
                    2 + strlen(fullshare) + passlen + strlen(dev),True);
@@ -796,16 +865,25 @@ BOOL cli_send_tconX(struct cli_state *cli,
 
        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, True);
+       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 */
@@ -828,8 +906,8 @@ BOOL cli_tdis(struct cli_state *cli)
        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, True);
+       if (!cli_receive_smb(cli))
                return False;
        
        return CVAL(cli->inbuf,smb_rcls) == 0;
@@ -860,8 +938,8 @@ BOOL cli_rename(struct cli_state *cli, char *fname_src, char *fname_dst)
         *p++ = 4;
         pstrcpy(p,fname_dst);
 
-        send_smb(cli->fd,cli->outbuf);
-        if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+        cli_send_smb(cli, True);
+        if (!cli_receive_smb(cli)) {
                 return False;
         }
 
@@ -894,8 +972,8 @@ BOOL cli_unlink(struct cli_state *cli, char *fname)
        *p++ = 4;      
        pstrcpy(p,fname);
 
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+       cli_send_smb(cli, True);
+       if (!cli_receive_smb(cli)) {
                return False;
        }
 
@@ -926,8 +1004,8 @@ BOOL cli_mkdir(struct cli_state *cli, char *dname)
        *p++ = 4;      
        pstrcpy(p,dname);
 
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+       cli_send_smb(cli, True);
+       if (!cli_receive_smb(cli)) {
                return False;
        }
 
@@ -958,8 +1036,8 @@ BOOL cli_rmdir(struct cli_state *cli, char *dname)
        *p++ = 4;      
        pstrcpy(p,dname);
 
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+       cli_send_smb(cli, True);
+       if (!cli_receive_smb(cli)) {
                return False;
        }
 
@@ -972,6 +1050,50 @@ BOOL cli_rmdir(struct cli_state *cli, char *dname)
 
 
 
+/****************************************************************************
+open a file
+****************************************************************************/
+int cli_nt_create(struct cli_state *cli, char *fname)
+{
+       char *p;
+
+       bzero(cli->outbuf,smb_size);
+       bzero(cli->inbuf,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);
+       p = skip_string(p,1);
+
+       cli_send_smb(cli, True);
+       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
 ****************************************************************************/
@@ -983,7 +1105,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;
        }
 
@@ -1030,8 +1152,8 @@ int cli_open(struct cli_state *cli, char *fname, int flags, int share_mode)
        pstrcpy(p,fname);
        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, True);
+       if (!cli_receive_smb(cli)) {
                return -1;
        }
 
@@ -1062,8 +1184,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, True);
+       if (!cli_receive_smb(cli)) {
                return False;
        }
 
@@ -1081,6 +1203,7 @@ 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);
@@ -1102,12 +1225,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, True);
 
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+        cli->timeout = (timeout == -1) ? 0x7FFFFFFF : 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;
        }
@@ -1143,8 +1271,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, True);
+       if (!cli_receive_smb(cli)) {
                return False;
        }
 
@@ -1179,7 +1307,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, True);
 }
 
 /****************************************************************************
@@ -1196,6 +1324,8 @@ size_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t
        int mid;
        int blocks = (size + (block-1)) / block;
 
+       if (size == 0) return 0;
+
        while (received < blocks) {
                int size2;
 
@@ -1205,7 +1335,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;
                }
 
@@ -1241,7 +1371,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++;
        }
        
@@ -1252,7 +1382,7 @@ 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;
@@ -1268,8 +1398,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));
@@ -1279,13 +1413,19 @@ 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);
+       cli_send_smb(cli, True);
 }
 
 /****************************************************************************
   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;
@@ -1295,17 +1435,21 @@ size_t cli_write(struct cli_state *cli, int fnum, char *buf, off_t offset, size_
        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,
+                       cli_issue_write(cli, fnum, offset+issued*block,
+                                       write_mode,
+                                       buf + issued*block,
                                        size1, issued);
                        issued++;
                }
 
-               if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+               if (!cli_receive_smb(cli)) {
                        return total;
                }
 
@@ -1331,7 +1475,7 @@ size_t cli_write(struct cli_state *cli, int fnum, char *buf, off_t offset, size_
        }
 
        while (received < issued) {
-               client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
+               cli_receive_smb(cli);
                received++;
        }
        
@@ -1343,7 +1487,7 @@ 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);
@@ -1357,8 +1501,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, True);
+       if (!cli_receive_smb(cli)) {
                return False;
        }
        
@@ -1394,7 +1538,7 @@ 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;
 
@@ -1411,8 +1555,8 @@ BOOL cli_getatr(struct cli_state *cli, char *fname,
        *p = 4;
        pstrcpy(p+1, fname);
 
-       send_smb(cli->fd,cli->outbuf);
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+       cli_send_smb(cli, True);
+       if (!cli_receive_smb(cli)) {
                return False;
        }
        
@@ -1440,7 +1584,7 @@ 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;
 
@@ -1462,8 +1606,8 @@ BOOL cli_setatr(struct cli_state *cli, char *fname, int attr, time_t t)
        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, True);
+       if (!cli_receive_smb(cli)) {
                return False;
        }
        
@@ -1479,7 +1623,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;
@@ -1552,9 +1696,10 @@ BOOL cli_qpathinfo(struct cli_state *cli, const char *fname,
 /****************************************************************************
 send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level
 ****************************************************************************/
-BOOL cli_qpathinfo2(struct cli_state *cli, char *fname, 
+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, uint32 *size)
+                   time_t *w_time, size_t *size, uint16 *mode,
+                   SMB_INO_T *ino)
 {
        int data_len = 0;
        int param_len = 0;
@@ -1600,9 +1745,15 @@ BOOL cli_qpathinfo2(struct cli_state *cli, 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);
        }
+       if (ino) {
+               *ino = IVAL(rdata, 64);
+       }
 
        if (rdata) free(rdata);
        if (rparam) free(rparam);
@@ -1614,8 +1765,9 @@ BOOL cli_qpathinfo2(struct cli_state *cli, 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;
@@ -1623,11 +1775,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 */
@@ -1645,24 +1801,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, 40);
+       }
+       if (ino) {
+               *ino = IVAL(rdata, 64);
        }
 
        if (rdata) free(rdata);
@@ -1781,7 +1943,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. */
@@ -1932,7 +2095,7 @@ int cli_list(struct cli_state *cli,char *Mask,int attribute,void (*fn)(file_info
 
        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 */
@@ -1945,8 +2108,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];
@@ -1957,7 +2120,6 @@ 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;
@@ -1967,11 +2129,6 @@ BOOL cli_oem_change_password(struct cli_state *cli, char *user, char *new_passwo
     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");
@@ -1985,26 +2142,19 @@ 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);
   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);
+       if (!make_oem_passwd_hash( data, new_password, old_pw_hash, False))
+       {
+               return False;
+       }
 
   /* 
    * Now place the old password hash in the data.
@@ -2019,13 +2169,14 @@ BOOL cli_oem_change_password(struct cli_state *cli, char *user, char *new_passwo
 
   data_len = 532;
     
-  if (cli_send_trans(cli,SMBtrans,
+  if (!cli_send_trans(cli,SMBtrans,
                     PIPE_LANMAN,strlen(PIPE_LANMAN),      /* name, length */
                     0,0,                                  /* fid, flags */
                     NULL,0,0,                             /* setup, length, max */
                     param,param_len,2,                    /* param, length, max */
                     data,data_len,0                       /* data, length, max */
-                   ) == False) {
+                   ))
+  {
     DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
               user ));
     return False;
@@ -2079,11 +2230,11 @@ 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, True);
+       if (!cli_receive_smb(cli))
+       {
                return False;
-
-       show_msg(cli->inbuf);
+       }
 
        if (CVAL(cli->inbuf,smb_rcls) != 0 || 
            ((int)SVAL(cli->inbuf,smb_vwv0) >= numprots)) {
@@ -2161,10 +2312,10 @@ BOOL cli_session_request(struct cli_state *cli,
 retry:
 #endif /* WITH_SSL */
 
-       send_smb(cli->fd,cli->outbuf);
+       cli_send_smb(cli, False);
        DEBUG(5,("Sent session request\n"));
 
-       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
+       if (!cli_receive_smb(cli))
                return False;
 
 #ifdef WITH_SSL
@@ -2188,23 +2339,24 @@ 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;
        }
 
 
-       cli->fd = open_socket_out(SOCK_STREAM, &dest_ip, 139, cli->timeout);
+       cli->fd = open_socket_out(SOCK_STREAM, &cli->dest_ip, 
+                                 139, cli->timeout);
        if (cli->fd == -1)
                return False;
 
@@ -2215,12 +2367,21 @@ 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);
+       }
+
+       if (cli->initialised) {
+               cli_shutdown(cli);
+       }
+
+       ZERO_STRUCTP(cli);
 
-       memset(cli, 0, sizeof(*cli));
        cli->fd = -1;
        cli->cnum = -1;
        cli->pid = (uint16)getpid();
@@ -2233,9 +2394,13 @@ BOOL cli_initialise(struct cli_state *cli)
        cli->outbuf = (char *)malloc(cli->bufsize);
        cli->inbuf = (char *)malloc(cli->bufsize);
        if (!cli->outbuf || !cli->inbuf)
-      return False;
+       {
+               return False;
+       }
+
        cli->initialised = 1;
-       return True;
+
+       return cli;
 }
 
 /****************************************************************************
@@ -2243,16 +2408,23 @@ shutdown a client structure
 ****************************************************************************/
 void cli_shutdown(struct cli_state *cli)
 {
+       DEBUG(10,("cli_shutdown\n"));
        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);
 #endif /* WITH_SSL */
        if (cli->fd != -1) 
-      close(cli->fd);
+       {
+               close(cli->fd);
+       }
        memset(cli, 0, sizeof(*cli));
 }
 
@@ -2267,10 +2439,18 @@ void cli_shutdown(struct cli_state *cli)
 ****************************************************************************/
 int cli_error(struct cli_state *cli, uint8 *eclass, uint32 *num)
 {
-       int  flgs2 = SVAL(cli->inbuf,smb_flg2);
+       int  flgs2;
        char rcls;
        int code;
 
+       if (!cli->initialised)
+       {
+               DEBUG(0,("cli_error: client state uninitialised!\n"));
+               return EINVAL;
+       }
+
+       flgs2 = SVAL(cli->inbuf,smb_flg2);
+
        if (eclass) *eclass = 0;
        if (num   ) *num = 0;
 
@@ -2291,6 +2471,7 @@ int cli_error(struct cli_state *cli, uint8 *eclass, uint32 *num)
                case NT_STATUS_OBJECT_NAME_NOT_FOUND: return ENOENT;
                case NT_STATUS_SHARING_VIOLATION: return EBUSY;
                case NT_STATUS_OBJECT_PATH_INVALID: return ENOTDIR;
+               case NT_STATUS_OBJECT_NAME_COLLISION: return EEXIST;
                }
 
                /* for all other cases - a default code */
@@ -2309,11 +2490,19 @@ int cli_error(struct cli_state *cli, uint8 *eclass, uint32 *num)
                case ERRbadfile: return ENOENT;
                case ERRbadpath: return ENOTDIR;
                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 */
@@ -2339,17 +2528,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)
        {
@@ -2369,16 +2558,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;
 }
 
 /****************************************************************************
@@ -2391,7 +2590,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 */
@@ -2406,7 +2605,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;
                }
        }
@@ -2429,21 +2628,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)
@@ -2453,7 +2664,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;
                        }
                }
@@ -2476,7 +2689,9 @@ BOOL cli_establish_connection(struct cli_state *cli,
                {
                        DEBUG(1,("failed session setup\n"));
                        if (do_shutdown)
-              cli_shutdown(cli);
+                       {
+                               cli_shutdown(cli);
+                       }
                        return False;
                }
 
@@ -2487,19 +2702,113 @@ 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;
                        }
                }
        }
 
        if (do_shutdown)
-      cli_shutdown(cli);
+       {
+               cli_shutdown(cli);
+       }
 
        return True;
 }
 
 
+/****************************************************************************
+ connect to one of multiple servers: don't care which
+****************************************************************************/
+BOOL cli_connect_serverlist(struct cli_state *cli, char *p)
+{
+       extern pstring global_myname;
+       extern pstring scope;
+       fstring remote_machine;
+       struct in_addr dest_ip;
+       struct nmb_name calling, called, stupid_smbserver_called;
+       BOOL connected_ok = False;
+
+       /*
+       * Treat each name in the 'password server =' line as a potential
+       * PDC/BDC. Contact each in turn and try and authenticate.
+       */
+
+       while(p && next_token(&p,remote_machine,LIST_SEP,sizeof(remote_machine)))
+       {
+               ZERO_STRUCTP(cli);
+
+               if (!cli_initialise(cli))
+               {
+                       DEBUG(0,("cli_connect_serverlist: unable to initialize client connection.\n"));
+                       return False;
+               }
+
+               standard_sub_basic(remote_machine);
+               strupper(remote_machine);
+
+               if (!resolve_name( remote_machine, &dest_ip, 0x20))
+               {
+                       DEBUG(1,("cli_connect_serverlist: Can't resolve address for %s\n", remote_machine));
+                       continue;
+               }   
+
+               if (ismyip(dest_ip))
+               {
+                       DEBUG(1,("cli_connect_serverlist: Password server loop - not using password server %s\n", remote_machine));
+                       continue;
+               }
+
+               make_nmb_name(&calling, global_myname , 0x0 , scope);
+               make_nmb_name(&called , remote_machine, 0x20, scope);
+               /* stupid microsoft destruction of the ability of netbios
+                * to provide multiple netbios servers on one host.
+                */
+               make_nmb_name(&stupid_smbserver_called , "*SMBSERVER", 0x20, scope);
+
+               pwd_set_nullpwd(&cli->pwd);
+
+               if (!cli_establish_connection(cli, remote_machine, &dest_ip,
+                                             &calling, &called,
+                                             "IPC$", "IPC", 
+                                             False, True) &&
+                   !cli_establish_connection(cli, remote_machine, &dest_ip,
+                                             &calling, &stupid_smbserver_called,
+                                             "IPC$", "IPC", 
+                                             False, True))
+               {
+                       cli_shutdown(cli);
+                       continue;
+               }      
+
+               if (cli->protocol < PROTOCOL_LANMAN2 ||
+                   !IS_BITS_SET_ALL(cli->sec_mode, 1))
+               {
+                       DEBUG(1,("cli_connect_serverlist: machine %s isn't in user level security mode\n",
+                                 remote_machine));
+                       cli_shutdown(cli);
+                       continue;
+               }
+
+               /*
+                * We have an anonymous connection to IPC$.
+                */
+
+               connected_ok = True;
+               break;
+       }
+
+       if (!connected_ok)
+       {
+               DEBUG(0,("cli_connect_serverlist: Domain password server not available.\n"));
+               cli_shutdown(cli);
+       }
+
+       return connected_ok;
+}
+
 /****************************************************************************
   cancel a print job
   ****************************************************************************/
@@ -2608,3 +2917,154 @@ 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)
+{
+       fstring path2;
+       char *p;
+       
+       fstrcpy(path2,path);
+       trim_string(path2,NULL,"\\");
+       if (!*path2) *path2 = '\\';
+       
+       bzero(cli->outbuf,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;
+       fstrcpy(p,path2);
+
+       cli_send_smb(cli, True);
+       if (!cli_receive_smb(cli)) {
+               return False;
+       }
+
+       if (cli_error(cli, 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 */
+       bzero(cli->outbuf,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);
+       p = skip_string(p,1);
+       *p++ = 4;
+       pstrcpy(p,host);
+       p = skip_string(p,1);
+       
+       set_message(cli->outbuf,0,PTR_DIFF(p,smb_buf(cli->outbuf)),False);
+       
+       cli_send_smb(cli, True);        
+       
+       if (!cli_receive_smb(cli)) {
+               return False;
+       }
+
+       if (cli_error(cli, 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;
+
+       bzero(cli->outbuf,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, True);
+
+       if (!cli_receive_smb(cli)) {
+               return False;
+       }
+
+       if (cli_error(cli, NULL, NULL)) return False;
+
+       return True;
+}      
+
+/****************************************************************************
+end a message 
+****************************************************************************/
+BOOL cli_message_end(struct cli_state *cli, int grp)
+{
+       bzero(cli->outbuf,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, True);
+
+       if (!cli_receive_smb(cli)) {
+               return False;
+       }
+
+       if (cli_error(cli, NULL, NULL)) return False;
+
+       return True;
+}      
+
+
+/****************************************************************************
+query disk space
+****************************************************************************/
+BOOL cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
+{
+       bzero(cli->outbuf,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, True);
+       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;
+}