s3:smbd: use 'struct user_struct' instead of typedef'ed 'user_struct'
[gd/samba-autobuild/.git] / source3 / smbd / smb2_tcon.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../libcli/security/security.h"
26 #include "auth.h"
27 #include "lib/param/loadparm.h"
28 #include "../lib/util/tevent_ntstatus.h"
29
30 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
31                                         struct tevent_context *ev,
32                                         struct smbd_smb2_request *smb2req,
33                                         const char *in_path);
34 static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
35                                             uint8_t *out_share_type,
36                                             uint32_t *out_share_flags,
37                                             uint32_t *out_capabilities,
38                                             uint32_t *out_maximal_access,
39                                             uint32_t *out_tree_id);
40
41 static void smbd_smb2_request_tcon_done(struct tevent_req *subreq);
42
43 NTSTATUS smbd_smb2_request_process_tcon(struct smbd_smb2_request *req)
44 {
45         const uint8_t *inbody;
46         int i = req->current_idx;
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 = (const uint8_t *)req->in.vector[i+1].iov_base;
61
62         in_path_offset = SVAL(inbody, 0x04);
63         in_path_length = SVAL(inbody, 0x06);
64
65         if (in_path_offset != (SMB2_HDR_BODY + req->in.vector[i+1].iov_len)) {
66                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
67         }
68
69         if (in_path_length > req->in.vector[i+2].iov_len) {
70                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
71         }
72
73         in_path_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
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         int i = req->current_idx;
111         uint8_t *outhdr;
112         DATA_BLOB outbody;
113         uint8_t out_share_type = 0;
114         uint32_t out_share_flags = 0;
115         uint32_t out_capabilities = 0;
116         uint32_t out_maximal_access = 0;
117         uint32_t out_tree_id = 0;
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         TALLOC_FREE(subreq);
128         if (!NT_STATUS_IS_OK(status)) {
129                 error = smbd_smb2_request_error(req, status);
130                 if (!NT_STATUS_IS_OK(error)) {
131                         smbd_server_connection_terminate(req->sconn,
132                                                          nt_errstr(error));
133                         return;
134                 }
135                 return;
136         }
137
138         outhdr = (uint8_t *)req->out.vector[i].iov_base;
139
140         outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
141         if (outbody.data == NULL) {
142                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
143                 if (!NT_STATUS_IS_OK(error)) {
144                         smbd_server_connection_terminate(req->sconn,
145                                                          nt_errstr(error));
146                         return;
147                 }
148                 return;
149         }
150
151         SIVAL(outhdr, SMB2_HDR_TID, out_tree_id);
152
153         SSVAL(outbody.data, 0x00, 0x10);        /* struct size */
154         SCVAL(outbody.data, 0x02,
155               out_share_type);                  /* share type */
156         SCVAL(outbody.data, 0x03, 0);           /* reserved */
157         SIVAL(outbody.data, 0x04,
158               out_share_flags);                 /* share flags */
159         SIVAL(outbody.data, 0x08,
160               out_capabilities);                /* capabilities */
161         SIVAL(outbody.data, 0x0C,
162               out_maximal_access);              /* maximal access */
163
164         error = smbd_smb2_request_done(req, outbody, NULL);
165         if (!NT_STATUS_IS_OK(error)) {
166                 smbd_server_connection_terminate(req->sconn,
167                                                  nt_errstr(error));
168                 return;
169         }
170 }
171
172 static int smbd_smb2_tcon_destructor(struct smbd_smb2_tcon *tcon)
173 {
174         if (tcon->session == NULL) {
175                 return 0;
176         }
177
178         idr_remove(tcon->session->tcons.idtree, tcon->tid);
179         DLIST_REMOVE(tcon->session->tcons.list, tcon);
180
181         if (tcon->compat_conn) {
182                 set_current_service(tcon->compat_conn, 0, true);
183                 close_cnum(tcon->compat_conn, tcon->session->vuid);
184         }
185
186         tcon->compat_conn = NULL;
187         tcon->tid = 0;
188         tcon->session = NULL;
189
190         return 0;
191 }
192
193 static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
194                                        const char *in_path,
195                                        uint8_t *out_share_type,
196                                        uint32_t *out_share_flags,
197                                        uint32_t *out_capabilities,
198                                        uint32_t *out_maximal_access,
199                                        uint32_t *out_tree_id)
200 {
201         const char *share = in_path;
202         char *service = NULL;
203         int snum = -1;
204         struct smbd_smb2_tcon *tcon;
205         connection_struct *compat_conn = NULL;
206         struct user_struct *compat_vuser = req->session->compat_vuser;
207         int id;
208         NTSTATUS status;
209
210         if (strncmp(share, "\\\\", 2) == 0) {
211                 const char *p = strchr(share+2, '\\');
212                 if (p) {
213                         share = p + 1;
214                 }
215         }
216
217         DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
218                   in_path, share));
219
220         service = talloc_strdup(talloc_tos(), share);
221         if(!service) {
222                 return NT_STATUS_NO_MEMORY;
223         }
224
225         strlower_m(service);
226
227         /* TODO: do more things... */
228         if (strequal(service,HOMES_NAME)) {
229                 if (compat_vuser->homes_snum == -1) {
230                         DEBUG(2, ("[homes] share not available for "
231                                 "user %s because it was not found "
232                                 "or created at session setup "
233                                 "time\n",
234                                 compat_vuser->session_info->unix_info->unix_name));
235                         return NT_STATUS_BAD_NETWORK_NAME;
236                 }
237                 snum = compat_vuser->homes_snum;
238         } else if ((compat_vuser->homes_snum != -1)
239                    && strequal(service,
240                         lp_servicename(compat_vuser->homes_snum))) {
241                 snum = compat_vuser->homes_snum;
242         } else {
243                 snum = find_service(talloc_tos(), service, &service);
244                 if (!service) {
245                         return NT_STATUS_NO_MEMORY;
246                 }
247         }
248
249         if (snum < 0) {
250                 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
251                          service));
252                 return NT_STATUS_BAD_NETWORK_NAME;
253         }
254
255         /* create a new tcon as child of the session */
256         tcon = talloc_zero(req->session, struct smbd_smb2_tcon);
257         if (tcon == NULL) {
258                 return NT_STATUS_NO_MEMORY;
259         }
260         id = idr_get_new_random(req->session->tcons.idtree,
261                                 tcon,
262                                 req->session->tcons.limit);
263         if (id == -1) {
264                 TALLOC_FREE(tcon);
265                 return NT_STATUS_INSUFFICIENT_RESOURCES;
266         }
267         tcon->tid = id;
268         tcon->snum = snum;
269
270         DLIST_ADD_END(req->session->tcons.list, tcon,
271                       struct smbd_smb2_tcon *);
272         tcon->session = req->session;
273         talloc_set_destructor(tcon, smbd_smb2_tcon_destructor);
274
275         compat_conn = make_connection_smb2(req->sconn,
276                                         tcon,
277                                         req->session->compat_vuser,
278                                         "???",
279                                         &status);
280         if (compat_conn == NULL) {
281                 TALLOC_FREE(tcon);
282                 return status;
283         }
284         tcon->compat_conn = talloc_move(tcon, &compat_conn);
285
286         if (IS_PRINT(tcon->compat_conn)) {
287                 *out_share_type = SMB2_SHARE_TYPE_PRINT;
288         } else if (IS_IPC(tcon->compat_conn)) {
289                 *out_share_type = SMB2_SHARE_TYPE_PIPE;
290         } else {
291                 *out_share_type = SMB2_SHARE_TYPE_DISK;
292         }
293
294         *out_share_flags = 0;
295
296         if (lp_msdfs_root(SNUM(tcon->compat_conn)) && lp_host_msdfs()) {
297                 *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
298                 *out_capabilities = SMB2_SHARE_CAP_DFS;
299         } else {
300                 *out_capabilities = 0;
301         }
302
303         switch(lp_csc_policy(SNUM(tcon->compat_conn))) {
304         case CSC_POLICY_MANUAL:
305                 break;
306         case CSC_POLICY_DOCUMENTS:
307                 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
308                 break;
309         case CSC_POLICY_PROGRAMS:
310                 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
311                 break;
312         case CSC_POLICY_DISABLE:
313                 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
314                 break;
315         default:
316                 break;
317         }
318
319         if (lp_hideunreadable(SNUM(tcon->compat_conn)) ||
320             lp_hideunwriteable_files(SNUM(tcon->compat_conn))) {
321                 *out_share_flags |= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM;
322         }
323
324         *out_maximal_access = tcon->compat_conn->share_access;
325
326         *out_tree_id = tcon->tid;
327         return NT_STATUS_OK;
328 }
329
330 struct smbd_smb2_tree_connect_state {
331         const char *in_path;
332         uint8_t out_share_type;
333         uint32_t out_share_flags;
334         uint32_t out_capabilities;
335         uint32_t out_maximal_access;
336         uint32_t out_tree_id;
337 };
338
339 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
340                                         struct tevent_context *ev,
341                                         struct smbd_smb2_request *smb2req,
342                                         const char *in_path)
343 {
344         struct tevent_req *req;
345         struct smbd_smb2_tree_connect_state *state;
346         NTSTATUS status;
347
348         req = tevent_req_create(mem_ctx, &state,
349                                 struct smbd_smb2_tree_connect_state);
350         if (req == NULL) {
351                 return NULL;
352         }
353         state->in_path = in_path;
354
355         status = smbd_smb2_tree_connect(smb2req,
356                                         state->in_path,
357                                         &state->out_share_type,
358                                         &state->out_share_flags,
359                                         &state->out_capabilities,
360                                         &state->out_maximal_access,
361                                         &state->out_tree_id);
362         if (tevent_req_nterror(req, status)) {
363                 return tevent_req_post(req, ev);
364         }
365
366         tevent_req_done(req);
367         return tevent_req_post(req, ev);
368 }
369
370 static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
371                                             uint8_t *out_share_type,
372                                             uint32_t *out_share_flags,
373                                             uint32_t *out_capabilities,
374                                             uint32_t *out_maximal_access,
375                                             uint32_t *out_tree_id)
376 {
377         struct smbd_smb2_tree_connect_state *state =
378                 tevent_req_data(req,
379                 struct smbd_smb2_tree_connect_state);
380         NTSTATUS status;
381
382         if (tevent_req_is_nterror(req, &status)) {
383                 tevent_req_received(req);
384                 return status;
385         }
386
387         *out_share_type = state->out_share_type;
388         *out_share_flags = state->out_share_flags;
389         *out_capabilities = state->out_capabilities;
390         *out_maximal_access = state->out_maximal_access;
391         *out_tree_id = state->out_tree_id;
392
393         tevent_req_received(req);
394         return NT_STATUS_OK;
395 }
396
397 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
398 {
399         NTSTATUS status;
400         DATA_BLOB outbody;
401
402         status = smbd_smb2_request_verify_sizes(req, 0x04);
403         if (!NT_STATUS_IS_OK(status)) {
404                 return smbd_smb2_request_error(req, status);
405         }
406
407         /*
408          * TODO: cancel all outstanding requests on the tcon
409          *       and delete all file handles.
410          */
411         TALLOC_FREE(req->tcon);
412
413         outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
414         if (outbody.data == NULL) {
415                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
416         }
417
418         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
419         SSVAL(outbody.data, 0x02, 0);           /* reserved */
420
421         return smbd_smb2_request_done(req, outbody, NULL);
422 }