s4:dsdb: Fix stack use after scope in gkdi_create_root_key()
[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         SMB_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 SMB_STRUCT_DIR *shadow_copy_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
77 {
78         shadow_copy_Dir *dirp;
79         SMB_STRUCT_DIR *p = SMB_VFS_NEXT_OPENDIR(handle,fname,mask,attr);
80
81         if (!p) {
82                 DEBUG(0,("shadow_copy_opendir: SMB_VFS_NEXT_OPENDIR() failed for [%s]\n",fname));
83                 return NULL;
84         }
85
86         dirp = SMB_MALLOC_P(shadow_copy_Dir);
87         if (!dirp) {
88                 DEBUG(0,("shadow_copy_opendir: Out of memory\n"));
89                 SMB_VFS_NEXT_CLOSEDIR(handle,p);
90                 return NULL;
91         }
92
93         ZERO_STRUCTP(dirp);
94
95         while (True) {
96                 SMB_STRUCT_DIRENT *d;
97
98                 d = SMB_VFS_NEXT_READDIR(handle, p, NULL);
99                 if (d == NULL) {
100                         break;
101                 }
102
103                 if (shadow_copy_match_name(d->d_name)) {
104                         DEBUG(8,("shadow_copy_opendir: hide [%s]\n",d->d_name));
105                         continue;
106                 }
107
108                 DEBUG(10,("shadow_copy_opendir: not hide [%s]\n",d->d_name));
109
110                 dirp->dirs = SMB_REALLOC_ARRAY(dirp->dirs,SMB_STRUCT_DIRENT, dirp->num+1);
111                 if (!dirp->dirs) {
112                         DEBUG(0,("shadow_copy_opendir: Out of memory\n"));
113                         break;
114                 }
115
116                 dirp->dirs[dirp->num++] = *d;
117         }
118
119         SMB_VFS_NEXT_CLOSEDIR(handle,p);
120         return((SMB_STRUCT_DIR *)dirp);
121 }
122
123 static SMB_STRUCT_DIR *shadow_copy_fdopendir(vfs_handle_struct *handle, files_struct *fsp, const char *mask, uint32 attr)
124 {
125         shadow_copy_Dir *dirp;
126         SMB_STRUCT_DIR *p = SMB_VFS_NEXT_FDOPENDIR(handle,fsp,mask,attr);
127
128         if (!p) {
129                 DEBUG(10,("shadow_copy_opendir: SMB_VFS_NEXT_FDOPENDIR() failed for [%s]\n",
130                         smb_fname_str_dbg(fsp->fsp_name)));
131                 return NULL;
132         }
133
134         dirp = SMB_MALLOC_P(shadow_copy_Dir);
135         if (!dirp) {
136                 DEBUG(0,("shadow_copy_fdopendir: Out of memory\n"));
137                 SMB_VFS_NEXT_CLOSEDIR(handle,p);
138                 /* We have now closed the fd in fsp. */
139                 fsp->fh->fd = -1;
140                 return NULL;
141         }
142
143         ZERO_STRUCTP(dirp);
144
145         while (True) {
146                 SMB_STRUCT_DIRENT *d;
147
148                 d = SMB_VFS_NEXT_READDIR(handle, p, NULL);
149                 if (d == NULL) {
150                         break;
151                 }
152
153                 if (shadow_copy_match_name(d->d_name)) {
154                         DEBUG(8,("shadow_copy_fdopendir: hide [%s]\n",d->d_name));
155                         continue;
156                 }
157
158                 DEBUG(10,("shadow_copy_fdopendir: not hide [%s]\n",d->d_name));
159
160                 dirp->dirs = SMB_REALLOC_ARRAY(dirp->dirs,SMB_STRUCT_DIRENT, dirp->num+1);
161                 if (!dirp->dirs) {
162                         DEBUG(0,("shadow_copy_fdopendir: Out of memory\n"));
163                         break;
164                 }
165
166                 dirp->dirs[dirp->num++] = *d;
167         }
168
169         SMB_VFS_NEXT_CLOSEDIR(handle,p);
170         /* We have now closed the fd in fsp. */
171         fsp->fh->fd = -1;
172         return((SMB_STRUCT_DIR *)dirp);
173 }
174
175 static SMB_STRUCT_DIRENT *shadow_copy_readdir(vfs_handle_struct *handle,
176                                               SMB_STRUCT_DIR *_dirp,
177                                               SMB_STRUCT_STAT *sbuf)
178 {
179         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
180
181         if (dirp->pos < dirp->num) {
182                 return &(dirp->dirs[dirp->pos++]);
183         }
184
185         return NULL;
186 }
187
188 static void shadow_copy_seekdir(struct vfs_handle_struct *handle, SMB_STRUCT_DIR *_dirp, long offset)
189 {
190         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
191
192         if (offset < dirp->num) {
193                 dirp->pos = offset ;
194         }
195 }
196
197 static long shadow_copy_telldir(struct vfs_handle_struct *handle, SMB_STRUCT_DIR *_dirp)
198 {
199         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
200         return( dirp->pos ) ;
201 }
202
203 static void shadow_copy_rewinddir(struct vfs_handle_struct *handle, SMB_STRUCT_DIR *_dirp)
204 {
205         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
206         dirp->pos = 0 ;
207 }
208
209 static int shadow_copy_closedir(vfs_handle_struct *handle, SMB_STRUCT_DIR *_dirp)
210 {
211         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
212
213         SAFE_FREE(dirp->dirs);
214         SAFE_FREE(dirp);
215  
216         return 0;       
217 }
218
219 static int shadow_copy_get_shadow_copy_data(vfs_handle_struct *handle, files_struct *fsp, SHADOW_COPY_DATA *shadow_copy_data, bool labels)
220 {
221         SMB_STRUCT_DIR *p = SMB_VFS_NEXT_OPENDIR(handle,fsp->conn->connectpath,NULL,0);
222
223         shadow_copy_data->num_volumes = 0;
224         shadow_copy_data->labels = NULL;
225
226         if (!p) {
227                 DEBUG(0,("shadow_copy_get_shadow_copy_data: SMB_VFS_NEXT_OPENDIR() failed for [%s]\n",fsp->conn->connectpath));
228                 return -1;
229         }
230
231         while (True) {
232                 SHADOW_COPY_LABEL *tlabels;
233                 SMB_STRUCT_DIRENT *d;
234
235                 d = SMB_VFS_NEXT_READDIR(handle, p, NULL);
236                 if (d == NULL) {
237                         break;
238                 }
239
240                 /* */
241                 if (!shadow_copy_match_name(d->d_name)) {
242                         DEBUG(10,("shadow_copy_get_shadow_copy_data: ignore [%s]\n",d->d_name));
243                         continue;
244                 }
245
246                 DEBUG(7,("shadow_copy_get_shadow_copy_data: not ignore [%s]\n",d->d_name));
247
248                 if (!labels) {
249                         shadow_copy_data->num_volumes++;
250                         continue;
251                 }
252
253                 tlabels = (SHADOW_COPY_LABEL *)TALLOC_REALLOC(shadow_copy_data->mem_ctx,
254                                                                         shadow_copy_data->labels,
255                                                                         (shadow_copy_data->num_volumes+1)*sizeof(SHADOW_COPY_LABEL));
256                 if (tlabels == NULL) {
257                         DEBUG(0,("shadow_copy_get_shadow_copy_data: Out of memory\n"));
258                         SMB_VFS_NEXT_CLOSEDIR(handle,p);
259                         return -1;
260                 }
261
262                 snprintf(tlabels[shadow_copy_data->num_volumes++], sizeof(*tlabels), "%s",d->d_name);
263
264                 shadow_copy_data->labels = tlabels;
265         }
266
267         SMB_VFS_NEXT_CLOSEDIR(handle,p);
268         return 0;
269 }
270
271 static struct vfs_fn_pointers vfs_shadow_copy_fns = {
272         .opendir = shadow_copy_opendir,
273         .fdopendir = shadow_copy_fdopendir,
274         .readdir = shadow_copy_readdir,
275         .seekdir = shadow_copy_seekdir,
276         .telldir = shadow_copy_telldir,
277         .rewind_dir = shadow_copy_rewinddir,
278         .closedir = shadow_copy_closedir,
279         .get_shadow_copy_data = shadow_copy_get_shadow_copy_data,
280 };
281
282 NTSTATUS vfs_shadow_copy_init(void);
283 NTSTATUS vfs_shadow_copy_init(void)
284 {
285         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
286                                         "shadow_copy", &vfs_shadow_copy_fns);
287
288         if (!NT_STATUS_IS_OK(ret))
289                 return ret;
290
291         vfs_shadow_copy_debug_level = debug_add_class("shadow_copy");
292         if (vfs_shadow_copy_debug_level == -1) {
293                 vfs_shadow_copy_debug_level = DBGC_VFS;
294                 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
295                         "vfs_shadow_copy_init"));
296         } else {
297                 DEBUG(10, ("%s: Debug class number of '%s': %d\n", 
298                         "vfs_shadow_copy_init","shadow_copy",vfs_shadow_copy_debug_level));
299         }
300
301         return ret;
302 }