0a33b105cd884a3cd6b96002c1e9aa1e31836630
[jelmer/samba4-debian.git] / source / smb_server / 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 "smbd/service_stream.h"
25
26
27 /* initialise the auth_context for this server and return the cryptkey */
28 static NTSTATUS get_challenge(struct smbsrv_connection *smb_conn, uint8_t buff[8]) 
29 {
30         NTSTATUS nt_status;
31         const uint8_t *challenge;
32
33         /* muliple negprots are not premitted */
34         if (smb_conn->negotiate.auth_context) {
35                 DEBUG(3,("get challenge: is this a secondary negprot?  auth_context is non-NULL!\n"));
36                 return NT_STATUS_FOOBAR;
37         }
38
39         DEBUG(10, ("get challenge: creating negprot_global_auth_context\n"));
40
41         nt_status = auth_context_create(smb_conn, lp_auth_methods(), &smb_conn->negotiate.auth_context);
42         if (!NT_STATUS_IS_OK(nt_status)) {
43                 DEBUG(0, ("auth_context_create() returned %s", nt_errstr(nt_status)));
44                 return nt_status;
45         }
46
47         nt_status = auth_get_challenge(smb_conn->negotiate.auth_context, &challenge);
48         if (!NT_STATUS_IS_OK(nt_status)) {
49                 DEBUG(0, ("auth_get_challenge() returned %s", nt_errstr(nt_status)));
50                 return nt_status;
51         }
52
53         memcpy(buff, challenge, 8);
54
55         return NT_STATUS_OK;
56 }
57
58 /****************************************************************************
59  Reply for the core protocol.
60 ****************************************************************************/
61 static void reply_corep(struct smbsrv_request *req, uint16_t choice)
62 {
63         req_setup_reply(req, 1, 0);
64
65         SSVAL(req->out.vwv, VWV(0), choice);
66
67         req->smb_conn->negotiate.protocol = PROTOCOL_CORE;
68
69         if (req->smb_conn->signing.mandatory_signing) {
70                 smbsrv_terminate_connection(req->smb_conn, 
71                                             "CORE does not support SMB signing, and it is mandatory\n");
72                 return;
73         }
74
75         req_send_reply(req);
76 }
77
78 /****************************************************************************
79  Reply for the coreplus protocol.
80 this is quite incomplete - we only fill in a small part of the reply, but as nobody uses
81 this any more it probably doesn't matter
82 ****************************************************************************/
83 static void reply_coreplus(struct smbsrv_request *req, uint16_t choice)
84 {
85         uint16_t raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
86
87         req_setup_reply(req, 13, 0);
88
89         /* Reply, SMBlockread, SMBwritelock supported. */
90         SCVAL(req->out.hdr,HDR_FLG,
91               CVAL(req->out.hdr, HDR_FLG) | FLAG_SUPPORT_LOCKREAD);
92
93         SSVAL(req->out.vwv, VWV(0), choice);
94         SSVAL(req->out.vwv, VWV(1), 0x1); /* user level security, don't encrypt */      
95
96         /* tell redirector we support
97            readbraw and writebraw (possibly) */
98         SSVAL(req->out.vwv, VWV(5), raw); 
99
100         req->smb_conn->negotiate.protocol = PROTOCOL_COREPLUS;
101
102         if (req->smb_conn->signing.mandatory_signing) {
103                 smbsrv_terminate_connection(req->smb_conn, 
104                                             "COREPLUS does not support SMB signing, and it is mandatory\n");
105                 return;
106         }
107
108         req_send_reply(req);
109 }
110
111 /****************************************************************************
112  Reply for the lanman 1.0 protocol.
113 ****************************************************************************/
114 static void reply_lanman1(struct smbsrv_request *req, uint16_t choice)
115 {
116         int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
117         int secword=0;
118         time_t t = req->request_time.tv_sec;
119
120         req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
121
122         if (lp_security() != SEC_SHARE)
123                 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
124
125         if (req->smb_conn->negotiate.encrypted_passwords)
126                 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
127
128         req->smb_conn->negotiate.protocol = PROTOCOL_LANMAN1;
129
130         req_setup_reply(req, 13, req->smb_conn->negotiate.encrypted_passwords ? 8 : 0);
131
132         /* SMBlockread, SMBwritelock supported. */
133         SCVAL(req->out.hdr,HDR_FLG,
134               CVAL(req->out.hdr, HDR_FLG) | FLAG_SUPPORT_LOCKREAD);
135
136         SSVAL(req->out.vwv, VWV(0), choice);
137         SSVAL(req->out.vwv, VWV(1), secword); 
138         SSVAL(req->out.vwv, VWV(2), req->smb_conn->negotiate.max_recv);
139         SSVAL(req->out.vwv, VWV(3), lp_maxmux());
140         SSVAL(req->out.vwv, VWV(4), 1);
141         SSVAL(req->out.vwv, VWV(5), raw); 
142         SIVAL(req->out.vwv, VWV(6), req->smb_conn->connection->server_id);
143         srv_push_dos_date(req->smb_conn, req->out.vwv, VWV(8), t);
144         SSVAL(req->out.vwv, VWV(10), req->smb_conn->negotiate.zone_offset/60);
145         SIVAL(req->out.vwv, VWV(11), 0); /* reserved */
146
147         /* Create a token value and add it to the outgoing packet. */
148         if (req->smb_conn->negotiate.encrypted_passwords) {
149                 NTSTATUS nt_status;
150
151                 SSVAL(req->out.vwv, VWV(11), 8);
152
153                 nt_status = get_challenge(req->smb_conn, req->out.data);
154                 if (!NT_STATUS_IS_OK(nt_status)) {
155                         smbsrv_terminate_connection(req->smb_conn, "LANMAN1 get_challenge failed\n");
156                         return;
157                 }
158         }
159
160         if (req->smb_conn->signing.mandatory_signing) {
161                 smbsrv_terminate_connection(req->smb_conn, 
162                                             "LANMAN1 does not support SMB signing, and it is mandatory\n");
163                 return;
164         }
165
166         req_send_reply(req);    
167 }
168
169 /****************************************************************************
170  Reply for the lanman 2.0 protocol.
171 ****************************************************************************/
172 static void reply_lanman2(struct smbsrv_request *req, uint16_t choice)
173 {
174         int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
175         int secword=0;
176         time_t t = req->request_time.tv_sec;
177
178         req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
179   
180         if (lp_security() != SEC_SHARE)
181                 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
182
183         if (req->smb_conn->negotiate.encrypted_passwords)
184                 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
185
186         req->smb_conn->negotiate.protocol = PROTOCOL_LANMAN2;
187
188         req_setup_reply(req, 13, 0);
189
190         SSVAL(req->out.vwv, VWV(0), choice);
191         SSVAL(req->out.vwv, VWV(1), secword); 
192         SSVAL(req->out.vwv, VWV(2), req->smb_conn->negotiate.max_recv);
193         SSVAL(req->out.vwv, VWV(3), lp_maxmux());
194         SSVAL(req->out.vwv, VWV(4), 1);
195         SSVAL(req->out.vwv, VWV(5), raw); 
196         SIVAL(req->out.vwv, VWV(6), req->smb_conn->connection->server_id);
197         srv_push_dos_date(req->smb_conn, req->out.vwv, VWV(8), t);
198         SSVAL(req->out.vwv, VWV(10), req->smb_conn->negotiate.zone_offset/60);
199         SIVAL(req->out.vwv, VWV(11), 0);
200
201         /* Create a token value and add it to the outgoing packet. */
202         if (req->smb_conn->negotiate.encrypted_passwords) {
203                 SSVAL(req->out.vwv, VWV(11), 8);
204                 req_grow_data(req, 8);
205                 get_challenge(req->smb_conn, req->out.data);
206         }
207
208         req_push_str(req, NULL, lp_workgroup(), -1, STR_TERMINATE);
209
210         if (req->smb_conn->signing.mandatory_signing) {
211                 smbsrv_terminate_connection(req->smb_conn, 
212                                             "LANMAN2 does not support SMB signing, and it is mandatory\n");
213                 return;
214         }
215
216         req_send_reply(req);
217 }
218
219 /****************************************************************************
220  Reply for the nt protocol.
221 ****************************************************************************/
222 static void reply_nt1(struct smbsrv_request *req, uint16_t choice)
223 {
224         /* dual names + lock_and_read + nt SMBs + remote API calls */
225         int capabilities;
226         int secword=0;
227         time_t t = req->request_time.tv_sec;
228         NTTIME nttime;
229         BOOL negotiate_spnego = False;
230
231         unix_to_nt_time(&nttime, t);
232
233         capabilities = 
234                 CAP_NT_FIND | CAP_LOCK_AND_READ | 
235                 CAP_LEVEL_II_OPLOCKS | CAP_NT_SMBS | CAP_RPC_REMOTE_APIS;
236
237         req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
238
239         /* do spnego in user level security if the client
240            supports it and we can do encrypted passwords */
241         
242         if (req->smb_conn->negotiate.encrypted_passwords && 
243             (lp_security() != SEC_SHARE) &&
244             lp_use_spnego() &&
245             (req->flags2 & FLAGS2_EXTENDED_SECURITY)) {
246                 negotiate_spnego = True; 
247                 capabilities |= CAP_EXTENDED_SECURITY;
248         }
249         
250         if (lp_unix_extensions()) {
251                 capabilities |= CAP_UNIX;
252         }
253         
254         if (lp_large_readwrite()) {
255                 capabilities |= CAP_LARGE_READX | CAP_LARGE_WRITEX | CAP_W2K_SMBS;
256         }
257         
258         capabilities |= CAP_LARGE_FILES;
259
260         if (lp_readraw() && lp_writeraw()) {
261                 capabilities |= CAP_RAW_MODE;
262         }
263         
264         /* allow for disabling unicode */
265         if (lp_unicode()) {
266                 capabilities |= CAP_UNICODE;
267         }
268
269         if (lp_nt_status_support()) {
270                 capabilities |= CAP_STATUS32;
271         }
272         
273         if (lp_host_msdfs()) {
274                 capabilities |= CAP_DFS;
275         }
276         
277         if (lp_security() != SEC_SHARE) {
278                 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
279         }
280
281         if (req->smb_conn->negotiate.encrypted_passwords) {
282                 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
283         }
284
285         if (req->smb_conn->signing.allow_smb_signing) {
286                 secword |= NEGOTIATE_SECURITY_SIGNATURES_ENABLED;
287         }
288
289         if (req->smb_conn->signing.mandatory_signing) {
290                 secword |= NEGOTIATE_SECURITY_SIGNATURES_REQUIRED;
291         }
292         
293         req->smb_conn->negotiate.protocol = PROTOCOL_NT1;
294
295         req_setup_reply(req, 17, 0);
296         
297         SSVAL(req->out.vwv, VWV(0), choice);
298         SCVAL(req->out.vwv, VWV(1), secword);
299
300         /* notice the strange +1 on vwv here? That's because
301            this is the one and only SMB packet that is malformed in
302            the specification - all the command words after the secword
303            are offset by 1 byte */
304         SSVAL(req->out.vwv+1, VWV(1), lp_maxmux());
305         SSVAL(req->out.vwv+1, VWV(2), 1); /* num vcs */
306         SIVAL(req->out.vwv+1, VWV(3), req->smb_conn->negotiate.max_recv);
307         SIVAL(req->out.vwv+1, VWV(5), 0x10000); /* raw size. full 64k */
308         SIVAL(req->out.vwv+1, VWV(7), req->smb_conn->connection->server_id); /* session key */
309         SIVAL(req->out.vwv+1, VWV(9), capabilities);
310         push_nttime(req->out.vwv+1, VWV(11), nttime);
311         SSVALS(req->out.vwv+1,VWV(15), req->smb_conn->negotiate.zone_offset/60);
312         
313         if (!negotiate_spnego) {
314                 /* Create a token value and add it to the outgoing packet. */
315                 if (req->smb_conn->negotiate.encrypted_passwords) {
316                         req_grow_data(req, 8);
317                         /* note that we do not send a challenge at all if
318                            we are using plaintext */
319                         get_challenge(req->smb_conn, req->out.ptr);
320                         req->out.ptr += 8;
321                         SCVAL(req->out.vwv+1, VWV(16), 8);
322                 }
323                 req_push_str(req, NULL, lp_workgroup(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
324                 req_push_str(req, NULL, lp_netbios_name(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
325                 DEBUG(3,("not using SPNEGO\n"));
326         } else {
327                 struct gensec_security *gensec_security;
328                 DATA_BLOB null_data_blob = data_blob(NULL, 0);
329                 DATA_BLOB blob;
330                 NTSTATUS nt_status = gensec_server_start(req->smb_conn, &gensec_security);
331                 
332                 if (req->smb_conn->negotiate.auth_context) {
333                         smbsrv_terminate_connection(req->smb_conn, "reply_nt1: is this a secondary negprot?  auth_context is non-NULL!\n");
334                         return;
335                 }
336
337                 req->smb_conn->negotiate.auth_context = NULL;
338                 
339                 if (!NT_STATUS_IS_OK(nt_status)) {
340                         DEBUG(0, ("Failed to start GENSEC: %s\n", nt_errstr(nt_status)));
341                         smbsrv_terminate_connection(req->smb_conn, "Failed to start GENSEC\n");
342                         return;
343                 }
344
345                 nt_status = gensec_start_mech_by_oid(gensec_security, GENSEC_OID_SPNEGO);
346                 
347                 if (!NT_STATUS_IS_OK(nt_status)) {
348                         DEBUG(0, ("Failed to start SPNEGO: %s\n", nt_errstr(nt_status)));
349                         smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO\n");
350                         return;
351                 }
352
353                 nt_status = gensec_update(gensec_security, req, null_data_blob, &blob);
354
355                 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
356                         DEBUG(0, ("Failed to get SPNEGO to give us the first token: %s\n", nt_errstr(nt_status)));
357                         smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO - no first token\n");
358                         return;
359                 }
360
361                 req->smb_conn->negotiate.spnego_negotiated = True;
362         
363                 req_grow_data(req, blob.length + 16);
364                 /* a NOT very random guid */
365                 memset(req->out.ptr, '\0', 16);
366                 req->out.ptr += 16;
367
368                 memcpy(req->out.ptr, blob.data, blob.length);
369                 SCVAL(req->out.vwv+1, VWV(16), blob.length + 16);
370                 req->out.ptr += blob.length;
371                 DEBUG(3,("using SPNEGO\n"));
372         }
373         
374         req_send_reply_nosign(req);     
375 }
376
377  
378 /* List of supported protocols, most desired first */
379 static const struct {
380         const char *proto_name;
381         const char *short_name;
382         void (*proto_reply_fn)(struct smbsrv_request *req, uint16_t choice);
383         int protocol_level;
384 } supported_protocols[] = {
385         {"NT LANMAN 1.0",           "NT1",      reply_nt1,      PROTOCOL_NT1},
386         {"NT LM 0.12",              "NT1",      reply_nt1,      PROTOCOL_NT1},
387         {"LANMAN2.1",               "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
388         {"LM1.2X002",               "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
389         {"Samba",                   "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
390         {"DOS LM1.2X002",           "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
391         {"LANMAN1.0",               "LANMAN1",  reply_lanman1,  PROTOCOL_LANMAN1},
392         {"MICROSOFT NETWORKS 3.0",  "LANMAN1",  reply_lanman1,  PROTOCOL_LANMAN1},
393         {"MICROSOFT NETWORKS 1.03", "COREPLUS", reply_coreplus, PROTOCOL_COREPLUS},
394         {"PC NETWORK PROGRAM 1.0",  "CORE",     reply_corep,    PROTOCOL_CORE}, 
395         {NULL,NULL,NULL,0},
396 };
397
398 /****************************************************************************
399  Reply to a negprot.
400 ****************************************************************************/
401
402 void reply_negprot(struct smbsrv_request *req)
403 {
404         int Index=0;
405         int choice = -1;
406         int protocol;
407         uint8_t *p;
408
409         if (req->smb_conn->negotiate.done_negprot) {
410                 smbsrv_terminate_connection(req->smb_conn, "multiple negprot's are not permitted");
411                 return;
412         }
413         req->smb_conn->negotiate.done_negprot = True;
414
415         p = req->in.data + 1;
416
417         while (p < req->in.data + req->in.data_size) { 
418                 Index++;
419                 DEBUG(3,("Requested protocol [%s]\n",(const char *)p));
420                 p += strlen((const char *)p) + 2;
421         }
422     
423         /* Check for protocols, most desirable first */
424         for (protocol = 0; supported_protocols[protocol].proto_name; protocol++) {
425                 p = req->in.data+1;
426                 Index = 0;
427                 if ((supported_protocols[protocol].protocol_level <= lp_maxprotocol()) &&
428                                 (supported_protocols[protocol].protocol_level >= lp_minprotocol()))
429                         while (p < (req->in.data + req->in.data_size)) { 
430                                 if (strequal((const char *)p,supported_protocols[protocol].proto_name))
431                                         choice = Index;
432                                 Index++;
433                                 p += strlen((const char *)p) + 2;
434                         }
435                 if(choice != -1)
436                         break;
437         }
438   
439         if(choice != -1) {
440                 sub_set_remote_proto(supported_protocols[protocol].short_name);
441                 supported_protocols[protocol].proto_reply_fn(req, choice);
442                 DEBUG(3,("Selected protocol %s\n",supported_protocols[protocol].proto_name));
443         } else {
444                 DEBUG(0,("No protocol supported !\n"));
445         }
446   
447         DEBUG(5,("negprot index=%d\n", choice));
448 }