Removed global_myworkgroup, global_myname, global_myscope. Added liberal
[ira/wip.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           DEBUG(0,("modify_trust_password: Connection to %s failed!\n", remote_machine));
53           return NT_STATUS_UNSUCCESSFUL;
54   }
55       
56   /*
57    * Ok - we have an anonymous connection to the IPC$ share.
58    * Now start the NT Domain stuff :-).
59    */
60
61   if(cli_nt_session_open(cli, PI_NETLOGON) == False) {
62     DEBUG(0,("modify_trust_password: unable to open the domain client session to \
63 machine %s. Error was : %s.\n", remote_machine, cli_errstr(cli)));
64     cli_nt_session_close(cli);
65     cli_ulogoff(cli);
66     cli_shutdown(cli);
67     return NT_STATUS_UNSUCCESSFUL;
68   } 
69
70   nt_status = trust_pw_change_and_store_it(cli, cli->mem_ctx,
71                                            orig_trust_passwd_hash);
72   
73   cli_nt_session_close(cli);
74   cli_ulogoff(cli);
75   cli_shutdown(cli);
76   return nt_status;
77 }
78
79 /************************************************************************
80  Change the trust account password for a domain.
81 ************************************************************************/
82
83 NTSTATUS change_trust_account_password( const char *domain, const char *remote_machine_list)
84 {
85   fstring remote_machine;
86   unsigned char old_trust_passwd_hash[16];
87   time_t lct;
88   NTSTATUS res = NT_STATUS_UNSUCCESSFUL;
89
90   if(!secrets_fetch_trust_account_password(domain, old_trust_passwd_hash, &lct)) {
91     DEBUG(0,("change_trust_account_password: unable to read the machine \
92 account password for domain %s.\n", domain));
93     return NT_STATUS_UNSUCCESSFUL;
94   }
95
96   while(remote_machine_list && 
97         next_token(&remote_machine_list, remote_machine, 
98                    LIST_SEP, sizeof(remote_machine))) {
99     strupper(remote_machine);
100     if(strequal(remote_machine, "*")) {
101
102       /*
103        * We have been asked to dynamcially determine the IP addresses of the PDC.
104        */
105
106       struct in_addr pdc_ip;
107       fstring dc_name;
108
109       /* Use the PDC *only* for this. */
110       if(!get_pdc_ip(domain, &pdc_ip))
111         continue;
112
113       /*
114        * Try and connect to the PDC/BDC list in turn as an IP
115        * address used as a string.
116        */
117
118         if(!lookup_dc_name(global_myname(), domain, &pdc_ip, dc_name))
119           continue;
120         if(NT_STATUS_IS_OK(res = modify_trust_password( domain, dc_name,
121                                          old_trust_passwd_hash)))
122           break;
123     } else {
124             res = modify_trust_password( domain, remote_machine,
125                                          old_trust_passwd_hash);
126     }
127
128   }
129
130   if (!NT_STATUS_IS_OK(res)) {
131           DEBUG(0,("%s : change_trust_account_password: Failed to change password for \
132 domain %s.\n", timestring(False), domain));
133   }
134   
135   return res;
136 }