0f52f9e71c57b5083f8116ae2d923145c9eeefbd
[samba.git] / source3 / modules / vfs_snapper.c
1 /*
2  * Module for snapshot IO using snapper
3  *
4  * Copyright (C) David Disseldorp 2012-2014
5  *
6  * Portions taken from vfs_shadow_copy2.c:
7  * Copyright (C) Andrew Tridgell   2007
8  * Copyright (C) Ed Plese          2009
9  * Copyright (C) Volker Lendecke   2011
10  * Copyright (C) Christian Ambach  2011
11  * Copyright (C) Michael Adam      2013
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, see <http://www.gnu.org/licenses/>.
25  */
26
27 #include <dbus/dbus.h>
28 #ifdef HAVE_LINUX_IOCTL_H
29 #include <linux/ioctl.h>
30 #endif
31 #include <sys/ioctl.h>
32 #include <dirent.h>
33 #include <libgen.h>
34 #include "includes.h"
35 #include "include/ntioctl.h"
36 #include "include/smb.h"
37 #include "system/filesys.h"
38 #include "smbd/smbd.h"
39 #include "lib/util/tevent_ntstatus.h"
40
41 #define SNAPPER_SIG_LIST_SNAPS_RSP "a(uquxussa{ss})"
42 #define SNAPPER_SIG_LIST_CONFS_RSP "a(ssa{ss})"
43 #define SNAPPER_SIG_CREATE_SNAP_RSP "u"
44 #define SNAPPER_SIG_DEL_SNAPS_RSP ""
45 #define SNAPPER_SIG_STRING_DICT "{ss}"
46
47 struct snapper_dict {
48         char *key;
49         char *val;
50 };
51
52 struct snapper_snap {
53         uint32_t id;
54         uint16_t type;
55         uint32_t pre_id;
56         int64_t time;
57         uint32_t creator_uid;
58         char *desc;
59         char *cleanup;
60         uint32_t num_user_data;
61         struct snapper_dict *user_data;
62 };
63
64 struct snapper_conf {
65         char *name;
66         char *mnt;
67         uint32_t num_attrs;
68         struct snapper_dict *attrs;
69 };
70
71 static const struct {
72         const char *snapper_err_str;
73         NTSTATUS status;
74 } snapper_err_map[] = {
75         { "error.no_permissions", NT_STATUS_ACCESS_DENIED },
76 };
77
78 static NTSTATUS snapper_err_ntstatus_map(const char *snapper_err_str)
79 {
80         int i;
81
82         if (snapper_err_str == NULL) {
83                 return NT_STATUS_UNSUCCESSFUL;
84         }
85         for (i = 0; i < ARRAY_SIZE(snapper_err_map); i++) {
86                 if (!strcmp(snapper_err_map[i].snapper_err_str,
87                             snapper_err_str)) {
88                         return snapper_err_map[i].status;
89                 }
90         }
91         DEBUG(2, ("no explicit mapping for dbus error: %s\n", snapper_err_str));
92
93         return NT_STATUS_UNSUCCESSFUL;
94 }
95
96 /*
97  * Strings are UTF-8. Other characters must be encoded hexadecimal as "\x??".
98  * As a consequence "\" must be encoded as "\\".
99  */
100 static NTSTATUS snapper_dbus_str_encode(TALLOC_CTX *mem_ctx, const char *in_str,
101                                         char **_out_str)
102 {
103         size_t in_len;
104         char *out_str;
105         int i;
106         int out_off;
107         int out_len;
108
109         if (in_str == NULL) {
110                 return NT_STATUS_INVALID_PARAMETER;
111         }
112
113         in_len = strlen(in_str);
114
115         /* output can be max 4 times the length of @in_str, +1 for terminator */
116         out_len = (in_len * 4) + 1;
117
118         out_str = talloc_array(mem_ctx, char, out_len);
119         if (out_str == NULL) {
120                 return NT_STATUS_NO_MEMORY;
121         }
122
123         out_off = 0;
124         for (i = 0; i < in_len; i++) {
125                 size_t pushed;
126
127                 if (in_str[i] == '\\') {
128                         pushed = snprintf(out_str + out_off, out_len - out_off,
129                                           "\\\\");
130                 } else if ((unsigned char)in_str[i] > 127) {
131                         pushed = snprintf(out_str + out_off, out_len - out_off,
132                                           "\\x%02x", (unsigned char)in_str[i]);
133                 } else {
134                         /* regular character */
135                         *(out_str + out_off) = in_str[i];
136                         pushed = sizeof(char);
137                 }
138                 if (pushed >= out_len - out_off) {
139                         /* truncated, should never happen */
140                         talloc_free(out_str);
141                         return NT_STATUS_INTERNAL_ERROR;
142                 }
143                 out_off += pushed;
144         }
145
146         *(out_str + out_off) = '\0';
147         *_out_str = out_str;
148
149         return NT_STATUS_OK;
150 }
151
152 static NTSTATUS snapper_dbus_str_decode(TALLOC_CTX *mem_ctx, const char *in_str,
153                                         char **_out_str)
154 {
155         size_t in_len;
156         char *out_str;
157         int i;
158         int out_off;
159         int out_len;
160
161         if (in_str == NULL) {
162                 return NT_STATUS_INVALID_PARAMETER;
163         }
164
165         in_len = strlen(in_str);
166
167         /* output cannot be larger than input, +1 for terminator */
168         out_len = in_len + 1;
169
170         out_str = talloc_array(mem_ctx, char, out_len);
171         if (out_str == NULL) {
172                 return NT_STATUS_NO_MEMORY;
173         }
174
175         out_off = 0;
176         for (i = 0; i < in_len; i++) {
177                 int j;
178                 char hex_buf[3];
179                 unsigned int non_ascii_byte;
180
181                 if (in_str[i] != '\\') {
182                         out_str[out_off] = in_str[i];
183                         out_off++;
184                         continue;
185                 }
186
187                 i++;
188                 if (in_str[i] == '\\') {
189                         out_str[out_off] = '\\';
190                         out_off++;
191                         continue;
192                 } else if (in_str[i] != 'x') {
193                         goto err_invalid_src_encoding;
194                 }
195
196                 /* non-ASCII, encoded as two hex chars */
197                 for (j = 0; j < 2; j++) {
198                         i++;
199                         if ((in_str[i] == '\0') || !isxdigit(in_str[i])) {
200                                 goto err_invalid_src_encoding;
201                         }
202                         hex_buf[j] = in_str[i];
203                 }
204                 hex_buf[2] = '\0';
205
206                 sscanf(hex_buf, "%x", &non_ascii_byte);
207                 out_str[out_off] = (unsigned char)non_ascii_byte;
208                 out_off++;
209         }
210
211         out_str[out_off] = '\0';
212         *_out_str = out_str;
213
214         return NT_STATUS_OK;
215 err_invalid_src_encoding:
216         DEBUG(0, ("invalid encoding %s\n", in_str));
217         return NT_STATUS_INVALID_PARAMETER;
218 }
219
220 static DBusConnection *snapper_dbus_conn_create(void)
221 {
222         DBusError err;
223         DBusConnection *dconn;
224
225         dbus_error_init(&err);
226
227         /*
228          * Always create a new DBus connection, to ensure snapperd detects the
229          * correct client [E]UID. With dbus_bus_get() it does not!
230          */
231         dconn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
232         if (dbus_error_is_set(&err)) {
233                 DEBUG(0, ("dbus connection error: %s\n", err.message));
234                 dbus_error_free(&err);
235         }
236         if (dconn == NULL) {
237                 return NULL;
238         }
239
240         /* dbus_bus_get_private() sets exit-on-disconnect by default, undo it */
241         dbus_connection_set_exit_on_disconnect(dconn, false);
242
243         return dconn;
244 }
245
246 static void snapper_dbus_conn_destroy(DBusConnection *dconn)
247 {
248         if (dconn == NULL) {
249                 DEBUG(2, ("attempt to destroy NULL dbus connection\n"));
250                 return;
251         }
252
253         dbus_connection_close(dconn);
254         dbus_connection_unref(dconn);
255 }
256
257 /*
258  * send the message @send_msg over the dbus and wait for a response, return the
259  * responsee via @recv_msg_out.
260  * @send_msg is not freed, dbus_message_unref() must be handled by the caller.
261  */
262 static NTSTATUS snapper_dbus_msg_xchng(DBusConnection *dconn,
263                                        DBusMessage *send_msg,
264                                        DBusMessage **recv_msg_out)
265 {
266         DBusPendingCall *pending;
267         DBusMessage *recv_msg;
268
269         /* send message and get a handle for a reply */
270         if (!dbus_connection_send_with_reply(dconn, send_msg, &pending, -1)) {
271                 return NT_STATUS_NO_MEMORY;
272         }
273         if (NULL == pending) {
274                 DEBUG(0, ("dbus msg send failed\n"));
275                 return NT_STATUS_UNSUCCESSFUL;
276         }
277
278         dbus_connection_flush(dconn);
279
280         /* block until we receive a reply */
281         dbus_pending_call_block(pending);
282
283         /* get the reply message */
284         recv_msg = dbus_pending_call_steal_reply(pending);
285         if (recv_msg == NULL) {
286                 DEBUG(0, ("Reply Null\n"));
287                 return NT_STATUS_UNSUCCESSFUL;
288         }
289         /* free the pending message handle */
290         dbus_pending_call_unref(pending);
291         *recv_msg_out = recv_msg;
292
293         return NT_STATUS_OK;
294 }
295
296 static NTSTATUS snapper_type_check(DBusMessageIter *iter,
297                                    int expected_type)
298 {
299         int type = dbus_message_iter_get_arg_type(iter);
300         if (type != expected_type) {
301                 DEBUG(0, ("got type %d, expecting %d\n",
302                         type, expected_type));
303                 return NT_STATUS_INVALID_PARAMETER;
304         }
305
306         return NT_STATUS_OK;
307 }
308
309 static NTSTATUS snapper_type_check_get(DBusMessageIter *iter,
310                                        int expected_type,
311                                        void *val)
312 {
313         NTSTATUS status;
314         status = snapper_type_check(iter, expected_type);
315         if (!NT_STATUS_IS_OK(status)) {
316                 return status;
317         }
318
319         dbus_message_iter_get_basic(iter, val);
320
321         return NT_STATUS_OK;
322 }
323
324 static NTSTATUS snapper_dict_unpack(TALLOC_CTX *mem_ctx,
325                                     DBusMessageIter *iter,
326                                     struct snapper_dict *dict_out)
327
328 {
329         NTSTATUS status;
330         DBusMessageIter dct_iter;
331         char *key_encoded;
332         char *val_encoded;
333
334         status = snapper_type_check(iter, DBUS_TYPE_DICT_ENTRY);
335         if (!NT_STATUS_IS_OK(status)) {
336                 return status;
337         }
338         dbus_message_iter_recurse(iter, &dct_iter);
339
340         status = snapper_type_check_get(&dct_iter, DBUS_TYPE_STRING,
341                                         &key_encoded);
342         if (!NT_STATUS_IS_OK(status)) {
343                 return status;
344         }
345         status = snapper_dbus_str_decode(mem_ctx, key_encoded, &dict_out->key);
346         if (!NT_STATUS_IS_OK(status)) {
347                 return status;
348         }
349
350         dbus_message_iter_next(&dct_iter);
351         status = snapper_type_check_get(&dct_iter, DBUS_TYPE_STRING,
352                                         &val_encoded);
353         if (!NT_STATUS_IS_OK(status)) {
354                 talloc_free(dict_out->key);
355                 return status;
356         }
357         status = snapper_dbus_str_decode(mem_ctx, val_encoded, &dict_out->val);
358         if (!NT_STATUS_IS_OK(status)) {
359                 talloc_free(dict_out->key);
360                 return status;
361         }
362
363         return NT_STATUS_OK;
364 }
365
366 static void snapper_dict_array_print(uint32_t num_dicts,
367                                      struct snapper_dict *dicts)
368 {
369         int i;
370
371         for (i = 0; i < num_dicts; i++) {
372                 DEBUG(10, ("dict (key: %s, val: %s)\n",
373                            dicts[i].key, dicts[i].val));
374         }
375 }
376
377 static NTSTATUS snapper_dict_array_unpack(TALLOC_CTX *mem_ctx,
378                                           DBusMessageIter *iter,
379                                           uint32_t *num_dicts_out,
380                                           struct snapper_dict **dicts_out)
381 {
382         NTSTATUS status;
383         DBusMessageIter array_iter;
384         uint32_t num_dicts;
385         struct snapper_dict *dicts = NULL;
386
387         status = snapper_type_check(iter, DBUS_TYPE_ARRAY);
388         if (!NT_STATUS_IS_OK(status)) {
389                 return status;
390         }
391         dbus_message_iter_recurse(iter, &array_iter);
392
393         num_dicts = 0;
394         while (dbus_message_iter_get_arg_type(&array_iter)
395                                                         != DBUS_TYPE_INVALID) {
396                 num_dicts++;
397                 dicts = talloc_realloc(mem_ctx, dicts, struct snapper_dict,
398                                        num_dicts);
399                 if (dicts == NULL)
400                         abort();
401
402                 status = snapper_dict_unpack(mem_ctx, &array_iter,
403                                              &dicts[num_dicts - 1]);
404                 if (!NT_STATUS_IS_OK(status)) {
405                         talloc_free(dicts);
406                         return status;
407                 }
408                 dbus_message_iter_next(&array_iter);
409         }
410
411         *num_dicts_out = num_dicts;
412         *dicts_out = dicts;
413
414         return NT_STATUS_OK;
415 }
416
417 static NTSTATUS snapper_list_confs_pack(DBusMessage **req_msg_out)
418 {
419         DBusMessage *msg;
420
421         msg = dbus_message_new_method_call("org.opensuse.Snapper",
422                                            "/org/opensuse/Snapper",
423                                            "org.opensuse.Snapper",
424                                            "ListConfigs");
425         if (msg == NULL) {
426                 DEBUG(0, ("null msg\n"));
427                 return NT_STATUS_NO_MEMORY;
428         }
429
430         /* no arguments to append */
431         *req_msg_out = msg;
432
433         return NT_STATUS_OK;
434 }
435
436 static NTSTATUS snapper_conf_unpack(TALLOC_CTX *mem_ctx,
437                                     DBusMessageIter *iter,
438                                     struct snapper_conf *conf_out)
439 {
440         NTSTATUS status;
441         DBusMessageIter st_iter;
442         char *name_encoded;
443         char *mnt_encoded;
444
445         status = snapper_type_check(iter, DBUS_TYPE_STRUCT);
446         if (!NT_STATUS_IS_OK(status)) {
447                 return status;
448         }
449         dbus_message_iter_recurse(iter, &st_iter);
450
451         status = snapper_type_check_get(&st_iter, DBUS_TYPE_STRING,
452                                         &name_encoded);
453         if (!NT_STATUS_IS_OK(status)) {
454                 return status;
455         }
456
457         status = snapper_dbus_str_decode(mem_ctx, name_encoded,
458                                          &conf_out->name);
459         if (!NT_STATUS_IS_OK(status)) {
460                 return status;
461         }
462
463         dbus_message_iter_next(&st_iter);
464         status = snapper_type_check_get(&st_iter, DBUS_TYPE_STRING,
465                                         &mnt_encoded);
466         if (!NT_STATUS_IS_OK(status)) {
467                 talloc_free(conf_out->name);
468                 return status;
469         }
470
471         status = snapper_dbus_str_decode(mem_ctx, mnt_encoded,
472                                          &conf_out->mnt);
473         if (!NT_STATUS_IS_OK(status)) {
474                 talloc_free(conf_out->name);
475                 return status;
476         }
477
478         dbus_message_iter_next(&st_iter);
479         status = snapper_dict_array_unpack(mem_ctx, &st_iter,
480                                            &conf_out->num_attrs,
481                                            &conf_out->attrs);
482         if (!NT_STATUS_IS_OK(status)) {
483                 talloc_free(conf_out->mnt);
484                 talloc_free(conf_out->name);
485                 return status;
486         }
487
488         return NT_STATUS_OK;
489 }
490
491 static struct snapper_conf *snapper_conf_array_base_find(int32_t num_confs,
492                                                 struct snapper_conf *confs,
493                                                          const char *base)
494 {
495         int i;
496
497         for (i = 0; i < num_confs; i++) {
498                 if (strcmp(confs[i].mnt, base) == 0) {
499                         DEBUG(5, ("found snapper conf %s for path %s\n",
500                                   confs[i].name, base));
501                         return &confs[i];
502                 }
503         }
504         DEBUG(5, ("config for base %s not found\n", base));
505
506         return NULL;
507 }
508
509 static void snapper_conf_array_print(int32_t num_confs,
510                                      struct snapper_conf *confs)
511 {
512         int i;
513
514         for (i = 0; i < num_confs; i++) {
515                 DEBUG(10, ("name: %s, mnt: %s\n",
516                            confs[i].name, confs[i].mnt));
517                 snapper_dict_array_print(confs[i].num_attrs, confs[i].attrs);
518         }
519 }
520
521 static NTSTATUS snapper_conf_array_unpack(TALLOC_CTX *mem_ctx,
522                                           DBusMessageIter *iter,
523                                           uint32_t *num_confs_out,
524                                           struct snapper_conf **confs_out)
525 {
526         uint32_t num_confs;
527         NTSTATUS status;
528         struct snapper_conf *confs = NULL;
529         DBusMessageIter array_iter;
530
531
532         status = snapper_type_check(iter, DBUS_TYPE_ARRAY);
533         if (!NT_STATUS_IS_OK(status)) {
534                 return status;
535         }
536         dbus_message_iter_recurse(iter, &array_iter);
537
538         num_confs = 0;
539         while (dbus_message_iter_get_arg_type(&array_iter)
540                                                         != DBUS_TYPE_INVALID) {
541                 num_confs++;
542                 confs = talloc_realloc(mem_ctx, confs, struct snapper_conf,
543                                        num_confs);
544                 if (confs == NULL)
545                         abort();
546
547                 status = snapper_conf_unpack(confs, &array_iter,
548                                              &confs[num_confs - 1]);
549                 if (!NT_STATUS_IS_OK(status)) {
550                         talloc_free(confs);
551                         return status;
552                 }
553                 dbus_message_iter_next(&array_iter);
554         }
555
556         *num_confs_out = num_confs;
557         *confs_out = confs;
558
559         return NT_STATUS_OK;
560 }
561
562 static NTSTATUS snapper_list_confs_unpack(TALLOC_CTX *mem_ctx,
563                                           DBusConnection *dconn,
564                                           DBusMessage *rsp_msg,
565                                           uint32_t *num_confs_out,
566                                           struct snapper_conf **confs_out)
567 {
568         NTSTATUS status;
569         DBusMessageIter iter;
570         int msg_type;
571         uint32_t num_confs;
572         struct snapper_conf *confs;
573         const char *sig;
574
575         msg_type = dbus_message_get_type(rsp_msg);
576         if (msg_type == DBUS_MESSAGE_TYPE_ERROR) {
577                 const char *err_str = dbus_message_get_error_name(rsp_msg);
578                 DEBUG(0, ("list_confs error response: %s\n", err_str));
579                 return snapper_err_ntstatus_map(err_str);
580         }
581
582         if (msg_type != DBUS_MESSAGE_TYPE_METHOD_RETURN) {
583                 DEBUG(0, ("unexpected list_confs ret type: %d\n",
584                           msg_type));
585                 return NT_STATUS_INVALID_PARAMETER;
586         }
587
588         sig = dbus_message_get_signature(rsp_msg);
589         if ((sig == NULL)
590          || (strcmp(sig, SNAPPER_SIG_LIST_CONFS_RSP) != 0)) {
591                 DEBUG(0, ("bad list confs response sig: %s, expected: %s\n",
592                           (sig ? sig : "NULL"), SNAPPER_SIG_LIST_CONFS_RSP));
593                 return NT_STATUS_INVALID_PARAMETER;
594         }
595
596         if (!dbus_message_iter_init(rsp_msg, &iter)) {
597                 /* FIXME return empty? */
598                 DEBUG(0, ("Message has no arguments!\n"));
599                 return NT_STATUS_INVALID_PARAMETER;
600         }
601
602         status = snapper_conf_array_unpack(mem_ctx, &iter, &num_confs, &confs);
603         if (!NT_STATUS_IS_OK(status)) {
604                 DEBUG(0, ("failed to unpack conf array\n"));
605                 return status;
606         }
607
608         snapper_conf_array_print(num_confs, confs);
609
610         *num_confs_out = num_confs;
611         *confs_out = confs;
612
613         return NT_STATUS_OK;
614 }
615
616 static NTSTATUS snapper_list_snaps_pack(TALLOC_CTX *mem_ctx,
617                                         char *snapper_conf,
618                                         DBusMessage **req_msg_out)
619 {
620         DBusMessage *msg;
621         DBusMessageIter args;
622         char *conf_encoded;
623         NTSTATUS status;
624
625         msg = dbus_message_new_method_call("org.opensuse.Snapper", /* target for the method call */
626                                            "/org/opensuse/Snapper", /* object to call on */
627                                            "org.opensuse.Snapper", /* interface to call on */
628                                            "ListSnapshots"); /* method name */
629         if (msg == NULL) {
630                 DEBUG(0, ("failed to create list snaps message\n"));
631                 return NT_STATUS_NO_MEMORY;
632         }
633
634         status = snapper_dbus_str_encode(mem_ctx, snapper_conf, &conf_encoded);
635         if (!NT_STATUS_IS_OK(status)) {
636                 dbus_message_unref(msg);
637                 return status;
638         }
639
640         /* append arguments */
641         dbus_message_iter_init_append(msg, &args);
642         if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
643                                             &conf_encoded)) {
644                 talloc_free(conf_encoded);
645                 dbus_message_unref(msg);
646                 return NT_STATUS_NO_MEMORY;
647         }
648
649         *req_msg_out = msg;
650
651         return NT_STATUS_OK;
652 }
653
654 static NTSTATUS snapper_snap_struct_unpack(TALLOC_CTX *mem_ctx,
655                                            DBusMessageIter *iter,
656                                            struct snapper_snap *snap_out)
657 {
658         NTSTATUS status;
659         DBusMessageIter st_iter;
660         char *desc_encoded;
661         char *cleanup_encoded;
662
663         status = snapper_type_check(iter, DBUS_TYPE_STRUCT);
664         if (!NT_STATUS_IS_OK(status)) {
665                 return status;
666         }
667         dbus_message_iter_recurse(iter, &st_iter);
668
669         status = snapper_type_check_get(&st_iter, DBUS_TYPE_UINT32,
670                                         &snap_out->id);
671         if (!NT_STATUS_IS_OK(status)) {
672                 return status;
673         }
674
675         dbus_message_iter_next(&st_iter);
676         status = snapper_type_check_get(&st_iter, DBUS_TYPE_UINT16,
677                                         &snap_out->type);
678         if (!NT_STATUS_IS_OK(status)) {
679                 return status;
680         }
681
682         dbus_message_iter_next(&st_iter);
683         status = snapper_type_check_get(&st_iter, DBUS_TYPE_UINT32,
684                                         &snap_out->pre_id);
685         if (!NT_STATUS_IS_OK(status)) {
686                 return status;
687         }
688
689         dbus_message_iter_next(&st_iter);
690         status = snapper_type_check_get(&st_iter, DBUS_TYPE_INT64,
691                                         &snap_out->time);
692         if (!NT_STATUS_IS_OK(status)) {
693                 return status;
694         }
695
696         dbus_message_iter_next(&st_iter);
697         status = snapper_type_check_get(&st_iter, DBUS_TYPE_UINT32,
698                                         &snap_out->creator_uid);
699         if (!NT_STATUS_IS_OK(status)) {
700                 return status;
701         }
702
703         dbus_message_iter_next(&st_iter);
704         status = snapper_type_check_get(&st_iter, DBUS_TYPE_STRING,
705                                         &desc_encoded);
706         if (!NT_STATUS_IS_OK(status)) {
707                 return status;
708         }
709
710         status = snapper_dbus_str_decode(mem_ctx, desc_encoded,
711                                          &snap_out->desc);
712         if (!NT_STATUS_IS_OK(status)) {
713                 return status;
714         }
715
716         dbus_message_iter_next(&st_iter);
717         status = snapper_type_check_get(&st_iter, DBUS_TYPE_STRING,
718                                         &cleanup_encoded);
719         if (!NT_STATUS_IS_OK(status)) {
720                 talloc_free(snap_out->desc);
721                 return status;
722         }
723
724         status = snapper_dbus_str_decode(mem_ctx, cleanup_encoded,
725                                          &snap_out->cleanup);
726         if (!NT_STATUS_IS_OK(status)) {
727                 talloc_free(snap_out->desc);
728                 return status;
729         }
730
731         dbus_message_iter_next(&st_iter);
732         status = snapper_dict_array_unpack(mem_ctx, &st_iter,
733                                            &snap_out->num_user_data,
734                                            &snap_out->user_data);
735         if (!NT_STATUS_IS_OK(status)) {
736                 talloc_free(snap_out->cleanup);
737                 talloc_free(snap_out->desc);
738                 return status;
739         }
740
741         return NT_STATUS_OK;
742 }
743
744 static void snapper_snap_array_print(int32_t num_snaps,
745                                      struct snapper_snap *snaps)
746 {
747         int i;
748
749         for (i = 0; i < num_snaps; i++) {
750                 DEBUG(10, ("id: %u, "
751                            "type: %u, "
752                            "pre_id: %u, "
753                            "time: %ld, "
754                            "creator_uid: %u, "
755                            "desc: %s, "
756                            "cleanup: %s\n",
757                            (unsigned int)snaps[i].id,
758                            (unsigned int)snaps[i].type,
759                            (unsigned int)snaps[i].pre_id,
760                            (long int)snaps[i].time,
761                            (unsigned int)snaps[i].creator_uid,
762                            snaps[i].desc,
763                            snaps[i].cleanup));
764                 snapper_dict_array_print(snaps[i].num_user_data,
765                                          snaps[i].user_data);
766         }
767 }
768
769 static NTSTATUS snapper_snap_array_unpack(TALLOC_CTX *mem_ctx,
770                                           DBusMessageIter *iter,
771                                           uint32_t *num_snaps_out,
772                                           struct snapper_snap **snaps_out)
773 {
774         uint32_t num_snaps;
775         NTSTATUS status;
776         struct snapper_snap *snaps = NULL;
777         DBusMessageIter array_iter;
778
779
780         status = snapper_type_check(iter, DBUS_TYPE_ARRAY);
781         if (!NT_STATUS_IS_OK(status)) {
782                 return status;
783         }
784         dbus_message_iter_recurse(iter, &array_iter);
785
786         num_snaps = 0;
787         while (dbus_message_iter_get_arg_type(&array_iter)
788                                                         != DBUS_TYPE_INVALID) {
789                 num_snaps++;
790                 snaps = talloc_realloc(mem_ctx, snaps, struct snapper_snap,
791                                        num_snaps);
792                 if (snaps == NULL)
793                         abort();
794
795                 status = snapper_snap_struct_unpack(snaps, &array_iter,
796                                                     &snaps[num_snaps - 1]);
797                 if (!NT_STATUS_IS_OK(status)) {
798                         talloc_free(snaps);
799                         return status;
800                 }
801                 dbus_message_iter_next(&array_iter);
802         }
803
804         *num_snaps_out = num_snaps;
805         *snaps_out = snaps;
806
807         return NT_STATUS_OK;
808 }
809
810 static NTSTATUS snapper_list_snaps_unpack(TALLOC_CTX *mem_ctx,
811                                           DBusMessage *rsp_msg,
812                                           uint32_t *num_snaps_out,
813                                           struct snapper_snap **snaps_out)
814 {
815         NTSTATUS status;
816         DBusMessageIter iter;
817         int msg_type;
818         uint32_t num_snaps;
819         struct snapper_snap *snaps;
820         const char *sig;
821
822         msg_type = dbus_message_get_type(rsp_msg);
823         if (msg_type == DBUS_MESSAGE_TYPE_ERROR) {
824                 const char *err_str = dbus_message_get_error_name(rsp_msg);
825                 DEBUG(0, ("list_snaps error response: %s\n", err_str));
826                 return snapper_err_ntstatus_map(err_str);
827         }
828
829         if (msg_type != DBUS_MESSAGE_TYPE_METHOD_RETURN) {
830                 DEBUG(0,("unexpected list_snaps ret type: %d\n",
831                          msg_type));
832                 return NT_STATUS_INVALID_PARAMETER;
833         }
834
835         sig = dbus_message_get_signature(rsp_msg);
836         if ((sig == NULL)
837          || (strcmp(sig, SNAPPER_SIG_LIST_SNAPS_RSP) != 0)) {
838                 DEBUG(0, ("bad list snaps response sig: %s, "
839                           "expected: %s\n",
840                           (sig ? sig : "NULL"),
841                           SNAPPER_SIG_LIST_SNAPS_RSP));
842                 return NT_STATUS_INVALID_PARAMETER;
843         }
844
845         /* read the parameters */
846         if (!dbus_message_iter_init(rsp_msg, &iter)) {
847                 DEBUG(0, ("response has no arguments!\n"));
848                 return NT_STATUS_INVALID_PARAMETER;
849         }
850
851         status = snapper_snap_array_unpack(mem_ctx, &iter, &num_snaps, &snaps);
852         if (!NT_STATUS_IS_OK(status)) {
853                 DEBUG(0, ("failed to unpack snap array\n"));
854                 return NT_STATUS_INVALID_PARAMETER;
855         }
856
857         snapper_snap_array_print(num_snaps, snaps);
858
859         *num_snaps_out = num_snaps;
860         *snaps_out = snaps;
861
862         return NT_STATUS_OK;
863 }
864
865 static NTSTATUS snapper_create_snap_pack(TALLOC_CTX *mem_ctx,
866                                          const char *snapper_conf,
867                                          const char *desc,
868                                          uint32_t num_user_data,
869                                          struct snapper_dict *user_data,
870                                          DBusMessage **req_msg_out)
871 {
872         DBusMessage *msg;
873         DBusMessageIter args;
874         DBusMessageIter array_iter;
875         DBusMessageIter struct_iter;
876         const char *empty = "";
877         char *str_encoded;
878         uint32_t i;
879         bool ok;
880         TALLOC_CTX *enc_ctx;
881         NTSTATUS status;
882
883         DEBUG(10, ("CreateSingleSnapshot: %s, %s, %s, num user %u\n",
884                   snapper_conf, desc, empty, num_user_data));
885
886         enc_ctx = talloc_new(mem_ctx);
887         if (enc_ctx == NULL) {
888                 return NT_STATUS_NO_MEMORY;
889         }
890
891         msg = dbus_message_new_method_call("org.opensuse.Snapper",
892                                            "/org/opensuse/Snapper",
893                                            "org.opensuse.Snapper",
894                                            "CreateSingleSnapshot");
895         if (msg == NULL) {
896                 DEBUG(0, ("failed to create req msg\n"));
897                 talloc_free(enc_ctx);
898                 return NT_STATUS_NO_MEMORY;
899         }
900
901         status = snapper_dbus_str_encode(enc_ctx, snapper_conf, &str_encoded);
902         if (!NT_STATUS_IS_OK(status)) {
903                 dbus_message_unref(msg);
904                 talloc_free(enc_ctx);
905                 return status;
906         }
907
908         /* append arguments */
909         dbus_message_iter_init_append(msg, &args);
910         ok = dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
911                                             &str_encoded);
912         if (!ok) {
913                 dbus_message_unref(msg);
914                 talloc_free(enc_ctx);
915                 return NT_STATUS_NO_MEMORY;
916         }
917
918         status = snapper_dbus_str_encode(enc_ctx, desc, &str_encoded);
919         if (!NT_STATUS_IS_OK(status)) {
920                 dbus_message_unref(msg);
921                 talloc_free(enc_ctx);
922                 return status;
923         }
924
925         ok = dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
926                                             &str_encoded);
927         if (!ok) {
928                 dbus_message_unref(msg);
929                 talloc_free(enc_ctx);
930                 return NT_STATUS_NO_MEMORY;
931         }
932
933         /* cleanup - no need to encode empty string */
934         ok = dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
935                                             &empty);
936         if (!ok) {
937                 dbus_message_unref(msg);
938                 talloc_free(enc_ctx);
939                 return NT_STATUS_NO_MEMORY;
940         }
941
942         ok = dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY,
943                                               SNAPPER_SIG_STRING_DICT,
944                                               &array_iter);
945         if (!ok) {
946                 dbus_message_unref(msg);
947                 talloc_free(enc_ctx);
948                 return NT_STATUS_NO_MEMORY;
949         }
950
951         for (i = 0; i < num_user_data; i++) {
952                 ok = dbus_message_iter_open_container(&array_iter,
953                                                       DBUS_TYPE_DICT_ENTRY,
954                                                       NULL, &struct_iter);
955                 if (!ok) {
956                         dbus_message_unref(msg);
957                         talloc_free(enc_ctx);
958                         return NT_STATUS_NO_MEMORY;
959                 }
960
961                 status = snapper_dbus_str_encode(enc_ctx, user_data[i].key,
962                                                  &str_encoded);
963                 if (!NT_STATUS_IS_OK(status)) {
964                         dbus_message_unref(msg);
965                         talloc_free(enc_ctx);
966                         return status;
967                 }
968
969                 ok = dbus_message_iter_append_basic(&struct_iter,
970                                                     DBUS_TYPE_STRING,
971                                                     &str_encoded);
972                 if (!ok) {
973                         dbus_message_unref(msg);
974                         talloc_free(enc_ctx);
975                         return NT_STATUS_NO_MEMORY;
976                 }
977
978                 status = snapper_dbus_str_encode(enc_ctx, user_data[i].val,
979                                                  &str_encoded);
980                 if (!NT_STATUS_IS_OK(status)) {
981                         dbus_message_unref(msg);
982                         talloc_free(enc_ctx);
983                         return status;
984                 }
985
986                 ok = dbus_message_iter_append_basic(&struct_iter,
987                                                     DBUS_TYPE_STRING,
988                                                     &str_encoded);
989                 if (!ok) {
990                         dbus_message_unref(msg);
991                         talloc_free(enc_ctx);
992                         return NT_STATUS_NO_MEMORY;
993                 }
994
995                 ok = dbus_message_iter_close_container(&array_iter, &struct_iter);
996                 if (!ok) {
997                         dbus_message_unref(msg);
998                         talloc_free(enc_ctx);
999                         return NT_STATUS_NO_MEMORY;
1000                 }
1001         }
1002
1003         ok = dbus_message_iter_close_container(&args, &array_iter);
1004         if (!ok) {
1005                 dbus_message_unref(msg);
1006                 talloc_free(enc_ctx);
1007                 return NT_STATUS_NO_MEMORY;
1008         }
1009
1010         *req_msg_out = msg;
1011
1012         return NT_STATUS_OK;
1013 }
1014
1015 static NTSTATUS snapper_create_snap_unpack(DBusConnection *conn,
1016                                            DBusMessage *rsp_msg,
1017                                            uint32_t *snap_id_out)
1018 {
1019         NTSTATUS status;
1020         DBusMessageIter iter;
1021         int msg_type;
1022         const char *sig;
1023         uint32_t snap_id;
1024
1025         msg_type = dbus_message_get_type(rsp_msg);
1026         if (msg_type == DBUS_MESSAGE_TYPE_ERROR) {
1027                 const char *err_str = dbus_message_get_error_name(rsp_msg);
1028                 DEBUG(0, ("create snap error response: %s, euid %d egid %d\n",
1029                           err_str, geteuid(), getegid()));
1030                 return snapper_err_ntstatus_map(err_str);
1031         }
1032
1033         if (msg_type != DBUS_MESSAGE_TYPE_METHOD_RETURN) {
1034                 DEBUG(0, ("unexpected create snap ret type: %d\n",
1035                           msg_type));
1036                 return NT_STATUS_INVALID_PARAMETER;
1037         }
1038
1039         sig = dbus_message_get_signature(rsp_msg);
1040         if ((sig == NULL)
1041          || (strcmp(sig, SNAPPER_SIG_CREATE_SNAP_RSP) != 0)) {
1042                 DEBUG(0, ("bad create snap response sig: %s, expected: %s\n",
1043                           (sig ? sig : "NULL"), SNAPPER_SIG_CREATE_SNAP_RSP));
1044                 return NT_STATUS_INVALID_PARAMETER;
1045         }
1046
1047         /* read the parameters */
1048         if (!dbus_message_iter_init(rsp_msg, &iter)) {
1049                 DEBUG(0, ("response has no arguments!\n"));
1050                 return NT_STATUS_INVALID_PARAMETER;
1051         }
1052
1053         status = snapper_type_check_get(&iter, DBUS_TYPE_UINT32, &snap_id);
1054         if (!NT_STATUS_IS_OK(status)) {
1055                 return status;
1056         }
1057         *snap_id_out = snap_id;
1058
1059         return NT_STATUS_OK;
1060 }
1061
1062 static NTSTATUS snapper_del_snap_pack(TALLOC_CTX *mem_ctx,
1063                                       const char *snapper_conf,
1064                                       uint32_t snap_id,
1065                                       DBusMessage **req_msg_out)
1066 {
1067         DBusMessage *msg;
1068         DBusMessageIter args;
1069         DBusMessageIter array_iter;
1070         char *conf_encoded;
1071         bool ok;
1072         NTSTATUS status;
1073
1074         msg = dbus_message_new_method_call("org.opensuse.Snapper",
1075                                            "/org/opensuse/Snapper",
1076                                            "org.opensuse.Snapper",
1077                                            "DeleteSnapshots");
1078         if (msg == NULL) {
1079                 DEBUG(0, ("failed to create req msg\n"));
1080                 return NT_STATUS_NO_MEMORY;
1081         }
1082
1083         status = snapper_dbus_str_encode(mem_ctx, snapper_conf, &conf_encoded);
1084         if (!NT_STATUS_IS_OK(status)) {
1085                 dbus_message_unref(msg);
1086                 return status;
1087         }
1088
1089         /* append arguments */
1090         dbus_message_iter_init_append(msg, &args);
1091         ok = dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
1092                                             &conf_encoded);
1093         if (!ok) {
1094                 talloc_free(conf_encoded);
1095                 dbus_message_unref(msg);
1096                 return NT_STATUS_NO_MEMORY;
1097         }
1098
1099         ok = dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY,
1100                                                DBUS_TYPE_UINT32_AS_STRING,
1101                                                &array_iter);
1102         if (!ok) {
1103                 talloc_free(conf_encoded);
1104                 dbus_message_unref(msg);
1105                 return NT_STATUS_NO_MEMORY;
1106         }
1107
1108         ok = dbus_message_iter_append_basic(&array_iter,
1109                                             DBUS_TYPE_UINT32,
1110                                             &snap_id);
1111         if (!ok) {
1112                 talloc_free(conf_encoded);
1113                 dbus_message_unref(msg);
1114                 return NT_STATUS_NO_MEMORY;
1115         }
1116
1117         dbus_message_iter_close_container(&args, &array_iter);
1118         *req_msg_out = msg;
1119
1120         return NT_STATUS_OK;
1121 }
1122
1123 static NTSTATUS snapper_del_snap_unpack(DBusConnection *conn,
1124                                         DBusMessage *rsp_msg)
1125 {
1126         int msg_type;
1127         const char *sig;
1128
1129         msg_type = dbus_message_get_type(rsp_msg);
1130         if (msg_type == DBUS_MESSAGE_TYPE_ERROR) {
1131                 const char *err_str = dbus_message_get_error_name(rsp_msg);
1132                 DEBUG(0, ("del snap error response: %s\n", err_str));
1133                 return snapper_err_ntstatus_map(err_str);
1134         }
1135
1136         if (msg_type != DBUS_MESSAGE_TYPE_METHOD_RETURN) {
1137                 DEBUG(0, ("unexpected del snap ret type: %d\n",
1138                           msg_type));
1139                 return NT_STATUS_INVALID_PARAMETER;
1140         }
1141
1142         sig = dbus_message_get_signature(rsp_msg);
1143         if ((sig == NULL)
1144          || (strcmp(sig, SNAPPER_SIG_DEL_SNAPS_RSP) != 0)) {
1145                 DEBUG(0, ("bad create snap response sig: %s, expected: %s\n",
1146                           (sig ? sig : "NULL"), SNAPPER_SIG_DEL_SNAPS_RSP));
1147                 return NT_STATUS_INVALID_PARAMETER;
1148         }
1149
1150         /* no parameters in response */
1151
1152         return NT_STATUS_OK;
1153 }
1154
1155 static NTSTATUS snapper_list_snaps_at_time_pack(TALLOC_CTX *mem_ctx,
1156                                                 const char *snapper_conf,
1157                                                 time_t time_lower,
1158                                                 time_t time_upper,
1159                                                 DBusMessage **req_msg_out)
1160 {
1161         DBusMessage *msg;
1162         DBusMessageIter args;
1163         char *conf_encoded;
1164         NTSTATUS status;
1165
1166         msg = dbus_message_new_method_call("org.opensuse.Snapper",
1167                                            "/org/opensuse/Snapper",
1168                                            "org.opensuse.Snapper",
1169                                            "ListSnapshotsAtTime");
1170         if (msg == NULL) {
1171                 DEBUG(0, ("failed to create list snaps message\n"));
1172                 return NT_STATUS_NO_MEMORY;
1173         }
1174
1175         status = snapper_dbus_str_encode(mem_ctx, snapper_conf, &conf_encoded);
1176         if (!NT_STATUS_IS_OK(status)) {
1177                 dbus_message_unref(msg);
1178                 return status;
1179         }
1180
1181         dbus_message_iter_init_append(msg, &args);
1182         if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
1183                                             &conf_encoded)) {
1184                 talloc_free(conf_encoded);
1185                 dbus_message_unref(msg);
1186                 return NT_STATUS_NO_MEMORY;
1187         }
1188
1189         if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT64,
1190                                             &time_lower)) {
1191                 talloc_free(conf_encoded);
1192                 dbus_message_unref(msg);
1193                 return NT_STATUS_NO_MEMORY;
1194         }
1195
1196         if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT64,
1197                                             &time_upper)) {
1198                 talloc_free(conf_encoded);
1199                 dbus_message_unref(msg);
1200                 return NT_STATUS_NO_MEMORY;
1201         }
1202
1203         *req_msg_out = msg;
1204
1205         return NT_STATUS_OK;
1206 }
1207 /* no snapper_list_snaps_at_time_unpack, use snapper_list_snaps_unpack */
1208
1209 /*
1210  * Determine the snapper snapshot id given a path.
1211  * Ideally this should be determined via a lookup.
1212  */
1213 static NTSTATUS snapper_snap_path_to_id(TALLOC_CTX *mem_ctx,
1214                                         const char *snap_path,
1215                                         uint32_t *snap_id_out)
1216 {
1217         char *path_dup;
1218         char *str_idx;
1219         char *str_end;
1220         uint32_t snap_id;
1221         int error = 0;
1222
1223         path_dup = talloc_strdup(mem_ctx, snap_path);
1224         if (path_dup == NULL) {
1225                 return NT_STATUS_NO_MEMORY;
1226         }
1227
1228         /* trim trailing '/' */
1229         str_idx = path_dup + strlen(path_dup) - 1;
1230         while (*str_idx == '/') {
1231                 *str_idx = '\0';
1232                 str_idx--;
1233         }
1234
1235         str_idx = strrchr(path_dup, '/');
1236         if ((str_idx == NULL)
1237          || (strcmp(str_idx + 1, "snapshot") != 0)) {
1238                 talloc_free(path_dup);
1239                 return NT_STATUS_INVALID_PARAMETER;
1240         }
1241
1242         while (*str_idx == '/') {
1243                 *str_idx = '\0';
1244                 str_idx--;
1245         }
1246
1247         str_idx = strrchr(path_dup, '/');
1248         if (str_idx == NULL) {
1249                 talloc_free(path_dup);
1250                 return NT_STATUS_INVALID_PARAMETER;
1251         }
1252
1253         str_idx++;
1254         snap_id = strtoul_err(str_idx, &str_end, 10, &error);
1255         if (error != 0 || str_idx == str_end) {
1256                 talloc_free(path_dup);
1257                 return NT_STATUS_INVALID_PARAMETER;
1258         }
1259
1260         talloc_free(path_dup);
1261         *snap_id_out = snap_id;
1262         return NT_STATUS_OK;
1263 }
1264
1265 /*
1266  * Determine the snapper snapshot path given an id and base.
1267  * Ideally this should be determined via a lookup.
1268  */
1269 static NTSTATUS snapper_snap_id_to_path(TALLOC_CTX *mem_ctx,
1270                                         const char *base_path,
1271                                         uint32_t snap_id,
1272                                         char **snap_path_out)
1273 {
1274         char *snap_path;
1275
1276         snap_path = talloc_asprintf(mem_ctx, "%s/.snapshots/%u/snapshot",
1277                                     base_path, snap_id);
1278         if (snap_path == NULL) {
1279                 return NT_STATUS_NO_MEMORY;
1280         }
1281
1282         *snap_path_out = snap_path;
1283         return NT_STATUS_OK;
1284 }
1285
1286 static NTSTATUS snapper_get_conf_call(TALLOC_CTX *mem_ctx,
1287                                       DBusConnection *dconn,
1288                                       const char *path,
1289                                       char **conf_name_out,
1290                                       char **base_path_out)
1291 {
1292         NTSTATUS status;
1293         DBusMessage *req_msg;
1294         DBusMessage *rsp_msg;
1295         uint32_t num_confs = 0;
1296         struct snapper_conf *confs = NULL;
1297         struct snapper_conf *conf;
1298         char *conf_name;
1299         char *base_path;
1300
1301         status = snapper_list_confs_pack(&req_msg);
1302         if (!NT_STATUS_IS_OK(status)) {
1303                 goto err_out;
1304         }
1305
1306         status = snapper_dbus_msg_xchng(dconn, req_msg, &rsp_msg);
1307         if (!NT_STATUS_IS_OK(status)) {
1308                 goto err_req_free;
1309         }
1310
1311         status = snapper_list_confs_unpack(mem_ctx, dconn, rsp_msg,
1312                                            &num_confs, &confs);
1313         if (!NT_STATUS_IS_OK(status)) {
1314                 goto err_rsp_free;
1315         }
1316
1317         /*
1318          * for now we only support shares where the path directly corresponds
1319          * to a snapper configuration.
1320          */
1321         conf = snapper_conf_array_base_find(num_confs, confs,
1322                                             path);
1323         if (conf == NULL) {
1324                 status = NT_STATUS_NOT_SUPPORTED;
1325                 goto err_array_free;
1326         }
1327
1328         conf_name = talloc_strdup(mem_ctx, conf->name);
1329         if (conf_name == NULL) {
1330                 status = NT_STATUS_NO_MEMORY;
1331                 goto err_array_free;
1332         }
1333         base_path = talloc_strdup(mem_ctx, conf->mnt);
1334         if (base_path == NULL) {
1335                 status = NT_STATUS_NO_MEMORY;
1336                 goto err_conf_name_free;
1337         }
1338
1339         talloc_free(confs);
1340         dbus_message_unref(rsp_msg);
1341         dbus_message_unref(req_msg);
1342
1343         *conf_name_out = conf_name;
1344         *base_path_out = base_path;
1345
1346         return NT_STATUS_OK;
1347
1348 err_conf_name_free:
1349         talloc_free(conf_name);
1350 err_array_free:
1351         talloc_free(confs);
1352 err_rsp_free:
1353         dbus_message_unref(rsp_msg);
1354 err_req_free:
1355         dbus_message_unref(req_msg);
1356 err_out:
1357         return status;
1358 }
1359
1360 /*
1361  * Check whether a path can be shadow copied. Return the base volume, allowing
1362  * the caller to determine if multiple paths lie on the same base volume.
1363  */
1364 static NTSTATUS snapper_snap_check_path(struct vfs_handle_struct *handle,
1365                                         TALLOC_CTX *mem_ctx,
1366                                         const char *service_path,
1367                                         char **base_volume)
1368 {
1369         NTSTATUS status;
1370         DBusConnection *dconn;
1371         char *conf_name;
1372         char *base_path;
1373
1374         dconn = snapper_dbus_conn_create();
1375         if (dconn == NULL) {
1376                 return NT_STATUS_UNSUCCESSFUL;
1377         }
1378
1379         status = snapper_get_conf_call(mem_ctx, dconn, service_path,
1380                                        &conf_name, &base_path);
1381         if (!NT_STATUS_IS_OK(status)) {
1382                 goto err_conn_close;
1383         }
1384
1385         talloc_free(conf_name);
1386         *base_volume = base_path;
1387         snapper_dbus_conn_destroy(dconn);
1388
1389         return NT_STATUS_OK;
1390
1391 err_conn_close:
1392         snapper_dbus_conn_destroy(dconn);
1393         return status;
1394 }
1395
1396 static NTSTATUS snapper_create_snap_call(TALLOC_CTX *mem_ctx,
1397                                          DBusConnection *dconn,
1398                                          const char *conf_name,
1399                                          const char *base_path,
1400                                          const char *snap_desc,
1401                                          uint32_t num_user_data,
1402                                          struct snapper_dict *user_data,
1403                                          char **snap_path_out)
1404 {
1405         NTSTATUS status;
1406         DBusMessage *req_msg;
1407         DBusMessage *rsp_msg;
1408         uint32_t snap_id = 0;
1409         char *snap_path;
1410
1411         status = snapper_create_snap_pack(mem_ctx,
1412                                           conf_name,
1413                                           snap_desc,
1414                                           num_user_data,
1415                                           user_data,
1416                                           &req_msg);
1417         if (!NT_STATUS_IS_OK(status)) {
1418                 goto err_out;
1419         }
1420
1421         status = snapper_dbus_msg_xchng(dconn, req_msg, &rsp_msg);
1422         if (!NT_STATUS_IS_OK(status)) {
1423                 goto err_req_free;
1424         }
1425
1426         status = snapper_create_snap_unpack(dconn, rsp_msg, &snap_id);
1427         if (!NT_STATUS_IS_OK(status)) {
1428                 goto err_rsp_free;
1429         }
1430
1431         status = snapper_snap_id_to_path(mem_ctx, base_path, snap_id,
1432                                          &snap_path);
1433         if (!NT_STATUS_IS_OK(status)) {
1434                 goto err_rsp_free;
1435         }
1436
1437         dbus_message_unref(rsp_msg);
1438         dbus_message_unref(req_msg);
1439
1440         DEBUG(6, ("created new snapshot %u at %s\n", snap_id, snap_path));
1441         *snap_path_out = snap_path;
1442
1443         return NT_STATUS_OK;
1444
1445 err_rsp_free:
1446         dbus_message_unref(rsp_msg);
1447 err_req_free:
1448         dbus_message_unref(req_msg);
1449 err_out:
1450         return status;
1451 }
1452
1453 static NTSTATUS snapper_snap_create(struct vfs_handle_struct *handle,
1454                                     TALLOC_CTX *mem_ctx,
1455                                     const char *base_volume,
1456                                     time_t *tstamp,
1457                                     bool rw,
1458                                     char **_base_path,
1459                                     char **_snap_path)
1460 {
1461         DBusConnection *dconn;
1462         NTSTATUS status;
1463         char *conf_name;
1464         char *base_path;
1465         char *snap_path = NULL;
1466         TALLOC_CTX *tmp_ctx;
1467
1468         tmp_ctx = talloc_new(mem_ctx);
1469         if (tmp_ctx == NULL) {
1470                 return NT_STATUS_NO_MEMORY;
1471         }
1472
1473         dconn = snapper_dbus_conn_create();
1474         if (dconn == NULL) {
1475                 talloc_free(tmp_ctx);
1476                 return NT_STATUS_UNSUCCESSFUL;
1477         }
1478
1479         status = snapper_get_conf_call(tmp_ctx, dconn, base_volume,
1480                                        &conf_name, &base_path);
1481         if (!NT_STATUS_IS_OK(status)) {
1482                 snapper_dbus_conn_destroy(dconn);
1483                 talloc_free(tmp_ctx);
1484                 return status;
1485         }
1486
1487         status = snapper_create_snap_call(tmp_ctx, dconn,
1488                                           conf_name, base_path,
1489                                           "Snapshot created by Samba",
1490                                           0, NULL,
1491                                           &snap_path);
1492         if (!NT_STATUS_IS_OK(status)) {
1493                 snapper_dbus_conn_destroy(dconn);
1494                 talloc_free(tmp_ctx);
1495                 return status;
1496         }
1497
1498         snapper_dbus_conn_destroy(dconn);
1499         *_base_path = talloc_steal(mem_ctx, base_path);
1500         *_snap_path = talloc_steal(mem_ctx, snap_path);
1501         talloc_free(tmp_ctx);
1502
1503         return NT_STATUS_OK;
1504 }
1505
1506 static NTSTATUS snapper_delete_snap_call(TALLOC_CTX *mem_ctx,
1507                                          DBusConnection *dconn,
1508                                          const char *conf_name,
1509                                          uint32_t snap_id)
1510 {
1511         NTSTATUS status;
1512         DBusMessage *req_msg = NULL;
1513         DBusMessage *rsp_msg;
1514
1515         status = snapper_del_snap_pack(mem_ctx, conf_name, snap_id, &req_msg);
1516         if (!NT_STATUS_IS_OK(status)) {
1517                 goto err_out;
1518         }
1519
1520         status = snapper_dbus_msg_xchng(dconn, req_msg, &rsp_msg);
1521         if (!NT_STATUS_IS_OK(status)) {
1522                 goto err_req_free;
1523         }
1524
1525         status = snapper_del_snap_unpack(dconn, rsp_msg);
1526         if (!NT_STATUS_IS_OK(status)) {
1527                 goto err_rsp_free;
1528         }
1529
1530         dbus_message_unref(rsp_msg);
1531         dbus_message_unref(req_msg);
1532
1533         DEBUG(6, ("deleted snapshot %u\n", snap_id));
1534
1535         return NT_STATUS_OK;
1536
1537 err_rsp_free:
1538         dbus_message_unref(rsp_msg);
1539 err_req_free:
1540         dbus_message_unref(req_msg);
1541 err_out:
1542         return status;
1543 }
1544
1545 static NTSTATUS snapper_snap_delete(struct vfs_handle_struct *handle,
1546                                     TALLOC_CTX *mem_ctx,
1547                                     char *base_path,
1548                                     char *snap_path)
1549 {
1550         DBusConnection *dconn;
1551         NTSTATUS status;
1552         char *conf_name;
1553         char *snap_base_path;
1554         uint32_t snap_id;
1555         TALLOC_CTX *tmp_ctx;
1556
1557         tmp_ctx = talloc_new(mem_ctx);
1558         if (tmp_ctx == NULL) {
1559                 return NT_STATUS_NO_MEMORY;
1560         }
1561
1562         dconn = snapper_dbus_conn_create();
1563         if (dconn == NULL) {
1564                 talloc_free(tmp_ctx);
1565                 return NT_STATUS_UNSUCCESSFUL;
1566         }
1567
1568         status = snapper_get_conf_call(tmp_ctx, dconn, base_path,
1569                                        &conf_name, &snap_base_path);
1570         if (!NT_STATUS_IS_OK(status)) {
1571                 snapper_dbus_conn_destroy(dconn);
1572                 talloc_free(tmp_ctx);
1573                 return status;
1574         }
1575
1576         status = snapper_snap_path_to_id(tmp_ctx, snap_path, &snap_id);
1577         if (!NT_STATUS_IS_OK(status)) {
1578                 snapper_dbus_conn_destroy(dconn);
1579                 talloc_free(tmp_ctx);
1580                 return status;
1581         }
1582
1583         status = snapper_delete_snap_call(tmp_ctx, dconn, conf_name, snap_id);
1584         if (!NT_STATUS_IS_OK(status)) {
1585                 snapper_dbus_conn_destroy(dconn);
1586                 talloc_free(tmp_ctx);
1587                 return status;
1588         }
1589
1590         snapper_dbus_conn_destroy(dconn);
1591         talloc_free(tmp_ctx);
1592
1593         return NT_STATUS_OK;
1594 }
1595
1596 /* sc_data used as parent talloc context for all labels */
1597 static int snapper_get_shadow_copy_data(struct vfs_handle_struct *handle,
1598                                         struct files_struct *fsp,
1599                                         struct shadow_copy_data *sc_data,
1600                                         bool labels)
1601 {
1602         DBusConnection *dconn;
1603         TALLOC_CTX *tmp_ctx;
1604         NTSTATUS status;
1605         char *conf_name;
1606         char *base_path;
1607         DBusMessage *req_msg = NULL;
1608         DBusMessage *rsp_msg;
1609         uint32_t num_snaps;
1610         struct snapper_snap *snaps;
1611         uint32_t i;
1612         uint32_t lbl_off;
1613
1614         tmp_ctx = talloc_new(sc_data);
1615         if (tmp_ctx == NULL) {
1616                 status = NT_STATUS_NO_MEMORY;
1617                 goto err_out;
1618         }
1619
1620         dconn = snapper_dbus_conn_create();
1621         if (dconn == NULL) {
1622                 status = NT_STATUS_UNSUCCESSFUL;
1623                 goto err_mem_ctx_free;
1624         }
1625
1626         if (fsp->conn->connectpath == NULL) {
1627                 status = NT_STATUS_INVALID_PARAMETER;
1628                 goto err_conn_free;
1629         }
1630
1631         status = snapper_get_conf_call(tmp_ctx, dconn,
1632                                        fsp->conn->connectpath,
1633                                        &conf_name,
1634                                        &base_path);
1635         if (!NT_STATUS_IS_OK(status)) {
1636                 goto err_conn_free;
1637         }
1638
1639         status = snapper_list_snaps_pack(tmp_ctx, conf_name, &req_msg);
1640         if (!NT_STATUS_IS_OK(status)) {
1641                 goto err_conn_free;
1642         }
1643
1644         status = snapper_dbus_msg_xchng(dconn, req_msg, &rsp_msg);
1645         if (!NT_STATUS_IS_OK(status)) {
1646                 goto err_req_free;
1647         }
1648
1649         status = snapper_list_snaps_unpack(tmp_ctx, rsp_msg,
1650                                            &num_snaps, &snaps);
1651         if (!NT_STATUS_IS_OK(status)) {
1652                 goto err_rsp_free;
1653         }
1654         /* we should always get at least one snapshot (current) */
1655         if (num_snaps == 0) {
1656                 DEBUG(1, ("zero snapshots in snap list response\n"));
1657                 status = NT_STATUS_UNSUCCESSFUL;
1658                 goto err_rsp_free;
1659         }
1660
1661         /* subtract 1, (current) snapshot is not returned */
1662         sc_data->num_volumes = num_snaps - 1;
1663         sc_data->labels = NULL;
1664
1665         if ((labels == false) || (sc_data->num_volumes == 0)) {
1666                 /* tokens need not be added to the labels array */
1667                 goto done;
1668         }
1669
1670         sc_data->labels = talloc_array(sc_data, SHADOW_COPY_LABEL,
1671                                        sc_data->num_volumes);
1672         if (sc_data->labels == NULL) {
1673                 status = NT_STATUS_NO_MEMORY;
1674                 goto err_rsp_free;
1675         }
1676
1677         /* start at end for decending order, do not include 0 (current) */
1678         lbl_off = 0;
1679         for (i = num_snaps - 1; i > 0; i--) {
1680                 char *lbl = sc_data->labels[lbl_off++];
1681                 struct tm gmt_snap_time;
1682                 struct tm *tm_ret;
1683                 size_t str_sz;
1684
1685                 tm_ret = gmtime_r((time_t *)&snaps[i].time, &gmt_snap_time);
1686                 if (tm_ret == NULL) {
1687                         status = NT_STATUS_UNSUCCESSFUL;
1688                         goto err_labels_free;
1689                 }
1690                 str_sz = strftime(lbl, sizeof(SHADOW_COPY_LABEL),
1691                                   "@GMT-%Y.%m.%d-%H.%M.%S", &gmt_snap_time);
1692                 if (str_sz == 0) {
1693                         status = NT_STATUS_UNSUCCESSFUL;
1694                         goto err_labels_free;
1695                 }
1696         }
1697
1698 done:
1699         talloc_free(tmp_ctx);
1700         dbus_message_unref(rsp_msg);
1701         dbus_message_unref(req_msg);
1702         snapper_dbus_conn_destroy(dconn);
1703
1704         return 0;
1705
1706 err_labels_free:
1707         TALLOC_FREE(sc_data->labels);
1708 err_rsp_free:
1709         dbus_message_unref(rsp_msg);
1710 err_req_free:
1711         dbus_message_unref(req_msg);
1712 err_conn_free:
1713         snapper_dbus_conn_destroy(dconn);
1714 err_mem_ctx_free:
1715         talloc_free(tmp_ctx);
1716 err_out:
1717         errno = map_errno_from_nt_status(status);
1718         return -1;
1719 }
1720
1721 static bool snapper_gmt_strip_snapshot(TALLOC_CTX *mem_ctx,
1722                                        struct vfs_handle_struct *handle,
1723                                        const char *name,
1724                                        time_t *ptimestamp,
1725                                        char **pstripped)
1726 {
1727         struct tm tm;
1728         time_t timestamp;
1729         const char *p;
1730         char *q;
1731         char *stripped;
1732         size_t rest_len, dst_len;
1733         ptrdiff_t len_before_gmt;
1734
1735         p = strstr_m(name, "@GMT-");
1736         if (p == NULL) {
1737                 goto no_snapshot;
1738         }
1739         if ((p > name) && (p[-1] != '/')) {
1740                 goto no_snapshot;
1741         }
1742         len_before_gmt = p - name;
1743         q = strptime(p, GMT_FORMAT, &tm);
1744         if (q == NULL) {
1745                 goto no_snapshot;
1746         }
1747         tm.tm_isdst = -1;
1748         timestamp = timegm(&tm);
1749         if (timestamp == (time_t)-1) {
1750                 goto no_snapshot;
1751         }
1752         if (q[0] == '\0') {
1753                 /*
1754                  * The name consists of only the GMT token or the GMT
1755                  * token is at the end of the path. XP seems to send
1756                  * @GMT- at the end under certain circumstances even
1757                  * with a path prefix.
1758                  */
1759                 if (pstripped != NULL) {
1760                         if (len_before_gmt > 0) {
1761                                 /*
1762                                  * There is a slash before
1763                                  * the @GMT-. Remove it.
1764                                  */
1765                                 len_before_gmt -= 1;
1766                         }
1767                         stripped = talloc_strndup(mem_ctx, name,
1768                                         len_before_gmt);
1769                         if (stripped == NULL) {
1770                                 return false;
1771                         }
1772                         *pstripped = stripped;
1773                 }
1774                 *ptimestamp = timestamp;
1775                 return true;
1776         }
1777         if (q[0] != '/') {
1778                 /*
1779                  * It is not a complete path component, i.e. the path
1780                  * component continues after the gmt-token.
1781                  */
1782                 goto no_snapshot;
1783         }
1784         q += 1;
1785
1786         rest_len = strlen(q);
1787         dst_len = len_before_gmt + rest_len;
1788
1789         if (pstripped != NULL) {
1790                 stripped = talloc_array(mem_ctx, char, dst_len+1);
1791                 if (stripped == NULL) {
1792                         errno = ENOMEM;
1793                         return false;
1794                 }
1795                 if (p > name) {
1796                         memcpy(stripped, name, len_before_gmt);
1797                 }
1798                 if (rest_len > 0) {
1799                         memcpy(stripped + len_before_gmt, q, rest_len);
1800                 }
1801                 stripped[dst_len] = '\0';
1802                 *pstripped = stripped;
1803         }
1804         *ptimestamp = timestamp;
1805         return true;
1806 no_snapshot:
1807         *ptimestamp = 0;
1808         return true;
1809 }
1810
1811 static NTSTATUS snapper_get_snap_at_time_call(TALLOC_CTX *mem_ctx,
1812                                               DBusConnection *dconn,
1813                                               const char *conf_name,
1814                                               const char *base_path,
1815                                               time_t snaptime,
1816                                               char **snap_path_out)
1817 {
1818         NTSTATUS status;
1819         DBusMessage *req_msg = NULL;
1820         DBusMessage *rsp_msg;
1821         uint32_t num_snaps;
1822         struct snapper_snap *snaps;
1823         char *snap_path;
1824
1825         status = snapper_list_snaps_at_time_pack(mem_ctx,
1826                                                  conf_name,
1827                                                  snaptime,
1828                                                  snaptime,
1829                                                  &req_msg);
1830         if (!NT_STATUS_IS_OK(status)) {
1831                 goto err_out;
1832         }
1833
1834         status = snapper_dbus_msg_xchng(dconn, req_msg, &rsp_msg);
1835         if (!NT_STATUS_IS_OK(status)) {
1836                 goto err_req_free;
1837         }
1838
1839         status = snapper_list_snaps_unpack(mem_ctx, rsp_msg,
1840                                            &num_snaps, &snaps);
1841         if (!NT_STATUS_IS_OK(status)) {
1842                 goto err_rsp_free;
1843         }
1844
1845         if (num_snaps == 0) {
1846                 DEBUG(4, ("no snapshots found with time: %lu\n",
1847                           (unsigned long)snaptime));
1848                 status = NT_STATUS_INVALID_PARAMETER;
1849                 goto err_snap_array_free;
1850         } else if (num_snaps > 0) {
1851                 DEBUG(4, ("got %u snapshots for single time %lu, using top\n",
1852                           num_snaps, (unsigned long)snaptime));
1853         }
1854
1855         status = snapper_snap_id_to_path(mem_ctx, base_path, snaps[0].id,
1856                                          &snap_path);
1857         if (!NT_STATUS_IS_OK(status)) {
1858                 goto err_snap_array_free;
1859         }
1860
1861         *snap_path_out = snap_path;
1862 err_snap_array_free:
1863         talloc_free(snaps);
1864 err_rsp_free:
1865         dbus_message_unref(rsp_msg);
1866 err_req_free:
1867         dbus_message_unref(req_msg);
1868 err_out:
1869         return status;
1870 }
1871
1872 static NTSTATUS snapper_snap_path_expand(struct connection_struct *conn,
1873                                          TALLOC_CTX *mem_ctx,
1874                                          time_t snap_time,
1875                                          char **snap_dir_out)
1876 {
1877         DBusConnection *dconn;
1878         NTSTATUS status;
1879         char *conf_name;
1880         char *base_path;
1881         char *snap_path = NULL;
1882
1883         dconn = snapper_dbus_conn_create();
1884         if (dconn == NULL) {
1885                 status = NT_STATUS_UNSUCCESSFUL;
1886                 goto err_out;
1887         }
1888
1889         if (conn->connectpath == NULL) {
1890                 status = NT_STATUS_INVALID_PARAMETER;
1891                 goto err_conn_free;
1892         }
1893
1894         status = snapper_get_conf_call(mem_ctx, dconn,
1895                                        conn->connectpath,
1896                                        &conf_name,
1897                                        &base_path);
1898         if (!NT_STATUS_IS_OK(status)) {
1899                 goto err_conn_free;
1900         }
1901
1902         status = snapper_get_snap_at_time_call(mem_ctx, dconn,
1903                                                conf_name, base_path, snap_time,
1904                                                &snap_path);
1905         if (!NT_STATUS_IS_OK(status)) {
1906                 goto err_conf_name_free;
1907         }
1908
1909         /* confirm snapshot path is nested under base path */
1910         if (strncmp(snap_path, base_path, strlen(base_path)) != 0) {
1911                 status = NT_STATUS_INVALID_PARAMETER;
1912                 goto err_snap_path_free;
1913         }
1914
1915         talloc_free(conf_name);
1916         talloc_free(base_path);
1917         snapper_dbus_conn_destroy(dconn);
1918         *snap_dir_out = snap_path;
1919
1920         return NT_STATUS_OK;
1921
1922 err_snap_path_free:
1923         talloc_free(snap_path);
1924 err_conf_name_free:
1925         talloc_free(conf_name);
1926         talloc_free(base_path);
1927 err_conn_free:
1928         snapper_dbus_conn_destroy(dconn);
1929 err_out:
1930         return status;
1931 }
1932
1933 static char *snapper_gmt_convert(TALLOC_CTX *mem_ctx,
1934                              struct vfs_handle_struct *handle,
1935                              const char *name, time_t timestamp)
1936 {
1937         char *snap_path = NULL;
1938         char *path = NULL;
1939         NTSTATUS status;
1940         int saved_errno;
1941
1942         status = snapper_snap_path_expand(handle->conn, mem_ctx, timestamp,
1943                                           &snap_path);
1944         if (!NT_STATUS_IS_OK(status)) {
1945                 errno = map_errno_from_nt_status(status);
1946                 goto err_out;
1947         }
1948
1949         path = talloc_asprintf(mem_ctx, "%s/%s", snap_path, name);
1950         if (path == NULL) {
1951                 errno = ENOMEM;
1952                 goto err_snap_path_free;
1953         }
1954
1955         DEBUG(10, ("converted %s/%s @ time to %s\n",
1956                    handle->conn->connectpath, name, path));
1957         return path;
1958
1959 err_snap_path_free:
1960         saved_errno = errno;
1961         talloc_free(snap_path);
1962         errno = saved_errno;
1963 err_out:
1964         return NULL;
1965 }
1966
1967 static DIR *snapper_gmt_opendir(vfs_handle_struct *handle,
1968                                 const struct smb_filename *smb_fname,
1969                                 const char *mask,
1970                                 uint32_t attr)
1971 {
1972         time_t timestamp;
1973         char *stripped;
1974         DIR *ret;
1975         int saved_errno;
1976         char *conv;
1977         struct smb_filename *conv_smb_fname = NULL;
1978
1979         if (!snapper_gmt_strip_snapshot(talloc_tos(),
1980                         handle,
1981                         smb_fname->base_name,
1982                         &timestamp,
1983                         &stripped)) {
1984                 return NULL;
1985         }
1986         if (timestamp == 0) {
1987                 return SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask, attr);
1988         }
1989         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1990         TALLOC_FREE(stripped);
1991         if (conv == NULL) {
1992                 return NULL;
1993         }
1994         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1995                                         conv,
1996                                         NULL,
1997                                         NULL,
1998                                         smb_fname->flags);
1999         if (conv_smb_fname == NULL) {
2000                 TALLOC_FREE(conv);
2001                 errno = ENOMEM;
2002                 return NULL;
2003         }
2004
2005         ret = SMB_VFS_NEXT_OPENDIR(handle, conv_smb_fname, mask, attr);
2006         saved_errno = errno;
2007         TALLOC_FREE(conv);
2008         TALLOC_FREE(conv_smb_fname);
2009         errno = saved_errno;
2010         return ret;
2011 }
2012
2013 static int snapper_gmt_rename(vfs_handle_struct *handle,
2014                               const struct smb_filename *smb_fname_src,
2015                               const struct smb_filename *smb_fname_dst)
2016 {
2017         time_t timestamp_src, timestamp_dst;
2018
2019         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2020                                         smb_fname_src->base_name,
2021                                         &timestamp_src, NULL)) {
2022                 return -1;
2023         }
2024         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2025                                         smb_fname_dst->base_name,
2026                                         &timestamp_dst, NULL)) {
2027                 return -1;
2028         }
2029         if (timestamp_src != 0) {
2030                 errno = EXDEV;
2031                 return -1;
2032         }
2033         if (timestamp_dst != 0) {
2034                 errno = EROFS;
2035                 return -1;
2036         }
2037         return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
2038 }
2039
2040 static int snapper_gmt_symlink(vfs_handle_struct *handle,
2041                                 const char *link_contents,
2042                                 const struct smb_filename *new_smb_fname)
2043 {
2044         time_t timestamp_old = 0;
2045         time_t timestamp_new = 0;
2046
2047         if (!snapper_gmt_strip_snapshot(talloc_tos(),
2048                                 handle,
2049                                 link_contents,
2050                                 &timestamp_old,
2051                                 NULL)) {
2052                 return -1;
2053         }
2054         if (!snapper_gmt_strip_snapshot(talloc_tos(),
2055                                 handle,
2056                                 new_smb_fname->base_name,
2057                                 &timestamp_new,
2058                                 NULL)) {
2059                 return -1;
2060         }
2061         if ((timestamp_old != 0) || (timestamp_new != 0)) {
2062                 errno = EROFS;
2063                 return -1;
2064         }
2065         return SMB_VFS_NEXT_SYMLINK(handle, link_contents, new_smb_fname);
2066 }
2067
2068 static int snapper_gmt_link(vfs_handle_struct *handle,
2069                                 const struct smb_filename *old_smb_fname,
2070                                 const struct smb_filename *new_smb_fname)
2071 {
2072         time_t timestamp_old = 0;
2073         time_t timestamp_new = 0;
2074
2075         if (!snapper_gmt_strip_snapshot(talloc_tos(),
2076                                 handle,
2077                                 old_smb_fname->base_name,
2078                                 &timestamp_old,
2079                                 NULL)) {
2080                 return -1;
2081         }
2082         if (!snapper_gmt_strip_snapshot(talloc_tos(),
2083                                 handle,
2084                                 new_smb_fname->base_name,
2085                                 &timestamp_new,
2086                                 NULL)) {
2087                 return -1;
2088         }
2089         if ((timestamp_old != 0) || (timestamp_new != 0)) {
2090                 errno = EROFS;
2091                 return -1;
2092         }
2093         return SMB_VFS_NEXT_LINK(handle, old_smb_fname, new_smb_fname);
2094 }
2095
2096 static int snapper_gmt_stat(vfs_handle_struct *handle,
2097                             struct smb_filename *smb_fname)
2098 {
2099         time_t timestamp;
2100         char *stripped, *tmp;
2101         int ret, saved_errno;
2102
2103         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2104                                         smb_fname->base_name,
2105                                         &timestamp, &stripped)) {
2106                 return -1;
2107         }
2108         if (timestamp == 0) {
2109                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
2110         }
2111
2112         tmp = smb_fname->base_name;
2113         smb_fname->base_name = snapper_gmt_convert(talloc_tos(), handle,
2114                                                    stripped, timestamp);
2115         TALLOC_FREE(stripped);
2116
2117         if (smb_fname->base_name == NULL) {
2118                 smb_fname->base_name = tmp;
2119                 return -1;
2120         }
2121
2122         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
2123         saved_errno = errno;
2124
2125         TALLOC_FREE(smb_fname->base_name);
2126         smb_fname->base_name = tmp;
2127
2128         errno = saved_errno;
2129         return ret;
2130 }
2131
2132 static int snapper_gmt_lstat(vfs_handle_struct *handle,
2133                              struct smb_filename *smb_fname)
2134 {
2135         time_t timestamp;
2136         char *stripped, *tmp;
2137         int ret, saved_errno;
2138
2139         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2140                                         smb_fname->base_name,
2141                                         &timestamp, &stripped)) {
2142                 return -1;
2143         }
2144         if (timestamp == 0) {
2145                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2146         }
2147
2148         tmp = smb_fname->base_name;
2149         smb_fname->base_name = snapper_gmt_convert(talloc_tos(), handle,
2150                                                    stripped, timestamp);
2151         TALLOC_FREE(stripped);
2152
2153         if (smb_fname->base_name == NULL) {
2154                 smb_fname->base_name = tmp;
2155                 return -1;
2156         }
2157
2158         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2159         saved_errno = errno;
2160
2161         TALLOC_FREE(smb_fname->base_name);
2162         smb_fname->base_name = tmp;
2163
2164         errno = saved_errno;
2165         return ret;
2166 }
2167
2168 static int snapper_gmt_fstat(vfs_handle_struct *handle, files_struct *fsp,
2169                              SMB_STRUCT_STAT *sbuf)
2170 {
2171         time_t timestamp;
2172         int ret;
2173
2174         ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
2175         if (ret == -1) {
2176                 return ret;
2177         }
2178         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2179                                         fsp->fsp_name->base_name,
2180                                         &timestamp, NULL)) {
2181                 return 0;
2182         }
2183         return 0;
2184 }
2185
2186 static int snapper_gmt_open(vfs_handle_struct *handle,
2187                             struct smb_filename *smb_fname, files_struct *fsp,
2188                             int flags, mode_t mode)
2189 {
2190         time_t timestamp;
2191         char *stripped, *tmp;
2192         int ret, saved_errno;
2193
2194         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2195                                         smb_fname->base_name,
2196                                         &timestamp, &stripped)) {
2197                 return -1;
2198         }
2199         if (timestamp == 0) {
2200                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2201         }
2202
2203         tmp = smb_fname->base_name;
2204         smb_fname->base_name = snapper_gmt_convert(talloc_tos(), handle,
2205                                                    stripped, timestamp);
2206         TALLOC_FREE(stripped);
2207
2208         if (smb_fname->base_name == NULL) {
2209                 smb_fname->base_name = tmp;
2210                 return -1;
2211         }
2212
2213         ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2214         saved_errno = errno;
2215
2216         TALLOC_FREE(smb_fname->base_name);
2217         smb_fname->base_name = tmp;
2218
2219         errno = saved_errno;
2220         return ret;
2221 }
2222
2223 static int snapper_gmt_unlink(vfs_handle_struct *handle,
2224                               const struct smb_filename *smb_fname)
2225 {
2226         time_t timestamp;
2227         char *stripped;
2228         int ret, saved_errno;
2229         struct smb_filename *conv;
2230
2231         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2232                                         smb_fname->base_name,
2233                                         &timestamp, &stripped)) {
2234                 return -1;
2235         }
2236         if (timestamp == 0) {
2237                 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2238         }
2239         conv = cp_smb_filename(talloc_tos(), smb_fname);
2240         if (conv == NULL) {
2241                 errno = ENOMEM;
2242                 return -1;
2243         }
2244         conv->base_name = snapper_gmt_convert(conv, handle,
2245                                               stripped, timestamp);
2246         TALLOC_FREE(stripped);
2247         if (conv->base_name == NULL) {
2248                 return -1;
2249         }
2250         ret = SMB_VFS_NEXT_UNLINK(handle, conv);
2251         saved_errno = errno;
2252         TALLOC_FREE(conv);
2253         errno = saved_errno;
2254         return ret;
2255 }
2256
2257 static int snapper_gmt_chmod(vfs_handle_struct *handle,
2258                         const struct smb_filename *smb_fname,
2259                         mode_t mode)
2260 {
2261         time_t timestamp;
2262         char *stripped = NULL;
2263         int ret, saved_errno;
2264         char *conv = NULL;
2265         struct smb_filename *conv_smb_fname = NULL;
2266
2267         if (!snapper_gmt_strip_snapshot(talloc_tos(),
2268                                 handle,
2269                                 smb_fname->base_name,
2270                                 &timestamp,
2271                                 &stripped)) {
2272                 return -1;
2273         }
2274         if (timestamp == 0) {
2275                 TALLOC_FREE(stripped);
2276                 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
2277         }
2278         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2279         TALLOC_FREE(stripped);
2280         if (conv == NULL) {
2281                 return -1;
2282         }
2283         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2284                                         conv,
2285                                         NULL,
2286                                         NULL,
2287                                         smb_fname->flags);
2288         if (conv_smb_fname == NULL) {
2289                 TALLOC_FREE(conv);
2290                 errno = ENOMEM;
2291                 return -1;
2292         }
2293
2294         ret = SMB_VFS_NEXT_CHMOD(handle, conv_smb_fname, mode);
2295         saved_errno = errno;
2296         TALLOC_FREE(conv);
2297         TALLOC_FREE(conv_smb_fname);
2298         errno = saved_errno;
2299         return ret;
2300 }
2301
2302 static int snapper_gmt_chown(vfs_handle_struct *handle,
2303                         const struct smb_filename *smb_fname,
2304                         uid_t uid,
2305                         gid_t gid)
2306 {
2307         time_t timestamp;
2308         char *stripped = NULL;
2309         int ret, saved_errno;
2310         char *conv = NULL;
2311         struct smb_filename *conv_smb_fname = NULL;
2312
2313         if (!snapper_gmt_strip_snapshot(talloc_tos(),
2314                                 handle,
2315                                 smb_fname->base_name,
2316                                 &timestamp,
2317                                 &stripped)) {
2318                 return -1;
2319         }
2320         if (timestamp == 0) {
2321                 TALLOC_FREE(stripped);
2322                 return SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
2323         }
2324         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2325         TALLOC_FREE(stripped);
2326         if (conv == NULL) {
2327                 return -1;
2328         }
2329         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2330                                         conv,
2331                                         NULL,
2332                                         NULL,
2333                                         smb_fname->flags);
2334         if (conv_smb_fname == NULL) {
2335                 TALLOC_FREE(conv);
2336                 errno = ENOMEM;
2337                 return -1;
2338         }
2339         ret = SMB_VFS_NEXT_CHOWN(handle, conv_smb_fname, uid, gid);
2340         saved_errno = errno;
2341         TALLOC_FREE(conv);
2342         TALLOC_FREE(conv_smb_fname);
2343         errno = saved_errno;
2344         return ret;
2345 }
2346
2347 static int snapper_gmt_chdir(vfs_handle_struct *handle,
2348                         const struct smb_filename *smb_fname)
2349 {
2350         time_t timestamp = 0;
2351         char *stripped = NULL;
2352         int ret;
2353         int saved_errno = 0;
2354         char *conv = NULL;
2355         struct smb_filename *conv_smb_fname = NULL;
2356
2357         if (!snapper_gmt_strip_snapshot(talloc_tos(),
2358                                 handle,
2359                                 smb_fname->base_name,
2360                                 &timestamp,
2361                                 &stripped)) {
2362                 return -1;
2363         }
2364         if (timestamp == 0) {
2365                 return SMB_VFS_NEXT_CHDIR(handle, smb_fname);
2366         }
2367         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2368         TALLOC_FREE(stripped);
2369         if (conv == NULL) {
2370                 return -1;
2371         }
2372         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2373                                         conv,
2374                                         NULL,
2375                                         NULL,
2376                                         smb_fname->flags);
2377         if (conv_smb_fname == NULL) {
2378                 TALLOC_FREE(conv);
2379                 errno = ENOMEM;
2380                 return -1;
2381         }
2382         ret = SMB_VFS_NEXT_CHDIR(handle, conv_smb_fname);
2383         if (ret == -1) {
2384                 saved_errno = errno;
2385         }
2386         TALLOC_FREE(conv);
2387         TALLOC_FREE(conv_smb_fname);
2388         if (saved_errno != 0) {
2389                 errno = saved_errno;
2390         }
2391         return ret;
2392 }
2393
2394 static int snapper_gmt_ntimes(vfs_handle_struct *handle,
2395                               const struct smb_filename *smb_fname,
2396                               struct smb_file_time *ft)
2397 {
2398         time_t timestamp;
2399         char *stripped;
2400         int ret, saved_errno;
2401         struct smb_filename *conv;
2402
2403         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2404                                         smb_fname->base_name,
2405                                         &timestamp, &stripped)) {
2406                 return -1;
2407         }
2408         if (timestamp == 0) {
2409                 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
2410         }
2411         conv = cp_smb_filename(talloc_tos(), smb_fname);
2412         if (conv == NULL) {
2413                 errno = ENOMEM;
2414                 return -1;
2415         }
2416         conv->base_name = snapper_gmt_convert(conv, handle,
2417                                               stripped, timestamp);
2418         TALLOC_FREE(stripped);
2419         if (conv->base_name == NULL) {
2420                 return -1;
2421         }
2422         ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
2423         saved_errno = errno;
2424         TALLOC_FREE(conv);
2425         errno = saved_errno;
2426         return ret;
2427 }
2428
2429 static int snapper_gmt_readlink(vfs_handle_struct *handle,
2430                                 const struct smb_filename *smb_fname,
2431                                 char *buf,
2432                                 size_t bufsiz)
2433 {
2434         time_t timestamp = 0;
2435         char *stripped = NULL;
2436         int ret;
2437         int saved_errno = 0;
2438         struct smb_filename *conv = NULL;
2439
2440         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2441                                         smb_fname->base_name,
2442                                         &timestamp, &stripped)) {
2443                 return -1;
2444         }
2445         if (timestamp == 0) {
2446                 return SMB_VFS_NEXT_READLINK(handle, smb_fname, buf, bufsiz);
2447         }
2448         conv = cp_smb_filename(talloc_tos(), smb_fname);
2449         if (conv == NULL) {
2450                 TALLOC_FREE(stripped);
2451                 errno = ENOMEM;
2452                 return -1;
2453         }
2454         conv->base_name = snapper_gmt_convert(conv, handle,
2455                                               stripped, timestamp);
2456         TALLOC_FREE(stripped);
2457         if (conv->base_name == NULL) {
2458                 return -1;
2459         }
2460         ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
2461         if (ret == -1) {
2462                 saved_errno = errno;
2463         }
2464         TALLOC_FREE(conv);
2465         if (saved_errno != 0) {
2466                 errno = saved_errno;
2467         }
2468         return ret;
2469 }
2470
2471 static int snapper_gmt_mknod(vfs_handle_struct *handle,
2472                         const struct smb_filename *smb_fname,
2473                         mode_t mode,
2474                         SMB_DEV_T dev)
2475 {
2476         time_t timestamp = (time_t)0;
2477         char *stripped = NULL;
2478         int ret, saved_errno = 0;
2479         struct smb_filename *conv_smb_fname = NULL;
2480
2481         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2482                                         smb_fname->base_name,
2483                                         &timestamp, &stripped)) {
2484                 return -1;
2485         }
2486         if (timestamp == 0) {
2487                 return SMB_VFS_NEXT_MKNOD(handle, smb_fname, mode, dev);
2488         }
2489         conv_smb_fname = cp_smb_filename(talloc_tos(), smb_fname);
2490         if (conv_smb_fname == NULL) {
2491                 errno = ENOMEM;
2492                 return -1;
2493         }
2494         conv_smb_fname->base_name = snapper_gmt_convert(conv_smb_fname, handle,
2495                                               stripped, timestamp);
2496         TALLOC_FREE(stripped);
2497         if (conv_smb_fname->base_name == NULL) {
2498                 return -1;
2499         }
2500         ret = SMB_VFS_NEXT_MKNOD(handle, conv_smb_fname, mode, dev);
2501         if (ret == -1) {
2502                 saved_errno = errno;
2503         }
2504         TALLOC_FREE(conv_smb_fname);
2505         if (saved_errno != 0) {
2506                 errno = saved_errno;
2507         }
2508         return ret;
2509 }
2510
2511 static struct smb_filename *snapper_gmt_realpath(vfs_handle_struct *handle,
2512                                 TALLOC_CTX *ctx,
2513                                 const struct smb_filename *smb_fname)
2514 {
2515         time_t timestamp = 0;
2516         char *stripped = NULL;
2517         struct smb_filename *result_fname = NULL;
2518         struct smb_filename *conv_smb_fname = NULL;
2519         int saved_errno = 0;
2520
2521         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2522                                         smb_fname->base_name,
2523                                         &timestamp, &stripped)) {
2524                 goto done;
2525         }
2526         if (timestamp == 0) {
2527                 return SMB_VFS_NEXT_REALPATH(handle, ctx, smb_fname);
2528         }
2529
2530         conv_smb_fname = cp_smb_filename(talloc_tos(), smb_fname);
2531         if (conv_smb_fname == NULL) {
2532                 goto done;
2533         }
2534         conv_smb_fname->base_name = snapper_gmt_convert(conv_smb_fname, handle,
2535                                               stripped, timestamp);
2536         if (conv_smb_fname->base_name == NULL) {
2537                 goto done;
2538         }
2539
2540         result_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, conv_smb_fname);
2541
2542 done:
2543         if (result_fname == NULL) {
2544                 saved_errno = errno;
2545         }
2546         TALLOC_FREE(conv_smb_fname);
2547         TALLOC_FREE(stripped);
2548         if (saved_errno != 0) {
2549                 errno = saved_errno;
2550         }
2551         return result_fname;
2552 }
2553
2554 static NTSTATUS snapper_gmt_fget_nt_acl(vfs_handle_struct *handle,
2555                                         struct files_struct *fsp,
2556                                         uint32_t security_info,
2557                                         TALLOC_CTX *mem_ctx,
2558                                         struct security_descriptor **ppdesc)
2559 {
2560         time_t timestamp;
2561         char *stripped;
2562         NTSTATUS status;
2563         char *conv;
2564         struct smb_filename *smb_fname = NULL;
2565
2566         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2567                                         fsp->fsp_name->base_name,
2568                                         &timestamp, &stripped)) {
2569                 return map_nt_error_from_unix(errno);
2570         }
2571         if (timestamp == 0) {
2572                 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
2573                                                 mem_ctx,
2574                                                 ppdesc);
2575         }
2576         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2577         TALLOC_FREE(stripped);
2578         if (conv == NULL) {
2579                 return map_nt_error_from_unix(errno);
2580         }
2581
2582         smb_fname = synthetic_smb_fname(talloc_tos(),
2583                                         conv,
2584                                         NULL,
2585                                         NULL,
2586                                         fsp->fsp_name->flags);
2587         TALLOC_FREE(conv);
2588         if (smb_fname == NULL) {
2589                 return NT_STATUS_NO_MEMORY;
2590         }
2591
2592         status = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
2593                                          mem_ctx, ppdesc);
2594         TALLOC_FREE(smb_fname);
2595         return status;
2596 }
2597
2598 static NTSTATUS snapper_gmt_get_nt_acl(vfs_handle_struct *handle,
2599                                        const struct smb_filename *fname,
2600                                        uint32_t security_info,
2601                                        TALLOC_CTX *mem_ctx,
2602                                        struct security_descriptor **ppdesc)
2603 {
2604         time_t timestamp;
2605         char *stripped;
2606         NTSTATUS status;
2607         char *conv;
2608         struct smb_filename *smb_fname = NULL;
2609
2610         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname->base_name,
2611                                         &timestamp, &stripped)) {
2612                 return map_nt_error_from_unix(errno);
2613         }
2614         if (timestamp == 0) {
2615                 return SMB_VFS_NEXT_GET_NT_ACL(handle, fname, security_info,
2616                                                mem_ctx, ppdesc);
2617         }
2618         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2619         TALLOC_FREE(stripped);
2620         if (conv == NULL) {
2621                 return map_nt_error_from_unix(errno);
2622         }
2623         smb_fname = synthetic_smb_fname(talloc_tos(),
2624                                         conv,
2625                                         NULL,
2626                                         NULL,
2627                                         fname->flags);
2628         TALLOC_FREE(conv);
2629         if (smb_fname == NULL) {
2630                 return NT_STATUS_NO_MEMORY;
2631         }
2632
2633         status = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
2634                                          mem_ctx, ppdesc);
2635         TALLOC_FREE(smb_fname);
2636         return status;
2637 }
2638
2639 static int snapper_gmt_mkdir(vfs_handle_struct *handle,
2640                                 const struct smb_filename *fname,
2641                                 mode_t mode)
2642 {
2643         time_t timestamp;
2644         char *stripped;
2645         int ret, saved_errno;
2646         char *conv;
2647         struct smb_filename *smb_fname = NULL;
2648
2649         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname->base_name,
2650                                         &timestamp, &stripped)) {
2651                 return -1;
2652         }
2653         if (timestamp == 0) {
2654                 return SMB_VFS_NEXT_MKDIR(handle, fname, mode);
2655         }
2656         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2657         TALLOC_FREE(stripped);
2658         if (conv == NULL) {
2659                 return -1;
2660         }
2661         smb_fname = synthetic_smb_fname(talloc_tos(),
2662                                         conv,
2663                                         NULL,
2664                                         NULL,
2665                                         fname->flags);
2666         TALLOC_FREE(conv);
2667         if (smb_fname == NULL) {
2668                 errno = ENOMEM;
2669                 return -1;
2670         }
2671
2672         ret = SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
2673         saved_errno = errno;
2674         TALLOC_FREE(smb_fname);
2675         errno = saved_errno;
2676         return ret;
2677 }
2678
2679 static int snapper_gmt_rmdir(vfs_handle_struct *handle,
2680                                 const struct smb_filename *fname)
2681 {
2682         time_t timestamp;
2683         char *stripped;
2684         int ret, saved_errno;
2685         char *conv;
2686         struct smb_filename *smb_fname = NULL;
2687
2688         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname->base_name,
2689                                         &timestamp, &stripped)) {
2690                 return -1;
2691         }
2692         if (timestamp == 0) {
2693                 return SMB_VFS_NEXT_RMDIR(handle, fname);
2694         }
2695         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2696         TALLOC_FREE(stripped);
2697         if (conv == NULL) {
2698                 return -1;
2699         }
2700         smb_fname = synthetic_smb_fname(talloc_tos(),
2701                                         conv,
2702                                         NULL,
2703                                         NULL,
2704                                         fname->flags);
2705         TALLOC_FREE(conv);
2706         if (smb_fname == NULL) {
2707                 errno = ENOMEM;
2708                 return -1;
2709         }
2710         ret = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
2711         saved_errno = errno;
2712         TALLOC_FREE(smb_fname);
2713         errno = saved_errno;
2714         return ret;
2715 }
2716
2717 static int snapper_gmt_chflags(vfs_handle_struct *handle,
2718                                 const struct smb_filename *smb_fname,
2719                                 unsigned int flags)
2720 {
2721         time_t timestamp = 0;
2722         char *stripped = NULL;
2723         int ret = -1;
2724         int saved_errno = 0;
2725         char *conv = NULL;
2726         struct smb_filename *conv_smb_fname = NULL;
2727
2728         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2729                                 smb_fname->base_name, &timestamp, &stripped)) {
2730                 return -1;
2731         }
2732         if (timestamp == 0) {
2733                 return SMB_VFS_NEXT_CHFLAGS(handle, smb_fname, flags);
2734         }
2735         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2736         TALLOC_FREE(stripped);
2737         if (conv == NULL) {
2738                 return -1;
2739         }
2740         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2741                                         conv,
2742                                         NULL,
2743                                         NULL,
2744                                         smb_fname->flags);
2745         TALLOC_FREE(conv);
2746         if (conv_smb_fname == NULL) {
2747                 errno = ENOMEM;
2748                 return -1;
2749         }
2750         ret = SMB_VFS_NEXT_CHFLAGS(handle, conv_smb_fname, flags);
2751         if (ret == -1) {
2752                 saved_errno = errno;
2753         }
2754         TALLOC_FREE(conv_smb_fname);
2755         if (saved_errno != 0) {
2756                 errno = saved_errno;
2757         }
2758         return ret;
2759 }
2760
2761 static ssize_t snapper_gmt_getxattr(vfs_handle_struct *handle,
2762                                 const struct smb_filename *smb_fname,
2763                                 const char *aname,
2764                                 void *value,
2765                                 size_t size)
2766 {
2767         time_t timestamp = 0;
2768         char *stripped = NULL;
2769         ssize_t ret;
2770         int saved_errno = 0;
2771         char *conv = NULL;
2772         struct smb_filename *conv_smb_fname = NULL;
2773
2774         if (!snapper_gmt_strip_snapshot(talloc_tos(),
2775                                         handle,
2776                                         smb_fname->base_name,
2777                                         &timestamp,
2778                                         &stripped)) {
2779                 return -1;
2780         }
2781         if (timestamp == 0) {
2782                 return SMB_VFS_NEXT_GETXATTR(handle, smb_fname, aname, value,
2783                                              size);
2784         }
2785         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2786         TALLOC_FREE(stripped);
2787         if (conv == NULL) {
2788                 return -1;
2789         }
2790         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2791                                         conv,
2792                                         NULL,
2793                                         NULL,
2794                                         smb_fname->flags);
2795         TALLOC_FREE(conv);
2796         if (conv_smb_fname == NULL) {
2797                 errno = ENOMEM;
2798                 return -1;
2799         }
2800         ret = SMB_VFS_NEXT_GETXATTR(handle, conv_smb_fname, aname, value, size);
2801         if (ret == -1) {
2802                 saved_errno = errno;
2803         }
2804         TALLOC_FREE(conv_smb_fname);
2805         TALLOC_FREE(conv);
2806         if (saved_errno != 0) {
2807                 errno = saved_errno;
2808         }
2809         return ret;
2810 }
2811
2812 static ssize_t snapper_gmt_listxattr(struct vfs_handle_struct *handle,
2813                                      const struct smb_filename *smb_fname,
2814                                      char *list, size_t size)
2815 {
2816         time_t timestamp = 0;
2817         char *stripped = NULL;
2818         ssize_t ret;
2819         int saved_errno = 0;
2820         char *conv = NULL;
2821         struct smb_filename *conv_smb_fname = NULL;
2822
2823         if (!snapper_gmt_strip_snapshot(talloc_tos(),
2824                                         handle,
2825                                         smb_fname->base_name,
2826                                         &timestamp,
2827                                         &stripped)) {
2828                 return -1;
2829         }
2830         if (timestamp == 0) {
2831                 return SMB_VFS_NEXT_LISTXATTR(handle, smb_fname, list, size);
2832         }
2833         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2834         TALLOC_FREE(stripped);
2835         if (conv == NULL) {
2836                 return -1;
2837         }
2838         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2839                                         conv,
2840                                         NULL,
2841                                         NULL,
2842                                         smb_fname->flags);
2843         TALLOC_FREE(conv);
2844         if (conv_smb_fname == NULL) {
2845                 errno = ENOMEM;
2846                 return -1;
2847         }
2848         ret = SMB_VFS_NEXT_LISTXATTR(handle, conv_smb_fname, list, size);
2849         if (ret == -1) {
2850                 saved_errno = errno;
2851         }
2852         TALLOC_FREE(conv_smb_fname);
2853         TALLOC_FREE(conv);
2854         if (saved_errno != 0) {
2855                 errno = saved_errno;
2856         }
2857         return ret;
2858 }
2859
2860 static int snapper_gmt_removexattr(vfs_handle_struct *handle,
2861                                 const struct smb_filename *smb_fname,
2862                                 const char *aname)
2863 {
2864         time_t timestamp = 0;
2865         char *stripped = NULL;
2866         ssize_t ret;
2867         int saved_errno = 0;
2868         char *conv = NULL;
2869         struct smb_filename *conv_smb_fname = NULL;
2870
2871         if (!snapper_gmt_strip_snapshot(talloc_tos(),
2872                                         handle,
2873                                         smb_fname->base_name,
2874                                         &timestamp,
2875                                         &stripped)) {
2876                 return -1;
2877         }
2878         if (timestamp == 0) {
2879                 return SMB_VFS_NEXT_REMOVEXATTR(handle, smb_fname, aname);
2880         }
2881         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2882         TALLOC_FREE(stripped);
2883         if (conv == NULL) {
2884                 return -1;
2885         }
2886         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2887                                         conv,
2888                                         NULL,
2889                                         NULL,
2890                                         smb_fname->flags);
2891         TALLOC_FREE(conv);
2892         if (conv_smb_fname == NULL) {
2893                 errno = ENOMEM;
2894                 return -1;
2895         }
2896         ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv_smb_fname, aname);
2897         if (ret == -1) {
2898                 saved_errno = errno;
2899         }
2900         TALLOC_FREE(conv_smb_fname);
2901         TALLOC_FREE(conv);
2902         if (saved_errno != 0) {
2903                 errno = saved_errno;
2904         }
2905         return ret;
2906 }
2907
2908 static int snapper_gmt_setxattr(struct vfs_handle_struct *handle,
2909                                 const struct smb_filename *smb_fname,
2910                                 const char *aname, const void *value,
2911                                 size_t size, int flags)
2912 {
2913         time_t timestamp = 0;
2914         char *stripped = NULL;
2915         ssize_t ret;
2916         int saved_errno = 0;
2917         char *conv = NULL;
2918         struct smb_filename *conv_smb_fname = NULL;
2919
2920         if (!snapper_gmt_strip_snapshot(talloc_tos(),
2921                                         handle,
2922                                         smb_fname->base_name,
2923                                         &timestamp,
2924                                         &stripped)) {
2925                 return -1;
2926         }
2927         if (timestamp == 0) {
2928                 return SMB_VFS_NEXT_SETXATTR(handle, smb_fname,
2929                                         aname, value, size, flags);
2930         }
2931         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2932         TALLOC_FREE(stripped);
2933         if (conv == NULL) {
2934                 return -1;
2935         }
2936         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2937                                         conv,
2938                                         NULL,
2939                                         NULL,
2940                                         smb_fname->flags);
2941         TALLOC_FREE(conv);
2942         if (conv_smb_fname == NULL) {
2943                 errno = ENOMEM;
2944                 return -1;
2945         }
2946         ret = SMB_VFS_NEXT_SETXATTR(handle, conv_smb_fname,
2947                                 aname, value, size, flags);
2948         if (ret == -1) {
2949                 saved_errno = errno;
2950         }
2951         TALLOC_FREE(conv_smb_fname);
2952         TALLOC_FREE(conv);
2953         if (saved_errno != 0) {
2954                 errno = saved_errno;
2955         }
2956         return ret;
2957 }
2958
2959 static int snapper_gmt_get_real_filename(struct vfs_handle_struct *handle,
2960                                          const char *path,
2961                                          const char *name,
2962                                          TALLOC_CTX *mem_ctx,
2963                                          char **found_name)
2964 {
2965         time_t timestamp;
2966         char *stripped;
2967         ssize_t ret;
2968         int saved_errno;
2969         char *conv;
2970
2971         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, path,
2972                                         &timestamp, &stripped)) {
2973                 return -1;
2974         }
2975         if (timestamp == 0) {
2976                 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
2977                                                       mem_ctx, found_name);
2978         }
2979         if (stripped[0] == '\0') {
2980                 *found_name = talloc_strdup(mem_ctx, name);
2981                 if (*found_name == NULL) {
2982                         errno = ENOMEM;
2983                         return -1;
2984                 }
2985                 return 0;
2986         }
2987         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2988         TALLOC_FREE(stripped);
2989         if (conv == NULL) {
2990                 return -1;
2991         }
2992         ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
2993                                              mem_ctx, found_name);
2994         saved_errno = errno;
2995         TALLOC_FREE(conv);
2996         errno = saved_errno;
2997         return ret;
2998 }
2999
3000 static uint64_t snapper_gmt_disk_free(vfs_handle_struct *handle,
3001                                 const struct smb_filename *smb_fname,
3002                                 uint64_t *bsize,
3003                                 uint64_t *dfree,
3004                                 uint64_t *dsize)
3005 {
3006         time_t timestamp = 0;
3007         char *stripped = NULL;
3008         uint64_t ret;
3009         int saved_errno = 0;
3010         char *conv = NULL;
3011         struct smb_filename *conv_smb_fname = NULL;
3012
3013         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
3014                         smb_fname->base_name, &timestamp, &stripped)) {
3015                 return (uint64_t)-1;
3016         }
3017         if (timestamp == 0) {
3018                 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
3019                                               bsize, dfree, dsize);
3020         }
3021
3022         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
3023         TALLOC_FREE(stripped);
3024         if (conv == NULL) {
3025                 return (uint64_t)-1;
3026         }
3027         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
3028                                         conv,
3029                                         NULL,
3030                                         NULL,
3031                                         smb_fname->flags);
3032         if (conv_smb_fname == NULL) {
3033                 TALLOC_FREE(conv);
3034                 errno = ENOMEM;
3035                 return (uint64_t)-1;
3036         }
3037
3038         ret = SMB_VFS_NEXT_DISK_FREE(handle, conv_smb_fname,
3039                                 bsize, dfree, dsize);
3040
3041         if (ret == (uint64_t)-1) {
3042                 saved_errno = errno;
3043         }
3044         TALLOC_FREE(conv_smb_fname);
3045         if (saved_errno != 0) {
3046                 errno = saved_errno;
3047         }
3048         return ret;
3049 }
3050
3051 static int snapper_gmt_get_quota(vfs_handle_struct *handle,
3052                         const struct smb_filename *smb_fname,
3053                         enum SMB_QUOTA_TYPE qtype,
3054                         unid_t id,
3055                         SMB_DISK_QUOTA *dq)
3056 {
3057         time_t timestamp = 0;
3058         char *stripped = NULL;
3059         int ret;
3060         int saved_errno = 0;
3061         char *conv = NULL;
3062         struct smb_filename *conv_smb_fname = NULL;
3063
3064         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
3065                                 smb_fname->base_name, &timestamp, &stripped)) {
3066                 return -1;
3067         }
3068         if (timestamp == 0) {
3069                 return SMB_VFS_NEXT_GET_QUOTA(handle, smb_fname, qtype, id, dq);
3070         }
3071
3072         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
3073         TALLOC_FREE(stripped);
3074         if (conv == NULL) {
3075                 return -1;
3076         }
3077         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
3078                                         conv,
3079                                         NULL,
3080                                         NULL,
3081                                         smb_fname->flags);
3082         TALLOC_FREE(conv);
3083         if (conv_smb_fname == NULL) {
3084                 errno = ENOMEM;
3085                 return -1;
3086         }
3087
3088         ret = SMB_VFS_NEXT_GET_QUOTA(handle, conv_smb_fname, qtype, id, dq);
3089
3090         if (ret == -1) {
3091                 saved_errno = errno;
3092         }
3093         TALLOC_FREE(conv_smb_fname);
3094         if (saved_errno != 0) {
3095                 errno = saved_errno;
3096         }
3097         return ret;
3098 }
3099
3100
3101 static struct vfs_fn_pointers snapper_fns = {
3102         .snap_check_path_fn = snapper_snap_check_path,
3103         .snap_create_fn = snapper_snap_create,
3104         .snap_delete_fn = snapper_snap_delete,
3105         .get_shadow_copy_data_fn = snapper_get_shadow_copy_data,
3106         .opendir_fn = snapper_gmt_opendir,
3107         .disk_free_fn = snapper_gmt_disk_free,
3108         .get_quota_fn = snapper_gmt_get_quota,
3109         .rename_fn = snapper_gmt_rename,
3110         .link_fn = snapper_gmt_link,
3111         .symlink_fn = snapper_gmt_symlink,
3112         .stat_fn = snapper_gmt_stat,
3113         .lstat_fn = snapper_gmt_lstat,
3114         .fstat_fn = snapper_gmt_fstat,
3115         .open_fn = snapper_gmt_open,
3116         .unlink_fn = snapper_gmt_unlink,
3117         .chmod_fn = snapper_gmt_chmod,
3118         .chown_fn = snapper_gmt_chown,
3119         .chdir_fn = snapper_gmt_chdir,
3120         .ntimes_fn = snapper_gmt_ntimes,
3121         .readlink_fn = snapper_gmt_readlink,
3122         .mknod_fn = snapper_gmt_mknod,
3123         .realpath_fn = snapper_gmt_realpath,
3124         .get_nt_acl_fn = snapper_gmt_get_nt_acl,
3125         .fget_nt_acl_fn = snapper_gmt_fget_nt_acl,
3126         .mkdir_fn = snapper_gmt_mkdir,
3127         .rmdir_fn = snapper_gmt_rmdir,
3128         .getxattr_fn = snapper_gmt_getxattr,
3129         .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
3130         .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
3131         .listxattr_fn = snapper_gmt_listxattr,
3132         .removexattr_fn = snapper_gmt_removexattr,
3133         .setxattr_fn = snapper_gmt_setxattr,
3134         .chflags_fn = snapper_gmt_chflags,
3135         .get_real_filename_fn = snapper_gmt_get_real_filename,
3136 };
3137
3138 static_decl_vfs;
3139 NTSTATUS vfs_snapper_init(TALLOC_CTX *ctx)
3140 {
3141         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
3142                                 "snapper", &snapper_fns);
3143 }