summaryrefslogtreecommitdiffstats
path: root/src/connections/openvpn
blob: 170d58258f0b8f4ab22481a5e7266ee831a26f89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /bin/bash
# Originally contributed by Thomas Jost: https://bugs.archlinux.org/task/21490

. /usr/lib/network/network

openvpn_up() {
    load_profile "$1"
    [[ -z "$OVPN_CONFIG" ]] && OVPN_CONFIG="/etc/openvpn/openvpn.conf"
    [[ -z "$OVPN_PID_FILE" ]] && OVPN_PID_FILE="/run/openvpn-$1.pid"
    [[ -z "$OVPN_FLAGS" ]] && OVPN_FLAGS=""

    OVPN_CONF_DIR="`dirname $OVPN_CONFIG`"
    OVPN_CONF_FILE="`basename $OVPN_CONFIG`"

    /usr/sbin/openvpn --writepid $OVPN_PID_FILE --daemon --cd "$OVPN_CONF_DIR" --config "$OVPN_CONF_FILE" $OVPN_FLAGS

    if [[ $? -ne 0 ]]; then
       report_fail "OpenVPN connection failed"
       exit 1
    fi

    # Generate a new resolv.conf
    if [[ -n "$DNS" ]]; then
        [[ -e /etc/resolv.conf ]] && cp /etc/resolv.conf /tmp/openvpn-$1-resolv.conf
        : >/etc/resolv.conf
        [[ -n "$DOMAIN" ]] && echo "domain $DOMAIN" >>/etc/resolv.conf
        [[ -n "$SEARCH" ]] && echo "search $SEARCH" >>/etc/resolv.conf
        for dns in "${DNS[@]}"; do
            echo "nameserver $dns" >>/etc/resolv.conf
        done
        for dnsoption in "${DNS_OPTIONS[@]}"; do
            echo "options $dnsoption" >>/etc/resolv.conf
        done
    fi
}

openvpn_down() {
    load_profile "$1"
    [[ -z "$OVPN_PID_FILE" ]] && OVPN_PID_FILE="/run/openvpn-$1.pid"

    kill $(head -1 $OVPN_PID_FILE)
    rm $OVPN_PID_FILE

    # Restore an old resolv.conf
    if [[ -e /tmp/openvpn-$1-resolv.conf ]]; then
        mv -f /tmp/openvpn-$1-resolv.conf /etc/resolv.conf
    fi
}

openvpn_$1 "$2"
exit $?
# vim: ft=sh ts=4 et sw=4: