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