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"
26 /* initialise the auth_context for this server and return the cryptkey */
27 static NTSTATUS get_challenge(struct smbsrv_connection *smb_conn, uint8_t buff[8])
30 const uint8_t *challenge;
32 /* muliple negprots are not premitted */
33 if (smb_conn->negotiate.auth_context) {
34 DEBUG(3,("get challenge: is this a secondary negprot? auth_context is non-NULL!\n"));
35 return NT_STATUS_FOOBAR;
38 DEBUG(10, ("get challenge: creating negprot_global_auth_context\n"));
40 nt_status = auth_context_create(smb_conn, lp_auth_methods(), &smb_conn->negotiate.auth_context);
41 if (!NT_STATUS_IS_OK(nt_status)) {
42 DEBUG(0, ("auth_context_create() returned %s", nt_errstr(nt_status)));
46 nt_status = auth_context_create(smb_conn, lp_auth_methods(), &smb_conn->negotiate.auth_context);
47 if (!NT_STATUS_IS_OK(nt_status)) {
48 DEBUG(0, ("auth_context_create() returned %s", nt_errstr(nt_status)));
52 nt_status = auth_get_challenge(smb_conn->negotiate.auth_context, &challenge);
53 if (!NT_STATUS_IS_OK(nt_status)) {
54 DEBUG(0, ("auth_get_challenge() returned %s", nt_errstr(nt_status)));
58 memcpy(buff, challenge, 8);
63 /****************************************************************************
64 Reply for the core protocol.
65 ****************************************************************************/
66 static void reply_corep(struct smbsrv_request *req, uint16_t choice)
68 req_setup_reply(req, 1, 0);
70 SSVAL(req->out.vwv, VWV(0), choice);
72 req->smb_conn->negotiate.protocol = PROTOCOL_CORE;
74 if (req->smb_conn->signing.mandatory_signing) {
75 smbsrv_terminate_connection(req->smb_conn,
76 "CORE does not support SMB signing, and it is mandatory\n");
83 /****************************************************************************
84 Reply for the coreplus protocol.
85 this is quite incomplete - we only fill in a small part of the reply, but as nobody uses
86 this any more it probably doesn't matter
87 ****************************************************************************/
88 static void reply_coreplus(struct smbsrv_request *req, uint16_t choice)
90 uint16_t raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
92 req_setup_reply(req, 13, 0);
94 /* Reply, SMBlockread, SMBwritelock supported. */
95 SCVAL(req->out.hdr,HDR_FLG,
96 CVAL(req->out.hdr, HDR_FLG) | FLAG_SUPPORT_LOCKREAD);
98 SSVAL(req->out.vwv, VWV(0), choice);
99 SSVAL(req->out.vwv, VWV(1), 0x1); /* user level security, don't encrypt */
101 /* tell redirector we support
102 readbraw and writebraw (possibly) */
103 SSVAL(req->out.vwv, VWV(5), raw);
105 req->smb_conn->negotiate.protocol = PROTOCOL_COREPLUS;
107 if (req->smb_conn->signing.mandatory_signing) {
108 smbsrv_terminate_connection(req->smb_conn,
109 "COREPLUS does not support SMB signing, and it is mandatory\n");
116 /****************************************************************************
117 Reply for the lanman 1.0 protocol.
118 ****************************************************************************/
119 static void reply_lanman1(struct smbsrv_request *req, uint16_t choice)
121 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
123 time_t t = req->request_time.tv_sec;
125 req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
127 if (lp_security() != SEC_SHARE)
128 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
130 if (req->smb_conn->negotiate.encrypted_passwords)
131 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
133 req->smb_conn->negotiate.protocol = PROTOCOL_LANMAN1;
135 req_setup_reply(req, 13, req->smb_conn->negotiate.encrypted_passwords ? 8 : 0);
137 /* SMBlockread, SMBwritelock supported. */
138 SCVAL(req->out.hdr,HDR_FLG,
139 CVAL(req->out.hdr, HDR_FLG) | FLAG_SUPPORT_LOCKREAD);
141 SSVAL(req->out.vwv, VWV(0), choice);
142 SSVAL(req->out.vwv, VWV(1), secword);
143 SSVAL(req->out.vwv, VWV(2), req->smb_conn->negotiate.max_recv);
144 SSVAL(req->out.vwv, VWV(3), lp_maxmux());
145 SSVAL(req->out.vwv, VWV(4), 1);
146 SSVAL(req->out.vwv, VWV(5), raw);
147 SIVAL(req->out.vwv, VWV(6), req->smb_conn->connection->connection.id);
148 srv_push_dos_date(req->smb_conn, req->out.vwv, VWV(8), t);
149 SSVAL(req->out.vwv, VWV(10), req->smb_conn->negotiate.zone_offset/60);
150 SIVAL(req->out.vwv, VWV(11), 0); /* reserved */
152 /* Create a token value and add it to the outgoing packet. */
153 if (req->smb_conn->negotiate.encrypted_passwords) {
156 SSVAL(req->out.vwv, VWV(11), 8);
158 nt_status = get_challenge(req->smb_conn, req->out.data);
159 if (!NT_STATUS_IS_OK(nt_status)) {
160 smbsrv_terminate_connection(req->smb_conn, "LANMAN1 get_challenge failed\n");
165 if (req->smb_conn->signing.mandatory_signing) {
166 smbsrv_terminate_connection(req->smb_conn,
167 "LANMAN1 does not support SMB signing, and it is mandatory\n");
174 /****************************************************************************
175 Reply for the lanman 2.0 protocol.
176 ****************************************************************************/
177 static void reply_lanman2(struct smbsrv_request *req, uint16_t choice)
179 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
181 time_t t = req->request_time.tv_sec;
183 req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
185 if (lp_security() != SEC_SHARE)
186 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
188 if (req->smb_conn->negotiate.encrypted_passwords)
189 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
191 req->smb_conn->negotiate.protocol = PROTOCOL_LANMAN2;
193 req_setup_reply(req, 13, 0);
195 SSVAL(req->out.vwv, VWV(0), choice);
196 SSVAL(req->out.vwv, VWV(1), secword);
197 SSVAL(req->out.vwv, VWV(2), req->smb_conn->negotiate.max_recv);
198 SSVAL(req->out.vwv, VWV(3), lp_maxmux());
199 SSVAL(req->out.vwv, VWV(4), 1);
200 SSVAL(req->out.vwv, VWV(5), raw);
201 SIVAL(req->out.vwv, VWV(6), req->smb_conn->connection->connection.id);
202 srv_push_dos_date(req->smb_conn, req->out.vwv, VWV(8), t);
203 SSVAL(req->out.vwv, VWV(10), req->smb_conn->negotiate.zone_offset/60);
204 SIVAL(req->out.vwv, VWV(11), 0);
206 /* Create a token value and add it to the outgoing packet. */
207 if (req->smb_conn->negotiate.encrypted_passwords) {
208 SSVAL(req->out.vwv, VWV(11), 8);
209 req_grow_data(req, 8);
210 get_challenge(req->smb_conn, req->out.data);
213 req_push_str(req, NULL, lp_workgroup(), -1, STR_TERMINATE);
215 if (req->smb_conn->signing.mandatory_signing) {
216 smbsrv_terminate_connection(req->smb_conn,
217 "LANMAN2 does not support SMB signing, and it is mandatory\n");
224 /****************************************************************************
225 Reply for the nt protocol.
226 ****************************************************************************/
227 static void reply_nt1(struct smbsrv_request *req, uint16_t choice)
229 /* dual names + lock_and_read + nt SMBs + remote API calls */
232 time_t t = req->request_time.tv_sec;
234 BOOL negotiate_spnego = False;
236 unix_to_nt_time(&nttime, t);
239 CAP_NT_FIND | CAP_LOCK_AND_READ |
240 CAP_LEVEL_II_OPLOCKS | CAP_NT_SMBS | CAP_RPC_REMOTE_APIS;
242 req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
244 /* do spnego in user level security if the client
245 supports it and we can do encrypted passwords */
247 if (req->smb_conn->negotiate.encrypted_passwords &&
248 (lp_security() != SEC_SHARE) &&
250 (req->flags2 & FLAGS2_EXTENDED_SECURITY)) {
251 negotiate_spnego = True;
252 capabilities |= CAP_EXTENDED_SECURITY;
255 if (lp_unix_extensions()) {
256 capabilities |= CAP_UNIX;
259 if (lp_large_readwrite()) {
260 capabilities |= CAP_LARGE_READX | CAP_LARGE_WRITEX | CAP_W2K_SMBS;
263 capabilities |= CAP_LARGE_FILES;
265 if (lp_readraw() && lp_writeraw()) {
266 capabilities |= CAP_RAW_MODE;
269 /* allow for disabling unicode */
271 capabilities |= CAP_UNICODE;
274 if (lp_nt_status_support()) {
275 capabilities |= CAP_STATUS32;
278 if (lp_host_msdfs()) {
279 capabilities |= CAP_DFS;
282 if (lp_security() != SEC_SHARE) {
283 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
286 if (req->smb_conn->negotiate.encrypted_passwords) {
287 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
290 if (req->smb_conn->signing.allow_smb_signing) {
291 secword |= NEGOTIATE_SECURITY_SIGNATURES_ENABLED;
294 if (req->smb_conn->signing.mandatory_signing) {
295 secword |= NEGOTIATE_SECURITY_SIGNATURES_REQUIRED;
298 req->smb_conn->negotiate.protocol = PROTOCOL_NT1;
300 req_setup_reply(req, 17, 0);
302 SSVAL(req->out.vwv, VWV(0), choice);
303 SCVAL(req->out.vwv, VWV(1), secword);
305 /* notice the strange +1 on vwv here? That's because
306 this is the one and only SMB packet that is malformed in
307 the specification - all the command words after the secword
308 are offset by 1 byte */
309 SSVAL(req->out.vwv+1, VWV(1), lp_maxmux());
310 SSVAL(req->out.vwv+1, VWV(2), 1); /* num vcs */
311 SIVAL(req->out.vwv+1, VWV(3), req->smb_conn->negotiate.max_recv);
312 SIVAL(req->out.vwv+1, VWV(5), 0x10000); /* raw size. full 64k */
313 SIVAL(req->out.vwv+1, VWV(7), req->smb_conn->connection->connection.id); /* session key */
314 SIVAL(req->out.vwv+1, VWV(9), capabilities);
315 push_nttime(req->out.vwv+1, VWV(11), nttime);
316 SSVALS(req->out.vwv+1,VWV(15), req->smb_conn->negotiate.zone_offset/60);
318 if (!negotiate_spnego) {
319 /* Create a token value and add it to the outgoing packet. */
320 if (req->smb_conn->negotiate.encrypted_passwords) {
321 req_grow_data(req, 8);
322 /* note that we do not send a challenge at all if
323 we are using plaintext */
324 get_challenge(req->smb_conn, req->out.ptr);
326 SCVAL(req->out.vwv+1, VWV(16), 8);
328 req_push_str(req, NULL, lp_workgroup(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
329 req_push_str(req, NULL, lp_netbios_name(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
330 DEBUG(3,("not using SPNEGO\n"));
332 struct gensec_security *gensec_security;
333 DATA_BLOB null_data_blob = data_blob(NULL, 0);
335 NTSTATUS nt_status = gensec_server_start(req->smb_conn, &gensec_security);
337 if (req->smb_conn->negotiate.auth_context) {
338 smbsrv_terminate_connection(req->smb_conn, "reply_nt1: is this a secondary negprot? auth_context is non-NULL!\n");
342 req->smb_conn->negotiate.auth_context = NULL;
344 if (!NT_STATUS_IS_OK(nt_status)) {
345 DEBUG(0, ("Failed to start GENSEC: %s\n", nt_errstr(nt_status)));
346 smbsrv_terminate_connection(req->smb_conn, "Failed to start GENSEC\n");
350 nt_status = gensec_start_mech_by_oid(gensec_security, GENSEC_OID_SPNEGO);
352 if (!NT_STATUS_IS_OK(nt_status)) {
353 DEBUG(0, ("Failed to start SPNEGO: %s\n", nt_errstr(nt_status)));
354 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO\n");
358 nt_status = gensec_update(gensec_security, req, null_data_blob, &blob);
360 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
361 DEBUG(0, ("Failed to get SPNEGO to give us the first token: %s\n", nt_errstr(nt_status)));
362 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO - no first token\n");
366 req->smb_conn->negotiate.spnego_negotiated = True;
368 req_grow_data(req, blob.length + 16);
369 /* a NOT very random guid */
370 memset(req->out.ptr, '\0', 16);
373 memcpy(req->out.ptr, blob.data, blob.length);
374 SCVAL(req->out.vwv+1, VWV(16), blob.length + 16);
375 req->out.ptr += blob.length;
376 DEBUG(3,("using SPNEGO\n"));
379 req_send_reply_nosign(req);
383 /* List of supported protocols, most desired first */
384 static const struct {
385 const char *proto_name;
386 const char *short_name;
387 void (*proto_reply_fn)(struct smbsrv_request *req, uint16_t choice);
389 } supported_protocols[] = {
390 {"NT LANMAN 1.0", "NT1", reply_nt1, PROTOCOL_NT1},
391 {"NT LM 0.12", "NT1", reply_nt1, PROTOCOL_NT1},
392 {"LANMAN2.1", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
393 {"LM1.2X002", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
394 {"Samba", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
395 {"DOS LM1.2X002", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
396 {"LANMAN1.0", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
397 {"MICROSOFT NETWORKS 3.0", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
398 {"MICROSOFT NETWORKS 1.03", "COREPLUS", reply_coreplus, PROTOCOL_COREPLUS},
399 {"PC NETWORK PROGRAM 1.0", "CORE", reply_corep, PROTOCOL_CORE},
403 /****************************************************************************
405 ****************************************************************************/
407 void reply_negprot(struct smbsrv_request *req)
414 if (req->smb_conn->negotiate.done_negprot) {
415 smbsrv_terminate_connection(req->smb_conn, "multiple negprot's are not permitted");
418 req->smb_conn->negotiate.done_negprot = True;
420 p = req->in.data + 1;
422 while (p < req->in.data + req->in.data_size) {
424 DEBUG(3,("Requested protocol [%s]\n",(const char *)p));
425 p += strlen((const char *)p) + 2;
428 /* Check for protocols, most desirable first */
429 for (protocol = 0; supported_protocols[protocol].proto_name; protocol++) {
432 if ((supported_protocols[protocol].protocol_level <= lp_maxprotocol()) &&
433 (supported_protocols[protocol].protocol_level >= lp_minprotocol()))
434 while (p < (req->in.data + req->in.data_size)) {
435 if (strequal((const char *)p,supported_protocols[protocol].proto_name))
438 p += strlen((const char *)p) + 2;
445 sub_set_remote_proto(supported_protocols[protocol].short_name);
446 reload_services(req->smb_conn, True);
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));