]> git.ipfire.org Git - ipfire-2.x.git/blob - src/misc-progs/ipcoprscfg.c
git-svn-id: http://svn.ipfire.org/svn/ipfire/IPFire/source@16 ea5c0bd1-69bd-2848...
[ipfire-2.x.git] / src / misc-progs / ipcoprscfg.c
1 /*
2 * This file is part of the IPCop Firewall.
3 *
4 * IPCop is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * IPCop is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with IPCop; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Copyright (C) 2003-06-25 Tim Butterfield <timbutterfield@mindspring.com>
19 *
20 * $Id: ipcoprscfg.c,v 1.2.2.6 2005/11/21 00:11:39 franck78 Exp $
21 *
22 */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <grp.h>
32 #include <dirent.h>
33 #include "setuid.h"
34
35 #define TMP_FILEZ "/tmp/TMPFILE.tar.gz"
36 #define TMP_FILE "/tmp/TMPFILE.tar"
37
38 /* check existence of a data file */
39 int data_exists(const char *hostname) {
40 char fname[STRING_SIZE];
41 snprintf (fname, STRING_SIZE-1, MOUNTPOINT"/%s.dat", hostname);
42 return file_exists(fname);
43 }
44
45
46 int main(int argc, char**argv) {
47 int rshardware=0;
48 char command[STRING_SIZE];
49 char hostname[STRING_SIZE];
50
51 if (argc==2 && strcmp(argv[1],"--hardware")==0)
52 rshardware=1; // restore hardware settings
53
54 gethostname(hostname, STRING_SIZE-1);
55
56 /* Init setuid */
57 if (!initsetuid())
58 exit(1);
59
60 /* if a key file exists, an encrypted .dat is required */
61 if (!file_exists(BACKUP_KEY)) {
62 fprintf (stderr, "Missing encryption key\n");
63 exit (ERR_DECRYPT);
64 }
65
66
67 if (!data_exists(hostname)) {
68 fprintf (stderr, "Missing encrypted archive "MOUNTPOINT"/%s.dat archive\n", hostname);
69 exit (ERR_DAT);
70 }
71
72 /* decrypt .dat file to tmp file */
73 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);
74 if (safe_system (command)) {
75 fprintf (stderr, "Couldn't decrypt "MOUNTPOINT"/%s.dat archive\n", hostname);
76 exit (ERR_DECRYPT);
77 }
78
79 /* create temporary directory for testing untar */
80 char tmp_dir[STRING_SIZE];
81
82 strcpy (tmp_dir,"cfg_XXXXXXX");
83 if (mkdtemp (tmp_dir)==NULL) {
84 unlink (TMP_FILEZ);
85 exit (ERR_ANY);
86 }
87
88 /* Start (test) untarring files from compressed archive */
89 snprintf (command, STRING_SIZE-1, "/bin/tar -C %s -xzvf "TMP_FILEZ" > /dev/null 2> /dev/null",tmp_dir);
90 if (safe_system (command)) {
91 fprintf (stderr, "Archive have errors!\n");
92 unlink (TMP_FILEZ);
93 exit (ERR_UNTARTST);
94 }
95
96 /* remove temporary directory */
97 snprintf (command, STRING_SIZE-1, "/bin/rm -rf %s > /dev/null 2> /dev/null",tmp_dir);
98 safe_system (command);
99
100 /* Start (real) untarring files from compressed archive */
101 char extraX[STRING_SIZE] = "";
102 int retcode = 0;
103 if (rshardware==0) { /* extra eXclusion from restore */
104 strcpy (extraX, "-X "CONFIG_ROOT"/backup/exclude.hardware ");
105 }
106 snprintf (command, STRING_SIZE-1, "/bin/tar -C / -xzvf "TMP_FILEZ" -X "CONFIG_ROOT"/backup/exclude.system %s > /dev/null 2> /dev/null", extraX);
107 if (safe_system (command)) {
108 fprintf (stderr, "Error restoring archive\n");
109 retcode = ERR_UNTAR;
110 }
111
112 /* remove temporary archive copy */
113 unlink (TMP_FILEZ);
114
115 exit(retcode);
116 }