first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[kamenim/samba-autobuild/.git] / source3 / libsmb / passchange.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB client password change routine
5    Copyright (C) Andrew Tridgell 1994-1998
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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24
25 extern pstring global_myname;
26 extern pstring scope;
27
28 /*************************************************************
29 change a password on a remote machine using IPC calls
30 *************************************************************/
31 BOOL remote_password_change(const char *remote_machine, const char *user_name, 
32                             const char *old_passwd, const char *new_passwd,
33                             char *err_str, size_t err_str_len)
34 {
35         struct nmb_name calling, called;
36         struct cli_state cli;
37         struct in_addr ip;
38
39         *err_str = '\0';
40
41         if(!resolve_name( remote_machine, &ip, 0x20)) {
42                 slprintf(err_str, err_str_len-1, "unable to find an IP address for machine %s.\n",
43                         remote_machine );
44                 return False;
45         }
46  
47         ZERO_STRUCT(cli);
48  
49         if (!cli_initialise(&cli) || !cli_connect(&cli, remote_machine, &ip)) {
50                 slprintf(err_str, err_str_len-1, "unable to connect to SMB server on machine %s. Error was : %s.\n",
51                         remote_machine, cli_errstr(&cli) );
52                 return False;
53         }
54   
55         make_nmb_name(&calling, global_myname , 0x0 , scope);
56         make_nmb_name(&called , remote_machine, 0x20, scope);
57         
58         if (!cli_session_request(&cli, &calling, &called)) {
59                 slprintf(err_str, err_str_len-1, "machine %s rejected the session setup. Error was : %s.\n",
60                         remote_machine, cli_errstr(&cli) );
61                 cli_shutdown(&cli);
62                 return False;
63         }
64   
65         cli.protocol = PROTOCOL_NT1;
66
67         if (!cli_negprot(&cli)) {
68                 slprintf(err_str, err_str_len-1, "machine %s rejected the negotiate protocol. Error was : %s.\n",        
69                         remote_machine, cli_errstr(&cli) );
70                 cli_shutdown(&cli);
71                 return False;
72         }
73   
74         /*
75          * We should connect as the anonymous user here, in case
76          * the server has "must change password" checked...
77          * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
78          */
79
80         if (!cli_session_setup(&cli, "", "", 0, "", 0, "")) {
81                 slprintf(err_str, err_str_len-1, "machine %s rejected the session setup. Error was : %s.\n",        
82                         remote_machine, cli_errstr(&cli) );
83                 cli_shutdown(&cli);
84                 return False;
85         }               
86
87         if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
88                 slprintf(err_str, err_str_len-1, "machine %s rejected the tconX on the IPC$ share. Error was : %s.\n",
89                         remote_machine, cli_errstr(&cli) );
90                 cli_shutdown(&cli);
91                 return False;
92         }
93
94         if(!cli_oem_change_password(&cli, user_name, new_passwd, old_passwd)) {
95                 slprintf(err_str, err_str_len-1, "machine %s rejected the password change: Error was : %s.\n",
96                         remote_machine, cli_errstr(&cli) );
97                 cli_shutdown(&cli);
98                 return False;
99         }
100         
101         cli_shutdown(&cli);
102         return True;
103 }