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