Line data Source code
1 : /*
2 : Unix SMB/Netbios implementation.
3 : Version 1.9.
4 : VFS initialisation and support functions
5 : Copyright (C) Tim Potter 1999
6 : Copyright (C) Alexander Bokovoy 2002
7 : Copyright (C) James Peach 2006
8 : Copyright (C) Volker Lendecke 2009
9 :
10 : This program is free software; you can redistribute it and/or modify
11 : it under the terms of the GNU General Public License as published by
12 : the Free Software Foundation; either version 3 of the License, or
13 : (at your option) any later version.
14 :
15 : This program is distributed in the hope that it will be useful,
16 : but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 : GNU General Public License for more details.
19 :
20 : You should have received a copy of the GNU General Public License
21 : along with this program. If not, see <http://www.gnu.org/licenses/>.
22 :
23 : This work was sponsored by Optifacio Software Services, Inc.
24 : */
25 :
26 : #include "includes.h"
27 : #include "system/filesys.h"
28 : #include "smbd/smbd.h"
29 : #include "smbd/globals.h"
30 : #include "../lib/util/memcache.h"
31 : #include "transfer_file.h"
32 : #include "ntioctl.h"
33 : #include "lib/util/tevent_unix.h"
34 : #include "lib/util/tevent_ntstatus.h"
35 : #include "lib/util/sys_rw.h"
36 :
37 : #undef DBGC_CLASS
38 : #define DBGC_CLASS DBGC_VFS
39 :
40 : static_decl_vfs;
41 :
42 : struct vfs_fsp_data {
43 : struct vfs_fsp_data *next;
44 : struct vfs_handle_struct *owner;
45 : void (*destroy)(void *p_data);
46 : void *_dummy_;
47 : /* NOTE: This structure contains four pointers so that we can guarantee
48 : * that the end of the structure is always both 4-byte and 8-byte aligned.
49 : */
50 : };
51 :
52 : struct vfs_init_function_entry {
53 : char *name;
54 : struct vfs_init_function_entry *prev, *next;
55 : const struct vfs_fn_pointers *fns;
56 : };
57 :
58 : /****************************************************************************
59 : maintain the list of available backends
60 : ****************************************************************************/
61 :
62 114303 : static struct vfs_init_function_entry *vfs_find_backend_entry(const char *name)
63 : {
64 114303 : struct vfs_init_function_entry *entry = backends;
65 :
66 114303 : DEBUG(10, ("vfs_find_backend_entry called for %s\n", name));
67 :
68 611360 : while(entry) {
69 468254 : if (strcmp(entry->name, name)==0) return entry;
70 417922 : entry = entry->next;
71 : }
72 :
73 63971 : return NULL;
74 : }
75 :
76 42389 : NTSTATUS smb_register_vfs(int version, const char *name,
77 : const struct vfs_fn_pointers *fns)
78 : {
79 42389 : struct vfs_init_function_entry *entry = backends;
80 :
81 42389 : if ((version != SMB_VFS_INTERFACE_VERSION)) {
82 0 : DEBUG(0, ("Failed to register vfs module.\n"
83 : "The module was compiled against SMB_VFS_INTERFACE_VERSION %d,\n"
84 : "current SMB_VFS_INTERFACE_VERSION is %d.\n"
85 : "Please recompile against the current Samba Version!\n",
86 : version, SMB_VFS_INTERFACE_VERSION));
87 0 : return NT_STATUS_OBJECT_TYPE_MISMATCH;
88 : }
89 :
90 42389 : if (!name || !name[0]) {
91 0 : DEBUG(0,("smb_register_vfs() called with NULL pointer or empty name!\n"));
92 0 : return NT_STATUS_INVALID_PARAMETER;
93 : }
94 :
95 42389 : if (vfs_find_backend_entry(name)) {
96 0 : DEBUG(0,("VFS module %s already loaded!\n", name));
97 0 : return NT_STATUS_OBJECT_NAME_COLLISION;
98 : }
99 :
100 42389 : entry = SMB_XMALLOC_P(struct vfs_init_function_entry);
101 42389 : entry->name = smb_xstrdup(name);
102 42389 : entry->fns = fns;
103 :
104 42389 : DLIST_ADD(backends, entry);
105 42389 : DEBUG(5, ("Successfully added vfs backend '%s'\n", name));
106 42389 : return NT_STATUS_OK;
107 : }
108 :
109 : /****************************************************************************
110 : initialise default vfs hooks
111 : ****************************************************************************/
112 :
113 7966 : static void vfs_init_default(connection_struct *conn)
114 : {
115 7966 : DEBUG(3, ("Initialising default vfs hooks\n"));
116 7966 : vfs_init_custom(conn, DEFAULT_VFS_MODULE_NAME);
117 7966 : }
118 :
119 : /****************************************************************************
120 : initialise custom vfs hooks
121 : ****************************************************************************/
122 :
123 50332 : bool vfs_init_custom(connection_struct *conn, const char *vfs_object)
124 : {
125 50332 : char *module_path = NULL;
126 50332 : char *module_name = NULL;
127 50332 : char *module_param = NULL, *p;
128 : vfs_handle_struct *handle;
129 : const struct vfs_init_function_entry *entry;
130 :
131 50332 : if (!conn||!vfs_object||!vfs_object[0]) {
132 0 : DEBUG(0, ("vfs_init_custom() called with NULL pointer or "
133 : "empty vfs_object!\n"));
134 0 : return False;
135 : }
136 :
137 50332 : if(!backends) {
138 4875 : static_init_vfs(NULL);
139 : }
140 :
141 50332 : DEBUG(3, ("Initialising custom vfs hooks from [%s]\n", vfs_object));
142 :
143 50332 : module_path = smb_xstrdup(vfs_object);
144 :
145 50332 : p = strchr_m(module_path, ':');
146 :
147 50332 : if (p) {
148 0 : *p = 0;
149 0 : module_param = p+1;
150 0 : trim_char(module_param, ' ', ' ');
151 : }
152 :
153 50332 : trim_char(module_path, ' ', ' ');
154 :
155 50332 : module_name = smb_xstrdup(module_path);
156 :
157 55869 : if ((module_name[0] == '/') &&
158 7966 : (strcmp(module_path, DEFAULT_VFS_MODULE_NAME) != 0)) {
159 :
160 : /*
161 : * Extract the module name from the path. Just use the base
162 : * name of the last path component.
163 : */
164 :
165 0 : SAFE_FREE(module_name);
166 0 : module_name = smb_xstrdup(strrchr_m(module_path, '/')+1);
167 :
168 0 : p = strchr_m(module_name, '.');
169 :
170 0 : if (p != NULL) {
171 0 : *p = '\0';
172 : }
173 : }
174 :
175 : /* First, try to load the module with the new module system */
176 50332 : entry = vfs_find_backend_entry(module_name);
177 50332 : if (!entry) {
178 : NTSTATUS status;
179 :
180 21582 : DEBUG(5, ("vfs module [%s] not loaded - trying to load...\n",
181 : vfs_object));
182 :
183 21582 : status = smb_load_module("vfs", module_path);
184 21582 : if (!NT_STATUS_IS_OK(status)) {
185 0 : DEBUG(0, ("error probing vfs module '%s': %s\n",
186 : module_path, nt_errstr(status)));
187 0 : goto fail;
188 : }
189 :
190 21582 : entry = vfs_find_backend_entry(module_name);
191 21582 : if (!entry) {
192 0 : DEBUG(0,("Can't find a vfs module [%s]\n",vfs_object));
193 0 : goto fail;
194 : }
195 : }
196 :
197 50332 : DEBUGADD(5,("Successfully loaded vfs module [%s] with the new modules system\n", vfs_object));
198 :
199 50332 : handle = talloc_zero(conn, vfs_handle_struct);
200 50332 : if (!handle) {
201 0 : DEBUG(0,("TALLOC_ZERO() failed!\n"));
202 0 : goto fail;
203 : }
204 50332 : handle->conn = conn;
205 50332 : handle->fns = entry->fns;
206 50332 : if (module_param) {
207 0 : handle->param = talloc_strdup(conn, module_param);
208 : }
209 50332 : DLIST_ADD(conn->vfs_handles, handle);
210 :
211 50332 : SAFE_FREE(module_path);
212 50332 : SAFE_FREE(module_name);
213 50332 : return True;
214 :
215 0 : fail:
216 0 : SAFE_FREE(module_path);
217 0 : SAFE_FREE(module_name);
218 0 : return False;
219 : }
220 :
221 : /*****************************************************************
222 : Allow VFS modules to extend files_struct with VFS-specific state.
223 : This will be ok for small numbers of extensions, but might need to
224 : be refactored if it becomes more widely used.
225 : ******************************************************************/
226 :
227 : #define EXT_DATA_AREA(e) ((uint8_t *)(e) + sizeof(struct vfs_fsp_data))
228 :
229 2406 : void *vfs_add_fsp_extension_notype(vfs_handle_struct *handle,
230 : files_struct *fsp, size_t ext_size,
231 : void (*destroy_fn)(void *p_data))
232 : {
233 : struct vfs_fsp_data *ext;
234 : void * ext_data;
235 :
236 : /* Prevent VFS modules adding multiple extensions. */
237 2406 : if ((ext_data = vfs_fetch_fsp_extension(handle, fsp))) {
238 0 : return ext_data;
239 : }
240 :
241 2406 : ext = talloc_zero_size(
242 : handle->conn, sizeof(struct vfs_fsp_data) + ext_size);
243 2406 : if (ext == NULL) {
244 0 : return NULL;
245 : }
246 :
247 2406 : ext->owner = handle;
248 2406 : ext->next = fsp->vfs_extension;
249 2406 : ext->destroy = destroy_fn;
250 2406 : fsp->vfs_extension = ext;
251 2406 : return EXT_DATA_AREA(ext);
252 : }
253 :
254 2382 : void vfs_remove_fsp_extension(vfs_handle_struct *handle, files_struct *fsp)
255 : {
256 : struct vfs_fsp_data *curr;
257 : struct vfs_fsp_data *prev;
258 :
259 4419 : for (curr = fsp->vfs_extension, prev = NULL;
260 345 : curr;
261 0 : prev = curr, curr = curr->next) {
262 2382 : if (curr->owner == handle) {
263 2382 : if (prev) {
264 0 : prev->next = curr->next;
265 : } else {
266 2382 : fsp->vfs_extension = curr->next;
267 : }
268 2382 : if (curr->destroy) {
269 0 : curr->destroy(EXT_DATA_AREA(curr));
270 : }
271 2382 : TALLOC_FREE(curr);
272 2382 : return;
273 : }
274 : }
275 : }
276 :
277 146114 : void vfs_remove_all_fsp_extensions(files_struct *fsp)
278 : {
279 : struct vfs_fsp_data *curr;
280 : struct vfs_fsp_data *next;
281 :
282 146138 : for (curr = fsp->vfs_extension; curr; curr = next) {
283 :
284 24 : next = curr->next;
285 24 : fsp->vfs_extension = next;
286 :
287 24 : if (curr->destroy) {
288 0 : curr->destroy(EXT_DATA_AREA(curr));
289 : }
290 24 : TALLOC_FREE(curr);
291 : }
292 146114 : }
293 :
294 4828 : void *vfs_memctx_fsp_extension(vfs_handle_struct *handle,
295 : const struct files_struct *fsp)
296 : {
297 : struct vfs_fsp_data *head;
298 :
299 4828 : for (head = fsp->vfs_extension; head; head = head->next) {
300 1781 : if (head->owner == handle) {
301 1781 : return head;
302 : }
303 : }
304 :
305 3047 : return NULL;
306 : }
307 :
308 4780 : void *vfs_fetch_fsp_extension(vfs_handle_struct *handle,
309 : const struct files_struct *fsp)
310 : {
311 : struct vfs_fsp_data *head;
312 :
313 4780 : head = (struct vfs_fsp_data *)vfs_memctx_fsp_extension(handle, fsp);
314 4780 : if (head != NULL) {
315 1733 : return EXT_DATA_AREA(head);
316 : }
317 :
318 3047 : return NULL;
319 : }
320 :
321 : #undef EXT_DATA_AREA
322 :
323 : /*
324 : * Ensure this module catches all VFS functions.
325 : */
326 : #ifdef DEVELOPER
327 12048 : void smb_vfs_assert_all_fns(const struct vfs_fn_pointers* fns,
328 : const char *module)
329 : {
330 12048 : bool missing_fn = false;
331 : unsigned int idx;
332 12048 : const uintptr_t *end = (const uintptr_t *)(fns + 1);
333 :
334 1216848 : for (idx = 0; ((const uintptr_t *)fns + idx) < end; idx++) {
335 1204800 : if (*((const uintptr_t *)fns + idx) == 0) {
336 0 : DBG_ERR("VFS function at index %d not implemented "
337 : "in module %s\n", idx, module);
338 0 : missing_fn = true;
339 : }
340 : }
341 :
342 12048 : if (missing_fn) {
343 0 : smb_panic("Required VFS function not implemented in module.\n");
344 : }
345 12048 : }
346 : #else
347 : void smb_vfs_assert_all_fns(const struct vfs_fn_pointers* fns,
348 : const char *module)
349 : {
350 : }
351 : #endif
352 :
353 : /*****************************************************************
354 : Generic VFS init.
355 : ******************************************************************/
356 :
357 7966 : bool smbd_vfs_init(connection_struct *conn)
358 : {
359 : const char **vfs_objects;
360 7966 : unsigned int i = 0;
361 7966 : int j = 0;
362 :
363 : /* Normal share - initialise with disk access functions */
364 7966 : vfs_init_default(conn);
365 :
366 : /* No need to load vfs modules for printer connections */
367 7966 : if (conn->printer) {
368 0 : return True;
369 : }
370 :
371 7966 : if (lp_widelinks(SNUM(conn))) {
372 : /*
373 : * As the widelinks logic is now moving into a
374 : * vfs_widelinks module, we need to custom load
375 : * it after the default module is initialized.
376 : * That way no changes to smb.conf files are
377 : * needed.
378 : */
379 8 : bool ok = vfs_init_custom(conn, "widelinks");
380 8 : if (!ok) {
381 0 : DBG_ERR("widelinks enabled and vfs_init_custom "
382 : "failed for vfs_widelinks module\n");
383 0 : return false;
384 : }
385 : }
386 :
387 7966 : vfs_objects = lp_vfs_objects(SNUM(conn));
388 :
389 : /* Override VFS functions if 'vfs object' was not specified*/
390 7966 : if (!vfs_objects || !vfs_objects[0])
391 0 : return True;
392 :
393 55861 : for (i=0; vfs_objects[i] ;) {
394 42358 : i++;
395 : }
396 :
397 50324 : for (j=i-1; j >= 0; j--) {
398 42358 : if (!vfs_init_custom(conn, vfs_objects[j])) {
399 0 : DEBUG(0, ("smbd_vfs_init: vfs_init_custom failed for %s\n", vfs_objects[j]));
400 0 : return False;
401 : }
402 : }
403 7966 : return True;
404 : }
405 :
406 : /*******************************************************************
407 : Check if a file exists in the vfs.
408 : ********************************************************************/
409 :
410 0 : NTSTATUS vfs_file_exist(connection_struct *conn, struct smb_filename *smb_fname)
411 : {
412 : /* Only return OK if stat was successful and S_ISREG */
413 0 : if ((SMB_VFS_STAT(conn, smb_fname) != -1) &&
414 0 : S_ISREG(smb_fname->st.st_ex_mode)) {
415 0 : return NT_STATUS_OK;
416 : }
417 :
418 0 : return NT_STATUS_OBJECT_NAME_NOT_FOUND;
419 : }
420 :
421 349 : bool vfs_valid_pread_range(off_t offset, size_t length)
422 : {
423 349 : return sys_valid_io_range(offset, length);
424 : }
425 :
426 490 : bool vfs_valid_pwrite_range(off_t offset, size_t length)
427 : {
428 : /*
429 : * See MAXFILESIZE in [MS-FSA] 2.1.5.3 Server Requests a Write
430 : */
431 : static const uint64_t maxfilesize = 0xfffffff0000;
432 : uint64_t last_byte_ofs;
433 : bool ok;
434 :
435 490 : ok = sys_valid_io_range(offset, length);
436 490 : if (!ok) {
437 0 : return false;
438 : }
439 :
440 490 : if (length == 0) {
441 19 : return true;
442 : }
443 :
444 471 : last_byte_ofs = offset + length;
445 471 : if (last_byte_ofs > maxfilesize) {
446 0 : return false;
447 : }
448 :
449 471 : return true;
450 : }
451 :
452 24 : ssize_t vfs_pwrite_data(struct smb_request *req,
453 : files_struct *fsp,
454 : const char *buffer,
455 : size_t N,
456 : off_t offset)
457 : {
458 24 : size_t total=0;
459 : ssize_t ret;
460 : bool ok;
461 :
462 24 : ok = vfs_valid_pwrite_range(offset, N);
463 24 : if (!ok) {
464 0 : errno = EINVAL;
465 0 : return -1;
466 : }
467 :
468 24 : if (req && req->unread_bytes) {
469 0 : int sockfd = req->xconn->transport.sock;
470 0 : SMB_ASSERT(req->unread_bytes == N);
471 : /* VFS_RECVFILE must drain the socket
472 : * before returning. */
473 0 : req->unread_bytes = 0;
474 : /*
475 : * Leave the socket non-blocking and
476 : * use SMB_VFS_RECVFILE. If it returns
477 : * EAGAIN || EWOULDBLOCK temporarily set
478 : * the socket blocking and retry
479 : * the RECVFILE.
480 : */
481 0 : while (total < N) {
482 0 : ret = SMB_VFS_RECVFILE(sockfd,
483 : fsp,
484 : offset + total,
485 : N - total);
486 0 : if (ret == 0 || (ret == -1 &&
487 0 : (errno == EAGAIN ||
488 0 : errno == EWOULDBLOCK))) {
489 : int old_flags;
490 : /* Ensure the socket is blocking. */
491 0 : old_flags = fcntl(sockfd, F_GETFL, 0);
492 0 : if (set_blocking(sockfd, true) == -1) {
493 0 : return (ssize_t)-1;
494 : }
495 0 : ret = SMB_VFS_RECVFILE(sockfd,
496 : fsp,
497 : offset + total,
498 : N - total);
499 0 : if (fcntl(sockfd, F_SETFL, old_flags) == -1) {
500 0 : return (ssize_t)-1;
501 : }
502 0 : if (ret == -1) {
503 0 : return (ssize_t)-1;
504 : }
505 0 : total += ret;
506 0 : return (ssize_t)total;
507 : }
508 : /* Any other error case. */
509 0 : if (ret == -1) {
510 0 : return ret;
511 : }
512 0 : total += ret;
513 : }
514 0 : return (ssize_t)total;
515 : }
516 :
517 60 : while (total < N) {
518 24 : ret = SMB_VFS_PWRITE(fsp, buffer + total, N - total,
519 : offset + total);
520 :
521 24 : if (ret == -1)
522 0 : return -1;
523 24 : if (ret == 0)
524 0 : return total;
525 :
526 24 : total += ret;
527 : }
528 24 : return (ssize_t)total;
529 : }
530 : /****************************************************************************
531 : An allocate file space call using the vfs interface.
532 : Allocates space for a file from a filedescriptor.
533 : Returns 0 on success, -1 on failure.
534 : ****************************************************************************/
535 :
536 19 : int vfs_allocate_file_space(files_struct *fsp, uint64_t len)
537 : {
538 : int ret;
539 19 : connection_struct *conn = fsp->conn;
540 : uint64_t space_avail;
541 : uint64_t bsize,dfree,dsize;
542 : NTSTATUS status;
543 : bool ok;
544 :
545 : /*
546 : * Actually try and commit the space on disk....
547 : */
548 :
549 19 : DEBUG(10,("vfs_allocate_file_space: file %s, len %.0f\n",
550 : fsp_str_dbg(fsp), (double)len));
551 :
552 19 : ok = vfs_valid_pwrite_range((off_t)len, 0);
553 19 : if (!ok) {
554 0 : DEBUG(0,("vfs_allocate_file_space: %s negative/invalid len "
555 : "requested.\n", fsp_str_dbg(fsp)));
556 0 : errno = EINVAL;
557 0 : return -1;
558 : }
559 :
560 19 : status = vfs_stat_fsp(fsp);
561 19 : if (!NT_STATUS_IS_OK(status)) {
562 0 : return -1;
563 : }
564 :
565 19 : if (len == (uint64_t)fsp->fsp_name->st.st_ex_size)
566 0 : return 0;
567 :
568 19 : if (len < (uint64_t)fsp->fsp_name->st.st_ex_size) {
569 : /* Shrink - use ftruncate. */
570 :
571 0 : DEBUG(10,("vfs_allocate_file_space: file %s, shrink. Current "
572 : "size %.0f\n", fsp_str_dbg(fsp),
573 : (double)fsp->fsp_name->st.st_ex_size));
574 :
575 0 : contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_ALLOC_SHRINK);
576 :
577 0 : ret = SMB_VFS_FTRUNCATE(fsp, (off_t)len);
578 :
579 0 : contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_ALLOC_SHRINK);
580 :
581 0 : return ret;
582 : }
583 :
584 : /* Grow - we need to test if we have enough space. */
585 :
586 19 : contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_ALLOC_GROW);
587 :
588 19 : if (lp_strict_allocate(SNUM(fsp->conn))) {
589 : /* See if we have a syscall that will allocate beyond
590 : end-of-file without changing EOF. */
591 0 : ret = SMB_VFS_FALLOCATE(fsp, VFS_FALLOCATE_FL_KEEP_SIZE,
592 : 0, len);
593 : } else {
594 19 : ret = 0;
595 : }
596 :
597 19 : contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_ALLOC_GROW);
598 :
599 19 : if (ret == 0) {
600 : /* We changed the allocation size on disk, but not
601 : EOF - exactly as required. We're done ! */
602 19 : return 0;
603 : }
604 :
605 0 : if (ret == -1 && errno == ENOSPC) {
606 0 : return -1;
607 : }
608 :
609 0 : len -= fsp->fsp_name->st.st_ex_size;
610 0 : len /= 1024; /* Len is now number of 1k blocks needed. */
611 0 : space_avail =
612 0 : get_dfree_info(conn, fsp->fsp_name, &bsize, &dfree, &dsize);
613 0 : if (space_avail == (uint64_t)-1) {
614 0 : return -1;
615 : }
616 :
617 0 : DEBUG(10,("vfs_allocate_file_space: file %s, grow. Current size %.0f, "
618 : "needed blocks = %.0f, space avail = %.0f\n",
619 : fsp_str_dbg(fsp), (double)fsp->fsp_name->st.st_ex_size, (double)len,
620 : (double)space_avail));
621 :
622 0 : if (len > space_avail) {
623 0 : errno = ENOSPC;
624 0 : return -1;
625 : }
626 :
627 0 : return 0;
628 : }
629 :
630 : /****************************************************************************
631 : A vfs set_filelen call.
632 : set the length of a file from a filedescriptor.
633 : Returns 0 on success, -1 on failure.
634 : ****************************************************************************/
635 :
636 0 : int vfs_set_filelen(files_struct *fsp, off_t len)
637 : {
638 : int ret;
639 : bool ok;
640 :
641 0 : ok = vfs_valid_pwrite_range(len, 0);
642 0 : if (!ok) {
643 0 : errno = EINVAL;
644 0 : return -1;
645 : }
646 :
647 0 : contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_SET_FILE_LEN);
648 :
649 0 : DEBUG(10,("vfs_set_filelen: ftruncate %s to len %.0f\n",
650 : fsp_str_dbg(fsp), (double)len));
651 0 : if ((ret = SMB_VFS_FTRUNCATE(fsp, len)) != -1) {
652 0 : notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
653 : FILE_NOTIFY_CHANGE_SIZE
654 : | FILE_NOTIFY_CHANGE_ATTRIBUTES,
655 0 : fsp->fsp_name->base_name);
656 : }
657 :
658 0 : contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_SET_FILE_LEN);
659 :
660 0 : return ret;
661 : }
662 :
663 : /****************************************************************************
664 : A slow version of fallocate. Fallback code if SMB_VFS_FALLOCATE
665 : fails. Needs to be outside of the default version of SMB_VFS_FALLOCATE
666 : as this is also called from the default SMB_VFS_FTRUNCATE code.
667 : Always extends the file size.
668 : Returns 0 on success, -1 on failure.
669 : ****************************************************************************/
670 :
671 : #define SPARSE_BUF_WRITE_SIZE (32*1024)
672 :
673 0 : int vfs_slow_fallocate(files_struct *fsp, off_t offset, off_t len)
674 : {
675 : ssize_t pwrite_ret;
676 0 : size_t total = 0;
677 : bool ok;
678 :
679 0 : ok = vfs_valid_pwrite_range(offset, len);
680 0 : if (!ok) {
681 0 : errno = EINVAL;
682 0 : return -1;
683 : }
684 :
685 0 : if (!sparse_buf) {
686 0 : sparse_buf = SMB_CALLOC_ARRAY(char, SPARSE_BUF_WRITE_SIZE);
687 0 : if (!sparse_buf) {
688 0 : errno = ENOMEM;
689 0 : return -1;
690 : }
691 : }
692 :
693 0 : while (total < len) {
694 0 : size_t curr_write_size = MIN(SPARSE_BUF_WRITE_SIZE, (len - total));
695 :
696 0 : pwrite_ret = SMB_VFS_PWRITE(fsp, sparse_buf, curr_write_size, offset + total);
697 0 : if (pwrite_ret == -1) {
698 0 : int saved_errno = errno;
699 0 : DEBUG(10,("vfs_slow_fallocate: SMB_VFS_PWRITE for file "
700 : "%s failed with error %s\n",
701 : fsp_str_dbg(fsp), strerror(saved_errno)));
702 0 : errno = saved_errno;
703 0 : return -1;
704 : }
705 0 : total += pwrite_ret;
706 : }
707 :
708 0 : return 0;
709 : }
710 :
711 : /****************************************************************************
712 : A vfs fill sparse call.
713 : Writes zeros from the end of file to len, if len is greater than EOF.
714 : Used only by strict_sync.
715 : Returns 0 on success, -1 on failure.
716 : ****************************************************************************/
717 :
718 0 : int vfs_fill_sparse(files_struct *fsp, off_t len)
719 : {
720 : int ret;
721 : NTSTATUS status;
722 : off_t offset;
723 : size_t num_to_write;
724 : bool ok;
725 :
726 0 : ok = vfs_valid_pwrite_range(len, 0);
727 0 : if (!ok) {
728 0 : errno = EINVAL;
729 0 : return -1;
730 : }
731 :
732 0 : status = vfs_stat_fsp(fsp);
733 0 : if (!NT_STATUS_IS_OK(status)) {
734 0 : return -1;
735 : }
736 :
737 0 : if (len <= fsp->fsp_name->st.st_ex_size) {
738 0 : return 0;
739 : }
740 :
741 : #ifdef S_ISFIFO
742 0 : if (S_ISFIFO(fsp->fsp_name->st.st_ex_mode)) {
743 0 : return 0;
744 : }
745 : #endif
746 :
747 0 : DEBUG(10,("vfs_fill_sparse: write zeros in file %s from len %.0f to "
748 : "len %.0f (%.0f bytes)\n", fsp_str_dbg(fsp),
749 : (double)fsp->fsp_name->st.st_ex_size, (double)len,
750 : (double)(len - fsp->fsp_name->st.st_ex_size)));
751 :
752 0 : contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_FILL_SPARSE);
753 :
754 0 : offset = fsp->fsp_name->st.st_ex_size;
755 0 : num_to_write = len - fsp->fsp_name->st.st_ex_size;
756 :
757 : /* Only do this on non-stream file handles. */
758 0 : if (!fsp_is_alternate_stream(fsp)) {
759 : /* for allocation try fallocate first. This can fail on some
760 : * platforms e.g. when the filesystem doesn't support it and no
761 : * emulation is being done by the libc (like on AIX with JFS1). In that
762 : * case we do our own emulation. fallocate implementations can
763 : * return ENOTSUP or EINVAL in cases like that. */
764 0 : ret = SMB_VFS_FALLOCATE(fsp, 0, offset, num_to_write);
765 0 : if (ret == -1 && errno == ENOSPC) {
766 0 : goto out;
767 : }
768 0 : if (ret == 0) {
769 0 : goto out;
770 : }
771 0 : DEBUG(10,("vfs_fill_sparse: SMB_VFS_FALLOCATE failed with "
772 : "error %d. Falling back to slow manual allocation\n", ret));
773 : }
774 :
775 0 : ret = vfs_slow_fallocate(fsp, offset, num_to_write);
776 :
777 0 : out:
778 :
779 0 : contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_FILL_SPARSE);
780 0 : return ret;
781 : }
782 :
783 : /*******************************************************************************
784 : Set a fd into blocking/nonblocking mode through VFS
785 : *******************************************************************************/
786 :
787 1167 : int vfs_set_blocking(files_struct *fsp, bool set)
788 : {
789 : int val;
790 : #ifdef O_NONBLOCK
791 : #define FLAG_TO_SET O_NONBLOCK
792 : #else
793 : #ifdef SYSV
794 : #define FLAG_TO_SET O_NDELAY
795 : #else /* BSD */
796 : #define FLAG_TO_SET FNDELAY
797 : #endif
798 : #endif
799 :
800 1167 : if (fsp->fsp_flags.is_pathref) {
801 0 : return 0;
802 : }
803 :
804 1167 : val = SMB_VFS_FCNTL(fsp, F_GETFL, 0);
805 1167 : if (val == -1) {
806 0 : return -1;
807 : }
808 :
809 1167 : if (set) {
810 1167 : val &= ~FLAG_TO_SET;
811 : } else {
812 0 : val |= FLAG_TO_SET;
813 : }
814 :
815 1167 : return SMB_VFS_FCNTL(fsp, F_SETFL, val);
816 : #undef FLAG_TO_SET
817 : }
818 :
819 : /****************************************************************************
820 : Transfer some data (n bytes) between two file_struct's.
821 : ****************************************************************************/
822 :
823 0 : static ssize_t vfs_pread_fn(void *file, void *buf, size_t len, off_t offset)
824 : {
825 0 : struct files_struct *fsp = (struct files_struct *)file;
826 :
827 0 : return SMB_VFS_PREAD(fsp, buf, len, offset);
828 : }
829 :
830 0 : static ssize_t vfs_pwrite_fn(void *file, const void *buf, size_t len, off_t offset)
831 : {
832 0 : struct files_struct *fsp = (struct files_struct *)file;
833 :
834 0 : return SMB_VFS_PWRITE(fsp, buf, len, offset);
835 : }
836 :
837 0 : off_t vfs_transfer_file(files_struct *in, files_struct *out, off_t n)
838 : {
839 0 : return transfer_file_internal((void *)in, (void *)out, n,
840 : vfs_pread_fn, vfs_pwrite_fn);
841 : }
842 :
843 : /*******************************************************************
844 : A vfs_readdir wrapper which just returns the file name.
845 : ********************************************************************/
846 :
847 34255 : const char *vfs_readdirname(connection_struct *conn,
848 : struct files_struct *dirfsp,
849 : void *p,
850 : SMB_STRUCT_STAT *sbuf,
851 : char **talloced)
852 : {
853 34255 : struct dirent *ptr= NULL;
854 : const char *dname;
855 : char *translated;
856 : NTSTATUS status;
857 :
858 34255 : if (!p)
859 0 : return(NULL);
860 :
861 34255 : ptr = SMB_VFS_READDIR(conn, dirfsp, (DIR *)p, sbuf);
862 34255 : if (!ptr)
863 4769 : return(NULL);
864 :
865 29486 : dname = ptr->d_name;
866 :
867 29486 : status = SMB_VFS_TRANSLATE_NAME(conn, dname, vfs_translate_to_windows,
868 : talloc_tos(), &translated);
869 29486 : if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
870 29486 : *talloced = NULL;
871 29486 : return dname;
872 : }
873 0 : *talloced = translated;
874 0 : if (!NT_STATUS_IS_OK(status)) {
875 0 : return NULL;
876 : }
877 0 : return translated;
878 : }
879 :
880 : /*******************************************************************
881 : A wrapper for vfs_chdir().
882 : ********************************************************************/
883 :
884 179596 : int vfs_ChDir(connection_struct *conn, const struct smb_filename *smb_fname)
885 : {
886 : int ret;
887 179596 : struct smb_filename *cwd = NULL;
888 :
889 179596 : if (!LastDir) {
890 27588 : LastDir = SMB_STRDUP("");
891 : }
892 :
893 179596 : if (ISDOT(smb_fname->base_name)) {
894 : /*
895 : * passing a '.' is a noop,
896 : * and we only expect this after
897 : * everything is initialized.
898 : *
899 : * So the first vfs_ChDir() on a given
900 : * connection_struct must not be '.'.
901 : *
902 : * Note: conn_new() sets
903 : * conn->cwd_fsp->fh->fd = -1
904 : * and vfs_ChDir() leaves with
905 : * conn->cwd_fsp->fh->fd = AT_FDCWD
906 : * on success!
907 : */
908 0 : if (fsp_get_pathref_fd(conn->cwd_fsp) != AT_FDCWD) {
909 : /*
910 : * This should never happen and
911 : * we might change this to
912 : * SMB_ASSERT() in future.
913 : */
914 0 : DBG_ERR("Called with '.' as first operation!\n");
915 0 : log_stack_trace();
916 0 : errno = EINVAL;
917 0 : return -1;
918 : }
919 0 : return 0;
920 : }
921 :
922 351174 : if (smb_fname->base_name[0] == '/' &&
923 171578 : strcsequal(LastDir,smb_fname->base_name))
924 : {
925 : /*
926 : * conn->cwd_fsp->fsp_name and the kernel
927 : * are already correct, but conn->cwd_fsp->fh->fd
928 : * might still be -1 as initialized in conn_new().
929 : *
930 : * This can happen when a client made a 2nd
931 : * tree connect to a share with the same underlying
932 : * path (may or may not the same share).
933 : */
934 124341 : fsp_set_fd(conn->cwd_fsp, AT_FDCWD);
935 124341 : return 0;
936 : }
937 :
938 55255 : DEBUG(4,("vfs_ChDir to %s\n", smb_fname->base_name));
939 :
940 55255 : ret = SMB_VFS_CHDIR(conn, smb_fname);
941 55255 : if (ret != 0) {
942 851 : return -1;
943 : }
944 :
945 : /*
946 : * Always replace conn->cwd_fsp. We
947 : * don't know if it's been modified by
948 : * VFS modules in the stack.
949 : */
950 54404 : fsp_set_fd(conn->cwd_fsp, AT_FDCWD);
951 :
952 : /* conn cache. */
953 54404 : cwd = vfs_GetWd(conn, conn);
954 54404 : if (cwd == NULL) {
955 : /*
956 : * vfs_GetWd() failed.
957 : * We must be able to read cwd.
958 : * Return to original directory
959 : * and return -1.
960 : */
961 0 : int saved_errno = errno;
962 :
963 0 : if (conn->cwd_fsp->fsp_name == NULL) {
964 : /*
965 : * Failed on the very first chdir()+getwd()
966 : * for this connection. We can't
967 : * continue.
968 : */
969 0 : smb_panic("conn->cwd getwd failed\n");
970 : /* NOTREACHED */
971 : return -1;
972 : }
973 :
974 : /* Return to the previous $cwd. */
975 0 : ret = SMB_VFS_CHDIR(conn, conn->cwd_fsp->fsp_name);
976 0 : if (ret != 0) {
977 0 : smb_panic("conn->cwd getwd failed\n");
978 : /* NOTREACHED */
979 : return -1;
980 : }
981 0 : errno = saved_errno;
982 : /* And fail the chdir(). */
983 0 : return -1;
984 : }
985 :
986 : /* vfs_GetWd() succeeded. */
987 : /* Replace global cache. */
988 54404 : SAFE_FREE(LastDir);
989 54404 : LastDir = SMB_STRDUP(smb_fname->base_name);
990 :
991 : /*
992 : * (Indirect) Callers of vfs_ChDir() may still hold references to the
993 : * old conn->cwd_fsp->fsp_name. Move it to talloc_tos(), that way
994 : * callers can use it for the lifetime of the SMB request.
995 : */
996 54404 : talloc_move(talloc_tos(), &conn->cwd_fsp->fsp_name);
997 :
998 54404 : conn->cwd_fsp->fsp_name = talloc_move(conn->cwd_fsp, &cwd);
999 :
1000 54404 : DBG_INFO("vfs_ChDir got %s\n", fsp_str_dbg(conn->cwd_fsp));
1001 :
1002 54404 : return ret;
1003 : }
1004 :
1005 : /*******************************************************************
1006 : Return the absolute current directory path - given a UNIX pathname.
1007 : Note that this path is returned in DOS format, not UNIX
1008 : format. Note this can be called with conn == NULL.
1009 : ********************************************************************/
1010 :
1011 70066 : struct smb_filename *vfs_GetWd(TALLOC_CTX *ctx, connection_struct *conn)
1012 : {
1013 70066 : struct smb_filename *current_dir_fname = NULL;
1014 : struct file_id key;
1015 70066 : struct smb_filename *smb_fname_dot = NULL;
1016 70066 : struct smb_filename *smb_fname_full = NULL;
1017 70066 : struct smb_filename *result = NULL;
1018 :
1019 70066 : if (!lp_getwd_cache()) {
1020 0 : goto nocache;
1021 : }
1022 :
1023 70066 : smb_fname_dot = synthetic_smb_fname(ctx,
1024 : ".",
1025 : NULL,
1026 : NULL,
1027 : 0,
1028 : 0);
1029 70066 : if (smb_fname_dot == NULL) {
1030 0 : errno = ENOMEM;
1031 0 : goto out;
1032 : }
1033 :
1034 70066 : if (SMB_VFS_STAT(conn, smb_fname_dot) == -1) {
1035 : /*
1036 : * Known to fail for root: the directory may be NFS-mounted
1037 : * and exported with root_squash (so has no root access).
1038 : */
1039 0 : DEBUG(1,("vfs_GetWd: couldn't stat \".\" error %s "
1040 : "(NFS problem ?)\n", strerror(errno) ));
1041 0 : goto nocache;
1042 : }
1043 :
1044 70066 : key = vfs_file_id_from_sbuf(conn, &smb_fname_dot->st);
1045 :
1046 70066 : smb_fname_full = (struct smb_filename *)memcache_lookup_talloc(
1047 : smbd_memcache(),
1048 : GETWD_CACHE,
1049 : data_blob_const(&key, sizeof(key)));
1050 :
1051 70066 : if (smb_fname_full == NULL) {
1052 13103 : goto nocache;
1053 : }
1054 :
1055 101663 : if ((SMB_VFS_STAT(conn, smb_fname_full) == 0) &&
1056 101663 : (smb_fname_dot->st.st_ex_dev == smb_fname_full->st.st_ex_dev) &&
1057 101663 : (smb_fname_dot->st.st_ex_ino == smb_fname_full->st.st_ex_ino) &&
1058 56963 : (S_ISDIR(smb_fname_dot->st.st_ex_mode))) {
1059 : /*
1060 : * Ok, we're done
1061 : * Note: smb_fname_full is owned by smbd_memcache()
1062 : * so we must make a copy to return.
1063 : */
1064 56963 : result = cp_smb_filename(ctx, smb_fname_full);
1065 56963 : if (result == NULL) {
1066 0 : errno = ENOMEM;
1067 : }
1068 56963 : goto out;
1069 : }
1070 :
1071 0 : nocache:
1072 :
1073 : /*
1074 : * We don't have the information to hand so rely on traditional
1075 : * methods. The very slow getcwd, which spawns a process on some
1076 : * systems, or the not quite so bad getwd.
1077 : */
1078 :
1079 13103 : current_dir_fname = SMB_VFS_GETWD(conn, ctx);
1080 13103 : if (current_dir_fname == NULL) {
1081 0 : DEBUG(0, ("vfs_GetWd: SMB_VFS_GETWD call failed: %s\n",
1082 : strerror(errno)));
1083 0 : goto out;
1084 : }
1085 :
1086 13103 : if (lp_getwd_cache() && VALID_STAT(smb_fname_dot->st)) {
1087 13103 : key = vfs_file_id_from_sbuf(conn, &smb_fname_dot->st);
1088 :
1089 : /*
1090 : * smbd_memcache() will own current_dir_fname after the
1091 : * memcache_add_talloc call, so we must make
1092 : * a copy on ctx to return.
1093 : */
1094 13103 : result = cp_smb_filename(ctx, current_dir_fname);
1095 13103 : if (result == NULL) {
1096 0 : errno = ENOMEM;
1097 : }
1098 :
1099 : /*
1100 : * Ensure the memory going into the cache
1101 : * doesn't have a destructor so it can be
1102 : * cleanly freed.
1103 : */
1104 13103 : talloc_set_destructor(current_dir_fname, NULL);
1105 :
1106 13103 : memcache_add_talloc(smbd_memcache(),
1107 : GETWD_CACHE,
1108 : data_blob_const(&key, sizeof(key)),
1109 : ¤t_dir_fname);
1110 : /* current_dir_fname is now == NULL here. */
1111 : } else {
1112 : /* current_dir_fname is already allocated on ctx. */
1113 0 : result = current_dir_fname;
1114 : }
1115 :
1116 70066 : out:
1117 70066 : TALLOC_FREE(smb_fname_dot);
1118 : /*
1119 : * Don't free current_dir_fname here. It's either been moved
1120 : * to the memcache or is being returned in result.
1121 : */
1122 70066 : return result;
1123 : }
1124 :
1125 : /*******************************************************************
1126 : Reduce a file name, removing .. elements and checking that
1127 : it is below dir in the hierarchy. This uses realpath.
1128 :
1129 : If cwd_name == NULL then fname is a client given path relative
1130 : to the root path of the share.
1131 :
1132 : If cwd_name != NULL then fname is a client given path relative
1133 : to cwd_name. cwd_name is relative to the root path of the share.
1134 : ********************************************************************/
1135 :
1136 60101 : NTSTATUS check_reduced_name(connection_struct *conn,
1137 : const struct smb_filename *cwd_fname,
1138 : const struct smb_filename *smb_fname)
1139 : {
1140 60101 : TALLOC_CTX *ctx = talloc_tos();
1141 60101 : const char *cwd_name = cwd_fname ? cwd_fname->base_name : NULL;
1142 60101 : const char *fname = smb_fname->base_name;
1143 : struct smb_filename *resolved_fname;
1144 60101 : char *resolved_name = NULL;
1145 60101 : char *new_fname = NULL;
1146 60101 : bool allow_symlinks = true;
1147 : const char *conn_rootdir;
1148 : size_t rootdir_len;
1149 60101 : bool parent_dir_checked = false;
1150 :
1151 60101 : DBG_DEBUG("check_reduced_name [%s] [%s]\n", fname, conn->connectpath);
1152 :
1153 60101 : resolved_fname = SMB_VFS_REALPATH(conn, ctx, smb_fname);
1154 :
1155 60101 : if (resolved_fname == NULL) {
1156 : NTSTATUS status;
1157 0 : struct smb_filename *dir_fname = NULL;
1158 0 : struct smb_filename *last_component = NULL;
1159 :
1160 0 : if (errno == ENOTDIR) {
1161 0 : DBG_NOTICE("Component not a directory in getting "
1162 : "realpath for %s\n",
1163 : fname);
1164 0 : return NT_STATUS_OBJECT_PATH_NOT_FOUND;
1165 : }
1166 0 : if (errno != ENOENT) {
1167 0 : status = map_nt_error_from_unix(errno);
1168 0 : DBG_NOTICE("couldn't get realpath for %s: %s\n",
1169 : fname,
1170 : strerror(errno));
1171 0 : return status;
1172 : }
1173 :
1174 : /* errno == ENOENT */
1175 :
1176 : /*
1177 : * Last component didn't exist. Remove it and try and
1178 : * canonicalise the directory name.
1179 : */
1180 :
1181 0 : status = SMB_VFS_PARENT_PATHNAME(conn,
1182 : ctx,
1183 : smb_fname,
1184 : &dir_fname,
1185 : &last_component);
1186 0 : if (!NT_STATUS_IS_OK(status)) {
1187 0 : return status;
1188 : }
1189 :
1190 0 : resolved_fname = SMB_VFS_REALPATH(conn, ctx, dir_fname);
1191 0 : if (resolved_fname == NULL) {
1192 0 : status = map_nt_error_from_unix(errno);
1193 :
1194 0 : if (errno == ENOENT || errno == ENOTDIR) {
1195 0 : status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
1196 : }
1197 :
1198 0 : DBG_NOTICE("couldn't get realpath for "
1199 : "%s (%s)\n",
1200 : smb_fname_str_dbg(dir_fname),
1201 : nt_errstr(status));
1202 0 : return status;
1203 : }
1204 0 : resolved_name = talloc_asprintf(ctx,
1205 : "%s/%s",
1206 : resolved_fname->base_name,
1207 0 : last_component->base_name);
1208 0 : if (resolved_name == NULL) {
1209 0 : return NT_STATUS_NO_MEMORY;
1210 : }
1211 0 : parent_dir_checked = true;
1212 : } else {
1213 60101 : resolved_name = resolved_fname->base_name;
1214 : }
1215 :
1216 60101 : DEBUG(10,("check_reduced_name realpath [%s] -> [%s]\n", fname,
1217 : resolved_name));
1218 :
1219 60101 : if (*resolved_name != '/') {
1220 0 : DEBUG(0,("check_reduced_name: realpath doesn't return "
1221 : "absolute paths !\n"));
1222 0 : TALLOC_FREE(resolved_fname);
1223 0 : return NT_STATUS_OBJECT_NAME_INVALID;
1224 : }
1225 :
1226 : /* Common widelinks and symlinks checks. */
1227 60101 : conn_rootdir = SMB_VFS_CONNECTPATH(conn, smb_fname);
1228 60101 : if (conn_rootdir == NULL) {
1229 0 : DBG_NOTICE("Could not get conn_rootdir\n");
1230 0 : TALLOC_FREE(resolved_fname);
1231 0 : return NT_STATUS_ACCESS_DENIED;
1232 : }
1233 :
1234 60101 : rootdir_len = strlen(conn_rootdir);
1235 :
1236 : /*
1237 : * In the case of rootdir_len == 1, we know that
1238 : * conn_rootdir is "/", and we also know that
1239 : * resolved_name starts with a slash. So, in this
1240 : * corner case, resolved_name is automatically a
1241 : * sub-directory of the conn_rootdir. Thus we can skip
1242 : * the string comparison and the next character checks
1243 : * (which are even wrong in this case).
1244 : */
1245 60101 : if (rootdir_len != 1) {
1246 : bool matched;
1247 :
1248 56446 : matched = (strncmp(conn_rootdir, resolved_name,
1249 : rootdir_len) == 0);
1250 83155 : if (!matched || (resolved_name[rootdir_len] != '/' &&
1251 36250 : resolved_name[rootdir_len] != '\0')) {
1252 9033 : DBG_NOTICE("Bad access attempt: %s is a symlink "
1253 : "outside the "
1254 : "share path\n"
1255 : "conn_rootdir =%s\n"
1256 : "resolved_name=%s\n",
1257 : fname,
1258 : conn_rootdir,
1259 : resolved_name);
1260 9033 : TALLOC_FREE(resolved_fname);
1261 9033 : if (parent_dir_checked) {
1262 : /* Part of a component path. */
1263 0 : return NT_STATUS_OBJECT_PATH_NOT_FOUND;
1264 : } else {
1265 : /* End of a path. */
1266 9033 : return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1267 : }
1268 : }
1269 : }
1270 :
1271 : /* Extra checks if all symlinks are disallowed. */
1272 51068 : allow_symlinks = lp_follow_symlinks(SNUM(conn));
1273 51068 : if (!allow_symlinks) {
1274 : /* fname can't have changed in resolved_path. */
1275 468 : const char *p = &resolved_name[rootdir_len];
1276 :
1277 : /*
1278 : * UNIX filesystem semantics, names consisting
1279 : * only of "." or ".." CANNOT be symlinks.
1280 : */
1281 468 : if (ISDOT(fname) || ISDOTDOT(fname)) {
1282 234 : goto out;
1283 : }
1284 :
1285 0 : if (*p != '/') {
1286 0 : DBG_NOTICE("logic error (%c) "
1287 : "in resolved_name: %s\n",
1288 : *p,
1289 : fname);
1290 0 : TALLOC_FREE(resolved_fname);
1291 0 : return NT_STATUS_ACCESS_DENIED;
1292 : }
1293 :
1294 0 : p++;
1295 :
1296 : /*
1297 : * If cwd_name is present and not ".",
1298 : * then fname is relative to that, not
1299 : * the root of the share. Make sure the
1300 : * path we check is the one the client
1301 : * sent (cwd_name+fname).
1302 : */
1303 0 : if (cwd_name != NULL && !ISDOT(cwd_name)) {
1304 0 : new_fname = talloc_asprintf(ctx,
1305 : "%s/%s",
1306 : cwd_name,
1307 : fname);
1308 0 : if (new_fname == NULL) {
1309 0 : TALLOC_FREE(resolved_fname);
1310 0 : return NT_STATUS_NO_MEMORY;
1311 : }
1312 0 : fname = new_fname;
1313 : }
1314 :
1315 0 : if (strcmp(fname, p)!=0) {
1316 0 : DBG_NOTICE("Bad access "
1317 : "attempt: %s is a symlink to %s\n",
1318 : fname,
1319 : p);
1320 0 : TALLOC_FREE(resolved_fname);
1321 0 : TALLOC_FREE(new_fname);
1322 0 : if (parent_dir_checked) {
1323 : /* Part of a component path. */
1324 0 : return NT_STATUS_OBJECT_PATH_NOT_FOUND;
1325 : } else {
1326 : /* End of a path. */
1327 0 : return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1328 : }
1329 : }
1330 : }
1331 :
1332 50834 : out:
1333 :
1334 51068 : DBG_INFO("%s reduced to %s\n", fname, resolved_name);
1335 51068 : TALLOC_FREE(resolved_fname);
1336 51068 : TALLOC_FREE(new_fname);
1337 51068 : return NT_STATUS_OK;
1338 : }
1339 :
1340 : /*
1341 : * Ensure LSTAT is called for POSIX paths.
1342 : */
1343 4700 : int vfs_stat(struct connection_struct *conn,
1344 : struct smb_filename *smb_fname)
1345 : {
1346 4700 : if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
1347 0 : return SMB_VFS_LSTAT(conn, smb_fname);
1348 : }
1349 4700 : return SMB_VFS_STAT(conn, smb_fname);
1350 : }
1351 :
1352 : /**
1353 : * XXX: This is temporary and there should be no callers of this once
1354 : * smb_filename is plumbed through all path based operations.
1355 : *
1356 : * Called when we know stream name parsing has already been done.
1357 : */
1358 0 : int vfs_stat_smb_basename(struct connection_struct *conn,
1359 : const struct smb_filename *smb_fname_in,
1360 : SMB_STRUCT_STAT *psbuf)
1361 : {
1362 0 : struct smb_filename smb_fname = {
1363 0 : .base_name = discard_const_p(char, smb_fname_in->base_name),
1364 0 : .flags = smb_fname_in->flags,
1365 0 : .twrp = smb_fname_in->twrp,
1366 : };
1367 : int ret;
1368 :
1369 0 : ret = vfs_stat(conn, &smb_fname);
1370 0 : if (ret != -1) {
1371 0 : *psbuf = smb_fname.st;
1372 : }
1373 0 : return ret;
1374 : }
1375 :
1376 : /**
1377 : * Ensure LSTAT is called for POSIX paths.
1378 : */
1379 :
1380 139823 : NTSTATUS vfs_stat_fsp(files_struct *fsp)
1381 : {
1382 : int ret;
1383 139823 : struct stat_ex saved_stat = fsp->fsp_name->st;
1384 :
1385 139823 : if (fsp_get_pathref_fd(fsp) == -1) {
1386 0 : if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
1387 0 : ret = SMB_VFS_LSTAT(fsp->conn, fsp->fsp_name);
1388 : } else {
1389 0 : ret = SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
1390 : }
1391 : } else {
1392 139823 : ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
1393 : }
1394 139823 : if (ret == -1) {
1395 0 : return map_nt_error_from_unix(errno);
1396 : }
1397 139823 : update_stat_ex_from_saved_stat(&fsp->fsp_name->st, &saved_stat);
1398 139823 : fsp->fsp_flags.is_directory = S_ISDIR(fsp->fsp_name->st.st_ex_mode);
1399 139823 : return NT_STATUS_OK;
1400 : }
1401 :
1402 2350 : void init_smb_file_time(struct smb_file_time *ft)
1403 : {
1404 2350 : *ft = (struct smb_file_time) {
1405 2350 : .atime = make_omit_timespec(),
1406 2350 : .ctime = make_omit_timespec(),
1407 2350 : .mtime = make_omit_timespec(),
1408 2350 : .create_time = make_omit_timespec()
1409 : };
1410 2350 : }
1411 :
1412 : /**
1413 : * Initialize num_streams and streams, then call VFS op streaminfo
1414 : */
1415 :
1416 2813 : NTSTATUS vfs_fstreaminfo(struct files_struct *fsp,
1417 : TALLOC_CTX *mem_ctx,
1418 : unsigned int *num_streams,
1419 : struct stream_struct **streams)
1420 : {
1421 2813 : *num_streams = 0;
1422 2813 : *streams = NULL;
1423 :
1424 2813 : if (fsp == NULL) {
1425 : /*
1426 : * Callers may pass fsp == NULL when passing smb_fname->fsp of a
1427 : * symlink. This is ok, handle it here, by just return no
1428 : * streams on a symlink.
1429 : */
1430 0 : return NT_STATUS_OK;
1431 : }
1432 :
1433 2813 : if (fsp_get_pathref_fd(fsp) == -1) {
1434 : /*
1435 : * No streams on non-real files/directories.
1436 : */
1437 0 : return NT_STATUS_OK;
1438 : }
1439 :
1440 2813 : return SMB_VFS_FSTREAMINFO(fsp,
1441 : mem_ctx,
1442 : num_streams,
1443 : streams);
1444 : }
1445 :
1446 24 : int vfs_fake_fd(void)
1447 : {
1448 : int pipe_fds[2];
1449 : int ret;
1450 :
1451 : /*
1452 : * Return a valid fd, but ensure any attempt to use
1453 : * it returns an error (EPIPE).
1454 : */
1455 24 : ret = pipe(pipe_fds);
1456 24 : if (ret != 0) {
1457 0 : return -1;
1458 : }
1459 :
1460 24 : close(pipe_fds[1]);
1461 24 : return pipe_fds[0];
1462 : }
1463 :
1464 : /*
1465 : * This is just a helper to make
1466 : * users of vfs_fake_fd() more symmetric
1467 : */
1468 20 : int vfs_fake_fd_close(int fd)
1469 : {
1470 20 : return close(fd);
1471 : }
1472 :
1473 : /*
1474 : generate a file_id from a stat structure
1475 : */
1476 227250 : struct file_id vfs_file_id_from_sbuf(connection_struct *conn, const SMB_STRUCT_STAT *sbuf)
1477 : {
1478 227250 : return SMB_VFS_FILE_ID_CREATE(conn, sbuf);
1479 : }
1480 :
1481 1844 : NTSTATUS vfs_at_fspcwd(TALLOC_CTX *mem_ctx,
1482 : struct connection_struct *conn,
1483 : struct files_struct **_fsp)
1484 : {
1485 1844 : struct files_struct *fsp = NULL;
1486 :
1487 1844 : fsp = talloc_zero(mem_ctx, struct files_struct);
1488 1844 : if (fsp == NULL) {
1489 0 : return NT_STATUS_NO_MEMORY;
1490 : }
1491 :
1492 1844 : fsp->fsp_name = synthetic_smb_fname(fsp, ".", NULL, NULL, 0, 0);
1493 1844 : if (fsp->fsp_name == NULL) {
1494 0 : TALLOC_FREE(fsp);
1495 0 : return NT_STATUS_NO_MEMORY;
1496 : }
1497 :
1498 1844 : fsp->fh = fd_handle_create(fsp);
1499 1844 : if (fsp->fh == NULL) {
1500 0 : TALLOC_FREE(fsp);
1501 0 : return NT_STATUS_NO_MEMORY;
1502 : }
1503 :
1504 1844 : fsp_set_fd(fsp, AT_FDCWD);
1505 1844 : fsp->fnum = FNUM_FIELD_INVALID;
1506 1844 : fsp->conn = conn;
1507 :
1508 1844 : *_fsp = fsp;
1509 1844 : return NT_STATUS_OK;
1510 : }
1511 :
1512 30496 : NTSTATUS vfs_fget_dos_attributes(struct files_struct *fsp,
1513 : uint32_t *dosmode)
1514 : {
1515 : NTSTATUS status;
1516 :
1517 : /*
1518 : * First make sure to pass the base_fsp to the VFS
1519 : */
1520 30496 : status = SMB_VFS_FGET_DOS_ATTRIBUTES(
1521 : fsp->conn, metadata_fsp(fsp), dosmode);
1522 30496 : if (!NT_STATUS_IS_OK(status)) {
1523 17399 : return status;
1524 : }
1525 :
1526 : /*
1527 : * If this isn't a stream fsp we're done, ...
1528 : */
1529 13097 : if (!fsp_is_alternate_stream(fsp)) {
1530 12933 : return NT_STATUS_OK;
1531 : }
1532 :
1533 : /*
1534 : * ...otherwise the VFS might have updated the btime, propagate the
1535 : * btime from the base_fsp to the stream fsp.
1536 : */
1537 :
1538 164 : if (fsp->base_fsp->fsp_name->st.st_ex_iflags & ST_EX_IFLAG_CALCULATED_BTIME) {
1539 : /*
1540 : * Not a value from backend storage, ignore it
1541 : */
1542 0 : return NT_STATUS_OK;
1543 : }
1544 :
1545 164 : update_stat_ex_create_time(&fsp->fsp_name->st,
1546 164 : fsp->base_fsp->fsp_name->st.st_ex_btime);
1547 :
1548 164 : return NT_STATUS_OK;
1549 : }
1550 :
1551 42370 : int smb_vfs_call_connect(struct vfs_handle_struct *handle,
1552 : const char *service, const char *user)
1553 : {
1554 44797 : VFS_FIND(connect);
1555 42370 : return handle->fns->connect_fn(handle, service, user);
1556 : }
1557 :
1558 18488 : void smb_vfs_call_disconnect(struct vfs_handle_struct *handle)
1559 : {
1560 28204 : VFS_FIND(disconnect);
1561 18488 : handle->fns->disconnect_fn(handle);
1562 18488 : }
1563 :
1564 741 : uint64_t smb_vfs_call_disk_free(struct vfs_handle_struct *handle,
1565 : const struct smb_filename *smb_fname,
1566 : uint64_t *bsize,
1567 : uint64_t *dfree,
1568 : uint64_t *dsize)
1569 : {
1570 1515 : VFS_FIND(disk_free);
1571 741 : return handle->fns->disk_free_fn(handle, smb_fname,
1572 : bsize, dfree, dsize);
1573 : }
1574 :
1575 1482 : int smb_vfs_call_get_quota(struct vfs_handle_struct *handle,
1576 : const struct smb_filename *smb_fname,
1577 : enum SMB_QUOTA_TYPE qtype,
1578 : unid_t id,
1579 : SMB_DISK_QUOTA *qt)
1580 : {
1581 3030 : VFS_FIND(get_quota);
1582 1482 : return handle->fns->get_quota_fn(handle, smb_fname, qtype, id, qt);
1583 : }
1584 :
1585 0 : int smb_vfs_call_set_quota(struct vfs_handle_struct *handle,
1586 : enum SMB_QUOTA_TYPE qtype, unid_t id,
1587 : SMB_DISK_QUOTA *qt)
1588 : {
1589 0 : VFS_FIND(set_quota);
1590 0 : return handle->fns->set_quota_fn(handle, qtype, id, qt);
1591 : }
1592 :
1593 132 : int smb_vfs_call_get_shadow_copy_data(struct vfs_handle_struct *handle,
1594 : struct files_struct *fsp,
1595 : struct shadow_copy_data *shadow_copy_data,
1596 : bool labels)
1597 : {
1598 220 : VFS_FIND(get_shadow_copy_data);
1599 132 : return handle->fns->get_shadow_copy_data_fn(handle, fsp,
1600 : shadow_copy_data,
1601 : labels);
1602 : }
1603 7365 : int smb_vfs_call_statvfs(struct vfs_handle_struct *handle,
1604 : const struct smb_filename *smb_fname,
1605 : struct vfs_statvfs_struct *statbuf)
1606 : {
1607 12193 : VFS_FIND(statvfs);
1608 7365 : return handle->fns->statvfs_fn(handle, smb_fname, statbuf);
1609 : }
1610 :
1611 11262 : uint32_t smb_vfs_call_fs_capabilities(struct vfs_handle_struct *handle,
1612 : enum timestamp_set_resolution *p_ts_res)
1613 : {
1614 14956 : VFS_FIND(fs_capabilities);
1615 11262 : return handle->fns->fs_capabilities_fn(handle, p_ts_res);
1616 : }
1617 :
1618 4755 : NTSTATUS smb_vfs_call_get_dfs_referrals(struct vfs_handle_struct *handle,
1619 : struct dfs_GetDFSReferral *r)
1620 : {
1621 7943 : VFS_FIND(get_dfs_referrals);
1622 4755 : return handle->fns->get_dfs_referrals_fn(handle, r);
1623 : }
1624 :
1625 0 : NTSTATUS smb_vfs_call_create_dfs_pathat(struct vfs_handle_struct *handle,
1626 : struct files_struct *dirfsp,
1627 : const struct smb_filename *smb_fname,
1628 : const struct referral *reflist,
1629 : size_t referral_count)
1630 : {
1631 0 : VFS_FIND(create_dfs_pathat);
1632 0 : return handle->fns->create_dfs_pathat_fn(handle,
1633 : dirfsp,
1634 : smb_fname,
1635 : reflist,
1636 : referral_count);
1637 : }
1638 :
1639 33558 : NTSTATUS smb_vfs_call_read_dfs_pathat(struct vfs_handle_struct *handle,
1640 : TALLOC_CTX *mem_ctx,
1641 : struct files_struct *dirfsp,
1642 : struct smb_filename *smb_fname,
1643 : struct referral **ppreflist,
1644 : size_t *preferral_count)
1645 : {
1646 55882 : VFS_FIND(read_dfs_pathat);
1647 33558 : return handle->fns->read_dfs_pathat_fn(handle,
1648 : mem_ctx,
1649 : dirfsp,
1650 : smb_fname,
1651 : ppreflist,
1652 : preferral_count);
1653 : }
1654 :
1655 10917 : DIR *smb_vfs_call_fdopendir(struct vfs_handle_struct *handle,
1656 : struct files_struct *fsp,
1657 : const char *mask,
1658 : uint32_t attributes)
1659 : {
1660 15747 : VFS_FIND(fdopendir);
1661 10917 : return handle->fns->fdopendir_fn(handle, fsp, mask, attributes);
1662 : }
1663 :
1664 73383 : struct dirent *smb_vfs_call_readdir(struct vfs_handle_struct *handle,
1665 : struct files_struct *dirfsp,
1666 : DIR *dirp,
1667 : SMB_STRUCT_STAT *sbuf)
1668 : {
1669 113875 : VFS_FIND(readdir);
1670 73383 : return handle->fns->readdir_fn(handle, dirfsp, dirp, sbuf);
1671 : }
1672 :
1673 0 : void smb_vfs_call_seekdir(struct vfs_handle_struct *handle,
1674 : DIR *dirp, long offset)
1675 : {
1676 0 : VFS_FIND(seekdir);
1677 0 : handle->fns->seekdir_fn(handle, dirp, offset);
1678 0 : }
1679 :
1680 49752 : long smb_vfs_call_telldir(struct vfs_handle_struct *handle,
1681 : DIR *dirp)
1682 : {
1683 80550 : VFS_FIND(telldir);
1684 49752 : return handle->fns->telldir_fn(handle, dirp);
1685 : }
1686 :
1687 0 : void smb_vfs_call_rewind_dir(struct vfs_handle_struct *handle,
1688 : DIR *dirp)
1689 : {
1690 0 : VFS_FIND(rewind_dir);
1691 0 : handle->fns->rewind_dir_fn(handle, dirp);
1692 0 : }
1693 :
1694 2588 : int smb_vfs_call_mkdirat(struct vfs_handle_struct *handle,
1695 : struct files_struct *dirfsp,
1696 : const struct smb_filename *smb_fname,
1697 : mode_t mode)
1698 : {
1699 2752 : VFS_FIND(mkdirat);
1700 2588 : return handle->fns->mkdirat_fn(handle,
1701 : dirfsp,
1702 : smb_fname,
1703 : mode);
1704 : }
1705 :
1706 10917 : int smb_vfs_call_closedir(struct vfs_handle_struct *handle,
1707 : DIR *dir)
1708 : {
1709 15747 : VFS_FIND(closedir);
1710 10917 : return handle->fns->closedir_fn(handle, dir);
1711 : }
1712 :
1713 610029 : int smb_vfs_call_openat(struct vfs_handle_struct *handle,
1714 : const struct files_struct *dirfsp,
1715 : const struct smb_filename *smb_fname,
1716 : struct files_struct *fsp,
1717 : const struct vfs_open_how *how)
1718 : {
1719 697787 : VFS_FIND(openat);
1720 610029 : return handle->fns->openat_fn(handle,
1721 : dirfsp,
1722 : smb_fname,
1723 : fsp,
1724 : how);
1725 : }
1726 :
1727 25266 : NTSTATUS smb_vfs_call_create_file(struct vfs_handle_struct *handle,
1728 : struct smb_request *req,
1729 : struct files_struct *dirfsp,
1730 : struct smb_filename *smb_fname,
1731 : uint32_t access_mask,
1732 : uint32_t share_access,
1733 : uint32_t create_disposition,
1734 : uint32_t create_options,
1735 : uint32_t file_attributes,
1736 : uint32_t oplock_request,
1737 : const struct smb2_lease *lease,
1738 : uint64_t allocation_size,
1739 : uint32_t private_flags,
1740 : struct security_descriptor *sd,
1741 : struct ea_list *ea_list,
1742 : files_struct **result,
1743 : int *pinfo,
1744 : const struct smb2_create_blobs *in_context_blobs,
1745 : struct smb2_create_blobs *out_context_blobs)
1746 : {
1747 30466 : VFS_FIND(create_file);
1748 25266 : return handle->fns->create_file_fn(
1749 : handle, req, dirfsp, smb_fname,
1750 : access_mask, share_access, create_disposition, create_options,
1751 : file_attributes, oplock_request, lease, allocation_size,
1752 : private_flags, sd, ea_list,
1753 : result, pinfo, in_context_blobs, out_context_blobs);
1754 : }
1755 :
1756 206932 : int smb_vfs_call_close(struct vfs_handle_struct *handle,
1757 : struct files_struct *fsp)
1758 : {
1759 315725 : VFS_FIND(close);
1760 206932 : return handle->fns->close_fn(handle, fsp);
1761 : }
1762 :
1763 0 : ssize_t smb_vfs_call_pread(struct vfs_handle_struct *handle,
1764 : struct files_struct *fsp, void *data, size_t n,
1765 : off_t offset)
1766 : {
1767 0 : VFS_FIND(pread);
1768 0 : return handle->fns->pread_fn(handle, fsp, data, n, offset);
1769 : }
1770 :
1771 : struct smb_vfs_call_pread_state {
1772 : ssize_t (*recv_fn)(struct tevent_req *req, struct vfs_aio_state *vfs_aio_state);
1773 : ssize_t retval;
1774 : struct vfs_aio_state vfs_aio_state;
1775 : };
1776 :
1777 : static void smb_vfs_call_pread_done(struct tevent_req *subreq);
1778 :
1779 387 : struct tevent_req *smb_vfs_call_pread_send(struct vfs_handle_struct *handle,
1780 : TALLOC_CTX *mem_ctx,
1781 : struct tevent_context *ev,
1782 : struct files_struct *fsp,
1783 : void *data,
1784 : size_t n, off_t offset)
1785 : {
1786 : struct tevent_req *req, *subreq;
1787 : struct smb_vfs_call_pread_state *state;
1788 :
1789 387 : req = tevent_req_create(mem_ctx, &state,
1790 : struct smb_vfs_call_pread_state);
1791 387 : if (req == NULL) {
1792 0 : return NULL;
1793 : }
1794 419 : VFS_FIND(pread_send);
1795 387 : state->recv_fn = handle->fns->pread_recv_fn;
1796 :
1797 387 : subreq = handle->fns->pread_send_fn(handle, state, ev, fsp, data, n,
1798 : offset);
1799 387 : if (tevent_req_nomem(subreq, req)) {
1800 0 : return tevent_req_post(req, ev);
1801 : }
1802 387 : tevent_req_set_callback(subreq, smb_vfs_call_pread_done, req);
1803 387 : return req;
1804 : }
1805 :
1806 387 : static void smb_vfs_call_pread_done(struct tevent_req *subreq)
1807 : {
1808 387 : struct tevent_req *req = tevent_req_callback_data(
1809 : subreq, struct tevent_req);
1810 387 : struct smb_vfs_call_pread_state *state = tevent_req_data(
1811 : req, struct smb_vfs_call_pread_state);
1812 :
1813 387 : state->retval = state->recv_fn(subreq, &state->vfs_aio_state);
1814 387 : TALLOC_FREE(subreq);
1815 387 : if (state->retval == -1) {
1816 0 : tevent_req_error(req, state->vfs_aio_state.error);
1817 0 : return;
1818 : }
1819 387 : tevent_req_done(req);
1820 : }
1821 :
1822 387 : ssize_t SMB_VFS_PREAD_RECV(struct tevent_req *req,
1823 : struct vfs_aio_state *vfs_aio_state)
1824 : {
1825 387 : struct smb_vfs_call_pread_state *state = tevent_req_data(
1826 : req, struct smb_vfs_call_pread_state);
1827 : ssize_t retval;
1828 :
1829 387 : if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1830 0 : tevent_req_received(req);
1831 0 : return -1;
1832 : }
1833 387 : *vfs_aio_state = state->vfs_aio_state;
1834 387 : retval = state->retval;
1835 387 : tevent_req_received(req);
1836 387 : return retval;
1837 : }
1838 :
1839 64 : ssize_t smb_vfs_call_pwrite(struct vfs_handle_struct *handle,
1840 : struct files_struct *fsp, const void *data,
1841 : size_t n, off_t offset)
1842 : {
1843 104 : VFS_FIND(pwrite);
1844 64 : return handle->fns->pwrite_fn(handle, fsp, data, n, offset);
1845 : }
1846 :
1847 : struct smb_vfs_call_pwrite_state {
1848 : ssize_t (*recv_fn)(struct tevent_req *req, struct vfs_aio_state *vfs_aio_state);
1849 : ssize_t retval;
1850 : struct vfs_aio_state vfs_aio_state;
1851 : };
1852 :
1853 : static void smb_vfs_call_pwrite_done(struct tevent_req *subreq);
1854 :
1855 521 : struct tevent_req *smb_vfs_call_pwrite_send(struct vfs_handle_struct *handle,
1856 : TALLOC_CTX *mem_ctx,
1857 : struct tevent_context *ev,
1858 : struct files_struct *fsp,
1859 : const void *data,
1860 : size_t n, off_t offset)
1861 : {
1862 : struct tevent_req *req, *subreq;
1863 : struct smb_vfs_call_pwrite_state *state;
1864 :
1865 521 : req = tevent_req_create(mem_ctx, &state,
1866 : struct smb_vfs_call_pwrite_state);
1867 521 : if (req == NULL) {
1868 0 : return NULL;
1869 : }
1870 609 : VFS_FIND(pwrite_send);
1871 521 : state->recv_fn = handle->fns->pwrite_recv_fn;
1872 :
1873 521 : subreq = handle->fns->pwrite_send_fn(handle, state, ev, fsp, data, n,
1874 : offset);
1875 521 : if (tevent_req_nomem(subreq, req)) {
1876 0 : return tevent_req_post(req, ev);
1877 : }
1878 521 : tevent_req_set_callback(subreq, smb_vfs_call_pwrite_done, req);
1879 521 : return req;
1880 : }
1881 :
1882 521 : static void smb_vfs_call_pwrite_done(struct tevent_req *subreq)
1883 : {
1884 521 : struct tevent_req *req = tevent_req_callback_data(
1885 : subreq, struct tevent_req);
1886 521 : struct smb_vfs_call_pwrite_state *state = tevent_req_data(
1887 : req, struct smb_vfs_call_pwrite_state);
1888 :
1889 521 : state->retval = state->recv_fn(subreq, &state->vfs_aio_state);
1890 521 : TALLOC_FREE(subreq);
1891 521 : if (state->retval == -1) {
1892 0 : tevent_req_error(req, state->vfs_aio_state.error);
1893 0 : return;
1894 : }
1895 521 : tevent_req_done(req);
1896 : }
1897 :
1898 521 : ssize_t SMB_VFS_PWRITE_RECV(struct tevent_req *req,
1899 : struct vfs_aio_state *vfs_aio_state)
1900 : {
1901 521 : struct smb_vfs_call_pwrite_state *state = tevent_req_data(
1902 : req, struct smb_vfs_call_pwrite_state);
1903 : ssize_t retval;
1904 :
1905 521 : if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1906 0 : tevent_req_received(req);
1907 0 : return -1;
1908 : }
1909 521 : *vfs_aio_state = state->vfs_aio_state;
1910 521 : retval = state->retval;
1911 521 : tevent_req_received(req);
1912 521 : return retval;
1913 : }
1914 :
1915 0 : off_t smb_vfs_call_lseek(struct vfs_handle_struct *handle,
1916 : struct files_struct *fsp, off_t offset,
1917 : int whence)
1918 : {
1919 0 : VFS_FIND(lseek);
1920 0 : return handle->fns->lseek_fn(handle, fsp, offset, whence);
1921 : }
1922 :
1923 0 : ssize_t smb_vfs_call_sendfile(struct vfs_handle_struct *handle, int tofd,
1924 : files_struct *fromfsp, const DATA_BLOB *header,
1925 : off_t offset, size_t count)
1926 : {
1927 0 : VFS_FIND(sendfile);
1928 0 : return handle->fns->sendfile_fn(handle, tofd, fromfsp, header, offset,
1929 : count);
1930 : }
1931 :
1932 0 : ssize_t smb_vfs_call_recvfile(struct vfs_handle_struct *handle, int fromfd,
1933 : files_struct *tofsp, off_t offset,
1934 : size_t count)
1935 : {
1936 0 : VFS_FIND(recvfile);
1937 0 : return handle->fns->recvfile_fn(handle, fromfd, tofsp, offset, count);
1938 : }
1939 :
1940 80 : int smb_vfs_call_renameat(struct vfs_handle_struct *handle,
1941 : files_struct *srcfsp,
1942 : const struct smb_filename *smb_fname_src,
1943 : files_struct *dstfsp,
1944 : const struct smb_filename *smb_fname_dst)
1945 : {
1946 110 : VFS_FIND(renameat);
1947 80 : return handle->fns->renameat_fn(handle,
1948 : srcfsp,
1949 : smb_fname_src,
1950 : dstfsp,
1951 : smb_fname_dst);
1952 : }
1953 :
1954 : struct smb_vfs_call_fsync_state {
1955 : int (*recv_fn)(struct tevent_req *req, struct vfs_aio_state *vfs_aio_state);
1956 : int retval;
1957 : struct vfs_aio_state vfs_aio_state;
1958 : };
1959 :
1960 : static void smb_vfs_call_fsync_done(struct tevent_req *subreq);
1961 :
1962 0 : struct tevent_req *smb_vfs_call_fsync_send(struct vfs_handle_struct *handle,
1963 : TALLOC_CTX *mem_ctx,
1964 : struct tevent_context *ev,
1965 : struct files_struct *fsp)
1966 : {
1967 : struct tevent_req *req, *subreq;
1968 : struct smb_vfs_call_fsync_state *state;
1969 :
1970 0 : req = tevent_req_create(mem_ctx, &state,
1971 : struct smb_vfs_call_fsync_state);
1972 0 : if (req == NULL) {
1973 0 : return NULL;
1974 : }
1975 0 : VFS_FIND(fsync_send);
1976 0 : state->recv_fn = handle->fns->fsync_recv_fn;
1977 :
1978 0 : subreq = handle->fns->fsync_send_fn(handle, state, ev, fsp);
1979 0 : if (tevent_req_nomem(subreq, req)) {
1980 0 : return tevent_req_post(req, ev);
1981 : }
1982 0 : tevent_req_set_callback(subreq, smb_vfs_call_fsync_done, req);
1983 0 : return req;
1984 : }
1985 :
1986 0 : static void smb_vfs_call_fsync_done(struct tevent_req *subreq)
1987 : {
1988 0 : struct tevent_req *req = tevent_req_callback_data(
1989 : subreq, struct tevent_req);
1990 0 : struct smb_vfs_call_fsync_state *state = tevent_req_data(
1991 : req, struct smb_vfs_call_fsync_state);
1992 :
1993 0 : state->retval = state->recv_fn(subreq, &state->vfs_aio_state);
1994 0 : TALLOC_FREE(subreq);
1995 0 : if (state->retval == -1) {
1996 0 : tevent_req_error(req, state->vfs_aio_state.error);
1997 0 : return;
1998 : }
1999 0 : tevent_req_done(req);
2000 : }
2001 :
2002 0 : int SMB_VFS_FSYNC_RECV(struct tevent_req *req, struct vfs_aio_state *vfs_aio_state)
2003 : {
2004 0 : struct smb_vfs_call_fsync_state *state = tevent_req_data(
2005 : req, struct smb_vfs_call_fsync_state);
2006 : ssize_t retval;
2007 :
2008 0 : if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2009 0 : tevent_req_received(req);
2010 0 : return -1;
2011 : }
2012 0 : *vfs_aio_state = state->vfs_aio_state;
2013 0 : retval = state->retval;
2014 0 : tevent_req_received(req);
2015 0 : return retval;
2016 : }
2017 :
2018 : /*
2019 : * Synchronous version of fsync, built from backend
2020 : * async VFS primitives. Uses a temporary sub-event
2021 : * context (NOT NESTED).
2022 : */
2023 :
2024 0 : int smb_vfs_fsync_sync(files_struct *fsp)
2025 : {
2026 0 : TALLOC_CTX *frame = talloc_stackframe();
2027 0 : struct tevent_req *req = NULL;
2028 0 : struct vfs_aio_state aio_state = { 0 };
2029 0 : int ret = -1;
2030 : bool ok;
2031 0 : struct tevent_context *ev = samba_tevent_context_init(frame);
2032 :
2033 0 : if (ev == NULL) {
2034 0 : goto out;
2035 : }
2036 :
2037 0 : req = SMB_VFS_FSYNC_SEND(talloc_tos(), ev, fsp);
2038 0 : if (req == NULL) {
2039 0 : goto out;
2040 : }
2041 :
2042 0 : ok = tevent_req_poll(req, ev);
2043 0 : if (!ok) {
2044 0 : goto out;
2045 : }
2046 :
2047 0 : ret = SMB_VFS_FSYNC_RECV(req, &aio_state);
2048 :
2049 0 : out:
2050 :
2051 0 : TALLOC_FREE(frame);
2052 0 : if (aio_state.error != 0) {
2053 0 : errno = aio_state.error;
2054 : }
2055 0 : return ret;
2056 : }
2057 :
2058 536649 : int smb_vfs_call_stat(struct vfs_handle_struct *handle,
2059 : struct smb_filename *smb_fname)
2060 : {
2061 609039 : VFS_FIND(stat);
2062 536649 : return handle->fns->stat_fn(handle, smb_fname);
2063 : }
2064 :
2065 1317016 : int smb_vfs_call_fstat(struct vfs_handle_struct *handle,
2066 : struct files_struct *fsp, SMB_STRUCT_STAT *sbuf)
2067 : {
2068 1521580 : VFS_FIND(fstat);
2069 1317016 : return handle->fns->fstat_fn(handle, fsp, sbuf);
2070 : }
2071 :
2072 6148 : int smb_vfs_call_lstat(struct vfs_handle_struct *handle,
2073 : struct smb_filename *smb_filename)
2074 : {
2075 6352 : VFS_FIND(lstat);
2076 6148 : return handle->fns->lstat_fn(handle, smb_filename);
2077 : }
2078 :
2079 0 : int smb_vfs_call_fstatat(
2080 : struct vfs_handle_struct *handle,
2081 : const struct files_struct *dirfsp,
2082 : const struct smb_filename *smb_fname,
2083 : SMB_STRUCT_STAT *sbuf,
2084 : int flags)
2085 : {
2086 0 : VFS_FIND(fstatat);
2087 0 : return handle->fns->fstatat_fn(handle, dirfsp, smb_fname, sbuf, flags);
2088 : }
2089 :
2090 51835 : uint64_t smb_vfs_call_get_alloc_size(struct vfs_handle_struct *handle,
2091 : struct files_struct *fsp,
2092 : const SMB_STRUCT_STAT *sbuf)
2093 : {
2094 75430 : VFS_FIND(get_alloc_size);
2095 51835 : return handle->fns->get_alloc_size_fn(handle, fsp, sbuf);
2096 : }
2097 :
2098 6202 : int smb_vfs_call_unlinkat(struct vfs_handle_struct *handle,
2099 : struct files_struct *dirfsp,
2100 : const struct smb_filename *smb_fname,
2101 : int flags)
2102 : {
2103 6345 : VFS_FIND(unlinkat);
2104 6202 : return handle->fns->unlinkat_fn(handle,
2105 : dirfsp,
2106 : smb_fname,
2107 : flags);
2108 : }
2109 :
2110 0 : int smb_vfs_call_fchmod(struct vfs_handle_struct *handle,
2111 : struct files_struct *fsp, mode_t mode)
2112 : {
2113 0 : VFS_FIND(fchmod);
2114 0 : return handle->fns->fchmod_fn(handle, fsp, mode);
2115 : }
2116 :
2117 2729 : int smb_vfs_call_fchown(struct vfs_handle_struct *handle,
2118 : struct files_struct *fsp, uid_t uid, gid_t gid)
2119 : {
2120 3262 : VFS_FIND(fchown);
2121 2729 : return handle->fns->fchown_fn(handle, fsp, uid, gid);
2122 : }
2123 :
2124 0 : int smb_vfs_call_lchown(struct vfs_handle_struct *handle,
2125 : const struct smb_filename *smb_fname,
2126 : uid_t uid,
2127 : gid_t gid)
2128 : {
2129 0 : VFS_FIND(lchown);
2130 0 : return handle->fns->lchown_fn(handle, smb_fname, uid, gid);
2131 : }
2132 :
2133 91683 : int smb_vfs_call_chdir(struct vfs_handle_struct *handle,
2134 : const struct smb_filename *smb_fname)
2135 : {
2136 147507 : VFS_FIND(chdir);
2137 91683 : return handle->fns->chdir_fn(handle, smb_fname);
2138 : }
2139 :
2140 19673 : struct smb_filename *smb_vfs_call_getwd(struct vfs_handle_struct *handle,
2141 : TALLOC_CTX *ctx)
2142 : {
2143 35447 : VFS_FIND(getwd);
2144 19673 : return handle->fns->getwd_fn(handle, ctx);
2145 : }
2146 :
2147 725 : int smb_vfs_call_fntimes(struct vfs_handle_struct *handle,
2148 : struct files_struct *fsp,
2149 : struct smb_file_time *ft)
2150 : {
2151 953 : VFS_FIND(fntimes);
2152 725 : return handle->fns->fntimes_fn(handle, fsp, ft);
2153 : }
2154 :
2155 100 : int smb_vfs_call_ftruncate(struct vfs_handle_struct *handle,
2156 : struct files_struct *fsp, off_t offset)
2157 : {
2158 112 : VFS_FIND(ftruncate);
2159 100 : return handle->fns->ftruncate_fn(handle, fsp, offset);
2160 : }
2161 :
2162 0 : int smb_vfs_call_fallocate(struct vfs_handle_struct *handle,
2163 : struct files_struct *fsp,
2164 : uint32_t mode,
2165 : off_t offset,
2166 : off_t len)
2167 : {
2168 0 : VFS_FIND(fallocate);
2169 0 : return handle->fns->fallocate_fn(handle, fsp, mode, offset, len);
2170 : }
2171 :
2172 0 : int smb_vfs_call_filesystem_sharemode(struct vfs_handle_struct *handle,
2173 : struct files_struct *fsp,
2174 : uint32_t share_mode,
2175 : uint32_t access_mask)
2176 : {
2177 0 : VFS_FIND(filesystem_sharemode);
2178 0 : return handle->fns->filesystem_sharemode_fn(handle,
2179 : fsp,
2180 : share_mode,
2181 : access_mask);
2182 : }
2183 :
2184 3586 : int smb_vfs_call_fcntl(struct vfs_handle_struct *handle,
2185 : struct files_struct *fsp, int cmd, ...)
2186 : {
2187 : int result;
2188 : va_list cmd_arg;
2189 :
2190 4670 : VFS_FIND(fcntl);
2191 :
2192 3586 : va_start(cmd_arg, cmd);
2193 3586 : result = handle->fns->fcntl_fn(handle, fsp, cmd, cmd_arg);
2194 3586 : va_end(cmd_arg);
2195 :
2196 3586 : return result;
2197 : }
2198 :
2199 0 : int smb_vfs_call_linux_setlease(struct vfs_handle_struct *handle,
2200 : struct files_struct *fsp, int leasetype)
2201 : {
2202 0 : VFS_FIND(linux_setlease);
2203 0 : return handle->fns->linux_setlease_fn(handle, fsp, leasetype);
2204 : }
2205 :
2206 0 : int smb_vfs_call_symlinkat(struct vfs_handle_struct *handle,
2207 : const struct smb_filename *link_target,
2208 : struct files_struct *dirfsp,
2209 : const struct smb_filename *new_smb_fname)
2210 : {
2211 0 : VFS_FIND(symlinkat);
2212 0 : return handle->fns->symlinkat_fn(handle,
2213 : link_target,
2214 : dirfsp,
2215 : new_smb_fname);
2216 : }
2217 :
2218 44250 : int smb_vfs_call_readlinkat(struct vfs_handle_struct *handle,
2219 : const struct files_struct *dirfsp,
2220 : const struct smb_filename *smb_fname,
2221 : char *buf,
2222 : size_t bufsiz)
2223 : {
2224 73710 : VFS_FIND(readlinkat);
2225 44250 : return handle->fns->readlinkat_fn(handle,
2226 : dirfsp,
2227 : smb_fname,
2228 : buf,
2229 : bufsiz);
2230 : }
2231 :
2232 12 : int smb_vfs_call_linkat(struct vfs_handle_struct *handle,
2233 : struct files_struct *srcfsp,
2234 : const struct smb_filename *old_smb_fname,
2235 : struct files_struct *dstfsp,
2236 : const struct smb_filename *new_smb_fname,
2237 : int flags)
2238 : {
2239 20 : VFS_FIND(linkat);
2240 12 : return handle->fns->linkat_fn(handle,
2241 : srcfsp,
2242 : old_smb_fname,
2243 : dstfsp,
2244 : new_smb_fname,
2245 : flags);
2246 : }
2247 :
2248 0 : int smb_vfs_call_mknodat(struct vfs_handle_struct *handle,
2249 : struct files_struct *dirfsp,
2250 : const struct smb_filename *smb_fname,
2251 : mode_t mode,
2252 : SMB_DEV_T dev)
2253 : {
2254 0 : VFS_FIND(mknodat);
2255 0 : return handle->fns->mknodat_fn(handle,
2256 : dirfsp,
2257 : smb_fname,
2258 : mode,
2259 : dev);
2260 : }
2261 :
2262 136904 : struct smb_filename *smb_vfs_call_realpath(struct vfs_handle_struct *handle,
2263 : TALLOC_CTX *ctx,
2264 : const struct smb_filename *smb_fname)
2265 : {
2266 221375 : VFS_FIND(realpath);
2267 136904 : return handle->fns->realpath_fn(handle, ctx, smb_fname);
2268 : }
2269 :
2270 0 : int smb_vfs_call_fchflags(struct vfs_handle_struct *handle,
2271 : struct files_struct *fsp,
2272 : unsigned int flags)
2273 : {
2274 0 : VFS_FIND(fchflags);
2275 0 : return handle->fns->fchflags_fn(handle, fsp, flags);
2276 : }
2277 :
2278 1299215 : struct file_id smb_vfs_call_file_id_create(struct vfs_handle_struct *handle,
2279 : const SMB_STRUCT_STAT *sbuf)
2280 : {
2281 1612878 : VFS_FIND(file_id_create);
2282 1299215 : return handle->fns->file_id_create_fn(handle, sbuf);
2283 : }
2284 :
2285 22247 : uint64_t smb_vfs_call_fs_file_id(struct vfs_handle_struct *handle,
2286 : const SMB_STRUCT_STAT *sbuf)
2287 : {
2288 36065 : VFS_FIND(fs_file_id);
2289 22247 : return handle->fns->fs_file_id_fn(handle, sbuf);
2290 : }
2291 :
2292 6898 : NTSTATUS smb_vfs_call_fstreaminfo(struct vfs_handle_struct *handle,
2293 : struct files_struct *fsp,
2294 : TALLOC_CTX *mem_ctx,
2295 : unsigned int *num_streams,
2296 : struct stream_struct **streams)
2297 : {
2298 7708 : VFS_FIND(fstreaminfo);
2299 6898 : return handle->fns->fstreaminfo_fn(handle, fsp, mem_ctx,
2300 : num_streams, streams);
2301 : }
2302 :
2303 6211 : NTSTATUS smb_vfs_call_get_real_filename_at(struct vfs_handle_struct *handle,
2304 : struct files_struct *dirfsp,
2305 : const char *name,
2306 : TALLOC_CTX *mem_ctx,
2307 : char **found_name)
2308 : {
2309 8935 : VFS_FIND(get_real_filename_at);
2310 6211 : return handle->fns->get_real_filename_at_fn(
2311 : handle, dirfsp, name, mem_ctx, found_name);
2312 : }
2313 :
2314 173660 : const char *smb_vfs_call_connectpath(struct vfs_handle_struct *handle,
2315 : const struct smb_filename *smb_fname)
2316 : {
2317 278003 : VFS_FIND(connectpath);
2318 173660 : return handle->fns->connectpath_fn(handle, smb_fname);
2319 : }
2320 :
2321 972 : bool smb_vfs_call_strict_lock_check(struct vfs_handle_struct *handle,
2322 : struct files_struct *fsp,
2323 : struct lock_struct *plock)
2324 : {
2325 1132 : VFS_FIND(strict_lock_check);
2326 972 : return handle->fns->strict_lock_check_fn(handle, fsp, plock);
2327 : }
2328 :
2329 65936 : NTSTATUS smb_vfs_call_translate_name(struct vfs_handle_struct *handle,
2330 : const char *name,
2331 : enum vfs_translate_direction direction,
2332 : TALLOC_CTX *mem_ctx,
2333 : char **mapped_name)
2334 : {
2335 103482 : VFS_FIND(translate_name);
2336 65936 : return handle->fns->translate_name_fn(handle, name, direction, mem_ctx,
2337 : mapped_name);
2338 : }
2339 :
2340 139066 : NTSTATUS smb_vfs_call_parent_pathname(struct vfs_handle_struct *handle,
2341 : TALLOC_CTX *mem_ctx,
2342 : const struct smb_filename *smb_fname_in,
2343 : struct smb_filename **parent_dir_out,
2344 : struct smb_filename **atname_out)
2345 : {
2346 208311 : VFS_FIND(parent_pathname);
2347 139066 : return handle->fns->parent_pathname_fn(handle,
2348 : mem_ctx,
2349 : smb_fname_in,
2350 : parent_dir_out,
2351 : atname_out);
2352 : }
2353 :
2354 144 : NTSTATUS smb_vfs_call_fsctl(struct vfs_handle_struct *handle,
2355 : struct files_struct *fsp,
2356 : TALLOC_CTX *ctx,
2357 : uint32_t function,
2358 : uint16_t req_flags,
2359 : const uint8_t *in_data,
2360 : uint32_t in_len,
2361 : uint8_t **out_data,
2362 : uint32_t max_out_len,
2363 : uint32_t *out_len)
2364 : {
2365 240 : VFS_FIND(fsctl);
2366 144 : return handle->fns->fsctl_fn(handle, fsp, ctx, function, req_flags,
2367 : in_data, in_len, out_data, max_out_len,
2368 : out_len);
2369 : }
2370 :
2371 62742 : NTSTATUS smb_vfs_call_fget_dos_attributes(struct vfs_handle_struct *handle,
2372 : struct files_struct *fsp,
2373 : uint32_t *dosmode)
2374 : {
2375 82446 : VFS_FIND(fget_dos_attributes);
2376 62742 : return handle->fns->fget_dos_attributes_fn(handle, fsp, dosmode);
2377 : }
2378 :
2379 2311 : NTSTATUS smb_vfs_call_fset_dos_attributes(struct vfs_handle_struct *handle,
2380 : struct files_struct *fsp,
2381 : uint32_t dosmode)
2382 : {
2383 3063 : VFS_FIND(fset_dos_attributes);
2384 2311 : return handle->fns->fset_dos_attributes_fn(handle, fsp, dosmode);
2385 : }
2386 :
2387 0 : struct tevent_req *smb_vfs_call_offload_read_send(TALLOC_CTX *mem_ctx,
2388 : struct tevent_context *ev,
2389 : struct vfs_handle_struct *handle,
2390 : struct files_struct *fsp,
2391 : uint32_t fsctl,
2392 : uint32_t ttl,
2393 : off_t offset,
2394 : size_t to_copy)
2395 : {
2396 0 : VFS_FIND(offload_read_send);
2397 0 : return handle->fns->offload_read_send_fn(mem_ctx, ev, handle,
2398 : fsp, fsctl,
2399 : ttl, offset, to_copy);
2400 : }
2401 :
2402 0 : NTSTATUS smb_vfs_call_offload_read_recv(struct tevent_req *req,
2403 : struct vfs_handle_struct *handle,
2404 : TALLOC_CTX *mem_ctx,
2405 : uint32_t *flags,
2406 : uint64_t *xferlen,
2407 : DATA_BLOB *token_blob)
2408 : {
2409 0 : VFS_FIND(offload_read_recv);
2410 0 : return handle->fns->offload_read_recv_fn(req, handle, mem_ctx, flags, xferlen, token_blob);
2411 : }
2412 :
2413 0 : struct tevent_req *smb_vfs_call_offload_write_send(struct vfs_handle_struct *handle,
2414 : TALLOC_CTX *mem_ctx,
2415 : struct tevent_context *ev,
2416 : uint32_t fsctl,
2417 : DATA_BLOB *token,
2418 : off_t transfer_offset,
2419 : struct files_struct *dest_fsp,
2420 : off_t dest_off,
2421 : off_t num)
2422 : {
2423 0 : VFS_FIND(offload_write_send);
2424 0 : return handle->fns->offload_write_send_fn(handle, mem_ctx, ev, fsctl,
2425 : token, transfer_offset,
2426 : dest_fsp, dest_off, num);
2427 : }
2428 :
2429 0 : NTSTATUS smb_vfs_call_offload_write_recv(struct vfs_handle_struct *handle,
2430 : struct tevent_req *req,
2431 : off_t *copied)
2432 : {
2433 0 : VFS_FIND(offload_write_recv);
2434 0 : return handle->fns->offload_write_recv_fn(handle, req, copied);
2435 : }
2436 :
2437 : struct smb_vfs_call_get_dos_attributes_state {
2438 : files_struct *dir_fsp;
2439 : NTSTATUS (*recv_fn)(struct tevent_req *req,
2440 : struct vfs_aio_state *aio_state,
2441 : uint32_t *dosmode);
2442 : struct vfs_aio_state aio_state;
2443 : uint32_t dos_attributes;
2444 : };
2445 :
2446 : static void smb_vfs_call_get_dos_attributes_done(struct tevent_req *subreq);
2447 :
2448 0 : struct tevent_req *smb_vfs_call_get_dos_attributes_send(
2449 : TALLOC_CTX *mem_ctx,
2450 : struct tevent_context *ev,
2451 : struct vfs_handle_struct *handle,
2452 : files_struct *dir_fsp,
2453 : struct smb_filename *smb_fname)
2454 : {
2455 0 : struct tevent_req *req = NULL;
2456 0 : struct smb_vfs_call_get_dos_attributes_state *state = NULL;
2457 0 : struct tevent_req *subreq = NULL;
2458 :
2459 0 : req = tevent_req_create(mem_ctx, &state,
2460 : struct smb_vfs_call_get_dos_attributes_state);
2461 0 : if (req == NULL) {
2462 0 : return NULL;
2463 : }
2464 :
2465 0 : VFS_FIND(get_dos_attributes_send);
2466 :
2467 0 : *state = (struct smb_vfs_call_get_dos_attributes_state) {
2468 : .dir_fsp = dir_fsp,
2469 0 : .recv_fn = handle->fns->get_dos_attributes_recv_fn,
2470 : };
2471 :
2472 0 : subreq = handle->fns->get_dos_attributes_send_fn(mem_ctx,
2473 : ev,
2474 : handle,
2475 : dir_fsp,
2476 : smb_fname);
2477 0 : if (tevent_req_nomem(subreq, req)) {
2478 0 : return tevent_req_post(req, ev);
2479 : }
2480 0 : tevent_req_defer_callback(req, ev);
2481 :
2482 0 : tevent_req_set_callback(subreq,
2483 : smb_vfs_call_get_dos_attributes_done,
2484 : req);
2485 :
2486 0 : return req;
2487 : }
2488 :
2489 0 : static void smb_vfs_call_get_dos_attributes_done(struct tevent_req *subreq)
2490 : {
2491 0 : struct tevent_req *req =
2492 0 : tevent_req_callback_data(subreq,
2493 : struct tevent_req);
2494 0 : struct smb_vfs_call_get_dos_attributes_state *state =
2495 0 : tevent_req_data(req,
2496 : struct smb_vfs_call_get_dos_attributes_state);
2497 : NTSTATUS status;
2498 : bool ok;
2499 :
2500 : /*
2501 : * Make sure we run as the user again
2502 : */
2503 0 : ok = change_to_user_and_service_by_fsp(state->dir_fsp);
2504 0 : SMB_ASSERT(ok);
2505 :
2506 0 : status = state->recv_fn(subreq,
2507 : &state->aio_state,
2508 : &state->dos_attributes);
2509 0 : TALLOC_FREE(subreq);
2510 0 : if (tevent_req_nterror(req, status)) {
2511 0 : return;
2512 : }
2513 :
2514 0 : tevent_req_done(req);
2515 : }
2516 :
2517 0 : NTSTATUS smb_vfs_call_get_dos_attributes_recv(
2518 : struct tevent_req *req,
2519 : struct vfs_aio_state *aio_state,
2520 : uint32_t *dos_attributes)
2521 : {
2522 0 : struct smb_vfs_call_get_dos_attributes_state *state =
2523 0 : tevent_req_data(req,
2524 : struct smb_vfs_call_get_dos_attributes_state);
2525 : NTSTATUS status;
2526 :
2527 0 : if (tevent_req_is_nterror(req, &status)) {
2528 0 : tevent_req_received(req);
2529 0 : return status;
2530 : }
2531 :
2532 0 : *aio_state = state->aio_state;
2533 0 : *dos_attributes = state->dos_attributes;
2534 0 : tevent_req_received(req);
2535 0 : return NT_STATUS_OK;
2536 : }
2537 :
2538 0 : NTSTATUS smb_vfs_call_fget_compression(vfs_handle_struct *handle,
2539 : TALLOC_CTX *mem_ctx,
2540 : struct files_struct *fsp,
2541 : uint16_t *_compression_fmt)
2542 : {
2543 0 : VFS_FIND(fget_compression);
2544 0 : return handle->fns->fget_compression_fn(handle, mem_ctx, fsp,
2545 : _compression_fmt);
2546 : }
2547 :
2548 0 : NTSTATUS smb_vfs_call_set_compression(vfs_handle_struct *handle,
2549 : TALLOC_CTX *mem_ctx,
2550 : struct files_struct *fsp,
2551 : uint16_t compression_fmt)
2552 : {
2553 0 : VFS_FIND(set_compression);
2554 0 : return handle->fns->set_compression_fn(handle, mem_ctx, fsp,
2555 : compression_fmt);
2556 : }
2557 :
2558 0 : NTSTATUS smb_vfs_call_snap_check_path(vfs_handle_struct *handle,
2559 : TALLOC_CTX *mem_ctx,
2560 : const char *service_path,
2561 : char **base_volume)
2562 : {
2563 0 : VFS_FIND(snap_check_path);
2564 0 : return handle->fns->snap_check_path_fn(handle, mem_ctx, service_path,
2565 : base_volume);
2566 : }
2567 :
2568 0 : NTSTATUS smb_vfs_call_snap_create(struct vfs_handle_struct *handle,
2569 : TALLOC_CTX *mem_ctx,
2570 : const char *base_volume,
2571 : time_t *tstamp,
2572 : bool rw,
2573 : char **base_path,
2574 : char **snap_path)
2575 : {
2576 0 : VFS_FIND(snap_create);
2577 0 : return handle->fns->snap_create_fn(handle, mem_ctx, base_volume, tstamp,
2578 : rw, base_path, snap_path);
2579 : }
2580 :
2581 0 : NTSTATUS smb_vfs_call_snap_delete(struct vfs_handle_struct *handle,
2582 : TALLOC_CTX *mem_ctx,
2583 : char *base_path,
2584 : char *snap_path)
2585 : {
2586 0 : VFS_FIND(snap_delete);
2587 0 : return handle->fns->snap_delete_fn(handle, mem_ctx, base_path,
2588 : snap_path);
2589 : }
2590 :
2591 17203 : NTSTATUS smb_vfs_call_fget_nt_acl(struct vfs_handle_struct *handle,
2592 : struct files_struct *fsp,
2593 : uint32_t security_info,
2594 : TALLOC_CTX *mem_ctx,
2595 : struct security_descriptor **ppdesc)
2596 : {
2597 21510 : VFS_FIND(fget_nt_acl);
2598 17203 : return handle->fns->fget_nt_acl_fn(handle, fsp, security_info,
2599 : mem_ctx, ppdesc);
2600 : }
2601 :
2602 5464 : NTSTATUS smb_vfs_call_fset_nt_acl(struct vfs_handle_struct *handle,
2603 : struct files_struct *fsp,
2604 : uint32_t security_info_sent,
2605 : const struct security_descriptor *psd)
2606 : {
2607 6688 : VFS_FIND(fset_nt_acl);
2608 5464 : return handle->fns->fset_nt_acl_fn(handle, fsp, security_info_sent,
2609 : psd);
2610 : }
2611 :
2612 0 : NTSTATUS smb_vfs_call_audit_file(struct vfs_handle_struct *handle,
2613 : struct smb_filename *file,
2614 : struct security_acl *sacl,
2615 : uint32_t access_requested,
2616 : uint32_t access_denied)
2617 : {
2618 0 : VFS_FIND(audit_file);
2619 0 : return handle->fns->audit_file_fn(handle,
2620 : file,
2621 : sacl,
2622 : access_requested,
2623 : access_denied);
2624 : }
2625 :
2626 26098 : SMB_ACL_T smb_vfs_call_sys_acl_get_fd(struct vfs_handle_struct *handle,
2627 : struct files_struct *fsp,
2628 : SMB_ACL_TYPE_T type,
2629 : TALLOC_CTX *mem_ctx)
2630 : {
2631 29611 : VFS_FIND(sys_acl_get_fd);
2632 26098 : return handle->fns->sys_acl_get_fd_fn(handle, fsp, type, mem_ctx);
2633 : }
2634 :
2635 7655 : int smb_vfs_call_sys_acl_blob_get_fd(struct vfs_handle_struct *handle,
2636 : struct files_struct *fsp,
2637 : TALLOC_CTX *mem_ctx,
2638 : char **blob_description,
2639 : DATA_BLOB *blob)
2640 : {
2641 7655 : VFS_FIND(sys_acl_blob_get_fd);
2642 7655 : return handle->fns->sys_acl_blob_get_fd_fn(handle, fsp, mem_ctx, blob_description, blob);
2643 : }
2644 :
2645 3436 : int smb_vfs_call_sys_acl_set_fd(struct vfs_handle_struct *handle,
2646 : struct files_struct *fsp,
2647 : SMB_ACL_TYPE_T type,
2648 : SMB_ACL_T theacl)
2649 : {
2650 3661 : VFS_FIND(sys_acl_set_fd);
2651 3436 : return handle->fns->sys_acl_set_fd_fn(handle, fsp, type, theacl);
2652 : }
2653 :
2654 5 : int smb_vfs_call_sys_acl_delete_def_fd(struct vfs_handle_struct *handle,
2655 : struct files_struct *fsp)
2656 : {
2657 7 : VFS_FIND(sys_acl_delete_def_fd);
2658 5 : return handle->fns->sys_acl_delete_def_fd_fn(handle, fsp);
2659 : }
2660 :
2661 : struct smb_vfs_call_getxattrat_state {
2662 : files_struct *dir_fsp;
2663 : ssize_t (*recv_fn)(struct tevent_req *req,
2664 : struct vfs_aio_state *aio_state,
2665 : TALLOC_CTX *mem_ctx,
2666 : uint8_t **xattr_value);
2667 : ssize_t retval;
2668 : uint8_t *xattr_value;
2669 : struct vfs_aio_state aio_state;
2670 : };
2671 :
2672 : static void smb_vfs_call_getxattrat_done(struct tevent_req *subreq);
2673 :
2674 0 : struct tevent_req *smb_vfs_call_getxattrat_send(
2675 : TALLOC_CTX *mem_ctx,
2676 : struct tevent_context *ev,
2677 : struct vfs_handle_struct *handle,
2678 : files_struct *dir_fsp,
2679 : const struct smb_filename *smb_fname,
2680 : const char *xattr_name,
2681 : size_t alloc_hint)
2682 : {
2683 0 : struct tevent_req *req = NULL;
2684 0 : struct smb_vfs_call_getxattrat_state *state = NULL;
2685 0 : struct tevent_req *subreq = NULL;
2686 :
2687 0 : req = tevent_req_create(mem_ctx, &state,
2688 : struct smb_vfs_call_getxattrat_state);
2689 0 : if (req == NULL) {
2690 0 : return NULL;
2691 : }
2692 :
2693 0 : VFS_FIND(getxattrat_send);
2694 :
2695 0 : *state = (struct smb_vfs_call_getxattrat_state) {
2696 : .dir_fsp = dir_fsp,
2697 0 : .recv_fn = handle->fns->getxattrat_recv_fn,
2698 : };
2699 :
2700 0 : subreq = handle->fns->getxattrat_send_fn(mem_ctx,
2701 : ev,
2702 : handle,
2703 : dir_fsp,
2704 : smb_fname,
2705 : xattr_name,
2706 : alloc_hint);
2707 0 : if (tevent_req_nomem(subreq, req)) {
2708 0 : return tevent_req_post(req, ev);
2709 : }
2710 0 : tevent_req_defer_callback(req, ev);
2711 :
2712 0 : tevent_req_set_callback(subreq, smb_vfs_call_getxattrat_done, req);
2713 0 : return req;
2714 : }
2715 :
2716 0 : static void smb_vfs_call_getxattrat_done(struct tevent_req *subreq)
2717 : {
2718 0 : struct tevent_req *req = tevent_req_callback_data(
2719 : subreq, struct tevent_req);
2720 0 : struct smb_vfs_call_getxattrat_state *state = tevent_req_data(
2721 : req, struct smb_vfs_call_getxattrat_state);
2722 : bool ok;
2723 :
2724 : /*
2725 : * Make sure we run as the user again
2726 : */
2727 0 : ok = change_to_user_and_service_by_fsp(state->dir_fsp);
2728 0 : SMB_ASSERT(ok);
2729 :
2730 0 : state->retval = state->recv_fn(subreq,
2731 : &state->aio_state,
2732 : state,
2733 : &state->xattr_value);
2734 0 : TALLOC_FREE(subreq);
2735 0 : if (state->retval == -1) {
2736 0 : tevent_req_error(req, state->aio_state.error);
2737 0 : return;
2738 : }
2739 :
2740 0 : tevent_req_done(req);
2741 : }
2742 :
2743 0 : ssize_t smb_vfs_call_getxattrat_recv(struct tevent_req *req,
2744 : struct vfs_aio_state *aio_state,
2745 : TALLOC_CTX *mem_ctx,
2746 : uint8_t **xattr_value)
2747 : {
2748 0 : struct smb_vfs_call_getxattrat_state *state = tevent_req_data(
2749 : req, struct smb_vfs_call_getxattrat_state);
2750 : size_t xattr_size;
2751 :
2752 0 : if (tevent_req_is_unix_error(req, &aio_state->error)) {
2753 0 : tevent_req_received(req);
2754 0 : return -1;
2755 : }
2756 :
2757 0 : *aio_state = state->aio_state;
2758 0 : xattr_size = state->retval;
2759 0 : if (xattr_value != NULL) {
2760 0 : *xattr_value = talloc_move(mem_ctx, &state->xattr_value);
2761 : }
2762 :
2763 0 : tevent_req_received(req);
2764 0 : return xattr_size;
2765 : }
2766 :
2767 548200 : ssize_t smb_vfs_call_fgetxattr(struct vfs_handle_struct *handle,
2768 : struct files_struct *fsp, const char *name,
2769 : void *value, size_t size)
2770 : {
2771 555401 : VFS_FIND(fgetxattr);
2772 548200 : return handle->fns->fgetxattr_fn(handle, fsp, name, value, size);
2773 : }
2774 :
2775 20720 : ssize_t smb_vfs_call_flistxattr(struct vfs_handle_struct *handle,
2776 : struct files_struct *fsp, char *list,
2777 : size_t size)
2778 : {
2779 24113 : VFS_FIND(flistxattr);
2780 20720 : return handle->fns->flistxattr_fn(handle, fsp, list, size);
2781 : }
2782 :
2783 71 : int smb_vfs_call_fremovexattr(struct vfs_handle_struct *handle,
2784 : struct files_struct *fsp, const char *name)
2785 : {
2786 89 : VFS_FIND(fremovexattr);
2787 71 : return handle->fns->fremovexattr_fn(handle, fsp, name);
2788 : }
2789 :
2790 14230 : int smb_vfs_call_fsetxattr(struct vfs_handle_struct *handle,
2791 : struct files_struct *fsp, const char *name,
2792 : const void *value, size_t size, int flags)
2793 : {
2794 14956 : VFS_FIND(fsetxattr);
2795 14230 : return handle->fns->fsetxattr_fn(handle, fsp, name, value, size, flags);
2796 : }
2797 :
2798 0 : bool smb_vfs_call_aio_force(struct vfs_handle_struct *handle,
2799 : struct files_struct *fsp)
2800 : {
2801 0 : VFS_FIND(aio_force);
2802 0 : return handle->fns->aio_force_fn(handle, fsp);
2803 : }
2804 :
2805 0 : NTSTATUS smb_vfs_call_durable_cookie(struct vfs_handle_struct *handle,
2806 : struct files_struct *fsp,
2807 : TALLOC_CTX *mem_ctx,
2808 : DATA_BLOB *cookie)
2809 : {
2810 0 : VFS_FIND(durable_cookie);
2811 0 : return handle->fns->durable_cookie_fn(handle, fsp, mem_ctx, cookie);
2812 : }
2813 :
2814 0 : NTSTATUS smb_vfs_call_durable_disconnect(struct vfs_handle_struct *handle,
2815 : struct files_struct *fsp,
2816 : const DATA_BLOB old_cookie,
2817 : TALLOC_CTX *mem_ctx,
2818 : DATA_BLOB *new_cookie)
2819 : {
2820 0 : VFS_FIND(durable_disconnect);
2821 0 : return handle->fns->durable_disconnect_fn(handle, fsp, old_cookie,
2822 : mem_ctx, new_cookie);
2823 : }
2824 :
2825 0 : NTSTATUS smb_vfs_call_durable_reconnect(struct vfs_handle_struct *handle,
2826 : struct smb_request *smb1req,
2827 : struct smbXsrv_open *op,
2828 : const DATA_BLOB old_cookie,
2829 : TALLOC_CTX *mem_ctx,
2830 : struct files_struct **fsp,
2831 : DATA_BLOB *new_cookie)
2832 : {
2833 0 : VFS_FIND(durable_reconnect);
2834 0 : return handle->fns->durable_reconnect_fn(handle, smb1req, op,
2835 : old_cookie, mem_ctx, fsp,
2836 : new_cookie);
2837 : }
2838 :
2839 19343 : NTSTATUS smb_vfs_call_freaddir_attr(struct vfs_handle_struct *handle,
2840 : struct files_struct *fsp,
2841 : TALLOC_CTX *mem_ctx,
2842 : struct readdir_attr_data **attr_data)
2843 : {
2844 31259 : VFS_FIND(freaddir_attr);
2845 19343 : return handle->fns->freaddir_attr_fn(handle,
2846 : fsp,
2847 : mem_ctx,
2848 : attr_data);
2849 : }
|