first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[nivanova/samba-autobuild/.git] / source / rpc_client / ntclienttrust.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NT Domain Authentication SMB / MSRPC client
5    Copyright (C) Andrew Tridgell 1994-1997
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1997
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #ifdef SYSLOG
24 #undef SYSLOG
25 #endif
26
27 #include "includes.h"
28 #include "nterr.h"
29
30 extern int DEBUGLEVEL;
31
32
33 /************************************************************************
34  check workstation trust account status
35  ************************************************************************/
36 BOOL trust_account_check(struct in_addr dest_ip, char *dest_host,
37                                 char *hostname, char *domain, fstring mach_acct,
38                                 fstring new_mach_pwd)
39 {
40         pstring tmp;
41         fstring mach_pwd;
42         struct cli_state cli_trust;
43         uchar lm_owf_mach_pwd[16];
44         uchar nt_owf_mach_pwd[16];
45         uchar lm_sess_pwd[24];
46         uchar nt_sess_pwd[24];
47
48         BOOL right_error_code = False;
49         uint8 err_cls;
50         uint32 err_num;
51
52         char *start_mach_pwd;
53         char *change_mach_pwd;
54
55         /* initial machine password */
56         fstrcpy(mach_pwd, hostname);
57         strlower(mach_pwd);
58
59         slprintf(tmp, sizeof(tmp) - 1,"Enter Workstation Trust Account password for [%s].\nDefault is [%s].\nPassword:",
60                                 mach_acct, mach_pwd);
61
62         start_mach_pwd = (char*)getpass(tmp);
63
64         if (start_mach_pwd[0] != 0)
65         {
66                 fstrcpy(mach_pwd, start_mach_pwd);
67         }
68
69         slprintf(tmp, sizeof(tmp)-1, "Enter new Workstation Trust Account password for [%s]\nPress Return to leave at old value.\nNew Password:",
70                                 mach_acct);
71
72         change_mach_pwd = (char*)getpass(tmp);
73
74         if (change_mach_pwd[0] != 0)
75         {
76                 fstrcpy(new_mach_pwd, change_mach_pwd);
77         }
78         else
79         {
80                 DEBUG(1,("trust_account_check: password change not requested\n"));
81                 change_mach_pwd[0] = 0;
82         }
83
84         DEBUG(1,("initialise cli_trust connection\n"));
85
86         if (!cli_initialise(&cli_trust))
87         {
88                 DEBUG(1,("cli_initialise failed for cli_trust\n"));
89                 return False;
90         }
91
92         DEBUG(1,("server connect for cli_trust\n"));
93
94         if (!server_connect_init(&cli_trust, hostname, dest_ip, dest_host))
95         {
96                 cli_error(&cli_trust, &err_cls, &err_num, NULL);
97                 DEBUG(1,("server_connect_init failed (%s)\n", cli_errstr(&cli_trust)));
98
99                 cli_shutdown(&cli_trust);
100                 return False;
101         }
102
103         DEBUG(1,("server connect cli_trust succeeded\n"));
104
105         nt_lm_owf_gen(mach_pwd, nt_owf_mach_pwd, lm_owf_mach_pwd);
106
107         DEBUG(5,("generating nt owf from initial machine pwd: %s\n", mach_pwd));
108
109 #ifdef DEBUG_PASSWORD
110         DEBUG(100,("client cryptkey: "));
111         dump_data(100, cli_trust.cryptkey, sizeof(cli_trust.cryptkey));
112 #endif
113
114         SMBencrypt(nt_owf_mach_pwd, cli_trust.cryptkey, nt_sess_pwd);
115
116 #ifdef DEBUG_PASSWORD
117         DEBUG(100,("nt_owf_mach_pwd: "));
118         dump_data(100, nt_owf_mach_pwd, sizeof(lm_owf_mach_pwd));
119         DEBUG(100,("nt_sess_pwd: "));
120         dump_data(100, nt_sess_pwd, sizeof(nt_sess_pwd));
121 #endif
122
123         SMBencrypt(lm_owf_mach_pwd, cli_trust.cryptkey, lm_sess_pwd);
124
125 #ifdef DEBUG_PASSWORD
126         DEBUG(100,("lm_owf_mach_pwd: "));
127         dump_data(100, lm_owf_mach_pwd, sizeof(lm_owf_mach_pwd));
128         DEBUG(100,("lm_sess_pwd: "));
129         dump_data(100, lm_sess_pwd, sizeof(lm_sess_pwd));
130 #endif
131
132         right_error_code = False;
133
134         if (cli_session_setup(&cli_trust, mach_acct, 
135                         nt_owf_mach_pwd, sizeof(nt_owf_mach_pwd),
136                         nt_owf_mach_pwd, sizeof(nt_owf_mach_pwd), domain))
137         {
138                 DEBUG(0,("cli_session_setup: NO ERROR! AAAGH! BUG IN SERVER DETECTED!!!\n"));
139                 cli_shutdown(&cli_trust);
140         
141                 return False;
142         }
143
144         cli_error(&cli_trust, &err_cls, &err_num, NULL);
145
146         if (err_num == (0xC0000000 | NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT))
147         {
148                 DEBUG(1,("cli_send_tconX: valid workstation trust account exists\n"));
149                 right_error_code = True;
150         }
151
152         if (err_num == (0xC0000000 | NT_STATUS_NO_SUCH_USER))
153         {
154                 DEBUG(1,("cli_send_tconX: workstation trust account does not exist\n"));
155                 right_error_code = False;
156         }
157
158         if (!right_error_code)
159         {
160                 DEBUG(1,("server_validate failed (%s)\n", cli_errstr(&cli_trust)));
161         }
162
163         cli_shutdown(&cli_trust);
164         return right_error_code;
165 }
166
167