Imported Debian patch 2.5.1p1-1
[debian/amanda] / client-src / sendbackup.c
index e8050448a6544d6b2f298187ac0d835b233fd17d..5af8dcef5be8ceb048dc46eadbf892721c37a11d 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
- * Copyright (c) 1991-1998 University of Maryland at College Park
+ * Copyright (c) 1991-1999 University of Maryland at College Park
  * All Rights Reserved.
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
@@ -24,7 +24,7 @@
  * file named AUTHORS, in the root directory of this distribution.
  */
 /* 
- * $Id: sendbackup.c,v 1.44.2.9.4.4.2.16.2.1 2005/09/20 21:31:52 jrjackson Exp $
+ * $Id: sendbackup.c,v 1.88 2006/07/25 18:27:56 martinea Exp $
  *
  * common code for the sendbackup-* programs.
  */
 #include "clock.h"
 #include "pipespawn.h"
 #include "amfeatures.h"
-#include "stream.h"
+#include "amandad.h"
 #include "arglist.h"
 #include "getfsent.h"
 #include "version.h"
+#include "clientconf.h"
 
 #define TIMEOUT 30
 
-int comppid = -1;
-int dumppid = -1;
-int tarpid = -1;
-int encpid = -1;
-int indexpid = -1;
+pid_t comppid = (pid_t)-1;
+pid_t dumppid = (pid_t)-1;
+pid_t tarpid = (pid_t)-1;
+pid_t encpid = (pid_t)-1;
+pid_t indexpid = (pid_t)-1;
 char *errorstr = NULL;
 
-int data_socket, data_port, dataf;
-int mesg_socket, mesg_port, mesgf;
-int index_socket, index_port, indexf;
+int datafd;
+int mesgfd;
+int indexfd;
 
 option_t *options;
-
-#ifdef KRB4_SECURITY
-#include "sendbackup-krb4.h"
-#else                                  /* I'd tell you what this does */
-#define NAUGHTY_BITS                   /* but then I'd have to kill you */
-#endif
+g_option_t *g_options = NULL;
 
 long dump_size = -1;
 
@@ -66,48 +62,77 @@ backup_program_t *program = NULL;
 
 static am_feature_t *our_features = NULL;
 static char *our_feature_string = NULL;
-static g_option_t *g_options = NULL;
+static char *amandad_auth = NULL;
 
 /* local functions */
-int main P((int argc, char **argv));
-char *optionstr P((option_t *options));
-char *childstr P((int pid));
-int check_status P((int pid, amwait_t w));
+int main(int argc, char **argv);
+char *optionstr(option_t *options);
+char *childstr(pid_t pid);
+int check_status(pid_t pid, amwait_t w);
+
+pid_t pipefork(void (*func)(void), char *fname, int *stdinfd,
+               int stdoutfd, int stderrfd);
+void parse_backup_messages(int mesgin);
+static void process_dumpline(char *str);
+static void save_fd(int *, int);
 
-int pipefork P((void (*func) P((void)), char *fname, int *stdinfd,
-               int stdoutfd, int stderrfd));
-void parse_backup_messages P((int mesgin));
-static void process_dumpline P((char *str));
+double first_num(char *str);
 
 
-char *optionstr(option_t *options)
+
+char *
+optionstr(
+    option_t * options)
 {
     static char *optstr = NULL;
-    char *compress_opt = "";
+    char *compress_opt;
+    char *encrypt_opt;
+    char *decrypt_opt;
     char *record_opt = "";
-    char *bsd_opt = "";
-    char *krb4_opt = "";
-    char *kencrypt_opt = "";
     char *index_opt = "";
+    char *auth_opt;
     char *exclude_file_opt;
     char *exclude_list_opt;
     char *exc = NULL;
     sle_t *excl;
 
     if(options->compress == COMPR_BEST)
-       compress_opt = "compress-best;";
+       compress_opt = stralloc("compress-best;");
     else if(options->compress == COMPR_FAST)
-       compress_opt = "compress-fast;";
+       compress_opt = stralloc("compress-fast;");
     else if(options->compress == COMPR_SERVER_BEST)
-       compress_opt = "srvcomp-best;";
+       compress_opt = stralloc("srvcomp-best;");
     else if(options->compress == COMPR_SERVER_FAST)
-       compress_opt = "srvcomp-fast;";
+       compress_opt = stralloc("srvcomp-fast;");
+    else if(options->compress == COMPR_SERVER_CUST)
+       compress_opt = vstralloc("srvcomp-cust=", options->srvcompprog, ";", NULL);
+    else if(options->compress == COMPR_CUST)
+       compress_opt = vstralloc("comp-cust=", options->clntcompprog, ";", NULL);
+    else
+       compress_opt = stralloc("");
+    
+    if(options->encrypt == ENCRYPT_CUST) {
+      encrypt_opt = vstralloc("encrypt-cust=", options->clnt_encrypt, ";", NULL);
+      if (options->clnt_decrypt_opt)
+       decrypt_opt = vstralloc("client-decrypt-option=", options->clnt_decrypt_opt, ";", NULL);
+      else
+       decrypt_opt = stralloc("");
+    }
+    else if(options->encrypt == ENCRYPT_SERV_CUST) {
+      encrypt_opt = vstralloc("encrypt-serv-cust=", options->srv_encrypt, ";", NULL);
+      if(options->srv_decrypt_opt)
+       decrypt_opt = vstralloc("server-decrypt-option=", options->srv_decrypt_opt, ";", NULL);
+      else
+       decrypt_opt = stralloc("");
+    }
+    else {
+       encrypt_opt = stralloc("");
+       decrypt_opt = stralloc("");
+    }
+
     if(options->no_record) record_opt = "no-record;";
-    if(options->bsd_auth) bsd_opt = "bsd-auth;";
-#ifdef KRB4_SECURITY
-    if(options->krb4_auth) krb4_opt = "krb4-auth;";
-    if(options->kencrypt) kencrypt_opt = "kencrypt;";
-#endif
+    if(options->auth) auth_opt = vstralloc("auth=", options->auth, ";", NULL);
+       else auth_opt = stralloc("");
     if(options->createindex) index_opt = "index;";
 
     exclude_file_opt = stralloc("");
@@ -124,30 +149,44 @@ char *optionstr(option_t *options)
            strappend(exclude_list_opt, exc);
        }
     }
+    amfree(exc);
     optstr = newvstralloc(optstr,
                          compress_opt,
+                         encrypt_opt,
+                         decrypt_opt,
                          record_opt,
-                         bsd_opt,
-                         krb4_opt,
-                         kencrypt_opt,
                          index_opt,
+                         auth_opt,
                          exclude_file_opt,
                          exclude_list_opt,
                          NULL);
+    amfree(compress_opt);
+    amfree(encrypt_opt);
+    amfree(decrypt_opt);
+    amfree(auth_opt);
+    amfree(exclude_file_opt);
+    amfree(exclude_list_opt);
     return optstr;
 }
 
 
-int main(argc, argv)
-int argc;
-char **argv;
+int
+main(
+    int                argc,
+    char **    argv)
 {
     int interactive = 0;
-    int level, mesgpipe[2];
-    char *prog, *disk, *amdevice, *dumpdate, *stroptions;
+    int level = 0;
+    int mesgpipe[2];
+    char *prog, *dumpdate, *stroptions;
+    char *disk = NULL;
+    char *qdisk = NULL;
+    char *amdevice = NULL;
+    char *qamdevice = NULL;
     char *line = NULL;
     char *err_extra = NULL;
     char *s;
+    char *conffile;
     int i;
     int ch;
     unsigned long malloc_hist_1, malloc_size_1;
@@ -155,27 +194,47 @@ char **argv;
 
     /* initialize */
 
-#ifdef KRB4_SECURITY
-    safe_fd(KEY_PIPE, 1);              /* XXX interface needs to be fixed */
-#else
-    safe_fd(-1, 0);
-#endif
+    safe_fd(DATA_FD_OFFSET, DATA_FD_COUNT*2);
 
     safe_cd();
 
     set_pname("sendbackup");
 
+    /* Don't die when child closes pipe */
+    signal(SIGPIPE, SIG_IGN);
+
+    /* Don't die when interrupt received */
+    signal(SIGINT, SIG_IGN);
+
     malloc_size_1 = malloc_inuse(&malloc_hist_1);
 
-    interactive = (argc > 1 && strcmp(argv[1],"-t") == 0);
+    if(argc > 1 && strcmp(argv[1],"-t") == 0) {
+       interactive = 1;
+       argc--;
+       argv++;
+    } else {
+       interactive = 0;
+    }
+
     erroutput_type = (ERR_INTERACTIVE|ERR_SYSLOG);
-    dbopen();
+    dbopen(DBG_SUBDIR_CLIENT);
     startclock();
-    dbprintf(("%s: version %s\n", argv[0], version()));
+    dbprintf(("%s: version %s\n", get_pname(), version()));
+
+    if(argc > 2 && strcmp(argv[1], "amandad") == 0) {
+       amandad_auth = stralloc(argv[2]);
+    }
 
     our_features = am_init_feature_set();
     our_feature_string = am_feature_to_string(our_features);
 
+    conffile = vstralloc(CONFIG_DIR, "/", "amanda-client.conf", NULL);
+    if (read_clientconf(conffile) > 0) {
+       error("error reading conffile: %s", conffile);
+       /*NOTREACHED*/
+    }
+    amfree(conffile);
+
     if(interactive) {
        /*
         * In interactive (debug) mode, the backup data is sent to
@@ -189,19 +248,20 @@ char **argv;
 
     prog = NULL;
     disk = NULL;
+    qdisk = NULL;
     amdevice = NULL;
     dumpdate = NULL;
     stroptions = NULL;
 
-    /* parse dump request */
-
     for(; (line = agets(stdin)) != NULL; free(line)) {
+       if (line[0] == '\0')
+           continue;
        if(interactive) {
            fprintf(stderr, "%s> ", get_pname());
            fflush(stderr);
        }
 #define sc "OPTIONS "
-       if(strncmp(line, sc, sizeof(sc)-1) == 0) {
+       if(strncmp(line, sc, SIZEOF(sc)-1) == 0) {
 #undef sc
            g_options = parse_g_options(line+8, 1);
            if(!g_options->hostname) {
@@ -209,6 +269,18 @@ char **argv;
                gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
                g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
            }
+
+           if (g_options->config) {
+               conffile = vstralloc(CONFIG_DIR, "/", g_options->config, "/",
+                                    "amanda-client.conf", NULL);
+               if (read_clientconf(conffile) > 0) {
+                   error("error reading conffile: %s", conffile);
+                   /*NOTREACHED*/
+               }
+               amfree(conffile);
+
+               dbrename(g_options->config, DBG_SUBDIR_CLIENT);
+           }
            continue;
        }
 
@@ -217,6 +289,7 @@ char **argv;
            goto err;
        }
 
+       dbprintf(("  sendbackup req: <%s>\n", line));
        s = line;
        ch = *s++;
 
@@ -235,11 +308,15 @@ char **argv;
            err_extra = "no disk name";
            goto err;                           /* no disk name */
        }
+
        amfree(disk);
-       disk = s - 1;
-       skip_non_whitespace(s, ch);
+       amfree(qdisk);
+       qdisk = s - 1;
+       ch = *qdisk;
+       skip_quoted_string(s, ch);
        s[-1] = '\0';
-       disk = stralloc(disk);
+       qdisk = stralloc(qdisk);
+       disk = unquote_string(qdisk);
 
        skip_whitespace(s, ch);                 /* find the device or level */
        if (ch == '\0') {
@@ -249,6 +326,7 @@ char **argv;
 
        if(!isdigit((int)s[-1])) {
            amfree(amdevice);
+           amfree(qamdevice);
            amdevice = s - 1;
            skip_non_whitespace(s, ch);
            s[-1] = '\0';
@@ -258,7 +336,7 @@ char **argv;
        else {
            amdevice = stralloc(disk);
        }
-
+       qamdevice = quote_string(amdevice);
                                                /* find the level number */
        if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
            err_extra = "bad level";
@@ -283,11 +361,11 @@ char **argv;
            goto err;                           /* no options */
        }
 #define sc "OPTIONS "
-       if(strncmp(s - 1, sc, sizeof(sc)-1) != 0) {
+       if(strncmp(s - 1, sc, SIZEOF(sc)-1) != 0) {
            err_extra = "no OPTIONS keyword";
            goto err;                           /* no options */
        }
-       s += sizeof(sc)-1;
+       s += SIZEOF(sc)-1;
        ch = s[-1];
 #undef sc
        skip_whitespace(s, ch);                 /* find the options string */
@@ -300,61 +378,54 @@ char **argv;
     }
     amfree(line);
 
+    if (prog       == NULL ||
+       disk       == NULL ||
+       amdevice   == NULL ||
+       dumpdate   == NULL ||
+       stroptions == NULL) {
+       err_extra = "no valid sendbackup request";
+       goto err;
+    }
+       
     dbprintf(("  parsed request as: program `%s'\n", prog));
-    dbprintf(("                     disk `%s'\n", disk));
-    dbprintf(("                     device `%s'\n", amdevice));
+    dbprintf(("                     disk `%s'\n", qdisk));
+    dbprintf(("                     device `%s'\n", qamdevice));
     dbprintf(("                     level %d\n", level));
     dbprintf(("                     since %s\n", dumpdate));
     dbprintf(("                     options `%s'\n", stroptions));
 
-    for(i = 0; programs[i] != NULL; i++) {
+    for(i = 0; programs[i]; i++) {
        if (strcmp(programs[i]->name, prog) == 0) {
            break;
        }
     }
     if (programs[i] == NULL) {
        error("ERROR [%s: unknown program %s]", get_pname(), prog);
+       /*NOTREACHED*/
     }
     program = programs[i];
 
     options = parse_options(stroptions, disk, amdevice, g_options->features, 0);
 
-#ifdef KRB4_SECURITY
-    /* modification by BIS@BBN 4/25/2003:
-     * with the option processing changes in amanda 2.4.4, must change
-     * the conditional from krb4_auth to options->krb4_auth */
-    if(options->krb4_auth) {
-       if(read(KEY_PIPE, session_key, sizeof session_key) 
-          != sizeof session_key) {
-         error("ERROR [%s: could not read session key]", get_pname());
-       }
-    }
-#endif
-
     if(!interactive) {
-      data_socket = stream_server(&data_port, STREAM_BUFSIZE, -1);
-      if(data_socket < 0) {
-       error("ERROR [%s: could not create data socket: %s]",
-             get_pname(), strerror(errno));
-      }
-      mesg_socket = stream_server(&mesg_port, -1, -1);
-      if(mesg_socket < 0) {
-       error("ERROR [%s: could not create mesg socket: %s]",
-             get_pname(), strerror(errno));
-      }
-    }
-    if (!interactive && options->createindex) {
-      index_socket = stream_server(&index_port, -1, -1);
-      if(index_socket < 0) {
-       error("ERROR [%s: could not create index socket: %s]",
-             get_pname(), strerror(errno));
-      }
-    } else {
-      index_port = -1;
+       datafd = DATA_FD_OFFSET + 0;
+       mesgfd = DATA_FD_OFFSET + 2;
+       indexfd = DATA_FD_OFFSET + 4;
+    }
+    if (!options->createindex)
+       indexfd = -1;
+
+    if(options->auth && amandad_auth) {
+       if(strcasecmp(options->auth, amandad_auth) != 0) {
+           printf("ERROR [client configured for auth=%s while server requested '%s']\n",
+                  amandad_auth, options->auth);
+           exit(-1);
+       }
     }
 
     printf("CONNECT DATA %d MESG %d INDEX %d\n",
-          data_port, mesg_port, index_port);
+          DATA_FD_OFFSET, DATA_FD_OFFSET+1,
+          indexfd == -1 ? -1 : DATA_FD_OFFSET+2);
     printf("OPTIONS ");
     if(am_has_feature(g_options->features, fe_rep_options_features)) {
        printf("features=%s;", our_feature_string);
@@ -367,91 +438,35 @@ char **argv;
     }
     printf("\n");
     fflush(stdout);
-    freopen("/dev/null", "w", stdout);
-
-    if (options->createindex)
-      dbprintf(("%s: waiting for connect on %d, then %d, then %d\n",
-               debug_prefix_time(NULL), data_port, mesg_port, index_port));
-    else
-      dbprintf(("%s: waiting for connect on %d, then %d\n",
-               debug_prefix_time(NULL), data_port, mesg_port));
-
-    if(interactive) {
-      if((dataf = open("/dev/null", O_RDWR)) < 0) {
-       error("ERROR [%s: open of /dev/null for debug data stream: %s]",
-             get_pname(), strerror(errno));
-      }
-      mesgf = 2;
-    } else {
-      dataf = stream_accept(data_socket, TIMEOUT, -1, -1);
-      if(dataf == -1) {
-       dbprintf(("%s: timeout on data port %d\n",
-                 debug_prefix_time(NULL), data_port));
-      }
-      mesgf = stream_accept(mesg_socket, TIMEOUT, -1, -1);
-      if(mesgf == -1) {
-        dbprintf(("%s: timeout on mesg port %d\n",
-                 debug_prefix_time(NULL), mesg_port));
-      }
+    if (freopen("/dev/null", "w", stdout) == NULL) {
+       dbprintf(("%s: error redirecting stdout to /dev/null: %s\n",
+           debug_prefix_time(NULL), mesgfd, strerror(errno)));
+        exit(1);
     }
+
     if(interactive) {
-      indexf = 1;
-    } else if (options->createindex) {
-      indexf = stream_accept(index_socket, TIMEOUT, -1, -1);
-      if (indexf == -1) {
-       dbprintf(("%s: timeout on index port %d\n",
-                 debug_prefix_time(NULL), index_port));
+      if((datafd = open("/dev/null", O_RDWR)) < 0) {
+       s = strerror(errno);
+       error("ERROR [%s: open of /dev/null for debug data stream: %s]\n",
+                 get_pname(), s);
+       /*NOTREACHED*/
       }
+      mesgfd = 2;
+      indexfd = 1;
     }
 
     if(!interactive) {
-      if(dataf == -1 || mesgf == -1 || (options->createindex && indexf == -1)) {
+      if(datafd == -1 || mesgfd == -1 || (options->createindex && indexfd == -1)) {
         dbclose();
         exit(1);
       }
     }
 
-    dbprintf(("%s: got all connections\n", debug_prefix_time(NULL)));
-
-#ifdef KRB4_SECURITY
-    if(!interactive) {
-      /* modification by BIS@BBN 4/25/2003:
-       * with the option processing changes in amanda 2.4.4, must change
-       * the conditional from krb4_auth to options->krb4_auth */
-      if (options->krb4_auth) {
-        if(kerberos_handshake(dataf, session_key) == 0) {
-           dbprintf(("%s: kerberos_handshake on data socket failed\n",
-                     debug_prefix_time(NULL)));
-           dbclose();
-           exit(1);
-        } else {
-           dbprintf(("%s: kerberos_handshake on data socket succeeded\n",
-                     debug_prefix_time(NULL)));
-
-       }
-
-        if(kerberos_handshake(mesgf, session_key) == 0) {
-           dbprintf(("%s: kerberos_handshake on mesg socket failed\n",
-                     debug_prefix_time(NULL)));
-           dbclose();
-           exit(1);
-        } else {
-           dbprintf(("%s: kerberos_handshake on mesg socket succeeded\n",
-                     debug_prefix_time(NULL)));
-
-       }
-
-        dbprintf(("%s: kerberos handshakes succeeded!\n",
-                 debug_prefix_time(NULL)));
-      }
-    }
-#endif
-
     if(!interactive) {
       /* redirect stderr */
-      if(dup2(mesgf, 2) == -1) {
-         dbprintf(("%s: error redirecting stderr: %s\n",
-                   debug_prefix(NULL), strerror(errno)));
+      if(dup2(mesgfd, 2) == -1) {
+         dbprintf(("%s: error redirecting stderr to fd %d: %s\n",
+             debug_prefix_time(NULL), mesgfd, strerror(errno)));
          dbclose();
          exit(1);
       }
@@ -459,15 +474,20 @@ char **argv;
 
     if(pipe(mesgpipe) == -1) {
       error("error [opening mesg pipe: %s]", strerror(errno));
+      /*NOTREACHED*/
     }
 
-    program->start_backup(g_options->hostname, disk, amdevice, level, dumpdate,
-                         dataf, mesgpipe[1], indexf);
+    program->start_backup(g_options->hostname, disk, amdevice, level, dumpdate, datafd, mesgpipe[1],
+                         indexfd);
+    dbprintf(("%s: started backup\n", debug_prefix_time(NULL)));
     parse_backup_messages(mesgpipe[0]);
+    dbprintf(("%s: parsed backup messages\n", debug_prefix_time(NULL)));
 
     amfree(prog);
     amfree(disk);
+    amfree(qdisk);
     amfree(amdevice);
+    amfree(qamdevice);
     amfree(dumpdate);
     amfree(stroptions);
     amfree(our_feature_string);
@@ -499,29 +519,34 @@ char **argv;
     return 1;
 }
 
-char *childstr(pid)
-int pid;
+
 /*
  * Returns a string for a child process.  Checks the saved dump and
  * compress pids to see which it is.
  */
+
+char *
+childstr(
+    pid_t pid)
 {
     if(pid == dumppid) return program->backup_name;
     if(pid == comppid) return "compress";
-    if(pid == encpid)  return "kencrypt";
+    if(pid == encpid) return "encrypt";
     if(pid == indexpid) return "index";
     return "unknown";
 }
 
 
-int check_status(pid, w)
-int pid;
-amwait_t w;
 /*
  * Determine if the child return status really indicates an error.
  * If so, add the error message to the error string; more than one
  * child can have an error.
  */
+
+int
+check_status(
+    pid_t      pid,
+    amwait_t   w)
 {
     char *thiserr = NULL;
     char *str;
@@ -588,10 +613,10 @@ amwait_t w;
     }
 
     if(ret == 0) {
-       ap_snprintf(number, sizeof(number), "%d", sig);
+       snprintf(number, SIZEOF(number), "%d", sig);
        thiserr = vstralloc(str, " got signal ", number, NULL);
     } else {
-       ap_snprintf(number, sizeof(number), "%d", ret);
+       snprintf(number, SIZEOF(number), "%d", ret);
        thiserr = vstralloc(str, " returned ", number, NULL);
     }
 
@@ -607,9 +632,11 @@ amwait_t w;
 }
 
 
-/* Send header info to the message file.
-*/
-void write_tapeheader()
+/*
+ *Send header info to the message file.
+ */
+void
+info_tapeheader(void)
 {
     fprintf(stderr, "%s: info BACKUP=%s\n", get_pname(), program->backup_name);
 
@@ -623,7 +650,7 @@ void write_tapeheader()
 #endif
                );
 
-    fprintf(stderr, "%s -f... -\n", program->restore_name);
+    fprintf(stderr, "%s -f - ...\n", program->restore_name);
 
     if (options->compress == COMPR_FAST || options->compress == COMPR_BEST)
        fprintf(stderr, "%s: info COMPRESS_SUFFIX=%s\n",
@@ -632,24 +659,29 @@ void write_tapeheader()
     fprintf(stderr, "%s: info end\n", get_pname());
 }
 
-int pipefork(func, fname, stdinfd, stdoutfd, stderrfd)
-void (*func) P((void));
-char *fname;
-int *stdinfd;
-int stdoutfd, stderrfd;
+pid_t
+pipefork(
+    void       (*func)(void),
+    char *     fname,
+    int *      stdinfd,
+    int                stdoutfd,
+    int                stderrfd)
 {
-    int pid, inpipe[2];
+    int inpipe[2];
+    pid_t pid;
 
     dbprintf(("%s: forking function %s in pipeline\n",
-             debug_prefix_time(NULL), fname));
+       debug_prefix_time(NULL), fname));
 
     if(pipe(inpipe) == -1) {
        error("error [open pipe to %s: %s]", fname, strerror(errno));
+       /*NOTREACHED*/
     }
 
     switch(pid = fork()) {
     case -1:
        error("error [fork %s: %s]", fname, strerror(errno));
+       /*NOTREACHED*/
     default:   /* parent process */
        aclose(inpipe[0]);      /* close input side of pipe */
        *stdinfd = inpipe[1];
@@ -658,26 +690,34 @@ int stdoutfd, stderrfd;
        aclose(inpipe[1]);      /* close output side of pipe */
 
        if(dup2(inpipe[0], 0) == -1) {
-           error("error [dup2 0 %s: dup2 in: %s]", fname, strerror(errno));
+           error("error [fork %s: dup2(%d, in): %s]",
+                 fname, inpipe[0], strerror(errno));
+           /*NOTRACHED*/
        }
        if(dup2(stdoutfd, 1) == -1) {
-           error("error [dup2 1 %s: dup2 out: %s]", fname, strerror(errno));
+           error("error [fork %s: dup2(%d, out): %s]",
+                 fname, stdoutfd, strerror(errno));
+           /*NOTRACHED*/
        }
        if(dup2(stderrfd, 2) == -1) {
-           error("error [dup2 2 %s: dup2 err: %s]", fname, strerror(errno));
+           error("error [fork %s: dup2(%d, err): %s]",
+                 fname, stderrfd, strerror(errno));
+           /*NOTRACHED*/
        }
 
        func();
        exit(0);
-       /* NOTREACHED */
+       /*NOTREACHED*/
     }
     return pid;
 }
 
-void parse_backup_messages(mesgin)
-int mesgin;
+void
+parse_backup_messages(
+    int                mesgin)
 {
-    int goterror, wpid;
+    int goterror;
+    pid_t wpid;
     amwait_t retstat;
     char *line;
 
@@ -690,6 +730,7 @@ int mesgin;
 
     if(errno) {
        error("error [read mesg pipe: %s]", strerror(errno));
+       /*NOTREACHED*/
     }
 
     while((wpid = wait(&retstat)) != -1) {
@@ -698,8 +739,10 @@ int mesgin;
 
     if(errorstr) {
        error("error [%s]", errorstr);
+       /*NOTREACHED*/
     } else if(dump_size == -1) {
        error("error [no backup size line]");
+       /*NOTREACHED*/
     }
 
     program->end_backup(goterror);
@@ -709,13 +752,13 @@ int mesgin;
 }
 
 
-double first_num P((char *str));
-
-double first_num(str)
-char *str;
 /*
  * Returns the value of the first integer in a string.
  */
+
+double
+first_num(
+    char *     str)
 {
     char *num;
     int ch;
@@ -727,14 +770,16 @@ char *str;
     while(isdigit(ch) || ch == '.') ch = *str++;
     str[-1] = '\0';
     d = atof(num);
-    str[-1] = ch;
+    str[-1] = (char)ch;
     return d;
 }
 
-static void process_dumpline(str)
-char *str;
+
+static void
+process_dumpline(
+    char *     str)
 {
-    regex_t *rp;
+    amregex_t *rp;
     char *type;
     char startchr;
 
@@ -781,36 +826,30 @@ char *str;
 }
 
 
-/* start_index.  Creates an index file from the output of dump/tar.
-   It arranges that input is the fd to be written by the dump process.
-   If createindex is not enabled, it does nothing.  If it is not, a
-   new process will be created that tees input both to a pipe whose
-   read fd is dup2'ed input and to a program that outputs an index
-   file to `index'.
-
-   make sure that the chat from restore doesn't go to stderr cause
-   this goes back to amanda which doesn't expect to see it
-   (2>/dev/null should do it)
-
-   Originally by Alan M. McIvor, 13 April 1996
-
-   Adapted by Alexandre Oliva, 1 May 1997
-
-   This program owes a lot to tee.c from GNU sh-utils and dumptee.c
-   from the DeeJay backup package.
-
-*/
-
-static volatile int index_finished = 0;
-
-static void index_closed(sig)
-int sig;
-{
-  index_finished = 1;
-}
+/*
+ * start_index.  Creates an index file from the output of dump/tar.
+ * It arranges that input is the fd to be written by the dump process.
+ * If createindex is not enabled, it does nothing.  If it is not, a
+ * new process will be created that tees input both to a pipe whose
+ * read fd is dup2'ed input and to a program that outputs an index
+ * file to `index'.
+ *
+ * make sure that the chat from restore doesn't go to stderr cause
+ * this goes back to amanda which doesn't expect to see it
+ * (2>/dev/null should do it)
+ *
+ * Originally by Alan M. McIvor, 13 April 1996
+ *
+ * Adapted by Alexandre Oliva, 1 May 1997
+ *
+ * This program owes a lot to tee.c from GNU sh-utils and dumptee.c
+ * from the DeeJay backup package.
+ */
 
-void save_fd(fd, min)
-int *fd, min;
+static void
+save_fd(
+    int *      fd,
+    int                min)
 {
   int origfd = *fd;
 
@@ -818,19 +857,22 @@ int *fd, min;
     int newfd = dup(*fd);
     if (newfd == -1)
       dbprintf(("%s: unable to save file descriptor [%s]\n",
-               debug_prefix(NULL), strerror(errno)));
+       debug_prefix_time(NULL), strerror(errno)));
     *fd = newfd;
   }
   if (origfd != *fd)
     dbprintf(("%s: dupped file descriptor %i to %i\n",
-             debug_prefix(NULL), origfd, *fd));
+      debug_prefix_time(NULL), origfd, *fd));
 }
 
-void start_index(createindex, input, mesg, index, cmd)
-int createindex, input, mesg, index;
-char *cmd;
+void
+start_index(
+    int                createindex,
+    int                input,
+    int                mesg,
+    int                index,
+    char *     cmd)
 {
-  struct sigaction act, oact;
   int pipefd[2];
   FILE *pipe_fp;
   int exitcode;
@@ -840,16 +882,19 @@ char *cmd;
 
   if (pipe(pipefd) != 0) {
     error("creating index pipe: %s", strerror(errno));
+    /*NOTREACHED*/
   }
 
   switch(indexpid = fork()) {
   case -1:
     error("forking index tee process: %s", strerror(errno));
+    /*NOTREACHED*/
 
   default:
     aclose(pipefd[0]);
     if (dup2(pipefd[1], input) == -1) {
       error("dup'ping index tee output: %s", strerror(errno));
+      /*NOTREACHED*/
     }
     aclose(pipefd[1]);
     return;
@@ -873,34 +918,26 @@ char *cmd;
     }
   }
 
-  /* set up a signal handler for SIGPIPE for when the pipe is finished
-     creating the index file */
-  /* at that point we obviously want to stop writing to it */
-  act.sa_handler = index_closed;
-  sigemptyset(&act.sa_mask);
-  act.sa_flags = 0;
-  if (sigaction(SIGPIPE, &act, &oact) != 0) {
-    error("couldn't set index SIGPIPE handler [%s]", strerror(errno));
-  }
-
   if ((pipe_fp = popen(cmd, "w")) == NULL) {
     error("couldn't start index creator [%s]", strerror(errno));
+    /*NOTREACHED*/
   }
 
   dbprintf(("%s: started index creator: \"%s\"\n",
-           debug_prefix_time(NULL), cmd));
+    debug_prefix_time(NULL), cmd));
   while(1) {
     char buffer[BUFSIZ], *ptr;
-    int bytes_read;
-    int bytes_written;
-    int just_written;
+    ssize_t bytes_read;
+    size_t bytes_written;
+    ssize_t just_written;
 
-    bytes_read = read(0, buffer, sizeof(buffer));
-    if ((bytes_read < 0) && (errno == EINTR))
-      continue;
+    do {
+       bytes_read = read(0, buffer, SIZEOF(buffer));
+    } while ((bytes_read < 0) && ((errno == EINTR) || (errno == EAGAIN)));
 
     if (bytes_read < 0) {
       error("index tee cannot read [%s]", strerror(errno));
+      /*NOTREACHED*/
     }
 
     if (bytes_read == 0)
@@ -909,36 +946,31 @@ char *cmd;
     /* write the stuff to the subprocess */
     ptr = buffer;
     bytes_written = 0;
-    while (bytes_read > bytes_written && !index_finished) {
-      just_written = write(fileno(pipe_fp), ptr, bytes_read - bytes_written);
-      if (just_written < 0) {
-         /* the signal handler may have assigned to index_finished
-          * just as we waited for write() to complete. */
-         if (!index_finished) {
-             dbprintf(("%s: index tee cannot write to index creator [%s]\n",
-                       debug_prefix_time(NULL), strerror(errno)));
-             index_finished = 1;
+    just_written = fullwrite(fileno(pipe_fp), ptr, (size_t)bytes_read);
+    if (just_written < 0) {
+       /* 
+        * just as we waited for write() to complete.
+        */
+       if (errno != EPIPE) {
+           dbprintf(("%s: index tee cannot write to index creator [%s]\n",
+                           debug_prefix_time(NULL), strerror(errno)));
        }
-      } else {
+    } else {
        bytes_written += just_written;
        ptr += just_written;
-      }
     }
 
     /* write the stuff to stdout, ensuring none lost when interrupt
        occurs */
     ptr = buffer;
     bytes_written = 0;
-    while (bytes_read > bytes_written) {
-      just_written = write(3, ptr, bytes_read - bytes_written);
-      if ((just_written < 0) && (errno == EINTR))
-       continue;
-      if (just_written < 0) {
+    just_written = fullwrite(3, ptr, (size_t)bytes_read);
+    if (just_written < 0) {
        error("index tee cannot write [%s]", strerror(errno));
-      } else {
+       /*NOTREACHED*/
+    } else {
        bytes_written += just_written;
        ptr += just_written;
-      }
     }
   }
 
@@ -948,7 +980,7 @@ char *cmd;
   /* check the exit code of the pipe and moan if not 0 */
   if ((exitcode = pclose(pipe_fp)) != 0) {
     dbprintf(("%s: index pipe returned %d\n",
-             debug_prefix_time(NULL), exitcode));
+      debug_prefix_time(NULL), exitcode));
   } else {
     dbprintf(("%s: index created successfully\n", debug_prefix_time(NULL)));
   }
@@ -962,7 +994,3 @@ extern backup_program_t dump_program, gnutar_program;
 backup_program_t *programs[] = {
   &dump_program, &gnutar_program, NULL
 };
-
-#ifdef KRB4_SECURITY
-#include "sendbackup-krb4.c"
-#endif