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/>.
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"
30 #include "libsmb/libsmb.h"
32 /*********************************************************
33 Change the domain password on the PDC.
34 Store the password ourselves, but use the supplied password
35 Caller must have already setup the connection to the NETLOGON pipe
36 **********************************************************/
38 NTSTATUS
trust_pw_change_and_store_it(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
40 const char *account_name
,
41 unsigned char orig_trust_passwd_hash
[16],
42 enum netr_SchannelType sec_channel_type
)
44 unsigned char new_trust_passwd_hash
[16];
45 char *new_trust_passwd
;
48 switch (sec_channel_type
) {
53 return NT_STATUS_NOT_SUPPORTED
;
56 /* Create a random machine account password */
57 new_trust_passwd
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
59 if (new_trust_passwd
== NULL
) {
60 DEBUG(0, ("talloc_strdup failed\n"));
61 return NT_STATUS_NO_MEMORY
;
64 E_md4hash(new_trust_passwd
, new_trust_passwd_hash
);
66 nt_status
= rpccli_netlogon_set_trust_password(cli
, mem_ctx
,
68 orig_trust_passwd_hash
,
70 new_trust_passwd_hash
,
73 if (NT_STATUS_IS_OK(nt_status
)) {
74 DEBUG(3,("%s : trust_pw_change_and_store_it: Changed password.\n",
75 current_timestring(talloc_tos(), False
)));
77 * Return the result of trying to write the new password
78 * back into the trust account file.
81 switch (sec_channel_type
) {
84 if (!secrets_store_machine_password(new_trust_passwd
, domain
, sec_channel_type
)) {
85 nt_status
= NT_STATUS_UNSUCCESSFUL
;
89 case SEC_CHAN_DOMAIN
: {
92 time_t pass_last_set_time
;
94 /* we need to get the sid first for the
95 * pdb_set_trusteddom_pw call */
97 if (!pdb_get_trusteddom_pw(domain
, &pwd
, &sid
, &pass_last_set_time
)) {
98 nt_status
= NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE
;
100 if (!pdb_set_trusteddom_pw(domain
, new_trust_passwd
, &sid
)) {
101 nt_status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
113 /*********************************************************
114 Change the domain password on the PDC.
115 Do most of the legwork ourselfs. Caller must have
116 already setup the connection to the NETLOGON pipe
117 **********************************************************/
119 NTSTATUS
trust_pw_find_change_and_store_it(struct rpc_pipe_client
*cli
,
123 unsigned char old_trust_passwd_hash
[16];
124 enum netr_SchannelType sec_channel_type
= SEC_CHAN_NULL
;
125 const char *account_name
;
127 if (!get_trust_pw_hash(domain
, old_trust_passwd_hash
, &account_name
,
128 &sec_channel_type
)) {
129 DEBUG(0, ("could not fetch domain secrets for domain %s!\n", domain
));
130 return NT_STATUS_UNSUCCESSFUL
;
133 return trust_pw_change_and_store_it(cli
, mem_ctx
, domain
,
135 old_trust_passwd_hash
,
139 /*********************************************************************
140 Enumerate the list of trusted domains from a DC
141 *********************************************************************/
143 bool enumerate_domain_trusts( TALLOC_CTX
*mem_ctx
, const char *domain
,
144 char ***domain_names
, uint32
*num_domains
,
145 struct dom_sid
**sids
)
147 struct policy_handle pol
;
148 NTSTATUS status
, result
;
150 struct sockaddr_storage dc_ss
;
152 struct cli_state
*cli
= NULL
;
153 struct rpc_pipe_client
*lsa_pipe
= NULL
;
154 struct lsa_DomainList dom_list
;
156 struct dcerpc_binding_handle
*b
= NULL
;
158 *domain_names
= NULL
;
162 /* lookup a DC first */
164 if ( !get_dc_name(domain
, NULL
, dc_name
, &dc_ss
) ) {
165 DEBUG(3,("enumerate_domain_trusts: can't locate a DC for domain %s\n",
170 /* setup the anonymous connection */
172 status
= cli_full_connection( &cli
, lp_netbios_name(), dc_name
, &dc_ss
, 0, "IPC$", "IPC",
173 "", "", "", 0, Undefined
);
174 if ( !NT_STATUS_IS_OK(status
) )
177 /* open the LSARPC_PIPE */
179 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
181 if (!NT_STATUS_IS_OK(status
)) {
185 b
= lsa_pipe
->binding_handle
;
189 status
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, True
,
190 LSA_POLICY_VIEW_LOCAL_INFORMATION
, &pol
);
191 if ( !NT_STATUS_IS_OK(status
) )
194 /* Lookup list of trusted domains */
196 status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
202 if ( !NT_STATUS_IS_OK(status
) )
204 if (!NT_STATUS_IS_OK(result
)) {
209 *num_domains
= dom_list
.count
;
211 *domain_names
= talloc_zero_array(mem_ctx
, char *, *num_domains
);
212 if (!*domain_names
) {
213 status
= NT_STATUS_NO_MEMORY
;
217 *sids
= talloc_zero_array(mem_ctx
, struct dom_sid
, *num_domains
);
219 status
= NT_STATUS_NO_MEMORY
;
223 for (i
=0; i
< *num_domains
; i
++) {
224 (*domain_names
)[i
] = discard_const_p(char, dom_list
.domains
[i
].name
.string
);
225 (*sids
)[i
] = *dom_list
.domains
[i
].sid
;
231 DEBUG(10,("enumerate_domain_trusts: shutting down connection...\n"));
235 return NT_STATUS_IS_OK(status
);
238 NTSTATUS
change_trust_account_password( const char *domain
, const char *remote_machine
)
240 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
241 struct sockaddr_storage pdc_ss
;
243 struct cli_state
*cli
= NULL
;
244 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
246 DEBUG(5,("change_trust_account_password: Attempting to change trust account password in domain %s....\n",
249 if (remote_machine
== NULL
|| !strcmp(remote_machine
, "*")) {
250 /* Use the PDC *only* for this */
252 if ( !get_pdc_ip(domain
, &pdc_ss
) ) {
253 DEBUG(0,("Can't get IP for PDC for domain %s\n", domain
));
257 if ( !name_status_find( domain
, 0x1b, 0x20, &pdc_ss
, dc_name
) )
260 /* supoport old deprecated "smbpasswd -j DOMAIN -r MACHINE" behavior */
261 fstrcpy( dc_name
, remote_machine
);
264 /* if this next call fails, then give up. We can't do
265 password changes on BDC's --jerry */
267 if (!NT_STATUS_IS_OK(cli_full_connection(&cli
, lp_netbios_name(), dc_name
,
271 "", 0, Undefined
))) {
272 DEBUG(0,("modify_trust_password: Connection to %s failed!\n", dc_name
));
273 nt_status
= NT_STATUS_UNSUCCESSFUL
;
278 * Ok - we have an anonymous connection to the IPC$ share.
279 * Now start the NT Domain stuff :-).
282 /* Shouldn't we open this with schannel ? JRA. */
284 nt_status
= cli_rpc_pipe_open_noauth(
285 cli
, &ndr_table_netlogon
.syntax_id
, &netlogon_pipe
);
286 if (!NT_STATUS_IS_OK(nt_status
)) {
287 DEBUG(0,("modify_trust_password: unable to open the domain client session to machine %s. Error was : %s.\n",
288 dc_name
, nt_errstr(nt_status
)));
294 nt_status
= trust_pw_find_change_and_store_it(
295 netlogon_pipe
, netlogon_pipe
, domain
);
301 if (!NT_STATUS_IS_OK(nt_status
)) {
302 DEBUG(0,("%s : change_trust_account_password: Failed to change password for domain %s.\n",
303 current_timestring(talloc_tos(), False
), domain
));
306 DEBUG(5,("change_trust_account_password: sucess!\n"));