2 Unix SMB/CIFS implementation.
3 Authenticate to a remote server
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Andrew Bartlett 2001
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 3 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, see <http://www.gnu.org/licenses/>.
24 #define DBGC_CLASS DBGC_AUTH
26 extern userdom_struct current_user_info;
28 /****************************************************************************
29 Support for server level security.
30 ****************************************************************************/
32 static struct cli_state *server_cryptkey(TALLOC_CTX *mem_ctx)
34 struct cli_state *cli = NULL;
35 char *desthost = NULL;
36 struct sockaddr_storage dest_ss;
39 bool connected_ok = False;
41 if (!(cli = cli_initialise()))
44 /* security = server just can't function with spnego */
45 cli->use_spnego = False;
47 pserver = talloc_strdup(mem_ctx, lp_passwordserver());
50 while(next_token_talloc(mem_ctx, &p, &desthost, LIST_SEP)) {
53 desthost = talloc_sub_basic(mem_ctx,
54 current_user_info.smb_name,
55 current_user_info.domain,
62 if(!resolve_name( desthost, &dest_ss, 0x20)) {
63 DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
67 if (ismyaddr(&dest_ss)) {
68 DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
72 /* we use a mutex to prevent two connections at once - when a
73 Win2k PDC get two connections where one hasn't completed a
74 session setup yet it will send a TCP reset to the first
75 connection (tridge) */
77 if (!grab_server_mutex(desthost)) {
81 status = cli_connect(cli, desthost, &dest_ss);
82 if (NT_STATUS_IS_OK(status)) {
83 DEBUG(3,("connected to password server %s\n",desthost));
87 DEBUG(10,("server_cryptkey: failed to connect to server %s. Error %s\n",
88 desthost, nt_errstr(status) ));
89 release_server_mutex();
93 DEBUG(0,("password server not available\n"));
98 if (!attempt_netbios_session_request(&cli, global_myname(),
99 desthost, &dest_ss)) {
100 release_server_mutex();
101 DEBUG(1,("password server fails session request\n"));
106 if (strequal(desthost,myhostname())) {
107 exit_server_cleanly("Password server loop!");
110 DEBUG(3,("got session\n"));
112 if (!cli_negprot(cli)) {
113 DEBUG(1,("%s rejected the negprot\n",desthost));
114 release_server_mutex();
119 if (cli->protocol < PROTOCOL_LANMAN2 ||
120 !(cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
121 DEBUG(1,("%s isn't in user level security mode\n",desthost));
122 release_server_mutex();
127 /* Get the first session setup done quickly, to avoid silly
128 Win2k bugs. (The next connection to the server will kill
132 if (!NT_STATUS_IS_OK(cli_session_setup(cli, "", "", 0, "", 0,
134 DEBUG(0,("%s rejected the initial session setup (%s)\n",
135 desthost, cli_errstr(cli)));
136 release_server_mutex();
141 release_server_mutex();
143 DEBUG(3,("password server OK\n"));
148 struct server_security_state {
149 struct cli_state *cli;
152 /****************************************************************************
153 Send a 'keepalive' packet down the cli pipe.
154 ****************************************************************************/
156 static bool send_server_keepalive(const struct timeval *now,
159 struct server_security_state *state = talloc_get_type_abort(
160 private_data, struct server_security_state);
162 if (!state->cli || !state->cli->initialised) {
166 if (send_keepalive(state->cli->fd)) {
170 DEBUG( 2, ( "send_server_keepalive: password server keepalive "
172 cli_shutdown(state->cli);
177 static int destroy_server_security(struct server_security_state *state)
180 cli_shutdown(state->cli);
185 static struct server_security_state *make_server_security_state(struct cli_state *cli)
187 struct server_security_state *result;
189 if (!(result = talloc(NULL, struct server_security_state))) {
190 DEBUG(0, ("talloc failed\n"));
196 talloc_set_destructor(result, destroy_server_security);
198 if (lp_keepalive() != 0) {
199 struct timeval interval;
200 interval.tv_sec = lp_keepalive();
201 interval.tv_usec = 0;
203 if (event_add_idle(smbd_event_context(), result, interval,
204 "server_security_keepalive",
205 send_server_keepalive,
207 DEBUG(0, ("event_add_idle failed\n"));
216 /****************************************************************************
217 Get the challenge out of a password server.
218 ****************************************************************************/
220 static DATA_BLOB auth_get_challenge_server(const struct auth_context *auth_context,
221 void **my_private_data,
224 struct cli_state *cli = server_cryptkey(mem_ctx);
227 DEBUG(3,("using password server validation\n"));
229 if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
230 /* We can't work with unencrypted password servers
231 unless 'encrypt passwords = no' */
232 DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
234 /* However, it is still a perfectly fine connection
235 to pass that unencrypted password over */
237 (void *)make_server_security_state(cli);
238 return data_blob_null;
239 } else if (cli->secblob.length < 8) {
240 /* We can't do much if we don't get a full challenge */
241 DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
243 return data_blob_null;
246 if (!(*my_private_data = (void *)make_server_security_state(cli))) {
247 return data_blob(NULL,0);
250 /* The return must be allocated on the caller's mem_ctx, as our own will be
251 destoyed just after the call. */
252 return data_blob_talloc(auth_context->mem_ctx, cli->secblob.data,8);
254 return data_blob_null;
259 /****************************************************************************
260 Check for a valid username and password in security=server mode.
261 - Validate a password with the password server.
262 ****************************************************************************/
264 static NTSTATUS check_smbserver_security(const struct auth_context *auth_context,
265 void *my_private_data,
267 const auth_usersupplied_info *user_info,
268 auth_serversupplied_info **server_info)
270 struct cli_state *cli;
271 static bool tested_password_server = False;
272 static bool bad_password_server = False;
273 NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
274 bool locally_made_cli = False;
276 cli = (struct cli_state *)my_private_data;
280 cli = server_cryptkey(mem_ctx);
281 locally_made_cli = True;
284 if (!cli || !cli->initialised) {
285 DEBUG(1,("password server is not connected (cli not initilised)\n"));
286 return NT_STATUS_LOGON_FAILURE;
289 if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
290 if (user_info->encrypted) {
291 DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
292 return NT_STATUS_LOGON_FAILURE;
295 if (memcmp(cli->secblob.data, auth_context->challenge.data, 8) != 0) {
296 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));
297 return NT_STATUS_LOGON_FAILURE;
302 * Attempt a session setup with a totally incorrect password.
303 * If this succeeds with the guest bit *NOT* set then the password
304 * server is broken and is not correctly setting the guest bit. We
305 * need to detect this as some versions of NT4.x are broken. JRA.
308 /* I sure as hell hope that there aren't servers out there that take
309 * NTLMv2 and have this bug, as we don't test for that...
310 * - abartlet@samba.org
313 if ((!tested_password_server) && (lp_paranoid_server_security())) {
314 unsigned char badpass[24];
315 char *baduser = NULL;
317 memset(badpass, 0x1f, sizeof(badpass));
319 if((user_info->nt_resp.length == sizeof(badpass)) &&
320 !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
322 * Very unlikely, our random bad password is the same as the users
325 memset(badpass, badpass[0]+1, sizeof(badpass));
328 baduser = talloc_asprintf(mem_ctx,
333 return NT_STATUS_NO_MEMORY;
336 if (NT_STATUS_IS_OK(cli_session_setup(cli, baduser,
341 user_info->domain))) {
344 * We connected to the password server so we
345 * can say we've tested it.
347 tested_password_server = True;
349 if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
350 DEBUG(0,("server_validate: password server %s allows users as non-guest \
351 with a bad password.\n", cli->desthost));
352 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
353 use this machine as the password server.\n"));
357 * Password server has the bug.
359 bad_password_server = True;
360 return NT_STATUS_LOGON_FAILURE;
367 * We have already tested the password server.
368 * Fail immediately if it has the bug.
371 if(bad_password_server) {
372 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
373 with a bad password.\n", cli->desthost));
374 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
375 use this machine as the password server.\n"));
376 return NT_STATUS_LOGON_FAILURE;
381 * Now we know the password server will correctly set the guest bit, or is
382 * not guest enabled, we can try with the real password.
385 if (!user_info->encrypted) {
386 /* Plaintext available */
387 nt_status = cli_session_setup(
388 cli, user_info->smb_name,
389 (char *)user_info->plaintext_password.data,
390 user_info->plaintext_password.length,
391 NULL, 0, user_info->domain);
394 nt_status = cli_session_setup(
395 cli, user_info->smb_name,
396 (char *)user_info->lm_resp.data,
397 user_info->lm_resp.length,
398 (char *)user_info->nt_resp.data,
399 user_info->nt_resp.length,
403 if (!NT_STATUS_IS_OK(nt_status)) {
404 DEBUG(1,("password server %s rejected the password: %s\n",
405 cli->desthost, nt_errstr(nt_status)));
408 /* if logged in as guest then reject */
409 if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
410 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
411 nt_status = NT_STATUS_LOGON_FAILURE;
416 if (NT_STATUS_IS_OK(nt_status)) {
417 fstring real_username;
420 if ( (pass = smb_getpwnam( NULL, user_info->internal_username,
421 real_username, True )) != NULL )
423 /* if a real user check pam account restrictions */
424 /* only really perfomed if "obey pam restriction" is true */
425 nt_status = smb_pam_accountcheck(pass->pw_name);
426 if ( !NT_STATUS_IS_OK(nt_status)) {
427 DEBUG(1, ("PAM account restriction prevents user login\n"));
430 nt_status = make_server_info_pw(server_info, pass->pw_name, pass);
436 nt_status = NT_STATUS_NO_SUCH_USER;
440 if (locally_made_cli) {
447 static NTSTATUS auth_init_smbserver(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
449 if (!make_auth_methods(auth_context, auth_method)) {
450 return NT_STATUS_NO_MEMORY;
452 (*auth_method)->name = "smbserver";
453 (*auth_method)->auth = check_smbserver_security;
454 (*auth_method)->get_chal = auth_get_challenge_server;
458 NTSTATUS auth_server_init(void)
460 return smb_register_auth(AUTH_INTERFACE_VERSION, "smbserver", auth_init_smbserver);