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