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)
249 NTSTATUS status = NT_STATUS_ACCESS_DENIED;
250 struct smbsrv_session *smb_sess;
251 struct gensec_security *gensec_ctx;
252 struct auth_session_info *session_info = NULL;
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);
269 smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
271 gensec_ctx = smb_sess->gensec_ctx;
273 status = gensec_server_start(req, &gensec_ctx,
274 req->smb_conn->connection->event.ctx);
275 if (!NT_STATUS_IS_OK(status)) {
276 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
280 gensec_set_credentials(gensec_ctx, req->smb_conn->negotiate.server_credentials);
282 gensec_set_target_service(gensec_ctx, "cifs");
284 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
286 status = gensec_start_mech_by_oid(gensec_ctx, GENSEC_OID_SPNEGO);
287 if (!NT_STATUS_IS_OK(status)) {
288 DEBUG(1, ("Failed to start GENSEC SPNEGO server code: %s\n", nt_errstr(status)));
292 smb_sess = smbsrv_session_new(req->smb_conn, gensec_ctx);
294 return NT_STATUS_ACCESS_DENIED;
298 status = gensec_update(gensec_ctx, req, sess->spnego.in.secblob, &sess->spnego.out.secblob);
299 if (NT_STATUS_IS_OK(status)) {
300 DATA_BLOB session_key;
302 status = gensec_session_info(gensec_ctx, &session_info);
303 if (!NT_STATUS_IS_OK(status)) {
304 talloc_free(smb_sess);
308 status = gensec_session_key(gensec_ctx,
310 /* TODO: what if getting the session key failed? */
311 if (NT_STATUS_IS_OK(status)
312 && session_info->server_info->authenticated
313 && srv_setup_signing(req->smb_conn, &session_key, NULL)) {
314 /* Force check of the request packet, now we know the session key */
315 req_signing_check_incoming(req);
317 srv_signing_restart(req->smb_conn, &session_key, NULL);
320 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
322 status = auth_nt_status_squash(status);
324 /* This invalidates the VUID of the failed login */
325 talloc_free(smb_sess);
329 if (NT_STATUS_IS_OK(status)) {
330 /* Ensure this is marked as a 'real' vuid, not one
331 * simply valid for the session setup leg */
332 status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
333 if (!NT_STATUS_IS_OK(status)) {
334 return auth_nt_status_squash(status);
336 req->session = smb_sess;
338 sess->spnego.out.vuid = smb_sess->vuid;
344 backend for sessionsetup call - this takes all 3 variants of the call
346 NTSTATUS sesssetup_backend(struct smbsrv_request *req,
347 union smb_sesssetup *sess)
349 NTSTATUS status = NT_STATUS_INVALID_LEVEL;
351 switch (sess->old.level) {
352 case RAW_SESSSETUP_OLD:
353 status = sesssetup_old(req, sess);
355 case RAW_SESSSETUP_NT1:
356 status = sesssetup_nt1(req, sess);
358 case RAW_SESSSETUP_SPNEGO:
359 status = sesssetup_spnego(req, sess);
363 if (NT_STATUS_IS_OK(status)) {
364 req->smb_conn->negotiate.done_sesssetup = True;