2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1998-2001
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
6 Copyright (C) Jim McDonough 2002
7 Copyright (C) Luke Howard 2003
8 Copyright (C) Stefan Metzmacher 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "auth/auth.h"
28 #include "smb_server/smb_server.h"
29 #include "smbd/service_stream.h"
32 setup the OS, Lanman and domain portions of a session setup reply
34 static void sesssetup_common_strings(struct smbsrv_request *req,
35 char **os, char **lanman, char **domain)
37 (*os) = talloc_asprintf(req, "Unix");
38 (*lanman) = talloc_asprintf(req, "Samba %s", SAMBA_VERSION_STRING);
39 (*domain) = talloc_asprintf(req, "%s", lp_workgroup());
44 handler for old style session setup
46 static NTSTATUS sesssetup_old(struct smbsrv_request *req, union smb_sesssetup *sess)
49 struct auth_usersupplied_info *user_info = NULL;
50 struct auth_serversupplied_info *server_info = NULL;
51 struct auth_session_info *session_info;
52 struct smbsrv_session *smb_sess;
53 const char *remote_machine = NULL;
55 sess->old.out.vuid = 0;
56 sess->old.out.action = 0;
58 if (!req->smb_conn->negotiate.done_sesssetup) {
59 req->smb_conn->negotiate.max_send = sess->old.in.bufsize;
62 if (req->smb_conn->negotiate.calling_name) {
63 remote_machine = req->smb_conn->negotiate.calling_name->name;
66 if (!remote_machine) {
67 remote_machine = socket_get_peer_addr(req->smb_conn->connection->socket, req);
70 user_info = talloc(req, struct auth_usersupplied_info);
71 NT_STATUS_HAVE_NO_MEMORY(user_info);
73 user_info->mapped_state = False;
74 user_info->logon_parameters = 0;
76 user_info->client.account_name = sess->old.in.user;
77 user_info->client.domain_name = sess->old.in.domain;
78 user_info->workstation_name = remote_machine;
79 user_info->remote_host = socket_get_peer_addr(req->smb_conn->connection->socket, user_info);
81 user_info->password_state = AUTH_PASSWORD_RESPONSE;
82 user_info->password.response.lanman = sess->old.in.password;
83 user_info->password.response.lanman.data = talloc_steal(user_info, sess->old.in.password.data);
84 user_info->password.response.nt = data_blob(NULL, 0);
86 status = auth_check_password(req->smb_conn->negotiate.auth_context,
87 req, user_info, &server_info);
88 if (!NT_STATUS_IS_OK(status)) {
89 return auth_nt_status_squash(status);
92 /* This references server_info into session_info */
93 status = auth_generate_session_info(req, server_info, &session_info);
94 if (!NT_STATUS_IS_OK(status)) {
95 return auth_nt_status_squash(status);
98 /* allocate a new session */
99 smb_sess = smbsrv_session_new(req->smb_conn, NULL);
101 return NT_STATUS_ACCESS_DENIED;
104 /* Ensure this is marked as a 'real' vuid, not one
105 * simply valid for the session setup leg */
106 status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
107 if (!NT_STATUS_IS_OK(status)) {
108 return auth_nt_status_squash(status);
111 /* To correctly process any AndX packet (like a tree connect)
112 * we need to fill in the session on the request here */
113 req->session = smb_sess;
114 sess->old.out.vuid = smb_sess->vuid;
116 sesssetup_common_strings(req,
118 &sess->old.out.lanman,
119 &sess->old.out.domain);
126 handler for NT1 style session setup
128 static NTSTATUS sesssetup_nt1(struct smbsrv_request *req, union smb_sesssetup *sess)
131 struct auth_context *auth_context;
132 struct auth_usersupplied_info *user_info = NULL;
133 struct auth_serversupplied_info *server_info = NULL;
134 struct auth_session_info *session_info;
135 struct smbsrv_session *smb_sess;
136 const char *remote_machine = NULL;
138 sess->nt1.out.vuid = 0;
139 sess->nt1.out.action = 0;
141 if (!req->smb_conn->negotiate.done_sesssetup) {
142 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
143 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
146 if (req->smb_conn->negotiate.spnego_negotiated) {
147 if (sess->nt1.in.user && *sess->nt1.in.user) {
148 /* We can't accept a normal login, because we
149 * don't have a challenge */
150 return NT_STATUS_LOGON_FAILURE;
153 /* TODO: should we use just "anonymous" here? */
154 status = auth_context_create(req, lp_auth_methods(),
156 req->smb_conn->connection->event.ctx);
157 NT_STATUS_NOT_OK_RETURN(status);
159 auth_context = req->smb_conn->negotiate.auth_context;
162 if (req->smb_conn->negotiate.calling_name) {
163 remote_machine = req->smb_conn->negotiate.calling_name->name;
166 if (!remote_machine) {
167 remote_machine = socket_get_peer_addr(req->smb_conn->connection->socket, req);
170 user_info = talloc(req, struct auth_usersupplied_info);
171 NT_STATUS_HAVE_NO_MEMORY(user_info);
173 user_info->mapped_state = False;
174 user_info->logon_parameters = 0;
175 user_info->flags = 0;
176 user_info->client.account_name = sess->nt1.in.user;
177 user_info->client.domain_name = sess->nt1.in.domain;
178 user_info->workstation_name = remote_machine;
179 user_info->remote_host = socket_get_peer_addr(req->smb_conn->connection->socket, user_info);
181 user_info->password_state = AUTH_PASSWORD_RESPONSE;
182 user_info->password.response.lanman = sess->nt1.in.password1;
183 user_info->password.response.lanman.data = talloc_steal(user_info, sess->nt1.in.password1.data);
184 user_info->password.response.nt = sess->nt1.in.password2;
185 user_info->password.response.nt.data = talloc_steal(user_info, sess->nt1.in.password2.data);
187 status = auth_check_password(auth_context, req, user_info, &server_info);
188 if (!NT_STATUS_IS_OK(status)) {
189 return auth_nt_status_squash(status);
192 /* This references server_info into session_info */
193 status = auth_generate_session_info(req, server_info, &session_info);
194 if (!NT_STATUS_IS_OK(status)) {
195 return auth_nt_status_squash(status);
198 /* allocate a new session */
199 smb_sess = smbsrv_session_new(req->smb_conn, NULL);
201 return NT_STATUS_ACCESS_DENIED;
204 /* Ensure this is marked as a 'real' vuid, not one
205 * simply valid for the session setup leg */
206 status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
207 if (!NT_STATUS_IS_OK(status)) {
208 return auth_nt_status_squash(status);
211 /* To correctly process any AndX packet (like a tree connect)
212 * we need to fill in the session on the request here */
213 req->session = smb_sess;
214 sess->nt1.out.vuid = smb_sess->vuid;
216 sesssetup_common_strings(req,
218 &sess->nt1.out.lanman,
219 &sess->nt1.out.domain);
221 if (!session_info->server_info->authenticated) {
225 if (!srv_setup_signing(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2)) {
226 /* Already signing, or disabled */
230 /* Force check of the request packet, now we know the session key */
231 req_signing_check_incoming(req);
232 /* TODO: why don't we check the result here? */
234 /* Unfortunetly win2k3 as a client doesn't sign the request
235 * packet here, so we have to force signing to start again */
237 srv_signing_restart(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2);
244 handler for SPNEGO style session setup
246 static NTSTATUS sesssetup_spnego(struct smbsrv_request *req, union smb_sesssetup *sess)
249 NTSTATUS skey_status;
250 struct smbsrv_session *smb_sess = NULL;
251 struct auth_session_info *session_info = NULL;
252 DATA_BLOB session_key;
255 sess->spnego.out.vuid = 0;
256 sess->spnego.out.action = 0;
258 sesssetup_common_strings(req,
259 &sess->spnego.out.os,
260 &sess->spnego.out.lanman,
261 &sess->spnego.out.workgroup);
263 if (!req->smb_conn->negotiate.done_sesssetup) {
264 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
265 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
268 vuid = SVAL(req->in.hdr,HDR_UID);
270 /* lookup an existing session */
271 smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
273 struct gensec_security *gensec_ctx;
275 status = gensec_server_start(req, &gensec_ctx,
276 req->smb_conn->connection->event.ctx);
277 if (!NT_STATUS_IS_OK(status)) {
278 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
282 gensec_set_credentials(gensec_ctx, req->smb_conn->negotiate.server_credentials);
284 gensec_set_target_service(gensec_ctx, "cifs");
286 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
288 status = gensec_start_mech_by_oid(gensec_ctx, GENSEC_OID_SPNEGO);
289 if (!NT_STATUS_IS_OK(status)) {
290 DEBUG(1, ("Failed to start GENSEC SPNEGO server code: %s\n", nt_errstr(status)));
294 /* allocate a new session */
295 smb_sess = smbsrv_session_new(req->smb_conn, gensec_ctx);
299 return NT_STATUS_ACCESS_DENIED;
302 if (!smb_sess->gensec_ctx) {
303 status = NT_STATUS_INTERNAL_ERROR;
304 DEBUG(1, ("Internal ERROR: no gensec_ctx on session: %s\n", nt_errstr(status)));
308 status = gensec_update(smb_sess->gensec_ctx, req, sess->spnego.in.secblob, &sess->spnego.out.secblob);
309 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
310 sess->spnego.out.vuid = smb_sess->vuid;
312 } else if (!NT_STATUS_IS_OK(status)) {
316 status = gensec_session_info(smb_sess->gensec_ctx, &session_info);
317 if (!NT_STATUS_IS_OK(status)) {
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 srv_setup_signing(req->smb_conn, &session_key, NULL)) {
325 /* Force check of the request packet, now we know the session key */
326 req_signing_check_incoming(req);
328 srv_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)) {
337 req->session = smb_sess;
339 sess->spnego.out.vuid = smb_sess->vuid;
343 talloc_free(smb_sess);
344 return auth_nt_status_squash(status);
348 backend for sessionsetup call - this takes all 3 variants of the call
350 NTSTATUS sesssetup_backend(struct smbsrv_request *req,
351 union smb_sesssetup *sess)
353 NTSTATUS status = NT_STATUS_INVALID_LEVEL;
355 switch (sess->old.level) {
356 case RAW_SESSSETUP_OLD:
357 status = sesssetup_old(req, sess);
359 case RAW_SESSSETUP_NT1:
360 status = sesssetup_nt1(req, sess);
362 case RAW_SESSSETUP_SPNEGO:
363 status = sesssetup_spnego(req, sess);
367 if (NT_STATUS_IS_OK(status)) {
368 req->smb_conn->negotiate.done_sesssetup = True;