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
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "auth/auth.h"
27 #include "smb_server/smb_server.h"
28 #include "smbd/service_stream.h"
29 #include "libcli/nbt/libnbt.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;
56 sess->old.out.vuid = UID_FIELD_INVALID;
57 sess->old.out.action = 0;
59 mem_ctx = talloc_named(req, 0, "OLD session setup");
60 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
62 if (!req->smb_conn->negotiate.done_sesssetup) {
63 req->smb_conn->negotiate.max_send = sess->old.in.bufsize;
66 remote_machine = socket_get_peer_addr(req->smb_conn->connection->socket, mem_ctx);
67 status = make_user_info_for_reply_enc(req->smb_conn,
68 sess->old.in.user, sess->old.in.domain,
70 sess->old.in.password,
73 if (!NT_STATUS_IS_OK(status)) {
75 return NT_STATUS_ACCESS_DENIED;
78 status = auth_check_password(req->smb_conn->negotiate.auth_context,
79 mem_ctx, user_info, &server_info);
80 if (!NT_STATUS_IS_OK(status)) {
82 return auth_nt_status_squash(status);
85 /* This references server_info into session_info */
86 status = auth_generate_session_info(req, server_info, &session_info);
88 if (!NT_STATUS_IS_OK(status)) {
89 return auth_nt_status_squash(status);
92 smb_sess = smbsrv_register_session(req->smb_conn, session_info, NULL);
94 return NT_STATUS_ACCESS_DENIED;
97 /* Ensure this is marked as a 'real' vuid, not one
98 * simply valid for the session setup leg */
99 smb_sess->finished_sesssetup = True;
101 /* To correctly process any AndX packet (like a tree connect)
102 * we need to fill in the session on the request here */
103 req->session = smb_sess;
104 sess->old.out.vuid = smb_sess->vuid;
106 sesssetup_common_strings(req,
108 &sess->old.out.lanman,
109 &sess->old.out.domain);
116 handler for NT1 style session setup
118 static NTSTATUS sesssetup_nt1(struct smbsrv_request *req, union smb_sesssetup *sess)
121 struct smbsrv_session *smb_sess;
122 struct auth_usersupplied_info *user_info = NULL;
123 struct auth_serversupplied_info *server_info = NULL;
124 struct auth_session_info *session_info;
127 sess->nt1.out.vuid = UID_FIELD_INVALID;
128 sess->nt1.out.action = 0;
130 mem_ctx = talloc_named(req, 0, "NT1 session setup");
131 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
133 if (!req->smb_conn->negotiate.done_sesssetup) {
134 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
135 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
138 if (req->smb_conn->negotiate.spnego_negotiated) {
139 struct auth_context *auth_context;
141 if (sess->nt1.in.user && *sess->nt1.in.user) {
142 return NT_STATUS_ACCESS_DENIED;
145 status = make_user_info_anonymous(req->smb_conn, &user_info);
146 if (!NT_STATUS_IS_OK(status)) {
147 talloc_free(mem_ctx);
151 /* TODO: should we use just "anonymous" here? */
152 status = auth_context_create(req->smb_conn, lp_auth_methods(),
154 req->smb_conn->connection->event.ctx);
155 if (!NT_STATUS_IS_OK(status)) {
156 talloc_free(mem_ctx);
160 status = auth_check_password(auth_context, mem_ctx,
161 user_info, &server_info);
163 const char *remote_machine = NULL;
165 if (req->smb_conn->negotiate.called_name) {
166 remote_machine = req->smb_conn->negotiate.called_name->name;
169 if (!remote_machine) {
170 remote_machine = socket_get_peer_addr(req->smb_conn->connection->socket, mem_ctx);
173 status = make_user_info_for_reply_enc(req->smb_conn,
174 sess->nt1.in.user, sess->nt1.in.domain,
176 sess->nt1.in.password1,
177 sess->nt1.in.password2,
179 if (!NT_STATUS_IS_OK(status)) {
180 talloc_free(mem_ctx);
181 return NT_STATUS_ACCESS_DENIED;
184 status = auth_check_password(req->smb_conn->negotiate.auth_context,
185 req, user_info, &server_info);
188 if (!NT_STATUS_IS_OK(status)) {
189 talloc_free(mem_ctx);
190 return auth_nt_status_squash(status);
193 /* This references server_info into session_info */
194 status = auth_generate_session_info(mem_ctx, server_info, &session_info);
195 if (!NT_STATUS_IS_OK(status)) {
196 talloc_free(mem_ctx);
197 return auth_nt_status_squash(status);
200 smb_sess = smbsrv_register_session(req->smb_conn, session_info, NULL);
201 talloc_free(mem_ctx);
203 return NT_STATUS_ACCESS_DENIED;
206 /* Ensure this is marked as a 'real' vuid, not one
207 * simply valid for the session setup leg */
208 smb_sess->finished_sesssetup = True;
210 /* To correctly process any AndX packet (like a tree connect)
211 * we need to fill in the session on the request here */
212 req->session = smb_sess;
213 sess->nt1.out.vuid = smb_sess->vuid;
215 sesssetup_common_strings(req,
217 &sess->nt1.out.lanman,
218 &sess->nt1.out.domain);
220 req->session = smbsrv_session_find(req->smb_conn, sess->nt1.out.vuid);
221 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);
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)
248 NTSTATUS status = NT_STATUS_ACCESS_DENIED;
249 struct smbsrv_session *smb_sess;
250 struct gensec_security *gensec_ctx ;
251 struct auth_session_info *session_info = NULL;
254 sess->spnego.out.vuid = UID_FIELD_INVALID;
255 sess->spnego.out.action = 0;
257 sesssetup_common_strings(req,
258 &sess->spnego.out.os,
259 &sess->spnego.out.lanman,
260 &sess->spnego.out.workgroup);
262 if (!req->smb_conn->negotiate.done_sesssetup) {
263 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
264 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
267 vuid = SVAL(req->in.hdr,HDR_UID);
268 smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
270 gensec_ctx = smb_sess->gensec_ctx;
271 status = gensec_update(gensec_ctx, req, sess->spnego.in.secblob, &sess->spnego.out.secblob);
273 status = gensec_server_start(req->smb_conn, &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_target_service(gensec_ctx, "cifs");
282 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
284 status = gensec_start_mech_by_oid(gensec_ctx, GENSEC_OID_SPNEGO);
285 if (!NT_STATUS_IS_OK(status)) {
286 DEBUG(1, ("Failed to start GENSEC SPNEGO server code: %s\n", nt_errstr(status)));
290 status = gensec_update(gensec_ctx, req, sess->spnego.in.secblob, &sess->spnego.out.secblob);
293 if (NT_STATUS_IS_OK(status)) {
294 DATA_BLOB session_key;
296 status = gensec_session_info(gensec_ctx, &session_info);
297 if (!NT_STATUS_IS_OK(status)) {
298 talloc_free(smb_sess);
302 status = gensec_session_key(gensec_ctx,
305 if (NT_STATUS_IS_OK(status)
306 && session_info->server_info->authenticated
307 && srv_setup_signing(req->smb_conn, &session_key, NULL)) {
308 /* Force check of the request packet, now we know the session key */
309 req_signing_check_incoming(req);
311 srv_signing_restart(req->smb_conn, &session_key, NULL);
314 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
316 status = auth_nt_status_squash(status);
318 /* This invalidates the VUID of the failed login */
319 talloc_free(smb_sess);
324 smb_sess = smbsrv_register_session(req->smb_conn,
325 session_info, gensec_ctx);
327 return NT_STATUS_ACCESS_DENIED;
329 req->session = smb_sess;
331 smb_sess->session_info = talloc_reference(smb_sess, session_info);
334 if (NT_STATUS_IS_OK(status)) {
335 /* Ensure this is marked as a 'real' vuid, not one
336 * simply valid for the session setup leg */
337 smb_sess->finished_sesssetup = True;
339 sess->spnego.out.vuid = smb_sess->vuid;
345 backend for sessionsetup call - this takes all 3 variants of the call
347 NTSTATUS sesssetup_backend(struct smbsrv_request *req,
348 union smb_sesssetup *sess)
350 NTSTATUS status = NT_STATUS_INVALID_LEVEL;
352 switch (sess->old.level) {
353 case RAW_SESSSETUP_OLD:
354 status = sesssetup_old(req, sess);
356 case RAW_SESSSETUP_NT1:
357 status = sesssetup_nt1(req, sess);
359 case RAW_SESSSETUP_SPNEGO:
360 status = sesssetup_spnego(req, sess);
364 if (NT_STATUS_IS_OK(status)) {
365 req->smb_conn->negotiate.done_sesssetup = True;