s3:passdb: add sid_check_object_is_for_passdb()
[kai/samba.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         tcon->global->session_global_id =
287                 req->session->global->session_global_id;
288
289         tcon->compat = talloc_move(tcon, &compat_conn);
290
291         tcon->status = NT_STATUS_OK;
292
293         status = smbXsrv_tcon_update(tcon);
294         if (!NT_STATUS_IS_OK(status)) {
295                 TALLOC_FREE(tcon);
296                 return status;
297         }
298
299         if (IS_PRINT(tcon->compat)) {
300                 *out_share_type = SMB2_SHARE_TYPE_PRINT;
301         } else if (IS_IPC(tcon->compat)) {
302                 *out_share_type = SMB2_SHARE_TYPE_PIPE;
303         } else {
304                 *out_share_type = SMB2_SHARE_TYPE_DISK;
305         }
306
307         *out_share_flags = 0;
308
309         if (lp_msdfs_root(SNUM(tcon->compat)) && lp_host_msdfs()) {
310                 *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
311                 *out_capabilities = SMB2_SHARE_CAP_DFS;
312         } else {
313                 *out_capabilities = 0;
314         }
315
316         switch(lp_csc_policy(SNUM(tcon->compat))) {
317         case CSC_POLICY_MANUAL:
318                 break;
319         case CSC_POLICY_DOCUMENTS:
320                 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
321                 break;
322         case CSC_POLICY_PROGRAMS:
323                 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
324                 break;
325         case CSC_POLICY_DISABLE:
326                 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
327                 break;
328         default:
329                 break;
330         }
331
332         if (lp_hideunreadable(SNUM(tcon->compat)) ||
333             lp_hideunwriteable_files(SNUM(tcon->compat))) {
334                 *out_share_flags |= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM;
335         }
336
337         if (encryption_required) {
338                 *out_share_flags |= SMB2_SHAREFLAG_ENCRYPT_DATA;
339         }
340
341         *out_maximal_access = tcon->compat->share_access;
342
343         *out_tree_id = tcon->global->tcon_wire_id;
344         return NT_STATUS_OK;
345 }
346
347 struct smbd_smb2_tree_connect_state {
348         const char *in_path;
349         uint8_t out_share_type;
350         uint32_t out_share_flags;
351         uint32_t out_capabilities;
352         uint32_t out_maximal_access;
353         uint32_t out_tree_id;
354 };
355
356 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
357                                         struct tevent_context *ev,
358                                         struct smbd_smb2_request *smb2req,
359                                         const char *in_path)
360 {
361         struct tevent_req *req;
362         struct smbd_smb2_tree_connect_state *state;
363         NTSTATUS status;
364
365         req = tevent_req_create(mem_ctx, &state,
366                                 struct smbd_smb2_tree_connect_state);
367         if (req == NULL) {
368                 return NULL;
369         }
370         state->in_path = in_path;
371
372         status = smbd_smb2_tree_connect(smb2req,
373                                         state->in_path,
374                                         &state->out_share_type,
375                                         &state->out_share_flags,
376                                         &state->out_capabilities,
377                                         &state->out_maximal_access,
378                                         &state->out_tree_id);
379         if (tevent_req_nterror(req, status)) {
380                 return tevent_req_post(req, ev);
381         }
382
383         tevent_req_done(req);
384         return tevent_req_post(req, ev);
385 }
386
387 static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
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 {
394         struct smbd_smb2_tree_connect_state *state =
395                 tevent_req_data(req,
396                 struct smbd_smb2_tree_connect_state);
397         NTSTATUS status;
398
399         if (tevent_req_is_nterror(req, &status)) {
400                 tevent_req_received(req);
401                 return status;
402         }
403
404         *out_share_type = state->out_share_type;
405         *out_share_flags = state->out_share_flags;
406         *out_capabilities = state->out_capabilities;
407         *out_maximal_access = state->out_maximal_access;
408         *out_tree_id = state->out_tree_id;
409
410         tevent_req_received(req);
411         return NT_STATUS_OK;
412 }
413
414 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
415 {
416         NTSTATUS status;
417         DATA_BLOB outbody;
418
419         status = smbd_smb2_request_verify_sizes(req, 0x04);
420         if (!NT_STATUS_IS_OK(status)) {
421                 return smbd_smb2_request_error(req, status);
422         }
423
424         /*
425          * TODO: cancel all outstanding requests on the tcon
426          */
427         status = smbXsrv_tcon_disconnect(req->tcon, req->tcon->compat->vuid);
428         if (!NT_STATUS_IS_OK(status)) {
429                 DEBUG(0, ("smbd_smb2_request_process_tdis: "
430                           "smbXsrv_tcon_disconnect() failed: %s\n",
431                           nt_errstr(status)));
432                 /*
433                  * If we hit this case, there is something completely
434                  * wrong, so we better disconnect the transport connection.
435                  */
436                 return status;
437         }
438
439         TALLOC_FREE(req->tcon);
440
441         outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
442         if (outbody.data == NULL) {
443                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
444         }
445
446         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
447         SSVAL(outbody.data, 0x02, 0);           /* reserved */
448
449         return smbd_smb2_request_done(req, outbody, NULL);
450 }