2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "auth/auth.h"
23 #include "smb_server/smb_server.h"
24 #include "smbd/service_stream.h"
27 /* initialise the auth_context for this server and return the cryptkey */
28 static NTSTATUS get_challenge(struct smbsrv_connection *smb_conn, uint8_t buff[8])
31 const uint8_t *challenge;
33 /* muliple negprots are not premitted */
34 if (smb_conn->negotiate.auth_context) {
35 DEBUG(3,("get challenge: is this a secondary negprot? auth_context is non-NULL!\n"));
36 return NT_STATUS_FOOBAR;
39 DEBUG(10, ("get challenge: creating negprot_global_auth_context\n"));
41 nt_status = auth_context_create(smb_conn, lp_auth_methods(), &smb_conn->negotiate.auth_context);
42 if (!NT_STATUS_IS_OK(nt_status)) {
43 DEBUG(0, ("auth_context_create() returned %s", nt_errstr(nt_status)));
47 nt_status = auth_context_create(smb_conn, lp_auth_methods(), &smb_conn->negotiate.auth_context);
48 if (!NT_STATUS_IS_OK(nt_status)) {
49 DEBUG(0, ("auth_context_create() returned %s", nt_errstr(nt_status)));
53 nt_status = auth_get_challenge(smb_conn->negotiate.auth_context, &challenge);
54 if (!NT_STATUS_IS_OK(nt_status)) {
55 DEBUG(0, ("auth_get_challenge() returned %s", nt_errstr(nt_status)));
59 memcpy(buff, challenge, 8);
64 /****************************************************************************
65 Reply for the core protocol.
66 ****************************************************************************/
67 static void reply_corep(struct smbsrv_request *req, uint16_t choice)
69 req_setup_reply(req, 1, 0);
71 SSVAL(req->out.vwv, VWV(0), choice);
73 req->smb_conn->negotiate.protocol = PROTOCOL_CORE;
75 if (req->smb_conn->signing.mandatory_signing) {
76 smbsrv_terminate_connection(req->smb_conn,
77 "CORE does not support SMB signing, and it is mandatory\n");
84 /****************************************************************************
85 Reply for the coreplus protocol.
86 this is quite incomplete - we only fill in a small part of the reply, but as nobody uses
87 this any more it probably doesn't matter
88 ****************************************************************************/
89 static void reply_coreplus(struct smbsrv_request *req, uint16_t choice)
91 uint16_t raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
93 req_setup_reply(req, 13, 0);
95 /* Reply, SMBlockread, SMBwritelock supported. */
96 SCVAL(req->out.hdr,HDR_FLG,
97 CVAL(req->out.hdr, HDR_FLG) | FLAG_SUPPORT_LOCKREAD);
99 SSVAL(req->out.vwv, VWV(0), choice);
100 SSVAL(req->out.vwv, VWV(1), 0x1); /* user level security, don't encrypt */
102 /* tell redirector we support
103 readbraw and writebraw (possibly) */
104 SSVAL(req->out.vwv, VWV(5), raw);
106 req->smb_conn->negotiate.protocol = PROTOCOL_COREPLUS;
108 if (req->smb_conn->signing.mandatory_signing) {
109 smbsrv_terminate_connection(req->smb_conn,
110 "COREPLUS does not support SMB signing, and it is mandatory\n");
117 /****************************************************************************
118 Reply for the lanman 1.0 protocol.
119 ****************************************************************************/
120 static void reply_lanman1(struct smbsrv_request *req, uint16_t choice)
122 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
124 time_t t = req->request_time.tv_sec;
126 req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
128 if (lp_security() != SEC_SHARE)
129 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
131 if (req->smb_conn->negotiate.encrypted_passwords)
132 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
134 req->smb_conn->negotiate.protocol = PROTOCOL_LANMAN1;
136 req_setup_reply(req, 13, req->smb_conn->negotiate.encrypted_passwords ? 8 : 0);
138 /* SMBlockread, SMBwritelock supported. */
139 SCVAL(req->out.hdr,HDR_FLG,
140 CVAL(req->out.hdr, HDR_FLG) | FLAG_SUPPORT_LOCKREAD);
142 SSVAL(req->out.vwv, VWV(0), choice);
143 SSVAL(req->out.vwv, VWV(1), secword);
144 SSVAL(req->out.vwv, VWV(2), req->smb_conn->negotiate.max_recv);
145 SSVAL(req->out.vwv, VWV(3), lp_maxmux());
146 SSVAL(req->out.vwv, VWV(4), 1);
147 SSVAL(req->out.vwv, VWV(5), raw);
148 SIVAL(req->out.vwv, VWV(6), req->smb_conn->connection->server_id);
149 srv_push_dos_date(req->smb_conn, req->out.vwv, VWV(8), t);
150 SSVAL(req->out.vwv, VWV(10), req->smb_conn->negotiate.zone_offset/60);
151 SIVAL(req->out.vwv, VWV(11), 0); /* reserved */
153 /* Create a token value and add it to the outgoing packet. */
154 if (req->smb_conn->negotiate.encrypted_passwords) {
157 SSVAL(req->out.vwv, VWV(11), 8);
159 nt_status = get_challenge(req->smb_conn, req->out.data);
160 if (!NT_STATUS_IS_OK(nt_status)) {
161 smbsrv_terminate_connection(req->smb_conn, "LANMAN1 get_challenge failed\n");
166 if (req->smb_conn->signing.mandatory_signing) {
167 smbsrv_terminate_connection(req->smb_conn,
168 "LANMAN1 does not support SMB signing, and it is mandatory\n");
175 /****************************************************************************
176 Reply for the lanman 2.0 protocol.
177 ****************************************************************************/
178 static void reply_lanman2(struct smbsrv_request *req, uint16_t choice)
180 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
182 time_t t = req->request_time.tv_sec;
184 req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
186 if (lp_security() != SEC_SHARE)
187 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
189 if (req->smb_conn->negotiate.encrypted_passwords)
190 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
192 req->smb_conn->negotiate.protocol = PROTOCOL_LANMAN2;
194 req_setup_reply(req, 13, 0);
196 SSVAL(req->out.vwv, VWV(0), choice);
197 SSVAL(req->out.vwv, VWV(1), secword);
198 SSVAL(req->out.vwv, VWV(2), req->smb_conn->negotiate.max_recv);
199 SSVAL(req->out.vwv, VWV(3), lp_maxmux());
200 SSVAL(req->out.vwv, VWV(4), 1);
201 SSVAL(req->out.vwv, VWV(5), raw);
202 SIVAL(req->out.vwv, VWV(6), req->smb_conn->connection->server_id);
203 srv_push_dos_date(req->smb_conn, req->out.vwv, VWV(8), t);
204 SSVAL(req->out.vwv, VWV(10), req->smb_conn->negotiate.zone_offset/60);
205 SIVAL(req->out.vwv, VWV(11), 0);
207 /* Create a token value and add it to the outgoing packet. */
208 if (req->smb_conn->negotiate.encrypted_passwords) {
209 SSVAL(req->out.vwv, VWV(11), 8);
210 req_grow_data(req, 8);
211 get_challenge(req->smb_conn, req->out.data);
214 req_push_str(req, NULL, lp_workgroup(), -1, STR_TERMINATE);
216 if (req->smb_conn->signing.mandatory_signing) {
217 smbsrv_terminate_connection(req->smb_conn,
218 "LANMAN2 does not support SMB signing, and it is mandatory\n");
225 /****************************************************************************
226 Reply for the nt protocol.
227 ****************************************************************************/
228 static void reply_nt1(struct smbsrv_request *req, uint16_t choice)
230 /* dual names + lock_and_read + nt SMBs + remote API calls */
233 time_t t = req->request_time.tv_sec;
235 BOOL negotiate_spnego = False;
237 unix_to_nt_time(&nttime, t);
240 CAP_NT_FIND | CAP_LOCK_AND_READ |
241 CAP_LEVEL_II_OPLOCKS | CAP_NT_SMBS | CAP_RPC_REMOTE_APIS;
243 req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
245 /* do spnego in user level security if the client
246 supports it and we can do encrypted passwords */
248 if (req->smb_conn->negotiate.encrypted_passwords &&
249 (lp_security() != SEC_SHARE) &&
251 (req->flags2 & FLAGS2_EXTENDED_SECURITY)) {
252 negotiate_spnego = True;
253 capabilities |= CAP_EXTENDED_SECURITY;
256 if (lp_unix_extensions()) {
257 capabilities |= CAP_UNIX;
260 if (lp_large_readwrite()) {
261 capabilities |= CAP_LARGE_READX | CAP_LARGE_WRITEX | CAP_W2K_SMBS;
264 capabilities |= CAP_LARGE_FILES;
266 if (lp_readraw() && lp_writeraw()) {
267 capabilities |= CAP_RAW_MODE;
270 /* allow for disabling unicode */
272 capabilities |= CAP_UNICODE;
275 if (lp_nt_status_support()) {
276 capabilities |= CAP_STATUS32;
279 if (lp_host_msdfs()) {
280 capabilities |= CAP_DFS;
283 if (lp_security() != SEC_SHARE) {
284 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
287 if (req->smb_conn->negotiate.encrypted_passwords) {
288 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
291 if (req->smb_conn->signing.allow_smb_signing) {
292 secword |= NEGOTIATE_SECURITY_SIGNATURES_ENABLED;
295 if (req->smb_conn->signing.mandatory_signing) {
296 secword |= NEGOTIATE_SECURITY_SIGNATURES_REQUIRED;
299 req->smb_conn->negotiate.protocol = PROTOCOL_NT1;
301 req_setup_reply(req, 17, 0);
303 SSVAL(req->out.vwv, VWV(0), choice);
304 SCVAL(req->out.vwv, VWV(1), secword);
306 /* notice the strange +1 on vwv here? That's because
307 this is the one and only SMB packet that is malformed in
308 the specification - all the command words after the secword
309 are offset by 1 byte */
310 SSVAL(req->out.vwv+1, VWV(1), lp_maxmux());
311 SSVAL(req->out.vwv+1, VWV(2), 1); /* num vcs */
312 SIVAL(req->out.vwv+1, VWV(3), req->smb_conn->negotiate.max_recv);
313 SIVAL(req->out.vwv+1, VWV(5), 0x10000); /* raw size. full 64k */
314 SIVAL(req->out.vwv+1, VWV(7), req->smb_conn->connection->server_id); /* session key */
315 SIVAL(req->out.vwv+1, VWV(9), capabilities);
316 push_nttime(req->out.vwv+1, VWV(11), nttime);
317 SSVALS(req->out.vwv+1,VWV(15), req->smb_conn->negotiate.zone_offset/60);
319 if (!negotiate_spnego) {
320 /* Create a token value and add it to the outgoing packet. */
321 if (req->smb_conn->negotiate.encrypted_passwords) {
322 req_grow_data(req, 8);
323 /* note that we do not send a challenge at all if
324 we are using plaintext */
325 get_challenge(req->smb_conn, req->out.ptr);
327 SCVAL(req->out.vwv+1, VWV(16), 8);
329 req_push_str(req, NULL, lp_workgroup(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
330 req_push_str(req, NULL, lp_netbios_name(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
331 DEBUG(3,("not using SPNEGO\n"));
333 struct gensec_security *gensec_security;
334 DATA_BLOB null_data_blob = data_blob(NULL, 0);
336 NTSTATUS nt_status = gensec_server_start(req->smb_conn, &gensec_security);
338 if (req->smb_conn->negotiate.auth_context) {
339 smbsrv_terminate_connection(req->smb_conn, "reply_nt1: is this a secondary negprot? auth_context is non-NULL!\n");
343 req->smb_conn->negotiate.auth_context = NULL;
345 if (!NT_STATUS_IS_OK(nt_status)) {
346 DEBUG(0, ("Failed to start GENSEC: %s\n", nt_errstr(nt_status)));
347 smbsrv_terminate_connection(req->smb_conn, "Failed to start GENSEC\n");
351 nt_status = gensec_start_mech_by_oid(gensec_security, GENSEC_OID_SPNEGO);
353 if (!NT_STATUS_IS_OK(nt_status)) {
354 DEBUG(0, ("Failed to start SPNEGO: %s\n", nt_errstr(nt_status)));
355 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO\n");
359 nt_status = gensec_update(gensec_security, req, null_data_blob, &blob);
361 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
362 DEBUG(0, ("Failed to get SPNEGO to give us the first token: %s\n", nt_errstr(nt_status)));
363 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO - no first token\n");
367 req->smb_conn->negotiate.spnego_negotiated = True;
369 req_grow_data(req, blob.length + 16);
370 /* a NOT very random guid */
371 memset(req->out.ptr, '\0', 16);
374 memcpy(req->out.ptr, blob.data, blob.length);
375 SCVAL(req->out.vwv+1, VWV(16), blob.length + 16);
376 req->out.ptr += blob.length;
377 DEBUG(3,("using SPNEGO\n"));
380 req_send_reply_nosign(req);
384 /* List of supported protocols, most desired first */
385 static const struct {
386 const char *proto_name;
387 const char *short_name;
388 void (*proto_reply_fn)(struct smbsrv_request *req, uint16_t choice);
390 } supported_protocols[] = {
391 {"NT LANMAN 1.0", "NT1", reply_nt1, PROTOCOL_NT1},
392 {"NT LM 0.12", "NT1", reply_nt1, PROTOCOL_NT1},
393 {"LANMAN2.1", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
394 {"LM1.2X002", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
395 {"Samba", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
396 {"DOS LM1.2X002", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
397 {"LANMAN1.0", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
398 {"MICROSOFT NETWORKS 3.0", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
399 {"MICROSOFT NETWORKS 1.03", "COREPLUS", reply_coreplus, PROTOCOL_COREPLUS},
400 {"PC NETWORK PROGRAM 1.0", "CORE", reply_corep, PROTOCOL_CORE},
404 /****************************************************************************
406 ****************************************************************************/
408 void reply_negprot(struct smbsrv_request *req)
415 if (req->smb_conn->negotiate.done_negprot) {
416 smbsrv_terminate_connection(req->smb_conn, "multiple negprot's are not permitted");
419 req->smb_conn->negotiate.done_negprot = True;
421 p = req->in.data + 1;
423 while (p < req->in.data + req->in.data_size) {
425 DEBUG(3,("Requested protocol [%s]\n",(const char *)p));
426 p += strlen((const char *)p) + 2;
429 /* Check for protocols, most desirable first */
430 for (protocol = 0; supported_protocols[protocol].proto_name; protocol++) {
433 if ((supported_protocols[protocol].protocol_level <= lp_maxprotocol()) &&
434 (supported_protocols[protocol].protocol_level >= lp_minprotocol()))
435 while (p < (req->in.data + req->in.data_size)) {
436 if (strequal((const char *)p,supported_protocols[protocol].proto_name))
439 p += strlen((const char *)p) + 2;
446 sub_set_remote_proto(supported_protocols[protocol].short_name);
447 supported_protocols[protocol].proto_reply_fn(req, choice);
448 DEBUG(3,("Selected protocol %s\n",supported_protocols[protocol].proto_name));
450 DEBUG(0,("No protocol supported !\n"));
453 DEBUG(5,("negprot index=%d\n", choice));