s3: VFS: snapper: Remove snapper_gmt_get_nt_acl_at().
[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 #include "lib/util/smb_strtox.h"
41
42 #define SNAPPER_SIG_LIST_SNAPS_RSP "a(uquxussa{ss})"
43 #define SNAPPER_SIG_LIST_CONFS_RSP "a(ssa{ss})"
44 #define SNAPPER_SIG_CREATE_SNAP_RSP "u"
45 #define SNAPPER_SIG_DEL_SNAPS_RSP ""
46 #define SNAPPER_SIG_STRING_DICT "{ss}"
47
48 struct snapper_dict {
49         char *key;
50         char *val;
51 };
52
53 struct snapper_snap {
54         uint32_t id;
55         uint16_t type;
56         uint32_t pre_id;
57         int64_t time;
58         uint32_t creator_uid;
59         char *desc;
60         char *cleanup;
61         uint32_t num_user_data;
62         struct snapper_dict *user_data;
63 };
64
65 struct snapper_conf {
66         char *name;
67         char *mnt;
68         uint32_t num_attrs;
69         struct snapper_dict *attrs;
70 };
71
72 static const struct {
73         const char *snapper_err_str;
74         NTSTATUS status;
75 } snapper_err_map[] = {
76         { "error.no_permissions", NT_STATUS_ACCESS_DENIED },
77 };
78
79 static NTSTATUS snapper_err_ntstatus_map(const char *snapper_err_str)
80 {
81         int i;
82
83         if (snapper_err_str == NULL) {
84                 return NT_STATUS_UNSUCCESSFUL;
85         }
86         for (i = 0; i < ARRAY_SIZE(snapper_err_map); i++) {
87                 if (!strcmp(snapper_err_map[i].snapper_err_str,
88                             snapper_err_str)) {
89                         return snapper_err_map[i].status;
90                 }
91         }
92         DEBUG(2, ("no explicit mapping for dbus error: %s\n", snapper_err_str));
93
94         return NT_STATUS_UNSUCCESSFUL;
95 }
96
97 /*
98  * Strings are UTF-8. Other characters must be encoded hexadecimal as "\x??".
99  * As a consequence "\" must be encoded as "\\".
100  */
101 static NTSTATUS snapper_dbus_str_encode(TALLOC_CTX *mem_ctx, const char *in_str,
102                                         char **_out_str)
103 {
104         size_t in_len;
105         char *out_str;
106         int i;
107         int out_off;
108         int out_len;
109
110         if (in_str == NULL) {
111                 return NT_STATUS_INVALID_PARAMETER;
112         }
113
114         in_len = strlen(in_str);
115
116         /* output can be max 4 times the length of @in_str, +1 for terminator */
117         out_len = (in_len * 4) + 1;
118
119         out_str = talloc_array(mem_ctx, char, out_len);
120         if (out_str == NULL) {
121                 return NT_STATUS_NO_MEMORY;
122         }
123
124         out_off = 0;
125         for (i = 0; i < in_len; i++) {
126                 size_t pushed;
127
128                 if (in_str[i] == '\\') {
129                         pushed = snprintf(out_str + out_off, out_len - out_off,
130                                           "\\\\");
131                 } else if ((unsigned char)in_str[i] > 127) {
132                         pushed = snprintf(out_str + out_off, out_len - out_off,
133                                           "\\x%02x", (unsigned char)in_str[i]);
134                 } else {
135                         /* regular character */
136                         *(out_str + out_off) = in_str[i];
137                         pushed = sizeof(char);
138                 }
139                 if (pushed >= out_len - out_off) {
140                         /* truncated, should never happen */
141                         talloc_free(out_str);
142                         return NT_STATUS_INTERNAL_ERROR;
143                 }
144                 out_off += pushed;
145         }
146
147         *(out_str + out_off) = '\0';
148         *_out_str = out_str;
149
150         return NT_STATUS_OK;
151 }
152
153 static NTSTATUS snapper_dbus_str_decode(TALLOC_CTX *mem_ctx, const char *in_str,
154                                         char **_out_str)
155 {
156         size_t in_len;
157         char *out_str;
158         int i;
159         int out_off;
160         int out_len;
161
162         if (in_str == NULL) {
163                 return NT_STATUS_INVALID_PARAMETER;
164         }
165
166         in_len = strlen(in_str);
167
168         /* output cannot be larger than input, +1 for terminator */
169         out_len = in_len + 1;
170
171         out_str = talloc_array(mem_ctx, char, out_len);
172         if (out_str == NULL) {
173                 return NT_STATUS_NO_MEMORY;
174         }
175
176         out_off = 0;
177         for (i = 0; i < in_len; i++) {
178                 int j;
179                 char hex_buf[3];
180                 unsigned int non_ascii_byte;
181
182                 if (in_str[i] != '\\') {
183                         out_str[out_off] = in_str[i];
184                         out_off++;
185                         continue;
186                 }
187
188                 i++;
189                 if (in_str[i] == '\\') {
190                         out_str[out_off] = '\\';
191                         out_off++;
192                         continue;
193                 } else if (in_str[i] != 'x') {
194                         goto err_invalid_src_encoding;
195                 }
196
197                 /* non-ASCII, encoded as two hex chars */
198                 for (j = 0; j < 2; j++) {
199                         i++;
200                         if ((in_str[i] == '\0') || !isxdigit(in_str[i])) {
201                                 goto err_invalid_src_encoding;
202                         }
203                         hex_buf[j] = in_str[i];
204                 }
205                 hex_buf[2] = '\0';
206
207                 sscanf(hex_buf, "%x", &non_ascii_byte);
208                 out_str[out_off] = (unsigned char)non_ascii_byte;
209                 out_off++;
210         }
211
212         out_str[out_off] = '\0';
213         *_out_str = out_str;
214
215         return NT_STATUS_OK;
216 err_invalid_src_encoding:
217         DEBUG(0, ("invalid encoding %s\n", in_str));
218         return NT_STATUS_INVALID_PARAMETER;
219 }
220
221 static DBusConnection *snapper_dbus_conn_create(void)
222 {
223         DBusError err;
224         DBusConnection *dconn;
225
226         dbus_error_init(&err);
227
228         /*
229          * Always create a new DBus connection, to ensure snapperd detects the
230          * correct client [E]UID. With dbus_bus_get() it does not!
231          */
232         dconn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
233         if (dbus_error_is_set(&err)) {
234                 DEBUG(0, ("dbus connection error: %s\n", err.message));
235                 dbus_error_free(&err);
236         }
237         if (dconn == NULL) {
238                 return NULL;
239         }
240
241         /* dbus_bus_get_private() sets exit-on-disconnect by default, undo it */
242         dbus_connection_set_exit_on_disconnect(dconn, false);
243
244         return dconn;
245 }
246
247 static void snapper_dbus_conn_destroy(DBusConnection *dconn)
248 {
249         if (dconn == NULL) {
250                 DEBUG(2, ("attempt to destroy NULL dbus connection\n"));
251                 return;
252         }
253
254         dbus_connection_close(dconn);
255         dbus_connection_unref(dconn);
256 }
257
258 /*
259  * send the message @send_msg over the dbus and wait for a response, return the
260  * responsee via @recv_msg_out.
261  * @send_msg is not freed, dbus_message_unref() must be handled by the caller.
262  */
263 static NTSTATUS snapper_dbus_msg_xchng(DBusConnection *dconn,
264                                        DBusMessage *send_msg,
265                                        DBusMessage **recv_msg_out)
266 {
267         DBusPendingCall *pending;
268         DBusMessage *recv_msg;
269
270         /* send message and get a handle for a reply */
271         if (!dbus_connection_send_with_reply(dconn, send_msg, &pending, -1)) {
272                 return NT_STATUS_NO_MEMORY;
273         }
274         if (NULL == pending) {
275                 DEBUG(0, ("dbus msg send failed\n"));
276                 return NT_STATUS_UNSUCCESSFUL;
277         }
278
279         dbus_connection_flush(dconn);
280
281         /* block until we receive a reply */
282         dbus_pending_call_block(pending);
283
284         /* get the reply message */
285         recv_msg = dbus_pending_call_steal_reply(pending);
286         if (recv_msg == NULL) {
287                 DEBUG(0, ("Reply Null\n"));
288                 return NT_STATUS_UNSUCCESSFUL;
289         }
290         /* free the pending message handle */
291         dbus_pending_call_unref(pending);
292         *recv_msg_out = recv_msg;
293
294         return NT_STATUS_OK;
295 }
296
297 static NTSTATUS snapper_type_check(DBusMessageIter *iter,
298                                    int expected_type)
299 {
300         int type = dbus_message_iter_get_arg_type(iter);
301         if (type != expected_type) {
302                 DEBUG(0, ("got type %d, expecting %d\n",
303                         type, expected_type));
304                 return NT_STATUS_INVALID_PARAMETER;
305         }
306
307         return NT_STATUS_OK;
308 }
309
310 static NTSTATUS snapper_type_check_get(DBusMessageIter *iter,
311                                        int expected_type,
312                                        void *val)
313 {
314         NTSTATUS status;
315         status = snapper_type_check(iter, expected_type);
316         if (!NT_STATUS_IS_OK(status)) {
317                 return status;
318         }
319
320         dbus_message_iter_get_basic(iter, val);
321
322         return NT_STATUS_OK;
323 }
324
325 static NTSTATUS snapper_dict_unpack(TALLOC_CTX *mem_ctx,
326                                     DBusMessageIter *iter,
327                                     struct snapper_dict *dict_out)
328
329 {
330         NTSTATUS status;
331         DBusMessageIter dct_iter;
332         char *key_encoded;
333         char *val_encoded;
334
335         status = snapper_type_check(iter, DBUS_TYPE_DICT_ENTRY);
336         if (!NT_STATUS_IS_OK(status)) {
337                 return status;
338         }
339         dbus_message_iter_recurse(iter, &dct_iter);
340
341         status = snapper_type_check_get(&dct_iter, DBUS_TYPE_STRING,
342                                         &key_encoded);
343         if (!NT_STATUS_IS_OK(status)) {
344                 return status;
345         }
346         status = snapper_dbus_str_decode(mem_ctx, key_encoded, &dict_out->key);
347         if (!NT_STATUS_IS_OK(status)) {
348                 return status;
349         }
350
351         dbus_message_iter_next(&dct_iter);
352         status = snapper_type_check_get(&dct_iter, DBUS_TYPE_STRING,
353                                         &val_encoded);
354         if (!NT_STATUS_IS_OK(status)) {
355                 talloc_free(dict_out->key);
356                 return status;
357         }
358         status = snapper_dbus_str_decode(mem_ctx, val_encoded, &dict_out->val);
359         if (!NT_STATUS_IS_OK(status)) {
360                 talloc_free(dict_out->key);
361                 return status;
362         }
363
364         return NT_STATUS_OK;
365 }
366
367 static void snapper_dict_array_print(uint32_t num_dicts,
368                                      struct snapper_dict *dicts)
369 {
370         int i;
371
372         for (i = 0; i < num_dicts; i++) {
373                 DEBUG(10, ("dict (key: %s, val: %s)\n",
374                            dicts[i].key, dicts[i].val));
375         }
376 }
377
378 static NTSTATUS snapper_dict_array_unpack(TALLOC_CTX *mem_ctx,
379                                           DBusMessageIter *iter,
380                                           uint32_t *num_dicts_out,
381                                           struct snapper_dict **dicts_out)
382 {
383         NTSTATUS status;
384         DBusMessageIter array_iter;
385         uint32_t num_dicts;
386         struct snapper_dict *dicts = NULL;
387
388         status = snapper_type_check(iter, DBUS_TYPE_ARRAY);
389         if (!NT_STATUS_IS_OK(status)) {
390                 return status;
391         }
392         dbus_message_iter_recurse(iter, &array_iter);
393
394         num_dicts = 0;
395         while (dbus_message_iter_get_arg_type(&array_iter)
396                                                         != DBUS_TYPE_INVALID) {
397                 num_dicts++;
398                 dicts = talloc_realloc(mem_ctx, dicts, struct snapper_dict,
399                                        num_dicts);
400                 if (dicts == NULL)
401                         abort();
402
403                 status = snapper_dict_unpack(mem_ctx, &array_iter,
404                                              &dicts[num_dicts - 1]);
405                 if (!NT_STATUS_IS_OK(status)) {
406                         talloc_free(dicts);
407                         return status;
408                 }
409                 dbus_message_iter_next(&array_iter);
410         }
411
412         *num_dicts_out = num_dicts;
413         *dicts_out = dicts;
414
415         return NT_STATUS_OK;
416 }
417
418 static NTSTATUS snapper_list_confs_pack(DBusMessage **req_msg_out)
419 {
420         DBusMessage *msg;
421
422         msg = dbus_message_new_method_call("org.opensuse.Snapper",
423                                            "/org/opensuse/Snapper",
424                                            "org.opensuse.Snapper",
425                                            "ListConfigs");
426         if (msg == NULL) {
427                 DEBUG(0, ("null msg\n"));
428                 return NT_STATUS_NO_MEMORY;
429         }
430
431         /* no arguments to append */
432         *req_msg_out = msg;
433
434         return NT_STATUS_OK;
435 }
436
437 static NTSTATUS snapper_conf_unpack(TALLOC_CTX *mem_ctx,
438                                     DBusMessageIter *iter,
439                                     struct snapper_conf *conf_out)
440 {
441         NTSTATUS status;
442         DBusMessageIter st_iter;
443         char *name_encoded;
444         char *mnt_encoded;
445
446         status = snapper_type_check(iter, DBUS_TYPE_STRUCT);
447         if (!NT_STATUS_IS_OK(status)) {
448                 return status;
449         }
450         dbus_message_iter_recurse(iter, &st_iter);
451
452         status = snapper_type_check_get(&st_iter, DBUS_TYPE_STRING,
453                                         &name_encoded);
454         if (!NT_STATUS_IS_OK(status)) {
455                 return status;
456         }
457
458         status = snapper_dbus_str_decode(mem_ctx, name_encoded,
459                                          &conf_out->name);
460         if (!NT_STATUS_IS_OK(status)) {
461                 return status;
462         }
463
464         dbus_message_iter_next(&st_iter);
465         status = snapper_type_check_get(&st_iter, DBUS_TYPE_STRING,
466                                         &mnt_encoded);
467         if (!NT_STATUS_IS_OK(status)) {
468                 talloc_free(conf_out->name);
469                 return status;
470         }
471
472         status = snapper_dbus_str_decode(mem_ctx, mnt_encoded,
473                                          &conf_out->mnt);
474         if (!NT_STATUS_IS_OK(status)) {
475                 talloc_free(conf_out->name);
476                 return status;
477         }
478
479         dbus_message_iter_next(&st_iter);
480         status = snapper_dict_array_unpack(mem_ctx, &st_iter,
481                                            &conf_out->num_attrs,
482                                            &conf_out->attrs);
483         if (!NT_STATUS_IS_OK(status)) {
484                 talloc_free(conf_out->mnt);
485                 talloc_free(conf_out->name);
486                 return status;
487         }
488
489         return NT_STATUS_OK;
490 }
491
492 static struct snapper_conf *snapper_conf_array_base_find(int32_t num_confs,
493                                                 struct snapper_conf *confs,
494                                                          const char *base)
495 {
496         int i;
497
498         for (i = 0; i < num_confs; i++) {
499                 if (strcmp(confs[i].mnt, base) == 0) {
500                         DEBUG(5, ("found snapper conf %s for path %s\n",
501                                   confs[i].name, base));
502                         return &confs[i];
503                 }
504         }
505         DEBUG(5, ("config for base %s not found\n", base));
506
507         return NULL;
508 }
509
510 static void snapper_conf_array_print(int32_t num_confs,
511                                      struct snapper_conf *confs)
512 {
513         int i;
514
515         for (i = 0; i < num_confs; i++) {
516                 DEBUG(10, ("name: %s, mnt: %s\n",
517                            confs[i].name, confs[i].mnt));
518                 snapper_dict_array_print(confs[i].num_attrs, confs[i].attrs);
519         }
520 }
521
522 static NTSTATUS snapper_conf_array_unpack(TALLOC_CTX *mem_ctx,
523                                           DBusMessageIter *iter,
524                                           uint32_t *num_confs_out,
525                                           struct snapper_conf **confs_out)
526 {
527         uint32_t num_confs;
528         NTSTATUS status;
529         struct snapper_conf *confs = NULL;
530         DBusMessageIter array_iter;
531
532
533         status = snapper_type_check(iter, DBUS_TYPE_ARRAY);
534         if (!NT_STATUS_IS_OK(status)) {
535                 return status;
536         }
537         dbus_message_iter_recurse(iter, &array_iter);
538
539         num_confs = 0;
540         while (dbus_message_iter_get_arg_type(&array_iter)
541                                                         != DBUS_TYPE_INVALID) {
542                 num_confs++;
543                 confs = talloc_realloc(mem_ctx, confs, struct snapper_conf,
544                                        num_confs);
545                 if (confs == NULL)
546                         abort();
547
548                 status = snapper_conf_unpack(confs, &array_iter,
549                                              &confs[num_confs - 1]);
550                 if (!NT_STATUS_IS_OK(status)) {
551                         talloc_free(confs);
552                         return status;
553                 }
554                 dbus_message_iter_next(&array_iter);
555         }
556
557         *num_confs_out = num_confs;
558         *confs_out = confs;
559
560         return NT_STATUS_OK;
561 }
562
563 static NTSTATUS snapper_list_confs_unpack(TALLOC_CTX *mem_ctx,
564                                           DBusConnection *dconn,
565                                           DBusMessage *rsp_msg,
566                                           uint32_t *num_confs_out,
567                                           struct snapper_conf **confs_out)
568 {
569         NTSTATUS status;
570         DBusMessageIter iter;
571         int msg_type;
572         uint32_t num_confs;
573         struct snapper_conf *confs;
574         const char *sig;
575
576         msg_type = dbus_message_get_type(rsp_msg);
577         if (msg_type == DBUS_MESSAGE_TYPE_ERROR) {
578                 const char *err_str = dbus_message_get_error_name(rsp_msg);
579                 DEBUG(0, ("list_confs error response: %s\n", err_str));
580                 return snapper_err_ntstatus_map(err_str);
581         }
582
583         if (msg_type != DBUS_MESSAGE_TYPE_METHOD_RETURN) {
584                 DEBUG(0, ("unexpected list_confs ret type: %d\n",
585                           msg_type));
586                 return NT_STATUS_INVALID_PARAMETER;
587         }
588
589         sig = dbus_message_get_signature(rsp_msg);
590         if ((sig == NULL)
591          || (strcmp(sig, SNAPPER_SIG_LIST_CONFS_RSP) != 0)) {
592                 DEBUG(0, ("bad list confs response sig: %s, expected: %s\n",
593                           (sig ? sig : "NULL"), SNAPPER_SIG_LIST_CONFS_RSP));
594                 return NT_STATUS_INVALID_PARAMETER;
595         }
596
597         if (!dbus_message_iter_init(rsp_msg, &iter)) {
598                 /* FIXME return empty? */
599                 DEBUG(0, ("Message has no arguments!\n"));
600                 return NT_STATUS_INVALID_PARAMETER;
601         }
602
603         status = snapper_conf_array_unpack(mem_ctx, &iter, &num_confs, &confs);
604         if (!NT_STATUS_IS_OK(status)) {
605                 DEBUG(0, ("failed to unpack conf array\n"));
606                 return status;
607         }
608
609         snapper_conf_array_print(num_confs, confs);
610
611         *num_confs_out = num_confs;
612         *confs_out = confs;
613
614         return NT_STATUS_OK;
615 }
616
617 static NTSTATUS snapper_list_snaps_pack(TALLOC_CTX *mem_ctx,
618                                         char *snapper_conf,
619                                         DBusMessage **req_msg_out)
620 {
621         DBusMessage *msg;
622         DBusMessageIter args;
623         char *conf_encoded;
624         NTSTATUS status;
625
626         msg = dbus_message_new_method_call("org.opensuse.Snapper", /* target for the method call */
627                                            "/org/opensuse/Snapper", /* object to call on */
628                                            "org.opensuse.Snapper", /* interface to call on */
629                                            "ListSnapshots"); /* method name */
630         if (msg == NULL) {
631                 DEBUG(0, ("failed to create list snaps message\n"));
632                 return NT_STATUS_NO_MEMORY;
633         }
634
635         status = snapper_dbus_str_encode(mem_ctx, snapper_conf, &conf_encoded);
636         if (!NT_STATUS_IS_OK(status)) {
637                 dbus_message_unref(msg);
638                 return status;
639         }
640
641         /* append arguments */
642         dbus_message_iter_init_append(msg, &args);
643         if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
644                                             &conf_encoded)) {
645                 talloc_free(conf_encoded);
646                 dbus_message_unref(msg);
647                 return NT_STATUS_NO_MEMORY;
648         }
649
650         *req_msg_out = msg;
651
652         return NT_STATUS_OK;
653 }
654
655 static NTSTATUS snapper_snap_struct_unpack(TALLOC_CTX *mem_ctx,
656                                            DBusMessageIter *iter,
657                                            struct snapper_snap *snap_out)
658 {
659         NTSTATUS status;
660         DBusMessageIter st_iter;
661         char *desc_encoded;
662         char *cleanup_encoded;
663
664         status = snapper_type_check(iter, DBUS_TYPE_STRUCT);
665         if (!NT_STATUS_IS_OK(status)) {
666                 return status;
667         }
668         dbus_message_iter_recurse(iter, &st_iter);
669
670         status = snapper_type_check_get(&st_iter, DBUS_TYPE_UINT32,
671                                         &snap_out->id);
672         if (!NT_STATUS_IS_OK(status)) {
673                 return status;
674         }
675
676         dbus_message_iter_next(&st_iter);
677         status = snapper_type_check_get(&st_iter, DBUS_TYPE_UINT16,
678                                         &snap_out->type);
679         if (!NT_STATUS_IS_OK(status)) {
680                 return status;
681         }
682
683         dbus_message_iter_next(&st_iter);
684         status = snapper_type_check_get(&st_iter, DBUS_TYPE_UINT32,
685                                         &snap_out->pre_id);
686         if (!NT_STATUS_IS_OK(status)) {
687                 return status;
688         }
689
690         dbus_message_iter_next(&st_iter);
691         status = snapper_type_check_get(&st_iter, DBUS_TYPE_INT64,
692                                         &snap_out->time);
693         if (!NT_STATUS_IS_OK(status)) {
694                 return status;
695         }
696
697         dbus_message_iter_next(&st_iter);
698         status = snapper_type_check_get(&st_iter, DBUS_TYPE_UINT32,
699                                         &snap_out->creator_uid);
700         if (!NT_STATUS_IS_OK(status)) {
701                 return status;
702         }
703
704         dbus_message_iter_next(&st_iter);
705         status = snapper_type_check_get(&st_iter, DBUS_TYPE_STRING,
706                                         &desc_encoded);
707         if (!NT_STATUS_IS_OK(status)) {
708                 return status;
709         }
710
711         status = snapper_dbus_str_decode(mem_ctx, desc_encoded,
712                                          &snap_out->desc);
713         if (!NT_STATUS_IS_OK(status)) {
714                 return status;
715         }
716
717         dbus_message_iter_next(&st_iter);
718         status = snapper_type_check_get(&st_iter, DBUS_TYPE_STRING,
719                                         &cleanup_encoded);
720         if (!NT_STATUS_IS_OK(status)) {
721                 talloc_free(snap_out->desc);
722                 return status;
723         }
724
725         status = snapper_dbus_str_decode(mem_ctx, cleanup_encoded,
726                                          &snap_out->cleanup);
727         if (!NT_STATUS_IS_OK(status)) {
728                 talloc_free(snap_out->desc);
729                 return status;
730         }
731
732         dbus_message_iter_next(&st_iter);
733         status = snapper_dict_array_unpack(mem_ctx, &st_iter,
734                                            &snap_out->num_user_data,
735                                            &snap_out->user_data);
736         if (!NT_STATUS_IS_OK(status)) {
737                 talloc_free(snap_out->cleanup);
738                 talloc_free(snap_out->desc);
739                 return status;
740         }
741
742         return NT_STATUS_OK;
743 }
744
745 static void snapper_snap_array_print(int32_t num_snaps,
746                                      struct snapper_snap *snaps)
747 {
748         int i;
749
750         for (i = 0; i < num_snaps; i++) {
751                 DEBUG(10, ("id: %u, "
752                            "type: %u, "
753                            "pre_id: %u, "
754                            "time: %ld, "
755                            "creator_uid: %u, "
756                            "desc: %s, "
757                            "cleanup: %s\n",
758                            (unsigned int)snaps[i].id,
759                            (unsigned int)snaps[i].type,
760                            (unsigned int)snaps[i].pre_id,
761                            (long int)snaps[i].time,
762                            (unsigned int)snaps[i].creator_uid,
763                            snaps[i].desc,
764                            snaps[i].cleanup));
765                 snapper_dict_array_print(snaps[i].num_user_data,
766                                          snaps[i].user_data);
767         }
768 }
769
770 static NTSTATUS snapper_snap_array_unpack(TALLOC_CTX *mem_ctx,
771                                           DBusMessageIter *iter,
772                                           uint32_t *num_snaps_out,
773                                           struct snapper_snap **snaps_out)
774 {
775         uint32_t num_snaps;
776         NTSTATUS status;
777         struct snapper_snap *snaps = NULL;
778         DBusMessageIter array_iter;
779
780
781         status = snapper_type_check(iter, DBUS_TYPE_ARRAY);
782         if (!NT_STATUS_IS_OK(status)) {
783                 return status;
784         }
785         dbus_message_iter_recurse(iter, &array_iter);
786
787         num_snaps = 0;
788         while (dbus_message_iter_get_arg_type(&array_iter)
789                                                         != DBUS_TYPE_INVALID) {
790                 num_snaps++;
791                 snaps = talloc_realloc(mem_ctx, snaps, struct snapper_snap,
792                                        num_snaps);
793                 if (snaps == NULL)
794                         abort();
795
796                 status = snapper_snap_struct_unpack(snaps, &array_iter,
797                                                     &snaps[num_snaps - 1]);
798                 if (!NT_STATUS_IS_OK(status)) {
799                         talloc_free(snaps);
800                         return status;
801                 }
802                 dbus_message_iter_next(&array_iter);
803         }
804
805         *num_snaps_out = num_snaps;
806         *snaps_out = snaps;
807
808         return NT_STATUS_OK;
809 }
810
811 static NTSTATUS snapper_list_snaps_unpack(TALLOC_CTX *mem_ctx,
812                                           DBusMessage *rsp_msg,
813                                           uint32_t *num_snaps_out,
814                                           struct snapper_snap **snaps_out)
815 {
816         NTSTATUS status;
817         DBusMessageIter iter;
818         int msg_type;
819         uint32_t num_snaps;
820         struct snapper_snap *snaps;
821         const char *sig;
822
823         msg_type = dbus_message_get_type(rsp_msg);
824         if (msg_type == DBUS_MESSAGE_TYPE_ERROR) {
825                 const char *err_str = dbus_message_get_error_name(rsp_msg);
826                 DEBUG(0, ("list_snaps error response: %s\n", err_str));
827                 return snapper_err_ntstatus_map(err_str);
828         }
829
830         if (msg_type != DBUS_MESSAGE_TYPE_METHOD_RETURN) {
831                 DEBUG(0,("unexpected list_snaps ret type: %d\n",
832                          msg_type));
833                 return NT_STATUS_INVALID_PARAMETER;
834         }
835
836         sig = dbus_message_get_signature(rsp_msg);
837         if ((sig == NULL)
838          || (strcmp(sig, SNAPPER_SIG_LIST_SNAPS_RSP) != 0)) {
839                 DEBUG(0, ("bad list snaps response sig: %s, "
840                           "expected: %s\n",
841                           (sig ? sig : "NULL"),
842                           SNAPPER_SIG_LIST_SNAPS_RSP));
843                 return NT_STATUS_INVALID_PARAMETER;
844         }
845
846         /* read the parameters */
847         if (!dbus_message_iter_init(rsp_msg, &iter)) {
848                 DEBUG(0, ("response has no arguments!\n"));
849                 return NT_STATUS_INVALID_PARAMETER;
850         }
851
852         status = snapper_snap_array_unpack(mem_ctx, &iter, &num_snaps, &snaps);
853         if (!NT_STATUS_IS_OK(status)) {
854                 DEBUG(0, ("failed to unpack snap array\n"));
855                 return NT_STATUS_INVALID_PARAMETER;
856         }
857
858         snapper_snap_array_print(num_snaps, snaps);
859
860         *num_snaps_out = num_snaps;
861         *snaps_out = snaps;
862
863         return NT_STATUS_OK;
864 }
865
866 static NTSTATUS snapper_create_snap_pack(TALLOC_CTX *mem_ctx,
867                                          const char *snapper_conf,
868                                          const char *desc,
869                                          uint32_t num_user_data,
870                                          struct snapper_dict *user_data,
871                                          DBusMessage **req_msg_out)
872 {
873         DBusMessage *msg;
874         DBusMessageIter args;
875         DBusMessageIter array_iter;
876         DBusMessageIter struct_iter;
877         const char *empty = "";
878         char *str_encoded;
879         uint32_t i;
880         bool ok;
881         TALLOC_CTX *enc_ctx;
882         NTSTATUS status;
883
884         DEBUG(10, ("CreateSingleSnapshot: %s, %s, %s, num user %u\n",
885                   snapper_conf, desc, empty, num_user_data));
886
887         enc_ctx = talloc_new(mem_ctx);
888         if (enc_ctx == NULL) {
889                 return NT_STATUS_NO_MEMORY;
890         }
891
892         msg = dbus_message_new_method_call("org.opensuse.Snapper",
893                                            "/org/opensuse/Snapper",
894                                            "org.opensuse.Snapper",
895                                            "CreateSingleSnapshot");
896         if (msg == NULL) {
897                 DEBUG(0, ("failed to create req msg\n"));
898                 talloc_free(enc_ctx);
899                 return NT_STATUS_NO_MEMORY;
900         }
901
902         status = snapper_dbus_str_encode(enc_ctx, snapper_conf, &str_encoded);
903         if (!NT_STATUS_IS_OK(status)) {
904                 dbus_message_unref(msg);
905                 talloc_free(enc_ctx);
906                 return status;
907         }
908
909         /* append arguments */
910         dbus_message_iter_init_append(msg, &args);
911         ok = dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
912                                             &str_encoded);
913         if (!ok) {
914                 dbus_message_unref(msg);
915                 talloc_free(enc_ctx);
916                 return NT_STATUS_NO_MEMORY;
917         }
918
919         status = snapper_dbus_str_encode(enc_ctx, desc, &str_encoded);
920         if (!NT_STATUS_IS_OK(status)) {
921                 dbus_message_unref(msg);
922                 talloc_free(enc_ctx);
923                 return status;
924         }
925
926         ok = dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
927                                             &str_encoded);
928         if (!ok) {
929                 dbus_message_unref(msg);
930                 talloc_free(enc_ctx);
931                 return NT_STATUS_NO_MEMORY;
932         }
933
934         /* cleanup - no need to encode empty string */
935         ok = dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
936                                             &empty);
937         if (!ok) {
938                 dbus_message_unref(msg);
939                 talloc_free(enc_ctx);
940                 return NT_STATUS_NO_MEMORY;
941         }
942
943         ok = dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY,
944                                               SNAPPER_SIG_STRING_DICT,
945                                               &array_iter);
946         if (!ok) {
947                 dbus_message_unref(msg);
948                 talloc_free(enc_ctx);
949                 return NT_STATUS_NO_MEMORY;
950         }
951
952         for (i = 0; i < num_user_data; i++) {
953                 ok = dbus_message_iter_open_container(&array_iter,
954                                                       DBUS_TYPE_DICT_ENTRY,
955                                                       NULL, &struct_iter);
956                 if (!ok) {
957                         dbus_message_unref(msg);
958                         talloc_free(enc_ctx);
959                         return NT_STATUS_NO_MEMORY;
960                 }
961
962                 status = snapper_dbus_str_encode(enc_ctx, user_data[i].key,
963                                                  &str_encoded);
964                 if (!NT_STATUS_IS_OK(status)) {
965                         dbus_message_unref(msg);
966                         talloc_free(enc_ctx);
967                         return status;
968                 }
969
970                 ok = dbus_message_iter_append_basic(&struct_iter,
971                                                     DBUS_TYPE_STRING,
972                                                     &str_encoded);
973                 if (!ok) {
974                         dbus_message_unref(msg);
975                         talloc_free(enc_ctx);
976                         return NT_STATUS_NO_MEMORY;
977                 }
978
979                 status = snapper_dbus_str_encode(enc_ctx, user_data[i].val,
980                                                  &str_encoded);
981                 if (!NT_STATUS_IS_OK(status)) {
982                         dbus_message_unref(msg);
983                         talloc_free(enc_ctx);
984                         return status;
985                 }
986
987                 ok = dbus_message_iter_append_basic(&struct_iter,
988                                                     DBUS_TYPE_STRING,
989                                                     &str_encoded);
990                 if (!ok) {
991                         dbus_message_unref(msg);
992                         talloc_free(enc_ctx);
993                         return NT_STATUS_NO_MEMORY;
994                 }
995
996                 ok = dbus_message_iter_close_container(&array_iter, &struct_iter);
997                 if (!ok) {
998                         dbus_message_unref(msg);
999                         talloc_free(enc_ctx);
1000                         return NT_STATUS_NO_MEMORY;
1001                 }
1002         }
1003
1004         ok = dbus_message_iter_close_container(&args, &array_iter);
1005         if (!ok) {
1006                 dbus_message_unref(msg);
1007                 talloc_free(enc_ctx);
1008                 return NT_STATUS_NO_MEMORY;
1009         }
1010
1011         *req_msg_out = msg;
1012
1013         return NT_STATUS_OK;
1014 }
1015
1016 static NTSTATUS snapper_create_snap_unpack(DBusConnection *conn,
1017                                            DBusMessage *rsp_msg,
1018                                            uint32_t *snap_id_out)
1019 {
1020         NTSTATUS status;
1021         DBusMessageIter iter;
1022         int msg_type;
1023         const char *sig;
1024         uint32_t snap_id;
1025
1026         msg_type = dbus_message_get_type(rsp_msg);
1027         if (msg_type == DBUS_MESSAGE_TYPE_ERROR) {
1028                 const char *err_str = dbus_message_get_error_name(rsp_msg);
1029                 DEBUG(0, ("create snap error response: %s, euid %d egid %d\n",
1030                           err_str, geteuid(), getegid()));
1031                 return snapper_err_ntstatus_map(err_str);
1032         }
1033
1034         if (msg_type != DBUS_MESSAGE_TYPE_METHOD_RETURN) {
1035                 DEBUG(0, ("unexpected create snap ret type: %d\n",
1036                           msg_type));
1037                 return NT_STATUS_INVALID_PARAMETER;
1038         }
1039
1040         sig = dbus_message_get_signature(rsp_msg);
1041         if ((sig == NULL)
1042          || (strcmp(sig, SNAPPER_SIG_CREATE_SNAP_RSP) != 0)) {
1043                 DEBUG(0, ("bad create snap response sig: %s, expected: %s\n",
1044                           (sig ? sig : "NULL"), SNAPPER_SIG_CREATE_SNAP_RSP));
1045                 return NT_STATUS_INVALID_PARAMETER;
1046         }
1047
1048         /* read the parameters */
1049         if (!dbus_message_iter_init(rsp_msg, &iter)) {
1050                 DEBUG(0, ("response has no arguments!\n"));
1051                 return NT_STATUS_INVALID_PARAMETER;
1052         }
1053
1054         status = snapper_type_check_get(&iter, DBUS_TYPE_UINT32, &snap_id);
1055         if (!NT_STATUS_IS_OK(status)) {
1056                 return status;
1057         }
1058         *snap_id_out = snap_id;
1059
1060         return NT_STATUS_OK;
1061 }
1062
1063 static NTSTATUS snapper_del_snap_pack(TALLOC_CTX *mem_ctx,
1064                                       const char *snapper_conf,
1065                                       uint32_t snap_id,
1066                                       DBusMessage **req_msg_out)
1067 {
1068         DBusMessage *msg;
1069         DBusMessageIter args;
1070         DBusMessageIter array_iter;
1071         char *conf_encoded;
1072         bool ok;
1073         NTSTATUS status;
1074
1075         msg = dbus_message_new_method_call("org.opensuse.Snapper",
1076                                            "/org/opensuse/Snapper",
1077                                            "org.opensuse.Snapper",
1078                                            "DeleteSnapshots");
1079         if (msg == NULL) {
1080                 DEBUG(0, ("failed to create req msg\n"));
1081                 return NT_STATUS_NO_MEMORY;
1082         }
1083
1084         status = snapper_dbus_str_encode(mem_ctx, snapper_conf, &conf_encoded);
1085         if (!NT_STATUS_IS_OK(status)) {
1086                 dbus_message_unref(msg);
1087                 return status;
1088         }
1089
1090         /* append arguments */
1091         dbus_message_iter_init_append(msg, &args);
1092         ok = dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
1093                                             &conf_encoded);
1094         if (!ok) {
1095                 talloc_free(conf_encoded);
1096                 dbus_message_unref(msg);
1097                 return NT_STATUS_NO_MEMORY;
1098         }
1099
1100         ok = dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY,
1101                                                DBUS_TYPE_UINT32_AS_STRING,
1102                                                &array_iter);
1103         if (!ok) {
1104                 talloc_free(conf_encoded);
1105                 dbus_message_unref(msg);
1106                 return NT_STATUS_NO_MEMORY;
1107         }
1108
1109         ok = dbus_message_iter_append_basic(&array_iter,
1110                                             DBUS_TYPE_UINT32,
1111                                             &snap_id);
1112         if (!ok) {
1113                 talloc_free(conf_encoded);
1114                 dbus_message_unref(msg);
1115                 return NT_STATUS_NO_MEMORY;
1116         }
1117
1118         dbus_message_iter_close_container(&args, &array_iter);
1119         *req_msg_out = msg;
1120
1121         return NT_STATUS_OK;
1122 }
1123
1124 static NTSTATUS snapper_del_snap_unpack(DBusConnection *conn,
1125                                         DBusMessage *rsp_msg)
1126 {
1127         int msg_type;
1128         const char *sig;
1129
1130         msg_type = dbus_message_get_type(rsp_msg);
1131         if (msg_type == DBUS_MESSAGE_TYPE_ERROR) {
1132                 const char *err_str = dbus_message_get_error_name(rsp_msg);
1133                 DEBUG(0, ("del snap error response: %s\n", err_str));
1134                 return snapper_err_ntstatus_map(err_str);
1135         }
1136
1137         if (msg_type != DBUS_MESSAGE_TYPE_METHOD_RETURN) {
1138                 DEBUG(0, ("unexpected del snap ret type: %d\n",
1139                           msg_type));
1140                 return NT_STATUS_INVALID_PARAMETER;
1141         }
1142
1143         sig = dbus_message_get_signature(rsp_msg);
1144         if ((sig == NULL)
1145          || (strcmp(sig, SNAPPER_SIG_DEL_SNAPS_RSP) != 0)) {
1146                 DEBUG(0, ("bad create snap response sig: %s, expected: %s\n",
1147                           (sig ? sig : "NULL"), SNAPPER_SIG_DEL_SNAPS_RSP));
1148                 return NT_STATUS_INVALID_PARAMETER;
1149         }
1150
1151         /* no parameters in response */
1152
1153         return NT_STATUS_OK;
1154 }
1155
1156 static NTSTATUS snapper_list_snaps_at_time_pack(TALLOC_CTX *mem_ctx,
1157                                                 const char *snapper_conf,
1158                                                 time_t time_lower,
1159                                                 time_t time_upper,
1160                                                 DBusMessage **req_msg_out)
1161 {
1162         DBusMessage *msg;
1163         DBusMessageIter args;
1164         char *conf_encoded;
1165         NTSTATUS status;
1166
1167         msg = dbus_message_new_method_call("org.opensuse.Snapper",
1168                                            "/org/opensuse/Snapper",
1169                                            "org.opensuse.Snapper",
1170                                            "ListSnapshotsAtTime");
1171         if (msg == NULL) {
1172                 DEBUG(0, ("failed to create list snaps message\n"));
1173                 return NT_STATUS_NO_MEMORY;
1174         }
1175
1176         status = snapper_dbus_str_encode(mem_ctx, snapper_conf, &conf_encoded);
1177         if (!NT_STATUS_IS_OK(status)) {
1178                 dbus_message_unref(msg);
1179                 return status;
1180         }
1181
1182         dbus_message_iter_init_append(msg, &args);
1183         if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
1184                                             &conf_encoded)) {
1185                 talloc_free(conf_encoded);
1186                 dbus_message_unref(msg);
1187                 return NT_STATUS_NO_MEMORY;
1188         }
1189
1190         if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT64,
1191                                             &time_lower)) {
1192                 talloc_free(conf_encoded);
1193                 dbus_message_unref(msg);
1194                 return NT_STATUS_NO_MEMORY;
1195         }
1196
1197         if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT64,
1198                                             &time_upper)) {
1199                 talloc_free(conf_encoded);
1200                 dbus_message_unref(msg);
1201                 return NT_STATUS_NO_MEMORY;
1202         }
1203
1204         *req_msg_out = msg;
1205
1206         return NT_STATUS_OK;
1207 }
1208 /* no snapper_list_snaps_at_time_unpack, use snapper_list_snaps_unpack */
1209
1210 /*
1211  * Determine the snapper snapshot id given a path.
1212  * Ideally this should be determined via a lookup.
1213  */
1214 static NTSTATUS snapper_snap_path_to_id(TALLOC_CTX *mem_ctx,
1215                                         const char *snap_path,
1216                                         uint32_t *snap_id_out)
1217 {
1218         char *path_dup;
1219         char *str_idx;
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 = smb_strtoul(str_idx, NULL, 10, &error, SMB_STR_STANDARD);
1255         if (error != 0) {
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 struct smb_filename *smb_fname,
1724                                        time_t *ptimestamp,
1725                                        char **pstripped)
1726 {
1727         char *stripped;
1728
1729         if (smb_fname->twrp == 0) {
1730                 goto no_snapshot;
1731         }
1732
1733         if (pstripped != NULL) {
1734                 stripped = talloc_strdup(mem_ctx, smb_fname->base_name);
1735                 if (stripped == NULL) {
1736                         return false;
1737                 }
1738                 *pstripped = stripped;
1739         }
1740
1741         *ptimestamp = nt_time_to_unix(smb_fname->twrp);
1742         return true;
1743 no_snapshot:
1744         *ptimestamp = 0;
1745         return true;
1746 }
1747
1748 static NTSTATUS snapper_get_snap_at_time_call(TALLOC_CTX *mem_ctx,
1749                                               DBusConnection *dconn,
1750                                               const char *conf_name,
1751                                               const char *base_path,
1752                                               time_t snaptime,
1753                                               char **snap_path_out)
1754 {
1755         NTSTATUS status;
1756         DBusMessage *req_msg = NULL;
1757         DBusMessage *rsp_msg;
1758         uint32_t num_snaps;
1759         struct snapper_snap *snaps;
1760         char *snap_path;
1761
1762         status = snapper_list_snaps_at_time_pack(mem_ctx,
1763                                                  conf_name,
1764                                                  snaptime,
1765                                                  snaptime,
1766                                                  &req_msg);
1767         if (!NT_STATUS_IS_OK(status)) {
1768                 goto err_out;
1769         }
1770
1771         status = snapper_dbus_msg_xchng(dconn, req_msg, &rsp_msg);
1772         if (!NT_STATUS_IS_OK(status)) {
1773                 goto err_req_free;
1774         }
1775
1776         status = snapper_list_snaps_unpack(mem_ctx, rsp_msg,
1777                                            &num_snaps, &snaps);
1778         if (!NT_STATUS_IS_OK(status)) {
1779                 goto err_rsp_free;
1780         }
1781
1782         if (num_snaps == 0) {
1783                 DEBUG(4, ("no snapshots found with time: %lu\n",
1784                           (unsigned long)snaptime));
1785                 status = NT_STATUS_INVALID_PARAMETER;
1786                 goto err_snap_array_free;
1787         } else if (num_snaps > 0) {
1788                 DEBUG(4, ("got %u snapshots for single time %lu, using top\n",
1789                           num_snaps, (unsigned long)snaptime));
1790         }
1791
1792         status = snapper_snap_id_to_path(mem_ctx, base_path, snaps[0].id,
1793                                          &snap_path);
1794         if (!NT_STATUS_IS_OK(status)) {
1795                 goto err_snap_array_free;
1796         }
1797
1798         *snap_path_out = snap_path;
1799 err_snap_array_free:
1800         talloc_free(snaps);
1801 err_rsp_free:
1802         dbus_message_unref(rsp_msg);
1803 err_req_free:
1804         dbus_message_unref(req_msg);
1805 err_out:
1806         return status;
1807 }
1808
1809 static NTSTATUS snapper_snap_path_expand(struct connection_struct *conn,
1810                                          TALLOC_CTX *mem_ctx,
1811                                          time_t snap_time,
1812                                          char **snap_dir_out)
1813 {
1814         DBusConnection *dconn;
1815         NTSTATUS status;
1816         char *conf_name;
1817         char *base_path;
1818         char *snap_path = NULL;
1819
1820         dconn = snapper_dbus_conn_create();
1821         if (dconn == NULL) {
1822                 status = NT_STATUS_UNSUCCESSFUL;
1823                 goto err_out;
1824         }
1825
1826         if (conn->connectpath == NULL) {
1827                 status = NT_STATUS_INVALID_PARAMETER;
1828                 goto err_conn_free;
1829         }
1830
1831         status = snapper_get_conf_call(mem_ctx, dconn,
1832                                        conn->connectpath,
1833                                        &conf_name,
1834                                        &base_path);
1835         if (!NT_STATUS_IS_OK(status)) {
1836                 goto err_conn_free;
1837         }
1838
1839         status = snapper_get_snap_at_time_call(mem_ctx, dconn,
1840                                                conf_name, base_path, snap_time,
1841                                                &snap_path);
1842         if (!NT_STATUS_IS_OK(status)) {
1843                 goto err_conf_name_free;
1844         }
1845
1846         /* confirm snapshot path is nested under base path */
1847         if (strncmp(snap_path, base_path, strlen(base_path)) != 0) {
1848                 status = NT_STATUS_INVALID_PARAMETER;
1849                 goto err_snap_path_free;
1850         }
1851
1852         talloc_free(conf_name);
1853         talloc_free(base_path);
1854         snapper_dbus_conn_destroy(dconn);
1855         *snap_dir_out = snap_path;
1856
1857         return NT_STATUS_OK;
1858
1859 err_snap_path_free:
1860         talloc_free(snap_path);
1861 err_conf_name_free:
1862         talloc_free(conf_name);
1863         talloc_free(base_path);
1864 err_conn_free:
1865         snapper_dbus_conn_destroy(dconn);
1866 err_out:
1867         return status;
1868 }
1869
1870 static char *snapper_gmt_convert(TALLOC_CTX *mem_ctx,
1871                              struct vfs_handle_struct *handle,
1872                              const char *name, time_t timestamp)
1873 {
1874         char *snap_path = NULL;
1875         char *path = NULL;
1876         NTSTATUS status;
1877         int saved_errno;
1878
1879         status = snapper_snap_path_expand(handle->conn, mem_ctx, timestamp,
1880                                           &snap_path);
1881         if (!NT_STATUS_IS_OK(status)) {
1882                 errno = map_errno_from_nt_status(status);
1883                 goto err_out;
1884         }
1885
1886         path = talloc_asprintf(mem_ctx, "%s/%s", snap_path, name);
1887         if (path == NULL) {
1888                 errno = ENOMEM;
1889                 goto err_snap_path_free;
1890         }
1891
1892         DEBUG(10, ("converted %s/%s @ time to %s\n",
1893                    handle->conn->connectpath, name, path));
1894         return path;
1895
1896 err_snap_path_free:
1897         saved_errno = errno;
1898         talloc_free(snap_path);
1899         errno = saved_errno;
1900 err_out:
1901         return NULL;
1902 }
1903
1904 static int snapper_gmt_renameat(vfs_handle_struct *handle,
1905                         files_struct *srcfsp,
1906                         const struct smb_filename *smb_fname_src,
1907                         files_struct *dstfsp,
1908                         const struct smb_filename *smb_fname_dst)
1909 {
1910         time_t timestamp_src, timestamp_dst;
1911
1912         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
1913                                         smb_fname_src,
1914                                         &timestamp_src, NULL)) {
1915                 return -1;
1916         }
1917         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
1918                                         smb_fname_dst,
1919                                         &timestamp_dst, NULL)) {
1920                 return -1;
1921         }
1922         if (timestamp_src != 0) {
1923                 errno = EXDEV;
1924                 return -1;
1925         }
1926         if (timestamp_dst != 0) {
1927                 errno = EROFS;
1928                 return -1;
1929         }
1930         return SMB_VFS_NEXT_RENAMEAT(handle,
1931                         srcfsp,
1932                         smb_fname_src,
1933                         dstfsp,
1934                         smb_fname_dst);
1935 }
1936
1937 static int snapper_gmt_symlinkat(vfs_handle_struct *handle,
1938                                 const struct smb_filename *link_contents,
1939                                 struct files_struct *dirfsp,
1940                                 const struct smb_filename *new_smb_fname)
1941 {
1942         time_t timestamp_old = 0;
1943         time_t timestamp_new = 0;
1944
1945         if (!snapper_gmt_strip_snapshot(talloc_tos(),
1946                                 handle,
1947                                 link_contents,
1948                                 &timestamp_old,
1949                                 NULL)) {
1950                 return -1;
1951         }
1952         if (!snapper_gmt_strip_snapshot(talloc_tos(),
1953                                 handle,
1954                                 new_smb_fname,
1955                                 &timestamp_new,
1956                                 NULL)) {
1957                 return -1;
1958         }
1959         if ((timestamp_old != 0) || (timestamp_new != 0)) {
1960                 errno = EROFS;
1961                 return -1;
1962         }
1963         return SMB_VFS_NEXT_SYMLINKAT(handle,
1964                         link_contents,
1965                         dirfsp,
1966                         new_smb_fname);
1967 }
1968
1969 static int snapper_gmt_linkat(vfs_handle_struct *handle,
1970                                 files_struct *srcfsp,
1971                                 const struct smb_filename *old_smb_fname,
1972                                 files_struct *dstfsp,
1973                                 const struct smb_filename *new_smb_fname,
1974                                 int flags)
1975 {
1976         time_t timestamp_old = 0;
1977         time_t timestamp_new = 0;
1978
1979         if (!snapper_gmt_strip_snapshot(talloc_tos(),
1980                                 handle,
1981                                 old_smb_fname,
1982                                 &timestamp_old,
1983                                 NULL)) {
1984                 return -1;
1985         }
1986         if (!snapper_gmt_strip_snapshot(talloc_tos(),
1987                                 handle,
1988                                 new_smb_fname,
1989                                 &timestamp_new,
1990                                 NULL)) {
1991                 return -1;
1992         }
1993         if ((timestamp_old != 0) || (timestamp_new != 0)) {
1994                 errno = EROFS;
1995                 return -1;
1996         }
1997         return SMB_VFS_NEXT_LINKAT(handle,
1998                                 srcfsp,
1999                                 old_smb_fname,
2000                                 dstfsp,
2001                                 new_smb_fname,
2002                                 flags);
2003 }
2004
2005 static int snapper_gmt_stat(vfs_handle_struct *handle,
2006                             struct smb_filename *smb_fname)
2007 {
2008         time_t timestamp;
2009         char *stripped, *tmp;
2010         int ret, saved_errno;
2011
2012         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2013                                         smb_fname,
2014                                         &timestamp, &stripped)) {
2015                 return -1;
2016         }
2017         if (timestamp == 0) {
2018                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
2019         }
2020
2021         tmp = smb_fname->base_name;
2022         smb_fname->base_name = snapper_gmt_convert(talloc_tos(), handle,
2023                                                    stripped, timestamp);
2024         TALLOC_FREE(stripped);
2025
2026         if (smb_fname->base_name == NULL) {
2027                 smb_fname->base_name = tmp;
2028                 return -1;
2029         }
2030
2031         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
2032         saved_errno = errno;
2033
2034         TALLOC_FREE(smb_fname->base_name);
2035         smb_fname->base_name = tmp;
2036
2037         errno = saved_errno;
2038         return ret;
2039 }
2040
2041 static int snapper_gmt_lstat(vfs_handle_struct *handle,
2042                              struct smb_filename *smb_fname)
2043 {
2044         time_t timestamp;
2045         char *stripped, *tmp;
2046         int ret, saved_errno;
2047
2048         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2049                                         smb_fname,
2050                                         &timestamp, &stripped)) {
2051                 return -1;
2052         }
2053         if (timestamp == 0) {
2054                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2055         }
2056
2057         tmp = smb_fname->base_name;
2058         smb_fname->base_name = snapper_gmt_convert(talloc_tos(), handle,
2059                                                    stripped, timestamp);
2060         TALLOC_FREE(stripped);
2061
2062         if (smb_fname->base_name == NULL) {
2063                 smb_fname->base_name = tmp;
2064                 return -1;
2065         }
2066
2067         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2068         saved_errno = errno;
2069
2070         TALLOC_FREE(smb_fname->base_name);
2071         smb_fname->base_name = tmp;
2072
2073         errno = saved_errno;
2074         return ret;
2075 }
2076
2077 static int snapper_gmt_openat(struct vfs_handle_struct *handle,
2078                               const struct files_struct *dirfsp,
2079                               const struct smb_filename *smb_fname_in,
2080                               struct files_struct *fsp,
2081                               int flags,
2082                               mode_t mode)
2083 {
2084         struct smb_filename *smb_fname = NULL;
2085         time_t timestamp;
2086         char *stripped = NULL;
2087         int ret;
2088         int saved_errno = 0;
2089
2090         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2091                                         smb_fname_in,
2092                                         &timestamp, &stripped)) {
2093                 return -1;
2094         }
2095         if (timestamp == 0) {
2096                 return SMB_VFS_NEXT_OPENAT(handle,
2097                                            dirfsp,
2098                                            smb_fname_in,
2099                                            fsp,
2100                                            flags,
2101                                            mode);
2102         }
2103
2104         smb_fname = cp_smb_filename(talloc_tos(), smb_fname_in);
2105         if (smb_fname == NULL) {
2106                 TALLOC_FREE(stripped);
2107                 return -1;
2108         }
2109
2110         smb_fname->base_name = snapper_gmt_convert(smb_fname, handle,
2111                                                    stripped, timestamp);
2112         TALLOC_FREE(stripped);
2113
2114         if (smb_fname->base_name == NULL) {
2115                 TALLOC_FREE(smb_fname);
2116                 errno = ENOMEM;
2117                 return -1;
2118         }
2119
2120         ret = SMB_VFS_NEXT_OPENAT(handle, dirfsp, smb_fname, fsp, flags, mode);
2121         if (ret == -1) {
2122                 saved_errno = errno;
2123         }
2124         TALLOC_FREE(smb_fname);
2125         if (saved_errno != 0) {
2126                 errno = saved_errno;
2127         }
2128         return ret;
2129 }
2130
2131 static int snapper_gmt_unlinkat(vfs_handle_struct *handle,
2132                         struct files_struct *dirfsp,
2133                         const struct smb_filename *smb_fname,
2134                         int flags)
2135 {
2136         time_t timestamp = 0;
2137
2138         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2139                                         smb_fname,
2140                                         &timestamp, NULL)) {
2141                 return -1;
2142         }
2143         if (timestamp != 0) {
2144                 errno = EROFS;
2145                 return -1;
2146         }
2147         return SMB_VFS_NEXT_UNLINKAT(handle,
2148                         dirfsp,
2149                         smb_fname,
2150                         flags);
2151 }
2152
2153 static int snapper_gmt_fchmod(vfs_handle_struct *handle,
2154                        struct files_struct *fsp,
2155                        mode_t mode)
2156 {
2157         time_t timestamp = 0;
2158         const struct smb_filename *smb_fname = NULL;
2159
2160         smb_fname = fsp->fsp_name;
2161
2162         if (!snapper_gmt_strip_snapshot(talloc_tos(),
2163                                         handle,
2164                                         smb_fname,
2165                                         &timestamp,
2166                                         NULL)) {
2167                 return -1;
2168         }
2169
2170         if (timestamp != 0) {
2171                 errno = EROFS;
2172                 return -1;
2173         }
2174         return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
2175 }
2176
2177 static int snapper_gmt_chdir(vfs_handle_struct *handle,
2178                         const struct smb_filename *smb_fname)
2179 {
2180         time_t timestamp = 0;
2181         char *stripped = NULL;
2182         int ret;
2183         int saved_errno = 0;
2184         char *conv = NULL;
2185         struct smb_filename *conv_smb_fname = NULL;
2186
2187         if (!snapper_gmt_strip_snapshot(talloc_tos(),
2188                                 handle,
2189                                 smb_fname,
2190                                 &timestamp,
2191                                 &stripped)) {
2192                 return -1;
2193         }
2194         if (timestamp == 0) {
2195                 return SMB_VFS_NEXT_CHDIR(handle, smb_fname);
2196         }
2197         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2198         TALLOC_FREE(stripped);
2199         if (conv == NULL) {
2200                 return -1;
2201         }
2202         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2203                                         conv,
2204                                         NULL,
2205                                         NULL,
2206                                         0,
2207                                         smb_fname->flags);
2208         if (conv_smb_fname == NULL) {
2209                 TALLOC_FREE(conv);
2210                 errno = ENOMEM;
2211                 return -1;
2212         }
2213         ret = SMB_VFS_NEXT_CHDIR(handle, conv_smb_fname);
2214         if (ret == -1) {
2215                 saved_errno = errno;
2216         }
2217         TALLOC_FREE(conv);
2218         TALLOC_FREE(conv_smb_fname);
2219         if (saved_errno != 0) {
2220                 errno = saved_errno;
2221         }
2222         return ret;
2223 }
2224
2225 static int snapper_gmt_fntimes(vfs_handle_struct *handle,
2226                                files_struct *fsp,
2227                                struct smb_file_time *ft)
2228 {
2229         time_t timestamp = 0;
2230
2231         if (!snapper_gmt_strip_snapshot(talloc_tos(),
2232                                         handle,
2233                                         fsp->fsp_name,
2234                                         &timestamp,
2235                                         NULL)) {
2236                 return -1;
2237         }
2238
2239         if (timestamp != 0) {
2240                 errno = EROFS;
2241                 return -1;
2242         }
2243
2244         return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
2245 }
2246
2247 static int snapper_gmt_readlinkat(vfs_handle_struct *handle,
2248                                 const struct files_struct *dirfsp,
2249                                 const struct smb_filename *smb_fname,
2250                                 char *buf,
2251                                 size_t bufsiz)
2252 {
2253         time_t timestamp = 0;
2254         int ret;
2255         int saved_errno = 0;
2256         struct smb_filename *full_fname = NULL;
2257
2258         /*
2259          * Now this function only looks at smb_fname->twrp
2260          * we don't need to copy out the path. Just use
2261          * smb_fname->base_name directly.
2262          */
2263         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2264                                         smb_fname,
2265                                         &timestamp, NULL)) {
2266                 return -1;
2267         }
2268         if (timestamp == 0) {
2269                 return SMB_VFS_NEXT_READLINKAT(handle,
2270                                 dirfsp,
2271                                 smb_fname,
2272                                 buf,
2273                                 bufsiz);
2274         }
2275         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2276                                 dirfsp,
2277                                 smb_fname);
2278         if (full_fname == NULL) {
2279                 return -1;
2280         }
2281
2282         /* Find the snapshot path from the full pathname. */
2283         full_fname->base_name = snapper_gmt_convert(full_fname,
2284                                         handle,
2285                                         full_fname->base_name,
2286                                         timestamp);
2287         if (full_fname->base_name == NULL) {
2288                 TALLOC_FREE(full_fname);
2289                 return -1;
2290         }
2291         ret = SMB_VFS_NEXT_READLINKAT(handle,
2292                                 handle->conn->cwd_fsp,
2293                                 full_fname,
2294                                 buf,
2295                                 bufsiz);
2296         if (ret == -1) {
2297                 saved_errno = errno;
2298         }
2299         TALLOC_FREE(full_fname);
2300         if (saved_errno != 0) {
2301                 errno = saved_errno;
2302         }
2303         return ret;
2304 }
2305
2306 static int snapper_gmt_mknodat(vfs_handle_struct *handle,
2307                         files_struct *dirfsp,
2308                         const struct smb_filename *smb_fname,
2309                         mode_t mode,
2310                         SMB_DEV_T dev)
2311 {
2312         time_t timestamp = (time_t)0;
2313
2314         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2315                                         smb_fname,
2316                                         &timestamp, NULL)) {
2317                 return -1;
2318         }
2319         if (timestamp != 0) {
2320                 errno = EROFS;
2321                 return -1;
2322         }
2323         return SMB_VFS_NEXT_MKNODAT(handle,
2324                         dirfsp,
2325                         smb_fname,
2326                         mode,
2327                         dev);
2328 }
2329
2330 static struct smb_filename *snapper_gmt_realpath(vfs_handle_struct *handle,
2331                                 TALLOC_CTX *ctx,
2332                                 const struct smb_filename *smb_fname)
2333 {
2334         time_t timestamp = 0;
2335         char *stripped = NULL;
2336         struct smb_filename *result_fname = NULL;
2337         struct smb_filename *conv_smb_fname = NULL;
2338         int saved_errno = 0;
2339
2340         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2341                                         smb_fname,
2342                                         &timestamp, &stripped)) {
2343                 goto done;
2344         }
2345         if (timestamp == 0) {
2346                 return SMB_VFS_NEXT_REALPATH(handle, ctx, smb_fname);
2347         }
2348
2349         conv_smb_fname = cp_smb_filename(talloc_tos(), smb_fname);
2350         if (conv_smb_fname == NULL) {
2351                 goto done;
2352         }
2353         conv_smb_fname->base_name = snapper_gmt_convert(conv_smb_fname, handle,
2354                                               stripped, timestamp);
2355         if (conv_smb_fname->base_name == NULL) {
2356                 goto done;
2357         }
2358
2359         result_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, conv_smb_fname);
2360
2361 done:
2362         if (result_fname == NULL) {
2363                 saved_errno = errno;
2364         }
2365         TALLOC_FREE(conv_smb_fname);
2366         TALLOC_FREE(stripped);
2367         if (saved_errno != 0) {
2368                 errno = saved_errno;
2369         }
2370         return result_fname;
2371 }
2372
2373 static int snapper_gmt_mkdirat(vfs_handle_struct *handle,
2374                                 struct files_struct *dirfsp,
2375                                 const struct smb_filename *fname,
2376                                 mode_t mode)
2377 {
2378         time_t timestamp = 0;
2379
2380         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
2381                                         &timestamp, NULL)) {
2382                 return -1;
2383         }
2384         if (timestamp != 0) {
2385                 errno = EROFS;
2386                 return -1;
2387         }
2388         return SMB_VFS_NEXT_MKDIRAT(handle,
2389                         dirfsp,
2390                         fname,
2391                         mode);
2392 }
2393
2394 static int snapper_gmt_chflags(vfs_handle_struct *handle,
2395                                 const struct smb_filename *smb_fname,
2396                                 unsigned int flags)
2397 {
2398         time_t timestamp = 0;
2399
2400         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2401                                 smb_fname, &timestamp, NULL)) {
2402                 return -1;
2403         }
2404         if (timestamp != 0) {
2405                 errno = EROFS;
2406                 return -1;
2407         }
2408         return SMB_VFS_NEXT_CHFLAGS(handle, smb_fname, flags);
2409 }
2410
2411 static ssize_t snapper_gmt_getxattr(vfs_handle_struct *handle,
2412                                 const struct smb_filename *smb_fname,
2413                                 const char *aname,
2414                                 void *value,
2415                                 size_t size)
2416 {
2417         time_t timestamp = 0;
2418         char *stripped = NULL;
2419         ssize_t ret;
2420         int saved_errno = 0;
2421         char *conv = NULL;
2422         struct smb_filename *conv_smb_fname = NULL;
2423
2424         if (!snapper_gmt_strip_snapshot(talloc_tos(),
2425                                         handle,
2426                                         smb_fname,
2427                                         &timestamp,
2428                                         &stripped)) {
2429                 return -1;
2430         }
2431         if (timestamp == 0) {
2432                 return SMB_VFS_NEXT_GETXATTR(handle, smb_fname, aname, value,
2433                                              size);
2434         }
2435         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2436         TALLOC_FREE(stripped);
2437         if (conv == NULL) {
2438                 return -1;
2439         }
2440         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2441                                         conv,
2442                                         NULL,
2443                                         NULL,
2444                                         0,
2445                                         smb_fname->flags);
2446         TALLOC_FREE(conv);
2447         if (conv_smb_fname == NULL) {
2448                 errno = ENOMEM;
2449                 return -1;
2450         }
2451         ret = SMB_VFS_NEXT_GETXATTR(handle, conv_smb_fname, aname, value, size);
2452         if (ret == -1) {
2453                 saved_errno = errno;
2454         }
2455         TALLOC_FREE(conv_smb_fname);
2456         TALLOC_FREE(conv);
2457         if (saved_errno != 0) {
2458                 errno = saved_errno;
2459         }
2460         return ret;
2461 }
2462
2463 static int snapper_gmt_fsetxattr(struct vfs_handle_struct *handle,
2464                                 struct files_struct *fsp,
2465                                 const char *aname, const void *value,
2466                                 size_t size, int flags)
2467 {
2468         time_t timestamp = 0;
2469         const struct smb_filename *smb_fname = NULL;
2470
2471         smb_fname = fsp->fsp_name;
2472
2473         if (!snapper_gmt_strip_snapshot(talloc_tos(),
2474                                         handle,
2475                                         smb_fname,
2476                                         &timestamp,
2477                                         NULL)) {
2478                 return -1;
2479         }
2480         if (timestamp != 0) {
2481                 errno = EROFS;
2482                 return -1;
2483         }
2484         return SMB_VFS_NEXT_FSETXATTR(handle, fsp,
2485                                 aname, value, size, flags);
2486 }
2487
2488 static int snapper_gmt_get_real_filename(struct vfs_handle_struct *handle,
2489                                          const struct smb_filename *fpath,
2490                                          const char *name,
2491                                          TALLOC_CTX *mem_ctx,
2492                                          char **found_name)
2493 {
2494         time_t timestamp;
2495         char *stripped;
2496         ssize_t ret;
2497         int saved_errno;
2498         char *conv;
2499         struct smb_filename conv_fname;
2500
2501         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fpath,
2502                                         &timestamp, &stripped)) {
2503                 return -1;
2504         }
2505         if (timestamp == 0) {
2506                 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, fpath, name,
2507                                                       mem_ctx, found_name);
2508         }
2509         if (stripped[0] == '\0') {
2510                 *found_name = talloc_strdup(mem_ctx, name);
2511                 if (*found_name == NULL) {
2512                         errno = ENOMEM;
2513                         return -1;
2514                 }
2515                 return 0;
2516         }
2517         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2518         TALLOC_FREE(stripped);
2519         if (conv == NULL) {
2520                 return -1;
2521         }
2522
2523         conv_fname = (struct smb_filename) {
2524                 .base_name = conv,
2525         };
2526
2527         ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, &conv_fname, name,
2528                                              mem_ctx, found_name);
2529         saved_errno = errno;
2530         TALLOC_FREE(conv);
2531         errno = saved_errno;
2532         return ret;
2533 }
2534
2535 static uint64_t snapper_gmt_disk_free(vfs_handle_struct *handle,
2536                                 const struct smb_filename *smb_fname,
2537                                 uint64_t *bsize,
2538                                 uint64_t *dfree,
2539                                 uint64_t *dsize)
2540 {
2541         time_t timestamp = 0;
2542         char *stripped = NULL;
2543         uint64_t ret;
2544         int saved_errno = 0;
2545         char *conv = NULL;
2546         struct smb_filename *conv_smb_fname = NULL;
2547
2548         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2549                         smb_fname, &timestamp, &stripped)) {
2550                 return (uint64_t)-1;
2551         }
2552         if (timestamp == 0) {
2553                 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
2554                                               bsize, dfree, dsize);
2555         }
2556
2557         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2558         TALLOC_FREE(stripped);
2559         if (conv == NULL) {
2560                 return (uint64_t)-1;
2561         }
2562         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2563                                         conv,
2564                                         NULL,
2565                                         NULL,
2566                                         0,
2567                                         smb_fname->flags);
2568         if (conv_smb_fname == NULL) {
2569                 TALLOC_FREE(conv);
2570                 errno = ENOMEM;
2571                 return (uint64_t)-1;
2572         }
2573
2574         ret = SMB_VFS_NEXT_DISK_FREE(handle, conv_smb_fname,
2575                                 bsize, dfree, dsize);
2576
2577         if (ret == (uint64_t)-1) {
2578                 saved_errno = errno;
2579         }
2580         TALLOC_FREE(conv_smb_fname);
2581         if (saved_errno != 0) {
2582                 errno = saved_errno;
2583         }
2584         return ret;
2585 }
2586
2587 static int snapper_gmt_get_quota(vfs_handle_struct *handle,
2588                         const struct smb_filename *smb_fname,
2589                         enum SMB_QUOTA_TYPE qtype,
2590                         unid_t id,
2591                         SMB_DISK_QUOTA *dq)
2592 {
2593         time_t timestamp = 0;
2594         char *stripped = NULL;
2595         int ret;
2596         int saved_errno = 0;
2597         char *conv = NULL;
2598         struct smb_filename *conv_smb_fname = NULL;
2599
2600         if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2601                                 smb_fname, &timestamp, &stripped)) {
2602                 return -1;
2603         }
2604         if (timestamp == 0) {
2605                 return SMB_VFS_NEXT_GET_QUOTA(handle, smb_fname, qtype, id, dq);
2606         }
2607
2608         conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2609         TALLOC_FREE(stripped);
2610         if (conv == NULL) {
2611                 return -1;
2612         }
2613         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2614                                         conv,
2615                                         NULL,
2616                                         NULL,
2617                                         0,
2618                                         smb_fname->flags);
2619         TALLOC_FREE(conv);
2620         if (conv_smb_fname == NULL) {
2621                 errno = ENOMEM;
2622                 return -1;
2623         }
2624
2625         ret = SMB_VFS_NEXT_GET_QUOTA(handle, conv_smb_fname, qtype, id, dq);
2626
2627         if (ret == -1) {
2628                 saved_errno = errno;
2629         }
2630         TALLOC_FREE(conv_smb_fname);
2631         if (saved_errno != 0) {
2632                 errno = saved_errno;
2633         }
2634         return ret;
2635 }
2636
2637 static NTSTATUS snapper_create_dfs_pathat(struct vfs_handle_struct *handle,
2638                                 struct files_struct *dirfsp,
2639                                 const struct smb_filename *smb_fname,
2640                                 const struct referral *reflist,
2641                                 size_t referral_count)
2642 {
2643         time_t timestamp = 0;
2644
2645         if (!snapper_gmt_strip_snapshot(talloc_tos(),
2646                                         handle,
2647                                         smb_fname,
2648                                         &timestamp,
2649                                         NULL)) {
2650                 return NT_STATUS_NO_MEMORY;
2651         }
2652         if (timestamp != 0) {
2653                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
2654         }
2655         return SMB_VFS_NEXT_CREATE_DFS_PATHAT(handle,
2656                         dirfsp,
2657                         smb_fname,
2658                         reflist,
2659                         referral_count);
2660 }
2661
2662 static struct vfs_fn_pointers snapper_fns = {
2663         .snap_check_path_fn = snapper_snap_check_path,
2664         .snap_create_fn = snapper_snap_create,
2665         .snap_delete_fn = snapper_snap_delete,
2666         .get_shadow_copy_data_fn = snapper_get_shadow_copy_data,
2667         .create_dfs_pathat_fn = snapper_create_dfs_pathat,
2668         .disk_free_fn = snapper_gmt_disk_free,
2669         .get_quota_fn = snapper_gmt_get_quota,
2670         .renameat_fn = snapper_gmt_renameat,
2671         .linkat_fn = snapper_gmt_linkat,
2672         .symlinkat_fn = snapper_gmt_symlinkat,
2673         .stat_fn = snapper_gmt_stat,
2674         .lstat_fn = snapper_gmt_lstat,
2675         .openat_fn = snapper_gmt_openat,
2676         .unlinkat_fn = snapper_gmt_unlinkat,
2677         .fchmod_fn = snapper_gmt_fchmod,
2678         .chdir_fn = snapper_gmt_chdir,
2679         .fntimes_fn = snapper_gmt_fntimes,
2680         .readlinkat_fn = snapper_gmt_readlinkat,
2681         .mknodat_fn = snapper_gmt_mknodat,
2682         .realpath_fn = snapper_gmt_realpath,
2683         .mkdirat_fn = snapper_gmt_mkdirat,
2684         .getxattr_fn = snapper_gmt_getxattr,
2685         .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
2686         .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
2687         .fsetxattr_fn = snapper_gmt_fsetxattr,
2688         .chflags_fn = snapper_gmt_chflags,
2689         .get_real_filename_fn = snapper_gmt_get_real_filename,
2690 };
2691
2692 static_decl_vfs;
2693 NTSTATUS vfs_snapper_init(TALLOC_CTX *ctx)
2694 {
2695         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2696                                 "snapper", &snapper_fns);
2697 }