s3:smbd: release the share mode lock before calling exit_server() (bug #9191)
[kai/samba-autobuild/.git] / source3 / smbd / smb2_tcon.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../libcli/security/security.h"
26 #include "auth.h"
27 #include "lib/param/loadparm.h"
28 #include "../lib/util/tevent_ntstatus.h"
29
30 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
31                                         struct tevent_context *ev,
32                                         struct smbd_smb2_request *smb2req,
33                                         const char *in_path);
34 static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
35                                             uint8_t *out_share_type,
36                                             uint32_t *out_share_flags,
37                                             uint32_t *out_capabilities,
38                                             uint32_t *out_maximal_access,
39                                             uint32_t *out_tree_id);
40
41 static void smbd_smb2_request_tcon_done(struct tevent_req *subreq);
42
43 NTSTATUS smbd_smb2_request_process_tcon(struct smbd_smb2_request *req)
44 {
45         const uint8_t *inbody;
46         uint16_t in_path_offset;
47         uint16_t in_path_length;
48         DATA_BLOB in_path_buffer;
49         char *in_path_string;
50         size_t in_path_string_size;
51         NTSTATUS status;
52         bool ok;
53         struct tevent_req *subreq;
54
55         status = smbd_smb2_request_verify_sizes(req, 0x09);
56         if (!NT_STATUS_IS_OK(status)) {
57                 return smbd_smb2_request_error(req, status);
58         }
59         inbody = SMBD_SMB2_IN_BODY_PTR(req);
60
61         in_path_offset = SVAL(inbody, 0x04);
62         in_path_length = SVAL(inbody, 0x06);
63
64         if (in_path_offset != (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req))) {
65                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
66         }
67
68         if (in_path_length > SMBD_SMB2_IN_DYN_LEN(req)) {
69                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
70         }
71
72         in_path_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
73         in_path_buffer.length = in_path_length;
74
75         ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
76                                    in_path_buffer.data,
77                                    in_path_buffer.length,
78                                    &in_path_string,
79                                    &in_path_string_size);
80         if (!ok) {
81                 return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
82         }
83
84         if (in_path_buffer.length == 0) {
85                 in_path_string_size = 0;
86         }
87
88         if (strlen(in_path_string) != in_path_string_size) {
89                 return smbd_smb2_request_error(req, NT_STATUS_BAD_NETWORK_NAME);
90         }
91
92         subreq = smbd_smb2_tree_connect_send(req,
93                                              req->sconn->ev_ctx,
94                                              req,
95                                              in_path_string);
96         if (subreq == NULL) {
97                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
98         }
99         tevent_req_set_callback(subreq, smbd_smb2_request_tcon_done, req);
100
101         return smbd_smb2_request_pending_queue(req, subreq, 500);
102 }
103
104 static void smbd_smb2_request_tcon_done(struct tevent_req *subreq)
105 {
106         struct smbd_smb2_request *req =
107                 tevent_req_callback_data(subreq,
108                 struct smbd_smb2_request);
109         uint8_t *outhdr;
110         DATA_BLOB outbody;
111         uint8_t out_share_type = 0;
112         uint32_t out_share_flags = 0;
113         uint32_t out_capabilities = 0;
114         uint32_t out_maximal_access = 0;
115         uint32_t out_tree_id = 0;
116         NTSTATUS status;
117         NTSTATUS error;
118
119         status = smbd_smb2_tree_connect_recv(subreq,
120                                              &out_share_type,
121                                              &out_share_flags,
122                                              &out_capabilities,
123                                              &out_maximal_access,
124                                              &out_tree_id);
125         TALLOC_FREE(subreq);
126         if (!NT_STATUS_IS_OK(status)) {
127                 error = smbd_smb2_request_error(req, status);
128                 if (!NT_STATUS_IS_OK(error)) {
129                         smbd_server_connection_terminate(req->sconn,
130                                                          nt_errstr(error));
131                         return;
132                 }
133                 return;
134         }
135
136         outhdr = SMBD_SMB2_OUT_HDR_PTR(req);
137
138         outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
139         if (outbody.data == NULL) {
140                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
141                 if (!NT_STATUS_IS_OK(error)) {
142                         smbd_server_connection_terminate(req->sconn,
143                                                          nt_errstr(error));
144                         return;
145                 }
146                 return;
147         }
148
149         SIVAL(outhdr, SMB2_HDR_TID, out_tree_id);
150
151         SSVAL(outbody.data, 0x00, 0x10);        /* struct size */
152         SCVAL(outbody.data, 0x02,
153               out_share_type);                  /* share type */
154         SCVAL(outbody.data, 0x03, 0);           /* reserved */
155         SIVAL(outbody.data, 0x04,
156               out_share_flags);                 /* share flags */
157         SIVAL(outbody.data, 0x08,
158               out_capabilities);                /* capabilities */
159         SIVAL(outbody.data, 0x0C,
160               out_maximal_access);              /* maximal access */
161
162         error = smbd_smb2_request_done(req, outbody, NULL);
163         if (!NT_STATUS_IS_OK(error)) {
164                 smbd_server_connection_terminate(req->sconn,
165                                                  nt_errstr(error));
166                 return;
167         }
168 }
169
170 static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
171                                        const char *in_path,
172                                        uint8_t *out_share_type,
173                                        uint32_t *out_share_flags,
174                                        uint32_t *out_capabilities,
175                                        uint32_t *out_maximal_access,
176                                        uint32_t *out_tree_id)
177 {
178         struct smbXsrv_connection *conn = req->sconn->conn;
179         const char *share = in_path;
180         char *service = NULL;
181         int snum = -1;
182         struct smbXsrv_tcon *tcon;
183         NTTIME now = timeval_to_nttime(&req->request_time);
184         connection_struct *compat_conn = NULL;
185         struct user_struct *compat_vuser = req->session->compat;
186         NTSTATUS status;
187         bool encryption_required = req->session->global->encryption_required;
188         bool guest_session = false;
189
190         if (strncmp(share, "\\\\", 2) == 0) {
191                 const char *p = strchr(share+2, '\\');
192                 if (p) {
193                         share = p + 1;
194                 }
195         }
196
197         DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
198                   in_path, share));
199
200         service = talloc_strdup(talloc_tos(), share);
201         if(!service) {
202                 return NT_STATUS_NO_MEMORY;
203         }
204
205         if (!strlower_m(service)) {
206                 DEBUG(2, ("strlower_m %s failed\n", service));
207                 return NT_STATUS_INVALID_PARAMETER;
208         }
209
210         /* TODO: do more things... */
211         if (strequal(service,HOMES_NAME)) {
212                 if (compat_vuser->homes_snum == -1) {
213                         DEBUG(2, ("[homes] share not available for "
214                                 "user %s because it was not found "
215                                 "or created at session setup "
216                                 "time\n",
217                                 compat_vuser->session_info->unix_info->unix_name));
218                         return NT_STATUS_BAD_NETWORK_NAME;
219                 }
220                 snum = compat_vuser->homes_snum;
221         } else if ((compat_vuser->homes_snum != -1)
222                    && strequal(service,
223                         lp_servicename(talloc_tos(), compat_vuser->homes_snum))) {
224                 snum = compat_vuser->homes_snum;
225         } else {
226                 snum = find_service(talloc_tos(), service, &service);
227                 if (!service) {
228                         return NT_STATUS_NO_MEMORY;
229                 }
230         }
231
232         if (snum < 0) {
233                 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
234                          service));
235                 return NT_STATUS_BAD_NETWORK_NAME;
236         }
237
238         if (lp_smb_encrypt(snum) == SMB_SIGNING_REQUIRED) {
239                 encryption_required = true;
240         }
241
242         if (security_session_user_level(compat_vuser->session_info, NULL) < SECURITY_USER) {
243                 guest_session = true;
244         }
245
246         if (guest_session && encryption_required) {
247                 DEBUG(1,("reject guest as encryption is required for service %s\n",
248                          service));
249                 return NT_STATUS_ACCESS_DENIED;
250         }
251
252         if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
253                 if (encryption_required) {
254                         DEBUG(1,("reject tcon with dialect[0x%04X] "
255                                  "as encryption is required for service %s\n",
256                                  conn->smb2.server.dialect, service));
257                         return NT_STATUS_ACCESS_DENIED;
258                 }
259         }
260
261         /* create a new tcon as child of the session */
262         status = smb2srv_tcon_create(req->session, now, &tcon);
263         if (!NT_STATUS_IS_OK(status)) {
264                 return status;
265         }
266
267         tcon->global->encryption_required = encryption_required;
268
269         compat_conn = make_connection_smb2(req->sconn,
270                                         tcon, snum,
271                                         req->session->compat,
272                                         "???",
273                                         &status);
274         if (compat_conn == NULL) {
275                 TALLOC_FREE(tcon);
276                 return status;
277         }
278
279         tcon->global->share_name = lp_servicename(tcon->global,
280                                                   SNUM(compat_conn));
281         if (tcon->global->share_name == NULL) {
282                 conn_free(compat_conn);
283                 TALLOC_FREE(tcon);
284                 return NT_STATUS_NO_MEMORY;
285         }
286
287         tcon->compat = talloc_move(tcon, &compat_conn);
288
289         tcon->status = NT_STATUS_OK;
290
291         status = smbXsrv_tcon_update(tcon);
292         if (!NT_STATUS_IS_OK(status)) {
293                 TALLOC_FREE(tcon);
294                 return status;
295         }
296
297         if (IS_PRINT(tcon->compat)) {
298                 *out_share_type = SMB2_SHARE_TYPE_PRINT;
299         } else if (IS_IPC(tcon->compat)) {
300                 *out_share_type = SMB2_SHARE_TYPE_PIPE;
301         } else {
302                 *out_share_type = SMB2_SHARE_TYPE_DISK;
303         }
304
305         *out_share_flags = 0;
306
307         if (lp_msdfs_root(SNUM(tcon->compat)) && lp_host_msdfs()) {
308                 *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
309                 *out_capabilities = SMB2_SHARE_CAP_DFS;
310         } else {
311                 *out_capabilities = 0;
312         }
313
314         switch(lp_csc_policy(SNUM(tcon->compat))) {
315         case CSC_POLICY_MANUAL:
316                 break;
317         case CSC_POLICY_DOCUMENTS:
318                 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
319                 break;
320         case CSC_POLICY_PROGRAMS:
321                 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
322                 break;
323         case CSC_POLICY_DISABLE:
324                 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
325                 break;
326         default:
327                 break;
328         }
329
330         if (lp_hideunreadable(SNUM(tcon->compat)) ||
331             lp_hideunwriteable_files(SNUM(tcon->compat))) {
332                 *out_share_flags |= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM;
333         }
334
335         if (encryption_required) {
336                 *out_share_flags |= SMB2_SHAREFLAG_ENCRYPT_DATA;
337         }
338
339         *out_maximal_access = tcon->compat->share_access;
340
341         *out_tree_id = tcon->global->tcon_wire_id;
342         return NT_STATUS_OK;
343 }
344
345 struct smbd_smb2_tree_connect_state {
346         const char *in_path;
347         uint8_t out_share_type;
348         uint32_t out_share_flags;
349         uint32_t out_capabilities;
350         uint32_t out_maximal_access;
351         uint32_t out_tree_id;
352 };
353
354 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
355                                         struct tevent_context *ev,
356                                         struct smbd_smb2_request *smb2req,
357                                         const char *in_path)
358 {
359         struct tevent_req *req;
360         struct smbd_smb2_tree_connect_state *state;
361         NTSTATUS status;
362
363         req = tevent_req_create(mem_ctx, &state,
364                                 struct smbd_smb2_tree_connect_state);
365         if (req == NULL) {
366                 return NULL;
367         }
368         state->in_path = in_path;
369
370         status = smbd_smb2_tree_connect(smb2req,
371                                         state->in_path,
372                                         &state->out_share_type,
373                                         &state->out_share_flags,
374                                         &state->out_capabilities,
375                                         &state->out_maximal_access,
376                                         &state->out_tree_id);
377         if (tevent_req_nterror(req, status)) {
378                 return tevent_req_post(req, ev);
379         }
380
381         tevent_req_done(req);
382         return tevent_req_post(req, ev);
383 }
384
385 static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
386                                             uint8_t *out_share_type,
387                                             uint32_t *out_share_flags,
388                                             uint32_t *out_capabilities,
389                                             uint32_t *out_maximal_access,
390                                             uint32_t *out_tree_id)
391 {
392         struct smbd_smb2_tree_connect_state *state =
393                 tevent_req_data(req,
394                 struct smbd_smb2_tree_connect_state);
395         NTSTATUS status;
396
397         if (tevent_req_is_nterror(req, &status)) {
398                 tevent_req_received(req);
399                 return status;
400         }
401
402         *out_share_type = state->out_share_type;
403         *out_share_flags = state->out_share_flags;
404         *out_capabilities = state->out_capabilities;
405         *out_maximal_access = state->out_maximal_access;
406         *out_tree_id = state->out_tree_id;
407
408         tevent_req_received(req);
409         return NT_STATUS_OK;
410 }
411
412 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
413 {
414         NTSTATUS status;
415         DATA_BLOB outbody;
416
417         status = smbd_smb2_request_verify_sizes(req, 0x04);
418         if (!NT_STATUS_IS_OK(status)) {
419                 return smbd_smb2_request_error(req, status);
420         }
421
422         /*
423          * TODO: cancel all outstanding requests on the tcon
424          */
425         status = smbXsrv_tcon_disconnect(req->tcon, req->tcon->compat->vuid);
426         if (!NT_STATUS_IS_OK(status)) {
427                 DEBUG(0, ("smbd_smb2_request_process_tdis: "
428                           "smbXsrv_tcon_disconnect() failed: %s\n",
429                           nt_errstr(status)));
430                 /*
431                  * If we hit this case, there is something completely
432                  * wrong, so we better disconnect the transport connection.
433                  */
434                 return status;
435         }
436
437         TALLOC_FREE(req->tcon);
438
439         outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
440         if (outbody.data == NULL) {
441                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
442         }
443
444         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
445         SSVAL(outbody.data, 0x02, 0);           /* reserved */
446
447         return smbd_smb2_request_done(req, outbody, NULL);
448 }