summaryrefslogtreecommitdiffstats
path: root/retiolum/scripts/tinc_setup/propagate-config
blob: 27a9880ae9d46995326a80e7330d157d734ba77e (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
#!/bin/sh -x
set -euf

main() {

  if [ $# -ne 1 ]; then
    usage
    exit 1
  fi

  TINCDIR=$1

  HOSTN=$(cat $TINCDIR/tinc.conf | awk '/Name ?=/ {gsub(/Name ?= ?/, ""); print}')
  NICK="${HOSTN}_$(head /dev/urandom | tr -dc "0123456789" | head -c3)"

  IRCCHANNEL=${IRCCHANNEL:-"#krebs_incoming"}
  IRCSERVER=${IRCSERVER:-"irc.freenode.net"}
  IRCPORT=${IRCPORT:-6667}

  test -z ${HOSTSDIR+x} && find_hostdir

  test -z ${TELNET+x} && find_telnet

  (   echo "NICK $NICK";
  echo "USER $NICK $IRCSERVER bla : $NICK";
  echo "JOIN $IRCCHANNEL";
  sleep 23;
  echo "PRIVMSG $IRCCHANNEL : This is $HOSTN";
  sed "s/^\(.*\)/PRIVMSG $IRCCHANNEL : \1/" $HOSTSDIR/$HOSTN;
  sleep 5; ) | $TELNET $IRCSERVER $IRCPORT
}

exists() {
  type "$1" >/dev/null 2>/dev/null;
}

find_hostdir() {
  if [ -e "$TINCDIR/hosts" ]; then
    HOSTSDIR="$TINCDIR/hosts"
  else
    echo 'cannot find hostsdir of tinc, please specify with HOSTSDIR=...'
    exit 1
  fi
}

find_telnet() {
  if exists telnet >/dev/null; then
    TELNET=$(command -v telnet)
  else
    echo "cannot find telnet binary, please install telnet-client"
    echo "bailing out!"
    exit 1
  fi
}

usage() {
  echo './propagate-config $TINCDIR'
  echo 'If the hosts dir is not in $TINC_DIR you have to specify it using HOSTSDIR=$path_to_hostsdir ./propagate $TINCDIR.'
}

main "$@"