new "domtrust" test command. r&d into inter-domain trust accounts.
[tprouty/samba.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, uint16 fnum,
33                                 const char* trust_acct,
34                                 unsigned char trust_pwd[16],
35                                 uint16 sec_chan)
36 {
37   DOM_CHAL clnt_chal;
38   DOM_CHAL srv_chal;
39
40   UTIME zerotime;
41
42   /******************* Request Challenge ********************/
43
44   generate_random_buffer( clnt_chal.data, 8, False);
45         
46   /* send a client challenge; receive a server challenge */
47   if (!cli_net_req_chal(cli, fnum, &clnt_chal, &srv_chal))
48   {
49     DEBUG(0,("cli_nt_setup_creds: request challenge failed\n"));
50     return False;
51   }
52
53   /**************** Long-term Session key **************/
54
55   /* calculate the session key */
56   cred_session_key(&clnt_chal, &srv_chal, (char *)trust_pwd, cli->sess_key);
57   bzero(cli->sess_key+8, 8);
58
59   /******************* Authenticate 2 ********************/
60
61   /* calculate auth-2 credentials */
62   zerotime.time = 0;
63   cred_create(cli->sess_key, &clnt_chal, zerotime, &(cli->clnt_cred.challenge));
64
65   /*  
66    * Send client auth-2 challenge.
67    * Receive an auth-2 challenge response and check it.
68    */
69
70   if (!cli_net_auth2(cli, fnum, trust_acct, sec_chan, 0x000001ff, &srv_chal))
71   {
72     DEBUG(0,("cli_nt_setup_creds: auth2 challenge failed\n"));
73     return False;
74   }
75
76   return True;
77 }
78
79 /****************************************************************************
80  Set machine password.
81  ****************************************************************************/
82
83 BOOL cli_nt_srv_pwset(struct cli_state *cli, uint16 fnum, unsigned char *new_hashof_trust_pwd)
84 {
85   unsigned char processed_new_pwd[16];
86
87   DEBUG(5,("cli_nt_srv_pwset: %d\n", __LINE__));
88
89 #ifdef DEBUG_PASSWORD
90   dump_data(6, new_hashof_trust_pwd, 16);
91 #endif
92
93   /* Process the new password. */
94   cred_hash3( processed_new_pwd, new_hashof_trust_pwd, cli->sess_key, 1);
95
96   /* send client srv_pwset challenge */
97   return cli_net_srv_pwset(cli, fnum, processed_new_pwd);
98 }
99
100 /****************************************************************************
101 NT login - interactive.
102 *NEVER* use this code. This method of doing a logon (sending the cleartext
103 password equivalents, protected by the session key) is inherently insecure
104 given the current design of the NT Domain system. JRA.
105  ****************************************************************************/
106 BOOL cli_nt_login_interactive(struct cli_state *cli, uint16 fnum, char *domain, char *username, 
107                               uint32 luid_low, char *password,
108                               NET_ID_INFO_CTR *ctr, NET_USER_INFO_3 *user_info3)
109 {
110   uchar lm_owf_user_pwd[16];
111   uchar nt_owf_user_pwd[16];
112   BOOL ret;
113
114   DEBUG(5,("cli_nt_login_interactive: %d\n", __LINE__));
115
116   nt_lm_owf_gen(password, nt_owf_user_pwd, lm_owf_user_pwd);
117
118 #ifdef DEBUG_PASSWORD
119
120   DEBUG(100,("nt owf of user password: "));
121   dump_data(100, lm_owf_user_pwd, 16);
122
123   DEBUG(100,("nt owf of user password: "));
124   dump_data(100, nt_owf_user_pwd, 16);
125
126 #endif
127
128   DEBUG(5,("cli_nt_login_interactive: %d\n", __LINE__));
129
130   /* indicate an "interactive" login */
131   ctr->switch_value = INTERACTIVE_LOGON_TYPE;
132
133   /* Create the structure needed for SAM logon. */
134   make_id_info1(&ctr->auth.id1, domain, 0, 
135                 luid_low, 0,
136                 username, cli->clnt_name_slash,
137                 (char *)cli->sess_key, lm_owf_user_pwd, nt_owf_user_pwd);
138
139   /* Ensure we overwrite all the plaintext password
140      equivalents. */
141   memset(lm_owf_user_pwd, '\0', sizeof(lm_owf_user_pwd));
142   memset(nt_owf_user_pwd, '\0', sizeof(nt_owf_user_pwd));
143
144   /* Send client sam-logon request - update credentials on success. */
145   ret = cli_net_sam_logon(cli, fnum, ctr, user_info3);
146
147   memset(ctr->auth.id1.lm_owf.data, '\0', sizeof(lm_owf_user_pwd));
148   memset(ctr->auth.id1.nt_owf.data, '\0', sizeof(nt_owf_user_pwd));
149
150   return ret;
151 }
152
153 /****************************************************************************
154 NT login - network.
155 *ALWAYS* use this call to validate a user as it does not expose plaintext
156 password equivalents over the network. JRA.
157 ****************************************************************************/
158
159 BOOL cli_nt_login_network(struct cli_state *cli, uint16 fnum, char *domain, char *username, 
160                           uint32 luid_low, char lm_chal[8], char lm_chal_resp[24],
161                           char nt_chal_resp[24],
162                           NET_ID_INFO_CTR *ctr, NET_USER_INFO_3 *user_info3)
163 {
164   DEBUG(5,("cli_nt_login_network: %d\n", __LINE__));
165
166   /* indicate a "network" login */
167   ctr->switch_value = NET_LOGON_TYPE;
168
169   /* Create the structure needed for SAM logon. */
170   make_id_info2(&ctr->auth.id2, domain, 0, 
171                 luid_low, 0,
172                 username, cli->clnt_name_slash,
173                 (uchar *)lm_chal, (uchar *)lm_chal_resp, (uchar *)nt_chal_resp);
174
175   /* Send client sam-logon request - update credentials on success. */
176   return cli_net_sam_logon(cli, fnum, ctr, user_info3);
177 }
178
179 /****************************************************************************
180 NT Logoff.
181 ****************************************************************************/
182 BOOL cli_nt_logoff(struct cli_state *cli, uint16 fnum, NET_ID_INFO_CTR *ctr)
183 {
184   DEBUG(5,("cli_nt_logoff: %d\n", __LINE__));
185
186   /* Send client sam-logoff request - update credentials on success. */
187   return cli_net_sam_logoff(cli, fnum, ctr);
188 }