r14542: Remove librpc, libndr and libnbt from includes.h
[samba.git] / source / smb_server / smb / sesssetup.c
1
2 /* 
3    Unix SMB/CIFS implementation.
4    handle SMBsessionsetup
5    Copyright (C) Andrew Tridgell                      1998-2001
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
7    Copyright (C) Jim McDonough                        2002
8    Copyright (C) Luke Howard                          2003
9    Copyright (C) Stefan Metzmacher                    2005
10    
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #include "includes.h"
27 #include "version.h"
28 #include "auth/auth.h"
29 #include "smb_server/smb_server.h"
30 #include "smbd/service_stream.h"
31 #include "librpc/gen_ndr/nbt.h"
32
33 /*
34   setup the OS, Lanman and domain portions of a session setup reply
35 */
36 static void sesssetup_common_strings(struct smbsrv_request *req,
37                                      char **os, char **lanman, char **domain)
38 {
39         (*os) = talloc_asprintf(req, "Unix");
40         (*lanman) = talloc_asprintf(req, "Samba %s", SAMBA_VERSION_STRING);
41         (*domain) = talloc_asprintf(req, "%s", lp_workgroup());
42 }
43
44
45 /*
46   handler for old style session setup
47 */
48 static NTSTATUS sesssetup_old(struct smbsrv_request *req, union smb_sesssetup *sess)
49 {
50         NTSTATUS status;
51         struct auth_usersupplied_info *user_info = NULL;
52         struct auth_serversupplied_info *server_info = NULL;
53         struct auth_session_info *session_info;
54         struct smbsrv_session *smb_sess;
55         struct socket_address *remote_address;
56         const char *remote_machine = NULL;
57
58         sess->old.out.vuid = 0;
59         sess->old.out.action = 0;
60
61         if (!req->smb_conn->negotiate.done_sesssetup) {
62                 req->smb_conn->negotiate.max_send = sess->old.in.bufsize;
63         }
64         
65         if (req->smb_conn->negotiate.calling_name) {
66                 remote_machine = req->smb_conn->negotiate.calling_name->name;
67         }
68         
69         remote_address = socket_get_peer_addr(req->smb_conn->connection->socket, req);
70         NT_STATUS_HAVE_NO_MEMORY(remote_address);
71
72         if (!remote_machine) {
73                 remote_machine = remote_address->addr;
74         }
75
76         user_info = talloc(req, struct auth_usersupplied_info);
77         NT_STATUS_HAVE_NO_MEMORY(user_info);
78         
79         user_info->mapped_state = False;
80         user_info->logon_parameters = 0;
81         user_info->flags = 0;
82         user_info->client.account_name = sess->old.in.user;
83         user_info->client.domain_name = sess->old.in.domain;
84         user_info->workstation_name = remote_machine;
85         user_info->remote_host = talloc_steal(user_info, remote_address);
86         
87         user_info->password_state = AUTH_PASSWORD_RESPONSE;
88         user_info->password.response.lanman = sess->old.in.password;
89         user_info->password.response.lanman.data = talloc_steal(user_info, sess->old.in.password.data);
90         user_info->password.response.nt = data_blob(NULL, 0);
91
92         status = auth_check_password(req->smb_conn->negotiate.auth_context,
93                                      req, user_info, &server_info);
94         if (!NT_STATUS_IS_OK(status)) {
95                 return auth_nt_status_squash(status);
96         }
97
98         /* This references server_info into session_info */
99         status = auth_generate_session_info(req, server_info, &session_info);
100         if (!NT_STATUS_IS_OK(status)) {
101                 return auth_nt_status_squash(status);
102         }
103
104         /* allocate a new session */
105         smb_sess = smbsrv_session_new(req->smb_conn, NULL);
106         if (!smb_sess) {
107                 return NT_STATUS_ACCESS_DENIED;
108         }
109
110         /* Ensure this is marked as a 'real' vuid, not one
111          * simply valid for the session setup leg */
112         status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
113         if (!NT_STATUS_IS_OK(status)) {
114                 return auth_nt_status_squash(status);
115         }
116
117         /* To correctly process any AndX packet (like a tree connect)
118          * we need to fill in the session on the request here */
119         req->session = smb_sess;
120         sess->old.out.vuid = smb_sess->vuid;
121
122         sesssetup_common_strings(req, 
123                                  &sess->old.out.os,
124                                  &sess->old.out.lanman,
125                                  &sess->old.out.domain);
126
127         return NT_STATUS_OK;
128 }
129
130
131 /*
132   handler for NT1 style session setup
133 */
134 static NTSTATUS sesssetup_nt1(struct smbsrv_request *req, union smb_sesssetup *sess)
135 {
136         NTSTATUS status;
137         struct auth_context *auth_context;
138         struct auth_usersupplied_info *user_info = NULL;
139         struct auth_serversupplied_info *server_info = NULL;
140         struct auth_session_info *session_info;
141         struct smbsrv_session *smb_sess;
142         struct socket_address *remote_address;
143         const char *remote_machine = NULL;
144         
145         sess->nt1.out.vuid = 0;
146         sess->nt1.out.action = 0;
147
148         if (!req->smb_conn->negotiate.done_sesssetup) {
149                 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
150                 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
151         }
152
153         if (req->smb_conn->negotiate.oid) {
154                 if (sess->nt1.in.user && *sess->nt1.in.user) {
155                         /* We can't accept a normal login, because we
156                          * don't have a challenge */
157                         return NT_STATUS_LOGON_FAILURE;
158                 }
159
160                 /* TODO: should we use just "anonymous" here? */
161                 status = auth_context_create(req, lp_auth_methods(), 
162                                              &auth_context, 
163                                              req->smb_conn->connection->event.ctx);
164                 NT_STATUS_NOT_OK_RETURN(status);
165         } else {
166                 auth_context = req->smb_conn->negotiate.auth_context;
167         }
168
169         if (req->smb_conn->negotiate.calling_name) {
170                 remote_machine = req->smb_conn->negotiate.calling_name->name;
171         }
172
173         remote_address = socket_get_peer_addr(req->smb_conn->connection->socket, req);
174         NT_STATUS_HAVE_NO_MEMORY(remote_address);
175
176         if (!remote_machine) {
177                 remote_machine = remote_address->addr;
178         }
179
180         user_info = talloc(req, struct auth_usersupplied_info);
181         NT_STATUS_HAVE_NO_MEMORY(user_info);
182         
183         user_info->mapped_state = False;
184         user_info->logon_parameters = 0;
185         user_info->flags = 0;
186         user_info->client.account_name = sess->nt1.in.user;
187         user_info->client.domain_name = sess->nt1.in.domain;
188         user_info->workstation_name = remote_machine;
189         user_info->remote_host = talloc_steal(user_info, remote_address);
190         
191         user_info->password_state = AUTH_PASSWORD_RESPONSE;
192         user_info->password.response.lanman = sess->nt1.in.password1;
193         user_info->password.response.lanman.data = talloc_steal(user_info, sess->nt1.in.password1.data);
194         user_info->password.response.nt = sess->nt1.in.password2;
195         user_info->password.response.nt.data = talloc_steal(user_info, sess->nt1.in.password2.data);
196
197         status = auth_check_password(auth_context, req, user_info, &server_info);
198         if (!NT_STATUS_IS_OK(status)) {
199                 return auth_nt_status_squash(status);
200         }
201
202         /* This references server_info into session_info */
203         status = auth_generate_session_info(req, server_info, &session_info);
204         if (!NT_STATUS_IS_OK(status)) {
205                 return auth_nt_status_squash(status);
206         }
207
208         /* allocate a new session */
209         smb_sess = smbsrv_session_new(req->smb_conn, NULL);
210         if (!smb_sess) {
211                 return NT_STATUS_ACCESS_DENIED;
212         }
213
214         /* Ensure this is marked as a 'real' vuid, not one
215          * simply valid for the session setup leg */
216         status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
217         if (!NT_STATUS_IS_OK(status)) {
218                 return auth_nt_status_squash(status);
219         }
220
221         /* To correctly process any AndX packet (like a tree connect)
222          * we need to fill in the session on the request here */
223         req->session = smb_sess;
224         sess->nt1.out.vuid = smb_sess->vuid;
225
226         sesssetup_common_strings(req, 
227                                  &sess->nt1.out.os,
228                                  &sess->nt1.out.lanman,
229                                  &sess->nt1.out.domain);
230
231         if (!session_info->server_info->authenticated) {
232                 return NT_STATUS_OK;
233         }
234
235         if (!smbsrv_setup_signing(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2)) {
236                 /* Already signing, or disabled */
237                 return NT_STATUS_OK;
238         }
239
240         /* Force check of the request packet, now we know the session key */
241         smbsrv_signing_check_incoming(req);
242 /* TODO: why don't we check the result here? */
243
244         /* Unfortunetly win2k3 as a client doesn't sign the request
245          * packet here, so we have to force signing to start again */
246
247         smbsrv_signing_restart(req->smb_conn,  &session_info->session_key, &sess->nt1.in.password2);
248
249         return NT_STATUS_OK;
250 }
251
252
253 /*
254   handler for SPNEGO style session setup
255 */
256 static NTSTATUS sesssetup_spnego(struct smbsrv_request *req, union smb_sesssetup *sess)
257 {
258         NTSTATUS status;
259         NTSTATUS skey_status;
260         struct smbsrv_session *smb_sess = NULL;
261         struct auth_session_info *session_info = NULL;
262         DATA_BLOB session_key;
263         uint16_t vuid;
264
265         sess->spnego.out.vuid = 0;
266         sess->spnego.out.action = 0;
267
268         sesssetup_common_strings(req, 
269                                  &sess->spnego.out.os,
270                                  &sess->spnego.out.lanman,
271                                  &sess->spnego.out.workgroup);
272
273         if (!req->smb_conn->negotiate.done_sesssetup) {
274                 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
275                 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
276         }
277
278         vuid = SVAL(req->in.hdr,HDR_UID);
279
280         /* lookup an existing session */
281         smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
282         if (!smb_sess) {
283                 struct gensec_security *gensec_ctx;
284
285                 status = gensec_server_start(req, &gensec_ctx,
286                                              req->smb_conn->connection->event.ctx);
287                 if (!NT_STATUS_IS_OK(status)) {
288                         DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
289                         return status;
290                 }
291
292                 gensec_set_credentials(gensec_ctx, req->smb_conn->negotiate.server_credentials);
293
294                 gensec_set_target_service(gensec_ctx, "cifs");
295
296                 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
297
298                 status = gensec_start_mech_by_oid(gensec_ctx, req->smb_conn->negotiate.oid);
299                 if (!NT_STATUS_IS_OK(status)) {
300                         DEBUG(1, ("Failed to start GENSEC %s server code: %s\n", 
301                                   gensec_get_name_by_oid(req->smb_conn->negotiate.oid), nt_errstr(status)));
302                         return status;
303                 }
304
305                 /* allocate a new session */
306                 smb_sess = smbsrv_session_new(req->smb_conn, gensec_ctx);
307         }
308
309         if (!smb_sess) {
310                 return NT_STATUS_ACCESS_DENIED;
311         }
312
313         if (!smb_sess->gensec_ctx) {
314                 status = NT_STATUS_INTERNAL_ERROR;
315                 DEBUG(1, ("Internal ERROR: no gensec_ctx on session: %s\n", nt_errstr(status)));
316                 goto failed;
317         }
318
319         status = gensec_update(smb_sess->gensec_ctx, req, sess->spnego.in.secblob, &sess->spnego.out.secblob);
320         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
321                 sess->spnego.out.vuid = smb_sess->vuid;
322                 return status;
323         } else if (!NT_STATUS_IS_OK(status)) {
324                 goto failed;
325         }
326
327         status = gensec_session_info(smb_sess->gensec_ctx, &session_info);
328         if (!NT_STATUS_IS_OK(status)) {
329                 goto failed;
330         }
331
332         skey_status = gensec_session_key(smb_sess->gensec_ctx, &session_key);
333         if (NT_STATUS_IS_OK(skey_status) &&
334             session_info->server_info->authenticated &&
335             smbsrv_setup_signing(req->smb_conn, &session_key, NULL)) {
336                 /* Force check of the request packet, now we know the session key */
337                 smbsrv_signing_check_incoming(req);
338
339                 smbsrv_signing_restart(req->smb_conn, &session_key, NULL);
340         }
341
342         /* Ensure this is marked as a 'real' vuid, not one
343          * simply valid for the session setup leg */
344         status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
345         if (!NT_STATUS_IS_OK(status)) {
346                 goto failed;
347         }
348         req->session = smb_sess;
349
350         sess->spnego.out.vuid = smb_sess->vuid;
351         return NT_STATUS_OK;
352
353 failed:
354         talloc_free(smb_sess);
355         return auth_nt_status_squash(status);
356 }
357
358 /*
359   backend for sessionsetup call - this takes all 3 variants of the call
360 */
361 NTSTATUS smbsrv_sesssetup_backend(struct smbsrv_request *req,
362                                   union smb_sesssetup *sess)
363 {
364         NTSTATUS status = NT_STATUS_INVALID_LEVEL;
365
366         switch (sess->old.level) {
367                 case RAW_SESSSETUP_OLD:
368                         status = sesssetup_old(req, sess);
369                         break;
370                 case RAW_SESSSETUP_NT1:
371                         status = sesssetup_nt1(req, sess);
372                         break;
373                 case RAW_SESSSETUP_SPNEGO:
374                         status = sesssetup_spnego(req, sess);
375                         break;
376         }
377
378         if (NT_STATUS_IS_OK(status)) {
379                 req->smb_conn->negotiate.done_sesssetup = True;
380         }
381
382         return status;
383 }