88777d9673c1fa5b5c37d8dd0efe212f5702fa79
[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      2001
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
26 /*
27   setup the OS, Lanman and domain portions of a session setup reply
28 */
29 static void sesssetup_common_strings(struct smbsrv_request *req,
30                                      char **os, char **lanman, char **domain)
31 {
32         (*os) = talloc_asprintf(req, "Unix");
33         (*lanman) = talloc_asprintf(req, "Samba %s", SAMBA_VERSION_STRING);
34         (*domain) = talloc_asprintf(req, "%s", lp_workgroup());
35 }
36
37
38 /*
39   handler for old style session setup
40 */
41 static NTSTATUS sesssetup_old(struct smbsrv_request *req, union smb_sesssetup *sess)
42 {
43         NTSTATUS status;
44         struct auth_usersupplied_info *user_info = NULL;
45         struct auth_serversupplied_info *server_info = NULL;
46         struct auth_session_info *session_info;
47
48         if (!req->smb_conn->negotiate.done_sesssetup) {
49                 req->smb_conn->negotiate.max_send = sess->old.in.bufsize;
50         }
51
52         status = make_user_info_for_reply_enc(&user_info, 
53                                               sess->old.in.user, sess->old.in.domain,
54                                               sess->old.in.password,
55                                               data_blob(NULL, 0));
56         if (!NT_STATUS_IS_OK(status)) {
57                 return NT_STATUS_ACCESS_DENIED;
58         }
59
60         status = req->smb_conn->negotiate.auth_context->check_ntlm_password(req->smb_conn->negotiate.auth_context, 
61                                                                        user_info, 
62                                                                        &server_info);
63         if (!NT_STATUS_IS_OK(status)) {
64                 return nt_status_squash(status);
65         }
66
67         status = make_session_info(server_info, &session_info);
68         if (!NT_STATUS_IS_OK(status)) {
69                 return nt_status_squash(status);
70         }
71
72         sess->old.out.action = 0;
73         sess->old.out.vuid = smbsrv_register_session(req->smb_conn, session_info, NULL);
74         if (sess->old.out.vuid == UID_FIELD_INVALID) {
75                 return NT_STATUS_ACCESS_DENIED;
76         }
77         sesssetup_common_strings(req, 
78                                  &sess->old.out.os,
79                                  &sess->old.out.lanman,
80                                  &sess->old.out.domain);
81
82         req->session = smbsrv_session_find(req->smb_conn, sess->old.out.vuid);
83
84         return NT_STATUS_OK;
85 }
86
87
88 /*
89   handler for NT1 style session setup
90 */
91 static NTSTATUS sesssetup_nt1(struct smbsrv_request *req, union smb_sesssetup *sess)
92 {
93         NTSTATUS status;
94         struct auth_usersupplied_info *user_info = NULL;
95         struct auth_serversupplied_info *server_info = NULL;
96         struct auth_session_info *session_info;
97
98         if (!req->smb_conn->negotiate.done_sesssetup) {
99                 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
100                 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
101         }
102
103         if (req->smb_conn->negotiate.spnego_negotiated) {
104                 struct auth_context *auth_context;
105
106                 if (sess->nt1.in.user && *sess->nt1.in.user) {
107                         return NT_STATUS_ACCESS_DENIED;
108                 } else {
109                         make_user_info_guest(&user_info);
110                 }
111                 
112                 status = make_auth_context_subsystem(&auth_context);
113
114                 if (!NT_STATUS_IS_OK(status)) {
115                         return status;
116                 }
117                 
118                 status = auth_context->check_ntlm_password(auth_context, 
119                                                            user_info, 
120                                                            &server_info);
121                 
122                 free_auth_context(&auth_context);
123
124         } else {
125                 status = make_user_info_for_reply_enc(&user_info, 
126                                                       sess->nt1.in.user, sess->nt1.in.domain,
127                                                       sess->nt1.in.password1,
128                                                       sess->nt1.in.password2);
129                 if (!NT_STATUS_IS_OK(status)) {
130                         return NT_STATUS_ACCESS_DENIED;
131                 }
132                 
133                 status = req->smb_conn->negotiate
134                         .auth_context->check_ntlm_password(req->smb_conn->negotiate
135                                                            .auth_context, 
136                                                            user_info, 
137                                                            &server_info);
138         }
139
140         if (!NT_STATUS_IS_OK(status)) {
141                 return nt_status_squash(status);
142         }
143
144         status = make_session_info(server_info, &session_info);
145         if (!NT_STATUS_IS_OK(status)) {
146                 return nt_status_squash(status);
147         }
148
149         sess->nt1.out.action = 0;
150         sess->nt1.out.vuid = smbsrv_register_session(req->smb_conn, session_info, NULL);
151         if (sess->nt1.out.vuid == UID_FIELD_INVALID) {
152                 return NT_STATUS_ACCESS_DENIED;
153         }
154         sesssetup_common_strings(req, 
155                                  &sess->nt1.out.os,
156                                  &sess->nt1.out.lanman,
157                                  &sess->nt1.out.domain);
158         
159         req->session = smbsrv_session_find(req->smb_conn, sess->nt1.out.vuid);
160         if (session_info->server_info->guest) {
161                 return NT_STATUS_OK;
162         }
163         if (!srv_setup_signing(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2)) {
164                 /* Already signing, or disabled */
165                 return NT_STATUS_OK;
166         }
167                 
168         /* Force check of the request packet, now we know the session key */
169         req_signing_check_incoming(req);
170
171         srv_signing_restart(req->smb_conn,  &session_info->session_key, &sess->nt1.in.password2);
172
173         return NT_STATUS_OK;
174 }
175
176
177 /*
178   handler for SPNEGO style session setup
179 */
180 static NTSTATUS sesssetup_spnego(struct smbsrv_request *req, union smb_sesssetup *sess)
181 {
182         NTSTATUS status = NT_STATUS_ACCESS_DENIED;
183         struct smbsrv_session *smb_sess;
184         struct gensec_security *gensec_ctx = NULL;
185         struct auth_session_info *session_info = NULL;
186         uint16_t vuid;
187
188         if (!req->smb_conn->negotiate.done_sesssetup) {
189                 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
190                 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
191         }
192
193         vuid = SVAL(req->in.hdr,HDR_UID);
194         smb_sess = smbsrv_session_find(req->smb_conn, vuid);
195         if (smb_sess) {
196                 if (!smb_sess->gensec_ctx) {
197                         return NT_STATUS_INVALID_HANDLE;
198                 }
199
200                 /* what is when the client is already successful authentificated? */
201                 if (smb_sess->session_info) {
202                         return NT_STATUS_ACCESS_DENIED;
203                 }
204
205                 status = gensec_update(smb_sess->gensec_ctx, req, sess->spnego.in.secblob, &sess->spnego.out.secblob);
206         } else {
207                 status = gensec_server_start(&gensec_ctx);
208                 if (!NT_STATUS_IS_OK(status)) {
209                         DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
210                         return status;
211                 }
212
213                 gensec_want_feature(gensec_ctx, GENSEC_WANT_SESSION_KEY);
214
215                 status = gensec_start_mech_by_oid(gensec_ctx, OID_SPNEGO);
216                 if (!NT_STATUS_IS_OK(status)) {
217                         DEBUG(1, ("Failed to start GENSEC SPNEGO server code: %s\n", nt_errstr(status)));
218                         return status;
219                 }
220
221                 status = gensec_update(gensec_ctx, req, sess->spnego.in.secblob, &sess->spnego.out.secblob);
222
223         }
224
225         if (!smb_sess) {
226                 vuid = smbsrv_register_session(req->smb_conn, session_info, gensec_ctx);
227                 if (vuid == UID_FIELD_INVALID) {
228                         return NT_STATUS_ACCESS_DENIED;
229                 }
230                 smb_sess = smbsrv_session_find(req->smb_conn, vuid);
231                 if (!smb_sess) {
232                         return NT_STATUS_FOOBAR;
233                 }
234         }
235
236         if (NT_STATUS_IS_OK(status)) {
237                 DATA_BLOB session_key;
238                 
239                 status = gensec_session_info(smb_sess->gensec_ctx, &smb_sess->session_info);
240                 if (!NT_STATUS_IS_OK(status)) {
241                         return status;
242                 }
243                 
244                 status = gensec_session_key(smb_sess->gensec_ctx, 
245                                             &session_key);
246                 if (NT_STATUS_IS_OK(status) 
247                     && !smb_sess->session_info->server_info->guest
248                     && srv_setup_signing(req->smb_conn, &session_key, NULL)) {
249                         /* Force check of the request packet, now we know the session key */
250                         req_signing_check_incoming(req);
251
252                         srv_signing_restart(req->smb_conn, &session_key, NULL);
253
254                 }
255         } else {
256                 status = nt_status_squash(status);
257         }
258
259         sess->spnego.out.action = 0;
260         sess->spnego.out.vuid = vuid;
261         sesssetup_common_strings(req, 
262                                  &sess->spnego.out.os,
263                                  &sess->spnego.out.lanman,
264                                  &sess->spnego.out.domain);
265
266         return status;
267 }
268
269 /*
270   backend for sessionsetup call - this takes all 3 variants of the call
271 */
272 NTSTATUS sesssetup_backend(struct smbsrv_request *req, 
273                            union smb_sesssetup *sess)
274 {
275         NTSTATUS status = NT_STATUS_INVALID_LEVEL;
276
277         switch (sess->generic.level) {
278                 case RAW_SESSSETUP_GENERIC:
279                         status = NT_STATUS_INVALID_LEVEL;
280                         break;
281                 case RAW_SESSSETUP_OLD:
282                         status = sesssetup_old(req, sess);
283                         break;
284                 case RAW_SESSSETUP_NT1:
285                         status = sesssetup_nt1(req, sess);
286                         break;
287                 case RAW_SESSSETUP_SPNEGO:
288                         status = sesssetup_spnego(req, sess);
289                         break;
290         }
291
292         if (NT_STATUS_IS_OK(status)) {
293                 req->smb_conn->negotiate.done_sesssetup = True;
294         }
295
296         return status;
297 }
298
299