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 /*********************************************************
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
,
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
;
46 switch (sec_channel_type
) {
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
,
66 orig_trust_passwd_hash
,
68 new_trust_passwd_hash
,
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
) {
82 if (!secrets_store_machine_password(new_trust_passwd
, domain
, sec_channel_type
)) {
83 nt_status
= NT_STATUS_UNSUCCESSFUL
;
87 case SEC_CHAN_DOMAIN
: {
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
;
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
,
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
,
133 old_trust_passwd_hash
,
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
;
148 struct sockaddr_storage dc_ss
;
150 struct cli_state
*cli
= NULL
;
151 struct rpc_pipe_client
*lsa_pipe
= NULL
;
152 struct lsa_DomainList dom_list
;
154 struct dcerpc_binding_handle
*b
= NULL
;
156 *domain_names
= 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",
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
) )
175 /* open the LSARPC_PIPE */
177 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
179 if (!NT_STATUS_IS_OK(status
)) {
183 b
= lsa_pipe
->binding_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
) )
192 /* Lookup list of trusted domains */
194 status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
200 if ( !NT_STATUS_IS_OK(status
) )
202 if (!NT_STATUS_IS_OK(result
)) {
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
;
215 *sids
= TALLOC_ZERO_ARRAY(mem_ctx
, struct dom_sid
, *num_domains
);
217 status
= NT_STATUS_NO_MEMORY
;
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
;
229 DEBUG(10,("enumerate_domain_trusts: shutting down connection...\n"));
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
;
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",
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
));
255 if ( !name_status_find( domain
, 0x1b, 0x20, &pdc_ss
, dc_name
) )
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
,
269 "", 0, Undefined
))) {
270 DEBUG(0,("modify_trust_password: Connection to %s failed!\n", dc_name
));
271 nt_status
= NT_STATUS_UNSUCCESSFUL
;
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
)));
292 nt_status
= trust_pw_find_change_and_store_it(
293 netlogon_pipe
, netlogon_pipe
, domain
);
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
));
304 DEBUG(5,("change_trust_account_password: sucess!\n"));