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