aboutsummaryrefslogtreecommitdiffstats
path: root/prepare-vpn-usb.sh
blob: 0c3653bdb727bb93d95ea1fc3881d60704440f04 (plain) (blame)
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
#!/bin/bash

set -o errexit

if [ ! -e /tmp/vpn-ramdisk ]; then
  mkdir /tmp/vpn-ramdisk
  mount -t ramfs -o size=1M ramfs /tmp/vpn-ramdisk
  chmod 0700 /tmp/vpn-ramdisk
fi

if [ ! -e /tmp/vpn-ramdisk/usb-mounted ]; then
  read -r -p "Enter path to USB device (empty for /dev/xvdi1 or /dev/xvdi, in that order): " usbdevice
  if [ -z "$usbdevice" ]; then
    if [ -e /dev/xvdi1 ]; then
      usbdevice="/dev/xvdi1"
    elif [ -e /dev/xvdi ]; then
      usbdevice="/dev/xvdi"
    fi
  fi
  mount "$usbdevice" /rw/config/vpn/
  touch /rw/config/vpn/no-userpassword.txt
  if [ -L /rw/config/vpn/vpn-client.conf ]; then
    echo "Relinking /rw/config/vpn/vpn-client.conf to" /rw/config/vpn/*.ovpn
    rm /rw/config/vpn/vpn-client.conf
    ln -s /rw/config/vpn/*.ovpn /rw/config/vpn/vpn-client.conf
  elif [ ! -e /rw/config/vpn/vpn-client.conf ]; then
    echo "Creating link /rw/config/vpn/vpn-client.conf to" /rw/config/vpn/*.ovpn
    ln -s /rw/config/vpn/*.ovpn /rw/config/vpn/vpn-client.conf
  else
    echo "vpn-client is probably a regular file. Doing nothing."
  fi
  touch /tmp/vpn-ramdisk/usb-mounted
fi

if [ ! -e /tmp/vpn-ramdisk/key-pass.txt ]; then
  read -s -r -p "Key password: " keypassword
  echo
  touch /tmp/vpn-ramdisk/key-pass.txt
  chown root:root /tmp/vpn-ramdisk/key-pass.txt
  chmod 0600 /tmp/vpn-ramdisk/key-pass.txt
  cat <<< "$keypassword" > /tmp/vpn-ramdisk/key-pass.txt
fi

if [ ! -e /tmp/vpn-ramdisk/userpassword.txt ]; then
  read -p "Username: " username
  read -s -r -p "Password: " upassword
  echo
  touch /tmp/vpn-ramdisk/userpassword.txt
  chown root:root /tmp/vpn-ramdisk/userpassword.txt
  chmod 0600 /tmp/vpn-ramdisk/userpassword.txt
  cat <<< "$username" > /tmp/vpn-ramdisk/userpassword.txt
  cat <<< "$upassword" >> /tmp/vpn-ramdisk/userpassword.txt
fi

Code dump for some of my projects as well as a mirror for other random or interesting projects. You can find my site at jimmy.axenhus.com