#!/usr/bin/perl # # updates /etc/resolv.conf for openvpn (www.openvpn.net) # this hack (c) 2006 torh@bogus.net # # add the following to your client.ovpn file(s): # # up /path/to/resolvfix.pl # down /path/to/resolvfix.pl # # (place this file as resolvfix.pl in /path/to) umask(022); $ENV{'PATH'}="/bin:/usr/bin"; if($ENV{'script_type'} eq "down") { ## quit if we can't find a resolv.conf.ovpnbak file OR if that file ## is zero size. if(-f "/etc/resolv.conf.ovpnbak" && -s "/etc/resolv.conf.ovpnbak") { $move=`mv -f /etc/resolv.conf.ovpnbak /etc/resolv.conf`; } exit(0); } exit(0) unless(defined($ENV{'foreign_option_1'})); # if no options, just quit if($ENV{'script_type'} eq "up") { ## make a backup of the resolver file ## $copy=`cp -p /etc/resolv.conf /etc/resolv.conf.ovpnbak 2>/dev/stdout`; if($copy) { die "Cannot create backup of /etc/resolv.conf: $copy"; exit(1); } ## overwrite the original with the pushed search and nameserver ## parameters open(OUT,">/etc/resolv.conf") || die "Can't write to /etc/resolv.conf: $!"; foreach $env (keys %ENV) { if($env =~ /foreign_option/) { if($ENV{$env} =~ /DOMAIN\s+(\S+)/) { print OUT "search $1\n"; } if($ENV{$env} =~ /DNS\s+(\d+\.\d+\.\d+\.\d+)/) { print OUT "nameserver $1\n"; } } } close(OUT); exit(0); } else { die "$0 called in unknown context."; }