2 Unix SMB/CIFS implementation.
3 Tar backup command extension
4 Copyright (C) Aurélien Aptel 2013
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * # General overview of the tar extension
23 * All tar_xxx() functions work on a `struct tar` which store most of
24 * the context of the backup process.
26 * The current tar context can be accessed via the global variable
27 * `tar_ctx`. It's publicly exported as an opaque handle via
30 * A tar context is first configured through tar_parse_args() which
31 * can be called from either the CLI (in client.c) or the interactive
32 * session (via the cmd_tar() callback).
34 * Once the configuration is done (successfully), the context is ready
35 * for processing and tar_to_process() returns true.
37 * The next step is to call tar_process() which dispatch the
38 * processing to either tar_create() or tar_extract(), depending on
43 * tar_create() creates an archive using the libarchive API then
45 * - iterates on the requested paths if the context is in inclusion
46 * mode with tar_create_from_list()
48 * - or iterates on the whole share (starting from the current dir) if
49 * in exclusion mode or if no specific path were requested
51 * The do_list() function from client.c is used to list recursively
52 * the share. In particular it takes a DOS path mask (eg. \mydir\*)
53 * and a callback function which will be called with each file name
54 * and attributes. The tar callback function is get_file_callback().
56 * The callback function checks whether the file should be skipped
57 * according the the configuration via tar_create_skip_path(). If it's
58 * not skipped it's downloaded and written to the archive in
61 * ## Archive extraction
63 * tar_extract() opens the archive and iterates on each file in
64 * it. For each file tar_extract_skip_path() checks whether it should
65 * be skipped according to the config. If it's not skipped it's
66 * uploaded on the server in tar_send_file().
70 #include "system/filesys.h"
71 #include "client/client_proto.h"
72 #include "client/clitar_proto.h"
73 #include "libsmb/libsmb.h"
75 #ifdef HAVE_LIBARCHIVE
78 #include <archive_entry.h>
80 /* prepend module name and line number to debug messages */
81 #define DBG(a, b) (DEBUG(a, ("tar:%-4d ", __LINE__)), DEBUG(a, b))
83 /* preprocessor magic to stringify __LINE__ (int) */
85 #define STR2(x) STR1(x)
88 * Number of byte in a block unit.
90 #define TAR_BLOCK_UNIT 512
93 * Default tar block size in TAR_BLOCK_UNIT.
95 #define TAR_DEFAULT_BLOCK_SIZE 20
98 * Maximum value for the blocksize field
100 #define TAR_MAX_BLOCK_SIZE 0xffff
103 * Size of the buffer used when downloading a file
105 #define TAR_CLI_READ_SIZE 0xff00
107 #define TAR_DO_LIST_ATTR (FILE_ATTRIBUTE_DIRECTORY \
108 | FILE_ATTRIBUTE_SYSTEM \
109 | FILE_ATTRIBUTE_HIDDEN)
114 TAR_CREATE
, /* c flag */
115 TAR_EXTRACT
, /* x flag */
120 TAR_INCLUDE
, /* I and F flag, default */
121 TAR_EXCLUDE
, /* X flag */
130 TALLOC_CTX
*talloc_ctx
;
132 /* in state that needs/can be processed? */
137 enum tar_operation operation
; /* create, extract */
138 enum tar_selection selection
; /* include, exclude */
139 int blocksize
; /* size in TAR_BLOCK_UNIT of a tar file block */
140 bool hidden
; /* backup hidden file? */
141 bool system
; /* backup system file? */
142 bool incremental
; /* backup _only_ archived file? */
143 bool reset
; /* unset archive bit? */
144 bool dry
; /* don't write tar file? */
145 bool regex
; /* XXX: never actually using regex... */
146 bool verbose
; /* XXX: ignored */
149 /* nb of bytes received */
152 /* path to tar archive name */
155 /* list of path to include or exclude */
160 struct archive
*archive
;
164 * Global context imported in client.c when needed.
168 struct tar tar_ctx
= {
169 .mode
.selection
= TAR_INCLUDE
,
170 .mode
.blocksize
= TAR_DEFAULT_BLOCK_SIZE
,
173 .mode
.incremental
= false,
177 .mode
.verbose
= false,
180 /* tar, local function */
181 static int tar_create(struct tar
* t
);
182 static int tar_create_from_list(struct tar
*t
);
183 static int tar_extract(struct tar
*t
);
184 static int tar_read_inclusion_file(struct tar
*t
, const char* filename
);
185 static int tar_send_file(struct tar
*t
, struct archive_entry
*entry
);
186 static int tar_set_blocksize(struct tar
*t
, int size
);
187 static int tar_set_newer_than(struct tar
*t
, const char *filename
);
188 static NTSTATUS
tar_add_selection_path(struct tar
*t
, const char *path
);
189 static void tar_dump(struct tar
*t
);
190 static NTSTATUS
tar_extract_skip_path(struct tar
*t
,
191 struct archive_entry
*entry
,
193 static TALLOC_CTX
*tar_reset_mem_context(struct tar
*t
);
194 static void tar_free_mem_context(struct tar
*t
);
195 static NTSTATUS
tar_create_skip_path(struct tar
*t
,
196 const char *fullpath
,
197 const struct file_info
*finfo
,
200 static NTSTATUS
tar_path_in_list(struct tar
*t
, const char *path
,
201 bool reverse
, bool *_is_in_list
);
203 static int tar_get_file(struct tar
*t
,
204 const char *full_dos_path
,
205 struct file_info
*finfo
);
207 static NTSTATUS
get_file_callback(struct cli_state
*cli
,
208 struct file_info
*finfo
,
212 static char *fix_unix_path(char *path
, bool removeprefix
);
213 static NTSTATUS
path_base_name(TALLOC_CTX
*ctx
, const char *path
, char **_base
);
214 static const char* skip_useless_char_in_path(const char *p
);
215 static int make_remote_path(const char *full_path
);
216 static int max_token (const char *str
);
217 static NTSTATUS
is_subpath(const char *sub
, const char *full
,
218 bool *_subpath_match
);
219 static int set_remote_attr(const char *filename
, uint16 new_attr
, int mode
);
222 * tar_get_ctx - retrieve global tar context handle
224 struct tar
*tar_get_ctx()
230 * cmd_block - interactive command to change tar blocksize
232 * Read a size from the client command line and update the current
237 /* XXX: from client.c */
238 const extern char *cmd_ptr
;
242 TALLOC_CTX
*ctx
= talloc_new(NULL
);
247 ok
= next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
);
249 DBG(0, ("blocksize <n>\n"));
254 ok
= tar_set_blocksize(&tar_ctx
, atoi(buf
));
256 DBG(0, ("invalid blocksize\n"));
261 DBG(2, ("blocksize is now %d\n", tar_ctx
.mode
.blocksize
));
269 * cmd_tarmode - interactive command to change tar behaviour
271 * Read one or more modes from the client command line and update the
274 int cmd_tarmode(void)
276 const extern char *cmd_ptr
;
286 {"full", &tar_ctx
.mode
.incremental
, false},
287 {"inc", &tar_ctx
.mode
.incremental
, true },
288 {"reset", &tar_ctx
.mode
.reset
, true },
289 {"noreset", &tar_ctx
.mode
.reset
, false},
290 {"system", &tar_ctx
.mode
.system
, true },
291 {"nosystem", &tar_ctx
.mode
.system
, false},
292 {"hidden", &tar_ctx
.mode
.hidden
, true },
293 {"nohidden", &tar_ctx
.mode
.hidden
, false},
294 {"verbose", &tar_ctx
.mode
.verbose
, true },
295 {"noquiet", &tar_ctx
.mode
.verbose
, true },
296 {"quiet", &tar_ctx
.mode
.verbose
, false},
297 {"noverbose", &tar_ctx
.mode
.verbose
, false},
300 ctx
= talloc_new(NULL
);
305 while (next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
306 for (i
= 0; i
< ARRAY_SIZE(table
); i
++) {
307 if (strequal(table
[i
].cmd
, buf
)) {
308 *table
[i
].p
= table
[i
].value
;
313 if (i
== ARRAY_SIZE(table
))
314 DBG(0, ("tarmode: unrecognised option %s\n", buf
));
317 DBG(0, ("tarmode is now %s, %s, %s, %s, %s\n",
318 tar_ctx
.mode
.incremental
? "incremental" : "full",
319 tar_ctx
.mode
.system
? "system" : "nosystem",
320 tar_ctx
.mode
.hidden
? "hidden" : "nohidden",
321 tar_ctx
.mode
.reset
? "reset" : "noreset",
322 tar_ctx
.mode
.verbose
? "verbose" : "quiet"));
329 * cmd_tar - interactive command to start a tar backup/restoration
331 * Check presence of argument, parse them and handle the request.
335 const extern char *cmd_ptr
;
339 int maxtok
= max_token(cmd_ptr
);
344 TALLOC_CTX
*ctx
= talloc_new(NULL
);
349 ok
= next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
);
351 DBG(0, ("tar <c|x>[IXFbganN] [options] <tar file> [path list]\n"));
357 val
= talloc_array(ctx
, const char *, maxtok
);
363 while (next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
367 rc
= tar_parse_args(&tar_ctx
, flag
, val
, i
);
369 DBG(0, ("parse_args failed\n"));
374 rc
= tar_process(&tar_ctx
);
376 DBG(0, ("tar_process failed\n"));
387 * cmd_setmode - interactive command to set DOS attributes
389 * Read a filename and mode from the client command line and update
390 * the file DOS attributes.
392 int cmd_setmode(void)
394 const extern char *cmd_ptr
;
397 uint16 attr
[2] = {0};
401 TALLOC_CTX
*ctx
= talloc_new(NULL
);
406 ok
= next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
);
408 DBG(0, ("setmode <filename> <[+|-]rsha>\n"));
413 fname
= talloc_asprintf(ctx
,
415 client_get_cur_dir(),
422 while (next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
434 attr
[mode
] |= FILE_ATTRIBUTE_READONLY
;
437 attr
[mode
] |= FILE_ATTRIBUTE_HIDDEN
;
440 attr
[mode
] |= FILE_ATTRIBUTE_SYSTEM
;
443 attr
[mode
] |= FILE_ATTRIBUTE_ARCHIVE
;
446 DBG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
453 if (attr
[ATTR_SET
] == 0 && attr
[ATTR_UNSET
] == 0) {
454 DBG(0, ("setmode <filename> <[+|-]rsha>\n"));
459 DBG(2, ("perm set %d %d\n", attr
[ATTR_SET
], attr
[ATTR_UNSET
]));
461 /* ignore return value: server might not store DOS attributes */
462 set_remote_attr(fname
, attr
[ATTR_SET
], ATTR_SET
);
463 set_remote_attr(fname
, attr
[ATTR_UNSET
], ATTR_UNSET
);
470 * tar_parse_args - parse and set tar command line arguments
471 * @flag: string pointing to tar options
472 * @val: number of tar arguments
473 * @valsize: table of arguments after the flags (number of element in val)
475 * tar arguments work in a weird way. For each flag f that takes a
476 * value v, the user is supposed to type:
479 * -Tf1f2f3 v1 v2 v3 TARFILE PATHS...
481 * in the interactive session:
482 * tar f1f2f3 v1 v2 v3 TARFILE PATHS...
484 * @flag has only flags (eg. "f1f2f3") and @val has the arguments
485 * (values) following them (eg. ["v1", "v2", "v3", "TARFILE", "PATH1",
488 * There are only 2 flags that take an arg: b and N. The other flags
489 * just change the semantic of PATH or TARFILE.
491 * PATH can be a list of included/excluded paths, the path to a file
492 * containing a list of included/excluded paths to use (F flag). If no
493 * PATH is provided, the whole share is used (/).
495 int tar_parse_args(struct tar
* t
,
501 bool do_read_list
= false;
502 /* index of next value to use */
507 DBG(0, ("Invalid tar context\n"));
511 ctx
= tar_reset_mem_context(t
);
516 * Reset back some options - could be from interactive version
517 * all other modes are left as they are
519 t
->mode
.operation
= TAR_NO_OPERATION
;
520 t
->mode
.selection
= TAR_NO_SELECTION
;
522 t
->to_process
= false;
525 while (flag
[0] != '\0') {
529 if (t
->mode
.operation
!= TAR_NO_OPERATION
) {
530 printf("Tar must be followed by only one of c or x.\n");
533 t
->mode
.operation
= TAR_CREATE
;
536 if (t
->mode
.operation
!= TAR_NO_OPERATION
) {
537 printf("Tar must be followed by only one of c or x.\n");
540 t
->mode
.operation
= TAR_EXTRACT
;
545 if (t
->mode
.selection
!= TAR_NO_SELECTION
) {
546 DBG(0,("Only one of I,X,F must be specified\n"));
549 t
->mode
.selection
= TAR_INCLUDE
;
552 if (t
->mode
.selection
!= TAR_NO_SELECTION
) {
553 DBG(0,("Only one of I,X,F must be specified\n"));
556 t
->mode
.selection
= TAR_EXCLUDE
;
559 if (t
->mode
.selection
!= TAR_NO_SELECTION
) {
560 DBG(0,("Only one of I,X,F must be specified\n"));
563 t
->mode
.selection
= TAR_INCLUDE
;
569 if (ival
>= valsize
) {
570 DBG(0, ("Option b must be followed by a blocksize\n"));
574 if (tar_set_blocksize(t
, atoi(val
[ival
]))) {
575 DBG(0, ("Option b must be followed by a valid blocksize\n"));
582 /* incremental mode */
584 t
->mode
.incremental
= true;
589 if (ival
>= valsize
) {
590 DBG(0, ("Option N must be followed by valid file name\n"));
594 if (tar_set_newer_than(t
, val
[ival
])) {
595 DBG(0,("Error setting newer-than time\n"));
604 t
->mode
.reset
= true;
609 t
->mode
.verbose
= true;
614 t
->mode
.regex
= true;
619 if (t
->mode
.operation
!= TAR_CREATE
) {
620 DBG(0, ("n is only meaningful when creating a tar-file\n"));
625 DBG(0, ("dry_run set\n"));
629 DBG(0,("Unknown tar option\n"));
636 /* no selection given? default selection is include */
637 if (t
->mode
.selection
== TAR_NO_SELECTION
) {
638 t
->mode
.selection
= TAR_INCLUDE
;
641 if (valsize
- ival
< 1) {
642 DBG(0, ("No tar file given.\n"));
647 t
->tar_path
= talloc_strdup(ctx
, val
[ival
]);
648 if (t
->tar_path
== NULL
) {
654 * Make sure that dbf points to stderr if we are using stdout for
657 if (t
->mode
.operation
== TAR_CREATE
&& strequal(t
->tar_path
, "-")) {
658 setup_logging("smbclient", DEBUG_STDERR
);
661 /* handle PATHs... */
663 /* flag F -> read file list */
665 if (valsize
- ival
!= 1) {
666 DBG(0,("Option F must be followed by exactly one filename.\n"));
670 rc
= tar_read_inclusion_file(t
, val
[ival
]);
675 /* otherwise store all the PATHs on the command line */
678 for (i
= ival
; i
< valsize
; i
++) {
680 status
= tar_add_selection_path(t
, val
[i
]);
681 if (!NT_STATUS_IS_OK(status
)) {
687 t
->to_process
= true;
693 * tar_process - start processing archive
695 * The talloc context of the fields is freed at the end of the call.
697 int tar_process(struct tar
*t
)
702 DBG(0, ("Invalid tar context\n"));
706 switch(t
->mode
.operation
) {
714 DBG(0, ("Invalid tar state\n"));
718 t
->to_process
= false;
719 tar_free_mem_context(t
);
720 DBG(5, ("tar_process done, err = %d\n", rc
));
725 * tar_create - create archive and fetch files
727 static int tar_create(struct tar
* t
)
733 TALLOC_CTX
*ctx
= talloc_new(NULL
);
738 t
->archive
= archive_write_new();
741 const int bsize
= t
->mode
.blocksize
* TAR_BLOCK_UNIT
;
742 r
= archive_write_set_bytes_per_block(t
->archive
, bsize
);
743 if (r
!= ARCHIVE_OK
) {
744 DBG(0, ("Can't use a block size of %d bytes", bsize
));
750 * Use PAX restricted format which is not the most
751 * conservative choice but has useful extensions and is widely
754 r
= archive_write_set_format_pax_restricted(t
->archive
);
755 if (r
!= ARCHIVE_OK
) {
756 DBG(0, ("Can't use pax restricted format: %s\n",
757 archive_error_string(t
->archive
)));
762 if (strequal(t
->tar_path
, "-")) {
763 r
= archive_write_open_fd(t
->archive
, STDOUT_FILENO
);
765 r
= archive_write_open_filename(t
->archive
, t
->tar_path
);
768 if (r
!= ARCHIVE_OK
) {
769 DBG(0, ("Can't open %s: %s\n", t
->tar_path
,
770 archive_error_string(t
->archive
)));
777 * In inclusion mode, iterate on the inclusion list
779 if (t
->mode
.selection
== TAR_INCLUDE
&& t
->path_list_size
> 0) {
780 if (tar_create_from_list(t
)) {
785 mask
= talloc_asprintf(ctx
, "%s\\*", client_get_cur_dir());
790 DBG(5, ("tar_process do_list with mask: %s\n", mask
));
791 status
= do_list(mask
, TAR_DO_LIST_ATTR
, get_file_callback
, false, true);
792 if (!NT_STATUS_IS_OK(status
)) {
793 DBG(0, ("do_list fail %s\n", nt_errstr(status
)));
800 DBG(0, ("Total bytes received: %" PRIu64
"\n", t
->total_size
));
803 r
= archive_write_close(t
->archive
);
804 if (r
!= ARCHIVE_OK
) {
805 DBG(0, ("Fatal: %s\n", archive_error_string(t
->archive
)));
811 archive_write_free(t
->archive
);
817 * tar_create_from_list - fetch from path list in include mode
819 static int tar_create_from_list(struct tar
*t
)
824 const char *path
, *mask
, *start_dir
;
826 TALLOC_CTX
*ctx
= talloc_new(NULL
);
831 start_dir
= talloc_strdup(ctx
, client_get_cur_dir());
832 if (start_dir
== NULL
) {
837 for (i
= 0; i
< t
->path_list_size
; i
++) {
838 path
= t
->path_list
[i
];
840 status
= path_base_name(ctx
, path
, &base
);
841 if (!NT_STATUS_IS_OK(status
)) {
845 mask
= talloc_asprintf(ctx
, "%s\\%s",
846 client_get_cur_dir(), path
);
852 DBG(5, ("incl. path='%s', base='%s', mask='%s'\n",
853 path
, base
? base
: "NULL", mask
));
856 base
= talloc_asprintf(ctx
, "%s%s\\",
857 client_get_cur_dir(), base
);
862 DBG(5, ("cd '%s' before do_list\n", base
));
863 client_set_cur_dir(base
);
865 status
= do_list(mask
, TAR_DO_LIST_ATTR
, get_file_callback
, false, true);
867 client_set_cur_dir(start_dir
);
869 if (!NT_STATUS_IS_OK(status
)) {
870 DBG(0, ("do_list failed on %s (%s)\n", path
, nt_errstr(status
)));
882 * get_file_callback - do_list callback
884 * Callback for client.c do_list(). Called for each file found on the
885 * share matching do_list mask. Recursively call do_list() with itself
886 * as callback when the current file is a directory.
888 static NTSTATUS
get_file_callback(struct cli_state
*cli
,
889 struct file_info
*finfo
,
892 NTSTATUS status
= NT_STATUS_OK
;
894 const char *initial_dir
= client_get_cur_dir();
897 TALLOC_CTX
*ctx
= talloc_new(NULL
);
899 return NT_STATUS_NO_MEMORY
;
902 remote_name
= talloc_asprintf(ctx
, "%s%s", initial_dir
, finfo
->name
);
903 if (remote_name
== NULL
) {
904 status
= NT_STATUS_NO_MEMORY
;
908 if (strequal(finfo
->name
, "..") || strequal(finfo
->name
, ".")) {
912 status
= tar_create_skip_path(&tar_ctx
, remote_name
, finfo
, &skip
);
913 if (!NT_STATUS_IS_OK(status
)) {
918 DBG(5, ("--- %s\n", remote_name
));
919 status
= NT_STATUS_OK
;
923 if (finfo
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
928 old_dir
= talloc_strdup(ctx
, initial_dir
);
929 new_dir
= talloc_asprintf(ctx
, "%s%s\\",
930 initial_dir
, finfo
->name
);
931 if ((old_dir
== NULL
) || (new_dir
== NULL
)) {
932 status
= NT_STATUS_NO_MEMORY
;
935 mask
= talloc_asprintf(ctx
, "%s*", new_dir
);
937 status
= NT_STATUS_NO_MEMORY
;
941 rc
= tar_get_file(&tar_ctx
, remote_name
, finfo
);
943 status
= NT_STATUS_UNSUCCESSFUL
;
947 client_set_cur_dir(new_dir
);
948 do_list(mask
, TAR_DO_LIST_ATTR
, get_file_callback
, false, true);
949 client_set_cur_dir(old_dir
);
951 rc
= tar_get_file(&tar_ctx
, remote_name
, finfo
);
953 status
= NT_STATUS_UNSUCCESSFUL
;
964 * tar_get_file - fetch a remote file to the local archive
965 * @full_dos_path: path to the file to fetch
966 * @finfo: attributes of the file to fetch
968 static int tar_get_file(struct tar
*t
,
969 const char *full_dos_path
,
970 struct file_info
*finfo
)
972 extern struct cli_state
*cli
;
974 struct archive_entry
*entry
;
975 char *full_unix_path
;
976 char buf
[TAR_CLI_READ_SIZE
];
979 uint16_t remote_fd
= (uint16_t)-1;
981 const bool isdir
= finfo
->mode
& FILE_ATTRIBUTE_DIRECTORY
;
982 TALLOC_CTX
*ctx
= talloc_new(NULL
);
987 DBG(5, ("+++ %s\n", full_dos_path
));
989 t
->total_size
+= finfo
->size
;
996 /* ignore return value: server might not store DOS attributes */
997 set_remote_attr(full_dos_path
, FILE_ATTRIBUTE_ARCHIVE
, ATTR_UNSET
);
1000 full_unix_path
= talloc_asprintf(ctx
, ".%s", full_dos_path
);
1001 if (full_unix_path
== NULL
) {
1005 string_replace(full_unix_path
, '\\', '/');
1006 entry
= archive_entry_new();
1007 archive_entry_copy_pathname(entry
, full_unix_path
);
1008 archive_entry_set_filetype(entry
, isdir
? AE_IFDIR
: AE_IFREG
);
1009 archive_entry_set_atime(entry
,
1010 finfo
->atime_ts
.tv_sec
,
1011 finfo
->atime_ts
.tv_nsec
);
1012 archive_entry_set_mtime(entry
,
1013 finfo
->mtime_ts
.tv_sec
,
1014 finfo
->mtime_ts
.tv_nsec
);
1015 archive_entry_set_ctime(entry
,
1016 finfo
->ctime_ts
.tv_sec
,
1017 finfo
->ctime_ts
.tv_nsec
);
1018 archive_entry_set_perm(entry
, isdir
? 0755 : 0644);
1020 * check if we can safely cast unsigned file size to libarchive
1021 * signed size. Very unlikely problem (>9 exabyte file)
1023 if (finfo
->size
> INT64_MAX
) {
1024 DBG(0, ("Remote file %s too big\n", full_dos_path
));
1028 archive_entry_set_size(entry
, (int64_t)finfo
->size
);
1030 r
= archive_write_header(t
->archive
, entry
);
1031 if (r
!= ARCHIVE_OK
) {
1032 DBG(0, ("Fatal: %s\n", archive_error_string(t
->archive
)));
1038 DBG(5, ("get_file skip dir %s\n", full_dos_path
));
1042 status
= cli_open(cli
, full_dos_path
, O_RDONLY
, DENY_NONE
, &remote_fd
);
1043 if (!NT_STATUS_IS_OK(status
)) {
1044 DBG(0,("%s opening remote file %s\n",
1045 nt_errstr(status
), full_dos_path
));
1050 status
= cli_read(cli
, remote_fd
, buf
, off
, sizeof(buf
), &len
);
1051 if (!NT_STATUS_IS_OK(status
)) {
1052 DBG(0,("Error reading file %s : %s\n",
1053 full_dos_path
, nt_errstr(status
)));
1060 r
= archive_write_data(t
->archive
, buf
, len
);
1062 DBG(0, ("Fatal: %s\n", archive_error_string(t
->archive
)));
1067 } while (off
< finfo
->size
);
1070 cli_close(cli
, remote_fd
);
1073 archive_entry_free(entry
);
1081 * tar_extract - open archive and send files.
1083 static int tar_extract(struct tar
*t
)
1087 struct archive_entry
*entry
;
1088 const size_t bsize
= t
->mode
.blocksize
* TAR_BLOCK_UNIT
;
1091 t
->archive
= archive_read_new();
1092 archive_read_support_format_all(t
->archive
);
1093 archive_read_support_filter_all(t
->archive
);
1095 if (strequal(t
->tar_path
, "-")) {
1096 r
= archive_read_open_fd(t
->archive
, STDIN_FILENO
, bsize
);
1098 r
= archive_read_open_filename(t
->archive
, t
->tar_path
, bsize
);
1101 if (r
!= ARCHIVE_OK
) {
1102 DBG(0, ("Can't open %s : %s\n", t
->tar_path
,
1103 archive_error_string(t
->archive
)));
1111 r
= archive_read_next_header(t
->archive
, &entry
);
1112 if (r
== ARCHIVE_EOF
) {
1115 if (r
== ARCHIVE_WARN
) {
1116 DBG(0, ("Warning: %s\n", archive_error_string(t
->archive
)));
1118 if (r
== ARCHIVE_FATAL
) {
1119 DBG(0, ("Fatal: %s\n", archive_error_string(t
->archive
)));
1124 status
= tar_extract_skip_path(t
, entry
, &skip
);
1125 if (!NT_STATUS_IS_OK(status
)) {
1130 DBG(5, ("--- %s\n", archive_entry_pathname(entry
)));
1134 DBG(5, ("+++ %s\n", archive_entry_pathname(entry
)));
1136 rc
= tar_send_file(t
, entry
);
1144 r
= archive_read_free(t
->archive
);
1145 if (r
!= ARCHIVE_OK
) {
1146 DBG(0, ("Can't close %s : %s\n", t
->tar_path
,
1147 archive_error_string(t
->archive
)));
1154 * tar_send_file - send @entry to the remote server
1155 * @entry: current archive entry
1157 * Handle the creation of the parent directories and transfer the
1158 * entry to a new remote file.
1160 static int tar_send_file(struct tar
*t
, struct archive_entry
*entry
)
1162 extern struct cli_state
*cli
;
1166 uint16_t remote_fd
= (uint16_t) -1;
1168 int flags
= O_RDWR
| O_CREAT
| O_TRUNC
;
1169 mode_t mode
= archive_entry_filetype(entry
);
1171 TALLOC_CTX
*ctx
= talloc_new(NULL
);
1176 dos_path
= talloc_strdup(ctx
, archive_entry_pathname(entry
));
1177 if (dos_path
== NULL
) {
1181 fix_unix_path(dos_path
, true);
1183 full_path
= talloc_strdup(ctx
, client_get_cur_dir());
1184 if (full_path
== NULL
) {
1188 full_path
= talloc_strdup_append(full_path
, dos_path
);
1189 if (full_path
== NULL
) {
1194 if (mode
!= AE_IFREG
&& mode
!= AE_IFDIR
) {
1195 DBG(0, ("Skipping non-dir & non-regular file %s\n", full_path
));
1199 rc
= make_remote_path(full_path
);
1205 if (mode
== AE_IFDIR
) {
1209 status
= cli_open(cli
, full_path
, flags
, DENY_NONE
, &remote_fd
);
1210 if (!NT_STATUS_IS_OK(status
)) {
1211 DBG(0, ("Error opening remote file %s: %s\n",
1212 full_path
, nt_errstr(status
)));
1223 r
= archive_read_data_block(t
->archive
, &buf
, &len
, &off
);
1224 if (r
== ARCHIVE_EOF
) {
1227 if (r
== ARCHIVE_WARN
) {
1228 DBG(0, ("Warning: %s\n", archive_error_string(t
->archive
)));
1230 if (r
== ARCHIVE_FATAL
) {
1231 DBG(0, ("Fatal: %s\n", archive_error_string(t
->archive
)));
1236 status
= cli_writeall(cli
, remote_fd
, 0, buf
, off
, len
, NULL
);
1237 if (!NT_STATUS_IS_OK(status
)) {
1238 DBG(0, ("Error writing remote file %s: %s\n",
1239 full_path
, nt_errstr(status
)));
1246 status
= cli_close(cli
, remote_fd
);
1247 if (!NT_STATUS_IS_OK(status
)) {
1248 DBG(0, ("Error losing remote file %s: %s\n",
1249 full_path
, nt_errstr(status
)));
1259 * tar_add_selection_path - add a path to the path list
1260 * @path: path to add
1262 static NTSTATUS
tar_add_selection_path(struct tar
*t
, const char *path
)
1265 TALLOC_CTX
*ctx
= t
->talloc_ctx
;
1266 if (!t
->path_list
) {
1267 t
->path_list
= str_list_make_empty(ctx
);
1268 if (t
->path_list
== NULL
) {
1269 return NT_STATUS_NO_MEMORY
;
1271 t
->path_list_size
= 0;
1274 /* cast to silence gcc const-qual warning */
1275 list
= str_list_add((void *)t
->path_list
, path
);
1277 return NT_STATUS_NO_MEMORY
;
1279 t
->path_list
= discard_const_p(char *, list
);
1280 t
->path_list_size
++;
1281 fix_unix_path(t
->path_list
[t
->path_list_size
- 1], true);
1283 return NT_STATUS_OK
;
1287 * tar_set_blocksize - set block size in TAR_BLOCK_UNIT
1289 static int tar_set_blocksize(struct tar
*t
, int size
)
1291 if (size
<= 0 || size
> TAR_MAX_BLOCK_SIZE
) {
1295 t
->mode
.blocksize
= size
;
1301 * tar_set_newer_than - set date threshold of saved files
1302 * @filename: local path to a file
1304 * Only files newer than the modification time of @filename will be
1307 * Note: this function set the global variable newer_than from
1308 * client.c. Thus the time is not a field of the tar structure. See
1309 * cmd_newer() to change its value from an interactive session.
1311 static int tar_set_newer_than(struct tar
*t
, const char *filename
)
1313 extern time_t newer_than
;
1314 SMB_STRUCT_STAT stbuf
;
1317 rc
= sys_stat(filename
, &stbuf
, false);
1319 DBG(0, ("Error setting newer-than time\n"));
1323 newer_than
= convert_timespec_to_time_t(stbuf
.st_ex_mtime
);
1324 DBG(1, ("Getting files newer than %s\n", time_to_asc(newer_than
)));
1329 * tar_read_inclusion_file - set path list from file
1330 * @filename: path to the list file
1332 * Read and add each line of @filename to the path list.
1334 static int tar_read_inclusion_file(struct tar
*t
, const char* filename
)
1339 TALLOC_CTX
*ctx
= talloc_new(NULL
);
1344 fd
= open(filename
, O_RDONLY
);
1346 DBG(0, ("Can't open inclusion file '%s': %s\n", filename
, strerror(errno
)));
1351 for (line
= afdgets(fd
, ctx
, 0);
1353 line
= afdgets(fd
, ctx
, 0)) {
1355 status
= tar_add_selection_path(t
, line
);
1356 if (!NT_STATUS_IS_OK(status
)) {
1370 * tar_path_in_list - check whether @path is in the path list
1371 * @path: path to find
1372 * @reverse: when true also try to find path list element in @path
1373 * @_is_in_list: set if @path is in the path list
1375 * Look at each path of the path list and set @_is_in_list if @path is a
1376 * subpath of one of them.
1378 * If you want /path to be in the path list (path/a/, path/b/) set
1379 * @reverse to true to try to match the other way around.
1381 static NTSTATUS
tar_path_in_list(struct tar
*t
, const char *path
,
1382 bool reverse
, bool *_is_in_list
)
1386 const char *pattern
;
1388 if (path
== NULL
|| path
[0] == '\0') {
1389 *_is_in_list
= false;
1390 return NT_STATUS_OK
;
1393 p
= skip_useless_char_in_path(path
);
1395 for (i
= 0; i
< t
->path_list_size
; i
++) {
1399 pattern
= skip_useless_char_in_path(t
->path_list
[i
]);
1400 status
= is_subpath(p
, pattern
, &is_in_list
);
1401 if (!NT_STATUS_IS_OK(status
)) {
1404 if (reverse
&& !is_in_list
) {
1405 status
= is_subpath(pattern
, p
, &is_in_list
);
1406 if (!NT_STATUS_IS_OK(status
)) {
1411 *_is_in_list
= true;
1412 return NT_STATUS_OK
;
1416 *_is_in_list
= false;
1417 return NT_STATUS_OK
;
1421 * tar_extract_skip_path - check if @entry should be skipped
1422 * @entry: current tar entry
1423 * @_skip: set true if path should be skipped, otherwise false
1425 * Skip predicate for tar extraction (archive to server) only.
1427 static NTSTATUS
tar_extract_skip_path(struct tar
*t
,
1428 struct archive_entry
*entry
,
1431 const char *fullpath
= archive_entry_pathname(entry
);
1434 if (t
->path_list_size
<= 0) {
1436 return NT_STATUS_OK
;
1439 if (t
->mode
.regex
) {
1440 in
= mask_match_list(fullpath
, t
->path_list
, t
->path_list_size
, true);
1442 NTSTATUS status
= tar_path_in_list(t
, fullpath
, false, &in
);
1443 if (!NT_STATUS_IS_OK(status
)) {
1448 if (t
->mode
.selection
== TAR_EXCLUDE
) {
1454 return NT_STATUS_OK
;
1458 * tar_create_skip_path - check if @fullpath shoud be skipped
1459 * @fullpath: full remote path of the current file
1460 * @finfo: remote file attributes
1461 * @_skip: returned skip not
1463 * Skip predicate for tar creation (server to archive) only.
1465 static NTSTATUS
tar_create_skip_path(struct tar
*t
,
1466 const char *fullpath
,
1467 const struct file_info
*finfo
,
1470 /* syntaxic sugar */
1471 const mode_t mode
= finfo
->mode
;
1472 const bool isdir
= mode
& FILE_ATTRIBUTE_DIRECTORY
;
1473 const bool exclude
= t
->mode
.selection
== TAR_EXCLUDE
;
1478 /* 1. if we dont want X and we have X, skip */
1479 if (!t
->mode
.system
&& (mode
& FILE_ATTRIBUTE_SYSTEM
)) {
1481 return NT_STATUS_OK
;
1484 if (!t
->mode
.hidden
&& (mode
& FILE_ATTRIBUTE_HIDDEN
)) {
1486 return NT_STATUS_OK
;
1489 /* 2. if we only want archive and it's not, skip */
1491 if (t
->mode
.incremental
&& !(mode
& FILE_ATTRIBUTE_ARCHIVE
)) {
1493 return NT_STATUS_OK
;
1497 /* 3. is it in the selection list? */
1500 * tar_create_from_list() use the include list as a starting
1501 * point, no need to check
1505 return NT_STATUS_OK
;
1508 /* we are now in exclude mode */
1510 /* no matter the selection, no list => include everything */
1511 if (t
->path_list_size
<= 0) {
1513 return NT_STATUS_OK
;
1516 if (t
->mode
.regex
) {
1517 in
= mask_match_list(fullpath
, t
->path_list
, t
->path_list_size
, true);
1519 bool reverse
= isdir
&& !exclude
;
1520 NTSTATUS status
= tar_path_in_list(t
, fullpath
, reverse
, &in
);
1521 if (!NT_STATUS_IS_OK(status
)) {
1527 return NT_STATUS_OK
;
1531 * tar_to_process - return true if @t is ready to be processed
1533 * @t is ready if it properly parsed command line arguments.
1535 bool tar_to_process(struct tar
*t
)
1538 DBG(0, ("Invalid tar context\n"));
1541 return t
->to_process
;
1545 * skip_useless_char_in_path - skip leading slashes/dots
1547 * Skip leading slashes, backslashes and dot-slashes.
1549 static const char* skip_useless_char_in_path(const char *p
)
1552 if (*p
== '/' || *p
== '\\') {
1555 else if (p
[0] == '.' && (p
[1] == '/' || p
[1] == '\\')) {
1565 * is_subpath - check if the path @sub is a subpath of @full.
1566 * @sub: path to test
1567 * @full: container path
1568 * @_subpath_match: set true if @sub is a subpath of @full, otherwise false
1570 * String comparaison is case-insensitive.
1572 static NTSTATUS
is_subpath(const char *sub
, const char *full
,
1573 bool *_subpath_match
)
1575 NTSTATUS status
= NT_STATUS_OK
;
1578 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1579 if (tmp_ctx
== NULL
) {
1580 status
= NT_STATUS_NO_MEMORY
;
1584 f
= strlower_talloc(tmp_ctx
, full
);
1586 status
= NT_STATUS_NO_MEMORY
;
1589 string_replace(f
, '\\', '/');
1590 s
= strlower_talloc(tmp_ctx
, sub
);
1592 status
= NT_STATUS_NO_MEMORY
;
1595 string_replace(s
, '\\', '/');
1597 /* find the point where sub and full diverge */
1598 while ((*f
!= '\0') && (*s
!= '\0') && (*f
== *s
)) {
1604 if ((*f
== '\0') && (*s
== '\0')) {
1605 *_subpath_match
= true; /* sub and full match */
1609 if ((*f
== '\0') && (len
> 0) && (*(f
- 1) == '/')) {
1610 /* sub diverges from full at path separator */
1611 *_subpath_match
= true;
1615 if ((*s
== '\0') && (strcmp(f
, "/") == 0)) {
1616 /* full diverges from sub with trailing slash only */
1617 *_subpath_match
= true;
1621 if ((*s
== '/') && (*f
== '\0')) {
1622 /* sub diverges from full with extra path component */
1623 *_subpath_match
= true;
1626 *_subpath_match
= false;
1629 talloc_free(tmp_ctx
);
1635 * set_remote_attr - set DOS attributes of a remote file
1636 * @filename: path to the file name
1637 * @new_attr: attribute bit mask to use
1638 * @mode: one of ATTR_SET or ATTR_UNSET
1640 * Update the file attributes with the one provided.
1642 static int set_remote_attr(const char *filename
, uint16 new_attr
, int mode
)
1644 extern struct cli_state
*cli
;
1648 status
= cli_getatr(cli
, filename
, &old_attr
, NULL
, NULL
);
1649 if (!NT_STATUS_IS_OK(status
)) {
1650 DBG(0, ("cli_getatr failed: %s\n", nt_errstr(status
)));
1654 if (mode
== ATTR_SET
) {
1655 new_attr
|= old_attr
;
1657 new_attr
= old_attr
& ~new_attr
;
1660 status
= cli_setatr(cli
, filename
, new_attr
, 0);
1661 if (!NT_STATUS_IS_OK(status
)) {
1662 DBG(1, ("cli_setatr failed: %s\n", nt_errstr(status
)));
1671 * make_remote_path - recursively make remote dirs
1672 * @full_path: full hierarchy to create
1674 * Create @full_path and each parent directories as needed.
1676 static int make_remote_path(const char *full_path
)
1678 extern struct cli_state
*cli
;
1682 char *last_backslash
;
1687 TALLOC_CTX
*ctx
= talloc_new(NULL
);
1692 subpath
= talloc_strdup(ctx
, full_path
);
1693 if (subpath
== NULL
) {
1697 path
= talloc_strdup(ctx
, full_path
);
1702 len
= talloc_get_size(path
) - 1;
1704 last_backslash
= strrchr_m(path
, '\\');
1705 if (last_backslash
== NULL
) {
1709 *last_backslash
= 0;
1712 p
= strtok_r(path
, "\\", &state
);
1715 strlcat(subpath
, p
, len
);
1716 status
= cli_chkpath(cli
, subpath
);
1717 if (!NT_STATUS_IS_OK(status
)) {
1718 status
= cli_mkdir(cli
, subpath
);
1719 if (!NT_STATUS_IS_OK(status
)) {
1720 DBG(0, ("Can't mkdir %s: %s\n", subpath
, nt_errstr(status
)));
1724 DBG(3, ("mkdir %s\n", subpath
));
1727 strlcat(subpath
, "\\", len
);
1728 p
= strtok_r(NULL
, "/\\", &state
);
1738 * tar_reset_mem_context - reset talloc context associated with @t
1740 * At the start of the program the context is NULL so a new one is
1741 * allocated. On the following runs (interactive session only), simply
1742 * free the children.
1744 static TALLOC_CTX
*tar_reset_mem_context(struct tar
*t
)
1746 tar_free_mem_context(t
);
1747 t
->talloc_ctx
= talloc_new(NULL
);
1748 return t
->talloc_ctx
;
1752 * tar_free_mem_context - free talloc context associated with @t
1754 static void tar_free_mem_context(struct tar
*t
)
1756 if (t
->talloc_ctx
) {
1757 talloc_free(t
->talloc_ctx
);
1758 t
->talloc_ctx
= NULL
;
1759 t
->path_list_size
= 0;
1760 t
->path_list
= NULL
;
1765 #define XSET(v) [v] = #v
1766 #define XTABLE(v, t) DBG(2, ("DUMP:%-20.20s = %s\n", #v, t[v]))
1767 #define XBOOL(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v ? 1 : 0))
1768 #define XSTR(v) DBG(2, ("DUMP:%-20.20s = %s\n", #v, v ? v : "NULL"))
1769 #define XINT(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v))
1770 #define XUINT64(v) DBG(2, ("DUMP:%-20.20s = %" PRIu64 "\n", #v, v))
1773 * tar_dump - dump tar structure on stdout
1775 static void tar_dump(struct tar
*t
)
1778 const char* op
[] = {
1779 XSET(TAR_NO_OPERATION
),
1784 const char* sel
[] = {
1785 XSET(TAR_NO_SELECTION
),
1790 XBOOL(t
->to_process
);
1791 XTABLE(t
->mode
.operation
, op
);
1792 XTABLE(t
->mode
.selection
, sel
);
1793 XINT(t
->mode
.blocksize
);
1794 XBOOL(t
->mode
.hidden
);
1795 XBOOL(t
->mode
.system
);
1796 XBOOL(t
->mode
.incremental
);
1797 XBOOL(t
->mode
.reset
);
1799 XBOOL(t
->mode
.verbose
);
1800 XUINT64(t
->total_size
);
1802 XINT(t
->path_list_size
);
1804 for (i
= 0; t
->path_list
&& t
->path_list
[i
]; i
++) {
1805 DBG(2, ("DUMP: t->path_list[%2d] = %s\n", i
, t
->path_list
[i
]));
1808 DBG(2, ("DUMP:t->path_list @ %p (%d elem)\n", t
->path_list
, i
));
1817 * max_token - return upper limit for the number of token in @str
1819 * The result is not exact, the actual number of token might be less
1820 * than what is returned.
1822 static int max_token(const char *str
)
1832 while (s
[0] != '\0') {
1833 if (isspace((int)s
[0])) {
1845 * fix_unix_path - convert @path to a DOS path
1846 * @path: path to convert
1847 * @removeprefix: if true, remove leading ./ or /.
1849 static char *fix_unix_path(char *path
, bool do_remove_prefix
)
1851 char *from
= path
, *to
= path
;
1853 if (path
== NULL
|| path
[0] == '\0') {
1861 if (do_remove_prefix
) {
1863 if (path
[0] == '/' || path
[0] == '\\') {
1868 if (path
[1] != '\0' && path
[0] == '.' && (path
[1] == '/' || path
[1] == '\\')) {
1873 /* replace / with \ */
1874 while (from
[0] != '\0') {
1875 if (from
[0] == '/') {
1890 * path_base_name - return @path basename
1892 * If @path doesn't contain any directory separator return NULL.
1894 static NTSTATUS
path_base_name(TALLOC_CTX
*ctx
, const char *path
, char **_base
)
1900 for (i
= 0; path
[i
]; i
++) {
1901 if (path
[i
] == '\\' || path
[i
] == '/') {
1907 base
= talloc_strdup(ctx
, path
);
1909 return NT_STATUS_NO_MEMORY
;
1916 return NT_STATUS_OK
;
1921 #define NOT_IMPLEMENTED DEBUG(0, ("tar mode not compiled. build with --with-libarchive\n"))
1929 int cmd_tarmode(void)
1935 int cmd_setmode(void)
1947 int tar_process(struct tar
* tar
)
1953 int tar_parse_args(struct tar
*tar
, const char *flag
, const char **val
, int valsize
)
1959 bool tar_to_process(struct tar
*tar
)
1964 struct tar
*tar_get_ctx()