vfs_snapper: add DBus string encoding and decoding helpers
[Samba.git] / source3 / modules / vfs_snapper.c
blob35f2a82c53b29c11ca8d68a48a6ad47d8f6e85e6
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_STRING_DICT "{ss}"
45 struct snapper_dict {
46 char *key;
47 char *val;
50 struct snapper_snap {
51 uint32_t id;
52 uint16_t type;
53 uint32_t pre_id;
54 int64_t time;
55 uint32_t creator_uid;
56 char *desc;
57 char *cleanup;
58 uint32_t num_user_data;
59 struct snapper_dict *user_data;
62 struct snapper_conf {
63 char *name;
64 char *mnt;
65 uint32_t num_attrs;
66 struct snapper_dict *attrs;
69 static const struct {
70 const char *snapper_err_str;
71 NTSTATUS status;
72 } snapper_err_map[] = {
73 { "error.no_permissions", NT_STATUS_ACCESS_DENIED },
76 static NTSTATUS snapper_err_ntstatus_map(const char *snapper_err_str)
78 int i;
80 if (snapper_err_str == NULL) {
81 return NT_STATUS_UNSUCCESSFUL;
83 for (i = 0; i < ARRAY_SIZE(snapper_err_map); i++) {
84 if (!strcmp(snapper_err_map[i].snapper_err_str,
85 snapper_err_str)) {
86 return snapper_err_map[i].status;
89 DEBUG(2, ("no explicit mapping for dbus error: %s\n", snapper_err_str));
91 return NT_STATUS_UNSUCCESSFUL;
95 * Strings are UTF-8. Other characters must be encoded hexadecimal as "\x??".
96 * As a consequence "\" must be encoded as "\\".
98 static NTSTATUS snapper_dbus_str_encode(TALLOC_CTX *mem_ctx, const char *in_str,
99 char **_out_str)
101 size_t in_len;
102 char *out_str;
103 int i;
104 int out_off;
105 int out_len;
107 if (in_str == NULL) {
108 return NT_STATUS_INVALID_PARAMETER;
111 in_len = strlen(in_str);
113 /* output can be max 4 times the length of @in_str, +1 for terminator */
114 out_len = (in_len * 4) + 1;
116 out_str = talloc_array(mem_ctx, char, out_len);
117 if (out_str == NULL) {
118 return NT_STATUS_NO_MEMORY;
121 out_off = 0;
122 for (i = 0; i < in_len; i++) {
123 size_t pushed;
125 if (in_str[i] == '\\') {
126 pushed = snprintf(out_str + out_off, out_len - out_off,
127 "\\\\");
128 } else if ((unsigned char)in_str[i] > 127) {
129 pushed = snprintf(out_str + out_off, out_len - out_off,
130 "\\x%02x", (unsigned char)in_str[i]);
131 } else {
132 /* regular character */
133 *(out_str + out_off) = in_str[i];
134 pushed = sizeof(char);
136 if (pushed >= out_len - out_off) {
137 /* truncated, should never happen */
138 talloc_free(out_str);
139 return NT_STATUS_INTERNAL_ERROR;
141 out_off += pushed;
144 *(out_str + out_off) = '\0';
145 *_out_str = out_str;
147 return NT_STATUS_OK;
150 static NTSTATUS snapper_dbus_str_decode(TALLOC_CTX *mem_ctx, const char *in_str,
151 char **_out_str)
153 size_t in_len;
154 char *out_str;
155 int i;
156 int out_off;
157 int out_len;
159 if (in_str == NULL) {
160 return NT_STATUS_INVALID_PARAMETER;
163 in_len = strlen(in_str);
165 /* output cannot be larger than input, +1 for terminator */
166 out_len = in_len + 1;
168 out_str = talloc_array(mem_ctx, char, out_len);
169 if (out_str == NULL) {
170 return NT_STATUS_NO_MEMORY;
173 out_off = 0;
174 for (i = 0; i < in_len; i++) {
175 int j;
176 char hex_buf[3];
177 unsigned int non_ascii_byte;
179 if (in_str[i] != '\\') {
180 out_str[out_off] = in_str[i];
181 out_off++;
182 continue;
185 i++;
186 if (in_str[i] == '\\') {
187 out_str[out_off] = '\\';
188 out_off++;
189 continue;
190 } else if (in_str[i] != 'x') {
191 goto err_invalid_src_encoding;
194 /* non-ASCII, encoded as two hex chars */
195 for (j = 0; j < 2; j++) {
196 i++;
197 if ((in_str[i] == '\0') || !isxdigit(in_str[i])) {
198 goto err_invalid_src_encoding;
200 hex_buf[j] = in_str[i];
202 hex_buf[2] = '\0';
204 sscanf(hex_buf, "%x", &non_ascii_byte);
205 out_str[out_off] = (unsigned char)non_ascii_byte;
206 out_off++;
209 out_str[out_off] = '\0';
210 *_out_str = out_str;
212 return NT_STATUS_OK;
213 err_invalid_src_encoding:
214 DEBUG(0, ("invalid encoding %s\n", in_str));
215 return NT_STATUS_INVALID_PARAMETER;
218 static DBusConnection *snapper_dbus_conn_create(void)
220 DBusError err;
221 DBusConnection *dconn;
223 dbus_error_init(&err);
226 * Always create a new DBus connection, to ensure snapperd detects the
227 * correct client [E]UID. With dbus_bus_get() it does not!
229 dconn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
230 if (dbus_error_is_set(&err)) {
231 DEBUG(0, ("dbus connection error: %s\n", err.message));
232 dbus_error_free(&err);
234 if (dconn == NULL) {
235 return NULL;
238 /* dbus_bus_get_private() sets exit-on-disconnect by default, undo it */
239 dbus_connection_set_exit_on_disconnect(dconn, false);
241 return dconn;
244 static void snapper_dbus_conn_destroy(DBusConnection *dconn)
246 if (dconn == NULL) {
247 DEBUG(2, ("attempt to destroy NULL dbus connection\n"));
248 return;
251 dbus_connection_close(dconn);
252 dbus_connection_unref(dconn);
256 * send the message @send_msg over the dbus and wait for a response, return the
257 * responsee via @recv_msg_out.
258 * @send_msg is not freed, dbus_message_unref() must be handled by the caller.
260 static NTSTATUS snapper_dbus_msg_xchng(DBusConnection *dconn,
261 DBusMessage *send_msg,
262 DBusMessage **recv_msg_out)
264 DBusPendingCall *pending;
265 DBusMessage *recv_msg;
267 /* send message and get a handle for a reply */
268 if (!dbus_connection_send_with_reply(dconn, send_msg, &pending, -1)) {
269 return NT_STATUS_NO_MEMORY;
271 if (NULL == pending) {
272 DEBUG(0, ("dbus msg send failed\n"));
273 return NT_STATUS_UNSUCCESSFUL;
276 dbus_connection_flush(dconn);
278 /* block until we receive a reply */
279 dbus_pending_call_block(pending);
281 /* get the reply message */
282 recv_msg = dbus_pending_call_steal_reply(pending);
283 if (recv_msg == NULL) {
284 DEBUG(0, ("Reply Null\n"));
285 return NT_STATUS_UNSUCCESSFUL;
287 /* free the pending message handle */
288 dbus_pending_call_unref(pending);
289 *recv_msg_out = recv_msg;
291 return NT_STATUS_OK;
294 static NTSTATUS snapper_type_check(DBusMessageIter *iter,
295 int expected_type)
297 int type = dbus_message_iter_get_arg_type(iter);
298 if (type != expected_type) {
299 DEBUG(0, ("got type %d, expecting %d\n",
300 type, expected_type));
301 return NT_STATUS_INVALID_PARAMETER;
304 return NT_STATUS_OK;
307 static NTSTATUS snapper_type_check_get(DBusMessageIter *iter,
308 int expected_type,
309 void *val)
311 NTSTATUS status;
312 status = snapper_type_check(iter, expected_type);
313 if (!NT_STATUS_IS_OK(status)) {
314 return status;
317 dbus_message_iter_get_basic(iter, val);
319 return NT_STATUS_OK;
322 static NTSTATUS snapper_dict_unpack(DBusMessageIter *iter,
323 struct snapper_dict *dict_out)
326 NTSTATUS status;
327 DBusMessageIter dct_iter;
329 status = snapper_type_check(iter, DBUS_TYPE_DICT_ENTRY);
330 if (!NT_STATUS_IS_OK(status)) {
331 return status;
333 dbus_message_iter_recurse(iter, &dct_iter);
335 status = snapper_type_check_get(&dct_iter, DBUS_TYPE_STRING,
336 &dict_out->key);
337 if (!NT_STATUS_IS_OK(status)) {
338 return status;
341 dbus_message_iter_next(&dct_iter);
342 status = snapper_type_check_get(&dct_iter, DBUS_TYPE_STRING,
343 &dict_out->val);
344 if (!NT_STATUS_IS_OK(status)) {
345 return status;
348 return NT_STATUS_OK;
351 static void snapper_dict_array_print(uint32_t num_dicts,
352 struct snapper_dict *dicts)
354 int i;
356 for (i = 0; i < num_dicts; i++) {
357 DEBUG(10, ("dict (key: %s, val: %s)\n",
358 dicts[i].key, dicts[i].val));
362 static NTSTATUS snapper_dict_array_unpack(TALLOC_CTX *mem_ctx,
363 DBusMessageIter *iter,
364 uint32_t *num_dicts_out,
365 struct snapper_dict **dicts_out)
367 NTSTATUS status;
368 DBusMessageIter array_iter;
369 uint32_t num_dicts;
370 struct snapper_dict *dicts = NULL;
372 status = snapper_type_check(iter, DBUS_TYPE_ARRAY);
373 if (!NT_STATUS_IS_OK(status)) {
374 return status;
376 dbus_message_iter_recurse(iter, &array_iter);
378 num_dicts = 0;
379 while (dbus_message_iter_get_arg_type(&array_iter)
380 != DBUS_TYPE_INVALID) {
381 num_dicts++;
382 dicts = talloc_realloc(mem_ctx, dicts, struct snapper_dict,
383 num_dicts);
384 if (dicts == NULL)
385 abort();
387 status = snapper_dict_unpack(&array_iter,
388 &dicts[num_dicts - 1]);
389 if (!NT_STATUS_IS_OK(status)) {
390 talloc_free(dicts);
391 return status;
393 dbus_message_iter_next(&array_iter);
396 *num_dicts_out = num_dicts;
397 *dicts_out = dicts;
399 return NT_STATUS_OK;
402 static NTSTATUS snapper_list_confs_pack(DBusMessage **req_msg_out)
404 DBusMessage *msg;
406 msg = dbus_message_new_method_call("org.opensuse.Snapper",
407 "/org/opensuse/Snapper",
408 "org.opensuse.Snapper",
409 "ListConfigs");
410 if (msg == NULL) {
411 DEBUG(0, ("null msg\n"));
412 return NT_STATUS_NO_MEMORY;
415 /* no arguments to append */
416 *req_msg_out = msg;
418 return NT_STATUS_OK;
421 static NTSTATUS snapper_conf_unpack(TALLOC_CTX *mem_ctx,
422 DBusMessageIter *iter,
423 struct snapper_conf *conf_out)
425 NTSTATUS status;
426 DBusMessageIter st_iter;
428 status = snapper_type_check(iter, DBUS_TYPE_STRUCT);
429 if (!NT_STATUS_IS_OK(status)) {
430 return status;
432 dbus_message_iter_recurse(iter, &st_iter);
434 status = snapper_type_check_get(&st_iter, DBUS_TYPE_STRING,
435 &conf_out->name);
436 if (!NT_STATUS_IS_OK(status)) {
437 return status;
440 dbus_message_iter_next(&st_iter);
441 status = snapper_type_check_get(&st_iter, DBUS_TYPE_STRING,
442 &conf_out->mnt);
443 if (!NT_STATUS_IS_OK(status)) {
444 return status;
447 dbus_message_iter_next(&st_iter);
448 status = snapper_dict_array_unpack(mem_ctx, &st_iter,
449 &conf_out->num_attrs,
450 &conf_out->attrs);
452 return status;
455 static struct snapper_conf *snapper_conf_array_base_find(int32_t num_confs,
456 struct snapper_conf *confs,
457 const char *base)
459 int i;
461 for (i = 0; i < num_confs; i++) {
462 if (strcmp(confs[i].mnt, base) == 0) {
463 DEBUG(5, ("found snapper conf %s for path %s\n",
464 confs[i].name, base));
465 return &confs[i];
468 DEBUG(5, ("config for base %s not found\n", base));
470 return NULL;
473 static void snapper_conf_array_print(int32_t num_confs,
474 struct snapper_conf *confs)
476 int i;
478 for (i = 0; i < num_confs; i++) {
479 DEBUG(10, ("name: %s, mnt: %s\n",
480 confs[i].name, confs[i].mnt));
481 snapper_dict_array_print(confs[i].num_attrs, confs[i].attrs);
485 static NTSTATUS snapper_conf_array_unpack(TALLOC_CTX *mem_ctx,
486 DBusMessageIter *iter,
487 uint32_t *num_confs_out,
488 struct snapper_conf **confs_out)
490 uint32_t num_confs;
491 NTSTATUS status;
492 struct snapper_conf *confs = NULL;
493 DBusMessageIter array_iter;
496 status = snapper_type_check(iter, DBUS_TYPE_ARRAY);
497 if (!NT_STATUS_IS_OK(status)) {
498 return status;
500 dbus_message_iter_recurse(iter, &array_iter);
502 num_confs = 0;
503 while (dbus_message_iter_get_arg_type(&array_iter)
504 != DBUS_TYPE_INVALID) {
505 num_confs++;
506 confs = talloc_realloc(mem_ctx, confs, struct snapper_conf,
507 num_confs);
508 if (confs == NULL)
509 abort();
511 status = snapper_conf_unpack(confs, &array_iter,
512 &confs[num_confs - 1]);
513 if (!NT_STATUS_IS_OK(status)) {
514 talloc_free(confs);
515 return status;
517 dbus_message_iter_next(&array_iter);
520 *num_confs_out = num_confs;
521 *confs_out = confs;
523 return NT_STATUS_OK;
526 static NTSTATUS snapper_list_confs_unpack(TALLOC_CTX *mem_ctx,
527 DBusConnection *dconn,
528 DBusMessage *rsp_msg,
529 uint32_t *num_confs_out,
530 struct snapper_conf **confs_out)
532 NTSTATUS status;
533 DBusMessageIter iter;
534 int msg_type;
535 uint32_t num_confs;
536 struct snapper_conf *confs;
537 const char *sig;
539 msg_type = dbus_message_get_type(rsp_msg);
540 if (msg_type == DBUS_MESSAGE_TYPE_ERROR) {
541 const char *err_str = dbus_message_get_error_name(rsp_msg);
542 DEBUG(0, ("list_confs error response: %s\n", err_str));
543 return snapper_err_ntstatus_map(err_str);
546 if (msg_type != DBUS_MESSAGE_TYPE_METHOD_RETURN) {
547 DEBUG(0, ("unexpected list_confs ret type: %d\n",
548 msg_type));
549 return NT_STATUS_INVALID_PARAMETER;
552 sig = dbus_message_get_signature(rsp_msg);
553 if ((sig == NULL)
554 || (strcmp(sig, SNAPPER_SIG_LIST_CONFS_RSP) != 0)) {
555 DEBUG(0, ("bad list confs response sig: %s, expected: %s\n",
556 (sig ? sig : "NULL"), SNAPPER_SIG_LIST_CONFS_RSP));
557 return NT_STATUS_INVALID_PARAMETER;
560 if (!dbus_message_iter_init(rsp_msg, &iter)) {
561 /* FIXME return empty? */
562 DEBUG(0, ("Message has no arguments!\n"));
563 return NT_STATUS_INVALID_PARAMETER;
566 status = snapper_conf_array_unpack(mem_ctx, &iter, &num_confs, &confs);
567 if (!NT_STATUS_IS_OK(status)) {
568 DEBUG(0, ("failed to unpack conf array\n"));
569 return status;
572 snapper_conf_array_print(num_confs, confs);
574 *num_confs_out = num_confs;
575 *confs_out = confs;
577 return NT_STATUS_OK;
580 static NTSTATUS snapper_list_snaps_pack(char *snapper_conf,
581 DBusMessage **req_msg_out)
583 DBusMessage *msg;
584 DBusMessageIter args;
586 msg = dbus_message_new_method_call("org.opensuse.Snapper", /* target for the method call */
587 "/org/opensuse/Snapper", /* object to call on */
588 "org.opensuse.Snapper", /* interface to call on */
589 "ListSnapshots"); /* method name */
590 if (msg == NULL) {
591 DEBUG(0, ("failed to create list snaps message\n"));
592 return NT_STATUS_NO_MEMORY;
595 /* append arguments */
596 dbus_message_iter_init_append(msg, &args);
597 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
598 &snapper_conf)) {
599 dbus_message_unref(msg);
600 return NT_STATUS_NO_MEMORY;
603 *req_msg_out = msg;
605 return NT_STATUS_OK;
608 static NTSTATUS snapper_snap_struct_unpack(TALLOC_CTX *mem_ctx,
609 DBusMessageIter *iter,
610 struct snapper_snap *snap_out)
612 NTSTATUS status;
613 DBusMessageIter st_iter;
615 status = snapper_type_check(iter, DBUS_TYPE_STRUCT);
616 if (!NT_STATUS_IS_OK(status)) {
617 return status;
619 dbus_message_iter_recurse(iter, &st_iter);
621 status = snapper_type_check_get(&st_iter, DBUS_TYPE_UINT32,
622 &snap_out->id);
623 if (!NT_STATUS_IS_OK(status)) {
624 return status;
627 dbus_message_iter_next(&st_iter);
628 status = snapper_type_check_get(&st_iter, DBUS_TYPE_UINT16,
629 &snap_out->type);
630 if (!NT_STATUS_IS_OK(status)) {
631 return status;
634 dbus_message_iter_next(&st_iter);
635 status = snapper_type_check_get(&st_iter, DBUS_TYPE_UINT32,
636 &snap_out->pre_id);
637 if (!NT_STATUS_IS_OK(status)) {
638 return status;
641 dbus_message_iter_next(&st_iter);
642 status = snapper_type_check_get(&st_iter, DBUS_TYPE_INT64,
643 &snap_out->time);
644 if (!NT_STATUS_IS_OK(status)) {
645 return status;
648 dbus_message_iter_next(&st_iter);
649 status = snapper_type_check_get(&st_iter, DBUS_TYPE_UINT32,
650 &snap_out->creator_uid);
651 if (!NT_STATUS_IS_OK(status)) {
652 return status;
655 dbus_message_iter_next(&st_iter);
656 status = snapper_type_check_get(&st_iter, DBUS_TYPE_STRING,
657 &snap_out->desc);
658 if (!NT_STATUS_IS_OK(status)) {
659 return status;
662 dbus_message_iter_next(&st_iter);
663 status = snapper_type_check_get(&st_iter, DBUS_TYPE_STRING,
664 &snap_out->cleanup);
665 if (!NT_STATUS_IS_OK(status)) {
666 return status;
669 dbus_message_iter_next(&st_iter);
670 status = snapper_dict_array_unpack(mem_ctx, &st_iter,
671 &snap_out->num_user_data,
672 &snap_out->user_data);
674 return status;
677 static void snapper_snap_array_print(int32_t num_snaps,
678 struct snapper_snap *snaps)
680 int i;
682 for (i = 0; i < num_snaps; i++) {
683 DEBUG(10, ("id: %u, "
684 "type: %u, "
685 "pre_id: %u, "
686 "time: %ld, "
687 "creator_uid: %u, "
688 "desc: %s, "
689 "cleanup: %s\n",
690 (unsigned int)snaps[i].id,
691 (unsigned int)snaps[i].type,
692 (unsigned int)snaps[i].pre_id,
693 (long int)snaps[i].time,
694 (unsigned int)snaps[i].creator_uid,
695 snaps[i].desc,
696 snaps[i].cleanup));
697 snapper_dict_array_print(snaps[i].num_user_data,
698 snaps[i].user_data);
702 static NTSTATUS snapper_snap_array_unpack(TALLOC_CTX *mem_ctx,
703 DBusMessageIter *iter,
704 uint32_t *num_snaps_out,
705 struct snapper_snap **snaps_out)
707 uint32_t num_snaps;
708 NTSTATUS status;
709 struct snapper_snap *snaps = NULL;
710 DBusMessageIter array_iter;
713 status = snapper_type_check(iter, DBUS_TYPE_ARRAY);
714 if (!NT_STATUS_IS_OK(status)) {
715 return status;
717 dbus_message_iter_recurse(iter, &array_iter);
719 num_snaps = 0;
720 while (dbus_message_iter_get_arg_type(&array_iter)
721 != DBUS_TYPE_INVALID) {
722 num_snaps++;
723 snaps = talloc_realloc(mem_ctx, snaps, struct snapper_snap,
724 num_snaps);
725 if (snaps == NULL)
726 abort();
728 status = snapper_snap_struct_unpack(snaps, &array_iter,
729 &snaps[num_snaps - 1]);
730 if (!NT_STATUS_IS_OK(status)) {
731 talloc_free(snaps);
732 return status;
734 dbus_message_iter_next(&array_iter);
737 *num_snaps_out = num_snaps;
738 *snaps_out = snaps;
740 return NT_STATUS_OK;
743 static NTSTATUS snapper_list_snaps_unpack(TALLOC_CTX *mem_ctx,
744 DBusMessage *rsp_msg,
745 uint32_t *num_snaps_out,
746 struct snapper_snap **snaps_out)
748 NTSTATUS status;
749 DBusMessageIter iter;
750 int msg_type;
751 uint32_t num_snaps;
752 struct snapper_snap *snaps;
753 const char *sig;
755 msg_type = dbus_message_get_type(rsp_msg);
756 if (msg_type == DBUS_MESSAGE_TYPE_ERROR) {
757 const char *err_str = dbus_message_get_error_name(rsp_msg);
758 DEBUG(0, ("list_snaps error response: %s\n", err_str));
759 return snapper_err_ntstatus_map(err_str);
762 if (msg_type != DBUS_MESSAGE_TYPE_METHOD_RETURN) {
763 DEBUG(0,("unexpected list_snaps ret type: %d\n",
764 msg_type));
765 return NT_STATUS_INVALID_PARAMETER;
768 sig = dbus_message_get_signature(rsp_msg);
769 if ((sig == NULL)
770 || (strcmp(sig, SNAPPER_SIG_LIST_SNAPS_RSP) != 0)) {
771 DEBUG(0, ("bad list snaps response sig: %s, "
772 "expected: %s\n",
773 (sig ? sig : "NULL"),
774 SNAPPER_SIG_LIST_SNAPS_RSP));
775 return NT_STATUS_INVALID_PARAMETER;
778 /* read the parameters */
779 if (!dbus_message_iter_init(rsp_msg, &iter)) {
780 DEBUG(0, ("response has no arguments!\n"));
781 return NT_STATUS_INVALID_PARAMETER;
784 status = snapper_snap_array_unpack(mem_ctx, &iter, &num_snaps, &snaps);
785 if (!NT_STATUS_IS_OK(status)) {
786 DEBUG(0, ("failed to unpack snap array\n"));
787 return NT_STATUS_INVALID_PARAMETER;
790 snapper_snap_array_print(num_snaps, snaps);
792 *num_snaps_out = num_snaps;
793 *snaps_out = snaps;
795 return NT_STATUS_OK;
798 static NTSTATUS snapper_list_snaps_at_time_pack(const char *snapper_conf,
799 time_t time_lower,
800 time_t time_upper,
801 DBusMessage **req_msg_out)
803 DBusMessage *msg;
804 DBusMessageIter args;
806 msg = dbus_message_new_method_call("org.opensuse.Snapper",
807 "/org/opensuse/Snapper",
808 "org.opensuse.Snapper",
809 "ListSnapshotsAtTime");
810 if (msg == NULL) {
811 DEBUG(0, ("failed to create list snaps message\n"));
812 return NT_STATUS_NO_MEMORY;
815 dbus_message_iter_init_append(msg, &args);
816 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
817 &snapper_conf)) {
818 dbus_message_unref(msg);
819 return NT_STATUS_NO_MEMORY;
822 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT64,
823 &time_lower)) {
824 dbus_message_unref(msg);
825 return NT_STATUS_NO_MEMORY;
828 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT64,
829 &time_upper)) {
830 dbus_message_unref(msg);
831 return NT_STATUS_NO_MEMORY;
834 *req_msg_out = msg;
836 return NT_STATUS_OK;
838 /* no snapper_list_snaps_at_time_unpack, use snapper_list_snaps_unpack */
841 * Determine the snapper snapshot path given an id and base.
842 * Ideally this should be determined via a lookup.
844 static NTSTATUS snapper_snap_id_to_path(TALLOC_CTX *mem_ctx,
845 const char *base_path,
846 uint32_t snap_id,
847 char **snap_path_out)
849 char *snap_path;
851 snap_path = talloc_asprintf(mem_ctx, "%s/.snapshots/%u/snapshot",
852 base_path, snap_id);
853 if (snap_path == NULL) {
854 return NT_STATUS_NO_MEMORY;
857 *snap_path_out = snap_path;
858 return NT_STATUS_OK;
861 static NTSTATUS snapper_get_conf_call(TALLOC_CTX *mem_ctx,
862 DBusConnection *dconn,
863 const char *path,
864 char **conf_name_out,
865 char **base_path_out)
867 NTSTATUS status;
868 DBusMessage *req_msg;
869 DBusMessage *rsp_msg;
870 uint32_t num_confs = 0;
871 struct snapper_conf *confs = NULL;
872 struct snapper_conf *conf;
873 char *conf_name;
874 char *base_path;
876 status = snapper_list_confs_pack(&req_msg);
877 if (!NT_STATUS_IS_OK(status)) {
878 goto err_out;
881 status = snapper_dbus_msg_xchng(dconn, req_msg, &rsp_msg);
882 if (!NT_STATUS_IS_OK(status)) {
883 goto err_req_free;
886 status = snapper_list_confs_unpack(mem_ctx, dconn, rsp_msg,
887 &num_confs, &confs);
888 if (!NT_STATUS_IS_OK(status)) {
889 goto err_rsp_free;
893 * for now we only support shares where the path directly corresponds
894 * to a snapper configuration.
896 conf = snapper_conf_array_base_find(num_confs, confs,
897 path);
898 if (conf == NULL) {
899 status = NT_STATUS_NOT_SUPPORTED;
900 goto err_array_free;
903 conf_name = talloc_strdup(mem_ctx, conf->name);
904 if (conf_name == NULL) {
905 status = NT_STATUS_NO_MEMORY;
906 goto err_array_free;
908 base_path = talloc_strdup(mem_ctx, conf->mnt);
909 if (base_path == NULL) {
910 status = NT_STATUS_NO_MEMORY;
911 goto err_conf_name_free;
914 talloc_free(confs);
915 dbus_message_unref(rsp_msg);
916 dbus_message_unref(req_msg);
918 *conf_name_out = conf_name;
919 *base_path_out = base_path;
921 return NT_STATUS_OK;
923 err_conf_name_free:
924 talloc_free(conf_name);
925 err_array_free:
926 talloc_free(confs);
927 err_rsp_free:
928 dbus_message_unref(rsp_msg);
929 err_req_free:
930 dbus_message_unref(req_msg);
931 err_out:
932 return status;
935 /* sc_data used as parent talloc context for all labels */
936 static int snapper_get_shadow_copy_data(struct vfs_handle_struct *handle,
937 struct files_struct *fsp,
938 struct shadow_copy_data *sc_data,
939 bool labels)
941 DBusConnection *dconn;
942 TALLOC_CTX *tmp_ctx;
943 NTSTATUS status;
944 char *conf_name;
945 char *base_path;
946 DBusMessage *req_msg;
947 DBusMessage *rsp_msg;
948 uint32_t num_snaps;
949 struct snapper_snap *snaps;
950 uint32_t i;
951 uint32_t lbl_off;
953 tmp_ctx = talloc_new(sc_data);
954 if (tmp_ctx == NULL) {
955 status = NT_STATUS_NO_MEMORY;
956 goto err_out;
959 dconn = snapper_dbus_conn_create();
960 if (dconn == NULL) {
961 status = NT_STATUS_UNSUCCESSFUL;
962 goto err_mem_ctx_free;
965 if (fsp->conn->connectpath == NULL) {
966 status = NT_STATUS_INVALID_PARAMETER;
967 goto err_conn_free;
970 status = snapper_get_conf_call(tmp_ctx, dconn,
971 fsp->conn->connectpath,
972 &conf_name,
973 &base_path);
974 if (!NT_STATUS_IS_OK(status)) {
975 goto err_conn_free;
978 status = snapper_list_snaps_pack(conf_name, &req_msg);
979 if (!NT_STATUS_IS_OK(status)) {
980 goto err_conn_free;
983 status = snapper_dbus_msg_xchng(dconn, req_msg, &rsp_msg);
984 if (!NT_STATUS_IS_OK(status)) {
985 goto err_req_free;
988 status = snapper_list_snaps_unpack(tmp_ctx, rsp_msg,
989 &num_snaps, &snaps);
990 if (!NT_STATUS_IS_OK(status)) {
991 goto err_rsp_free;
993 /* we should always get at least one snapshot (current) */
994 if (num_snaps == 0) {
995 DEBUG(1, ("zero snapshots in snap list response\n"));
996 status = NT_STATUS_UNSUCCESSFUL;
997 goto err_rsp_free;
1000 /* subtract 1, (current) snapshot is not returned */
1001 sc_data->num_volumes = num_snaps - 1;
1002 sc_data->labels = NULL;
1004 if ((labels == false) || (sc_data->num_volumes == 0)) {
1005 /* tokens need not be added to the labels array */
1006 goto done;
1009 sc_data->labels = talloc_array(sc_data, SHADOW_COPY_LABEL,
1010 sc_data->num_volumes);
1011 if (sc_data->labels == NULL) {
1012 status = NT_STATUS_NO_MEMORY;
1013 goto err_rsp_free;
1016 /* start at end for decending order, do not include 0 (current) */
1017 lbl_off = 0;
1018 for (i = num_snaps - 1; i > 0; i--) {
1019 char *lbl = sc_data->labels[lbl_off++];
1020 struct tm gmt_snap_time;
1021 struct tm *tm_ret;
1022 size_t str_sz;
1024 tm_ret = gmtime_r((time_t *)&snaps[i].time, &gmt_snap_time);
1025 if (tm_ret == NULL) {
1026 status = NT_STATUS_UNSUCCESSFUL;
1027 goto err_labels_free;
1029 str_sz = strftime(lbl, sizeof(SHADOW_COPY_LABEL),
1030 "@GMT-%Y.%m.%d-%H.%M.%S", &gmt_snap_time);
1031 if (str_sz == 0) {
1032 status = NT_STATUS_UNSUCCESSFUL;
1033 goto err_labels_free;
1037 done:
1038 talloc_free(tmp_ctx);
1039 dbus_message_unref(rsp_msg);
1040 dbus_message_unref(req_msg);
1041 snapper_dbus_conn_destroy(dconn);
1043 return 0;
1045 err_labels_free:
1046 TALLOC_FREE(sc_data->labels);
1047 err_rsp_free:
1048 dbus_message_unref(rsp_msg);
1049 err_req_free:
1050 dbus_message_unref(req_msg);
1051 err_conn_free:
1052 snapper_dbus_conn_destroy(dconn);
1053 err_mem_ctx_free:
1054 talloc_free(tmp_ctx);
1055 err_out:
1056 errno = map_errno_from_nt_status(status);
1057 return -1;
1060 static bool snapper_gmt_strip_snapshot(TALLOC_CTX *mem_ctx,
1061 struct vfs_handle_struct *handle,
1062 const char *name,
1063 time_t *ptimestamp,
1064 char **pstripped)
1066 struct tm tm;
1067 time_t timestamp;
1068 const char *p;
1069 char *q;
1070 char *stripped;
1071 size_t rest_len, dst_len;
1073 p = strstr_m(name, "@GMT-");
1074 if (p == NULL) {
1075 goto no_snapshot;
1077 if ((p > name) && (p[-1] != '/')) {
1078 goto no_snapshot;
1080 q = strptime(p, GMT_FORMAT, &tm);
1081 if (q == NULL) {
1082 goto no_snapshot;
1084 tm.tm_isdst = -1;
1085 timestamp = timegm(&tm);
1086 if (timestamp == (time_t)-1) {
1087 goto no_snapshot;
1089 if ((p == name) && (q[0] == '\0')) {
1090 if (pstripped != NULL) {
1091 stripped = talloc_strdup(mem_ctx, "");
1092 if (stripped == NULL) {
1093 return false;
1095 *pstripped = stripped;
1097 *ptimestamp = timestamp;
1098 return true;
1100 if (q[0] != '/') {
1101 goto no_snapshot;
1103 q += 1;
1105 rest_len = strlen(q);
1106 dst_len = (p-name) + rest_len;
1108 if (pstripped != NULL) {
1109 stripped = talloc_array(mem_ctx, char, dst_len+1);
1110 if (stripped == NULL) {
1111 errno = ENOMEM;
1112 return false;
1114 if (p > name) {
1115 memcpy(stripped, name, p-name);
1117 if (rest_len > 0) {
1118 memcpy(stripped + (p-name), q, rest_len);
1120 stripped[dst_len] = '\0';
1121 *pstripped = stripped;
1123 *ptimestamp = timestamp;
1124 return true;
1125 no_snapshot:
1126 *ptimestamp = 0;
1127 return true;
1130 static NTSTATUS snapper_get_snap_at_time_call(TALLOC_CTX *mem_ctx,
1131 DBusConnection *dconn,
1132 const char *conf_name,
1133 const char *base_path,
1134 time_t snaptime,
1135 char **snap_path_out)
1137 NTSTATUS status;
1138 DBusMessage *req_msg;
1139 DBusMessage *rsp_msg;
1140 uint32_t num_snaps;
1141 struct snapper_snap *snaps;
1142 char *snap_path;
1144 status = snapper_list_snaps_at_time_pack(conf_name,
1145 snaptime,
1146 snaptime,
1147 &req_msg);
1148 if (!NT_STATUS_IS_OK(status)) {
1149 goto err_out;
1152 status = snapper_dbus_msg_xchng(dconn, req_msg, &rsp_msg);
1153 if (!NT_STATUS_IS_OK(status)) {
1154 goto err_req_free;
1157 status = snapper_list_snaps_unpack(mem_ctx, rsp_msg,
1158 &num_snaps, &snaps);
1159 if (!NT_STATUS_IS_OK(status)) {
1160 goto err_rsp_free;
1163 if (num_snaps == 0) {
1164 DEBUG(4, ("no snapshots found with time: %lu\n", snaptime));
1165 status = NT_STATUS_INVALID_PARAMETER;
1166 goto err_snap_array_free;
1167 } else if (num_snaps > 0) {
1168 DEBUG(4, ("got %u snapshots for single time %lu, using top\n",
1169 num_snaps, snaptime));
1172 status = snapper_snap_id_to_path(mem_ctx, base_path, snaps[0].id,
1173 &snap_path);
1174 if (!NT_STATUS_IS_OK(status)) {
1175 goto err_snap_array_free;
1178 *snap_path_out = snap_path;
1179 err_snap_array_free:
1180 talloc_free(snaps);
1181 err_rsp_free:
1182 dbus_message_unref(rsp_msg);
1183 err_req_free:
1184 dbus_message_unref(req_msg);
1185 err_out:
1186 return status;
1189 static NTSTATUS snapper_snap_path_expand(struct connection_struct *conn,
1190 TALLOC_CTX *mem_ctx,
1191 time_t snap_time,
1192 char **snap_dir_out)
1194 DBusConnection *dconn;
1195 NTSTATUS status;
1196 char *conf_name;
1197 char *base_path;
1198 char *snap_path;
1200 dconn = snapper_dbus_conn_create();
1201 if (dconn == NULL) {
1202 status = NT_STATUS_UNSUCCESSFUL;
1203 goto err_out;
1206 if (conn->connectpath == NULL) {
1207 status = NT_STATUS_INVALID_PARAMETER;
1208 goto err_conn_free;
1211 status = snapper_get_conf_call(mem_ctx, dconn,
1212 conn->connectpath,
1213 &conf_name,
1214 &base_path);
1215 if (!NT_STATUS_IS_OK(status)) {
1216 goto err_conn_free;
1219 status = snapper_get_snap_at_time_call(mem_ctx, dconn,
1220 conf_name, base_path, snap_time,
1221 &snap_path);
1222 if (!NT_STATUS_IS_OK(status)) {
1223 goto err_conf_name_free;
1226 /* confirm snapshot path is nested under base path */
1227 if (strncmp(snap_path, base_path, strlen(base_path)) != 0) {
1228 status = NT_STATUS_INVALID_PARAMETER;
1229 goto err_snap_path_free;
1232 talloc_free(conf_name);
1233 talloc_free(base_path);
1234 snapper_dbus_conn_destroy(dconn);
1235 *snap_dir_out = snap_path;
1237 return NT_STATUS_OK;
1239 err_snap_path_free:
1240 talloc_free(snap_path);
1241 err_conf_name_free:
1242 talloc_free(conf_name);
1243 talloc_free(base_path);
1244 err_conn_free:
1245 snapper_dbus_conn_destroy(dconn);
1246 err_out:
1247 return status;
1250 static char *snapper_gmt_convert(TALLOC_CTX *mem_ctx,
1251 struct vfs_handle_struct *handle,
1252 const char *name, time_t timestamp)
1254 char *snap_path = NULL;
1255 char *path = NULL;
1256 NTSTATUS status;
1257 int saved_errno;
1259 status = snapper_snap_path_expand(handle->conn, mem_ctx, timestamp,
1260 &snap_path);
1261 if (!NT_STATUS_IS_OK(status)) {
1262 errno = map_errno_from_nt_status(status);
1263 goto err_out;
1266 path = talloc_asprintf(mem_ctx, "%s/%s", snap_path, name);
1267 if (path == NULL) {
1268 errno = ENOMEM;
1269 goto err_snap_path_free;
1272 DEBUG(10, ("converted %s/%s @ time to %s\n",
1273 handle->conn->connectpath, name, path));
1274 return path;
1276 err_snap_path_free:
1277 saved_errno = errno;
1278 talloc_free(snap_path);
1279 errno = saved_errno;
1280 err_out:
1281 return NULL;
1284 static DIR *snapper_gmt_opendir(vfs_handle_struct *handle,
1285 const char *fname,
1286 const char *mask,
1287 uint32_t attr)
1289 time_t timestamp;
1290 char *stripped;
1291 DIR *ret;
1292 int saved_errno;
1293 char *conv;
1295 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
1296 &timestamp, &stripped)) {
1297 return NULL;
1299 if (timestamp == 0) {
1300 return SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
1302 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1303 TALLOC_FREE(stripped);
1304 if (conv == NULL) {
1305 return NULL;
1307 ret = SMB_VFS_NEXT_OPENDIR(handle, conv, mask, attr);
1308 saved_errno = errno;
1309 TALLOC_FREE(conv);
1310 errno = saved_errno;
1311 return ret;
1314 static int snapper_gmt_rename(vfs_handle_struct *handle,
1315 const struct smb_filename *smb_fname_src,
1316 const struct smb_filename *smb_fname_dst)
1318 time_t timestamp_src, timestamp_dst;
1320 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
1321 smb_fname_src->base_name,
1322 &timestamp_src, NULL)) {
1323 return -1;
1325 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
1326 smb_fname_dst->base_name,
1327 &timestamp_dst, NULL)) {
1328 return -1;
1330 if (timestamp_src != 0) {
1331 errno = EXDEV;
1332 return -1;
1334 if (timestamp_dst != 0) {
1335 errno = EROFS;
1336 return -1;
1338 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
1341 static int snapper_gmt_symlink(vfs_handle_struct *handle,
1342 const char *oldname, const char *newname)
1344 time_t timestamp_old, timestamp_new;
1346 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, oldname,
1347 &timestamp_old, NULL)) {
1348 return -1;
1350 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, newname,
1351 &timestamp_new, NULL)) {
1352 return -1;
1354 if ((timestamp_old != 0) || (timestamp_new != 0)) {
1355 errno = EROFS;
1356 return -1;
1358 return SMB_VFS_NEXT_SYMLINK(handle, oldname, newname);
1361 static int snapper_gmt_link(vfs_handle_struct *handle,
1362 const char *oldname, const char *newname)
1364 time_t timestamp_old, timestamp_new;
1366 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, oldname,
1367 &timestamp_old, NULL)) {
1368 return -1;
1370 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, newname,
1371 &timestamp_new, NULL)) {
1372 return -1;
1374 if ((timestamp_old != 0) || (timestamp_new != 0)) {
1375 errno = EROFS;
1376 return -1;
1378 return SMB_VFS_NEXT_LINK(handle, oldname, newname);
1381 static int snapper_gmt_stat(vfs_handle_struct *handle,
1382 struct smb_filename *smb_fname)
1384 time_t timestamp;
1385 char *stripped, *tmp;
1386 int ret, saved_errno;
1388 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
1389 smb_fname->base_name,
1390 &timestamp, &stripped)) {
1391 return -1;
1393 if (timestamp == 0) {
1394 return SMB_VFS_NEXT_STAT(handle, smb_fname);
1397 tmp = smb_fname->base_name;
1398 smb_fname->base_name = snapper_gmt_convert(talloc_tos(), handle,
1399 stripped, timestamp);
1400 TALLOC_FREE(stripped);
1402 if (smb_fname->base_name == NULL) {
1403 smb_fname->base_name = tmp;
1404 return -1;
1407 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1408 saved_errno = errno;
1410 TALLOC_FREE(smb_fname->base_name);
1411 smb_fname->base_name = tmp;
1413 errno = saved_errno;
1414 return ret;
1417 static int snapper_gmt_lstat(vfs_handle_struct *handle,
1418 struct smb_filename *smb_fname)
1420 time_t timestamp;
1421 char *stripped, *tmp;
1422 int ret, saved_errno;
1424 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
1425 smb_fname->base_name,
1426 &timestamp, &stripped)) {
1427 return -1;
1429 if (timestamp == 0) {
1430 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1433 tmp = smb_fname->base_name;
1434 smb_fname->base_name = snapper_gmt_convert(talloc_tos(), handle,
1435 stripped, timestamp);
1436 TALLOC_FREE(stripped);
1438 if (smb_fname->base_name == NULL) {
1439 smb_fname->base_name = tmp;
1440 return -1;
1443 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1444 saved_errno = errno;
1446 TALLOC_FREE(smb_fname->base_name);
1447 smb_fname->base_name = tmp;
1449 errno = saved_errno;
1450 return ret;
1453 static int snapper_gmt_fstat(vfs_handle_struct *handle, files_struct *fsp,
1454 SMB_STRUCT_STAT *sbuf)
1456 time_t timestamp;
1457 int ret;
1459 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1460 if (ret == -1) {
1461 return ret;
1463 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
1464 fsp->fsp_name->base_name,
1465 &timestamp, NULL)) {
1466 return 0;
1468 return 0;
1471 static int snapper_gmt_open(vfs_handle_struct *handle,
1472 struct smb_filename *smb_fname, files_struct *fsp,
1473 int flags, mode_t mode)
1475 time_t timestamp;
1476 char *stripped, *tmp;
1477 int ret, saved_errno;
1479 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
1480 smb_fname->base_name,
1481 &timestamp, &stripped)) {
1482 return -1;
1484 if (timestamp == 0) {
1485 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1488 tmp = smb_fname->base_name;
1489 smb_fname->base_name = snapper_gmt_convert(talloc_tos(), handle,
1490 stripped, timestamp);
1491 TALLOC_FREE(stripped);
1493 if (smb_fname->base_name == NULL) {
1494 smb_fname->base_name = tmp;
1495 return -1;
1498 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1499 saved_errno = errno;
1501 TALLOC_FREE(smb_fname->base_name);
1502 smb_fname->base_name = tmp;
1504 errno = saved_errno;
1505 return ret;
1508 static int snapper_gmt_unlink(vfs_handle_struct *handle,
1509 const struct smb_filename *smb_fname)
1511 time_t timestamp;
1512 char *stripped;
1513 int ret, saved_errno;
1514 struct smb_filename *conv;
1516 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
1517 smb_fname->base_name,
1518 &timestamp, &stripped)) {
1519 return -1;
1521 if (timestamp == 0) {
1522 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
1524 conv = cp_smb_filename(talloc_tos(), smb_fname);
1525 if (conv == NULL) {
1526 errno = ENOMEM;
1527 return -1;
1529 conv->base_name = snapper_gmt_convert(conv, handle,
1530 stripped, timestamp);
1531 TALLOC_FREE(stripped);
1532 if (conv->base_name == NULL) {
1533 return -1;
1535 ret = SMB_VFS_NEXT_UNLINK(handle, conv);
1536 saved_errno = errno;
1537 TALLOC_FREE(conv);
1538 errno = saved_errno;
1539 return ret;
1542 static int snapper_gmt_chmod(vfs_handle_struct *handle, const char *fname,
1543 mode_t mode)
1545 time_t timestamp;
1546 char *stripped;
1547 int ret, saved_errno;
1548 char *conv;
1550 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
1551 &timestamp, &stripped)) {
1552 return -1;
1554 if (timestamp == 0) {
1555 return SMB_VFS_NEXT_CHMOD(handle, fname, mode);
1557 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1558 TALLOC_FREE(stripped);
1559 if (conv == NULL) {
1560 return -1;
1562 ret = SMB_VFS_NEXT_CHMOD(handle, conv, mode);
1563 saved_errno = errno;
1564 TALLOC_FREE(conv);
1565 errno = saved_errno;
1566 return ret;
1569 static int snapper_gmt_chown(vfs_handle_struct *handle, const char *fname,
1570 uid_t uid, gid_t gid)
1572 time_t timestamp;
1573 char *stripped;
1574 int ret, saved_errno;
1575 char *conv;
1577 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
1578 &timestamp, &stripped)) {
1579 return -1;
1581 if (timestamp == 0) {
1582 return SMB_VFS_NEXT_CHOWN(handle, fname, uid, gid);
1584 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1585 TALLOC_FREE(stripped);
1586 if (conv == NULL) {
1587 return -1;
1589 ret = SMB_VFS_NEXT_CHOWN(handle, conv, uid, gid);
1590 saved_errno = errno;
1591 TALLOC_FREE(conv);
1592 errno = saved_errno;
1593 return ret;
1596 static int snapper_gmt_chdir(vfs_handle_struct *handle,
1597 const char *fname)
1599 time_t timestamp;
1600 char *stripped;
1601 int ret, saved_errno;
1602 char *conv;
1604 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
1605 &timestamp, &stripped)) {
1606 return -1;
1608 if (timestamp == 0) {
1609 return SMB_VFS_NEXT_CHDIR(handle, fname);
1611 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1612 TALLOC_FREE(stripped);
1613 if (conv == NULL) {
1614 return -1;
1616 ret = SMB_VFS_NEXT_CHDIR(handle, conv);
1617 saved_errno = errno;
1618 TALLOC_FREE(conv);
1619 errno = saved_errno;
1620 return ret;
1623 static int snapper_gmt_ntimes(vfs_handle_struct *handle,
1624 const struct smb_filename *smb_fname,
1625 struct smb_file_time *ft)
1627 time_t timestamp;
1628 char *stripped;
1629 int ret, saved_errno;
1630 struct smb_filename *conv;
1632 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
1633 smb_fname->base_name,
1634 &timestamp, &stripped)) {
1635 return -1;
1637 if (timestamp == 0) {
1638 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1640 conv = cp_smb_filename(talloc_tos(), smb_fname);
1641 if (conv == NULL) {
1642 errno = ENOMEM;
1643 return -1;
1645 conv->base_name = snapper_gmt_convert(conv, handle,
1646 stripped, timestamp);
1647 TALLOC_FREE(stripped);
1648 if (conv->base_name == NULL) {
1649 return -1;
1651 ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
1652 saved_errno = errno;
1653 TALLOC_FREE(conv);
1654 errno = saved_errno;
1655 return ret;
1658 static int snapper_gmt_readlink(vfs_handle_struct *handle,
1659 const char *fname, char *buf, size_t bufsiz)
1661 time_t timestamp;
1662 char *stripped;
1663 int ret, saved_errno;
1664 char *conv;
1666 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
1667 &timestamp, &stripped)) {
1668 return -1;
1670 if (timestamp == 0) {
1671 return SMB_VFS_NEXT_READLINK(handle, fname, buf, bufsiz);
1673 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1674 TALLOC_FREE(stripped);
1675 if (conv == NULL) {
1676 return -1;
1678 ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
1679 saved_errno = errno;
1680 TALLOC_FREE(conv);
1681 errno = saved_errno;
1682 return ret;
1685 static int snapper_gmt_mknod(vfs_handle_struct *handle,
1686 const char *fname, mode_t mode, SMB_DEV_T dev)
1688 time_t timestamp;
1689 char *stripped;
1690 int ret, saved_errno;
1691 char *conv;
1693 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
1694 &timestamp, &stripped)) {
1695 return -1;
1697 if (timestamp == 0) {
1698 return SMB_VFS_NEXT_MKNOD(handle, fname, mode, dev);
1700 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1701 TALLOC_FREE(stripped);
1702 if (conv == NULL) {
1703 return -1;
1705 ret = SMB_VFS_NEXT_MKNOD(handle, conv, mode, dev);
1706 saved_errno = errno;
1707 TALLOC_FREE(conv);
1708 errno = saved_errno;
1709 return ret;
1712 static char *snapper_gmt_realpath(vfs_handle_struct *handle,
1713 const char *fname)
1715 time_t timestamp;
1716 char *stripped = NULL;
1717 char *tmp = NULL;
1718 char *result = NULL;
1719 int saved_errno;
1721 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
1722 &timestamp, &stripped)) {
1723 goto done;
1725 if (timestamp == 0) {
1726 return SMB_VFS_NEXT_REALPATH(handle, fname);
1729 tmp = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1730 if (tmp == NULL) {
1731 goto done;
1734 result = SMB_VFS_NEXT_REALPATH(handle, tmp);
1735 if (result == NULL) {
1736 goto done;
1739 done:
1740 saved_errno = errno;
1741 TALLOC_FREE(tmp);
1742 TALLOC_FREE(stripped);
1743 errno = saved_errno;
1744 return result;
1747 static NTSTATUS snapper_gmt_fget_nt_acl(vfs_handle_struct *handle,
1748 struct files_struct *fsp,
1749 uint32 security_info,
1750 TALLOC_CTX *mem_ctx,
1751 struct security_descriptor **ppdesc)
1753 time_t timestamp;
1754 char *stripped;
1755 NTSTATUS status;
1756 char *conv;
1758 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle,
1759 fsp->fsp_name->base_name,
1760 &timestamp, &stripped)) {
1761 return map_nt_error_from_unix(errno);
1763 if (timestamp == 0) {
1764 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1765 mem_ctx,
1766 ppdesc);
1768 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1769 TALLOC_FREE(stripped);
1770 if (conv == NULL) {
1771 return map_nt_error_from_unix(errno);
1773 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv, security_info,
1774 mem_ctx, ppdesc);
1775 TALLOC_FREE(conv);
1776 return status;
1779 static NTSTATUS snapper_gmt_get_nt_acl(vfs_handle_struct *handle,
1780 const char *fname,
1781 uint32 security_info,
1782 TALLOC_CTX *mem_ctx,
1783 struct security_descriptor **ppdesc)
1785 time_t timestamp;
1786 char *stripped;
1787 NTSTATUS status;
1788 char *conv;
1790 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
1791 &timestamp, &stripped)) {
1792 return map_nt_error_from_unix(errno);
1794 if (timestamp == 0) {
1795 return SMB_VFS_NEXT_GET_NT_ACL(handle, fname, security_info,
1796 mem_ctx, ppdesc);
1798 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1799 TALLOC_FREE(stripped);
1800 if (conv == NULL) {
1801 return map_nt_error_from_unix(errno);
1803 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv, security_info,
1804 mem_ctx, ppdesc);
1805 TALLOC_FREE(conv);
1806 return status;
1809 static int snapper_gmt_mkdir(vfs_handle_struct *handle,
1810 const char *fname, mode_t mode)
1812 time_t timestamp;
1813 char *stripped;
1814 int ret, saved_errno;
1815 char *conv;
1817 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
1818 &timestamp, &stripped)) {
1819 return -1;
1821 if (timestamp == 0) {
1822 return SMB_VFS_NEXT_MKDIR(handle, fname, mode);
1824 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1825 TALLOC_FREE(stripped);
1826 if (conv == NULL) {
1827 return -1;
1829 ret = SMB_VFS_NEXT_MKDIR(handle, conv, mode);
1830 saved_errno = errno;
1831 TALLOC_FREE(conv);
1832 errno = saved_errno;
1833 return ret;
1836 static int snapper_gmt_rmdir(vfs_handle_struct *handle, const char *fname)
1838 time_t timestamp;
1839 char *stripped;
1840 int ret, saved_errno;
1841 char *conv;
1843 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
1844 &timestamp, &stripped)) {
1845 return -1;
1847 if (timestamp == 0) {
1848 return SMB_VFS_NEXT_RMDIR(handle, fname);
1850 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1851 TALLOC_FREE(stripped);
1852 if (conv == NULL) {
1853 return -1;
1855 ret = SMB_VFS_NEXT_RMDIR(handle, conv);
1856 saved_errno = errno;
1857 TALLOC_FREE(conv);
1858 errno = saved_errno;
1859 return ret;
1862 static int snapper_gmt_chflags(vfs_handle_struct *handle, const char *fname,
1863 unsigned int flags)
1865 time_t timestamp;
1866 char *stripped;
1867 int ret, saved_errno;
1868 char *conv;
1870 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
1871 &timestamp, &stripped)) {
1872 return -1;
1874 if (timestamp == 0) {
1875 return SMB_VFS_NEXT_CHFLAGS(handle, fname, flags);
1877 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1878 TALLOC_FREE(stripped);
1879 if (conv == NULL) {
1880 return -1;
1882 ret = SMB_VFS_NEXT_CHFLAGS(handle, conv, flags);
1883 saved_errno = errno;
1884 TALLOC_FREE(conv);
1885 errno = saved_errno;
1886 return ret;
1889 static ssize_t snapper_gmt_getxattr(vfs_handle_struct *handle,
1890 const char *fname, const char *aname,
1891 void *value, size_t size)
1893 time_t timestamp;
1894 char *stripped;
1895 ssize_t ret;
1896 int saved_errno;
1897 char *conv;
1899 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
1900 &timestamp, &stripped)) {
1901 return -1;
1903 if (timestamp == 0) {
1904 return SMB_VFS_NEXT_GETXATTR(handle, fname, aname, value,
1905 size);
1907 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1908 TALLOC_FREE(stripped);
1909 if (conv == NULL) {
1910 return -1;
1912 ret = SMB_VFS_NEXT_GETXATTR(handle, conv, aname, value, size);
1913 saved_errno = errno;
1914 TALLOC_FREE(conv);
1915 errno = saved_errno;
1916 return ret;
1919 static ssize_t snapper_gmt_listxattr(struct vfs_handle_struct *handle,
1920 const char *fname,
1921 char *list, size_t size)
1923 time_t timestamp;
1924 char *stripped;
1925 ssize_t ret;
1926 int saved_errno;
1927 char *conv;
1929 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
1930 &timestamp, &stripped)) {
1931 return -1;
1933 if (timestamp == 0) {
1934 return SMB_VFS_NEXT_LISTXATTR(handle, fname, list, size);
1936 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1937 TALLOC_FREE(stripped);
1938 if (conv == NULL) {
1939 return -1;
1941 ret = SMB_VFS_NEXT_LISTXATTR(handle, conv, list, size);
1942 saved_errno = errno;
1943 TALLOC_FREE(conv);
1944 errno = saved_errno;
1945 return ret;
1948 static int snapper_gmt_removexattr(vfs_handle_struct *handle,
1949 const char *fname, const char *aname)
1951 time_t timestamp;
1952 char *stripped;
1953 int ret, saved_errno;
1954 char *conv;
1956 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
1957 &timestamp, &stripped)) {
1958 return -1;
1960 if (timestamp == 0) {
1961 return SMB_VFS_NEXT_REMOVEXATTR(handle, fname, aname);
1963 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1964 TALLOC_FREE(stripped);
1965 if (conv == NULL) {
1966 return -1;
1968 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv, aname);
1969 saved_errno = errno;
1970 TALLOC_FREE(conv);
1971 errno = saved_errno;
1972 return ret;
1975 static int snapper_gmt_setxattr(struct vfs_handle_struct *handle,
1976 const char *fname,
1977 const char *aname, const void *value,
1978 size_t size, int flags)
1980 time_t timestamp;
1981 char *stripped;
1982 ssize_t ret;
1983 int saved_errno;
1984 char *conv;
1986 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
1987 &timestamp, &stripped)) {
1988 return -1;
1990 if (timestamp == 0) {
1991 return SMB_VFS_NEXT_SETXATTR(handle, fname, aname, value, size,
1992 flags);
1994 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
1995 TALLOC_FREE(stripped);
1996 if (conv == NULL) {
1997 return -1;
1999 ret = SMB_VFS_NEXT_SETXATTR(handle, conv, aname, value, size, flags);
2000 saved_errno = errno;
2001 TALLOC_FREE(conv);
2002 errno = saved_errno;
2003 return ret;
2006 static int snapper_gmt_chmod_acl(vfs_handle_struct *handle,
2007 const char *fname, mode_t mode)
2009 time_t timestamp;
2010 char *stripped;
2011 ssize_t ret;
2012 int saved_errno;
2013 char *conv;
2015 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, fname,
2016 &timestamp, &stripped)) {
2017 return -1;
2019 if (timestamp == 0) {
2020 return SMB_VFS_NEXT_CHMOD_ACL(handle, fname, mode);
2022 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2023 TALLOC_FREE(stripped);
2024 if (conv == NULL) {
2025 return -1;
2027 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv, mode);
2028 saved_errno = errno;
2029 TALLOC_FREE(conv);
2030 errno = saved_errno;
2031 return ret;
2034 static int snapper_gmt_get_real_filename(struct vfs_handle_struct *handle,
2035 const char *path,
2036 const char *name,
2037 TALLOC_CTX *mem_ctx,
2038 char **found_name)
2040 time_t timestamp;
2041 char *stripped;
2042 ssize_t ret;
2043 int saved_errno;
2044 char *conv;
2046 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, path,
2047 &timestamp, &stripped)) {
2048 return -1;
2050 if (timestamp == 0) {
2051 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
2052 mem_ctx, found_name);
2054 if (stripped[0] == '\0') {
2055 *found_name = talloc_strdup(mem_ctx, name);
2056 if (*found_name == NULL) {
2057 errno = ENOMEM;
2058 return -1;
2060 return 0;
2062 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2063 TALLOC_FREE(stripped);
2064 if (conv == NULL) {
2065 return -1;
2067 ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
2068 mem_ctx, found_name);
2069 saved_errno = errno;
2070 TALLOC_FREE(conv);
2071 errno = saved_errno;
2072 return ret;
2075 static uint64_t snapper_gmt_disk_free(vfs_handle_struct *handle,
2076 const char *path, bool small_query,
2077 uint64_t *bsize, uint64_t *dfree,
2078 uint64_t *dsize)
2080 time_t timestamp;
2081 char *stripped;
2082 ssize_t ret;
2083 int saved_errno;
2084 char *conv;
2086 if (!snapper_gmt_strip_snapshot(talloc_tos(), handle, path,
2087 &timestamp, &stripped)) {
2088 return -1;
2090 if (timestamp == 0) {
2091 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
2092 bsize, dfree, dsize);
2095 conv = snapper_gmt_convert(talloc_tos(), handle, stripped, timestamp);
2096 TALLOC_FREE(stripped);
2097 if (conv == NULL) {
2098 return -1;
2101 ret = SMB_VFS_NEXT_DISK_FREE(handle, conv, small_query, bsize, dfree,
2102 dsize);
2104 saved_errno = errno;
2105 TALLOC_FREE(conv);
2106 errno = saved_errno;
2108 return ret;
2112 static struct vfs_fn_pointers snapper_fns = {
2113 .get_shadow_copy_data_fn = snapper_get_shadow_copy_data,
2114 .opendir_fn = snapper_gmt_opendir,
2115 .disk_free_fn = snapper_gmt_disk_free,
2116 .rename_fn = snapper_gmt_rename,
2117 .link_fn = snapper_gmt_link,
2118 .symlink_fn = snapper_gmt_symlink,
2119 .stat_fn = snapper_gmt_stat,
2120 .lstat_fn = snapper_gmt_lstat,
2121 .fstat_fn = snapper_gmt_fstat,
2122 .open_fn = snapper_gmt_open,
2123 .unlink_fn = snapper_gmt_unlink,
2124 .chmod_fn = snapper_gmt_chmod,
2125 .chown_fn = snapper_gmt_chown,
2126 .chdir_fn = snapper_gmt_chdir,
2127 .ntimes_fn = snapper_gmt_ntimes,
2128 .readlink_fn = snapper_gmt_readlink,
2129 .mknod_fn = snapper_gmt_mknod,
2130 .realpath_fn = snapper_gmt_realpath,
2131 .get_nt_acl_fn = snapper_gmt_get_nt_acl,
2132 .fget_nt_acl_fn = snapper_gmt_fget_nt_acl,
2133 .mkdir_fn = snapper_gmt_mkdir,
2134 .rmdir_fn = snapper_gmt_rmdir,
2135 .getxattr_fn = snapper_gmt_getxattr,
2136 .listxattr_fn = snapper_gmt_listxattr,
2137 .removexattr_fn = snapper_gmt_removexattr,
2138 .setxattr_fn = snapper_gmt_setxattr,
2139 .chmod_acl_fn = snapper_gmt_chmod_acl,
2140 .chflags_fn = snapper_gmt_chflags,
2141 .get_real_filename_fn = snapper_gmt_get_real_filename,
2144 NTSTATUS vfs_snapper_init(void);
2145 NTSTATUS vfs_snapper_init(void)
2147 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2148 "snapper", &snapper_fns);