1 /*
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Password cacheing.  obfuscation is planned
5 
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1998
7 
8    Copyright (C) 2011-2021
9    Free Software Foundation, Inc.
10 
11    This file is part of the Midnight Commander.
12 
13    The Midnight Commander is free software: you can redistribute it
14    and/or modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation, either version 3 of the License,
16    or (at your option) any later version.
17 
18    The Midnight Commander is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21    GNU General Public License for more details.
22 
23    You should have received a copy of the GNU General Public License
24    along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 #include "includes.h"
28 
29 extern int DEBUGLEVEL;
30 
31 
32 /****************************************************************************
33 initialises a password structure
34 ****************************************************************************/
35 void
pwd_init(struct pwd_info * pwd)36 pwd_init (struct pwd_info *pwd)
37 {
38     memset ((char *) pwd->password, '\0', sizeof (pwd->password));
39     memset ((char *) pwd->smb_lm_pwd, '\0', sizeof (pwd->smb_lm_pwd));
40     memset ((char *) pwd->smb_nt_pwd, '\0', sizeof (pwd->smb_nt_pwd));
41     memset ((char *) pwd->smb_lm_owf, '\0', sizeof (pwd->smb_lm_owf));
42     memset ((char *) pwd->smb_nt_owf, '\0', sizeof (pwd->smb_nt_owf));
43 
44     pwd->null_pwd = True;       /* safest option... */
45     pwd->cleartext = False;
46     pwd->crypted = False;
47 }
48 
49 /****************************************************************************
50 de-obfuscates a password
51 ****************************************************************************/
52 static void
pwd_deobfuscate(struct pwd_info * pwd)53 pwd_deobfuscate (struct pwd_info *pwd)
54 {
55     (void) pwd;
56 }
57 
58 /****************************************************************************
59 obfuscates a password
60 ****************************************************************************/
61 static void
pwd_obfuscate(struct pwd_info * pwd)62 pwd_obfuscate (struct pwd_info *pwd)
63 {
64     (void) pwd;
65 }
66 
67 /****************************************************************************
68 sets the obfuscation key info
69 ****************************************************************************/
70 void
pwd_obfuscate_key(struct pwd_info * pwd,uint32 int_key,char * str_key)71 pwd_obfuscate_key (struct pwd_info *pwd, uint32 int_key, char *str_key)
72 {
73     (void) pwd;
74     (void) int_key;
75     (void) str_key;
76 }
77 
78 #if 0
79 /****************************************************************************
80 reads a password
81 ****************************************************************************/
82 void
83 pwd_read (struct pwd_info *pwd, char *passwd_report, BOOL do_encrypt)
84 {
85     /* grab a password */
86     char *user_pass;
87 
88     pwd_init (pwd);
89 
90     user_pass = (char *) getpass (passwd_report);
91 
92     if (user_pass == NULL || user_pass[0] == 0)
93     {
94         pwd_set_nullpwd (pwd);
95     }
96     else if (do_encrypt)
97     {
98         pwd_make_lm_nt_16 (pwd, user_pass);
99     }
100     else
101     {
102         pwd_set_cleartext (pwd, user_pass);
103     }
104 }
105 
106 
107 /****************************************************************************
108  stores a cleartext password
109  ****************************************************************************/
110 void
111 pwd_set_nullpwd (struct pwd_info *pwd)
112 {
113     pwd_init (pwd);
114 
115     pwd->cleartext = False;
116     pwd->null_pwd = True;
117     pwd->crypted = False;
118 }
119 #endif /* 0 */
120 
121 /****************************************************************************
122  stores a cleartext password
123  ****************************************************************************/
124 void
pwd_set_cleartext(struct pwd_info * pwd,char * clr)125 pwd_set_cleartext (struct pwd_info *pwd, char *clr)
126 {
127     pwd_init (pwd);
128     fstrcpy (pwd->password, clr);
129     pwd->cleartext = True;
130     pwd->null_pwd = False;
131     pwd->crypted = False;
132 
133     pwd_obfuscate (pwd);
134 }
135 
136 /****************************************************************************
137  gets a cleartext password
138  ****************************************************************************/
139 void
pwd_get_cleartext(struct pwd_info * pwd,char * clr)140 pwd_get_cleartext (struct pwd_info *pwd, char *clr)
141 {
142     pwd_deobfuscate (pwd);
143     if (pwd->cleartext)
144     {
145         fstrcpy (clr, pwd->password);
146     }
147     else
148     {
149         clr[0] = 0;
150     }
151     pwd_obfuscate (pwd);
152 }
153 
154 /****************************************************************************
155  stores lm and nt hashed passwords
156  ****************************************************************************/
157 void
pwd_set_lm_nt_16(struct pwd_info * pwd,uchar lm_pwd[16],uchar nt_pwd[16])158 pwd_set_lm_nt_16 (struct pwd_info *pwd, uchar lm_pwd[16], uchar nt_pwd[16])
159 {
160     pwd_init (pwd);
161 
162     if (lm_pwd)
163     {
164         memcpy (pwd->smb_lm_pwd, lm_pwd, 16);
165     }
166     else
167     {
168         memset ((char *) pwd->smb_lm_pwd, '\0', 16);
169     }
170 
171     if (nt_pwd)
172     {
173         memcpy (pwd->smb_nt_pwd, nt_pwd, 16);
174     }
175     else
176     {
177         memset ((char *) pwd->smb_nt_pwd, '\0', 16);
178     }
179 
180     pwd->null_pwd = False;
181     pwd->cleartext = False;
182     pwd->crypted = False;
183 
184     pwd_obfuscate (pwd);
185 }
186 
187 /****************************************************************************
188  gets lm and nt hashed passwords
189  ****************************************************************************/
190 void
pwd_get_lm_nt_16(struct pwd_info * pwd,uchar lm_pwd[16],uchar nt_pwd[16])191 pwd_get_lm_nt_16 (struct pwd_info *pwd, uchar lm_pwd[16], uchar nt_pwd[16])
192 {
193     pwd_deobfuscate (pwd);
194     if (lm_pwd != NULL)
195     {
196         memcpy (lm_pwd, pwd->smb_lm_pwd, 16);
197     }
198     if (nt_pwd != NULL)
199     {
200         memcpy (nt_pwd, pwd->smb_nt_pwd, 16);
201     }
202     pwd_obfuscate (pwd);
203 }
204 
205 /****************************************************************************
206  makes lm and nt hashed passwords
207  ****************************************************************************/
208 void
pwd_make_lm_nt_16(struct pwd_info * pwd,char * clr)209 pwd_make_lm_nt_16 (struct pwd_info *pwd, char *clr)
210 {
211     pwd_init (pwd);
212 
213     nt_lm_owf_gen (clr, pwd->smb_nt_pwd, pwd->smb_lm_pwd);
214     pwd->null_pwd = False;
215     pwd->cleartext = False;
216     pwd->crypted = False;
217 
218     pwd_obfuscate (pwd);
219 }
220 
221 /****************************************************************************
222  makes lm and nt OWF crypts
223  ****************************************************************************/
224 void
pwd_make_lm_nt_owf(struct pwd_info * pwd,uchar cryptkey[8])225 pwd_make_lm_nt_owf (struct pwd_info *pwd, uchar cryptkey[8])
226 {
227     pwd_deobfuscate (pwd);
228 
229 #ifdef DEBUG_PASSWORD
230     DEBUG (100, ("client cryptkey: "));
231     dump_data (100, (char *) cryptkey, 8);
232 #endif
233 
234     SMBOWFencrypt (pwd->smb_nt_pwd, cryptkey, pwd->smb_nt_owf);
235 
236 #ifdef DEBUG_PASSWORD
237     DEBUG (100, ("nt_owf_passwd: "));
238     dump_data (100, (char *) pwd->smb_nt_owf, sizeof (pwd->smb_nt_owf));
239     DEBUG (100, ("nt_sess_pwd: "));
240     dump_data (100, (char *) pwd->smb_nt_pwd, sizeof (pwd->smb_nt_pwd));
241 #endif
242 
243     SMBOWFencrypt (pwd->smb_lm_pwd, cryptkey, pwd->smb_lm_owf);
244 
245 #ifdef DEBUG_PASSWORD
246     DEBUG (100, ("lm_owf_passwd: "));
247     dump_data (100, (char *) pwd->smb_lm_owf, sizeof (pwd->smb_lm_owf));
248     DEBUG (100, ("lm_sess_pwd: "));
249     dump_data (100, (char *) pwd->smb_lm_pwd, sizeof (pwd->smb_lm_pwd));
250 #endif
251 
252     pwd->crypted = True;
253 
254     pwd_obfuscate (pwd);
255 }
256 
257 /****************************************************************************
258  gets lm and nt crypts
259  ****************************************************************************/
260 void
pwd_get_lm_nt_owf(struct pwd_info * pwd,uchar lm_owf[24],uchar nt_owf[24])261 pwd_get_lm_nt_owf (struct pwd_info *pwd, uchar lm_owf[24], uchar nt_owf[24])
262 {
263     pwd_deobfuscate (pwd);
264     if (lm_owf != NULL)
265     {
266         memcpy (lm_owf, pwd->smb_lm_owf, 24);
267     }
268     if (nt_owf != NULL)
269     {
270         memcpy (nt_owf, pwd->smb_nt_owf, 24);
271     }
272     pwd_obfuscate (pwd);
273 }
274