r4728: split up server_services into:
[samba.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 "smb_server/smb_server.h"
24
25
26 /* initialise the auth_context for this server and return the cryptkey */
27 static NTSTATUS get_challenge(struct smbsrv_connection *smb_conn, uint8_t buff[8]) 
28 {
29         NTSTATUS nt_status;
30         const uint8_t *challenge;
31
32         /* muliple negprots are not premitted */
33         if (smb_conn->negotiate.auth_context) {
34                 DEBUG(3,("get challenge: is this a secondary negprot?  auth_context is non-NULL!\n"));
35                 return NT_STATUS_FOOBAR;
36         }
37
38         DEBUG(10, ("get challenge: creating negprot_global_auth_context\n"));
39
40         nt_status = auth_context_create(smb_conn, lp_auth_methods(), &smb_conn->negotiate.auth_context);
41         if (!NT_STATUS_IS_OK(nt_status)) {
42                 DEBUG(0, ("auth_context_create() returned %s", nt_errstr(nt_status)));
43                 return nt_status;
44         }
45
46         nt_status = auth_context_create(smb_conn, lp_auth_methods(), &smb_conn->negotiate.auth_context);
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         req_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         req_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         req_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         req_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         req_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->connection.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         req_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         req_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->connection.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         req_send_reply(req);
222 }
223
224 /****************************************************************************
225  Reply for the nt protocol.
226 ****************************************************************************/
227 static void reply_nt1(struct smbsrv_request *req, uint16_t choice)
228 {
229         /* dual names + lock_and_read + nt SMBs + remote API calls */
230         int capabilities;
231         int secword=0;
232         time_t t = req->request_time.tv_sec;
233         NTTIME nttime;
234         BOOL negotiate_spnego = False;
235
236         unix_to_nt_time(&nttime, t);
237
238         capabilities = 
239                 CAP_NT_FIND | CAP_LOCK_AND_READ | 
240                 CAP_LEVEL_II_OPLOCKS | CAP_NT_SMBS | CAP_RPC_REMOTE_APIS;
241
242         req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
243
244         /* do spnego in user level security if the client
245            supports it and we can do encrypted passwords */
246         
247         if (req->smb_conn->negotiate.encrypted_passwords && 
248             (lp_security() != SEC_SHARE) &&
249             lp_use_spnego() &&
250             (req->flags2 & FLAGS2_EXTENDED_SECURITY)) {
251                 negotiate_spnego = True; 
252                 capabilities |= CAP_EXTENDED_SECURITY;
253         }
254         
255         if (lp_unix_extensions()) {
256                 capabilities |= CAP_UNIX;
257         }
258         
259         if (lp_large_readwrite()) {
260                 capabilities |= CAP_LARGE_READX | CAP_LARGE_WRITEX | CAP_W2K_SMBS;
261         }
262         
263         capabilities |= CAP_LARGE_FILES;
264
265         if (lp_readraw() && lp_writeraw()) {
266                 capabilities |= CAP_RAW_MODE;
267         }
268         
269         /* allow for disabling unicode */
270         if (lp_unicode()) {
271                 capabilities |= CAP_UNICODE;
272         }
273
274         if (lp_nt_status_support()) {
275                 capabilities |= CAP_STATUS32;
276         }
277         
278         if (lp_host_msdfs()) {
279                 capabilities |= CAP_DFS;
280         }
281         
282         if (lp_security() != SEC_SHARE) {
283                 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
284         }
285
286         if (req->smb_conn->negotiate.encrypted_passwords) {
287                 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
288         }
289
290         if (req->smb_conn->signing.allow_smb_signing) {
291                 secword |= NEGOTIATE_SECURITY_SIGNATURES_ENABLED;
292         }
293
294         if (req->smb_conn->signing.mandatory_signing) {
295                 secword |= NEGOTIATE_SECURITY_SIGNATURES_REQUIRED;
296         }
297         
298         req->smb_conn->negotiate.protocol = PROTOCOL_NT1;
299
300         req_setup_reply(req, 17, 0);
301         
302         SSVAL(req->out.vwv, VWV(0), choice);
303         SCVAL(req->out.vwv, VWV(1), secword);
304
305         /* notice the strange +1 on vwv here? That's because
306            this is the one and only SMB packet that is malformed in
307            the specification - all the command words after the secword
308            are offset by 1 byte */
309         SSVAL(req->out.vwv+1, VWV(1), lp_maxmux());
310         SSVAL(req->out.vwv+1, VWV(2), 1); /* num vcs */
311         SIVAL(req->out.vwv+1, VWV(3), req->smb_conn->negotiate.max_recv);
312         SIVAL(req->out.vwv+1, VWV(5), 0x10000); /* raw size. full 64k */
313         SIVAL(req->out.vwv+1, VWV(7), req->smb_conn->connection->connection.id); /* session key */
314         SIVAL(req->out.vwv+1, VWV(9), capabilities);
315         push_nttime(req->out.vwv+1, VWV(11), nttime);
316         SSVALS(req->out.vwv+1,VWV(15), req->smb_conn->negotiate.zone_offset/60);
317         
318         if (!negotiate_spnego) {
319                 /* Create a token value and add it to the outgoing packet. */
320                 if (req->smb_conn->negotiate.encrypted_passwords) {
321                         req_grow_data(req, 8);
322                         /* note that we do not send a challenge at all if
323                            we are using plaintext */
324                         get_challenge(req->smb_conn, req->out.ptr);
325                         req->out.ptr += 8;
326                         SCVAL(req->out.vwv+1, VWV(16), 8);
327                 }
328                 req_push_str(req, NULL, lp_workgroup(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
329                 req_push_str(req, NULL, lp_netbios_name(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
330                 DEBUG(3,("not using SPNEGO\n"));
331         } else {
332                 struct gensec_security *gensec_security;
333                 DATA_BLOB null_data_blob = data_blob(NULL, 0);
334                 DATA_BLOB blob;
335                 NTSTATUS nt_status = gensec_server_start(req->smb_conn, &gensec_security);
336                 
337                 if (req->smb_conn->negotiate.auth_context) {
338                         smbsrv_terminate_connection(req->smb_conn, "reply_nt1: is this a secondary negprot?  auth_context is non-NULL!\n");
339                         return;
340                 }
341
342                 req->smb_conn->negotiate.auth_context = NULL;
343                 
344                 if (!NT_STATUS_IS_OK(nt_status)) {
345                         DEBUG(0, ("Failed to start GENSEC: %s\n", nt_errstr(nt_status)));
346                         smbsrv_terminate_connection(req->smb_conn, "Failed to start GENSEC\n");
347                         return;
348                 }
349
350                 nt_status = gensec_start_mech_by_oid(gensec_security, GENSEC_OID_SPNEGO);
351                 
352                 if (!NT_STATUS_IS_OK(nt_status)) {
353                         DEBUG(0, ("Failed to start SPNEGO: %s\n", nt_errstr(nt_status)));
354                         smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO\n");
355                         return;
356                 }
357
358                 nt_status = gensec_update(gensec_security, req, null_data_blob, &blob);
359
360                 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
361                         DEBUG(0, ("Failed to get SPNEGO to give us the first token: %s\n", nt_errstr(nt_status)));
362                         smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO - no first token\n");
363                         return;
364                 }
365
366                 req->smb_conn->negotiate.spnego_negotiated = True;
367         
368                 req_grow_data(req, blob.length + 16);
369                 /* a NOT very random guid */
370                 memset(req->out.ptr, '\0', 16);
371                 req->out.ptr += 16;
372
373                 memcpy(req->out.ptr, blob.data, blob.length);
374                 SCVAL(req->out.vwv+1, VWV(16), blob.length + 16);
375                 req->out.ptr += blob.length;
376                 DEBUG(3,("using SPNEGO\n"));
377         }
378         
379         req_send_reply_nosign(req);     
380 }
381
382  
383 /* List of supported protocols, most desired first */
384 static const struct {
385         const char *proto_name;
386         const char *short_name;
387         void (*proto_reply_fn)(struct smbsrv_request *req, uint16_t choice);
388         int protocol_level;
389 } supported_protocols[] = {
390         {"NT LANMAN 1.0",           "NT1",      reply_nt1,      PROTOCOL_NT1},
391         {"NT LM 0.12",              "NT1",      reply_nt1,      PROTOCOL_NT1},
392         {"LANMAN2.1",               "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
393         {"LM1.2X002",               "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
394         {"Samba",                   "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
395         {"DOS LM1.2X002",           "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
396         {"LANMAN1.0",               "LANMAN1",  reply_lanman1,  PROTOCOL_LANMAN1},
397         {"MICROSOFT NETWORKS 3.0",  "LANMAN1",  reply_lanman1,  PROTOCOL_LANMAN1},
398         {"MICROSOFT NETWORKS 1.03", "COREPLUS", reply_coreplus, PROTOCOL_COREPLUS},
399         {"PC NETWORK PROGRAM 1.0",  "CORE",     reply_corep,    PROTOCOL_CORE}, 
400         {NULL,NULL,NULL,0},
401 };
402
403 /****************************************************************************
404  Reply to a negprot.
405 ****************************************************************************/
406
407 void reply_negprot(struct smbsrv_request *req)
408 {
409         int Index=0;
410         int choice = -1;
411         int protocol;
412         uint8_t *p;
413
414         if (req->smb_conn->negotiate.done_negprot) {
415                 smbsrv_terminate_connection(req->smb_conn, "multiple negprot's are not permitted");
416                 return;
417         }
418         req->smb_conn->negotiate.done_negprot = True;
419
420         p = req->in.data + 1;
421
422         while (p < req->in.data + req->in.data_size) { 
423                 Index++;
424                 DEBUG(3,("Requested protocol [%s]\n",(const char *)p));
425                 p += strlen((const char *)p) + 2;
426         }
427     
428         /* Check for protocols, most desirable first */
429         for (protocol = 0; supported_protocols[protocol].proto_name; protocol++) {
430                 p = req->in.data+1;
431                 Index = 0;
432                 if ((supported_protocols[protocol].protocol_level <= lp_maxprotocol()) &&
433                                 (supported_protocols[protocol].protocol_level >= lp_minprotocol()))
434                         while (p < (req->in.data + req->in.data_size)) { 
435                                 if (strequal((const char *)p,supported_protocols[protocol].proto_name))
436                                         choice = Index;
437                                 Index++;
438                                 p += strlen((const char *)p) + 2;
439                         }
440                 if(choice != -1)
441                         break;
442         }
443   
444         if(choice != -1) {
445                 sub_set_remote_proto(supported_protocols[protocol].short_name);
446                 reload_services(req->smb_conn, True);
447                 supported_protocols[protocol].proto_reply_fn(req, choice);
448                 DEBUG(3,("Selected protocol %s\n",supported_protocols[protocol].proto_name));
449         } else {
450                 DEBUG(0,("No protocol supported !\n"));
451         }
452   
453         DEBUG(5,("negprot index=%d\n", choice));
454 }