[GLUE] Rsync SAMBA_3_2_0 SVN r25598 in order to create the v3-2-test branch.
[samba.git] / source / libsmb / cliconnect.c
index 81188b004ffc7a729b3e736f966be8fb04975212..78cc63de50f8d2dcfdfa81275432378d7a0f3851 100644 (file)
@@ -6,7 +6,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -15,8 +15,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
@@ -33,6 +32,7 @@ static const struct {
        {PROTOCOL_LANMAN1,"LANMAN1.0"},
        {PROTOCOL_LANMAN2,"LM1.2X002"},
        {PROTOCOL_LANMAN2,"DOS LANMAN2.1"},
+       {PROTOCOL_LANMAN2,"LANMAN2.1"},
        {PROTOCOL_LANMAN2,"Samba"},
        {PROTOCOL_NT1,"NT LANMAN 1.0"},
        {PROTOCOL_NT1,"NT LM 0.12"},
@@ -60,8 +60,8 @@ static NTSTATUS cli_session_setup_lanman2(struct cli_state *cli,
                                          const char *pass, size_t passlen,
                                          const char *workgroup)
 {
-       DATA_BLOB session_key = data_blob(NULL, 0);
-       DATA_BLOB lm_response = data_blob(NULL, 0);
+       DATA_BLOB session_key = data_blob_null;
+       DATA_BLOB lm_response = data_blob_null;
        fstring pword;
        char *p;
 
@@ -298,9 +298,9 @@ static NTSTATUS cli_session_setup_nt1(struct cli_state *cli, const char *user,
                                      const char *workgroup)
 {
        uint32 capabilities = cli_session_setup_capabilities(cli);
-       DATA_BLOB lm_response = data_blob(NULL, 0);
-       DATA_BLOB nt_response = data_blob(NULL, 0);
-       DATA_BLOB session_key = data_blob(NULL, 0);
+       DATA_BLOB lm_response = data_blob_null;
+       DATA_BLOB nt_response = data_blob_null;
+       DATA_BLOB session_key = data_blob_null;
        NTSTATUS result;
        char *p;
 
@@ -333,7 +333,7 @@ static NTSTATUS cli_session_setup_nt1(struct cli_state *cli, const char *user,
                        E_md4hash(pass, nt_hash);
 
 #ifdef LANMAN_ONLY
-                       nt_response = data_blob(NULL, 0);
+                       nt_response = data_blob_null;
 #else
                        nt_response = data_blob(NULL, 24);
                        SMBNTencrypt(pass,cli->secblob.data,nt_response.data);
@@ -484,7 +484,7 @@ static BOOL cli_session_setup_blob_send(struct cli_state *cli, DATA_BLOB blob)
 
 static DATA_BLOB cli_session_setup_blob_receive(struct cli_state *cli)
 {
-       DATA_BLOB blob2 = data_blob(NULL, 0);
+       DATA_BLOB blob2 = data_blob_null;
        char *p;
        size_t len;
 
@@ -516,19 +516,79 @@ static DATA_BLOB cli_session_setup_blob_receive(struct cli_state *cli)
 }
 
 #ifdef HAVE_KRB5
-
 /****************************************************************************
  Send a extended security session setup blob, returning a reply blob.
 ****************************************************************************/
 
-static DATA_BLOB cli_session_setup_blob(struct cli_state *cli, DATA_BLOB blob)
+/* The following is calculated from :
+ * (smb_size-4) = 35
+ * (smb_wcnt * 2) = 24 (smb_wcnt == 12 in cli_session_setup_blob_send() )
+ * (strlen("Unix") + 1 + strlen("Samba") + 1) * 2 = 22 (unicode strings at
+ * end of packet.
+ */
+
+#define BASE_SESSSETUP_BLOB_PACKET_SIZE (35 + 24 + 22)
+
+static BOOL cli_session_setup_blob(struct cli_state *cli, DATA_BLOB blob, DATA_BLOB session_key_krb5)
 {
-       DATA_BLOB blob2 = data_blob(NULL, 0);
-       if (!cli_session_setup_blob_send(cli, blob)) {
-               return blob2;
+       int32 remaining = blob.length;
+       int32 cur = 0;
+       DATA_BLOB send_blob = data_blob_null;
+       int32 max_blob_size = 0;
+       DATA_BLOB receive_blob = data_blob_null;
+
+       if (cli->max_xmit < BASE_SESSSETUP_BLOB_PACKET_SIZE + 1) {
+               DEBUG(0,("cli_session_setup_blob: cli->max_xmit too small "
+                       "(was %u, need minimum %u)\n",
+                       (unsigned int)cli->max_xmit,
+                       BASE_SESSSETUP_BLOB_PACKET_SIZE));
+               cli_set_nt_error(cli, NT_STATUS_INVALID_PARAMETER);
+               return False;
        }
-               
-       return cli_session_setup_blob_receive(cli);
+
+       max_blob_size = cli->max_xmit - BASE_SESSSETUP_BLOB_PACKET_SIZE;
+
+       while ( remaining > 0) {
+               if (remaining >= max_blob_size) {
+                       send_blob.length = max_blob_size;
+                       remaining -= max_blob_size;
+               } else {
+                       DATA_BLOB null_blob = data_blob_null;
+
+                       send_blob.length = remaining; 
+                        remaining = 0;
+
+                       /* This is the last packet in the sequence - turn signing on. */
+                       cli_simple_set_signing(cli, session_key_krb5, null_blob); 
+               }
+
+               send_blob.data =  &blob.data[cur];
+               cur += send_blob.length;
+
+               DEBUG(10, ("cli_session_setup_blob: Remaining (%u) sending (%u) current (%u)\n", 
+                       (unsigned int)remaining,
+                       (unsigned int)send_blob.length,
+                       (unsigned int)cur ));
+
+               if (!cli_session_setup_blob_send(cli, send_blob)) {
+                       DEBUG(0, ("cli_session_setup_blob: send failed\n"));
+                       return False;
+               }
+
+               receive_blob = cli_session_setup_blob_receive(cli);
+               data_blob_free(&receive_blob);
+
+               if (cli_is_error(cli) &&
+                               !NT_STATUS_EQUAL( cli_get_nt_error(cli), 
+                                       NT_STATUS_MORE_PROCESSING_REQUIRED)) {
+                       DEBUG(0, ("cli_session_setup_blob: recieve failed (%s)\n",
+                               nt_errstr(cli_get_nt_error(cli)) ));
+                       cli->vuid = 0;
+                       return False;
+               }
+       }
+
+       return True;
 }
 
 /****************************************************************************
@@ -545,18 +605,18 @@ static void use_in_memory_ccache(void) {
 
 static ADS_STATUS cli_session_setup_kerberos(struct cli_state *cli, const char *principal, const char *workgroup)
 {
-       DATA_BLOB blob2, negTokenTarg;
+       DATA_BLOB negTokenTarg;
        DATA_BLOB session_key_krb5;
-       DATA_BLOB null_blob = data_blob(NULL, 0);
        int rc;
 
        DEBUG(2,("Doing kerberos session setup\n"));
 
        /* generate the encapsulated kerberos5 ticket */
-       rc = spnego_gen_negTokenTarg(principal, 0, &negTokenTarg, &session_key_krb5, 0);
+       rc = spnego_gen_negTokenTarg(principal, 0, &negTokenTarg, &session_key_krb5, 0, NULL);
 
        if (rc) {
-               DEBUG(1, ("spnego_gen_negTokenTarg failed: %s\n", error_message(rc)));
+               DEBUG(1, ("cli_session_setup_kerberos: spnego_gen_negTokenTarg failed: %s\n",
+                       error_message(rc)));
                return ADS_ERROR_KRB5(rc);
        }
 
@@ -564,12 +624,11 @@ static ADS_STATUS cli_session_setup_kerberos(struct cli_state *cli, const char *
        file_save("negTokenTarg.dat", negTokenTarg.data, negTokenTarg.length);
 #endif
 
-       cli_simple_set_signing(cli, session_key_krb5, null_blob); 
-                       
-       blob2 = cli_session_setup_blob(cli, negTokenTarg);
-
-       /* we don't need this blob for kerberos */
-       data_blob_free(&blob2);
+       if (!cli_session_setup_blob(cli, negTokenTarg, session_key_krb5)) {
+               data_blob_free(&negTokenTarg);
+               data_blob_free(&session_key_krb5);
+               ADS_ERROR_NT(cli_nt_error(cli));
+       }
 
        cli_set_session_key(cli, session_key_krb5);
 
@@ -597,9 +656,9 @@ static NTSTATUS cli_session_setup_ntlmssp(struct cli_state *cli, const char *use
        NTSTATUS nt_status;
        int turn = 1;
        DATA_BLOB msg1;
-       DATA_BLOB blob = data_blob(NULL, 0);
-       DATA_BLOB blob_in = data_blob(NULL, 0);
-       DATA_BLOB blob_out = data_blob(NULL, 0);
+       DATA_BLOB blob = data_blob_null;
+       DATA_BLOB blob_in = data_blob_null;
+       DATA_BLOB blob_out = data_blob_null;
 
        cli_temp_set_signing(cli);
 
@@ -636,8 +695,6 @@ static NTSTATUS cli_session_setup_ntlmssp(struct cli_state *cli, const char *use
                                DEBUG(3, ("Failed to send NTLMSSP/SPNEGO blob to server!\n"));
                                nt_status = NT_STATUS_UNSUCCESSFUL;
                        } else {
-                               data_blob_free(&msg1);
-                               
                                blob = cli_session_setup_blob_receive(cli);
                                
                                nt_status = cli_nt_error(cli);
@@ -649,6 +706,7 @@ static NTSTATUS cli_session_setup_ntlmssp(struct cli_state *cli, const char *use
                                        }
                                }
                        }
+                       data_blob_free(&msg1);
                }
                
                if (!blob.length) {
@@ -657,7 +715,7 @@ static NTSTATUS cli_session_setup_ntlmssp(struct cli_state *cli, const char *use
                        }
                } else if ((turn == 1) && 
                           NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
-                       DATA_BLOB tmp_blob = data_blob(NULL, 0);
+                       DATA_BLOB tmp_blob = data_blob_null;
                        /* the server might give us back two challenges */
                        if (!spnego_parse_challenge(blob, &blob_in, 
                                                    &tmp_blob)) {
@@ -666,7 +724,7 @@ static NTSTATUS cli_session_setup_ntlmssp(struct cli_state *cli, const char *use
                        }
                        data_blob_free(&tmp_blob);
                } else {
-                       if (!spnego_parse_auth_response(blob, nt_status, 
+                       if (!spnego_parse_auth_response(blob, nt_status, OID_NTLMSSP, 
                                                        &blob_in)) {
                                DEBUG(3,("Failed to parse auth response\n"));
                                if (NT_STATUS_IS_OK(nt_status) 
@@ -679,11 +737,13 @@ static NTSTATUS cli_session_setup_ntlmssp(struct cli_state *cli, const char *use
                turn++;
        } while (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED));
 
+       data_blob_free(&blob_in);
+
        if (NT_STATUS_IS_OK(nt_status)) {
 
                DATA_BLOB key = data_blob(ntlmssp_state->session_key.data,
                                          ntlmssp_state->session_key.length);
-               DATA_BLOB null_blob = data_blob(NULL, 0);
+               DATA_BLOB null_blob = data_blob_null;
                BOOL res;
 
                fstrcpy(cli->server_domain, ntlmssp_state->server_domain);
@@ -710,6 +770,9 @@ static NTSTATUS cli_session_setup_ntlmssp(struct cli_state *cli, const char *use
 
        ntlmssp_end(&ntlmssp_state);
 
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               cli->vuid = 0;
+       }
        return nt_status;
 }
 
@@ -1007,6 +1070,7 @@ BOOL cli_send_tconX(struct cli_state *cli,
        cli_setup_packet(cli);
 
        SSVAL(cli->outbuf,smb_vwv0,0xFF);
+       SSVAL(cli->outbuf,smb_vwv2,TCONX_FLAG_EXTENDED_RESPONSE);
        SSVAL(cli->outbuf,smb_vwv3,passlen);
 
        p = smb_buf(cli->outbuf);
@@ -1341,7 +1405,7 @@ BOOL cli_session_request(struct cli_state *cli,
  Open the client sockets.
 ****************************************************************************/
 
-BOOL cli_connect(struct cli_state *cli, const char *host, struct in_addr *ip)
+NTSTATUS cli_connect(struct cli_state *cli, const char *host, struct in_addr *ip)
 {
        int name_type = 0x20;
        char *p;
@@ -1359,7 +1423,7 @@ BOOL cli_connect(struct cli_state *cli, const char *host, struct in_addr *ip)
        
        if (!ip || is_zero_ip(*ip)) {
                 if (!resolve_name(cli->desthost, &cli->dest_ip, name_type)) {
-                        return False;
+                       return NT_STATUS_BAD_NETWORK_NAME;
                 }
                if (ip) *ip = cli->dest_ip;
        } else {
@@ -1384,12 +1448,12 @@ BOOL cli_connect(struct cli_state *cli, const char *host, struct in_addr *ip)
        if (cli->fd == -1) {
                DEBUG(1,("Error connecting to %s (%s)\n",
                         ip?inet_ntoa(*ip):host,strerror(errno)));
-               return False;
+               return map_nt_error_from_unix(errno);
        }
 
        set_socket_options(cli->fd,user_socket_options);
 
-       return True;
+       return NT_STATUS_OK;
 }
 
 /**
@@ -1443,15 +1507,12 @@ again:
 
        DEBUG(3,("Connecting to host=%s\n", dest_host));
        
-       if (!cli_connect(cli, dest_host, &ip)) {
-               DEBUG(1,("cli_start_connection: failed to connect to %s (%s)\n",
-                        nmb_namestr(&called), inet_ntoa(ip)));
+       nt_status = cli_connect(cli, dest_host, &ip);
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               DEBUG(1,("cli_start_connection: failed to connect to %s (%s). Error %s\n",
+                        nmb_namestr(&called), inet_ntoa(ip), nt_errstr(nt_status) ));
                cli_shutdown(cli);
-               if (is_zero_ip(ip)) {
-                       return NT_STATUS_BAD_NETWORK_NAME;
-               } else {
-                       return NT_STATUS_CONNECTION_REFUSED;
-               }
+               return nt_status;
        }
 
        if (retry)
@@ -1596,6 +1657,7 @@ BOOL attempt_netbios_session_request(struct cli_state **ppcli, const char *srcho
        }
 
        if (!cli_session_request(*ppcli, &calling, &called)) {
+               NTSTATUS status;
                struct nmb_name smbservername;
 
                make_nmb_name(&smbservername , "*SMBSERVER", 0x20);
@@ -1625,7 +1687,8 @@ with error %s.\n", desthost, cli_errstr(*ppcli) ));
                        return False;
                }
 
-               if (!cli_connect(*ppcli, desthost, pdest_ip) ||
+               status = cli_connect(*ppcli, desthost, pdest_ip);
+               if (!NT_STATUS_IS_OK(status) ||
                                !cli_session_request(*ppcli, &calling, &smbservername)) {
                        DEBUG(0,("attempt_netbios_session_request: %s rejected the session for \
 name *SMBSERVER with error %s\n", desthost, cli_errstr(*ppcli) ));
@@ -1785,7 +1848,8 @@ struct cli_state *get_ipc_connect_master_ip_bcast(pstring workgroup, struct user
 
         /* Go looking for workgroups by broadcasting on the local network */ 
 
-        if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
+        if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
+                                               &count))) {
                 DEBUG(99, ("No master browsers responded\n"));
                 return False;
         }