This looks like a big change but really isn't.
[nivanova/samba-autobuild/.git] / source / rpc_client / cli_login.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 #include "includes.h"
24 #include "nterr.h"
25
26 extern int DEBUGLEVEL;
27
28 /****************************************************************************
29 Initialize domain session credentials.
30 ****************************************************************************/
31
32 BOOL cli_nt_setup_creds(struct cli_state *cli, unsigned char mach_pwd[16])
33 {
34   DOM_CHAL clnt_chal;
35   DOM_CHAL srv_chal;
36
37   DOM_CHAL auth2_srv_chal;
38
39   UTIME zerotime;
40
41   RPC_IFACE abstract;
42   RPC_IFACE transfer;
43
44   /******************** initialise ********************************/
45
46   zerotime.time = 0;
47
48   /******************* Request Challenge ********************/
49
50   generate_random_buffer( clnt_chal.data, 8, False);
51         
52   /* send a client challenge; receive a server challenge */
53   if (!cli_net_req_chal(cli, &clnt_chal, &srv_chal))
54   {
55     DEBUG(0,("do_nt_session_open: request challenge failed\n"));
56     return False;
57   }
58
59   /**************** Long-term Session key **************/
60
61   /* calculate the session key */
62   cred_session_key(&clnt_chal, &srv_chal, mach_pwd, sess_key);
63   bzero(sess_key+8, 8);
64
65   /******************* Authenticate 2 ********************/
66
67   /* calculate auth-2 credentials */
68   cred_create(sess_key, &clnt_chal, zerotime, &(clnt_cred->challenge));
69
70   /* send client auth-2 challenge; receive an auth-2 challenge */
71   if (!cli_net_auth2(cli, SEC_CHAN_WKSTA, 0x000001ff, 
72                      &(cli->clnt_cred->challenge), &auth2_srv_chal))
73   {
74     DEBUG(0,("do_nt_session_open: auth2 challenge failed\n"));
75     return False;
76   }
77
78   return True;
79 }
80
81 #if 0
82 /****************************************************************************
83  server password set
84  ****************************************************************************/
85
86 BOOL do_nt_srv_pwset(struct cli_state *cli, 
87                      uint8 sess_key[16], DOM_CRED *clnt_cred, DOM_CRED *rtn_cred,
88                      char *new_mach_pwd,
89                      char *dest_host, char *mach_acct, char *myhostname)
90 {
91   DOM_CRED cred;
92   char nt_cypher[16];
93   uint8 mode = 1;
94   char nt_owf_new_mach_pwd[16];
95
96 #ifdef DEBUG_PASSWORD
97   DEBUG(100,("generating nt owf from new machine pwd: %s\n", new_mach_pwd));
98 #endif
99   nt_owf_gen(new_mach_pwd, nt_owf_new_mach_pwd);
100
101 #ifdef DEBUG_PASSWORD
102   dump_data(6, nt_owf_new_mach_pwd, 16);
103 #endif
104
105   if (!obfuscate_pwd(nt_cypher, nt_owf_new_mach_pwd, mode))
106   {
107     DEBUG(5,("do_nt_srv_pwset: encrypt mach pwd failed\n"));
108     return False;
109   }
110         
111   clnt_cred->timestamp.time = time(NULL);
112
113   memcpy(&cred, clnt_cred, sizeof(cred));
114
115   /* calculate credentials */
116   cred_create(sess_key, &(clnt_cred->challenge),
117               cred.timestamp, &(cred.challenge));
118
119   /* send client srv_pwset challenge */
120   return do_net_srv_pwset(cli, fnum, sess_key, clnt_cred,
121                           dest_host, mach_acct, 2, myhostname,
122                           &cred, rtn_cred, nt_cypher);
123 }
124
125 /****************************************************************************
126  make interactive sam login info
127  ****************************************************************************/
128
129 void make_nt_login_interactive(NET_ID_INFO_CTR *ctr,
130                                uchar sess_key[16],
131                                char *workgroup, char *myhostname,
132                                uint32 smb_userid, char *username)
133 {
134   /****************** SAM Info Preparation *******************/
135
136   char *smb_user_passwd = getpass("Enter NT Login Password:");
137
138   char lm_owf_user_pwd[16];
139   char nt_owf_user_pwd[16];
140
141   nt_lm_owf_gen(smb_user_passwd, nt_owf_user_pwd, lm_owf_user_pwd);
142
143 #ifdef DEBUG_PASSWORD
144
145   DEBUG(100,("nt owf of user password: "));
146   dump_data(100, lm_owf_user_pwd, 16);
147
148   DEBUG(100,("nt owf of user password: "));
149   dump_data(100, nt_owf_user_pwd, 16);
150
151 #endif
152
153   /* indicate an "interactive" login */
154   ctr->switch_value = 1;
155
156   /* this is used in both the SAM Logon and the SAM Logoff */
157   make_id_info1(&ctr->auth.id1, workgroup, 0,
158                 smb_userid, 0, username, myhostname,
159                 sess_key, lm_owf_user_pwd, nt_owf_user_pwd);
160 }
161 #endif
162
163 /****************************************************************************
164  make network sam login info
165  ****************************************************************************/
166
167 void make_nt_login_network(NET_ID_INFO_CTR *ctr, uint32 smb_userid, char *username,
168                            char lm_chal[8], char lm_chal_resp[24], 
169                            char nt_chal_resp[24])
170 {
171   /* indicate a "network" login */
172   ctr->switch_value = 2;
173
174   /* this is used in both the SAM Logon and the SAM Logoff */
175   make_id_info2(&ctr->auth.id2, myworkgroup, 0, smb_userid, 0,
176                 username, myhostname,
177                 lm_chal, lm_chal_resp, nt_chal_resp);
178 }
179
180 /****************************************************************************
181 NT login.
182 ****************************************************************************/
183
184 BOOL cli_nt_login(struct cli_state *cli,
185                  DOM_CRED *clnt_cred, DOM_CRED *rtn_cred,
186                  NET_ID_INFO_CTR *ctr, NET_USER_INFO_3 *user_info3)
187 {
188   DOM_CRED sam_logon_rtn_cred;
189   DOM_CRED cred;
190   fstring dest_srv;
191   fstring my_host_name;
192
193   DEBUG(5,("do_nt_login: %d\n", __LINE__));
194
195   /*********************** SAM Logon **********************/
196
197   cli->clnt_cred->timestamp.time = time(NULL);
198
199   memcpy(&cred, cli->clnt_cred, sizeof(cred));
200
201   /* calculate sam logon credentials */
202   cred_create(sess_key, &(cli->clnt_cred->challenge),
203               cred.timestamp, &(cred.challenge));
204
205   strcpy(dest_srv, "\\\\");
206   strcat(dest_srv, dest_host);
207   strupper(dest_srv);
208
209   fstrcpy(my_host_name, myhostname);
210   strupper(my_host_name);
211
212   /* send client sam-logon challenge */
213   return cli_net_sam_logon(cli, dest_srv, my_host_name, 
214                           &cred, &sam_logon_rtn_cred,
215                           ctr->switch_value, ctr, 3, user_info3,
216                           rtn_cred);
217 }
218
219 /****************************************************************************
220 nt sam logoff
221 ****************************************************************************/
222
223 BOOL cli_nt_logoff(struct cli_state *cli, DOM_CRED *rtn_cred,
224                   NET_ID_INFO_CTR *ctr, char *dest_host, char *myhostname)
225 {
226   DOM_CRED sam_logoff_rtn_cred;
227   DOM_CRED cred;
228   fstring dest_srv;
229   fstring my_host_name;
230
231   DEBUG(5,("do_nt_logoff: %d\n", __LINE__));
232
233   /*********************** SAM Logoff *********************/
234
235   clnt_cred->timestamp.time = time(NULL);
236
237   memcpy(&cred, cli->clnt_cred, sizeof(cred));
238
239   /* calculate sam logoff credentials */
240   cred_create(sess_key, &(cli->clnt_cred->challenge),
241               cred.timestamp, &(cred.challenge));
242
243   strcpy(dest_srv, "\\\\");
244   strcat(dest_srv, dest_host);
245   strupper(dest_srv);
246
247   fstrcpy(my_host_name, myhostname);
248   strupper(my_host_name);
249
250   /* send client sam-logoff challenge; receive a sam-logoff challenge */
251   return cli_net_sam_logoff(cli, fnum, sess_key, clnt_cred,
252                            dest_srv, my_host_name, 
253                            &cred, &sam_logoff_rtn_cred,
254                            ctr->switch_value, ctr, 3,
255                            rtn_cred);
256 }