Fix bug #9117 - smbclient can't connect to a Windows 7 server using NTLMv2 (crypto...
[samba.git] / source3 / libsmb / cliconnect.c
index 0ff9f253ef7725f2f6498750d7861614c15821ce..7b00469ffff8f3cef60bc75bffa18031bb7e7edd 100644 (file)
@@ -19,6 +19,9 @@
 */
 
 #include "includes.h"
+#include "../libcli/auth/libcli_auth.h"
+#include "../libcli/auth/spnego.h"
+#include "smb_krb5.h"
 
 static const struct {
        int prot;
@@ -168,13 +171,15 @@ static uint32 cli_session_setup_capabilities(struct cli_state *cli)
 struct cli_session_setup_guest_state {
        struct cli_state *cli;
        uint16_t vwv[16];
+       struct iovec bytes;
 };
 
 static void cli_session_setup_guest_done(struct tevent_req *subreq);
 
-struct tevent_req *cli_session_setup_guest_send(TALLOC_CTX *mem_ctx,
-                                               struct event_context *ev,
-                                               struct cli_state *cli)
+struct tevent_req *cli_session_setup_guest_create(TALLOC_CTX *mem_ctx,
+                                                 struct event_context *ev,
+                                                 struct cli_state *cli,
+                                                 struct tevent_req **psmbreq)
 {
        struct tevent_req *req, *subreq;
        struct cli_session_setup_guest_state *state;
@@ -211,16 +216,42 @@ struct tevent_req *cli_session_setup_guest_send(TALLOC_CTX *mem_ctx,
        bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "Unix", 5, NULL);
        bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "Samba", 6, NULL);
 
-       if (tevent_req_nomem(bytes, req)) {
-               return tevent_req_post(req, ev);
+       if (bytes == NULL) {
+               TALLOC_FREE(req);
+               return NULL;
        }
 
-       subreq = cli_smb_send(state, ev, cli, SMBsesssetupX, 0, 13, vwv,
-                             talloc_get_size(bytes), bytes);
-       if (tevent_req_nomem(subreq, req)) {
-               return tevent_req_post(req, ev);
+       state->bytes.iov_base = (void *)bytes;
+       state->bytes.iov_len = talloc_get_size(bytes);
+
+       subreq = cli_smb_req_create(state, ev, cli, SMBsesssetupX, 0, 13, vwv,
+                                   1, &state->bytes);
+       if (subreq == NULL) {
+               TALLOC_FREE(req);
+               return NULL;
        }
        tevent_req_set_callback(subreq, cli_session_setup_guest_done, req);
+       *psmbreq = subreq;
+       return req;
+}
+
+struct tevent_req *cli_session_setup_guest_send(TALLOC_CTX *mem_ctx,
+                                               struct event_context *ev,
+                                               struct cli_state *cli)
+{
+       struct tevent_req *req, *subreq;
+       NTSTATUS status;
+
+       req = cli_session_setup_guest_create(mem_ctx, ev, cli, &subreq);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       status = cli_smb_req_send(subreq);
+       if (NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return tevent_req_post(req, ev);
+       }
        return req;
 }
 
@@ -433,11 +464,11 @@ static NTSTATUS cli_session_setup_nt1(struct cli_state *cli, const char *user,
                           the server's domain at this point.  The 'server name' is also
                           dodgy... 
                        */
-                       names_blob = NTLMv2_generate_names_blob(cli->called.name, workgroup);
+                       names_blob = NTLMv2_generate_names_blob(NULL, cli->called.name, workgroup);
 
-                       if (!SMBNTLMv2encrypt(user, workgroup, pass, &server_chal, 
+                       if (!SMBNTLMv2encrypt(NULL, user, workgroup, pass, &server_chal, 
                                              &names_blob,
-                                             &lm_response, &nt_response, &session_key)) {
+                                             &lm_response, &nt_response, NULL, &session_key)) {
                                data_blob_free(&names_blob);
                                data_blob_free(&server_chal);
                                return NT_STATUS_ACCESS_DENIED;
@@ -474,7 +505,7 @@ static NTSTATUS cli_session_setup_nt1(struct cli_state *cli, const char *user,
                        E_deshash(pass, session_key.data);
                        memset(&session_key.data[8], '\0', 8);
 #else
-                       SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
+                       SMBsesskeygen_ntv1(nt_hash, session_key.data);
 #endif
                }
                cli_temp_set_signing(cli);
@@ -642,9 +673,30 @@ static DATA_BLOB cli_session_setup_blob_receive(struct cli_state *cli)
 
        /* w2k with kerberos doesn't properly null terminate this field */
        len = smb_bufrem(cli->inbuf, p);
-       p += clistr_pull(cli->inbuf, cli->server_type, p, sizeof(fstring),
-                        len, 0);
+       if (p + len < cli->inbuf + cli->bufsize+SAFETY_MARGIN - 2) {
+               char *end_of_buf = p + len;
 
+               SSVAL(p, len, 0);
+               /* Now it's null terminated. */
+               p += clistr_pull(cli->inbuf, cli->server_type, p, sizeof(fstring),
+                       -1, STR_TERMINATE);
+               /*
+                * See if there's another string. If so it's the
+                * server domain (part of the 'standard' Samba
+                * server signature).
+                */
+               if (p < end_of_buf) {
+                       p += clistr_pull(cli->inbuf, cli->server_domain, p, sizeof(fstring),
+                               -1, STR_TERMINATE);
+               }
+       } else {
+               /*
+                * No room to null terminate so we can't see if there
+                * is another string (server_domain) afterwards.
+                */
+               p += clistr_pull(cli->inbuf, cli->server_type, p, sizeof(fstring),
+                                len, 0);
+       }
        return blob2;
 }
 
@@ -814,6 +866,9 @@ static NTSTATUS cli_session_setup_ntlmssp(struct cli_state *cli, const char *use
                return nt_status;
        }
        ntlmssp_want_feature(ntlmssp_state, NTLMSSP_FEATURE_SESSION_KEY);
+       if (cli->use_ccache) {
+               ntlmssp_want_feature(ntlmssp_state, NTLMSSP_FEATURE_CCACHE);
+       }
 
        if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_username(ntlmssp_state, user))) {
                return nt_status;
@@ -889,7 +944,9 @@ static NTSTATUS cli_session_setup_ntlmssp(struct cli_state *cli, const char *use
 
        if (NT_STATUS_IS_OK(nt_status)) {
 
-               fstrcpy(cli->server_domain, ntlmssp_state->server_domain);
+               if (cli->server_domain[0] == '\0') {
+                       fstrcpy(cli->server_domain, ntlmssp_state->server_domain);
+               }
                cli_set_session_key(cli, ntlmssp_state->session_key);
 
                if (cli_simple_set_signing(
@@ -950,7 +1007,8 @@ ADS_STATUS cli_session_setup_spnego(struct cli_state *cli, const char *user,
         * negprot reply. It is WRONG to depend on the principal sent in the
         * negprot reply, but right now we do it. If we don't receive one,
         * we try to best guess, then fall back to NTLM.  */
-       if (!spnego_parse_negTokenInit(blob, OIDs, &principal)) {
+       if (!spnego_parse_negTokenInit(blob, OIDs, &principal) ||
+                       OIDs[0] == NULL) {
                data_blob_free(&blob);
                return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
        }
@@ -958,7 +1016,10 @@ ADS_STATUS cli_session_setup_spnego(struct cli_state *cli, const char *user,
 
        /* make sure the server understands kerberos */
        for (i=0;OIDs[i];i++) {
-               DEBUG(3,("got OID=%s\n", OIDs[i]));
+               if (i == 0)
+                       DEBUG(3,("got OID=%s\n", OIDs[i]));
+               else
+                       DEBUGADD(3,("got OID=%s\n", OIDs[i]));
                if (strcmp(OIDs[i], OID_KERBEROS5_OLD) == 0 ||
                    strcmp(OIDs[i], OID_KERBEROS5) == 0) {
                        cli->got_kerberos_mechanism = True;
@@ -995,10 +1056,9 @@ ADS_STATUS cli_session_setup_spnego(struct cli_state *cli, const char *user,
                        }
                }
 
-               /* If we get a bad principal, try to guess it if
-                  we have a valid host NetBIOS name.
+               /* We may not be allowed to use the server-supplied SPNEGO principal, or it may not have been supplied to us
                 */
-               if (strequal(principal, ADS_IGNORE_PRINCIPAL)) {
+               if (!lp_client_use_spnego_principal() || strequal(principal, ADS_IGNORE_PRINCIPAL)) {
                        TALLOC_FREE(principal);
                }
 
@@ -1007,41 +1067,54 @@ ADS_STATUS cli_session_setup_spnego(struct cli_state *cli, const char *user,
                        !strequal(STAR_SMBSERVER,
                                cli->desthost)) {
                        char *realm = NULL;
-                       char *machine = NULL;
                        char *host = NULL;
-                       DEBUG(3,("cli_session_setup_spnego: got a "
-                               "bad server principal, trying to guess ...\n"));
+                       DEBUG(3,("cli_session_setup_spnego: using target "
+                                "hostname not SPNEGO principal\n"));
 
                        host = strchr_m(cli->desthost, '.');
-                       if (host) {
-                               machine = SMB_STRNDUP(cli->desthost,
-                                       host - cli->desthost);
-                       } else {
-                               machine = SMB_STRDUP(cli->desthost);
-                       }
-                       if (machine == NULL) {
-                               return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
-                       }
-
                        if (dest_realm) {
                                realm = SMB_STRDUP(dest_realm);
+                               if (!realm) {
+                                       return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
+                               }
                                strupper_m(realm);
                        } else {
-                               realm = kerberos_get_default_realm_from_ccache();
+                               if (host) {
+                                       /* DNS name. */
+                                       realm = kerberos_get_realm_from_hostname(cli->desthost);
+                               } else {
+                                       /* NetBIOS name - use our realm. */
+                                       realm = kerberos_get_default_realm_from_ccache();
+                               }
                        }
-                       if (realm && *realm) {
-                               principal = talloc_asprintf(NULL, "%s$@%s",
-                                                       machine, realm);
-                               if (!principal) {
-                                       SAFE_FREE(machine);
-                                       SAFE_FREE(realm);
+
+                       if (realm == NULL || *realm == '\0') {
+                               realm = SMB_STRDUP(lp_realm());
+                               if (!realm) {
                                        return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
                                }
-                               DEBUG(3,("cli_session_setup_spnego: guessed "
-                                       "server principal=%s\n",
-                                       principal ? principal : "<null>"));
+                               strupper_m(realm);
+                               DEBUG(3,("cli_session_setup_spnego: cannot "
+                                       "get realm from dest_realm %s, "
+                                       "desthost %s. Using default "
+                                       "smb.conf realm %s\n",
+                                       dest_realm ? dest_realm : "<null>",
+                                       cli->desthost,
+                                       realm));
+                       }
+
+                       principal = talloc_asprintf(talloc_tos(),
+                                                   "cifs/%s@%s",
+                                                   cli->desthost,
+                                                   realm);
+                       if (!principal) {
+                               SAFE_FREE(realm);
+                               return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
                        }
-                       SAFE_FREE(machine);
+                       DEBUG(3,("cli_session_setup_spnego: guessed "
+                               "server principal=%s\n",
+                               principal ? principal : "<null>"));
+
                        SAFE_FREE(realm);
                }
 
@@ -1105,6 +1178,7 @@ NTSTATUS cli_session_setup(struct cli_state *cli,
            (p=strchr_m(user2,*lp_winbind_separator()))) {
                *p = 0;
                user = p+1;
+               strupper_m(user2);
                workgroup = user2;
        }
 
@@ -1222,15 +1296,17 @@ bool cli_ulogoff(struct cli_state *cli)
 struct cli_tcon_andx_state {
        struct cli_state *cli;
        uint16_t vwv[4];
+       struct iovec bytes;
 };
 
 static void cli_tcon_andx_done(struct tevent_req *subreq);
 
-struct tevent_req *cli_tcon_andx_send(TALLOC_CTX *mem_ctx,
-                                     struct event_context *ev,
-                                     struct cli_state *cli,
-                                     const char *share, const char *dev,
-                                     const char *pass, int passlen)
+struct tevent_req *cli_tcon_andx_create(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       struct cli_state *cli,
+                                       const char *share, const char *dev,
+                                       const char *pass, int passlen,
+                                       struct tevent_req **psmbreq)
 {
        struct tevent_req *req, *subreq;
        struct cli_tcon_andx_state *state;
@@ -1239,6 +1315,8 @@ struct tevent_req *cli_tcon_andx_send(TALLOC_CTX *mem_ctx,
        char *tmp = NULL;
        uint8_t *bytes;
 
+       *psmbreq = NULL;
+
        req = tevent_req_create(mem_ctx, &state, struct cli_tcon_andx_state);
        if (req == NULL) {
                return NULL;
@@ -1318,8 +1396,9 @@ struct tevent_req *cli_tcon_andx_send(TALLOC_CTX *mem_ctx,
         */
        tmp = talloc_asprintf_strupper_m(talloc_tos(), "\\\\%s\\%s",
                                         cli->desthost, share);
-       if (tevent_req_nomem(tmp, req)) {
-               return tevent_req_post(req, ev);
+       if (tmp == NULL) {
+               TALLOC_FREE(req);
+               return NULL;
        }
        bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), tmp, strlen(tmp)+1,
                                   NULL);
@@ -1329,22 +1408,29 @@ struct tevent_req *cli_tcon_andx_send(TALLOC_CTX *mem_ctx,
         * Add the devicetype
         */
        tmp = talloc_strdup_upper(talloc_tos(), dev);
-       if (tevent_req_nomem(tmp, req)) {
-               return tevent_req_post(req, ev);
+       if (tmp == NULL) {
+               TALLOC_FREE(req);
+               return NULL;
        }
        bytes = smb_bytes_push_str(bytes, false, tmp, strlen(tmp)+1, NULL);
        TALLOC_FREE(tmp);
 
-       if (tevent_req_nomem(bytes, req)) {
-               return tevent_req_post(req, ev);
+       if (bytes == NULL) {
+               TALLOC_FREE(req);
+               return NULL;
        }
 
-       subreq = cli_smb_send(state, ev, cli, SMBtconX, 0, 4, vwv,
-                             talloc_get_size(bytes), bytes);
-       if (tevent_req_nomem(subreq, req)) {
-               return tevent_req_post(req, ev);
+       state->bytes.iov_base = (void *)bytes;
+       state->bytes.iov_len = talloc_get_size(bytes);
+
+       subreq = cli_smb_req_create(state, ev, cli, SMBtconX, 0, 4, vwv,
+                                   1, &state->bytes);
+       if (subreq == NULL) {
+               TALLOC_FREE(req);
+               return NULL;
        }
        tevent_req_set_callback(subreq, cli_tcon_andx_done, req);
+       *psmbreq = subreq;
        return req;
 
  access_denied:
@@ -1352,6 +1438,31 @@ struct tevent_req *cli_tcon_andx_send(TALLOC_CTX *mem_ctx,
        return tevent_req_post(req, ev);
 }
 
+struct tevent_req *cli_tcon_andx_send(TALLOC_CTX *mem_ctx,
+                                     struct event_context *ev,
+                                     struct cli_state *cli,
+                                     const char *share, const char *dev,
+                                     const char *pass, int passlen)
+{
+       struct tevent_req *req, *subreq;
+       NTSTATUS status;
+
+       req = cli_tcon_andx_create(mem_ctx, ev, cli, share, dev, pass, passlen,
+                                  &subreq);
+       if (req == NULL) {
+               return NULL;
+       }
+       if (subreq == NULL) {
+               return req;
+       }
+       status = cli_smb_req_send(subreq);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return tevent_req_post(req, ev);
+       }
+       return req;
+}
+
 static void cli_tcon_andx_done(struct tevent_req *subreq)
 {
        struct tevent_req *req = tevent_req_callback_data(
@@ -1520,6 +1631,7 @@ struct tevent_req *cli_negprot_send(TALLOC_CTX *mem_ctx,
        struct cli_negprot_state *state;
        uint8_t *bytes = NULL;
        int numprots;
+       uint16_t cnum;
 
        req = tevent_req_create(mem_ctx, &state, struct cli_negprot_state);
        if (req == NULL) {
@@ -1550,8 +1662,13 @@ struct tevent_req *cli_negprot_send(TALLOC_CTX *mem_ctx,
                }
        }
 
+       cnum = cli->cnum;
+
+       cli->cnum = 0;
        subreq = cli_smb_send(state, ev, cli, SMBnegprot, 0, 0, NULL,
                              talloc_get_size(bytes), bytes);
+       cli->cnum = cnum;
+
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -1576,6 +1693,7 @@ static void cli_negprot_done(struct tevent_req *subreq)
        status = cli_smb_recv(subreq, 1, &wct, &vwv, &num_bytes, &bytes);
        if (!NT_STATUS_IS_OK(status)) {
                TALLOC_FREE(subreq);
+               tevent_req_nterror(req, status);
                return;
        }
 
@@ -1600,6 +1718,11 @@ static void cli_negprot_done(struct tevent_req *subreq)
                struct timespec ts;
                bool negotiated_smb_signing = false;
 
+               if (wct != 0x11) {
+                       tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+                       return;
+               }
+
                /* NT protocol */
                cli->sec_mode = CVAL(vwv + 1, 0);
                cli->max_mux = SVAL(vwv + 1, 1);
@@ -1665,6 +1788,11 @@ static void cli_negprot_done(struct tevent_req *subreq)
                }
 
        } else if (cli->protocol >= PROTOCOL_LANMAN1) {
+               if (wct != 0x0D) {
+                       tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+                       return;
+               }
+
                cli->use_spnego = False;
                cli->sec_mode = SVAL(vwv + 1, 0);
                cli->max_xmit = SVAL(vwv + 2, 0);
@@ -1749,6 +1877,7 @@ bool cli_session_request(struct cli_state *cli,
 {
        char *p;
        int len = 4;
+       int namelen = 0;
        char *tmp;
 
        /* 445 doesn't have session request */
@@ -1767,8 +1896,11 @@ bool cli_session_request(struct cli_state *cli,
        }
 
        p = cli->outbuf+len;
-       memcpy(p, tmp, name_len(tmp));
-       len += name_len(tmp);
+       namelen = name_len((unsigned char *)tmp, talloc_get_size(tmp));
+       if (namelen > 0) {
+               memcpy(p, tmp, namelen);
+               len += namelen;
+       }
        TALLOC_FREE(tmp);
 
        /* and my name */
@@ -1780,8 +1912,11 @@ bool cli_session_request(struct cli_state *cli,
        }
 
        p = cli->outbuf+len;
-       memcpy(p, tmp, name_len(tmp));
-       len += name_len(tmp);
+       namelen = name_len((unsigned char *)tmp, talloc_get_size(tmp));
+       if (namelen > 0) {
+               memcpy(p, tmp, namelen);
+               len += namelen;
+       }
        TALLOC_FREE(tmp);
 
        /* send a session request (RFC 1002) */
@@ -2114,6 +2249,9 @@ again:
             cli->use_kerberos) {
                cli->fallback_after_kerberos = true;
        }
+       if (flags & CLI_FULL_CONNECTION_USE_CCACHE) {
+               cli->use_ccache = true;
+       }
 
        nt_status = cli_negprot(cli);
        if (!NT_STATUS_IS_OK(nt_status)) {