Imported Debian patch 2.5.1p3-1
[debian/amanda] / recover-src / extract_list.c
index a38eec912a9c9e90e1dcc23f892947aae83ab547..4cd35b22e5e23cf701d7e8ff02f92f25fb426392 100644 (file)
@@ -24,7 +24,7 @@
  * file named AUTHORS, in the root directory of this distribution.
  */
 /*
- * $Id: extract_list.c,v 1.43.2.13.4.6.2.19 2003/03/04 21:13:58 martinea Exp $
+ * $Id: extract_list.c,v 1.117.2.2 2006/12/22 15:10:26 martinea Exp $
  *
  * implements the "extract" command in amrecover
  */
 #include "fileheader.h"
 #include "dgram.h"
 #include "stream.h"
+#include "tapelist.h"
 #ifdef SAMBA_CLIENT
 #include "findpass.h"
 #endif
-
-#if defined(KRB4_SECURITY)
-#include "krb4-security.h"
-#endif
 #include "util.h"
+#include "clientconf.h"
+#include "protocol.h"
+#include "event.h"
+#include "security.h"
 
-typedef struct EXTRACT_LIST_ITEM
-{
-    char path[1024];
-
+typedef struct EXTRACT_LIST_ITEM {
+    char *path;
     struct EXTRACT_LIST_ITEM *next;
 }
 EXTRACT_LIST_ITEM;
 
-typedef struct EXTRACT_LIST
-{
-    char date[11];                     /* date tape created */
-    int  level;                                /* level of dump */
-    char tape[256];                    /* tape label */
-    int fileno;                                /* fileno on tape */
-    EXTRACT_LIST_ITEM *files;          /* files to get off tape */
+typedef struct EXTRACT_LIST {
+    char *date;                        /* date tape created */
+    int  level;                        /* level of dump */
+    char *tape;                        /* tape label */
+    off_t fileno;              /* fileno on tape */
+    EXTRACT_LIST_ITEM *files;  /* files to get off tape */
 
     struct EXTRACT_LIST *next;
 }
@@ -67,15 +65,32 @@ EXTRACT_LIST;
 #define SKIP_TAPE 2
 #define RETRY_TAPE 3
 
-char *dump_device_name = NULL;
+static struct {
+    const char *name;
+    security_stream_t *fd;
+} amidxtaped_streams[] = {
+#define CTLFD  0
+    { "CTL", NULL },
+#define DATAFD  1
+    { "DATA", NULL },
+};
+#define NSTREAMS  (int)(sizeof(amidxtaped_streams) / sizeof(amidxtaped_streams[0]))
+
+
+static void amidxtaped_response(void *, pkt_t *, security_handle_t *);
+static void stop_amidxtaped(void);
+char *amidxtaped_client_get_security_conf(char *, void *);
+static char *dump_device_name = NULL;
+static char *errstr;
+static char *amidxtaped_line = NULL;
 
 extern char *localhost;
 
 /* global pid storage for interrupt handler */
 pid_t extract_restore_child_pid = -1;
 
-
 static EXTRACT_LIST *extract_list = NULL;
+static const security_driver_t *amidxtaped_secdrv;
 
 #ifdef SAMBA_CLIENT
 unsigned short samba_extract_method = SAMBA_TAR;
@@ -83,106 +98,165 @@ unsigned short samba_extract_method = SAMBA_TAR;
 
 #define READ_TIMEOUT   240*60
 
-static int okay_to_continue P((int, int,  int));
+EXTRACT_LIST *first_tape_list(void);
+EXTRACT_LIST *next_tape_list(EXTRACT_LIST *list);
+static int is_empty_dir(char *fname);
+int is_extract_list_nonempty(void);
+int length_of_tape_list(EXTRACT_LIST *tape_list);
+void add_file(char *path, char *regex);
+void add_glob(char *glob);
+void add_regex(char *regex);
+void clear_extract_list(void);
+void clean_tape_list(EXTRACT_LIST *tape_list);
+void clean_extract_list(void);
+void check_file_overwrite(char *filename);
+void delete_file(char *path, char *regex);
+void delete_glob(char *glob);
+void delete_regex(char *regex);
+void delete_tape_list(EXTRACT_LIST *tape_list);
+void display_extract_list(char *file);
+void extract_files(void);
+void read_file_header(char *buffer,
+                       dumpfile_t *file,
+                       size_t buflen,
+                       int tapedev);
+static int add_extract_item(DIR_ITEM *ditem);
+static int delete_extract_item(DIR_ITEM *ditem);
+static int extract_files_setup(char *label, off_t fsf);
+static int okay_to_continue(int allow_tape,
+                       int allow_skip,
+                       int allow_retry);
+static ssize_t read_buffer(int datafd,
+                       char *buffer,
+                       size_t buflen,
+                       long timeout_s);
+static void clear_tape_list(EXTRACT_LIST *tape_list);
+static void extract_files_child(int in_fd, EXTRACT_LIST *elist);
+static void send_to_tape_server(security_stream_t *stream, char *cmd);
+int writer_intermediary(EXTRACT_LIST *elist);
+int get_amidxtaped_line(void);
+static void read_amidxtaped_data(void *, void *, ssize_t);
+
+/*
+ * Function:  ssize_t read_buffer(datafd, buffer, buflen, timeout_s)
+ *
+ * Description:
+ *     read data from input file desciptor waiting up to timeout_s
+ *     seconds before returning data.
+ *
+ * Inputs:
+ *     datafd    - File descriptor to read from.
+ *     buffer    - Buffer to read into.
+ *     buflen    - Maximum number of bytes to read into buffer.
+ *     timeout_s - Seconds to wait before returning what was already read.
+ *
+ * Returns:
+ *      >0       - Number of data bytes in buffer.
+ *       0       - EOF
+ *      -1        - errno == ETIMEDOUT if no data available in specified time.
+ *                  errno == ENFILE if datafd is invalid.
+ *                  otherwise errno is set by select or read..
+ */
 
-ssize_t read_buffer(datafd, buffer, buflen)
-int datafd;
-char *buffer;
-size_t buflen;
+static ssize_t
+read_buffer(
+    int                datafd,
+    char *     buffer,
+    size_t     buflen,
+    long       timeout_s)
 {
-    int maxfd, nfound = 0;
     ssize_t size = 0;
-    fd_set readset, selectset;
+    fd_set readset;
     struct timeval timeout;
     char *dataptr;
-    size_t spaceleft;
-    int eof;
+    ssize_t spaceleft;
+    int nfound;
 
-    if(datafd < 0 || datafd >= FD_SETSIZE) {
+    if(datafd < 0 || datafd >= (int)FD_SETSIZE) {
        errno = EMFILE;                                 /* out of range */
        return -1;
     }
 
     dataptr = buffer;
-    spaceleft = buflen;
-
-    maxfd = datafd + 1;
-    eof = 0;
-
-    FD_ZERO(&readset);
-    FD_SET(datafd, &readset);
+    spaceleft = (ssize_t)buflen;
 
     do {
+        FD_ZERO(&readset);
+        FD_SET(datafd, &readset);
+        timeout.tv_sec = timeout_s;
+        timeout.tv_usec = 0;
+        nfound = select(datafd+1, &readset, NULL, NULL, &timeout);
+        if(nfound < 0 ) {
+            /* Select returned an error. */
+           fprintf(stderr,"select error: %s\n", strerror(errno));
+            size = -1;
+           break;
+        }
+
+       if (nfound == 0) {
+            /* Select timed out. */
+            if (timeout_s != 0)  {
+                /* Not polling: a real read timeout */
+                fprintf(stderr,"timeout waiting for restore\n");
+                fprintf(stderr,"increase READ_TIMEOUT in recover-src/extract_list.c if your tape is slow\n");
+            }
+            errno = ETIMEDOUT;
+            size = -1;
+           break;
+        }
 
-       timeout.tv_sec = READ_TIMEOUT;
-       timeout.tv_usec = 0;
-       memcpy(&selectset, &readset, sizeof(fd_set));
-
-       nfound = select(maxfd, (SELECT_ARG_TYPE *)(&selectset), NULL, NULL, &timeout);
-
-       /* check for errors or timeout */
-
-       if(nfound == 0)  {
-           size=-2;
-           fprintf(stderr,"timeout waiting for amrestore\n");
-           fprintf(stderr,"increase READ_TIMEOUT in recover-src/extract_list.c if your tape is slow\n");
-       }
-       if(nfound == -1) {
-           size=-3;
-           fprintf(stderr,"nfound == -1\n");
-       }
-
-       /* read any data */
+       if(!FD_ISSET(datafd, &readset))
+           continue;
 
-       if(FD_ISSET(datafd, &selectset)) {
-           size = read(datafd, dataptr, spaceleft);
-           switch(size) {
-           case -1:
-               break;
-           case 0:
-               spaceleft -= size;
-               dataptr += size;
-               fprintf(stderr,
-                       "EOF, check amidxtaped.<timestamp>.debug file on %s.\n",
-                       tape_server_name);
-               break;
-           default:
-               spaceleft -= size;
-               dataptr += size;
-               break;
+        /* Select says data is available, so read it.  */
+        size = read(datafd, dataptr, (size_t)spaceleft);
+        if (size < 0) {
+           if ((errno == EINTR) || (errno == EAGAIN)) {
+               continue;
+           }
+           if (errno != EPIPE) {
+               fprintf(stderr, "read_buffer: read error - %s",
+                   strerror(errno));
+               break;
            }
+           size = 0;
        }
-    } while (spaceleft>0 && size>0);
+        spaceleft -= size;
+        dataptr += size;
+    } while ((size > 0) && (spaceleft > 0));
 
-    if(size<0) {
-       return -1;
-    }
-    return (ssize_t)(buflen-spaceleft);
+    return ((((ssize_t)buflen-spaceleft) > 0) ? ((ssize_t)buflen-spaceleft) : size);
 }
 
-EXTRACT_LIST *first_tape_list P((void))
+
+EXTRACT_LIST *
+first_tape_list(void)
 {
     return extract_list;
 }
 
-EXTRACT_LIST *next_tape_list(list)
-EXTRACT_LIST *list;
+EXTRACT_LIST *
+next_tape_list(
+    /*@keep@*/EXTRACT_LIST *list)
 {
     if (list == NULL)
        return NULL;
     return list->next;
 }
 
-static void clear_tape_list(tape_list)
-EXTRACT_LIST *tape_list;
+static void
+clear_tape_list(
+    EXTRACT_LIST *     tape_list)
 {
     EXTRACT_LIST_ITEM *this, *next;
+    
 
     this = tape_list->files;
     while (this != NULL)
     {
        next = this->next;
-       free(this);
+        amfree(this->path);
+        amfree(this);
        this = next;
     }
     tape_list->files = NULL;
@@ -191,16 +265,22 @@ EXTRACT_LIST *tape_list;
 
 /* remove a tape list from the extract list, clearing the tape list
    beforehand if necessary */
-void delete_tape_list(tape_list)
-EXTRACT_LIST *tape_list;
+void
+delete_tape_list(
+    EXTRACT_LIST *tape_list)
 {
     EXTRACT_LIST *this, *prev;
 
+    if (tape_list == NULL)
+        return;
+
     /* is it first on the list? */
     if (tape_list == extract_list)
     {
-       clear_tape_list(tape_list);
        extract_list = tape_list->next;
+       clear_tape_list(tape_list);
+        amfree(tape_list->date);
+        amfree(tape_list->tape);
        amfree(tape_list);
        return;
     }
@@ -212,8 +292,10 @@ EXTRACT_LIST *tape_list;
     {
        if (this == tape_list)
        {
-           clear_tape_list(tape_list);
            prev->next = tape_list->next;
+           clear_tape_list(tape_list);
+            amfree(tape_list->date);
+            amfree(tape_list->tape);
            amfree(tape_list);
            return;
        }
@@ -225,8 +307,9 @@ EXTRACT_LIST *tape_list;
 
 
 /* return the number of files on a tape's list */
-int length_of_tape_list(tape_list)
-EXTRACT_LIST *tape_list;
+int
+length_of_tape_list(
+    EXTRACT_LIST *tape_list)
 {
     EXTRACT_LIST_ITEM *fn;
     int n;
@@ -239,18 +322,235 @@ EXTRACT_LIST *tape_list;
 }
 
 
-void clear_extract_list P((void))
+void
+clear_extract_list(void)
 {
     while (extract_list != NULL)
        delete_tape_list(extract_list);
 }
 
 
+void
+clean_tape_list(
+    EXTRACT_LIST *tape_list)
+{
+    EXTRACT_LIST_ITEM *fn1, *pfn1, *ofn1;
+    EXTRACT_LIST_ITEM *fn2, *pfn2, *ofn2;
+    int remove_fn1;
+    int remove_fn2;
+
+    pfn1 = NULL;
+    fn1 = tape_list->files;
+    while (fn1 != NULL) {
+       remove_fn1 = 0;
+
+       pfn2 = fn1;
+       fn2 = fn1->next;
+       while (fn2 != NULL && remove_fn1 == 0) {
+           remove_fn2 = 0;
+           if(strcmp(fn1->path, fn2->path) == 0) {
+               remove_fn2 = 1;
+           } else if (strncmp(fn1->path, fn2->path, strlen(fn1->path)) == 0 &&
+                      ((strlen(fn2->path) > strlen(fn1->path) &&
+                        fn2->path[strlen(fn1->path)] == '/') ||
+                      (fn1->path[strlen(fn1->path)-1] == '/'))) {
+               remove_fn2 = 1;
+           } else if (strncmp(fn2->path, fn1->path, strlen(fn2->path)) == 0 &&
+                      ((strlen(fn1->path) > strlen(fn2->path) &&
+                        fn1->path[strlen(fn2->path)] == '/')  ||
+                      (fn2->path[strlen(fn2->path)-1] == '/'))) {
+               remove_fn1 = 1;
+               break;
+           }
+
+           if (remove_fn2) {
+               dbprintf(("removing path %s, it is included in %s\n",
+                         fn2->path, fn1->path));
+               ofn2 = fn2;
+               fn2 = fn2->next;
+               amfree(ofn2->path);
+               amfree(ofn2);
+               pfn2->next = fn2;
+           } else if (remove_fn1 == 0) {
+               pfn2 = fn2;
+               fn2 = fn2->next;
+           }
+       }
+
+       if(remove_fn1 != 0) {
+           /* fn2->path is always valid */
+           /*@i@*/ dbprintf(("removing path %s, it is included in %s\n",
+           /*@i@*/           fn1->path, fn2->path));
+           ofn1 = fn1;
+           fn1 = fn1->next;
+           amfree(ofn1->path);
+           if(pfn1 == NULL) {
+               amfree(tape_list->files);
+               tape_list->files = fn1;
+           } else {
+               amfree(pfn1->next);
+               pfn1->next = fn1;
+           }
+       } else {
+           pfn1 = fn1;
+           fn1 = fn1->next;
+       }
+    }
+}
+
+
+void
+clean_extract_list(void)
+{
+    EXTRACT_LIST *this;
+
+    for (this = extract_list; this != NULL; this = this->next)
+       clean_tape_list(this);
+}
+
+
+int add_to_unlink_list(char *path);
+int do_unlink_list(void);
+void free_unlink_list(void);
+
+typedef struct s_unlink_list {
+    char *path;
+    struct s_unlink_list *next;
+} t_unlink_list;
+t_unlink_list *unlink_list = NULL;
+
+int
+add_to_unlink_list(
+    char *path)
+{
+    t_unlink_list *ul;
+
+    if (!unlink_list) {
+       unlink_list = alloc(SIZEOF(*unlink_list));
+       unlink_list->path = stralloc(path);
+       unlink_list->next = NULL;
+    } else {
+       for (ul = unlink_list; ul != NULL; ul = ul->next) {
+           if (strcmp(ul->path, path) == 0)
+               return 0;
+       }
+       ul = alloc(SIZEOF(*ul));
+       ul->path = stralloc(path);
+       ul->next = unlink_list;
+       unlink_list = ul;
+    }
+    return 1;
+}
+
+int
+do_unlink_list(void)
+{
+    t_unlink_list *ul;
+    int ret = 1;
+
+    for (ul = unlink_list; ul != NULL; ul = ul->next) {
+       if (unlink(ul->path) < 0) {
+           fprintf(stderr,"Can't unlink %s: %s\n", ul->path, strerror(errno));
+           ret = 0;
+       }
+    }
+    return ret;
+}
+
+
+void
+free_unlink_list(void)
+{
+    t_unlink_list *ul, *ul1;
+
+    for (ul = unlink_list; ul != NULL; ul = ul1) {
+       amfree(ul->path);
+       ul1 = ul->next;
+       amfree(ul);
+    }
+
+    unlink_list = NULL;
+}
+
+
+
+void
+check_file_overwrite(
+    char *dir)
+{
+    EXTRACT_LIST      *this;
+    EXTRACT_LIST_ITEM *fn;
+    struct stat        stat_buf;
+    char              *filename;
+    char              *path, *s;
+
+    for (this = extract_list; this != NULL; this = this->next) {
+       for (fn = this->files; fn != NULL ; fn = fn->next) {
+
+           /* Check path component of fn->path */
+
+           path = stralloc2(dir, fn->path);
+           if (path[strlen(path)-1] == '/') {
+               path[strlen(path)-1] = '\0';
+           }
+
+           s = path + strlen(dir) + 1;
+           while((s = strchr(s, '/'))) {
+               *s = '\0';
+               if (lstat(path, &stat_buf) == 0) {
+                   if(!S_ISDIR(stat_buf.st_mode)) {
+                       if (add_to_unlink_list(path)) {
+                           printf("WARNING: %s is not a directory, "
+                                  "it will be deleted.\n",
+                                  path);
+                       }
+                   }
+               }
+               else if (errno != ENOENT) {
+                   printf("Can't stat %s: %s\n", path, strerror(errno));
+               }
+               *s = '/';
+               s++;
+           }
+           amfree(path);
+
+           /* Check fn->path */
+
+           filename = stralloc2(dir, fn->path);
+           if (filename[strlen(filename)-1] == '/') {
+               filename[strlen(filename)-1] = '\0';
+           }
+
+           if (lstat(filename, &stat_buf) == 0) {
+               if(S_ISDIR(stat_buf.st_mode)) {
+                   if(!is_empty_dir(filename)) {
+                       printf("WARNING: All existing files in %s "
+                              "will be deleted.\n", filename);
+                   }
+               } else if(S_ISREG(stat_buf.st_mode)) {
+                   printf("WARNING: Existing file %s will be overwritten\n",
+                          filename);
+               } else {
+                   if (add_to_unlink_list(filename)) {
+                       printf("WARNING: Existing entry %s will be deleted\n",
+                              filename);
+                   }
+               }
+           } else if (errno != ENOENT) {
+               printf("Can't stat %s: %s\n", filename, strerror(errno));
+           }
+           amfree(filename);
+       }
+    }
+}
+
+
 /* returns -1 if error */
 /* returns  0 on succes */
 /* returns  1 if already added */
-static int add_extract_item(ditem)
-DIR_ITEM *ditem;
+static int
+add_extract_item(
+    DIR_ITEM *ditem)
 {
     EXTRACT_LIST *this, *this1;
     EXTRACT_LIST_ITEM *that, *curr;
@@ -275,8 +575,7 @@ DIR_ITEM *ditem;
                curr=curr->next;
            }
            that = (EXTRACT_LIST_ITEM *)alloc(sizeof(EXTRACT_LIST_ITEM));
-           strncpy(that->path, ditem_path, sizeof(that->path)-1);
-           that->path[sizeof(that->path)-1] = '\0';
+            that->path = stralloc(ditem_path);
            that->next = this->files;
            this->files = that;         /* add at front since easiest */
            amfree(ditem_path);
@@ -286,15 +585,12 @@ DIR_ITEM *ditem;
 
     /* so this is the first time we have seen this tape */
     this = (EXTRACT_LIST *)alloc(sizeof(EXTRACT_LIST));
-    strncpy(this->tape, ditem->tape, sizeof(this->tape)-1);
-    this->tape[sizeof(this->tape)-1] ='\0';
+    this->tape = stralloc(ditem->tape);
     this->level = ditem->level;
     this->fileno = ditem->fileno;
-    strncpy(this->date, ditem->date, sizeof(this->date)-1);
-    this->date[sizeof(this->date)-1] = '\0';
+    this->date = stralloc(ditem->date);
     that = (EXTRACT_LIST_ITEM *)alloc(sizeof(EXTRACT_LIST_ITEM));
-    strncpy(that->path, ditem_path, sizeof(that->path)-1);
-    that->path[sizeof(that->path)-1] = '\0';
+    that->path = stralloc(ditem_path);
     that->next = NULL;
     this->files = that;
 
@@ -330,8 +626,9 @@ DIR_ITEM *ditem;
 /* returns -1 if error */
 /* returns  0 on deletion */
 /* returns  1 if not there */
-static int delete_extract_item(ditem)
-DIR_ITEM *ditem;
+static int
+delete_extract_item(
+    DIR_ITEM *ditem)
 {
     EXTRACT_LIST *this;
     EXTRACT_LIST_ITEM *that, *prev;
@@ -351,6 +648,7 @@ DIR_ITEM *ditem;
            {
                /* first on list */
                this->files = that->next;
+                amfree(that->path);
                amfree(that);
                /* if list empty delete it */
                if (this->files == NULL)
@@ -365,6 +663,7 @@ DIR_ITEM *ditem;
                if (strcmp(that->path, ditem_path) == 0)
                {
                    prev->next = that->next;
+                    amfree(that->path);
                    amfree(that);
                    amfree(ditem_path);
                    return 0;
@@ -382,51 +681,58 @@ DIR_ITEM *ditem;
 }
 
 
-void add_glob(glob)
-char *glob;
+void
+add_glob(
+    char *     glob)
 {
     char *regex;
     char *regex_path;
     char *s;
-
-    regex = glob_to_regex(glob);
-    dbprintf(("add_glob (%s) -> %s\n", glob, regex));
+    char *uqglob = unquote_string(glob);
+    regex = glob_to_regex(uqglob);
+    dbprintf(("add_glob (%s) -> %s\n", uqglob, regex));
     if ((s = validate_regexp(regex)) != NULL) {
-       printf("\"%s\" is not a valid shell wildcard pattern: ", glob);
+       printf("%s is not a valid shell wildcard pattern: ", glob);
        puts(s);
-       return;
+    } else {
+        /*
+         * glob_to_regex() anchors the beginning of the pattern with ^,
+         * but we will be tacking it onto the end of the current directory
+         * in add_file, so strip that off.  Also, it anchors the end with
+         * $, but we need to match an optional trailing /, so tack that on
+         * the end.
+         */
+        regex_path = stralloc(regex + 1);
+        regex_path[strlen(regex_path) - 1] = '\0';
+        strappend(regex_path, "[/]*$");
+        add_file(uqglob, regex_path);
+        amfree(regex_path);
     }
-    /*
-     * glob_to_regex() anchors the beginning of the pattern with ^,
-     * but we will be tacking it onto the end of the current directory
-     * in add_file, so strip that off.  Also, it anchors the end with
-     * $, but we need to match an optional trailing /, so tack that on
-     * the end.
-     */
-    regex_path = stralloc(regex + 1);
     amfree(regex);
-    regex_path[strlen(regex_path) - 1] = '\0';
-    strappend(regex_path, "[/]*$");
-    add_file(glob, regex_path);
-    amfree(regex_path);
+    amfree(uqglob);
 }
 
-void add_regex(regex)
-char *regex;
+void
+add_regex(
+    char *     regex)
 {
     char *s;
-
-    if ((s = validate_regexp(regex)) != NULL) {
+    char *uqregex = unquote_string(regex);
+    if ((s = validate_regexp(uqregex)) != NULL) {
        printf("\"%s\" is not a valid regular expression: ", regex);
        puts(s);
-       return;
+    } else {
+        add_file(uqregex, regex);
     }
-    add_file(regex, regex);
+    amfree(uqregex);
 }
 
-void add_file(path, regex)
-char *path;
-char *regex;
+void
+add_file(
+    char *     path,
+    char *     regex)
 {
     DIR_ITEM *ditem, lditem;
     char *path_on_disk = NULL;
@@ -434,30 +740,47 @@ char *regex;
     char *cmd = NULL;
     char *err = NULL;
     int i;
-    int j;
+    ssize_t j;
     char *dir, *dir_undo, dir_undo_ch = '\0';
     char *ditem_path = NULL;
+    char *qditem_path = NULL;
     char *l = NULL;
     int  added;
-    char *s, *fp;
+    char *s, *fp, *quoted;
     int ch;
     int found_one;
+    int dir_entries;
 
     if (disk_path == NULL) {
        printf("Must select directory before adding files\n");
        return;
     }
+    memset(&lditem, 0, sizeof(lditem)); /* Prevent use of bogus data... */
 
     dbprintf(("add_file: Looking for \"%s\"\n", regex));
 
-    /* remove "/" at end of path */
-    j = strlen(regex)-1;
-    while(j >= 0 && regex[j] == '/') regex[j--] = '\0';
+    if(strcmp(regex, "/[/]*$") == 0) { /* "/" behave like "." */
+       regex = "\\.[/]*$";
+    }
+    else if(strcmp(regex, "[^/]*[/]*$") == 0) {                /* "*" */
+       regex = "([^/.]|\\.[^/]+|[^/.][^/]*)[/]*$";
+    } else {
+       /* remove "/" at end of path */
+       j = (ssize_t)(strlen(regex) - 1);
+       while(j >= 0 && regex[j] == '/')
+           regex[j--] = '\0';
+    }
 
     /* convert path (assumed in cwd) to one on disk */
-    if (strcmp(disk_path, "/") == 0)
-       path_on_disk = stralloc2("/", regex);
-    else {
+    if (strcmp(disk_path, "/") == 0) {
+        if (*regex == '/') {
+           /* No mods needed if already starts with '/' */
+           path_on_disk = stralloc(regex);
+       } else {
+           /* Prepend '/' */
+           path_on_disk = stralloc2("/", regex);
+       }
+    } else {
        char *clean_disk_path = clean_regex(disk_path);
        path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
        amfree(clean_disk_path);
@@ -469,22 +792,27 @@ char *regex;
              regex, path_on_disk));
 
     found_one = 0;
+    dir_entries = 0;
     for (ditem=get_dir_list(); ditem!=NULL; ditem=get_next_dir_item(ditem))
     {
-       dbprintf(("add_file: Pondering ditem->path=\"%s\"\n", ditem->path));
+       dir_entries++;
+       quoted = quote_string(ditem->path);
+       dbprintf(("add_file: Pondering ditem->path=%s\n", quoted));
+       amfree(quoted);
        if (match(path_on_disk, ditem->path)
            || match(path_on_disk_slash, ditem->path))
        {
            found_one = 1;
-           j = strlen(ditem->path);
+           j = (ssize_t)strlen(ditem->path);
            if((j > 0 && ditem->path[j-1] == '/')
               || (j > 1 && ditem->path[j-2] == '/' && ditem->path[j-1] == '.'))
            {   /* It is a directory */
-
                ditem_path = newstralloc(ditem_path, ditem->path);
                clean_pathname(ditem_path);
 
-               cmd = stralloc2("ORLD ", ditem_path);
+               qditem_path = quote_string(ditem_path);
+               cmd = stralloc2("ORLD ", qditem_path);
+               amfree(qditem_path);
                if(send_command(cmd) == -1) {
                    amfree(cmd);
                    amfree(ditem_path);
@@ -493,6 +821,7 @@ char *regex;
                    exit(1);
                }
                amfree(cmd);
+               cmd = NULL;
                /* skip preamble */
                if ((i = get_reply_line()) == -1) {
                    amfree(ditem_path);
@@ -500,8 +829,7 @@ char *regex;
                    amfree(path_on_disk_slash);
                    exit(1);
                }
-               if(i==0)                /* assume something wrong */
-               {
+               if(i==0) {              /* assume something wrong */
                    amfree(ditem_path);
                    amfree(path_on_disk);
                    amfree(path_on_disk_slash);
@@ -509,14 +837,12 @@ char *regex;
                    printf("%s\n", l);
                    return;
                }
-               amfree(err);
                dir_undo = NULL;
                added=0;
-               strncpy(lditem.path, ditem_path, sizeof(lditem.path)-1);
-               lditem.path[sizeof(lditem.path)-1] = '\0';
+                lditem.path = newstralloc(lditem.path, ditem->path);
                /* skip the last line -- duplicate of the preamble */
-               while ((i = get_reply_line()) != 0)
-               {
+
+               while ((i = get_reply_line()) != 0) {
                    if (i == -1) {
                        amfree(ditem_path);
                        amfree(path_on_disk);
@@ -550,11 +876,11 @@ char *regex;
                        err = "bad reply: missing date field";
                        continue;
                    }
-                   copy_string(s, ch, lditem.date, sizeof(lditem.date), fp);
-                   if(fp == NULL) {
-                       err = "bad reply: date field too large";
-                       continue;
-                   }
+                    fp = s-1;
+                    skip_non_whitespace(s, ch);
+                    s[-1] = '\0';
+                    lditem.date = newstralloc(lditem.date, fp);
+                    s[-1] = (char)ch;
 
                    skip_whitespace(s, ch);
                    if(ch == '\0' || sscanf(s - 1, "%d", &lditem.level) != 1) {
@@ -568,15 +894,16 @@ char *regex;
                        err = "bad reply: missing tape field";
                        continue;
                    }
-                   copy_string(s, ch, lditem.tape, sizeof(lditem.tape), fp);
-                   if(fp == NULL) {
-                       err = "bad reply: tape field too large";
-                       continue;
-                   }
+                    fp = s-1;
+                    skip_non_whitespace(s, ch);
+                    s[-1] = '\0';
+                    lditem.tape = newstralloc(lditem.tape, fp);
+                    s[-1] = (char)ch;
 
-                   if(am_has_feature(their_features, fe_amindexd_fileno_in_ORLD)) {
+                   if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_ORLD)) {
                        skip_whitespace(s, ch);
-                       if(ch == '\0' || sscanf(s - 1, "%d", &lditem.fileno) != 1) {
+                       if(ch == '\0' || sscanf(s - 1, OFF_T_FMT,
+                               (OFF_T_FMT_TYPE *)&lditem.fileno) != 1) {
                            err = "bad reply: cannot parse fileno field";
                            continue;
                        }
@@ -589,7 +916,7 @@ char *regex;
                        continue;
                    }
                    dir = s - 1;
-                   skip_non_whitespace(s, ch);
+                   skip_quoted_string(s, ch);
                    dir_undo = s - 1;
                    dir_undo_ch = *dir_undo;
                    *dir_undo = '\0';
@@ -599,13 +926,17 @@ char *regex;
                        printf("System error\n");
                        dbprintf(("add_file: (Failed) System error\n"));
                        break;
+
                    case  0:
+                       quoted = quote_string(lditem.path);
                        printf("Added dir %s at date %s\n",
-                              ditem_path, lditem.date);
+                              quoted, lditem.date);
                        dbprintf(("add_file: (Successful) Added dir %s at date %s\n",
-                                 ditem_path,lditem.date));
+                                 quoted, lditem.date));
+                       amfree(quoted);
                        added=1;
                        break;
+
                    case  1:
                        break;
                    }
@@ -613,13 +944,15 @@ char *regex;
                if(!server_happy()) {
                    puts(reply_line());
                } else if(err) {
-                   if(*err) {
+                   if (*err)
                        puts(err);
-                   }
-                   puts(cmd);
+                   if (cmd)
+                       puts(cmd);
                } else if(added == 0) {
-                   printf("dir %s already added\n", ditem_path);
-                   dbprintf(("add_file: dir %s already added\n", ditem_path));
+                   quoted = quote_string(ditem_path);
+                   printf("dir %s already added\n", quoted);
+                   dbprintf(("add_file: dir %s already added\n", quoted));
+                   amfree(quoted);
                }
            }
            else /* It is a file */
@@ -629,78 +962,95 @@ char *regex;
                    printf("System error\n");
                    dbprintf(("add_file: (Failed) System error\n"));
                    break;
+
                case  0:
-                   printf("Added %s\n", ditem->path);
-                   dbprintf(("add_file: (Successful) Added %s\n",
-                             ditem->path));
+                   quoted = quote_string(ditem->path);
+                   printf("Added file %s\n", quoted);
+                   dbprintf(("add_file: (Successful) Added %s\n", quoted));
+                   amfree(quoted);
                    break;
+
                case  1:
-                   printf("File %s already added\n", ditem->path);
-                   dbprintf(("add_file: file %s already added\n",
-                             ditem->path));
-                   break;
+                   quoted = quote_string(ditem->path);
+                   printf("File %s already added\n", quoted);
+                   dbprintf(("add_file: file %s already added\n", quoted));
+                   amfree(quoted);
                }
            }
        }
     }
+
     amfree(cmd);
     amfree(ditem_path);
     amfree(path_on_disk);
     amfree(path_on_disk_slash);
 
+    amfree(lditem.path);
+    amfree(lditem.date);
+    amfree(lditem.tape);
+
     if(! found_one) {
-       printf("File %s doesn't exist in directory\n", path);
+       quoted = quote_string(path);
+       printf("File %s doesn't exist in directory\n", quoted);
        dbprintf(("add_file: (Failed) File %s doesn't exist in directory\n",
-                 path));
+                 quoted));
+       amfree(quoted);
     }
 }
 
 
-void delete_glob(glob)
-char *glob;
+void
+delete_glob(
+    char *     glob)
 {
     char *regex;
     char *regex_path;
     char *s;
-
-    regex = glob_to_regex(glob);
-    dbprintf(("delete_glob (%s) -> %s\n", glob, regex));
+    char *uqglob = unquote_string(glob);
+    regex = glob_to_regex(uqglob);
+    dbprintf(("delete_glob (%s) -> %s\n", uqglob, regex));
     if ((s = validate_regexp(regex)) != NULL) {
        printf("\"%s\" is not a valid shell wildcard pattern: ", glob);
        puts(s);
-       return;
+    } else {
+        /*
+         * glob_to_regex() anchors the beginning of the pattern with ^,
+         * but we will be tacking it onto the end of the current directory
+         * in add_file, so strip that off.  Also, it anchors the end with
+         * $, but we need to match an optional trailing /, so tack that on
+         * the end.
+         */
+        regex_path = stralloc(regex + 1);
+        regex_path[strlen(regex_path) - 1] = '\0';
+        strappend(regex_path, "[/]*$");
+        delete_file(uqglob, regex_path);
+        amfree(regex_path);
     }
-    /*
-     * glob_to_regex() anchors the beginning of the pattern with ^,
-     * but we will be tacking it onto the end of the current directory
-     * in add_file, so strip that off.  Also, it anchors the end with
-     * $, but we need to match an optional trailing /, so tack that on
-     * the end.
-     */
-    regex_path = stralloc(regex + 1);
     amfree(regex);
-    regex_path[strlen(regex_path) - 1] = '\0';
-    strappend(regex_path, "[/]*$");
-    delete_file(glob, regex_path);
-    amfree(regex_path);
+    amfree(uqglob);
 }
 
-void delete_regex(regex)
-char *regex;
+void
+delete_regex(
+    char *     regex)
 {
     char *s;
+    char *uqregex = unquote_string(regex);
 
     if ((s = validate_regexp(regex)) != NULL) {
        printf("\"%s\" is not a valid regular expression: ", regex);
        puts(s);
-       return;
+    } else {
+       delete_file(uqregex, uqregex);
     }
-    delete_file(regex, regex);
+    amfree(uqregex);
 }
 
-void delete_file(path, regex)
-char *path;
-char *regex;
+void
+delete_file(
+    char *     path,
+    char *     regex)
 {
     DIR_ITEM *ditem, lditem;
     char *path_on_disk = NULL;
@@ -708,32 +1058,53 @@ char *regex;
     char *cmd = NULL;
     char *err = NULL;
     int i;
-    int j;
-    char *date, *date_undo, date_undo_ch = '\0';
+    ssize_t j;
+    char *date;
     char *tape, *tape_undo, tape_undo_ch = '\0';
-    char *dir, *dir_undo, dir_undo_ch = '\0';
-    int  level, fileno;
+    char *dir_undo, dir_undo_ch = '\0';
+    int  level = 0;
+    off_t fileno;
     char *ditem_path = NULL;
+    char *qditem_path;
     char *l = NULL;
     int  deleted;
     char *s;
     int ch;
     int found_one;
+    char *quoted;
 
     if (disk_path == NULL) {
        printf("Must select directory before deleting files\n");
        return;
     }
+    memset(&lditem, 0, sizeof(lditem)); /* Prevent use of bogus data... */
 
     dbprintf(("delete_file: Looking for \"%s\"\n", path));
-    /* remove "/" at the end of the path */
-    j = strlen(regex)-1;
-    while(j >= 0 && regex[j] == '/') regex[j--] = '\0';
+
+    if (strcmp(regex, "[^/]*[/]*$") == 0) {
+       /* Looking for * find everything but single . */
+       regex = "([^/.]|\\.[^/]+|[^/.][^/]*)[/]*$";
+    } else {
+       /* remove "/" at end of path */
+       j = (ssize_t)(strlen(regex) - 1);
+       while(j >= 0 && regex[j] == '/') regex[j--] = '\0';
+    }
 
     /* convert path (assumed in cwd) to one on disk */
-    if (strcmp(disk_path, "/") == 0)
-       path_on_disk = stralloc2("/", regex);
-    else {
+    if (strcmp(disk_path, "/") == 0) {
+        if (*regex == '/') {
+           if (strcmp(regex, "/[/]*$") == 0) {
+               /* We want "/" to match the directory itself: "/." */
+               path_on_disk = stralloc("/\\.[/]*$");
+           } else {
+               /* No mods needed if already starts with '/' */
+               path_on_disk = stralloc(regex);
+           }
+       } else {
+           /* Prepend '/' */
+           path_on_disk = stralloc2("/", regex);
+       }
+    } else {
        char *clean_disk_path = clean_regex(disk_path);
        path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
        amfree(clean_disk_path);
@@ -746,19 +1117,23 @@ char *regex;
     found_one = 0;
     for (ditem=get_dir_list(); ditem!=NULL; ditem=get_next_dir_item(ditem))
     {
-       dbprintf(("delete_file: Pondering ditem->path=\"%s\"\n", ditem->path));
+       quoted = quote_string(ditem->path);
+       dbprintf(("delete_file: Pondering ditem->path=%s\n", quoted));
+       amfree(quoted);
        if (match(path_on_disk, ditem->path)
            || match(path_on_disk_slash, ditem->path))
        {
            found_one = 1;
-           j = strlen(ditem->path);
+           j = (ssize_t)strlen(ditem->path);
            if((j > 0 && ditem->path[j-1] == '/')
               || (j > 1 && ditem->path[j-2] == '/' && ditem->path[j-1] == '.'))
            {   /* It is a directory */
                ditem_path = newstralloc(ditem_path, ditem->path);
                clean_pathname(ditem_path);
 
-               cmd = stralloc2("ORLD ", ditem_path);
+               qditem_path = quote_string(ditem_path);
+               cmd = stralloc2("ORLD ", qditem_path);
+               amfree(qditem_path);
                if(send_command(cmd) == -1) {
                    amfree(cmd);
                    amfree(ditem_path);
@@ -784,11 +1159,9 @@ char *regex;
                    return;
                }
                deleted=0;
-               strncpy(lditem.path, ditem->path, sizeof(lditem.path)-1);
-               lditem.path[sizeof(lditem.path)-1] = '\0';
+                lditem.path = newstralloc(lditem.path, ditem->path);
                amfree(cmd);
-               amfree(err);
-               date_undo = tape_undo = dir_undo = NULL;
+               tape_undo = dir_undo = NULL;
                /* skip the last line -- duplicate of the preamble */
                while ((i = get_reply_line()) != 0)
                {
@@ -802,7 +1175,7 @@ char *regex;
                        if(cmd == NULL) {
                            if(tape_undo) *tape_undo = tape_undo_ch;
                            if(dir_undo) *dir_undo = dir_undo_ch;
-                           date_undo = tape_undo = dir_undo = NULL;
+                           tape_undo = dir_undo = NULL;
                            cmd = stralloc(l);  /* save for the error report */
                        }
                        continue;       /* throw the rest of the lines away */
@@ -827,9 +1200,7 @@ char *regex;
                    }
                    date = s - 1;
                    skip_non_whitespace(s, ch);
-                   date_undo = s - 1;
-                   date_undo_ch = *date_undo;
-                   *date_undo = '\0';
+                   *(s - 1) = '\0';
 
                    skip_whitespace(s, ch);
                    if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
@@ -849,9 +1220,10 @@ char *regex;
                    tape_undo_ch = *tape_undo;
                    *tape_undo = '\0';
 
-                   if(am_has_feature(their_features, fe_amindexd_fileno_in_ORLD)) {
+                   if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_ORLD)) {
                        skip_whitespace(s, ch);
-                       if(ch == '\0' || sscanf(s - 1, "%d", &fileno) != 1) {
+                       if(ch == '\0' || sscanf(s - 1, OFF_T_FMT,
+                               (OFF_T_FMT_TYPE *)&fileno) != 1) {
                            err = "bad reply: cannot parse fileno field";
                            continue;
                        }
@@ -863,17 +1235,14 @@ char *regex;
                        err = "bad reply: missing directory field";
                        continue;
                    }
-                   dir = s - 1;
                    skip_non_whitespace(s, ch);
                    dir_undo = s - 1;
                    dir_undo_ch = *dir_undo;
                    *dir_undo = '\0';
 
-                   strncpy(lditem.date, date, sizeof(lditem.date)-1);
-                   lditem.date[sizeof(lditem.date)-1] = '\0';
+                    lditem.date = newstralloc(lditem.date, date);
                    lditem.level=level;
-                   strncpy(lditem.tape, tape, sizeof(lditem.tape)-1);
-                   lditem.tape[sizeof(lditem.tape)-1] = '\0';
+                    lditem.tape = newstralloc(lditem.tape, tape);
                    switch(delete_extract_item(&lditem)) {
                    case -1:
                        printf("System error\n");
@@ -892,10 +1261,10 @@ char *regex;
                if(!server_happy()) {
                    puts(reply_line());
                } else if(err) {
-                   if(*err) {
+                   if (*err)
                        puts(err);
-                   }
-                   puts(cmd);
+                   if (cmd)
+                       puts(cmd);
                } else if(deleted == 0) {
                    printf("Warning - dir '%s' not on tape list\n",
                           ditem_path);
@@ -939,14 +1308,16 @@ char *regex;
 
 
 /* print extract list into file. If NULL ptr passed print to screen */
-void display_extract_list(file)
-char *file;
+void
+display_extract_list(
+    char *     file)
 {
     EXTRACT_LIST *this;
     EXTRACT_LIST_ITEM *that;
     FILE *fp;
     char *pager;
     char *pager_command;
+    char *uqfile;
 
     if (file == NULL)
     {
@@ -968,11 +1339,14 @@ char *file;
     }
     else
     {
-       if ((fp = fopen(file, "w")) == NULL)
+       uqfile = unquote_string(file);
+       if ((fp = fopen(uqfile, "w")) == NULL)
        {
-           printf("Can't open file '%s' to print extract list into\n", file);
+           printf("Can't open file %s to print extract list into\n", file);
+           amfree(uqfile);
            return;
        }
+       amfree(uqfile);
     }
 
     for (this = extract_list; this != NULL; this = this->next)
@@ -992,8 +1366,30 @@ char *file;
 }
 
 
+static int
+is_empty_dir(
+    char *fname)
+{
+    DIR *dir;
+    struct dirent *entry;
+    int gotentry;
+
+    if((dir = opendir(fname)) == NULL)
+        return 1;
+
+    gotentry = 0;
+    while(!gotentry && (entry = readdir(dir)) != NULL) {
+        gotentry = !is_dot_or_dotdot(entry->d_name);
+    }
+
+    closedir(dir);
+    return !gotentry;
+
+}
+
 /* returns 0 if extract list empty and 1 if it isn't */
-int is_extract_list_nonempty P((void))
+int
+is_extract_list_nonempty(void)
 {
     return (extract_list != NULL);
 }
@@ -1001,10 +1397,11 @@ int is_extract_list_nonempty P((void))
 
 /* prints continue prompt and waits for response,
    returns 0 if don't, non-0 if do */
-static int okay_to_continue(allow_tape, allow_skip, allow_retry)
-    int allow_tape;
-    int allow_skip;
-    int allow_retry;
+static int
+okay_to_continue(
+    int        allow_tape,
+    int        allow_skip,
+    int        allow_retry)
 {
     int ch;
     int ret = -1;
@@ -1040,7 +1437,9 @@ static int okay_to_continue(allow_tape, allow_skip, allow_retry)
            break;
        }
        s = line;
-       while ((ch = *s++) != '\0' && isspace(ch)) {}
+       while ((ch = *s++) != '\0' && isspace(ch)) {
+           (void)ch;   /* Quiet empty loop compiler warning */
+       }
        if (ch == '?') {
            if (get_tape) {
                printf("Enter a new device ([host:]device) or \"default\"\n");
@@ -1072,95 +1471,65 @@ static int okay_to_continue(allow_tape, allow_skip, allow_retry)
            ret = SKIP_TAPE;
        }
     }
+    /*@ignore@*/
     amfree(line);
+    /*@end@*/
     return ret;
 }
 
-static void send_to_tape_server(tss, cmd)
-int tss;
-char *cmd;
+static void
+send_to_tape_server(
+    security_stream_t *        stream,
+    char *             cmd)
 {
-    size_t l, n;
-    ssize_t s;
-    char *end;
+    char *msg = stralloc2(cmd, "\r\n");
 
-    for (l = 0, n = strlen(cmd); l < n; l += s)
-       if ((s = write(tss, cmd + l, n - l)) < 0)
-       {
-           perror("Error writing to tape server");
-           exit(101);
-       }
-    end = "\r\n";
-    for (l = 0, n = strlen(end); l < n; l += s)
-       if ((s = write(tss, end + l, n - l)) < 0)
-       {
-           perror("Error writing to tape server");
-           exit(101);
-       }
+    if (security_stream_write(stream, msg, strlen(msg)) < 0)
+    {
+       error("Error writing to tape server");
+       exit(101);
+       /*NOTREACHED*/
+    }
+    amfree(msg);
 }
 
 
 /* start up connection to tape server and set commands to initiate
    transfer of dump image.
    Return tape server socket on success, -1 on error. */
-static int extract_files_setup(label, fsf)
-char *label;
-int fsf;
+static int
+extract_files_setup(
+    char *     label,
+    off_t      fsf)
 {
-    struct servent *sp;
-    int my_port;
-    int tape_server_socket;
     char *disk_regex = NULL;
     char *host_regex = NULL;
-    char *service_name = NULL;
-    char *line = NULL;
     char *clean_datestamp, *ch, *ch1;
-
-    service_name = stralloc2("amidxtape", SERVICE_SUFFIX);
-
-    /* get tape server details */
-    if ((sp = getservbyname(service_name, "tcp")) == NULL)
-    {
-       printf("%s/tcp unknown protocol - config error?\n", service_name);
-       amfree(service_name);
-       return -1;
+    char *tt = NULL;
+    char *req;
+    int response_error;
+
+    amidxtaped_secdrv = security_getdriver(authopt);
+    if (amidxtaped_secdrv == NULL) {
+       error("no '%s' security driver available for host '%s'",
+             authopt, tape_server_name);
     }
-    amfree(service_name);
-    seteuid(0);                                        /* it either works ... */
-    setegid(0);
-    tape_server_socket = stream_client_privileged(tape_server_name,
-                                                 ntohs(sp->s_port),
-                                                 -1,
-                                                 STREAM_BUFSIZE,
-                                                 &my_port);
-    if (tape_server_socket < 0)
-    {
-       printf("cannot connect to %s: %s\n", tape_server_name, strerror(errno));
-       return -1;
-    }
-    if (my_port >= IPPORT_RESERVED) {
-       aclose(tape_server_socket);
-       printf("did not get a reserved port: %d\n", my_port);
-       return -1;
-    }
-    setegid(getgid());
-    seteuid(getuid());                         /* put it back */
 
-    /* do the security thing */
-#if defined(KRB4_SECURITY)
-#if 0 /* not yet implemented */
-    if(krb4_auth)
-    {
-       line = get_krb_security();
-    }
-#endif /* 0 */
-#endif
-    {
-       line = get_bsd_security();
+    /* We assume that amidxtaped support fe_amidxtaped_options_features */
+    /*                               and fe_amidxtaped_options_auth     */
+    /* We should send a noop to really know                             */
+    req = vstralloc("SERVICE amidxtaped\n",
+                   "OPTIONS ", "features=", our_features_string, ";",
+                               "auth=", authopt, ";",
+                   "\n", NULL);
+    protocol_sendreq(tape_server_name, amidxtaped_secdrv,
+                    amidxtaped_client_get_security_conf, req, STARTUP_TIMEOUT,
+                    amidxtaped_response, &response_error);
+    amfree(req);
+    protocol_run();
+    if(response_error != 0) {
+       return -1;
     }
-    send_to_tape_server(tape_server_socket, line);
-    memset(line, '\0', strlen(line));
-    amfree(line);
 
     disk_regex = alloc(strlen(disk_name) * 2 + 3);
 
@@ -1218,46 +1587,61 @@ int fsf;
        }
     }
     *ch = '\0';
+    /* push our feature list off to the tape server */
+    /* XXX assumes that index server and tape server are equivalent, ew */
+
+    if(am_has_feature(indexsrv_features, fe_amidxtaped_exchange_features)){
+       tt = newstralloc2(tt, "FEATURES=", our_features_string);
+       send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
+       get_amidxtaped_line();
+       if(strncmp(amidxtaped_line,"FEATURES=",9) == 0) {
+           tapesrv_features = am_string_to_feature(amidxtaped_line+9);
+       } else {
+           fprintf(stderr, "amrecover - expecting FEATURES line from amidxtaped\n");
+           stop_amidxtaped();
+           amfree(disk_regex);
+           amfree(host_regex);
+           amfree(clean_datestamp);
+           return -1;
+       }
+       am_release_feature_set(tapesrv_features);
+    }
 
-    if(am_has_feature(their_features, fe_amidxtaped_header) &&
-       am_has_feature(their_features, fe_amidxtaped_device) &&
-       am_has_feature(their_features, fe_amidxtaped_host) &&
-       am_has_feature(their_features, fe_amidxtaped_disk) &&
-       am_has_feature(their_features, fe_amidxtaped_datestamp)) {
 
-       char *tt = NULL;
+    if(am_has_feature(indexsrv_features, fe_amidxtaped_header) &&
+       am_has_feature(indexsrv_features, fe_amidxtaped_device) &&
+       am_has_feature(indexsrv_features, fe_amidxtaped_host) &&
+       am_has_feature(indexsrv_features, fe_amidxtaped_disk) &&
+       am_has_feature(indexsrv_features, fe_amidxtaped_datestamp)) {
 
-       if(am_has_feature(their_features, fe_amidxtaped_config)) {
+       if(am_has_feature(indexsrv_features, fe_amidxtaped_config)) {
            tt = newstralloc2(tt, "CONFIG=", config);
-           send_to_tape_server(tape_server_socket, tt);
+           send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
        }
-       if(am_has_feature(their_features, fe_amidxtaped_label) &&
+       if(am_has_feature(indexsrv_features, fe_amidxtaped_label) &&
           label && label[0] != '/') {
            tt = newstralloc2(tt,"LABEL=",label);
-           send_to_tape_server(tape_server_socket, tt);
+           send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
        }
-       if(am_has_feature(their_features, fe_amidxtaped_fsf)) {
+       if(am_has_feature(indexsrv_features, fe_amidxtaped_fsf)) {
            char v_fsf[100];
-           ap_snprintf(v_fsf, 99, "%d", fsf);
+           snprintf(v_fsf, 99, OFF_T_FMT, (OFF_T_FMT_TYPE)fsf);
            tt = newstralloc2(tt, "FSF=",v_fsf);
-           send_to_tape_server(tape_server_socket, tt);
+           send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
        }
-       send_to_tape_server(tape_server_socket, "HEADER");
+       send_to_tape_server(amidxtaped_streams[CTLFD].fd, "HEADER");
        tt = newstralloc2(tt, "DEVICE=", dump_device_name);
-       send_to_tape_server(tape_server_socket, tt);
+       send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
        tt = newstralloc2(tt, "HOST=", host_regex);
-       send_to_tape_server(tape_server_socket, tt);
+       send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
        tt = newstralloc2(tt, "DISK=", disk_regex);
-       send_to_tape_server(tape_server_socket, tt);
+       send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
        tt = newstralloc2(tt, "DATESTAMP=", clean_datestamp);
-       send_to_tape_server(tape_server_socket, tt);
-       send_to_tape_server(tape_server_socket, "END");
+       send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
+       send_to_tape_server(amidxtaped_streams[CTLFD].fd, "END");
        amfree(tt);
     }
-    else if(1 /* am_has_feature(their_features, fe_amidxtaped_nargs) */) {
-       /* 2.4.3 doesn't set fe_amidxtaped_nargs but support it */
-       /* must be supported without test until 2005 */
-
+    else if(am_has_feature(indexsrv_features, fe_amidxtaped_nargs)) {
        /* send to the tape server what tape file we want */
        /* 6 args:
         *   "-h"
@@ -1267,13 +1651,13 @@ int fsf;
         *   "diskname"
         *   "datestamp"
         */
-       send_to_tape_server(tape_server_socket, "6");
-       send_to_tape_server(tape_server_socket, "-h");
-       send_to_tape_server(tape_server_socket, "-p");
-       send_to_tape_server(tape_server_socket, dump_device_name);
-       send_to_tape_server(tape_server_socket, host_regex);
-       send_to_tape_server(tape_server_socket, disk_regex);
-       send_to_tape_server(tape_server_socket, clean_datestamp);
+       send_to_tape_server(amidxtaped_streams[CTLFD].fd, "6");
+       send_to_tape_server(amidxtaped_streams[CTLFD].fd, "-h");
+       send_to_tape_server(amidxtaped_streams[CTLFD].fd, "-p");
+       send_to_tape_server(amidxtaped_streams[CTLFD].fd, dump_device_name);
+       send_to_tape_server(amidxtaped_streams[CTLFD].fd, host_regex);
+       send_to_tape_server(amidxtaped_streams[CTLFD].fd, disk_regex);
+       send_to_tape_server(amidxtaped_streams[CTLFD].fd, clean_datestamp);
 
        dbprintf(("Started amidxtaped with arguments \"6 -h -p %s %s %s %s\"\n",
                  dump_device_name, host_regex, disk_regex, clean_datestamp));
@@ -1283,42 +1667,54 @@ int fsf;
     amfree(host_regex);
     amfree(clean_datestamp);
 
-    return tape_server_socket;
+    return 0;
 }
 
 
-size_t read_file_header(buffer, file, buflen, tapedev)
-char *buffer;
-dumpfile_t *file;
-size_t buflen;
-int tapedev;
 /*
  * Reads the first block of a tape file.
  */
+
+void
+read_file_header(
+    char *     buffer,
+    dumpfile_t *file,
+    size_t     buflen,
+    int                tapedev)
 {
     ssize_t bytes_read;
-    bytes_read=read_buffer(tapedev,buffer,buflen);
+    bytes_read = read_buffer(tapedev, buffer, buflen, READ_TIMEOUT);
     if(bytes_read < 0) {
-       error("error reading tape: %s", strerror(errno));
+       error("error reading header (%s), check amidxtaped.*.debug on server",
+             strerror(errno));
+       /*NOTREACHED*/
     }
-    else if((size_t)bytes_read < buflen) {
+
+    if((size_t)bytes_read < buflen) {
        fprintf(stderr, "%s: short block %d byte%s\n",
                get_pname(), (int)bytes_read, (bytes_read == 1) ? "" : "s");
        print_header(stdout, file);
        error("Can't read file header");
+       /*NOTREACHED*/
     }
-    else { /* bytes_read == buflen */
-       parse_file_header(buffer, file, bytes_read);
-    }
-    return((size_t)bytes_read);
-}
 
-enum dumptypes {IS_UNKNOWN, IS_DUMP, IS_GNUTAR, IS_TAR, IS_SAMBA, IS_SAMBA_TAR};
+    /* bytes_read == buflen */
+    parse_file_header(buffer, file, (size_t)bytes_read);
+}
 
-/* exec restore to do the actual restoration */
-static void extract_files_child(in_fd, elist)
-    int in_fd;
-    EXTRACT_LIST *elist;
+enum dumptypes {
+       IS_UNKNOWN,
+       IS_DUMP,
+       IS_GNUTAR,
+       IS_TAR,
+       IS_SAMBA,
+       IS_SAMBA_TAR
+};
+
+static void
+extract_files_child(
+    int                        in_fd,
+    EXTRACT_LIST *     elist)
 {
     int save_errno;
     int extra_params = 0;
@@ -1329,7 +1725,6 @@ static void extract_files_child(in_fd, elist)
     enum dumptypes dumptype = IS_UNKNOWN;
     char buffer[DISK_BLOCK_BYTES];
     dumpfile_t file;
-    size_t buflen;
     size_t len_program;
     char *cmd = NULL;
     int passwd_field = -1;
@@ -1343,17 +1738,18 @@ static void extract_files_child(in_fd, elist)
     /* make in_fd be our stdin */
     if (dup2(in_fd, STDIN_FILENO) == -1)
     {
-       perror("extract_list - extract files client");
-       exit(1);
+       error("dup2 failed in extract_files_child: %s", strerror(errno));
+       /*NOTREACHED*/
     }
 
     /* read the file header */
     fh_init(&file);
-    buflen=read_file_header(buffer, &file, sizeof(buffer), STDIN_FILENO);
+    read_file_header(buffer, &file, sizeof(buffer), STDIN_FILENO);
 
-    if(buflen == 0 || file.type != F_DUMPFILE) {
+    if(file.type != F_DUMPFILE) {
        print_header(stdout, &file);
        error("bad header");
+       /*NOTREACHED*/
     }
 
     if (file.program != NULL) {
@@ -1389,6 +1785,8 @@ static void extract_files_child(in_fd, elist)
 #endif
     case IS_TAR:
     case IS_GNUTAR:
+        extra_params = 4;
+        break;
     case IS_SAMBA_TAR:
         extra_params = 3;
         break;
@@ -1409,8 +1807,8 @@ static void extract_files_child(in_fd, elist)
        break;
     }
 
-    restore_args = (char **)alloc((extra_params + files_off_tape + 1)
-                                 * sizeof(char *));
+    restore_args = (char **)alloc((size_t)((extra_params + files_off_tape + 1)
+                                 * sizeof(char *)));
     switch(dumptype) {
     case IS_SAMBA:
 #ifdef SAMBA_CLIENT
@@ -1436,6 +1834,7 @@ static void extract_files_child(in_fd, elist)
     case IS_TAR:
     case IS_GNUTAR:
        restore_args[j++] = stralloc("tar");
+       restore_args[j++] = stralloc("--numeric-owner");
        restore_args[j++] = stralloc("-xpGvf");
        restore_args[j++] = stralloc("-");      /* data on stdin */
        break;
@@ -1477,7 +1876,10 @@ static void extract_files_child(in_fd, elist)
        case IS_GNUTAR:
        case IS_SAMBA_TAR:
        case IS_SAMBA:
-           restore_args[j++] = stralloc2(".", fn->path);
+           if (strcmp(fn->path, "/") == 0)
+               restore_args[j++] = stralloc(".");
+           else
+               restore_args[j++] = stralloc2(".", fn->path);
            break;
        case IS_UNKNOWN:
        case IS_DUMP:
@@ -1575,32 +1977,119 @@ static void extract_files_child(in_fd, elist)
     /*NOT REACHED */
 }
 
+/*
+ * Interpose something between the process writing out the dump (writing it to
+ * some extraction program, really) and the socket from which we're reading, so
+ * that we can do things like prompt for human interaction for multiple tapes.
+ */
+int
+writer_intermediary(
+    EXTRACT_LIST *     elist)
+{
+    int child_pipe[2];
+    pid_t pid;
+    amwait_t extractor_status;
+
+    if(pipe(child_pipe) == -1) {
+       error("extract_list - error setting up pipe to extractor: %s\n",
+             strerror(errno));
+       /*NOTREACHED*/
+    }
+
+    /* okay, ready to extract. fork a child to do the actual work */
+    if ((pid = fork()) == 0) {
+       /* this is the child process */
+       /* never gets out of this clause */
+       aclose(child_pipe[1]);
+        extract_files_child(child_pipe[0], elist);
+       /*NOTREACHED*/
+    }
+
+    /* This is the parent */
+    if (pid == -1) {
+       printf("writer_intermediary - error forking child");
+       return -1;
+    }
+
+    aclose(child_pipe[0]);
+
+    security_stream_read(amidxtaped_streams[DATAFD].fd,
+                        read_amidxtaped_data, &(child_pipe[1]));
+
+    while(get_amidxtaped_line() >= 0) {
+       char desired_tape[MAX_TAPE_LABEL_BUF];
+                
+       /* if prompted for a tape, relay said prompt to the user */
+       if(sscanf(amidxtaped_line, "FEEDME %132s\n", desired_tape) == 1) {
+           int done;
+           printf("Load tape %s now\n", desired_tape);
+           done = okay_to_continue(am_has_feature(indexsrv_features,
+                                                  fe_amrecover_feedme_tape),
+                                   0, 0);
+           if (done == 1) {
+               if (am_has_feature(indexsrv_features,
+                                  fe_amrecover_feedme_tape)) {
+                   char *reply = stralloc2("TAPE ", tape_device_name);
+                   send_to_tape_server(amidxtaped_streams[CTLFD].fd, reply);
+                   amfree(reply);
+               } else {
+                   send_to_tape_server(amidxtaped_streams[CTLFD].fd, "OK");
+               }
+           } else {
+               send_to_tape_server(amidxtaped_streams[CTLFD].fd, "ERROR");
+               break;
+           }
+       } else if(strncmp(amidxtaped_line, "MESSAGE ", 8) == 0) {
+           printf("%s\n",&amidxtaped_line[8]);
+       } else {
+           fprintf(stderr, "Strange message from tape server: %s",
+                   amidxtaped_line);
+           break;
+       }
+    }
+
+    /* CTL might be close before DATA */
+    event_loop(0);
+    aclose(child_pipe[1]);
+
+    waitpid(pid, &extractor_status, 0);
+    if(WEXITSTATUS(extractor_status) != 0){
+       int ret = WEXITSTATUS(extractor_status);
+        if(ret == 255) ret = -1;
+       printf("Extractor child exited with status %d\n", ret);
+       return -1;
+    }
+    return(0);
+}
+
+/* exec restore to do the actual restoration */
 
 /* does the actual extraction of files */
-/* The original design had the dump image being returned exactly as it
-   appears on the tape, and this routine getting from the index server
-   whether or not it is compressed, on the assumption that the tape
-   server may not know how to uncompress it. But
-   - Amrestore can't do that. It returns either compressed or uncompressed
-   (always). Amrestore assumes it can uncompress files. It is thus a good
-   idea to run the tape server on a machine with gzip.
-   - The information about compression in the disklist is really only
-   for future dumps. It is possible to change compression on a drive
-   so the information in the disklist may not necessarily relate to
-   the dump image on the tape.
-     Consequently the design was changed to assuming that amrestore can
-   uncompress any dump image and have it return an uncompressed file
-   always. */
-void extract_files P((void))
+/*
+ * The original design had the dump image being returned exactly as it
+ * appears on the tape, and this routine getting from the index server
+ * whether or not it is compressed, on the assumption that the tape
+ * server may not know how to uncompress it. But
+ * - Amrestore can't do that. It returns either compressed or uncompressed
+ * (always). Amrestore assumes it can uncompress files. It is thus a good
+ * idea to run the tape server on a machine with gzip.
+ * - The information about compression in the disklist is really only
+ * for future dumps. It is possible to change compression on a drive
+ * so the information in the disklist may not necessarily relate to
+ * the dump image on the tape.
+ *   Consequently the design was changed to assuming that amrestore can
+ * uncompress any dump image and have it return an uncompressed file
+ * always.
+ */
+void
+extract_files(void)
 {
     EXTRACT_LIST *elist;
-    pid_t pid;
-    amwait_t child_stat;
-    char buf[STR_SIZE];
+    char cwd[STR_SIZE];
     char *l;
-    int tape_server_socket;
     int first;
     int otc;
+    tapelist_t *tlist = NULL, *a_tlist;
 
     if (!is_extract_list_nonempty())
     {
@@ -1608,6 +2097,8 @@ void extract_files P((void))
        return;
     }
 
+    clean_extract_list();
+
     /* get tape device name from index server if none specified */
     if (tape_server_name == NULL) {
        tape_server_name = newstralloc(tape_server_name, server_name);
@@ -1634,7 +2125,7 @@ void extract_files P((void))
     }
 
     first=1;
-    for (elist = first_tape_list(); elist != NULL; elist = next_tape_list(elist))
+    for (elist = first_tape_list(); elist != NULL; elist = next_tape_list(elist)) {
        if(elist->tape[0]!='/') {
            if(first) {
                printf("\nExtracting files using tape drive %s on host %s.\n",
@@ -1644,10 +2135,15 @@ void extract_files P((void))
            }
            else
                printf("                               ");
-           printf(" %s\n", elist->tape);
+           tlist = unmarshal_tapelist_str(elist->tape); 
+           for(a_tlist = tlist ; a_tlist != NULL; a_tlist = a_tlist->next)
+               printf(" %s", a_tlist->label);
+           printf("\n");
+           free_tapelist(tlist);
        }
+    }
     first=1;
-    for (elist = first_tape_list(); elist != NULL; elist = next_tape_list(elist))
+    for (elist = first_tape_list(); elist != NULL; elist = next_tape_list(elist)) {
        if(elist->tape[0]=='/') {
            if(first) {
                printf("\nExtracting files from holding disk on host %s.\n",
@@ -1657,11 +2153,23 @@ void extract_files P((void))
            }
            else
                printf("                               ");
-           printf(" %s\n", elist->tape);
+           tlist = unmarshal_tapelist_str(elist->tape); 
+           for(a_tlist = tlist; a_tlist != NULL; a_tlist = a_tlist->next)
+               printf(" %s", a_tlist->label);
+           printf("\n");
+           free_tapelist(tlist);
        }
+    }
     printf("\n");
-    getcwd(buf, sizeof(buf));
-    printf("Restoring files into directory %s\n", buf);
+
+    if (getcwd(cwd, sizeof(cwd)) == NULL) {
+       perror("extract_list: Current working directory unavailable");
+       exit(1);
+    }
+
+    printf("Restoring files into directory %s\n", cwd);
+    check_file_overwrite(cwd);
+
 #ifdef SAMBA_CLIENT
     if (samba_extract_method == SAMBA_SMBCLIENT)
       printf("(unless it is a Samba backup, that will go through to the SMB server)\n");
@@ -1670,16 +2178,30 @@ void extract_files P((void))
        return;
     printf("\n");
 
+    if (!do_unlink_list()) {
+       fprintf(stderr, "Can't recover because I can't cleanup the cwd (%s)\n",
+               cwd);
+       return;
+    }
+    free_unlink_list();
+
     while ((elist = first_tape_list()) != NULL)
     {
        if(elist->tape[0]=='/') {
            dump_device_name = newstralloc(dump_device_name, elist->tape);
-           printf("Extracting from file %s\n",dump_device_name);
+           printf("Extracting from file ");
+           tlist = unmarshal_tapelist_str(dump_device_name); 
+           for(a_tlist = tlist; a_tlist != NULL; a_tlist = a_tlist->next)
+               printf(" %s", a_tlist->label);
+           printf("\n");
+           free_tapelist(tlist);
        }
        else {
            printf("Extracting files using tape drive %s on host %s.\n",
                   tape_device_name, tape_server_name);
-           printf("Load tape %s now\n", elist->tape);
+           tlist = unmarshal_tapelist_str(elist->tape); 
+           printf("Load tape %s now\n", tlist->label);
+           free_tapelist(tlist);
            otc = okay_to_continue(1,1,0);
            if (otc == 0)
                return;
@@ -1692,63 +2214,347 @@ void extract_files P((void))
        dump_datestamp = newstralloc(dump_datestamp, elist->date);
 
        /* connect to the tape handler daemon on the tape drive server */
-       if ((tape_server_socket = extract_files_setup(elist->tape, elist->fileno)) == -1)
+       if ((extract_files_setup(elist->tape, elist->fileno)) == -1)
        {
            fprintf(stderr, "amrecover - can't talk to tape server\n");
            return;
        }
 
-       /* okay, ready to extract. fork a child to do the actual work */
-       if ((pid = fork()) == 0)
-       {
-           /* this is the child process */
-           /* never gets out of this clause */
-           extract_files_child(tape_server_socket, elist);
-           /*NOT REACHED*/
-       }
-       /* this is the parent */
-       if (pid == -1)
-       {
-           perror("extract_list - error forking child");
-           exit(1);
-       }
+       /* if the server have fe_amrecover_feedme_tape, it has asked for
+        * the tape itself, even if the restore didn't succeed, we should
+        * remove it.
+        */
+       if(writer_intermediary(elist) == 0 ||
+          am_has_feature(indexsrv_features, fe_amrecover_feedme_tape))
+           delete_tape_list(elist);    /* tape done so delete from list */
 
-       /* store the child pid globally so that it can be killed on intr */
-       extract_restore_child_pid = pid;
+       stop_amidxtaped();
+    }
+}
 
-       aclose(tape_server_socket);
+static void
+amidxtaped_response(
+    void *             datap,
+    pkt_t *            pkt,
+    security_handle_t *        sech)
+{
+    int ports[NSTREAMS], *response_error = datap, i;
+    char *p;
+    char *tok;
+    char *extra = NULL;
+
+    assert(response_error != NULL);
+    assert(sech != NULL);
+    memset(ports, -1, SIZEOF(ports));
+
+    security_close_connection(sech, dump_hostname);
+    if (pkt == NULL) {
+       errstr = newvstralloc(errstr, "[request failed: ",
+                            security_geterror(sech), "]", NULL);
+       *response_error = 1;
+       return;
+    }
 
-       /* wait for the child process to finish */
-       if ((pid = waitpid(-1, &child_stat, 0)) == (pid_t)-1)
-       {
-           perror("extract_list - error waiting for child");
-           exit(1);
+    if (pkt->type == P_NAK) {
+#if defined(PACKET_DEBUG)
+       fprintf(stderr, "got nak response:\n----\n%s\n----\n\n", pkt->body);
+#endif
+
+       tok = strtok(pkt->body, " ");
+       if (tok == NULL || strcmp(tok, "ERROR") != 0)
+           goto bad_nak;
+
+       tok = strtok(NULL, "\n");
+       if (tok != NULL) {
+           errstr = newvstralloc(errstr, "NAK: ", tok, NULL);
+           *response_error = 1;
+       } else {
+bad_nak:
+           errstr = newstralloc(errstr, "request NAK");
+           *response_error = 2;
        }
-       if (pid == extract_restore_child_pid)
-       {
-           extract_restore_child_pid = -1;
+       return;
+    }
+
+    if (pkt->type != P_REP) {
+       errstr = newvstralloc(errstr, "received strange packet type ",
+                             pkt_type2str(pkt->type), ": ", pkt->body, NULL);
+       *response_error = 1;
+       return;
+    }
+
+#if defined(PACKET_DEBUG)
+    fprintf(stderr, "got response:\n----\n%s\n----\n\n", pkt->body);
+#endif
+
+    for(i = 0; i < NSTREAMS; i++) {
+        ports[i] = -1;
+        amidxtaped_streams[i].fd = NULL;
+    }
+
+    p = pkt->body;
+    while((tok = strtok(p, " \n")) != NULL) {
+       p = NULL;
+
+       /*
+        * Error response packets have "ERROR" followed by the error message
+        * followed by a newline.
+        */
+       if (strcmp(tok, "ERROR") == 0) {
+           tok = strtok(NULL, "\n");
+           if (tok == NULL)
+               tok = "[bogus error packet]";
+           errstr = newstralloc(errstr, tok);
+           *response_error = 2;
+           return;
        }
-       else
-       {
-           fprintf(stderr, "extract list - unknown child terminated?\n");
-           exit(1);
+
+
+        /*
+         * Regular packets have CONNECT followed by three streams
+         */
+        if (strcmp(tok, "CONNECT") == 0) {
+
+           /*
+            * Parse the three stream specifiers out of the packet.
+            */
+           for (i = 0; i < NSTREAMS; i++) {
+               tok = strtok(NULL, " ");
+               if (tok == NULL || strcmp(tok, amidxtaped_streams[i].name) != 0) {
+                   extra = vstralloc("CONNECT token is \"",
+                                     tok ? tok : "(null)",
+                                     "\": expected \"",
+                                     amidxtaped_streams[i].name,
+                                     "\"",
+                                     NULL);
+                   goto parse_error;
+               }
+               tok = strtok(NULL, " \n");
+               if (tok == NULL || sscanf(tok, "%d", &ports[i]) != 1) {
+                   extra = vstralloc("CONNECT ",
+                                     amidxtaped_streams[i].name,
+                                     " token is \"",
+                                     tok ? tok : "(null)",
+                                     "\": expected a port number",
+                                     NULL);
+                   goto parse_error;
+               }
+           }
+           continue;
        }
-       if ((WIFEXITED(child_stat) != 0) && (WEXITSTATUS(child_stat) != 0))
-       {
-           fprintf(stderr,
-                   "extract_list - child returned non-zero status: %d\n",
-                   WEXITSTATUS(child_stat));
-           otc = okay_to_continue(0,0,1);
-           if(otc == 0)
-               return;
-           else if(otc == 1) {
-               delete_tape_list(elist); /* tape failed so delete from list */
+
+       /*
+        * OPTIONS [options string] '\n'
+        */
+       if (strcmp(tok, "OPTIONS") == 0) {
+           tok = strtok(NULL, "\n");
+           if (tok == NULL) {
+               extra = stralloc("OPTIONS token is missing");
+               goto parse_error;
            }
-           else { /* RETRY_TAPE */
+/*
+           while((p = strchr(tok, ';')) != NULL) {
+               *p++ = '\0';
+#define sc "features="
+               if(strncmp(tok, sc, sizeof(sc)-1) == 0) {
+                   tok += sizeof(sc) - 1;
+#undef sc
+                   am_release_feature_set(their_features);
+                   if((their_features = am_string_to_feature(tok)) == NULL) {
+                       errstr = newvstralloc(errstr,
+                                             "OPTIONS: bad features value: ",
+                                             tok,
+                                             NULL);
+                       goto parse_error;
+                   }
+               }
+               tok = p;
            }
+*/
+           continue;
        }
-       else {
-           delete_tape_list(elist);    /* tape done so delete from list */
+/*
+       extra = vstralloc("next token is \"",
+                         tok ? tok : "(null)",
+                         "\": expected \"CONNECT\", \"ERROR\" or \"OPTIONS\"",
+                         NULL);
+       goto parse_error;
+*/
+    }
+
+    /*
+     * Connect the streams to their remote ports
+     */
+    for (i = 0; i < NSTREAMS; i++) {
+       if (ports[i] == -1)
+           continue;
+       amidxtaped_streams[i].fd = security_stream_client(sech, ports[i]);
+       dbprintf(("amidxtaped_streams[%d].fd = %p\n",i, amidxtaped_streams[i].fd));
+       if (amidxtaped_streams[i].fd == NULL) {
+           errstr = newvstralloc(errstr,
+                       "[could not connect ", amidxtaped_streams[i].name, " stream: ",
+                       security_geterror(sech), "]", NULL);
+           goto connect_error;
        }
     }
+    /*
+     * Authenticate the streams
+     */
+    for (i = 0; i < NSTREAMS; i++) {
+       if (amidxtaped_streams[i].fd == NULL)
+           continue;
+       if (security_stream_auth(amidxtaped_streams[i].fd) < 0) {
+           errstr = newvstralloc(errstr,
+               "[could not authenticate ", amidxtaped_streams[i].name, " stream: ",
+               security_stream_geterror(amidxtaped_streams[i].fd), "]", NULL);
+           goto connect_error;
+       }
+    }
+
+    /*
+     * The CTLFD and DATAFD streams are mandatory.  If we didn't get
+     * them, complain.
+     */
+    if (amidxtaped_streams[CTLFD].fd == NULL) {
+        errstr = newstralloc(errstr, "[couldn't open CTL streams]");
+        goto connect_error;
+    }
+    if (amidxtaped_streams[DATAFD].fd == NULL) {
+        errstr = newstralloc(errstr, "[couldn't open DATA streams]");
+        goto connect_error;
+    }
+
+    /* everything worked */
+    *response_error = 0;
+    return;
+
+parse_error:
+    errstr = newvstralloc(errstr,
+                         "[parse of reply message failed: ",
+                         extra ? extra : "(no additional information)",
+                         "]",
+                         NULL);
+    amfree(extra);
+    *response_error = 2;
+    return;
+
+connect_error:
+    stop_amidxtaped();
+    *response_error = 1;
+}
+
+/*
+ * This is called when everything needs to shut down so event_loop()
+ * will exit.
+ */
+static void
+stop_amidxtaped(void)
+{
+    int i;
+
+    for (i = 0; i < NSTREAMS; i++) {
+        if (amidxtaped_streams[i].fd != NULL) {
+            security_stream_close(amidxtaped_streams[i].fd);
+            amidxtaped_streams[i].fd = NULL;
+        }
+    }
+}
+
+static char* ctl_buffer = NULL;
+/* gets a "line" from server and put in server_line */
+/* server_line is terminated with \0, \r\n is striped */
+/* returns -1 if error */
+
+int
+get_amidxtaped_line(void)
+{
+    ssize_t size;
+    char *newbuf, *s;
+    void *buf;
+
+    amfree(amidxtaped_line);
+    if (!ctl_buffer)
+       ctl_buffer = stralloc("");
+
+    while (!strstr(ctl_buffer,"\r\n")) {
+        size = security_stream_read_sync(amidxtaped_streams[CTLFD].fd, &buf);
+        if(size < 0) {
+            return -1;
+        }
+        else if(size == 0) {
+            return -1;
+        }
+        newbuf = alloc(strlen(ctl_buffer)+size+1);
+        strncpy(newbuf, ctl_buffer, (size_t)(strlen(ctl_buffer) + size + 1));
+        memcpy(newbuf+strlen(ctl_buffer), buf, (size_t)size);
+        newbuf[strlen(ctl_buffer)+size] = '\0';
+        amfree(ctl_buffer);
+        ctl_buffer = newbuf;
+    }
+
+    s = strstr(ctl_buffer,"\r\n");
+    *s = '\0';
+    newbuf = stralloc(s+2);
+    amidxtaped_line = stralloc(ctl_buffer);
+    amfree(ctl_buffer);
+    ctl_buffer = newbuf;
+    return 0;
+}
+
+
+static void
+read_amidxtaped_data(
+    void *     cookie,
+    void *     buf,
+    ssize_t    size)
+{
+    int fd;
+
+    assert(cookie != NULL);
+
+    fd = *(int *)cookie;
+    if (size < 0) {
+       errstr = newstralloc2(errstr, "amidxtaped read: ",
+                security_stream_geterror(amidxtaped_streams[DATAFD].fd));
+       return;
+    }
+
+    /*
+     * EOF.  Stop and return.
+     */
+    if (size == 0) {
+       security_stream_close(amidxtaped_streams[DATAFD].fd);
+       amidxtaped_streams[DATAFD].fd = NULL;
+       /*
+        * If the mesg fd has also shut down, then we're done.
+        */
+       return;
+    }
+
+    assert(buf != NULL);
+
+    /*
+     * We ignore errors while writing to the index file.
+     */
+    (void)fullwrite(fd, buf, (size_t)size);
+    security_stream_read(amidxtaped_streams[DATAFD].fd, read_amidxtaped_data, cookie);
+}
+
+char *
+amidxtaped_client_get_security_conf(
+    char *     string,
+    void *     arg)
+{
+    (void)arg; /* Quiet unused parameter warning */
+
+    if(!string || !*string)
+       return(NULL);
+
+    if(strcmp(string, "auth")==0) {
+       return(client_getconf_str(CLN_AUTH));
+    }
+    if(strcmp(string, "ssh_keys")==0) {
+       return(client_getconf_str(CLN_SSH_KEYS));
+    }
+    return(NULL);
 }