4bafc6cbf4f9bbeb68d214087120e137f6caad8b
[bbaumbach/samba-autobuild/.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
31 /*
32   setup the OS, Lanman and domain portions of a session setup reply
33 */
34 static void sesssetup_common_strings(struct smbsrv_request *req,
35                                      char **os, char **lanman, char **domain)
36 {
37         (*os) = talloc_asprintf(req, "Unix");
38         (*lanman) = talloc_asprintf(req, "Samba %s", SAMBA_VERSION_STRING);
39         (*domain) = talloc_asprintf(req, "%s", lp_workgroup());
40 }
41
42
43 /*
44   handler for old style session setup
45 */
46 static NTSTATUS sesssetup_old(struct smbsrv_request *req, union smb_sesssetup *sess)
47 {
48         NTSTATUS status;
49         struct auth_usersupplied_info *user_info = NULL;
50         struct auth_serversupplied_info *server_info = NULL;
51         struct auth_session_info *session_info;
52         struct smbsrv_session *smb_sess;
53         const char *remote_machine = NULL;
54
55         sess->old.out.vuid = 0;
56         sess->old.out.action = 0;
57
58         if (!req->smb_conn->negotiate.done_sesssetup) {
59                 req->smb_conn->negotiate.max_send = sess->old.in.bufsize;
60         }
61         
62         if (req->smb_conn->negotiate.calling_name) {
63                 remote_machine = req->smb_conn->negotiate.calling_name->name;
64         }
65         
66         if (!remote_machine) {
67                 remote_machine = socket_get_peer_addr(req->smb_conn->connection->socket, req);
68         }
69
70         user_info = talloc(req, struct auth_usersupplied_info);
71         NT_STATUS_HAVE_NO_MEMORY(user_info);
72         
73         user_info->mapped_state = False;
74         user_info->logon_parameters = 0;
75         user_info->flags = 0;
76         user_info->client.account_name = sess->old.in.user;
77         user_info->client.domain_name = sess->old.in.domain;
78         user_info->workstation_name = remote_machine;
79         user_info->remote_host = socket_get_peer_addr(req->smb_conn->connection->socket, user_info);
80         
81         user_info->password_state = AUTH_PASSWORD_RESPONSE;
82         user_info->password.response.lanman = sess->old.in.password;
83         user_info->password.response.lanman.data = talloc_steal(user_info, sess->old.in.password.data);
84         user_info->password.response.nt = data_blob(NULL, 0);
85
86         status = auth_check_password(req->smb_conn->negotiate.auth_context,
87                                      req, user_info, &server_info);
88         if (!NT_STATUS_IS_OK(status)) {
89                 return auth_nt_status_squash(status);
90         }
91
92         /* This references server_info into session_info */
93         status = auth_generate_session_info(req, server_info, &session_info);
94         if (!NT_STATUS_IS_OK(status)) {
95                 return auth_nt_status_squash(status);
96         }
97
98         /* allocate a new session */
99         smb_sess = smbsrv_session_new(req->smb_conn, NULL);
100         if (!smb_sess) {
101                 return NT_STATUS_ACCESS_DENIED;
102         }
103
104         /* Ensure this is marked as a 'real' vuid, not one
105          * simply valid for the session setup leg */
106         status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
107         if (!NT_STATUS_IS_OK(status)) {
108                 return auth_nt_status_squash(status);
109         }
110
111         /* To correctly process any AndX packet (like a tree connect)
112          * we need to fill in the session on the request here */
113         req->session = smb_sess;
114         sess->old.out.vuid = smb_sess->vuid;
115
116         sesssetup_common_strings(req, 
117                                  &sess->old.out.os,
118                                  &sess->old.out.lanman,
119                                  &sess->old.out.domain);
120
121         return NT_STATUS_OK;
122 }
123
124
125 /*
126   handler for NT1 style session setup
127 */
128 static NTSTATUS sesssetup_nt1(struct smbsrv_request *req, union smb_sesssetup *sess)
129 {
130         NTSTATUS status;
131         struct auth_context *auth_context;
132         struct auth_usersupplied_info *user_info = NULL;
133         struct auth_serversupplied_info *server_info = NULL;
134         struct auth_session_info *session_info;
135         struct smbsrv_session *smb_sess;
136         const char *remote_machine = NULL;
137         
138         sess->nt1.out.vuid = 0;
139         sess->nt1.out.action = 0;
140
141         if (!req->smb_conn->negotiate.done_sesssetup) {
142                 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
143                 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
144         }
145
146         if (req->smb_conn->negotiate.spnego_negotiated) {
147                 if (sess->nt1.in.user && *sess->nt1.in.user) {
148                         /* We can't accept a normal login, because we
149                          * don't have a challenge */
150                         return NT_STATUS_LOGON_FAILURE;
151                 }
152
153                 /* TODO: should we use just "anonymous" here? */
154                 status = auth_context_create(req, lp_auth_methods(), 
155                                              &auth_context, 
156                                              req->smb_conn->connection->event.ctx);
157                 NT_STATUS_NOT_OK_RETURN(status);
158         } else {
159                 auth_context = req->smb_conn->negotiate.auth_context;
160         }
161
162         if (req->smb_conn->negotiate.calling_name) {
163                 remote_machine = req->smb_conn->negotiate.calling_name->name;
164         }
165
166         if (!remote_machine) {
167                 remote_machine = socket_get_peer_addr(req->smb_conn->connection->socket, req);
168         }
169         
170         user_info = talloc(req, struct auth_usersupplied_info);
171         NT_STATUS_HAVE_NO_MEMORY(user_info);
172         
173         user_info->mapped_state = False;
174         user_info->logon_parameters = 0;
175         user_info->flags = 0;
176         user_info->client.account_name = sess->nt1.in.user;
177         user_info->client.domain_name = sess->nt1.in.domain;
178         user_info->workstation_name = remote_machine;
179         user_info->remote_host = socket_get_peer_addr(req->smb_conn->connection->socket, user_info);
180         
181         user_info->password_state = AUTH_PASSWORD_RESPONSE;
182         user_info->password.response.lanman = sess->nt1.in.password1;
183         user_info->password.response.lanman.data = talloc_steal(user_info, sess->nt1.in.password1.data);
184         user_info->password.response.nt = sess->nt1.in.password2;
185         user_info->password.response.nt.data = talloc_steal(user_info, sess->nt1.in.password2.data);
186
187         status = auth_check_password(auth_context, req, user_info, &server_info);
188         if (!NT_STATUS_IS_OK(status)) {
189                 return auth_nt_status_squash(status);
190         }
191
192         /* This references server_info into session_info */
193         status = auth_generate_session_info(req, server_info, &session_info);
194         if (!NT_STATUS_IS_OK(status)) {
195                 return auth_nt_status_squash(status);
196         }
197
198         /* allocate a new session */
199         smb_sess = smbsrv_session_new(req->smb_conn, NULL);
200         if (!smb_sess) {
201                 return NT_STATUS_ACCESS_DENIED;
202         }
203
204         /* Ensure this is marked as a 'real' vuid, not one
205          * simply valid for the session setup leg */
206         status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
207         if (!NT_STATUS_IS_OK(status)) {
208                 return auth_nt_status_squash(status);
209         }
210
211         /* To correctly process any AndX packet (like a tree connect)
212          * we need to fill in the session on the request here */
213         req->session = smb_sess;
214         sess->nt1.out.vuid = smb_sess->vuid;
215
216         sesssetup_common_strings(req, 
217                                  &sess->nt1.out.os,
218                                  &sess->nt1.out.lanman,
219                                  &sess->nt1.out.domain);
220
221         if (!session_info->server_info->authenticated) {
222                 return NT_STATUS_OK;
223         }
224
225         if (!srv_setup_signing(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2)) {
226                 /* Already signing, or disabled */
227                 return NT_STATUS_OK;
228         }
229
230         /* Force check of the request packet, now we know the session key */
231         req_signing_check_incoming(req);
232 /* TODO: why don't we check the result here? */
233
234         /* Unfortunetly win2k3 as a client doesn't sign the request
235          * packet here, so we have to force signing to start again */
236
237         srv_signing_restart(req->smb_conn,  &session_info->session_key, &sess->nt1.in.password2);
238
239         return NT_STATUS_OK;
240 }
241
242
243 /*
244   handler for SPNEGO style session setup
245 */
246 static NTSTATUS sesssetup_spnego(struct smbsrv_request *req, union smb_sesssetup *sess)
247 {
248         NTSTATUS status;
249         NTSTATUS skey_status;
250         struct smbsrv_session *smb_sess = NULL;
251         struct auth_session_info *session_info = NULL;
252         DATA_BLOB session_key;
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
270         /* lookup an existing session */
271         smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
272         if (!smb_sess) {
273                 struct gensec_security *gensec_ctx;
274
275                 status = gensec_server_start(req, &gensec_ctx,
276                                              req->smb_conn->connection->event.ctx);
277                 if (!NT_STATUS_IS_OK(status)) {
278                         DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
279                         return status;
280                 }
281
282                 gensec_set_credentials(gensec_ctx, req->smb_conn->negotiate.server_credentials);
283
284                 gensec_set_target_service(gensec_ctx, "cifs");
285
286                 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
287
288                 status = gensec_start_mech_by_oid(gensec_ctx, GENSEC_OID_SPNEGO);
289                 if (!NT_STATUS_IS_OK(status)) {
290                         DEBUG(1, ("Failed to start GENSEC SPNEGO server code: %s\n", nt_errstr(status)));
291                         return status;
292                 }
293
294                 /* allocate a new session */
295                 smb_sess = smbsrv_session_new(req->smb_conn, gensec_ctx);
296         }
297
298         if (!smb_sess) {
299                 return NT_STATUS_ACCESS_DENIED;
300         }
301
302         if (!smb_sess->gensec_ctx) {
303                 status = NT_STATUS_INTERNAL_ERROR;
304                 DEBUG(1, ("Internal ERROR: no gensec_ctx on session: %s\n", nt_errstr(status)));
305                 goto failed;
306         }
307
308         status = gensec_update(smb_sess->gensec_ctx, req, sess->spnego.in.secblob, &sess->spnego.out.secblob);
309         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
310                 sess->spnego.out.vuid = smb_sess->vuid;
311                 return status;
312         } else if (!NT_STATUS_IS_OK(status)) {
313                 goto failed;
314         }
315
316         status = gensec_session_info(smb_sess->gensec_ctx, &session_info);
317         if (!NT_STATUS_IS_OK(status)) {
318                 goto failed;
319         }
320
321         skey_status = gensec_session_key(smb_sess->gensec_ctx, &session_key);
322         if (NT_STATUS_IS_OK(skey_status) &&
323             session_info->server_info->authenticated &&
324             srv_setup_signing(req->smb_conn, &session_key, NULL)) {
325                 /* Force check of the request packet, now we know the session key */
326                 req_signing_check_incoming(req);
327
328                 srv_signing_restart(req->smb_conn, &session_key, NULL);
329         }
330
331         /* Ensure this is marked as a 'real' vuid, not one
332          * simply valid for the session setup leg */
333         status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
334         if (!NT_STATUS_IS_OK(status)) {
335                 goto failed;
336         }
337         req->session = smb_sess;
338
339         sess->spnego.out.vuid = smb_sess->vuid;
340         return NT_STATUS_OK;
341
342 failed:
343         talloc_free(smb_sess);
344         return auth_nt_status_squash(status);
345 }
346
347 /*
348   backend for sessionsetup call - this takes all 3 variants of the call
349 */
350 NTSTATUS sesssetup_backend(struct smbsrv_request *req, 
351                            union smb_sesssetup *sess)
352 {
353         NTSTATUS status = NT_STATUS_INVALID_LEVEL;
354
355         switch (sess->old.level) {
356                 case RAW_SESSSETUP_OLD:
357                         status = sesssetup_old(req, sess);
358                         break;
359                 case RAW_SESSSETUP_NT1:
360                         status = sesssetup_nt1(req, sess);
361                         break;
362                 case RAW_SESSSETUP_SPNEGO:
363                         status = sesssetup_spnego(req, sess);
364                         break;
365         }
366
367         if (NT_STATUS_IS_OK(status)) {
368                 req->smb_conn->negotiate.done_sesssetup = True;
369         }
370
371         return status;
372 }
373
374