]>
git.ipfire.org Git - people/trikolon/ipfire-2.x.git/blob - src/misc-progs/ipcopbkcfg.c
2 * This file is part of the IPCop Firewall.
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.
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.
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
18 * Copyright (C) 2003-06-25 Tim Butterfield <timbutterfield@mindspring.com>
20 * $Id: ipcopbkcfg.c,v 1.2.2.6 2005/11/20 23:20:13 franck78 Exp $
28 #include <sys/types.h>
36 #define EXCLUDE_HARDWARE "exclude.hardware" // exclude file not used on backup but only optionally on restore
37 #define TMP_TAR "/tmp/backup.tar"
39 char tempincfilename
[STRING_SIZE
] = ""; /* temp include file name */
40 char tempexcfilename
[STRING_SIZE
] = ""; /* temp exclude file name */
41 char temptarfilename
[STRING_SIZE
] = "";
43 /* add fname contents to outfile */
44 void add_file(int outfile
, const char *fname
, int verbose
)
47 char fbuff
[STRING_SIZE
];
49 if (!(freadfile
= fopen(fname
, "r"))) {
54 while (fgets(fbuff
, STRING_SIZE
-1, freadfile
) != NULL
) {
59 /* trim string in place - don't remove spaces in middle */
62 if (*ch
== '\r' || *ch
== '\n') {
70 if (*ch
== '\t' || *ch
== ' ') {
81 /* remove trailing spaces */
82 ch
= fbuff
+ strlen(fbuff
) - 1;
84 if (*ch
== '\t' || *ch
== ' ') {
92 /* validate name and add it */
93 chdir ("/"); /* support both absolute and relative path */
95 if (file_exists_w(fbuff
)) {
97 write(outfile
, fbuff
, strlen(fbuff
));
99 fprintf(stdout
, " %s", fbuff
);
107 /* combine files starting with fnamebase into outfile */
108 int cmb_files(int outfile
, const char *fnamebase
, int verbose
)
110 /* scan the directory and add matching files */
111 struct dirent
**namelist
;
113 char addfilename
[STRING_SIZE
];
115 /* scan the directory and get a count of the files */
116 if ((namecount
=scandir(CONFIG_ROOT
"/backup", &namelist
, 0, alphasort
))<0) {
117 fprintf(stderr
, "No files found\n");
121 /* process the scanned names */
122 while (namecount
--) {
123 /* check names - compare beginning of name, ignoring case, ignore EXCLUDE_HARDWARE */
124 if ((strncasecmp(fnamebase
, namelist
[namecount
]->d_name
, strlen(fnamebase
))==0) &&
125 (strncmp(EXCLUDE_HARDWARE
,namelist
[namecount
]->d_name
, strlen(EXCLUDE_HARDWARE
)))) {
126 /* add the contents for this name to output file */
127 sprintf(addfilename
, CONFIG_ROOT
"/backup/%s", namelist
[namecount
]->d_name
);
129 fprintf(stdout
, "%s\n", namelist
[namecount
]->d_name
);
130 add_file(outfile
, addfilename
, verbose
);
131 free(namelist
[namecount
]);
133 fprintf(stdout
, "\n");
140 void exithandler(void)
142 /* clean up temporary files */
144 unlink (temptarfilename
);
146 unlink (tempincfilename
);
148 unlink (tempexcfilename
);
151 int main(int argc
, char**argv
)
154 char command
[STRING_SIZE
];
155 char hostname
[STRING_SIZE
];
156 int includefile
, excludefile
;
161 if (argc
==2 && strcmp(argv
[1],"--verbose")==0)
162 verbose
=1; // display to stdout wich (ex|in)clude files are used
164 gethostname(hostname
, STRING_SIZE
-1);
166 if (!file_exists(BACKUP_KEY
)) {
167 fprintf (stderr
, "Couldn't locate encryption key\n");
171 /* now exithandler will have something to erase */
174 /* combine every include and exclude files in backup directory into two temp file
175 * at the exception of exclude.hardware only used optionally on restore */
176 /* create/open temp output file */
177 // Todo: use -X exclude.files and for include.files, build the list on command line
178 // to avoid unneccesary files manipulations
179 strcpy (tempincfilename
, "/tmp/backup-inclusion.XXXXXX");
180 strcpy (tempexcfilename
, "/tmp/backup-exclusion.XXXXXX");
181 if ( (!(includefile
= mkstemp (tempincfilename
)) > 0) ||
182 (!(excludefile
= mkstemp (tempexcfilename
)) > 0) ){
183 fprintf(stderr
, "Couldn't create temporary file.\n");
186 cmb_files(includefile
, "include.", verbose
);
188 cmb_files(excludefile
, "exclude.", verbose
);
191 /* Create temporary tarfile */
192 strcpy (temptarfilename
, TMP_TAR
);
194 /* Start tarring files to temp archive
195 W (verify) and z (compress) tar options can't be used together, so separate tar from gzip */
196 snprintf (command
, STRING_SIZE
-1, "/bin/tar -T %s -X %s -C / -cWf %s > /dev/null 2> /dev/null",
197 tempincfilename
, tempexcfilename
, temptarfilename
);
198 if (safe_system (command
)) {
199 fprintf (stderr
, "Couldn't create %s file\n", temptarfilename
);
202 unlink (tempincfilename
);
203 strcpy (tempincfilename
,"");
204 unlink (tempexcfilename
);
205 strcpy (tempincfilename
,"");
207 /* Compress archive */
208 snprintf (command
, STRING_SIZE
-1, "/bin/gzip -c < %s > "MOUNTPOINT
"/%s.tar.gz", temptarfilename
, hostname
);
209 if (safe_system (command
)) {
210 fprintf (stderr
, "Couldn't create "MOUNTPOINT
"%s.tar.gz file\n", hostname
);
213 unlink (temptarfilename
);
214 strcpy (temptarfilename
,"");
216 /* Display to stdout include files names */
217 snprintf (command
, STRING_SIZE
-1, "/bin/tar -ztf "MOUNTPOINT
"/%s.tar.gz", hostname
);
218 if (safe_system (command
)) {
219 fprintf (stderr
, "Couldn't read %s.tar.gz file\n", hostname
);
223 /* Encrypt archive */
224 snprintf (command
, STRING_SIZE
-1,
225 "/usr/bin/openssl des3 -e -salt -in "MOUNTPOINT
"/%s.tar.gz "
226 "-out "MOUNTPOINT
"/%s.dat -kfile " BACKUP_KEY
, hostname
, hostname
);
227 if (safe_system (command
)) {
228 fprintf (stderr
, "Couldn't encrypt archive\n");
231 snprintf (command
, STRING_SIZE
-1, MOUNTPOINT
"/%s.tar.gz", hostname
);
234 /* Make sure web can overwrite */
235 snprintf (command
, STRING_SIZE
-1, MOUNTPOINT
"/%s.dat", hostname
);
236 chown (command
, 99, 99);