summarylogtreecommitdiffstats
path: root/kvpnc
blob: 50c5aa70c56638b72cc1c1d72441c5acd5500ea6 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/bash

. /etc/conf.d/kvpnc.conf

COMMAND_TIMEOUT=3
NAME="kvpnc"
DESC="Kerio Control VPN client"

dialogbox () {
  dialog \
    --backtitle "$DAEMON_NAME configuration" \
    --stdout \
    $5 \
    --title "$2" \
    "$1" "\n$3" 9 78 $4
}

inputbox () {
  dialogbox --inputbox "$1" "$2" "$3"
}

passwordbox () {
  # Insecure means that asterisks will be shown in the input field
  dialogbox --passwordbox "$1" "$2" "$3" --insecure
}

yesnobox () {
  dialogbox --yesno "$1" "$2" "$3"
  case "$?" in
    0)
      echo yes
      ;;
    1)
      echo no
      ;;
    *)
      ;;
  esac
}

messagebox () {
    dialogbox --msgbox "$1" "$2"
}

die () {
  echo "Procedure aborted: $2" >&2
  exit $1
}

execute_with_timeout() {
  local PID
  local TIMEOUT
  local ITERATION
  TIMEOUT=$1
  shift
  "$@" &
  PID=$!
  ITERATION=0
  while [ $ITERATION -lt $TIMEOUT ]; do
    # Check if the PID is still running
    [ ! -d "/proc/$PID" ] && return 0
    # Sleep 1 second for each iteration
    sleep 1
    ((ITERATION++))
  done
  # If the process is still running then try to kill it
  [ -d "/proc/$PID" ] && killtree $PID 9
  return 1
}

killtree() {
  # Kill a process and all its children (from children to parent)
  local _CHILD
  for _CHILD in $(ps -o pid --no-headers --ppid $1); do
    killtree ${_CHILD} ${2-TERM}
  done
  kill -${2-TERM} $1 1>&2 2> /dev/null
}

configure() {
  local SERVER_NAME
  local PORT_NUMBER
  local SSL_REPLY
  local AUTODETECT_FP
  local DETECTED_FP
  local ACCEPT_FP
  local FINGERPRINT
  local USER_NAME
  local PASSWORD
  local CONFIRM_PW
  local XOR_PW
  local ITERATION

  SERVER_NAME=$(inputbox \
    "Kerio VPN server address" \
    "Please enter the host name or IP address of your Kerio VPN server." \
  )
  [ -z "$SERVER_NAME" ] && die 2 "The Kerio VPN server address must be set."

  PORT_NUMBER=$(inputbox \
    "Kerio VPN server port number" \
    "Please enter the TCP port number of your Kerio VPN server." \
    "4090" \
  )
  [ -z "$PORT_NUMBER" ] && die 3 "The Kerio VPN server port number must be set."

  SSL_REPLY=$(execute_with_timeout $COMMAND_TIMEOUT bash -c "echo | \
      openssl s_client -connect $SERVER_NAME:$PORT_NUMBER 2> /dev/null"
  )
  [ -z $SSL_REPLY ] && die 4 "Unable to connect to the Kerio VPN server"

  AUTODETECT_FP=$(yesnobox \
    "Autodetect fingerprint" \
    "Automaticaly detect fingerprint from the Kerio VPN server?" \
  )
  [ -z "$AUTODETECT_FP" ] && die 5 "The user has cancelled."

  if [ "$AUTODETECT_FP" = "yes" ]
  then
    # Detect the SSL server fingerprint
    DETECTED_FP=$(echo "$SSL_REPLY" | \
      openssl x509 -fingerprint -md5 -noout | \
      sed s'/.*=//'
    )
    # A fingerprint was detected?
    if [ -z "$DETECTED_FP" ]
    then
      # Fingerprint not detected, ask for manual input
      messagebox \
        "Automatic fingerprint detection failed" \
        "The Kerio VPN server fingerprint was not automatically detected."
      AUTODETECT_FP="no"
    else
      # Accept the autodetected fingerprint?
      ACCEPT_FP=$(yesnobox \
        "Accept detected fingerprint?" \
        "The automatically detected fingerprint is $DETECTED_FP.\n\n
Please check if the automaticaly detected fingerprint is correct." \
      )
      [ $ACCEPT_FP = "yes" ] && FINGERPRINT="$DETECTED_FP"
    fi
  fi

  if [ "$AUTODETECT_FP" = "no" ]
  then
    # Ask for SSL server fingerprint
    FINGERPRINT=$(inputbox \
      "Kerio VPN server fingerprint" \
      "Enter the Kerio VPN server's fingerprint."
    )
  fi
  [ -z "$FINGERPRINT" ] && die 6 "The Kerio VPN server fingerprint must be set."

  USER_NAME=$(inputbox \
    "Kerio VPN server user name" \
    "Please enter the user name of your Kerio VPN server account." \
  )
  [ -z "$USER_NAME" ] && die 7 "User name must be set."

  PASSWORD=$(passwordbox \
    "Kerio VPN server password for user $USER_NAME" \
    "Please enter the password of your Kerio VPN server account." \
  )
  [ -z "$PASSWORD" ] && die 8 "Password must be set."

  CONFIRM_PW=$(passwordbox \
    "Kerio VPN server password for user $USER_NAME" \
    "Please confirm the previous password of your Kerio VPN server account." \
  )
  [ "$CONFIRM_PW" != "$PASSWORD" ] && die 9 "Confirmed password is different."

  for ITERATION in $(echo -n "$PASSWORD" | od -t d1 -A n)
  do
    XOR_PW=$(printf "%s%02x" "$XOR_PW" $((ITERATION ^ 85)))
  done
	cat > "$CFGFILE" << EOF
<config>
  <connections>
    <connection type="persistent">
      <server>${SERVER_NAME}</server>
      <port>${PORT_NUMBER}</port>
      <username>${USER_NAME}</username>
      <password>XOR:${XOR_PW}</password>
      <fingerprint>${FINGERPRINT}</fingerprint>
      <active>1</active>
    </connection>
  </connections>
</config>
EOF
	chmod 0600 "$CFGFILE"
}

case "$1" in
  start)
    # Check and prepare environment
    [ ! -f "$CFGFILE" ] && die 1 "Configuration file not found, configure with $0 configure"
    [ ! -d "$LIBDIR" ] && mkdir -p "$LIBDIR"
    [ ! -d "$LOGDIR" ] && mkdir -p "$LOGDIR"
    [ ! -h "$LIBDIR/logs" ] && ln -s -f -n "$LOGDIR" "$LIBDIR/logs"
    # Start the daemon and append the output to the system logger
    "$BINSVC" "$LIBDIR" > /dev/null
    # Get exit status of the first command BEFORE the pipe
    RET=$?
    if [ $RET -gt 0 ]; then
      die 1 "The kvpnsvc daemon died with exit code $RET"
    else
      pidof -o %PPID kvpncsvc > "$PIDFILE"
      mac=$(grep -a MAC "$LOGDIR/debug.log" | tail -1 | tr - : | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')
      ip link set kvnet addr $mac
    fi
    ;;
  stop)
    killall -q kvpncsvc
    rm -f "$PIDFILE"
    ;;
  configure)
    configure
    ;;
  *)
    echo "usage: $0 {start|stop|configure}"
    ;;
esac