630f38b15948489497cd4330f0d49f5d3ba72e13
[kai/samba-autobuild/.git] / source4 / smb_server / smb2 / negprot.c
1 /* 
2    Unix SMB2 implementation.
3    
4    Copyright (C) Andrew Bartlett        2001-2005
5    Copyright (C) Stefan Metzmacher      2005
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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "auth/auth.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "smb_server/smb_server.h"
27 #include "smb_server/smb2/smb2_server.h"
28 #include "smbd/service_stream.h"
29
30 static NTSTATUS smb2srv_negprot_secblob(struct smb2srv_request *req, DATA_BLOB *_blob)
31 {
32         struct gensec_security *gensec_security;
33         DATA_BLOB null_data_blob = data_blob(NULL, 0);
34         DATA_BLOB blob;
35         NTSTATUS nt_status;
36         struct cli_credentials *server_credentials;
37
38         nt_status = gensec_server_start(req, &gensec_security,
39                                         req->smb_conn->connection->event.ctx);
40         if (!NT_STATUS_IS_OK(nt_status)) {
41                 DEBUG(0, ("Failed to start GENSEC: %s\n", nt_errstr(nt_status)));
42                 smbsrv_terminate_connection(req->smb_conn, "Failed to start GENSEC\n");
43                 return nt_status;
44         }
45
46         server_credentials = cli_credentials_init(req);
47         if (!server_credentials) {
48                 smbsrv_terminate_connection(req->smb_conn, "Failed to init server credentials\n");
49                 return NT_STATUS_NO_MEMORY;
50         }
51
52         cli_credentials_set_conf(server_credentials);
53         nt_status = cli_credentials_set_machine_account(server_credentials);
54         if (!NT_STATUS_IS_OK(nt_status)) {
55                 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(nt_status)));
56                 talloc_free(server_credentials);
57                 server_credentials = NULL;
58         }
59
60         req->smb_conn->negotiate.server_credentials = talloc_steal(req->smb_conn, server_credentials);
61
62         gensec_set_target_service(gensec_security, "cifs");
63
64         gensec_set_credentials(gensec_security, server_credentials);
65
66         nt_status = gensec_start_mech_by_oid(gensec_security, GENSEC_OID_SPNEGO);
67         if (!NT_STATUS_IS_OK(nt_status)) {
68                 DEBUG(0, ("Failed to start SPNEGO: %s\n", nt_errstr(nt_status)));
69                 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO\n");
70                 return nt_status;
71         }
72
73         nt_status = gensec_update(gensec_security, req, null_data_blob, &blob);
74         if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
75                 DEBUG(0, ("Failed to get SPNEGO to give us the first token: %s\n", nt_errstr(nt_status)));
76                 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO - no first token\n");
77                 return nt_status;
78         }
79
80         *_blob = blob;
81         return NT_STATUS_OK;
82 }
83
84 static NTSTATUS smb2srv_negprot_backend(struct smb2srv_request *req, struct smb2_negprot *io)
85 {
86         NTSTATUS status;
87         struct timeval current_time;
88         struct timeval boot_time;
89
90         current_time = timeval_current(); /* TODO: handle timezone?! */
91         boot_time = timeval_current(); /* TODO: fix me */
92
93         io->out._pad            = 0;
94         io->out.unknown2        = 0x06;
95         ZERO_STRUCT(io->out.sessid);
96         io->out.unknown3        = 0x0d;
97         io->out.unknown4        = 0x00;
98         io->out.unknown5        = 0x01;
99         io->out.unknown6        = 0x01;
100         io->out.unknown7        = 0x01;
101         io->out.current_time    = timeval_to_nttime(&current_time);
102         io->out.boot_time       = timeval_to_nttime(&boot_time);
103         status = smb2srv_negprot_secblob(req, &io->out.secblob);
104         NT_STATUS_NOT_OK_RETURN(status);
105         io->out.unknown9        = 0x204d4c20;
106
107         return NT_STATUS_OK;
108 }
109
110 static void smb2srv_negprot_send(struct smb2srv_request *req, struct smb2_negprot *io)
111 {
112         NTSTATUS status;
113
114         if (NT_STATUS_IS_ERR(req->status)) {
115                 smb2srv_send_error(req, req->status); /* TODO: is this correct? */
116                 return;
117         }
118
119         status = smb2srv_setup_reply(req, 0x40, io->out.secblob.length);
120         if (!NT_STATUS_IS_OK(status)) {
121                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
122                 talloc_free(req);
123                 return;
124         }
125
126         SIVAL(req->out.hdr, SMB2_HDR_STATUS, NT_STATUS_V(req->status));
127
128         SSVAL(req->out.body, 0x02, io->out._pad);
129         SIVAL(req->out.body, 0x04, io->out.unknown2);
130         memcpy(req->out.body+0x08, io->out.sessid, 16);
131         SIVAL(req->out.body, 0x18, io->out.unknown3);
132         SSVAL(req->out.body, 0x1C, io->out.unknown4);
133         SIVAL(req->out.body, 0x1E, io->out.unknown5);
134         SIVAL(req->out.body, 0x22, io->out.unknown6);
135         SSVAL(req->out.body, 0x26, io->out.unknown7);
136         push_nttime(req->out.body, 0x28, io->out.current_time);
137         push_nttime(req->out.body, 0x30, io->out.boot_time);
138         status = smb2_push_o16s16_blob(&req->out, 0x38, io->out.secblob);
139         if (!NT_STATUS_IS_OK(status)) {
140                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
141                 talloc_free(req);
142                 return;
143         }
144
145         SIVAL(req->out.body, 0x3C, io->out.unknown9);
146
147         smb2srv_send_reply(req);
148 }
149
150 void smb2srv_negprot_recv(struct smb2srv_request *req)
151 {
152         struct smb2_negprot *io;
153
154         if (req->in.body_size < 0x26) {
155                 smb2srv_send_error(req,  NT_STATUS_FOOBAR);
156                 return;
157         }
158
159         io = talloc(req, struct smb2_negprot);
160         if (!io) {
161                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
162                 talloc_free(req);
163                 return;
164         }
165
166         io->in.unknown1 = SVAL(req->in.body, 0x02);
167         memcpy(io->in.unknown2, req->in.body + 0x04, 0x20);
168         io->in.unknown3 = SVAL(req->in.body, 0x24);
169
170         req->status = smb2srv_negprot_backend(req, io);
171
172         if (req->control_flags & SMB2SRV_REQ_CTRL_FLAG_NOT_REPLY) {
173                 talloc_free(req);
174                 return;
175         }
176         smb2srv_negprot_send(req, io);
177 }