summaryrefslogtreecommitdiff
path: root/source4/smb_server/smb/negprot.c
blob: 04bae886b6d22c2bb51161c4c78f63d311d147cd (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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/* 
   Unix SMB/CIFS implementation.
   negprot reply code
   Copyright (C) Andrew Tridgell 1992-1998
   
   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "auth/credentials/credentials.h"
#include "auth/gensec/gensec.h"
#include "auth/auth.h"
#include "smb_server/smb_server.h"
#include "libcli/smb2/smb2.h"
#include "smb_server/smb2/smb2_server.h"
#include "smb_server/service_smb_proto.h"
#include "smbd/service_stream.h"
#include "lib/stream/packet.h"


/* initialise the auth_context for this server and return the cryptkey */
static NTSTATUS get_challenge(struct smbsrv_connection *smb_conn, uint8_t buff[8]) 
{
	NTSTATUS nt_status;
	const uint8_t *challenge;

	/* muliple negprots are not premitted */
	if (smb_conn->negotiate.auth_context) {
		DEBUG(3,("get challenge: is this a secondary negprot?  auth_context is non-NULL!\n"));
		return NT_STATUS_FOOBAR;
	}

	DEBUG(10, ("get challenge: creating negprot_global_auth_context\n"));

	nt_status = auth_context_create(smb_conn, lp_auth_methods(), 
					smb_conn->connection->event.ctx,
					smb_conn->connection->msg_ctx,
					&smb_conn->negotiate.auth_context);
	if (!NT_STATUS_IS_OK(nt_status)) {
		DEBUG(0, ("auth_context_create() returned %s", nt_errstr(nt_status)));
		return nt_status;
	}

	nt_status = auth_get_challenge(smb_conn->negotiate.auth_context, &challenge);
	if (!NT_STATUS_IS_OK(nt_status)) {
		DEBUG(0, ("auth_get_challenge() returned %s", nt_errstr(nt_status)));
		return nt_status;
	}

	memcpy(buff, challenge, 8);

	return NT_STATUS_OK;
}

/****************************************************************************
 Reply for the core protocol.
****************************************************************************/
static void reply_corep(struct smbsrv_request *req, uint16_t choice)
{
	smbsrv_setup_reply(req, 1, 0);

	SSVAL(req->out.vwv, VWV(0), choice);

	req->smb_conn->negotiate.protocol = PROTOCOL_CORE;

	if (req->smb_conn->signing.mandatory_signing) {
		smbsrv_terminate_connection(req->smb_conn, 
					    "CORE does not support SMB signing, and it is mandatory\n");
		return;
	}

	smbsrv_send_reply(req);
}

/****************************************************************************
 Reply for the coreplus protocol.
this is quite incomplete - we only fill in a small part of the reply, but as nobody uses
this any more it probably doesn't matter
****************************************************************************/
static void reply_coreplus(struct smbsrv_request *req, uint16_t choice)
{
	uint16_t raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);

	smbsrv_setup_reply(req, 13, 0);

	/* Reply, SMBlockread, SMBwritelock supported. */
	SCVAL(req->out.hdr,HDR_FLG,
	      CVAL(req->out.hdr, HDR_FLG) | FLAG_SUPPORT_LOCKREAD);

	SSVAL(req->out.vwv, VWV(0), choice);
	SSVAL(req->out.vwv, VWV(1), 0x1); /* user level security, don't encrypt */	

	/* tell redirector we support
	   readbraw and writebraw (possibly) */
	SSVAL(req->out.vwv, VWV(5), raw); 

	req->smb_conn->negotiate.protocol = PROTOCOL_COREPLUS;

	if (req->smb_conn->signing.mandatory_signing) {
		smbsrv_terminate_connection(req->smb_conn, 
					    "COREPLUS does not support SMB signing, and it is mandatory\n");
		return;
	}

	smbsrv_send_reply(req);
}

/****************************************************************************
 Reply for the lanman 1.0 protocol.
****************************************************************************/
static void reply_lanman1(struct smbsrv_request *req, uint16_t choice)
{
	int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
	int secword=0;
	time_t t = req->request_time.tv_sec;

	req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();

	if (lp_security() != SEC_SHARE)
		secword |= NEGOTIATE_SECURITY_USER_LEVEL;

	if (req->smb_conn->negotiate.encrypted_passwords)
		secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;

	req->smb_conn->negotiate.protocol = PROTOCOL_LANMAN1;

	smbsrv_setup_reply(req, 13, req->smb_conn->negotiate.encrypted_passwords ? 8 : 0);

	/* SMBlockread, SMBwritelock supported. */
	SCVAL(req->out.hdr,HDR_FLG,
	      CVAL(req->out.hdr, HDR_FLG) | FLAG_SUPPORT_LOCKREAD);

	SSVAL(req->out.vwv, VWV(0), choice);
	SSVAL(req->out.vwv, VWV(1), secword); 
	SSVAL(req->out.vwv, VWV(2), req->smb_conn->negotiate.max_recv);
	SSVAL(req->out.vwv, VWV(3), lp_maxmux());
	SSVAL(req->out.vwv, VWV(4), 1);
	SSVAL(req->out.vwv, VWV(5), raw); 
	SIVAL(req->out.vwv, VWV(6), req->smb_conn->connection->server_id.id);
	srv_push_dos_date(req->smb_conn, req->out.vwv, VWV(8), t);
	SSVAL(req->out.vwv, VWV(10), req->smb_conn->negotiate.zone_offset/60);
	SIVAL(req->out.vwv, VWV(11), 0); /* reserved */

	/* Create a token value and add it to the outgoing packet. */
	if (req->smb_conn->negotiate.encrypted_passwords) {
		NTSTATUS nt_status;

		SSVAL(req->out.vwv, VWV(11), 8);

		nt_status = get_challenge(req->smb_conn, req->out.data);
		if (!NT_STATUS_IS_OK(nt_status)) {
			smbsrv_terminate_connection(req->smb_conn, "LANMAN1 get_challenge failed\n");
			return;
		}
	}

	if (req->smb_conn->signing.mandatory_signing) {
		smbsrv_terminate_connection(req->smb_conn, 
					    "LANMAN1 does not support SMB signing, and it is mandatory\n");
		return;
	}

	smbsrv_send_reply(req);	
}

/****************************************************************************
 Reply for the lanman 2.0 protocol.
****************************************************************************/
static void reply_lanman2(struct smbsrv_request *req, uint16_t choice)
{
	int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
	int secword=0;
	time_t t = req->request_time.tv_sec;

	req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
  
	if (lp_security() != SEC_SHARE)
		secword |= NEGOTIATE_SECURITY_USER_LEVEL;

	if (req->smb_conn->negotiate.encrypted_passwords)
		secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;

	req->smb_conn->negotiate.protocol = PROTOCOL_LANMAN2;

	smbsrv_setup_reply(req, 13, 0);

	SSVAL(req->out.vwv, VWV(0), choice);
	SSVAL(req->out.vwv, VWV(1), secword); 
	SSVAL(req->out.vwv, VWV(2), req->smb_conn->negotiate.max_recv);
	SSVAL(req->out.vwv, VWV(3), lp_maxmux());
	SSVAL(req->out.vwv, VWV(4), 1);
	SSVAL(req->out.vwv, VWV(5), raw); 
	SIVAL(req->out.vwv, VWV(6), req->smb_conn->connection->server_id.id);
	srv_push_dos_date(req->smb_conn, req->out.vwv, VWV(8), t);
	SSVAL(req->out.vwv, VWV(10), req->smb_conn->negotiate.zone_offset/60);
	SIVAL(req->out.vwv, VWV(11), 0);

	/* Create a token value and add it to the outgoing packet. */
	if (req->smb_conn->negotiate.encrypted_passwords) {
		SSVAL(req->out.vwv, VWV(11), 8);
		req_grow_data(req, 8);
		get_challenge(req->smb_conn, req->out.data);
	}

	req_push_str(req, NULL, lp_workgroup(), -1, STR_TERMINATE);

	if (req->smb_conn->signing.mandatory_signing) {
		smbsrv_terminate_connection(req->smb_conn, 
					    "LANMAN2 does not support SMB signing, and it is mandatory\n");
		return;
	}

	smbsrv_send_reply(req);
}

static void reply_nt1_orig(struct smbsrv_request *req)
{
	/* Create a token value and add it to the outgoing packet. */
	if (req->smb_conn->negotiate.encrypted_passwords) {
		req_grow_data(req, 8);
		/* note that we do not send a challenge at all if
		   we are using plaintext */
		get_challenge(req->smb_conn, req->out.ptr);
		req->out.ptr += 8;
		SCVAL(req->out.vwv+1, VWV(16), 8);
	}
	req_push_str(req, NULL, lp_workgroup(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
	req_push_str(req, NULL, lp_netbios_name(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
	DEBUG(3,("not using extended security (SPNEGO or NTLMSSP)\n"));
}

/****************************************************************************
 Reply for the nt protocol.
****************************************************************************/
static void reply_nt1(struct smbsrv_request *req, uint16_t choice)
{
	/* dual names + lock_and_read + nt SMBs + remote API calls */
	int capabilities;
	int secword=0;
	time_t t = req->request_time.tv_sec;
	NTTIME nttime;
	BOOL negotiate_spnego = False;
	char *large_test_path;

	unix_to_nt_time(&nttime, t);

	capabilities = 
		CAP_NT_FIND | CAP_LOCK_AND_READ | 
		CAP_LEVEL_II_OPLOCKS | CAP_NT_SMBS | CAP_RPC_REMOTE_APIS;

	req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();

	/* do spnego in user level security if the client
	   supports it and we can do encrypted passwords */
	
	if (req->smb_conn->negotiate.encrypted_passwords && 
	    (lp_security() != SEC_SHARE) &&
	    lp_use_spnego() &&
	    (req->flags2 & FLAGS2_EXTENDED_SECURITY)) {
		negotiate_spnego = True; 
		capabilities |= CAP_EXTENDED_SECURITY;
	}
	
	if (lp_unix_extensions()) {
		capabilities |= CAP_UNIX;
	}
	
	if (lp_large_readwrite()) {
		capabilities |= CAP_LARGE_READX | CAP_LARGE_WRITEX | CAP_W2K_SMBS;
	}

	large_test_path = lock_path(req, "large_test.dat");
	if (large_file_support(large_test_path)) {
		capabilities |= CAP_LARGE_FILES;
	}

	if (lp_readraw() && lp_writeraw()) {
		capabilities |= CAP_RAW_MODE;
	}
	
	/* allow for disabling unicode */
	if (lp_unicode()) {
		capabilities |= CAP_UNICODE;
	}

	if (lp_nt_status_support()) {
		capabilities |= CAP_STATUS32;
	}
	
	if (lp_host_msdfs()) {
		capabilities |= CAP_DFS;
	}
	
	if (lp_security() != SEC_SHARE) {
		secword |= NEGOTIATE_SECURITY_USER_LEVEL;
	}

	if (req->smb_conn->negotiate.encrypted_passwords) {
		secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
	}

	if (req->smb_conn->signing.allow_smb_signing) {
		secword |= NEGOTIATE_SECURITY_SIGNATURES_ENABLED;
	}

	if (req->smb_conn->signing.mandatory_signing) {
		secword |= NEGOTIATE_SECURITY_SIGNATURES_REQUIRED;
	}
	
	req->smb_conn->negotiate.protocol = PROTOCOL_NT1;

	smbsrv_setup_reply(req, 17, 0);
	
	SSVAL(req->out.vwv, VWV(0), choice);
	SCVAL(req->out.vwv, VWV(1), secword);

	/* notice the strange +1 on vwv here? That's because
	   this is the one and only SMB packet that is malformed in
	   the specification - all the command words after the secword
	   are offset by 1 byte */
	SSVAL(req->out.vwv+1, VWV(1), lp_maxmux());
	SSVAL(req->out.vwv+1, VWV(2), 1); /* num vcs */
	SIVAL(req->out.vwv+1, VWV(3), req->smb_conn->negotiate.max_recv);
	SIVAL(req->out.vwv+1, VWV(5), 0x10000); /* raw size. full 64k */
	SIVAL(req->out.vwv+1, VWV(7), req->smb_conn->connection->server_id.id); /* session key */
	SIVAL(req->out.vwv+1, VWV(9), capabilities);
	push_nttime(req->out.vwv+1, VWV(11), nttime);
	SSVALS(req->out.vwv+1,VWV(15), req->smb_conn->negotiate.zone_offset/60);
	
	if (!negotiate_spnego) {
		reply_nt1_orig(req);
	} else {
		struct cli_credentials *server_credentials;
		struct gensec_security *gensec_security;
		DATA_BLOB null_data_blob = data_blob(NULL, 0);
		DATA_BLOB blob;
		const char *oid;
		NTSTATUS nt_status;

		nt_status = gensec_server_start(req->smb_conn,
						req->smb_conn->connection->event.ctx,
						req->smb_conn->connection->msg_ctx,
						&gensec_security);
		if (!NT_STATUS_IS_OK(nt_status)) {
			DEBUG(0, ("Failed to start GENSEC: %s\n", nt_errstr(nt_status)));
			smbsrv_terminate_connection(req->smb_conn, "Failed to start GENSEC\n");
			return;
		}

		if (req->smb_conn->negotiate.auth_context) {
			smbsrv_terminate_connection(req->smb_conn, "reply_nt1: is this a secondary negprot?  auth_context is non-NULL!\n");
			return;
		}

		server_credentials 
			= cli_credentials_init(req);
		if (!server_credentials) {
			smbsrv_terminate_connection(req->smb_conn, "Failed to init server credentials\n");
			return;
		}
		
		cli_credentials_set_conf(server_credentials);
		nt_status = cli_credentials_set_machine_account(server_credentials);
		if (!NT_STATUS_IS_OK(nt_status)) {
			DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(nt_status)));
			talloc_free(server_credentials);
			server_credentials = NULL;
		}

		req->smb_conn->negotiate.server_credentials = talloc_steal(req->smb_conn, server_credentials);

		gensec_set_target_service(gensec_security, "cifs");

		gensec_set_credentials(gensec_security, server_credentials);

		oid = GENSEC_OID_SPNEGO;
		nt_status = gensec_start_mech_by_oid(gensec_security, oid);
		
		if (NT_STATUS_IS_OK(nt_status)) {
			/* Get and push the proposed OID list into the packets */
			nt_status = gensec_update(gensec_security, req, null_data_blob, &blob);

			if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
				DEBUG(1, ("Failed to get SPNEGO to give us the first token: %s\n", nt_errstr(nt_status)));
			}
		}

		if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
			DEBUG(3,("using SPNEGO\n"));
		} else {
			DEBUG(5, ("Failed to start SPNEGO, falling back to NTLMSSP only: %s\n", nt_errstr(nt_status)));
			oid = GENSEC_OID_NTLMSSP;
			nt_status = gensec_start_mech_by_oid(gensec_security, oid);
			
			if (!NT_STATUS_IS_OK(nt_status)) {
				DEBUG(0, ("Failed to start SPNEGO as well as NTLMSSP fallback: %s\n", nt_errstr(nt_status)));
				reply_nt1_orig(req);
				return;
			}
			/* NTLMSSP is a client-first exchange */
			blob = data_blob(NULL, 0);
			DEBUG(3,("using raw-NTLMSSP\n"));
		}

		req->smb_conn->negotiate.oid = oid;
	
		req_grow_data(req, blob.length + 16);
		/* a NOT very random guid, perhaps we should get it
		 * from the credentials (kitchen sink...) */
		memset(req->out.ptr, '\0', 16);
		req->out.ptr += 16;

		memcpy(req->out.ptr, blob.data, blob.length);
		SCVAL(req->out.vwv+1, VWV(16), blob.length + 16);
		req->out.ptr += blob.length;
	}
	
	smbsrv_send_reply_nosign(req);	
}

/****************************************************************************
 Reply for the SMB2 2.001 protocol
****************************************************************************/
static void reply_smb2(struct smbsrv_request *req, uint16_t choice)
{
	struct smbsrv_connection *smb_conn = req->smb_conn;

	/* reply with a SMB2 packet */
	packet_set_callback(smb_conn->packet, smbsrv_recv_smb2_request);
	smb2srv_reply_smb_negprot(req);
	req = NULL;
}

/* List of supported protocols, most desired first */
static const struct {
	const char *proto_name;
	const char *short_name;
	void (*proto_reply_fn)(struct smbsrv_request *req, uint16_t choice);
	int protocol_level;
} supported_protocols[] = {
	{"SMB 2.001",			"SMB2",		reply_smb2,	PROTOCOL_SMB2},
	{"NT LANMAN 1.0",		"NT1",		reply_nt1,	PROTOCOL_NT1},
	{"NT LM 0.12",			"NT1",		reply_nt1,	PROTOCOL_NT1},
	{"LANMAN2.1",			"LANMAN2",	reply_lanman2,	PROTOCOL_LANMAN2},
	{"LM1.2X002",			"LANMAN2",	reply_lanman2,	PROTOCOL_LANMAN2},
	{"Samba",			"LANMAN2",	reply_lanman2,	PROTOCOL_LANMAN2},
	{"DOS LM1.2X002",		"LANMAN2",	reply_lanman2,	PROTOCOL_LANMAN2},
	{"Windows for Workgroups 3.1a",	"LANMAN1",	reply_lanman1,	PROTOCOL_LANMAN1},
	{"LANMAN1.0",			"LANMAN1",	reply_lanman1,	PROTOCOL_LANMAN1},
	{"MICROSOFT NETWORKS 3.0",	"LANMAN1",	reply_lanman1,	PROTOCOL_LANMAN1},
	{"MICROSOFT NETWORKS 1.03",	"COREPLUS",	reply_coreplus,	PROTOCOL_COREPLUS},
	{"PC NETWORK PROGRAM 1.0",	"CORE",		reply_corep,	PROTOCOL_CORE},
	{NULL,NULL,NULL,0},
};

/****************************************************************************
 Reply to a negprot.
****************************************************************************/

void smbsrv_reply_negprot(struct smbsrv_request *req)
{
	int protocol;
	uint8_t *p;
	uint32_t protos_count = 0;
	char **protos = NULL;

	if (req->smb_conn->negotiate.done_negprot) {
		smbsrv_terminate_connection(req->smb_conn, "multiple negprot's are not permitted");
		return;
	}
	req->smb_conn->negotiate.done_negprot = True;

	p = req->in.data;
	while (True) {
		size_t len;

		protos = talloc_realloc(req, protos, char *, protos_count + 1);
		if (!protos) {
			smbsrv_terminate_connection(req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
			return;
		}
		protos[protos_count] = NULL;
		len = req_pull_ascii4(req, (const char **)&protos[protos_count], p, STR_ASCII|STR_TERMINATE);
		p += len;
		if (len == 0 || !protos[protos_count]) break;

		DEBUG(5,("Requested protocol [%d][%s]\n", protos_count, protos[protos_count]));
		protos_count++;
	}

	/* Check for protocols, most desirable first */
	for (protocol = 0; supported_protocols[protocol].proto_name; protocol++) {
		int i;

		if (supported_protocols[protocol].protocol_level > lp_srv_maxprotocol()) continue;
		if (supported_protocols[protocol].protocol_level < lp_srv_minprotocol()) continue;

		for (i = 0; i < protos_count; i++) {
			if (strcmp(supported_protocols[protocol].proto_name, protos[i]) != 0) continue;

			supported_protocols[protocol].proto_reply_fn(req, i);
			DEBUG(3,("Selected protocol [%d][%s]\n",
				i, supported_protocols[protocol].proto_name));
			return;
		}
	}

	smbsrv_terminate_connection(req->smb_conn, "No protocol supported !");
}