s4:smbtorture: Fix samba3.smb.dir on btrfs
[samba4-gss.git] / source3 / modules / vfs_unityed_media.c
blobcf1447e8325f124e1aa35e0e4ba95a50eaf9a189
1 /*
2 * Samba VFS module supporting multiple AVID clients sharing media.
4 * Copyright (C) 2005 Philip de Nier <philipn@users.sourceforge.net>
5 * Copyright (C) 2012 Andrew Klaassen <clawsoon@yahoo.com>
6 * Copyright (C) 2013 Milos Lukacek
7 * Copyright (C) 2013 Ralph Boehme <slow@samba.org>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
26 * Unityed Media is a Samba VFS module that allows multiple AVID
27 * clients to share media.
29 * Add this module to the vfs objects option in your Samba share
30 * configuration.
31 * eg.
33 * [avid_win]
34 * path = /video
35 * vfs objects = unityed_media
36 * ...
38 * It is recommended that you separate out Samba shares for Mac
39 * and Windows clients, and add the following options to the shares
40 * for Windows clients (NOTE: replace @ with *):
42 * veto files = /.DS_Store/._@/.Trash@/.Spotlight@/.hidden/.hotfiles@/.vol/
43 * delete veto files = yes
45 * This prevents hidden files from Mac clients interfering with Windows
46 * clients. If you find any more problem hidden files then add them to
47 * the list.
49 * Notes:
50 * This module is designed to work with AVID editing applications that
51 * look in the Avid MediaFiles or OMFI MediaFiles directory for media.
52 * It is not designed to work as expected in all circumstances for
53 * general use.
57 #include "includes.h"
58 #include "system/filesys.h"
59 #include "smbd/smbd.h"
60 #include "../smbd/globals.h"
61 #include "auth.h"
62 #include "../lib/tsocket/tsocket.h"
63 #include "lib/util/smb_strtox.h"
64 #include <libgen.h>
65 #include "source3/lib/substitute.h"
67 #define UM_PARAM_TYPE_NAME "unityed_media"
69 static const char *AVID_MXF_DIRNAME = "Avid MediaFiles/MXF";
70 static const size_t AVID_MXF_DIRNAME_LEN = 19;
71 static const char *OMFI_MEDIAFILES_DIRNAME = "OMFI MediaFiles";
72 static const size_t OMFI_MEDIAFILES_DIRNAME_LEN = 15;
73 static const char *APPLE_DOUBLE_PREFIX = "._";
74 static const size_t APPLE_DOUBLE_PREFIX_LEN = 2;
75 static int vfs_um_debug_level = DBGC_VFS;
77 enum um_clientid {UM_CLIENTID_NAME, UM_CLIENTID_IP, UM_CLIENTID_HOSTNAME};
79 struct um_config_data {
80 enum um_clientid clientid;
83 static const struct enum_list um_clientid[] = {
84 {UM_CLIENTID_NAME, "user"},
85 {UM_CLIENTID_IP, "ip"},
86 {UM_CLIENTID_HOSTNAME, "hostname"},
87 {-1, NULL}
90 /* supplements the directory list stream */
91 typedef struct um_dirinfo_struct {
92 DIR* dirstream;
93 char *dirpath;
94 char *clientPath;
95 bool isInMediaFiles;
96 char *clientSubDirname;
97 } um_dirinfo_struct;
99 /**
100 * Returns true and first group of digits in path, false and 0 otherwise
102 static bool get_digit_group(const char *path, uintmax_t *digit)
104 const char *p = path;
105 codepoint_t cp;
106 size_t size;
107 int error = 0;
109 DEBUG(10, ("get_digit_group entering with path '%s'\n",
110 path));
113 * Delibiretly initialize to 0 because callers use this result
114 * even though the string doesn't contain any number and we
115 * returned false
117 *digit = 0;
119 while (*p) {
120 cp = next_codepoint(p, &size);
121 if (cp == -1) {
122 return false;
124 if ((size == 1) && (isdigit(cp))) {
125 *digit = (uintmax_t)smb_strtoul(p,
126 NULL,
128 &error,
129 SMB_STR_STANDARD);
130 if (error != 0) {
131 return false;
133 DEBUG(10, ("num_suffix = '%ju'\n",
134 *digit));
135 return true;
137 p += size;
140 return false;
143 /* Add "_<remote_name>.<number>" suffix to path or filename.
145 * Success: return 0
146 * Failure: set errno, path NULL, return -1
149 static int alloc_append_client_suffix(vfs_handle_struct *handle,
150 char **path)
152 int status = 0;
153 uintmax_t number;
154 const char *clientid;
155 struct um_config_data *config;
157 DEBUG(10, ("Entering with path '%s'\n", *path));
159 SMB_VFS_HANDLE_GET_DATA(handle, config,
160 struct um_config_data,
161 return -1);
163 (void)get_digit_group(*path, &number);
165 switch (config->clientid) {
167 case UM_CLIENTID_IP:
168 clientid = tsocket_address_inet_addr_string(
169 handle->conn->sconn->remote_address, talloc_tos());
170 if (clientid == NULL) {
171 errno = ENOMEM;
172 status = -1;
173 goto err;
175 break;
177 case UM_CLIENTID_HOSTNAME:
178 clientid = get_remote_machine_name();
179 break;
181 case UM_CLIENTID_NAME:
182 default:
183 clientid = get_current_username();
184 break;
187 *path = talloc_asprintf_append(*path, "_%s.%ju",
188 clientid, number);
189 if (*path == NULL) {
190 DEBUG(1, ("alloc_append_client_suffix "
191 "out of memory\n"));
192 errno = ENOMEM;
193 status = -1;
194 goto err;
196 DEBUG(10, ("Leaving with *path '%s'\n", *path));
197 err:
198 return status;
201 /* Returns true if the file or directory begins with the appledouble
202 * prefix.
204 static bool is_apple_double(const char* fname)
206 bool ret = false;
208 DEBUG(10, ("Entering with fname '%s'\n", fname));
210 if (strnequal(APPLE_DOUBLE_PREFIX, fname, APPLE_DOUBLE_PREFIX_LEN)) {
211 ret = true;
213 DEBUG(10, ("Leaving with ret '%s'\n",
214 ret == true ? "true" : "false"));
215 return ret;
218 static bool starts_with_media_dir(const char* media_dirname,
219 size_t media_dirname_len,
220 const char *path)
222 bool ret = false;
223 const char *path_start = path;
225 DEBUG(10, ("Entering with media_dirname '%s' "
226 "path '%s'\n", media_dirname, path));
228 /* Sometimes Samba gives us "./OMFI MediaFiles". */
229 if (strnequal(path, "./", 2)) {
230 path_start += 2;
233 if (strnequal(media_dirname, path_start, media_dirname_len)
235 ((path_start[media_dirname_len] == '\0') ||
236 (path_start[media_dirname_len] == '/'))) {
237 ret = true;
240 DEBUG(10, ("Leaving with ret '%s'\n",
241 ret == true ? "true" : "false"));
242 return ret;
246 * Returns true if the file or directory referenced by the path is ONE
247 * LEVEL below the AVID_MXF_DIRNAME or OMFI_MEDIAFILES_DIRNAME
248 * directory
250 static bool is_in_media_dir(const char *path)
252 int transition_count = 0;
253 const char *path_start = path;
254 const char *p;
255 const char *media_dirname;
256 size_t media_dirname_len;
258 DEBUG(10, ("Entering with path '%s'\n", path));
260 /* Sometimes Samba gives us "./OMFI MediaFiles". */
261 if (strnequal(path, "./", 2)) {
262 path_start += 2;
265 if (strnequal(path_start, AVID_MXF_DIRNAME, AVID_MXF_DIRNAME_LEN)) {
266 media_dirname = AVID_MXF_DIRNAME;
267 media_dirname_len = AVID_MXF_DIRNAME_LEN;
268 } else if (strnequal(path_start,
269 OMFI_MEDIAFILES_DIRNAME,
270 OMFI_MEDIAFILES_DIRNAME_LEN)) {
271 media_dirname = OMFI_MEDIAFILES_DIRNAME;
272 media_dirname_len = OMFI_MEDIAFILES_DIRNAME_LEN;
273 } else {
274 return false;
277 if (path_start[media_dirname_len] == '\0') {
278 goto out;
281 p = path_start + media_dirname_len + 1;
283 while (true) {
284 if (*p == '\0' || *p == '/') {
285 if (strnequal(p - 3, "/..", 3)) {
286 transition_count--;
287 } else if ((p[-1] != '/') || !strnequal(p - 2, "/.", 2)) {
288 transition_count++;
291 if (*p == '\0') {
292 break;
294 p++;
297 out:
298 DEBUG(10, ("Going out with transition_count '%i'\n",
299 transition_count));
300 if (((transition_count == 1) && (media_dirname == AVID_MXF_DIRNAME))
302 ((transition_count == 0) && (media_dirname == OMFI_MEDIAFILES_DIRNAME))) {
303 return true;
305 else return false;
309 * Returns true if the file or directory referenced by the path is
310 * below the AVID_MEDIAFILES_DIRNAME or OMFI_MEDIAFILES_DIRNAME
311 * directory The AVID_MEDIAFILES_DIRNAME and OMFI_MEDIAFILES_DIRNAME
312 * are assumed to be in the root directory, which is generally a safe
313 * assumption in the fixed-path world of Avid.
315 static bool is_in_media_files(const char *path)
317 bool ret = false;
319 DEBUG(10, ("Entering with path '%s'\n", path));
321 if (starts_with_media_dir(AVID_MXF_DIRNAME,
322 AVID_MXF_DIRNAME_LEN, path) ||
323 starts_with_media_dir(OMFI_MEDIAFILES_DIRNAME,
324 OMFI_MEDIAFILES_DIRNAME_LEN, path)) {
325 ret = true;
327 DEBUG(10, ("Leaving with ret '%s'\n",
328 ret == true ? "true" : "false"));
329 return ret;
333 /* Add client suffix to "pure-number" path.
335 * Caller must free newPath.
337 * Success: return 0
338 * Failure: set errno, newPath NULL, return -1
340 static int alloc_get_client_path(vfs_handle_struct *handle,
341 TALLOC_CTX *ctx,
342 const char *path_in,
343 char **path_out)
345 int status = 0;
346 char *p;
347 char *digits;
348 size_t digits_len;
349 uintmax_t number;
351 *path_out = talloc_strdup(ctx, path_in);
352 if (*path_out == NULL) {
353 DEBUG(1, ("alloc_get_client_path ENOMEM\n"));
354 return -1;
357 (void)get_digit_group(*path_out, &number);
359 digits = talloc_asprintf(NULL, "%ju", number);
360 if (digits == NULL) {
361 DEBUG(1, ("alloc_get_client_path ENOMEM\n"));
362 return -1;
364 digits_len = strlen(digits);
366 p = strstr_m(path_in, digits);
367 if ((p)
369 ((p[digits_len] == '\0') || (p[digits_len] == '/'))
371 (((p - path_in > 0) && (p[-1] == '/'))
373 (((p - path_in) > APPLE_DOUBLE_PREFIX_LEN)
375 is_apple_double(p - APPLE_DOUBLE_PREFIX_LEN)
377 (p[-(APPLE_DOUBLE_PREFIX_LEN + 1)] == '/'))))
379 (*path_out)[p - path_in + digits_len] = '\0';
381 status = alloc_append_client_suffix(handle, path_out);
382 if (status != 0) {
383 goto out;
386 *path_out = talloc_strdup_append(*path_out, p + digits_len);
387 if (*path_out == NULL) {
388 DEBUG(1, ("alloc_get_client_path ENOMEM\n"));
389 status = -1;
390 goto out;
393 out:
394 /* path_out must be freed in caller. */
395 DEBUG(10, ("Result:'%s'\n", *path_out));
396 return status;
400 * Success: return 0
401 * Failure: set errno, return -1
403 static int alloc_get_client_smb_fname(struct vfs_handle_struct *handle,
404 TALLOC_CTX *ctx,
405 const struct smb_filename *smb_fname,
406 struct smb_filename **client_fname)
408 int status ;
410 DEBUG(10, ("Entering with smb_fname->base_name '%s'\n",
411 smb_fname->base_name));
413 *client_fname = cp_smb_filename(ctx, smb_fname);
414 if (*client_fname == NULL) {
415 DEBUG(1, ("cp_smb_filename returned NULL\n"));
416 return -1;
418 status = alloc_get_client_path(handle, ctx,
419 smb_fname->base_name,
420 &(*client_fname)->base_name);
421 if (status != 0) {
422 return -1;
425 DEBUG(10, ("Leaving with (*client_fname)->base_name "
426 "'%s'\n", (*client_fname)->base_name));
428 return 0;
433 * Success: return 0
434 * Failure: set errno, return -1
436 static int alloc_set_client_dirinfo_path(struct vfs_handle_struct *handle,
437 TALLOC_CTX *ctx,
438 char **path,
439 const char *suffix_number)
441 int status;
443 DEBUG(10, ("Entering with suffix_number '%s'\n",
444 suffix_number));
446 *path = talloc_strdup(ctx, suffix_number);
447 if (*path == NULL) {
448 DEBUG(1, ("alloc_set_client_dirinfo_path ENOMEM\n"));
449 return -1;
451 status = alloc_append_client_suffix(handle, path);
452 if (status != 0) {
453 return -1;
456 DEBUG(10, ("Leaving with *path '%s'\n", *path));
458 return 0;
461 static int alloc_set_client_dirinfo(vfs_handle_struct *handle,
462 const char *fname,
463 struct um_dirinfo_struct **di_result)
465 int status = 0;
466 char *digits;
467 uintmax_t number;
468 struct um_dirinfo_struct *dip;
470 DEBUG(10, ("Entering with fname '%s'\n", fname));
472 *di_result = talloc(NULL, struct um_dirinfo_struct);
473 if (*di_result == NULL) {
474 goto err;
476 dip = *di_result;
478 dip->dirpath = talloc_strdup(dip, fname);
479 if (dip->dirpath == NULL) {
480 goto err;
483 if (!is_in_media_files(fname)) {
484 dip->isInMediaFiles = false;
485 dip->clientPath = NULL;
486 dip->clientSubDirname = NULL;
487 goto out;
490 dip->isInMediaFiles = true;
492 (void)get_digit_group(fname, &number);
493 digits = talloc_asprintf(talloc_tos(), "%ju", number);
494 if (digits == NULL) {
495 goto err;
498 status = alloc_set_client_dirinfo_path(handle, dip,
499 &dip->clientSubDirname,
500 digits);
501 if (status != 0) {
502 goto err;
505 status = alloc_get_client_path(handle, dip, fname,
506 &dip->clientPath);
507 if (status != 0 || dip->clientPath == NULL) {
508 goto err;
511 out:
512 DEBUG(10, ("Leaving with (*dirInfo)->dirpath '%s', "
513 "(*dirInfo)->clientPath '%s'\n",
514 dip->dirpath, dip->clientPath));
515 return status;
517 err:
518 DEBUG(1, ("Failing with fname '%s'\n", fname));
519 TALLOC_FREE(*di_result);
520 status = -1;
521 errno = ENOMEM;
522 return status;
525 /**********************************************************************
526 * VFS functions
527 **********************************************************************/
530 * Success: return 0
531 * Failure: set errno, return -1
533 static int um_statvfs(struct vfs_handle_struct *handle,
534 const struct smb_filename *smb_fname,
535 struct vfs_statvfs_struct *statbuf)
537 int status;
538 struct smb_filename *client_fname = NULL;
540 DEBUG(10, ("Entering with path '%s'\n", smb_fname->base_name));
542 if (!is_in_media_files(smb_fname->base_name)) {
543 return SMB_VFS_NEXT_STATVFS(handle, smb_fname, statbuf);
546 status = alloc_get_client_smb_fname(handle,
547 talloc_tos(),
548 smb_fname,
549 &client_fname);
550 if (status != 0) {
551 goto err;
554 status = SMB_VFS_NEXT_STATVFS(handle, client_fname, statbuf);
555 err:
556 TALLOC_FREE(client_fname);
557 DEBUG(10, ("Leaving with path '%s'\n", smb_fname->base_name));
558 return status;
561 static DIR *um_fdopendir(vfs_handle_struct *handle,
562 files_struct *fsp,
563 const char *mask,
564 uint32_t attr)
566 struct um_dirinfo_struct *dirInfo = NULL;
567 DIR *dirstream;
569 DEBUG(10, ("Entering with fsp->fsp_name->base_name '%s'\n",
570 fsp->fsp_name->base_name));
572 dirstream = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
573 if (!dirstream) {
574 goto err;
577 if (alloc_set_client_dirinfo(handle,
578 fsp->fsp_name->base_name,
579 &dirInfo)) {
580 goto err;
583 dirInfo->dirstream = dirstream;
585 if (!dirInfo->isInMediaFiles) {
587 * FIXME: this is the original code, something must be
588 * missing here, but what? -slow
590 goto out;
593 out:
594 DEBUG(10, ("Leaving with dirInfo->dirpath '%s', "
595 "dirInfo->clientPath '%s', "
596 "fsp->fsp_name->st.st_ex_mtime %s",
597 dirInfo->dirpath,
598 dirInfo->clientPath,
599 ctime(&(fsp->fsp_name->st.st_ex_mtime.tv_sec))));
600 return (DIR *) dirInfo;
602 err:
603 DEBUG(1, ("Failing with fsp->fsp_name->base_name '%s'\n",
604 fsp->fsp_name->base_name));
605 TALLOC_FREE(dirInfo);
606 return NULL;
610 * skip own suffixed directory
611 * replace own suffixed directory with non suffixed.
613 * Success: return dirent
614 * End of data: return NULL
615 * Failure: set errno, return NULL
617 static struct dirent *
618 um_readdir(vfs_handle_struct *handle, struct files_struct *dirfsp, DIR *dirp)
620 um_dirinfo_struct* dirInfo = (um_dirinfo_struct*)dirp;
621 struct dirent *d = NULL;
622 int skip;
624 DEBUG(10, ("dirInfo->dirpath '%s', "
625 "dirInfo->clientPath '%s', "
626 "dirInfo->isInMediaFiles '%s', "
627 "dirInfo->clientSubDirname '%s'\n",
628 dirInfo->dirpath,
629 dirInfo->clientPath,
630 dirInfo->isInMediaFiles ? "true" : "false",
631 dirInfo->clientSubDirname));
633 if (!dirInfo->isInMediaFiles) {
634 return SMB_VFS_NEXT_READDIR(handle, dirfsp, dirInfo->dirstream);
637 do {
638 const char* dname;
639 bool isAppleDouble;
640 char *digits;
641 size_t digits_len;
642 uintmax_t number;
644 skip = false;
645 d = SMB_VFS_NEXT_READDIR(handle, dirfsp, dirInfo->dirstream);
647 if (d == NULL) {
648 break;
651 /* ignore apple double prefix for logic below */
652 if (is_apple_double(d->d_name)) {
653 dname = &d->d_name[APPLE_DOUBLE_PREFIX_LEN];
654 isAppleDouble = true;
655 } else {
656 dname = d->d_name;
657 isAppleDouble = false;
660 DEBUG(10, ("dname = '%s'\n", dname));
662 (void)get_digit_group(dname, &number);
663 digits = talloc_asprintf(talloc_tos(), "%ju", number);
664 if (digits == NULL) {
665 DEBUG(1, ("out of memory\n"));
666 goto err;
668 digits_len = strlen(digits);
670 if (alloc_set_client_dirinfo_path(handle,
671 dirInfo,
672 &((dirInfo)->clientSubDirname),
673 digits)) {
674 goto err;
678 * If set to "true", vfs shows digits-only
679 * non-suffixed subdirectories. Normally, such
680 * subdirectories can exists only in non-media
681 * directories, so we set it to "false". Otherwise,
682 * if we have such subdirectories (probably created
683 * over not "unityed" connection), it can be little
684 * bit confusing.
686 if (strequal(dname, digits)) {
687 skip = false;
688 } else if (strequal(dname, dirInfo->clientSubDirname)) {
690 * Remove suffix of this client's suffixed
691 * subdirectories
693 if (isAppleDouble) {
694 d->d_name[digits_len + APPLE_DOUBLE_PREFIX_LEN] = '\0';
695 } else {
696 d->d_name[digits_len] = '\0';
698 } else if (strnequal(digits, dname, digits_len)) {
700 * Set to false to see another clients subdirectories
702 skip = false;
704 } while (skip);
706 DEBUG(10, ("Leaving um_readdir\n"));
707 return d;
708 err:
709 TALLOC_FREE(dirInfo);
710 return NULL;
713 static void um_rewinddir(vfs_handle_struct *handle,
714 DIR *dirp)
716 DEBUG(10, ("Entering and leaving um_rewinddir\n"));
717 SMB_VFS_NEXT_REWINDDIR(handle,
718 ((um_dirinfo_struct*)dirp)->dirstream);
721 static int um_mkdirat(vfs_handle_struct *handle,
722 struct files_struct *dirfsp,
723 const struct smb_filename *smb_fname,
724 mode_t mode)
726 int status;
727 const char *path = NULL;
728 struct smb_filename *client_fname = NULL;
729 struct smb_filename *full_fname = NULL;
731 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
732 dirfsp,
733 smb_fname);
734 if (full_fname == NULL) {
735 return -1;
738 path = full_fname->base_name;
739 DEBUG(10, ("Entering with path '%s'\n", path));
741 if (!is_in_media_files(path) || !is_in_media_dir(path)) {
742 TALLOC_FREE(full_fname);
743 return SMB_VFS_NEXT_MKDIRAT(handle,
744 dirfsp,
745 smb_fname,
746 mode);
749 status = alloc_get_client_smb_fname(handle,
750 talloc_tos(),
751 full_fname,
752 &client_fname);
753 if (status != 0) {
754 goto err;
757 status = SMB_VFS_NEXT_MKDIRAT(handle,
758 handle->conn->cwd_fsp,
759 client_fname,
760 mode);
761 err:
762 DEBUG(10, ("Leaving with path '%s'\n", path));
763 TALLOC_FREE(client_fname);
764 TALLOC_FREE(full_fname);
765 return status;
768 static int um_closedir(vfs_handle_struct *handle,
769 DIR *dirp)
771 DIR *realdirp = ((um_dirinfo_struct*)dirp)->dirstream;
773 TALLOC_FREE(dirp);
775 return SMB_VFS_NEXT_CLOSEDIR(handle, realdirp);
778 static int um_openat(struct vfs_handle_struct *handle,
779 const struct files_struct *dirfsp,
780 const struct smb_filename *smb_fname,
781 struct files_struct *fsp,
782 const struct vfs_open_how *how)
784 struct smb_filename *client_fname = NULL;
785 int ret;
787 DBG_DEBUG("Entering with smb_fname->base_name '%s'\n",
788 smb_fname->base_name);
790 if (!is_in_media_files(smb_fname->base_name)) {
791 return SMB_VFS_NEXT_OPENAT(handle,
792 dirfsp,
793 smb_fname,
794 fsp,
795 how);
798 if (alloc_get_client_smb_fname(handle, talloc_tos(),
799 smb_fname,
800 &client_fname)) {
801 ret = -1;
802 goto err;
806 * FIXME:
807 * What about fsp->fsp_name? We also have to get correct stat
808 * info into fsp and smb_fname for DB files, don't we?
811 DEBUG(10, ("Leaving with smb_fname->base_name '%s' "
812 "smb_fname->st.st_ex_mtime %s"
813 "fsp->fsp_name->st.st_ex_mtime %s",
814 smb_fname->base_name,
815 ctime(&(smb_fname->st.st_ex_mtime.tv_sec)),
816 ctime(&(fsp->fsp_name->st.st_ex_mtime.tv_sec))));
818 ret = SMB_VFS_NEXT_OPENAT(handle,
819 dirfsp,
820 client_fname,
821 fsp,
822 how);
823 err:
824 TALLOC_FREE(client_fname);
825 DEBUG(10, ("Leaving with smb_fname->base_name '%s'\n",
826 smb_fname->base_name));
827 return ret;
830 static NTSTATUS um_create_file(vfs_handle_struct *handle,
831 struct smb_request *req,
832 struct files_struct *dirfsp,
833 struct smb_filename *smb_fname,
834 uint32_t access_mask,
835 uint32_t share_access,
836 uint32_t create_disposition,
837 uint32_t create_options,
838 uint32_t file_attributes,
839 uint32_t oplock_request,
840 const struct smb2_lease *lease,
841 uint64_t allocation_size,
842 uint32_t private_flags,
843 struct security_descriptor *sd,
844 struct ea_list *ea_list,
845 files_struct **result_fsp,
846 int *pinfo,
847 const struct smb2_create_blobs *in_context_blobs,
848 struct smb2_create_blobs *out_context_blobs)
850 NTSTATUS status;
851 struct smb_filename *client_fname = NULL;
853 DEBUG(10, ("Entering with smb_fname->base_name '%s'\n",
854 smb_fname->base_name));
856 if (!is_in_media_files(smb_fname->base_name)) {
857 return SMB_VFS_NEXT_CREATE_FILE(
858 handle,
859 req,
860 dirfsp,
861 smb_fname,
862 access_mask,
863 share_access,
864 create_disposition,
865 create_options,
866 file_attributes,
867 oplock_request,
868 lease,
869 allocation_size,
870 private_flags,
872 ea_list,
873 result_fsp,
874 pinfo,
875 in_context_blobs,
876 out_context_blobs);
879 if (alloc_get_client_smb_fname(handle, talloc_tos(),
880 smb_fname,
881 &client_fname)) {
882 status = map_nt_error_from_unix(errno);
883 goto err;
887 * FIXME:
888 * This only creates files, so we don't have to worry about
889 * our fake directory stat'ing here. But we still need to
890 * route stat calls for DB files properly, right?
892 status = SMB_VFS_NEXT_CREATE_FILE(
893 handle,
894 req,
895 dirfsp,
896 client_fname,
897 access_mask,
898 share_access,
899 create_disposition,
900 create_options,
901 file_attributes,
902 oplock_request,
903 lease,
904 allocation_size,
905 private_flags,
907 ea_list,
908 result_fsp,
909 pinfo,
910 in_context_blobs,
911 out_context_blobs);
912 err:
913 TALLOC_FREE(client_fname);
914 DEBUG(10, ("Leaving with smb_fname->base_name '%s'"
915 "smb_fname->st.st_ex_mtime %s"
916 " fsp->fsp_name->st.st_ex_mtime %s",
917 smb_fname->base_name,
918 ctime(&(smb_fname->st.st_ex_mtime.tv_sec)),
919 (*result_fsp) && VALID_STAT((*result_fsp)->fsp_name->st) ?
920 ctime(&((*result_fsp)->fsp_name->st.st_ex_mtime.tv_sec)) :
921 "No fsp time\n"));
922 return status;
925 static int um_renameat(vfs_handle_struct *handle,
926 files_struct *srcfsp,
927 const struct smb_filename *smb_fname_src,
928 files_struct *dstfsp,
929 const struct smb_filename *smb_fname_dst,
930 const struct vfs_rename_how *how)
932 int status;
933 struct smb_filename *src_full_fname = NULL;
934 struct smb_filename *dst_full_fname = NULL;
935 struct smb_filename *src_client_fname = NULL;
936 struct smb_filename *dst_client_fname = NULL;
938 src_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
939 srcfsp,
940 smb_fname_src);
941 if (src_full_fname == NULL) {
942 errno = ENOMEM;
943 return -1;
945 dst_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
946 dstfsp,
947 smb_fname_dst);
948 if (dst_full_fname == NULL) {
949 TALLOC_FREE(src_full_fname);
950 errno = ENOMEM;
951 return -1;
954 DBG_DEBUG( "Entering with "
955 "smb_fname_src->base_name '%s', "
956 "smb_fname_dst->base_name '%s'\n",
957 smb_fname_src->base_name,
958 smb_fname_dst->base_name);
960 if (!is_in_media_files(src_full_fname->base_name)
962 !is_in_media_files(dst_full_fname->base_name)) {
963 TALLOC_FREE(src_full_fname);
964 TALLOC_FREE(dst_full_fname);
965 return SMB_VFS_NEXT_RENAMEAT(handle,
966 srcfsp,
967 smb_fname_src,
968 dstfsp,
969 smb_fname_dst,
970 how);
973 status = alloc_get_client_smb_fname(handle, talloc_tos(),
974 src_full_fname,
975 &src_client_fname);
976 if (status != 0) {
977 goto err;
980 status = alloc_get_client_smb_fname(handle, talloc_tos(),
981 dst_full_fname,
982 &dst_client_fname);
984 if (status != 0) {
985 goto err;
988 status = SMB_VFS_NEXT_RENAMEAT(handle,
989 handle->conn->cwd_fsp,
990 src_client_fname,
991 handle->conn->cwd_fsp,
992 dst_client_fname,
993 how);
995 err:
996 TALLOC_FREE(dst_client_fname);
997 TALLOC_FREE(src_client_fname);
998 TALLOC_FREE(src_full_fname);
999 TALLOC_FREE(dst_full_fname);
1000 DBG_DEBUG( "Leaving with smb_fname_src->base_name '%s',"
1001 " smb_fname_dst->base_name '%s'\n",
1002 smb_fname_src->base_name,
1003 smb_fname_dst->base_name);
1004 return status;
1009 * Success: return 0
1010 * Failure: set errno, return -1
1012 static int um_stat(vfs_handle_struct *handle,
1013 struct smb_filename *smb_fname)
1015 int status = 0;
1016 struct smb_filename *client_fname = NULL;
1018 DEBUG(10, ("Entering with smb_fname->base_name '%s'\n",
1019 smb_fname->base_name));
1021 if (!is_in_media_files(smb_fname->base_name)) {
1022 return SMB_VFS_NEXT_STAT(handle, smb_fname);
1025 status = alloc_get_client_smb_fname(handle, talloc_tos(),
1026 smb_fname,
1027 &client_fname);
1028 if (status != 0) {
1029 goto err;
1031 DEBUG(10, ("Stat'ing client_fname->base_name '%s'\n",
1032 client_fname->base_name));
1034 status = SMB_VFS_NEXT_STAT(handle, client_fname);
1035 if (status != 0) {
1036 goto err;
1040 * Unlike functions with const smb_filename, we have to modify
1041 * smb_fname itself to pass our info back up.
1043 DEBUG(10, ("Setting smb_fname '%s' stat from client_fname '%s'\n",
1044 smb_fname->base_name, client_fname->base_name));
1045 smb_fname->st = client_fname->st;
1047 err:
1048 TALLOC_FREE(client_fname);
1049 DEBUG(10, ("Leaving with smb_fname->st.st_ex_mtime %s",
1050 ctime(&(smb_fname->st.st_ex_mtime.tv_sec))));
1051 return status;
1054 static int um_lstat(vfs_handle_struct *handle,
1055 struct smb_filename *smb_fname)
1057 int status = 0;
1058 struct smb_filename *client_fname = NULL;
1060 DEBUG(10, ("Entering with smb_fname->base_name '%s'\n",
1061 smb_fname->base_name));
1063 if (!is_in_media_files(smb_fname->base_name)) {
1064 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1067 client_fname = NULL;
1069 status = alloc_get_client_smb_fname(handle, talloc_tos(),
1070 smb_fname,
1071 &client_fname);
1072 if (status != 0) {
1073 goto err;
1075 status = SMB_VFS_NEXT_LSTAT(handle, client_fname);
1076 if (status != 0) {
1077 goto err;
1080 smb_fname->st = client_fname->st;
1082 err:
1083 TALLOC_FREE(client_fname);
1084 DEBUG(10, ("Leaving with smb_fname->st.st_ex_mtime %s",
1085 ctime(&(smb_fname->st.st_ex_mtime.tv_sec))));
1086 return status;
1089 static int um_fstat(vfs_handle_struct *handle,
1090 files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1092 int status = 0;
1094 DEBUG(10, ("Entering with fsp->fsp_name->base_name "
1095 "'%s'\n", fsp_str_dbg(fsp)));
1097 status = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1098 if (status != 0) {
1099 goto out;
1102 if ((fsp->fsp_name == NULL) ||
1103 !is_in_media_files(fsp->fsp_name->base_name)) {
1104 goto out;
1107 status = um_stat(handle, fsp->fsp_name);
1108 if (status != 0) {
1109 goto out;
1112 *sbuf = fsp->fsp_name->st;
1114 out:
1115 DEBUG(10, ("Leaving with fsp->fsp_name->st.st_ex_mtime %s",
1116 fsp->fsp_name != NULL ?
1117 ctime(&(fsp->fsp_name->st.st_ex_mtime.tv_sec)) : "0\n"));
1118 return status;
1121 static int um_unlinkat(vfs_handle_struct *handle,
1122 struct files_struct *dirfsp,
1123 const struct smb_filename *smb_fname,
1124 int flags)
1126 int ret;
1127 struct smb_filename *full_fname = NULL;
1128 struct smb_filename *client_fname = NULL;
1130 DEBUG(10, ("Entering um_unlinkat\n"));
1132 if (!is_in_media_files(smb_fname->base_name)) {
1133 return SMB_VFS_NEXT_UNLINKAT(handle,
1134 dirfsp,
1135 smb_fname,
1136 flags);
1139 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1140 dirfsp,
1141 smb_fname);
1142 if (full_fname == NULL) {
1143 return -1;
1146 ret = alloc_get_client_smb_fname(handle, talloc_tos(),
1147 full_fname,
1148 &client_fname);
1149 if (ret != 0) {
1150 goto err;
1153 ret = SMB_VFS_NEXT_UNLINKAT(handle,
1154 dirfsp->conn->cwd_fsp,
1155 client_fname,
1156 flags);
1158 err:
1159 TALLOC_FREE(full_fname);
1160 TALLOC_FREE(client_fname);
1161 return ret;
1164 static int um_lchown(vfs_handle_struct *handle,
1165 const struct smb_filename *smb_fname,
1166 uid_t uid,
1167 gid_t gid)
1169 int status;
1170 struct smb_filename *client_fname = NULL;
1172 DEBUG(10, ("Entering um_lchown\n"));
1173 if (!is_in_media_files(smb_fname->base_name)) {
1174 return SMB_VFS_NEXT_LCHOWN(handle, smb_fname, uid, gid);
1177 status = alloc_get_client_smb_fname(handle,
1178 talloc_tos(),
1179 smb_fname,
1180 &client_fname);
1181 if (status != 0) {
1182 goto err;
1185 status = SMB_VFS_NEXT_LCHOWN(handle, client_fname, uid, gid);
1187 err:
1188 TALLOC_FREE(client_fname);
1189 return status;
1192 static int um_chdir(vfs_handle_struct *handle,
1193 const struct smb_filename *smb_fname)
1195 int status;
1196 struct smb_filename *client_fname = NULL;
1198 DEBUG(10, ("Entering um_chdir\n"));
1200 if (!is_in_media_files(smb_fname->base_name)) {
1201 return SMB_VFS_NEXT_CHDIR(handle, smb_fname);
1204 status = alloc_get_client_smb_fname(handle,
1205 talloc_tos(),
1206 smb_fname,
1207 &client_fname);
1208 if (status != 0) {
1209 goto err;
1212 status = SMB_VFS_NEXT_CHDIR(handle, client_fname);
1214 err:
1215 TALLOC_FREE(client_fname);
1216 return status;
1219 static int um_symlinkat(vfs_handle_struct *handle,
1220 const struct smb_filename *link_contents,
1221 struct files_struct *dirfsp,
1222 const struct smb_filename *new_smb_fname)
1224 int status;
1225 struct smb_filename *new_link_target = NULL;
1226 struct smb_filename *new_client_fname = NULL;
1227 struct smb_filename *full_fname = NULL;
1229 DEBUG(10, ("Entering um_symlinkat\n"));
1231 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1232 dirfsp,
1233 new_smb_fname);
1234 if (full_fname == NULL) {
1235 return -1;
1238 if (!is_in_media_files(link_contents->base_name) &&
1239 !is_in_media_files(full_fname->base_name)) {
1240 TALLOC_FREE(full_fname);
1241 return SMB_VFS_NEXT_SYMLINKAT(handle,
1242 link_contents,
1243 dirfsp,
1244 new_smb_fname);
1247 status = alloc_get_client_smb_fname(handle, talloc_tos(),
1248 link_contents, &new_link_target);
1249 if (status != 0) {
1250 goto err;
1252 status = alloc_get_client_smb_fname(handle, talloc_tos(),
1253 full_fname, &new_client_fname);
1254 if (status != 0) {
1255 goto err;
1258 status = SMB_VFS_NEXT_SYMLINKAT(handle,
1259 new_link_target,
1260 handle->conn->cwd_fsp,
1261 new_client_fname);
1263 err:
1264 TALLOC_FREE(new_link_target);
1265 TALLOC_FREE(new_client_fname);
1266 TALLOC_FREE(full_fname);
1267 return status;
1270 static int um_readlinkat(vfs_handle_struct *handle,
1271 const struct files_struct *dirfsp,
1272 const struct smb_filename *smb_fname,
1273 char *buf,
1274 size_t bufsiz)
1276 int status;
1277 struct smb_filename *client_fname = NULL;
1278 struct smb_filename *full_fname = NULL;
1280 DEBUG(10, ("Entering um_readlinkat\n"));
1282 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1283 dirfsp,
1284 smb_fname);
1285 if (full_fname == NULL) {
1286 return -1;
1289 if (!is_in_media_files(full_fname->base_name)) {
1290 TALLOC_FREE(full_fname);
1291 return SMB_VFS_NEXT_READLINKAT(handle,
1292 dirfsp,
1293 smb_fname,
1294 buf,
1295 bufsiz);
1298 status = alloc_get_client_smb_fname(handle, talloc_tos(),
1299 full_fname, &client_fname);
1300 if (status != 0) {
1301 goto err;
1304 status = SMB_VFS_NEXT_READLINKAT(handle,
1305 handle->conn->cwd_fsp,
1306 client_fname,
1307 buf,
1308 bufsiz);
1310 err:
1311 TALLOC_FREE(full_fname);
1312 TALLOC_FREE(client_fname);
1313 return status;
1316 static int um_linkat(vfs_handle_struct *handle,
1317 files_struct *srcfsp,
1318 const struct smb_filename *old_smb_fname,
1319 files_struct *dstfsp,
1320 const struct smb_filename *new_smb_fname,
1321 int flags)
1323 int status;
1324 struct smb_filename *old_full_fname = NULL;
1325 struct smb_filename *new_full_fname = NULL;
1326 struct smb_filename *old_client_fname = NULL;
1327 struct smb_filename *new_client_fname = NULL;
1329 old_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1330 srcfsp,
1331 old_smb_fname);
1332 if (old_full_fname == NULL) {
1333 return -1;
1335 new_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1336 dstfsp,
1337 new_smb_fname);
1338 if (new_full_fname == NULL) {
1339 TALLOC_FREE(old_full_fname);
1340 return -1;
1343 DEBUG(10, ("Entering um_linkat\n"));
1344 if (!is_in_media_files(old_full_fname->base_name) &&
1345 !is_in_media_files(new_full_fname->base_name)) {
1346 TALLOC_FREE(old_full_fname);
1347 TALLOC_FREE(new_full_fname);
1348 return SMB_VFS_NEXT_LINKAT(handle,
1349 srcfsp,
1350 old_smb_fname,
1351 dstfsp,
1352 new_smb_fname,
1353 flags);
1356 status = alloc_get_client_smb_fname(handle, talloc_tos(),
1357 old_full_fname, &old_client_fname);
1358 if (status != 0) {
1359 goto err;
1361 status = alloc_get_client_smb_fname(handle, talloc_tos(),
1362 new_full_fname, &new_client_fname);
1363 if (status != 0) {
1364 goto err;
1367 status = SMB_VFS_NEXT_LINKAT(handle,
1368 handle->conn->cwd_fsp,
1369 old_client_fname,
1370 handle->conn->cwd_fsp,
1371 new_client_fname,
1372 flags);
1374 err:
1375 TALLOC_FREE(old_full_fname);
1376 TALLOC_FREE(new_full_fname);
1377 TALLOC_FREE(old_client_fname);
1378 TALLOC_FREE(new_client_fname);
1379 return status;
1382 static int um_mknodat(vfs_handle_struct *handle,
1383 files_struct *dirfsp,
1384 const struct smb_filename *smb_fname,
1385 mode_t mode,
1386 SMB_DEV_T dev)
1388 int status;
1389 struct smb_filename *client_fname = NULL;
1390 struct smb_filename *full_fname = NULL;
1392 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1393 dirfsp,
1394 smb_fname);
1395 if (full_fname == NULL) {
1396 return -1;
1399 DEBUG(10, ("Entering um_mknodat\n"));
1400 if (!is_in_media_files(full_fname->base_name)) {
1401 TALLOC_FREE(full_fname);
1402 return SMB_VFS_NEXT_MKNODAT(handle,
1403 dirfsp,
1404 smb_fname,
1405 mode,
1406 dev);
1409 status = alloc_get_client_smb_fname(handle, talloc_tos(),
1410 full_fname, &client_fname);
1411 if (status != 0) {
1412 goto err;
1415 status = SMB_VFS_NEXT_MKNODAT(handle,
1416 handle->conn->cwd_fsp,
1417 client_fname,
1418 mode,
1419 dev);
1421 err:
1422 TALLOC_FREE(client_fname);
1423 TALLOC_FREE(full_fname);
1424 return status;
1427 static struct smb_filename *um_realpath(vfs_handle_struct *handle,
1428 TALLOC_CTX *ctx,
1429 const struct smb_filename *smb_fname)
1431 struct smb_filename *client_fname = NULL;
1432 struct smb_filename *result_fname = NULL;
1433 int status;
1435 DEBUG(10, ("Entering um_realpath\n"));
1437 if (!is_in_media_files(smb_fname->base_name)) {
1438 return SMB_VFS_NEXT_REALPATH(handle, ctx, smb_fname);
1441 status = alloc_get_client_smb_fname(handle, talloc_tos(),
1442 smb_fname, &client_fname);
1443 if (status != 0) {
1444 goto err;
1447 result_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, client_fname);
1449 err:
1450 TALLOC_FREE(client_fname);
1451 return result_fname;
1454 static int um_connect(vfs_handle_struct *handle,
1455 const char *service,
1456 const char *user)
1458 int rc;
1459 struct um_config_data *config;
1460 int enumval;
1462 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1463 if (rc != 0) {
1464 return rc;
1467 config = talloc_zero(handle->conn, struct um_config_data);
1468 if (!config) {
1469 DEBUG(1, ("talloc_zero() failed\n"));
1470 errno = ENOMEM;
1471 return -1;
1474 enumval = lp_parm_enum(SNUM(handle->conn), UM_PARAM_TYPE_NAME,
1475 "clientid", um_clientid, UM_CLIENTID_NAME);
1476 if (enumval == -1) {
1477 DEBUG(1, ("value for %s: type unknown\n",
1478 UM_PARAM_TYPE_NAME));
1479 return -1;
1481 config->clientid = (enum um_clientid)enumval;
1483 SMB_VFS_HANDLE_SET_DATA(handle, config,
1484 NULL, struct um_config_data,
1485 return -1);
1487 return 0;
1490 /* VFS operations structure */
1492 static struct vfs_fn_pointers vfs_um_fns = {
1493 .connect_fn = um_connect,
1495 /* Disk operations */
1497 .statvfs_fn = um_statvfs,
1499 /* Directory operations */
1501 .fdopendir_fn = um_fdopendir,
1502 .readdir_fn = um_readdir,
1503 .rewind_dir_fn = um_rewinddir,
1504 .mkdirat_fn = um_mkdirat,
1505 .closedir_fn = um_closedir,
1507 /* File operations */
1509 .openat_fn = um_openat,
1510 .create_file_fn = um_create_file,
1511 .renameat_fn = um_renameat,
1512 .stat_fn = um_stat,
1513 .lstat_fn = um_lstat,
1514 .fstat_fn = um_fstat,
1515 .unlinkat_fn = um_unlinkat,
1516 .lchown_fn = um_lchown,
1517 .chdir_fn = um_chdir,
1518 .symlinkat_fn = um_symlinkat,
1519 .readlinkat_fn = um_readlinkat,
1520 .linkat_fn = um_linkat,
1521 .mknodat_fn = um_mknodat,
1522 .realpath_fn = um_realpath,
1524 /* EA operations. */
1525 .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
1526 .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
1529 static_decl_vfs;
1530 NTSTATUS vfs_unityed_media_init(TALLOC_CTX *ctx)
1532 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1533 "unityed_media", &vfs_um_fns);
1534 if (!NT_STATUS_IS_OK(ret)) {
1535 return ret;
1538 vfs_um_debug_level = debug_add_class("unityed_media");
1540 if (vfs_um_debug_level == -1) {
1541 vfs_um_debug_level = DBGC_VFS;
1542 DEBUG(1, ("unityed_media_init: Couldn't register custom "
1543 "debugging class.\n"));
1546 return ret;