smbd: add twrp arg to synthetic_smb_fname()
[samba.git] / source3 / modules / vfs_shadow_copy.c
1 /* 
2  * implementation of an Shadow Copy module
3  *
4  * Copyright (C) Stefan Metzmacher      2003-2004
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *  
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *  
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "smbd/smbd.h"
22 #include "ntioctl.h"
23
24 /*
25     Please read the VFS module Samba-HowTo-Collection.
26     there's a chapter about this module
27
28     For this share
29     Z:\
30
31     the ShadowCopies are in this directories
32
33     Z:\@GMT-2003.08.05-12.00.00\
34     Z:\@GMT-2003.08.05-12.01.00\
35     Z:\@GMT-2003.08.05-12.02.00\
36
37     e.g.
38     
39     Z:\testfile.txt
40     Z:\@GMT-2003.08.05-12.02.00\testfile.txt
41
42     or:
43
44     Z:\testdir\testfile.txt
45     Z:\@GMT-2003.08.05-12.02.00\testdir\testfile.txt
46
47
48     Note: Files must differ to be displayed via Windows Explorer!
49           Directories are always displayed...    
50 */
51
52 static int vfs_shadow_copy_debug_level = DBGC_VFS;
53
54 #undef DBGC_CLASS
55 #define DBGC_CLASS vfs_shadow_copy_debug_level
56
57 #define SHADOW_COPY_PREFIX "@GMT-"
58 #define SHADOW_COPY_SAMPLE "@GMT-2004.02.18-15.44.00"
59
60 typedef struct {
61         int pos;
62         int num;
63         struct dirent *dirs;
64 } shadow_copy_Dir;
65
66 static bool shadow_copy_match_name(const char *name)
67 {
68         if (strncmp(SHADOW_COPY_PREFIX,name, sizeof(SHADOW_COPY_PREFIX)-1)==0 &&
69                 (strlen(SHADOW_COPY_SAMPLE) == strlen(name))) {
70                 return True;
71         }
72
73         return False;
74 }
75
76 static DIR *shadow_copy_fdopendir(vfs_handle_struct *handle, files_struct *fsp, const char *mask, uint32_t attr)
77 {
78         shadow_copy_Dir *dirp;
79         DIR *p = SMB_VFS_NEXT_FDOPENDIR(handle,fsp,mask,attr);
80
81         if (!p) {
82                 DEBUG(10,("shadow_copy_opendir: SMB_VFS_NEXT_FDOPENDIR() failed for [%s]\n",
83                         smb_fname_str_dbg(fsp->fsp_name)));
84                 return NULL;
85         }
86
87         dirp = SMB_MALLOC_P(shadow_copy_Dir);
88         if (!dirp) {
89                 DEBUG(0,("shadow_copy_fdopendir: Out of memory\n"));
90                 SMB_VFS_NEXT_CLOSEDIR(handle,p);
91                 /* We have now closed the fd in fsp. */
92                 fsp->fh->fd = -1;
93                 return NULL;
94         }
95
96         ZERO_STRUCTP(dirp);
97
98         while (True) {
99                 struct dirent *d;
100
101                 d = SMB_VFS_NEXT_READDIR(handle, p, NULL);
102                 if (d == NULL) {
103                         break;
104                 }
105
106                 if (shadow_copy_match_name(d->d_name)) {
107                         DEBUG(8,("shadow_copy_fdopendir: hide [%s]\n",d->d_name));
108                         continue;
109                 }
110
111                 DEBUG(10,("shadow_copy_fdopendir: not hide [%s]\n",d->d_name));
112
113                 dirp->dirs = SMB_REALLOC_ARRAY(dirp->dirs,struct dirent, dirp->num+1);
114                 if (!dirp->dirs) {
115                         DEBUG(0,("shadow_copy_fdopendir: Out of memory\n"));
116                         break;
117                 }
118
119                 dirp->dirs[dirp->num++] = *d;
120         }
121
122         SMB_VFS_NEXT_CLOSEDIR(handle,p);
123         /* We have now closed the fd in fsp. */
124         fsp->fh->fd = -1;
125         return((DIR *)dirp);
126 }
127
128 static struct dirent *shadow_copy_readdir(vfs_handle_struct *handle,
129                                               DIR *_dirp,
130                                               SMB_STRUCT_STAT *sbuf)
131 {
132         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
133
134         if (dirp->pos < dirp->num) {
135                 return &(dirp->dirs[dirp->pos++]);
136         }
137
138         return NULL;
139 }
140
141 static void shadow_copy_seekdir(struct vfs_handle_struct *handle, DIR *_dirp, long offset)
142 {
143         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
144
145         if (offset < dirp->num) {
146                 dirp->pos = offset ;
147         }
148 }
149
150 static long shadow_copy_telldir(struct vfs_handle_struct *handle, DIR *_dirp)
151 {
152         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
153         return( dirp->pos ) ;
154 }
155
156 static void shadow_copy_rewinddir(struct vfs_handle_struct *handle, DIR *_dirp)
157 {
158         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
159         dirp->pos = 0 ;
160 }
161
162 static int shadow_copy_closedir(vfs_handle_struct *handle, DIR *_dirp)
163 {
164         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
165
166         SAFE_FREE(dirp->dirs);
167         SAFE_FREE(dirp);
168  
169         return 0;       
170 }
171
172 static int shadow_copy_get_shadow_copy_data(vfs_handle_struct *handle,
173                                             files_struct *fsp,
174                                             struct shadow_copy_data *shadow_copy_data,
175                                             bool labels)
176 {
177         struct smb_Dir *dir_hnd = NULL;
178         const char *dname = NULL;
179         char *talloced = NULL;
180         long offset = 0;
181         struct smb_filename *smb_fname = synthetic_smb_fname(talloc_tos(),
182                                                 fsp->conn->connectpath,
183                                                 NULL,
184                                                 NULL,
185                                                 0,
186                                                 0);
187         if (smb_fname == NULL) {
188                 errno = ENOMEM;
189                 return -1;
190         }
191
192         dir_hnd = OpenDir(talloc_tos(), handle->conn, smb_fname, NULL, 0);
193         TALLOC_FREE(smb_fname);
194         if (dir_hnd == NULL) {
195                 DBG_ERR("SMB_VFS_NEXT_OPENDIR() failed for [%s]\n",
196                         fsp->conn->connectpath);
197                 return -1;
198         }
199
200         shadow_copy_data->num_volumes = 0;
201         shadow_copy_data->labels = NULL;
202
203         while (True) {
204                 SHADOW_COPY_LABEL *tlabels;
205                 int ret;
206
207                 dname = ReadDirName(dir_hnd, &offset, NULL, &talloced);
208                 if (dname == NULL) {
209                         break;
210                 }
211
212                 /* */
213                 if (!shadow_copy_match_name(dname)) {
214                         DBG_DEBUG("ignore [%s]\n", dname);
215                         TALLOC_FREE(talloced);
216                         continue;
217                 }
218
219                 DBG_DEBUG("not ignore [%s]\n", dname);
220
221                 if (!labels) {
222                         shadow_copy_data->num_volumes++;
223                         TALLOC_FREE(talloced);
224                         continue;
225                 }
226
227                 tlabels = (SHADOW_COPY_LABEL *)TALLOC_REALLOC(shadow_copy_data,
228                                                                         shadow_copy_data->labels,
229                                                                         (shadow_copy_data->num_volumes+1)*sizeof(SHADOW_COPY_LABEL));
230                 if (tlabels == NULL) {
231                         DEBUG(0,("shadow_copy_get_shadow_copy_data: Out of memory\n"));
232                         TALLOC_FREE(talloced);
233                         TALLOC_FREE(dir_hnd);
234                         return -1;
235                 }
236
237                 ret = strlcpy(tlabels[shadow_copy_data->num_volumes], dname,
238                               sizeof(tlabels[shadow_copy_data->num_volumes]));
239                 if (ret != sizeof(tlabels[shadow_copy_data->num_volumes]) - 1) {
240                         DBG_ERR("malformed label %s\n", dname);
241                         TALLOC_FREE(talloced);
242                         TALLOC_FREE(dir_hnd);
243                         return -1;
244                 }
245                 shadow_copy_data->num_volumes++;
246
247                 shadow_copy_data->labels = tlabels;
248                 TALLOC_FREE(talloced);
249         }
250
251         TALLOC_FREE(dir_hnd);
252         return 0;
253 }
254
255 static struct vfs_fn_pointers vfs_shadow_copy_fns = {
256         .fdopendir_fn = shadow_copy_fdopendir,
257         .readdir_fn = shadow_copy_readdir,
258         .seekdir_fn = shadow_copy_seekdir,
259         .telldir_fn = shadow_copy_telldir,
260         .rewind_dir_fn = shadow_copy_rewinddir,
261         .closedir_fn = shadow_copy_closedir,
262         .get_shadow_copy_data_fn = shadow_copy_get_shadow_copy_data,
263 };
264
265 static_decl_vfs;
266 NTSTATUS vfs_shadow_copy_init(TALLOC_CTX *ctx)
267 {
268         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
269                                         "shadow_copy", &vfs_shadow_copy_fns);
270
271         if (!NT_STATUS_IS_OK(ret))
272                 return ret;
273
274         vfs_shadow_copy_debug_level = debug_add_class("shadow_copy");
275         if (vfs_shadow_copy_debug_level == -1) {
276                 vfs_shadow_copy_debug_level = DBGC_VFS;
277                 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
278                         "vfs_shadow_copy_init"));
279         } else {
280                 DEBUG(10, ("%s: Debug class number of '%s': %d\n", 
281                         "vfs_shadow_copy_init","shadow_copy",vfs_shadow_copy_debug_level));
282         }
283
284         return ret;
285 }