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