dns: Use new DNS debugclass in DNS server
[kai/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 "rpc_client/cli_netlogon.h"
24 #include "rpc_client/cli_pipe.h"
25 #include "../librpc/gen_ndr/ndr_netlogon.h"
26 #include "secrets.h"
27 #include "passdb.h"
28 #include "libsmb/libsmb.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 NTSTATUS change_trust_account_password( const char *domain, const char *remote_machine)
138 {
139         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
140         struct sockaddr_storage pdc_ss;
141         fstring dc_name;
142         struct cli_state *cli = NULL;
143         struct rpc_pipe_client *netlogon_pipe = NULL;
144
145         DEBUG(5,("change_trust_account_password: Attempting to change trust account password in domain %s....\n",
146                 domain));
147
148         if (remote_machine == NULL || !strcmp(remote_machine, "*")) {
149                 /* Use the PDC *only* for this */
150
151                 if ( !get_pdc_ip(domain, &pdc_ss) ) {
152                         DEBUG(0,("Can't get IP for PDC for domain %s\n", domain));
153                         goto failed;
154                 }
155
156                 if ( !name_status_find( domain, 0x1b, 0x20, &pdc_ss, dc_name) )
157                         goto failed;
158         } else {
159                 /* supoport old deprecated "smbpasswd -j DOMAIN -r MACHINE" behavior */
160                 fstrcpy( dc_name, remote_machine );
161         }
162
163         /* if this next call fails, then give up.  We can't do
164            password changes on BDC's  --jerry */
165
166         if (!NT_STATUS_IS_OK(cli_full_connection(&cli, lp_netbios_name(), dc_name,
167                                            NULL, 0,
168                                            "IPC$", "IPC",
169                                            "", "",
170                                            "", 0, SMB_SIGNING_DEFAULT))) {
171                 DEBUG(0,("modify_trust_password: Connection to %s failed!\n", dc_name));
172                 nt_status = NT_STATUS_UNSUCCESSFUL;
173                 goto failed;
174         }
175
176         /*
177          * Ok - we have an anonymous connection to the IPC$ share.
178          * Now start the NT Domain stuff :-).
179          */
180
181         /* Shouldn't we open this with schannel ? JRA. */
182
183         nt_status = cli_rpc_pipe_open_noauth(
184                 cli, &ndr_table_netlogon.syntax_id, &netlogon_pipe);
185         if (!NT_STATUS_IS_OK(nt_status)) {
186                 DEBUG(0,("modify_trust_password: unable to open the domain client session to machine %s. Error was : %s.\n",
187                         dc_name, nt_errstr(nt_status)));
188                 cli_shutdown(cli);
189                 cli = NULL;
190                 goto failed;
191         }
192
193         nt_status = trust_pw_find_change_and_store_it(
194                 netlogon_pipe, netlogon_pipe, domain);
195
196         cli_shutdown(cli);
197         cli = NULL;
198
199 failed:
200         if (!NT_STATUS_IS_OK(nt_status)) {
201                 DEBUG(0,("%s : change_trust_account_password: Failed to change password for domain %s.\n",
202                         current_timestring(talloc_tos(), False), domain));
203         }
204         else
205                 DEBUG(5,("change_trust_account_password: sucess!\n"));
206
207         return nt_status;
208 }