compile with optimisation by default on all compilers
[Samba/gbeck.git] / source / libsmb / pwd_cache.c
blobbbd5f63d4b7dd6ac905133b09ce2ebbbcd90d96a
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
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.
22 #include "includes.h"
24 extern int DEBUGLEVEL;
27 /****************************************************************************
28 initialises a password structure
29 ****************************************************************************/
30 void pwd_init(struct pwd_info *pwd)
32 bzero(pwd->password , sizeof(pwd->password ));
33 bzero(pwd->smb_lm_pwd, sizeof(pwd->smb_lm_pwd));
34 bzero(pwd->smb_nt_pwd, sizeof(pwd->smb_nt_pwd));
35 bzero(pwd->smb_lm_owf, sizeof(pwd->smb_lm_owf));
36 bzero(pwd->smb_nt_owf, sizeof(pwd->smb_nt_owf));
38 pwd->null_pwd = True; /* safest option... */
39 pwd->cleartext = False;
40 pwd->crypted = False;
43 /****************************************************************************
44 de-obfuscates a password
45 ****************************************************************************/
46 static void pwd_deobfuscate(struct pwd_info *pwd)
50 /****************************************************************************
51 obfuscates a password
52 ****************************************************************************/
53 static void pwd_obfuscate(struct pwd_info *pwd)
57 /****************************************************************************
58 sets the obfuscation key info
59 ****************************************************************************/
60 void pwd_obfuscate_key(struct pwd_info *pwd, uint32 int_key, char *str_key)
64 /****************************************************************************
65 reads a password
66 ****************************************************************************/
67 void pwd_read(struct pwd_info *pwd, char *passwd_report, BOOL do_encrypt)
69 /* grab a password */
70 char *user_pass;
72 pwd_init(pwd);
74 user_pass = (char*)getpass(passwd_report);
76 if (user_pass == NULL || user_pass[0] == 0)
78 pwd_set_nullpwd(pwd);
80 else if (do_encrypt)
82 pwd_make_lm_nt_16(pwd, user_pass);
84 else
86 pwd_set_cleartext(pwd, user_pass);
90 /****************************************************************************
91 stores a cleartext password
92 ****************************************************************************/
93 void pwd_set_nullpwd(struct pwd_info *pwd)
95 pwd_init(pwd);
97 pwd->cleartext = False;
98 pwd->null_pwd = True;
99 pwd->crypted = False;
102 /****************************************************************************
103 stores a cleartext password
104 ****************************************************************************/
105 void pwd_set_cleartext(struct pwd_info *pwd, char *clr)
107 pwd_init(pwd);
108 fstrcpy(pwd->password, clr);
109 pwd->cleartext = True;
110 pwd->null_pwd = False;
111 pwd->crypted = False;
113 pwd_obfuscate(pwd);
116 /****************************************************************************
117 gets a cleartext password
118 ****************************************************************************/
119 void pwd_get_cleartext(struct pwd_info *pwd, char *clr)
121 pwd_deobfuscate(pwd);
122 if (pwd->cleartext)
124 fstrcpy(clr, pwd->password);
126 else
128 clr[0] = 0;
130 pwd_obfuscate(pwd);
133 /****************************************************************************
134 stores lm and nt hashed passwords
135 ****************************************************************************/
136 void pwd_set_lm_nt_16(struct pwd_info *pwd, uchar lm_pwd[16], uchar nt_pwd[16])
138 pwd_init(pwd);
140 if (lm_pwd)
142 memcpy(pwd->smb_lm_pwd, lm_pwd, 16);
144 else
146 bzero(pwd->smb_lm_pwd, 16);
149 if (nt_pwd)
151 memcpy(pwd->smb_nt_pwd, nt_pwd, 16);
153 else
155 bzero(pwd->smb_nt_pwd, 16);
158 pwd->null_pwd = False;
159 pwd->cleartext = False;
160 pwd->crypted = False;
162 pwd_obfuscate(pwd);
165 /****************************************************************************
166 gets lm and nt hashed passwords
167 ****************************************************************************/
168 void pwd_get_lm_nt_16(struct pwd_info *pwd, uchar lm_pwd[16], uchar nt_pwd[16])
170 pwd_deobfuscate(pwd);
171 if (lm_pwd != NULL)
173 memcpy(lm_pwd, pwd->smb_lm_pwd, 16);
175 if (nt_pwd != NULL)
177 memcpy(nt_pwd, pwd->smb_nt_pwd, 16);
179 pwd_obfuscate(pwd);
182 /****************************************************************************
183 makes lm and nt hashed passwords
184 ****************************************************************************/
185 void pwd_make_lm_nt_16(struct pwd_info *pwd, char *clr)
187 pwd_init(pwd);
189 nt_lm_owf_gen(clr, pwd->smb_nt_pwd, pwd->smb_lm_pwd);
190 pwd->null_pwd = False;
191 pwd->cleartext = False;
192 pwd->crypted = False;
194 pwd_obfuscate(pwd);
197 /****************************************************************************
198 makes lm and nt OWF crypts
199 ****************************************************************************/
200 void pwd_make_lm_nt_owf(struct pwd_info *pwd, uchar cryptkey[8])
202 pwd_deobfuscate(pwd);
204 #ifdef DEBUG_PASSWORD
205 DEBUG(100,("client cryptkey: "));
206 dump_data(100, cryptkey, 8);
207 #endif
209 SMBOWFencrypt(pwd->smb_nt_pwd, cryptkey, pwd->smb_nt_owf);
211 #ifdef DEBUG_PASSWORD
212 DEBUG(100,("nt_owf_passwd: "));
213 dump_data(100, pwd->smb_nt_owf, sizeof(pwd->smb_nt_owf));
214 DEBUG(100,("nt_sess_pwd: "));
215 dump_data(100, pwd->smb_nt_pwd, sizeof(pwd->smb_nt_pwd));
216 #endif
218 SMBOWFencrypt(pwd->smb_lm_pwd, cryptkey, pwd->smb_lm_owf);
220 #ifdef DEBUG_PASSWORD
221 DEBUG(100,("lm_owf_passwd: "));
222 dump_data(100, pwd->smb_lm_owf, sizeof(pwd->smb_lm_owf));
223 DEBUG(100,("lm_sess_pwd: "));
224 dump_data(100, pwd->smb_lm_pwd, sizeof(pwd->smb_lm_pwd));
225 #endif
227 pwd->crypted = True;
229 pwd_obfuscate(pwd);
232 /****************************************************************************
233 gets lm and nt crypts
234 ****************************************************************************/
235 void pwd_get_lm_nt_owf(struct pwd_info *pwd, uchar lm_owf[24], uchar nt_owf[24])
237 pwd_deobfuscate(pwd);
238 if (lm_owf != NULL)
240 memcpy(lm_owf, pwd->smb_lm_owf, 24);
242 if (nt_owf != NULL)
244 memcpy(nt_owf, pwd->smb_nt_owf, 24);
246 pwd_obfuscate(pwd);