4d7acd198889439de43dbadc8a50d2f2d4252f8c
[kai/samba-autobuild/.git] / source3 / libsmb / trust_passwd.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Routines to change trust account passwords.
4  *  Copyright (C) Andrew Bartlett                   2001.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include "includes.h"
22
23 extern pstring global_myname;
24
25 /*********************************************************
26  Change the domain password on the PDC.
27
28  Just changes the password betwen the two values specified.
29
30  Caller must have the cli connected to the netlogon pipe
31  already.
32 **********************************************************/
33 static NTSTATUS just_change_the_password(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
34                                          unsigned char orig_trust_passwd_hash[16],
35                                          unsigned char new_trust_passwd_hash[16])
36 {
37         NTSTATUS result;
38         uint32 neg_flags = 0x000001ff;
39
40         result = cli_nt_setup_creds(cli, get_sec_chan(), orig_trust_passwd_hash, &neg_flags, 2);
41         
42         if (!NT_STATUS_IS_OK(result)) {
43                 DEBUG(1,("just_change_the_password: unable to setup creds (%s)!\n",
44                          nt_errstr(result)));
45                 return result;
46         }
47
48         result = cli_net_srv_pwset(cli, mem_ctx, global_myname, new_trust_passwd_hash);
49
50         if (!NT_STATUS_IS_OK(result)) {
51                 DEBUG(0,("just_change_the_password: unable to change password (%s)!\n",
52                          nt_errstr(result)));
53         }
54         return result;
55 }
56
57 /*********************************************************
58  Change the domain password on the PDC.
59  Store the password ourselves, but use the supplied password
60  Caller must have already setup the connection to the NETLOGON pipe
61 **********************************************************/
62
63 NTSTATUS trust_pw_change_and_store_it(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
64                                       unsigned char orig_trust_passwd_hash[16])
65 {
66         unsigned char new_trust_passwd_hash[16];
67         char *new_trust_passwd;
68         char *str;
69         NTSTATUS nt_status;
70                 
71         /* Create a random machine account password */
72         str = generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
73         new_trust_passwd = talloc_strdup(mem_ctx, str);
74         
75         E_md4hash(new_trust_passwd, new_trust_passwd_hash);
76
77         nt_status = just_change_the_password(cli, mem_ctx, orig_trust_passwd_hash,
78                                              new_trust_passwd_hash);
79         
80         if (NT_STATUS_IS_OK(nt_status)) {
81                 DEBUG(3,("%s : trust_pw_change_and_store_it: Changed password.\n", 
82                          timestring(False)));
83                 /*
84                  * Return the result of trying to write the new password
85                  * back into the trust account file.
86                  */
87                 if (!secrets_store_machine_password(new_trust_passwd)) {
88                         nt_status = NT_STATUS_UNSUCCESSFUL;
89                 }
90         }
91
92         return nt_status;
93 }
94
95 /*********************************************************
96  Change the domain password on the PDC.
97  Do most of the legwork ourselfs.  Caller must have
98  already setup the connection to the NETLOGON pipe
99 **********************************************************/
100
101 NTSTATUS trust_pw_find_change_and_store_it(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
102                                            char *domain) 
103 {
104         unsigned char old_trust_passwd_hash[16];
105         char *up_domain;
106         
107         up_domain = talloc_strdup(mem_ctx, domain);
108
109         if (!secrets_fetch_trust_account_password(domain,
110                                                   old_trust_passwd_hash, 
111                                                   NULL)) {
112                 DEBUG(0, ("could not fetch domain secrets for domain %s!\n", domain));
113                 return NT_STATUS_UNSUCCESSFUL;
114         }
115         
116         return trust_pw_change_and_store_it(cli, mem_ctx, old_trust_passwd_hash);
117         
118 }