Remove "store create time" code, cause create time to be stored
[Samba/fernandojvsilva.git] / source3 / libsmb / trusts_util.c
blobe127c9a67be4f613d7573d8ec8701668eab6b434
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"
24 /*********************************************************
25 Change the domain password on the PDC.
26 Store the password ourselves, but use the supplied password
27 Caller must have already setup the connection to the NETLOGON pipe
28 **********************************************************/
30 NTSTATUS trust_pw_change_and_store_it(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
31 const char *domain,
32 const char *account_name,
33 unsigned char orig_trust_passwd_hash[16],
34 enum netr_SchannelType sec_channel_type)
36 unsigned char new_trust_passwd_hash[16];
37 char *new_trust_passwd;
38 NTSTATUS nt_status;
40 switch (sec_channel_type) {
41 case SEC_CHAN_WKSTA:
42 case SEC_CHAN_DOMAIN:
43 break;
44 default:
45 return NT_STATUS_NOT_SUPPORTED;
48 /* Create a random machine account password */
49 new_trust_passwd = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
51 if (new_trust_passwd == NULL) {
52 DEBUG(0, ("talloc_strdup failed\n"));
53 return NT_STATUS_NO_MEMORY;
56 E_md4hash(new_trust_passwd, new_trust_passwd_hash);
58 nt_status = rpccli_netlogon_set_trust_password(cli, mem_ctx,
59 account_name,
60 orig_trust_passwd_hash,
61 new_trust_passwd,
62 new_trust_passwd_hash,
63 sec_channel_type);
65 if (NT_STATUS_IS_OK(nt_status)) {
66 DEBUG(3,("%s : trust_pw_change_and_store_it: Changed password.\n",
67 current_timestring(talloc_tos(), False)));
69 * Return the result of trying to write the new password
70 * back into the trust account file.
73 switch (sec_channel_type) {
75 case SEC_CHAN_WKSTA:
76 if (!secrets_store_machine_password(new_trust_passwd, domain, sec_channel_type)) {
77 nt_status = NT_STATUS_UNSUCCESSFUL;
79 break;
81 case SEC_CHAN_DOMAIN: {
82 char *pwd;
83 struct dom_sid sid;
84 time_t pass_last_set_time;
86 /* we need to get the sid first for the
87 * pdb_set_trusteddom_pw call */
89 if (!pdb_get_trusteddom_pw(domain, &pwd, &sid, &pass_last_set_time)) {
90 nt_status = NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE;
92 if (!pdb_set_trusteddom_pw(domain, new_trust_passwd, &sid)) {
93 nt_status = NT_STATUS_INTERNAL_DB_CORRUPTION;
95 break;
97 default:
98 break;
102 return nt_status;
105 /*********************************************************
106 Change the domain password on the PDC.
107 Do most of the legwork ourselfs. Caller must have
108 already setup the connection to the NETLOGON pipe
109 **********************************************************/
111 NTSTATUS trust_pw_find_change_and_store_it(struct rpc_pipe_client *cli,
112 TALLOC_CTX *mem_ctx,
113 const char *domain)
115 unsigned char old_trust_passwd_hash[16];
116 enum netr_SchannelType sec_channel_type = SEC_CHAN_NULL;
117 const char *account_name;
119 if (!get_trust_pw_hash(domain, old_trust_passwd_hash, &account_name,
120 &sec_channel_type)) {
121 DEBUG(0, ("could not fetch domain secrets for domain %s!\n", domain));
122 return NT_STATUS_UNSUCCESSFUL;
125 return trust_pw_change_and_store_it(cli, mem_ctx, domain,
126 account_name,
127 old_trust_passwd_hash,
128 sec_channel_type);
131 /*********************************************************************
132 Enumerate the list of trusted domains from a DC
133 *********************************************************************/
135 bool enumerate_domain_trusts( TALLOC_CTX *mem_ctx, const char *domain,
136 char ***domain_names, uint32 *num_domains,
137 DOM_SID **sids )
139 struct policy_handle pol;
140 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
141 fstring dc_name;
142 struct sockaddr_storage dc_ss;
143 uint32 enum_ctx = 0;
144 struct cli_state *cli = NULL;
145 struct rpc_pipe_client *lsa_pipe = NULL;
146 bool retry;
147 struct lsa_DomainList dom_list;
148 int i;
150 *domain_names = NULL;
151 *num_domains = 0;
152 *sids = NULL;
154 /* lookup a DC first */
156 if ( !get_dc_name(domain, NULL, dc_name, &dc_ss) ) {
157 DEBUG(3,("enumerate_domain_trusts: can't locate a DC for domain %s\n",
158 domain));
159 return False;
162 /* setup the anonymous connection */
164 result = cli_full_connection( &cli, global_myname(), dc_name, &dc_ss, 0, "IPC$", "IPC",
165 "", "", "", 0, Undefined, &retry);
166 if ( !NT_STATUS_IS_OK(result) )
167 goto done;
169 /* open the LSARPC_PIPE */
171 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
172 &lsa_pipe);
173 if (!NT_STATUS_IS_OK(result)) {
174 goto done;
177 /* get a handle */
179 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
180 LSA_POLICY_VIEW_LOCAL_INFORMATION, &pol);
181 if ( !NT_STATUS_IS_OK(result) )
182 goto done;
184 /* Lookup list of trusted domains */
186 result = rpccli_lsa_EnumTrustDom(lsa_pipe, mem_ctx,
187 &pol,
188 &enum_ctx,
189 &dom_list,
190 (uint32_t)-1);
191 if ( !NT_STATUS_IS_OK(result) )
192 goto done;
194 *num_domains = dom_list.count;
196 *domain_names = TALLOC_ZERO_ARRAY(mem_ctx, char *, *num_domains);
197 if (!*domain_names) {
198 result = NT_STATUS_NO_MEMORY;
199 goto done;
202 *sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, *num_domains);
203 if (!*sids) {
204 result = NT_STATUS_NO_MEMORY;
205 goto done;
208 for (i=0; i< *num_domains; i++) {
209 (*domain_names)[i] = CONST_DISCARD(char *, dom_list.domains[i].name.string);
210 (*sids)[i] = *dom_list.domains[i].sid;
213 done:
214 /* cleanup */
215 if (cli) {
216 DEBUG(10,("enumerate_domain_trusts: shutting down connection...\n"));
217 cli_shutdown( cli );
220 return NT_STATUS_IS_OK(result);