summaryrefslogtreecommitdiff
path: root/source3/auth/auth_server.c
blob: d50ed7a024b48983263311a3e6bbb3642c54ed10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/* 
   Unix SMB/CIFS implementation.
   Authenticate to a remote server
   Copyright (C) Andrew Tridgell 1992-1998
   Copyright (C) Andrew Bartlett 2001

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "auth.h"
#include "system/passwd.h"
#include "smbd/smbd.h"
#include "libsmb/libsmb.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_AUTH

extern userdom_struct current_user_info;

/****************************************************************************
 Support for server level security.
****************************************************************************/

static struct cli_state *server_cryptkey(TALLOC_CTX *mem_ctx)
{
	struct cli_state *cli = NULL;
	char *desthost = NULL;
	struct sockaddr_storage dest_ss;
	const char *p;
	char *pserver = NULL;
	bool connected_ok = False;
	struct named_mutex *mutex = NULL;
	NTSTATUS status;

        pserver = talloc_strdup(mem_ctx, lp_passwordserver());
	p = pserver;

        while(next_token_talloc(mem_ctx, &p, &desthost, LIST_SEP)) {

		desthost = talloc_sub_basic(mem_ctx,
				current_user_info.smb_name,
				current_user_info.domain,
				desthost);
		if (!desthost) {
			return NULL;
		}
		strupper_m(desthost);

		if (strequal(desthost, myhostname())) {
			DEBUG(1,("Password server loop - disabling "
				 "password server %s\n", desthost));
			continue;
		}

		if(!resolve_name( desthost, &dest_ss, 0x20, false)) {
			DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
			continue;
		}

		if (ismyaddr((struct sockaddr *)(void *)&dest_ss)) {
			DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
			continue;
		}

		/* we use a mutex to prevent two connections at once - when a
		   Win2k PDC get two connections where one hasn't completed a
		   session setup yet it will send a TCP reset to the first
		   connection (tridge) */

		mutex = grab_named_mutex(talloc_tos(), desthost, 10);
		if (mutex == NULL) {
			return NULL;
		}

		status = cli_connect_nb(desthost, &dest_ss, 0, 0x20,
					lp_netbios_name(), Undefined, &cli);
		if (NT_STATUS_IS_OK(status)) {
			DEBUG(3,("connected to password server %s\n",desthost));
			connected_ok = True;
			break;
		}
		DEBUG(10,("server_cryptkey: failed to connect to server %s. Error %s\n",
			desthost, nt_errstr(status) ));
		TALLOC_FREE(mutex);
	}

	if (!connected_ok) {
		DEBUG(0,("password server not available\n"));
		return NULL;
	}

	/* security = server just can't function with spnego */
	cli->use_spnego = False;

	DEBUG(3,("got session\n"));

	status = cli_negprot(cli);

	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(mutex);
		DEBUG(1, ("%s rejected the negprot: %s\n",
			  desthost, nt_errstr(status)));
		cli_shutdown(cli);
		return NULL;
	}

	if (cli->protocol < PROTOCOL_LANMAN2 ||
	    !(cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
		TALLOC_FREE(mutex);
		DEBUG(1,("%s isn't in user level security mode\n",desthost));
		cli_shutdown(cli);
		return NULL;
	}

	/* Get the first session setup done quickly, to avoid silly
	   Win2k bugs.  (The next connection to the server will kill
	   this one...
	*/

	status = cli_session_setup(cli, "", "", 0, "", 0, "");
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(mutex);
		DEBUG(0,("%s rejected the initial session setup (%s)\n",
			 desthost, nt_errstr(status)));
		cli_shutdown(cli);
		return NULL;
	}

	TALLOC_FREE(mutex);

	DEBUG(3,("password server OK\n"));

	return cli;
}

struct server_security_state {
	struct cli_state *cli;
};

/****************************************************************************
 Send a 'keepalive' packet down the cli pipe.
****************************************************************************/

static bool send_server_keepalive(const struct timeval *now,
				  void *private_data)
{
	struct server_security_state *state = talloc_get_type_abort(
		private_data, struct server_security_state);

	if (!state->cli || !state->cli->initialised) {
		return False;
	}

	if (send_keepalive(state->cli->fd)) {
		return True;
	}

	DEBUG( 2, ( "send_server_keepalive: password server keepalive "
		    "failed.\n"));
	cli_shutdown(state->cli);
	state->cli = NULL;
	return False;
}

static int destroy_server_security(struct server_security_state *state)
{
	if (state->cli) {
		cli_shutdown(state->cli);
	}
	return 0;
}

static struct server_security_state *make_server_security_state(struct cli_state *cli)
{
	struct server_security_state *result;

	if (!(result = talloc(NULL, struct server_security_state))) {
		DEBUG(0, ("talloc failed\n"));
		cli_shutdown(cli);
		return NULL;
	}

	result->cli = cli;
	talloc_set_destructor(result, destroy_server_security);

	if (lp_keepalive() != 0) {
		struct timeval interval;
		interval.tv_sec = lp_keepalive();
		interval.tv_usec = 0;

		if (event_add_idle(server_event_context(), result, interval,
				   "server_security_keepalive",
				   send_server_keepalive,
				   result) == NULL) {
			DEBUG(0, ("event_add_idle failed\n"));
			TALLOC_FREE(result);
			return NULL;
		}
	}

	return result;
}

/****************************************************************************
 Get the challenge out of a password server.
****************************************************************************/

static DATA_BLOB auth_get_challenge_server(const struct auth_context *auth_context,
					   void **my_private_data, 
					   TALLOC_CTX *mem_ctx)
{
	struct cli_state *cli = server_cryptkey(mem_ctx);

	if (cli) {
		DEBUG(3,("using password server validation\n"));

		if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
			/* We can't work with unencrypted password servers
			   unless 'encrypt passwords = no' */
			DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));

			/* However, it is still a perfectly fine connection
			   to pass that unencrypted password over */
			*my_private_data =
				(void *)make_server_security_state(cli);
			return data_blob_null;
		} else if (cli->secblob.length < 8) {
			/* We can't do much if we don't get a full challenge */
			DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
			cli_shutdown(cli);
			return data_blob_null;
		}

		if (!(*my_private_data = (void *)make_server_security_state(cli))) {
			return data_blob(NULL,0);
		}

		/* The return must be allocated on the caller's mem_ctx, as our own will be
		   destoyed just after the call. */
		return data_blob_talloc(discard_const_p(TALLOC_CTX, auth_context), cli->secblob.data,8);
	} else {
		return data_blob_null;
	}
}


/****************************************************************************
 Check for a valid username and password in security=server mode.
  - Validate a password with the password server.
****************************************************************************/

static NTSTATUS check_smbserver_security(const struct auth_context *auth_context,
					 void *my_private_data, 
					 TALLOC_CTX *mem_ctx,
					 const struct auth_usersupplied_info *user_info,
					 struct auth_serversupplied_info **server_info)
{
	struct server_security_state *state = talloc_get_type_abort(
		my_private_data, struct server_security_state);
	struct cli_state *cli;
	static bool tested_password_server = False;
	static bool bad_password_server = False;
	NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
	bool locally_made_cli = False;

	DEBUG(10, ("Check auth for: [%s]\n", user_info->mapped.account_name));

	cli = state->cli;

	if (cli) {
	} else {
		cli = server_cryptkey(mem_ctx);
		locally_made_cli = True;
	}

	if (!cli || !cli->initialised) {
		DEBUG(1,("password server is not connected (cli not initialised)\n"));
		return NT_STATUS_LOGON_FAILURE;
	}  

	if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
		if (user_info->password_state != AUTH_PASSWORD_PLAIN) {
			DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
			return NT_STATUS_LOGON_FAILURE;		
		}
	} else {
		if (memcmp(cli->secblob.data, auth_context->challenge.data, 8) != 0) {
			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));
			return NT_STATUS_LOGON_FAILURE;		
		}
	}

	/*
	 * Attempt a session setup with a totally incorrect password.
	 * If this succeeds with the guest bit *NOT* set then the password
	 * server is broken and is not correctly setting the guest bit. We
	 * need to detect this as some versions of NT4.x are broken. JRA.
	 */

	/* I sure as hell hope that there aren't servers out there that take 
	 * NTLMv2 and have this bug, as we don't test for that... 
	 *  - abartlet@samba.org
	 */

	if ((!tested_password_server) && (lp_paranoid_server_security())) {
		unsigned char badpass[24];
		char *baduser = NULL;

		memset(badpass, 0x1f, sizeof(badpass));

		if((user_info->password.response.nt.length == sizeof(badpass)) &&
		   !memcmp(badpass, user_info->password.response.nt.data, sizeof(badpass))) {
			/* 
			 * Very unlikely, our random bad password is the same as the users
			 * password.
			 */
			memset(badpass, badpass[0]+1, sizeof(badpass));
		}

		baduser = talloc_asprintf(mem_ctx,
					"%s%s",
					INVALID_USER_PREFIX,
					lp_netbios_name());
		if (!baduser) {
			return NT_STATUS_NO_MEMORY;
		}

		if (NT_STATUS_IS_OK(cli_session_setup(cli, baduser,
						      (char *)badpass,
						      sizeof(badpass), 
						      (char *)badpass,
						      sizeof(badpass),
						      user_info->mapped.domain_name))) {

			/*
			 * We connected to the password server so we
			 * can say we've tested it.
			 */
			tested_password_server = True;

			if (!cli->is_guestlogin) {
				DEBUG(0,("server_validate: password server %s allows users as non-guest \
with a bad password.\n", cli->desthost));
				DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
use this machine as the password server.\n"));
				cli_ulogoff(cli);

				/*
				 * Password server has the bug.
				 */
				bad_password_server = True;
				return NT_STATUS_LOGON_FAILURE;
			}
			cli_ulogoff(cli);
		}
	} else {

		/*
		 * We have already tested the password server.
		 * Fail immediately if it has the bug.
		 */

		if(bad_password_server) {
			DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
with a bad password.\n", cli->desthost));
			DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
use this machine as the password server.\n"));
			return NT_STATUS_LOGON_FAILURE;
		}
	}

	/*
	 * Now we know the password server will correctly set the guest bit, or is
	 * not guest enabled, we can try with the real password.
	 */
	switch (user_info->password_state) {
	case AUTH_PASSWORD_PLAIN:
		/* Plaintext available */
		nt_status = cli_session_setup(
			cli, user_info->client.account_name,
			user_info->password.plaintext,
			strlen(user_info->password.plaintext),
			NULL, 0, user_info->mapped.domain_name);
		break;

	/* currently the hash values include a challenge-response as well */
	case AUTH_PASSWORD_HASH:
	case AUTH_PASSWORD_RESPONSE:
		nt_status = cli_session_setup(
			cli, user_info->client.account_name,
			(char *)user_info->password.response.lanman.data,
			user_info->password.response.lanman.length,
			(char *)user_info->password.response.nt.data,
			user_info->password.response.nt.length,
			user_info->mapped.domain_name);
		break;
	default:
		DEBUG(0,("user_info constructed for user '%s' was invalid - password_state=%u invalid.\n",user_info->mapped.account_name, user_info->password_state));
		nt_status = NT_STATUS_INTERNAL_ERROR;
	}

	if (!NT_STATUS_IS_OK(nt_status)) {
		DEBUG(1,("password server %s rejected the password: %s\n",
			 cli->desthost, nt_errstr(nt_status)));
	}

	/* if logged in as guest then reject */
	if (cli->is_guestlogin) {
		DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
		nt_status = NT_STATUS_LOGON_FAILURE;
	}

	cli_ulogoff(cli);

	if (NT_STATUS_IS_OK(nt_status)) {
		char *real_username = NULL;
		struct passwd *pass = NULL;

		if ( (pass = smb_getpwnam(talloc_tos(), user_info->mapped.account_name,
			&real_username, True )) != NULL )
		{
			nt_status = make_server_info_pw(server_info, pass->pw_name, pass);
			TALLOC_FREE(pass);
			TALLOC_FREE(real_username);
		}
		else
		{
			nt_status = NT_STATUS_NO_SUCH_USER;
		}
	}

	if (locally_made_cli) {
		cli_shutdown(cli);
	}

	return(nt_status);
}

static NTSTATUS auth_init_smbserver(struct auth_context *auth_context, const char* param, auth_methods **auth_method) 
{
	struct auth_methods *result;

	result = talloc_zero(auth_context, struct auth_methods);
	if (result == NULL) {
		return NT_STATUS_NO_MEMORY;
	}
	result->name = "smbserver";
	result->auth = check_smbserver_security;
	result->get_chal = auth_get_challenge_server;

        *auth_method = result;
	return NT_STATUS_OK;
}

NTSTATUS auth_server_init(void)
{
	return smb_register_auth(AUTH_INTERFACE_VERSION, "smbserver", auth_init_smbserver);
}