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"
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..
169 SELECT_ARG_TYPE readset;
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;
743 char *dir, *dir_undo, dir_undo_ch = '\0';
744 char *ditem_path = NULL;
745 char *qditem_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) { /* "*" */
765 regex = "([^/.]|\\.[^/]+|[^/.][^/]*)[/]*$";
767 /* remove "/" at end of path */
768 j = (ssize_t)(strlen(regex) - 1);
769 while(j >= 0 && regex[j] == '/')
773 /* convert path (assumed in cwd) to one on disk */
774 if (strcmp(disk_path, "/") == 0) {
776 /* No mods needed if already starts with '/' */
777 path_on_disk = stralloc(regex);
780 path_on_disk = stralloc2("/", regex);
783 char *clean_disk_path = clean_regex(disk_path);
784 path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
785 amfree(clean_disk_path);
788 dbprintf(("add_file: Converted path=\"%s\" to path_on_disk=\"%s\"\n",
789 regex, path_on_disk));
793 for (ditem=get_dir_list(); ditem!=NULL; ditem=get_next_dir_item(ditem))
796 quoted = quote_string(ditem->path);
797 dbprintf(("add_file: Pondering ditem->path=%s\n", quoted));
799 if (match(path_on_disk, ditem->path))
802 j = (ssize_t)strlen(ditem->path);
803 if((j > 0 && ditem->path[j-1] == '/')
804 || (j > 1 && ditem->path[j-2] == '/' && ditem->path[j-1] == '.'))
805 { /* It is a directory */
806 ditem_path = newstralloc(ditem_path, ditem->path);
807 clean_pathname(ditem_path);
809 qditem_path = quote_string(ditem_path);
810 cmd = stralloc2("ORLD ", qditem_path);
812 if(send_command(cmd) == -1) {
815 amfree(path_on_disk);
821 if ((i = get_reply_line()) == -1) {
823 amfree(path_on_disk);
826 if(i==0) { /* assume something wrong */
828 amfree(path_on_disk);
835 lditem.path = newstralloc(lditem.path, ditem->path);
836 /* skip the last line -- duplicate of the preamble */
838 while ((i = get_reply_line()) != 0) {
841 amfree(path_on_disk);
846 if(dir_undo) *dir_undo = dir_undo_ch;
848 cmd = stralloc(l); /* save for error report */
850 continue; /* throw the rest of the lines away */
853 if (!server_happy()) {
859 if(strncmp_const_skip(l, "201-", s, ch) != 0) {
860 err = "bad reply: not 201-";
865 skip_whitespace(s, ch);
867 err = "bad reply: missing date field";
871 skip_non_whitespace(s, ch);
873 lditem.date = newstralloc(lditem.date, fp);
876 skip_whitespace(s, ch);
877 if(ch == '\0' || sscanf(s - 1, "%d", &lditem.level) != 1) {
878 err = "bad reply: cannot parse level field";
883 skip_whitespace(s, ch);
885 err = "bad reply: missing tape field";
889 skip_non_whitespace(s, ch);
891 lditem.tape = newstralloc(lditem.tape, fp);
894 if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_ORLD)) {
895 OFF_T_FMT_TYPE fileno_ = (OFF_T_FMT_TYPE)0;
896 skip_whitespace(s, ch);
898 sscanf(s - 1, OFF_T_FMT, &fileno_) != 1) {
899 err = "bad reply: cannot parse fileno field";
902 lditem.fileno = (off_t)fileno_;
906 skip_whitespace(s, ch);
908 err = "bad reply: missing directory field";
912 skip_quoted_string(s, ch);
914 dir_undo_ch = *dir_undo;
917 switch(add_extract_item(&lditem)) {
919 printf("System error\n");
920 dbprintf(("add_file: (Failed) System error\n"));
924 quoted = quote_string(lditem.path);
925 printf("Added dir %s at date %s\n",
926 quoted, lditem.date);
927 dbprintf(("add_file: (Successful) Added dir %s at date %s\n",
928 quoted, lditem.date));
937 if(!server_happy()) {
944 } else if(added == 0) {
945 quoted = quote_string(ditem_path);
946 printf("dir %s already added\n", quoted);
947 dbprintf(("add_file: dir %s already added\n", quoted));
951 else /* It is a file */
953 switch(add_extract_item(ditem)) {
955 printf("System error\n");
956 dbprintf(("add_file: (Failed) System error\n"));
960 quoted = quote_string(ditem->path);
961 printf("Added file %s\n", quoted);
962 dbprintf(("add_file: (Successful) Added %s\n", quoted));
967 quoted = quote_string(ditem->path);
968 printf("File %s already added\n", quoted);
969 dbprintf(("add_file: file %s already added\n", quoted));
978 amfree(path_on_disk);
985 quoted = quote_string(path);
986 printf("File %s doesn't exist in directory\n", quoted);
987 dbprintf(("add_file: (Failed) File %s doesn't exist in directory\n",
1001 char *uqglob = unquote_string(glob);
1003 regex = glob_to_regex(uqglob);
1004 dbprintf(("delete_glob (%s) -> %s\n", uqglob, regex));
1005 if ((s = validate_regexp(regex)) != NULL) {
1006 printf("\"%s\" is not a valid shell wildcard pattern: ", glob);
1010 * glob_to_regex() anchors the beginning of the pattern with ^,
1011 * but we will be tacking it onto the end of the current directory
1012 * in add_file, so strip that off. Also, it anchors the end with
1013 * $, but we need to match an optional trailing /, so tack that on
1016 regex_path = stralloc(regex + 1);
1017 regex_path[strlen(regex_path) - 1] = '\0';
1018 strappend(regex_path, "[/]*$");
1019 delete_file(uqglob, regex_path);
1031 char *uqregex = unquote_string(regex);
1033 if ((s = validate_regexp(regex)) != NULL) {
1034 printf("\"%s\" is not a valid regular expression: ", regex);
1037 delete_file(uqregex, uqregex);
1047 DIR_ITEM *ditem, lditem;
1048 char *path_on_disk = NULL;
1054 char *tape, *tape_undo, tape_undo_ch = '\0';
1055 char *dir_undo, dir_undo_ch = '\0';
1058 char *ditem_path = NULL;
1067 if (disk_path == NULL) {
1068 printf("Must select directory before deleting files\n");
1071 memset(&lditem, 0, sizeof(lditem)); /* Prevent use of bogus data... */
1073 dbprintf(("delete_file: Looking for \"%s\"\n", path));
1075 if (strcmp(regex, "[^/]*[/]*$") == 0) {
1076 /* Looking for * find everything but single . */
1077 regex = "([^/.]|\\.[^/]+|[^/.][^/]*)[/]*$";
1079 /* remove "/" at end of path */
1080 j = (ssize_t)(strlen(regex) - 1);
1081 while(j >= 0 && regex[j] == '/') regex[j--] = '\0';
1084 /* convert path (assumed in cwd) to one on disk */
1085 if (strcmp(disk_path, "/") == 0) {
1086 if (*regex == '/') {
1087 if (strcmp(regex, "/[/]*$") == 0) {
1088 /* We want "/" to match the directory itself: "/." */
1089 path_on_disk = stralloc("/\\.[/]*$");
1091 /* No mods needed if already starts with '/' */
1092 path_on_disk = stralloc(regex);
1096 path_on_disk = stralloc2("/", regex);
1099 char *clean_disk_path = clean_regex(disk_path);
1100 path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
1101 amfree(clean_disk_path);
1104 dbprintf(("delete_file: Converted path=\"%s\" to path_on_disk=\"%s\"\n",
1105 regex, path_on_disk));
1107 for (ditem=get_dir_list(); ditem!=NULL; ditem=get_next_dir_item(ditem))
1109 quoted = quote_string(ditem->path);
1110 dbprintf(("delete_file: Pondering ditem->path=%s\n", quoted));
1112 if (match(path_on_disk, ditem->path))
1115 j = (ssize_t)strlen(ditem->path);
1116 if((j > 0 && ditem->path[j-1] == '/')
1117 || (j > 1 && ditem->path[j-2] == '/' && ditem->path[j-1] == '.'))
1118 { /* It is a directory */
1119 ditem_path = newstralloc(ditem_path, ditem->path);
1120 clean_pathname(ditem_path);
1122 qditem_path = quote_string(ditem_path);
1123 cmd = stralloc2("ORLD ", qditem_path);
1124 amfree(qditem_path);
1125 if(send_command(cmd) == -1) {
1128 amfree(path_on_disk);
1133 if ((i = get_reply_line()) == -1) {
1135 amfree(path_on_disk);
1138 if(i==0) /* assume something wrong */
1141 amfree(path_on_disk);
1147 lditem.path = newstralloc(lditem.path, ditem->path);
1149 tape_undo = dir_undo = NULL;
1150 /* skip the last line -- duplicate of the preamble */
1151 while ((i = get_reply_line()) != 0)
1155 amfree(path_on_disk);
1160 if(tape_undo) *tape_undo = tape_undo_ch;
1161 if(dir_undo) *dir_undo = dir_undo_ch;
1162 tape_undo = dir_undo = NULL;
1163 cmd = stralloc(l); /* save for the error report */
1165 continue; /* throw the rest of the lines away */
1168 if (!server_happy()) {
1174 if(strncmp_const_skip(l, "201-", s, ch) != 0) {
1175 err = "bad reply: not 201-";
1180 skip_whitespace(s, ch);
1182 err = "bad reply: missing date field";
1186 skip_non_whitespace(s, ch);
1189 skip_whitespace(s, ch);
1190 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
1191 err = "bad reply: cannot parse level field";
1194 skip_integer(s, ch);
1196 skip_whitespace(s, ch);
1198 err = "bad reply: missing tape field";
1202 skip_non_whitespace(s, ch);
1204 tape_undo_ch = *tape_undo;
1207 if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_ORLD)) {
1208 OFF_T_FMT_TYPE fileno_ = (OFF_T_FMT_TYPE)0;
1209 skip_whitespace(s, ch);
1211 sscanf(s - 1, OFF_T_FMT, &fileno_) != 1) {
1212 err = "bad reply: cannot parse fileno field";
1215 fileno = (off_t)fileno_;
1216 skip_integer(s, ch);
1219 skip_whitespace(s, ch);
1221 err = "bad reply: missing directory field";
1224 skip_non_whitespace(s, ch);
1226 dir_undo_ch = *dir_undo;
1229 lditem.date = newstralloc(lditem.date, date);
1231 lditem.tape = newstralloc(lditem.tape, tape);
1232 switch(delete_extract_item(&lditem)) {
1234 printf("System error\n");
1235 dbprintf(("delete_file: (Failed) System error\n"));
1238 printf("Deleted dir %s at date %s\n", ditem_path, date);
1239 dbprintf(("delete_file: (Successful) Deleted dir %s at date %s\n",
1247 if(!server_happy()) {
1254 } else if(deleted == 0) {
1255 printf("Warning - dir '%s' not on tape list\n",
1257 dbprintf(("delete_file: dir '%s' not on tape list\n",
1263 switch(delete_extract_item(ditem)) {
1265 printf("System error\n");
1266 dbprintf(("delete_file: (Failed) System error\n"));
1269 printf("Deleted %s\n", ditem->path);
1270 dbprintf(("delete_file: (Successful) Deleted %s\n",
1274 printf("Warning - file '%s' not on tape list\n",
1276 dbprintf(("delete_file: file '%s' not on tape list\n",
1285 amfree(path_on_disk);
1288 printf("File %s doesn't exist in directory\n", path);
1289 dbprintf(("delete_file: (Failed) File %s doesn't exist in directory\n",
1295 /* print extract list into file. If NULL ptr passed print to screen */
1297 display_extract_list(
1301 EXTRACT_LIST_ITEM *that;
1304 char *pager_command;
1309 if ((pager = getenv("PAGER")) == NULL)
1314 * Set up the pager command so if the pager is terminated, we do
1315 * not get a SIGPIPE back.
1317 pager_command = stralloc2(pager, " ; /bin/cat > /dev/null");
1318 if ((fp = popen(pager_command, "w")) == NULL)
1320 printf("Warning - can't pipe through %s\n", pager);
1323 amfree(pager_command);
1327 uqfile = unquote_string(file);
1328 if ((fp = fopen(uqfile, "w")) == NULL)
1330 printf("Can't open file %s to print extract list into\n", file);
1337 for (this = extract_list; this != NULL; this = this->next)
1339 fprintf(fp, "TAPE %s LEVEL %d DATE %s\n",
1340 this->tape, this->level, this->date);
1341 for (that = this->files; that != NULL; that = that->next)
1342 fprintf(fp, "\t%s\n", that->path);
1348 printf("Extract list written to file %s\n", file);
1359 struct dirent *entry;
1362 if((dir = opendir(fname)) == NULL)
1366 while(!gotentry && (entry = readdir(dir)) != NULL) {
1367 gotentry = !is_dot_or_dotdot(entry->d_name);
1375 /* returns 0 if extract list empty and 1 if it isn't */
1377 is_extract_list_nonempty(void)
1379 return (extract_list != NULL);
1383 /* prints continue prompt and waits for response,
1384 returns 0 if don't, non-0 if do */
1401 prompt = "New tape device [?]: ";
1402 } else if (allow_tape && allow_skip) {
1403 prompt = "Continue [?/Y/n/s/t]? ";
1404 } else if (allow_tape && !allow_skip) {
1405 prompt = "Continue [?/Y/n/t]? ";
1406 } else if (allow_retry) {
1407 prompt = "Continue [?/Y/n/r]? ";
1409 prompt = "Continue [?/Y/n]? ";
1411 fputs(prompt, stdout);
1412 fflush(stdout); fflush(stderr);
1414 if ((line = agets(stdin)) == NULL) {
1425 while ((ch = *s++) != '\0' && isspace(ch)) {
1426 (void)ch; /* Quiet empty loop compiler warning */
1430 printf("Enter a new device ([host:]device) or \"default\"\n");
1432 printf("Enter \"y\"es to continue, \"n\"o to stop");
1434 printf(", \"s\"kip this tape");
1437 printf(" or \"r\"etry this tape");
1440 printf(" or \"t\"ape to change tape drives");
1444 } else if (get_tape) {
1447 } else if (ch == '\0' || ch == 'Y' || ch == 'y') {
1449 } else if (allow_tape && (ch == 'T' || ch == 't')) {
1451 } else if (ch == 'N' || ch == 'n') {
1453 } else if (allow_retry && (ch == 'R' || ch == 'r')) {
1455 } else if (allow_skip && (ch == 'S' || ch == 's')) {
1466 send_to_tape_server(
1467 security_stream_t * stream,
1470 char *msg = stralloc2(cmd, "\r\n");
1472 if (security_stream_write(stream, msg, strlen(msg)) < 0)
1474 error("Error writing to tape server");
1482 /* start up connection to tape server and set commands to initiate
1483 transfer of dump image.
1484 Return tape server socket on success, -1 on error. */
1486 extract_files_setup(
1490 char *disk_regex = NULL;
1491 char *host_regex = NULL;
1492 char *clean_datestamp, *ch, *ch1;
1497 amidxtaped_secdrv = security_getdriver(authopt);
1498 if (amidxtaped_secdrv == NULL) {
1499 error("no '%s' security driver available for host '%s'",
1500 authopt, tape_server_name);
1503 /* We assume that amidxtaped support fe_amidxtaped_options_features */
1504 /* and fe_amidxtaped_options_auth */
1505 /* We should send a noop to really know */
1506 req = vstralloc("SERVICE amidxtaped\n",
1507 "OPTIONS ", "features=", our_features_string, ";",
1508 "auth=", authopt, ";",
1510 protocol_sendreq(tape_server_name, amidxtaped_secdrv,
1511 generic_client_get_security_conf, req, STARTUP_TIMEOUT,
1512 amidxtaped_response, &response_error);
1515 if(response_error != 0) {
1519 disk_regex = alloc(strlen(disk_name) * 2 + 3);
1524 /* we want to force amrestore to only match disk_name exactly */
1527 /* We need to escape some characters first... NT compatibilty crap */
1528 for (; *ch != 0; ch++, ch1++) {
1529 switch (*ch) { /* done this way in case there are more */
1532 /* no break; we do want to fall through... */
1538 /* we want to force amrestore to only match disk_name exactly */
1543 host_regex = alloc(strlen(dump_hostname) * 2 + 3);
1548 /* we want to force amrestore to only match dump_hostname exactly */
1551 /* We need to escape some characters first... NT compatibilty crap */
1552 for (; *ch != 0; ch++, ch1++) {
1553 switch (*ch) { /* done this way in case there are more */
1556 /* no break; we do want to fall through... */
1562 /* we want to force amrestore to only match dump_hostname exactly */
1567 clean_datestamp = stralloc(dump_datestamp);
1568 for(ch=ch1=clean_datestamp;*ch1 != '\0';ch1++) {
1575 /* push our feature list off to the tape server */
1576 /* XXX assumes that index server and tape server are equivalent, ew */
1578 if(am_has_feature(indexsrv_features, fe_amidxtaped_exchange_features)){
1579 tt = newstralloc2(tt, "FEATURES=", our_features_string);
1580 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1581 get_amidxtaped_line();
1582 if(strncmp_const(amidxtaped_line,"FEATURES=") == 0) {
1583 tapesrv_features = am_string_to_feature(amidxtaped_line+9);
1585 fprintf(stderr, "amrecover - expecting FEATURES line from amidxtaped\n");
1589 amfree(clean_datestamp);
1592 am_release_feature_set(tapesrv_features);
1596 if(am_has_feature(indexsrv_features, fe_amidxtaped_header) &&
1597 am_has_feature(indexsrv_features, fe_amidxtaped_device) &&
1598 am_has_feature(indexsrv_features, fe_amidxtaped_host) &&
1599 am_has_feature(indexsrv_features, fe_amidxtaped_disk) &&
1600 am_has_feature(indexsrv_features, fe_amidxtaped_datestamp)) {
1602 if(am_has_feature(indexsrv_features, fe_amidxtaped_config)) {
1603 tt = newstralloc2(tt, "CONFIG=", config);
1604 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1606 if(am_has_feature(indexsrv_features, fe_amidxtaped_label) &&
1607 label && label[0] != '/') {
1608 tt = newstralloc2(tt,"LABEL=",label);
1609 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1611 if(am_has_feature(indexsrv_features, fe_amidxtaped_fsf)) {
1613 snprintf(v_fsf, 99, OFF_T_FMT, (OFF_T_FMT_TYPE)fsf);
1614 tt = newstralloc2(tt, "FSF=",v_fsf);
1615 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1617 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "HEADER");
1618 tt = newstralloc2(tt, "DEVICE=", dump_device_name);
1619 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1620 tt = newstralloc2(tt, "HOST=", host_regex);
1621 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1622 tt = newstralloc2(tt, "DISK=", disk_regex);
1623 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1624 tt = newstralloc2(tt, "DATESTAMP=", clean_datestamp);
1625 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1626 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "END");
1629 else if(am_has_feature(indexsrv_features, fe_amidxtaped_nargs)) {
1630 /* send to the tape server what tape file we want */
1639 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "6");
1640 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "-h");
1641 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "-p");
1642 send_to_tape_server(amidxtaped_streams[CTLFD].fd, dump_device_name);
1643 send_to_tape_server(amidxtaped_streams[CTLFD].fd, host_regex);
1644 send_to_tape_server(amidxtaped_streams[CTLFD].fd, disk_regex);
1645 send_to_tape_server(amidxtaped_streams[CTLFD].fd, clean_datestamp);
1647 dbprintf(("Started amidxtaped with arguments \"6 -h -p %s %s %s %s\"\n",
1648 dump_device_name, host_regex, disk_regex, clean_datestamp));
1653 amfree(clean_datestamp);
1660 * Reads the first block of a tape file.
1671 bytes_read = read_buffer(tapedev, buffer, buflen, READ_TIMEOUT);
1672 if(bytes_read < 0) {
1673 error("error reading header (%s), check amidxtaped.*.debug on server",
1678 if((size_t)bytes_read < buflen) {
1679 fprintf(stderr, "%s: short block %d byte%s\n",
1680 get_pname(), (int)bytes_read, (bytes_read == 1) ? "" : "s");
1681 print_header(stdout, file);
1682 error("Can't read file header");
1686 /* bytes_read == buflen */
1687 parse_file_header(buffer, file, (size_t)bytes_read);
1701 extract_files_child(
1703 EXTRACT_LIST * elist)
1706 int extra_params = 0;
1708 char **restore_args = NULL;
1710 EXTRACT_LIST_ITEM *fn;
1711 enum dumptypes dumptype = IS_UNKNOWN;
1712 char buffer[DISK_BLOCK_BYTES];
1716 int passwd_field = -1;
1718 char *domain = NULL, *smbpass = NULL;
1721 /* code executed by child to do extraction */
1724 /* make in_fd be our stdin */
1725 if (dup2(in_fd, STDIN_FILENO) == -1)
1727 error("dup2 failed in extract_files_child: %s", strerror(errno));
1731 /* read the file header */
1733 read_file_header(buffer, &file, sizeof(buffer), STDIN_FILENO);
1735 if(file.type != F_DUMPFILE) {
1736 print_header(stdout, &file);
1737 error("bad header");
1741 if (file.program != NULL) {
1742 if (strcmp(file.program, "BACKUP") == 0)
1743 dumptype = IS_BACKUP_API;
1745 if (strcmp(file.program, GNUTAR) == 0)
1746 dumptype = IS_GNUTAR;
1749 if (dumptype == IS_UNKNOWN) {
1750 len_program = strlen(file.program);
1751 if(len_program >= 3 &&
1752 strcmp(&file.program[len_program-3],"tar") == 0)
1757 if (dumptype == IS_UNKNOWN && strcmp(file.program, SAMBA_CLIENT) ==0) {
1758 if (samba_extract_method == SAMBA_TAR)
1759 dumptype = IS_SAMBA_TAR;
1761 dumptype = IS_SAMBA;
1766 /* form the arguments to restore */
1767 files_off_tape = length_of_tape_list(elist);
1786 #if defined(XFSDUMP)
1787 if (strcmp(file.program, XFSDUMP) == 0) {
1788 extra_params = 4 + files_off_tape;
1800 restore_args = (char **)alloc((size_t)((extra_params + files_off_tape + 1)
1805 restore_args[j++] = stralloc("smbclient");
1806 smbpass = findpass(file.disk, &domain);
1808 restore_args[j++] = stralloc(file.disk);
1810 restore_args[j++] = stralloc("-U");
1811 restore_args[j++] = smbpass;
1813 restore_args[j++] = stralloc("-W");
1814 restore_args[j++] = stralloc(domain);
1819 restore_args[j++] = stralloc("-d0");
1820 restore_args[j++] = stralloc("-Tx");
1821 restore_args[j++] = stralloc("-"); /* data on stdin */
1826 restore_args[j++] = stralloc("tar");
1827 restore_args[j++] = stralloc("--numeric-owner");
1828 restore_args[j++] = stralloc("-xpGvf");
1829 restore_args[j++] = stralloc("-"); /* data on stdin */
1832 restore_args[j++] = stralloc("tar");
1833 restore_args[j++] = stralloc("-xpvf");
1834 restore_args[j++] = stralloc("-"); /* data on stdin */
1838 restore_args[j++] = stralloc("restore");
1840 restore_args[j++] = stralloc("-xB");
1842 #if defined(XFSDUMP)
1843 if (strcmp(file.program, XFSDUMP) == 0) {
1844 restore_args[j++] = stralloc("-v");
1845 restore_args[j++] = stralloc("silent");
1849 if (strcmp(file.program, VDUMP) == 0) {
1850 restore_args[j++] = stralloc("xf");
1851 restore_args[j++] = stralloc("-"); /* data on stdin */
1855 restore_args[j++] = stralloc("xbf");
1856 restore_args[j++] = stralloc("2"); /* read in units of 1K */
1857 restore_args[j++] = stralloc("-"); /* data on stdin */
1862 restore_args[j++] = stralloc(file.dumper);
1863 restore_args[j++] = stralloc("restore");
1864 restore_args[j++] = stralloc("--config");
1865 restore_args[j++] = stralloc(config);
1866 restore_args[j++] = stralloc("--disk");
1867 restore_args[j++] = stralloc(file.disk);
1871 for (i = 0, fn = elist->files; i < files_off_tape; i++, fn = fn->next)
1879 if (strcmp(fn->path, "/") == 0)
1880 restore_args[j++] = stralloc(".");
1882 restore_args[j++] = stralloc2(".", fn->path);
1886 #if defined(XFSDUMP)
1887 if (strcmp(file.program, XFSDUMP) == 0) {
1889 * xfsrestore needs a -s option before each file to be
1890 * restored, and also wants them to be relative paths.
1892 restore_args[j++] = stralloc("-s");
1893 restore_args[j++] = stralloc(fn->path + 1);
1897 restore_args[j++] = stralloc(fn->path);
1902 #if defined(XFSDUMP)
1903 if (strcmp(file.program, XFSDUMP) == 0) {
1904 restore_args[j++] = stralloc("-");
1905 restore_args[j++] = stralloc(".");
1908 restore_args[j] = NULL;
1913 cmd = stralloc(SAMBA_CLIENT);
1916 /* fall through to ... */
1922 fprintf(stderr, "warning: GNUTAR program not available.\n");
1923 cmd = stralloc("tar");
1925 cmd = stralloc(GNUTAR);
1932 if (strcmp(file.program, DUMP) == 0) {
1933 cmd = stralloc(RESTORE);
1937 if (strcmp(file.program, VDUMP) == 0) {
1938 cmd = stralloc(VRESTORE);
1942 if (strcmp(file.program, VXDUMP) == 0) {
1943 cmd = stralloc(VXRESTORE);
1946 #if defined(XFSDUMP)
1947 if (strcmp(file.program, XFSDUMP) == 0) {
1948 cmd = stralloc(XFSRESTORE);
1952 fprintf(stderr, "warning: restore program for %s not available.\n",
1954 cmd = stralloc("restore");
1958 cmd = vstralloc(DUMPER_DIR, "/", file.dumper, NULL);
1962 dbprintf(("Exec'ing %s with arguments:\n", cmd));
1963 for (i = 0; i < j; i++) {
1964 if( i == passwd_field)
1965 dbprintf(("\tXXXXX\n"));
1967 dbprintf(("\t%s\n", restore_args[i]));
1969 (void)execv(cmd, restore_args);
1970 /* only get here if exec failed */
1972 for (i = 0; i < j; i++) {
1973 amfree(restore_args[i]);
1975 amfree(restore_args);
1977 perror("amrecover couldn't exec");
1978 fprintf(stderr, " problem executing %s\n", cmd);
1986 * Interpose something between the process writing out the dump (writing it to
1987 * some extraction program, really) and the socket from which we're reading, so
1988 * that we can do things like prompt for human interaction for multiple tapes.
1991 writer_intermediary(
1992 EXTRACT_LIST * elist)
1996 amwait_t extractor_status;
1998 if(pipe(child_pipe) == -1) {
1999 error("extract_list - error setting up pipe to extractor: %s\n",
2004 /* okay, ready to extract. fork a child to do the actual work */
2005 if ((pid = fork()) == 0) {
2006 /* this is the child process */
2007 /* never gets out of this clause */
2008 aclose(child_pipe[1]);
2009 extract_files_child(child_pipe[0], elist);
2013 /* This is the parent */
2015 printf("writer_intermediary - error forking child");
2019 aclose(child_pipe[0]);
2021 security_stream_read(amidxtaped_streams[DATAFD].fd,
2022 read_amidxtaped_data, &(child_pipe[1]));
2024 while(get_amidxtaped_line() >= 0) {
2025 char desired_tape[MAX_TAPE_LABEL_BUF];
2027 /* if prompted for a tape, relay said prompt to the user */
2028 if(sscanf(amidxtaped_line, "FEEDME %132s\n", desired_tape) == 1) {
2030 printf("Load tape %s now\n", desired_tape);
2031 done = okay_to_continue(am_has_feature(indexsrv_features,
2032 fe_amrecover_feedme_tape),
2035 if (am_has_feature(indexsrv_features,
2036 fe_amrecover_feedme_tape)) {
2037 char *reply = stralloc2("TAPE ", tape_device_name);
2038 send_to_tape_server(amidxtaped_streams[CTLFD].fd, reply);
2041 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "OK");
2044 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "ERROR");
2047 } else if(strncmp_const(amidxtaped_line, "MESSAGE ") == 0) {
2048 printf("%s\n",&amidxtaped_line[8]);
2050 fprintf(stderr, "Strange message from tape server: %s",
2056 /* CTL might be close before DATA */
2058 aclose(child_pipe[1]);
2060 waitpid(pid, &extractor_status, 0);
2061 if(WEXITSTATUS(extractor_status) != 0){
2062 int ret = WEXITSTATUS(extractor_status);
2063 if(ret == 255) ret = -1;
2064 printf("Extractor child exited with status %d\n", ret);
2070 /* exec restore to do the actual restoration */
2072 /* does the actual extraction of files */
2074 * The original design had the dump image being returned exactly as it
2075 * appears on the tape, and this routine getting from the index server
2076 * whether or not it is compressed, on the assumption that the tape
2077 * server may not know how to uncompress it. But
2078 * - Amrestore can't do that. It returns either compressed or uncompressed
2079 * (always). Amrestore assumes it can uncompress files. It is thus a good
2080 * idea to run the tape server on a machine with gzip.
2081 * - The information about compression in the disklist is really only
2082 * for future dumps. It is possible to change compression on a drive
2083 * so the information in the disklist may not necessarily relate to
2084 * the dump image on the tape.
2085 * Consequently the design was changed to assuming that amrestore can
2086 * uncompress any dump image and have it return an uncompressed file
2092 EXTRACT_LIST *elist;
2097 tapelist_t *tlist = NULL, *a_tlist;
2099 if (!is_extract_list_nonempty())
2101 printf("Extract list empty - No files to extract!\n");
2105 clean_extract_list();
2107 /* get tape device name from index server if none specified */
2108 if (tape_server_name == NULL) {
2109 tape_server_name = newstralloc(tape_server_name, server_name);
2111 if (tape_device_name == NULL) {
2112 if (send_command("TAPE") == -1)
2114 if (get_reply_line() == -1)
2117 if (!server_happy())
2122 /* skip reply number */
2123 tape_device_name = newstralloc(tape_device_name, l+4);
2126 if (strcmp(tape_device_name, "/dev/null") == 0)
2128 printf("amrecover: warning: using %s as the tape device will not work\n",
2133 for (elist = first_tape_list(); elist != NULL; elist = next_tape_list(elist)) {
2134 if(elist->tape[0]!='/') {
2136 printf("\nExtracting files using tape drive %s on host %s.\n",
2137 tape_device_name, tape_server_name);
2138 printf("The following tapes are needed:");
2143 tlist = unmarshal_tapelist_str(elist->tape);
2144 for(a_tlist = tlist ; a_tlist != NULL; a_tlist = a_tlist->next)
2145 printf(" %s", a_tlist->label);
2147 free_tapelist(tlist);
2151 for (elist = first_tape_list(); elist != NULL; elist = next_tape_list(elist)) {
2152 if(elist->tape[0]=='/') {
2154 printf("\nExtracting files from holding disk on host %s.\n",
2156 printf("The following files are needed:");
2161 tlist = unmarshal_tapelist_str(elist->tape);
2162 for(a_tlist = tlist; a_tlist != NULL; a_tlist = a_tlist->next)
2163 printf(" %s", a_tlist->label);
2165 free_tapelist(tlist);
2170 if (getcwd(cwd, sizeof(cwd)) == NULL) {
2171 perror("extract_list: Current working directory unavailable");
2175 printf("Restoring files into directory %s\n", cwd);
2176 check_file_overwrite(cwd);
2179 if (samba_extract_method == SAMBA_SMBCLIENT)
2180 printf("(unless it is a Samba backup, that will go through to the SMB server)\n");
2182 if (!okay_to_continue(0,0,0))
2186 if (!do_unlink_list()) {
2187 fprintf(stderr, "Can't recover because I can't cleanup the cwd (%s)\n",
2193 while ((elist = first_tape_list()) != NULL)
2195 if(elist->tape[0]=='/') {
2196 dump_device_name = newstralloc(dump_device_name, elist->tape);
2197 printf("Extracting from file ");
2198 tlist = unmarshal_tapelist_str(dump_device_name);
2199 for(a_tlist = tlist; a_tlist != NULL; a_tlist = a_tlist->next)
2200 printf(" %s", a_tlist->label);
2202 free_tapelist(tlist);
2205 printf("Extracting files using tape drive %s on host %s.\n",
2206 tape_device_name, tape_server_name);
2207 tlist = unmarshal_tapelist_str(elist->tape);
2208 printf("Load tape %s now\n", tlist->label);
2209 free_tapelist(tlist);
2210 otc = okay_to_continue(1,1,0);
2213 else if (otc == SKIP_TAPE) {
2214 delete_tape_list(elist); /* skip this tape */
2217 dump_device_name = newstralloc(dump_device_name, tape_device_name);
2219 dump_datestamp = newstralloc(dump_datestamp, elist->date);
2221 /* connect to the tape handler daemon on the tape drive server */
2222 if ((extract_files_setup(elist->tape, elist->fileno)) == -1)
2224 fprintf(stderr, "amrecover - can't talk to tape server: %s\n",errstr);
2228 /* if the server have fe_amrecover_feedme_tape, it has asked for
2229 * the tape itself, even if the restore didn't succeed, we should
2232 if(writer_intermediary(elist) == 0 ||
2233 am_has_feature(indexsrv_features, fe_amrecover_feedme_tape))
2234 delete_tape_list(elist); /* tape done so delete from list */
2241 amidxtaped_response(
2244 security_handle_t * sech)
2246 int ports[NSTREAMS], *response_error = datap, i;
2251 assert(response_error != NULL);
2252 assert(sech != NULL);
2253 memset(ports, -1, SIZEOF(ports));
2256 errstr = newvstralloc(errstr, "[request failed: ",
2257 security_geterror(sech), "]", NULL);
2258 *response_error = 1;
2261 security_close_connection(sech, dump_hostname);
2263 if (pkt->type == P_NAK) {
2264 #if defined(PACKET_DEBUG)
2265 fprintf(stderr, "got nak response:\n----\n%s\n----\n\n", pkt->body);
2268 tok = strtok(pkt->body, " ");
2269 if (tok == NULL || strcmp(tok, "ERROR") != 0)
2272 tok = strtok(NULL, "\n");
2274 errstr = newvstralloc(errstr, "NAK: ", tok, NULL);
2275 *response_error = 1;
2278 errstr = newstralloc(errstr, "request NAK");
2279 *response_error = 2;
2284 if (pkt->type != P_REP) {
2285 errstr = newvstralloc(errstr, "received strange packet type ",
2286 pkt_type2str(pkt->type), ": ", pkt->body, NULL);
2287 *response_error = 1;
2291 #if defined(PACKET_DEBUG)
2292 fprintf(stderr, "got response:\n----\n%s\n----\n\n", pkt->body);
2295 for(i = 0; i < NSTREAMS; i++) {
2297 amidxtaped_streams[i].fd = NULL;
2301 while((tok = strtok(p, " \n")) != NULL) {
2305 * Error response packets have "ERROR" followed by the error message
2306 * followed by a newline.
2308 if (strcmp(tok, "ERROR") == 0) {
2309 tok = strtok(NULL, "\n");
2311 tok = "[bogus error packet]";
2312 errstr = newstralloc(errstr, tok);
2313 *response_error = 2;
2319 * Regular packets have CONNECT followed by three streams
2321 if (strcmp(tok, "CONNECT") == 0) {
2324 * Parse the three stream specifiers out of the packet.
2326 for (i = 0; i < NSTREAMS; i++) {
2327 tok = strtok(NULL, " ");
2328 if (tok == NULL || strcmp(tok, amidxtaped_streams[i].name) != 0) {
2329 extra = vstralloc("CONNECT token is \"",
2330 tok ? tok : "(null)",
2332 amidxtaped_streams[i].name,
2337 tok = strtok(NULL, " \n");
2338 if (tok == NULL || sscanf(tok, "%d", &ports[i]) != 1) {
2339 extra = vstralloc("CONNECT ",
2340 amidxtaped_streams[i].name,
2342 tok ? tok : "(null)",
2343 "\": expected a port number",
2352 * OPTIONS [options string] '\n'
2354 if (strcmp(tok, "OPTIONS") == 0) {
2355 tok = strtok(NULL, "\n");
2357 extra = stralloc("OPTIONS token is missing");
2361 while((p = strchr(tok, ';')) != NULL) {
2363 if(strncmp_const(tok, "features=") == 0) {
2364 tok += sizeof("features=") - 1;
2365 am_release_feature_set(their_features);
2366 if((their_features = am_string_to_feature(tok)) == NULL) {
2367 errstr = newvstralloc(errstr,
2368 "OPTIONS: bad features value: ",
2380 extra = vstralloc("next token is \"",
2381 tok ? tok : "(null)",
2382 "\": expected \"CONNECT\", \"ERROR\" or \"OPTIONS\"",
2389 * Connect the streams to their remote ports
2391 for (i = 0; i < NSTREAMS; i++) {
2394 amidxtaped_streams[i].fd = security_stream_client(sech, ports[i]);
2395 dbprintf(("amidxtaped_streams[%d].fd = %p\n",i, amidxtaped_streams[i].fd));
2396 if (amidxtaped_streams[i].fd == NULL) {
2397 errstr = newvstralloc(errstr,
2398 "[could not connect ", amidxtaped_streams[i].name, " stream: ",
2399 security_geterror(sech), "]", NULL);
2404 * Authenticate the streams
2406 for (i = 0; i < NSTREAMS; i++) {
2407 if (amidxtaped_streams[i].fd == NULL)
2409 if (security_stream_auth(amidxtaped_streams[i].fd) < 0) {
2410 errstr = newvstralloc(errstr,
2411 "[could not authenticate ", amidxtaped_streams[i].name, " stream: ",
2412 security_stream_geterror(amidxtaped_streams[i].fd), "]", NULL);
2418 * The CTLFD and DATAFD streams are mandatory. If we didn't get
2421 if (amidxtaped_streams[CTLFD].fd == NULL) {
2422 errstr = newstralloc(errstr, "[couldn't open CTL streams]");
2425 if (amidxtaped_streams[DATAFD].fd == NULL) {
2426 errstr = newstralloc(errstr, "[couldn't open DATA streams]");
2430 /* everything worked */
2431 *response_error = 0;
2435 errstr = newvstralloc(errstr,
2436 "[parse of reply message failed: ",
2437 extra ? extra : "(no additional information)",
2441 *response_error = 2;
2446 *response_error = 1;
2450 * This is called when everything needs to shut down so event_loop()
2454 stop_amidxtaped(void)
2458 for (i = 0; i < NSTREAMS; i++) {
2459 if (amidxtaped_streams[i].fd != NULL) {
2460 security_stream_close(amidxtaped_streams[i].fd);
2461 amidxtaped_streams[i].fd = NULL;
2466 static char* ctl_buffer = NULL;
2467 /* gets a "line" from server and put in server_line */
2468 /* server_line is terminated with \0, \r\n is striped */
2469 /* returns -1 if error */
2472 get_amidxtaped_line(void)
2478 amfree(amidxtaped_line);
2480 ctl_buffer = stralloc("");
2482 while (!strstr(ctl_buffer,"\r\n")) {
2483 size = security_stream_read_sync(amidxtaped_streams[CTLFD].fd, &buf);
2487 else if(size == 0) {
2490 newbuf = alloc(strlen(ctl_buffer)+size+1);
2491 strncpy(newbuf, ctl_buffer, (size_t)(strlen(ctl_buffer) + size + 1));
2492 memcpy(newbuf+strlen(ctl_buffer), buf, (size_t)size);
2493 newbuf[strlen(ctl_buffer)+size] = '\0';
2495 ctl_buffer = newbuf;
2498 s = strstr(ctl_buffer,"\r\n");
2500 newbuf = stralloc(s+2);
2501 amidxtaped_line = stralloc(ctl_buffer);
2503 ctl_buffer = newbuf;
2509 read_amidxtaped_data(
2516 assert(cookie != NULL);
2518 fd = *(int *)cookie;
2520 errstr = newstralloc2(errstr, "amidxtaped read: ",
2521 security_stream_geterror(amidxtaped_streams[DATAFD].fd));
2526 * EOF. Stop and return.
2529 security_stream_close(amidxtaped_streams[DATAFD].fd);
2530 amidxtaped_streams[DATAFD].fd = NULL;
2532 * If the mesg fd has also shut down, then we're done.
2537 assert(buf != NULL);
2540 * We ignore errors while writing to the index file.
2542 (void)fullwrite(fd, buf, (size_t)size);
2543 security_stream_read(amidxtaped_streams[DATAFD].fd, read_amidxtaped_data, cookie);
2547 amidxtaped_client_get_security_conf(
2551 (void)arg; /* Quiet unused parameter warning */
2553 if(!string || !*string)
2556 if(strcmp(string, "auth")==0) {
2557 return(getconf_str(CNF_AUTH));
2559 if(strcmp(string, "ssh_keys")==0) {
2560 return(getconf_str(CNF_SSH_KEYS));