Do not panic unnecessarily
[ira/wip.git] / source3 / rpc_client / cli_netlogon.c
index 478a8558824be1a562aea6e606ed58e36f9baae5..fd10721645ce0f8de2da6248597b5e03edb81b19 100644 (file)
 */
 
 #include "includes.h"
-
-/* LSA Request Challenge. Sends our challenge to server, then gets
-   server response. These are used to generate the credentials.
- The sent and received challenges are stored in the netlog pipe
- private data. Only call this via rpccli_netlogon_setup_creds(). JRA.
-*/
-
-/* instead of rpccli_net_req_chal() we use rpccli_netr_ServerReqChallenge() now - gd */
-
-#if 0
-/****************************************************************************
-LSA Authenticate 2
-
-Send the client credential, receive back a server credential.
-Ensure that the server credential returned matches the session key
-encrypt of the server challenge originally received. JRA.
-****************************************************************************/
-
-  NTSTATUS rpccli_net_auth2(struct rpc_pipe_client *cli,
-                      uint16 sec_chan,
-                      uint32 *neg_flags, DOM_CHAL *srv_chal)
-{
-        prs_struct qbuf, rbuf;
-        NET_Q_AUTH_2 q;
-        NET_R_AUTH_2 r;
-        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       fstring machine_acct;
-
-       if ( sec_chan == SEC_CHAN_DOMAIN )
-               fstr_sprintf( machine_acct, "%s$", lp_workgroup() );
-       else
-               fstrcpy( machine_acct, cli->mach_acct );
-
-        /* create and send a MSRPC command with api NET_AUTH2 */
-
-        DEBUG(4,("cli_net_auth2: srv:%s acct:%s sc:%x mc: %s chal %s neg: %x\n",
-                 cli->srv_name_slash, machine_acct, sec_chan, global_myname(),
-                 credstr(cli->clnt_cred.challenge.data), *neg_flags));
-
-        /* store the parameters */
-
-        init_q_auth_2(&q, cli->srv_name_slash, machine_acct,
-                      sec_chan, global_myname(), &cli->clnt_cred.challenge,
-                      *neg_flags);
-
-        /* turn parameters into data stream */
-
-       CLI_DO_RPC(cli, mem_ctx, PI_NETLOGON, NET_AUTH2,
-               q, r,
-               qbuf, rbuf,
-               net_io_q_auth_2,
-               net_io_r_auth_2,
-               NT_STATUS_UNSUCCESSFUL);
-
-        result = r.status;
-
-        if (NT_STATUS_IS_OK(result)) {
-                UTIME zerotime;
-
-                /*
-                 * Check the returned value using the initial
-                 * server received challenge.
-                 */
-
-                zerotime.time = 0;
-                if (cred_assert( &r.srv_chal, cli->sess_key, srv_chal, zerotime) == 0) {
-
-                        /*
-                         * Server replied with bad credential. Fail.
-                         */
-                        DEBUG(0,("cli_net_auth2: server %s replied with bad credential (bad machine \
-password ?).\n", cli->cli->desthost ));
-                       return NT_STATUS_ACCESS_DENIED;
-                }
-               *neg_flags = r.srv_flgs.neg_flags;
-        }
-
-        return result;
-}
-#endif
-
-/****************************************************************************
- LSA Authenticate 2
-
- Send the client credential, receive back a server credential.
- The caller *must* ensure that the server credential returned matches the session key
- encrypt of the server challenge originally received. JRA.
-****************************************************************************/
-
-/* instead of rpccli_net_auth2() we use rpccli_netr_ServerAuthenticate2() now -  gd */
-
+#include "../libcli/auth/libcli_auth.h"
 
 /****************************************************************************
  Wrapper function that uses the auth and auth2 calls to set up a NETLOGON
@@ -131,28 +41,21 @@ NTSTATUS rpccli_netlogon_setup_creds(struct rpc_pipe_client *cli,
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        struct netr_Credential clnt_chal_send;
        struct netr_Credential srv_chal_recv;
-       struct dcinfo *dc;
+       struct samr_Password password;
        bool retried = false;
+       fstring mach_acct;
 
-       SMB_ASSERT(cli->pipe_idx == PI_NETLOGON);
-
-       dc = cli->dc;
-       if (!dc) {
+       if (!ndr_syntax_id_equal(&cli->abstract_syntax,
+                                &ndr_table_netlogon.syntax_id)) {
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       /* Ensure we don't reuse any of this state. */
-       ZERO_STRUCTP(dc);
+       TALLOC_FREE(cli->dc);
 
        /* Store the machine account password we're going to use. */
-       memcpy(dc->mach_pw, machine_pwd, 16);
-
-       fstrcpy(dc->remote_machine, "\\\\");
-       fstrcat(dc->remote_machine, server_name);
-
-       fstrcpy(dc->domain, domain);
+       memcpy(password.hash, machine_pwd, 16);
 
-       fstr_sprintf( dc->mach_acct, "%s$", machine_account);
+       fstr_sprintf( mach_acct, "%s$", machine_account);
 
  again:
        /* Create the client challenge. */
@@ -160,7 +63,7 @@ NTSTATUS rpccli_netlogon_setup_creds(struct rpc_pipe_client *cli,
 
        /* Get the server challenge. */
        result = rpccli_netr_ServerReqChallenge(cli, talloc_tos(),
-                                               dc->remote_machine,
+                                               cli->srv_name_slash,
                                                clnt_name,
                                                &clnt_chal_send,
                                                &srv_chal_recv);
@@ -169,22 +72,29 @@ NTSTATUS rpccli_netlogon_setup_creds(struct rpc_pipe_client *cli,
        }
 
        /* Calculate the session key and client credentials */
-       creds_client_init(*neg_flags_inout,
-                       dc,
-                       &clnt_chal_send,
-                       &srv_chal_recv,
-                       machine_pwd,
-                       &clnt_chal_send);
+       
+       cli->dc = netlogon_creds_client_init(cli,
+                                   mach_acct,
+                                   clnt_name,
+                                   &clnt_chal_send,
+                                   &srv_chal_recv,
+                                   &password,
+                                   &clnt_chal_send,
+                                   *neg_flags_inout);
+
+       if (!cli->dc) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
        /*
         * Send client auth-2 challenge and receive server repy.
         */
 
        result = rpccli_netr_ServerAuthenticate2(cli, talloc_tos(),
-                                                dc->remote_machine,
-                                                dc->mach_acct,
+                                                cli->srv_name_slash,
+                                                cli->dc->account_name,
                                                 sec_chan_type,
-                                                clnt_name,
+                                                cli->dc->computer_name,
                                                 &clnt_chal_send, /* input. */
                                                 &srv_chal_recv, /* output. */
                                                 neg_flags_inout);
@@ -194,6 +104,7 @@ NTSTATUS rpccli_netlogon_setup_creds(struct rpc_pipe_client *cli,
 
        if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) && !retried) {
                retried = true;
+               TALLOC_FREE(cli->dc);
                goto again;
        }
 
@@ -206,19 +117,19 @@ NTSTATUS rpccli_netlogon_setup_creds(struct rpc_pipe_client *cli,
         * server received challenge.
         */
 
-       if (!netlogon_creds_client_check(dc, &srv_chal_recv)) {
+       if (!netlogon_creds_client_check(cli->dc, &srv_chal_recv)) {
                /*
                 * Server replied with bad credential. Fail.
                 */
                DEBUG(0,("rpccli_netlogon_setup_creds: server %s "
                        "replied with bad credential\n",
-                       cli->cli->desthost ));
+                       cli->desthost ));
                return NT_STATUS_ACCESS_DENIED;
        }
 
        DEBUG(5,("rpccli_netlogon_setup_creds: server %s credential "
                "chain established.\n",
-               cli->cli->desthost ));
+               cli->desthost ));
 
        return NT_STATUS_OK;
 }
@@ -237,7 +148,7 @@ NTSTATUS rpccli_netlogon_sam_logon(struct rpc_pipe_client *cli,
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        struct netr_Authenticator clnt_creds;
        struct netr_Authenticator ret_creds;
-       union netr_LogonInfo *logon;
+       union netr_LogonLevel *logon;
        union netr_Validation validation;
        uint8_t authoritative;
        int validation_level = 3;
@@ -247,7 +158,7 @@ NTSTATUS rpccli_netlogon_sam_logon(struct rpc_pipe_client *cli,
        ZERO_STRUCT(ret_creds);
        ZERO_STRUCT(zeros);
 
-       logon = TALLOC_ZERO_P(mem_ctx, union netr_LogonInfo);
+       logon = TALLOC_ZERO_P(mem_ctx, union netr_LogonLevel);
        if (!logon) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -260,53 +171,30 @@ NTSTATUS rpccli_netlogon_sam_logon(struct rpc_pipe_client *cli,
 
        /* Initialise input parameters */
 
-       netlogon_creds_client_step(cli->dc, &clnt_creds);
+       netlogon_creds_client_authenticator(cli->dc, &clnt_creds);
 
        switch (logon_type) {
-       case INTERACTIVE_LOGON_TYPE: {
+       case NetlogonInteractiveInformation: {
 
                struct netr_PasswordInfo *password_info;
 
                struct samr_Password lmpassword;
                struct samr_Password ntpassword;
 
-               unsigned char lm_owf_user_pwd[16], nt_owf_user_pwd[16];
-
-               unsigned char lm_owf[16];
-               unsigned char nt_owf[16];
-               unsigned char key[16];
-
                password_info = TALLOC_ZERO_P(mem_ctx, struct netr_PasswordInfo);
                if (!password_info) {
                        return NT_STATUS_NO_MEMORY;
                }
 
-               nt_lm_owf_gen(password, nt_owf_user_pwd, lm_owf_user_pwd);
+               nt_lm_owf_gen(password, ntpassword.hash, lmpassword.hash);
 
-#ifdef DEBUG_PASSWORD
-               DEBUG(100,("lm cypher:"));
-               dump_data(100, lm_owf_user_pwd, 16);
-
-               DEBUG(100,("nt cypher:"));
-               dump_data(100, nt_owf_user_pwd, 16);
-#endif
-               memset(key, 0, 16);
-               memcpy(key, cli->dc->sess_key, 8);
-
-               memcpy(lm_owf, lm_owf_user_pwd, 16);
-               SamOEMhash(lm_owf, key, 16);
-               memcpy(nt_owf, nt_owf_user_pwd, 16);
-               SamOEMhash(nt_owf, key, 16);
-
-#ifdef DEBUG_PASSWORD
-               DEBUG(100,("encrypt of lm owf password:"));
-               dump_data(100, lm_owf, 16);
-
-               DEBUG(100,("encrypt of nt owf password:"));
-               dump_data(100, nt_owf, 16);
-#endif
-               memcpy(lmpassword.hash, lm_owf, 16);
-               memcpy(ntpassword.hash, nt_owf, 16);
+               if (cli->dc->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
+                       netlogon_creds_arcfour_crypt(cli->dc, lmpassword.hash, 16);
+                       netlogon_creds_arcfour_crypt(cli->dc, ntpassword.hash, 16);
+               } else {
+                       netlogon_creds_des_encrypt(cli->dc, &lmpassword);
+                       netlogon_creds_des_encrypt(cli->dc, &ntpassword);
+               }
 
                init_netr_PasswordInfo(password_info,
                                       domain,
@@ -322,7 +210,7 @@ NTSTATUS rpccli_netlogon_sam_logon(struct rpc_pipe_client *cli,
 
                break;
        }
-       case NET_LOGON_TYPE: {
+       case NetlogonNetworkInformation: {
                struct netr_NetworkInfo *network_info;
                uint8 chal[8];
                unsigned char local_lm_response[24];
@@ -371,7 +259,7 @@ NTSTATUS rpccli_netlogon_sam_logon(struct rpc_pipe_client *cli,
        }
 
        result = rpccli_netr_LogonSamLogon(cli, mem_ctx,
-                                          cli->dc->remote_machine,
+                                          cli->srv_name_slash,
                                           global_myname(),
                                           &clnt_creds,
                                           &ret_creds,
@@ -381,12 +269,10 @@ NTSTATUS rpccli_netlogon_sam_logon(struct rpc_pipe_client *cli,
                                           &validation,
                                           &authoritative);
 
-       if (memcmp(zeros, &ret_creds.cred.data, sizeof(ret_creds.cred.data)) != 0) {
-               /* Check returned credentials if present. */
-               if (!netlogon_creds_client_check(cli->dc, &ret_creds.cred)) {
-                       DEBUG(0,("rpccli_netlogon_sam_logon: credentials chain check failed\n"));
-                       return NT_STATUS_ACCESS_DENIED;
-               }
+       /* Always check returned credentials */
+       if (!netlogon_creds_client_check(cli->dc, &ret_creds.cred)) {
+               DEBUG(0,("rpccli_netlogon_sam_logon: credentials chain check failed\n"));
+               return NT_STATUS_ACCESS_DENIED;
        }
 
        return result;
@@ -418,14 +304,12 @@ NTSTATUS rpccli_netlogon_sam_network_logon(struct rpc_pipe_client *cli,
        uint8 zeros[16];
        struct netr_Authenticator clnt_creds;
        struct netr_Authenticator ret_creds;
-       union netr_LogonInfo *logon = NULL;
+       union netr_LogonLevel *logon = NULL;
        struct netr_NetworkInfo *network_info;
        uint8_t authoritative;
        union netr_Validation validation;
        struct netr_ChallengeResponse lm;
        struct netr_ChallengeResponse nt;
-       struct netr_UserSessionKey user_session_key;
-       struct netr_LMSessionKey lmsesskey;
 
        *info3 = NULL;
 
@@ -435,7 +319,7 @@ NTSTATUS rpccli_netlogon_sam_network_logon(struct rpc_pipe_client *cli,
        ZERO_STRUCT(lm);
        ZERO_STRUCT(nt);
 
-       logon = TALLOC_ZERO_P(mem_ctx, union netr_LogonInfo);
+       logon = TALLOC_ZERO_P(mem_ctx, union netr_LogonLevel);
        if (!logon) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -445,7 +329,7 @@ NTSTATUS rpccli_netlogon_sam_network_logon(struct rpc_pipe_client *cli,
                return NT_STATUS_NO_MEMORY;
        }
 
-       netlogon_creds_client_step(cli->dc, &clnt_creds);
+       netlogon_creds_client_authenticator(cli->dc, &clnt_creds);
 
        if (server[0] != '\\' && server[1] != '\\') {
                server_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", server);
@@ -491,7 +375,7 @@ NTSTATUS rpccli_netlogon_sam_network_logon(struct rpc_pipe_client *cli,
                                           global_myname(),
                                           &clnt_creds,
                                           &ret_creds,
-                                          NET_LOGON_TYPE,
+                                          NetlogonNetworkInformation,
                                           logon,
                                           validation_level,
                                           &validation,
@@ -500,24 +384,13 @@ NTSTATUS rpccli_netlogon_sam_network_logon(struct rpc_pipe_client *cli,
                return result;
        }
 
-       user_session_key = validation.sam3->base.key;
-       lmsesskey = validation.sam3->base.LMSessKey;
-
-       if (memcmp(zeros, user_session_key.key, 16) != 0) {
-               SamOEMhash(user_session_key.key, cli->dc->sess_key, 16);
-       }
-
-       if (memcmp(zeros, lmsesskey.key, 8) != 0) {
-               SamOEMhash(lmsesskey.key, cli->dc->sess_key, 8);
+       /* Always check returned credentials. */
+       if (!netlogon_creds_client_check(cli->dc, &ret_creds.cred)) {
+               DEBUG(0,("rpccli_netlogon_sam_network_logon: credentials chain check failed\n"));
+               return NT_STATUS_ACCESS_DENIED;
        }
 
-       if (memcmp(zeros, ret_creds.cred.data, sizeof(ret_creds.cred.data)) != 0) {
-               /* Check returned credentials if present. */
-               if (!netlogon_creds_client_check(cli->dc, &ret_creds.cred)) {
-                       DEBUG(0,("rpccli_netlogon_sam_network_logon: credentials chain check failed\n"));
-                       return NT_STATUS_ACCESS_DENIED;
-               }
-       }
+       netlogon_creds_decrypt_samlogon(cli->dc, validation_level, &validation);
 
        *info3 = validation.sam3;
 
@@ -541,14 +414,12 @@ NTSTATUS rpccli_netlogon_sam_network_logon_ex(struct rpc_pipe_client *cli,
        const char *workstation_name_slash;
        const char *server_name_slash;
        uint8 zeros[16];
-       union netr_LogonInfo *logon = NULL;
+       union netr_LogonLevel *logon = NULL;
        struct netr_NetworkInfo *network_info;
        uint8_t authoritative;
        union netr_Validation validation;
        struct netr_ChallengeResponse lm;
        struct netr_ChallengeResponse nt;
-       struct netr_UserSessionKey user_session_key;
-       struct netr_LMSessionKey lmsesskey;
        uint32_t flags = 0;
 
        *info3 = NULL;
@@ -558,7 +429,7 @@ NTSTATUS rpccli_netlogon_sam_network_logon_ex(struct rpc_pipe_client *cli,
        ZERO_STRUCT(lm);
        ZERO_STRUCT(nt);
 
-       logon = TALLOC_ZERO_P(mem_ctx, union netr_LogonInfo);
+       logon = TALLOC_ZERO_P(mem_ctx, union netr_LogonLevel);
        if (!logon) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -610,7 +481,7 @@ NTSTATUS rpccli_netlogon_sam_network_logon_ex(struct rpc_pipe_client *cli,
        result = rpccli_netr_LogonSamLogonEx(cli, mem_ctx,
                                             server_name_slash,
                                             global_myname(),
-                                            NET_LOGON_TYPE,
+                                            NetlogonNetworkInformation,
                                             logon,
                                             validation_level,
                                             &validation,
@@ -620,18 +491,98 @@ NTSTATUS rpccli_netlogon_sam_network_logon_ex(struct rpc_pipe_client *cli,
                return result;
        }
 
-       user_session_key = validation.sam3->base.key;
-       lmsesskey = validation.sam3->base.LMSessKey;
+       netlogon_creds_decrypt_samlogon(cli->dc, validation_level, &validation);
+
+       *info3 = validation.sam3;
+
+       return result;
+}
+
+/*********************************************************
+ Change the domain password on the PDC.
+
+ Just changes the password betwen the two values specified.
+
+ Caller must have the cli connected to the netlogon pipe
+ already.
+**********************************************************/
+
+NTSTATUS rpccli_netlogon_set_trust_password(struct rpc_pipe_client *cli,
+                                           TALLOC_CTX *mem_ctx,
+                                           const unsigned char orig_trust_passwd_hash[16],
+                                           const char *new_trust_pwd_cleartext,
+                                           const unsigned char new_trust_passwd_hash[16],
+                                           uint32_t sec_channel_type)
+{
+       NTSTATUS result;
+       uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
+       struct netr_Authenticator clnt_creds, srv_cred;
+
+       result = rpccli_netlogon_setup_creds(cli,
+                                            cli->desthost, /* server name */
+                                            lp_workgroup(), /* domain */
+                                            global_myname(), /* client name */
+                                            global_myname(), /* machine account name */
+                                            orig_trust_passwd_hash,
+                                            sec_channel_type,
+                                            &neg_flags);
 
-       if (memcmp(zeros, user_session_key.key, 16) != 0) {
-               SamOEMhash(user_session_key.key, cli->dc->sess_key, 16);
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(3,("rpccli_netlogon_set_trust_password: unable to setup creds (%s)!\n",
+                        nt_errstr(result)));
+               return result;
        }
 
-       if (memcmp(zeros, lmsesskey.key, 8) != 0) {
-               SamOEMhash(lmsesskey.key, cli->dc->sess_key, 8);
+       netlogon_creds_client_authenticator(cli->dc, &clnt_creds);
+
+       if (neg_flags & NETLOGON_NEG_PASSWORD_SET2) {
+
+               struct netr_CryptPassword new_password;
+
+               init_netr_CryptPassword(new_trust_pwd_cleartext,
+                                       cli->dc->session_key,
+                                       &new_password);
+
+               result = rpccli_netr_ServerPasswordSet2(cli, mem_ctx,
+                                                       cli->srv_name_slash,
+                                                       cli->dc->account_name,
+                                                       sec_channel_type,
+                                                       cli->dc->computer_name,
+                                                       &clnt_creds,
+                                                       &srv_cred,
+                                                       &new_password);
+               if (!NT_STATUS_IS_OK(result)) {
+                       DEBUG(0,("rpccli_netr_ServerPasswordSet2 failed: %s\n",
+                               nt_errstr(result)));
+                       return result;
+               }
+       } else {
+
+               struct samr_Password new_password;
+               memcpy(new_password.hash, new_trust_passwd_hash, sizeof(new_password.hash));
+               netlogon_creds_des_encrypt(cli->dc, &new_password);
+
+               result = rpccli_netr_ServerPasswordSet(cli, mem_ctx,
+                                                      cli->srv_name_slash,
+                                                      cli->dc->account_name,
+                                                      sec_channel_type,
+                                                      cli->dc->computer_name,
+                                                      &clnt_creds,
+                                                      &srv_cred,
+                                                      &new_password);
+               if (!NT_STATUS_IS_OK(result)) {
+                       DEBUG(0,("rpccli_netr_ServerPasswordSet failed: %s\n",
+                               nt_errstr(result)));
+                       return result;
+               }
        }
 
-       *info3 = validation.sam3;
+       /* Always check returned credentials. */
+       if (!netlogon_creds_client_check(cli->dc, &srv_cred.cred)) {
+               DEBUG(0,("credentials chain check failed\n"));
+               return NT_STATUS_ACCESS_DENIED;
+       }
 
        return result;
 }
+