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"
30 #include "libcli/nbt/libnbt.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 const char *remote_machine = NULL;
56 sess->old.out.vuid = 0;
57 sess->old.out.action = 0;
59 if (!req->smb_conn->negotiate.done_sesssetup) {
60 req->smb_conn->negotiate.max_send = sess->old.in.bufsize;
63 if (req->smb_conn->negotiate.calling_name) {
64 remote_machine = req->smb_conn->negotiate.calling_name->name;
67 if (!remote_machine) {
68 remote_machine = socket_get_peer_addr(req->smb_conn->connection->socket, req);
71 user_info = talloc(req, struct auth_usersupplied_info);
72 NT_STATUS_HAVE_NO_MEMORY(user_info);
74 user_info->mapped_state = False;
75 user_info->logon_parameters = 0;
77 user_info->client.account_name = sess->old.in.user;
78 user_info->client.domain_name = sess->old.in.domain;
79 user_info->workstation_name = remote_machine;
80 user_info->remote_host = socket_get_peer_addr(req->smb_conn->connection->socket, user_info);
82 user_info->password_state = AUTH_PASSWORD_RESPONSE;
83 user_info->password.response.lanman = sess->old.in.password;
84 user_info->password.response.lanman.data = talloc_steal(user_info, sess->old.in.password.data);
85 user_info->password.response.nt = data_blob(NULL, 0);
87 status = auth_check_password(req->smb_conn->negotiate.auth_context,
88 req, user_info, &server_info);
89 if (!NT_STATUS_IS_OK(status)) {
90 return auth_nt_status_squash(status);
93 /* This references server_info into session_info */
94 status = auth_generate_session_info(req, server_info, &session_info);
95 if (!NT_STATUS_IS_OK(status)) {
96 return auth_nt_status_squash(status);
99 /* allocate a new session */
100 smb_sess = smbsrv_session_new(req->smb_conn, NULL);
102 return NT_STATUS_ACCESS_DENIED;
105 /* Ensure this is marked as a 'real' vuid, not one
106 * simply valid for the session setup leg */
107 status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
108 if (!NT_STATUS_IS_OK(status)) {
109 return auth_nt_status_squash(status);
112 /* To correctly process any AndX packet (like a tree connect)
113 * we need to fill in the session on the request here */
114 req->session = smb_sess;
115 sess->old.out.vuid = smb_sess->vuid;
117 sesssetup_common_strings(req,
119 &sess->old.out.lanman,
120 &sess->old.out.domain);
127 handler for NT1 style session setup
129 static NTSTATUS sesssetup_nt1(struct smbsrv_request *req, union smb_sesssetup *sess)
132 struct auth_context *auth_context;
133 struct auth_usersupplied_info *user_info = NULL;
134 struct auth_serversupplied_info *server_info = NULL;
135 struct auth_session_info *session_info;
136 struct smbsrv_session *smb_sess;
137 const char *remote_machine = NULL;
139 sess->nt1.out.vuid = 0;
140 sess->nt1.out.action = 0;
142 if (!req->smb_conn->negotiate.done_sesssetup) {
143 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
144 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
147 if (req->smb_conn->negotiate.spnego_negotiated) {
148 if (sess->nt1.in.user && *sess->nt1.in.user) {
149 /* We can't accept a normal login, because we
150 * don't have a challenge */
151 return NT_STATUS_LOGON_FAILURE;
154 /* TODO: should we use just "anonymous" here? */
155 status = auth_context_create(req, lp_auth_methods(),
157 req->smb_conn->connection->event.ctx);
158 NT_STATUS_NOT_OK_RETURN(status);
160 auth_context = req->smb_conn->negotiate.auth_context;
163 if (req->smb_conn->negotiate.calling_name) {
164 remote_machine = req->smb_conn->negotiate.calling_name->name;
167 if (!remote_machine) {
168 remote_machine = socket_get_peer_addr(req->smb_conn->connection->socket, req);
171 user_info = talloc(req, struct auth_usersupplied_info);
172 NT_STATUS_HAVE_NO_MEMORY(user_info);
174 user_info->mapped_state = False;
175 user_info->logon_parameters = 0;
176 user_info->flags = 0;
177 user_info->client.account_name = sess->nt1.in.user;
178 user_info->client.domain_name = sess->nt1.in.domain;
179 user_info->workstation_name = remote_machine;
180 user_info->remote_host = socket_get_peer_addr(req->smb_conn->connection->socket, user_info);
182 user_info->password_state = AUTH_PASSWORD_RESPONSE;
183 user_info->password.response.lanman = sess->nt1.in.password1;
184 user_info->password.response.lanman.data = talloc_steal(user_info, sess->nt1.in.password1.data);
185 user_info->password.response.nt = sess->nt1.in.password2;
186 user_info->password.response.nt.data = talloc_steal(user_info, sess->nt1.in.password2.data);
188 status = auth_check_password(auth_context, req, user_info, &server_info);
189 if (!NT_STATUS_IS_OK(status)) {
190 return auth_nt_status_squash(status);
193 /* This references server_info into session_info */
194 status = auth_generate_session_info(req, server_info, &session_info);
195 if (!NT_STATUS_IS_OK(status)) {
196 return auth_nt_status_squash(status);
199 /* allocate a new session */
200 smb_sess = smbsrv_session_new(req->smb_conn, NULL);
202 return NT_STATUS_ACCESS_DENIED;
205 /* Ensure this is marked as a 'real' vuid, not one
206 * simply valid for the session setup leg */
207 status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
208 if (!NT_STATUS_IS_OK(status)) {
209 return auth_nt_status_squash(status);
212 /* To correctly process any AndX packet (like a tree connect)
213 * we need to fill in the session on the request here */
214 req->session = smb_sess;
215 sess->nt1.out.vuid = smb_sess->vuid;
217 sesssetup_common_strings(req,
219 &sess->nt1.out.lanman,
220 &sess->nt1.out.domain);
222 if (!session_info->server_info->authenticated) {
226 if (!srv_setup_signing(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2)) {
227 /* Already signing, or disabled */
231 /* Force check of the request packet, now we know the session key */
232 req_signing_check_incoming(req);
233 /* TODO: why don't we check the result here? */
235 /* Unfortunetly win2k3 as a client doesn't sign the request
236 * packet here, so we have to force signing to start again */
238 srv_signing_restart(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2);
245 handler for SPNEGO style session setup
247 static NTSTATUS sesssetup_spnego(struct smbsrv_request *req, union smb_sesssetup *sess)
250 NTSTATUS skey_status;
251 struct smbsrv_session *smb_sess = NULL;
252 struct auth_session_info *session_info = NULL;
253 DATA_BLOB session_key;
256 sess->spnego.out.vuid = 0;
257 sess->spnego.out.action = 0;
259 sesssetup_common_strings(req,
260 &sess->spnego.out.os,
261 &sess->spnego.out.lanman,
262 &sess->spnego.out.workgroup);
264 if (!req->smb_conn->negotiate.done_sesssetup) {
265 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
266 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
269 vuid = SVAL(req->in.hdr,HDR_UID);
271 /* lookup an existing session */
272 smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
274 struct gensec_security *gensec_ctx;
276 status = gensec_server_start(req, &gensec_ctx,
277 req->smb_conn->connection->event.ctx);
278 if (!NT_STATUS_IS_OK(status)) {
279 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
283 gensec_set_credentials(gensec_ctx, req->smb_conn->negotiate.server_credentials);
285 gensec_set_target_service(gensec_ctx, "cifs");
287 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
289 status = gensec_start_mech_by_oid(gensec_ctx, GENSEC_OID_SPNEGO);
290 if (!NT_STATUS_IS_OK(status)) {
291 DEBUG(1, ("Failed to start GENSEC SPNEGO server code: %s\n", nt_errstr(status)));
295 /* allocate a new session */
296 smb_sess = smbsrv_session_new(req->smb_conn, gensec_ctx);
300 return NT_STATUS_ACCESS_DENIED;
303 if (!smb_sess->gensec_ctx) {
304 status = NT_STATUS_INTERNAL_ERROR;
305 DEBUG(1, ("Internal ERROR: no gensec_ctx on session: %s\n", nt_errstr(status)));
309 status = gensec_update(smb_sess->gensec_ctx, req, sess->spnego.in.secblob, &sess->spnego.out.secblob);
310 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
311 sess->spnego.out.vuid = smb_sess->vuid;
313 } else if (!NT_STATUS_IS_OK(status)) {
317 status = gensec_session_info(smb_sess->gensec_ctx, &session_info);
318 if (!NT_STATUS_IS_OK(status)) {
322 skey_status = gensec_session_key(smb_sess->gensec_ctx, &session_key);
323 if (NT_STATUS_IS_OK(skey_status) &&
324 session_info->server_info->authenticated &&
325 srv_setup_signing(req->smb_conn, &session_key, NULL)) {
326 /* Force check of the request packet, now we know the session key */
327 req_signing_check_incoming(req);
329 srv_signing_restart(req->smb_conn, &session_key, NULL);
332 /* Ensure this is marked as a 'real' vuid, not one
333 * simply valid for the session setup leg */
334 status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
335 if (!NT_STATUS_IS_OK(status)) {
338 req->session = smb_sess;
340 sess->spnego.out.vuid = smb_sess->vuid;
344 talloc_free(smb_sess);
345 return auth_nt_status_squash(status);
349 backend for sessionsetup call - this takes all 3 variants of the call
351 NTSTATUS sesssetup_backend(struct smbsrv_request *req,
352 union smb_sesssetup *sess)
354 NTSTATUS status = NT_STATUS_INVALID_LEVEL;
356 switch (sess->old.level) {
357 case RAW_SESSSETUP_OLD:
358 status = sesssetup_old(req, sess);
360 case RAW_SESSSETUP_NT1:
361 status = sesssetup_nt1(req, sess);
363 case RAW_SESSSETUP_SPNEGO:
364 status = sesssetup_spnego(req, sess);
368 if (NT_STATUS_IS_OK(status)) {
369 req->smb_conn->negotiate.done_sesssetup = True;