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 2 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, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include "auth/auth.h"
29 #include "smb_server/smb_server.h"
30 #include "smbd/service_stream.h"
31 #include "librpc/gen_ndr/nbt.h"
34 setup the OS, Lanman and domain portions of a session setup reply
36 static void sesssetup_common_strings(struct smbsrv_request *req,
37 char **os, char **lanman, char **domain)
39 (*os) = talloc_asprintf(req, "Unix");
40 (*lanman) = talloc_asprintf(req, "Samba %s", SAMBA_VERSION_STRING);
41 (*domain) = talloc_asprintf(req, "%s", lp_workgroup());
46 handler for old style session setup
48 static NTSTATUS sesssetup_old(struct smbsrv_request *req, union smb_sesssetup *sess)
51 struct auth_usersupplied_info *user_info = NULL;
52 struct auth_serversupplied_info *server_info = NULL;
53 struct auth_session_info *session_info;
54 struct smbsrv_session *smb_sess;
55 struct socket_address *remote_address;
56 const char *remote_machine = NULL;
58 sess->old.out.vuid = 0;
59 sess->old.out.action = 0;
61 if (!req->smb_conn->negotiate.done_sesssetup) {
62 req->smb_conn->negotiate.max_send = sess->old.in.bufsize;
65 if (req->smb_conn->negotiate.calling_name) {
66 remote_machine = req->smb_conn->negotiate.calling_name->name;
69 remote_address = socket_get_peer_addr(req->smb_conn->connection->socket, req);
70 NT_STATUS_HAVE_NO_MEMORY(remote_address);
72 if (!remote_machine) {
73 remote_machine = remote_address->addr;
76 user_info = talloc(req, struct auth_usersupplied_info);
77 NT_STATUS_HAVE_NO_MEMORY(user_info);
79 user_info->mapped_state = False;
80 user_info->logon_parameters = 0;
82 user_info->client.account_name = sess->old.in.user;
83 user_info->client.domain_name = sess->old.in.domain;
84 user_info->workstation_name = remote_machine;
85 user_info->remote_host = talloc_steal(user_info, remote_address);
87 user_info->password_state = AUTH_PASSWORD_RESPONSE;
88 user_info->password.response.lanman = sess->old.in.password;
89 user_info->password.response.lanman.data = talloc_steal(user_info, sess->old.in.password.data);
90 user_info->password.response.nt = data_blob(NULL, 0);
92 status = auth_check_password(req->smb_conn->negotiate.auth_context,
93 req, user_info, &server_info);
94 if (!NT_STATUS_IS_OK(status)) {
95 return auth_nt_status_squash(status);
98 /* This references server_info into session_info */
99 status = auth_generate_session_info(req, server_info, &session_info);
100 if (!NT_STATUS_IS_OK(status)) {
101 return auth_nt_status_squash(status);
104 /* allocate a new session */
105 smb_sess = smbsrv_session_new(req->smb_conn, NULL);
107 return NT_STATUS_ACCESS_DENIED;
110 /* Ensure this is marked as a 'real' vuid, not one
111 * simply valid for the session setup leg */
112 status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
113 if (!NT_STATUS_IS_OK(status)) {
114 return auth_nt_status_squash(status);
117 /* To correctly process any AndX packet (like a tree connect)
118 * we need to fill in the session on the request here */
119 req->session = smb_sess;
120 sess->old.out.vuid = smb_sess->vuid;
122 sesssetup_common_strings(req,
124 &sess->old.out.lanman,
125 &sess->old.out.domain);
132 handler for NT1 style session setup
134 static NTSTATUS sesssetup_nt1(struct smbsrv_request *req, union smb_sesssetup *sess)
137 struct auth_context *auth_context;
138 struct auth_usersupplied_info *user_info = NULL;
139 struct auth_serversupplied_info *server_info = NULL;
140 struct auth_session_info *session_info;
141 struct smbsrv_session *smb_sess;
142 struct socket_address *remote_address;
143 const char *remote_machine = NULL;
145 sess->nt1.out.vuid = 0;
146 sess->nt1.out.action = 0;
148 if (!req->smb_conn->negotiate.done_sesssetup) {
149 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
150 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
153 if (req->smb_conn->negotiate.oid) {
154 if (sess->nt1.in.user && *sess->nt1.in.user) {
155 /* We can't accept a normal login, because we
156 * don't have a challenge */
157 return NT_STATUS_LOGON_FAILURE;
160 /* TODO: should we use just "anonymous" here? */
161 status = auth_context_create(req, lp_auth_methods(),
163 req->smb_conn->connection->event.ctx);
164 NT_STATUS_NOT_OK_RETURN(status);
166 auth_context = req->smb_conn->negotiate.auth_context;
169 if (req->smb_conn->negotiate.calling_name) {
170 remote_machine = req->smb_conn->negotiate.calling_name->name;
173 remote_address = socket_get_peer_addr(req->smb_conn->connection->socket, req);
174 NT_STATUS_HAVE_NO_MEMORY(remote_address);
176 if (!remote_machine) {
177 remote_machine = remote_address->addr;
180 user_info = talloc(req, struct auth_usersupplied_info);
181 NT_STATUS_HAVE_NO_MEMORY(user_info);
183 user_info->mapped_state = False;
184 user_info->logon_parameters = 0;
185 user_info->flags = 0;
186 user_info->client.account_name = sess->nt1.in.user;
187 user_info->client.domain_name = sess->nt1.in.domain;
188 user_info->workstation_name = remote_machine;
189 user_info->remote_host = talloc_steal(user_info, remote_address);
191 user_info->password_state = AUTH_PASSWORD_RESPONSE;
192 user_info->password.response.lanman = sess->nt1.in.password1;
193 user_info->password.response.lanman.data = talloc_steal(user_info, sess->nt1.in.password1.data);
194 user_info->password.response.nt = sess->nt1.in.password2;
195 user_info->password.response.nt.data = talloc_steal(user_info, sess->nt1.in.password2.data);
197 status = auth_check_password(auth_context, req, user_info, &server_info);
198 if (!NT_STATUS_IS_OK(status)) {
199 return auth_nt_status_squash(status);
202 /* This references server_info into session_info */
203 status = auth_generate_session_info(req, server_info, &session_info);
204 if (!NT_STATUS_IS_OK(status)) {
205 return auth_nt_status_squash(status);
208 /* allocate a new session */
209 smb_sess = smbsrv_session_new(req->smb_conn, NULL);
211 return NT_STATUS_ACCESS_DENIED;
214 /* Ensure this is marked as a 'real' vuid, not one
215 * simply valid for the session setup leg */
216 status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
217 if (!NT_STATUS_IS_OK(status)) {
218 return auth_nt_status_squash(status);
221 /* To correctly process any AndX packet (like a tree connect)
222 * we need to fill in the session on the request here */
223 req->session = smb_sess;
224 sess->nt1.out.vuid = smb_sess->vuid;
226 sesssetup_common_strings(req,
228 &sess->nt1.out.lanman,
229 &sess->nt1.out.domain);
231 if (!session_info->server_info->authenticated) {
235 if (!smbsrv_setup_signing(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2)) {
236 /* Already signing, or disabled */
240 /* Force check of the request packet, now we know the session key */
241 smbsrv_signing_check_incoming(req);
242 /* TODO: why don't we check the result here? */
244 /* Unfortunetly win2k3 as a client doesn't sign the request
245 * packet here, so we have to force signing to start again */
247 smbsrv_signing_restart(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2);
254 handler for SPNEGO style session setup
256 static NTSTATUS sesssetup_spnego(struct smbsrv_request *req, union smb_sesssetup *sess)
259 NTSTATUS skey_status;
260 struct smbsrv_session *smb_sess = NULL;
261 struct auth_session_info *session_info = NULL;
262 DATA_BLOB session_key;
265 sess->spnego.out.vuid = 0;
266 sess->spnego.out.action = 0;
268 sesssetup_common_strings(req,
269 &sess->spnego.out.os,
270 &sess->spnego.out.lanman,
271 &sess->spnego.out.workgroup);
273 if (!req->smb_conn->negotiate.done_sesssetup) {
274 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
275 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
278 vuid = SVAL(req->in.hdr,HDR_UID);
280 /* lookup an existing session */
281 smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
283 struct gensec_security *gensec_ctx;
285 status = gensec_server_start(req, &gensec_ctx,
286 req->smb_conn->connection->event.ctx);
287 if (!NT_STATUS_IS_OK(status)) {
288 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
292 gensec_set_credentials(gensec_ctx, req->smb_conn->negotiate.server_credentials);
294 gensec_set_target_service(gensec_ctx, "cifs");
296 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
298 status = gensec_start_mech_by_oid(gensec_ctx, req->smb_conn->negotiate.oid);
299 if (!NT_STATUS_IS_OK(status)) {
300 DEBUG(1, ("Failed to start GENSEC %s server code: %s\n",
301 gensec_get_name_by_oid(req->smb_conn->negotiate.oid), nt_errstr(status)));
305 /* allocate a new session */
306 smb_sess = smbsrv_session_new(req->smb_conn, gensec_ctx);
310 return NT_STATUS_ACCESS_DENIED;
313 if (!smb_sess->gensec_ctx) {
314 status = NT_STATUS_INTERNAL_ERROR;
315 DEBUG(1, ("Internal ERROR: no gensec_ctx on session: %s\n", nt_errstr(status)));
319 status = gensec_update(smb_sess->gensec_ctx, req, sess->spnego.in.secblob, &sess->spnego.out.secblob);
320 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
321 sess->spnego.out.vuid = smb_sess->vuid;
323 } else if (!NT_STATUS_IS_OK(status)) {
327 status = gensec_session_info(smb_sess->gensec_ctx, &session_info);
328 if (!NT_STATUS_IS_OK(status)) {
332 skey_status = gensec_session_key(smb_sess->gensec_ctx, &session_key);
333 if (NT_STATUS_IS_OK(skey_status) &&
334 session_info->server_info->authenticated &&
335 smbsrv_setup_signing(req->smb_conn, &session_key, NULL)) {
336 /* Force check of the request packet, now we know the session key */
337 smbsrv_signing_check_incoming(req);
339 smbsrv_signing_restart(req->smb_conn, &session_key, NULL);
342 /* Ensure this is marked as a 'real' vuid, not one
343 * simply valid for the session setup leg */
344 status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
345 if (!NT_STATUS_IS_OK(status)) {
348 req->session = smb_sess;
350 sess->spnego.out.vuid = smb_sess->vuid;
354 talloc_free(smb_sess);
355 return auth_nt_status_squash(status);
359 backend for sessionsetup call - this takes all 3 variants of the call
361 NTSTATUS smbsrv_sesssetup_backend(struct smbsrv_request *req,
362 union smb_sesssetup *sess)
364 NTSTATUS status = NT_STATUS_INVALID_LEVEL;
366 switch (sess->old.level) {
367 case RAW_SESSSETUP_OLD:
368 status = sesssetup_old(req, sess);
370 case RAW_SESSSETUP_NT1:
371 status = sesssetup_nt1(req, sess);
373 case RAW_SESSSETUP_SPNEGO:
374 status = sesssetup_spnego(req, sess);
378 if (NT_STATUS_IS_OK(status)) {
379 req->smb_conn->negotiate.done_sesssetup = True;