a9794edc9538366cd2d857ad95bf6dc019543492
[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 "../librpc/gen_ndr/ndr_lsa_c.h"
24 #include "rpc_client/cli_lsarpc.h"
25 #include "rpc_client/cli_netlogon.h"
26 #include "../librpc/gen_ndr/ndr_netlogon.h"
27 #include "secrets.h"
28
29 /*********************************************************
30  Change the domain password on the PDC.
31  Store the password ourselves, but use the supplied password
32  Caller must have already setup the connection to the NETLOGON pipe
33 **********************************************************/
34
35 NTSTATUS trust_pw_change_and_store_it(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, 
36                                       const char *domain,
37                                       const char *account_name,
38                                       unsigned char orig_trust_passwd_hash[16],
39                                       enum netr_SchannelType sec_channel_type)
40 {
41         unsigned char new_trust_passwd_hash[16];
42         char *new_trust_passwd;
43         NTSTATUS nt_status;
44
45         switch (sec_channel_type) {
46         case SEC_CHAN_WKSTA:
47         case SEC_CHAN_DOMAIN:
48                 break;
49         default:
50                 return NT_STATUS_NOT_SUPPORTED;
51         }
52
53         /* Create a random machine account password */
54         new_trust_passwd = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
55
56         if (new_trust_passwd == NULL) {
57                 DEBUG(0, ("talloc_strdup failed\n"));
58                 return NT_STATUS_NO_MEMORY;
59         }
60
61         E_md4hash(new_trust_passwd, new_trust_passwd_hash);
62
63         nt_status = rpccli_netlogon_set_trust_password(cli, mem_ctx,
64                                                        account_name,
65                                                        orig_trust_passwd_hash,
66                                                        new_trust_passwd,
67                                                        new_trust_passwd_hash,
68                                                        sec_channel_type);
69
70         if (NT_STATUS_IS_OK(nt_status)) {
71                 DEBUG(3,("%s : trust_pw_change_and_store_it: Changed password.\n", 
72                          current_timestring(talloc_tos(), False)));
73                 /*
74                  * Return the result of trying to write the new password
75                  * back into the trust account file.
76                  */
77
78                 switch (sec_channel_type) {
79
80                 case SEC_CHAN_WKSTA:
81                         if (!secrets_store_machine_password(new_trust_passwd, domain, sec_channel_type)) {
82                                 nt_status = NT_STATUS_UNSUCCESSFUL;
83                         }
84                         break;
85
86                 case SEC_CHAN_DOMAIN: {
87                         char *pwd;
88                         struct dom_sid sid;
89                         time_t pass_last_set_time;
90
91                         /* we need to get the sid first for the
92                          * pdb_set_trusteddom_pw call */
93
94                         if (!pdb_get_trusteddom_pw(domain, &pwd, &sid, &pass_last_set_time)) {
95                                 nt_status = NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE;
96                         }
97                         if (!pdb_set_trusteddom_pw(domain, new_trust_passwd, &sid)) {
98                                 nt_status = NT_STATUS_INTERNAL_DB_CORRUPTION;
99                         }
100                         break;
101                 }
102                 default:
103                         break;
104                 }
105         }
106
107         return nt_status;
108 }
109
110 /*********************************************************
111  Change the domain password on the PDC.
112  Do most of the legwork ourselfs.  Caller must have
113  already setup the connection to the NETLOGON pipe
114 **********************************************************/
115
116 NTSTATUS trust_pw_find_change_and_store_it(struct rpc_pipe_client *cli, 
117                                            TALLOC_CTX *mem_ctx, 
118                                            const char *domain) 
119 {
120         unsigned char old_trust_passwd_hash[16];
121         enum netr_SchannelType sec_channel_type = SEC_CHAN_NULL;
122         const char *account_name;
123
124         if (!get_trust_pw_hash(domain, old_trust_passwd_hash, &account_name,
125                                &sec_channel_type)) {
126                 DEBUG(0, ("could not fetch domain secrets for domain %s!\n", domain));
127                 return NT_STATUS_UNSUCCESSFUL;
128         }
129
130         return trust_pw_change_and_store_it(cli, mem_ctx, domain,
131                                             account_name,
132                                             old_trust_passwd_hash,
133                                             sec_channel_type);
134 }
135
136 /*********************************************************************
137  Enumerate the list of trusted domains from a DC
138 *********************************************************************/
139
140 bool enumerate_domain_trusts( TALLOC_CTX *mem_ctx, const char *domain,
141                                      char ***domain_names, uint32 *num_domains,
142                                      struct dom_sid **sids )
143 {
144         struct policy_handle    pol;
145         NTSTATUS status, result;
146         fstring         dc_name;
147         struct sockaddr_storage dc_ss;
148         uint32          enum_ctx = 0;
149         struct cli_state *cli = NULL;
150         struct rpc_pipe_client *lsa_pipe = NULL;
151         struct lsa_DomainList dom_list;
152         int i;
153         struct dcerpc_binding_handle *b = NULL;
154
155         *domain_names = NULL;
156         *num_domains = 0;
157         *sids = NULL;
158
159         /* lookup a DC first */
160
161         if ( !get_dc_name(domain, NULL, dc_name, &dc_ss) ) {
162                 DEBUG(3,("enumerate_domain_trusts: can't locate a DC for domain %s\n",
163                         domain));
164                 return False;
165         }
166
167         /* setup the anonymous connection */
168
169         status = cli_full_connection( &cli, global_myname(), dc_name, &dc_ss, 0, "IPC$", "IPC",
170                 "", "", "", 0, Undefined);
171         if ( !NT_STATUS_IS_OK(status) )
172                 goto done;
173
174         /* open the LSARPC_PIPE */
175
176         status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
177                                           &lsa_pipe);
178         if (!NT_STATUS_IS_OK(status)) {
179                 goto done;
180         }
181
182         b = lsa_pipe->binding_handle;
183
184         /* get a handle */
185
186         status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
187                 LSA_POLICY_VIEW_LOCAL_INFORMATION, &pol);
188         if ( !NT_STATUS_IS_OK(status) )
189                 goto done;
190
191         /* Lookup list of trusted domains */
192
193         status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
194                                          &pol,
195                                          &enum_ctx,
196                                          &dom_list,
197                                          (uint32_t)-1,
198                                          &result);
199         if ( !NT_STATUS_IS_OK(status) )
200                 goto done;
201         if (!NT_STATUS_IS_OK(result)) {
202                 status = result;
203                 goto done;
204         }
205
206         *num_domains = dom_list.count;
207
208         *domain_names = TALLOC_ZERO_ARRAY(mem_ctx, char *, *num_domains);
209         if (!*domain_names) {
210                 status = NT_STATUS_NO_MEMORY;
211                 goto done;
212         }
213
214         *sids = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, *num_domains);
215         if (!*sids) {
216                 status = NT_STATUS_NO_MEMORY;
217                 goto done;
218         }
219
220         for (i=0; i< *num_domains; i++) {
221                 (*domain_names)[i] = CONST_DISCARD(char *, dom_list.domains[i].name.string);
222                 (*sids)[i] = *dom_list.domains[i].sid;
223         }
224
225 done:
226         /* cleanup */
227         if (cli) {
228                 DEBUG(10,("enumerate_domain_trusts: shutting down connection...\n"));
229                 cli_shutdown( cli );
230         }
231
232         return NT_STATUS_IS_OK(status);
233 }
234
235 NTSTATUS change_trust_account_password( const char *domain, const char *remote_machine)
236 {
237         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
238         struct sockaddr_storage pdc_ss;
239         fstring dc_name;
240         struct cli_state *cli = NULL;
241         struct rpc_pipe_client *netlogon_pipe = NULL;
242
243         DEBUG(5,("change_trust_account_password: Attempting to change trust account password in domain %s....\n",
244                 domain));
245
246         if (remote_machine == NULL || !strcmp(remote_machine, "*")) {
247                 /* Use the PDC *only* for this */
248
249                 if ( !get_pdc_ip(domain, &pdc_ss) ) {
250                         DEBUG(0,("Can't get IP for PDC for domain %s\n", domain));
251                         goto failed;
252                 }
253
254                 if ( !name_status_find( domain, 0x1b, 0x20, &pdc_ss, dc_name) )
255                         goto failed;
256         } else {
257                 /* supoport old deprecated "smbpasswd -j DOMAIN -r MACHINE" behavior */
258                 fstrcpy( dc_name, remote_machine );
259         }
260
261         /* if this next call fails, then give up.  We can't do
262            password changes on BDC's  --jerry */
263
264         if (!NT_STATUS_IS_OK(cli_full_connection(&cli, global_myname(), dc_name,
265                                            NULL, 0,
266                                            "IPC$", "IPC",
267                                            "", "",
268                                            "", 0, Undefined))) {
269                 DEBUG(0,("modify_trust_password: Connection to %s failed!\n", dc_name));
270                 nt_status = NT_STATUS_UNSUCCESSFUL;
271                 goto failed;
272         }
273
274         /*
275          * Ok - we have an anonymous connection to the IPC$ share.
276          * Now start the NT Domain stuff :-).
277          */
278
279         /* Shouldn't we open this with schannel ? JRA. */
280
281         nt_status = cli_rpc_pipe_open_noauth(
282                 cli, &ndr_table_netlogon.syntax_id, &netlogon_pipe);
283         if (!NT_STATUS_IS_OK(nt_status)) {
284                 DEBUG(0,("modify_trust_password: unable to open the domain client session to machine %s. Error was : %s.\n",
285                         dc_name, nt_errstr(nt_status)));
286                 cli_shutdown(cli);
287                 cli = NULL;
288                 goto failed;
289         }
290
291         nt_status = trust_pw_find_change_and_store_it(
292                 netlogon_pipe, netlogon_pipe, domain);
293
294         cli_shutdown(cli);
295         cli = NULL;
296
297 failed:
298         if (!NT_STATUS_IS_OK(nt_status)) {
299                 DEBUG(0,("%s : change_trust_account_password: Failed to change password for domain %s.\n",
300                         current_timestring(talloc_tos(), False), domain));
301         }
302         else
303                 DEBUG(5,("change_trust_account_password: sucess!\n"));
304
305         return nt_status;
306 }