s3-libsmb Don't ever ask for machine$ principals as a target.
[samba.git] / source3 / libsmb / cliconnect.c
index 49da8edb7cfd40d5be30eab2f9f2cec918741333..65f6924a688ddcb7f306bc321226ec6525658083 100644 (file)
@@ -26,6 +26,7 @@
 #include "../libcli/auth/ntlmssp.h"
 #include "libads/kerberos_proto.h"
 #include "krb5_env.h"
+#include "async_smb.h"
 
 static const struct {
        int prot;
@@ -1228,7 +1229,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(talloc_tos(), blob, OIDs, &principal, NULL)) {
+       if (!spnego_parse_negTokenInit(talloc_tos(), blob, OIDs, &principal, NULL) ||
+                       OIDs[0] == NULL) {
                data_blob_free(&blob);
                return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
        }
@@ -1277,10 +1279,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);
                }
 
@@ -1289,24 +1290,11 @@ 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) {
-                               /* We had a '.' in the name. */
-                               machine = SMB_STRNDUP(cli->desthost,
-                                       host - cli->desthost);
-                       } else {
-                               machine = SMB_STRDUP(cli->desthost);
-                       }
-                       if (machine == NULL) {
-                               TALLOC_FREE(principal);
-                               return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
-                       }
-
                        if (dest_realm) {
                                realm = SMB_STRDUP(dest_realm);
                                strupper_m(realm);
@@ -1321,21 +1309,11 @@ ADS_STATUS cli_session_setup_spnego(struct cli_state *cli, const char *user,
                        }
 
                        if (realm && *realm) {
-                               if (host) {
-                                       /* DNS name. */
-                                       principal = talloc_asprintf(talloc_tos(),
-                                                       "cifs/%s@%s",
-                                                       cli->desthost,
-                                                       realm);
-                               } else {
-                                       /* NetBIOS name, use machine account. */
-                                       principal = talloc_asprintf(talloc_tos(),
-                                                       "%s$@%s",
-                                                       machine,
-                                                       realm);
-                               }
+                               principal = talloc_asprintf(talloc_tos(),
+                                                           "cifs/%s@%s",
+                                                           cli->desthost,
+                                                           realm);
                                if (!principal) {
-                                       SAFE_FREE(machine);
                                        SAFE_FREE(realm);
                                        return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
                                }
@@ -1343,7 +1321,6 @@ ADS_STATUS cli_session_setup_spnego(struct cli_state *cli, const char *user,
                                        "server principal=%s\n",
                                        principal ? principal : "<null>"));
                        }
-                       SAFE_FREE(machine);
                        SAFE_FREE(realm);
                }
 
@@ -1498,7 +1475,7 @@ NTSTATUS cli_session_setup(struct cli_state *cli,
 
 struct cli_ulogoff_state {
        struct cli_state *cli;
-       uint16_t vwv[2];
+       uint16_t vwv[3];
 };
 
 static void cli_ulogoff_done(struct tevent_req *subreq);
@@ -2135,6 +2112,11 @@ static void cli_negprot_done(struct tevent_req *subreq)
                        SAFE_FREE(cli->inbuf);
                        cli->outbuf = (char *)SMB_MALLOC(CLI_SAMBA_MAX_LARGE_READX_SIZE+LARGE_WRITEX_HDR_SIZE+SAFETY_MARGIN);
                        cli->inbuf = (char *)SMB_MALLOC(CLI_SAMBA_MAX_LARGE_READX_SIZE+LARGE_WRITEX_HDR_SIZE+SAFETY_MARGIN);
+                       if (!cli->outbuf || !cli->inbuf) {
+                               tevent_req_nterror(req,
+                                               NT_STATUS_NO_MEMORY);
+                               return;
+                       }
                        cli->bufsize = CLI_SAMBA_MAX_LARGE_READX_SIZE + LARGE_WRITEX_HDR_SIZE;
                }
 
@@ -2223,6 +2205,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 */
@@ -2241,8 +2224,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 */
@@ -2254,8 +2240,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) */