summaryrefslogtreecommitdiff
path: root/source4/smb_server/sesssetup.c
blob: 1c336864073bb777786d97a97cebe35a2afd2baa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* 
   Unix SMB/CIFS implementation.
   handle SMBsessionsetup
   Copyright (C) Andrew Tridgell 1998-2001
   Copyright (C) Andrew Bartlett      2001
   Copyright (C) Jim McDonough        2002
   Copyright (C) Luke Howard          2003

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

/*
  setup the OS, Lanman and domain portions of a session setup reply
*/
static void sesssetup_common_strings(struct smbsrv_request *req,
				     char **os, char **lanman, char **domain)
{
	(*os) = talloc_asprintf(req->mem_ctx, "Unix");
	(*lanman) = talloc_asprintf(req->mem_ctx, "Samba %s", SAMBA_VERSION_STRING);
	(*domain) = talloc_asprintf(req->mem_ctx, "%s", lp_workgroup());
}


/*
  handler for old style session setup
*/
static NTSTATUS sesssetup_old(struct smbsrv_request *req, union smb_sesssetup *sess)
{
	NTSTATUS status;
	struct auth_usersupplied_info *user_info = NULL;
	struct auth_serversupplied_info *server_info = NULL;
	struct auth_session_info *session_info;
	DATA_BLOB null_blob;

	if (!req->smb_conn->negotiate.done_sesssetup) {
		req->smb_conn->negotiate.max_send = sess->old.in.bufsize;
	}

	null_blob.length = 0;

	status = make_user_info_for_reply_enc(&user_info, 
					      sess->old.in.user, sess->old.in.domain,
					      sess->old.in.password,
					      null_blob);
	if (!NT_STATUS_IS_OK(status)) {
		return NT_STATUS_ACCESS_DENIED;
	}

	status = req->smb_conn->negotiate.auth_context->check_ntlm_password(req->smb_conn->negotiate.auth_context, 
								       user_info, 
								       &server_info);
	if (!NT_STATUS_IS_OK(status)) {
		return nt_status_squash(status);
	}

	status = make_session_info(server_info, &session_info);
	if (!NT_STATUS_IS_OK(status)) {
		return nt_status_squash(status);
	}

	sess->old.out.action = 0;
	sess->old.out.vuid = smbsrv_register_session(req->smb_conn, session_info, NULL);
	if (sess->old.out.vuid == UID_FIELD_INVALID) {
		return NT_STATUS_ACCESS_DENIED;
	}
	sesssetup_common_strings(req, 
				 &sess->old.out.os,
				 &sess->old.out.lanman,
				 &sess->old.out.domain);

	req->session = smbsrv_session_find(req->smb_conn, sess->old.out.vuid);

	return NT_STATUS_OK;
}


/*
  handler for NT1 style session setup
*/
static NTSTATUS sesssetup_nt1(struct smbsrv_request *req, union smb_sesssetup *sess)
{
	NTSTATUS status;
	struct auth_usersupplied_info *user_info = NULL;
	struct auth_serversupplied_info *server_info = NULL;
	struct auth_session_info *session_info;

	if (!req->smb_conn->negotiate.done_sesssetup) {
		req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
		req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
	}

	if (req->smb_conn->negotiate.spnego_negotiated) {
		struct auth_context *auth_context;

		if (sess->nt1.in.user && *sess->nt1.in.user) {
			return NT_STATUS_ACCESS_DENIED;
		} else {
			make_user_info_guest(&user_info);
		}
		
		status = make_auth_context_subsystem(&auth_context);

		if (!NT_STATUS_IS_OK(status)) {
			return status;
		}
		
		status = auth_context->check_ntlm_password(auth_context, 
							   user_info, 
							   &server_info);
		
		free_auth_context(&auth_context);

	} else {
		status = make_user_info_for_reply_enc(&user_info, 
						      sess->nt1.in.user, sess->nt1.in.domain,
						      sess->nt1.in.password1,
						      sess->nt1.in.password2);
		if (!NT_STATUS_IS_OK(status)) {
			return NT_STATUS_ACCESS_DENIED;
		}
		
		status = req->smb_conn->negotiate
			.auth_context->check_ntlm_password(req->smb_conn->negotiate
							   .auth_context, 
							   user_info, 
							   &server_info);
	}

	if (!NT_STATUS_IS_OK(status)) {
		return nt_status_squash(status);
	}

	status = make_session_info(server_info, &session_info);
	if (!NT_STATUS_IS_OK(status)) {
		return nt_status_squash(status);
	}

	sess->nt1.out.action = 0;
	sess->nt1.out.vuid = smbsrv_register_session(req->smb_conn, session_info, NULL);
	if (sess->nt1.out.vuid == UID_FIELD_INVALID) {
		return NT_STATUS_ACCESS_DENIED;
	}
	sesssetup_common_strings(req, 
				 &sess->nt1.out.os,
				 &sess->nt1.out.lanman,
				 &sess->nt1.out.domain);
	
	req->session = smbsrv_session_find(req->smb_conn, sess->nt1.out.vuid);
	if (session_info->server_info->guest) {
		return NT_STATUS_OK;
	}
	if (!srv_setup_signing(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2)) {
		/* Already signing, or disabled */
		return NT_STATUS_OK;
	}
		
	/* Force check of the request packet, now we know the session key */
	req_signing_check_incoming(req);

	srv_signing_restart(req->smb_conn,  &session_info->session_key, &sess->nt1.in.password2);

	return NT_STATUS_OK;
}


/*
  handler for SPNEGO style session setup
*/
static NTSTATUS sesssetup_spnego(struct smbsrv_request *req, union smb_sesssetup *sess)
{
	NTSTATUS status = NT_STATUS_ACCESS_DENIED;
	struct smbsrv_session *smb_sess;
	struct gensec_security *gensec_ctx = NULL;
	struct auth_session_info *session_info = NULL;
	uint16_t vuid;

	if (!req->smb_conn->negotiate.done_sesssetup) {
		req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
		req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
	}

	vuid = SVAL(req->in.hdr,HDR_UID);
	smb_sess = smbsrv_session_find(req->smb_conn, vuid);
	if (smb_sess) {
		if (!smb_sess->gensec_ctx) {
			return NT_STATUS_INVALID_HANDLE;
		}

		/* what is when the client is already successful authentificated? */
		if (smb_sess->session_info) {
			return NT_STATUS_ACCESS_DENIED;
		}

		status = gensec_update(smb_sess->gensec_ctx, req->mem_ctx, sess->spnego.in.secblob, &sess->spnego.out.secblob);
	} else {
		status = gensec_server_start(&gensec_ctx);
		if (!NT_STATUS_IS_OK(status)) {
			DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
			return status;
		}

		gensec_want_feature(gensec_ctx, GENSEC_WANT_SESSION_KEY);

		status = gensec_start_mech_by_oid(gensec_ctx, OID_SPNEGO);
		if (!NT_STATUS_IS_OK(status)) {
			DEBUG(1, ("Failed to start GENSEC SPNEGO server code: %s\n", nt_errstr(status)));
			return status;
		}

		status = gensec_update(gensec_ctx, req->mem_ctx, sess->spnego.in.secblob, &sess->spnego.out.secblob);

	}

	if (!smb_sess) {
		vuid = smbsrv_register_session(req->smb_conn, session_info, gensec_ctx);
		if (vuid == UID_FIELD_INVALID) {
			return NT_STATUS_ACCESS_DENIED;
		}
		smb_sess = smbsrv_session_find(req->smb_conn, vuid);
		if (!smb_sess) {
			return NT_STATUS_FOOBAR;
		}
	}

	if (NT_STATUS_IS_OK(status)) {
		DATA_BLOB session_key;
		
		status = gensec_session_info(smb_sess->gensec_ctx, &smb_sess->session_info);
		if (!NT_STATUS_IS_OK(status)) {
			return status;
		}
		
		status = gensec_session_key(smb_sess->gensec_ctx, 
					    &session_key);
		if (NT_STATUS_IS_OK(status) 
		    && !smb_sess->session_info->server_info->guest
		    && srv_setup_signing(req->smb_conn, &session_key, NULL)) {
			/* Force check of the request packet, now we know the session key */
			req_signing_check_incoming(req);

			srv_signing_restart(req->smb_conn, &session_key, NULL);

		}
	} else {
		status = nt_status_squash(status);
	}

	sess->spnego.out.action = 0;
	sess->spnego.out.vuid = vuid;
	sesssetup_common_strings(req, 
				 &sess->spnego.out.os,
				 &sess->spnego.out.lanman,
				 &sess->spnego.out.domain);

	return status;
}

/*
  backend for sessionsetup call - this takes all 3 variants of the call
*/
NTSTATUS sesssetup_backend(struct smbsrv_request *req, 
			   union smb_sesssetup *sess)
{
	NTSTATUS status = NT_STATUS_INVALID_LEVEL;

	switch (sess->generic.level) {
		case RAW_SESSSETUP_GENERIC:
			status = NT_STATUS_INVALID_LEVEL;
			break;
		case RAW_SESSSETUP_OLD:
			status = sesssetup_old(req, sess);
			break;
		case RAW_SESSSETUP_NT1:
			status = sesssetup_nt1(req, sess);
			break;
		case RAW_SESSSETUP_SPNEGO:
			status = sesssetup_spnego(req, sess);
			break;
	}

	if (NT_STATUS_IS_OK(status)) {
		req->smb_conn->negotiate.done_sesssetup = True;
	}

	return status;
}