smbd: Re-register notify requests
[vlendec/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                                             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.client.capabilities & SMB2_CAP_ENCRYPTION)) {
272                 encryption_desired = true;
273         }
274
275         if (lp_smb_encrypt(snum) == SMB_SIGNING_REQUIRED) {
276                 encryption_desired = true;
277                 encryption_required = true;
278         }
279
280         if (guest_session && encryption_required) {
281                 DEBUG(1,("reject guest as encryption is required for service %s\n",
282                          service));
283                 return NT_STATUS_ACCESS_DENIED;
284         }
285
286         if (conn->smb2.server.cipher == 0) {
287                 if (encryption_required) {
288                         DEBUG(1,("reject tcon with dialect[0x%04X] "
289                                  "as encryption is required for service %s\n",
290                                  conn->smb2.server.dialect, service));
291                         return NT_STATUS_ACCESS_DENIED;
292                 }
293         }
294
295         /* create a new tcon as child of the session */
296         status = smb2srv_tcon_create(req->session, now, &tcon);
297         if (!NT_STATUS_IS_OK(status)) {
298                 return status;
299         }
300
301         if (encryption_desired) {
302                 tcon->global->encryption_flags |= SMBXSRV_ENCRYPTION_DESIRED;
303         }
304         if (encryption_required) {
305                 tcon->global->encryption_flags |= SMBXSRV_ENCRYPTION_REQUIRED;
306         }
307
308         compat_conn = make_connection_smb2(req,
309                                         tcon, snum,
310                                         req->session->compat,
311                                         "???",
312                                         &status);
313         if (compat_conn == NULL) {
314                 TALLOC_FREE(tcon);
315                 return status;
316         }
317
318         tcon->global->share_name = lp_servicename(tcon->global,
319                                                   SNUM(compat_conn));
320         if (tcon->global->share_name == NULL) {
321                 conn_free(compat_conn);
322                 TALLOC_FREE(tcon);
323                 return NT_STATUS_NO_MEMORY;
324         }
325         tcon->global->session_global_id =
326                 req->session->global->session_global_id;
327
328         tcon->compat = talloc_move(tcon, &compat_conn);
329
330         tcon->status = NT_STATUS_OK;
331
332         status = smbXsrv_tcon_update(tcon);
333         if (!NT_STATUS_IS_OK(status)) {
334                 TALLOC_FREE(tcon);
335                 return status;
336         }
337
338         if (IS_PRINT(tcon->compat)) {
339                 *out_share_type = SMB2_SHARE_TYPE_PRINT;
340         } else if (IS_IPC(tcon->compat)) {
341                 *out_share_type = SMB2_SHARE_TYPE_PIPE;
342         } else {
343                 *out_share_type = SMB2_SHARE_TYPE_DISK;
344         }
345
346         *out_share_flags = 0;
347
348         if (lp_msdfs_root(SNUM(tcon->compat)) && lp_host_msdfs()) {
349                 *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
350                 *out_capabilities = SMB2_SHARE_CAP_DFS;
351         } else {
352                 *out_capabilities = 0;
353         }
354
355         switch(lp_csc_policy(SNUM(tcon->compat))) {
356         case CSC_POLICY_MANUAL:
357                 break;
358         case CSC_POLICY_DOCUMENTS:
359                 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
360                 break;
361         case CSC_POLICY_PROGRAMS:
362                 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
363                 break;
364         case CSC_POLICY_DISABLE:
365                 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
366                 break;
367         default:
368                 break;
369         }
370
371         if (lp_hide_unreadable(SNUM(tcon->compat)) ||
372             lp_hide_unwriteable_files(SNUM(tcon->compat))) {
373                 *out_share_flags |= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM;
374         }
375
376         if (encryption_desired) {
377                 *out_share_flags |= SMB2_SHAREFLAG_ENCRYPT_DATA;
378         }
379
380         *out_maximal_access = tcon->compat->share_access;
381
382         *out_tree_id = tcon->global->tcon_wire_id;
383         return NT_STATUS_OK;
384 }
385
386 struct smbd_smb2_tree_connect_state {
387         const char *in_path;
388         uint8_t out_share_type;
389         uint32_t out_share_flags;
390         uint32_t out_capabilities;
391         uint32_t out_maximal_access;
392         uint32_t out_tree_id;
393         bool disconnect;
394 };
395
396 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
397                                         struct tevent_context *ev,
398                                         struct smbd_smb2_request *smb2req,
399                                         const char *in_path)
400 {
401         struct tevent_req *req;
402         struct smbd_smb2_tree_connect_state *state;
403         NTSTATUS status;
404
405         req = tevent_req_create(mem_ctx, &state,
406                                 struct smbd_smb2_tree_connect_state);
407         if (req == NULL) {
408                 return NULL;
409         }
410         state->in_path = in_path;
411
412         status = smbd_smb2_tree_connect(smb2req,
413                                         state->in_path,
414                                         &state->out_share_type,
415                                         &state->out_share_flags,
416                                         &state->out_capabilities,
417                                         &state->out_maximal_access,
418                                         &state->out_tree_id,
419                                         &state->disconnect);
420         if (tevent_req_nterror(req, status)) {
421                 return tevent_req_post(req, ev);
422         }
423
424         tevent_req_done(req);
425         return tevent_req_post(req, ev);
426 }
427
428 static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
429                                             uint8_t *out_share_type,
430                                             uint32_t *out_share_flags,
431                                             uint32_t *out_capabilities,
432                                             uint32_t *out_maximal_access,
433                                             uint32_t *out_tree_id,
434                                             bool *disconnect)
435 {
436         struct smbd_smb2_tree_connect_state *state =
437                 tevent_req_data(req,
438                 struct smbd_smb2_tree_connect_state);
439         NTSTATUS status;
440
441         if (tevent_req_is_nterror(req, &status)) {
442                 tevent_req_received(req);
443                 return status;
444         }
445
446         *out_share_type = state->out_share_type;
447         *out_share_flags = state->out_share_flags;
448         *out_capabilities = state->out_capabilities;
449         *out_maximal_access = state->out_maximal_access;
450         *out_tree_id = state->out_tree_id;
451         *disconnect = state->disconnect;
452
453         tevent_req_received(req);
454         return NT_STATUS_OK;
455 }
456
457 static struct tevent_req *smbd_smb2_tdis_send(TALLOC_CTX *mem_ctx,
458                                         struct tevent_context *ev,
459                                         struct smbd_smb2_request *smb2req);
460 static NTSTATUS smbd_smb2_tdis_recv(struct tevent_req *req);
461 static void smbd_smb2_request_tdis_done(struct tevent_req *subreq);
462
463 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
464 {
465         NTSTATUS status;
466         struct tevent_req *subreq = NULL;
467
468         status = smbd_smb2_request_verify_sizes(req, 0x04);
469         if (!NT_STATUS_IS_OK(status)) {
470                 return smbd_smb2_request_error(req, status);
471         }
472
473         subreq = smbd_smb2_tdis_send(req, req->sconn->ev_ctx, req);
474         if (subreq == NULL) {
475                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
476         }
477         tevent_req_set_callback(subreq, smbd_smb2_request_tdis_done, req);
478
479         /*
480          * Wait a long time before going async on this to allow
481          * requests we're waiting on to finish. Set timeout to 10 secs.
482          */
483         return smbd_smb2_request_pending_queue(req, subreq, 10000000);
484 }
485
486 static void smbd_smb2_request_tdis_done(struct tevent_req *subreq)
487 {
488         struct smbd_smb2_request *smb2req =
489                 tevent_req_callback_data(subreq,
490                 struct smbd_smb2_request);
491         DATA_BLOB outbody;
492         NTSTATUS status;
493         NTSTATUS error;
494
495         status = smbd_smb2_tdis_recv(subreq);
496         TALLOC_FREE(subreq);
497         if (!NT_STATUS_IS_OK(status)) {
498                 error = smbd_smb2_request_error(smb2req, status);
499                 if (!NT_STATUS_IS_OK(error)) {
500                         smbd_server_connection_terminate(smb2req->xconn,
501                                                         nt_errstr(error));
502                         return;
503                 }
504                 return;
505         }
506
507         outbody = smbd_smb2_generate_outbody(smb2req, 0x04);
508         if (outbody.data == NULL) {
509                 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
510                 if (!NT_STATUS_IS_OK(error)) {
511                         smbd_server_connection_terminate(smb2req->xconn,
512                                                         nt_errstr(error));
513                         return;
514                 }
515                 return;
516         }
517
518         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
519         SSVAL(outbody.data, 0x02, 0);           /* reserved */
520
521         error = smbd_smb2_request_done(smb2req, outbody, NULL);
522         if (!NT_STATUS_IS_OK(error)) {
523                 smbd_server_connection_terminate(smb2req->xconn,
524                                                 nt_errstr(error));
525                 return;
526         }
527 }
528
529 struct smbd_smb2_tdis_state {
530         struct smbd_smb2_request *smb2req;
531         struct tevent_queue *wait_queue;
532 };
533
534 static void smbd_smb2_tdis_wait_done(struct tevent_req *subreq);
535
536 static struct tevent_req *smbd_smb2_tdis_send(TALLOC_CTX *mem_ctx,
537                                         struct tevent_context *ev,
538                                         struct smbd_smb2_request *smb2req)
539 {
540         struct tevent_req *req;
541         struct smbd_smb2_tdis_state *state;
542         struct tevent_req *subreq;
543         struct smbXsrv_connection *xconn = NULL;
544
545         req = tevent_req_create(mem_ctx, &state,
546                         struct smbd_smb2_tdis_state);
547         if (req == NULL) {
548                 return NULL;
549         }
550         state->smb2req = smb2req;
551
552         state->wait_queue = tevent_queue_create(state, "tdis_wait_queue");
553         if (tevent_req_nomem(state->wait_queue, req)) {
554                 return tevent_req_post(req, ev);
555         }
556
557         /*
558          * Make sure that no new request will be able to use this tcon.
559          */
560         smb2req->tcon->status = NT_STATUS_NETWORK_NAME_DELETED;
561
562         xconn = smb2req->xconn->client->connections;
563         for (; xconn != NULL; xconn = xconn->next) {
564                 struct smbd_smb2_request *preq;
565
566                 for (preq = xconn->smb2.requests; preq != NULL; preq = preq->next) {
567                         if (preq == smb2req) {
568                                 /* Can't cancel current request. */
569                                 continue;
570                         }
571                         if (preq->tcon != smb2req->tcon) {
572                                 /* Request on different tcon. */
573                                 continue;
574                         }
575
576                         /*
577                          * Never cancel anything in a compound
578                          * request. Way too hard to deal with
579                          * the result.
580                          */
581                         if (!preq->compound_related && preq->subreq != NULL) {
582                                 tevent_req_cancel(preq->subreq);
583                         }
584
585                         /*
586                          * Now wait until the request is finished.
587                          *
588                          * We don't set a callback, as we just want to block the
589                          * wait queue and the talloc_free() of the request will
590                          * remove the item from the wait queue.
591                          */
592                         subreq = tevent_queue_wait_send(preq, ev, state->wait_queue);
593                         if (tevent_req_nomem(subreq, req)) {
594                                 return tevent_req_post(req, ev);
595                         }
596                 }
597         }
598
599         /*
600          * Now we add our own waiter to the end of the queue,
601          * this way we get notified when all pending requests are finished
602          * and send to the socket.
603          */
604         subreq = tevent_queue_wait_send(state, ev, state->wait_queue);
605         if (tevent_req_nomem(subreq, req)) {
606                 return tevent_req_post(req, ev);
607         }
608         tevent_req_set_callback(subreq, smbd_smb2_tdis_wait_done, req);
609
610         return req;
611 }
612
613 static void smbd_smb2_tdis_wait_done(struct tevent_req *subreq)
614 {
615         struct tevent_req *req = tevent_req_callback_data(
616                 subreq, struct tevent_req);
617         struct smbd_smb2_tdis_state *state = tevent_req_data(
618                 req, struct smbd_smb2_tdis_state);
619         NTSTATUS status;
620
621         tevent_queue_wait_recv(subreq);
622         TALLOC_FREE(subreq);
623
624         /*
625          * As we've been awoken, we may have changed
626          * uid in the meantime. Ensure we're still
627          * root (SMB2_OP_TDIS has .as_root = true).
628          */
629         change_to_root_user();
630
631         status = smbXsrv_tcon_disconnect(state->smb2req->tcon,
632                                          state->smb2req->tcon->compat->vuid);
633         if (tevent_req_nterror(req, status)) {
634                 return;
635         }
636
637         /* We did tear down the tcon. */
638         TALLOC_FREE(state->smb2req->tcon);
639         tevent_req_done(req);
640 }
641
642 static NTSTATUS smbd_smb2_tdis_recv(struct tevent_req *req)
643 {
644         return tevent_req_simple_recv_ntstatus(req);
645 }