Removed version number from file header.
[samba.git] / source / libsmb / passchange.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB client password change routine
4    Copyright (C) Andrew Tridgell 1994-1998
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23
24 extern pstring global_myname;
25
26 /*************************************************************
27 change a password on a remote machine using IPC calls
28 *************************************************************/
29 BOOL remote_password_change(const char *remote_machine, const char *user_name, 
30                             const char *old_passwd, const char *new_passwd,
31                             char *err_str, size_t err_str_len)
32 {
33         struct nmb_name calling, called;
34         struct cli_state cli;
35         struct in_addr ip;
36
37         *err_str = '\0';
38
39         if(!resolve_name( remote_machine, &ip, 0x20)) {
40                 slprintf(err_str, err_str_len-1, "unable to find an IP address for machine %s.\n",
41                         remote_machine );
42                 return False;
43         }
44  
45         ZERO_STRUCT(cli);
46  
47         if (!cli_initialise(&cli) || !cli_connect(&cli, remote_machine, &ip)) {
48                 slprintf(err_str, err_str_len-1, "unable to connect to SMB server on machine %s. Error was : %s.\n",
49                         remote_machine, cli_errstr(&cli) );
50                 return False;
51         }
52   
53         make_nmb_name(&calling, global_myname , 0x0);
54         make_nmb_name(&called , remote_machine, 0x20);
55         
56         if (!cli_session_request(&cli, &calling, &called)) {
57                 slprintf(err_str, err_str_len-1, "machine %s rejected the session setup. Error was : %s.\n",
58                         remote_machine, cli_errstr(&cli) );
59                 cli_shutdown(&cli);
60                 return False;
61         }
62   
63         cli.protocol = PROTOCOL_NT1;
64
65         if (!cli_negprot(&cli)) {
66                 slprintf(err_str, err_str_len-1, "machine %s rejected the negotiate protocol. Error was : %s.\n",        
67                         remote_machine, cli_errstr(&cli) );
68                 cli_shutdown(&cli);
69                 return False;
70         }
71   
72         /*
73          * We should connect as the anonymous user here, in case
74          * the server has "must change password" checked...
75          * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
76          */
77
78         if (!cli_session_setup(&cli, "", "", 0, "", 0, "")) {
79                 slprintf(err_str, err_str_len-1, "machine %s rejected the session setup. Error was : %s.\n",        
80                         remote_machine, cli_errstr(&cli) );
81                 cli_shutdown(&cli);
82                 return False;
83         }               
84
85         if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
86                 slprintf(err_str, err_str_len-1, "machine %s rejected the tconX on the IPC$ share. Error was : %s.\n",
87                         remote_machine, cli_errstr(&cli) );
88                 cli_shutdown(&cli);
89                 return False;
90         }
91
92         if(!cli_oem_change_password(&cli, user_name, new_passwd, old_passwd)) {
93                 slprintf(err_str, err_str_len-1, "machine %s rejected the password change: Error was : %s.\n",
94                         remote_machine, cli_errstr(&cli) );
95                 cli_shutdown(&cli);
96                 return False;
97         }
98         
99         cli_shutdown(&cli);
100         return True;
101 }