08377fbfbc6220e248fa25283e2f2d765ee7f80f
[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 "rpc_client/cli_pipe.h"
27 #include "../librpc/gen_ndr/ndr_netlogon.h"
28 #include "secrets.h"
29 #include "passdb.h"
30
31 /*********************************************************
32  Change the domain password on the PDC.
33  Store the password ourselves, but use the supplied password
34  Caller must have already setup the connection to the NETLOGON pipe
35 **********************************************************/
36
37 NTSTATUS trust_pw_change_and_store_it(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, 
38                                       const char *domain,
39                                       const char *account_name,
40                                       unsigned char orig_trust_passwd_hash[16],
41                                       enum netr_SchannelType sec_channel_type)
42 {
43         unsigned char new_trust_passwd_hash[16];
44         char *new_trust_passwd;
45         NTSTATUS nt_status;
46
47         switch (sec_channel_type) {
48         case SEC_CHAN_WKSTA:
49         case SEC_CHAN_DOMAIN:
50                 break;
51         default:
52                 return NT_STATUS_NOT_SUPPORTED;
53         }
54
55         /* Create a random machine account password */
56         new_trust_passwd = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
57
58         if (new_trust_passwd == NULL) {
59                 DEBUG(0, ("talloc_strdup 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
138 /*********************************************************************
139  Enumerate the list of trusted domains from a DC
140 *********************************************************************/
141
142 bool enumerate_domain_trusts( TALLOC_CTX *mem_ctx, const char *domain,
143                                      char ***domain_names, uint32 *num_domains,
144                                      struct dom_sid **sids )
145 {
146         struct policy_handle    pol;
147         NTSTATUS status, result;
148         fstring         dc_name;
149         struct sockaddr_storage dc_ss;
150         uint32          enum_ctx = 0;
151         struct cli_state *cli = NULL;
152         struct rpc_pipe_client *lsa_pipe = NULL;
153         struct lsa_DomainList dom_list;
154         int i;
155         struct dcerpc_binding_handle *b = NULL;
156
157         *domain_names = NULL;
158         *num_domains = 0;
159         *sids = NULL;
160
161         /* lookup a DC first */
162
163         if ( !get_dc_name(domain, NULL, dc_name, &dc_ss) ) {
164                 DEBUG(3,("enumerate_domain_trusts: can't locate a DC for domain %s\n",
165                         domain));
166                 return False;
167         }
168
169         /* setup the anonymous connection */
170
171         status = cli_full_connection( &cli, global_myname(), dc_name, &dc_ss, 0, "IPC$", "IPC",
172                 "", "", "", 0, Undefined);
173         if ( !NT_STATUS_IS_OK(status) )
174                 goto done;
175
176         /* open the LSARPC_PIPE */
177
178         status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
179                                           &lsa_pipe);
180         if (!NT_STATUS_IS_OK(status)) {
181                 goto done;
182         }
183
184         b = lsa_pipe->binding_handle;
185
186         /* get a handle */
187
188         status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
189                 LSA_POLICY_VIEW_LOCAL_INFORMATION, &pol);
190         if ( !NT_STATUS_IS_OK(status) )
191                 goto done;
192
193         /* Lookup list of trusted domains */
194
195         status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
196                                          &pol,
197                                          &enum_ctx,
198                                          &dom_list,
199                                          (uint32_t)-1,
200                                          &result);
201         if ( !NT_STATUS_IS_OK(status) )
202                 goto done;
203         if (!NT_STATUS_IS_OK(result)) {
204                 status = result;
205                 goto done;
206         }
207
208         *num_domains = dom_list.count;
209
210         *domain_names = TALLOC_ZERO_ARRAY(mem_ctx, char *, *num_domains);
211         if (!*domain_names) {
212                 status = NT_STATUS_NO_MEMORY;
213                 goto done;
214         }
215
216         *sids = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, *num_domains);
217         if (!*sids) {
218                 status = NT_STATUS_NO_MEMORY;
219                 goto done;
220         }
221
222         for (i=0; i< *num_domains; i++) {
223                 (*domain_names)[i] = CONST_DISCARD(char *, dom_list.domains[i].name.string);
224                 (*sids)[i] = *dom_list.domains[i].sid;
225         }
226
227 done:
228         /* cleanup */
229         if (cli) {
230                 DEBUG(10,("enumerate_domain_trusts: shutting down connection...\n"));
231                 cli_shutdown( cli );
232         }
233
234         return NT_STATUS_IS_OK(status);
235 }
236
237 NTSTATUS change_trust_account_password( const char *domain, const char *remote_machine)
238 {
239         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
240         struct sockaddr_storage pdc_ss;
241         fstring dc_name;
242         struct cli_state *cli = NULL;
243         struct rpc_pipe_client *netlogon_pipe = NULL;
244
245         DEBUG(5,("change_trust_account_password: Attempting to change trust account password in domain %s....\n",
246                 domain));
247
248         if (remote_machine == NULL || !strcmp(remote_machine, "*")) {
249                 /* Use the PDC *only* for this */
250
251                 if ( !get_pdc_ip(domain, &pdc_ss) ) {
252                         DEBUG(0,("Can't get IP for PDC for domain %s\n", domain));
253                         goto failed;
254                 }
255
256                 if ( !name_status_find( domain, 0x1b, 0x20, &pdc_ss, dc_name) )
257                         goto failed;
258         } else {
259                 /* supoport old deprecated "smbpasswd -j DOMAIN -r MACHINE" behavior */
260                 fstrcpy( dc_name, remote_machine );
261         }
262
263         /* if this next call fails, then give up.  We can't do
264            password changes on BDC's  --jerry */
265
266         if (!NT_STATUS_IS_OK(cli_full_connection(&cli, global_myname(), dc_name,
267                                            NULL, 0,
268                                            "IPC$", "IPC",
269                                            "", "",
270                                            "", 0, Undefined))) {
271                 DEBUG(0,("modify_trust_password: Connection to %s failed!\n", dc_name));
272                 nt_status = NT_STATUS_UNSUCCESSFUL;
273                 goto failed;
274         }
275
276         /*
277          * Ok - we have an anonymous connection to the IPC$ share.
278          * Now start the NT Domain stuff :-).
279          */
280
281         /* Shouldn't we open this with schannel ? JRA. */
282
283         nt_status = cli_rpc_pipe_open_noauth(
284                 cli, &ndr_table_netlogon.syntax_id, &netlogon_pipe);
285         if (!NT_STATUS_IS_OK(nt_status)) {
286                 DEBUG(0,("modify_trust_password: unable to open the domain client session to machine %s. Error was : %s.\n",
287                         dc_name, nt_errstr(nt_status)));
288                 cli_shutdown(cli);
289                 cli = NULL;
290                 goto failed;
291         }
292
293         nt_status = trust_pw_find_change_and_store_it(
294                 netlogon_pipe, netlogon_pipe, domain);
295
296         cli_shutdown(cli);
297         cli = NULL;
298
299 failed:
300         if (!NT_STATUS_IS_OK(nt_status)) {
301                 DEBUG(0,("%s : change_trust_account_password: Failed to change password for domain %s.\n",
302                         current_timestring(talloc_tos(), False), domain));
303         }
304         else
305                 DEBUG(5,("change_trust_account_password: sucess!\n"));
306
307         return nt_status;
308 }