2 Unix SMB/Netbios implementation.
4 Password cacheing. obfuscation is planned
5 Copyright (C) Luke Kenneth Casson Leighton 1996-1998
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.
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.
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.
24 extern int DEBUGLEVEL;
27 /****************************************************************************
28 initialises a password structure
29 ****************************************************************************/
30 void pwd_init(struct pwd_info *pwd)
32 ZERO_STRUCT(pwd->password );
33 ZERO_STRUCT(pwd->smb_lm_pwd);
34 ZERO_STRUCT(pwd->smb_nt_pwd);
35 ZERO_STRUCT(pwd->smb_lm_owf);
36 ZERO_STRUCT(pwd->smb_nt_owf);
37 ZERO_STRUCT(pwd->sess_key );
40 pwd->null_pwd = True; /* safest option... */
41 pwd->cleartext = False;
45 /****************************************************************************
46 de-obfuscates a password
47 ****************************************************************************/
48 static void pwd_deobfuscate(const struct pwd_info *pwd)
52 /****************************************************************************
54 ****************************************************************************/
55 static void pwd_obfuscate(const struct pwd_info *pwd)
59 /****************************************************************************
60 sets the obfuscation key info
61 ****************************************************************************/
62 void pwd_obfuscate_key(struct pwd_info *pwd, uint32 int_key, char *str_key)
66 /****************************************************************************
67 compares two passwords. hmm, not as trivial as expected. hmm.
68 ****************************************************************************/
69 BOOL pwd_compare(struct pwd_info *pwd1, struct pwd_info *pwd2)
71 pwd_deobfuscate(pwd1);
72 pwd_deobfuscate(pwd2);
73 if (pwd1->cleartext && pwd2->cleartext)
75 if (strequal(pwd1->password, pwd2->password))
82 if (pwd1->null_pwd && pwd2->null_pwd)
88 if (pwd1->crypted || pwd2->crypted)
90 DEBUG(5,("pwd_compare: cannot compare crypted passwords\n"));
96 if (!pwd1->crypted && !pwd2->crypted &&
97 !pwd1->null_pwd && !pwd2->null_pwd &&
98 !pwd1->cleartext && !pwd2->cleartext)
100 if (memcmp(pwd1->smb_nt_pwd, pwd2->smb_nt_pwd, 16) == 0)
106 if (memcmp(pwd1->smb_lm_pwd, pwd2->smb_lm_pwd, 16) == 0)
117 /****************************************************************************
119 ****************************************************************************/
120 void pwd_read(struct pwd_info *pwd, char *passwd_report, BOOL do_encrypt)
122 /* grab a password */
127 user_pass = (char*)getpass(passwd_report);
129 if (user_pass == NULL || user_pass[0] == 0)
131 pwd_set_nullpwd(pwd);
135 pwd_make_lm_nt_16(pwd, user_pass);
139 pwd_set_cleartext(pwd, user_pass);
143 /****************************************************************************
144 stores a cleartext password
145 ****************************************************************************/
146 void pwd_set_nullpwd(struct pwd_info *pwd)
150 pwd->cleartext = False;
151 pwd->null_pwd = True;
152 pwd->crypted = False;
155 /****************************************************************************
156 stores a cleartext password
157 ****************************************************************************/
158 void pwd_set_cleartext(struct pwd_info *pwd, char *clr)
161 fstrcpy(pwd->password, clr);
162 pwd->cleartext = True;
163 pwd->null_pwd = False;
164 pwd->crypted = False;
169 /****************************************************************************
170 gets a cleartext password
171 ****************************************************************************/
172 void pwd_get_cleartext(struct pwd_info *pwd, char *clr)
174 pwd_deobfuscate(pwd);
177 fstrcpy(clr, pwd->password);
186 /****************************************************************************
187 stores lm and nt hashed passwords
188 ****************************************************************************/
189 void pwd_set_lm_nt_16(struct pwd_info *pwd, uchar lm_pwd[16], uchar nt_pwd[16])
195 memcpy(pwd->smb_lm_pwd, lm_pwd, 16);
199 bzero(pwd->smb_lm_pwd, 16);
204 memcpy(pwd->smb_nt_pwd, nt_pwd, 16);
208 bzero(pwd->smb_nt_pwd, 16);
211 pwd->null_pwd = False;
212 pwd->cleartext = False;
213 pwd->crypted = False;
218 /****************************************************************************
219 gets lm and nt hashed passwords
220 ****************************************************************************/
221 void pwd_get_lm_nt_16(const struct pwd_info *pwd, uchar lm_pwd[16], uchar nt_pwd[16])
223 pwd_deobfuscate(pwd);
226 memcpy(lm_pwd, pwd->smb_lm_pwd, 16);
230 memcpy(nt_pwd, pwd->smb_nt_pwd, 16);
235 /****************************************************************************
236 makes lm and nt hashed passwords
237 ****************************************************************************/
238 void pwd_make_lm_nt_16(struct pwd_info *pwd, char *clr)
242 nt_lm_owf_gen(clr, pwd->smb_nt_pwd, pwd->smb_lm_pwd);
243 pwd->null_pwd = False;
244 pwd->cleartext = False;
245 pwd->crypted = False;
250 /****************************************************************************
251 makes lm and nt OWF crypts
252 ****************************************************************************/
253 void pwd_make_lm_nt_owf2(struct pwd_info *pwd, const uchar srv_key[8],
254 const char *user, const char *server, const char *domain)
258 DEBUG(10,("pwd_make_lm_nt_owf2: user %s, srv %s, dom %s\n",
259 user, server, domain));
261 pwd_deobfuscate(pwd);
263 SMBgenclientchals(pwd->lm_cli_chal,
265 &pwd->nt_cli_chal_len,
268 ntv2_owf_gen(pwd->smb_nt_pwd, user, domain, kr);
271 SMBOWFencrypt_ntv2(kr,
275 memcpy(&pwd->smb_lm_owf[16], pwd->lm_cli_chal, 8);
278 SMBOWFencrypt_ntv2(kr,
280 pwd->nt_cli_chal, pwd->nt_cli_chal_len,
282 memcpy(&pwd->smb_nt_owf[16], pwd->nt_cli_chal, pwd->nt_cli_chal_len);
283 pwd->nt_owf_len = pwd->nt_cli_chal_len + 16;
285 SMBsesskeygen_ntv2(kr, pwd->smb_nt_owf, pwd->sess_key);
290 #ifdef DEBUG_PASSWORD
291 DEBUG(100,("server cryptkey: "));
292 dump_data(100, srv_key, 8);
294 DEBUG(100,("client lmv2 cryptkey: "));
295 dump_data(100, pwd->lm_cli_chal, 8);
297 DEBUG(100,("client ntv2 cryptkey: "));
298 dump_data(100, pwd->nt_cli_chal, pwd->nt_cli_chal_len);
300 DEBUG(100,("ntv2_owf_passwd: "));
301 dump_data(100, pwd->smb_nt_owf, pwd->nt_owf_len);
302 DEBUG(100,("nt_sess_pwd: "));
303 dump_data(100, pwd->smb_nt_pwd, sizeof(pwd->smb_nt_pwd));
305 DEBUG(100,("lmv2_owf_passwd: "));
306 dump_data(100, pwd->smb_lm_owf, sizeof(pwd->smb_lm_owf));
307 DEBUG(100,("lm_sess_pwd: "));
308 dump_data(100, pwd->smb_lm_pwd, sizeof(pwd->smb_lm_pwd));
310 DEBUG(100,("session key:\n"));
311 dump_data(100, pwd->sess_key, sizeof(pwd->sess_key));
318 /****************************************************************************
319 makes lm and nt OWF crypts
320 ****************************************************************************/
321 void pwd_make_lm_nt_owf(struct pwd_info *pwd, uchar cryptkey[8])
325 #ifdef DEBUG_PASSWORD
326 DEBUG(100,("pwd_make_lm_nt_owf: NULL password\n"));
331 pwd_deobfuscate(pwd);
333 /* generate 24-byte hashes */
334 SMBOWFencrypt(pwd->smb_lm_pwd, cryptkey, pwd->smb_lm_owf);
335 SMBOWFencrypt(pwd->smb_nt_pwd, cryptkey, pwd->smb_nt_owf);
336 pwd->nt_owf_len = 24;
338 SMBsesskeygen_ntv1(pwd->smb_nt_pwd, pwd->smb_nt_owf, pwd->sess_key);
340 #ifdef DEBUG_PASSWORD
341 DEBUG(100,("client cryptkey: "));
342 dump_data(100, cryptkey, 8);
344 DEBUG(100,("nt_owf_passwd: "));
345 dump_data(100, pwd->smb_nt_owf, pwd->nt_owf_len);
346 DEBUG(100,("nt_sess_pwd: "));
347 dump_data(100, pwd->smb_nt_pwd, sizeof(pwd->smb_nt_pwd));
349 DEBUG(100,("lm_owf_passwd: "));
350 dump_data(100, pwd->smb_lm_owf, sizeof(pwd->smb_lm_owf));
351 DEBUG(100,("lm_sess_pwd: "));
352 dump_data(100, pwd->smb_lm_pwd, sizeof(pwd->smb_lm_pwd));
354 DEBUG(100,("session key:\n"));
355 dump_data(100, pwd->sess_key, sizeof(pwd->sess_key));
363 /****************************************************************************
364 gets lm and nt crypts
365 ****************************************************************************/
366 void pwd_get_lm_nt_owf(struct pwd_info *pwd, uchar lm_owf[24],
367 uchar *nt_owf, size_t *nt_owf_len,
372 #ifdef DEBUG_PASSWORD
373 DEBUG(100,("pwd_get_lm_nt_owf: NULL password\n"));
375 if (nt_owf_len != NULL)
382 pwd_deobfuscate(pwd);
385 memcpy(lm_owf, pwd->smb_lm_owf, 24);
389 memcpy(nt_owf, pwd->smb_nt_owf, pwd->nt_owf_len);
391 if (sess_key != NULL)
393 memcpy(sess_key, pwd->sess_key, 16);
395 if (nt_owf_len != NULL)
397 *nt_owf_len = pwd->nt_owf_len;