keep obsolete file in samba4 source directory.
[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 3 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, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "version.h"
27 #include "auth/credentials/credentials.h"
28 #include "auth/gensec/gensec.h"
29 #include "auth/auth.h"
30 #include "smb_server/smb_server.h"
31 #include "smbd/service_stream.h"
32 #include "librpc/gen_ndr/nbt.h"
33 #include "param/param.h"
34
35 /*
36   setup the OS, Lanman and domain portions of a session setup reply
37 */
38 static void sesssetup_common_strings(struct smbsrv_request *req,
39                                      char **os, char **lanman, char **domain)
40 {
41         (*os) = talloc_asprintf(req, "Unix");
42         (*lanman) = talloc_asprintf(req, "Samba %s", SAMBA_VERSION_STRING);
43         (*domain) = talloc_asprintf(req, "%s", 
44                                     lp_workgroup(req->smb_conn->lp_ctx));
45 }
46
47 static void smbsrv_sesssetup_backend_send(struct smbsrv_request *req,
48                                           union smb_sesssetup *sess,
49                                           NTSTATUS status)
50 {
51         if (NT_STATUS_IS_OK(status)) {
52                 req->smb_conn->negotiate.done_sesssetup = true;
53                 /* we need to keep the session long term */
54                 req->session = talloc_steal(req->smb_conn, req->session);
55         }
56         smbsrv_reply_sesssetup_send(req, sess, status);
57 }
58
59 static void sesssetup_old_send(struct auth_check_password_request *areq,
60                                void *private_data)
61 {
62         struct smbsrv_request *req = talloc_get_type(private_data, struct smbsrv_request);
63         union smb_sesssetup *sess = talloc_get_type(req->io_ptr, union smb_sesssetup);
64         struct auth_serversupplied_info *server_info = NULL;
65         struct auth_session_info *session_info;
66         struct smbsrv_session *smb_sess;
67         NTSTATUS status;
68
69         status = auth_check_password_recv(areq, req, &server_info);
70         if (!NT_STATUS_IS_OK(status)) goto failed;
71
72         /* This references server_info into session_info */
73         status = auth_generate_session_info(req, req->smb_conn->connection->event.ctx, req->smb_conn->lp_ctx, 
74                                             server_info, &session_info);
75         if (!NT_STATUS_IS_OK(status)) goto failed;
76
77         /* allocate a new session */
78         smb_sess = smbsrv_session_new(req->smb_conn, req, NULL);
79         if (!smb_sess) {
80                 status = NT_STATUS_INSUFFICIENT_RESOURCES;
81                 goto failed;
82         }
83
84         /* Ensure this is marked as a 'real' vuid, not one
85          * simply valid for the session setup leg */
86         status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
87         if (!NT_STATUS_IS_OK(status)) goto failed;
88
89         /* To correctly process any AndX packet (like a tree connect)
90          * we need to fill in the session on the request here */
91         req->session = smb_sess;
92         sess->old.out.vuid = smb_sess->vuid;
93
94 failed:
95         status = auth_nt_status_squash(status);
96         smbsrv_sesssetup_backend_send(req, sess, status);
97 }
98
99 /*
100   handler for old style session setup
101 */
102 static void sesssetup_old(struct smbsrv_request *req, union smb_sesssetup *sess)
103 {
104         struct auth_usersupplied_info *user_info = NULL;
105         struct socket_address *remote_address;
106         const char *remote_machine = NULL;
107
108         sess->old.out.vuid = 0;
109         sess->old.out.action = 0;
110
111         sesssetup_common_strings(req, 
112                                  &sess->old.out.os,
113                                  &sess->old.out.lanman,
114                                  &sess->old.out.domain);
115
116         if (!req->smb_conn->negotiate.done_sesssetup) {
117                 req->smb_conn->negotiate.max_send = sess->old.in.bufsize;
118         }
119
120         if (req->smb_conn->negotiate.calling_name) {
121                 remote_machine = req->smb_conn->negotiate.calling_name->name;
122         }
123         
124         remote_address = socket_get_peer_addr(req->smb_conn->connection->socket, req);
125         if (!remote_address) goto nomem;
126
127         if (!remote_machine) {
128                 remote_machine = remote_address->addr;
129         }
130
131         user_info = talloc(req, struct auth_usersupplied_info);
132         if (!user_info) goto nomem;
133         
134         user_info->mapped_state = false;
135         user_info->logon_parameters = 0;
136         user_info->flags = 0;
137         user_info->client.account_name = sess->old.in.user;
138         user_info->client.domain_name = sess->old.in.domain;
139         user_info->workstation_name = remote_machine;
140         user_info->remote_host = talloc_steal(user_info, remote_address);
141         
142         user_info->password_state = AUTH_PASSWORD_RESPONSE;
143         user_info->password.response.lanman = sess->old.in.password;
144         user_info->password.response.lanman.data = talloc_steal(user_info, sess->old.in.password.data);
145         user_info->password.response.nt = data_blob(NULL, 0);
146
147         auth_check_password_send(req->smb_conn->negotiate.auth_context, user_info,
148                                  sesssetup_old_send, req);
149         return;
150
151 nomem:
152         smbsrv_sesssetup_backend_send(req, sess, NT_STATUS_NO_MEMORY);
153 }
154
155 static void sesssetup_nt1_send(struct auth_check_password_request *areq,
156                                void *private_data)
157 {
158         struct smbsrv_request *req = talloc_get_type(private_data, struct smbsrv_request);
159         union smb_sesssetup *sess = talloc_get_type(req->io_ptr, union smb_sesssetup);
160         struct auth_serversupplied_info *server_info = NULL;
161         struct auth_session_info *session_info;
162         struct smbsrv_session *smb_sess;
163         NTSTATUS status;
164
165         status = auth_check_password_recv(areq, req, &server_info);
166         if (!NT_STATUS_IS_OK(status)) goto failed;
167
168         /* This references server_info into session_info */
169         status = auth_generate_session_info(req, req->smb_conn->connection->event.ctx, 
170                                             req->smb_conn->lp_ctx, 
171                                             server_info, &session_info);
172         if (!NT_STATUS_IS_OK(status)) goto failed;
173
174         /* allocate a new session */
175         smb_sess = smbsrv_session_new(req->smb_conn, req, NULL);
176         if (!smb_sess) {
177                 status = NT_STATUS_INSUFFICIENT_RESOURCES;
178                 goto failed;
179         }
180
181         /* Ensure this is marked as a 'real' vuid, not one
182          * simply valid for the session setup leg */
183         status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
184         if (!NT_STATUS_IS_OK(status)) goto failed;
185
186         /* To correctly process any AndX packet (like a tree connect)
187          * we need to fill in the session on the request here */
188         req->session = smb_sess;
189         sess->nt1.out.vuid = smb_sess->vuid;
190
191         if (!smbsrv_setup_signing(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2)) {
192                 /* Already signing, or disabled */
193                 goto done;
194         }
195
196         /* Force check of the request packet, now we know the session key */
197         smbsrv_signing_check_incoming(req);
198 /* TODO: why don't we check the result here? */
199
200         /* Unfortunetly win2k3 as a client doesn't sign the request
201          * packet here, so we have to force signing to start again */
202
203         smbsrv_signing_restart(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2, 
204                                session_info->server_info->authenticated);
205
206 done:
207         status = NT_STATUS_OK;
208 failed:
209         status = auth_nt_status_squash(status);
210         smbsrv_sesssetup_backend_send(req, sess, status);
211 }
212
213 /*
214   handler for NT1 style session setup
215 */
216 static void sesssetup_nt1(struct smbsrv_request *req, union smb_sesssetup *sess)
217 {
218         NTSTATUS status;
219         struct auth_context *auth_context;
220         struct auth_usersupplied_info *user_info = NULL;
221         struct socket_address *remote_address;
222         const char *remote_machine = NULL;
223         
224         sess->nt1.out.vuid = 0;
225         sess->nt1.out.action = 0;
226
227         sesssetup_common_strings(req, 
228                                  &sess->nt1.out.os,
229                                  &sess->nt1.out.lanman,
230                                  &sess->nt1.out.domain);
231
232         if (!req->smb_conn->negotiate.done_sesssetup) {
233                 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
234                 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
235         }
236
237         if (req->smb_conn->negotiate.oid) {
238                 if (sess->nt1.in.user && *sess->nt1.in.user) {
239                         /* We can't accept a normal login, because we
240                          * don't have a challenge */
241                         status = NT_STATUS_LOGON_FAILURE;
242                         goto failed;
243                 }
244
245                 /* TODO: should we use just "anonymous" here? */
246                 status = auth_context_create(req, 
247                                              req->smb_conn->connection->event.ctx,
248                                              req->smb_conn->connection->msg_ctx,
249                                              req->smb_conn->lp_ctx,
250                                              &auth_context);
251                 if (!NT_STATUS_IS_OK(status)) goto failed;
252         } else {
253                 auth_context = req->smb_conn->negotiate.auth_context;
254         }
255
256         if (req->smb_conn->negotiate.calling_name) {
257                 remote_machine = req->smb_conn->negotiate.calling_name->name;
258         }
259
260         remote_address = socket_get_peer_addr(req->smb_conn->connection->socket, req);
261         if (!remote_address) goto nomem;
262
263         if (!remote_machine) {
264                 remote_machine = remote_address->addr;
265         }
266
267         user_info = talloc(req, struct auth_usersupplied_info);
268         if (!user_info) goto nomem;
269
270         user_info->mapped_state = false;
271         user_info->logon_parameters = 0;
272         user_info->flags = 0;
273         user_info->client.account_name = sess->nt1.in.user;
274         user_info->client.domain_name = sess->nt1.in.domain;
275         user_info->workstation_name = remote_machine;
276         user_info->remote_host = talloc_steal(user_info, remote_address);
277         
278         user_info->password_state = AUTH_PASSWORD_RESPONSE;
279         user_info->password.response.lanman = sess->nt1.in.password1;
280         user_info->password.response.lanman.data = talloc_steal(user_info, sess->nt1.in.password1.data);
281         user_info->password.response.nt = sess->nt1.in.password2;
282         user_info->password.response.nt.data = talloc_steal(user_info, sess->nt1.in.password2.data);
283
284         auth_check_password_send(auth_context, user_info,
285                                  sesssetup_nt1_send, req);
286         return;
287
288 nomem:
289         status = NT_STATUS_NO_MEMORY;
290 failed:
291         status = auth_nt_status_squash(status);
292         smbsrv_sesssetup_backend_send(req, sess, status);
293 }
294
295 struct sesssetup_spnego_state {
296         struct smbsrv_request *req;
297         union smb_sesssetup *sess;
298         struct smbsrv_session *smb_sess;
299 };
300
301 static void sesssetup_spnego_send(struct gensec_update_request *greq, void *private_data)
302 {
303         struct sesssetup_spnego_state *s = talloc_get_type(private_data,
304                                            struct sesssetup_spnego_state);
305         struct smbsrv_request *req = s->req;
306         union smb_sesssetup *sess = s->sess;
307         struct smbsrv_session *smb_sess = s->smb_sess;
308         struct auth_session_info *session_info = NULL;
309         NTSTATUS status;
310         NTSTATUS skey_status;
311         DATA_BLOB session_key;
312
313         status = gensec_update_recv(greq, req, &sess->spnego.out.secblob);
314         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
315                 goto done;
316         } else if (!NT_STATUS_IS_OK(status)) {
317                 goto failed;
318         }
319
320         status = gensec_session_info(smb_sess->gensec_ctx, &session_info);
321         if (!NT_STATUS_IS_OK(status)) goto failed;
322
323         skey_status = gensec_session_key(smb_sess->gensec_ctx, &session_key);
324         if (NT_STATUS_IS_OK(skey_status) &&
325             smbsrv_setup_signing(req->smb_conn, &session_key, NULL)) {
326                 /* Force check of the request packet, now we know the session key */
327                 smbsrv_signing_check_incoming(req);
328
329                 smbsrv_signing_restart(req->smb_conn, &session_key, NULL, 
330                                        session_info->server_info->authenticated);
331         }
332
333         /* Ensure this is marked as a 'real' vuid, not one
334          * simply valid for the session setup leg */
335         status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
336         if (!NT_STATUS_IS_OK(status)) goto failed;
337
338         req->session = smb_sess;
339
340 done:
341         sess->spnego.out.vuid = smb_sess->vuid;
342 failed:
343         status = auth_nt_status_squash(status);
344         smbsrv_sesssetup_backend_send(req, sess, status);
345         if (!NT_STATUS_IS_OK(status) && 
346             !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
347                 talloc_free(smb_sess);
348         }
349 }
350
351 /*
352   handler for SPNEGO style session setup
353 */
354 static void sesssetup_spnego(struct smbsrv_request *req, union smb_sesssetup *sess)
355 {
356         NTSTATUS status;
357         struct smbsrv_session *smb_sess = NULL;
358         struct sesssetup_spnego_state *s = NULL;
359         uint16_t vuid;
360
361         sess->spnego.out.vuid = 0;
362         sess->spnego.out.action = 0;
363
364         sesssetup_common_strings(req, 
365                                  &sess->spnego.out.os,
366                                  &sess->spnego.out.lanman,
367                                  &sess->spnego.out.workgroup);
368
369         if (!req->smb_conn->negotiate.done_sesssetup) {
370                 req->smb_conn->negotiate.max_send = sess->spnego.in.bufsize;
371                 req->smb_conn->negotiate.client_caps = sess->spnego.in.capabilities;
372         }
373
374         vuid = SVAL(req->in.hdr,HDR_UID);
375
376         /* lookup an existing session */
377         smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
378         if (!smb_sess) {
379                 struct gensec_security *gensec_ctx;
380
381                 status = gensec_server_start(req,
382                                              req->smb_conn->connection->event.ctx,
383                                              req->smb_conn->lp_ctx,
384                                              req->smb_conn->connection->msg_ctx,
385                                              &gensec_ctx);
386                 if (!NT_STATUS_IS_OK(status)) {
387                         DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
388                         goto failed;
389                 }
390
391                 gensec_set_credentials(gensec_ctx, req->smb_conn->negotiate.server_credentials);
392
393                 gensec_set_target_service(gensec_ctx, "cifs");
394
395                 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
396
397                 status = gensec_start_mech_by_oid(gensec_ctx, req->smb_conn->negotiate.oid);
398                 if (!NT_STATUS_IS_OK(status)) {
399                         DEBUG(1, ("Failed to start GENSEC %s server code: %s\n", 
400                                   gensec_get_name_by_oid(req->smb_conn->negotiate.oid), nt_errstr(status)));
401                         goto failed;
402                 }
403
404                 /* allocate a new session */
405                 smb_sess = smbsrv_session_new(req->smb_conn, req->smb_conn, gensec_ctx);
406                 if (!smb_sess) {
407                         status = NT_STATUS_INSUFFICIENT_RESOURCES;
408                         goto failed;
409                 }
410         }
411
412         if (!smb_sess) {
413                 status = NT_STATUS_ACCESS_DENIED;
414                 goto failed;
415         }
416
417         if (!smb_sess->gensec_ctx) {
418                 status = NT_STATUS_INTERNAL_ERROR;
419                 DEBUG(1, ("Internal ERROR: no gensec_ctx on session: %s\n", nt_errstr(status)));
420                 goto failed;
421         }
422
423         s = talloc(req, struct sesssetup_spnego_state);
424         if (!s) goto nomem;
425         s->req          = req;
426         s->sess         = sess;
427         s->smb_sess     = smb_sess;
428
429         gensec_update_send(smb_sess->gensec_ctx, sess->spnego.in.secblob,
430                            sesssetup_spnego_send, s);
431         return;
432
433 nomem:
434         status = NT_STATUS_NO_MEMORY;
435 failed:
436         talloc_free(smb_sess);
437         status = auth_nt_status_squash(status);
438         smbsrv_sesssetup_backend_send(req, sess, status);
439 }
440
441 /*
442   backend for sessionsetup call - this takes all 3 variants of the call
443 */
444 void smbsrv_sesssetup_backend(struct smbsrv_request *req,
445                               union smb_sesssetup *sess)
446 {
447         switch (sess->old.level) {
448                 case RAW_SESSSETUP_OLD:
449                         sesssetup_old(req, sess);
450                         return;
451
452                 case RAW_SESSSETUP_NT1:
453                         sesssetup_nt1(req, sess);
454                         return;
455
456                 case RAW_SESSSETUP_SPNEGO:
457                         sesssetup_spnego(req, sess);
458                         return;
459
460                 case RAW_SESSSETUP_SMB2:
461                         break;
462         }
463
464         smbsrv_sesssetup_backend_send(req, sess, NT_STATUS_INVALID_LEVEL);
465 }