1 | /*
|
---|
2 | * implementation of an Shadow Copy module - version 2
|
---|
3 | *
|
---|
4 | * Copyright (C) Andrew Tridgell 2007
|
---|
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 2 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, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "includes.h"
|
---|
22 |
|
---|
23 | /*
|
---|
24 |
|
---|
25 | This is a 2nd implemetation of a shadow copy module for exposing
|
---|
26 | snapshots to windows clients as shadow copies. This version has the
|
---|
27 | following features:
|
---|
28 |
|
---|
29 | 1) you don't need to populate your shares with symlinks to the
|
---|
30 | snapshots. This can be very important when you have thousands of
|
---|
31 | shares, or use [homes]
|
---|
32 |
|
---|
33 | 2) the inode number of the files is altered so it is different
|
---|
34 | from the original. This allows the 'restore' button to work
|
---|
35 | without a sharing violation
|
---|
36 |
|
---|
37 | Module options:
|
---|
38 |
|
---|
39 | shadow:snapdir = <directory where snapshots are kept>
|
---|
40 |
|
---|
41 | This is the directory containing the @GMT-* snapshot directories. If it is an absolute
|
---|
42 | path it is used as-is. If it is a relative path, then it is taken relative to the mount
|
---|
43 | point of the filesystem that the root of this share is on
|
---|
44 |
|
---|
45 | shadow:basedir = <base directory that snapshots are from>
|
---|
46 |
|
---|
47 | This is an optional parameter that specifies the directory that
|
---|
48 | the snapshots are relative to. It defaults to the filesystem
|
---|
49 | mount point
|
---|
50 |
|
---|
51 | shadow:fixinodes = yes/no
|
---|
52 |
|
---|
53 | If you enable shadow:fixinodes then this module will modify the
|
---|
54 | apparent inode number of files in the snapshot directories using
|
---|
55 | a hash of the files path. This is needed for snapshot systems
|
---|
56 | where the snapshots have the same device:inode number as the
|
---|
57 | original files (such as happens with GPFS snapshots). If you
|
---|
58 | don't set this option then the 'restore' button in the shadow
|
---|
59 | copy UI will fail with a sharing violation.
|
---|
60 |
|
---|
61 | Note that the directory names in the snapshot directory must take the form
|
---|
62 | @GMT-YYYY.MM.DD-HH.MM.SS
|
---|
63 |
|
---|
64 | The following command would generate a correctly formatted directory name:
|
---|
65 | date -u +@GMT-%Y.%m.%d-%H.%M.%S
|
---|
66 |
|
---|
67 | */
|
---|
68 |
|
---|
69 | static int vfs_shadow_copy2_debug_level = DBGC_VFS;
|
---|
70 |
|
---|
71 | #undef DBGC_CLASS
|
---|
72 | #define DBGC_CLASS vfs_shadow_copy2_debug_level
|
---|
73 |
|
---|
74 | #define GMT_NAME_LEN 24 /* length of a @GMT- name */
|
---|
75 |
|
---|
76 | /*
|
---|
77 | make very sure it is one of our special names
|
---|
78 | */
|
---|
79 | static inline bool shadow_copy2_match_name(const char *name, const char **gmt_start)
|
---|
80 | {
|
---|
81 | unsigned year, month, day, hr, min, sec;
|
---|
82 | const char *p;
|
---|
83 | if (gmt_start) {
|
---|
84 | (*gmt_start) = NULL;
|
---|
85 | }
|
---|
86 | p = strstr_m(name, "@GMT-");
|
---|
87 | if (p == NULL) return false;
|
---|
88 | if (p > name && p[-1] != '/') return False;
|
---|
89 | if (sscanf(p, "@GMT-%04u.%02u.%02u-%02u.%02u.%02u", &year, &month,
|
---|
90 | &day, &hr, &min, &sec) != 6) {
|
---|
91 | return False;
|
---|
92 | }
|
---|
93 | if (p[24] != 0 && p[24] != '/') {
|
---|
94 | return False;
|
---|
95 | }
|
---|
96 | if (gmt_start) {
|
---|
97 | (*gmt_start) = p;
|
---|
98 | }
|
---|
99 | return True;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /*
|
---|
103 | shadow copy paths can also come into the server in this form:
|
---|
104 |
|
---|
105 | /foo/bar/@GMT-XXXXX/some/file
|
---|
106 |
|
---|
107 | This function normalises the filename to be of the form:
|
---|
108 |
|
---|
109 | @GMT-XXXX/foo/bar/some/file
|
---|
110 | */
|
---|
111 | static const char *shadow_copy2_normalise_path(TALLOC_CTX *mem_ctx, const char *path, const char *gmt_start)
|
---|
112 | {
|
---|
113 | char *pcopy;
|
---|
114 | char buf[GMT_NAME_LEN];
|
---|
115 | size_t prefix_len;
|
---|
116 |
|
---|
117 | if (path == gmt_start) {
|
---|
118 | return path;
|
---|
119 | }
|
---|
120 |
|
---|
121 | prefix_len = gmt_start - path - 1;
|
---|
122 |
|
---|
123 | DEBUG(10, ("path=%s, gmt_start=%s, prefix_len=%d\n", path, gmt_start,
|
---|
124 | (int)prefix_len));
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * We've got a/b/c/@GMT-YYYY.MM.DD-HH.MM.SS/d/e. convert to
|
---|
128 | * @GMT-YYYY.MM.DD-HH.MM.SS/a/b/c/d/e before further
|
---|
129 | * processing. As many VFS calls provide a const char *,
|
---|
130 | * unfortunately we have to make a copy.
|
---|
131 | */
|
---|
132 |
|
---|
133 | pcopy = talloc_strdup(talloc_tos(), path);
|
---|
134 | if (pcopy == NULL) {
|
---|
135 | return NULL;
|
---|
136 | }
|
---|
137 |
|
---|
138 | gmt_start = pcopy + prefix_len;
|
---|
139 |
|
---|
140 | /*
|
---|
141 | * Copy away "@GMT-YYYY.MM.DD-HH.MM.SS"
|
---|
142 | */
|
---|
143 | memcpy(buf, gmt_start+1, GMT_NAME_LEN);
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * Make space for it including a trailing /
|
---|
147 | */
|
---|
148 | memmove(pcopy + GMT_NAME_LEN + 1, pcopy, prefix_len);
|
---|
149 |
|
---|
150 | /*
|
---|
151 | * Move in "@GMT-YYYY.MM.DD-HH.MM.SS/" at the beginning again
|
---|
152 | */
|
---|
153 | memcpy(pcopy, buf, GMT_NAME_LEN);
|
---|
154 | pcopy[GMT_NAME_LEN] = '/';
|
---|
155 |
|
---|
156 | DEBUG(10, ("shadow_copy2_normalise_path: %s -> %s\n", path, pcopy));
|
---|
157 |
|
---|
158 | return pcopy;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /*
|
---|
162 | convert a name to the shadow directory
|
---|
163 | */
|
---|
164 |
|
---|
165 | #define _SHADOW2_NEXT(op, args, rtype, eret, extra) do { \
|
---|
166 | const char *name = fname; \
|
---|
167 | const char *gmt_start; \
|
---|
168 | if (shadow_copy2_match_name(fname, &gmt_start)) { \
|
---|
169 | char *name2; \
|
---|
170 | rtype ret; \
|
---|
171 | name2 = convert_shadow2_name(handle, fname, gmt_start); \
|
---|
172 | if (name2 == NULL) { \
|
---|
173 | errno = EINVAL; \
|
---|
174 | return eret; \
|
---|
175 | } \
|
---|
176 | name = name2; \
|
---|
177 | ret = SMB_VFS_NEXT_ ## op args; \
|
---|
178 | talloc_free(name2); \
|
---|
179 | if (ret != eret) extra; \
|
---|
180 | return ret; \
|
---|
181 | } else { \
|
---|
182 | return SMB_VFS_NEXT_ ## op args; \
|
---|
183 | } \
|
---|
184 | } while (0)
|
---|
185 |
|
---|
186 | #define _SHADOW2_NEXT_SMB_FNAME(op, args, rtype, eret, extra) do { \
|
---|
187 | const char *gmt_start; \
|
---|
188 | if (shadow_copy2_match_name(smb_fname->base_name, &gmt_start)) { \
|
---|
189 | char *name2; \
|
---|
190 | char *smb_base_name_tmp = NULL; \
|
---|
191 | rtype ret; \
|
---|
192 | name2 = convert_shadow2_name(handle, smb_fname->base_name, gmt_start); \
|
---|
193 | if (name2 == NULL) { \
|
---|
194 | errno = EINVAL; \
|
---|
195 | return eret; \
|
---|
196 | } \
|
---|
197 | smb_base_name_tmp = smb_fname->base_name; \
|
---|
198 | smb_fname->base_name = name2; \
|
---|
199 | ret = SMB_VFS_NEXT_ ## op args; \
|
---|
200 | smb_fname->base_name = smb_base_name_tmp; \
|
---|
201 | talloc_free(name2); \
|
---|
202 | if (ret != eret) extra; \
|
---|
203 | return ret; \
|
---|
204 | } else { \
|
---|
205 | return SMB_VFS_NEXT_ ## op args; \
|
---|
206 | } \
|
---|
207 | } while (0)
|
---|
208 |
|
---|
209 | /*
|
---|
210 | convert a name to the shadow directory: NTSTATUS-specific handling
|
---|
211 | */
|
---|
212 |
|
---|
213 | #define _SHADOW2_NTSTATUS_NEXT(op, args, eret, extra) do { \
|
---|
214 | const char *name = fname; \
|
---|
215 | const char *gmt_start; \
|
---|
216 | if (shadow_copy2_match_name(fname, &gmt_start)) { \
|
---|
217 | char *name2; \
|
---|
218 | NTSTATUS ret; \
|
---|
219 | name2 = convert_shadow2_name(handle, fname, gmt_start); \
|
---|
220 | if (name2 == NULL) { \
|
---|
221 | errno = EINVAL; \
|
---|
222 | return eret; \
|
---|
223 | } \
|
---|
224 | name = name2; \
|
---|
225 | ret = SMB_VFS_NEXT_ ## op args; \
|
---|
226 | talloc_free(name2); \
|
---|
227 | if (!NT_STATUS_EQUAL(ret, eret)) extra; \
|
---|
228 | return ret; \
|
---|
229 | } else { \
|
---|
230 | return SMB_VFS_NEXT_ ## op args; \
|
---|
231 | } \
|
---|
232 | } while (0)
|
---|
233 |
|
---|
234 | #define SHADOW2_NTSTATUS_NEXT(op, args, eret) _SHADOW2_NTSTATUS_NEXT(op, args, eret, )
|
---|
235 |
|
---|
236 | #define SHADOW2_NEXT(op, args, rtype, eret) _SHADOW2_NEXT(op, args, rtype, eret, )
|
---|
237 |
|
---|
238 | #define SHADOW2_NEXT_SMB_FNAME(op, args, rtype, eret) _SHADOW2_NEXT_SMB_FNAME(op, args, rtype, eret, )
|
---|
239 |
|
---|
240 | #define SHADOW2_NEXT2(op, args) do { \
|
---|
241 | const char *gmt_start1, *gmt_start2; \
|
---|
242 | if (shadow_copy2_match_name(oldname, &gmt_start1) || \
|
---|
243 | shadow_copy2_match_name(newname, &gmt_start2)) { \
|
---|
244 | errno = EROFS; \
|
---|
245 | return -1; \
|
---|
246 | } else { \
|
---|
247 | return SMB_VFS_NEXT_ ## op args; \
|
---|
248 | } \
|
---|
249 | } while (0)
|
---|
250 |
|
---|
251 | #define SHADOW2_NEXT2_SMB_FNAME(op, args) do { \
|
---|
252 | const char *gmt_start1, *gmt_start2; \
|
---|
253 | if (shadow_copy2_match_name(smb_fname_src->base_name, &gmt_start1) || \
|
---|
254 | shadow_copy2_match_name(smb_fname_dst->base_name, &gmt_start2)) { \
|
---|
255 | errno = EROFS; \
|
---|
256 | return -1; \
|
---|
257 | } else { \
|
---|
258 | return SMB_VFS_NEXT_ ## op args; \
|
---|
259 | } \
|
---|
260 | } while (0)
|
---|
261 |
|
---|
262 |
|
---|
263 | /*
|
---|
264 | find the mount point of a filesystem
|
---|
265 | */
|
---|
266 | static char *find_mount_point(TALLOC_CTX *mem_ctx, vfs_handle_struct *handle)
|
---|
267 | {
|
---|
268 | char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
|
---|
269 | dev_t dev;
|
---|
270 | struct stat st;
|
---|
271 | char *p;
|
---|
272 |
|
---|
273 | if (stat(path, &st) != 0) {
|
---|
274 | talloc_free(path);
|
---|
275 | return NULL;
|
---|
276 | }
|
---|
277 |
|
---|
278 | dev = st.st_dev;
|
---|
279 |
|
---|
280 | while ((p = strrchr(path, '/')) && p > path) {
|
---|
281 | *p = 0;
|
---|
282 | if (stat(path, &st) != 0) {
|
---|
283 | talloc_free(path);
|
---|
284 | return NULL;
|
---|
285 | }
|
---|
286 | if (st.st_dev != dev) {
|
---|
287 | *p = '/';
|
---|
288 | break;
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | return path;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /*
|
---|
296 | work out the location of the snapshot for this share
|
---|
297 | */
|
---|
298 | static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx, vfs_handle_struct *handle)
|
---|
299 | {
|
---|
300 | const char *snapdir;
|
---|
301 | char *mount_point;
|
---|
302 | const char *ret;
|
---|
303 |
|
---|
304 | snapdir = lp_parm_const_string(SNUM(handle->conn), "shadow", "snapdir", NULL);
|
---|
305 | if (snapdir == NULL) {
|
---|
306 | return NULL;
|
---|
307 | }
|
---|
308 | /* if its an absolute path, we're done */
|
---|
309 | if (*snapdir == '/') {
|
---|
310 | return snapdir;
|
---|
311 | }
|
---|
312 |
|
---|
313 | /* other its relative to the filesystem mount point */
|
---|
314 | mount_point = find_mount_point(mem_ctx, handle);
|
---|
315 | if (mount_point == NULL) {
|
---|
316 | return NULL;
|
---|
317 | }
|
---|
318 |
|
---|
319 | ret = talloc_asprintf(mem_ctx, "%s/%s", mount_point, snapdir);
|
---|
320 | talloc_free(mount_point);
|
---|
321 | return ret;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /*
|
---|
325 | work out the location of the base directory for snapshots of this share
|
---|
326 | */
|
---|
327 | static const char *shadow_copy2_find_basedir(TALLOC_CTX *mem_ctx, vfs_handle_struct *handle)
|
---|
328 | {
|
---|
329 | const char *basedir = lp_parm_const_string(SNUM(handle->conn), "shadow", "basedir", NULL);
|
---|
330 |
|
---|
331 | /* other its the filesystem mount point */
|
---|
332 | if (basedir == NULL) {
|
---|
333 | basedir = find_mount_point(mem_ctx, handle);
|
---|
334 | }
|
---|
335 |
|
---|
336 | return basedir;
|
---|
337 | }
|
---|
338 |
|
---|
339 | /*
|
---|
340 | convert a filename from a share relative path, to a path in the
|
---|
341 | snapshot directory
|
---|
342 | */
|
---|
343 | static char *convert_shadow2_name(vfs_handle_struct *handle, const char *fname, const char *gmt_path)
|
---|
344 | {
|
---|
345 | TALLOC_CTX *tmp_ctx = talloc_new(handle->data);
|
---|
346 | const char *snapdir, *relpath, *baseoffset, *basedir;
|
---|
347 | size_t baselen;
|
---|
348 | char *ret;
|
---|
349 |
|
---|
350 | snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle);
|
---|
351 | if (snapdir == NULL) {
|
---|
352 | DEBUG(2,("no snapdir found for share at %s\n", handle->conn->connectpath));
|
---|
353 | talloc_free(tmp_ctx);
|
---|
354 | return NULL;
|
---|
355 | }
|
---|
356 |
|
---|
357 | basedir = shadow_copy2_find_basedir(tmp_ctx, handle);
|
---|
358 | if (basedir == NULL) {
|
---|
359 | DEBUG(2,("no basedir found for share at %s\n", handle->conn->connectpath));
|
---|
360 | talloc_free(tmp_ctx);
|
---|
361 | return NULL;
|
---|
362 | }
|
---|
363 |
|
---|
364 | if (strncmp(fname, "@GMT-", 5) != 0) {
|
---|
365 | fname = shadow_copy2_normalise_path(tmp_ctx, fname, gmt_path);
|
---|
366 | if (fname == NULL) {
|
---|
367 | talloc_free(tmp_ctx);
|
---|
368 | return NULL;
|
---|
369 | }
|
---|
370 | }
|
---|
371 |
|
---|
372 | relpath = fname + GMT_NAME_LEN;
|
---|
373 | baselen = strlen(basedir);
|
---|
374 | baseoffset = handle->conn->connectpath + baselen;
|
---|
375 |
|
---|
376 | /* some sanity checks */
|
---|
377 | if (strncmp(basedir, handle->conn->connectpath, baselen) != 0 ||
|
---|
378 | (handle->conn->connectpath[baselen] != 0 && handle->conn->connectpath[baselen] != '/')) {
|
---|
379 | DEBUG(0,("convert_shadow2_name: basedir %s is not a parent of %s\n",
|
---|
380 | basedir, handle->conn->connectpath));
|
---|
381 | talloc_free(tmp_ctx);
|
---|
382 | return NULL;
|
---|
383 | }
|
---|
384 |
|
---|
385 | if (*relpath == '/') relpath++;
|
---|
386 | if (*baseoffset == '/') baseoffset++;
|
---|
387 |
|
---|
388 | ret = talloc_asprintf(handle->data, "%s/%.*s/%s/%s",
|
---|
389 | snapdir,
|
---|
390 | GMT_NAME_LEN, fname,
|
---|
391 | baseoffset,
|
---|
392 | relpath);
|
---|
393 | DEBUG(6,("convert_shadow2_name: '%s' -> '%s'\n", fname, ret));
|
---|
394 | talloc_free(tmp_ctx);
|
---|
395 | return ret;
|
---|
396 | }
|
---|
397 |
|
---|
398 |
|
---|
399 | /*
|
---|
400 | simple string hash
|
---|
401 | */
|
---|
402 | static uint32 string_hash(const char *s)
|
---|
403 | {
|
---|
404 | uint32 n = 0;
|
---|
405 | while (*s) {
|
---|
406 | n = ((n << 5) + n) ^ (uint32)(*s++);
|
---|
407 | }
|
---|
408 | return n;
|
---|
409 | }
|
---|
410 |
|
---|
411 | /*
|
---|
412 | modify a sbuf return to ensure that inodes in the shadow directory
|
---|
413 | are different from those in the main directory
|
---|
414 | */
|
---|
415 | static void convert_sbuf(vfs_handle_struct *handle, const char *fname, SMB_STRUCT_STAT *sbuf)
|
---|
416 | {
|
---|
417 | if (lp_parm_bool(SNUM(handle->conn), "shadow", "fixinodes", False)) {
|
---|
418 | /* some snapshot systems, like GPFS, return the name
|
---|
419 | device:inode for the snapshot files as the current
|
---|
420 | files. That breaks the 'restore' button in the shadow copy
|
---|
421 | GUI, as the client gets a sharing violation.
|
---|
422 |
|
---|
423 | This is a crude way of allowing both files to be
|
---|
424 | open at once. It has a slight chance of inode
|
---|
425 | number collision, but I can't see a better approach
|
---|
426 | without significant VFS changes
|
---|
427 | */
|
---|
428 | uint32_t shash = string_hash(fname) & 0xFF000000;
|
---|
429 | if (shash == 0) {
|
---|
430 | shash = 1;
|
---|
431 | }
|
---|
432 | sbuf->st_ex_ino ^= shash;
|
---|
433 | }
|
---|
434 | }
|
---|
435 |
|
---|
436 | static int shadow_copy2_rename(vfs_handle_struct *handle,
|
---|
437 | const struct smb_filename *smb_fname_src,
|
---|
438 | const struct smb_filename *smb_fname_dst)
|
---|
439 | {
|
---|
440 | SHADOW2_NEXT2_SMB_FNAME(RENAME,
|
---|
441 | (handle, smb_fname_src, smb_fname_dst));
|
---|
442 | }
|
---|
443 |
|
---|
444 | static int shadow_copy2_symlink(vfs_handle_struct *handle,
|
---|
445 | const char *oldname, const char *newname)
|
---|
446 | {
|
---|
447 | SHADOW2_NEXT2(SYMLINK, (handle, oldname, newname));
|
---|
448 | }
|
---|
449 |
|
---|
450 | static int shadow_copy2_link(vfs_handle_struct *handle,
|
---|
451 | const char *oldname, const char *newname)
|
---|
452 | {
|
---|
453 | SHADOW2_NEXT2(LINK, (handle, oldname, newname));
|
---|
454 | }
|
---|
455 |
|
---|
456 | static int shadow_copy2_open(vfs_handle_struct *handle,
|
---|
457 | struct smb_filename *smb_fname, files_struct *fsp,
|
---|
458 | int flags, mode_t mode)
|
---|
459 | {
|
---|
460 | SHADOW2_NEXT_SMB_FNAME(OPEN,
|
---|
461 | (handle, smb_fname, fsp, flags, mode),
|
---|
462 | int, -1);
|
---|
463 | }
|
---|
464 |
|
---|
465 | static SMB_STRUCT_DIR *shadow_copy2_opendir(vfs_handle_struct *handle,
|
---|
466 | const char *fname, const char *mask, uint32 attr)
|
---|
467 | {
|
---|
468 | SHADOW2_NEXT(OPENDIR, (handle, name, mask, attr), SMB_STRUCT_DIR *, NULL);
|
---|
469 | }
|
---|
470 |
|
---|
471 | static int shadow_copy2_stat(vfs_handle_struct *handle,
|
---|
472 | struct smb_filename *smb_fname)
|
---|
473 | {
|
---|
474 | _SHADOW2_NEXT_SMB_FNAME(STAT, (handle, smb_fname), int, -1,
|
---|
475 | convert_sbuf(handle, smb_fname->base_name,
|
---|
476 | &smb_fname->st));
|
---|
477 | }
|
---|
478 |
|
---|
479 | static int shadow_copy2_lstat(vfs_handle_struct *handle,
|
---|
480 | struct smb_filename *smb_fname)
|
---|
481 | {
|
---|
482 | _SHADOW2_NEXT_SMB_FNAME(LSTAT, (handle, smb_fname), int, -1,
|
---|
483 | convert_sbuf(handle, smb_fname->base_name,
|
---|
484 | &smb_fname->st));
|
---|
485 | }
|
---|
486 |
|
---|
487 | static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp, SMB_STRUCT_STAT *sbuf)
|
---|
488 | {
|
---|
489 | int ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
|
---|
490 | if (ret == 0 && shadow_copy2_match_name(fsp->fsp_name->base_name, NULL)) {
|
---|
491 | convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
|
---|
492 | }
|
---|
493 | return ret;
|
---|
494 | }
|
---|
495 |
|
---|
496 | static int shadow_copy2_unlink(vfs_handle_struct *handle,
|
---|
497 | const struct smb_filename *smb_fname_in)
|
---|
498 | {
|
---|
499 | struct smb_filename *smb_fname = NULL;
|
---|
500 | NTSTATUS status;
|
---|
501 |
|
---|
502 | status = copy_smb_filename(talloc_tos(), smb_fname_in, &smb_fname);
|
---|
503 | if (!NT_STATUS_IS_OK(status)) {
|
---|
504 | errno = map_errno_from_nt_status(status);
|
---|
505 | return -1;
|
---|
506 | }
|
---|
507 |
|
---|
508 | SHADOW2_NEXT_SMB_FNAME(UNLINK, (handle, smb_fname), int, -1);
|
---|
509 | }
|
---|
510 |
|
---|
511 | static int shadow_copy2_chmod(vfs_handle_struct *handle,
|
---|
512 | const char *fname, mode_t mode)
|
---|
513 | {
|
---|
514 | SHADOW2_NEXT(CHMOD, (handle, name, mode), int, -1);
|
---|
515 | }
|
---|
516 |
|
---|
517 | static int shadow_copy2_chown(vfs_handle_struct *handle,
|
---|
518 | const char *fname, uid_t uid, gid_t gid)
|
---|
519 | {
|
---|
520 | SHADOW2_NEXT(CHOWN, (handle, name, uid, gid), int, -1);
|
---|
521 | }
|
---|
522 |
|
---|
523 | static int shadow_copy2_chdir(vfs_handle_struct *handle,
|
---|
524 | const char *fname)
|
---|
525 | {
|
---|
526 | SHADOW2_NEXT(CHDIR, (handle, name), int, -1);
|
---|
527 | }
|
---|
528 |
|
---|
529 | static int shadow_copy2_ntimes(vfs_handle_struct *handle,
|
---|
530 | const struct smb_filename *smb_fname_in,
|
---|
531 | struct smb_file_time *ft)
|
---|
532 | {
|
---|
533 | struct smb_filename *smb_fname = NULL;
|
---|
534 | NTSTATUS status;
|
---|
535 |
|
---|
536 | status = copy_smb_filename(talloc_tos(), smb_fname_in, &smb_fname);
|
---|
537 | if (!NT_STATUS_IS_OK(status)) {
|
---|
538 | errno = map_errno_from_nt_status(status);
|
---|
539 | return -1;
|
---|
540 | }
|
---|
541 |
|
---|
542 | SHADOW2_NEXT_SMB_FNAME(NTIMES, (handle, smb_fname, ft), int, -1);
|
---|
543 | }
|
---|
544 |
|
---|
545 | static int shadow_copy2_readlink(vfs_handle_struct *handle,
|
---|
546 | const char *fname, char *buf, size_t bufsiz)
|
---|
547 | {
|
---|
548 | SHADOW2_NEXT(READLINK, (handle, name, buf, bufsiz), int, -1);
|
---|
549 | }
|
---|
550 |
|
---|
551 | static int shadow_copy2_mknod(vfs_handle_struct *handle,
|
---|
552 | const char *fname, mode_t mode, SMB_DEV_T dev)
|
---|
553 | {
|
---|
554 | SHADOW2_NEXT(MKNOD, (handle, name, mode, dev), int, -1);
|
---|
555 | }
|
---|
556 |
|
---|
557 | static char *shadow_copy2_realpath(vfs_handle_struct *handle,
|
---|
558 | const char *fname, char *resolved_path)
|
---|
559 | {
|
---|
560 | const char *gmt;
|
---|
561 |
|
---|
562 | if (shadow_copy2_match_name(fname, &gmt)
|
---|
563 | && (gmt[GMT_NAME_LEN] == '\0')) {
|
---|
564 | char *copy, *result;
|
---|
565 |
|
---|
566 | copy = talloc_strdup(talloc_tos(), fname);
|
---|
567 | if (copy == NULL) {
|
---|
568 | errno = ENOMEM;
|
---|
569 | return NULL;
|
---|
570 | }
|
---|
571 |
|
---|
572 | copy[gmt - fname] = '.';
|
---|
573 |
|
---|
574 | DEBUG(10, ("calling NEXT_REALPATH with %s\n", copy));
|
---|
575 | result = SMB_VFS_NEXT_REALPATH(handle, copy, resolved_path);
|
---|
576 | TALLOC_FREE(copy);
|
---|
577 | return result;
|
---|
578 | }
|
---|
579 | SHADOW2_NEXT(REALPATH, (handle, name, resolved_path), char *, NULL);
|
---|
580 | }
|
---|
581 |
|
---|
582 | static const char *shadow_copy2_connectpath(struct vfs_handle_struct *handle,
|
---|
583 | const char *fname)
|
---|
584 | {
|
---|
585 | TALLOC_CTX *tmp_ctx;
|
---|
586 | const char *snapdir, *baseoffset, *basedir, *gmt_start;
|
---|
587 | size_t baselen;
|
---|
588 | char *ret;
|
---|
589 |
|
---|
590 | DEBUG(10, ("shadow_copy2_connectpath called with %s\n", fname));
|
---|
591 |
|
---|
592 | if (!shadow_copy2_match_name(fname, &gmt_start)) {
|
---|
593 | return handle->conn->connectpath;
|
---|
594 | }
|
---|
595 |
|
---|
596 | /*
|
---|
597 | * We have to create a real temporary context because we have
|
---|
598 | * to put our result on talloc_tos(). Thus we can't use a
|
---|
599 | * talloc_stackframe() here.
|
---|
600 | */
|
---|
601 | tmp_ctx = talloc_new(talloc_tos());
|
---|
602 |
|
---|
603 | fname = shadow_copy2_normalise_path(tmp_ctx, fname, gmt_start);
|
---|
604 | if (fname == NULL) {
|
---|
605 | TALLOC_FREE(tmp_ctx);
|
---|
606 | return NULL;
|
---|
607 | }
|
---|
608 |
|
---|
609 | snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle);
|
---|
610 | if (snapdir == NULL) {
|
---|
611 | DEBUG(2,("no snapdir found for share at %s\n",
|
---|
612 | handle->conn->connectpath));
|
---|
613 | TALLOC_FREE(tmp_ctx);
|
---|
614 | return NULL;
|
---|
615 | }
|
---|
616 |
|
---|
617 | basedir = shadow_copy2_find_basedir(tmp_ctx, handle);
|
---|
618 | if (basedir == NULL) {
|
---|
619 | DEBUG(2,("no basedir found for share at %s\n",
|
---|
620 | handle->conn->connectpath));
|
---|
621 | TALLOC_FREE(tmp_ctx);
|
---|
622 | return NULL;
|
---|
623 | }
|
---|
624 |
|
---|
625 | baselen = strlen(basedir);
|
---|
626 | baseoffset = handle->conn->connectpath + baselen;
|
---|
627 |
|
---|
628 | /* some sanity checks */
|
---|
629 | if (strncmp(basedir, handle->conn->connectpath, baselen) != 0 ||
|
---|
630 | (handle->conn->connectpath[baselen] != 0
|
---|
631 | && handle->conn->connectpath[baselen] != '/')) {
|
---|
632 | DEBUG(0,("shadow_copy2_connectpath: basedir %s is not a "
|
---|
633 | "parent of %s\n", basedir,
|
---|
634 | handle->conn->connectpath));
|
---|
635 | TALLOC_FREE(tmp_ctx);
|
---|
636 | return NULL;
|
---|
637 | }
|
---|
638 |
|
---|
639 | if (*baseoffset == '/') baseoffset++;
|
---|
640 |
|
---|
641 | ret = talloc_asprintf(talloc_tos(), "%s/%.*s/%s",
|
---|
642 | snapdir,
|
---|
643 | GMT_NAME_LEN, fname,
|
---|
644 | baseoffset);
|
---|
645 | DEBUG(6,("shadow_copy2_connectpath: '%s' -> '%s'\n", fname, ret));
|
---|
646 | TALLOC_FREE(tmp_ctx);
|
---|
647 | return ret;
|
---|
648 | }
|
---|
649 |
|
---|
650 | static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
|
---|
651 | const char *fname, uint32 security_info,
|
---|
652 | struct security_descriptor **ppdesc)
|
---|
653 | {
|
---|
654 | SHADOW2_NTSTATUS_NEXT(GET_NT_ACL, (handle, name, security_info, ppdesc), NT_STATUS_ACCESS_DENIED);
|
---|
655 | }
|
---|
656 |
|
---|
657 | static int shadow_copy2_mkdir(vfs_handle_struct *handle, const char *fname, mode_t mode)
|
---|
658 | {
|
---|
659 | SHADOW2_NEXT(MKDIR, (handle, name, mode), int, -1);
|
---|
660 | }
|
---|
661 |
|
---|
662 | static int shadow_copy2_rmdir(vfs_handle_struct *handle, const char *fname)
|
---|
663 | {
|
---|
664 | SHADOW2_NEXT(RMDIR, (handle, name), int, -1);
|
---|
665 | }
|
---|
666 |
|
---|
667 | static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
|
---|
668 | unsigned int flags)
|
---|
669 | {
|
---|
670 | SHADOW2_NEXT(CHFLAGS, (handle, name, flags), int, -1);
|
---|
671 | }
|
---|
672 |
|
---|
673 | static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
|
---|
674 | const char *fname, const char *aname, void *value, size_t size)
|
---|
675 | {
|
---|
676 | SHADOW2_NEXT(GETXATTR, (handle, name, aname, value, size), ssize_t, -1);
|
---|
677 | }
|
---|
678 |
|
---|
679 | static ssize_t shadow_copy2_lgetxattr(vfs_handle_struct *handle,
|
---|
680 | const char *fname, const char *aname, void *value, size_t size)
|
---|
681 | {
|
---|
682 | SHADOW2_NEXT(LGETXATTR, (handle, name, aname, value, size), ssize_t, -1);
|
---|
683 | }
|
---|
684 |
|
---|
685 | static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle, const char *fname,
|
---|
686 | char *list, size_t size)
|
---|
687 | {
|
---|
688 | SHADOW2_NEXT(LISTXATTR, (handle, name, list, size), ssize_t, -1);
|
---|
689 | }
|
---|
690 |
|
---|
691 | static int shadow_copy2_removexattr(struct vfs_handle_struct *handle, const char *fname,
|
---|
692 | const char *aname)
|
---|
693 | {
|
---|
694 | SHADOW2_NEXT(REMOVEXATTR, (handle, name, aname), int, -1);
|
---|
695 | }
|
---|
696 |
|
---|
697 | static int shadow_copy2_lremovexattr(struct vfs_handle_struct *handle, const char *fname,
|
---|
698 | const char *aname)
|
---|
699 | {
|
---|
700 | SHADOW2_NEXT(LREMOVEXATTR, (handle, name, aname), int, -1);
|
---|
701 | }
|
---|
702 |
|
---|
703 | static int shadow_copy2_setxattr(struct vfs_handle_struct *handle, const char *fname,
|
---|
704 | const char *aname, const void *value, size_t size, int flags)
|
---|
705 | {
|
---|
706 | SHADOW2_NEXT(SETXATTR, (handle, name, aname, value, size, flags), int, -1);
|
---|
707 | }
|
---|
708 |
|
---|
709 | static int shadow_copy2_lsetxattr(struct vfs_handle_struct *handle, const char *fname,
|
---|
710 | const char *aname, const void *value, size_t size, int flags)
|
---|
711 | {
|
---|
712 | SHADOW2_NEXT(LSETXATTR, (handle, name, aname, value, size, flags), int, -1);
|
---|
713 | }
|
---|
714 |
|
---|
715 | static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
|
---|
716 | const char *fname, mode_t mode)
|
---|
717 | {
|
---|
718 | SHADOW2_NEXT(CHMOD_ACL, (handle, name, mode), int, -1);
|
---|
719 | }
|
---|
720 |
|
---|
721 | static int shadow_copy2_get_shadow_copy2_data(vfs_handle_struct *handle,
|
---|
722 | files_struct *fsp,
|
---|
723 | SHADOW_COPY_DATA *shadow_copy2_data,
|
---|
724 | bool labels)
|
---|
725 | {
|
---|
726 | SMB_STRUCT_DIR *p;
|
---|
727 | const char *snapdir;
|
---|
728 | SMB_STRUCT_DIRENT *d;
|
---|
729 | TALLOC_CTX *tmp_ctx = talloc_new(handle->data);
|
---|
730 |
|
---|
731 | snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle);
|
---|
732 | if (snapdir == NULL) {
|
---|
733 | DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
|
---|
734 | handle->conn->connectpath));
|
---|
735 | errno = EINVAL;
|
---|
736 | talloc_free(tmp_ctx);
|
---|
737 | return -1;
|
---|
738 | }
|
---|
739 |
|
---|
740 | p = SMB_VFS_NEXT_OPENDIR(handle, snapdir, NULL, 0);
|
---|
741 |
|
---|
742 | if (!p) {
|
---|
743 | DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
|
---|
744 | " - %s\n", snapdir, strerror(errno)));
|
---|
745 | talloc_free(tmp_ctx);
|
---|
746 | errno = ENOSYS;
|
---|
747 | return -1;
|
---|
748 | }
|
---|
749 |
|
---|
750 | talloc_free(tmp_ctx);
|
---|
751 |
|
---|
752 | shadow_copy2_data->num_volumes = 0;
|
---|
753 | shadow_copy2_data->labels = NULL;
|
---|
754 |
|
---|
755 | while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
|
---|
756 | SHADOW_COPY_LABEL *tlabels;
|
---|
757 |
|
---|
758 | /* ignore names not of the right form in the snapshot directory */
|
---|
759 | if (!shadow_copy2_match_name(d->d_name, NULL)) {
|
---|
760 | continue;
|
---|
761 | }
|
---|
762 |
|
---|
763 | if (!labels) {
|
---|
764 | /* the caller doesn't want the labels */
|
---|
765 | shadow_copy2_data->num_volumes++;
|
---|
766 | continue;
|
---|
767 | }
|
---|
768 |
|
---|
769 | tlabels = talloc_realloc(shadow_copy2_data->mem_ctx,
|
---|
770 | shadow_copy2_data->labels,
|
---|
771 | SHADOW_COPY_LABEL, shadow_copy2_data->num_volumes+1);
|
---|
772 | if (tlabels == NULL) {
|
---|
773 | DEBUG(0,("shadow_copy2: out of memory\n"));
|
---|
774 | SMB_VFS_NEXT_CLOSEDIR(handle, p);
|
---|
775 | return -1;
|
---|
776 | }
|
---|
777 |
|
---|
778 | strlcpy(tlabels[shadow_copy2_data->num_volumes], d->d_name, sizeof(*tlabels));
|
---|
779 | shadow_copy2_data->num_volumes++;
|
---|
780 | shadow_copy2_data->labels = tlabels;
|
---|
781 | }
|
---|
782 |
|
---|
783 | SMB_VFS_NEXT_CLOSEDIR(handle,p);
|
---|
784 | return 0;
|
---|
785 | }
|
---|
786 |
|
---|
787 | static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
|
---|
788 | .opendir = shadow_copy2_opendir,
|
---|
789 | .mkdir = shadow_copy2_mkdir,
|
---|
790 | .rmdir = shadow_copy2_rmdir,
|
---|
791 | .chflags = shadow_copy2_chflags,
|
---|
792 | .getxattr = shadow_copy2_getxattr,
|
---|
793 | .lgetxattr = shadow_copy2_lgetxattr,
|
---|
794 | .listxattr = shadow_copy2_listxattr,
|
---|
795 | .removexattr = shadow_copy2_removexattr,
|
---|
796 | .lremovexattr = shadow_copy2_lremovexattr,
|
---|
797 | .setxattr = shadow_copy2_setxattr,
|
---|
798 | .lsetxattr = shadow_copy2_lsetxattr,
|
---|
799 | .open = shadow_copy2_open,
|
---|
800 | .rename = shadow_copy2_rename,
|
---|
801 | .stat = shadow_copy2_stat,
|
---|
802 | .lstat = shadow_copy2_lstat,
|
---|
803 | .fstat = shadow_copy2_fstat,
|
---|
804 | .unlink = shadow_copy2_unlink,
|
---|
805 | .chmod = shadow_copy2_chmod,
|
---|
806 | .chown = shadow_copy2_chown,
|
---|
807 | .chdir = shadow_copy2_chdir,
|
---|
808 | .ntimes = shadow_copy2_ntimes,
|
---|
809 | .symlink = shadow_copy2_symlink,
|
---|
810 | .vfs_readlink = shadow_copy2_readlink,
|
---|
811 | .link = shadow_copy2_link,
|
---|
812 | .mknod = shadow_copy2_mknod,
|
---|
813 | .realpath = shadow_copy2_realpath,
|
---|
814 | .connectpath = shadow_copy2_connectpath,
|
---|
815 | .get_nt_acl = shadow_copy2_get_nt_acl,
|
---|
816 | .chmod_acl = shadow_copy2_chmod_acl,
|
---|
817 | .get_shadow_copy_data = shadow_copy2_get_shadow_copy2_data,
|
---|
818 | };
|
---|
819 |
|
---|
820 | NTSTATUS vfs_shadow_copy2_init(void);
|
---|
821 | NTSTATUS vfs_shadow_copy2_init(void)
|
---|
822 | {
|
---|
823 | NTSTATUS ret;
|
---|
824 |
|
---|
825 | ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "shadow_copy2",
|
---|
826 | &vfs_shadow_copy2_fns);
|
---|
827 |
|
---|
828 | if (!NT_STATUS_IS_OK(ret))
|
---|
829 | return ret;
|
---|
830 |
|
---|
831 | vfs_shadow_copy2_debug_level = debug_add_class("shadow_copy2");
|
---|
832 | if (vfs_shadow_copy2_debug_level == -1) {
|
---|
833 | vfs_shadow_copy2_debug_level = DBGC_VFS;
|
---|
834 | DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
|
---|
835 | "vfs_shadow_copy2_init"));
|
---|
836 | } else {
|
---|
837 | DEBUG(10, ("%s: Debug class number of '%s': %d\n",
|
---|
838 | "vfs_shadow_copy2_init","shadow_copy2",vfs_shadow_copy2_debug_level));
|
---|
839 | }
|
---|
840 |
|
---|
841 | return ret;
|
---|
842 | }
|
---|