Removed version number from file header.
[kai/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 extern pstring global_myname;
28
29 /*********************************************************
30  Change the domain password on the PDC.
31 **********************************************************/
32
33 static NTSTATUS modify_trust_password( char *domain, char *remote_machine, 
34                                    unsigned char orig_trust_passwd_hash[16])
35 {
36   struct cli_state *cli;
37   DOM_SID domain_sid;
38   struct in_addr dest_ip;
39   NTSTATUS nt_status;
40
41   /*
42    * Ensure we have the domain SID for this domain.
43    */
44
45   if (!secrets_fetch_domain_sid(domain, &domain_sid)) {
46     DEBUG(0, ("domain_client_validate: unable to fetch domain sid.\n"));
47     return NT_STATUS_UNSUCCESSFUL;
48   }
49
50   if(!resolve_name( remote_machine, &dest_ip, 0x20)) {
51           DEBUG(0,("modify_trust_password: Can't resolve address for %s\n", remote_machine));
52           return NT_STATUS_UNSUCCESSFUL;
53   }
54   
55   if (!NT_STATUS_IS_OK(cli_full_connection(&cli, global_myname, remote_machine, 
56                                            &dest_ip, 0,
57                                            "IPC$", "IPC",  
58                                            "", "",
59                                            "", 0))) {
60           DEBUG(0,("modify_trust_password: Connection to %s failed!\n", remote_machine));
61           return NT_STATUS_UNSUCCESSFUL;
62   }
63       
64   /*
65    * Ok - we have an anonymous connection to the IPC$ share.
66    * Now start the NT Domain stuff :-).
67    */
68
69   if(cli_nt_session_open(cli, PIPE_NETLOGON) == False) {
70     DEBUG(0,("modify_trust_password: unable to open the domain client session to \
71 machine %s. Error was : %s.\n", remote_machine, cli_errstr(cli)));
72     cli_nt_session_close(cli);
73     cli_ulogoff(cli);
74     cli_shutdown(cli);
75     return NT_STATUS_UNSUCCESSFUL;
76   } 
77
78   nt_status = trust_pw_change_and_store_it(cli, cli->mem_ctx,
79                                            orig_trust_passwd_hash);
80   
81   cli_nt_session_close(cli);
82   cli_ulogoff(cli);
83   cli_shutdown(cli);
84   return nt_status;
85 }
86
87 /************************************************************************
88  Change the trust account password for a domain.
89 ************************************************************************/
90
91 NTSTATUS change_trust_account_password( char *domain, char *remote_machine_list)
92 {
93   fstring remote_machine;
94   unsigned char old_trust_passwd_hash[16];
95   time_t lct;
96   NTSTATUS res = NT_STATUS_UNSUCCESSFUL;
97
98   if(!secrets_fetch_trust_account_password(domain, old_trust_passwd_hash, &lct)) {
99     DEBUG(0,("change_trust_account_password: unable to read the machine \
100 account password for domain %s.\n", domain));
101     return NT_STATUS_UNSUCCESSFUL;
102   }
103
104   while(remote_machine_list && 
105         next_token(&remote_machine_list, remote_machine, 
106                    LIST_SEP, sizeof(remote_machine))) {
107     strupper(remote_machine);
108     if(strequal(remote_machine, "*")) {
109
110       /*
111        * We have been asked to dynamcially determine the IP addresses of the PDC.
112        */
113
114       struct in_addr *ip_list = NULL;
115       int count = 0;
116       int i;
117
118       /* Use the PDC *only* for this. */
119       if(!get_dc_list(True, domain, &ip_list, &count))
120         continue;
121
122       /*
123        * Try and connect to the PDC/BDC list in turn as an IP
124        * address used as a string.
125        */
126
127       for(i = 0; i < count; i++) {
128         fstring dc_name;
129         if(!lookup_dc_name(global_myname, domain, &ip_list[i], dc_name))
130           continue;
131         if(NT_STATUS_IS_OK(res = modify_trust_password( domain, dc_name,
132                                          old_trust_passwd_hash)))
133           break;
134       }
135
136       SAFE_FREE(ip_list);
137
138     } else {
139             res = modify_trust_password( domain, remote_machine,
140                                          old_trust_passwd_hash);
141     }
142
143   }
144
145   if (!NT_STATUS_IS_OK(res)) {
146           DEBUG(0,("%s : change_trust_account_password: Failed to change password for \
147 domain %s.\n", timestring(False), domain));
148   }
149   
150   return res;
151 }