2 Unix SMB/Netbios implementation.
4 Authenticate to a remote server
5 Copyright (C) Andrew Tridgell 1992-1998
6 Copyright (C) Andrew Bartlett 2001
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.
25 extern pstring global_myname;
26 extern userdom_struct current_user_info;
28 /****************************************************************************
29 Support for server level security.
30 ****************************************************************************/
32 static struct cli_state *server_cryptkey(void)
34 struct cli_state *cli = NULL;
36 struct in_addr dest_ip;
38 BOOL connected_ok = False;
40 if (!(cli = cli_initialise(cli)))
43 /* security = server just can't function with spnego */
44 cli->use_spnego = False;
46 pserver = strdup(lp_passwordserver());
49 while(next_token( &p, desthost, LIST_SEP, sizeof(desthost))) {
50 standard_sub_basic(current_user_info.smb_name, desthost);
53 if(!resolve_name( desthost, &dest_ip, 0x20)) {
54 DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
58 if (ismyip(dest_ip)) {
59 DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
63 if (cli_connect(cli, desthost, &dest_ip)) {
64 DEBUG(3,("connected to password server %s\n",desthost));
73 DEBUG(0,("password server not available\n"));
78 if (!attempt_netbios_session_request(cli, global_myname, desthost, &dest_ip))
81 if (strequal(desthost,myhostname())) {
82 exit_server("Password server loop!");
85 DEBUG(3,("got session\n"));
87 if (!cli_negprot(cli)) {
88 DEBUG(1,("%s rejected the negprot\n",desthost));
93 if (cli->protocol < PROTOCOL_LANMAN2 ||
94 !(cli->sec_mode & 1)) {
95 DEBUG(1,("%s isn't in user level security mode\n",desthost));
100 DEBUG(3,("password server OK\n"));
105 /****************************************************************************
106 Clean up our allocated cli.
107 ****************************************************************************/
109 static void free_server_private_data(void **private_data_pointer)
111 struct cli_state **cli = (struct cli_state **)private_data_pointer;
112 if (*cli && (*cli)->initialised) {
117 /****************************************************************************
118 Send a 'keepalive' packet down the cli pipe.
119 ****************************************************************************/
121 static void send_server_keepalive(void **private_data_pointer)
123 struct cli_state **cli = (struct cli_state **)private_data_pointer;
125 /* also send a keepalive to the password server if its still
127 if (cli && *cli && (*cli)->initialised) {
128 if (!send_keepalive((*cli)->fd)) {
129 DEBUG( 2, ( "password server keepalive failed.\n"));
135 /****************************************************************************
136 Get the challenge out of a password server.
137 ****************************************************************************/
139 static DATA_BLOB auth_get_challenge_server(void **my_private_data, const struct authsupplied_info *auth_info)
141 struct cli_state *cli = server_cryptkey();
144 DEBUG(3,("using password server validation\n"));
145 if ((cli->sec_mode & 2) == 0) {
146 /* We can't work with unencrypted password servers
147 unless 'encrypt passwords = no' */
148 DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
150 *my_private_data = (void *)cli;
151 return data_blob(NULL, 0);
153 } else if (cli->secblob.length < 8) {
154 /* We can't do much if we don't get a full challenge */
155 DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
157 return data_blob(NULL, 0);
160 *my_private_data = (void *)cli;
162 return data_blob(cli->secblob.data,8);
164 return data_blob(NULL, 0);
169 /****************************************************************************
170 Check for a valid username and password in security=server mode.
171 - Validate a password with the password server.
172 ****************************************************************************/
174 static NTSTATUS check_smbserver_security(void *my_private_data,
175 const auth_usersupplied_info *user_info,
176 const auth_authsupplied_info *auth_info,
177 auth_serversupplied_info **server_info)
179 struct cli_state *cli;
180 static unsigned char badpass[24];
181 static fstring baduser;
182 static BOOL tested_password_server = False;
183 static BOOL bad_password_server = False;
184 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
185 BOOL locally_made_cli = False;
188 * Check that the requested domain is not our own machine name.
189 * If it is, we should never check the PDC here, we use our own local
193 if(is_netbios_alias_or_name(user_info->domain.str)) {
194 DEBUG(3,("check_smbserver_security: Requested domain was for this machine.\n"));
195 return NT_STATUS_LOGON_FAILURE;
198 cli = my_private_data;
202 cli = server_cryptkey();
203 locally_made_cli = True;
206 if (!cli || !cli->initialised) {
207 DEBUG(1,("password server %s is not connected\n", cli->desthost));
208 return NT_STATUS_LOGON_FAILURE;
211 if ((cli->sec_mode & 2) == 0) {
212 if (user_info->encrypted) {
213 DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
214 return NT_STATUS_LOGON_FAILURE;
217 if (memcmp(cli->secblob.data, auth_info->challenge.data, 8) != 0) {
218 DEBUG(1,("the challenge that the password server (%s) supplied us is not the one we gave our client. This just can't work :-(\n", cli->desthost));
219 return NT_STATUS_LOGON_FAILURE;
224 memset(badpass, 0x1f, sizeof(badpass));
226 if((user_info->nt_resp.length == sizeof(badpass)) &&
227 !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
229 * Very unlikely, our random bad password is the same as the users
232 memset(badpass, badpass[0]+1, sizeof(badpass));
235 if(baduser[0] == 0) {
236 fstrcpy(baduser, INVALID_USER_PREFIX);
237 fstrcat(baduser, global_myname);
241 * Attempt a session setup with a totally incorrect password.
242 * If this succeeds with the guest bit *NOT* set then the password
243 * server is broken and is not correctly setting the guest bit. We
244 * need to detect this as some versions of NT4.x are broken. JRA.
247 /* I sure as hell hope that there arn't servers out there that take
248 * NTLMv2 and have this bug, as we don't test for that...
249 * - abartlet@samba.org
252 if ((!tested_password_server) && (lp_paranoid_server_security())) {
253 if (cli_session_setup(cli, baduser, (char *)badpass, sizeof(badpass),
254 (char *)badpass, sizeof(badpass), user_info->domain.str)) {
257 * We connected to the password server so we
258 * can say we've tested it.
260 tested_password_server = True;
262 if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
263 DEBUG(0,("server_validate: password server %s allows users as non-guest \
264 with a bad password.\n", cli->desthost));
265 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
266 use this machine as the password server.\n"));
270 * Password server has the bug.
272 bad_password_server = True;
273 return NT_STATUS_LOGON_FAILURE;
280 * We have already tested the password server.
281 * Fail immediately if it has the bug.
284 if(bad_password_server) {
285 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
286 with a bad password.\n", cli->desthost));
287 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
288 use this machine as the password server.\n"));
289 return NT_STATUS_LOGON_FAILURE;
294 * Now we know the password server will correctly set the guest bit, or is
295 * not guest enabled, we can try with the real password.
298 if (!user_info->encrypted) {
299 /* Plaintext available */
300 if (!cli_session_setup(cli, user_info->smb_name.str,
301 (char *)user_info->plaintext_password.data,
302 user_info->plaintext_password.length,
304 user_info->domain.str)) {
305 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
306 /* Make this cli_nt_error() when the conversion is in */
307 nt_status = cli_nt_error(cli);
309 nt_status = NT_STATUS_OK;
312 if (!cli_session_setup(cli, user_info->smb_name.str,
313 (char *)user_info->lm_resp.data,
314 user_info->lm_resp.length,
315 (char *)user_info->nt_resp.data,
316 user_info->nt_resp.length,
317 user_info->domain.str)) {
318 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
319 /* Make this cli_nt_error() when the conversion is in */
320 nt_status = cli_nt_error(cli);
322 nt_status = NT_STATUS_OK;
326 /* if logged in as guest then reject */
327 if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
328 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
329 nt_status = NT_STATUS_LOGON_FAILURE;
334 if NT_STATUS_IS_OK(nt_status) {
335 struct passwd *pass = Get_Pwnam(user_info->internal_username.str);
337 if (!make_server_info_pw(server_info, pass)) {
338 nt_status = NT_STATUS_NO_MEMORY;
341 nt_status = NT_STATUS_NO_SUCH_USER;
345 if (locally_made_cli) {
352 BOOL auth_init_smbserver(auth_methods **auth_method)
354 if (!make_auth_methods(auth_method)) {
357 (*auth_method)->auth = check_smbserver_security;
358 (*auth_method)->get_chal = auth_get_challenge_server;
359 (*auth_method)->send_keepalive = send_server_keepalive;
360 (*auth_method)->free_private_data = free_server_private_data;