8a0e53dbe51bcc1d58d9c64ffcfceeea68f9c79c
[mat/samba.git] / source3 / libsmb / trusts_util.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Routines to operate on various trust relationships
4  *  Copyright (C) Andrew Bartlett                   2001
5  *  Copyright (C) Rafal Szczesniak                  2003
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 3 of the License, or
10  *  (at your option) any later version.
11  *  
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *  
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "includes.h"
22 #include "../libcli/auth/libcli_auth.h"
23 #include "rpc_client/cli_netlogon.h"
24 #include "rpc_client/cli_pipe.h"
25 #include "../librpc/gen_ndr/ndr_netlogon.h"
26 #include "secrets.h"
27 #include "passdb.h"
28 #include "libsmb/libsmb.h"
29
30 /*********************************************************
31  Change the domain password on the PDC.
32  Store the password ourselves, but use the supplied password
33  Caller must have already setup the connection to the NETLOGON pipe
34 **********************************************************/
35
36 NTSTATUS trust_pw_change_and_store_it(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, 
37                                       const char *domain,
38                                       const char *account_name,
39                                       unsigned char orig_trust_passwd_hash[16],
40                                       enum netr_SchannelType sec_channel_type)
41 {
42         unsigned char new_trust_passwd_hash[16];
43         char *new_trust_passwd;
44         NTSTATUS nt_status;
45
46         switch (sec_channel_type) {
47         case SEC_CHAN_WKSTA:
48         case SEC_CHAN_DOMAIN:
49                 break;
50         default:
51                 return NT_STATUS_NOT_SUPPORTED;
52         }
53
54         /* Create a random machine account password */
55         new_trust_passwd = generate_random_password(mem_ctx,
56                                 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
57                                 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
58         if (new_trust_passwd == NULL) {
59                 DEBUG(0, ("generate_random_password failed\n"));
60                 return NT_STATUS_NO_MEMORY;
61         }
62
63         E_md4hash(new_trust_passwd, new_trust_passwd_hash);
64
65         nt_status = rpccli_netlogon_set_trust_password(cli, mem_ctx,
66                                                        account_name,
67                                                        orig_trust_passwd_hash,
68                                                        new_trust_passwd,
69                                                        new_trust_passwd_hash,
70                                                        sec_channel_type);
71
72         if (NT_STATUS_IS_OK(nt_status)) {
73                 DEBUG(3,("%s : trust_pw_change_and_store_it: Changed password.\n", 
74                          current_timestring(talloc_tos(), False)));
75                 /*
76                  * Return the result of trying to write the new password
77                  * back into the trust account file.
78                  */
79
80                 switch (sec_channel_type) {
81
82                 case SEC_CHAN_WKSTA:
83                         if (!secrets_store_machine_password(new_trust_passwd, domain, sec_channel_type)) {
84                                 nt_status = NT_STATUS_UNSUCCESSFUL;
85                         }
86                         break;
87
88                 case SEC_CHAN_DOMAIN: {
89                         char *pwd;
90                         struct dom_sid sid;
91                         time_t pass_last_set_time;
92
93                         /* we need to get the sid first for the
94                          * pdb_set_trusteddom_pw call */
95
96                         if (!pdb_get_trusteddom_pw(domain, &pwd, &sid, &pass_last_set_time)) {
97                                 nt_status = NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE;
98                         }
99                         if (!pdb_set_trusteddom_pw(domain, new_trust_passwd, &sid)) {
100                                 nt_status = NT_STATUS_INTERNAL_DB_CORRUPTION;
101                         }
102                         break;
103                 }
104                 default:
105                         break;
106                 }
107         }
108
109         return nt_status;
110 }
111
112 /*********************************************************
113  Change the domain password on the PDC.
114  Do most of the legwork ourselfs.  Caller must have
115  already setup the connection to the NETLOGON pipe
116 **********************************************************/
117
118 NTSTATUS trust_pw_find_change_and_store_it(struct rpc_pipe_client *cli, 
119                                            TALLOC_CTX *mem_ctx, 
120                                            const char *domain) 
121 {
122         unsigned char old_trust_passwd_hash[16];
123         enum netr_SchannelType sec_channel_type = SEC_CHAN_NULL;
124         const char *account_name;
125
126         if (!get_trust_pw_hash(domain, old_trust_passwd_hash, &account_name,
127                                &sec_channel_type)) {
128                 DEBUG(0, ("could not fetch domain secrets for domain %s!\n", domain));
129                 return NT_STATUS_UNSUCCESSFUL;
130         }
131
132         return trust_pw_change_and_store_it(cli, mem_ctx, domain,
133                                             account_name,
134                                             old_trust_passwd_hash,
135                                             sec_channel_type);
136 }
137