]>
Commit | Line | Data |
---|---|---|
c7b7a70d | 1 | #!/bin/bash |
70df8302 MT |
2 | ############################################################################### |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
c1a5a49b | 5 | # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> # |
70df8302 MT |
6 | # # |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
cf29614f | 21 | |
13f6473a MT |
22 | shopt -s nullglob |
23 | ||
c7b7a70d MT |
24 | NOW="$(date "+%Y-%m-%d-%H:%M")" |
25 | ||
26 | list_addons() { | |
27 | local file | |
28 | for file in /var/ipfire/backup/addons/includes/*; do | |
29 | if [ -f "${file}" ]; then | |
30 | basename "${file}" | |
31 | fi | |
32 | done | |
33 | ||
34 | return 0 | |
8e8bbd9d | 35 | } |
c7b7a70d MT |
36 | |
37 | process_includes() { | |
38 | local include | |
c7b7a70d MT |
39 | for include in $@; do |
40 | local file | |
41 | while read -r file; do | |
d5d3748b MT |
42 | # Skip any empty line (which will include /) |
43 | [ -n "${file}" ] || continue | |
44 | ||
13f6473a | 45 | for file in /${file}; do |
b275771f MT |
46 | if [ -e "${file}" ]; then |
47 | echo "${file}" | |
48 | fi | |
4f10c0b3 | 49 | done |
c7b7a70d MT |
50 | done < "${include}" |
51 | done | sort -u | |
5ad5a6bc | 52 | } |
c7b7a70d MT |
53 | |
54 | make_backup() { | |
55 | local filename="${1}" | |
56 | shift | |
57 | ||
58 | # Backup all addons first | |
59 | local addon | |
60 | for addon in $(list_addons); do | |
61 | make_addon_backup "${addon}" | |
62 | done | |
63 | ||
e5f3e039 | 64 | # Backup using global exclude/include definitions |
c7e0d73e | 65 | tar cvfz "${filename}" -C / \ |
c7b7a70d MT |
66 | --exclude-from="/var/ipfire/backup/exclude" \ |
67 | --exclude-from="/var/ipfire/backup/exclude.user" \ | |
51ed815f | 68 | $(process_includes "/var/ipfire/backup/include") \ |
e5f3e039 | 69 | $(process_includes "/var/ipfire/backup/include.user") \ |
c7b7a70d MT |
70 | "$@" |
71 | ||
72 | return 0 | |
cf29614f | 73 | } |
c7b7a70d MT |
74 | |
75 | restore_backup() { | |
76 | local filename="${1}" | |
77 | ||
fc717041 | 78 | # Extract backup |
3f8e70f6 MT |
79 | if ! tar xvzpf "${filename}" -C / \ |
80 | --exclude-from="/var/ipfire/backup/exclude" \ | |
81 | --exclude-from="/var/ipfire/backup/exclude.user"; then | |
fc717041 MT |
82 | echo "Could not extract backup" >&2 |
83 | return 1 | |
84 | fi | |
c7b7a70d | 85 | |
28797d48 TF |
86 | # Restart syslogd, httpd and suricata in case we've just loaded old logs |
87 | apachectl -k graceful | |
88 | /bin/kill -HUP `cat /var/run/suricata.pid 2> /dev/null` 2> /dev/null | |
89 | /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null | |
90 | ||
5562f26f AF |
91 | # remove wrong vnstat tag file |
92 | rm -f /var/log/vnstat/tag | |
93 | ||
34b7b986 AF |
94 | # create dhcpcd user |
95 | groupadd -g 52 dhcpcd | |
96 | useradd -c 'dhcpcd privsep user' \ | |
97 | -d /run/dhcpcd/chroot \ | |
98 | -g dhcpcd \ | |
99 | -s /bin/false \ | |
100 | -u 52 dhcpcd | |
101 | ||
c7b7a70d MT |
102 | # Run converters |
103 | ||
104 | # Outgoing Firewall | |
105 | if [ -d "/var/ipfire/outgoing" ]; then | |
106 | # Reset files | |
107 | local file | |
108 | for file in /var/ipfire/firewall/{config,outgoing} \ | |
109 | /var/ipfire/fwhosts/custom{hosts,groups,networks}; do | |
110 | : > "${file}" | |
111 | chown nobody:nobody "${file}" | |
112 | done | |
113 | ||
114 | # Run converter | |
115 | convert-outgoingfw | |
116 | ||
117 | # Remove old configuration | |
118 | rm -rf "/var/ipfire/outgoing" | |
119 | fi | |
120 | ||
121 | # External Access | |
122 | if [ -d "/var/ipfire/xtaccess" ]; then | |
123 | : > /var/ipfire/firewall/config | |
124 | chown nobody:nobody "/var/ipfire/firewall/config" | |
125 | ||
126 | # Run converter | |
127 | convert-xtaccess | |
128 | ||
129 | # Remove old configuration | |
130 | rm -rf "/var/ipfire/xtaccess" | |
131 | fi | |
132 | ||
133 | # DMZ Holes | |
134 | if [ -d "/var/ipfire/dmzholes" ] || [ -d "/var/ipfire/portfw" ]; then | |
135 | : > /var/ipfire/firewall/config | |
136 | chown nobody:nobody "/var/ipfire/firewall/config" | |
137 | ||
138 | # Run converter | |
139 | convert-dmz | |
140 | ||
141 | # Remove old configuration | |
142 | rm -rf "/var/ipfire/dmzholes" | |
143 | fi | |
144 | ||
145 | # Port Forwardings | |
146 | if [ -d "/var/ipfire/portfw" ]; then | |
147 | # Run converter | |
148 | convert-portfw | |
149 | ||
150 | # Remove old configuration | |
151 | rm -rf "/var/ipfire/portfw" | |
152 | fi | |
153 | ||
f1d982cc SS |
154 | # Convert location |
155 | convert-to-location | |
156 | ||
c7b7a70d MT |
157 | # Reload firewall |
158 | firewallctrl | |
159 | ||
8c273724 SS |
160 | # Snort to suricata converter. |
161 | if [ -d "/var/ipfire/snort" ]; then | |
162 | # Run converter | |
163 | convert-snort | |
164 | ||
165 | # Remove old configuration directory. | |
166 | rm -rf "/var/ipfire/snort" | |
167 | fi | |
168 | ||
4aa1382e SS |
169 | # IDS multiple providers converter. |
170 | if [ -e "/var/ipfire/suricata/rules-settings" ]; then | |
171 | # Run the converter | |
172 | convert-ids-multiple-providers | |
173 | fi | |
174 | ||
b3dbe9ef SS |
175 | # IDS backend converter. |
176 | if [ -e "/var/ipfire/suricata/oinkmaster.conf" ]; then | |
177 | # Run the converter | |
178 | convert-ids-backend-files | |
179 | fi | |
180 | ||
ecbf6676 MT |
181 | # Convert DNS settings |
182 | convert-dns-settings | |
183 | ||
6df8a22b AF |
184 | # move nobeeps if exist |
185 | [ -e "/var/ipfire/ppp/nobeeps" ] && mv /var/ipfire/ppp/nobeeps /var/ipfire/red/nobeeps | |
186 | ||
bbbb0b9e PM |
187 | # Replace previously used OpenVPN Diffie-Hellman parameter by ffdhe4096 |
188 | sed -i 's|/var/ipfire/ovpn/ca/dh1024.pem|/etc/ssl/ffdhe4096.pem|' /var/ipfire/ovpn/server.conf /var/ipfire/ovpn/n2nconf/*/*.conf | |
189 | ||
233baacd MT |
190 | # Update OpenVPN CRL |
191 | /etc/fcron.daily/openvpn-crl-updater | |
9eb2086e AB |
192 | |
193 | # Update OpenVPN N2N Client Configs | |
194 | ## Add providers legacy default line to n2n client config files | |
195 | # Check if ovpnconfig exists and is not empty | |
196 | if [ -s /var/ipfire/ovpn/ovpnconfig ]; then | |
197 | # Identify all n2n connections | |
198 | for y in $(awk -F',' '/net/ { print $3 }' /var/ipfire/ovpn/ovpnconfig); do | |
199 | # Add the legacy option to all N2N client conf files if it does not already exist | |
200 | if [ $(grep -c "Open VPN Client Config" /var/ipfire/ovpn/n2nconf/${y}/${y}.conf) -eq 1 ] ; then | |
201 | if [ $(grep -c "providers legacy default" /var/ipfire/ovpn/n2nconf/${y}/${y}.conf) -eq 0 ] ; then | |
202 | echo "providers legacy default" >> /var/ipfire/ovpn/n2nconf/${y}/${y}.conf | |
203 | fi | |
204 | fi | |
205 | done | |
206 | fi | |
233baacd | 207 | |
c7b7a70d | 208 | return 0 |
901a50cf | 209 | } |
c7b7a70d MT |
210 | |
211 | find_logfiles() { | |
c1a5a49b | 212 | local filelist=( /var/log/logwatch/* /var/log/messages* /var/log/*.log /var/log/**/*.log ) |
c7b7a70d MT |
213 | |
214 | echo "${filelist[@]}" | |
b90a7e56 | 215 | } |
c7b7a70d MT |
216 | |
217 | make_addon_backup() { | |
218 | local name="${1}" | |
219 | shift | |
220 | ||
221 | if [ ! -f "/var/ipfire/backup/addons/includes/${name}" ]; then | |
222 | echo "${name} does not have any backup includes" >&2 | |
223 | return 1 | |
224 | fi | |
225 | ||
226 | local filename="/var/ipfire/backup/addons/backup/${name}.ipf" | |
227 | ||
228 | tar cvzf "${filename}" \ | |
229 | $(process_includes "/var/ipfire/backup/addons/includes/${name}") | |
8e8bbd9d | 230 | } |
c7b7a70d MT |
231 | |
232 | restore_addon_backup() { | |
233 | local name="${1}" | |
234 | ||
235 | if [ -d "/tmp/${name}.ipf" ]; then | |
236 | mv "/tmp/${name}.ipf" "/var/ipfire/backup/addons/backup/${name}.ipf" | |
237 | fi | |
238 | ||
fc717041 MT |
239 | # Extract backup |
240 | if ! tar xvzpf "/var/ipfire/backup/addons/backup/${name}.ipf" -C /; then | |
241 | echo "Could not extract backup" >&2 | |
242 | return 1 | |
243 | fi | |
a609bfb0 | 244 | } |
8e8bbd9d | 245 | |
c7b7a70d MT |
246 | main() { |
247 | local command="${1}" | |
248 | shift | |
249 | ||
c7b7a70d MT |
250 | case "${command}" in |
251 | include) | |
175f5c06 MT |
252 | local filename="${1}" |
253 | ||
254 | if [ -z "${filename}" ]; then | |
51ed815f | 255 | filename="/var/ipfire/backup/${NOW}.ipf" |
175f5c06 MT |
256 | fi |
257 | ||
c7b7a70d MT |
258 | make_backup "${filename}" $(find_logfiles) |
259 | ;; | |
260 | ||
261 | exclude) | |
175f5c06 MT |
262 | local filename="${1}" |
263 | ||
264 | if [ -z "${filename}" ]; then | |
51ed815f | 265 | filename="/var/ipfire/backup/${NOW}.ipf" |
175f5c06 MT |
266 | fi |
267 | ||
c7b7a70d MT |
268 | make_backup "${filename}" |
269 | ;; | |
270 | ||
271 | restore) | |
175f5c06 MT |
272 | local filename="${1}" |
273 | ||
274 | if [ -z "${filename}" ]; then | |
275 | filename="/tmp/restore.ipf" | |
276 | fi | |
277 | ||
d9db9160 | 278 | restore_backup "${filename}" |
c7b7a70d MT |
279 | ;; |
280 | ||
281 | addonbackup) | |
282 | make_addon_backup "$@" | |
283 | ;; | |
cf29614f | 284 | |
c7b7a70d MT |
285 | restoreaddon) |
286 | restore_addon_backup "${1/.ipf/}" | |
287 | ;; | |
84578512 | 288 | |
c7b7a70d | 289 | iso) |
175f5c06 MT |
290 | # Desired backup filename |
291 | local filename="/var/ipfire/backup/${NOW}.ipf" | |
292 | ||
c7b7a70d | 293 | if make_backup "${filename}"; then |
45a5df5a | 294 | /usr/local/bin/backupiso "${NOW}" |
c7b7a70d MT |
295 | fi |
296 | ;; | |
297 | ||
298 | makedirs) | |
299 | mkdir -p /var/ipfire/backup/addons/{backup,includes} | |
300 | ;; | |
301 | ||
4f10c0b3 MT |
302 | list) |
303 | process_includes "/var/ipfire/backup/include" "/var/ipfire/backup/include.user" | |
304 | ;; | |
305 | ||
c7b7a70d MT |
306 | /var/ipfire/backup/*.ipf|/var/ipfire/backup/addons/backup/*.ipf|/var/tmp/backupiso/*.iso) |
307 | unlink "${command}" | |
308 | ;; | |
309 | ||
310 | *) | |
311 | echo "${0}: [include|exclude|restore|addonbackup <addon>|restoreaddon <addon>|iso]" >&2 | |
312 | return 2 | |
313 | ;; | |
314 | esac | |
315 | ||
316 | return $? | |
cf29614f | 317 | } |
c7b7a70d MT |
318 | |
319 | main "$@" || exit $? |