3 Unix SMB/CIFS implementation.
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
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.
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.
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/>.
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"
35 setup the OS, Lanman and domain portions of a session setup reply
37 static void sesssetup_common_strings(struct smbsrv_request *req,
38 char **os, char **lanman, char **domain)
40 (*os) = talloc_asprintf(req, "Unix");
41 (*lanman) = talloc_asprintf(req, "Samba %s", SAMBA_VERSION_STRING);
42 (*domain) = talloc_asprintf(req, "%s", lp_workgroup());
45 static void smbsrv_sesssetup_backend_send(struct smbsrv_request *req,
46 union smb_sesssetup *sess,
49 if (NT_STATUS_IS_OK(status)) {
50 req->smb_conn->negotiate.done_sesssetup = True;
51 /* we need to keep the session long term */
52 req->session = talloc_steal(req->smb_conn, req->session);
54 smbsrv_reply_sesssetup_send(req, sess, status);
57 static void sesssetup_old_send(struct auth_check_password_request *areq,
60 struct smbsrv_request *req = talloc_get_type(private_data, struct smbsrv_request);
61 union smb_sesssetup *sess = talloc_get_type(req->io_ptr, union smb_sesssetup);
62 struct auth_serversupplied_info *server_info = NULL;
63 struct auth_session_info *session_info;
64 struct smbsrv_session *smb_sess;
67 status = auth_check_password_recv(areq, req, &server_info);
68 if (!NT_STATUS_IS_OK(status)) goto failed;
70 /* This references server_info into session_info */
71 status = auth_generate_session_info(req, server_info, &session_info);
72 if (!NT_STATUS_IS_OK(status)) goto failed;
74 /* allocate a new session */
75 smb_sess = smbsrv_session_new(req->smb_conn, req, NULL);
77 status = NT_STATUS_INSUFFICIENT_RESOURCES;
81 /* Ensure this is marked as a 'real' vuid, not one
82 * simply valid for the session setup leg */
83 status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
84 if (!NT_STATUS_IS_OK(status)) goto failed;
86 /* To correctly process any AndX packet (like a tree connect)
87 * we need to fill in the session on the request here */
88 req->session = smb_sess;
89 sess->old.out.vuid = smb_sess->vuid;
92 status = auth_nt_status_squash(status);
93 smbsrv_sesssetup_backend_send(req, sess, status);
97 handler for old style session setup
99 static void sesssetup_old(struct smbsrv_request *req, union smb_sesssetup *sess)
101 struct auth_usersupplied_info *user_info = NULL;
102 struct socket_address *remote_address;
103 const char *remote_machine = NULL;
105 sess->old.out.vuid = 0;
106 sess->old.out.action = 0;
108 sesssetup_common_strings(req,
110 &sess->old.out.lanman,
111 &sess->old.out.domain);
113 if (!req->smb_conn->negotiate.done_sesssetup) {
114 req->smb_conn->negotiate.max_send = sess->old.in.bufsize;
117 if (req->smb_conn->negotiate.calling_name) {
118 remote_machine = req->smb_conn->negotiate.calling_name->name;
121 remote_address = socket_get_peer_addr(req->smb_conn->connection->socket, req);
122 if (!remote_address) goto nomem;
124 if (!remote_machine) {
125 remote_machine = remote_address->addr;
128 user_info = talloc(req, struct auth_usersupplied_info);
129 if (!user_info) goto nomem;
131 user_info->mapped_state = False;
132 user_info->logon_parameters = 0;
133 user_info->flags = 0;
134 user_info->client.account_name = sess->old.in.user;
135 user_info->client.domain_name = sess->old.in.domain;
136 user_info->workstation_name = remote_machine;
137 user_info->remote_host = talloc_steal(user_info, remote_address);
139 user_info->password_state = AUTH_PASSWORD_RESPONSE;
140 user_info->password.response.lanman = sess->old.in.password;
141 user_info->password.response.lanman.data = talloc_steal(user_info, sess->old.in.password.data);
142 user_info->password.response.nt = data_blob(NULL, 0);
144 auth_check_password_send(req->smb_conn->negotiate.auth_context, user_info,
145 sesssetup_old_send, req);
149 smbsrv_sesssetup_backend_send(req, sess, NT_STATUS_NO_MEMORY);
152 static void sesssetup_nt1_send(struct auth_check_password_request *areq,
155 struct smbsrv_request *req = talloc_get_type(private_data, struct smbsrv_request);
156 union smb_sesssetup *sess = talloc_get_type(req->io_ptr, union smb_sesssetup);
157 struct auth_serversupplied_info *server_info = NULL;
158 struct auth_session_info *session_info;
159 struct smbsrv_session *smb_sess;
162 status = auth_check_password_recv(areq, req, &server_info);
163 if (!NT_STATUS_IS_OK(status)) goto failed;
165 /* This references server_info into session_info */
166 status = auth_generate_session_info(req, server_info, &session_info);
167 if (!NT_STATUS_IS_OK(status)) goto failed;
169 /* allocate a new session */
170 smb_sess = smbsrv_session_new(req->smb_conn, req, NULL);
172 status = NT_STATUS_INSUFFICIENT_RESOURCES;
176 /* Ensure this is marked as a 'real' vuid, not one
177 * simply valid for the session setup leg */
178 status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
179 if (!NT_STATUS_IS_OK(status)) goto failed;
181 /* To correctly process any AndX packet (like a tree connect)
182 * we need to fill in the session on the request here */
183 req->session = smb_sess;
184 sess->nt1.out.vuid = smb_sess->vuid;
186 if (!session_info->server_info->authenticated) {
187 /* don't try signing as anonymous */
191 if (!smbsrv_setup_signing(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2)) {
192 /* Already signing, or disabled */
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? */
200 /* Unfortunetly win2k3 as a client doesn't sign the request
201 * packet here, so we have to force signing to start again */
203 smbsrv_signing_restart(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2);
206 status = NT_STATUS_OK;
208 status = auth_nt_status_squash(status);
209 smbsrv_sesssetup_backend_send(req, sess, status);
213 handler for NT1 style session setup
215 static void sesssetup_nt1(struct smbsrv_request *req, union smb_sesssetup *sess)
218 struct auth_context *auth_context;
219 struct auth_usersupplied_info *user_info = NULL;
220 struct socket_address *remote_address;
221 const char *remote_machine = NULL;
223 sess->nt1.out.vuid = 0;
224 sess->nt1.out.action = 0;
226 sesssetup_common_strings(req,
228 &sess->nt1.out.lanman,
229 &sess->nt1.out.domain);
231 if (!req->smb_conn->negotiate.done_sesssetup) {
232 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
233 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
236 if (req->smb_conn->negotiate.oid) {
237 if (sess->nt1.in.user && *sess->nt1.in.user) {
238 /* We can't accept a normal login, because we
239 * don't have a challenge */
240 status = NT_STATUS_LOGON_FAILURE;
244 /* TODO: should we use just "anonymous" here? */
245 status = auth_context_create(req,
246 req->smb_conn->connection->event.ctx,
247 req->smb_conn->connection->msg_ctx,
249 if (!NT_STATUS_IS_OK(status)) goto failed;
251 auth_context = req->smb_conn->negotiate.auth_context;
254 if (req->smb_conn->negotiate.calling_name) {
255 remote_machine = req->smb_conn->negotiate.calling_name->name;
258 remote_address = socket_get_peer_addr(req->smb_conn->connection->socket, req);
259 if (!remote_address) goto nomem;
261 if (!remote_machine) {
262 remote_machine = remote_address->addr;
265 user_info = talloc(req, struct auth_usersupplied_info);
266 if (!user_info) goto nomem;
268 user_info->mapped_state = False;
269 user_info->logon_parameters = 0;
270 user_info->flags = 0;
271 user_info->client.account_name = sess->nt1.in.user;
272 user_info->client.domain_name = sess->nt1.in.domain;
273 user_info->workstation_name = remote_machine;
274 user_info->remote_host = talloc_steal(user_info, remote_address);
276 user_info->password_state = AUTH_PASSWORD_RESPONSE;
277 user_info->password.response.lanman = sess->nt1.in.password1;
278 user_info->password.response.lanman.data = talloc_steal(user_info, sess->nt1.in.password1.data);
279 user_info->password.response.nt = sess->nt1.in.password2;
280 user_info->password.response.nt.data = talloc_steal(user_info, sess->nt1.in.password2.data);
282 auth_check_password_send(auth_context, user_info,
283 sesssetup_nt1_send, req);
287 status = NT_STATUS_NO_MEMORY;
289 status = auth_nt_status_squash(status);
290 smbsrv_sesssetup_backend_send(req, sess, status);
293 struct sesssetup_spnego_state {
294 struct smbsrv_request *req;
295 union smb_sesssetup *sess;
296 struct smbsrv_session *smb_sess;
299 static void sesssetup_spnego_send(struct gensec_update_request *greq, void *private_data)
301 struct sesssetup_spnego_state *s = talloc_get_type(private_data,
302 struct sesssetup_spnego_state);
303 struct smbsrv_request *req = s->req;
304 union smb_sesssetup *sess = s->sess;
305 struct smbsrv_session *smb_sess = s->smb_sess;
306 struct auth_session_info *session_info = NULL;
308 NTSTATUS skey_status;
309 DATA_BLOB session_key;
311 status = gensec_update_recv(greq, req, &sess->spnego.out.secblob);
312 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
314 } else if (!NT_STATUS_IS_OK(status)) {
318 status = gensec_session_info(smb_sess->gensec_ctx, &session_info);
319 if (!NT_STATUS_IS_OK(status)) goto failed;
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 smbsrv_setup_signing(req->smb_conn, &session_key, NULL)) {
325 /* Force check of the request packet, now we know the session key */
326 smbsrv_signing_check_incoming(req);
328 smbsrv_signing_restart(req->smb_conn, &session_key, NULL);
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)) goto failed;
336 req->session = smb_sess;
339 sess->spnego.out.vuid = smb_sess->vuid;
341 status = auth_nt_status_squash(status);
342 smbsrv_sesssetup_backend_send(req, sess, status);
343 if (!NT_STATUS_IS_OK(status) &&
344 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
345 talloc_free(smb_sess);
350 handler for SPNEGO style session setup
352 static void sesssetup_spnego(struct smbsrv_request *req, union smb_sesssetup *sess)
355 struct smbsrv_session *smb_sess = NULL;
356 struct sesssetup_spnego_state *s = NULL;
359 sess->spnego.out.vuid = 0;
360 sess->spnego.out.action = 0;
362 sesssetup_common_strings(req,
363 &sess->spnego.out.os,
364 &sess->spnego.out.lanman,
365 &sess->spnego.out.workgroup);
367 if (!req->smb_conn->negotiate.done_sesssetup) {
368 req->smb_conn->negotiate.max_send = sess->spnego.in.bufsize;
369 req->smb_conn->negotiate.client_caps = sess->spnego.in.capabilities;
372 vuid = SVAL(req->in.hdr,HDR_UID);
374 /* lookup an existing session */
375 smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
377 struct gensec_security *gensec_ctx;
379 status = gensec_server_start(req,
380 req->smb_conn->connection->event.ctx,
381 req->smb_conn->connection->msg_ctx,
383 if (!NT_STATUS_IS_OK(status)) {
384 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
388 gensec_set_credentials(gensec_ctx, req->smb_conn->negotiate.server_credentials);
390 gensec_set_target_service(gensec_ctx, "cifs");
392 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
394 status = gensec_start_mech_by_oid(gensec_ctx, req->smb_conn->negotiate.oid);
395 if (!NT_STATUS_IS_OK(status)) {
396 DEBUG(1, ("Failed to start GENSEC %s server code: %s\n",
397 gensec_get_name_by_oid(req->smb_conn->negotiate.oid), nt_errstr(status)));
401 /* allocate a new session */
402 smb_sess = smbsrv_session_new(req->smb_conn, req->smb_conn, gensec_ctx);
404 status = NT_STATUS_INSUFFICIENT_RESOURCES;
410 status = NT_STATUS_ACCESS_DENIED;
414 if (!smb_sess->gensec_ctx) {
415 status = NT_STATUS_INTERNAL_ERROR;
416 DEBUG(1, ("Internal ERROR: no gensec_ctx on session: %s\n", nt_errstr(status)));
420 s = talloc(req, struct sesssetup_spnego_state);
424 s->smb_sess = smb_sess;
426 gensec_update_send(smb_sess->gensec_ctx, sess->spnego.in.secblob,
427 sesssetup_spnego_send, s);
431 status = NT_STATUS_NO_MEMORY;
433 talloc_free(smb_sess);
434 status = auth_nt_status_squash(status);
435 smbsrv_sesssetup_backend_send(req, sess, status);
439 backend for sessionsetup call - this takes all 3 variants of the call
441 void smbsrv_sesssetup_backend(struct smbsrv_request *req,
442 union smb_sesssetup *sess)
444 switch (sess->old.level) {
445 case RAW_SESSSETUP_OLD:
446 sesssetup_old(req, sess);
449 case RAW_SESSSETUP_NT1:
450 sesssetup_nt1(req, sess);
453 case RAW_SESSSETUP_SPNEGO:
454 sesssetup_spnego(req, sess);
457 case RAW_SESSSETUP_SMB2:
461 smbsrv_sesssetup_backend_send(req, sess, NT_STATUS_INVALID_LEVEL);