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"
33 setup the OS, Lanman and domain portions of a session setup reply
35 static void sesssetup_common_strings(struct smbsrv_request *req,
36 char **os, char **lanman, char **domain)
38 (*os) = talloc_asprintf(req, "Unix");
39 (*lanman) = talloc_asprintf(req, "Samba %s", SAMBA_VERSION_STRING);
40 (*domain) = talloc_asprintf(req, "%s", lp_workgroup());
45 handler for old style session setup
47 static NTSTATUS sesssetup_old(struct smbsrv_request *req, union smb_sesssetup *sess)
50 struct auth_usersupplied_info *user_info = NULL;
51 struct auth_serversupplied_info *server_info = NULL;
52 struct auth_session_info *session_info;
53 struct smbsrv_session *smb_sess;
54 struct socket_address *remote_address;
55 const char *remote_machine = NULL;
57 sess->old.out.vuid = 0;
58 sess->old.out.action = 0;
60 if (!req->smb_conn->negotiate.done_sesssetup) {
61 req->smb_conn->negotiate.max_send = sess->old.in.bufsize;
64 if (req->smb_conn->negotiate.calling_name) {
65 remote_machine = req->smb_conn->negotiate.calling_name->name;
68 remote_address = socket_get_peer_addr(req->smb_conn->connection->socket, req);
69 NT_STATUS_HAVE_NO_MEMORY(remote_address);
71 if (!remote_machine) {
72 remote_machine = remote_address->addr;
75 user_info = talloc(req, struct auth_usersupplied_info);
76 NT_STATUS_HAVE_NO_MEMORY(user_info);
78 user_info->mapped_state = False;
79 user_info->logon_parameters = 0;
81 user_info->client.account_name = sess->old.in.user;
82 user_info->client.domain_name = sess->old.in.domain;
83 user_info->workstation_name = remote_machine;
84 user_info->remote_host = talloc_steal(user_info, remote_address);
86 user_info->password_state = AUTH_PASSWORD_RESPONSE;
87 user_info->password.response.lanman = sess->old.in.password;
88 user_info->password.response.lanman.data = talloc_steal(user_info, sess->old.in.password.data);
89 user_info->password.response.nt = data_blob(NULL, 0);
91 status = auth_check_password(req->smb_conn->negotiate.auth_context,
92 req, user_info, &server_info);
93 if (!NT_STATUS_IS_OK(status)) {
94 return auth_nt_status_squash(status);
97 /* This references server_info into session_info */
98 status = auth_generate_session_info(req, server_info, &session_info);
99 if (!NT_STATUS_IS_OK(status)) {
100 return auth_nt_status_squash(status);
103 /* allocate a new session */
104 smb_sess = smbsrv_session_new(req->smb_conn, NULL);
106 return NT_STATUS_ACCESS_DENIED;
109 /* Ensure this is marked as a 'real' vuid, not one
110 * simply valid for the session setup leg */
111 status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
112 if (!NT_STATUS_IS_OK(status)) {
113 return auth_nt_status_squash(status);
116 /* To correctly process any AndX packet (like a tree connect)
117 * we need to fill in the session on the request here */
118 req->session = smb_sess;
119 sess->old.out.vuid = smb_sess->vuid;
121 sesssetup_common_strings(req,
123 &sess->old.out.lanman,
124 &sess->old.out.domain);
131 handler for NT1 style session setup
133 static NTSTATUS sesssetup_nt1(struct smbsrv_request *req, union smb_sesssetup *sess)
136 struct auth_context *auth_context;
137 struct auth_usersupplied_info *user_info = NULL;
138 struct auth_serversupplied_info *server_info = NULL;
139 struct auth_session_info *session_info;
140 struct smbsrv_session *smb_sess;
141 struct socket_address *remote_address;
142 const char *remote_machine = NULL;
144 sess->nt1.out.vuid = 0;
145 sess->nt1.out.action = 0;
147 if (!req->smb_conn->negotiate.done_sesssetup) {
148 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
149 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
152 if (req->smb_conn->negotiate.spnego_negotiated) {
153 if (sess->nt1.in.user && *sess->nt1.in.user) {
154 /* We can't accept a normal login, because we
155 * don't have a challenge */
156 return NT_STATUS_LOGON_FAILURE;
159 /* TODO: should we use just "anonymous" here? */
160 status = auth_context_create(req, lp_auth_methods(),
162 req->smb_conn->connection->event.ctx);
163 NT_STATUS_NOT_OK_RETURN(status);
165 auth_context = req->smb_conn->negotiate.auth_context;
168 if (req->smb_conn->negotiate.calling_name) {
169 remote_machine = req->smb_conn->negotiate.calling_name->name;
172 remote_address = socket_get_peer_addr(req->smb_conn->connection->socket, req);
173 NT_STATUS_HAVE_NO_MEMORY(remote_address);
175 if (!remote_machine) {
176 remote_machine = remote_address->addr;
179 user_info = talloc(req, struct auth_usersupplied_info);
180 NT_STATUS_HAVE_NO_MEMORY(user_info);
182 user_info->mapped_state = False;
183 user_info->logon_parameters = 0;
184 user_info->flags = 0;
185 user_info->client.account_name = sess->nt1.in.user;
186 user_info->client.domain_name = sess->nt1.in.domain;
187 user_info->workstation_name = remote_machine;
188 user_info->remote_host = talloc_steal(user_info, remote_address);
190 user_info->password_state = AUTH_PASSWORD_RESPONSE;
191 user_info->password.response.lanman = sess->nt1.in.password1;
192 user_info->password.response.lanman.data = talloc_steal(user_info, sess->nt1.in.password1.data);
193 user_info->password.response.nt = sess->nt1.in.password2;
194 user_info->password.response.nt.data = talloc_steal(user_info, sess->nt1.in.password2.data);
196 status = auth_check_password(auth_context, req, user_info, &server_info);
197 if (!NT_STATUS_IS_OK(status)) {
198 return auth_nt_status_squash(status);
201 /* This references server_info into session_info */
202 status = auth_generate_session_info(req, server_info, &session_info);
203 if (!NT_STATUS_IS_OK(status)) {
204 return auth_nt_status_squash(status);
207 /* allocate a new session */
208 smb_sess = smbsrv_session_new(req->smb_conn, NULL);
210 return NT_STATUS_ACCESS_DENIED;
213 /* Ensure this is marked as a 'real' vuid, not one
214 * simply valid for the session setup leg */
215 status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
216 if (!NT_STATUS_IS_OK(status)) {
217 return auth_nt_status_squash(status);
220 /* To correctly process any AndX packet (like a tree connect)
221 * we need to fill in the session on the request here */
222 req->session = smb_sess;
223 sess->nt1.out.vuid = smb_sess->vuid;
225 sesssetup_common_strings(req,
227 &sess->nt1.out.lanman,
228 &sess->nt1.out.domain);
230 if (!session_info->server_info->authenticated) {
234 if (!srv_setup_signing(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2)) {
235 /* Already signing, or disabled */
239 /* Force check of the request packet, now we know the session key */
240 req_signing_check_incoming(req);
241 /* TODO: why don't we check the result here? */
243 /* Unfortunetly win2k3 as a client doesn't sign the request
244 * packet here, so we have to force signing to start again */
246 srv_signing_restart(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2);
253 handler for SPNEGO style session setup
255 static NTSTATUS sesssetup_spnego(struct smbsrv_request *req, union smb_sesssetup *sess)
258 NTSTATUS skey_status;
259 struct smbsrv_session *smb_sess = NULL;
260 struct auth_session_info *session_info = NULL;
261 DATA_BLOB session_key;
264 sess->spnego.out.vuid = 0;
265 sess->spnego.out.action = 0;
267 sesssetup_common_strings(req,
268 &sess->spnego.out.os,
269 &sess->spnego.out.lanman,
270 &sess->spnego.out.workgroup);
272 if (!req->smb_conn->negotiate.done_sesssetup) {
273 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
274 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
277 vuid = SVAL(req->in.hdr,HDR_UID);
279 /* lookup an existing session */
280 smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
282 struct gensec_security *gensec_ctx;
284 status = gensec_server_start(req, &gensec_ctx,
285 req->smb_conn->connection->event.ctx);
286 if (!NT_STATUS_IS_OK(status)) {
287 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
291 gensec_set_credentials(gensec_ctx, req->smb_conn->negotiate.server_credentials);
293 gensec_set_target_service(gensec_ctx, "cifs");
295 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
297 status = gensec_start_mech_by_oid(gensec_ctx, GENSEC_OID_SPNEGO);
298 if (!NT_STATUS_IS_OK(status)) {
299 DEBUG(1, ("Failed to start GENSEC SPNEGO server code: %s\n", nt_errstr(status)));
303 /* allocate a new session */
304 smb_sess = smbsrv_session_new(req->smb_conn, gensec_ctx);
308 return NT_STATUS_ACCESS_DENIED;
311 if (!smb_sess->gensec_ctx) {
312 status = NT_STATUS_INTERNAL_ERROR;
313 DEBUG(1, ("Internal ERROR: no gensec_ctx on session: %s\n", nt_errstr(status)));
317 status = gensec_update(smb_sess->gensec_ctx, req, sess->spnego.in.secblob, &sess->spnego.out.secblob);
318 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
319 sess->spnego.out.vuid = smb_sess->vuid;
321 } else if (!NT_STATUS_IS_OK(status)) {
325 status = gensec_session_info(smb_sess->gensec_ctx, &session_info);
326 if (!NT_STATUS_IS_OK(status)) {
330 skey_status = gensec_session_key(smb_sess->gensec_ctx, &session_key);
331 if (NT_STATUS_IS_OK(skey_status) &&
332 session_info->server_info->authenticated &&
333 srv_setup_signing(req->smb_conn, &session_key, NULL)) {
334 /* Force check of the request packet, now we know the session key */
335 req_signing_check_incoming(req);
337 srv_signing_restart(req->smb_conn, &session_key, NULL);
340 /* Ensure this is marked as a 'real' vuid, not one
341 * simply valid for the session setup leg */
342 status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
343 if (!NT_STATUS_IS_OK(status)) {
346 req->session = smb_sess;
348 sess->spnego.out.vuid = smb_sess->vuid;
352 talloc_free(smb_sess);
353 return auth_nt_status_squash(status);
357 backend for sessionsetup call - this takes all 3 variants of the call
359 NTSTATUS sesssetup_backend(struct smbsrv_request *req,
360 union smb_sesssetup *sess)
362 NTSTATUS status = NT_STATUS_INVALID_LEVEL;
364 switch (sess->old.level) {
365 case RAW_SESSSETUP_OLD:
366 status = sesssetup_old(req, sess);
368 case RAW_SESSSETUP_NT1:
369 status = sesssetup_nt1(req, sess);
371 case RAW_SESSSETUP_SPNEGO:
372 status = sesssetup_spnego(req, sess);
376 if (NT_STATUS_IS_OK(status)) {
377 req->smb_conn->negotiate.done_sesssetup = True;