r26268: Avoid more use of global_loadparm - put lp_ctx in smb_server and wbsrv_connec...
[kai/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->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->lp_ctx, 
170                                             server_info, &session_info);
171         if (!NT_STATUS_IS_OK(status)) goto failed;
172
173         /* allocate a new session */
174         smb_sess = smbsrv_session_new(req->smb_conn, req, NULL);
175         if (!smb_sess) {
176                 status = NT_STATUS_INSUFFICIENT_RESOURCES;
177                 goto failed;
178         }
179
180         /* Ensure this is marked as a 'real' vuid, not one
181          * simply valid for the session setup leg */
182         status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
183         if (!NT_STATUS_IS_OK(status)) goto failed;
184
185         /* To correctly process any AndX packet (like a tree connect)
186          * we need to fill in the session on the request here */
187         req->session = smb_sess;
188         sess->nt1.out.vuid = smb_sess->vuid;
189
190         if (!session_info->server_info->authenticated) {
191                 /* don't try signing as anonymous */
192                 goto done;
193         }
194
195         if (!smbsrv_setup_signing(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2)) {
196                 /* Already signing, or disabled */
197                 goto done;
198         }
199
200         /* Force check of the request packet, now we know the session key */
201         smbsrv_signing_check_incoming(req);
202 /* TODO: why don't we check the result here? */
203
204         /* Unfortunetly win2k3 as a client doesn't sign the request
205          * packet here, so we have to force signing to start again */
206
207         smbsrv_signing_restart(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2);
208
209 done:
210         status = NT_STATUS_OK;
211 failed:
212         status = auth_nt_status_squash(status);
213         smbsrv_sesssetup_backend_send(req, sess, status);
214 }
215
216 /*
217   handler for NT1 style session setup
218 */
219 static void sesssetup_nt1(struct smbsrv_request *req, union smb_sesssetup *sess)
220 {
221         NTSTATUS status;
222         struct auth_context *auth_context;
223         struct auth_usersupplied_info *user_info = NULL;
224         struct socket_address *remote_address;
225         const char *remote_machine = NULL;
226         
227         sess->nt1.out.vuid = 0;
228         sess->nt1.out.action = 0;
229
230         sesssetup_common_strings(req, 
231                                  &sess->nt1.out.os,
232                                  &sess->nt1.out.lanman,
233                                  &sess->nt1.out.domain);
234
235         if (!req->smb_conn->negotiate.done_sesssetup) {
236                 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
237                 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
238         }
239
240         if (req->smb_conn->negotiate.oid) {
241                 if (sess->nt1.in.user && *sess->nt1.in.user) {
242                         /* We can't accept a normal login, because we
243                          * don't have a challenge */
244                         status = NT_STATUS_LOGON_FAILURE;
245                         goto failed;
246                 }
247
248                 /* TODO: should we use just "anonymous" here? */
249                 status = auth_context_create(req, 
250                                              req->smb_conn->connection->event.ctx,
251                                              req->smb_conn->connection->msg_ctx,
252                                              req->smb_conn->lp_ctx,
253                                              &auth_context);
254                 if (!NT_STATUS_IS_OK(status)) goto failed;
255         } else {
256                 auth_context = req->smb_conn->negotiate.auth_context;
257         }
258
259         if (req->smb_conn->negotiate.calling_name) {
260                 remote_machine = req->smb_conn->negotiate.calling_name->name;
261         }
262
263         remote_address = socket_get_peer_addr(req->smb_conn->connection->socket, req);
264         if (!remote_address) goto nomem;
265
266         if (!remote_machine) {
267                 remote_machine = remote_address->addr;
268         }
269
270         user_info = talloc(req, struct auth_usersupplied_info);
271         if (!user_info) goto nomem;
272
273         user_info->mapped_state = false;
274         user_info->logon_parameters = 0;
275         user_info->flags = 0;
276         user_info->client.account_name = sess->nt1.in.user;
277         user_info->client.domain_name = sess->nt1.in.domain;
278         user_info->workstation_name = remote_machine;
279         user_info->remote_host = talloc_steal(user_info, remote_address);
280         
281         user_info->password_state = AUTH_PASSWORD_RESPONSE;
282         user_info->password.response.lanman = sess->nt1.in.password1;
283         user_info->password.response.lanman.data = talloc_steal(user_info, sess->nt1.in.password1.data);
284         user_info->password.response.nt = sess->nt1.in.password2;
285         user_info->password.response.nt.data = talloc_steal(user_info, sess->nt1.in.password2.data);
286
287         auth_check_password_send(auth_context, user_info,
288                                  sesssetup_nt1_send, req);
289         return;
290
291 nomem:
292         status = NT_STATUS_NO_MEMORY;
293 failed:
294         status = auth_nt_status_squash(status);
295         smbsrv_sesssetup_backend_send(req, sess, status);
296 }
297
298 struct sesssetup_spnego_state {
299         struct smbsrv_request *req;
300         union smb_sesssetup *sess;
301         struct smbsrv_session *smb_sess;
302 };
303
304 static void sesssetup_spnego_send(struct gensec_update_request *greq, void *private_data)
305 {
306         struct sesssetup_spnego_state *s = talloc_get_type(private_data,
307                                            struct sesssetup_spnego_state);
308         struct smbsrv_request *req = s->req;
309         union smb_sesssetup *sess = s->sess;
310         struct smbsrv_session *smb_sess = s->smb_sess;
311         struct auth_session_info *session_info = NULL;
312         NTSTATUS status;
313         NTSTATUS skey_status;
314         DATA_BLOB session_key;
315
316         status = gensec_update_recv(greq, req, &sess->spnego.out.secblob);
317         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
318                 goto done;
319         } else if (!NT_STATUS_IS_OK(status)) {
320                 goto failed;
321         }
322
323         status = gensec_session_info(smb_sess->gensec_ctx, &session_info);
324         if (!NT_STATUS_IS_OK(status)) goto failed;
325
326         skey_status = gensec_session_key(smb_sess->gensec_ctx, &session_key);
327         if (NT_STATUS_IS_OK(skey_status) &&
328             session_info->server_info->authenticated &&
329             smbsrv_setup_signing(req->smb_conn, &session_key, NULL)) {
330                 /* Force check of the request packet, now we know the session key */
331                 smbsrv_signing_check_incoming(req);
332
333                 smbsrv_signing_restart(req->smb_conn, &session_key, NULL);
334         }
335
336         /* Ensure this is marked as a 'real' vuid, not one
337          * simply valid for the session setup leg */
338         status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
339         if (!NT_STATUS_IS_OK(status)) goto failed;
340
341         req->session = smb_sess;
342
343 done:
344         sess->spnego.out.vuid = smb_sess->vuid;
345 failed:
346         status = auth_nt_status_squash(status);
347         smbsrv_sesssetup_backend_send(req, sess, status);
348         if (!NT_STATUS_IS_OK(status) && 
349             !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
350                 talloc_free(smb_sess);
351         }
352 }
353
354 /*
355   handler for SPNEGO style session setup
356 */
357 static void sesssetup_spnego(struct smbsrv_request *req, union smb_sesssetup *sess)
358 {
359         NTSTATUS status;
360         struct smbsrv_session *smb_sess = NULL;
361         struct sesssetup_spnego_state *s = NULL;
362         uint16_t vuid;
363
364         sess->spnego.out.vuid = 0;
365         sess->spnego.out.action = 0;
366
367         sesssetup_common_strings(req, 
368                                  &sess->spnego.out.os,
369                                  &sess->spnego.out.lanman,
370                                  &sess->spnego.out.workgroup);
371
372         if (!req->smb_conn->negotiate.done_sesssetup) {
373                 req->smb_conn->negotiate.max_send = sess->spnego.in.bufsize;
374                 req->smb_conn->negotiate.client_caps = sess->spnego.in.capabilities;
375         }
376
377         vuid = SVAL(req->in.hdr,HDR_UID);
378
379         /* lookup an existing session */
380         smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
381         if (!smb_sess) {
382                 struct gensec_security *gensec_ctx;
383
384                 status = gensec_server_start(req,
385                                              req->smb_conn->connection->event.ctx,
386                                              req->smb_conn->lp_ctx,
387                                              req->smb_conn->connection->msg_ctx,
388                                              &gensec_ctx);
389                 if (!NT_STATUS_IS_OK(status)) {
390                         DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
391                         goto failed;
392                 }
393
394                 gensec_set_credentials(gensec_ctx, req->smb_conn->negotiate.server_credentials);
395
396                 gensec_set_target_service(gensec_ctx, "cifs");
397
398                 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
399
400                 status = gensec_start_mech_by_oid(gensec_ctx, req->smb_conn->negotiate.oid);
401                 if (!NT_STATUS_IS_OK(status)) {
402                         DEBUG(1, ("Failed to start GENSEC %s server code: %s\n", 
403                                   gensec_get_name_by_oid(req->smb_conn->negotiate.oid), nt_errstr(status)));
404                         goto failed;
405                 }
406
407                 /* allocate a new session */
408                 smb_sess = smbsrv_session_new(req->smb_conn, req->smb_conn, gensec_ctx);
409                 if (!smb_sess) {
410                         status = NT_STATUS_INSUFFICIENT_RESOURCES;
411                         goto failed;
412                 }
413         }
414
415         if (!smb_sess) {
416                 status = NT_STATUS_ACCESS_DENIED;
417                 goto failed;
418         }
419
420         if (!smb_sess->gensec_ctx) {
421                 status = NT_STATUS_INTERNAL_ERROR;
422                 DEBUG(1, ("Internal ERROR: no gensec_ctx on session: %s\n", nt_errstr(status)));
423                 goto failed;
424         }
425
426         s = talloc(req, struct sesssetup_spnego_state);
427         if (!s) goto nomem;
428         s->req          = req;
429         s->sess         = sess;
430         s->smb_sess     = smb_sess;
431
432         gensec_update_send(smb_sess->gensec_ctx, sess->spnego.in.secblob,
433                            sesssetup_spnego_send, s);
434         return;
435
436 nomem:
437         status = NT_STATUS_NO_MEMORY;
438 failed:
439         talloc_free(smb_sess);
440         status = auth_nt_status_squash(status);
441         smbsrv_sesssetup_backend_send(req, sess, status);
442 }
443
444 /*
445   backend for sessionsetup call - this takes all 3 variants of the call
446 */
447 void smbsrv_sesssetup_backend(struct smbsrv_request *req,
448                               union smb_sesssetup *sess)
449 {
450         switch (sess->old.level) {
451                 case RAW_SESSSETUP_OLD:
452                         sesssetup_old(req, sess);
453                         return;
454
455                 case RAW_SESSSETUP_NT1:
456                         sesssetup_nt1(req, sess);
457                         return;
458
459                 case RAW_SESSSETUP_SPNEGO:
460                         sesssetup_spnego(req, sess);
461                         return;
462
463                 case RAW_SESSSETUP_SMB2:
464                         break;
465         }
466
467         smbsrv_sesssetup_backend_send(req, sess, NT_STATUS_INVALID_LEVEL);
468 }