s3: Add "print notify backchannel" parameter to prevent us doing the reverse print...
[Samba.git] / source3 / libsmb / trusts_util.c
blob7ecc167343b40110f08b770be1aea024cb5d455f
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
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.
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.
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/>.
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"
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 **********************************************************/
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)
42 unsigned char new_trust_passwd_hash[16];
43 char *new_trust_passwd;
44 NTSTATUS nt_status;
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;
54 /* Create a random machine account password */
55 new_trust_passwd = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
57 if (new_trust_passwd == NULL) {
58 DEBUG(0, ("talloc_strdup failed\n"));
59 return NT_STATUS_NO_MEMORY;
62 E_md4hash(new_trust_passwd, new_trust_passwd_hash);
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);
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)));
75 * Return the result of trying to write the new password
76 * back into the trust account file.
79 switch (sec_channel_type) {
81 case SEC_CHAN_WKSTA:
82 if (!secrets_store_machine_password(new_trust_passwd, domain, sec_channel_type)) {
83 nt_status = NT_STATUS_UNSUCCESSFUL;
85 break;
87 case SEC_CHAN_DOMAIN: {
88 char *pwd;
89 struct dom_sid sid;
90 time_t pass_last_set_time;
92 /* we need to get the sid first for the
93 * pdb_set_trusteddom_pw call */
95 if (!pdb_get_trusteddom_pw(domain, &pwd, &sid, &pass_last_set_time)) {
96 nt_status = NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE;
98 if (!pdb_set_trusteddom_pw(domain, new_trust_passwd, &sid)) {
99 nt_status = NT_STATUS_INTERNAL_DB_CORRUPTION;
101 break;
103 default:
104 break;
108 return nt_status;
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 **********************************************************/
117 NTSTATUS trust_pw_find_change_and_store_it(struct rpc_pipe_client *cli,
118 TALLOC_CTX *mem_ctx,
119 const char *domain)
121 unsigned char old_trust_passwd_hash[16];
122 enum netr_SchannelType sec_channel_type = SEC_CHAN_NULL;
123 const char *account_name;
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;
131 return trust_pw_change_and_store_it(cli, mem_ctx, domain,
132 account_name,
133 old_trust_passwd_hash,
134 sec_channel_type);
137 /*********************************************************************
138 Enumerate the list of trusted domains from a DC
139 *********************************************************************/
141 bool enumerate_domain_trusts( TALLOC_CTX *mem_ctx, const char *domain,
142 char ***domain_names, uint32 *num_domains,
143 struct dom_sid **sids )
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;
156 *domain_names = NULL;
157 *num_domains = 0;
158 *sids = NULL;
160 /* lookup a DC first */
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;
168 /* setup the anonymous connection */
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;
175 /* open the LSARPC_PIPE */
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;
183 b = lsa_pipe->binding_handle;
185 /* get a handle */
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;
192 /* Lookup list of trusted domains */
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;
207 *num_domains = dom_list.count;
209 *domain_names = TALLOC_ZERO_ARRAY(mem_ctx, char *, *num_domains);
210 if (!*domain_names) {
211 status = NT_STATUS_NO_MEMORY;
212 goto done;
215 *sids = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, *num_domains);
216 if (!*sids) {
217 status = NT_STATUS_NO_MEMORY;
218 goto done;
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;
226 done:
227 /* cleanup */
228 if (cli) {
229 DEBUG(10,("enumerate_domain_trusts: shutting down connection...\n"));
230 cli_shutdown( cli );
233 return NT_STATUS_IS_OK(status);
236 NTSTATUS change_trust_account_password( const char *domain, const char *remote_machine)
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;
244 DEBUG(5,("change_trust_account_password: Attempting to change trust account password in domain %s....\n",
245 domain));
247 if (remote_machine == NULL || !strcmp(remote_machine, "*")) {
248 /* Use the PDC *only* for this */
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;
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 );
262 /* if this next call fails, then give up. We can't do
263 password changes on BDC's --jerry */
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;
276 * Ok - we have an anonymous connection to the IPC$ share.
277 * Now start the NT Domain stuff :-).
280 /* Shouldn't we open this with schannel ? JRA. */
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;
292 nt_status = trust_pw_find_change_and_store_it(
293 netlogon_pipe, netlogon_pipe, domain);
295 cli_shutdown(cli);
296 cli = NULL;
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));
303 else
304 DEBUG(5,("change_trust_account_password: sucess!\n"));
306 return nt_status;