r11786: move all SMB protocol specific stuff to smb_server/smb/
[samba.git] / source4 / smb_server / smb / sesssetup.c
1 /* 
2    Unix SMB/CIFS implementation.
3    handle SMBsessionsetup
4    Copyright (C) Andrew Tridgell                      1998-2001
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
6    Copyright (C) Jim McDonough                        2002
7    Copyright (C) Luke Howard                          2003
8    Copyright (C) Stefan Metzmacher                    2005
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "version.h"
27 #include "auth/auth.h"
28 #include "smb_server/smb_server.h"
29 #include "smbd/service_stream.h"
30 #include "libcli/nbt/libnbt.h"
31
32 /*
33   setup the OS, Lanman and domain portions of a session setup reply
34 */
35 static void sesssetup_common_strings(struct smbsrv_request *req,
36                                      char **os, char **lanman, char **domain)
37 {
38         (*os) = talloc_asprintf(req, "Unix");
39         (*lanman) = talloc_asprintf(req, "Samba %s", SAMBA_VERSION_STRING);
40         (*domain) = talloc_asprintf(req, "%s", lp_workgroup());
41 }
42
43
44 /*
45   handler for old style session setup
46 */
47 static NTSTATUS sesssetup_old(struct smbsrv_request *req, union smb_sesssetup *sess)
48 {
49         NTSTATUS status;
50         struct auth_usersupplied_info *user_info = NULL;
51         struct auth_serversupplied_info *server_info = NULL;
52         struct auth_session_info *session_info;
53         struct smbsrv_session *smb_sess;
54         const char *remote_machine = NULL;
55
56         sess->old.out.vuid = 0;
57         sess->old.out.action = 0;
58
59         if (!req->smb_conn->negotiate.done_sesssetup) {
60                 req->smb_conn->negotiate.max_send = sess->old.in.bufsize;
61         }
62         
63         if (req->smb_conn->negotiate.calling_name) {
64                 remote_machine = req->smb_conn->negotiate.calling_name->name;
65         }
66         
67         if (!remote_machine) {
68                 remote_machine = socket_get_peer_addr(req->smb_conn->connection->socket, req);
69         }
70
71         user_info = talloc(req, struct auth_usersupplied_info);
72         NT_STATUS_HAVE_NO_MEMORY(user_info);
73         
74         user_info->mapped_state = False;
75         user_info->logon_parameters = 0;
76         user_info->flags = 0;
77         user_info->client.account_name = sess->old.in.user;
78         user_info->client.domain_name = sess->old.in.domain;
79         user_info->workstation_name = remote_machine;
80         user_info->remote_host = socket_get_peer_addr(req->smb_conn->connection->socket, user_info);
81         
82         user_info->password_state = AUTH_PASSWORD_RESPONSE;
83         user_info->password.response.lanman = sess->old.in.password;
84         user_info->password.response.lanman.data = talloc_steal(user_info, sess->old.in.password.data);
85         user_info->password.response.nt = data_blob(NULL, 0);
86
87         status = auth_check_password(req->smb_conn->negotiate.auth_context,
88                                      req, user_info, &server_info);
89         if (!NT_STATUS_IS_OK(status)) {
90                 return auth_nt_status_squash(status);
91         }
92
93         /* This references server_info into session_info */
94         status = auth_generate_session_info(req, server_info, &session_info);
95         if (!NT_STATUS_IS_OK(status)) {
96                 return auth_nt_status_squash(status);
97         }
98
99         /* allocate a new session */
100         smb_sess = smbsrv_session_new(req->smb_conn, NULL);
101         if (!smb_sess) {
102                 return NT_STATUS_ACCESS_DENIED;
103         }
104
105         /* Ensure this is marked as a 'real' vuid, not one
106          * simply valid for the session setup leg */
107         status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
108         if (!NT_STATUS_IS_OK(status)) {
109                 return auth_nt_status_squash(status);
110         }
111
112         /* To correctly process any AndX packet (like a tree connect)
113          * we need to fill in the session on the request here */
114         req->session = smb_sess;
115         sess->old.out.vuid = smb_sess->vuid;
116
117         sesssetup_common_strings(req, 
118                                  &sess->old.out.os,
119                                  &sess->old.out.lanman,
120                                  &sess->old.out.domain);
121
122         return NT_STATUS_OK;
123 }
124
125
126 /*
127   handler for NT1 style session setup
128 */
129 static NTSTATUS sesssetup_nt1(struct smbsrv_request *req, union smb_sesssetup *sess)
130 {
131         NTSTATUS status;
132         struct auth_context *auth_context;
133         struct auth_usersupplied_info *user_info = NULL;
134         struct auth_serversupplied_info *server_info = NULL;
135         struct auth_session_info *session_info;
136         struct smbsrv_session *smb_sess;
137         const char *remote_machine = NULL;
138         
139         sess->nt1.out.vuid = 0;
140         sess->nt1.out.action = 0;
141
142         if (!req->smb_conn->negotiate.done_sesssetup) {
143                 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
144                 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
145         }
146
147         if (req->smb_conn->negotiate.spnego_negotiated) {
148                 if (sess->nt1.in.user && *sess->nt1.in.user) {
149                         /* We can't accept a normal login, because we
150                          * don't have a challenge */
151                         return NT_STATUS_LOGON_FAILURE;
152                 }
153
154                 /* TODO: should we use just "anonymous" here? */
155                 status = auth_context_create(req, lp_auth_methods(), 
156                                              &auth_context, 
157                                              req->smb_conn->connection->event.ctx);
158                 NT_STATUS_NOT_OK_RETURN(status);
159         } else {
160                 auth_context = req->smb_conn->negotiate.auth_context;
161         }
162
163         if (req->smb_conn->negotiate.calling_name) {
164                 remote_machine = req->smb_conn->negotiate.calling_name->name;
165         }
166
167         if (!remote_machine) {
168                 remote_machine = socket_get_peer_addr(req->smb_conn->connection->socket, req);
169         }
170         
171         user_info = talloc(req, struct auth_usersupplied_info);
172         NT_STATUS_HAVE_NO_MEMORY(user_info);
173         
174         user_info->mapped_state = False;
175         user_info->logon_parameters = 0;
176         user_info->flags = 0;
177         user_info->client.account_name = sess->nt1.in.user;
178         user_info->client.domain_name = sess->nt1.in.domain;
179         user_info->workstation_name = remote_machine;
180         user_info->remote_host = socket_get_peer_addr(req->smb_conn->connection->socket, user_info);
181         
182         user_info->password_state = AUTH_PASSWORD_RESPONSE;
183         user_info->password.response.lanman = sess->nt1.in.password1;
184         user_info->password.response.lanman.data = talloc_steal(user_info, sess->nt1.in.password1.data);
185         user_info->password.response.nt = sess->nt1.in.password2;
186         user_info->password.response.nt.data = talloc_steal(user_info, sess->nt1.in.password2.data);
187
188         status = auth_check_password(auth_context, req, user_info, &server_info);
189         if (!NT_STATUS_IS_OK(status)) {
190                 return auth_nt_status_squash(status);
191         }
192
193         /* This references server_info into session_info */
194         status = auth_generate_session_info(req, server_info, &session_info);
195         if (!NT_STATUS_IS_OK(status)) {
196                 return auth_nt_status_squash(status);
197         }
198
199         /* allocate a new session */
200         smb_sess = smbsrv_session_new(req->smb_conn, NULL);
201         if (!smb_sess) {
202                 return NT_STATUS_ACCESS_DENIED;
203         }
204
205         /* Ensure this is marked as a 'real' vuid, not one
206          * simply valid for the session setup leg */
207         status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
208         if (!NT_STATUS_IS_OK(status)) {
209                 return auth_nt_status_squash(status);
210         }
211
212         /* To correctly process any AndX packet (like a tree connect)
213          * we need to fill in the session on the request here */
214         req->session = smb_sess;
215         sess->nt1.out.vuid = smb_sess->vuid;
216
217         sesssetup_common_strings(req, 
218                                  &sess->nt1.out.os,
219                                  &sess->nt1.out.lanman,
220                                  &sess->nt1.out.domain);
221
222         if (!session_info->server_info->authenticated) {
223                 return NT_STATUS_OK;
224         }
225
226         if (!srv_setup_signing(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2)) {
227                 /* Already signing, or disabled */
228                 return NT_STATUS_OK;
229         }
230
231         /* Force check of the request packet, now we know the session key */
232         req_signing_check_incoming(req);
233 /* TODO: why don't we check the result here? */
234
235         /* Unfortunetly win2k3 as a client doesn't sign the request
236          * packet here, so we have to force signing to start again */
237
238         srv_signing_restart(req->smb_conn,  &session_info->session_key, &sess->nt1.in.password2);
239
240         return NT_STATUS_OK;
241 }
242
243
244 /*
245   handler for SPNEGO style session setup
246 */
247 static NTSTATUS sesssetup_spnego(struct smbsrv_request *req, union smb_sesssetup *sess)
248 {
249         NTSTATUS status = NT_STATUS_ACCESS_DENIED;
250         struct smbsrv_session *smb_sess;
251         struct gensec_security *gensec_ctx;
252         struct auth_session_info *session_info = NULL;
253         uint16_t vuid;
254
255         sess->spnego.out.vuid = 0;
256         sess->spnego.out.action = 0;
257
258         sesssetup_common_strings(req, 
259                                  &sess->spnego.out.os,
260                                  &sess->spnego.out.lanman,
261                                  &sess->spnego.out.workgroup);
262
263         if (!req->smb_conn->negotiate.done_sesssetup) {
264                 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
265                 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
266         }
267
268         vuid = SVAL(req->in.hdr,HDR_UID);
269         smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
270         if (smb_sess) {
271                 gensec_ctx = smb_sess->gensec_ctx;
272         } else {
273                 status = gensec_server_start(req, &gensec_ctx,
274                                              req->smb_conn->connection->event.ctx);
275                 if (!NT_STATUS_IS_OK(status)) {
276                         DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
277                         return status;
278                 }
279
280                 gensec_set_credentials(gensec_ctx, req->smb_conn->negotiate.server_credentials);
281
282                 gensec_set_target_service(gensec_ctx, "cifs");
283
284                 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
285
286                 status = gensec_start_mech_by_oid(gensec_ctx, GENSEC_OID_SPNEGO);
287                 if (!NT_STATUS_IS_OK(status)) {
288                         DEBUG(1, ("Failed to start GENSEC SPNEGO server code: %s\n", nt_errstr(status)));
289                         return status;
290                 }
291
292                 smb_sess = smbsrv_session_new(req->smb_conn, gensec_ctx);
293                 if (!smb_sess) {
294                         return NT_STATUS_ACCESS_DENIED;
295                 }
296         }
297
298         status = gensec_update(gensec_ctx, req, sess->spnego.in.secblob, &sess->spnego.out.secblob);
299         if (NT_STATUS_IS_OK(status)) {
300                 DATA_BLOB session_key;
301                 
302                 status = gensec_session_info(gensec_ctx, &session_info);
303                 if (!NT_STATUS_IS_OK(status)) {
304                         talloc_free(smb_sess);
305                         return status;
306                 }
307                 
308                 status = gensec_session_key(gensec_ctx, 
309                                             &session_key);
310 /* TODO: what if getting the session key failed? */
311                 if (NT_STATUS_IS_OK(status) 
312                     && session_info->server_info->authenticated
313                     && srv_setup_signing(req->smb_conn, &session_key, NULL)) {
314                         /* Force check of the request packet, now we know the session key */
315                         req_signing_check_incoming(req);
316
317                         srv_signing_restart(req->smb_conn, &session_key, NULL);
318                 }
319
320         } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
321         } else {
322                 status = auth_nt_status_squash(status);
323                 
324                 /* This invalidates the VUID of the failed login */
325                 talloc_free(smb_sess);
326                 return status;
327         }
328                 
329         if (NT_STATUS_IS_OK(status)) {
330                 /* Ensure this is marked as a 'real' vuid, not one
331                  * simply valid for the session setup leg */
332                 status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
333                 if (!NT_STATUS_IS_OK(status)) {
334                         return auth_nt_status_squash(status);
335                 }
336                 req->session = smb_sess;
337         }
338         sess->spnego.out.vuid = smb_sess->vuid;
339
340         return status;
341 }
342
343 /*
344   backend for sessionsetup call - this takes all 3 variants of the call
345 */
346 NTSTATUS sesssetup_backend(struct smbsrv_request *req, 
347                            union smb_sesssetup *sess)
348 {
349         NTSTATUS status = NT_STATUS_INVALID_LEVEL;
350
351         switch (sess->old.level) {
352                 case RAW_SESSSETUP_OLD:
353                         status = sesssetup_old(req, sess);
354                         break;
355                 case RAW_SESSSETUP_NT1:
356                         status = sesssetup_nt1(req, sess);
357                         break;
358                 case RAW_SESSSETUP_SPNEGO:
359                         status = sesssetup_spnego(req, sess);
360                         break;
361         }
362
363         if (NT_STATUS_IS_OK(status)) {
364                 req->smb_conn->negotiate.done_sesssetup = True;
365         }
366
367         return status;
368 }
369
370