OpenVPN client down script fails to restore original resolv.conf
The default installation of OpenVPN includes an up.sh and down.sh script in the /etc/openvpn directory. When using these scripts (i.e. openvpn —script-security 2 —up /etc/openvpn/up.sh —down /etc/openvpn/down.sh) the up script successfully backs up the original resolv.conf script to resolv.conf-tun0.sv but upon stopping the OpenVPN client the down script fails to restore the default resolv.conf. I’ve tracked the issue down to line 18 of /etc/openvpn/down.sh:
cp /etc/resolv.conf-"${dev}".sv /etc/resolv.conf
When running this command with verbose output you get the following error message:
cp: can't create '/etc/resolv.conf': File exists
(from redmine: issue id 5027, created on 2016-01-17, closed on 2016-03-18)
- Changesets:
- Revision 34c1dfa8 by Chris Kankiewicz on 2016-01-26T22:11:23Z:
main/openvpn: fix down script not restoring original resolv.conf
This patch fixes an error where, in the provided OpenVPN down script, the
cp command would fail due to the resolv.conf file already existing.
Instead of using cp we cat the file contents over the exising resolv.conf
file to avoid the error and preserve any symlinks that may exist.
fixes #5027
(cherry picked from commit bfdc08d49cfd221709ce29ad6f81a651461c71e2)