7ba6cfc96f0a86ec58fde26a2870e22290403644
[ira/wip.git] / source3 / libsmb / pwd_cache.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Password cacheing.  obfuscation is planned
4    Copyright (C) Luke Kenneth Casson Leighton 1996-1998
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 /****************************************************************************
24  Initialises a password structure.
25 ****************************************************************************/
26
27 static void pwd_init(struct pwd_info *pwd)
28 {
29         memset((char *)pwd->password  , '\0', sizeof(pwd->password  ));
30         memset((char *)pwd->smb_lm_pwd, '\0', sizeof(pwd->smb_lm_pwd));
31         memset((char *)pwd->smb_nt_pwd, '\0', sizeof(pwd->smb_nt_pwd));
32         memset((char *)pwd->smb_lm_owf, '\0', sizeof(pwd->smb_lm_owf));
33         memset((char *)pwd->smb_nt_owf, '\0', sizeof(pwd->smb_nt_owf));
34
35         pwd->null_pwd  = True; /* safest option... */
36         pwd->cleartext = False;
37         pwd->crypted   = False;
38 }
39
40 /****************************************************************************
41  Makes lm and nt hashed passwords.
42 ****************************************************************************/
43
44 static void pwd_make_lm_nt_16(struct pwd_info *pwd, const char *clr)
45 {
46         pwd_init(pwd);
47
48         if (!clr) {
49                 ZERO_STRUCT(pwd->smb_nt_pwd);
50                 ZERO_STRUCT(pwd->smb_lm_pwd);
51                 pwd->null_pwd  = True;
52         } else {
53                 nt_lm_owf_gen(clr, pwd->smb_nt_pwd, pwd->smb_lm_pwd);
54                 pwd->null_pwd  = False;
55         }
56         pwd->crypted = False;
57 }
58
59 /****************************************************************************
60  Stores a cleartext password.
61 ****************************************************************************/
62
63 void pwd_set_cleartext(struct pwd_info *pwd, const char *clr)
64 {
65         pwd_make_lm_nt_16(pwd, clr);
66         fstrcpy(pwd->password, clr);
67         pwd->cleartext = True;
68 }
69
70 /****************************************************************************
71  Gets a cleartext password.
72 ****************************************************************************/
73
74 void pwd_get_cleartext(struct pwd_info *pwd, fstring clr)
75 {
76         if (pwd->cleartext)
77                 fstrcpy(clr, pwd->password);
78         else
79                 clr[0] = 0;
80
81 }
82
83 /****************************************************************************
84  Gets lm and nt hashed passwords.
85 ****************************************************************************/
86
87 void pwd_get_lm_nt_16(struct pwd_info *pwd, uchar lm_pwd[16], uchar nt_pwd[16])
88 {
89         if (lm_pwd != NULL)
90                 memcpy(lm_pwd, pwd->smb_lm_pwd, 16);
91         if (nt_pwd != NULL)
92                 memcpy(nt_pwd, pwd->smb_nt_pwd, 16);
93 }
94
95 /****************************************************************************
96  Makes lm and nt OWF crypts.
97 ****************************************************************************/
98
99 void pwd_make_lm_nt_owf(struct pwd_info *pwd, uchar cryptkey[8])
100 {
101
102 #ifdef DEBUG_PASSWORD
103         DEBUG(100,("client cryptkey: "));
104         dump_data(100, (char *)cryptkey, 8);
105 #endif
106
107         SMBOWFencrypt(pwd->smb_nt_pwd, cryptkey, pwd->smb_nt_owf);
108
109 #ifdef DEBUG_PASSWORD
110         DEBUG(100,("nt_owf_passwd: "));
111         dump_data(100, (char *)pwd->smb_nt_owf, sizeof(pwd->smb_nt_owf));
112         DEBUG(100,("nt_sess_pwd: "));
113         dump_data(100, (char *)pwd->smb_nt_pwd, sizeof(pwd->smb_nt_pwd));
114 #endif
115
116         SMBOWFencrypt(pwd->smb_lm_pwd, cryptkey, pwd->smb_lm_owf);
117
118 #ifdef DEBUG_PASSWORD
119         DEBUG(100,("lm_owf_passwd: "));
120         dump_data(100, (char *)pwd->smb_lm_owf, sizeof(pwd->smb_lm_owf));
121         DEBUG(100,("lm_sess_pwd: "));
122         dump_data(100, (char *)pwd->smb_lm_pwd, sizeof(pwd->smb_lm_pwd));
123 #endif
124
125         pwd->crypted = True;
126 }
127
128 /****************************************************************************
129  Gets lm and nt crypts.
130 ****************************************************************************/
131
132 void pwd_get_lm_nt_owf(struct pwd_info *pwd, uchar lm_owf[24], uchar nt_owf[24])
133 {
134         if (lm_owf != NULL)
135                 memcpy(lm_owf, pwd->smb_lm_owf, 24);
136         if (nt_owf != NULL)
137                 memcpy(nt_owf, pwd->smb_nt_owf, 24);
138 }