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