914eb87aa8c2ec6d66c8cf23f57cbf977236489a
[metze/samba/wip.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                                             bool *disconnect);
41
42 static void smbd_smb2_request_tcon_done(struct tevent_req *subreq);
43
44 NTSTATUS smbd_smb2_request_process_tcon(struct smbd_smb2_request *req)
45 {
46         const uint8_t *inbody;
47         uint16_t in_path_offset;
48         uint16_t in_path_length;
49         DATA_BLOB in_path_buffer;
50         char *in_path_string;
51         size_t in_path_string_size;
52         NTSTATUS status;
53         bool ok;
54         struct tevent_req *subreq;
55
56         status = smbd_smb2_request_verify_sizes(req, 0x09);
57         if (!NT_STATUS_IS_OK(status)) {
58                 return smbd_smb2_request_error(req, status);
59         }
60         inbody = SMBD_SMB2_IN_BODY_PTR(req);
61
62         in_path_offset = SVAL(inbody, 0x04);
63         in_path_length = SVAL(inbody, 0x06);
64
65         if (in_path_offset != (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req))) {
66                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
67         }
68
69         if (in_path_length > SMBD_SMB2_IN_DYN_LEN(req)) {
70                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
71         }
72
73         in_path_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
74         in_path_buffer.length = in_path_length;
75
76         ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
77                                    in_path_buffer.data,
78                                    in_path_buffer.length,
79                                    &in_path_string,
80                                    &in_path_string_size);
81         if (!ok) {
82                 return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
83         }
84
85         if (in_path_buffer.length == 0) {
86                 in_path_string_size = 0;
87         }
88
89         if (strlen(in_path_string) != in_path_string_size) {
90                 return smbd_smb2_request_error(req, NT_STATUS_BAD_NETWORK_NAME);
91         }
92
93         subreq = smbd_smb2_tree_connect_send(req,
94                                              req->sconn->ev_ctx,
95                                              req,
96                                              in_path_string);
97         if (subreq == NULL) {
98                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
99         }
100         tevent_req_set_callback(subreq, smbd_smb2_request_tcon_done, req);
101
102         return smbd_smb2_request_pending_queue(req, subreq, 500);
103 }
104
105 static void smbd_smb2_request_tcon_done(struct tevent_req *subreq)
106 {
107         struct smbd_smb2_request *req =
108                 tevent_req_callback_data(subreq,
109                 struct smbd_smb2_request);
110         uint8_t *outhdr;
111         DATA_BLOB outbody;
112         uint8_t out_share_type = 0;
113         uint32_t out_share_flags = 0;
114         uint32_t out_capabilities = 0;
115         uint32_t out_maximal_access = 0;
116         uint32_t out_tree_id = 0;
117         bool disconnect = false;
118         NTSTATUS status;
119         NTSTATUS error;
120
121         status = smbd_smb2_tree_connect_recv(subreq,
122                                              &out_share_type,
123                                              &out_share_flags,
124                                              &out_capabilities,
125                                              &out_maximal_access,
126                                              &out_tree_id,
127                                              &disconnect);
128         TALLOC_FREE(subreq);
129         if (!NT_STATUS_IS_OK(status)) {
130                 if (disconnect) {
131                         smbd_server_connection_terminate(req->xconn,
132                                                          nt_errstr(status));
133                         return;
134                 }
135                 error = smbd_smb2_request_error(req, status);
136                 if (!NT_STATUS_IS_OK(error)) {
137                         smbd_server_connection_terminate(req->xconn,
138                                                          nt_errstr(error));
139                         return;
140                 }
141                 return;
142         }
143
144         outhdr = SMBD_SMB2_OUT_HDR_PTR(req);
145
146         outbody = smbd_smb2_generate_outbody(req, 0x10);
147         if (outbody.data == NULL) {
148                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
149                 if (!NT_STATUS_IS_OK(error)) {
150                         smbd_server_connection_terminate(req->xconn,
151                                                          nt_errstr(error));
152                         return;
153                 }
154                 return;
155         }
156
157         SIVAL(outhdr, SMB2_HDR_TID, out_tree_id);
158
159         SSVAL(outbody.data, 0x00, 0x10);        /* struct size */
160         SCVAL(outbody.data, 0x02,
161               out_share_type);                  /* share type */
162         SCVAL(outbody.data, 0x03, 0);           /* reserved */
163         SIVAL(outbody.data, 0x04,
164               out_share_flags);                 /* share flags */
165         SIVAL(outbody.data, 0x08,
166               out_capabilities);                /* capabilities */
167         SIVAL(outbody.data, 0x0C,
168               out_maximal_access);              /* maximal access */
169
170         error = smbd_smb2_request_done(req, outbody, NULL);
171         if (!NT_STATUS_IS_OK(error)) {
172                 smbd_server_connection_terminate(req->xconn,
173                                                  nt_errstr(error));
174                 return;
175         }
176 }
177
178 static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
179                                        const char *in_path,
180                                        uint8_t *out_share_type,
181                                        uint32_t *out_share_flags,
182                                        uint32_t *out_capabilities,
183                                        uint32_t *out_maximal_access,
184                                        uint32_t *out_tree_id,
185                                        bool *disconnect)
186 {
187         struct smbXsrv_connection *conn = req->xconn;
188         const char *share = in_path;
189         char *service = NULL;
190         int snum = -1;
191         struct smbXsrv_tcon *tcon;
192         NTTIME now = timeval_to_nttime(&req->request_time);
193         connection_struct *compat_conn = NULL;
194         struct user_struct *compat_vuser = req->session->compat;
195         NTSTATUS status;
196         bool encryption_desired = req->session->global->encryption_flags & SMBXSRV_ENCRYPTION_DESIRED;
197         bool encryption_required = req->session->global->encryption_flags & SMBXSRV_ENCRYPTION_REQUIRED;
198         bool guest_session = false;
199         bool require_signed_tcon = false;
200
201         *disconnect = false;
202
203         if (strncmp(share, "\\\\", 2) == 0) {
204                 const char *p = strchr(share+2, '\\');
205                 if (p) {
206                         share = p + 1;
207                 }
208         }
209
210         DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
211                   in_path, share));
212
213         if (security_session_user_level(compat_vuser->session_info, NULL) < SECURITY_USER) {
214                 guest_session = true;
215         }
216
217         if (conn->protocol >= PROTOCOL_SMB3_11 && !guest_session) {
218                 require_signed_tcon = true;
219         }
220
221         if (require_signed_tcon && !req->do_encryption && !req->do_signing) {
222                 DEBUG(1, ("smbd_smb2_tree_connect: reject request to share "
223                           "[%s] as '%s\\%s' without encryption or signing. "
224                           "Disconnecting.\n",
225                           share,
226                           req->session->global->auth_session_info->info->domain_name,
227                           req->session->global->auth_session_info->info->account_name));
228                 *disconnect = true;
229                 return NT_STATUS_ACCESS_DENIED;
230         }
231
232         service = talloc_strdup(talloc_tos(), share);
233         if(!service) {
234                 return NT_STATUS_NO_MEMORY;
235         }
236
237         if (!strlower_m(service)) {
238                 DEBUG(2, ("strlower_m %s failed\n", service));
239                 return NT_STATUS_INVALID_PARAMETER;
240         }
241
242         /* TODO: do more things... */
243         if (strequal(service,HOMES_NAME)) {
244                 if (compat_vuser->homes_snum == -1) {
245                         DEBUG(2, ("[homes] share not available for "
246                                 "user %s because it was not found "
247                                 "or created at session setup "
248                                 "time\n",
249                                 compat_vuser->session_info->unix_info->unix_name));
250                         return NT_STATUS_BAD_NETWORK_NAME;
251                 }
252                 snum = compat_vuser->homes_snum;
253         } else if ((compat_vuser->homes_snum != -1)
254                    && strequal(service,
255                         lp_servicename(talloc_tos(), compat_vuser->homes_snum))) {
256                 snum = compat_vuser->homes_snum;
257         } else {
258                 snum = find_service(talloc_tos(), service, &service);
259                 if (!service) {
260                         return NT_STATUS_NO_MEMORY;
261                 }
262         }
263
264         if (snum < 0) {
265                 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
266                          service));
267                 return NT_STATUS_BAD_NETWORK_NAME;
268         }
269
270         if ((lp_smb_encrypt(snum) >= SMB_SIGNING_DESIRED) &&
271             (conn->smb2.server.cipher != 0))
272         {
273                 encryption_desired = true;
274         }
275
276         if (lp_smb_encrypt(snum) == SMB_SIGNING_REQUIRED) {
277                 encryption_desired = true;
278                 encryption_required = true;
279         }
280
281         if (guest_session && encryption_required) {
282                 DEBUG(1,("reject guest as encryption is required for service %s\n",
283                          service));
284                 return NT_STATUS_ACCESS_DENIED;
285         }
286
287         if (conn->smb2.server.cipher == 0) {
288                 if (encryption_required) {
289                         DEBUG(1,("reject tcon with dialect[0x%04X] "
290                                  "as encryption is required for service %s\n",
291                                  conn->smb2.server.dialect, service));
292                         return NT_STATUS_ACCESS_DENIED;
293                 }
294         }
295
296         /* create a new tcon as child of the session */
297         status = smb2srv_tcon_create(req->session, now, &tcon);
298         if (!NT_STATUS_IS_OK(status)) {
299                 return status;
300         }
301
302         if (encryption_desired) {
303                 tcon->global->encryption_flags |= SMBXSRV_ENCRYPTION_DESIRED;
304         }
305         if (encryption_required) {
306                 tcon->global->encryption_flags |= SMBXSRV_ENCRYPTION_REQUIRED;
307         }
308
309         compat_conn = make_connection_smb2(req,
310                                         tcon, snum,
311                                         req->session->compat,
312                                         "???",
313                                         &status);
314         if (compat_conn == NULL) {
315                 TALLOC_FREE(tcon);
316                 return status;
317         }
318
319         tcon->global->share_name = lp_servicename(tcon->global,
320                                                   SNUM(compat_conn));
321         if (tcon->global->share_name == NULL) {
322                 conn_free(compat_conn);
323                 TALLOC_FREE(tcon);
324                 return NT_STATUS_NO_MEMORY;
325         }
326         tcon->global->session_global_id =
327                 req->session->global->session_global_id;
328
329         tcon->compat = talloc_move(tcon, &compat_conn);
330
331         tcon->status = NT_STATUS_OK;
332
333         status = smbXsrv_tcon_update(tcon);
334         if (!NT_STATUS_IS_OK(status)) {
335                 TALLOC_FREE(tcon);
336                 return status;
337         }
338
339         if (IS_PRINT(tcon->compat)) {
340                 *out_share_type = SMB2_SHARE_TYPE_PRINT;
341         } else if (IS_IPC(tcon->compat)) {
342                 *out_share_type = SMB2_SHARE_TYPE_PIPE;
343         } else {
344                 *out_share_type = SMB2_SHARE_TYPE_DISK;
345         }
346
347         *out_share_flags = 0;
348
349         if (lp_msdfs_root(SNUM(tcon->compat)) && lp_host_msdfs()) {
350                 *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
351                 *out_capabilities = SMB2_SHARE_CAP_DFS;
352         } else {
353                 *out_capabilities = 0;
354         }
355
356         switch(lp_csc_policy(SNUM(tcon->compat))) {
357         case CSC_POLICY_MANUAL:
358                 break;
359         case CSC_POLICY_DOCUMENTS:
360                 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
361                 break;
362         case CSC_POLICY_PROGRAMS:
363                 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
364                 break;
365         case CSC_POLICY_DISABLE:
366                 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
367                 break;
368         default:
369                 break;
370         }
371
372         if (lp_hide_unreadable(SNUM(tcon->compat)) ||
373             lp_hide_unwriteable_files(SNUM(tcon->compat))) {
374                 *out_share_flags |= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM;
375         }
376
377         if (encryption_desired) {
378                 *out_share_flags |= SMB2_SHAREFLAG_ENCRYPT_DATA;
379         }
380
381         *out_maximal_access = tcon->compat->share_access;
382
383         *out_tree_id = tcon->global->tcon_wire_id;
384         req->last_tid = tcon->global->tcon_wire_id;
385
386         return NT_STATUS_OK;
387 }
388
389 struct smbd_smb2_tree_connect_state {
390         const char *in_path;
391         uint8_t out_share_type;
392         uint32_t out_share_flags;
393         uint32_t out_capabilities;
394         uint32_t out_maximal_access;
395         uint32_t out_tree_id;
396         bool disconnect;
397 };
398
399 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
400                                         struct tevent_context *ev,
401                                         struct smbd_smb2_request *smb2req,
402                                         const char *in_path)
403 {
404         struct tevent_req *req;
405         struct smbd_smb2_tree_connect_state *state;
406         NTSTATUS status;
407
408         req = tevent_req_create(mem_ctx, &state,
409                                 struct smbd_smb2_tree_connect_state);
410         if (req == NULL) {
411                 return NULL;
412         }
413         state->in_path = in_path;
414
415         status = smbd_smb2_tree_connect(smb2req,
416                                         state->in_path,
417                                         &state->out_share_type,
418                                         &state->out_share_flags,
419                                         &state->out_capabilities,
420                                         &state->out_maximal_access,
421                                         &state->out_tree_id,
422                                         &state->disconnect);
423         if (tevent_req_nterror(req, status)) {
424                 return tevent_req_post(req, ev);
425         }
426
427         tevent_req_done(req);
428         return tevent_req_post(req, ev);
429 }
430
431 static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
432                                             uint8_t *out_share_type,
433                                             uint32_t *out_share_flags,
434                                             uint32_t *out_capabilities,
435                                             uint32_t *out_maximal_access,
436                                             uint32_t *out_tree_id,
437                                             bool *disconnect)
438 {
439         struct smbd_smb2_tree_connect_state *state =
440                 tevent_req_data(req,
441                 struct smbd_smb2_tree_connect_state);
442         NTSTATUS status;
443
444         if (tevent_req_is_nterror(req, &status)) {
445                 tevent_req_received(req);
446                 return status;
447         }
448
449         *out_share_type = state->out_share_type;
450         *out_share_flags = state->out_share_flags;
451         *out_capabilities = state->out_capabilities;
452         *out_maximal_access = state->out_maximal_access;
453         *out_tree_id = state->out_tree_id;
454         *disconnect = state->disconnect;
455
456         tevent_req_received(req);
457         return NT_STATUS_OK;
458 }
459
460 static struct tevent_req *smbd_smb2_tdis_send(TALLOC_CTX *mem_ctx,
461                                         struct tevent_context *ev,
462                                         struct smbd_smb2_request *smb2req);
463 static NTSTATUS smbd_smb2_tdis_recv(struct tevent_req *req);
464 static void smbd_smb2_request_tdis_done(struct tevent_req *subreq);
465
466 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
467 {
468         NTSTATUS status;
469         struct tevent_req *subreq = NULL;
470
471         status = smbd_smb2_request_verify_sizes(req, 0x04);
472         if (!NT_STATUS_IS_OK(status)) {
473                 return smbd_smb2_request_error(req, status);
474         }
475
476         subreq = smbd_smb2_tdis_send(req, req->sconn->ev_ctx, req);
477         if (subreq == NULL) {
478                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
479         }
480         tevent_req_set_callback(subreq, smbd_smb2_request_tdis_done, req);
481
482         /*
483          * Wait a long time before going async on this to allow
484          * requests we're waiting on to finish. Set timeout to 10 secs.
485          */
486         return smbd_smb2_request_pending_queue(req, subreq, 10000000);
487 }
488
489 static void smbd_smb2_request_tdis_done(struct tevent_req *subreq)
490 {
491         struct smbd_smb2_request *smb2req =
492                 tevent_req_callback_data(subreq,
493                 struct smbd_smb2_request);
494         DATA_BLOB outbody;
495         NTSTATUS status;
496         NTSTATUS error;
497
498         status = smbd_smb2_tdis_recv(subreq);
499         TALLOC_FREE(subreq);
500         if (!NT_STATUS_IS_OK(status)) {
501                 error = smbd_smb2_request_error(smb2req, status);
502                 if (!NT_STATUS_IS_OK(error)) {
503                         smbd_server_connection_terminate(smb2req->xconn,
504                                                         nt_errstr(error));
505                         return;
506                 }
507                 return;
508         }
509
510         outbody = smbd_smb2_generate_outbody(smb2req, 0x04);
511         if (outbody.data == NULL) {
512                 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
513                 if (!NT_STATUS_IS_OK(error)) {
514                         smbd_server_connection_terminate(smb2req->xconn,
515                                                         nt_errstr(error));
516                         return;
517                 }
518                 return;
519         }
520
521         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
522         SSVAL(outbody.data, 0x02, 0);           /* reserved */
523
524         error = smbd_smb2_request_done(smb2req, outbody, NULL);
525         if (!NT_STATUS_IS_OK(error)) {
526                 smbd_server_connection_terminate(smb2req->xconn,
527                                                 nt_errstr(error));
528                 return;
529         }
530 }
531
532 struct smbd_smb2_tdis_state {
533         struct smbd_smb2_request *smb2req;
534         struct tevent_queue *wait_queue;
535 };
536
537 static void smbd_smb2_tdis_wait_done(struct tevent_req *subreq);
538
539 static struct tevent_req *smbd_smb2_tdis_send(TALLOC_CTX *mem_ctx,
540                                         struct tevent_context *ev,
541                                         struct smbd_smb2_request *smb2req)
542 {
543         struct tevent_req *req;
544         struct smbd_smb2_tdis_state *state;
545         struct tevent_req *subreq;
546         struct smbXsrv_connection *xconn = NULL;
547
548         req = tevent_req_create(mem_ctx, &state,
549                         struct smbd_smb2_tdis_state);
550         if (req == NULL) {
551                 return NULL;
552         }
553         state->smb2req = smb2req;
554
555         state->wait_queue = tevent_queue_create(state, "tdis_wait_queue");
556         if (tevent_req_nomem(state->wait_queue, req)) {
557                 return tevent_req_post(req, ev);
558         }
559
560         /*
561          * Make sure that no new request will be able to use this tcon.
562          */
563         smb2req->tcon->status = NT_STATUS_NETWORK_NAME_DELETED;
564
565         xconn = smb2req->xconn->client->connections;
566         for (; xconn != NULL; xconn = xconn->next) {
567                 struct smbd_smb2_request *preq;
568
569                 for (preq = xconn->smb2.requests; preq != NULL; preq = preq->next) {
570                         if (preq == smb2req) {
571                                 /* Can't cancel current request. */
572                                 continue;
573                         }
574                         if (preq->tcon != smb2req->tcon) {
575                                 /* Request on different tcon. */
576                                 continue;
577                         }
578
579                         /*
580                          * Never cancel anything in a compound
581                          * request. Way too hard to deal with
582                          * the result.
583                          */
584                         if (!preq->compound_related && preq->subreq != NULL) {
585                                 tevent_req_cancel(preq->subreq);
586                         }
587
588                         /*
589                          * Now wait until the request is finished.
590                          *
591                          * We don't set a callback, as we just want to block the
592                          * wait queue and the talloc_free() of the request will
593                          * remove the item from the wait queue.
594                          */
595                         subreq = tevent_queue_wait_send(preq, ev, state->wait_queue);
596                         if (tevent_req_nomem(subreq, req)) {
597                                 return tevent_req_post(req, ev);
598                         }
599                 }
600         }
601
602         /*
603          * Now we add our own waiter to the end of the queue,
604          * this way we get notified when all pending requests are finished
605          * and send to the socket.
606          */
607         subreq = tevent_queue_wait_send(state, ev, state->wait_queue);
608         if (tevent_req_nomem(subreq, req)) {
609                 return tevent_req_post(req, ev);
610         }
611         tevent_req_set_callback(subreq, smbd_smb2_tdis_wait_done, req);
612
613         return req;
614 }
615
616 static void smbd_smb2_tdis_wait_done(struct tevent_req *subreq)
617 {
618         struct tevent_req *req = tevent_req_callback_data(
619                 subreq, struct tevent_req);
620         struct smbd_smb2_tdis_state *state = tevent_req_data(
621                 req, struct smbd_smb2_tdis_state);
622         NTSTATUS status;
623
624         tevent_queue_wait_recv(subreq);
625         TALLOC_FREE(subreq);
626
627         /*
628          * As we've been awoken, we may have changed
629          * uid in the meantime. Ensure we're still
630          * root (SMB2_OP_TDIS has .as_root = true).
631          */
632         change_to_root_user();
633
634         status = smbXsrv_tcon_disconnect(state->smb2req->tcon,
635                                          state->smb2req->tcon->compat->vuid);
636         if (tevent_req_nterror(req, status)) {
637                 return;
638         }
639
640         /* We did tear down the tcon. */
641         TALLOC_FREE(state->smb2req->tcon);
642         tevent_req_done(req);
643 }
644
645 static NTSTATUS smbd_smb2_tdis_recv(struct tevent_req *req)
646 {
647         return tevent_req_simple_recv_ntstatus(req);
648 }