2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998, 2000 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: extract_list.c,v 1.117 2006/08/24 01:57:15 paddy_s Exp $
29 * implements the "extract" command in amrecover
34 #include "amrecover.h"
35 #include "fileheader.h"
43 #include "clientconf.h"
48 typedef struct EXTRACT_LIST_ITEM {
50 struct EXTRACT_LIST_ITEM *next;
54 typedef struct EXTRACT_LIST {
55 char *date; /* date tape created */
56 int level; /* level of dump */
57 char *tape; /* tape label */
58 off_t fileno; /* fileno on tape */
59 EXTRACT_LIST_ITEM *files; /* files to get off tape */
61 struct EXTRACT_LIST *next;
70 security_stream_t *fd;
71 } amidxtaped_streams[] = {
77 #define NSTREAMS (int)(sizeof(amidxtaped_streams) / sizeof(amidxtaped_streams[0]))
80 static void amidxtaped_response(void *, pkt_t *, security_handle_t *);
81 static void stop_amidxtaped(void);
82 char *amidxtaped_client_get_security_conf(char *, void *);
83 static char *dump_device_name = NULL;
85 static char *amidxtaped_line = NULL;
87 extern char *localhost;
89 /* global pid storage for interrupt handler */
90 pid_t extract_restore_child_pid = -1;
92 static EXTRACT_LIST *extract_list = NULL;
93 static const security_driver_t *amidxtaped_secdrv;
96 unsigned short samba_extract_method = SAMBA_TAR;
97 #endif /* SAMBA_CLIENT */
99 #define READ_TIMEOUT 240*60
101 EXTRACT_LIST *first_tape_list(void);
102 EXTRACT_LIST *next_tape_list(EXTRACT_LIST *list);
103 static int is_empty_dir(char *fname);
104 int is_extract_list_nonempty(void);
105 int length_of_tape_list(EXTRACT_LIST *tape_list);
106 void add_file(char *path, char *regex);
107 void add_glob(char *glob);
108 void add_regex(char *regex);
109 void clear_extract_list(void);
110 void clean_tape_list(EXTRACT_LIST *tape_list);
111 void clean_extract_list(void);
112 void check_file_overwrite(char *filename);
113 void delete_file(char *path, char *regex);
114 void delete_glob(char *glob);
115 void delete_regex(char *regex);
116 void delete_tape_list(EXTRACT_LIST *tape_list);
117 void display_extract_list(char *file);
118 void extract_files(void);
119 void read_file_header(char *buffer,
123 static int add_extract_item(DIR_ITEM *ditem);
124 static int delete_extract_item(DIR_ITEM *ditem);
125 static int extract_files_setup(char *label, off_t fsf);
126 static int okay_to_continue(int allow_tape,
129 static ssize_t read_buffer(int datafd,
133 static void clear_tape_list(EXTRACT_LIST *tape_list);
134 static void extract_files_child(int in_fd, EXTRACT_LIST *elist);
135 static void send_to_tape_server(security_stream_t *stream, char *cmd);
136 int writer_intermediary(EXTRACT_LIST *elist);
137 int get_amidxtaped_line(void);
138 static void read_amidxtaped_data(void *, void *, ssize_t);
141 * Function: ssize_t read_buffer(datafd, buffer, buflen, timeout_s)
144 * read data from input file desciptor waiting up to timeout_s
145 * seconds before returning data.
148 * datafd - File descriptor to read from.
149 * buffer - Buffer to read into.
150 * buflen - Maximum number of bytes to read into buffer.
151 * timeout_s - Seconds to wait before returning what was already read.
154 * >0 - Number of data bytes in buffer.
156 * -1 - errno == ETIMEDOUT if no data available in specified time.
157 * errno == ENFILE if datafd is invalid.
158 * otherwise errno is set by select or read..
170 struct timeval timeout;
175 if(datafd < 0 || datafd >= (int)FD_SETSIZE) {
176 errno = EMFILE; /* out of range */
181 spaceleft = (ssize_t)buflen;
185 FD_SET(datafd, &readset);
186 timeout.tv_sec = timeout_s;
188 nfound = select(datafd+1, &readset, NULL, NULL, &timeout);
190 /* Select returned an error. */
191 fprintf(stderr,"select error: %s\n", strerror(errno));
197 /* Select timed out. */
198 if (timeout_s != 0) {
199 /* Not polling: a real read timeout */
200 fprintf(stderr,"timeout waiting for restore\n");
201 fprintf(stderr,"increase READ_TIMEOUT in recover-src/extract_list.c if your tape is slow\n");
208 if(!FD_ISSET(datafd, &readset))
211 /* Select says data is available, so read it. */
212 size = read(datafd, dataptr, (size_t)spaceleft);
214 if ((errno == EINTR) || (errno == EAGAIN)) {
217 if (errno != EPIPE) {
218 fprintf(stderr, "read_buffer: read error - %s",
226 } while ((size > 0) && (spaceleft > 0));
228 return ((((ssize_t)buflen-spaceleft) > 0) ? ((ssize_t)buflen-spaceleft) : size);
233 first_tape_list(void)
240 /*@keep@*/EXTRACT_LIST *list)
249 EXTRACT_LIST * tape_list)
251 EXTRACT_LIST_ITEM *this, *next;
254 this = tape_list->files;
262 tape_list->files = NULL;
266 /* remove a tape list from the extract list, clearing the tape list
267 beforehand if necessary */
270 EXTRACT_LIST *tape_list)
272 EXTRACT_LIST *this, *prev;
274 if (tape_list == NULL)
277 /* is it first on the list? */
278 if (tape_list == extract_list)
280 extract_list = tape_list->next;
281 clear_tape_list(tape_list);
282 amfree(tape_list->date);
283 amfree(tape_list->tape);
288 /* so not first on list - find it and delete */
290 this = extract_list->next;
293 if (this == tape_list)
295 prev->next = tape_list->next;
296 clear_tape_list(tape_list);
297 amfree(tape_list->date);
298 amfree(tape_list->tape);
309 /* return the number of files on a tape's list */
312 EXTRACT_LIST *tape_list)
314 EXTRACT_LIST_ITEM *fn;
318 for (fn = tape_list->files; fn != NULL; fn = fn->next)
326 clear_extract_list(void)
328 while (extract_list != NULL)
329 delete_tape_list(extract_list);
335 EXTRACT_LIST *tape_list)
337 EXTRACT_LIST_ITEM *fn1, *pfn1, *ofn1;
338 EXTRACT_LIST_ITEM *fn2, *pfn2, *ofn2;
343 fn1 = tape_list->files;
344 while (fn1 != NULL) {
349 while (fn2 != NULL && remove_fn1 == 0) {
351 if(strcmp(fn1->path, fn2->path) == 0) {
353 } else if (strncmp(fn1->path, fn2->path, strlen(fn1->path)) == 0 &&
354 ((strlen(fn2->path) > strlen(fn1->path) &&
355 fn2->path[strlen(fn1->path)] == '/') ||
356 (fn1->path[strlen(fn1->path)-1] == '/'))) {
358 } else if (strncmp(fn2->path, fn1->path, strlen(fn2->path)) == 0 &&
359 ((strlen(fn1->path) > strlen(fn2->path) &&
360 fn1->path[strlen(fn2->path)] == '/') ||
361 (fn2->path[strlen(fn2->path)-1] == '/'))) {
367 dbprintf(("removing path %s, it is included in %s\n",
368 fn2->path, fn1->path));
374 } else if (remove_fn1 == 0) {
380 if(remove_fn1 != 0) {
381 /* fn2->path is always valid */
382 /*@i@*/ dbprintf(("removing path %s, it is included in %s\n",
383 /*@i@*/ fn1->path, fn2->path));
388 amfree(tape_list->files);
389 tape_list->files = fn1;
403 clean_extract_list(void)
407 for (this = extract_list; this != NULL; this = this->next)
408 clean_tape_list(this);
412 int add_to_unlink_list(char *path);
413 int do_unlink_list(void);
414 void free_unlink_list(void);
416 typedef struct s_unlink_list {
418 struct s_unlink_list *next;
420 t_unlink_list *unlink_list = NULL;
429 unlink_list = alloc(SIZEOF(*unlink_list));
430 unlink_list->path = stralloc(path);
431 unlink_list->next = NULL;
433 for (ul = unlink_list; ul != NULL; ul = ul->next) {
434 if (strcmp(ul->path, path) == 0)
437 ul = alloc(SIZEOF(*ul));
438 ul->path = stralloc(path);
439 ul->next = unlink_list;
451 for (ul = unlink_list; ul != NULL; ul = ul->next) {
452 if (unlink(ul->path) < 0) {
453 fprintf(stderr,"Can't unlink %s: %s\n", ul->path, strerror(errno));
462 free_unlink_list(void)
464 t_unlink_list *ul, *ul1;
466 for (ul = unlink_list; ul != NULL; ul = ul1) {
478 check_file_overwrite(
482 EXTRACT_LIST_ITEM *fn;
483 struct stat stat_buf;
487 for (this = extract_list; this != NULL; this = this->next) {
488 for (fn = this->files; fn != NULL ; fn = fn->next) {
490 /* Check path component of fn->path */
492 path = stralloc2(dir, fn->path);
493 if (path[strlen(path)-1] == '/') {
494 path[strlen(path)-1] = '\0';
497 s = path + strlen(dir) + 1;
498 while((s = strchr(s, '/'))) {
500 if (lstat(path, &stat_buf) == 0) {
501 if(!S_ISDIR(stat_buf.st_mode)) {
502 if (add_to_unlink_list(path)) {
503 printf("WARNING: %s is not a directory, "
504 "it will be deleted.\n",
509 else if (errno != ENOENT) {
510 printf("Can't stat %s: %s\n", path, strerror(errno));
519 filename = stralloc2(dir, fn->path);
520 if (filename[strlen(filename)-1] == '/') {
521 filename[strlen(filename)-1] = '\0';
524 if (lstat(filename, &stat_buf) == 0) {
525 if(S_ISDIR(stat_buf.st_mode)) {
526 if(!is_empty_dir(filename)) {
527 printf("WARNING: All existing files in %s "
528 "will be deleted.\n", filename);
530 } else if(S_ISREG(stat_buf.st_mode)) {
531 printf("WARNING: Existing file %s will be overwritten\n",
534 if (add_to_unlink_list(filename)) {
535 printf("WARNING: Existing entry %s will be deleted\n",
539 } else if (errno != ENOENT) {
540 printf("Can't stat %s: %s\n", filename, strerror(errno));
548 /* returns -1 if error */
549 /* returns 0 on succes */
550 /* returns 1 if already added */
555 EXTRACT_LIST *this, *this1;
556 EXTRACT_LIST_ITEM *that, *curr;
557 char *ditem_path = NULL;
559 ditem_path = stralloc(ditem->path);
560 clean_pathname(ditem_path);
562 for (this = extract_list; this != NULL; this = this->next)
564 /* see if this is the list for the tape */
565 if (this->level == ditem->level && strcmp(this->tape, ditem->tape) == 0)
567 /* yes, so add to list */
571 if (strcmp(curr->path,ditem_path) == 0) {
577 that = (EXTRACT_LIST_ITEM *)alloc(sizeof(EXTRACT_LIST_ITEM));
578 that->path = stralloc(ditem_path);
579 that->next = this->files;
580 this->files = that; /* add at front since easiest */
586 /* so this is the first time we have seen this tape */
587 this = (EXTRACT_LIST *)alloc(sizeof(EXTRACT_LIST));
588 this->tape = stralloc(ditem->tape);
589 this->level = ditem->level;
590 this->fileno = ditem->fileno;
591 this->date = stralloc(ditem->date);
592 that = (EXTRACT_LIST_ITEM *)alloc(sizeof(EXTRACT_LIST_ITEM));
593 that->path = stralloc(ditem_path);
597 /* add this in date increasing order */
598 /* because restore must be done in this order */
599 /* add at begining */
600 if(extract_list==NULL || strcmp(this->date,extract_list->date) < 0)
602 this->next = extract_list;
607 for (this1 = extract_list; this1->next != NULL; this1 = this1->next)
609 /* add in the middle */
610 if(strcmp(this->date,this1->next->date) < 0)
612 this->next = this1->next;
626 /* returns -1 if error */
627 /* returns 0 on deletion */
628 /* returns 1 if not there */
634 EXTRACT_LIST_ITEM *that, *prev;
635 char *ditem_path = NULL;
637 ditem_path = stralloc(ditem->path);
638 clean_pathname(ditem_path);
640 for (this = extract_list; this != NULL; this = this->next)
642 /* see if this is the list for the tape */
643 if (this->level == ditem->level && strcmp(this->tape, ditem->tape) == 0)
645 /* yes, so find file on list */
647 if (strcmp(that->path, ditem_path) == 0)
650 this->files = that->next;
653 /* if list empty delete it */
654 if (this->files == NULL)
655 delete_tape_list(this);
663 if (strcmp(that->path, ditem_path) == 0)
665 prev->next = that->next;
691 char *uqglob = unquote_string(glob);
693 regex = glob_to_regex(uqglob);
694 dbprintf(("add_glob (%s) -> %s\n", uqglob, regex));
695 if ((s = validate_regexp(regex)) != NULL) {
696 printf("%s is not a valid shell wildcard pattern: ", glob);
700 * glob_to_regex() anchors the beginning of the pattern with ^,
701 * but we will be tacking it onto the end of the current directory
702 * in add_file, so strip that off. Also, it anchors the end with
703 * $, but we need to match an optional trailing /, so tack that on
706 regex_path = stralloc(regex + 1);
707 regex_path[strlen(regex_path) - 1] = '\0';
708 strappend(regex_path, "[/]*$");
709 add_file(uqglob, regex_path);
721 char *uqregex = unquote_string(regex);
723 if ((s = validate_regexp(uqregex)) != NULL) {
724 printf("\"%s\" is not a valid regular expression: ", regex);
727 add_file(uqregex, regex);
737 DIR_ITEM *ditem, lditem;
738 char *path_on_disk = NULL;
739 char *path_on_disk_slash = NULL;
744 char *dir, *dir_undo, dir_undo_ch = '\0';
745 char *ditem_path = NULL;
748 char *s, *fp, *quoted;
753 if (disk_path == NULL) {
754 printf("Must select directory before adding files\n");
757 memset(&lditem, 0, sizeof(lditem)); /* Prevent use of bogus data... */
759 dbprintf(("add_file: Looking for \"%s\"\n", regex));
761 if(strcmp(regex, "/[/]*$") == 0) { /* "/" behave like "." */
764 else if(strcmp(regex, "[^/]*[/]*$") == 0) { /* "*" */
766 regex = "([^/.]|\\.[^/]+|[^/.][^/]*)[/]*$";
768 /* remove "/" at end of path */
769 j = (ssize_t)(strlen(regex) - 1);
770 while(j >= 0 && regex[j] == '/')
774 /* convert path (assumed in cwd) to one on disk */
775 if (strcmp(disk_path, "/") == 0) {
777 /* No mods needed if already starts with '/' */
778 path_on_disk = stralloc(regex);
781 path_on_disk = stralloc2("/", regex);
784 char *clean_disk_path = clean_regex(disk_path);
785 path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
786 amfree(clean_disk_path);
789 path_on_disk_slash = stralloc2(path_on_disk, "/");
791 dbprintf(("add_file: Converted path=\"%s\" to path_on_disk=\"%s\"\n",
792 regex, path_on_disk));
796 for (ditem=get_dir_list(); ditem!=NULL; ditem=get_next_dir_item(ditem))
799 quoted = quote_string(ditem->path);
800 dbprintf(("add_file: Pondering ditem->path=%s\n", quoted));
802 if (match(path_on_disk, ditem->path)
803 || match(path_on_disk_slash, ditem->path))
806 j = (ssize_t)strlen(ditem->path);
807 if((j > 0 && ditem->path[j-1] == '/')
808 || (j > 1 && ditem->path[j-2] == '/' && ditem->path[j-1] == '.'))
809 { /* It is a directory */
810 ditem_path = newstralloc(ditem_path, ditem->path);
811 clean_pathname(ditem_path);
813 cmd = stralloc2("ORLD ", ditem_path);
814 if(send_command(cmd) == -1) {
817 amfree(path_on_disk);
818 amfree(path_on_disk_slash);
824 if ((i = get_reply_line()) == -1) {
826 amfree(path_on_disk);
827 amfree(path_on_disk_slash);
830 if(i==0) { /* assume something wrong */
832 amfree(path_on_disk);
833 amfree(path_on_disk_slash);
840 lditem.path = newstralloc(lditem.path, ditem->path);
841 /* skip the last line -- duplicate of the preamble */
843 while ((i = get_reply_line()) != 0) {
846 amfree(path_on_disk);
847 amfree(path_on_disk_slash);
852 if(dir_undo) *dir_undo = dir_undo_ch;
854 cmd = stralloc(l); /* save for error report */
856 continue; /* throw the rest of the lines away */
859 if (!server_happy()) {
864 if(strncmp(l, sc, sizeof(sc)-1) != 0) {
865 err = "bad reply: not 201-";
869 s = l + sizeof(sc)-1;
872 skip_whitespace(s, ch);
874 err = "bad reply: missing date field";
878 skip_non_whitespace(s, ch);
880 lditem.date = newstralloc(lditem.date, fp);
883 skip_whitespace(s, ch);
884 if(ch == '\0' || sscanf(s - 1, "%d", &lditem.level) != 1) {
885 err = "bad reply: cannot parse level field";
890 skip_whitespace(s, ch);
892 err = "bad reply: missing tape field";
896 skip_non_whitespace(s, ch);
898 lditem.tape = newstralloc(lditem.tape, fp);
901 if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_ORLD)) {
902 skip_whitespace(s, ch);
903 if(ch == '\0' || sscanf(s - 1, OFF_T_FMT,
904 (OFF_T_FMT_TYPE *)&lditem.fileno) != 1) {
905 err = "bad reply: cannot parse fileno field";
911 skip_whitespace(s, ch);
913 err = "bad reply: missing directory field";
917 skip_quoted_string(s, ch);
919 dir_undo_ch = *dir_undo;
922 switch(add_extract_item(&lditem)) {
924 printf("System error\n");
925 dbprintf(("add_file: (Failed) System error\n"));
929 quoted = quote_string(lditem.path);
930 printf("Added dir %s at date %s\n",
931 quoted, lditem.date);
932 dbprintf(("add_file: (Successful) Added dir %s at date %s\n",
933 quoted, lditem.date));
942 if(!server_happy()) {
949 } else if(added == 0) {
950 quoted = quote_string(ditem_path);
951 printf("dir %s already added\n", quoted);
952 dbprintf(("add_file: dir %s already added\n", quoted));
956 else /* It is a file */
958 switch(add_extract_item(ditem)) {
960 printf("System error\n");
961 dbprintf(("add_file: (Failed) System error\n"));
965 quoted = quote_string(ditem->path);
966 printf("Added file %s\n", quoted);
967 dbprintf(("add_file: (Successful) Added %s\n", quoted));
972 quoted = quote_string(ditem->path);
973 printf("File %s already added\n", quoted);
974 dbprintf(("add_file: file %s already added\n", quoted));
983 amfree(path_on_disk);
984 amfree(path_on_disk_slash);
991 quoted = quote_string(path);
992 printf("File %s doesn't exist in directory\n", quoted);
993 dbprintf(("add_file: (Failed) File %s doesn't exist in directory\n",
1007 char *uqglob = unquote_string(glob);
1009 regex = glob_to_regex(uqglob);
1010 dbprintf(("delete_glob (%s) -> %s\n", uqglob, regex));
1011 if ((s = validate_regexp(regex)) != NULL) {
1012 printf("\"%s\" is not a valid shell wildcard pattern: ", glob);
1016 * glob_to_regex() anchors the beginning of the pattern with ^,
1017 * but we will be tacking it onto the end of the current directory
1018 * in add_file, so strip that off. Also, it anchors the end with
1019 * $, but we need to match an optional trailing /, so tack that on
1022 regex_path = stralloc(regex + 1);
1023 regex_path[strlen(regex_path) - 1] = '\0';
1024 strappend(regex_path, "[/]*$");
1025 delete_file(uqglob, regex_path);
1037 char *uqregex = unquote_string(regex);
1039 if ((s = validate_regexp(regex)) != NULL) {
1040 printf("\"%s\" is not a valid regular expression: ", regex);
1043 delete_file(uqregex, uqregex);
1053 DIR_ITEM *ditem, lditem;
1054 char *path_on_disk = NULL;
1055 char *path_on_disk_slash = NULL;
1061 char *tape, *tape_undo, tape_undo_ch = '\0';
1062 char *dir_undo, dir_undo_ch = '\0';
1065 char *ditem_path = NULL;
1073 if (disk_path == NULL) {
1074 printf("Must select directory before deleting files\n");
1077 memset(&lditem, 0, sizeof(lditem)); /* Prevent use of bogus data... */
1079 dbprintf(("delete_file: Looking for \"%s\"\n", path));
1081 if (strcmp(regex, "[^/]*[/]*$") == 0) {
1082 /* Looking for * find everything but single . */
1083 regex = "([^/.]|\\.[^/]+|[^/.][^/]*)[/]*$";
1085 /* remove "/" at end of path */
1086 j = (ssize_t)(strlen(regex) - 1);
1087 while(j >= 0 && regex[j] == '/') regex[j--] = '\0';
1090 /* convert path (assumed in cwd) to one on disk */
1091 if (strcmp(disk_path, "/") == 0) {
1092 if (*regex == '/') {
1093 if (strcmp(regex, "/[/]*$") == 0) {
1094 /* We want "/" to match the directory itself: "/." */
1095 path_on_disk = stralloc("/\\.[/]*$");
1097 /* No mods needed if already starts with '/' */
1098 path_on_disk = stralloc(regex);
1102 path_on_disk = stralloc2("/", regex);
1105 char *clean_disk_path = clean_regex(disk_path);
1106 path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
1107 amfree(clean_disk_path);
1110 path_on_disk_slash = stralloc2(path_on_disk, "/");
1112 dbprintf(("delete_file: Converted path=\"%s\" to path_on_disk=\"%s\"\n",
1113 regex, path_on_disk));
1115 for (ditem=get_dir_list(); ditem!=NULL; ditem=get_next_dir_item(ditem))
1117 quoted = quote_string(ditem->path);
1118 dbprintf(("delete_file: Pondering ditem->path=%s\n", quoted));
1120 if (match(path_on_disk, ditem->path)
1121 || match(path_on_disk_slash, ditem->path))
1124 j = (ssize_t)strlen(ditem->path);
1125 if((j > 0 && ditem->path[j-1] == '/')
1126 || (j > 1 && ditem->path[j-2] == '/' && ditem->path[j-1] == '.'))
1127 { /* It is a directory */
1128 ditem_path = newstralloc(ditem_path, ditem->path);
1129 clean_pathname(ditem_path);
1131 cmd = stralloc2("ORLD ", ditem_path);
1132 if(send_command(cmd) == -1) {
1135 amfree(path_on_disk);
1136 amfree(path_on_disk_slash);
1141 if ((i = get_reply_line()) == -1) {
1143 amfree(path_on_disk);
1144 amfree(path_on_disk_slash);
1147 if(i==0) /* assume something wrong */
1150 amfree(path_on_disk);
1151 amfree(path_on_disk_slash);
1157 lditem.path = newstralloc(lditem.path, ditem->path);
1159 tape_undo = dir_undo = NULL;
1160 /* skip the last line -- duplicate of the preamble */
1161 while ((i = get_reply_line()) != 0)
1165 amfree(path_on_disk);
1166 amfree(path_on_disk_slash);
1171 if(tape_undo) *tape_undo = tape_undo_ch;
1172 if(dir_undo) *dir_undo = dir_undo_ch;
1173 tape_undo = dir_undo = NULL;
1174 cmd = stralloc(l); /* save for the error report */
1176 continue; /* throw the rest of the lines away */
1179 if (!server_happy()) {
1184 if(strncmp(l, sc, sizeof(sc)-1) != 0) {
1185 err = "bad reply: not 201-";
1188 s = l + sizeof(sc)-1;
1191 skip_whitespace(s, ch);
1193 err = "bad reply: missing date field";
1197 skip_non_whitespace(s, ch);
1200 skip_whitespace(s, ch);
1201 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
1202 err = "bad reply: cannot parse level field";
1205 skip_integer(s, ch);
1207 skip_whitespace(s, ch);
1209 err = "bad reply: missing tape field";
1213 skip_non_whitespace(s, ch);
1215 tape_undo_ch = *tape_undo;
1218 if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_ORLD)) {
1219 skip_whitespace(s, ch);
1220 if(ch == '\0' || sscanf(s - 1, OFF_T_FMT,
1221 (OFF_T_FMT_TYPE *)&fileno) != 1) {
1222 err = "bad reply: cannot parse fileno field";
1225 skip_integer(s, ch);
1228 skip_whitespace(s, ch);
1230 err = "bad reply: missing directory field";
1233 skip_non_whitespace(s, ch);
1235 dir_undo_ch = *dir_undo;
1238 lditem.date = newstralloc(lditem.date, date);
1240 lditem.tape = newstralloc(lditem.tape, tape);
1241 switch(delete_extract_item(&lditem)) {
1243 printf("System error\n");
1244 dbprintf(("delete_file: (Failed) System error\n"));
1247 printf("Deleted dir %s at date %s\n", ditem_path, date);
1248 dbprintf(("delete_file: (Successful) Deleted dir %s at date %s\n",
1256 if(!server_happy()) {
1263 } else if(deleted == 0) {
1264 printf("Warning - dir '%s' not on tape list\n",
1266 dbprintf(("delete_file: dir '%s' not on tape list\n",
1272 switch(delete_extract_item(ditem)) {
1274 printf("System error\n");
1275 dbprintf(("delete_file: (Failed) System error\n"));
1278 printf("Deleted %s\n", ditem->path);
1279 dbprintf(("delete_file: (Successful) Deleted %s\n",
1283 printf("Warning - file '%s' not on tape list\n",
1285 dbprintf(("delete_file: file '%s' not on tape list\n",
1294 amfree(path_on_disk);
1295 amfree(path_on_disk_slash);
1298 printf("File %s doesn't exist in directory\n", path);
1299 dbprintf(("delete_file: (Failed) File %s doesn't exist in directory\n",
1305 /* print extract list into file. If NULL ptr passed print to screen */
1307 display_extract_list(
1311 EXTRACT_LIST_ITEM *that;
1314 char *pager_command;
1319 if ((pager = getenv("PAGER")) == NULL)
1324 * Set up the pager command so if the pager is terminated, we do
1325 * not get a SIGPIPE back.
1327 pager_command = stralloc2(pager, " ; /bin/cat > /dev/null");
1328 if ((fp = popen(pager_command, "w")) == NULL)
1330 printf("Warning - can't pipe through %s\n", pager);
1333 amfree(pager_command);
1337 uqfile = unquote_string(file);
1338 if ((fp = fopen(uqfile, "w")) == NULL)
1340 printf("Can't open file %s to print extract list into\n", file);
1347 for (this = extract_list; this != NULL; this = this->next)
1349 fprintf(fp, "TAPE %s LEVEL %d DATE %s\n",
1350 this->tape, this->level, this->date);
1351 for (that = this->files; that != NULL; that = that->next)
1352 fprintf(fp, "\t%s\n", that->path);
1358 printf("Extract list written to file %s\n", file);
1369 struct dirent *entry;
1372 if((dir = opendir(fname)) == NULL)
1376 while(!gotentry && (entry = readdir(dir)) != NULL) {
1377 gotentry = !is_dot_or_dotdot(entry->d_name);
1385 /* returns 0 if extract list empty and 1 if it isn't */
1387 is_extract_list_nonempty(void)
1389 return (extract_list != NULL);
1393 /* prints continue prompt and waits for response,
1394 returns 0 if don't, non-0 if do */
1411 prompt = "New tape device [?]: ";
1412 } else if (allow_tape && allow_skip) {
1413 prompt = "Continue [?/Y/n/s/t]? ";
1414 } else if (allow_tape && !allow_skip) {
1415 prompt = "Continue [?/Y/n/t]? ";
1416 } else if (allow_retry) {
1417 prompt = "Continue [?/Y/n/r]? ";
1419 prompt = "Continue [?/Y/n]? ";
1421 fputs(prompt, stdout);
1422 fflush(stdout); fflush(stderr);
1424 if ((line = agets(stdin)) == NULL) {
1435 while ((ch = *s++) != '\0' && isspace(ch)) {
1436 (void)ch; /* Quiet empty loop compiler warning */
1440 printf("Enter a new device ([host:]device) or \"default\"\n");
1442 printf("Enter \"y\"es to continue, \"n\"o to stop");
1444 printf(", \"s\"kip this tape");
1447 printf(" or \"r\"etry this tape");
1450 printf(" or \"t\"ape to change tape drives");
1454 } else if (get_tape) {
1457 } else if (ch == '\0' || ch == 'Y' || ch == 'y') {
1459 } else if (allow_tape && (ch == 'T' || ch == 't')) {
1461 } else if (ch == 'N' || ch == 'n') {
1463 } else if (allow_retry && (ch == 'R' || ch == 'r')) {
1465 } else if (allow_skip && (ch == 'S' || ch == 's')) {
1476 send_to_tape_server(
1477 security_stream_t * stream,
1480 char *msg = stralloc2(cmd, "\r\n");
1482 if (security_stream_write(stream, msg, strlen(msg)) < 0)
1484 error("Error writing to tape server");
1492 /* start up connection to tape server and set commands to initiate
1493 transfer of dump image.
1494 Return tape server socket on success, -1 on error. */
1496 extract_files_setup(
1500 char *disk_regex = NULL;
1501 char *host_regex = NULL;
1502 char *clean_datestamp, *ch, *ch1;
1507 amidxtaped_secdrv = security_getdriver(authopt);
1508 if (amidxtaped_secdrv == NULL) {
1509 error("no '%s' security driver available for host '%s'",
1510 authopt, tape_server_name);
1513 /* We assume that amidxtaped support fe_amidxtaped_options_features */
1514 /* and fe_amidxtaped_options_auth */
1515 /* We should send a noop to really know */
1516 req = vstralloc("SERVICE amidxtaped\n",
1517 "OPTIONS ", "features=", our_features_string, ";",
1518 "auth=", authopt, ";",
1520 protocol_sendreq(tape_server_name, amidxtaped_secdrv,
1521 amidxtaped_client_get_security_conf, req, STARTUP_TIMEOUT,
1522 amidxtaped_response, &response_error);
1525 if(response_error != 0) {
1529 disk_regex = alloc(strlen(disk_name) * 2 + 3);
1534 /* we want to force amrestore to only match disk_name exactly */
1537 /* We need to escape some characters first... NT compatibilty crap */
1538 for (; *ch != 0; ch++, ch1++) {
1539 switch (*ch) { /* done this way in case there are more */
1542 /* no break; we do want to fall through... */
1548 /* we want to force amrestore to only match disk_name exactly */
1553 host_regex = alloc(strlen(dump_hostname) * 2 + 3);
1558 /* we want to force amrestore to only match dump_hostname exactly */
1561 /* We need to escape some characters first... NT compatibilty crap */
1562 for (; *ch != 0; ch++, ch1++) {
1563 switch (*ch) { /* done this way in case there are more */
1566 /* no break; we do want to fall through... */
1572 /* we want to force amrestore to only match dump_hostname exactly */
1577 clean_datestamp = stralloc(dump_datestamp);
1578 for(ch=ch1=clean_datestamp;*ch1 != '\0';ch1++) {
1585 /* push our feature list off to the tape server */
1586 /* XXX assumes that index server and tape server are equivalent, ew */
1588 if(am_has_feature(indexsrv_features, fe_amidxtaped_exchange_features)){
1589 tt = newstralloc2(tt, "FEATURES=", our_features_string);
1590 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1591 get_amidxtaped_line();
1592 if(strncmp(amidxtaped_line,"FEATURES=",9) == 0) {
1593 tapesrv_features = am_string_to_feature(amidxtaped_line+9);
1595 fprintf(stderr, "amrecover - expecting FEATURES line from amidxtaped\n");
1599 amfree(clean_datestamp);
1602 am_release_feature_set(tapesrv_features);
1606 if(am_has_feature(indexsrv_features, fe_amidxtaped_header) &&
1607 am_has_feature(indexsrv_features, fe_amidxtaped_device) &&
1608 am_has_feature(indexsrv_features, fe_amidxtaped_host) &&
1609 am_has_feature(indexsrv_features, fe_amidxtaped_disk) &&
1610 am_has_feature(indexsrv_features, fe_amidxtaped_datestamp)) {
1612 if(am_has_feature(indexsrv_features, fe_amidxtaped_config)) {
1613 tt = newstralloc2(tt, "CONFIG=", config);
1614 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1616 if(am_has_feature(indexsrv_features, fe_amidxtaped_label) &&
1617 label && label[0] != '/') {
1618 tt = newstralloc2(tt,"LABEL=",label);
1619 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1621 if(am_has_feature(indexsrv_features, fe_amidxtaped_fsf)) {
1623 snprintf(v_fsf, 99, OFF_T_FMT, (OFF_T_FMT_TYPE)fsf);
1624 tt = newstralloc2(tt, "FSF=",v_fsf);
1625 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1627 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "HEADER");
1628 tt = newstralloc2(tt, "DEVICE=", dump_device_name);
1629 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1630 tt = newstralloc2(tt, "HOST=", host_regex);
1631 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1632 tt = newstralloc2(tt, "DISK=", disk_regex);
1633 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1634 tt = newstralloc2(tt, "DATESTAMP=", clean_datestamp);
1635 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1636 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "END");
1639 else if(am_has_feature(indexsrv_features, fe_amidxtaped_nargs)) {
1640 /* send to the tape server what tape file we want */
1649 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "6");
1650 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "-h");
1651 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "-p");
1652 send_to_tape_server(amidxtaped_streams[CTLFD].fd, dump_device_name);
1653 send_to_tape_server(amidxtaped_streams[CTLFD].fd, host_regex);
1654 send_to_tape_server(amidxtaped_streams[CTLFD].fd, disk_regex);
1655 send_to_tape_server(amidxtaped_streams[CTLFD].fd, clean_datestamp);
1657 dbprintf(("Started amidxtaped with arguments \"6 -h -p %s %s %s %s\"\n",
1658 dump_device_name, host_regex, disk_regex, clean_datestamp));
1663 amfree(clean_datestamp);
1670 * Reads the first block of a tape file.
1681 bytes_read = read_buffer(tapedev, buffer, buflen, READ_TIMEOUT);
1682 if(bytes_read < 0) {
1683 error("error reading header (%s), check amidxtaped.*.debug on server",
1688 if((size_t)bytes_read < buflen) {
1689 fprintf(stderr, "%s: short block %d byte%s\n",
1690 get_pname(), (int)bytes_read, (bytes_read == 1) ? "" : "s");
1691 print_header(stdout, file);
1692 error("Can't read file header");
1696 /* bytes_read == buflen */
1697 parse_file_header(buffer, file, (size_t)bytes_read);
1710 extract_files_child(
1712 EXTRACT_LIST * elist)
1715 int extra_params = 0;
1717 char **restore_args = NULL;
1719 EXTRACT_LIST_ITEM *fn;
1720 enum dumptypes dumptype = IS_UNKNOWN;
1721 char buffer[DISK_BLOCK_BYTES];
1725 int passwd_field = -1;
1727 char *domain = NULL, *smbpass = NULL;
1730 /* code executed by child to do extraction */
1733 /* make in_fd be our stdin */
1734 if (dup2(in_fd, STDIN_FILENO) == -1)
1736 error("dup2 failed in extract_files_child: %s", strerror(errno));
1740 /* read the file header */
1742 read_file_header(buffer, &file, sizeof(buffer), STDIN_FILENO);
1744 if(file.type != F_DUMPFILE) {
1745 print_header(stdout, &file);
1746 error("bad header");
1750 if (file.program != NULL) {
1752 if (strcmp(file.program, GNUTAR) == 0)
1753 dumptype = IS_GNUTAR;
1756 if (dumptype == IS_UNKNOWN) {
1757 len_program = strlen(file.program);
1758 if(len_program >= 3 &&
1759 strcmp(&file.program[len_program-3],"tar") == 0)
1764 if (dumptype == IS_UNKNOWN && strcmp(file.program, SAMBA_CLIENT) ==0) {
1765 if (samba_extract_method == SAMBA_TAR)
1766 dumptype = IS_SAMBA_TAR;
1768 dumptype = IS_SAMBA;
1773 /* form the arguments to restore */
1774 files_off_tape = length_of_tape_list(elist);
1793 #if defined(XFSDUMP)
1794 if (strcmp(file.program, XFSDUMP) == 0) {
1795 extra_params = 4 + files_off_tape;
1805 restore_args = (char **)alloc((size_t)((extra_params + files_off_tape + 1)
1810 restore_args[j++] = stralloc("smbclient");
1811 smbpass = findpass(file.disk, &domain);
1813 restore_args[j++] = stralloc(file.disk);
1815 restore_args[j++] = stralloc("-U");
1816 restore_args[j++] = smbpass;
1818 restore_args[j++] = stralloc("-W");
1819 restore_args[j++] = stralloc(domain);
1824 restore_args[j++] = stralloc("-d0");
1825 restore_args[j++] = stralloc("-Tx");
1826 restore_args[j++] = stralloc("-"); /* data on stdin */
1831 restore_args[j++] = stralloc("tar");
1832 restore_args[j++] = stralloc("--numeric-owner");
1833 restore_args[j++] = stralloc("-xpGvf");
1834 restore_args[j++] = stralloc("-"); /* data on stdin */
1837 restore_args[j++] = stralloc("tar");
1838 restore_args[j++] = stralloc("-xpvf");
1839 restore_args[j++] = stralloc("-"); /* data on stdin */
1843 restore_args[j++] = stralloc("restore");
1845 restore_args[j++] = stralloc("-xB");
1847 #if defined(XFSDUMP)
1848 if (strcmp(file.program, XFSDUMP) == 0) {
1849 restore_args[j++] = stralloc("-v");
1850 restore_args[j++] = stralloc("silent");
1854 if (strcmp(file.program, VDUMP) == 0) {
1855 restore_args[j++] = stralloc("xf");
1856 restore_args[j++] = stralloc("-"); /* data on stdin */
1860 restore_args[j++] = stralloc("xbf");
1861 restore_args[j++] = stralloc("2"); /* read in units of 1K */
1862 restore_args[j++] = stralloc("-"); /* data on stdin */
1867 for (i = 0, fn = elist->files; i < files_off_tape; i++, fn = fn->next)
1874 if (strcmp(fn->path, "/") == 0)
1875 restore_args[j++] = stralloc(".");
1877 restore_args[j++] = stralloc2(".", fn->path);
1881 #if defined(XFSDUMP)
1882 if (strcmp(file.program, XFSDUMP) == 0) {
1884 * xfsrestore needs a -s option before each file to be
1885 * restored, and also wants them to be relative paths.
1887 restore_args[j++] = stralloc("-s");
1888 restore_args[j++] = stralloc(fn->path + 1);
1892 restore_args[j++] = stralloc(fn->path);
1896 #if defined(XFSDUMP)
1897 if (strcmp(file.program, XFSDUMP) == 0) {
1898 restore_args[j++] = stralloc("-");
1899 restore_args[j++] = stralloc(".");
1902 restore_args[j] = NULL;
1907 cmd = stralloc(SAMBA_CLIENT);
1910 /* fall through to ... */
1916 fprintf(stderr, "warning: GNUTAR program not available.\n");
1917 cmd = stralloc("tar");
1919 cmd = stralloc(GNUTAR);
1926 if (strcmp(file.program, DUMP) == 0) {
1927 cmd = stralloc(RESTORE);
1931 if (strcmp(file.program, VDUMP) == 0) {
1932 cmd = stralloc(VRESTORE);
1936 if (strcmp(file.program, VXDUMP) == 0) {
1937 cmd = stralloc(VXRESTORE);
1940 #if defined(XFSDUMP)
1941 if (strcmp(file.program, XFSDUMP) == 0) {
1942 cmd = stralloc(XFSRESTORE);
1946 fprintf(stderr, "warning: restore program for %s not available.\n",
1948 cmd = stralloc("restore");
1952 dbprintf(("Exec'ing %s with arguments:\n", cmd));
1953 for (i = 0; i < j; i++) {
1954 if( i == passwd_field)
1955 dbprintf(("\tXXXXX\n"));
1957 dbprintf(("\t%s\n", restore_args[i]));
1959 (void)execv(cmd, restore_args);
1960 /* only get here if exec failed */
1962 for (i = 0; i < j; i++) {
1963 amfree(restore_args[i]);
1965 amfree(restore_args);
1967 perror("amrecover couldn't exec");
1968 fprintf(stderr, " problem executing %s\n", cmd);
1976 * Interpose something between the process writing out the dump (writing it to
1977 * some extraction program, really) and the socket from which we're reading, so
1978 * that we can do things like prompt for human interaction for multiple tapes.
1981 writer_intermediary(
1982 EXTRACT_LIST * elist)
1986 amwait_t extractor_status;
1988 if(pipe(child_pipe) == -1) {
1989 error("extract_list - error setting up pipe to extractor: %s\n",
1994 /* okay, ready to extract. fork a child to do the actual work */
1995 if ((pid = fork()) == 0) {
1996 /* this is the child process */
1997 /* never gets out of this clause */
1998 aclose(child_pipe[1]);
1999 extract_files_child(child_pipe[0], elist);
2003 /* This is the parent */
2005 printf("writer_intermediary - error forking child");
2009 aclose(child_pipe[0]);
2011 security_stream_read(amidxtaped_streams[DATAFD].fd,
2012 read_amidxtaped_data, &(child_pipe[1]));
2014 while(get_amidxtaped_line() >= 0) {
2015 char desired_tape[MAX_TAPE_LABEL_BUF];
2017 /* if prompted for a tape, relay said prompt to the user */
2018 if(sscanf(amidxtaped_line, "FEEDME %132s\n", desired_tape) == 1) {
2020 printf("Load tape %s now\n", desired_tape);
2021 done = okay_to_continue(am_has_feature(indexsrv_features,
2022 fe_amrecover_feedme_tape),
2025 if (am_has_feature(indexsrv_features,
2026 fe_amrecover_feedme_tape)) {
2027 char *reply = stralloc2("TAPE ", tape_device_name);
2028 send_to_tape_server(amidxtaped_streams[CTLFD].fd, reply);
2031 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "OK");
2034 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "ERROR");
2037 } else if(strncmp(amidxtaped_line, "MESSAGE ", 8) == 0) {
2038 printf("%s\n",&amidxtaped_line[8]);
2040 fprintf(stderr, "Strange message from tape server: %s",
2046 /* CTL might be close before DATA */
2048 aclose(child_pipe[1]);
2050 waitpid(pid, &extractor_status, 0);
2051 if(WEXITSTATUS(extractor_status) != 0){
2052 int ret = WEXITSTATUS(extractor_status);
2053 if(ret == 255) ret = -1;
2054 printf("Extractor child exited with status %d\n", ret);
2060 /* exec restore to do the actual restoration */
2062 /* does the actual extraction of files */
2064 * The original design had the dump image being returned exactly as it
2065 * appears on the tape, and this routine getting from the index server
2066 * whether or not it is compressed, on the assumption that the tape
2067 * server may not know how to uncompress it. But
2068 * - Amrestore can't do that. It returns either compressed or uncompressed
2069 * (always). Amrestore assumes it can uncompress files. It is thus a good
2070 * idea to run the tape server on a machine with gzip.
2071 * - The information about compression in the disklist is really only
2072 * for future dumps. It is possible to change compression on a drive
2073 * so the information in the disklist may not necessarily relate to
2074 * the dump image on the tape.
2075 * Consequently the design was changed to assuming that amrestore can
2076 * uncompress any dump image and have it return an uncompressed file
2082 EXTRACT_LIST *elist;
2087 tapelist_t *tlist = NULL, *a_tlist;
2089 if (!is_extract_list_nonempty())
2091 printf("Extract list empty - No files to extract!\n");
2095 clean_extract_list();
2097 /* get tape device name from index server if none specified */
2098 if (tape_server_name == NULL) {
2099 tape_server_name = newstralloc(tape_server_name, server_name);
2101 if (tape_device_name == NULL) {
2102 if (send_command("TAPE") == -1)
2104 if (get_reply_line() == -1)
2107 if (!server_happy())
2112 /* skip reply number */
2113 tape_device_name = newstralloc(tape_device_name, l+4);
2116 if (strcmp(tape_device_name, "/dev/null") == 0)
2118 printf("amrecover: warning: using %s as the tape device will not work\n",
2123 for (elist = first_tape_list(); elist != NULL; elist = next_tape_list(elist)) {
2124 if(elist->tape[0]!='/') {
2126 printf("\nExtracting files using tape drive %s on host %s.\n",
2127 tape_device_name, tape_server_name);
2128 printf("The following tapes are needed:");
2133 tlist = unmarshal_tapelist_str(elist->tape);
2134 for(a_tlist = tlist ; a_tlist != NULL; a_tlist = a_tlist->next)
2135 printf(" %s", a_tlist->label);
2137 free_tapelist(tlist);
2141 for (elist = first_tape_list(); elist != NULL; elist = next_tape_list(elist)) {
2142 if(elist->tape[0]=='/') {
2144 printf("\nExtracting files from holding disk on host %s.\n",
2146 printf("The following files are needed:");
2151 tlist = unmarshal_tapelist_str(elist->tape);
2152 for(a_tlist = tlist; a_tlist != NULL; a_tlist = a_tlist->next)
2153 printf(" %s", a_tlist->label);
2155 free_tapelist(tlist);
2160 if (getcwd(cwd, sizeof(cwd)) == NULL) {
2161 perror("extract_list: Current working directory unavailable");
2165 printf("Restoring files into directory %s\n", cwd);
2166 check_file_overwrite(cwd);
2169 if (samba_extract_method == SAMBA_SMBCLIENT)
2170 printf("(unless it is a Samba backup, that will go through to the SMB server)\n");
2172 if (!okay_to_continue(0,0,0))
2176 if (!do_unlink_list()) {
2177 fprintf(stderr, "Can't recover because I can't cleanup the cwd (%s)\n",
2183 while ((elist = first_tape_list()) != NULL)
2185 if(elist->tape[0]=='/') {
2186 dump_device_name = newstralloc(dump_device_name, elist->tape);
2187 printf("Extracting from file ");
2188 tlist = unmarshal_tapelist_str(dump_device_name);
2189 for(a_tlist = tlist; a_tlist != NULL; a_tlist = a_tlist->next)
2190 printf(" %s", a_tlist->label);
2192 free_tapelist(tlist);
2195 printf("Extracting files using tape drive %s on host %s.\n",
2196 tape_device_name, tape_server_name);
2197 tlist = unmarshal_tapelist_str(elist->tape);
2198 printf("Load tape %s now\n", tlist->label);
2199 free_tapelist(tlist);
2200 otc = okay_to_continue(1,1,0);
2203 else if (otc == SKIP_TAPE) {
2204 delete_tape_list(elist); /* skip this tape */
2207 dump_device_name = newstralloc(dump_device_name, tape_device_name);
2209 dump_datestamp = newstralloc(dump_datestamp, elist->date);
2211 /* connect to the tape handler daemon on the tape drive server */
2212 if ((extract_files_setup(elist->tape, elist->fileno)) == -1)
2214 fprintf(stderr, "amrecover - can't talk to tape server\n");
2218 /* if the server have fe_amrecover_feedme_tape, it has asked for
2219 * the tape itself, even if the restore didn't succeed, we should
2222 if(writer_intermediary(elist) == 0 ||
2223 am_has_feature(indexsrv_features, fe_amrecover_feedme_tape))
2224 delete_tape_list(elist); /* tape done so delete from list */
2231 amidxtaped_response(
2234 security_handle_t * sech)
2236 int ports[NSTREAMS], *response_error = datap, i;
2241 assert(response_error != NULL);
2242 assert(sech != NULL);
2243 memset(ports, -1, SIZEOF(ports));
2245 security_close_connection(sech, dump_hostname);
2247 errstr = newvstralloc(errstr, "[request failed: ",
2248 security_geterror(sech), "]", NULL);
2249 *response_error = 1;
2253 if (pkt->type == P_NAK) {
2254 #if defined(PACKET_DEBUG)
2255 fprintf(stderr, "got nak response:\n----\n%s\n----\n\n", pkt->body);
2258 tok = strtok(pkt->body, " ");
2259 if (tok == NULL || strcmp(tok, "ERROR") != 0)
2262 tok = strtok(NULL, "\n");
2264 errstr = newvstralloc(errstr, "NAK: ", tok, NULL);
2265 *response_error = 1;
2268 errstr = newstralloc(errstr, "request NAK");
2269 *response_error = 2;
2274 if (pkt->type != P_REP) {
2275 errstr = newvstralloc(errstr, "received strange packet type ",
2276 pkt_type2str(pkt->type), ": ", pkt->body, NULL);
2277 *response_error = 1;
2281 #if defined(PACKET_DEBUG)
2282 fprintf(stderr, "got response:\n----\n%s\n----\n\n", pkt->body);
2285 for(i = 0; i < NSTREAMS; i++) {
2287 amidxtaped_streams[i].fd = NULL;
2291 while((tok = strtok(p, " \n")) != NULL) {
2295 * Error response packets have "ERROR" followed by the error message
2296 * followed by a newline.
2298 if (strcmp(tok, "ERROR") == 0) {
2299 tok = strtok(NULL, "\n");
2301 tok = "[bogus error packet]";
2302 errstr = newstralloc(errstr, tok);
2303 *response_error = 2;
2309 * Regular packets have CONNECT followed by three streams
2311 if (strcmp(tok, "CONNECT") == 0) {
2314 * Parse the three stream specifiers out of the packet.
2316 for (i = 0; i < NSTREAMS; i++) {
2317 tok = strtok(NULL, " ");
2318 if (tok == NULL || strcmp(tok, amidxtaped_streams[i].name) != 0) {
2319 extra = vstralloc("CONNECT token is \"",
2320 tok ? tok : "(null)",
2322 amidxtaped_streams[i].name,
2327 tok = strtok(NULL, " \n");
2328 if (tok == NULL || sscanf(tok, "%d", &ports[i]) != 1) {
2329 extra = vstralloc("CONNECT ",
2330 amidxtaped_streams[i].name,
2332 tok ? tok : "(null)",
2333 "\": expected a port number",
2342 * OPTIONS [options string] '\n'
2344 if (strcmp(tok, "OPTIONS") == 0) {
2345 tok = strtok(NULL, "\n");
2347 extra = stralloc("OPTIONS token is missing");
2351 while((p = strchr(tok, ';')) != NULL) {
2353 #define sc "features="
2354 if(strncmp(tok, sc, sizeof(sc)-1) == 0) {
2355 tok += sizeof(sc) - 1;
2357 am_release_feature_set(their_features);
2358 if((their_features = am_string_to_feature(tok)) == NULL) {
2359 errstr = newvstralloc(errstr,
2360 "OPTIONS: bad features value: ",
2372 extra = vstralloc("next token is \"",
2373 tok ? tok : "(null)",
2374 "\": expected \"CONNECT\", \"ERROR\" or \"OPTIONS\"",
2381 * Connect the streams to their remote ports
2383 for (i = 0; i < NSTREAMS; i++) {
2386 amidxtaped_streams[i].fd = security_stream_client(sech, ports[i]);
2387 dbprintf(("amidxtaped_streams[%d].fd = %p\n",i, amidxtaped_streams[i].fd));
2388 if (amidxtaped_streams[i].fd == NULL) {
2389 errstr = newvstralloc(errstr,
2390 "[could not connect ", amidxtaped_streams[i].name, " stream: ",
2391 security_geterror(sech), "]", NULL);
2396 * Authenticate the streams
2398 for (i = 0; i < NSTREAMS; i++) {
2399 if (amidxtaped_streams[i].fd == NULL)
2401 if (security_stream_auth(amidxtaped_streams[i].fd) < 0) {
2402 errstr = newvstralloc(errstr,
2403 "[could not authenticate ", amidxtaped_streams[i].name, " stream: ",
2404 security_stream_geterror(amidxtaped_streams[i].fd), "]", NULL);
2410 * The CTLFD and DATAFD streams are mandatory. If we didn't get
2413 if (amidxtaped_streams[CTLFD].fd == NULL) {
2414 errstr = newstralloc(errstr, "[couldn't open CTL streams]");
2417 if (amidxtaped_streams[DATAFD].fd == NULL) {
2418 errstr = newstralloc(errstr, "[couldn't open DATA streams]");
2422 /* everything worked */
2423 *response_error = 0;
2427 errstr = newvstralloc(errstr,
2428 "[parse of reply message failed: ",
2429 extra ? extra : "(no additional information)",
2433 *response_error = 2;
2438 *response_error = 1;
2442 * This is called when everything needs to shut down so event_loop()
2446 stop_amidxtaped(void)
2450 for (i = 0; i < NSTREAMS; i++) {
2451 if (amidxtaped_streams[i].fd != NULL) {
2452 security_stream_close(amidxtaped_streams[i].fd);
2453 amidxtaped_streams[i].fd = NULL;
2458 static char* ctl_buffer = NULL;
2459 /* gets a "line" from server and put in server_line */
2460 /* server_line is terminated with \0, \r\n is striped */
2461 /* returns -1 if error */
2464 get_amidxtaped_line(void)
2470 amfree(amidxtaped_line);
2472 ctl_buffer = stralloc("");
2474 while (!strstr(ctl_buffer,"\r\n")) {
2475 size = security_stream_read_sync(amidxtaped_streams[CTLFD].fd, &buf);
2479 else if(size == 0) {
2482 newbuf = alloc(strlen(ctl_buffer)+size+1);
2483 strncpy(newbuf, ctl_buffer, (size_t)(strlen(ctl_buffer) + size + 1));
2484 memcpy(newbuf+strlen(ctl_buffer), buf, (size_t)size);
2485 newbuf[strlen(ctl_buffer)+size] = '\0';
2487 ctl_buffer = newbuf;
2490 s = strstr(ctl_buffer,"\r\n");
2492 newbuf = stralloc(s+2);
2493 amidxtaped_line = stralloc(ctl_buffer);
2495 ctl_buffer = newbuf;
2501 read_amidxtaped_data(
2508 assert(cookie != NULL);
2510 fd = *(int *)cookie;
2512 errstr = newstralloc2(errstr, "amidxtaped read: ",
2513 security_stream_geterror(amidxtaped_streams[DATAFD].fd));
2518 * EOF. Stop and return.
2521 security_stream_close(amidxtaped_streams[DATAFD].fd);
2522 amidxtaped_streams[DATAFD].fd = NULL;
2524 * If the mesg fd has also shut down, then we're done.
2529 assert(buf != NULL);
2532 * We ignore errors while writing to the index file.
2534 (void)fullwrite(fd, buf, (size_t)size);
2535 security_stream_read(amidxtaped_streams[DATAFD].fd, read_amidxtaped_data, cookie);
2539 amidxtaped_client_get_security_conf(
2543 (void)arg; /* Quiet unused parameter warning */
2545 if(!string || !*string)
2548 if(strcmp(string, "auth")==0) {
2549 return(client_getconf_str(CLN_AUTH));
2551 if(strcmp(string, "ssh_keys")==0) {
2552 return(client_getconf_str(CLN_SSH_KEYS));