r7633: this patch started as an attempt to make the dcerpc code use a given
[kai/samba.git] / source4 / smb_server / sesssetup.c
1 /* 
2    Unix SMB/CIFS implementation.
3    handle SMBsessionsetup
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
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.
13    
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.
18    
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.
22 */
23
24 #include "includes.h"
25 #include "version.h"
26 #include "auth/auth.h"
27 #include "smb_server/smb_server.h"
28 #include "smbd/service_stream.h"
29 #include "libcli/nbt/libnbt.h"
30
31 /*
32   setup the OS, Lanman and domain portions of a session setup reply
33 */
34 static void sesssetup_common_strings(struct smbsrv_request *req,
35                                      char **os, char **lanman, char **domain)
36 {
37         (*os) = talloc_asprintf(req, "Unix");
38         (*lanman) = talloc_asprintf(req, "Samba %s", SAMBA_VERSION_STRING);
39         (*domain) = talloc_asprintf(req, "%s", lp_workgroup());
40 }
41
42
43 /*
44   handler for old style session setup
45 */
46 static NTSTATUS sesssetup_old(struct smbsrv_request *req, union smb_sesssetup *sess)
47 {
48         NTSTATUS status;
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         char *remote_machine;
54         TALLOC_CTX *mem_ctx;
55
56         sess->old.out.vuid = UID_FIELD_INVALID;
57         sess->old.out.action = 0;
58
59         mem_ctx = talloc_named(req, 0, "OLD session setup");
60         NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
61
62         if (!req->smb_conn->negotiate.done_sesssetup) {
63                 req->smb_conn->negotiate.max_send = sess->old.in.bufsize;
64         }
65         
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,
69                                               remote_machine,
70                                               sess->old.in.password,
71                                               data_blob(NULL, 0),
72                                               &user_info);
73         if (!NT_STATUS_IS_OK(status)) {
74                 talloc_free(mem_ctx);
75                 return NT_STATUS_ACCESS_DENIED;
76         }
77
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)) {
81                 talloc_free(mem_ctx);
82                 return auth_nt_status_squash(status);
83         }
84
85         /* This references server_info into session_info */
86         status = auth_generate_session_info(req, server_info, &session_info);
87         talloc_free(mem_ctx);
88         if (!NT_STATUS_IS_OK(status)) {
89                 return auth_nt_status_squash(status);
90         }
91
92         smb_sess = smbsrv_register_session(req->smb_conn, session_info, NULL);
93         if (!smb_sess) {
94                 return NT_STATUS_ACCESS_DENIED;
95         }
96
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;
100
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;
105
106         sesssetup_common_strings(req, 
107                                  &sess->old.out.os,
108                                  &sess->old.out.lanman,
109                                  &sess->old.out.domain);
110
111         return NT_STATUS_OK;
112 }
113
114
115 /*
116   handler for NT1 style session setup
117 */
118 static NTSTATUS sesssetup_nt1(struct smbsrv_request *req, union smb_sesssetup *sess)
119 {
120         NTSTATUS status;
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;
125         TALLOC_CTX *mem_ctx;
126         
127         sess->nt1.out.vuid = UID_FIELD_INVALID;
128         sess->nt1.out.action = 0;
129
130         mem_ctx = talloc_named(req, 0, "NT1 session setup");
131         NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
132
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;
136         }
137
138         if (req->smb_conn->negotiate.spnego_negotiated) {
139                 struct auth_context *auth_context;
140
141                 if (sess->nt1.in.user && *sess->nt1.in.user) {
142                         return NT_STATUS_ACCESS_DENIED;
143                 }
144
145                 status = make_user_info_anonymous(req->smb_conn, &user_info);
146                 if (!NT_STATUS_IS_OK(status)) {
147                         talloc_free(mem_ctx);
148                         return status;
149                 }
150
151                 /* TODO: should we use just "anonymous" here? */
152                 status = auth_context_create(req->smb_conn, lp_auth_methods(), 
153                                              &auth_context, 
154                                              req->smb_conn->connection->event.ctx);
155                 if (!NT_STATUS_IS_OK(status)) {
156                         talloc_free(mem_ctx);
157                         return status;
158                 }
159
160                 status = auth_check_password(auth_context, mem_ctx,
161                                              user_info, &server_info);
162         } else {
163                 const char *remote_machine = NULL;
164
165                 if (req->smb_conn->negotiate.called_name) {
166                         remote_machine = req->smb_conn->negotiate.called_name->name;
167                 }
168
169                 if (!remote_machine) {
170                         remote_machine = socket_get_peer_addr(req->smb_conn->connection->socket, mem_ctx);
171                 }
172
173                 status = make_user_info_for_reply_enc(req->smb_conn, 
174                                                       sess->nt1.in.user, sess->nt1.in.domain,
175                                                       remote_machine,
176                                                       sess->nt1.in.password1,
177                                                       sess->nt1.in.password2,
178                                                       &user_info);
179                 if (!NT_STATUS_IS_OK(status)) {
180                         talloc_free(mem_ctx);
181                         return NT_STATUS_ACCESS_DENIED;
182                 }
183                 
184                 status = auth_check_password(req->smb_conn->negotiate.auth_context, 
185                                              req, user_info, &server_info);
186         }
187
188         if (!NT_STATUS_IS_OK(status)) {
189                 talloc_free(mem_ctx);
190                 return auth_nt_status_squash(status);
191         }
192
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);
198         }
199
200         smb_sess = smbsrv_register_session(req->smb_conn, session_info, NULL);
201         talloc_free(mem_ctx);
202         if (!smb_sess) {
203                 return NT_STATUS_ACCESS_DENIED;
204         }
205
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;
209
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;
214
215         sesssetup_common_strings(req, 
216                                  &sess->nt1.out.os,
217                                  &sess->nt1.out.lanman,
218                                  &sess->nt1.out.domain);
219         
220         req->session = smbsrv_session_find(req->smb_conn, sess->nt1.out.vuid);
221         if (!session_info->server_info->authenticated) {
222                 return NT_STATUS_OK;
223         }
224
225
226         if (!srv_setup_signing(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2)) {
227                 /* Already signing, or disabled */
228                 return NT_STATUS_OK;
229         }
230
231         /* Force check of the request packet, now we know the session key */
232         req_signing_check_incoming(req);
233
234         /* Unfortunetly win2k3 as a client doesn't sign the request
235          * packet here, so we have to force signing to start again */
236
237         srv_signing_restart(req->smb_conn,  &session_info->session_key, &sess->nt1.in.password2);
238
239         return NT_STATUS_OK;
240 }
241
242
243 /*
244   handler for SPNEGO style session setup
245 */
246 static NTSTATUS sesssetup_spnego(struct smbsrv_request *req, union smb_sesssetup *sess)
247 {
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;
252         uint16_t vuid;
253
254         sess->spnego.out.vuid = UID_FIELD_INVALID;
255         sess->spnego.out.action = 0;
256
257         sesssetup_common_strings(req, 
258                                  &sess->spnego.out.os,
259                                  &sess->spnego.out.lanman,
260                                  &sess->spnego.out.workgroup);
261
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;
265         }
266
267         vuid = SVAL(req->in.hdr,HDR_UID);
268         smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
269         if (smb_sess) {
270                 gensec_ctx = smb_sess->gensec_ctx;
271                 status = gensec_update(gensec_ctx, req, sess->spnego.in.secblob, &sess->spnego.out.secblob);
272         } else {
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)));
277                         return status;
278                 }
279
280                 gensec_set_target_service(gensec_ctx, "cifs");
281
282                 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
283
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)));
287                         return status;
288                 }
289
290                 status = gensec_update(gensec_ctx, req, sess->spnego.in.secblob, &sess->spnego.out.secblob);
291         }
292
293         if (NT_STATUS_IS_OK(status)) {
294                 DATA_BLOB session_key;
295                 
296                 status = gensec_session_info(gensec_ctx, &session_info);
297                 if (!NT_STATUS_IS_OK(status)) {
298                         talloc_free(smb_sess);
299                         return status;
300                 }
301                 
302                 status = gensec_session_key(gensec_ctx, 
303                                             &session_key);
304
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);
310
311                         srv_signing_restart(req->smb_conn, &session_key, NULL);
312                 }
313
314         } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
315         } else {
316                 status = auth_nt_status_squash(status);
317                 
318                 /* This invalidates the VUID of the failed login */
319                 talloc_free(smb_sess);
320                 return status;
321         }
322                 
323         if (!smb_sess) {
324                 smb_sess = smbsrv_register_session(req->smb_conn, 
325                                                    session_info, gensec_ctx);
326                 if (!smb_sess) {
327                         return NT_STATUS_ACCESS_DENIED;
328                 }
329                 req->session = smb_sess;
330         } else {
331                 smb_sess->session_info = talloc_reference(smb_sess, session_info);
332         }
333
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;
338         }
339         sess->spnego.out.vuid = smb_sess->vuid;
340
341         return status;
342 }
343
344 /*
345   backend for sessionsetup call - this takes all 3 variants of the call
346 */
347 NTSTATUS sesssetup_backend(struct smbsrv_request *req, 
348                            union smb_sesssetup *sess)
349 {
350         NTSTATUS status = NT_STATUS_INVALID_LEVEL;
351
352         switch (sess->old.level) {
353                 case RAW_SESSSETUP_OLD:
354                         status = sesssetup_old(req, sess);
355                         break;
356                 case RAW_SESSSETUP_NT1:
357                         status = sesssetup_nt1(req, sess);
358                         break;
359                 case RAW_SESSSETUP_SPNEGO:
360                         status = sesssetup_spnego(req, sess);
361                         break;
362         }
363
364         if (NT_STATUS_IS_OK(status)) {
365                 req->smb_conn->negotiate.done_sesssetup = True;
366         }
367
368         return status;
369 }
370
371