change_trust_account_password() must always use the PDC for rpc
[gd/samba/.git] / source3 / smbd / change_trust_pw.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Periodic Trust account password changing.
4  *  Copyright (C) Andrew Tridgell              1992-1997,
5  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6  *  Copyright (C) Paul Ashton                       1997.
7  *  Copyright (C) Jeremy Allison                    1998.
8  *  Copyright (C) Andrew Bartlett                   2001.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include "includes.h"
26
27 /*********************************************************
28  Change the domain password on the PDC.
29 **********************************************************/
30
31 static NTSTATUS modify_trust_password( const char *domain, const char *remote_machine, 
32                                    unsigned char orig_trust_passwd_hash[16])
33 {
34         struct cli_state *cli;
35         DOM_SID domain_sid;
36         NTSTATUS nt_status;
37
38         /*
39          * Ensure we have the domain SID for this domain.
40          */
41
42         if (!secrets_fetch_domain_sid(domain, &domain_sid)) {
43                 DEBUG(0, ("modify_trust_password: unable to fetch domain sid.\n"));
44                 return NT_STATUS_UNSUCCESSFUL;
45         }
46
47         if (!NT_STATUS_IS_OK(cli_full_connection(&cli, global_myname(), remote_machine, 
48                                            NULL, 0,
49                                            "IPC$", "IPC",  
50                                            "", "",
51                                            "", 0, NULL))) 
52         {
53                 DEBUG(0,("modify_trust_password: Connection to %s failed!\n", remote_machine));
54                 return NT_STATUS_UNSUCCESSFUL;
55         }
56       
57         /*
58          * Ok - we have an anonymous connection to the IPC$ share.
59          * Now start the NT Domain stuff :-).
60          */
61
62         if(cli_nt_session_open(cli, PI_NETLOGON) == False) {
63                 DEBUG(0,("modify_trust_password: unable to open the domain client session to machine %s. Error was : %s.\n", 
64                         remote_machine, cli_errstr(cli)));
65                 cli_nt_session_close(cli);
66                 cli_ulogoff(cli);
67                 cli_shutdown(cli);
68                 return NT_STATUS_UNSUCCESSFUL;
69         }
70
71         nt_status = trust_pw_change_and_store_it(cli, cli->mem_ctx,
72                                            orig_trust_passwd_hash);
73   
74         cli_nt_session_close(cli);
75         cli_ulogoff(cli);
76         cli_shutdown(cli);
77         
78         return nt_status;
79 }
80
81 /************************************************************************
82  Change the trust account password for a domain.
83 ************************************************************************/
84
85 NTSTATUS change_trust_account_password( const char *domain, const char *remote_machine)
86 {
87         unsigned char old_trust_passwd_hash[16];
88         time_t lct;
89         NTSTATUS res = NT_STATUS_UNSUCCESSFUL;
90         struct in_addr pdc_ip;
91         fstring dc_name;
92
93
94         if(!secrets_fetch_trust_account_password(domain, old_trust_passwd_hash, &lct)) {
95                 DEBUG(0,("change_trust_account_password: unable to read the machine account password for domain %s.\n", 
96                         domain));
97                 return NT_STATUS_UNSUCCESSFUL;
98         }
99
100         if (remote_machine == NULL || !strcmp(remote_machine, "*")) {
101                 /* Use the PDC *only* for this */
102         
103                 if ( !get_pdc_ip(domain, &pdc_ip) ) {
104                         DEBUG(0,("Can't get IP for PDC for domain %s\n", domain));
105                         goto failed;
106                 }
107
108                 if ( !lookup_dc_name(global_myname(), domain, &pdc_ip, dc_name) ) 
109                         goto failed;
110         }
111         /* supoport old deprecated "smbpasswd -j DOMAIN -r MACHINE" behavior */
112         else {
113                 fstrcpy( dc_name, remote_machine );
114         }
115         
116         /* if this next call fails, then give up.  We can't do
117            password changes on BDC's  --jerry */
118            
119         res = modify_trust_password(domain, dc_name, old_trust_passwd_hash);    
120         
121 failed:
122         if (!NT_STATUS_IS_OK(res)) {
123                 DEBUG(0,("%s : change_trust_account_password: Failed to change password for domain %s.\n", 
124                         timestring(False), domain));
125         }
126   
127         return res;
128 }