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"
33 #include "param/param.h"
36 setup the OS, Lanman and domain portions of a session setup reply
38 static void sesssetup_common_strings(struct smbsrv_request *req,
39 char **os, char **lanman, char **domain)
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));
47 static void smbsrv_sesssetup_backend_send(struct smbsrv_request *req,
48 union smb_sesssetup *sess,
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);
56 smbsrv_reply_sesssetup_send(req, sess, status);
59 static void sesssetup_old_send(struct auth_check_password_request *areq,
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;
69 status = auth_check_password_recv(areq, req, &server_info);
70 if (!NT_STATUS_IS_OK(status)) goto failed;
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;
77 /* allocate a new session */
78 smb_sess = smbsrv_session_new(req->smb_conn, req, NULL);
80 status = NT_STATUS_INSUFFICIENT_RESOURCES;
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;
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;
95 status = auth_nt_status_squash(status);
96 smbsrv_sesssetup_backend_send(req, sess, status);
100 handler for old style session setup
102 static void sesssetup_old(struct smbsrv_request *req, union smb_sesssetup *sess)
104 struct auth_usersupplied_info *user_info = NULL;
105 struct socket_address *remote_address;
106 const char *remote_machine = NULL;
108 sess->old.out.vuid = 0;
109 sess->old.out.action = 0;
111 sesssetup_common_strings(req,
113 &sess->old.out.lanman,
114 &sess->old.out.domain);
116 if (!req->smb_conn->negotiate.done_sesssetup) {
117 req->smb_conn->negotiate.max_send = sess->old.in.bufsize;
120 if (req->smb_conn->negotiate.calling_name) {
121 remote_machine = req->smb_conn->negotiate.calling_name->name;
124 remote_address = socket_get_peer_addr(req->smb_conn->connection->socket, req);
125 if (!remote_address) goto nomem;
127 if (!remote_machine) {
128 remote_machine = remote_address->addr;
131 user_info = talloc(req, struct auth_usersupplied_info);
132 if (!user_info) goto nomem;
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);
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);
147 auth_check_password_send(req->smb_conn->negotiate.auth_context, user_info,
148 sesssetup_old_send, req);
152 smbsrv_sesssetup_backend_send(req, sess, NT_STATUS_NO_MEMORY);
155 static void sesssetup_nt1_send(struct auth_check_password_request *areq,
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;
165 status = auth_check_password_recv(areq, req, &server_info);
166 if (!NT_STATUS_IS_OK(status)) goto failed;
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;
174 /* allocate a new session */
175 smb_sess = smbsrv_session_new(req->smb_conn, req, NULL);
177 status = NT_STATUS_INSUFFICIENT_RESOURCES;
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;
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;
191 if (!session_info->server_info->authenticated) {
192 /* don't try signing as anonymous */
196 if (!smbsrv_setup_signing(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2)) {
197 /* Already signing, or disabled */
201 /* Force check of the request packet, now we know the session key */
202 smbsrv_signing_check_incoming(req);
203 /* TODO: why don't we check the result here? */
205 /* Unfortunetly win2k3 as a client doesn't sign the request
206 * packet here, so we have to force signing to start again */
208 smbsrv_signing_restart(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2);
211 status = NT_STATUS_OK;
213 status = auth_nt_status_squash(status);
214 smbsrv_sesssetup_backend_send(req, sess, status);
218 handler for NT1 style session setup
220 static void sesssetup_nt1(struct smbsrv_request *req, union smb_sesssetup *sess)
223 struct auth_context *auth_context;
224 struct auth_usersupplied_info *user_info = NULL;
225 struct socket_address *remote_address;
226 const char *remote_machine = NULL;
228 sess->nt1.out.vuid = 0;
229 sess->nt1.out.action = 0;
231 sesssetup_common_strings(req,
233 &sess->nt1.out.lanman,
234 &sess->nt1.out.domain);
236 if (!req->smb_conn->negotiate.done_sesssetup) {
237 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
238 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
241 if (req->smb_conn->negotiate.oid) {
242 if (sess->nt1.in.user && *sess->nt1.in.user) {
243 /* We can't accept a normal login, because we
244 * don't have a challenge */
245 status = NT_STATUS_LOGON_FAILURE;
249 /* TODO: should we use just "anonymous" here? */
250 status = auth_context_create(req,
251 req->smb_conn->connection->event.ctx,
252 req->smb_conn->connection->msg_ctx,
253 req->smb_conn->lp_ctx,
255 if (!NT_STATUS_IS_OK(status)) goto failed;
257 auth_context = req->smb_conn->negotiate.auth_context;
260 if (req->smb_conn->negotiate.calling_name) {
261 remote_machine = req->smb_conn->negotiate.calling_name->name;
264 remote_address = socket_get_peer_addr(req->smb_conn->connection->socket, req);
265 if (!remote_address) goto nomem;
267 if (!remote_machine) {
268 remote_machine = remote_address->addr;
271 user_info = talloc(req, struct auth_usersupplied_info);
272 if (!user_info) goto nomem;
274 user_info->mapped_state = false;
275 user_info->logon_parameters = 0;
276 user_info->flags = 0;
277 user_info->client.account_name = sess->nt1.in.user;
278 user_info->client.domain_name = sess->nt1.in.domain;
279 user_info->workstation_name = remote_machine;
280 user_info->remote_host = talloc_steal(user_info, remote_address);
282 user_info->password_state = AUTH_PASSWORD_RESPONSE;
283 user_info->password.response.lanman = sess->nt1.in.password1;
284 user_info->password.response.lanman.data = talloc_steal(user_info, sess->nt1.in.password1.data);
285 user_info->password.response.nt = sess->nt1.in.password2;
286 user_info->password.response.nt.data = talloc_steal(user_info, sess->nt1.in.password2.data);
288 auth_check_password_send(auth_context, user_info,
289 sesssetup_nt1_send, req);
293 status = NT_STATUS_NO_MEMORY;
295 status = auth_nt_status_squash(status);
296 smbsrv_sesssetup_backend_send(req, sess, status);
299 struct sesssetup_spnego_state {
300 struct smbsrv_request *req;
301 union smb_sesssetup *sess;
302 struct smbsrv_session *smb_sess;
305 static void sesssetup_spnego_send(struct gensec_update_request *greq, void *private_data)
307 struct sesssetup_spnego_state *s = talloc_get_type(private_data,
308 struct sesssetup_spnego_state);
309 struct smbsrv_request *req = s->req;
310 union smb_sesssetup *sess = s->sess;
311 struct smbsrv_session *smb_sess = s->smb_sess;
312 struct auth_session_info *session_info = NULL;
314 NTSTATUS skey_status;
315 DATA_BLOB session_key;
317 status = gensec_update_recv(greq, req, &sess->spnego.out.secblob);
318 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
320 } else if (!NT_STATUS_IS_OK(status)) {
324 status = gensec_session_info(smb_sess->gensec_ctx, &session_info);
325 if (!NT_STATUS_IS_OK(status)) goto failed;
327 skey_status = gensec_session_key(smb_sess->gensec_ctx, &session_key);
328 if (NT_STATUS_IS_OK(skey_status) &&
329 session_info->server_info->authenticated &&
330 smbsrv_setup_signing(req->smb_conn, &session_key, NULL)) {
331 /* Force check of the request packet, now we know the session key */
332 smbsrv_signing_check_incoming(req);
334 smbsrv_signing_restart(req->smb_conn, &session_key, NULL);
337 /* Ensure this is marked as a 'real' vuid, not one
338 * simply valid for the session setup leg */
339 status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
340 if (!NT_STATUS_IS_OK(status)) goto failed;
342 req->session = smb_sess;
345 sess->spnego.out.vuid = smb_sess->vuid;
347 status = auth_nt_status_squash(status);
348 smbsrv_sesssetup_backend_send(req, sess, status);
349 if (!NT_STATUS_IS_OK(status) &&
350 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
351 talloc_free(smb_sess);
356 handler for SPNEGO style session setup
358 static void sesssetup_spnego(struct smbsrv_request *req, union smb_sesssetup *sess)
361 struct smbsrv_session *smb_sess = NULL;
362 struct sesssetup_spnego_state *s = NULL;
365 sess->spnego.out.vuid = 0;
366 sess->spnego.out.action = 0;
368 sesssetup_common_strings(req,
369 &sess->spnego.out.os,
370 &sess->spnego.out.lanman,
371 &sess->spnego.out.workgroup);
373 if (!req->smb_conn->negotiate.done_sesssetup) {
374 req->smb_conn->negotiate.max_send = sess->spnego.in.bufsize;
375 req->smb_conn->negotiate.client_caps = sess->spnego.in.capabilities;
378 vuid = SVAL(req->in.hdr,HDR_UID);
380 /* lookup an existing session */
381 smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
383 struct gensec_security *gensec_ctx;
385 status = gensec_server_start(req,
386 req->smb_conn->connection->event.ctx,
387 req->smb_conn->lp_ctx,
388 req->smb_conn->connection->msg_ctx,
390 if (!NT_STATUS_IS_OK(status)) {
391 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
395 gensec_set_credentials(gensec_ctx, req->smb_conn->negotiate.server_credentials);
397 gensec_set_target_service(gensec_ctx, "cifs");
399 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
401 status = gensec_start_mech_by_oid(gensec_ctx, req->smb_conn->negotiate.oid);
402 if (!NT_STATUS_IS_OK(status)) {
403 DEBUG(1, ("Failed to start GENSEC %s server code: %s\n",
404 gensec_get_name_by_oid(req->smb_conn->negotiate.oid), nt_errstr(status)));
408 /* allocate a new session */
409 smb_sess = smbsrv_session_new(req->smb_conn, req->smb_conn, gensec_ctx);
411 status = NT_STATUS_INSUFFICIENT_RESOURCES;
417 status = NT_STATUS_ACCESS_DENIED;
421 if (!smb_sess->gensec_ctx) {
422 status = NT_STATUS_INTERNAL_ERROR;
423 DEBUG(1, ("Internal ERROR: no gensec_ctx on session: %s\n", nt_errstr(status)));
427 s = talloc(req, struct sesssetup_spnego_state);
431 s->smb_sess = smb_sess;
433 gensec_update_send(smb_sess->gensec_ctx, sess->spnego.in.secblob,
434 sesssetup_spnego_send, s);
438 status = NT_STATUS_NO_MEMORY;
440 talloc_free(smb_sess);
441 status = auth_nt_status_squash(status);
442 smbsrv_sesssetup_backend_send(req, sess, status);
446 backend for sessionsetup call - this takes all 3 variants of the call
448 void smbsrv_sesssetup_backend(struct smbsrv_request *req,
449 union smb_sesssetup *sess)
451 switch (sess->old.level) {
452 case RAW_SESSSETUP_OLD:
453 sesssetup_old(req, sess);
456 case RAW_SESSSETUP_NT1:
457 sesssetup_nt1(req, sess);
460 case RAW_SESSSETUP_SPNEGO:
461 sesssetup_spnego(req, sess);
464 case RAW_SESSSETUP_SMB2:
468 smbsrv_sesssetup_backend_send(req, sess, NT_STATUS_INVALID_LEVEL);