summaryrefslogtreecommitdiff
path: root/roles/ws/minet/files
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2020-12-28 21:29:49 +0100
committerChristian Pointner <equinox@spreadspace.org>2020-12-28 21:29:49 +0100
commit681f88d28fb3f1230f18bbdac23f75a9e62bd1b7 (patch)
tree0bb67d7144ca8785e0f06c30e7b1a6edf27e3907 /roles/ws/minet/files
parentadd host ch-equinox-t450s (diff)
ws/minet: initial version
Diffstat (limited to 'roles/ws/minet/files')
-rw-r--r--roles/ws/minet/files/_minet40
-rwxr-xr-xroles/ws/minet/files/minet111
-rw-r--r--roles/ws/minet/files/minet_helpers.sh53
3 files changed, 204 insertions, 0 deletions
diff --git a/roles/ws/minet/files/_minet b/roles/ws/minet/files/_minet
new file mode 100644
index 00000000..8f3e02ca
--- /dev/null
+++ b/roles/ws/minet/files/_minet
@@ -0,0 +1,40 @@
+#compdef _minet minet
+#autoload
+
+
+_minet () {
+ local cmd
+ if (( CURRENT > 2)); then
+ cmd=${words[2]}
+ # Set the context for the subcommand.
+ curcontext="${curcontext%:*:*}:pass-$cmd"
+ # Narrow the range of words we are looking at to exclude `pass'
+ (( CURRENT-- ))
+ shift words
+ # Run the completion for the subcommand
+ case "${cmd}" in
+ start)
+ if (( CURRENT == 2 )); then
+ _minet_complete_entries
+ fi
+ ;;
+ esac
+ else
+ local -a subcommands
+ subcommands=(
+ "start:Connect to network"
+ "stop:Disconnect from network"
+ "restart:Reconnect to network"
+ "suspend:Suspend current connection"
+ "resume:Resume suspended connection"
+ "status:Print connection status"
+ )
+ _describe -t commands 'minet' subcommands
+ fi
+}
+
+_minet_complete_entries () {
+ local IFS=$'\n'
+ local prefix="/usr/local/lib/minet"
+ _values -C 'connections' ${$(find -L "$prefix" \( -name minet_helpers.sh -o -name template \) -prune -o -type f -executable -print 2>/dev/null | sed -e "s#${prefix}/\{0,1\}##" | sort):-""}
+}
diff --git a/roles/ws/minet/files/minet b/roles/ws/minet/files/minet
new file mode 100755
index 00000000..6d6181e4
--- /dev/null
+++ b/roles/ws/minet/files/minet
@@ -0,0 +1,111 @@
+#!/bin/sh
+#
+# This is the main script of the Minimalistic Network Manager
+# Author: Christian Pointner <equinox@chaos-at-home.org>
+#
+
+NAME="Minimalistic Network Manager"
+CURRENT_FILE="/var/run/minet.current"
+SUSPENDED_FILE="$CURRENT_FILE.suspended"
+SCRIPT_ROOT="/usr/local/lib/minet"
+
+if [ -n "$1" ] && [ "$1" != "status" ] && [ "$USER" != "root" ]; then
+ echo "$NAME: only root can do that!"
+ exit 1
+fi
+
+. $SCRIPT_ROOT/minet_helpers.sh
+
+case "$1" in
+ start)
+ if [ -z "$2" ]; then
+ echo "$NAME: initializing..."
+ if [ -f $CURRENT_FILE ]; then
+ rm $CURRENT_FILE
+ fi
+ if [ -f $SUSPENDED_FILE ]; then
+ rm $SUSPENDED_FILE
+ fi
+ else
+ if [ -f $SUSPENDED_FILE ]; then
+ echo -n "$NAME: "
+ echo -n `cat $SUSPENDED_FILE`
+ echo " is suspended"
+ exit 0
+ fi
+ if [ -x $SCRIPT_ROOT/$2 ]; then
+ if [ -f $CURRENT_FILE ]; then
+ $SCRIPT_ROOT/`cat $CURRENT_FILE` stop
+ fi
+ $SCRIPT_ROOT/$2 start
+ echo $2 > $CURRENT_FILE
+ else
+ echo "Unknown Connection"
+ exit 2
+ fi
+ exit 0
+ fi
+ ;;
+ stop)
+ echo "$NAME: stopping..."
+ if [ -f $CURRENT_FILE ]; then
+ $SCRIPT_ROOT/`cat $CURRENT_FILE` stop
+ rm $CURRENT_FILE
+ fi
+ ;;
+ restart)
+ echo "$NAME: restarting..."
+ if [ -f $CURRENT_FILE ]; then
+ $SCRIPT_ROOT/`cat $CURRENT_FILE` stop
+ sleep 1
+ $SCRIPT_ROOT/`cat $CURRENT_FILE` start
+ fi
+ ;;
+ suspend)
+ echo -n "$NAME: "
+ if [ ! -f $CURRENT_FILE ]; then
+ echo "no active connection to suspend"
+ exit 0
+ else
+ echo -n "suspending connection: "
+ echo `cat $CURRENT_FILE`
+ ln $CURRENT_FILE $SUSPENDED_FILE
+ $SCRIPT_ROOT/`cat $CURRENT_FILE` stop
+ rm $CURRENT_FILE
+ fi
+ ;;
+ resume)
+ echo -n "$NAME: "
+ if [ ! -f $SUSPENDED_FILE ]; then
+ echo "no suspended connection to resume"
+ exit 0
+ else
+ echo -n "resuming connection: "
+ echo `cat $SUSPENDED_FILE`
+ ln $SUSPENDED_FILE $CURRENT_FILE
+ $SCRIPT_ROOT/`cat $CURRENT_FILE` start
+ rm $SUSPENDED_FILE
+ fi
+ ;;
+ status)
+ echo -n "$NAME status: "
+ if [ ! -f $CURRENT_FILE ]; then
+ if [ -f $SUSPENDED_FILE ]; then
+ echo -n `cat $SUSPENDED_FILE`
+ echo " is suspended"
+ exit 0
+ else
+ echo "not connected"
+ exit 0
+ fi
+ else
+ echo -n "connected with "
+ echo `cat $CURRENT_FILE`
+ fi
+ ;;
+ *)
+ echo "Usage: $0 (start [connection name]|stop|suspend|resume|status)"
+ ;;
+esac
+
+exit 0
diff --git a/roles/ws/minet/files/minet_helpers.sh b/roles/ws/minet/files/minet_helpers.sh
new file mode 100644
index 00000000..1b82e4a5
--- /dev/null
+++ b/roles/ws/minet/files/minet_helpers.sh
@@ -0,0 +1,53 @@
+# These are helper functions for the Minimalistic Network Manager
+# Author: Christian Pointner <equinox@chaos-at-home.org>
+
+activate_interface()
+{
+ echo 2 > /proc/sys/net/ipv6/conf/default/use_tempaddr
+ modprobe $2
+ while true; do
+ ip link set up dev $1 >/dev/null 2>&1
+ if [ $? -eq 0 ]; then
+ break
+ fi
+ echo -n "."
+ sleep 1
+ echo 2 > /proc/sys/net/ipv6/conf/$1/use_tempaddr
+ done
+ if [ $2 = "eth0" ]; then
+ sleep 2
+ ethtool -s $2 advertise 0x008
+ fi
+ echo ""
+}
+
+deactivate_interface()
+{
+ ip link set down dev $1 >/dev/null 2>&1
+ modprobe -r $2
+}
+
+wpa_select_ssid()
+{
+ NET=$(wpa_cli -i$1 list_networks | awk "BEGIN { FS = \"\\t\" } ; \$2 == \"$2\" { print(\$1) }")
+ if [ -n "$NET" ]; then
+ wpa_cli -i$1 select_network $NET
+ else
+ echo "SSID '$2' is not in wpa_supplicant.conf - ignoring it..."
+ fi
+}
+
+create_6to4()
+{
+ IPV6_ADDR=`printf "2002:%02x%02x:%02x%02x::1" \`echo "$2" | tr "." " "\``
+
+ ip tunnel add mode sit remote 192.88.99.1 name $1
+ ip link set dev $1 up
+ ip addr add dev $1 $IPV6_ADDR/48
+ ip -6 route add default dev $1
+}
+
+destroy_6to4()
+{
+ ip tunnel del name $1
+}