s3:libsmb: allow store_cldap_reply() to work with a ipv6 response
[samba.git] / source3 / modules / vfs_media_harmony.c
1 /*
2  * $Id: media_harmony.c,v 1.1 2007/11/06 10:07:22 stuart_hc Exp $
3  *
4  * Samba VFS module supporting multiple AVID clients sharing media.
5  *
6  * Copyright (C) 2005  Philip de Nier <philipn@users.sourceforge.net>
7  * Copyright (C) 2012  Andrew Klaassen <clawsoon@yahoo.com>
8  *
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.
13  *
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.
18  *
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.
23  */
24
25
26 /*
27  * Media Harmony is a Samba VFS module that allows multiple AVID
28  * clients to share media. Each client sees their own copy of the
29  * AVID msmMMOB.mdb and msmFMID.pmr files and Creating directories.
30  *
31  * Add this module to the vfs objects option in your Samba share
32  * configuration.
33  * eg.
34  *
35  *   [avid_win]
36  *      path = /video
37  *      vfs objects = media_harmony
38  *      ...
39  *
40  * It is recommended that you separate out Samba shares for Mac
41  * and Windows clients, and add the following options to the shares
42  * for Windows clients  (NOTE: replace @ with *):
43  *
44  *      veto files = /.DS_Store/._@/.Trash@/.Spotlight@/.hidden/.hotfiles@/.vol/
45  *      delete veto files = yes
46  *
47  * This prevents hidden files from Mac clients interfering with Windows
48  * clients. If you find any more problem hidden files then add them to
49  * the list.
50  *
51  *
52  * Andrew Klaassen, 2012-03-14
53  * To prevent Avid clients from interrupting each other (via Avid's habit
54  * of launching a database refresh whenever it notices an mtime update
55  * on media directories, i.e. whenever one editor adds new material to a
56  * shared share), I've added code that causes stat information for anything
57  * directly under "Avid MediaFile/MXF" to be taken from
58  * dirname_clientaddr_clientuser if it exists.  These files ~aren't~
59  * hidden, unlike the client-suffixed database files.
60  *
61  * For example, stat information for
62  *      Avid MediaFiles/MXF/1
63  * will come from
64  *      Avid MediaFiles/MXF/1_192.168.1.10_dave
65  * for dave working on 192.168.1.10, but will come from
66  *      Avid MediaFile/MXF/1_192.168.1.11_susan
67  * for susan working on 192.168.1.11.  If those alternate
68  * directories don't exist, the user will get the actual directory's stat
69  * info.  When an editor wants to force a database refresh, they update
70  * the mtime on "their" file.  This will cause Avid
71  * on that client to see an updated mtime for "Avid MediaFiles/MXF/1",
72  * which will trigger an Avid database refresh just for that editor.
73  *
74  *
75  * Notes:
76  * - This module is designed to work with AVID editing applications that
77  * look in the Avid MediaFiles or OMFI MediaFiles directory for media.
78  * It is not designed to work as expected in all circumstances for
79  * general use. For example: it is possibly to open client specific
80  * files such as msmMMOB.mdb_192.168.1.10_userx even though is doesn't
81  * show up in a directory listing.
82  *
83  */
84
85
86 #include "includes.h"
87 #include "system/filesys.h"
88 #include "smbd/smbd.h"
89 #include "../smbd/globals.h"
90 #include "auth.h"
91 #include "../lib/tsocket/tsocket.h"
92
93 #define MH_INFO_DEBUG 10
94 #define MH_ERR_DEBUG 0
95
96 static const char* MDB_FILENAME = "msmMMOB.mdb";
97 static const size_t MDB_FILENAME_LEN = 11;
98 static const char* PMR_FILENAME = "msmFMID.pmr";
99 static const size_t PMR_FILENAME_LEN = 11;
100 static const char* CREATING_DIRNAME = "Creating";
101 static const size_t CREATING_DIRNAME_LEN = 8;
102 static const char* AVID_MEDIAFILES_DIRNAME = "Avid MediaFiles";
103 static const size_t AVID_MEDIAFILES_DIRNAME_LEN = 15;
104 static const char* OMFI_MEDIAFILES_DIRNAME = "OMFI MediaFiles";
105 static const size_t OMFI_MEDIAFILES_DIRNAME_LEN = 15;
106 static const char* APPLE_DOUBLE_PREFIX = "._";
107 static const size_t APPLE_DOUBLE_PREFIX_LEN = 2;
108 static const char* AVID_MXF_DIRNAME = "Avid MediaFiles/MXF";
109 static const size_t AVID_MXF_DIRNAME_LEN = 19;
110
111 static int vfs_mh_debug_level = DBGC_VFS;
112
113 /* supplements the directory list stream */
114 typedef struct mh_dirinfo_struct
115 {
116         DIR* dirstream;
117         char *dirpath;
118         char *clientPath;
119         bool isInMediaFiles;
120         char *clientMDBFilename;
121         char *clientPMRFilename;
122         char *clientCreatingDirname;
123 } mh_dirinfo_struct;
124
125
126 /* Add "_<ip address>_<user name>" suffix to path or filename.
127  *
128  * Success: return 0
129  * Failure: set errno, path NULL, return -1
130  */
131 static int alloc_append_client_suffix(vfs_handle_struct *handle,
132                 char **path)
133 {
134         int status = 0;
135         char *raddr = NULL;
136
137         DEBUG(MH_INFO_DEBUG, ("Entering with *path '%s'\n", *path));
138
139         raddr = tsocket_address_inet_addr_string(
140                         handle->conn->sconn->remote_address, talloc_tos());
141         if (raddr == NULL)
142         {
143                 errno = ENOMEM;
144                 status = -1;
145                 goto err;
146         }
147
148         /* talloc_asprintf_append uses talloc_realloc, which
149          * frees original 'path' memory so we don't have to.
150          */
151         *path = talloc_asprintf_append(*path, "_%s_%s",
152                 raddr,
153                 handle->conn->session_info->unix_info->sanitized_username);
154         if (*path == NULL)
155         {
156                 DEBUG(MH_ERR_DEBUG, ("alloc_append_client_suffix "
157                                         "out of memory\n"));
158                 errno = ENOMEM;
159                 status = -1;
160                 goto err;
161         }
162         DEBUG(MH_INFO_DEBUG, ("Leaving with *path '%s'\n", *path));
163 err:
164         TALLOC_FREE(raddr);
165         return status;
166 }
167
168
169 /* Returns True if the file or directory begins with the appledouble
170  * prefix.
171  */
172 static bool is_apple_double(const char* fname)
173 {
174         bool ret = False;
175
176         DEBUG(MH_INFO_DEBUG, ("Entering with fname '%s'\n", fname));
177
178         if (strncmp(APPLE_DOUBLE_PREFIX, fname, APPLE_DOUBLE_PREFIX_LEN)
179                         == 0)
180         {
181                 ret = True;
182         }
183         DEBUG(MH_INFO_DEBUG, ("Leaving with ret '%s'\n",
184                                 ret == True ? "True" : "False"));
185         return ret;
186 }
187
188 static bool starts_with_media_dir(const char* media_dirname,
189                 size_t media_dirname_len, const char* path)
190 {
191         bool ret = False;
192         const char *path_start;
193
194         DEBUG(MH_INFO_DEBUG, ("Entering with media_dirname '%s' "
195                               "path '%s'\n", media_dirname, path));
196
197         /* Sometimes Samba gives us "./OMFI MediaFiles". */
198         if (strncmp(path, "./", 2) == 0)
199         {
200                 path_start = &path[2];
201         }
202         else {
203                 path_start = path;
204         }
205
206         if (strncmp(media_dirname, path_start, media_dirname_len) == 0
207                         &&
208                 (
209                         path_start[media_dirname_len] == '\0'
210                         ||
211                         path_start[media_dirname_len] == '/'
212                 )
213         )
214         {
215                 ret = True;
216         }
217
218         DEBUG(MH_INFO_DEBUG, ("Leaving with ret '%s'\n",
219                                 ret == True ? "True" : "False"));
220         return ret;
221 }
222
223 /*
224  * Returns True if the file or directory referenced by the path is below
225  * the AVID_MEDIAFILES_DIRNAME or OMFI_MEDIAFILES_DIRNAME directory
226  * The AVID_MEDIAFILES_DIRNAME and OMFI_MEDIAFILES_DIRNAME are assumed to
227  * be in the root directory, which is generally a safe assumption
228  * in the fixed-path world of Avid.
229  */
230 static bool is_in_media_files(const char* path)
231 {
232         bool ret = False;
233
234         DEBUG(MH_INFO_DEBUG, ("Entering with path '%s'\n", path));
235
236         if (
237                 starts_with_media_dir(AVID_MEDIAFILES_DIRNAME,
238                                 AVID_MEDIAFILES_DIRNAME_LEN, path)
239                 ||
240                 starts_with_media_dir(OMFI_MEDIAFILES_DIRNAME,
241                                 OMFI_MEDIAFILES_DIRNAME_LEN, path)
242         )
243         {
244                 ret = True;
245         }
246         DEBUG(MH_INFO_DEBUG, ("Leaving with ret '%s'\n",
247                                 ret == True ? "True" : "False"));
248         return ret;
249 }
250
251 /*
252  * Returns depth of path under media directory.  Deals with the
253  * occasional ..../. and ..../.. paths that get passed to stat.
254  *
255  * Assumes is_in_media_files has already been called and has returned
256  * true for the path; if it hasn't, this function will likely crash
257  * and burn.
258  *
259  * Not foolproof; something like "Avid MediaFiles/MXF/../foo/1"
260  * would fool it.  Haven't seen paths like that getting to the
261  * stat function yet, so ignoring that possibility for now.
262  */
263 static int depth_from_media_dir(const char* media_dirname,
264                 size_t media_dirname_len, const char* path)
265 {
266         int transition_count = 0;
267         const char *path_start;
268         const char *pathPtr;
269
270         DEBUG(MH_INFO_DEBUG, ("Entering with media_dirname '%s' "
271                               "path '%s'\n", media_dirname, path));
272
273         /* Sometimes Samba gives us "./OMFI MediaFiles". */
274         if (strncmp(path, "./", 2) == 0)
275         {
276                 path_start = &path[2];
277         }
278         else {
279                 path_start = path;
280         }
281
282         if (path_start[media_dirname_len] == '\0')
283         {
284                 goto out;
285         }
286
287         pathPtr = &path_start[media_dirname_len + 1];
288
289         while(1)
290         {
291                 if (*pathPtr == '\0' || *pathPtr == '/')
292                 {
293                         if (
294                                 *(pathPtr - 1) == '.'
295                                         &&
296                                 *(pathPtr - 2) == '.'
297                                         &&
298                                 *(pathPtr - 3) == '/'
299                         )
300                         {
301                                 transition_count--;
302                         }
303                         else if (
304                                 !
305                                 (
306                                         *(pathPtr - 1) == '/'
307                                         ||
308                                         (
309                                                 *(pathPtr - 1) == '.'
310                                                         &&
311                                                 *(pathPtr - 2) == '/'
312                                         )
313                                 )
314                         )
315                         {
316                                 transition_count++;
317                         }
318                 }
319                 if (*pathPtr == '\0')
320                 {
321                         break;
322                 }
323                 pathPtr++;
324         }
325
326         DEBUG(MH_INFO_DEBUG, ("Leaving with transition_count '%i'\n",
327                                 transition_count));
328 out:
329         return transition_count;
330 }
331
332 /* Identifies MDB and PMR files at end of path. */
333 static bool is_avid_database(
334                 char *path,
335                 size_t path_len,
336                 const char *avid_db_filename,
337                 const size_t avid_db_filename_len)
338 {
339         bool ret = False;
340
341         DEBUG(MH_INFO_DEBUG, ("Entering with path '%s', "
342                               "avid_db_filename '%s', "
343                               "path_len '%i', "
344                               "avid_db_filename_len '%i'\n",
345                               path, avid_db_filename,
346                               (int)path_len, (int)avid_db_filename_len));
347
348         if (
349                 path_len > avid_db_filename_len
350                         &&
351                 strcmp(&path[path_len - avid_db_filename_len],
352                                 avid_db_filename) == 0
353                         &&
354                 (
355                         path[path_len - avid_db_filename_len - 1] == '/'
356                         ||
357                         (path_len > avid_db_filename_len
358                                 + APPLE_DOUBLE_PREFIX_LEN
359                                 &&
360                         path[path_len - avid_db_filename_len
361                                 - APPLE_DOUBLE_PREFIX_LEN - 1] == '/'
362                                 &&
363                         is_apple_double(&path[path_len
364                                 - avid_db_filename_len
365                                 - APPLE_DOUBLE_PREFIX_LEN]))
366                 )
367         )
368         {
369                 ret = True;
370         }
371         DEBUG(MH_INFO_DEBUG, ("Leaving with ret '%s'\n",
372                                 ret == True ? "True" : "False"));
373         return ret;
374 }
375
376
377 /* Add client suffix to paths to MDB_FILENAME, PMR_FILENAME and
378  * CREATING_SUBDIRNAME.
379  *
380  * Caller must free newPath.
381  *
382  * Success: return 0
383  * Failure: set errno, newPath NULL, return -1
384  */
385 static int alloc_get_client_path(vfs_handle_struct *handle,
386                 TALLOC_CTX *ctx,
387                 const char *path,
388                 char **newPath)
389 {
390         /* replace /CREATING_DIRNAME/ or /._CREATING_DIRNAME/
391          * directory in path - potentially in middle of path
392          * - with suffixed name.
393          */
394         int status = 0;
395         char* pathPtr;
396         size_t intermPathLen;
397
398         DEBUG(MH_INFO_DEBUG, ("Entering with path '%s'\n", path));
399
400         *newPath = talloc_strdup(ctx, path);
401         if (*newPath == NULL)
402         {
403                 DEBUG(MH_ERR_DEBUG, ("alloc_get_client_path ENOMEM #1\n"));
404                 errno = ENOMEM;
405                 status = -1;
406                 goto out;
407         }
408         DEBUG(MH_INFO_DEBUG, ("newPath #1 %s\n", *newPath));
409         if (
410                 (pathPtr = strstr(path, CREATING_DIRNAME)) != NULL
411                         &&
412                 (
413                         *(pathPtr + CREATING_DIRNAME_LEN) == '\0'
414                         ||
415                         *(pathPtr + CREATING_DIRNAME_LEN) == '/'
416                 )
417                         &&
418                 (
419                         (pathPtr - path > 0
420                                 &&
421                          *(pathPtr - 1) == '/')
422                         ||
423                         (pathPtr - path > APPLE_DOUBLE_PREFIX_LEN
424                                 &&
425                         *(pathPtr - APPLE_DOUBLE_PREFIX_LEN - 1) == '/'
426                                 &&
427                          is_apple_double(pathPtr - APPLE_DOUBLE_PREFIX_LEN))
428                 )
429         )
430         {
431                 /* Insert client suffix into path. */
432                 (*newPath)[pathPtr - path + CREATING_DIRNAME_LEN] = '\0';
433                 DEBUG(MH_INFO_DEBUG, ("newPath #2 %s\n", *newPath));
434
435                 if ((status = alloc_append_client_suffix(handle, newPath)))
436                 {
437                         goto out;
438                 }
439
440                 DEBUG(MH_INFO_DEBUG, ("newPath #3 %s\n", *newPath));
441                 *newPath = talloc_strdup_append(*newPath,
442                                 pathPtr + CREATING_DIRNAME_LEN);
443                 if (*newPath == NULL)
444                 {
445                         DEBUG(MH_ERR_DEBUG, ("alloc_get_client_path "
446                                                 "ENOMEM #2\n"));
447                         errno = ENOMEM;
448                         status = -1;
449                         goto out;
450                 }
451                 DEBUG(MH_INFO_DEBUG, ("newPath #4 %s\n", *newPath));
452         }
453
454         /* replace /MDB_FILENAME or /PMR_FILENAME or /._MDB_FILENAME
455          * or /._PMR_FILENAME at newPath end with suffixed name.
456          */
457         intermPathLen = strlen(*newPath);
458         if (
459                 is_avid_database(*newPath, intermPathLen,
460                         MDB_FILENAME, MDB_FILENAME_LEN)
461                 ||
462                 is_avid_database(*newPath, intermPathLen,
463                         PMR_FILENAME, PMR_FILENAME_LEN)
464         )
465         {
466                 DEBUG(MH_INFO_DEBUG, ("newPath #5 %s\n", *newPath));
467                 if ((status = alloc_append_client_suffix(handle, newPath)))
468                 {
469                         goto out;
470                 }
471                 DEBUG(MH_INFO_DEBUG, ("newPath #6 %s\n", *newPath));
472         }
473 out:
474         /* newPath must be freed in caller. */
475         DEBUG(MH_INFO_DEBUG, ("Leaving with *newPath '%s'\n", *newPath));
476         return status;
477 }
478
479 /*
480  * Success: return 0
481  * Failure: set errno, return -1
482  */
483 static int alloc_get_client_smb_fname(struct vfs_handle_struct *handle,
484                 TALLOC_CTX *ctx,
485                 const struct smb_filename *smb_fname,
486                 struct smb_filename **clientFname)
487 {
488         int status = 0;
489
490         DEBUG(MH_INFO_DEBUG, ("Entering with smb_fname->base_name '%s'\n",
491                               smb_fname->base_name));
492
493         *clientFname = cp_smb_filename(ctx, smb_fname);
494         if ((*clientFname) == NULL) {
495                 DEBUG(MH_ERR_DEBUG, ("alloc_get_client_smb_fname "
496                                         "NTERR\n"));
497                 errno = ENOMEM;
498                 status = -1;
499                 goto err;
500         }
501         if ((status = alloc_get_client_path(handle, ctx,
502                                 smb_fname->base_name,
503                                 &(*clientFname)->base_name)))
504         {
505                 goto err;
506         }
507         DEBUG(MH_INFO_DEBUG, ("Leaving with (*clientFname)->base_name "
508                                 "'%s'\n", (*clientFname)->base_name));
509 err:
510         return status;
511 }
512
513
514 /*
515  * Success: return 0
516  * Failure: set errno, return -1
517  */
518 static int alloc_set_client_dirinfo_path(struct vfs_handle_struct *handle,
519                 TALLOC_CTX *ctx,
520                 char **path,
521                 const char *avid_db_filename)
522 {
523         int status = 0;
524
525         DEBUG(MH_INFO_DEBUG, ("Entering with avid_db_filename '%s'\n",
526                               avid_db_filename));
527
528         if ((*path = talloc_strdup(ctx, avid_db_filename)) == NULL)
529         {
530                 DEBUG(MH_ERR_DEBUG, ("alloc_set_client_dirinfo_path "
531                                         "ENOMEM\n"));
532                 errno = ENOMEM;
533                 status = -1;
534                 goto err;
535         }
536         if ((status = alloc_append_client_suffix(handle, path)))
537         {
538                 goto err;
539         }
540         DEBUG(MH_INFO_DEBUG, ("Leaving with *path '%s'\n", *path));
541 err:
542         return status;
543 }
544
545 /*
546  * Replace mtime on clientFname with mtime from client-suffixed
547  * equivalent, if it exists.
548  *
549  * Success: return 0
550  * Failure: set errno, return -1
551  */
552 static int set_fake_mtime(vfs_handle_struct *handle,
553                 TALLOC_CTX *ctx,
554                 struct smb_filename **clientFname,
555                 int (*statFn)(const char *, SMB_STRUCT_STAT *, bool))
556 {
557         int status = 0;
558         char *statPath;
559         SMB_STRUCT_STAT fakeStat;
560         int copy_len;
561
562         DEBUG(MH_INFO_DEBUG, ("Entering with (*clientFname)->base_name "
563                               "'%s', (*clientFname)->st.st_ex_mtime %s",
564                               (*clientFname)->base_name,
565                               ctime(&((*clientFname)->st.st_ex_mtime.tv_sec))));
566
567         if (
568                 depth_from_media_dir(AVID_MXF_DIRNAME,
569                                 AVID_MXF_DIRNAME_LEN,
570                                 (*clientFname)->base_name)
571                         != 1
572                         &&
573                 depth_from_media_dir(OMFI_MEDIAFILES_DIRNAME,
574                                 OMFI_MEDIAFILES_DIRNAME_LEN,
575                                 (*clientFname)->base_name)
576                         != 0
577         )
578         {
579                 goto out;
580         }
581
582         copy_len = strlen((*clientFname)->base_name);
583
584         /* Hack to deal with occasional "Avid MediaFiles/MXF/1/." paths.
585          * We know we're under a media dir, so paths are at least 2 chars
586          * long.
587          */
588         if ((*clientFname)->base_name[copy_len - 1] == '.' &&
589                         (*clientFname)->base_name[copy_len - 2] == '/')
590         {
591                 copy_len -= 2;
592         }
593
594         if (((statPath = talloc_strndup(ctx,
595                         (*clientFname)->base_name, copy_len)) == NULL))
596         {
597                 errno = ENOMEM;
598                 status = -1;
599                 goto err;
600         }
601         if ((status = alloc_append_client_suffix(handle, &statPath)))
602         {
603                 goto err;
604         }
605
606         DEBUG(MH_INFO_DEBUG, ("Fake stat'ing '%s'\n", statPath));
607         if (statFn(statPath, &fakeStat,
608                         lp_fake_directory_create_times(SNUM(handle->conn))))
609         {
610                 /* This can fail for legitimate reasons - i.e. the
611                  * fakeStat directory doesn't exist, which is okay
612                  * - so we don't set status.  But if it does fail,
613                  * we need to skip over the mtime assignment.
614                  */
615                 goto err;
616         }
617
618         DEBUG(MH_INFO_DEBUG, ("Setting fake mtime from '%s'\n", statPath));
619         (*clientFname)->st.st_ex_mtime = fakeStat.st_ex_mtime;
620 err:
621         TALLOC_FREE(statPath);
622 out:
623         DEBUG(MH_INFO_DEBUG, ("Leaving with (*clientFname)->base_name "
624                         "'%s', (*clientFname)->st.st_ex_mtime %s",
625                         (*clientFname)->base_name,
626                         ctime(&((*clientFname)->st.st_ex_mtime.tv_sec))));
627         return status;
628 }
629
630 /*
631  * Success: return 0
632  * Failure: set errno, return -1
633  */
634 static int mh_statvfs(struct vfs_handle_struct *handle,
635                 const struct smb_filename *smb_fname,
636                 struct vfs_statvfs_struct *statbuf)
637 {
638         int status;
639         struct smb_filename *clientFname = NULL;
640
641         DEBUG(MH_INFO_DEBUG, ("Entering with path '%s'\n",
642                         smb_fname->base_name));
643
644         if (!is_in_media_files(smb_fname->base_name))
645         {
646                 status = SMB_VFS_NEXT_STATVFS(handle, smb_fname, statbuf);
647                 goto out;
648         }
649
650         status = alloc_get_client_smb_fname(handle,
651                                 talloc_tos(),
652                                 smb_fname,
653                                 &clientFname);
654         if (status != 0) {
655                 goto err;
656         }
657
658         status = SMB_VFS_NEXT_STATVFS(handle, clientFname, statbuf);
659 err:
660         TALLOC_FREE(clientFname);
661 out:
662         DEBUG(MH_INFO_DEBUG, ("Leaving with path '%s'\n",
663                         smb_fname->base_name));
664         return status;
665 }
666
667 static int alloc_set_client_dirinfo(vfs_handle_struct *handle,
668                 const char *fname,
669                 struct mh_dirinfo_struct **dirInfo)
670 {
671         int status = 0;
672         char *clientPath;
673         TALLOC_CTX *ctx;
674
675         DEBUG(MH_INFO_DEBUG, ("Entering with fname '%s'\n", fname));
676
677         *dirInfo = talloc(NULL, struct mh_dirinfo_struct);
678         if (*dirInfo == NULL)
679         {
680                 goto err;
681         }
682
683         (*dirInfo)->dirpath = talloc_strdup(*dirInfo, fname);
684         if ((*dirInfo)->dirpath == NULL)
685         {
686                 goto err;
687         }
688
689         if (!is_in_media_files(fname))
690         {
691                 (*dirInfo)->clientPath = NULL;
692                 (*dirInfo)->clientMDBFilename = NULL;
693                 (*dirInfo)->clientPMRFilename = NULL;
694                 (*dirInfo)->clientCreatingDirname = NULL;
695                 (*dirInfo)->isInMediaFiles = False;
696                 goto out;
697         }
698
699         (*dirInfo)->isInMediaFiles = True;
700
701         if (alloc_set_client_dirinfo_path(handle,
702                                 *dirInfo,
703                                 &((*dirInfo)->clientMDBFilename),
704                                 MDB_FILENAME))
705         {
706                 goto err;
707         }
708
709         if (alloc_set_client_dirinfo_path(handle,
710                                 *dirInfo,
711                                 &((*dirInfo)->clientPMRFilename),
712                                 PMR_FILENAME))
713         {
714                 goto err;
715         }
716
717         if (alloc_set_client_dirinfo_path(handle,
718                                 *dirInfo,
719                                 &((*dirInfo)->clientCreatingDirname),
720                                 CREATING_DIRNAME))
721         {
722                 goto err;
723         }
724
725         clientPath = NULL;
726         ctx = talloc_tos();
727
728         if (alloc_get_client_path(handle, ctx,
729                                 fname,
730                                 &clientPath))
731         {
732                 goto err;
733         }
734
735         (*dirInfo)->clientPath = talloc_strdup(*dirInfo, clientPath);
736         if ((*dirInfo)->clientPath == NULL)
737         {
738                 goto err;
739         }
740
741         TALLOC_FREE(clientPath);
742
743 out:
744         DEBUG(MH_INFO_DEBUG, ("Leaving with (*dirInfo)->dirpath '%s', "
745                                 "(*dirInfo)->clientPath '%s'\n",
746                                 (*dirInfo)->dirpath,
747                                 (*dirInfo)->clientPath));
748         return status;
749
750 err:
751         DEBUG(MH_ERR_DEBUG, ("Failing with fname '%s'\n", fname));
752         TALLOC_FREE(*dirInfo);
753         status = -1;
754         errno = ENOMEM;
755         return status;
756 }
757
758 static DIR *mh_fdopendir(vfs_handle_struct *handle,
759                 files_struct *fsp,
760                 const char *mask,
761                 uint32_t attr)
762 {
763         struct mh_dirinfo_struct *dirInfo = NULL;
764         DIR *dirstream;
765
766         DEBUG(MH_INFO_DEBUG, ("Entering with fsp->fsp_name->base_name '%s'\n",
767                               fsp->fsp_name->base_name));
768
769         dirstream = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
770         if (!dirstream)
771         {
772                 goto err;
773         }
774
775         if (alloc_set_client_dirinfo(handle, fsp->fsp_name->base_name,
776                                         &dirInfo))
777         {
778                 goto err;
779         }
780
781         dirInfo->dirstream = dirstream;
782
783         if (! dirInfo->isInMediaFiles) {
784                 goto out;
785         }
786
787         if (set_fake_mtime(handle, fsp, &(fsp->fsp_name), sys_stat))
788         {
789                 goto err;
790         }
791
792 out:
793         DEBUG(MH_INFO_DEBUG, ("Leaving with dirInfo->dirpath '%s', "
794                         "dirInfo->clientPath '%s', "
795                         "fsp->fsp_name->st.st_ex_mtime %s",
796                         dirInfo->dirpath,
797                         dirInfo->clientPath,
798                         ctime(&(fsp->fsp_name->st.st_ex_mtime.tv_sec))));
799         /* Success is freed in closedir. */
800         return (DIR *) dirInfo;
801 err:
802         /* Failure is freed here. */
803         DEBUG(MH_ERR_DEBUG, ("Failing with fsp->fsp_name->base_name '%s'\n",
804                         fsp->fsp_name->base_name));
805         TALLOC_FREE(dirInfo);
806         return NULL;
807 }
808
809 /*
810  * skip MDB_FILENAME and PMR_FILENAME filenames and CREATING_DIRNAME
811  * directory, skip other client's suffixed MDB_FILENAME and PMR_FILENAME
812  * filenames and CREATING_DIRNAME directory, replace this client's
813  * suffixed MDB_FILENAME and PMR_FILENAME filenames and CREATING_DIRNAME
814  * directory with non suffixed.
815  *
816  * Success: return dirent
817  * End of data: return NULL
818  * Failure: set errno, return NULL
819  */
820 static struct dirent *
821 mh_readdir(vfs_handle_struct *handle, struct files_struct *dirfsp, DIR *dirp)
822 {
823         mh_dirinfo_struct* dirInfo = (mh_dirinfo_struct*)dirp;
824         struct dirent *d = NULL;
825         int skip;
826
827         DEBUG(MH_INFO_DEBUG, ("Entering mh_readdir\n"));
828
829         DEBUG(MH_INFO_DEBUG, ("dirInfo->dirpath '%s', "
830                               "dirInfo->clientPath '%s', "
831                               "dirInfo->isInMediaFiles '%s', "
832                               "dirInfo->clientMDBFilename '%s', "
833                               "dirInfo->clientPMRFilename '%s', "
834                               "dirInfo->clientCreatingDirname '%s'\n",
835                               dirInfo->dirpath,
836                               dirInfo->clientPath,
837                               dirInfo->isInMediaFiles ? "True" : "False",
838                               dirInfo->clientMDBFilename,
839                               dirInfo->clientPMRFilename,
840                               dirInfo->clientCreatingDirname));
841
842         if (! dirInfo->isInMediaFiles)
843         {
844                 d = SMB_VFS_NEXT_READDIR(handle, dirfsp, dirInfo->dirstream);
845                 goto out;
846         }
847
848         do
849         {
850                 const char* dname;
851                 bool isAppleDouble;
852
853                 skip = False;
854                 d = SMB_VFS_NEXT_READDIR(handle, dirfsp, dirInfo->dirstream);
855
856                 if (d == NULL)
857                 {
858                         break;
859                 }
860
861                 /* ignore apple double prefix for logic below */
862                 if (is_apple_double(d->d_name))
863                 {
864                         dname = &d->d_name[APPLE_DOUBLE_PREFIX_LEN];
865                         isAppleDouble = True;
866                 }
867                 else
868                 {
869                         dname = d->d_name;
870                         isAppleDouble = False;
871                 }
872
873                 /* skip Avid-special files with no client suffix */
874                 if (
875                         strcmp(dname, MDB_FILENAME) == 0
876                         ||
877                         strcmp(dname, PMR_FILENAME) == 0
878                         ||
879                         strcmp(dname, CREATING_DIRNAME) == 0
880                 )
881                 {
882                         skip = True;
883                 }
884                 /* chop client suffix off this client's suffixed files */
885                 else if (strcmp(dname, dirInfo->clientMDBFilename) == 0)
886                 {
887                         if (isAppleDouble)
888                         {
889                                 d->d_name[MDB_FILENAME_LEN
890                                         + APPLE_DOUBLE_PREFIX_LEN] = '\0';
891                         }
892                         else
893                         {
894                                 d->d_name[MDB_FILENAME_LEN] = '\0';
895                         }
896                 }
897                 else if (strcmp(dname, dirInfo->clientPMRFilename) == 0)
898                 {
899                         if (isAppleDouble)
900                         {
901                                 d->d_name[PMR_FILENAME_LEN
902                                         + APPLE_DOUBLE_PREFIX_LEN] = '\0';
903                         }
904                         else
905                         {
906                                 d->d_name[PMR_FILENAME_LEN] = '\0';
907                         }
908                 }
909                 else if (strcmp(dname, dirInfo->clientCreatingDirname)
910                                 == 0)
911                 {
912                         if (isAppleDouble)
913                         {
914                                 d->d_name[CREATING_DIRNAME_LEN
915                                         + APPLE_DOUBLE_PREFIX_LEN] = '\0';
916                         }
917                         else
918                         {
919                                 d->d_name[CREATING_DIRNAME_LEN] = '\0';
920                         }
921                 }
922                 /*
923                  * Anything that starts as an Avid-special file
924                  * that's made it this far should be skipped.  This
925                  * is different from the original behaviour, which
926                  * only skipped other client's suffixed files.
927                  */
928                 else if (
929                         strncmp(MDB_FILENAME, dname,
930                                 MDB_FILENAME_LEN) == 0
931                         ||
932                         strncmp(PMR_FILENAME, dname,
933                                 PMR_FILENAME_LEN) == 0
934                         ||
935                         strncmp(CREATING_DIRNAME, dname,
936                                 CREATING_DIRNAME_LEN) == 0
937                 )
938                 {
939                         skip = True;
940                 }
941         }
942         while (skip);
943
944 out:
945         DEBUG(MH_INFO_DEBUG, ("Leaving mh_readdir\n"));
946         return d;
947 }
948
949 /*
950  * Success: no success result defined.
951  * Failure: no failure result defined.
952  */
953 static void mh_rewinddir(vfs_handle_struct *handle,
954                 DIR *dirp)
955 {
956         DEBUG(MH_INFO_DEBUG, ("Entering and leaving mh_rewinddir\n"));
957         SMB_VFS_NEXT_REWINDDIR(handle,
958                         ((mh_dirinfo_struct*)dirp)->dirstream);
959 }
960
961 /*
962  * Success: return 0
963  * Failure: set errno, return -1
964  */
965 static int mh_mkdirat(vfs_handle_struct *handle,
966                 struct files_struct *dirfsp,
967                 const struct smb_filename *smb_fname,
968                 mode_t mode)
969 {
970         int status;
971         struct smb_filename *clientFname = NULL;
972         const char *path = smb_fname->base_name;
973         struct smb_filename *full_fname = NULL;
974
975         DEBUG(MH_INFO_DEBUG, ("Entering with path '%s'\n", path));
976
977         if (!is_in_media_files(path)) {
978                 status = SMB_VFS_NEXT_MKDIRAT(handle,
979                                 dirfsp,
980                                 smb_fname,
981                                 mode);
982                 goto out;
983         }
984
985         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
986                                                   dirfsp,
987                                                   smb_fname);
988         if (full_fname == NULL) {
989                 return -1;
990         }
991
992         status = alloc_get_client_smb_fname(handle,
993                                 talloc_tos(),
994                                 full_fname,
995                                 &clientFname);
996         if (status != 0) {
997                 goto err;
998         }
999
1000         status = SMB_VFS_NEXT_MKDIRAT(handle,
1001                         handle->conn->cwd_fsp,
1002                         clientFname,
1003                         mode);
1004 err:
1005         TALLOC_FREE(full_fname);
1006         TALLOC_FREE(clientFname);
1007 out:
1008         DEBUG(MH_INFO_DEBUG, ("Leaving with path '%s'\n", path));
1009         return status;
1010 }
1011
1012 /*
1013  * Success: return 0
1014  * Failure: set errno, return -1
1015  */
1016 static int mh_closedir(vfs_handle_struct *handle,
1017                 DIR *dirp)
1018 {
1019         DIR *realdirp = ((mh_dirinfo_struct*)dirp)->dirstream;
1020
1021         DEBUG(MH_INFO_DEBUG, ("Entering mh_closedir\n"));
1022         // Will this talloc_free destroy realdirp?
1023         TALLOC_FREE(dirp);
1024
1025         DEBUG(MH_INFO_DEBUG, ("Leaving mh_closedir\n"));
1026         return SMB_VFS_NEXT_CLOSEDIR(handle, realdirp);
1027 }
1028
1029 /*
1030  * Success: return non-negative file descriptor
1031  * Failure: set errno, return -1
1032  */
1033 static int mh_openat(struct vfs_handle_struct *handle,
1034                      const struct files_struct *dirfsp,
1035                      const struct smb_filename *smb_fname,
1036                      files_struct *fsp,
1037                      const struct vfs_open_how *how)
1038 {
1039         int ret;
1040         struct smb_filename *clientFname;
1041         TALLOC_CTX *ctx;
1042
1043         DEBUG(MH_INFO_DEBUG, ("Entering with smb_fname->base_name '%s'\n",
1044                               smb_fname->base_name));
1045
1046         if (!is_in_media_files(smb_fname->base_name)) {
1047                 ret = SMB_VFS_NEXT_OPENAT(handle,
1048                                           dirfsp,
1049                                           smb_fname,
1050                                           fsp,
1051                                           how);
1052                 goto out;
1053         }
1054
1055         clientFname = NULL;
1056         ctx = talloc_tos();
1057
1058         if (alloc_get_client_smb_fname(handle, ctx, smb_fname, &clientFname)) {
1059                 ret = -1;
1060                 goto err;
1061         }
1062
1063         /*
1064          * What about fsp->fsp_name? We also have to get correct stat info into
1065          * fsp and smb_fname for DB files, don't we?
1066          */
1067
1068         DEBUG(MH_INFO_DEBUG, ("Leaving with smb_fname->base_name '%s' "
1069                               "smb_fname->st.st_ex_mtime %s"
1070                               " fsp->fsp_name->st.st_ex_mtime %s",
1071                               smb_fname->base_name,
1072                               ctime(&(smb_fname->st.st_ex_mtime.tv_sec)),
1073                               ctime(&(fsp->fsp_name->st.st_ex_mtime.tv_sec))));
1074
1075         ret = SMB_VFS_NEXT_OPENAT(handle, dirfsp, clientFname, fsp, how);
1076 err:
1077         TALLOC_FREE(clientFname);
1078 out:
1079         DEBUG(MH_INFO_DEBUG, ("Leaving with smb_fname->base_name '%s'\n",
1080                                 smb_fname->base_name));
1081         return ret;
1082 }
1083
1084 /*
1085  * Success: return non-negative file descriptor
1086  * Failure: set errno, return -1
1087  */
1088 static NTSTATUS mh_create_file(vfs_handle_struct *handle,
1089                 struct smb_request *req,
1090                 struct files_struct *dirfsp,
1091                 struct smb_filename *smb_fname,
1092                 uint32_t access_mask,
1093                 uint32_t share_access,
1094                 uint32_t create_disposition,
1095                 uint32_t create_options,
1096                 uint32_t file_attributes,
1097                 uint32_t oplock_request,
1098                 const struct smb2_lease *lease,
1099                 uint64_t allocation_size,
1100                 uint32_t private_flags,
1101                 struct security_descriptor *sd,
1102                 struct ea_list *ea_list,
1103                 files_struct **result_fsp,
1104                 int *pinfo,
1105                 const struct smb2_create_blobs *in_context_blobs,
1106                 struct smb2_create_blobs *out_context_blobs)
1107 {
1108         NTSTATUS status;
1109         struct smb_filename *clientFname;
1110         TALLOC_CTX *ctx;
1111
1112
1113         DEBUG(MH_INFO_DEBUG, ("Entering with smb_fname->base_name '%s'\n",
1114                                 smb_fname->base_name));
1115         if (!is_in_media_files(smb_fname->base_name))
1116         {
1117                 status = SMB_VFS_NEXT_CREATE_FILE(
1118                         handle,
1119                         req,
1120                         dirfsp,
1121                         smb_fname,
1122                         access_mask,
1123                         share_access,
1124                         create_disposition,
1125                         create_options,
1126                         file_attributes,
1127                         oplock_request,
1128                         lease,
1129                         allocation_size,
1130                         private_flags,
1131                         sd,
1132                         ea_list,
1133                         result_fsp,
1134                         pinfo,
1135                         in_context_blobs,
1136                         out_context_blobs);
1137                 goto out;
1138         }
1139
1140         clientFname = NULL;
1141         ctx = talloc_tos();
1142
1143         if (alloc_get_client_smb_fname(handle, ctx,
1144                                 smb_fname,
1145                                 &clientFname))
1146         {
1147                 status = map_nt_error_from_unix(errno);
1148                 goto err;
1149         }
1150
1151         /* This only creates files, so we don't have to worry about
1152          * our fake directory stat'ing here.
1153          */
1154         // But we still need to route stat calls for DB files
1155         // properly, right?
1156         status = SMB_VFS_NEXT_CREATE_FILE(
1157                 handle,
1158                 req,
1159                 dirfsp,
1160                 clientFname,
1161                 access_mask,
1162                 share_access,
1163                 create_disposition,
1164                 create_options,
1165                 file_attributes,
1166                 oplock_request,
1167                 lease,
1168                 allocation_size,
1169                 private_flags,
1170                 sd,
1171                 ea_list,
1172                 result_fsp,
1173                 pinfo,
1174                 in_context_blobs,
1175                 out_context_blobs);
1176 err:
1177         TALLOC_FREE(clientFname);
1178 out:
1179         DEBUG(MH_INFO_DEBUG, ("Leaving with smb_fname->base_name '%s'"
1180                 "smb_fname->st.st_ex_mtime %s"
1181                 "               fsp->fsp_name->st.st_ex_mtime %s",
1182                 smb_fname->base_name,
1183                 ctime(&(smb_fname->st.st_ex_mtime.tv_sec)),
1184                 (*result_fsp) && VALID_STAT((*result_fsp)->fsp_name->st) ?
1185                 ctime(&((*result_fsp)->fsp_name->st.st_ex_mtime.tv_sec)) :
1186                 "No fsp time\n"));
1187         return status;
1188 }
1189
1190 /*
1191  * Success: return 0
1192  * Failure: set errno, return -1
1193  */
1194 static int mh_renameat(vfs_handle_struct *handle,
1195                 files_struct *srcfsp,
1196                 const struct smb_filename *smb_fname_src,
1197                 files_struct *dstfsp,
1198                 const struct smb_filename *smb_fname_dst)
1199 {
1200         int status = -1;
1201         struct smb_filename *full_fname_src = NULL;
1202         struct smb_filename *full_fname_dst = NULL;
1203         struct smb_filename *srcClientFname = NULL;
1204         struct smb_filename *dstClientFname = NULL;
1205
1206         DEBUG(MH_INFO_DEBUG, ("Entering with "
1207                               "smb_fname_src->base_name '%s', "
1208                               "smb_fname_dst->base_name '%s'\n",
1209                               smb_fname_src->base_name,
1210                               smb_fname_dst->base_name));
1211
1212         if (!is_in_media_files(smb_fname_src->base_name)
1213                                 &&
1214                         !is_in_media_files(smb_fname_dst->base_name))
1215         {
1216                 status = SMB_VFS_NEXT_RENAMEAT(handle,
1217                                 srcfsp,
1218                                 smb_fname_src,
1219                                 dstfsp,
1220                                 smb_fname_dst);
1221                 goto out;
1222         }
1223
1224         full_fname_src = full_path_from_dirfsp_atname(talloc_tos(),
1225                                                       srcfsp,
1226                                                       smb_fname_src);
1227         if (full_fname_src == NULL) {
1228                 errno = ENOMEM;
1229                 goto out;
1230         }
1231         full_fname_dst = full_path_from_dirfsp_atname(talloc_tos(),
1232                                                       dstfsp,
1233                                                       smb_fname_dst);
1234         if (full_fname_dst == NULL) {
1235                 errno = ENOMEM;
1236                 goto out;
1237         }
1238
1239         if ((status = alloc_get_client_smb_fname(handle,
1240                                 talloc_tos(),
1241                                 full_fname_src,
1242                                 &srcClientFname)))
1243         {
1244                 goto err;
1245         }
1246
1247         if ((status = alloc_get_client_smb_fname(handle,
1248                                 talloc_tos(),
1249                                 full_fname_dst,
1250                                 &dstClientFname)))
1251         {
1252                 goto err;
1253         }
1254
1255         status = SMB_VFS_NEXT_RENAMEAT(handle,
1256                                 srcfsp->conn->cwd_fsp,
1257                                 srcClientFname,
1258                                 dstfsp->conn->cwd_fsp,
1259                                 dstClientFname);
1260 err:
1261         TALLOC_FREE(full_fname_src);
1262         TALLOC_FREE(full_fname_dst);
1263         TALLOC_FREE(dstClientFname);
1264         TALLOC_FREE(srcClientFname);
1265 out:
1266         DEBUG(MH_INFO_DEBUG, ("Leaving with smb_fname_src->base_name '%s',"
1267                                 " smb_fname_dst->base_name '%s'\n",
1268                                 smb_fname_src->base_name,
1269                                 smb_fname_dst->base_name));
1270         return status;
1271 }
1272
1273 /*
1274  * Success: return 0
1275  * Failure: set errno, return -1
1276  */
1277 static int mh_stat(vfs_handle_struct *handle,
1278                 struct smb_filename *smb_fname)
1279 {
1280         int status = 0;
1281         struct smb_filename *clientFname;
1282         TALLOC_CTX *ctx;
1283
1284
1285         DEBUG(MH_INFO_DEBUG, ("Entering with smb_fname->base_name '%s'\n",
1286                               smb_fname->base_name));
1287
1288         if (!is_in_media_files(smb_fname->base_name))
1289         {
1290                 status = SMB_VFS_NEXT_STAT(handle, smb_fname);
1291                 goto out;
1292         }
1293
1294         clientFname = NULL;
1295         ctx = talloc_tos();
1296
1297         if ((status = alloc_get_client_smb_fname(handle, ctx,
1298                                 smb_fname,
1299                                 &clientFname)))
1300         {
1301                 goto err;
1302         }
1303         DEBUG(MH_INFO_DEBUG, ("Stat'ing clientFname->base_name '%s'\n",
1304                                 clientFname->base_name));
1305         if ((status = SMB_VFS_NEXT_STAT(handle, clientFname)))
1306         {
1307                 goto err;
1308         }
1309         if ((status = set_fake_mtime(handle, ctx, &clientFname, sys_stat)))
1310         {
1311                 goto err;
1312         }
1313
1314         /* Unlike functions with const smb_filename, we have to
1315          * modify smb_fname itself to pass our info back up.
1316          */
1317         DEBUG(MH_INFO_DEBUG, ("Setting smb_fname '%s' stat "
1318                                 "from clientFname '%s'\n",
1319                                 smb_fname->base_name,
1320                                 clientFname->base_name));
1321         smb_fname->st = clientFname->st;
1322 err:
1323         TALLOC_FREE(clientFname);
1324 out:
1325         DEBUG(MH_INFO_DEBUG, ("Leaving with smb_fname->st.st_ex_mtime %s",
1326                         ctime(&(smb_fname->st.st_ex_mtime.tv_sec))));
1327         return status;
1328 }
1329
1330 /*
1331  * Success: return 0
1332  * Failure: set errno, return -1
1333  */
1334 static int mh_lstat(vfs_handle_struct *handle,
1335                 struct smb_filename *smb_fname)
1336 {
1337         int status = 0;
1338         struct smb_filename *clientFname;
1339         TALLOC_CTX *ctx;
1340
1341         DEBUG(MH_INFO_DEBUG, ("Entering with smb_fname->base_name '%s'\n",
1342                               smb_fname->base_name));
1343
1344         if (!is_in_media_files(smb_fname->base_name))
1345         {
1346                 status = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1347                 goto out;
1348         }
1349
1350         clientFname = NULL;
1351         ctx = talloc_tos();
1352
1353         if ((status = alloc_get_client_smb_fname(handle, ctx,
1354                                 smb_fname,
1355                                 &clientFname)))
1356         {
1357                 goto err;
1358         }
1359         if ((status = SMB_VFS_NEXT_LSTAT(handle, clientFname)))
1360         {
1361                 goto err;
1362         }
1363
1364         if ((status = set_fake_mtime(handle, ctx, &clientFname, sys_lstat)))
1365         {
1366                 goto err;
1367         }
1368         /* Unlike functions with const smb_filename, we have to
1369          * modify smb_fname itself to pass our info back up.
1370          */
1371         smb_fname->st = clientFname->st;
1372 err:
1373         TALLOC_FREE(clientFname);
1374 out:
1375         DEBUG(MH_INFO_DEBUG, ("Leaving with smb_fname->st.st_ex_mtime %s",
1376                         ctime(&(smb_fname->st.st_ex_mtime.tv_sec))));
1377         return status;
1378 }
1379
1380 /*
1381  * Success: return 0
1382  * Failure: set errno, return -1
1383  */
1384 static int mh_fstat(vfs_handle_struct *handle,
1385                 files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1386 {
1387         int status = 0;
1388
1389         DEBUG(MH_INFO_DEBUG, ("Entering with fsp->fsp_name->base_name "
1390                                 "'%s'\n", fsp_str_dbg(fsp)));
1391
1392         if ((status = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf)))
1393         {
1394                 goto out;
1395         }
1396
1397         if (fsp->fsp_name == NULL
1398                         || !is_in_media_files(fsp->fsp_name->base_name))
1399         {
1400                 goto out;
1401         }
1402
1403         if ((status = mh_stat(handle, fsp->fsp_name)))
1404         {
1405                 goto out;
1406         }
1407
1408         *sbuf = fsp->fsp_name->st;
1409 out:
1410         DEBUG(MH_INFO_DEBUG, ("Leaving with fsp->fsp_name->st.st_ex_mtime "
1411                         "%s",
1412                         fsp->fsp_name != NULL ?
1413                                 ctime(&(fsp->fsp_name->st.st_ex_mtime.tv_sec)) :
1414                                 "0"));
1415         return status;
1416 }
1417
1418 /*
1419  * Success: return 0
1420  * Failure: set errno, return -1
1421  */
1422 static int mh_unlinkat(vfs_handle_struct *handle,
1423                 struct files_struct *dirfsp,
1424                 const struct smb_filename *smb_fname,
1425                 int flags)
1426 {
1427         int status;
1428         struct smb_filename *full_fname = NULL;
1429         struct smb_filename *clientFname;
1430         TALLOC_CTX *ctx;
1431
1432         DEBUG(MH_INFO_DEBUG, ("Entering mh_unlinkat\n"));
1433         if (!is_in_media_files(smb_fname->base_name)) {
1434                 status = SMB_VFS_NEXT_UNLINKAT(handle,
1435                                 dirfsp,
1436                                 smb_fname,
1437                                 flags);
1438                 goto out;
1439         }
1440
1441         clientFname = NULL;
1442         ctx = talloc_tos();
1443
1444         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1445                                                   dirfsp,
1446                                                   smb_fname);
1447         if (full_fname == NULL) {
1448                 return -1;
1449         }
1450
1451         if ((status = alloc_get_client_smb_fname(handle, ctx,
1452                                 full_fname,
1453                                 &clientFname))) {
1454                 goto err;
1455         }
1456
1457         status = SMB_VFS_NEXT_UNLINKAT(handle,
1458                                 dirfsp->conn->cwd_fsp,
1459                                 clientFname,
1460                                 flags);
1461 err:
1462         TALLOC_FREE(full_fname);
1463         TALLOC_FREE(clientFname);
1464 out:
1465         return status;
1466 }
1467
1468 /*
1469  * Success: return 0
1470  * Failure: set errno, return -1
1471  */
1472 static int mh_lchown(vfs_handle_struct *handle,
1473                 const struct smb_filename *smb_fname,
1474                 uid_t uid,
1475                 gid_t gid)
1476 {
1477         int status;
1478         struct smb_filename *clientFname = NULL;
1479
1480         DEBUG(MH_INFO_DEBUG, ("Entering mh_lchown\n"));
1481         if (!is_in_media_files(smb_fname->base_name))
1482         {
1483                 status = SMB_VFS_NEXT_LCHOWN(handle, smb_fname, uid, gid);
1484                 goto out;
1485         }
1486
1487         status = alloc_get_client_smb_fname(handle,
1488                                 talloc_tos(),
1489                                 smb_fname,
1490                                 &clientFname);
1491         if (status != 0) {
1492                 goto err;
1493         }
1494
1495         status = SMB_VFS_NEXT_LCHOWN(handle, clientFname, uid, gid);
1496 err:
1497         TALLOC_FREE(clientFname);
1498 out:
1499         return status;
1500 }
1501
1502 /*
1503  * Success: return 0
1504  * Failure: set errno, return -1
1505  */
1506 static int mh_chdir(vfs_handle_struct *handle,
1507                 const struct smb_filename *smb_fname)
1508 {
1509         int status;
1510         struct smb_filename *clientFname = NULL;
1511
1512         DEBUG(MH_INFO_DEBUG, ("Entering mh_chdir\n"));
1513         if (!is_in_media_files(smb_fname->base_name)) {
1514                 status = SMB_VFS_NEXT_CHDIR(handle, smb_fname);
1515                 goto out;
1516         }
1517
1518         status = alloc_get_client_smb_fname(handle,
1519                                 talloc_tos(),
1520                                 smb_fname,
1521                                 &clientFname);
1522         if (status != 0) {
1523                 goto err;
1524         }
1525
1526         status = SMB_VFS_NEXT_CHDIR(handle, clientFname);
1527 err:
1528         TALLOC_FREE(clientFname);
1529 out:
1530         return status;
1531 }
1532
1533 /*
1534  * Success: return 0
1535  * Failure: set errno, return -1
1536  */
1537
1538 static int mh_symlinkat(vfs_handle_struct *handle,
1539                 const struct smb_filename *link_contents,
1540                 struct files_struct *dirfsp,
1541                 const struct smb_filename *new_smb_fname)
1542 {
1543         int status = -1;
1544         struct smb_filename *full_fname = NULL;
1545         struct smb_filename *new_link_target = NULL;
1546         struct smb_filename *newclientFname = NULL;
1547
1548         DEBUG(MH_INFO_DEBUG, ("Entering mh_symlinkat\n"));
1549
1550         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1551                                                 dirfsp,
1552                                                 new_smb_fname);
1553         if (full_fname == NULL) {
1554                 status = -1;
1555                 goto err;
1556         }
1557
1558         if (!is_in_media_files(link_contents->base_name) &&
1559                         !is_in_media_files(full_fname->base_name)) {
1560                 status = SMB_VFS_NEXT_SYMLINKAT(handle,
1561                                 link_contents,
1562                                 dirfsp,
1563                                 new_smb_fname);
1564                 goto out;
1565         }
1566
1567         if ((status = alloc_get_client_smb_fname(handle, talloc_tos(),
1568                                 link_contents,
1569                                 &new_link_target))) {
1570                 goto err;
1571         }
1572         if ((status = alloc_get_client_smb_fname(handle, talloc_tos(),
1573                                 full_fname,
1574                                 &newclientFname))) {
1575                 goto err;
1576         }
1577
1578         status = SMB_VFS_NEXT_SYMLINKAT(handle,
1579                                 new_link_target,
1580                                 handle->conn->cwd_fsp,
1581                                 newclientFname);
1582 err:
1583         TALLOC_FREE(new_link_target);
1584         TALLOC_FREE(newclientFname);
1585 out:
1586         TALLOC_FREE(full_fname);
1587         return status;
1588 }
1589
1590 /*
1591  * Success: return byte count
1592  * Failure: set errno, return -1
1593  */
1594 static int mh_readlinkat(vfs_handle_struct *handle,
1595                 const struct files_struct *dirfsp,
1596                 const struct smb_filename *smb_fname,
1597                 char *buf,
1598                 size_t bufsiz)
1599 {
1600         int status;
1601         struct smb_filename *full_fname = NULL;
1602         struct smb_filename *clientFname = NULL;
1603
1604         DEBUG(MH_INFO_DEBUG, ("Entering mh_readlinkat\n"));
1605         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1606                                                 dirfsp,
1607                                                 smb_fname);
1608         if (full_fname == NULL) {
1609                 status = -1;
1610                 goto err;
1611         }
1612
1613         if (!is_in_media_files(full_fname->base_name)) {
1614                 status = SMB_VFS_NEXT_READLINKAT(handle,
1615                                 dirfsp,
1616                                 smb_fname,
1617                                 buf,
1618                                 bufsiz);
1619                 goto out;
1620         }
1621
1622         if ((status = alloc_get_client_smb_fname(handle, talloc_tos(),
1623                                 full_fname,
1624                                 &clientFname))) {
1625                 goto err;
1626         }
1627
1628         status = SMB_VFS_NEXT_READLINKAT(handle,
1629                                 handle->conn->cwd_fsp,
1630                                 clientFname,
1631                                 buf,
1632                                 bufsiz);
1633
1634 err:
1635         TALLOC_FREE(clientFname);
1636 out:
1637         TALLOC_FREE(full_fname);
1638         return status;
1639 }
1640
1641 /*
1642  * Success: return 0
1643  * Failure: set errno, return -1
1644  */
1645 static int mh_linkat(vfs_handle_struct *handle,
1646                 files_struct *srcfsp,
1647                 const struct smb_filename *old_smb_fname,
1648                 files_struct *dstfsp,
1649                 const struct smb_filename *new_smb_fname,
1650                 int flags)
1651 {
1652         int status;
1653         struct smb_filename *old_full_fname = NULL;
1654         struct smb_filename *oldclientFname = NULL;
1655         struct smb_filename *new_full_fname = NULL;
1656         struct smb_filename *newclientFname = NULL;
1657
1658         DEBUG(MH_INFO_DEBUG, ("Entering mh_linkat\n"));
1659
1660         old_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1661                                                 srcfsp,
1662                                                 old_smb_fname);
1663         if (old_full_fname == NULL) {
1664                 status = -1;
1665                 goto err;
1666         }
1667
1668         new_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1669                                                 dstfsp,
1670                                                 new_smb_fname);
1671         if (new_full_fname == NULL) {
1672                 status = -1;
1673                 goto err;
1674         }
1675
1676         if (!is_in_media_files(old_full_fname->base_name) &&
1677                         !is_in_media_files(new_full_fname->base_name)) {
1678                 TALLOC_FREE(old_full_fname);
1679                 TALLOC_FREE(new_full_fname);
1680
1681                 status = SMB_VFS_NEXT_LINKAT(handle,
1682                                 srcfsp,
1683                                 old_smb_fname,
1684                                 dstfsp,
1685                                 new_smb_fname,
1686                                 flags);
1687                 goto out;
1688         }
1689
1690         if ((status = alloc_get_client_smb_fname(handle, talloc_tos(),
1691                                 old_full_fname,
1692                                 &oldclientFname))) {
1693                 goto err;
1694         }
1695         if ((status = alloc_get_client_smb_fname(handle, talloc_tos(),
1696                                 new_full_fname,
1697                                 &newclientFname))) {
1698                 goto err;
1699         }
1700
1701         status = SMB_VFS_NEXT_LINKAT(handle,
1702                                 handle->conn->cwd_fsp,
1703                                 oldclientFname,
1704                                 handle->conn->cwd_fsp,
1705                                 newclientFname,
1706                                 flags);
1707
1708 err:
1709         TALLOC_FREE(old_full_fname);
1710         TALLOC_FREE(new_full_fname);
1711         TALLOC_FREE(newclientFname);
1712         TALLOC_FREE(oldclientFname);
1713 out:
1714         return status;
1715 }
1716
1717 /*
1718  * Success: return 0
1719  * Failure: set errno, return -1
1720  */
1721 static int mh_mknodat(vfs_handle_struct *handle,
1722                 files_struct *dirfsp,
1723                 const struct smb_filename *smb_fname,
1724                 mode_t mode,
1725                 SMB_DEV_T dev)
1726 {
1727         int status;
1728         struct smb_filename *full_fname = NULL;
1729         struct smb_filename *clientFname = NULL;
1730         TALLOC_CTX *ctx;
1731
1732         DEBUG(MH_INFO_DEBUG, ("Entering mh_mknodat\n"));
1733
1734         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1735                                                 dirfsp,
1736                                                 smb_fname);
1737         if (full_fname == NULL) {
1738                 status = -1;
1739                 goto err;
1740         }
1741
1742         if (!is_in_media_files(full_fname->base_name)) {
1743                 status = SMB_VFS_NEXT_MKNODAT(handle,
1744                                 dirfsp,
1745                                 smb_fname,
1746                                 mode,
1747                                 dev);
1748                 goto out;
1749         }
1750
1751         ctx = talloc_tos();
1752
1753         if ((status = alloc_get_client_smb_fname(handle, ctx,
1754                                 full_fname,
1755                                 &clientFname))) {
1756                 goto err;
1757         }
1758
1759         status = SMB_VFS_NEXT_MKNODAT(handle,
1760                         handle->conn->cwd_fsp,
1761                         clientFname,
1762                         mode,
1763                         dev);
1764
1765 err:
1766         TALLOC_FREE(clientFname);
1767 out:
1768         TALLOC_FREE(full_fname);
1769         return status;
1770 }
1771
1772 /*
1773  * Success: return path pointer
1774  * Failure: set errno, return NULL pointer
1775  */
1776 static struct smb_filename *mh_realpath(vfs_handle_struct *handle,
1777                                 TALLOC_CTX *ctx,
1778                                 const struct smb_filename *smb_fname)
1779 {
1780         struct smb_filename *result_fname = NULL;
1781         struct smb_filename *clientFname = NULL;
1782
1783         DEBUG(MH_INFO_DEBUG, ("Entering mh_realpath\n"));
1784         if (!is_in_media_files(smb_fname->base_name)) {
1785                 return SMB_VFS_NEXT_REALPATH(handle, ctx, smb_fname);
1786         }
1787
1788         if (alloc_get_client_smb_fname(handle, ctx,
1789                                 smb_fname,
1790                                 &clientFname) != 0) {
1791                 goto err;
1792         }
1793
1794         result_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, clientFname);
1795 err:
1796         TALLOC_FREE(clientFname);
1797         return result_fname;
1798 }
1799
1800 /* Ignoring get_real_filename function because the default
1801  * doesn't do anything.
1802  */
1803
1804 /*
1805  * Success: return 0
1806  * Failure: set errno, return -1
1807  * In this case, "name" is an attr name.
1808  */
1809
1810 /* VFS operations structure */
1811
1812 static struct vfs_fn_pointers vfs_mh_fns = {
1813         /* Disk operations */
1814
1815         .statvfs_fn = mh_statvfs,
1816
1817         /* Directory operations */
1818
1819         .fdopendir_fn = mh_fdopendir,
1820         .readdir_fn = mh_readdir,
1821         .rewind_dir_fn = mh_rewinddir,
1822         .mkdirat_fn = mh_mkdirat,
1823         .closedir_fn = mh_closedir,
1824
1825         /* File operations */
1826
1827         .openat_fn = mh_openat,
1828         .create_file_fn = mh_create_file,
1829         .renameat_fn = mh_renameat,
1830         .stat_fn = mh_stat,
1831         .lstat_fn = mh_lstat,
1832         .fstat_fn = mh_fstat,
1833         .unlinkat_fn = mh_unlinkat,
1834         .lchown_fn = mh_lchown,
1835         .chdir_fn = mh_chdir,
1836         .symlinkat_fn = mh_symlinkat,
1837         .readlinkat_fn = mh_readlinkat,
1838         .linkat_fn = mh_linkat,
1839         .mknodat_fn = mh_mknodat,
1840         .realpath_fn = mh_realpath,
1841
1842         /* EA operations. */
1843         .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
1844         .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
1845
1846         /* aio operations */
1847 };
1848
1849 static_decl_vfs;
1850 NTSTATUS vfs_media_harmony_init(TALLOC_CTX *ctx)
1851 {
1852         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1853                                 "media_harmony", &vfs_mh_fns);
1854         if (!NT_STATUS_IS_OK(ret))
1855         {
1856                 goto out;
1857         }
1858
1859         vfs_mh_debug_level = debug_add_class("media_harmony");
1860
1861         if (vfs_mh_debug_level == -1) {
1862                 vfs_mh_debug_level = DBGC_VFS;
1863                 DEBUG(1, ("media_harmony_init: Couldn't register custom "
1864                                 "debugging class.\n"));
1865         } else {
1866                 DEBUG(3, ("media_harmony_init: Debug class number of "
1867                                 "'media_harmony': %d\n",
1868                                 vfs_mh_debug_level));
1869         }
1870
1871 out:
1872         return ret;
1873 }