set of changes in the beginning of bringing rpcclient changes
[Samba/gbeck.git] / source / libsmb / pwd_cache.c
blob420b49ed2e74f045b5cc21e14176d49cbe080da6
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 memset((char *)pwd->password , '\0', sizeof(pwd->password ));
33 memset((char *)pwd->smb_lm_pwd, '\0', sizeof(pwd->smb_lm_pwd));
34 memset((char *)pwd->smb_nt_pwd, '\0', sizeof(pwd->smb_nt_pwd));
35 memset((char *)pwd->smb_lm_owf, '\0', sizeof(pwd->smb_lm_owf));
36 memset((char *)pwd->smb_nt_owf, '\0', sizeof(pwd->smb_nt_owf));
38 pwd->null_pwd = True; /* safest option... */
39 pwd->cleartext = False;
40 pwd->crypted = False;
43 /****************************************************************************
44 returns NULL password flag
45 ****************************************************************************/
46 BOOL pwd_is_nullpwd(const struct pwd_info *pwd)
48 return pwd->null_pwd;
52 /****************************************************************************
53 compares two passwords. hmm, not as trivial as expected. hmm.
54 ****************************************************************************/
55 BOOL pwd_compare(struct pwd_info *pwd1, struct pwd_info *pwd2)
57 if (pwd1->cleartext && pwd2->cleartext)
59 if (strequal(pwd1->password, pwd2->password))
61 return True;
64 if (pwd1->null_pwd && pwd2->null_pwd)
66 return True;
69 if (!pwd1->null_pwd && !pwd2->null_pwd &&
70 !pwd1->cleartext && !pwd2->cleartext)
72 #ifdef DEBUG_PASSWORD
73 DEBUG(100,("pwd compare: nt#\n"));
74 dump_data(100, pwd1->smb_nt_pwd, 16);
75 dump_data(100, pwd2->smb_nt_pwd, 16);
76 #endif
77 if (memcmp(pwd1->smb_nt_pwd, pwd2->smb_nt_pwd, 16) == 0)
79 return True;
81 #ifdef DEBUG_PASSWORD
82 DEBUG(100,("pwd compare: lm#\n"));
83 dump_data(100, pwd1->smb_lm_pwd, 16);
84 dump_data(100, pwd2->smb_lm_pwd, 16);
85 #endif
86 if (memcmp(pwd1->smb_lm_pwd, pwd2->smb_lm_pwd, 16) == 0)
88 return True;
91 return False;
94 /****************************************************************************
95 reads a password
96 ****************************************************************************/
97 void pwd_read(struct pwd_info *pwd, char *passwd_report, BOOL do_encrypt)
99 /* grab a password */
100 char *user_pass;
102 pwd_init(pwd);
104 user_pass = (char*)getpass(passwd_report);
107 * Do not assume that an empty string is a NULL password.
108 * If you do this will break the session key generation for
109 * and account with an emtpy password. If you wish to use
110 * a NULL password, use the -N option to smbclient and rpcclient
111 * --jerry
113 #if 0
114 if (user_pass == NULL || user_pass[0] == 0)
116 pwd_set_nullpwd(pwd);
118 else if (do_encrypt)
119 #endif
120 if (do_encrypt)
122 pwd_make_lm_nt_16(pwd, user_pass);
124 else
126 pwd_set_cleartext(pwd, user_pass);
130 /****************************************************************************
131 stores a cleartext password
132 ****************************************************************************/
133 void pwd_set_nullpwd(struct pwd_info *pwd)
135 pwd_init(pwd);
137 pwd->cleartext = False;
138 pwd->null_pwd = True;
139 pwd->crypted = False;
142 /****************************************************************************
143 stores a cleartext password
144 ****************************************************************************/
145 void pwd_set_cleartext(struct pwd_info *pwd, char *clr)
147 pwd_init(pwd);
148 fstrcpy(pwd->password, clr);
149 unix_to_dos(pwd->password,True);
150 pwd->cleartext = True;
151 pwd->null_pwd = False;
152 pwd->crypted = False;
155 /****************************************************************************
156 gets a cleartext password
157 ****************************************************************************/
158 void pwd_get_cleartext(struct pwd_info *pwd, char *clr)
160 if (pwd->cleartext)
162 fstrcpy(clr, pwd->password);
163 dos_to_unix(clr, True);
165 else
167 clr[0] = 0;
171 /****************************************************************************
172 stores lm and nt hashed passwords
173 ****************************************************************************/
174 void pwd_set_lm_nt_16(struct pwd_info *pwd, uchar lm_pwd[16], uchar nt_pwd[16])
176 pwd_init(pwd);
178 if (lm_pwd)
180 memcpy(pwd->smb_lm_pwd, lm_pwd, 16);
182 else
184 memset((char *)pwd->smb_lm_pwd, '\0', 16);
187 if (nt_pwd)
189 memcpy(pwd->smb_nt_pwd, nt_pwd, 16);
191 else
193 memset((char *)pwd->smb_nt_pwd, '\0', 16);
196 pwd->null_pwd = False;
197 pwd->cleartext = False;
198 pwd->crypted = False;
201 /****************************************************************************
202 gets lm and nt hashed passwords
203 ****************************************************************************/
204 void pwd_get_lm_nt_16(struct pwd_info *pwd, uchar lm_pwd[16], uchar nt_pwd[16])
206 if (lm_pwd != NULL)
208 memcpy(lm_pwd, pwd->smb_lm_pwd, 16);
210 if (nt_pwd != NULL)
212 memcpy(nt_pwd, pwd->smb_nt_pwd, 16);
216 /****************************************************************************
217 makes lm and nt hashed passwords
218 ****************************************************************************/
219 void pwd_make_lm_nt_16(struct pwd_info *pwd, char *clr)
221 pstring dos_passwd;
223 pwd_init(pwd);
225 pstrcpy(dos_passwd, clr);
226 unix_to_dos(dos_passwd, True);
228 nt_lm_owf_gen(dos_passwd, pwd->smb_nt_pwd, pwd->smb_lm_pwd);
229 pwd->null_pwd = False;
230 pwd->cleartext = False;
231 pwd->crypted = False;
234 /****************************************************************************
235 makes lm and nt OWF crypts
236 ****************************************************************************/
237 void pwd_make_lm_nt_owf(struct pwd_info *pwd, uchar cryptkey[8])
240 #ifdef DEBUG_PASSWORD
241 DEBUG(100,("client cryptkey: "));
242 dump_data(100, (char *)cryptkey, 8);
243 #endif
245 SMBOWFencrypt(pwd->smb_nt_pwd, cryptkey, pwd->smb_nt_owf);
247 #ifdef DEBUG_PASSWORD
248 DEBUG(100,("nt_owf_passwd: "));
249 dump_data(100, (char *)pwd->smb_nt_owf, sizeof(pwd->smb_nt_owf));
250 DEBUG(100,("nt_sess_pwd: "));
251 dump_data(100, (char *)pwd->smb_nt_pwd, sizeof(pwd->smb_nt_pwd));
252 #endif
254 SMBOWFencrypt(pwd->smb_lm_pwd, cryptkey, pwd->smb_lm_owf);
256 #ifdef DEBUG_PASSWORD
257 DEBUG(100,("lm_owf_passwd: "));
258 dump_data(100, (char *)pwd->smb_lm_owf, sizeof(pwd->smb_lm_owf));
259 DEBUG(100,("lm_sess_pwd: "));
260 dump_data(100, (char *)pwd->smb_lm_pwd, sizeof(pwd->smb_lm_pwd));
261 #endif
263 pwd->crypted = True;
266 /****************************************************************************
267 gets lm and nt crypts
268 ****************************************************************************/
269 void pwd_get_lm_nt_owf(struct pwd_info *pwd, uchar lm_owf[24], uchar nt_owf[24])
271 if (lm_owf != NULL)
273 memcpy(lm_owf, pwd->smb_lm_owf, 24);
275 if (nt_owf != NULL)
277 memcpy(nt_owf, pwd->smb_nt_owf, 24);