--- /dev/null
+/*\r
+ * This file is part of the IPCop Firewall.\r
+ *\r
+ * IPCop is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * IPCop is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with IPCop; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
+ *\r
+ * Copyright (C) 2003-06-25 Tim Butterfield <timbutterfield@mindspring.com>\r
+ *\r
+ * $Id: ipcoprscfg.c,v 1.2.2.6 2005/11/21 00:11:39 franck78 Exp $\r
+ *\r
+ */\r
+\r
+#include <stdio.h>\r
+#include <string.h>\r
+#include <stdlib.h>\r
+#include <unistd.h>\r
+#include <sys/types.h>\r
+#include <sys/stat.h>\r
+#include <fcntl.h>\r
+#include <grp.h>\r
+#include <dirent.h>\r
+#include "setuid.h"\r
+\r
+#define TMP_FILEZ "/tmp/TMPFILE.tar.gz"\r
+#define TMP_FILE "/tmp/TMPFILE.tar"\r
+\r
+/* check existence of a data file */\r
+int data_exists(const char *hostname) {\r
+ char fname[STRING_SIZE];\r
+ snprintf (fname, STRING_SIZE-1, MOUNTPOINT"/%s.dat", hostname);\r
+ return file_exists(fname);\r
+}\r
+\r
+\r
+int main(int argc, char**argv) {\r
+ int rshardware=0;\r
+ char command[STRING_SIZE];\r
+ char hostname[STRING_SIZE];\r
+\r
+ if (argc==2 && strcmp(argv[1],"--hardware")==0)\r
+ rshardware=1; // restore hardware settings\r
+\r
+ gethostname(hostname, STRING_SIZE-1);\r
+\r
+ /* Init setuid */\r
+ if (!initsetuid())\r
+ exit(1);\r
+\r
+ /* if a key file exists, an encrypted .dat is required */\r
+ if (!file_exists(BACKUP_KEY)) {\r
+ fprintf (stderr, "Missing encryption key\n");\r
+ exit (ERR_DECRYPT);\r
+ }\r
+ \r
+ \r
+ if (!data_exists(hostname)) {\r
+ fprintf (stderr, "Missing encrypted archive "MOUNTPOINT"/%s.dat archive\n", hostname);\r
+ exit (ERR_DAT);\r
+ }\r
+\r
+ /* decrypt .dat file to tmp file */\r
+ snprintf (command, STRING_SIZE-1, "/usr/bin/openssl des3 -d -salt -in "MOUNTPOINT"/%s.dat -out "TMP_FILEZ" -kfile "BACKUP_KEY" > /dev/null 2> /dev/null", hostname);\r
+ if (safe_system (command)) {\r
+ fprintf (stderr, "Couldn't decrypt "MOUNTPOINT"/%s.dat archive\n", hostname);\r
+ exit (ERR_DECRYPT);\r
+ }\r
+\r
+ /* create temporary directory for testing untar */\r
+ char tmp_dir[STRING_SIZE];\r
+\r
+ strcpy (tmp_dir,"cfg_XXXXXXX");\r
+ if (mkdtemp (tmp_dir)==NULL) {\r
+ unlink (TMP_FILEZ);\r
+ exit (ERR_ANY);\r
+ }\r
+\r
+ /* Start (test) untarring files from compressed archive */\r
+ snprintf (command, STRING_SIZE-1, "/bin/tar -C %s -xzvf "TMP_FILEZ" > /dev/null 2> /dev/null",tmp_dir);\r
+ if (safe_system (command)) {\r
+ fprintf (stderr, "Archive have errors!\n");\r
+ unlink (TMP_FILEZ);\r
+ exit (ERR_UNTARTST);\r
+ }\r
+\r
+ /* remove temporary directory */\r
+ snprintf (command, STRING_SIZE-1, "/bin/rm -rf %s > /dev/null 2> /dev/null",tmp_dir);\r
+ safe_system (command);\r
+ \r
+ /* Start (real) untarring files from compressed archive */\r
+ char extraX[STRING_SIZE] = "";\r
+ int retcode = 0;\r
+ if (rshardware==0) { /* extra eXclusion from restore */\r
+ strcpy (extraX, "-X "CONFIG_ROOT"/backup/exclude.hardware ");\r
+ }\r
+ snprintf (command, STRING_SIZE-1, "/bin/tar -C / -xzvf "TMP_FILEZ" -X "CONFIG_ROOT"/backup/exclude.system %s > /dev/null 2> /dev/null", extraX);\r
+ if (safe_system (command)) {\r
+ fprintf (stderr, "Error restoring archive\n");\r
+ retcode = ERR_UNTAR;\r
+ }\r
+\r
+ /* remove temporary archive copy */\r
+ unlink (TMP_FILEZ);\r
+\r
+ exit(retcode);\r
+}\r