preparing for release of alpha.2.5
[Samba.git] / source / lib / passcheck.c
blobee9e578f14ab63e3ac66be75d00a373e5c217c86
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Password and authentication handling
5 Copyright (C) Andrew Tridgell 1992-2000
6 Copyright (C) Luke Kenneth Casson Leighton 1996-2000
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
25 extern int DEBUGLEVEL;
28 /****************************************************************************
29 core of smb password checking routine.
30 ****************************************************************************/
31 static BOOL smb_pwd_check_ntlmv1(const char *password,
32 const uchar *part_passwd,
33 const uchar *c8,
34 uchar user_sess_key[16])
36 /* Finish the encryption of part_passwd. */
37 uchar p24[24];
39 if (part_passwd == NULL)
40 DEBUG(10,("No password set - allowing access\n"));
41 /* No password set - always true ! */
42 if (part_passwd == NULL)
43 return True;
45 SMBOWFencrypt(part_passwd, c8, p24);
46 if (user_sess_key != NULL)
48 SMBsesskeygen_ntv1(part_passwd, NULL, user_sess_key);
51 #if DEBUG_PASSWORD
52 DEBUG(100,("Part password (P16) was |"));
53 dump_data(100, part_passwd, 16);
54 DEBUG(100,("Password from client was |"));
55 dump_data(100, password, 24);
56 DEBUG(100,("Given challenge was |"));
57 dump_data(100, c8, 8);
58 DEBUG(100,("Value from encryption was |"));
59 dump_data(100, p24, 24);
60 #endif
61 return (memcmp(p24, password, 24) == 0);
64 /****************************************************************************
65 core of smb password checking routine.
66 ****************************************************************************/
67 static BOOL smb_pwd_check_ntlmv2(const char *password, size_t pwd_len,
68 uchar *part_passwd,
69 uchar const *c8,
70 const char *user, const char *domain,
71 char *user_sess_key)
73 /* Finish the encryption of part_passwd. */
74 uchar kr[16];
75 uchar resp[16];
77 if (part_passwd == NULL)
79 DEBUG(10,("No password set - allowing access\n"));
81 /* No password set - always true ! */
82 if (part_passwd == NULL)
84 return True;
87 ntv2_owf_gen(part_passwd, user, domain, kr);
88 SMBOWFencrypt_ntv2(kr, c8, 8, password+16, pwd_len-16, resp);
89 if (user_sess_key != NULL)
91 SMBsesskeygen_ntv2(kr, resp, user_sess_key);
94 #if DEBUG_PASSWORD
95 DEBUG(100,("Part password (P16) was |"));
96 dump_data(100, part_passwd, 16);
97 DEBUG(100,("Password from client was |"));
98 dump_data(100, password, pwd_len);
99 DEBUG(100,("Given challenge was |"));
100 dump_data(100, c8, 8);
101 DEBUG(100,("Value from encryption was |"));
102 dump_data(100, resp, 16);
103 #endif
105 return (memcmp(resp, password, 16) == 0);
108 /****************************************************************************
109 Do a specific test for an smb password being correct, given a smb_password and
110 the lanman and NT responses.
111 ****************************************************************************/
112 BOOL smb_password_ok(uint16 acct_ctrl,
113 uchar smb_passwd[16],
114 uchar smb_nt_passwd[16],
115 const uchar challenge[8],
116 const char *user, const char *domain,
117 const uchar *lm_pass, size_t lm_pwd_len,
118 const uchar *nt_pass, size_t nt_pwd_len,
119 uchar user_sess_key[16])
121 DEBUG(4,("Checking SMB password for user %s\n", user));
123 dump_data_pw("lm password:\n", lm_pass, lm_pwd_len);
124 dump_data_pw("nt password:\n", nt_pass, nt_pwd_len);
126 if (acct_ctrl & ACB_DISABLED)
128 DEBUG(3,("account for user %s was disabled.\n", user));
129 return False;
132 if (challenge == NULL)
134 DEBUG(1,("no challenge available - password failed\n"));
135 return False;
138 if (smb_nt_passwd != NULL)
140 /* We have the NT MD4 hash challenge available - see if we can
141 use it (ie. does it exist in the smbpasswd file).
143 if (lp_server_ntlmv2() != False && nt_pwd_len > 24)
145 DEBUG(4,("smb_password_ok: Check NTLMv2 password\n"));
146 if (smb_pwd_check_ntlmv2(nt_pass, nt_pwd_len,
147 (uchar *)smb_nt_passwd,
148 challenge, user, domain,
149 user_sess_key))
151 return True;
154 if (lp_server_ntlmv2() != True && nt_pwd_len == 24)
156 DEBUG(4,("smb_password_ok: Check NT MD4 password\n"));
157 if (smb_pwd_check_ntlmv1((const char *)nt_pass,
158 (const uchar *)smb_nt_passwd,
159 challenge,
160 user_sess_key))
162 DEBUG(4,("NT MD4 password check succeeded\n"));
163 return True;
166 DEBUG(4,("NT MD4 password check failed\n"));
169 if (lp_server_ntlmv2() == True)
171 DEBUG(4,("Not checking LM MD4 password\n"));
172 return False;
175 /* Try against the lanman password. smb_passwd == NULL means
176 no password, allow access. */
178 DEBUG(4,("Checking LM MD4 password\n"));
180 if ((smb_passwd == NULL) &&
181 (acct_ctrl & ACB_PWNOTREQ))
183 DEBUG(4,("no password required for user %s\n", user));
184 return True;
187 if ((smb_passwd != NULL) &&
188 smb_pwd_check_ntlmv1((const char *)lm_pass,
189 (const uchar *)smb_passwd,
190 challenge, user_sess_key))
192 DEBUG(4,("LM MD4 password check succeeded\n"));
193 return(True);
196 DEBUG(4,("LM MD4 password check failed\n"));
198 return False;