Fix signing problem in the client with transs requests
[samba.git] / source / libsmb / clitrans.c
index 3d3cd427d7608f71d5514d175f2b5cdb0480e7f0..441f5a0a8925b493ce27859fe6ee3de552fd0cce 100644 (file)
@@ -18,8 +18,6 @@
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
-#define NO_SYSLOG
-
 #include "includes.h"
 
 
@@ -40,6 +38,7 @@ BOOL cli_send_trans(struct cli_state *cli, int trans,
        char *outdata,*outparam;
        char *p;
        int pipe_name_len=0;
+       uint16 mid;
 
        this_lparam = MIN(lparam,cli->max_xmit - (500+lsetup*2)); /* hack */
        this_ldata = MIN(ldata,cli->max_xmit - (500+lsetup*2+this_lparam));
@@ -50,6 +49,13 @@ BOOL cli_send_trans(struct cli_state *cli, int trans,
        SSVAL(cli->outbuf,smb_tid, cli->cnum);
        cli_setup_packet(cli);
 
+       /*
+        * Save the mid we're using. We need this for finding
+        * signing replies.
+        */
+
+       mid = cli->mid;
+
        if (pipe_name) {
                pipe_name_len = clistr_push(cli, smb_buf(cli->outbuf), pipe_name, -1, STR_TERMINATE);
        }
@@ -84,13 +90,21 @@ BOOL cli_send_trans(struct cli_state *cli, int trans,
        cli_setup_bcc(cli, outdata+this_ldata);
 
        show_msg(cli->outbuf);
-       if (!cli_send_smb(cli))
+
+       if (!cli_send_smb(cli)) {
                return False;
+       }
+
+       /* Note we're in a trans state. Save the sequence
+        * numbers for replies. */
+       client_set_trans_sign_state_on(cli, mid);
 
        if (this_ldata < ldata || this_lparam < lparam) {
                /* receive interim response */
-               if (!cli_receive_smb(cli) || cli_is_error(cli))
+               if (!cli_receive_smb(cli) || cli_is_error(cli)) {
+                       client_set_trans_sign_state_off(cli, mid);
                        return(False);
+               }
 
                tot_data = this_ldata;
                tot_param = this_lparam;
@@ -99,6 +113,9 @@ BOOL cli_send_trans(struct cli_state *cli, int trans,
                        this_lparam = MIN(lparam-tot_param,cli->max_xmit - 500); /* hack */
                        this_ldata = MIN(ldata-tot_data,cli->max_xmit - (500+this_lparam));
 
+                       client_set_trans_sign_state_off(cli, mid);
+                       client_set_trans_sign_state_on(cli, mid);
+
                        set_message(cli->outbuf,trans==SMBtrans?8:9,0,True);
                        SCVAL(cli->outbuf,smb_com,(trans==SMBtrans ? SMBtranss : SMBtranss2));
                        
@@ -122,9 +139,20 @@ BOOL cli_send_trans(struct cli_state *cli, int trans,
                                memcpy(outdata,data+tot_data,this_ldata);
                        cli_setup_bcc(cli, outdata+this_ldata);
                        
+                       /*
+                        * Save the mid we're using. We need this for finding
+                        * signing replies.
+                        */
+                       mid = cli->mid;
+
                        show_msg(cli->outbuf);
-                       if (!cli_send_smb(cli))
+                       if (!cli_send_smb(cli)) {
+                               client_set_trans_sign_state_off(cli, mid);
                                return False;
+                       }
+
+                       /* Ensure we use the same mid for the secondaries. */
+                       cli->mid = mid;
                        
                        tot_data += this_ldata;
                        tot_param += this_lparam;
@@ -146,13 +174,13 @@ BOOL cli_receive_trans(struct cli_state *cli,int trans,
        unsigned int total_param=0;
        unsigned int this_data,this_param;
        NTSTATUS status;
-       char *tdata;
-       char *tparam;
+       BOOL ret = False;
 
        *data_len = *param_len = 0;
 
-       if (!cli_receive_smb(cli))
+       if (!cli_receive_smb(cli)) {
                return False;
+       }
 
        show_msg(cli->inbuf);
        
@@ -161,18 +189,22 @@ BOOL cli_receive_trans(struct cli_state *cli,int trans,
                DEBUG(0,("Expected %s response, got command 0x%02x\n",
                         trans==SMBtrans?"SMBtrans":"SMBtrans2", 
                         CVAL(cli->inbuf,smb_com)));
-               return(False);
+               return False;
        }
 
        /*
         * 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.
+        * be treated as such. Note that STATUS_NO_MORE_FILES is
+        * returned when a trans2 findfirst/next finishes.
         */
        status = cli_nt_error(cli);
        
-       if (NT_STATUS_IS_ERR(status))
-               return False;
+       if (NT_STATUS_IS_ERR(status) ||
+            NT_STATUS_EQUAL(status,STATUS_NO_MORE_FILES) ||
+            NT_STATUS_EQUAL(status,STATUS_INACCESSIBLE_SYSTEM_SHORTCUT)) {
+               goto out;
+       }
 
        /* parse out the lengths */
        total_data = SVAL(cli->inbuf,smb_tdrcnt);
@@ -180,23 +212,19 @@ BOOL cli_receive_trans(struct cli_state *cli,int trans,
 
        /* allocate it */
        if (total_data!=0) {
-               tdata = Realloc(*data,total_data);
-               if (!tdata) {
+               *data = (char *)SMB_REALLOC(*data,total_data);
+               if (!(*data)) {
                        DEBUG(0,("cli_receive_trans: failed to enlarge data buffer\n"));
-                       return False;
+                       goto out;
                }
-               else
-                       *data = tdata;
        }
 
        if (total_param!=0) {
-               tparam = Realloc(*param,total_param);
-               if (!tparam) {
+               *param = (char *)SMB_REALLOC(*param,total_param);
+               if (!(*param)) {
                        DEBUG(0,("cli_receive_trans: failed to enlarge param buffer\n"));
-                       return False;
+                       goto out;
                }
-               else
-                       *param = tparam;
        }
 
        for (;;)  {
@@ -206,7 +234,7 @@ BOOL cli_receive_trans(struct cli_state *cli,int trans,
                if (this_data + *data_len > total_data ||
                    this_param + *param_len > total_param) {
                        DEBUG(1,("Data overflow in cli_receive_trans\n"));
-                       return False;
+                       goto out;
                }
 
                if (this_data + *data_len < this_data ||
@@ -214,7 +242,7 @@ BOOL cli_receive_trans(struct cli_state *cli,int trans,
                                this_param + *param_len < this_param ||
                                this_param + *param_len < *param_len) {
                        DEBUG(1,("Data overflow in cli_receive_trans\n"));
-                       return False;
+                       goto out;
                }
 
                if (this_data) {
@@ -226,14 +254,14 @@ BOOL cli_receive_trans(struct cli_state *cli,int trans,
                                        data_offset_out + this_data < data_offset_out ||
                                        data_offset_out + this_data < this_data) {
                                DEBUG(1,("Data overflow in cli_receive_trans\n"));
-                               return False;
+                               goto out;
                        }
                        if (data_offset_in > cli->bufsize ||
                                        data_offset_in + this_data >  cli->bufsize ||
                                        data_offset_in + this_data < data_offset_in ||
                                        data_offset_in + this_data < this_data) {
                                DEBUG(1,("Data overflow in cli_receive_trans\n"));
-                               return False;
+                               goto out;
                        }
 
                        memcpy(*data + data_offset_out, smb_base(cli->inbuf) + data_offset_in, this_data);
@@ -247,14 +275,14 @@ BOOL cli_receive_trans(struct cli_state *cli,int trans,
                                        param_offset_out + this_param < param_offset_out ||
                                        param_offset_out + this_param < this_param) {
                                DEBUG(1,("Param overflow in cli_receive_trans\n"));
-                               return False;
+                               goto out;
                        }
                        if (param_offset_in > cli->bufsize ||
                                        param_offset_in + this_param >  cli->bufsize ||
                                        param_offset_in + this_param < param_offset_in ||
                                        param_offset_in + this_param < this_param) {
                                DEBUG(1,("Param overflow in cli_receive_trans\n"));
-                               return False;
+                               goto out;
                        }
 
                        memcpy(*param + param_offset_out, smb_base(cli->inbuf) + param_offset_in, this_param);
@@ -262,11 +290,14 @@ BOOL cli_receive_trans(struct cli_state *cli,int trans,
                *data_len += this_data;
                *param_len += this_param;
 
-               if (total_data <= *data_len && total_param <= *param_len)
+               if (total_data <= *data_len && total_param <= *param_len) {
+                       ret = True;
                        break;
+               }
                
-               if (!cli_receive_smb(cli))
-                       return False;
+               if (!cli_receive_smb(cli)) {
+                       goto out;
+               }
 
                show_msg(cli->inbuf);
                
@@ -275,10 +306,10 @@ BOOL cli_receive_trans(struct cli_state *cli,int trans,
                        DEBUG(0,("Expected %s response, got command 0x%02x\n",
                                 trans==SMBtrans?"SMBtrans":"SMBtrans2", 
                                 CVAL(cli->inbuf,smb_com)));
-                       return(False);
+                       goto out;
                }
                if (NT_STATUS_IS_ERR(cli_nt_error(cli))) {
-                       return(False);
+                       goto out;
                }
 
                /* parse out the total lengths again - they can shrink! */
@@ -287,12 +318,16 @@ BOOL cli_receive_trans(struct cli_state *cli,int trans,
                if (SVAL(cli->inbuf,smb_tprcnt) < total_param)
                        total_param = SVAL(cli->inbuf,smb_tprcnt);
                
-               if (total_data <= *data_len && total_param <= *param_len)
+               if (total_data <= *data_len && total_param <= *param_len) {
+                       ret = True;
                        break;
-               
+               }
        }
-       
-       return(True);
+
+  out:
+
+       client_set_trans_sign_state_off(cli, SVAL(cli->inbuf,smb_mid));
+       return ret;
 }
 
 /****************************************************************************
@@ -309,6 +344,7 @@ BOOL cli_send_nt_trans(struct cli_state *cli,
        unsigned int i;
        unsigned int this_ldata,this_lparam;
        unsigned int tot_data=0,tot_param=0;
+       uint16 mid;
        char *outdata,*outparam;
 
        this_lparam = MIN(lparam,cli->max_xmit - (500+lsetup*2)); /* hack */
@@ -320,6 +356,13 @@ BOOL cli_send_nt_trans(struct cli_state *cli,
        SSVAL(cli->outbuf,smb_tid, cli->cnum);
        cli_setup_packet(cli);
 
+       /*
+        * Save the mid we're using. We need this for finding
+        * signing replies.
+        */
+
+       mid = cli->mid;
+
        outparam = smb_buf(cli->outbuf)+3;
        outdata = outparam+this_lparam;
 
@@ -347,13 +390,20 @@ BOOL cli_send_nt_trans(struct cli_state *cli,
        cli_setup_bcc(cli, outdata+this_ldata);
 
        show_msg(cli->outbuf);
-       if (!cli_send_smb(cli))
+       if (!cli_send_smb(cli)) {
                return False;
+       }       
+
+       /* Note we're in a trans state. Save the sequence
+        * numbers for replies. */
+       client_set_trans_sign_state_on(cli, mid);
 
        if (this_ldata < ldata || this_lparam < lparam) {
                /* receive interim response */
-               if (!cli_receive_smb(cli) || cli_is_error(cli))
+               if (!cli_receive_smb(cli) || cli_is_error(cli)) {
+                       client_set_trans_sign_state_off(cli, mid);
                        return(False);
+               }
 
                tot_data = this_ldata;
                tot_param = this_lparam;
@@ -384,9 +434,21 @@ BOOL cli_send_nt_trans(struct cli_state *cli,
                                memcpy(outdata,data+tot_data,this_ldata);
                        cli_setup_bcc(cli, outdata+this_ldata);
                        
+                       /*
+                        * Save the mid we're using. We need this for finding
+                        * signing replies.
+                        */
+                       mid = cli->mid;
+
                        show_msg(cli->outbuf);
-                       if (!cli_send_smb(cli))
+
+                       if (!cli_send_smb(cli)) {
+                               client_set_trans_sign_state_off(cli, mid);
                                return False;
+                       }
+                       
+                       /* Ensure we use the same mid for the secondaries. */
+                       cli->mid = mid;
                        
                        tot_data += this_ldata;
                        tot_param += this_lparam;
@@ -396,11 +458,9 @@ BOOL cli_send_nt_trans(struct cli_state *cli,
        return(True);
 }
 
-
-
 /****************************************************************************
-  receive a SMB nttrans response allocating the necessary memory
-  ****************************************************************************/
+ Receive a SMB nttrans response allocating the necessary memory.
+****************************************************************************/
 
 BOOL cli_receive_nt_trans(struct cli_state *cli,
                          char **param, unsigned int *param_len,
@@ -411,13 +471,13 @@ BOOL cli_receive_nt_trans(struct cli_state *cli,
        unsigned int this_data,this_param;
        uint8 eclass;
        uint32 ecode;
-       char *tdata;
-       char *tparam;
+       BOOL ret = False;
 
        *data_len = *param_len = 0;
 
-       if (!cli_receive_smb(cli))
+       if (!cli_receive_smb(cli)) {
                return False;
+       }
 
        show_msg(cli->inbuf);
        
@@ -435,8 +495,19 @@ BOOL cli_receive_nt_trans(struct cli_state *cli,
         */
        if (cli_is_dos_error(cli)) {
                 cli_dos_error(cli, &eclass, &ecode);
-               if (cli->nt_pipe_fnum == 0 || !(eclass == ERRDOS && ecode == ERRmoredata))
-                       return(False);
+               if (!(eclass == ERRDOS && ecode == ERRmoredata)) {
+                       goto out;
+               }
+       }
+
+       /*
+        * Likewise for NT_STATUS_BUFFER_TOO_SMALL
+        */
+       if (cli_is_nt_error(cli)) {
+               if (!NT_STATUS_EQUAL(cli_nt_error(cli),
+                                    NT_STATUS_BUFFER_TOO_SMALL)) {
+                       goto out;
+               }
        }
 
        /* parse out the lengths */
@@ -445,22 +516,18 @@ BOOL cli_receive_nt_trans(struct cli_state *cli,
 
        /* allocate it */
        if (total_data) {
-               tdata = Realloc(*data,total_data);
-               if (!tdata) {
+               *data = (char *)SMB_REALLOC(*data,total_data);
+               if (!(*data)) {
                        DEBUG(0,("cli_receive_nt_trans: failed to enlarge data buffer to %d\n",total_data));
-                       return False;
-               } else {
-                       *data = tdata;
+                       goto out;
                }
        }
 
        if (total_param) {
-               tparam = Realloc(*param,total_param);
-               if (!tparam) {
+               *param = (char *)SMB_REALLOC(*param,total_param);
+               if (!(*param)) {
                        DEBUG(0,("cli_receive_nt_trans: failed to enlarge param buffer to %d\n", total_param));
-                       return False;
-               } else {
-                       *param = tparam;
+                       goto out;
                }
        }
 
@@ -471,7 +538,7 @@ BOOL cli_receive_nt_trans(struct cli_state *cli,
                if (this_data + *data_len > total_data ||
                    this_param + *param_len > total_param) {
                        DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
-                       return False;
+                       goto out;
                }
 
                if (this_data + *data_len < this_data ||
@@ -479,7 +546,7 @@ BOOL cli_receive_nt_trans(struct cli_state *cli,
                                this_param + *param_len < this_param ||
                                this_param + *param_len < *param_len) {
                        DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
-                       return False;
+                       goto out;
                }
 
                if (this_data) {
@@ -491,14 +558,14 @@ BOOL cli_receive_nt_trans(struct cli_state *cli,
                                        data_offset_out + this_data < data_offset_out ||
                                        data_offset_out + this_data < this_data) {
                                DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
-                               return False;
+                               goto out;
                        }
                        if (data_offset_in > cli->bufsize ||
                                        data_offset_in + this_data >  cli->bufsize ||
                                        data_offset_in + this_data < data_offset_in ||
                                        data_offset_in + this_data < this_data) {
                                DEBUG(1,("Data overflow in cli_receive_nt_trans\n"));
-                               return False;
+                               goto out;
                        }
 
                        memcpy(*data + data_offset_out, smb_base(cli->inbuf) + data_offset_in, this_data);
@@ -513,14 +580,14 @@ BOOL cli_receive_nt_trans(struct cli_state *cli,
                                        param_offset_out + this_param < param_offset_out ||
                                        param_offset_out + this_param < this_param) {
                                DEBUG(1,("Param overflow in cli_receive_nt_trans\n"));
-                               return False;
+                               goto out;
                        }
                        if (param_offset_in > cli->bufsize ||
                                        param_offset_in + this_param >  cli->bufsize ||
                                        param_offset_in + this_param < param_offset_in ||
                                        param_offset_in + this_param < this_param) {
                                DEBUG(1,("Param overflow in cli_receive_nt_trans\n"));
-                               return False;
+                               goto out;
                        }
 
                        memcpy(*param + param_offset_out, smb_base(cli->inbuf) + param_offset_in, this_param);
@@ -529,11 +596,14 @@ BOOL cli_receive_nt_trans(struct cli_state *cli,
                *data_len += this_data;
                *param_len += this_param;
 
-               if (total_data <= *data_len && total_param <= *param_len)
+               if (total_data <= *data_len && total_param <= *param_len) {
+                       ret = True;
                        break;
+               }
                
-               if (!cli_receive_smb(cli))
-                       return False;
+               if (!cli_receive_smb(cli)) {
+                       goto out;
+               }
 
                show_msg(cli->inbuf);
                
@@ -541,23 +611,38 @@ BOOL cli_receive_nt_trans(struct cli_state *cli,
                if (CVAL(cli->inbuf,smb_com) != SMBnttrans) {
                        DEBUG(0,("Expected SMBnttrans response, got command 0x%02x\n",
                                 CVAL(cli->inbuf,smb_com)));
-                       return(False);
+                       goto out;
                }
                if (cli_is_dos_error(cli)) {
                         cli_dos_error(cli, &eclass, &ecode);
-                       if(cli->nt_pipe_fnum == 0 || 
-                           !(eclass == ERRDOS && ecode == ERRmoredata))
-                               return(False);
+                       if(!(eclass == ERRDOS && ecode == ERRmoredata)) {
+                               goto out;
+                       }
                }
+               /*
+                * Likewise for NT_STATUS_BUFFER_TOO_SMALL
+                */
+               if (cli_is_nt_error(cli)) {
+                       if (!NT_STATUS_EQUAL(cli_nt_error(cli),
+                                            NT_STATUS_BUFFER_TOO_SMALL)) {
+                               goto out;
+                       }
+               }
+
                /* parse out the total lengths again - they can shrink! */
                if (SVAL(cli->inbuf,smb_ntr_TotalDataCount) < total_data)
                        total_data = SVAL(cli->inbuf,smb_ntr_TotalDataCount);
                if (SVAL(cli->inbuf,smb_ntr_TotalParameterCount) < total_param)
                        total_param = SVAL(cli->inbuf,smb_ntr_TotalParameterCount);
                
-               if (total_data <= *data_len && total_param <= *param_len)
+               if (total_data <= *data_len && total_param <= *param_len) {
+                       ret = True;
                        break;
+               }
        }
-       
-       return(True);
+
+  out:
+
+       client_set_trans_sign_state_off(cli, SVAL(cli->inbuf,smb_mid));
+       return ret;
 }