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"
24 #include "smb_server/smb_server.h"
27 /* initialise the auth_context for this server and return the cryptkey */
28 static void get_challenge(struct smbsrv_connection *smb_conn, char buff[8])
31 const uint8_t *cryptkey;
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 smb_panic("secondary negprot");
39 DEBUG(10, ("get challenge: creating negprot_global_auth_context\n"));
41 nt_status = make_auth_context_subsystem(smb_conn, &smb_conn->negotiate.auth_context);
43 if (!NT_STATUS_IS_OK(nt_status)) {
44 DEBUG(0, ("make_auth_context_subsystem returned %s", nt_errstr(nt_status)));
45 smb_panic("cannot make_negprot_global_auth_context!\n");
48 DEBUG(10, ("get challenge: getting challenge\n"));
49 cryptkey = smb_conn->negotiate.auth_context->get_ntlm_challenge(smb_conn->negotiate.auth_context);
50 memcpy(buff, cryptkey, 8);
53 /****************************************************************************
54 Reply for the core protocol.
55 ****************************************************************************/
56 static void reply_corep(struct smbsrv_request *req, uint16_t choice)
58 req_setup_reply(req, 1, 0);
60 SSVAL(req->out.vwv, VWV(0), choice);
62 req->smb_conn->negotiate.protocol = PROTOCOL_CORE;
64 if (req->smb_conn->signing.mandatory_signing) {
65 smbsrv_terminate_connection(req->smb_conn,
66 "CORE does not support SMB signing, and it is mandetory\n");
72 /****************************************************************************
73 Reply for the coreplus protocol.
74 this is quite incomplete - we only fill in a small part of the reply, but as nobody uses
75 this any more it probably doesn't matter
76 ****************************************************************************/
77 static void reply_coreplus(struct smbsrv_request *req, uint16_t choice)
79 uint16_t raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
81 req_setup_reply(req, 13, 0);
83 /* Reply, SMBlockread, SMBwritelock supported. */
84 SCVAL(req->out.hdr,HDR_FLG,
85 CVAL(req->out.hdr, HDR_FLG) | FLAG_SUPPORT_LOCKREAD);
87 SSVAL(req->out.vwv, VWV(0), choice);
88 SSVAL(req->out.vwv, VWV(1), 0x1); /* user level security, don't encrypt */
90 /* tell redirector we support
91 readbraw and writebraw (possibly) */
92 SSVAL(req->out.vwv, VWV(5), raw);
94 req->smb_conn->negotiate.protocol = PROTOCOL_COREPLUS;
96 if (req->smb_conn->signing.mandatory_signing) {
97 smbsrv_terminate_connection(req->smb_conn,
98 "COREPLUS does not support SMB signing, and it is mandetory\n");
104 /****************************************************************************
105 Reply for the lanman 1.0 protocol.
106 ****************************************************************************/
107 static void reply_lanman1(struct smbsrv_request *req, uint16_t choice)
109 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
111 time_t t = req->request_time.tv_sec;
113 req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
115 if (lp_security() != SEC_SHARE)
116 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
118 if (req->smb_conn->negotiate.encrypted_passwords)
119 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
121 req->smb_conn->negotiate.protocol = PROTOCOL_LANMAN1;
123 req_setup_reply(req, 13, req->smb_conn->negotiate.encrypted_passwords ? 8 : 0);
125 /* SMBlockread, SMBwritelock supported. */
126 SCVAL(req->out.hdr,HDR_FLG,
127 CVAL(req->out.hdr, HDR_FLG) | FLAG_SUPPORT_LOCKREAD);
129 SSVAL(req->out.vwv, VWV(0), choice);
130 SSVAL(req->out.vwv, VWV(1), secword);
131 SSVAL(req->out.vwv, VWV(2), req->smb_conn->negotiate.max_recv);
132 SSVAL(req->out.vwv, VWV(3), lp_maxmux());
133 SSVAL(req->out.vwv, VWV(4), 1);
134 SSVAL(req->out.vwv, VWV(5), raw);
135 SIVAL(req->out.vwv, VWV(6), req->smb_conn->pid);
136 srv_push_dos_date(req->smb_conn, req->out.vwv, VWV(8), t);
137 SSVAL(req->out.vwv, VWV(10), req->smb_conn->negotiate.zone_offset/60);
138 SIVAL(req->out.vwv, VWV(11), 0); /* reserved */
140 /* Create a token value and add it to the outgoing packet. */
141 if (req->smb_conn->negotiate.encrypted_passwords) {
142 SSVAL(req->out.vwv, VWV(11), 8);
143 get_challenge(req->smb_conn, req->out.data);
146 if (req->smb_conn->signing.mandatory_signing) {
147 smbsrv_terminate_connection(req->smb_conn,
148 "LANMAN1 does not support SMB signing, and it is mandetory\n");
154 /****************************************************************************
155 Reply for the lanman 2.0 protocol.
156 ****************************************************************************/
157 static void reply_lanman2(struct smbsrv_request *req, uint16_t choice)
159 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
161 time_t t = req->request_time.tv_sec;
163 req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
165 if (lp_security() != SEC_SHARE)
166 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
168 if (req->smb_conn->negotiate.encrypted_passwords)
169 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
171 req->smb_conn->negotiate.protocol = PROTOCOL_LANMAN2;
173 req_setup_reply(req, 13, 0);
175 SSVAL(req->out.vwv, VWV(0), choice);
176 SSVAL(req->out.vwv, VWV(1), secword);
177 SSVAL(req->out.vwv, VWV(2), req->smb_conn->negotiate.max_recv);
178 SSVAL(req->out.vwv, VWV(3), lp_maxmux());
179 SSVAL(req->out.vwv, VWV(4), 1);
180 SSVAL(req->out.vwv, VWV(5), raw);
181 SIVAL(req->out.vwv, VWV(6), req->smb_conn->pid);
182 srv_push_dos_date(req->smb_conn, req->out.vwv, VWV(8), t);
183 SSVAL(req->out.vwv, VWV(10), req->smb_conn->negotiate.zone_offset/60);
185 /* Create a token value and add it to the outgoing packet. */
186 if (req->smb_conn->negotiate.encrypted_passwords) {
187 SSVAL(req->out.vwv, VWV(11), 8);
188 req_grow_data(req, 8);
189 get_challenge(req->smb_conn, req->out.data);
192 req_push_str(req, NULL, lp_workgroup(), -1, STR_TERMINATE);
194 if (req->smb_conn->signing.mandatory_signing) {
195 smbsrv_terminate_connection(req->smb_conn,
196 "LANMAN2 does not support SMB signing, and it is mandetory\n");
202 /****************************************************************************
203 Reply for the nt protocol.
204 ****************************************************************************/
205 static void reply_nt1(struct smbsrv_request *req, uint16_t choice)
207 /* dual names + lock_and_read + nt SMBs + remote API calls */
210 time_t t = req->request_time.tv_sec;
212 BOOL negotiate_spnego = False;
214 unix_to_nt_time(&nttime, t);
217 CAP_NT_FIND | CAP_LOCK_AND_READ |
218 CAP_LEVEL_II_OPLOCKS | CAP_NT_SMBS | CAP_RPC_REMOTE_APIS;
220 req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
222 /* do spnego in user level security if the client
223 supports it and we can do encrypted passwords */
225 if (req->smb_conn->negotiate.encrypted_passwords &&
226 (lp_security() != SEC_SHARE) &&
228 (req->flags2 & FLAGS2_EXTENDED_SECURITY)) {
229 negotiate_spnego = True;
230 capabilities |= CAP_EXTENDED_SECURITY;
233 if (lp_unix_extensions()) {
234 capabilities |= CAP_UNIX;
237 if (lp_large_readwrite()) {
238 capabilities |= CAP_LARGE_READX | CAP_LARGE_WRITEX | CAP_W2K_SMBS;
241 capabilities |= CAP_LARGE_FILES;
243 if (lp_readraw() && lp_writeraw()) {
244 capabilities |= CAP_RAW_MODE;
247 /* allow for disabling unicode */
249 capabilities |= CAP_UNICODE;
252 if (lp_nt_status_support()) {
253 capabilities |= CAP_STATUS32;
256 if (lp_host_msdfs()) {
257 capabilities |= CAP_DFS;
260 if (lp_security() != SEC_SHARE) {
261 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
264 if (req->smb_conn->negotiate.encrypted_passwords) {
265 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
268 if (req->smb_conn->signing.allow_smb_signing) {
269 secword |= NEGOTIATE_SECURITY_SIGNATURES_ENABLED;
272 if (req->smb_conn->signing.mandatory_signing) {
273 secword |= NEGOTIATE_SECURITY_SIGNATURES_REQUIRED;
276 req->smb_conn->negotiate.protocol = PROTOCOL_NT1;
278 req_setup_reply(req, 17, 0);
280 SSVAL(req->out.vwv, VWV(0), choice);
281 SCVAL(req->out.vwv, VWV(1), secword);
283 /* notice the strange +1 on vwv here? That's because
284 this is the one and only SMB packet that is malformed in
285 the specification - all the command words after the secword
286 are offset by 1 byte */
287 SSVAL(req->out.vwv+1, VWV(1), lp_maxmux());
288 SSVAL(req->out.vwv+1, VWV(2), 1); /* num vcs */
289 SIVAL(req->out.vwv+1, VWV(3), req->smb_conn->negotiate.max_recv);
290 SIVAL(req->out.vwv+1, VWV(5), 0x10000); /* raw size. full 64k */
291 SIVAL(req->out.vwv+1, VWV(7), req->smb_conn->pid); /* session key */
292 SIVAL(req->out.vwv+1, VWV(9), capabilities);
293 push_nttime(req->out.vwv+1, VWV(11), nttime);
294 SSVALS(req->out.vwv+1,VWV(15), req->smb_conn->negotiate.zone_offset/60);
296 if (!negotiate_spnego) {
297 /* Create a token value and add it to the outgoing packet. */
298 if (req->smb_conn->negotiate.encrypted_passwords) {
299 req_grow_data(req, 8);
300 /* note that we do not send a challenge at all if
301 we are using plaintext */
302 get_challenge(req->smb_conn, req->out.ptr);
304 SCVAL(req->out.vwv+1, VWV(16), 8);
306 req_push_str(req, NULL, lp_workgroup(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
307 req_push_str(req, NULL, lp_netbios_name(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
308 DEBUG(3,("not using SPNEGO\n"));
310 struct gensec_security *gensec_security;
311 DATA_BLOB null_data_blob = data_blob(NULL, 0);
313 NTSTATUS nt_status = gensec_server_start(req->smb_conn, &gensec_security);
315 if (req->smb_conn->negotiate.auth_context) {
316 smbsrv_terminate_connection(req->smb_conn, "reply_nt1: is this a secondary negprot? auth_context is non-NULL!\n");
320 req->smb_conn->negotiate.auth_context = NULL;
322 if (!NT_STATUS_IS_OK(nt_status)) {
323 DEBUG(0, ("Failed to start GENSEC: %s\n", nt_errstr(nt_status)));
324 smbsrv_terminate_connection(req->smb_conn, "Failed to start GENSEC\n");
328 nt_status = gensec_start_mech_by_oid(gensec_security, OID_SPNEGO);
330 if (!NT_STATUS_IS_OK(nt_status)) {
331 DEBUG(0, ("Failed to start SPNEGO: %s\n", nt_errstr(nt_status)));
332 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO\n");
336 nt_status = gensec_update(gensec_security, req, null_data_blob, &blob);
338 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
339 DEBUG(0, ("Failed to get SPNEGO to give us the first token: %s\n", nt_errstr(nt_status)));
340 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO - no first token\n");
344 req->smb_conn->negotiate.spnego_negotiated = True;
346 req_grow_data(req, blob.length + 16);
347 /* a NOT very random guid */
348 memset(req->out.ptr, '\0', 16);
351 memcpy(req->out.ptr, blob.data, blob.length);
352 SCVAL(req->out.vwv+1, VWV(16), blob.length + 16);
353 req->out.ptr += blob.length;
354 DEBUG(3,("using SPNEGO\n"));
357 req_send_reply_nosign(req);
361 /* List of supported protocols, most desired first */
362 static const struct {
363 const char *proto_name;
364 const char *short_name;
365 void (*proto_reply_fn)(struct smbsrv_request *req, uint16_t choice);
367 } supported_protocols[] = {
368 {"NT LANMAN 1.0", "NT1", reply_nt1, PROTOCOL_NT1},
369 {"NT LM 0.12", "NT1", reply_nt1, PROTOCOL_NT1},
370 {"LM1.2X002", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
371 {"Samba", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
372 {"DOS LM1.2X002", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
373 {"LANMAN1.0", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
374 {"MICROSOFT NETWORKS 3.0", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
375 {"MICROSOFT NETWORKS 1.03", "COREPLUS", reply_coreplus, PROTOCOL_COREPLUS},
376 {"PC NETWORK PROGRAM 1.0", "CORE", reply_corep, PROTOCOL_CORE},
380 /****************************************************************************
382 ****************************************************************************/
384 void reply_negprot(struct smbsrv_request *req)
391 if (req->smb_conn->negotiate.done_negprot) {
392 smbsrv_terminate_connection(req->smb_conn, "multiple negprot's are not permitted");
395 req->smb_conn->negotiate.done_negprot = True;
397 p = req->in.data + 1;
399 while (p < req->in.data + req->in.data_size) {
401 DEBUG(3,("Requested protocol [%s]\n",p));
405 /* Check for protocols, most desirable first */
406 for (protocol = 0; supported_protocols[protocol].proto_name; protocol++) {
409 if ((supported_protocols[protocol].protocol_level <= lp_maxprotocol()) &&
410 (supported_protocols[protocol].protocol_level >= lp_minprotocol()))
411 while (p < (req->in.data + req->in.data_size)) {
412 if (strequal(p,supported_protocols[protocol].proto_name))
422 sub_set_remote_proto(supported_protocols[protocol].short_name);
423 reload_services(req->smb_conn, True);
424 supported_protocols[protocol].proto_reply_fn(req, choice);
425 DEBUG(3,("Selected protocol %s\n",supported_protocols[protocol].proto_name));
427 DEBUG(0,("No protocol supported !\n"));
430 DEBUG(5,("negprot index=%d\n", choice));