r16735: vista beta2 reuses the tcp connect where it does the
[kai/samba.git] / source4 / smb_server / smb / negprot.c
1 /* 
2    Unix SMB/CIFS implementation.
3    negprot reply code
4    Copyright (C) Andrew Tridgell 1992-1998
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22 #include "auth/auth.h"
23 #include "smb_server/smb_server.h"
24 #include "libcli/smb2/smb2.h"
25 #include "smb_server/smb2/smb2_server.h"
26 #include "smb_server/service_smb_proto.h"
27 #include "smbd/service_stream.h"
28 #include "lib/stream/packet.h"
29
30
31 /* initialise the auth_context for this server and return the cryptkey */
32 static NTSTATUS get_challenge(struct smbsrv_connection *smb_conn, uint8_t buff[8]) 
33 {
34         NTSTATUS nt_status;
35         const uint8_t *challenge;
36
37         /* muliple negprots are not premitted */
38         if (smb_conn->negotiate.auth_context) {
39                 DEBUG(3,("get challenge: is this a secondary negprot?  auth_context is non-NULL!\n"));
40                 return NT_STATUS_FOOBAR;
41         }
42
43         DEBUG(10, ("get challenge: creating negprot_global_auth_context\n"));
44
45         nt_status = auth_context_create(smb_conn, lp_auth_methods(), 
46                                         &smb_conn->negotiate.auth_context,
47                                         smb_conn->connection->event.ctx);
48         if (!NT_STATUS_IS_OK(nt_status)) {
49                 DEBUG(0, ("auth_context_create() returned %s", nt_errstr(nt_status)));
50                 return nt_status;
51         }
52
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)));
56                 return nt_status;
57         }
58
59         memcpy(buff, challenge, 8);
60
61         return NT_STATUS_OK;
62 }
63
64 /****************************************************************************
65  Reply for the core protocol.
66 ****************************************************************************/
67 static void reply_corep(struct smbsrv_request *req, uint16_t choice)
68 {
69         smbsrv_setup_reply(req, 1, 0);
70
71         SSVAL(req->out.vwv, VWV(0), choice);
72
73         req->smb_conn->negotiate.protocol = PROTOCOL_CORE;
74
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");
78                 return;
79         }
80
81         smbsrv_send_reply(req);
82 }
83
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)
90 {
91         uint16_t raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
92
93         smbsrv_setup_reply(req, 13, 0);
94
95         /* Reply, SMBlockread, SMBwritelock supported. */
96         SCVAL(req->out.hdr,HDR_FLG,
97               CVAL(req->out.hdr, HDR_FLG) | FLAG_SUPPORT_LOCKREAD);
98
99         SSVAL(req->out.vwv, VWV(0), choice);
100         SSVAL(req->out.vwv, VWV(1), 0x1); /* user level security, don't encrypt */      
101
102         /* tell redirector we support
103            readbraw and writebraw (possibly) */
104         SSVAL(req->out.vwv, VWV(5), raw); 
105
106         req->smb_conn->negotiate.protocol = PROTOCOL_COREPLUS;
107
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");
111                 return;
112         }
113
114         smbsrv_send_reply(req);
115 }
116
117 /****************************************************************************
118  Reply for the lanman 1.0 protocol.
119 ****************************************************************************/
120 static void reply_lanman1(struct smbsrv_request *req, uint16_t choice)
121 {
122         int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
123         int secword=0;
124         time_t t = req->request_time.tv_sec;
125
126         req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
127
128         if (lp_security() != SEC_SHARE)
129                 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
130
131         if (req->smb_conn->negotiate.encrypted_passwords)
132                 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
133
134         req->smb_conn->negotiate.protocol = PROTOCOL_LANMAN1;
135
136         smbsrv_setup_reply(req, 13, req->smb_conn->negotiate.encrypted_passwords ? 8 : 0);
137
138         /* SMBlockread, SMBwritelock supported. */
139         SCVAL(req->out.hdr,HDR_FLG,
140               CVAL(req->out.hdr, HDR_FLG) | FLAG_SUPPORT_LOCKREAD);
141
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 */
152
153         /* Create a token value and add it to the outgoing packet. */
154         if (req->smb_conn->negotiate.encrypted_passwords) {
155                 NTSTATUS nt_status;
156
157                 SSVAL(req->out.vwv, VWV(11), 8);
158
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");
162                         return;
163                 }
164         }
165
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");
169                 return;
170         }
171
172         smbsrv_send_reply(req); 
173 }
174
175 /****************************************************************************
176  Reply for the lanman 2.0 protocol.
177 ****************************************************************************/
178 static void reply_lanman2(struct smbsrv_request *req, uint16_t choice)
179 {
180         int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
181         int secword=0;
182         time_t t = req->request_time.tv_sec;
183
184         req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
185   
186         if (lp_security() != SEC_SHARE)
187                 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
188
189         if (req->smb_conn->negotiate.encrypted_passwords)
190                 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
191
192         req->smb_conn->negotiate.protocol = PROTOCOL_LANMAN2;
193
194         smbsrv_setup_reply(req, 13, 0);
195
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);
206
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);
212         }
213
214         req_push_str(req, NULL, lp_workgroup(), -1, STR_TERMINATE);
215
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");
219                 return;
220         }
221
222         smbsrv_send_reply(req);
223 }
224
225 static void reply_nt1_orig(struct smbsrv_request *req)
226 {
227         /* Create a token value and add it to the outgoing packet. */
228         if (req->smb_conn->negotiate.encrypted_passwords) {
229                 req_grow_data(req, 8);
230                 /* note that we do not send a challenge at all if
231                    we are using plaintext */
232                 get_challenge(req->smb_conn, req->out.ptr);
233                 req->out.ptr += 8;
234                 SCVAL(req->out.vwv+1, VWV(16), 8);
235         }
236         req_push_str(req, NULL, lp_workgroup(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
237         req_push_str(req, NULL, lp_netbios_name(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
238         DEBUG(3,("not using extended security (SPNEGO or NTLMSSP)\n"));
239 }
240
241 /****************************************************************************
242  Reply for the nt protocol.
243 ****************************************************************************/
244 static void reply_nt1(struct smbsrv_request *req, uint16_t choice)
245 {
246         /* dual names + lock_and_read + nt SMBs + remote API calls */
247         int capabilities;
248         int secword=0;
249         time_t t = req->request_time.tv_sec;
250         NTTIME nttime;
251         BOOL negotiate_spnego = False;
252
253         unix_to_nt_time(&nttime, t);
254
255         capabilities = 
256                 CAP_NT_FIND | CAP_LOCK_AND_READ | 
257                 CAP_LEVEL_II_OPLOCKS | CAP_NT_SMBS | CAP_RPC_REMOTE_APIS;
258
259         req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
260
261         /* do spnego in user level security if the client
262            supports it and we can do encrypted passwords */
263         
264         if (req->smb_conn->negotiate.encrypted_passwords && 
265             (lp_security() != SEC_SHARE) &&
266             lp_use_spnego() &&
267             (req->flags2 & FLAGS2_EXTENDED_SECURITY)) {
268                 negotiate_spnego = True; 
269                 capabilities |= CAP_EXTENDED_SECURITY;
270         }
271         
272         if (lp_unix_extensions()) {
273                 capabilities |= CAP_UNIX;
274         }
275         
276         if (lp_large_readwrite()) {
277                 capabilities |= CAP_LARGE_READX | CAP_LARGE_WRITEX | CAP_W2K_SMBS;
278         }
279         
280         capabilities |= CAP_LARGE_FILES;
281
282         if (lp_readraw() && lp_writeraw()) {
283                 capabilities |= CAP_RAW_MODE;
284         }
285         
286         /* allow for disabling unicode */
287         if (lp_unicode()) {
288                 capabilities |= CAP_UNICODE;
289         }
290
291         if (lp_nt_status_support()) {
292                 capabilities |= CAP_STATUS32;
293         }
294         
295         if (lp_host_msdfs()) {
296                 capabilities |= CAP_DFS;
297         }
298         
299         if (lp_security() != SEC_SHARE) {
300                 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
301         }
302
303         if (req->smb_conn->negotiate.encrypted_passwords) {
304                 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
305         }
306
307         if (req->smb_conn->signing.allow_smb_signing) {
308                 secword |= NEGOTIATE_SECURITY_SIGNATURES_ENABLED;
309         }
310
311         if (req->smb_conn->signing.mandatory_signing) {
312                 secword |= NEGOTIATE_SECURITY_SIGNATURES_REQUIRED;
313         }
314         
315         req->smb_conn->negotiate.protocol = PROTOCOL_NT1;
316
317         smbsrv_setup_reply(req, 17, 0);
318         
319         SSVAL(req->out.vwv, VWV(0), choice);
320         SCVAL(req->out.vwv, VWV(1), secword);
321
322         /* notice the strange +1 on vwv here? That's because
323            this is the one and only SMB packet that is malformed in
324            the specification - all the command words after the secword
325            are offset by 1 byte */
326         SSVAL(req->out.vwv+1, VWV(1), lp_maxmux());
327         SSVAL(req->out.vwv+1, VWV(2), 1); /* num vcs */
328         SIVAL(req->out.vwv+1, VWV(3), req->smb_conn->negotiate.max_recv);
329         SIVAL(req->out.vwv+1, VWV(5), 0x10000); /* raw size. full 64k */
330         SIVAL(req->out.vwv+1, VWV(7), req->smb_conn->connection->server_id); /* session key */
331         SIVAL(req->out.vwv+1, VWV(9), capabilities);
332         push_nttime(req->out.vwv+1, VWV(11), nttime);
333         SSVALS(req->out.vwv+1,VWV(15), req->smb_conn->negotiate.zone_offset/60);
334         
335         if (!negotiate_spnego) {
336                 reply_nt1_orig(req);
337         } else {
338                 struct cli_credentials *server_credentials;
339                 struct gensec_security *gensec_security;
340                 DATA_BLOB null_data_blob = data_blob(NULL, 0);
341                 DATA_BLOB blob;
342                 const char *oid;
343                 NTSTATUS nt_status = gensec_server_start(req->smb_conn, 
344                                                          &gensec_security,
345                                                          req->smb_conn->connection->event.ctx);
346                 
347                 if (!NT_STATUS_IS_OK(nt_status)) {
348                         DEBUG(0, ("Failed to start GENSEC: %s\n", nt_errstr(nt_status)));
349                         smbsrv_terminate_connection(req->smb_conn, "Failed to start GENSEC\n");
350                         return;
351                 }
352
353                 if (req->smb_conn->negotiate.auth_context) {
354                         smbsrv_terminate_connection(req->smb_conn, "reply_nt1: is this a secondary negprot?  auth_context is non-NULL!\n");
355                         return;
356                 }
357
358                 server_credentials 
359                         = cli_credentials_init(req);
360                 if (!server_credentials) {
361                         smbsrv_terminate_connection(req->smb_conn, "Failed to init server credentials\n");
362                         return;
363                 }
364                 
365                 cli_credentials_set_conf(server_credentials);
366                 nt_status = cli_credentials_set_machine_account(server_credentials);
367                 if (!NT_STATUS_IS_OK(nt_status)) {
368                         DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(nt_status)));
369                         talloc_free(server_credentials);
370                         server_credentials = NULL;
371                 }
372
373                 req->smb_conn->negotiate.server_credentials = talloc_steal(req->smb_conn, server_credentials);
374
375                 gensec_set_target_service(gensec_security, "cifs");
376
377                 gensec_set_credentials(gensec_security, server_credentials);
378
379                 oid = GENSEC_OID_SPNEGO;
380                 nt_status = gensec_start_mech_by_oid(gensec_security, oid);
381                 
382                 if (NT_STATUS_IS_OK(nt_status)) {
383                         /* Get and push the proposed OID list into the packets */
384                         nt_status = gensec_update(gensec_security, req, null_data_blob, &blob);
385
386                         if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
387                                 DEBUG(1, ("Failed to get SPNEGO to give us the first token: %s\n", nt_errstr(nt_status)));
388                         }
389                 }
390
391                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
392                         DEBUG(3,("using SPNEGO\n"));
393                 } else {
394                         DEBUG(5, ("Failed to start SPNEGO, falling back to NTLMSSP only: %s\n", nt_errstr(nt_status)));
395                         oid = GENSEC_OID_NTLMSSP;
396                         nt_status = gensec_start_mech_by_oid(gensec_security, oid);
397                         
398                         if (!NT_STATUS_IS_OK(nt_status)) {
399                                 DEBUG(0, ("Failed to start SPNEGO as well as NTLMSSP fallback: %s\n", nt_errstr(nt_status)));
400                                 reply_nt1_orig(req);
401                                 return;
402                         }
403                         /* NTLMSSP is a client-first exchange */
404                         blob = data_blob(NULL, 0);
405                         DEBUG(3,("using raw-NTLMSSP\n"));
406                 }
407
408                 req->smb_conn->negotiate.oid = oid;
409         
410                 req_grow_data(req, blob.length + 16);
411                 /* a NOT very random guid, perhaps we should get it
412                  * from the credentials (kitchen sink...) */
413                 memset(req->out.ptr, '\0', 16);
414                 req->out.ptr += 16;
415
416                 memcpy(req->out.ptr, blob.data, blob.length);
417                 SCVAL(req->out.vwv+1, VWV(16), blob.length + 16);
418                 req->out.ptr += blob.length;
419         }
420         
421         smbsrv_send_reply_nosign(req);  
422 }
423
424 /****************************************************************************
425  Reply for the SMB2 2.001 protocol
426 ****************************************************************************/
427 static void reply_smb2(struct smbsrv_request *req, uint16_t choice)
428 {
429         struct smbsrv_connection *smb_conn = req->smb_conn;
430
431         /* reply with a SMB2 packet */
432         packet_set_callback(smb_conn->packet, smbsrv_recv_smb2_request);
433         smb2srv_reply_smb_negprot(req);
434         req = NULL;
435 }
436
437 /* List of supported protocols, most desired first */
438 static const struct {
439         const char *proto_name;
440         const char *short_name;
441         void (*proto_reply_fn)(struct smbsrv_request *req, uint16_t choice);
442         int protocol_level;
443 } supported_protocols[] = {
444         {"SMB 2.001",                   "SMB2",         reply_smb2,     PROTOCOL_SMB2},
445         {"NT LANMAN 1.0",               "NT1",          reply_nt1,      PROTOCOL_NT1},
446         {"NT LM 0.12",                  "NT1",          reply_nt1,      PROTOCOL_NT1},
447         {"LANMAN2.1",                   "LANMAN2",      reply_lanman2,  PROTOCOL_LANMAN2},
448         {"LM1.2X002",                   "LANMAN2",      reply_lanman2,  PROTOCOL_LANMAN2},
449         {"Samba",                       "LANMAN2",      reply_lanman2,  PROTOCOL_LANMAN2},
450         {"DOS LM1.2X002",               "LANMAN2",      reply_lanman2,  PROTOCOL_LANMAN2},
451         {"Windows for Workgroups 3.1a", "LANMAN1",      reply_lanman1,  PROTOCOL_LANMAN1},
452         {"LANMAN1.0",                   "LANMAN1",      reply_lanman1,  PROTOCOL_LANMAN1},
453         {"MICROSOFT NETWORKS 3.0",      "LANMAN1",      reply_lanman1,  PROTOCOL_LANMAN1},
454         {"MICROSOFT NETWORKS 1.03",     "COREPLUS",     reply_coreplus, PROTOCOL_COREPLUS},
455         {"PC NETWORK PROGRAM 1.0",      "CORE",         reply_corep,    PROTOCOL_CORE},
456         {NULL,NULL,NULL,0},
457 };
458
459 /****************************************************************************
460  Reply to a negprot.
461 ****************************************************************************/
462
463 void smbsrv_reply_negprot(struct smbsrv_request *req)
464 {
465         int protocol;
466         uint8_t *p;
467         uint32_t protos_count = 0;
468         char **protos = NULL;
469
470         if (req->smb_conn->negotiate.done_negprot) {
471                 smbsrv_terminate_connection(req->smb_conn, "multiple negprot's are not permitted");
472                 return;
473         }
474         req->smb_conn->negotiate.done_negprot = True;
475
476         p = req->in.data;
477         while (True) {
478                 size_t len;
479
480                 protos = talloc_realloc(req, protos, char *, protos_count + 1);
481                 if (!protos) {
482                         smbsrv_terminate_connection(req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
483                         return;
484                 }
485                 protos[protos_count] = NULL;
486                 len = req_pull_ascii4(req, (const char **)&protos[protos_count], p, STR_ASCII|STR_TERMINATE);
487                 p += len;
488                 if (len == 0 || !protos[protos_count]) break;
489
490                 DEBUG(5,("Requested protocol [%d][%s]\n", protos_count, protos[protos_count]));
491                 protos_count++;
492         }
493
494         /* Check for protocols, most desirable first */
495         for (protocol = 0; supported_protocols[protocol].proto_name; protocol++) {
496                 int i;
497
498                 if (supported_protocols[protocol].protocol_level > lp_srv_maxprotocol()) continue;
499                 if (supported_protocols[protocol].protocol_level < lp_srv_minprotocol()) continue;
500
501                 for (i = 0; i < protos_count; i++) {
502                         if (strcmp(supported_protocols[protocol].proto_name, protos[i]) != 0) continue;
503
504                         supported_protocols[protocol].proto_reply_fn(req, i);
505                         sub_set_remote_proto(supported_protocols[protocol].short_name);
506                         DEBUG(3,("Selected protocol [%d][%s]\n",
507                                 i, supported_protocols[protocol].proto_name));
508                         return;
509                 }
510         }
511
512         smbsrv_terminate_connection(req->smb_conn, "No protocol supported !");
513 }