Fix bug #9117 - smbclient can't connect to a Windows 7 server using NTLMv2 (crypto...
[samba.git] / source3 / libsmb / cliconnect.c
index ffe2960967b040436ec336b53b6909419760af90..7b00469ffff8f3cef60bc75bffa18031bb7e7edd 100644 (file)
@@ -20,6 +20,8 @@
 
 #include "includes.h"
 #include "../libcli/auth/libcli_auth.h"
+#include "../libcli/auth/spnego.h"
+#include "smb_krb5.h"
 
 static const struct {
        int prot;
@@ -169,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;
@@ -212,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;
 }
 
@@ -643,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;
 }
 
@@ -815,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;
@@ -890,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(
@@ -951,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);
        }
@@ -959,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;
@@ -996,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);
                }
 
@@ -1008,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);
                }
 
@@ -1106,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;
        }
 
@@ -1223,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;
@@ -1240,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;
@@ -1319,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);
@@ -1330,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:
@@ -1353,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(
@@ -1521,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) {
@@ -1551,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);
        }
@@ -1577,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;
        }
 
@@ -1601,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);
@@ -1666,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);
@@ -1750,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 */
@@ -1768,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 */
@@ -1781,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) */
@@ -2115,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)) {