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