s3:smbd: add support for SMB2 Negotiate
[metze/samba/wip.git] / source3 / smbd / smb2_negprot.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/globals.h"
23 #include "../source4/libcli/smb2/smb2_constants.h"
24
25 extern enum protocol_types Protocol;
26
27 NTSTATUS smbd_smb2_request_process_negprot(struct smbd_smb2_request *req)
28 {
29         const uint8_t *inbody;
30         const uint8_t *indyn = NULL;
31         int i = req->current_idx;
32         DATA_BLOB outbody;
33         DATA_BLOB outdyn;
34         DATA_BLOB negprot_spnego_blob;
35         uint16_t security_offset;
36         DATA_BLOB security_buffer;
37         size_t expected_body_size = 0x24;
38         size_t body_size;
39         size_t expected_dyn_size = 0;
40         size_t c;
41         uint16_t dialect_count;
42         uint16_t dialect;
43
44 /* TODO: drop the connection with INVALI_PARAMETER */
45
46         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
47                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
48         }
49
50         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
51
52         body_size = SVAL(inbody, 0x00);
53         if (body_size != expected_body_size) {
54                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
55         }
56
57         dialect_count = SVAL(inbody, 0x02);
58         if (dialect_count == 0) {
59                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
60         }
61
62         expected_dyn_size = dialect_count * 2;
63         if (req->in.vector[i+2].iov_len < expected_dyn_size) {
64                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
65         }
66         indyn = (const uint8_t *)req->in.vector[i+2].iov_base;
67
68         for (c=0; c < dialect_count; c++) {
69                 dialect = SVAL(indyn, c*2);
70                 if (dialect == 0x0202) {
71                         break;
72                 }
73         }
74
75         if (dialect != 0x0202) {
76                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
77         }
78
79         Protocol = PROTOCOL_SMB2;
80
81         if (get_remote_arch() != RA_SAMBA) {
82                 set_remote_arch(RA_VISTA);
83         }
84
85         /* negprot_spnego() returns a the server guid in the first 16 bytes */
86         negprot_spnego_blob = negprot_spnego();
87         if (negprot_spnego_blob.data == NULL) {
88                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
89         }
90         talloc_steal(req, negprot_spnego_blob.data);
91
92         if (negprot_spnego_blob.length < 16) {
93                 return smbd_smb2_request_error(req, NT_STATUS_INTERNAL_ERROR);
94         }
95
96         security_offset = SMB2_HDR_BODY + 0x40;
97         security_buffer = data_blob_const(negprot_spnego_blob.data + 16,
98                                           negprot_spnego_blob.length - 16);
99
100         outbody = data_blob_talloc(req->out.vector, NULL, 0x40);
101         if (outbody.data == NULL) {
102                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
103         }
104
105         SSVAL(outbody.data, 0x00, 0x40 + 1);    /* struct size */
106 /*TODO: indicate signing enabled */
107         SSVAL(outbody.data, 0x02, 0);           /* security mode */
108         SSVAL(outbody.data, 0x04, dialect);     /* dialect revision */
109         SSVAL(outbody.data, 0x06, 0);           /* reserved */
110         memcpy(outbody.data + 0x08,
111                negprot_spnego_blob.data, 16);   /* server guid */
112         SIVAL(outbody.data, 0x18, 0);           /* capabilities */
113         SIVAL(outbody.data, 0x1C, 0x00010000);  /* max transact size */
114         SIVAL(outbody.data, 0x20, 0x00010000);  /* max read size */
115         SIVAL(outbody.data, 0x24, 0x00010000);  /* max write size */
116         SBVAL(outbody.data, 0x28, 0);           /* system time */
117         SBVAL(outbody.data, 0x30, 0);           /* server start time */
118         SSVAL(outbody.data, 0x38,
119               security_offset);                 /* security buffer offset */
120         SSVAL(outbody.data, 0x3A,
121               security_buffer.length);          /* security buffer length */
122         SIVAL(outbody.data, 0x3C, 0);           /* reserved */
123
124         outdyn = security_buffer;
125
126         return smbd_smb2_request_done(req, outbody, &outdyn);
127 }