aboutsummaryrefslogtreecommitdiff
path: root/network/dnscrypt-proxy/rc.dnscrypt-proxy
blob: fd747759d1371733d487c45132c5f05da1364c07 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash

CONFIGFILE="/etc/default/dnscrypt-proxy"
DAEMON="/usr/sbin/dnscrypt-proxy"

. $CONFIGFILE

start_instance() {
    if [ ! -r ${DNSCRYPTCONFIG[$1]} ]; then
        echo "No configuration for instance $1 found!"
        return
    fi
    PIDFILE=$(grep -i "^[[:space:]]*PidFile[[:space:]]\+." ${DNSCRYPTCONFIG[$1]} | awk '{print $2}')
    if [ -z ${PIDFILE} ]; then
        echo "No PID configuration for instance $1 found!"
        return
    fi
    if [ -r ${PIDFILE} ]; then
        echo "dnscrypt-proxy (instance $1) already running!"
        return
    fi

    # dnscrypt-proxy will work without this, but it drops privileges before
    # seeding the PRNG. libevent tries to work around a missing /dev/urandom
    # but it's safer just to make sure it is available in the chroot.
    if [ -n "${CHROOTDIR[$1]}" ]; then
        if [ "$(readlink -f ${CHROOTDIR[$1]})" != "/" ]; then
            if [ ! -d ${CHROOTDIR[$1]} ]; then
                mkdir -p ${CHROOTDIR[$1]}
                chmod 755 ${CHROOTDIR[$1]}
            fi
            if [ ! -d ${CHROOTDIR[$1]}/dev ]; then
                mkdir -p ${CHROOTDIR[$1]}/dev
                chmod 755 ${CHROOTDIR[$1]}/dev
            fi
            if [ ! -c ${CHROOTDIR[$1]}/dev/urandom ]; then
                mknod -m 666 ${CHROOTDIR[$1]}/dev/urandom c 1 9
            fi
            if [ ! -c ${CHROOTDIR[$1]}/dev/random ]; then
                mknod -m 666 ${CHROOTDIR[$1]}/dev/random c 1 8
            fi
        fi
    fi

    $DAEMON ${DNSCRYPTCONFIG[$1]}
}

stop_instance() {
    if [ ! -r ${DNSCRYPTCONFIG[$1]} ]; then
        echo "No configuration for instance $1 found!"
        return
    fi
    PIDFILE=$(grep -i "^[[:space:]]*PidFile[[:space:]]\+." ${DNSCRYPTCONFIG[$1]} | awk '{print $2}')
    if [ -z ${PIDFILE} ]; then
        echo "No PID configuration for instance $1 found!"
        return
    fi
    if [ ! -r ${PIDFILE} ]; then
        echo "dnscrypt-proxy (instance $1) is not running!"
        return
    fi
    echo "Stopping dnscrypt-proxy (instance $1)..."
    kill $(cat ${PIDFILE})
}

status_instance() {
    if [ ! -r ${DNSCRYPTCONFIG[$1]} ]; then
        echo "No configuration for instance $1 found!"
        return
    fi
    PIDFILE=$(grep -i "^[[:space:]]*PidFile[[:space:]]\+." ${DNSCRYPTCONFIG[$1]} | awk '{print $2}')
    if [ -z ${PIDFILE} ]; then
        echo "No PID configuration for instance $1 found!"
        return
    fi
    if [ ! -r ${PIDFILE} ]; then
        echo "dnscrypt-proxy (instance $1) is not running."
        return
    fi
    PID=$(cat ${PIDFILE})
    if [ -z "$PID" ]; then
        echo "PID file is empty! dnscrypt-proxy (instance $1) does not appear to be running, but there is a stale PID file."
    elif kill -0 $PID ; then
        echo "dnscrypt-proxy (instance $1) is running."
    else
        echo "dnscrypt-proxy (instance $1) is not running, but there is a stale PID file."
    fi
}

start() {
    for i in `/usr/bin/seq 0 $((${#DNSCRYPTCONFIG[@]}-1))`
    do
        start_instance $i
    done
}

stop() {
    for i in `/usr/bin/seq 0 $((${#DNSCRYPTCONFIG[@]}-1))`
    do
        stop_instance $i
    done
}

status() {
    for i in `/usr/bin/seq 0 $((${#DNSCRYPTCONFIG[@]}-1))`
    do
        status_instance $i
    done
}

case "$1" in
    'start')
        start
        ;;
    'stop')
        stop
        ;;
    'restart')
        stop
        start
        ;;
    'status')
        status
        ;;
    *_start)
        INSTANCE=`echo $1 | /bin/cut -d '_' -f 1`
        start_instance $INSTANCE
        ;;
    *_stop)
        INSTANCE=`echo $1 | /bin/cut -d '_' -f 1`
        stop_instance $INSTANCE
        ;;
    *_restart)
        INSTANCE=`echo $1 | /bin/cut -d '_' -f 1`
        stop_instance $INSTANCE
        sleep 1
        start_instance $INSTANCE
        ;;
    *_status)
        INSTANCE=`echo $1 | /bin/cut -d '_' -f 1`
        status_instance $INSTANCE
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status|#_start|#_stop|#_restart}"
        exit 1
        ;;
esac