From 5b052a390e5e9dfd3171f6c791f10ae8f3c7aba1 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Thu, 21 Sep 2017 11:20:39 -0700 Subject: [PATCH] Checking in new phone registration scripts; restructuring directories; fixing README --- README | 8 +- .../register/{ => version_1}/README | 0 .../register/{ => version_1}/devices.dat | 0 .../{ => version_1}/register_device.sh | 0 .../register/version_2/change_default_pw.sh | 27 ++++ .../register/version_2/connect_device.sh | 81 ++++++++++ .../register/version_2/register_device.sh | 151 ++++++++++++++++++ 7 files changed, 265 insertions(+), 2 deletions(-) rename sentinel_setup/register/{ => version_1}/README (100%) rename sentinel_setup/register/{ => version_1}/devices.dat (100%) rename sentinel_setup/register/{ => version_1}/register_device.sh (100%) create mode 100755 sentinel_setup/register/version_2/change_default_pw.sh create mode 100755 sentinel_setup/register/version_2/connect_device.sh create mode 100755 sentinel_setup/register/version_2/register_device.sh diff --git a/README b/README index 923097bb1c..1638446e0a 100644 --- a/README +++ b/README @@ -79,7 +79,11 @@ default firewall setup from LEDE, we can do "mv /etc/config/firewall /etc/config/firewall.bak" to make it unreadable by LEDE UCI when it is initializing the firewall rules when the system is booting up. -10) Last, we need to copy the setup scripts in sentinel_setup/setup into /setup on +10) We need to also create /root/sentinel_setup/register and copy the scripts in +sentinel_setup/register/version_2 into it. These Shell scripts work with the +Android app that registers and deletes devices to and fro the router. + +11) Last, we need to copy the setup scripts in sentinel_setup/setup into /setup on our LEDE system. This contains a number of scripts. - "startup.sh" will be run by the rc.local script when LEDE is booting up. This script contains the initial firewall rules for Sentinel router and a number of workaround @@ -94,4 +98,4 @@ on the system. - "show" shows the active iptables rules. - "transfer" contains commands to transfer files through the "scp" command. -12) Reboot the system and we will have a working LEDE router for Sentinel system. +13) Reboot the system and we will have a working LEDE router for Sentinel system. diff --git a/sentinel_setup/register/README b/sentinel_setup/register/version_1/README similarity index 100% rename from sentinel_setup/register/README rename to sentinel_setup/register/version_1/README diff --git a/sentinel_setup/register/devices.dat b/sentinel_setup/register/version_1/devices.dat similarity index 100% rename from sentinel_setup/register/devices.dat rename to sentinel_setup/register/version_1/devices.dat diff --git a/sentinel_setup/register/register_device.sh b/sentinel_setup/register/version_1/register_device.sh similarity index 100% rename from sentinel_setup/register/register_device.sh rename to sentinel_setup/register/version_1/register_device.sh diff --git a/sentinel_setup/register/version_2/change_default_pw.sh b/sentinel_setup/register/version_2/change_default_pw.sh new file mode 100755 index 0000000000..04bf06d564 --- /dev/null +++ b/sentinel_setup/register/version_2/change_default_pw.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +# Print usage +if [ "$#" -eq 0 ] || [ "$1" == "-h" ]; then + echo "This is a simple script that change the pre-shared key of a router" + echo "using UCI command change password written in /etc/config/wireless" + echo "" + echo "Usage:" + echo " ./change_default_pw.sh [-h]" + echo " ./change_default_pw.sh [-ch ]" + echo "" + echo "Options:" + echo " -h show this usage" + echo " -ch change default into a specific password" + echo "" + +elif [ "$1" == "-ch" ]; then + # Change the wireless.key option in the config file. + PW=$2 + uci set wireless.default_radio0.key=$PW + uci set wireless.default_radio1.key=$PW + uci commit + /sbin/wifi + +else + echo "Unknown option. Please run ./change_default_pw.sh -h for usage." +fi diff --git a/sentinel_setup/register/version_2/connect_device.sh b/sentinel_setup/register/version_2/connect_device.sh new file mode 100755 index 0000000000..175799f2d4 --- /dev/null +++ b/sentinel_setup/register/version_2/connect_device.sh @@ -0,0 +1,81 @@ +#!/bin/sh + +# Print usage +if [ "$#" -eq 0 ] || [ "$1" == "-h" ]; then + echo "This is a simple script that register connected device into the system" + echo "/etc/config/hostapd-psk /etc/config/dhcp will be changed accordingly" + echo "~/sentinel_setup/register/register_device.sh will be executed" + echo "" + echo "Usage:" + echo " ./connect_device.sh [-h]" + echo " ./connect_device.sh [-co ]" + echo "" + echo "Options:" + echo " -h show this usage" + echo " -co connect a new device" + echo "" + +elif [ "$1" == "-co" ]; then + + # 0 + # Get password from $2 + # Supposing that key for radio0 and radio1 are equal + PW=$2 + + # Save default password from $4. If the argument is empty, let's use THE default one. + DEFAULT="1qaz2wsx3edc" + if [ ! -z "$4" ]; then + DEFAULT=$4 + fi + + # 1 + # Get MAC address and IP address from dhcp.leases file. + # Below scripts will find the most recently connected device by sorting the first column of lease file, + # which is time of lease expiry, in epoch time + # Before that, the file named devices.dat could not exist, so touch it first + touch ~/sentinel_setup/register/devices.dat + chmod 666 ~/sentinel_setup/register/devices.dat + + # now ready to use devices.dat. Get numbers of records in devices.dat + NR=$(cat ~/sentinel_setup/register/devices.dat | wc -l) + + # use temp file for procedure so that we don't touch original lease file + cp /tmp/dhcp.leases leases.temp + + for i in `seq 1 $NR` + do + #get line number(LN) + LN="${i}p" + #get target MAC address(TMAC) from devices.dat + TMAC=$(sed -n ${LN} ~/sentinel_setup/register/devices.dat | awk '{print $1}') + #remove the record with certain TMAC in the dhcp file so that we can get new one at the end + sed -e /${TMAC}/d leases.temp > leases.temp.temp + rm leases.temp + mv leases.temp.temp leases.temp + done + + # Filter done. There should be only one line in leases.temp, but just in + # case sort the epoch time + RECENT=$(sort -nrk1,1 leases.temp | head -1) + + # Get MAC, IP, NAME from RECENT + MAC=$(echo $RECENT | awk '{print $2}') + IP=$(echo $RECENT | awk '{print $3}') + # below was used when there were no name argument in the script + # NAME=$(echo $RECENT | awk '{print $4}') + NAME=$3 + + # 2 + # Add record to the database with ~/sentinel_setup/register/register_device.sh + ~/sentinel_setup/register/register_device.sh -a $MAC $IP $PW $NAME + + # 3 + # rewind the default password and apply config files + uci commit + ~/sentinel_setup/register/change_default_pw.sh -ch $DEFAULT + /sbin/wifi + +else + echo "Unknown option. Please run ./connect_device.sh -h for usage." + +fi diff --git a/sentinel_setup/register/version_2/register_device.sh b/sentinel_setup/register/version_2/register_device.sh new file mode 100755 index 0000000000..e9c28c1be6 --- /dev/null +++ b/sentinel_setup/register/version_2/register_device.sh @@ -0,0 +1,151 @@ +#!/bin/sh + +# Print usage +if [ "$#" -eq 0 ] || [ "$1" == "-h" ]; then + echo "Device registration utility for Sentinel system" + echo "This is a simple script that register a new device" + echo "into /etc/config/dhcp and /etc/config/hostapd-psk" + echo "Copyright (c) 2015-2017, Rahmadi Trimananda PLRG@UCIrvine" + echo "" + echo "Usage:" + echo " ./register_device.sh [-h]" + echo " ./register_device.sh [-a ]" + echo " ./register_device.sh [-l]" + echo " ./register_device.sh [-ln]" + echo " ./register_device.sh [-dm ]" + echo " ./register_device.sh [-dn ]" + echo "" + echo "Options:" + echo " -h show this usage" + echo " -a adding device by putting MAC address, desired IP address, key, and device name (optional)" + echo " -l show list of devices registered" + echo " -ln show list of names of devices registered" + echo " -dm delete a specific registered device with MAC address" + echo " -dn delete a specific registered device with name" + echo "" + +# add a device +elif [ "$1" == "-a" ]; then + if [ "$2" == "" ] || [ "$3" == "" ] || [ "$4" == "" ]; then + echo "Empty or incomplete parameters! Please run ./register_device.sh -h for usage." + + else + # Add a new device + MAC=$2 + IP=$3 + KEY=$4 + + # Keep a local log + echo "$MAC $IP $KEY $5" >> ~/sentinel_setup/register/devices.dat + + # Insert into /etc/config/hostapd-psk + echo "$MAC $KEY" >> /etc/config/hostapd-psk + + # Insert into /etc/config/dhcp + echo "" >> /etc/config/dhcp + + if [ "$5" != "" ]; then # If device-name is not empty + echo "# $5" >> /etc/config/dhcp + fi + + echo "config host" >> /etc/config/dhcp + echo " option ip '$IP'" >> /etc/config/dhcp + echo " option mac '$MAC'" >> /etc/config/dhcp + + if [ "$5" != "" ]; then # If device-name is not empty + echo " option name '$5'" >> /etc/config/dhcp + fi + + echo "Device added!" + fi + +# Print list of devices +elif [ "$1" == "-l" ]; then + echo "List of devices" + cat ~/sentinel_setup/register/devices.dat + echo "" + echo "/etc/config/hostapd-psk" + cat /etc/config/hostapd-psk + +# Print only the devices' names list +elif [ "$1" == "-ln" ]; then + cat ~/sentinel_setup/register/devices.dat | awk '{print $4}' + +# Delete device by MAC address +elif [ "$1" == "-dm" ]; then + # Make new file without the line containing specific MAC address then swap + sed -e "/$2/d" ~/sentinel_setup/register/devices.dat > tmp.dat + chmod 666 tmp.dat + rm ~/sentinel_setup/register/devices.dat + mv tmp.dat ~/sentinel_setup/register/devices.dat + + # update /etc/config/hostapd + sed -e "/$2/d" /etc/config/hostapd-psk > hostapd.tmp + rm /etc/config/hostapd-psk + mv hostapd.tmp /etc/config/hostapd-psk + + # update /etc/config/dhcp + # get line number of dhcp including the MAC address + LN=$(sed -n "/$2/=" /etc/config/dhcp) + HEAD=$(expr ${LN} - 3) + + # add 1, not 2, in case of no name line in target device + TAIL=$(expr ${LN} + 1) + sed "${HEAD},${TAIL}d" /etc/config/dhcp > dhcp.tmp + rm /etc/config/dhcp + mv dhcp.tmp /etc/config/dhcp + + #show on screen + echo "device deleted!" + + #apply change + /sbin/wifi + +# Delete by name. Similar to deleting with MAC +elif [ "$1" == "-dn" ]; then + # back up first + cp /etc/config/hostapd-psk /etc/config/hostapd-psk.bak + cp /etc/config/dhcp /etc/config/dhcp.bak + + #Multiple name arguments can be given. + VAR1=$1 + shift 1 + for arg in "$@" + do + NAME=${arg} + # Get MAC Address first looking up the devices.dat file + MAC=$(grep ${NAME} ~/sentinel_setup/register/devices.dat | awk '{print $1}') + + # Make new file without the line containing specific device name then swap + sed -e "/${NAME}/d" ~/sentinel_setup/register/devices.dat > tmp.dat + chmod 666 tmp.dat + rm ~/sentinel_setup/register/devices.dat + mv tmp.dat ~/sentinel_setup/register/devices.dat + + # update /etc/config/hostapd + sed -e "/${MAC}/d" /etc/config/hostapd-psk > hostapd.tmp + rm /etc/config/hostapd-psk + mv hostapd.tmp /etc/config/hostapd-psk + + # update /etc/config/dhcp + # get line number of dhcp including the MAC address + LN=$(sed -n "/${MAC}/=" /etc/config/dhcp) + HEAD=$(expr ${LN} - 3) + + # add 1, not 2, in case of no name in the dhcp file + TAIL=$(expr ${LN} + 1) + sed "${HEAD},${TAIL}d" /etc/config/dhcp > dhcp.tmp + rm /etc/config/dhcp + mv dhcp.tmp /etc/config/dhcp + done + + #show on screen + echo "device deleted!" + + #apply change + /sbin/wifi + +else + echo "Unknown option. Please run ./register_device.sh -h for usage." + +fi -- 2.34.1