Imported Debian patch 2.5.2p1-1
[debian/amanda] / client-src / runtar.c
index c8e9e6b4f504fb6278ff6e34a372f852964ac185..f088ea13bf871f280308324e35b6c587d499c240 100644 (file)
  * file named AUTHORS, in the root directory of this distribution.
  */
 /*
- * $Id: runtar.c,v 1.11.4.2.6.1 2002/10/27 14:31:18 martinea Exp $
+ * $Id: runtar.c,v 1.24 2006/08/25 11:41:31 martinea Exp $
  *
  * runs GNUTAR program as root
+ *
+ * argv[0] is the runtar program name
+ * argv[1] is the config name or NOCONFIG
+ * argv[2] will be argv[0] of the gtar program
+ * ...
  */
 #include "amanda.h"
 #include "version.h"
+#include "util.h"
 
-int main P((int argc, char **argv));
+int main(int argc, char **argv);
 
-int main(argc, argv)
-int argc;
-char **argv;
+int
+main(
+    int                argc,
+    char **    argv)
 {
 #ifdef GNUTAR
     int i;
-#endif
-    int fd;
     char *e;
+    char *dbf;
+#endif
 
-    for(fd = 3; fd < FD_SETSIZE; fd++) {
-       /*
-        * Make sure nobody spoofs us with a lot of extra open files
-        * that would cause an open we do to get a very high file
-        * descriptor, which in turn might be used as an index into
-        * an array (e.g. an fd_set).
-        */
-       close(fd);
-    }
-
+    safe_fd(-1, 0);
     safe_cd();
 
     set_pname("runtar");
 
-    dbopen();
-    dbprintf(("%s: version %s\n", argv[0], version()));
+    /* Don't die when child closes pipe */
+    signal(SIGPIPE, SIG_IGN);
+
+    dbopen(DBG_SUBDIR_CLIENT);
+    if (argc < 3) {
+       error("%s: Need at least 3 arguments\n", debug_prefix_time(NULL));
+       /*NOTREACHED*/
+    }
+
+    dbprintf(("%s: version %s\n", debug_prefix_time(NULL), version()));
+
+    if (strcmp(argv[3], "--create") != 0) {
+       error("%s: Can only be used to create tar archives\n",
+             debug_prefix_time(NULL));
+       /*NOTREACHED*/
+    }
 
 #ifndef GNUTAR
 
@@ -69,17 +81,42 @@ char **argv;
 
 #else
 
+    /*
+     * Print out version information for tar.
+     */
+    do {
+       FILE *  version_file;
+       char    version_buf[80];
+
+       if ((version_file = popen(GNUTAR " --version 2>&1", "r")) != NULL) {
+           if (fgets(version_buf, (int)sizeof(version_buf), version_file) != NULL) {
+               dbprintf((GNUTAR " version: %s\n", version_buf));
+           } else {
+               if (ferror(version_file)) {
+                   dbprintf((GNUTAR " version: Read failure: %s\n", strerror(errno)));
+               } else {
+                   dbprintf((GNUTAR " version: Read failure; EOF\n"));
+               }
+           }
+       } else {
+           dbprintf((GNUTAR " version: unavailable: %s\n", strerror(errno)));
+       }
+    } while(0);
+
     if(client_uid == (uid_t) -1) {
        error("error [cannot find user %s in passwd file]\n", CLIENT_LOGIN);
+       /*NOTREACHED*/
     }
 
 #ifdef FORCE_USERID
     if (getuid() != client_uid) {
        error("error [must be invoked by %s]\n", CLIENT_LOGIN);
+       /*NOTREACHED*/
     }
 
     if (geteuid() != 0) {
        error("error [must be setuid root]\n");
+       /*NOTREACHED*/
     }
 #endif
 
@@ -87,14 +124,37 @@ char **argv;
     setuid(0);
 #endif
 
+    /* skip argv[0] */
+    argc--;
+    argv++;
+
+    dbprintf(("config: %s\n", argv[0]));
+    if (strcmp(argv[0], "NOCONFIG") != 0)
+       dbrename(argv[0], DBG_SUBDIR_CLIENT);
+    argc--;
+    argv++;
+
+
     dbprintf(("running: %s: ",GNUTAR));
-    for (i=0; argv[i]; i++)
-       dbprintf(("%s ", argv[i]));
+    for (i=0; argv[i]; i++) {
+       char *quoted;
+
+       quoted = quote_string(argv[i]);
+       dbprintf(("'%s' ", quoted));
+       amfree(quoted);
+    }
     dbprintf(("\n"));
+    dbf = dbfn();
+    if (dbf) {
+       dbf = stralloc(dbf);
+    }
+    dbclose();
 
     execve(GNUTAR, argv, safe_env());
 
     e = strerror(errno);
+    dbreopen(dbf, "more");
+    amfree(dbf);
     dbprintf(("execve of %s failed (%s)\n", GNUTAR, e));
     dbclose();