s3: vfs: snapper: Add and use len_before_gmt, calculated as (p-name).
[Samba.git] / source3 / modules / vfs_snapper.c
blobf5ccc15f4836daba73b580940631c5202fa874ab
1 /*
2 * Module for snapshot IO using snapper
4 * Copyright (C) David Disseldorp 2012-2014
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
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.
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.
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/>.
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"
41 #define SNAPPER_SIG_LIST_SNAPS_RSP "a(uquxussa{ss})"
42 #define SNAPPER_SIG_LIST_CONFS_RSP "a(ssa{ss})"
43 #define SNAPPER_SIG_CREATE_SNAP_RSP "u"
44 #define SNAPPER_SIG_DEL_SNAPS_RSP ""
45 #define SNAPPER_SIG_STRING_DICT "{ss}"
47 struct snapper_dict {
48 char *key;
49 char *val;
52 struct snapper_snap {
53 uint32_t id;
54 uint16_t type;
55 uint32_t pre_id;
56 int64_t time;
57 uint32_t creator_uid;
58 char *desc;
59 char *cleanup;
60 uint32_t num_user_data;
61 struct snapper_dict *user_data;
64 struct snapper_conf {
65 char *name;
66 char *mnt;
67 uint32_t num_attrs;
68 struct snapper_dict *attrs;
71 static const struct {
72 const char *snapper_err_str;
73 NTSTATUS status;
74 } snapper_err_map[] = {
75 { "error.no_permissions", NT_STATUS_ACCESS_DENIED },
78 static NTSTATUS snapper_err_ntstatus_map(const char *snapper_err_str)
80 int i;
82 if (snapper_err_str == NULL) {
83 return NT_STATUS_UNSUCCESSFUL;
85 for (i = 0; i < ARRAY_SIZE(snapper_err_map); i++) {
86 if (!strcmp(snapper_err_map[i].snapper_err_str,
87 snapper_err_str)) {
88 return snapper_err_map[i].status;
91 DEBUG(2, ("no explicit mapping for dbus error: %s\n", snapper_err_str));
93 return NT_STATUS_UNSUCCESSFUL;
97 * Strings are UTF-8. Other characters must be encoded hexadecimal as "\x??".
98 * As a consequence "\" must be encoded as "\\".
100 static NTSTATUS snapper_dbus_str_encode(TALLOC_CTX *mem_ctx, const char *in_str,
101 char **_out_str)
103 size_t in_len;
104 char *out_str;
105 int i;
106 int out_off;
107 int out_len;
109 if (in_str == NULL) {
110 return NT_STATUS_INVALID_PARAMETER;
113 in_len = strlen(in_str);
115 /* output can be max 4 times the length of @in_str, +1 for terminator */
116 out_len = (in_len * 4) + 1;
118 out_str = talloc_array(mem_ctx, char, out_len);
119 if (out_str == NULL) {
120 return NT_STATUS_NO_MEMORY;
123 out_off = 0;
124 for (i = 0; i < in_len; i++) {
125 size_t pushed;
127 if (in_str[i] == '\\') {
128 pushed = snprintf(out_str + out_off, out_len - out_off,
129 "\\\\");
130 } else if ((unsigned char)in_str[i] > 127) {
131 pushed = snprintf(out_str + out_off, out_len - out_off,
132 "\\x%02x", (unsigned char)in_str[i]);
133 } else {
134 /* regular character */
135 *(out_str + out_off) = in_str[i];
136 pushed = sizeof(char);
138 if (pushed >= out_len - out_off) {
139 /* truncated, should never happen */
140 talloc_free(out_str);
141 return NT_STATUS_INTERNAL_ERROR;
143 out_off += pushed;
146 *(out_str + out_off) = '\0';
147 *_out_str = out_str;
149 return NT_STATUS_OK;
152 static NTSTATUS snapper_dbus_str_decode(TALLOC_CTX *mem_ctx, const char *in_str,
153 char **_out_str)
155 size_t in_len;
156 char *out_str;
157 int i;
158 int out_off;
159 int out_len;
161 if (in_str == NULL) {
162 return NT_STATUS_INVALID_PARAMETER;
165 in_len = strlen(in_str);
167 /* output cannot be larger than input, +1 for terminator */
168 out_len = in_len + 1;
170 out_str = talloc_array(mem_ctx, char, out_len);
171 if (out_str == NULL) {
172 return NT_STATUS_NO_MEMORY;
175 out_off = 0;
176 for (i = 0; i < in_len; i++) {
177 int j;
178 char hex_buf[3];
179 unsigned int non_ascii_byte;
181 if (in_str[i] != '\\') {
182 out_str[out_off] = in_str[i];
183 out_off++;
184 continue;
187 i++;
188 if (in_str[i] == '\\') {
189 out_str[out_off] = '\\';
190 out_off++;
191 continue;
192 } else if (in_str[i] != 'x') {
193 goto err_invalid_src_encoding;
196 /* non-ASCII, encoded as two hex chars */
197 for (j = 0; j < 2; j++) {
198 i++;
199 if ((in_str[i] == '\0') || !isxdigit(in_str[i])) {
200 goto err_invalid_src_encoding;
202 hex_buf[j] = in_str[i];
204 hex_buf[2] = '\0';
206 sscanf(hex_buf, "%x", &non_ascii_byte);
207 out_str[out_off] = (unsigned char)non_ascii_byte;
208 out_off++;
211 out_str[out_off] = '\0';
212 *_out_str = out_str;
214 return NT_STATUS_OK;
215 err_invalid_src_encoding:
216 DEBUG(0, ("invalid encoding %s\n", in_str));
217 return NT_STATUS_INVALID_PARAMETER;
220 static DBusConnection *snapper_dbus_conn_create(void)
222 DBusError err;
223 DBusConnection *dconn;
225 dbus_error_init(&err);
228 * Always create a new DBus connection, to ensure snapperd detects the
229 * correct client [E]UID. With dbus_bus_get() it does not!
231 dconn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
232 if (dbus_error_is_set(&err)) {
233 DEBUG(0, ("dbus connection error: %s\n", err.message));
234 dbus_error_free(&err);
236 if (dconn == NULL) {
237 return NULL;
240 /* dbus_bus_get_private() sets exit-on-disconnect by default, undo it */
241 dbus_connection_set_exit_on_disconnect(dconn, false);
243 return dconn;
246 static void snapper_dbus_conn_destroy(DBusConnection *dconn)
248 if (dconn == NULL) {
249 DEBUG(2, ("attempt to destroy NULL dbus connection\n"));
250 return;
253 dbus_connection_close(dconn);
254 dbus_connection_unref(dconn);
258 * send the message @send_msg over the dbus and wait for a response, return the
259 * responsee via @recv_msg_out.
260 * @send_msg is not freed, dbus_message_unref() must be handled by the caller.
262 static NTSTATUS snapper_dbus_msg_xchng(DBusConnection *dconn,
263 DBusMessage *send_msg,
264 DBusMessage **recv_msg_out)
266 DBusPendingCall *pending;
267 DBusMessage *recv_msg;
269 /* send message and get a handle for a reply */
270 if (!dbus_connection_send_with_reply(dconn, send_msg, &pending, -1)) {
271 return NT_STATUS_NO_MEMORY;
273 if (NULL == pending) {
274 DEBUG(0, ("dbus msg send failed\n"));
275 return NT_STATUS_UNSUCCESSFUL;
278 dbus_connection_flush(dconn);
280 /* block until we receive a reply */
281 dbus_pending_call_block(pending);
283 /* get the reply message */
284 recv_msg = dbus_pending_call_steal_reply(pending);
285 if (recv_msg == NULL) {
286 DEBUG(0, ("Reply Null\n"));
287 return NT_STATUS_UNSUCCESSFUL;
289 /* free the pending message handle */
290 dbus_pending_call_unref(pending);
291 *recv_msg_out = recv_msg;
293 return NT_STATUS_OK;
296 static NTSTATUS snapper_type_check(DBusMessageIter *iter,
297 int expected_type)
299 int type = dbus_message_iter_get_arg_type(iter);
300 if (type != expected_type) {
301 DEBUG(0, ("got type %d, expecting %d\n",
302 type, expected_type));
303 return NT_STATUS_INVALID_PARAMETER;
306 return NT_STATUS_OK;
309 static NTSTATUS snapper_type_check_get(DBusMessageIter *iter,
310 int expected_type,
311 void *val)
313 NTSTATUS status;
314 status = snapper_type_check(iter, expected_type);
315 if (!NT_STATUS_IS_OK(status)) {
316 return status;
319 dbus_message_iter_get_basic(iter, val);
321 return NT_STATUS_OK;
324 static NTSTATUS snapper_dict_unpack(TALLOC_CTX *mem_ctx,
325 DBusMessageIter *iter,
326 struct snapper_dict *dict_out)
329 NTSTATUS status;
330 DBusMessageIter dct_iter;
331 char *key_encoded;
332 char *val_encoded;
334 status = snapper_type_check(iter, DBUS_TYPE_DICT_ENTRY);
335 if (!NT_STATUS_IS_OK(status)) {
336 return status;
338 dbus_message_iter_recurse(iter, &dct_iter);
340 status = snapper_type_check_get(&dct_iter, DBUS_TYPE_STRING,
341 &key_encoded);
342 if (!NT_STATUS_IS_OK(status)) {
343 return status;
345 status = snapper_dbus_str_decode(mem_ctx, key_encoded, &dict_out->key);
346 if (!NT_STATUS_IS_OK(status)) {
347 return status;
350 dbus_message_iter_next(&dct_iter);
351 status = snapper_type_check_get(&dct_iter, DBUS_TYPE_STRING,
352 &val_encoded);
353 if (!NT_STATUS_IS_OK(status)) {
354 talloc_free(dict_out->key);
355 return status;
357 status = snapper_dbus_str_decode(mem_ctx, val_encoded, &dict_out->val);
358 if (!NT_STATUS_IS_OK(status)) {
359 talloc_free(dict_out->key);
360 return status;
363 return NT_STATUS_OK;
366 static void snapper_dict_array_print(uint32_t num_dicts,
367 struct snapper_dict *dicts)
369 int i;
371 for (i = 0; i < num_dicts; i++) {
372 DEBUG(10, ("dict (key: %s, val: %s)\n",
373 dicts[i].key, dicts[i].val));
377 static NTSTATUS snapper_dict_array_unpack(TALLOC_CTX *mem_ctx,
378 DBusMessageIter *iter,
379 uint32_t *num_dicts_out,
380 struct snapper_dict **dicts_out)
382 NTSTATUS status;
383 DBusMessageIter array_iter;
384 uint32_t num_dicts;
385 struct snapper_dict *dicts = NULL;
387 status = snapper_type_check(iter, DBUS_TYPE_ARRAY);
388 if (!NT_STATUS_IS_OK(status)) {
389 return status;
391 dbus_message_iter_recurse(iter, &array_iter);
393 num_dicts = 0;
394 while (dbus_message_iter_get_arg_type(&array_iter)
395 != DBUS_TYPE_INVALID) {
396 num_dicts++;
397 dicts = talloc_realloc(mem_ctx, dicts, struct snapper_dict,
398 num_dicts);
399 if (dicts == NULL)
400 abort();
402 status = snapper_dict_unpack(mem_ctx, &array_iter,
403 &dicts[num_dicts - 1]);
404 if (!NT_STATUS_IS_OK(status)) {
405 talloc_free(dicts);
406 return status;
408 dbus_message_iter_next(&array_iter);
411 *num_dicts_out = num_dicts;
412 *dicts_out = dicts;
414 return NT_STATUS_OK;
417 static NTSTATUS snapper_list_confs_pack(DBusMessage **req_msg_out)
419 DBusMessage *msg;
421 msg = dbus_message_new_method_call("org.opensuse.Snapper",
422 "/org/opensuse/Snapper",
423 "org.opensuse.Snapper",
424 "ListConfigs");
425 if (msg == NULL) {
426 DEBUG(0, ("null msg\n"));
427 return NT_STATUS_NO_MEMORY;
430 /* no arguments to append */
431 *req_msg_out = msg;
433 return NT_STATUS_OK;
436 static NTSTATUS snapper_conf_unpack(TALLOC_CTX *mem_ctx,
437 DBusMessageIter *iter,
438 struct snapper_conf *conf_out)
440 NTSTATUS status;
441 DBusMessageIter st_iter;
442 char *name_encoded;
443 char *mnt_encoded;
445 status = snapper_type_check(iter, DBUS_TYPE_STRUCT);
446 if (!NT_STATUS_IS_OK(status)) {
447 return status;
449 dbus_message_iter_recurse(iter, &st_iter);
451 status = snapper_type_check_get(&st_iter, DBUS_TYPE_STRING,
452 &name_encoded);
453 if (!NT_STATUS_IS_OK(status)) {
454 return status;
457 status = snapper_dbus_str_decode(mem_ctx, name_encoded,
458 &conf_out->name);
459 if (!NT_STATUS_IS_OK(status)) {
460 return status;
463 dbus_message_iter_next(&st_iter);
464 status = snapper_type_check_get(&st_iter, DBUS_TYPE_STRING,
465 &mnt_encoded);
466 if (!NT_STATUS_IS_OK(status)) {
467 talloc_free(conf_out->name);
468 return status;
471 status = snapper_dbus_str_decode(mem_ctx, mnt_encoded,
472 &conf_out->mnt);
473 if (!NT_STATUS_IS_OK(status)) {
474 talloc_free(conf_out->name);
475 return status;
478 dbus_message_iter_next(&st_iter);
479 status = snapper_dict_array_unpack(mem_ctx, &st_iter,
480 &conf_out->num_attrs,
481 &conf_out->attrs);
482 if (!NT_STATUS_IS_OK(status)) {
483 talloc_free(conf_out->mnt);
484 talloc_free(conf_out->name);
485 return status;
488 return NT_STATUS_OK;
491 static struct snapper_conf *snapper_conf_array_base_find(int32_t num_confs,
492 struct snapper_conf *confs,
493 const char *base)
495 int i;
497 for (i = 0; i < num_confs; i++) {
498 if (strcmp(confs[i].mnt, base) == 0) {
499 DEBUG(5, ("found snapper conf %s for path %s\n",
500 confs[i].name, base));
501 return &confs[i];
504 DEBUG(5, ("config for base %s not found\n", base));
506 return NULL;
509 static void snapper_conf_array_print(int32_t num_confs,
510 struct snapper_conf *confs)
512 int i;
514 for (i = 0; i < num_confs; i++) {
515 DEBUG(10, ("name: %s, mnt: %s\n",
516 confs[i].name, confs[i].mnt));
517 snapper_dict_array_print(confs[i].num_attrs, confs[i].attrs);
521 static NTSTATUS snapper_conf_array_unpack(TALLOC_CTX *mem_ctx,
522 DBusMessageIter *iter,
523 uint32_t *num_confs_out,
524 struct snapper_conf **confs_out)
526 uint32_t num_confs;
527 NTSTATUS status;
528 struct snapper_conf *confs = NULL;
529 DBusMessageIter array_iter;
532 status = snapper_type_check(iter, DBUS_TYPE_ARRAY);
533 if (!NT_STATUS_IS_OK(status)) {
534 return status;
536 dbus_message_iter_recurse(iter, &array_iter);
538 num_confs = 0;
539 while (dbus_message_iter_get_arg_type(&array_iter)
540 != DBUS_TYPE_INVALID) {
541 num_confs++;
542 confs = talloc_realloc(mem_ctx, confs, struct snapper_conf,
543 num_confs);
544 if (confs == NULL)
545 abort();
547 status = snapper_conf_unpack(confs, &array_iter,
548 &confs[num_confs - 1]);
549 if (!NT_STATUS_IS_OK(status)) {
550 talloc_free(confs);
551 return status;
553 dbus_message_iter_next(&array_iter);
556 *num_confs_out = num_confs;
557 *confs_out = confs;
559 return NT_STATUS_OK;
562 static NTSTATUS snapper_list_confs_unpack(TALLOC_CTX *mem_ctx,
563 DBusConnection *dconn,
564 DBusMessage *rsp_msg,
565 uint32_t *num_confs_out,
566 struct snapper_conf **confs_out)
568 NTSTATUS status;
569 DBusMessageIter iter;
570 int msg_type;
571 uint32_t num_confs;
572 struct snapper_conf *confs;
573 const char *sig;
575 msg_type = dbus_message_get_type(rsp_msg);
576 if (msg_type == DBUS_MESSAGE_TYPE_ERROR) {
577 const char *err_str = dbus_message_get_error_name(rsp_msg);
578 DEBUG(0, ("list_confs error response: %s\n", err_str));
579 return snapper_err_ntstatus_map(err_str);
582 if (msg_type != DBUS_MESSAGE_TYPE_METHOD_RETURN) {
583 DEBUG(0, ("unexpected list_confs ret type: %d\n",
584 msg_type));
585 return NT_STATUS_INVALID_PARAMETER;
588 sig = dbus_message_get_signature(rsp_msg);
589 if ((sig == NULL)
590 || (strcmp(sig, SNAPPER_SIG_LIST_CONFS_RSP) != 0)) {
591 DEBUG(0, ("bad list confs response sig: %s, expected: %s\n",
592 (sig ? sig : "NULL"), SNAPPER_SIG_LIST_CONFS_RSP));
593 return NT_STATUS_INVALID_PARAMETER;
596 if (!dbus_message_iter_init(rsp_msg, &iter)) {
597 /* FIXME return empty? */
598 DEBUG(0, ("Message has no arguments!\n"));
599 return NT_STATUS_INVALID_PARAMETER;
602 status = snapper_conf_array_unpack(mem_ctx, &iter, &num_confs, &confs);
603 if (!NT_STATUS_IS_OK(status)) {
604 DEBUG(0, ("failed to unpack conf array\n"));
605 return status;
608 snapper_conf_array_print(num_confs, confs);
610 *num_confs_out = num_confs;
611 *confs_out = confs;
613 return NT_STATUS_OK;
616 static NTSTATUS snapper_list_snaps_pack(TALLOC_CTX *mem_ctx,
617 char *snapper_conf,
618 DBusMessage **req_msg_out)
620 DBusMessage *msg;
621 DBusMessageIter args;
622 char *conf_encoded;
623 NTSTATUS status;
625 msg = dbus_message_new_method_call("org.opensuse.Snapper", /* target for the method call */
626 "/org/opensuse/Snapper", /* object to call on */
627 "org.opensuse.Snapper", /* interface to call on */
628 "ListSnapshots"); /* method name */
629 if (msg == NULL) {
630 DEBUG(0, ("failed to create list snaps message\n"));
631 return NT_STATUS_NO_MEMORY;
634 status = snapper_dbus_str_encode(mem_ctx, snapper_conf, &conf_encoded);
635 if (!NT_STATUS_IS_OK(status)) {
636 dbus_message_unref(msg);
637 return status;
640 /* append arguments */
641 dbus_message_iter_init_append(msg, &args);
642 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
643 &conf_encoded)) {
644 talloc_free(conf_encoded);
645 dbus_message_unref(msg);
646 return NT_STATUS_NO_MEMORY;
649 *req_msg_out = msg;
651 return NT_STATUS_OK;
654 static NTSTATUS snapper_snap_struct_unpack(TALLOC_CTX *mem_ctx,
655 DBusMessageIter *iter,
656 struct snapper_snap *snap_out)
658 NTSTATUS status;
659 DBusMessageIter st_iter;
660 char *desc_encoded;
661 char *cleanup_encoded;
663 status = snapper_type_check(iter, DBUS_TYPE_STRUCT);
664 if (!NT_STATUS_IS_OK(status)) {
665 return status;
667 dbus_message_iter_recurse(iter, &st_iter);
669 status = snapper_type_check_get(&st_iter, DBUS_TYPE_UINT32,
670 &snap_out->id);
671 if (!NT_STATUS_IS_OK(status)) {
672 return status;
675 dbus_message_iter_next(&st_iter);
676 status = snapper_type_check_get(&st_iter, DBUS_TYPE_UINT16,
677 &snap_out->type);
678 if (!NT_STATUS_IS_OK(status)) {
679 return status;
682 dbus_message_iter_next(&st_iter);
683 status = snapper_type_check_get(&st_iter, DBUS_TYPE_UINT32,
684 &snap_out->pre_id);
685 if (!NT_STATUS_IS_OK(status)) {
686 return status;
689 dbus_message_iter_next(&st_iter);
690 status = snapper_type_check_get(&st_iter, DBUS_TYPE_INT64,
691 &snap_out->time);
692 if (!NT_STATUS_IS_OK(status)) {
693 return status;
696 dbus_message_iter_next(&st_iter);
697 status = snapper_type_check_get(&st_iter, DBUS_TYPE_UINT32,
698 &snap_out->creator_uid);
699 if (!NT_STATUS_IS_OK(status)) {
700 return status;
703 dbus_message_iter_next(&st_iter);
704 status = snapper_type_check_get(&st_iter, DBUS_TYPE_STRING,
705 &desc_encoded);
706 if (!NT_STATUS_IS_OK(status)) {
707 return status;
710 status = snapper_dbus_str_decode(mem_ctx, desc_encoded,
711 &snap_out->desc);
712 if (!NT_STATUS_IS_OK(status)) {
713 return status;
716 dbus_message_iter_next(&st_iter);
717 status = snapper_type_check_get(&st_iter, DBUS_TYPE_STRING,
718 &cleanup_encoded);
719 if (!NT_STATUS_IS_OK(status)) {
720 talloc_free(snap_out->desc);
721 return status;
724 status = snapper_dbus_str_decode(mem_ctx, cleanup_encoded,
725 &snap_out->cleanup);
726 if (!NT_STATUS_IS_OK(status)) {
727 talloc_free(snap_out->desc);
728 return status;
731 dbus_message_iter_next(&st_iter);
732 status = snapper_dict_array_unpack(mem_ctx, &st_iter,
733 &snap_out->num_user_data,
734 &snap_out->user_data);
735 if (!NT_STATUS_IS_OK(status)) {
736 talloc_free(snap_out->cleanup);
737 talloc_free(snap_out->desc);
738 return status;
741 return NT_STATUS_OK;
744 static void snapper_snap_array_print(int32_t num_snaps,
745 struct snapper_snap *snaps)
747 int i;
749 for (i = 0; i < num_snaps; i++) {
750 DEBUG(10, ("id: %u, "
751 "type: %u, "
752 "pre_id: %u, "
753 "time: %ld, "
754 "creator_uid: %u, "
755 "desc: %s, "
756 "cleanup: %s\n",
757 (unsigned int)snaps[i].id,
758 (unsigned int)snaps[i].type,
759 (unsigned int)snaps[i].pre_id,
760 (long int)snaps[i].time,
761 (unsigned int)snaps[i].creator_uid,
762 snaps[i].desc,
763 snaps[i].cleanup));
764 snapper_dict_array_print(snaps[i].num_user_data,
765 snaps[i].user_data);
769 static NTSTATUS snapper_snap_array_unpack(TALLOC_CTX *mem_ctx,
770 DBusMessageIter *iter,
771 uint32_t *num_snaps_out,
772 struct snapper_snap **snaps_out)
774 uint32_t num_snaps;
775 NTSTATUS status;
776 struct snapper_snap *snaps = NULL;
777 DBusMessageIter array_iter;
780 status = snapper_type_check(iter, DBUS_TYPE_ARRAY);
781 if (!NT_STATUS_IS_OK(status)) {
782 return status;
784 dbus_message_iter_recurse(iter, &array_iter);
786 num_snaps = 0;
787 while (dbus_message_iter_get_arg_type(&array_iter)
788 != DBUS_TYPE_INVALID) {
789 num_snaps++;
790 snaps = talloc_realloc(mem_ctx, snaps, struct snapper_snap,
791 num_snaps);
792 if (snaps == NULL)
793 abort();
795 status = snapper_snap_struct_unpack(snaps, &array_iter,
796 &snaps[num_snaps - 1]);
797 if (!NT_STATUS_IS_OK(status)) {
798 talloc_free(snaps);
799 return status;
801 dbus_message_iter_next(&array_iter);
804 *num_snaps_out = num_snaps;
805 *snaps_out = snaps;
807 return NT_STATUS_OK;
810 static NTSTATUS snapper_list_snaps_unpack(TALLOC_CTX *mem_ctx,
811 DBusMessage *rsp_msg,
812 uint32_t *num_snaps_out,
813 struct snapper_snap **snaps_out)
815 NTSTATUS status;
816 DBusMessageIter iter;
817 int msg_type;
818 uint32_t num_snaps;
819 struct snapper_snap *snaps;
820 const char *sig;
822 msg_type = dbus_message_get_type(rsp_msg);
823 if (msg_type == DBUS_MESSAGE_TYPE_ERROR) {
824 const char *err_str = dbus_message_get_error_name(rsp_msg);
825 DEBUG(0, ("list_snaps error response: %s\n", err_str));
826 return snapper_err_ntstatus_map(err_str);
829 if (msg_type != DBUS_MESSAGE_TYPE_METHOD_RETURN) {
830 DEBUG(0,("unexpected list_snaps ret type: %d\n",
831 msg_type));
832 return NT_STATUS_INVALID_PARAMETER;
835 sig = dbus_message_get_signature(rsp_msg);
836 if ((sig == NULL)
837 || (strcmp(sig, SNAPPER_SIG_LIST_SNAPS_RSP) != 0)) {
838 DEBUG(0, ("bad list snaps response sig: %s, "
839 "expected: %s\n",
840 (sig ? sig : "NULL"),
841 SNAPPER_SIG_LIST_SNAPS_RSP));
842 return NT_STATUS_INVALID_PARAMETER;
845 /* read the parameters */
846 if (!dbus_message_iter_init(rsp_msg, &iter)) {
847 DEBUG(0, ("response has no arguments!\n"));
848 return NT_STATUS_INVALID_PARAMETER;
851 status = snapper_snap_array_unpack(mem_ctx, &iter, &num_snaps, &snaps);
852 if (!NT_STATUS_IS_OK(status)) {
853 DEBUG(0, ("failed to unpack snap array\n"));
854 return NT_STATUS_INVALID_PARAMETER;
857 snapper_snap_array_print(num_snaps, snaps);
859 *num_snaps_out = num_snaps;
860 *snaps_out = snaps;
862 return NT_STATUS_OK;
865 static NTSTATUS snapper_create_snap_pack(TALLOC_CTX *mem_ctx,
866 const char *snapper_conf,
867 const char *desc,
868 uint32_t num_user_data,
869 struct snapper_dict *user_data,
870 DBusMessage **req_msg_out)
872 DBusMessage *msg;
873 DBusMessageIter args;
874 DBusMessageIter array_iter;
875 DBusMessageIter struct_iter;
876 const char *empty = "";
877 char *str_encoded;
878 uint32_t i;
879 bool ok;
880 TALLOC_CTX *enc_ctx;
881 NTSTATUS status;
883 DEBUG(10, ("CreateSingleSnapshot: %s, %s, %s, num user %u\n",
884 snapper_conf, desc, empty, num_user_data));
886 enc_ctx = talloc_new(mem_ctx);
887 if (enc_ctx == NULL) {
888 return NT_STATUS_NO_MEMORY;
891 msg = dbus_message_new_method_call("org.opensuse.Snapper",
892 "/org/opensuse/Snapper",
893 "org.opensuse.Snapper",
894 "CreateSingleSnapshot");
895 if (msg == NULL) {
896 DEBUG(0, ("failed to create req msg\n"));
897 talloc_free(enc_ctx);
898 return NT_STATUS_NO_MEMORY;
901 status = snapper_dbus_str_encode(enc_ctx, snapper_conf, &str_encoded);
902 if (!NT_STATUS_IS_OK(status)) {
903 dbus_message_unref(msg);
904 talloc_free(enc_ctx);
905 return status;
908 /* append arguments */
909 dbus_message_iter_init_append(msg, &args);
910 ok = dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
911 &str_encoded);
912 if (!ok) {
913 dbus_message_unref(msg);
914 talloc_free(enc_ctx);
915 return NT_STATUS_NO_MEMORY;
918 status = snapper_dbus_str_encode(enc_ctx, desc, &str_encoded);
919 if (!NT_STATUS_IS_OK(status)) {
920 dbus_message_unref(msg);
921 talloc_free(enc_ctx);
922 return status;
925 ok = dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
926 &str_encoded);
927 if (!ok) {
928 dbus_message_unref(msg);
929 talloc_free(enc_ctx);
930 return NT_STATUS_NO_MEMORY;
933 /* cleanup - no need to encode empty string */
934 ok = dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
935 &empty);
936 if (!ok) {
937 dbus_message_unref(msg);
938 talloc_free(enc_ctx);
939 return NT_STATUS_NO_MEMORY;
942 ok = dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY,
943 SNAPPER_SIG_STRING_DICT,
944 &array_iter);
945 if (!ok) {
946 dbus_message_unref(msg);
947 talloc_free(enc_ctx);
948 return NT_STATUS_NO_MEMORY;
951 for (i = 0; i < num_user_data; i++) {
952 ok = dbus_message_iter_open_container(&array_iter,
953 DBUS_TYPE_DICT_ENTRY,
954 NULL, &struct_iter);
955 if (!ok) {
956 dbus_message_unref(msg);
957 talloc_free(enc_ctx);
958 return NT_STATUS_NO_MEMORY;
961 status = snapper_dbus_str_encode(enc_ctx, user_data[i].key,
962 &str_encoded);
963 if (!NT_STATUS_IS_OK(status)) {
964 dbus_message_unref(msg);
965 talloc_free(enc_ctx);
966 return status;
969 ok = dbus_message_iter_append_basic(&struct_iter,
970 DBUS_TYPE_STRING,
971 &str_encoded);
972 if (!ok) {
973 dbus_message_unref(msg);
974 talloc_free(enc_ctx);
975 return NT_STATUS_NO_MEMORY;
978 status = snapper_dbus_str_encode(enc_ctx, user_data[i].val,
979 &str_encoded);
980 if (!NT_STATUS_IS_OK(status)) {
981 dbus_message_unref(msg);
982 talloc_free(enc_ctx);
983 return status;
986 ok = dbus_message_iter_append_basic(&struct_iter,
987 DBUS_TYPE_STRING,
988 &str_encoded);
989 if (!ok) {
990 dbus_message_unref(msg);
991 talloc_free(enc_ctx);
992 return NT_STATUS_NO_MEMORY;
995 ok = dbus_message_iter_close_container(&array_iter, &struct_iter);
996 if (!ok) {
997 dbus_message_unref(msg);
998 talloc_free(enc_ctx);
999 return NT_STATUS_NO_MEMORY;
1003 ok = dbus_message_iter_close_container(&args, &array_iter);
1004 if (!ok) {
1005 dbus_message_unref(msg);
1006 talloc_free(enc_ctx);
1007 return NT_STATUS_NO_MEMORY;
1010 *req_msg_out = msg;
1012 return NT_STATUS_OK;
1015 static NTSTATUS snapper_create_snap_unpack(DBusConnection *conn,
1016 DBusMessage *rsp_msg,
1017 uint32_t *snap_id_out)
1019 NTSTATUS status;
1020 DBusMessageIter iter;
1021 int msg_type;
1022 const char *sig;
1023 uint32_t snap_id;
1025 msg_type = dbus_message_get_type(rsp_msg);
1026 if (msg_type == DBUS_MESSAGE_TYPE_ERROR) {
1027 const char *err_str = dbus_message_get_error_name(rsp_msg);
1028 DEBUG(0, ("create snap error response: %s, euid %d egid %d\n",
1029 err_str, geteuid(), getegid()));
1030 return snapper_err_ntstatus_map(err_str);
1033 if (msg_type != DBUS_MESSAGE_TYPE_METHOD_RETURN) {
1034 DEBUG(0, ("unexpected create snap ret type: %d\n",
1035 msg_type));
1036 return NT_STATUS_INVALID_PARAMETER;
1039 sig = dbus_message_get_signature(rsp_msg);
1040 if ((sig == NULL)
1041 || (strcmp(sig, SNAPPER_SIG_CREATE_SNAP_RSP) != 0)) {
1042 DEBUG(0, ("bad create snap response sig: %s, expected: %s\n",
1043 (sig ? sig : "NULL"), SNAPPER_SIG_CREATE_SNAP_RSP));
1044 return NT_STATUS_INVALID_PARAMETER;
1047 /* read the parameters */
1048 if (!dbus_message_iter_init(rsp_msg, &iter)) {
1049 DEBUG(0, ("response has no arguments!\n"));
1050 return NT_STATUS_INVALID_PARAMETER;
1053 status = snapper_type_check_get(&iter, DBUS_TYPE_UINT32, &snap_id);
1054 if (!NT_STATUS_IS_OK(status)) {
1055 return status;
1057 *snap_id_out = snap_id;
1059 return NT_STATUS_OK;
1062 static NTSTATUS snapper_del_snap_pack(TALLOC_CTX *mem_ctx,
1063 const char *snapper_conf,
1064 uint32_t snap_id,
1065 DBusMessage **req_msg_out)
1067 DBusMessage *msg;
1068 DBusMessageIter args;
1069 DBusMessageIter array_iter;
1070 char *conf_encoded;
1071 bool ok;
1072 NTSTATUS status;
1074 msg = dbus_message_new_method_call("org.opensuse.Snapper",
1075 "/org/opensuse/Snapper",
1076 "org.opensuse.Snapper",
1077 "DeleteSnapshots");
1078 if (msg == NULL) {
1079 DEBUG(0, ("failed to create req msg\n"));
1080 return NT_STATUS_NO_MEMORY;
1083 status = snapper_dbus_str_encode(mem_ctx, snapper_conf, &conf_encoded);
1084 if (!NT_STATUS_IS_OK(status)) {
1085 dbus_message_unref(msg);
1086 return status;
1089 /* append arguments */
1090 dbus_message_iter_init_append(msg, &args);
1091 ok = dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
1092 &conf_encoded);
1093 if (!ok) {
1094 talloc_free(conf_encoded);
1095 dbus_message_unref(msg);
1096 return NT_STATUS_NO_MEMORY;
1099 ok = dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY,
1100 DBUS_TYPE_UINT32_AS_STRING,
1101 &array_iter);
1102 if (!ok) {
1103 talloc_free(conf_encoded);
1104 dbus_message_unref(msg);
1105 return NT_STATUS_NO_MEMORY;
1108 ok = dbus_message_iter_append_basic(&array_iter,
1109 DBUS_TYPE_UINT32,
1110 &snap_id);
1111 if (!ok) {
1112 talloc_free(conf_encoded);
1113 dbus_message_unref(msg);
1114 return NT_STATUS_NO_MEMORY;
1117 dbus_message_iter_close_container(&args, &array_iter);
1118 *req_msg_out = msg;
1120 return NT_STATUS_OK;
1123 static NTSTATUS snapper_del_snap_unpack(DBusConnection *conn,
1124 DBusMessage *rsp_msg)
1126 int msg_type;
1127 const char *sig;
1129 msg_type = dbus_message_get_type(rsp_msg);
1130 if (msg_type == DBUS_MESSAGE_TYPE_ERROR) {
1131 const char *err_str = dbus_message_get_error_name(rsp_msg);
1132 DEBUG(0, ("del snap error response: %s\n", err_str));
1133 return snapper_err_ntstatus_map(err_str);
1136 if (msg_type != DBUS_MESSAGE_TYPE_METHOD_RETURN) {
1137 DEBUG(0, ("unexpected del snap ret type: %d\n",
1138 msg_type));
1139 return NT_STATUS_INVALID_PARAMETER;
1142 sig = dbus_message_get_signature(rsp_msg);
1143 if ((sig == NULL)
1144 || (strcmp(sig, SNAPPER_SIG_DEL_SNAPS_RSP) != 0)) {
1145 DEBUG(0, ("bad create snap response sig: %s, expected: %s\n",
1146 (sig ? sig : "NULL"), SNAPPER_SIG_DEL_SNAPS_RSP));
1147 return NT_STATUS_INVALID_PARAMETER;
1150 /* no parameters in response */
1152 return NT_STATUS_OK;
1155 static NTSTATUS snapper_list_snaps_at_time_pack(TALLOC_CTX *mem_ctx,
1156 const char *snapper_conf,
1157 time_t time_lower,
1158 time_t time_upper,
1159 DBusMessage **req_msg_out)
1161 DBusMessage *msg;
1162 DBusMessageIter args;
1163 char *conf_encoded;
1164 NTSTATUS status;
1166 msg = dbus_message_new_method_call("org.opensuse.Snapper",
1167 "/org/opensuse/Snapper",
1168 "org.opensuse.Snapper",
1169 "ListSnapshotsAtTime");
1170 if (msg == NULL) {
1171 DEBUG(0, ("failed to create list snaps message\n"));
1172 return NT_STATUS_NO_MEMORY;
1175 status = snapper_dbus_str_encode(mem_ctx, snapper_conf, &conf_encoded);
1176 if (!NT_STATUS_IS_OK(status)) {
1177 dbus_message_unref(msg);
1178 return status;
1181 dbus_message_iter_init_append(msg, &args);
1182 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
1183 &conf_encoded)) {
1184 talloc_free(conf_encoded);
1185 dbus_message_unref(msg);
1186 return NT_STATUS_NO_MEMORY;
1189 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT64,
1190 &time_lower)) {
1191 talloc_free(conf_encoded);
1192 dbus_message_unref(msg);
1193 return NT_STATUS_NO_MEMORY;
1196 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT64,
1197 &time_upper)) {
1198 talloc_free(conf_encoded);
1199 dbus_message_unref(msg);
1200 return NT_STATUS_NO_MEMORY;
1203 *req_msg_out = msg;
1205 return NT_STATUS_OK;
1207 /* no snapper_list_snaps_at_time_unpack, use snapper_list_snaps_unpack */
1210 * Determine the snapper snapshot id given a path.
1211 * Ideally this should be determined via a lookup.
1213 static NTSTATUS snapper_snap_path_to_id(TALLOC_CTX *mem_ctx,
1214 const char *snap_path,
1215 uint32_t *snap_id_out)
1217 char *path_dup;
1218 char *str_idx;
1219 char *str_end;
1220 uint32_t snap_id;
1222 path_dup = talloc_strdup(mem_ctx, snap_path);
1223 if (path_dup == NULL) {
1224 return NT_STATUS_NO_MEMORY;
1227 /* trim trailing '/' */
1228 str_idx = path_dup + strlen(path_dup) - 1;
1229 while (*str_idx == '/') {
1230 *str_idx = '\0';
1231 str_idx--;
1234 str_idx = strrchr(path_dup, '/');
1235 if ((str_idx == NULL)
1236 || (strcmp(str_idx + 1, "snapshot") != 0)) {
1237 talloc_free(path_dup);
1238 return NT_STATUS_INVALID_PARAMETER;
1241 while (*str_idx == '/') {
1242 *str_idx = '\0';
1243 str_idx--;
1246 str_idx = strrchr(path_dup, '/');
1247 if (str_idx == NULL) {
1248 talloc_free(path_dup);
1249 return NT_STATUS_INVALID_PARAMETER;
1252 str_idx++;
1253 snap_id = strtoul(str_idx, &str_end, 10);
1254 if (str_idx == str_end) {
1255 talloc_free(path_dup);
1256 return NT_STATUS_INVALID_PARAMETER;
1259 talloc_free(path_dup);
1260 *snap_id_out = snap_id;
1261 return NT_STATUS_OK;
1265 * Determine the snapper snapshot path given an id and base.
1266 * Ideally this should be determined via a lookup.
1268 static NTSTATUS snapper_snap_id_to_path(TALLOC_CTX *mem_ctx,
1269 const char *base_path,
1270 uint32_t snap_id,
1271 char **snap_path_out)
1273 char *snap_path;
1275 snap_path = talloc_asprintf(mem_ctx, "%s/.snapshots/%u/snapshot",
1276 base_path, snap_id);
1277 if (snap_path == NULL) {
1278 return NT_STATUS_NO_MEMORY;
1281 *snap_path_out = snap_path;
1282 return NT_STATUS_OK;
1285 static NTSTATUS snapper_get_conf_call(TALLOC_CTX *mem_ctx,
1286 DBusConnection *dconn,
1287 const char *path,
1288 char **conf_name_out,
1289 char **base_path_out)
1291 NTSTATUS status;
1292 DBusMessage *req_msg;
1293 DBusMessage *rsp_msg;
1294 uint32_t num_confs = 0;
1295 struct snapper_conf *confs = NULL;
1296 struct snapper_conf *conf;
1297 char *conf_name;
1298 char *base_path;
1300 status = snapper_list_confs_pack(&req_msg);
1301 if (!NT_STATUS_IS_OK(status)) {
1302 goto err_out;
1305 status = snapper_dbus_msg_xchng(dconn, req_msg, &rsp_msg);
1306 if (!NT_STATUS_IS_OK(status)) {
1307 goto err_req_free;
1310 status = snapper_list_confs_unpack(mem_ctx, dconn, rsp_msg,
1311 &num_confs, &confs);
1312 if (!NT_STATUS_IS_OK(status)) {
1313 goto err_rsp_free;
1317 * for now we only support shares where the path directly corresponds
1318 * to a snapper configuration.
1320 conf = snapper_conf_array_base_find(num_confs, confs,
1321 path);
1322 if (conf == NULL) {
1323 status = NT_STATUS_NOT_SUPPORTED;
1324 goto err_array_free;
1327 conf_name = talloc_strdup(mem_ctx, conf->name);
1328 if (conf_name == NULL) {
1329 status = NT_STATUS_NO_MEMORY;
1330 goto err_array_free;
1332 base_path = talloc_strdup(mem_ctx, conf->mnt);
1333 if (base_path == NULL) {
1334 status = NT_STATUS_NO_MEMORY;
1335 goto err_conf_name_free;
1338 talloc_free(confs);
1339 dbus_message_unref(rsp_msg);
1340 dbus_message_unref(req_msg);
1342 *conf_name_out = conf_name;
1343 *base_path_out = base_path;
1345 return NT_STATUS_OK;
1347 err_conf_name_free:
1348 talloc_free(conf_name);
1349 err_array_free:
1350 talloc_free(confs);
1351 err_rsp_free:
1352 dbus_message_unref(rsp_msg);
1353 err_req_free:
1354 dbus_message_unref(req_msg);
1355 err_out:
1356 return status;
1360 * Check whether a path can be shadow copied. Return the base volume, allowing
1361 * the caller to determine if multiple paths lie on the same base volume.
1363 static NTSTATUS snapper_snap_check_path(struct vfs_handle_struct *handle,
1364 TALLOC_CTX *mem_ctx,
1365 const char *service_path,
1366 char **base_volume)
1368 NTSTATUS status;
1369 DBusConnection *dconn;
1370 char *conf_name;
1371 char *base_path;
1373 dconn = snapper_dbus_conn_create();
1374 if (dconn == NULL) {
1375 return NT_STATUS_UNSUCCESSFUL;
1378 status = snapper_get_conf_call(mem_ctx, dconn, service_path,
1379 &conf_name, &base_path);
1380 if (!NT_STATUS_IS_OK(status)) {
1381 goto err_conn_close;
1384 talloc_free(conf_name);
1385 *base_volume = base_path;
1386 snapper_dbus_conn_destroy(dconn);
1388 return NT_STATUS_OK;
1390 err_conn_close:
1391 snapper_dbus_conn_destroy(dconn);
1392 return status;
1395 static NTSTATUS snapper_create_snap_call(TALLOC_CTX *mem_ctx,
1396 DBusConnection *dconn,
1397 const char *conf_name,
1398 const char *base_path,
1399 const char *snap_desc,
1400 uint32_t num_user_data,
1401 struct snapper_dict *user_data,
1402 char **snap_path_out)
1404 NTSTATUS status;
1405 DBusMessage *req_msg;
1406 DBusMessage *rsp_msg;
1407 uint32_t snap_id = 0;
1408 char *snap_path;
1410 status = snapper_create_snap_pack(mem_ctx,
1411 conf_name,
1412 snap_desc,
1413 num_user_data,
1414 user_data,
1415 &req_msg);
1416 if (!NT_STATUS_IS_OK(status)) {
1417 goto err_out;
1420 status = snapper_dbus_msg_xchng(dconn, req_msg, &rsp_msg);
1421 if (!NT_STATUS_IS_OK(status)) {
1422 goto err_req_free;
1425 status = snapper_create_snap_unpack(dconn, rsp_msg, &snap_id);
1426 if (!NT_STATUS_IS_OK(status)) {
1427 goto err_rsp_free;
1430 status = snapper_snap_id_to_path(mem_ctx, base_path, snap_id,
1431 &snap_path);
1432 if (!NT_STATUS_IS_OK(status)) {
1433 goto err_rsp_free;
1436 dbus_message_unref(rsp_msg);
1437 dbus_message_unref(req_msg);
1439 DEBUG(6, ("created new snapshot %u at %s\n", snap_id, snap_path));
1440 *snap_path_out = snap_path;
1442 return NT_STATUS_OK;
1444 err_rsp_free:
1445 dbus_message_unref(rsp_msg);
1446 err_req_free:
1447 dbus_message_unref(req_msg);
1448 err_out:
1449 return status;
1452 static NTSTATUS snapper_snap_create(struct vfs_handle_struct *handle,
1453 TALLOC_CTX *mem_ctx,
1454 const char *base_volume,
1455 time_t *tstamp,
1456 bool rw,
1457 char **_base_path,
1458 char **_snap_path)
1460 DBusConnection *dconn;
1461 NTSTATUS status;
1462 char *conf_name;
1463 char *base_path;
1464 char *snap_path = NULL;
1465 TALLOC_CTX *tmp_ctx;
1467 tmp_ctx = talloc_new(mem_ctx);
1468 if (tmp_ctx == NULL) {
1469 return NT_STATUS_NO_MEMORY;
1472 dconn = snapper_dbus_conn_create();
1473 if (dconn == NULL) {
1474 talloc_free(tmp_ctx);
1475 return NT_STATUS_UNSUCCESSFUL;
1478 status = snapper_get_conf_call(tmp_ctx, dconn, base_volume,
1479 &conf_name, &base_path);
1480 if (!NT_STATUS_IS_OK(status)) {
1481 snapper_dbus_conn_destroy(dconn);
1482 talloc_free(tmp_ctx);
1483 return status;
1486 status = snapper_create_snap_call(tmp_ctx, dconn,
1487 conf_name, base_path,
1488 "Snapshot created by Samba",
1489 0, NULL,
1490 &snap_path);
1491 if (!NT_STATUS_IS_OK(status)) {
1492 snapper_dbus_conn_destroy(dconn);
1493 talloc_free(tmp_ctx);
1494 return status;
1497 snapper_dbus_conn_destroy(dconn);
1498 *_base_path = talloc_steal(mem_ctx, base_path);
1499 *_snap_path = talloc_steal(mem_ctx, snap_path);
1500 talloc_free(tmp_ctx);
1502 return NT_STATUS_OK;
1505 static NTSTATUS snapper_delete_snap_call(TALLOC_CTX *mem_ctx,
1506 DBusConnection *dconn,
1507 const char *conf_name,
1508 uint32_t snap_id)
1510 NTSTATUS status;
1511 DBusMessage *req_msg = NULL;
1512 DBusMessage *rsp_msg;
1514 status = snapper_del_snap_pack(mem_ctx, conf_name, snap_id, &req_msg);
1515 if (!NT_STATUS_IS_OK(status)) {
1516 goto err_out;
1519 status = snapper_dbus_msg_xchng(dconn, req_msg, &rsp_msg);
1520 if (!NT_STATUS_IS_OK(status)) {
1521 goto err_req_free;
1524 status = snapper_del_snap_unpack(dconn, rsp_msg);
1525 if (!NT_STATUS_IS_OK(status)) {
1526 goto err_rsp_free;
1529 dbus_message_unref(rsp_msg);
1530 dbus_message_unref(req_msg);
1532 DEBUG(6, ("deleted snapshot %u\n", snap_id));
1534 return NT_STATUS_OK;
1536 err_rsp_free:
1537 dbus_message_unref(rsp_msg);
1538 err_req_free:
1539 dbus_message_unref(req_msg);
1540 err_out:
1541 return status;
1544 static NTSTATUS snapper_snap_delete(struct vfs_handle_struct *handle,
1545 TALLOC_CTX *mem_ctx,
1546 char *base_path,
1547 char *snap_path)
1549 DBusConnection *dconn;
1550 NTSTATUS status;
1551 char *conf_name;
1552 char *snap_base_path;
1553 uint32_t snap_id;
1554 TALLOC_CTX *tmp_ctx;
1556 tmp_ctx = talloc_new(mem_ctx);
1557 if (tmp_ctx == NULL) {
1558 return NT_STATUS_NO_MEMORY;
1561 dconn = snapper_dbus_conn_create();
1562 if (dconn == NULL) {
1563 talloc_free(tmp_ctx);
1564 return NT_STATUS_UNSUCCESSFUL;
1567 status = snapper_get_conf_call(tmp_ctx, dconn, base_path,
1568 &conf_name, &snap_base_path);
1569 if (!NT_STATUS_IS_OK(status)) {
1570 snapper_dbus_conn_destroy(dconn);
1571 talloc_free(tmp_ctx);
1572 return status;
1575 status = snapper_snap_path_to_id(tmp_ctx, snap_path, &snap_id);
1576 if (!NT_STATUS_IS_OK(status)) {
1577 snapper_dbus_conn_destroy(dconn);
1578 talloc_free(tmp_ctx);
1579 return status;
1582 status = snapper_delete_snap_call(tmp_ctx, dconn, conf_name, snap_id);
1583 if (!NT_STATUS_IS_OK(status)) {
1584 snapper_dbus_conn_destroy(dconn);
1585 talloc_free(tmp_ctx);
1586 return status;
1589 snapper_dbus_conn_destroy(dconn);
1590 talloc_free(tmp_ctx);
1592 return NT_STATUS_OK;
1595 /* sc_data used as parent talloc context for all labels */
1596 static int snapper_get_shadow_copy_data(struct vfs_handle_struct *handle,
1597 struct files_struct *fsp,
1598 struct shadow_copy_data *sc_data,
1599 bool labels)
1601 DBusConnection *dconn;
1602 TALLOC_CTX *tmp_ctx;
1603 NTSTATUS status;
1604 char *conf_name;
1605 char *base_path;
1606 DBusMessage *req_msg = NULL;
1607 DBusMessage *rsp_msg;
1608 uint32_t num_snaps;
1609 struct snapper_snap *snaps;
1610 uint32_t i;
1611 uint32_t lbl_off;
1613 tmp_ctx = talloc_new(sc_data);
1614 if (tmp_ctx == NULL) {
1615 status = NT_STATUS_NO_MEMORY;
1616 goto err_out;
1619 dconn = snapper_dbus_conn_create();
1620 if (dconn == NULL) {
1621 status = NT_STATUS_UNSUCCESSFUL;
1622 goto err_mem_ctx_free;
1625 if (fsp->conn->connectpath == NULL) {
1626 status = NT_STATUS_INVALID_PARAMETER;
1627 goto err_conn_free;
1630 status = snapper_get_conf_call(tmp_ctx, dconn,
1631 fsp->conn->connectpath,
1632 &conf_name,
1633 &base_path);
1634 if (!NT_STATUS_IS_OK(status)) {
1635 goto err_conn_free;
1638 status = snapper_list_snaps_pack(tmp_ctx, conf_name, &req_msg);
1639 if (!NT_STATUS_IS_OK(status)) {
1640 goto err_conn_free;
1643 status = snapper_dbus_msg_xchng(dconn, req_msg, &rsp_msg);
1644 if (!NT_STATUS_IS_OK(status)) {
1645 goto err_req_free;
1648 status = snapper_list_snaps_unpack(tmp_ctx, rsp_msg,
1649 &num_snaps, &snaps);
1650 if (!NT_STATUS_IS_OK(status)) {
1651 goto err_rsp_free;
1653 /* we should always get at least one snapshot (current) */
1654 if (num_snaps == 0) {
1655 DEBUG(1, ("zero snapshots in snap list response\n"));
1656 status = NT_STATUS_UNSUCCESSFUL;
1657 goto err_rsp_free;
1660 /* subtract 1, (current) snapshot is not returned */
1661 sc_data->num_volumes = num_snaps - 1;
1662 sc_data->labels = NULL;
1664 if ((labels == false) || (sc_data->num_volumes == 0)) {
1665 /* tokens need not be added to the labels array */
1666 goto done;
1669 sc_data->labels = talloc_array(sc_data, SHADOW_COPY_LABEL,
1670 sc_data->num_volumes);
1671 if (sc_data->labels == NULL) {
1672 status = NT_STATUS_NO_MEMORY;
1673 goto err_rsp_free;
1676 /* start at end for decending order, do not include 0 (current) */
1677 lbl_off = 0;
1678 for (i = num_snaps - 1; i > 0; i--) {
1679 char *lbl = sc_data->labels[lbl_off++];
1680 struct tm gmt_snap_time;
1681 struct tm *tm_ret;
1682 size_t str_sz;
1684 tm_ret = gmtime_r((time_t *)&snaps[i].time, &gmt_snap_time);
1685 if (tm_ret == NULL) {
1686 status = NT_STATUS_UNSUCCESSFUL;
1687 goto err_labels_free;
1689 str_sz = strftime(lbl, sizeof(SHADOW_COPY_LABEL),
1690 "@GMT-%Y.%m.%d-%H.%M.%S", &gmt_snap_time);
1691 if (str_sz == 0) {
1692 status = NT_STATUS_UNSUCCESSFUL;
1693 goto err_labels_free;
1697 done:
1698 talloc_free(tmp_ctx);
1699 dbus_message_unref(rsp_msg);
1700 dbus_message_unref(req_msg);
1701 snapper_dbus_conn_destroy(dconn);
1703 return 0;
1705 err_labels_free:
1706 TALLOC_FREE(sc_data->labels);
1707 err_rsp_free:
1708 dbus_message_unref(rsp_msg);
1709 err_req_free:
1710 dbus_message_unref(req_msg);
1711 err_conn_free:
1712 snapper_dbus_conn_destroy(dconn);
1713 err_mem_ctx_free:
1714 talloc_free(tmp_ctx);
1715 err_out:
1716 errno = map_errno_from_nt_status(status);
1717 return -1;
1720 static bool snapper_gmt_strip_snapshot(TALLOC_CTX *mem_ctx,
1721 struct vfs_handle_struct *handle,
1722 const char *name,
1723 time_t *ptimestamp,
1724 char **pstripped)
1726 struct tm tm;
1727 time_t timestamp;
1728 const char *p;
1729 char *q;
1730 char *stripped;
1731 size_t rest_len, dst_len;
1732 ptrdiff_t len_before_gmt;
1734 p = strstr_m(name, "@GMT-");
1735 if (p == NULL) {
1736 goto no_snapshot;
1738 if ((p > name) && (p[-1] != '/')) {
1739 goto no_snapshot;
1741 len_before_gmt = p - name;
1742 q = strptime(p, GMT_FORMAT, &tm);
1743 if (q == NULL) {
1744 goto no_snapshot;
1746 tm.tm_isdst = -1;
1747 timestamp = timegm(&tm);
1748 if (timestamp == (time_t)-1) {
1749 goto no_snapshot;
1751 if ((p == name) && (q[0] == '\0')) {
1752 if (pstripped != NULL) {
1753 stripped = talloc_strdup(mem_ctx, "");
1754 if (stripped == NULL) {
1755 return false;
1757 *pstripped = stripped;
1759 *ptimestamp = timestamp;
1760 return true;
1762 if (q[0] != '/') {
1763 goto no_snapshot;
1765 q += 1;
1767 rest_len = strlen(q);
1768 dst_len = len_before_gmt + rest_len;
1770 if (pstripped != NULL) {
1771 stripped = talloc_array(mem_ctx, char, dst_len+1);
1772 if (stripped == NULL) {
1773 errno = ENOMEM;
1774 return false;
1776 if (p > name) {
1777 memcpy(stripped, name, len_before_gmt);
1779 if (rest_len > 0) {
1780 memcpy(stripped + len_before_gmt, q, rest_len);
1782 stripped[dst_len] = '\0';
1783 *pstripped = stripped;
1785 *ptimestamp = timestamp;
1786 return true;
1787 no_snapshot:
1788 *ptimestamp = 0;
1789 return true;
1792 static NTSTATUS snapper_get_snap_at_time_call(TALLOC_CTX *mem_ctx,
1793 DBusConnection *dconn,
1794 const char *conf_name,
1795 const char *base_path,
1796 time_t snaptime,
1797 char **snap_path_out)
1799 NTSTATUS status;
1800 DBusMessage *req_msg = NULL;
1801 DBusMessage *rsp_msg;
1802 uint32_t num_snaps;
1803 struct snapper_snap *snaps;
1804 char *snap_path;
1806 status = snapper_list_snaps_at_time_pack(mem_ctx,
1807 conf_name,
1808 snaptime,
1809 snaptime,
1810 &req_msg);
1811 if (!NT_STATUS_IS_OK(status)) {
1812 goto err_out;
1815 status = snapper_dbus_msg_xchng(dconn, req_msg, &rsp_msg);
1816 if (!NT_STATUS_IS_OK(status)) {
1817 goto err_req_free;
1820 status = snapper_list_snaps_unpack(mem_ctx, rsp_msg,
1821 &num_snaps, &snaps);
1822 if (!NT_STATUS_IS_OK(status)) {
1823 goto err_rsp_free;
1826 if (num_snaps == 0) {
1827 DEBUG(4, ("no snapshots found with time: %lu\n", snaptime));
1828 status = NT_STATUS_INVALID_PARAMETER;
1829 goto err_snap_array_free;
1830 } else if (num_snaps > 0) {
1831 DEBUG(4, ("got %u snapshots for single time %lu, using top\n",
1832 num_snaps, snaptime));
1835 status = snapper_snap_id_to_path(mem_ctx, base_path, snaps[0].id,
1836 &snap_path);
1837 if (!NT_STATUS_IS_OK(status)) {
1838 goto err_snap_array_free;
1841 *snap_path_out = snap_path;
1842 err_snap_array_free:
1843 talloc_free(snaps);
1844 err_rsp_free:
1845 dbus_message_unref(rsp_msg);
1846 err_req_free:
1847 dbus_message_unref(req_msg);
1848 err_out:
1849 return status;
1852 static NTSTATUS snapper_snap_path_expand(struct connection_struct *conn,
1853 TALLOC_CTX *mem_ctx,
1854 time_t snap_time,
1855 char **snap_dir_out)
1857 DBusConnection *dconn;
1858 NTSTATUS status;
1859 char *conf_name;
1860 char *base_path;
1861 char *snap_path = NULL;
1863 dconn = snapper_dbus_conn_create();
1864 if (dconn == NULL) {
1865 status = NT_STATUS_UNSUCCESSFUL;
1866 goto err_out;
1869 if (conn->connectpath == NULL) {
1870 status = NT_STATUS_INVALID_PARAMETER;
1871 goto err_conn_free;
1874 status = snapper_get_conf_call(mem_ctx, dconn,
1875 conn->connectpath,
1876 &conf_name,
1877 &base_path);
1878 if (!NT_STATUS_IS_OK(status)) {
1879 goto err_conn_free;
1882 status = snapper_get_snap_at_time_call(mem_ctx, dconn,
1883 conf_name, base_path, snap_time,
1884 &snap_path);
1885 if (!NT_STATUS_IS_OK(status)) {
1886 goto err_conf_name_free;
1889 /* confirm snapshot path is nested under base path */
1890 if (strncmp(snap_path, base_path, strlen(base_path)) != 0) {
1891 status = NT_STATUS_INVALID_PARAMETER;
1892 goto err_snap_path_free;
1895 talloc_free(conf_name);
1896 talloc_free(base_path);
1897 snapper_dbus_conn_destroy(dconn);
1898 *snap_dir_out = snap_path;
1900 return NT_STATUS_OK;
1902 err_snap_path_free:
1903 talloc_free(snap_path);
1904 err_conf_name_free:
1905 talloc_free(conf_name);
1906 talloc_free(base_path);
1907 err_conn_free:
1908 snapper_dbus_conn_destroy(dconn);
1909 err_out:
1910 return status;
1913 static char *snapper_gmt_convert(TALLOC_CTX *mem_ctx,
1914 struct vfs_handle_struct *handle,
1915 const char *name, time_t timestamp)
1917 char *snap_path = NULL;
1918 char *path = NULL;
1919 NTSTATUS status;
1920 int saved_errno;
1922 status = snapper_snap_path_expand(handle->conn, mem_ctx, timestamp,
1923 &snap_path);
1924 if (!NT_STATUS_IS_OK(status)) {
1925 errno = map_errno_from_nt_status(status);
1926 goto err_out;
1929 path = talloc_asprintf(mem_ctx, "%s/%s", snap_path, name);
1930 if (path == NULL) {
1931 errno = ENOMEM;
1932 goto err_snap_path_free;
1935 DEBUG(10, ("converted %s/%s @ time to %s\n",
1936 handle->conn->connectpath, name, path));
1937 return path;
1939 err_snap_path_free:
1940 saved_errno = errno;
1941 talloc_free(snap_path);
1942 errno = saved_errno;
1943 err_out:
1944 return NULL;
1947 static DIR *snapper_gmt_opendir(vfs_handle_struct *handle,
1948 const struct smb_filename *smb_fname,
1949 const char *mask,
1950 uint32_t attr)
1952 time_t timestamp;
1953 char *stripped;
1954 DIR *ret;
1955 int saved_errno;
1956 char *conv;
1957 struct smb_filename *conv_smb_fname = NULL;
1959 if (!snapper_gmt_strip_snapshot(talloc_tos(),
1960 handle,
1961 smb_fname->base_name,
1962 &timestamp,
1963 &stripped)) {
1964 return NULL;
1966 if (timestamp == 0) {
1967 return SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask, attr);
1969 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1970 TALLOC_FREE(stripped);
1971 if (conv == NULL) {
1972 return NULL;
1974 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1975 conv,
1976 NULL,
1977 NULL,
1978 smb_fname->flags);
1979 if (conv_smb_fname == NULL) {
1980 TALLOC_FREE(conv);
1981 errno = ENOMEM;
1982 return NULL;
1985 ret = SMB_VFS_NEXT_OPENDIR(handle, conv_smb_fname, mask, attr);
1986 saved_errno = errno;
1987 TALLOC_FREE(conv);
1988 TALLOC_FREE(conv_smb_fname);
1989 errno = saved_errno;
1990 return ret;
1993 static int snapper_gmt_rename(vfs_handle_struct *handle,
1994 const struct smb_filename *smb_fname_src,
1995 const struct smb_filename *smb_fname_dst)
1997 time_t timestamp_src, timestamp_dst;
1999 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2000 smb_fname_src->base_name,
2001 &timestamp_src, NULL)) {
2002 return -1;
2004 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2005 smb_fname_dst->base_name,
2006 &timestamp_dst, NULL)) {
2007 return -1;
2009 if (timestamp_src != 0) {
2010 errno = EXDEV;
2011 return -1;
2013 if (timestamp_dst != 0) {
2014 errno = EROFS;
2015 return -1;
2017 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
2020 static int snapper_gmt_symlink(vfs_handle_struct *handle,
2021 const char *oldname, const char *newname)
2023 time_t timestamp_old, timestamp_new;
2025 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, oldname,
2026 &timestamp_old, NULL)) {
2027 return -1;
2029 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, newname,
2030 &timestamp_new, NULL)) {
2031 return -1;
2033 if ((timestamp_old != 0) || (timestamp_new != 0)) {
2034 errno = EROFS;
2035 return -1;
2037 return SMB_VFS_NEXT_SYMLINK(handle, oldname, newname);
2040 static int snapper_gmt_link(vfs_handle_struct *handle,
2041 const char *oldname, const char *newname)
2043 time_t timestamp_old, timestamp_new;
2045 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, oldname,
2046 &timestamp_old, NULL)) {
2047 return -1;
2049 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, newname,
2050 &timestamp_new, NULL)) {
2051 return -1;
2053 if ((timestamp_old != 0) || (timestamp_new != 0)) {
2054 errno = EROFS;
2055 return -1;
2057 return SMB_VFS_NEXT_LINK(handle, oldname, newname);
2060 static int snapper_gmt_stat(vfs_handle_struct *handle,
2061 struct smb_filename *smb_fname)
2063 time_t timestamp;
2064 char *stripped, *tmp;
2065 int ret, saved_errno;
2067 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2068 smb_fname->base_name,
2069 &timestamp, &stripped)) {
2070 return -1;
2072 if (timestamp == 0) {
2073 return SMB_VFS_NEXT_STAT(handle, smb_fname);
2076 tmp = smb_fname->base_name;
2077 smb_fname->base_name = snapper_gmt_convert(talloc_tos(), handle,
2078 stripped, timestamp);
2079 TALLOC_FREE(stripped);
2081 if (smb_fname->base_name == NULL) {
2082 smb_fname->base_name = tmp;
2083 return -1;
2086 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
2087 saved_errno = errno;
2089 TALLOC_FREE(smb_fname->base_name);
2090 smb_fname->base_name = tmp;
2092 errno = saved_errno;
2093 return ret;
2096 static int snapper_gmt_lstat(vfs_handle_struct *handle,
2097 struct smb_filename *smb_fname)
2099 time_t timestamp;
2100 char *stripped, *tmp;
2101 int ret, saved_errno;
2103 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2104 smb_fname->base_name,
2105 &timestamp, &stripped)) {
2106 return -1;
2108 if (timestamp == 0) {
2109 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2112 tmp = smb_fname->base_name;
2113 smb_fname->base_name = snapper_gmt_convert(talloc_tos(), handle,
2114 stripped, timestamp);
2115 TALLOC_FREE(stripped);
2117 if (smb_fname->base_name == NULL) {
2118 smb_fname->base_name = tmp;
2119 return -1;
2122 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2123 saved_errno = errno;
2125 TALLOC_FREE(smb_fname->base_name);
2126 smb_fname->base_name = tmp;
2128 errno = saved_errno;
2129 return ret;
2132 static int snapper_gmt_fstat(vfs_handle_struct *handle, files_struct *fsp,
2133 SMB_STRUCT_STAT *sbuf)
2135 time_t timestamp;
2136 int ret;
2138 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
2139 if (ret == -1) {
2140 return ret;
2142 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2143 fsp->fsp_name->base_name,
2144 &timestamp, NULL)) {
2145 return 0;
2147 return 0;
2150 static int snapper_gmt_open(vfs_handle_struct *handle,
2151 struct smb_filename *smb_fname, files_struct *fsp,
2152 int flags, mode_t mode)
2154 time_t timestamp;
2155 char *stripped, *tmp;
2156 int ret, saved_errno;
2158 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2159 smb_fname->base_name,
2160 &timestamp, &stripped)) {
2161 return -1;
2163 if (timestamp == 0) {
2164 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2167 tmp = smb_fname->base_name;
2168 smb_fname->base_name = snapper_gmt_convert(talloc_tos(), handle,
2169 stripped, timestamp);
2170 TALLOC_FREE(stripped);
2172 if (smb_fname->base_name == NULL) {
2173 smb_fname->base_name = tmp;
2174 return -1;
2177 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2178 saved_errno = errno;
2180 TALLOC_FREE(smb_fname->base_name);
2181 smb_fname->base_name = tmp;
2183 errno = saved_errno;
2184 return ret;
2187 static int snapper_gmt_unlink(vfs_handle_struct *handle,
2188 const struct smb_filename *smb_fname)
2190 time_t timestamp;
2191 char *stripped;
2192 int ret, saved_errno;
2193 struct smb_filename *conv;
2195 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2196 smb_fname->base_name,
2197 &timestamp, &stripped)) {
2198 return -1;
2200 if (timestamp == 0) {
2201 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2203 conv = cp_smb_filename(talloc_tos(), smb_fname);
2204 if (conv == NULL) {
2205 errno = ENOMEM;
2206 return -1;
2208 conv->base_name = snapper_gmt_convert(conv, handle,
2209 stripped, timestamp);
2210 TALLOC_FREE(stripped);
2211 if (conv->base_name == NULL) {
2212 return -1;
2214 ret = SMB_VFS_NEXT_UNLINK(handle, conv);
2215 saved_errno = errno;
2216 TALLOC_FREE(conv);
2217 errno = saved_errno;
2218 return ret;
2221 static int snapper_gmt_chmod(vfs_handle_struct *handle,
2222 const struct smb_filename *smb_fname,
2223 mode_t mode)
2225 time_t timestamp;
2226 char *stripped = NULL;
2227 int ret, saved_errno;
2228 char *conv = NULL;
2229 struct smb_filename *conv_smb_fname = NULL;
2231 if (!snapper_gmt_strip_snapshot(talloc_tos(),
2232 handle,
2233 smb_fname->base_name,
2234 &timestamp,
2235 &stripped)) {
2236 return -1;
2238 if (timestamp == 0) {
2239 TALLOC_FREE(stripped);
2240 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
2242 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2243 TALLOC_FREE(stripped);
2244 if (conv == NULL) {
2245 return -1;
2247 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2248 conv,
2249 NULL,
2250 NULL,
2251 smb_fname->flags);
2252 if (conv_smb_fname == NULL) {
2253 TALLOC_FREE(conv);
2254 errno = ENOMEM;
2255 return -1;
2258 ret = SMB_VFS_NEXT_CHMOD(handle, conv_smb_fname, mode);
2259 saved_errno = errno;
2260 TALLOC_FREE(conv);
2261 TALLOC_FREE(conv_smb_fname);
2262 errno = saved_errno;
2263 return ret;
2266 static int snapper_gmt_chown(vfs_handle_struct *handle,
2267 const struct smb_filename *smb_fname,
2268 uid_t uid,
2269 gid_t gid)
2271 time_t timestamp;
2272 char *stripped = NULL;
2273 int ret, saved_errno;
2274 char *conv = NULL;
2275 struct smb_filename *conv_smb_fname = NULL;
2277 if (!snapper_gmt_strip_snapshot(talloc_tos(),
2278 handle,
2279 smb_fname->base_name,
2280 &timestamp,
2281 &stripped)) {
2282 return -1;
2284 if (timestamp == 0) {
2285 TALLOC_FREE(stripped);
2286 return SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
2288 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2289 TALLOC_FREE(stripped);
2290 if (conv == NULL) {
2291 return -1;
2293 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2294 conv,
2295 NULL,
2296 NULL,
2297 smb_fname->flags);
2298 if (conv_smb_fname == NULL) {
2299 TALLOC_FREE(conv);
2300 errno = ENOMEM;
2301 return -1;
2303 ret = SMB_VFS_NEXT_CHOWN(handle, conv_smb_fname, uid, gid);
2304 saved_errno = errno;
2305 TALLOC_FREE(conv);
2306 TALLOC_FREE(conv_smb_fname);
2307 errno = saved_errno;
2308 return ret;
2311 static int snapper_gmt_chdir(vfs_handle_struct *handle,
2312 const char *fname)
2314 time_t timestamp;
2315 char *stripped;
2316 int ret, saved_errno;
2317 char *conv;
2319 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
2320 &timestamp, &stripped)) {
2321 return -1;
2323 if (timestamp == 0) {
2324 return SMB_VFS_NEXT_CHDIR(handle, fname);
2326 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2327 TALLOC_FREE(stripped);
2328 if (conv == NULL) {
2329 return -1;
2331 ret = SMB_VFS_NEXT_CHDIR(handle, conv);
2332 saved_errno = errno;
2333 TALLOC_FREE(conv);
2334 errno = saved_errno;
2335 return ret;
2338 static int snapper_gmt_ntimes(vfs_handle_struct *handle,
2339 const struct smb_filename *smb_fname,
2340 struct smb_file_time *ft)
2342 time_t timestamp;
2343 char *stripped;
2344 int ret, saved_errno;
2345 struct smb_filename *conv;
2347 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2348 smb_fname->base_name,
2349 &timestamp, &stripped)) {
2350 return -1;
2352 if (timestamp == 0) {
2353 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
2355 conv = cp_smb_filename(talloc_tos(), smb_fname);
2356 if (conv == NULL) {
2357 errno = ENOMEM;
2358 return -1;
2360 conv->base_name = snapper_gmt_convert(conv, handle,
2361 stripped, timestamp);
2362 TALLOC_FREE(stripped);
2363 if (conv->base_name == NULL) {
2364 return -1;
2366 ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
2367 saved_errno = errno;
2368 TALLOC_FREE(conv);
2369 errno = saved_errno;
2370 return ret;
2373 static int snapper_gmt_readlink(vfs_handle_struct *handle,
2374 const char *fname, char *buf, size_t bufsiz)
2376 time_t timestamp;
2377 char *stripped;
2378 int ret, saved_errno;
2379 char *conv;
2381 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
2382 &timestamp, &stripped)) {
2383 return -1;
2385 if (timestamp == 0) {
2386 return SMB_VFS_NEXT_READLINK(handle, fname, buf, bufsiz);
2388 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2389 TALLOC_FREE(stripped);
2390 if (conv == NULL) {
2391 return -1;
2393 ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
2394 saved_errno = errno;
2395 TALLOC_FREE(conv);
2396 errno = saved_errno;
2397 return ret;
2400 static int snapper_gmt_mknod(vfs_handle_struct *handle,
2401 const char *fname, mode_t mode, SMB_DEV_T dev)
2403 time_t timestamp;
2404 char *stripped;
2405 int ret, saved_errno;
2406 char *conv;
2408 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
2409 &timestamp, &stripped)) {
2410 return -1;
2412 if (timestamp == 0) {
2413 return SMB_VFS_NEXT_MKNOD(handle, fname, mode, dev);
2415 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2416 TALLOC_FREE(stripped);
2417 if (conv == NULL) {
2418 return -1;
2420 ret = SMB_VFS_NEXT_MKNOD(handle, conv, mode, dev);
2421 saved_errno = errno;
2422 TALLOC_FREE(conv);
2423 errno = saved_errno;
2424 return ret;
2427 static char *snapper_gmt_realpath(vfs_handle_struct *handle,
2428 const char *fname)
2430 time_t timestamp;
2431 char *stripped = NULL;
2432 char *tmp = NULL;
2433 char *result = NULL;
2434 int saved_errno;
2436 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
2437 &timestamp, &stripped)) {
2438 goto done;
2440 if (timestamp == 0) {
2441 return SMB_VFS_NEXT_REALPATH(handle, fname);
2444 tmp = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2445 if (tmp == NULL) {
2446 goto done;
2449 result = SMB_VFS_NEXT_REALPATH(handle, tmp);
2450 if (result == NULL) {
2451 goto done;
2454 done:
2455 saved_errno = errno;
2456 TALLOC_FREE(tmp);
2457 TALLOC_FREE(stripped);
2458 errno = saved_errno;
2459 return result;
2462 static NTSTATUS snapper_gmt_fget_nt_acl(vfs_handle_struct *handle,
2463 struct files_struct *fsp,
2464 uint32_t security_info,
2465 TALLOC_CTX *mem_ctx,
2466 struct security_descriptor **ppdesc)
2468 time_t timestamp;
2469 char *stripped;
2470 NTSTATUS status;
2471 char *conv;
2472 struct smb_filename *smb_fname = NULL;
2474 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
2475 fsp->fsp_name->base_name,
2476 &timestamp, &stripped)) {
2477 return map_nt_error_from_unix(errno);
2479 if (timestamp == 0) {
2480 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
2481 mem_ctx,
2482 ppdesc);
2484 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2485 TALLOC_FREE(stripped);
2486 if (conv == NULL) {
2487 return map_nt_error_from_unix(errno);
2490 smb_fname = synthetic_smb_fname(talloc_tos(),
2491 conv,
2492 NULL,
2493 NULL,
2494 fsp->fsp_name->flags);
2495 TALLOC_FREE(conv);
2496 if (smb_fname == NULL) {
2497 return NT_STATUS_NO_MEMORY;
2500 status = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
2501 mem_ctx, ppdesc);
2502 TALLOC_FREE(smb_fname);
2503 return status;
2506 static NTSTATUS snapper_gmt_get_nt_acl(vfs_handle_struct *handle,
2507 const struct smb_filename *fname,
2508 uint32_t security_info,
2509 TALLOC_CTX *mem_ctx,
2510 struct security_descriptor **ppdesc)
2512 time_t timestamp;
2513 char *stripped;
2514 NTSTATUS status;
2515 char *conv;
2516 struct smb_filename *smb_fname = NULL;
2518 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname->base_name,
2519 &timestamp, &stripped)) {
2520 return map_nt_error_from_unix(errno);
2522 if (timestamp == 0) {
2523 return SMB_VFS_NEXT_GET_NT_ACL(handle, fname, security_info,
2524 mem_ctx, ppdesc);
2526 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2527 TALLOC_FREE(stripped);
2528 if (conv == NULL) {
2529 return map_nt_error_from_unix(errno);
2531 smb_fname = synthetic_smb_fname(talloc_tos(),
2532 conv,
2533 NULL,
2534 NULL,
2535 fname->flags);
2536 TALLOC_FREE(conv);
2537 if (smb_fname == NULL) {
2538 return NT_STATUS_NO_MEMORY;
2541 status = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
2542 mem_ctx, ppdesc);
2543 TALLOC_FREE(smb_fname);
2544 return status;
2547 static int snapper_gmt_mkdir(vfs_handle_struct *handle,
2548 const struct smb_filename *fname,
2549 mode_t mode)
2551 time_t timestamp;
2552 char *stripped;
2553 int ret, saved_errno;
2554 char *conv;
2555 struct smb_filename *smb_fname = NULL;
2557 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname->base_name,
2558 &timestamp, &stripped)) {
2559 return -1;
2561 if (timestamp == 0) {
2562 return SMB_VFS_NEXT_MKDIR(handle, fname, mode);
2564 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2565 TALLOC_FREE(stripped);
2566 if (conv == NULL) {
2567 return -1;
2569 smb_fname = synthetic_smb_fname(talloc_tos(),
2570 conv,
2571 NULL,
2572 NULL,
2573 fname->flags);
2574 TALLOC_FREE(conv);
2575 if (smb_fname == NULL) {
2576 errno = ENOMEM;
2577 return -1;
2580 ret = SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
2581 saved_errno = errno;
2582 TALLOC_FREE(smb_fname);
2583 errno = saved_errno;
2584 return ret;
2587 static int snapper_gmt_rmdir(vfs_handle_struct *handle,
2588 const struct smb_filename *fname)
2590 time_t timestamp;
2591 char *stripped;
2592 int ret, saved_errno;
2593 char *conv;
2594 struct smb_filename *smb_fname = NULL;
2596 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname->base_name,
2597 &timestamp, &stripped)) {
2598 return -1;
2600 if (timestamp == 0) {
2601 return SMB_VFS_NEXT_RMDIR(handle, fname);
2603 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2604 TALLOC_FREE(stripped);
2605 if (conv == NULL) {
2606 return -1;
2608 smb_fname = synthetic_smb_fname(talloc_tos(),
2609 conv,
2610 NULL,
2611 NULL,
2612 fname->flags);
2613 TALLOC_FREE(conv);
2614 if (smb_fname == NULL) {
2615 errno = ENOMEM;
2616 return -1;
2618 ret = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
2619 saved_errno = errno;
2620 TALLOC_FREE(smb_fname);
2621 errno = saved_errno;
2622 return ret;
2625 static int snapper_gmt_chflags(vfs_handle_struct *handle, const char *fname,
2626 unsigned int flags)
2628 time_t timestamp;
2629 char *stripped;
2630 int ret, saved_errno;
2631 char *conv;
2633 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
2634 &timestamp, &stripped)) {
2635 return -1;
2637 if (timestamp == 0) {
2638 return SMB_VFS_NEXT_CHFLAGS(handle, fname, flags);
2640 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2641 TALLOC_FREE(stripped);
2642 if (conv == NULL) {
2643 return -1;
2645 ret = SMB_VFS_NEXT_CHFLAGS(handle, conv, flags);
2646 saved_errno = errno;
2647 TALLOC_FREE(conv);
2648 errno = saved_errno;
2649 return ret;
2652 static ssize_t snapper_gmt_getxattr(vfs_handle_struct *handle,
2653 const char *fname, const char *aname,
2654 void *value, size_t size)
2656 time_t timestamp;
2657 char *stripped;
2658 ssize_t ret;
2659 int saved_errno;
2660 char *conv;
2662 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
2663 &timestamp, &stripped)) {
2664 return -1;
2666 if (timestamp == 0) {
2667 return SMB_VFS_NEXT_GETXATTR(handle, fname, aname, value,
2668 size);
2670 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2671 TALLOC_FREE(stripped);
2672 if (conv == NULL) {
2673 return -1;
2675 ret = SMB_VFS_NEXT_GETXATTR(handle, conv, aname, value, size);
2676 saved_errno = errno;
2677 TALLOC_FREE(conv);
2678 errno = saved_errno;
2679 return ret;
2682 static ssize_t snapper_gmt_listxattr(struct vfs_handle_struct *handle,
2683 const char *fname,
2684 char *list, size_t size)
2686 time_t timestamp;
2687 char *stripped;
2688 ssize_t ret;
2689 int saved_errno;
2690 char *conv;
2692 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
2693 &timestamp, &stripped)) {
2694 return -1;
2696 if (timestamp == 0) {
2697 return SMB_VFS_NEXT_LISTXATTR(handle, fname, list, size);
2699 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2700 TALLOC_FREE(stripped);
2701 if (conv == NULL) {
2702 return -1;
2704 ret = SMB_VFS_NEXT_LISTXATTR(handle, conv, list, size);
2705 saved_errno = errno;
2706 TALLOC_FREE(conv);
2707 errno = saved_errno;
2708 return ret;
2711 static int snapper_gmt_removexattr(vfs_handle_struct *handle,
2712 const char *fname, const char *aname)
2714 time_t timestamp;
2715 char *stripped;
2716 int ret, saved_errno;
2717 char *conv;
2719 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
2720 &timestamp, &stripped)) {
2721 return -1;
2723 if (timestamp == 0) {
2724 return SMB_VFS_NEXT_REMOVEXATTR(handle, fname, aname);
2726 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2727 TALLOC_FREE(stripped);
2728 if (conv == NULL) {
2729 return -1;
2731 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv, aname);
2732 saved_errno = errno;
2733 TALLOC_FREE(conv);
2734 errno = saved_errno;
2735 return ret;
2738 static int snapper_gmt_setxattr(struct vfs_handle_struct *handle,
2739 const char *fname,
2740 const char *aname, const void *value,
2741 size_t size, int flags)
2743 time_t timestamp;
2744 char *stripped;
2745 ssize_t ret;
2746 int saved_errno;
2747 char *conv;
2749 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
2750 &timestamp, &stripped)) {
2751 return -1;
2753 if (timestamp == 0) {
2754 return SMB_VFS_NEXT_SETXATTR(handle, fname, aname, value, size,
2755 flags);
2757 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2758 TALLOC_FREE(stripped);
2759 if (conv == NULL) {
2760 return -1;
2762 ret = SMB_VFS_NEXT_SETXATTR(handle, conv, aname, value, size, flags);
2763 saved_errno = errno;
2764 TALLOC_FREE(conv);
2765 errno = saved_errno;
2766 return ret;
2769 static int snapper_gmt_chmod_acl(vfs_handle_struct *handle,
2770 const struct smb_filename *smb_fname,
2771 mode_t mode)
2773 time_t timestamp;
2774 char *stripped;
2775 ssize_t ret;
2776 int saved_errno;
2777 char *conv;
2778 struct smb_filename *conv_smb_fname = NULL;
2780 if (!snapper_gmt_strip_snapshot(talloc_tos(),
2781 handle,
2782 smb_fname->base_name,
2783 &timestamp,
2784 &stripped)) {
2785 return -1;
2787 if (timestamp == 0) {
2788 return SMB_VFS_NEXT_CHMOD_ACL(handle, smb_fname, mode);
2790 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2791 TALLOC_FREE(stripped);
2792 if (conv == NULL) {
2793 return -1;
2795 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2796 conv,
2797 NULL,
2798 NULL,
2799 smb_fname->flags);
2800 if (conv_smb_fname == NULL) {
2801 TALLOC_FREE(conv);
2802 errno = ENOMEM;
2803 return -1;
2805 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv_smb_fname, mode);
2806 saved_errno = errno;
2807 TALLOC_FREE(conv);
2808 TALLOC_FREE(conv_smb_fname);
2809 errno = saved_errno;
2810 return ret;
2813 static int snapper_gmt_get_real_filename(struct vfs_handle_struct *handle,
2814 const char *path,
2815 const char *name,
2816 TALLOC_CTX *mem_ctx,
2817 char **found_name)
2819 time_t timestamp;
2820 char *stripped;
2821 ssize_t ret;
2822 int saved_errno;
2823 char *conv;
2825 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, path,
2826 &timestamp, &stripped)) {
2827 return -1;
2829 if (timestamp == 0) {
2830 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
2831 mem_ctx, found_name);
2833 if (stripped[0] == '\0') {
2834 *found_name = talloc_strdup(mem_ctx, name);
2835 if (*found_name == NULL) {
2836 errno = ENOMEM;
2837 return -1;
2839 return 0;
2841 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2842 TALLOC_FREE(stripped);
2843 if (conv == NULL) {
2844 return -1;
2846 ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
2847 mem_ctx, found_name);
2848 saved_errno = errno;
2849 TALLOC_FREE(conv);
2850 errno = saved_errno;
2851 return ret;
2854 static uint64_t snapper_gmt_disk_free(vfs_handle_struct *handle,
2855 const char *path, uint64_t *bsize,
2856 uint64_t *dfree, uint64_t *dsize)
2858 time_t timestamp;
2859 char *stripped;
2860 ssize_t ret;
2861 int saved_errno;
2862 char *conv;
2864 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, path,
2865 &timestamp, &stripped)) {
2866 return -1;
2868 if (timestamp == 0) {
2869 return SMB_VFS_NEXT_DISK_FREE(handle, path,
2870 bsize, dfree, dsize);
2873 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2874 TALLOC_FREE(stripped);
2875 if (conv == NULL) {
2876 return -1;
2879 ret = SMB_VFS_NEXT_DISK_FREE(handle, conv, bsize, dfree, dsize);
2881 saved_errno = errno;
2882 TALLOC_FREE(conv);
2883 errno = saved_errno;
2885 return ret;
2888 static int snapper_gmt_get_quota(vfs_handle_struct *handle, const char *path,
2889 enum SMB_QUOTA_TYPE qtype, unid_t id,
2890 SMB_DISK_QUOTA *dq)
2892 time_t timestamp;
2893 char *stripped;
2894 int ret;
2895 int saved_errno;
2896 char *conv;
2898 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, path, &timestamp,
2899 &stripped)) {
2900 return -1;
2902 if (timestamp == 0) {
2903 return SMB_VFS_NEXT_GET_QUOTA(handle, path, qtype, id, dq);
2906 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2907 TALLOC_FREE(stripped);
2908 if (conv == NULL) {
2909 return -1;
2912 ret = SMB_VFS_NEXT_GET_QUOTA(handle, conv, qtype, id, dq);
2914 saved_errno = errno;
2915 TALLOC_FREE(conv);
2916 errno = saved_errno;
2918 return ret;
2922 static struct vfs_fn_pointers snapper_fns = {
2923 .snap_check_path_fn = snapper_snap_check_path,
2924 .snap_create_fn = snapper_snap_create,
2925 .snap_delete_fn = snapper_snap_delete,
2926 .get_shadow_copy_data_fn = snapper_get_shadow_copy_data,
2927 .opendir_fn = snapper_gmt_opendir,
2928 .disk_free_fn = snapper_gmt_disk_free,
2929 .get_quota_fn = snapper_gmt_get_quota,
2930 .rename_fn = snapper_gmt_rename,
2931 .link_fn = snapper_gmt_link,
2932 .symlink_fn = snapper_gmt_symlink,
2933 .stat_fn = snapper_gmt_stat,
2934 .lstat_fn = snapper_gmt_lstat,
2935 .fstat_fn = snapper_gmt_fstat,
2936 .open_fn = snapper_gmt_open,
2937 .unlink_fn = snapper_gmt_unlink,
2938 .chmod_fn = snapper_gmt_chmod,
2939 .chown_fn = snapper_gmt_chown,
2940 .chdir_fn = snapper_gmt_chdir,
2941 .ntimes_fn = snapper_gmt_ntimes,
2942 .readlink_fn = snapper_gmt_readlink,
2943 .mknod_fn = snapper_gmt_mknod,
2944 .realpath_fn = snapper_gmt_realpath,
2945 .get_nt_acl_fn = snapper_gmt_get_nt_acl,
2946 .fget_nt_acl_fn = snapper_gmt_fget_nt_acl,
2947 .mkdir_fn = snapper_gmt_mkdir,
2948 .rmdir_fn = snapper_gmt_rmdir,
2949 .getxattr_fn = snapper_gmt_getxattr,
2950 .listxattr_fn = snapper_gmt_listxattr,
2951 .removexattr_fn = snapper_gmt_removexattr,
2952 .setxattr_fn = snapper_gmt_setxattr,
2953 .chmod_acl_fn = snapper_gmt_chmod_acl,
2954 .chflags_fn = snapper_gmt_chflags,
2955 .get_real_filename_fn = snapper_gmt_get_real_filename,
2958 NTSTATUS vfs_snapper_init(void);
2959 NTSTATUS vfs_snapper_init(void)
2961 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2962 "snapper", &snapper_fns);