diff --git a/README.md b/README.md index b2c0de9..30c917b 100644 --- a/README.md +++ b/README.md @@ -38,28 +38,22 @@ Use terminal to issue this command: ```bitsnet``` ``` ## How to install -Fire up a terminal and issue these commands: +- Fire up a terminal and issue these commands: ``` git clone https://github.com/OSDLabs/BitsnetLogin cd BitsnetLogin -``` -- Open the bitsnetrc file and specify your username and password -- Save and close -NOTE: You'll need to use this command if upgrading from version 1.x.x: -``` -rm ~/.bitsnetrc -``` -- Install the application: -``` ./install ``` -You can now login via terminal (```bitsnet```) or by launching the app +- You can now login via terminal (`bitsnet`) or by launching the app from launcher +- Run `man bitsnet` or `bitsnet -h` for help + +NOTE: You'll need to use this command if upgrading from version 1.x.x: `rm ~/.bitsnetrc` ## Suggestion, comments or complaints You can add your suggestions, complaints or any bugs you find [here](https://github.com/OSDLabs/BitsnetLogin/issues). ## Author -[UTkarsh Maheshwari](https://github.com/UtkarshMe), +[UTkarsh Maheshwari](https://github.com/UtkarshMe), **[OSDLabs](https://github.com/OSDLabs)** ## License diff --git a/bitsnet b/bitsnet index 7dc0e67..c574456 100755 --- a/bitsnet +++ b/bitsnet @@ -1,19 +1,70 @@ #!/bin/bash +# +# bitsnet +# Script for logging into the bitsnet +# + + +# # Load util functions -source ~/.local/lib/bitsnet/util.sh +# +if ! source /usr/lib/bitsnet/util.sh > /dev/null +then + # not found + echo "Could not load util file" + echo "Reinstall program" + + notify-send 'BITSnet' "Please reinstall program" \ + --icon=network-transmit --hint=int:transient:1 + exit 1 +fi + +# # Add a configuration file -source ~/.bitsnetrc +# +if ! grep -s "username\[1\]" ~/.bitsnetrc > /dev/null +then + # not found + echo "No bitsnetrc found" + + # create a new bitsnetrc file from the sample file + echo -n "Creating a bitsnetrc file... " + if ! cp /etc/bitsnet/bitsnetrc ~/.bitsnetrc + then + echo "failed" + exit 1 + else + echo "done" + fi +else + source ~/.bitsnetrc +fi + +# +# check if we can find at least one pair of credentials +# +if [[ ${username[1]} == "" ]] +then + echo "Please add credentials to ~/.bitsnetrc" + notify-send 'BITSnet' "Please add credentials to ~/.bitsnetrc" \ + --icon=network-transmit --hint=int:transient:1 + exit 1 +fi + + +# # Parse arguments +# while getopts "u:p:dhUofwq" o; do case "${o}" in u) - username=${OPTARG} + username[1]=${OPTARG} ;; p) - password=${OPTARG} + password[1]=${OPTARG} ;; d) debug=1 @@ -37,59 +88,83 @@ while getopts "u:p:dhUofwq" o; do ;; h | * | [?]) show_help + exit 1 ;; esac done + + +# +# check if login is forced +# if [[ "$f" == "1" ]]; then debug_msg "Forcing login attempt" fi +# +# display login url for debugging +# debug_msg "LoginURL: $login_url" -# Check if already logged in +# +# Check and exit if already logged in +# if [[ "$f" != "1" ]]; then debug_msg "Checking if already connected" - if wget -qT 5 -t 1 --spider --no-check-certificate https://google.com + if wget -qT 5 -t 1 --spider --no-check-certificate https://google.com -O /dev/null then send_msg "Already logged into the BITSnet" - exit + exit 0 fi fi -# Check if using wifi or ethernet +# +# Get device info +# debug_msg "" debug_msg "Checking device info" dev=$(get_device) debug_msg "dev = $dev" + +# # Exit if dev = 0 +# if [[ "$f" == "0" && "$dev" == "0" ]]; then debug_msg "Exiting because dev is zero" send_msg "Network interface unknown." - exit + exit 1 fi -# Log into Cisco Hostel Wifi router if dev = 2 +# +# Log into Cisco Hostel Wifi router if dev = 2 and IP is not of library +# if [[ "$dev" == "2" && "$(hostname -I)" != "10.20"* ]]; then cisco=1 + debug_msg "Connected to hostel wifi" fi -# Login +# +# Login to the Cisco hostel wifi router +# debug_msg "" debug_msg "Logging in" -# If dev is wifi and IP is not library IP if [[ $cisco == "1" ]]; then - # Log into the router - debug_msg "Connected to wifi. Sending request to cisco router form" + debug_msg "Sending request to cisco router form" router_login fi + +# # Log into the ldap +# All ID in the username array from bitsnetrc file are tried +# exit when successful login +# debug_msg "Logging into the LDAP" i=1 while [[ ! -z ${username[$i]} ]] @@ -100,6 +175,7 @@ do # Extract reply reply=$(extract_msg $reply) + # if logged in, get out if [[ "$reply" == "You have successfully logged in" ]]; then break else @@ -110,13 +186,17 @@ do done +# # Display reply or send a notification +# debug_msg "" send_msg "$reply" debug_msg "Reply sent" +# # Exit +# debug_msg "" debug_msg "Exiting" -exit +exit 0 diff --git a/bitsnet.1.gz b/bitsnet.1.gz new file mode 100644 index 0000000..72a3b6e Binary files /dev/null and b/bitsnet.1.gz differ diff --git a/bitsnetrc b/bitsnetrc index 250b797..c199e71 100644 --- a/bitsnetrc +++ b/bitsnetrc @@ -1,7 +1,8 @@ # # This file is for the user configuration -# Set some username and passwords according to you priority +# Uncomment lines to activate # +# Set some usernames and passwords according to your priority # eg: if you have 2 IDs with 'ID1' being the most used, # then write this: # @@ -12,12 +13,6 @@ # # Use can use UNLIMITED ID and password combinations -username[1]='f2015022' -password[1]='myPass' - -username[2]='f2015023' -password[2]='myPass2' - # # Configure features @@ -25,4 +20,18 @@ password[2]='myPass2' # change to '1' to always use autologin f=0 +# Always use debug mode: +# change to '1' to always use debug mode +debug=0 + +# Always force login to hostel wifi router: +# change to '1' to always send login request to hostel router +cisco=0 + +# Quiet mode +# change to '1' to never send notification on login +quiet=0 + + +# Change to custom login url if required login_url='https://10.1.0.10:8090/httpclient.html' diff --git a/install b/install index 5d041bf..92fde94 100755 --- a/install +++ b/install @@ -1,48 +1,132 @@ #!/bin/sh +sudo -v + echo "Installing BITSnet Login" +echo +echo "Checking dependencies" +echo + # Check if wget installed -echo "Checking if wget installed" +echo -n "Checking if wget installed... " if ! which wget > /dev/null ; then + echo "no" echo "wget not installed" - exit + exit 1 +else + echo "yes" fi # Check if git installed for update -echo "Checking if git installed" +echo -n "Checking if git installed... " if ! which git > /dev/null ; then + echo "no" echo "git not installed" - exit + exit 1 +else + echo "yes" +fi +# Check if libnotify installed +echo -n "Checking if libnotify installed... " +if ! which notify-send > /dev/null ; then + echo "no" + echo "libnotify not installed" + exit 1 +else + echo "yes" fi # Check if nmcli installed -echo "Checking if nmcli installed" +echo -n "Checking if nmcli installed... " if ! which nmcli > /dev/null ; then + echo "no" echo "nmcli not installed" - exit + exit 1 +else + echo "yes" fi -# Copy config file to the right place -if ! ls -a ~ | grep -x ".bitsnetrc" --quiet ; then - echo "Copying bitsnetrc" - cp ./bitsnetrc ~/.bitsnetrc -fi +echo +echo "All dependencies satisfied" +echo + +echo -n "Copying script... " # Copy the script to executables -echo "Copying script" -mkdir -p ~/.local/lib/bitsnet -mkdir -p ~/.local/bin -cp ./util.sh ~/.local/lib/bitsnet/util.sh -cp ./bitsnet ~/.local/bin/bitsnet +if [ ! -d /usr/lib/bitsnet ]; then + if ! sudo mkdir /usr/lib/bitsnet + then + echo "failed" + exit 1 + fi +fi +if ! sudo cp ./util.sh /usr/lib/bitsnet/util.sh +then + echo "failed" + exit 1 +fi +if ! sudo cp ./bitsnet /usr/bin/bitsnet +then + echo "failed" + exit 1 +fi +echo "done" + + +# Copy config file to the etc +echo -n "Copying config file to etc... " +if [ ! -d /etc/bitsnet ]; then + if ! sudo mkdir /etc/bitsnet + then + echo "failed" + exit 1 + fi +fi +if ! sudo cp ./bitsnetrc /etc/bitsnet/bitsnetrc +then + echo "failed" + exit 1 +fi +echo "done" + # Create a desktop file -echo "Creating app launcher" -cp ./bitsnet.desktop ~/.local/share/applications/bitsnet.desktop +echo -n "Creating app launcher... " +if ! sudo cp ./bitsnet.desktop /usr/share/applications/bitsnet.desktop +then + echo "failed" + exit 1 +else + echo "done" +fi # Set to autostart -echo "Setting up autologin" +echo -n "Setting up autologin... " if [ ! -d ~/.config/autostart ]; then - mkdir -p ~/.config/autostart + if ! mkdir -p ~/.config/autostart + then + echo "failed" + exit 1 + fi +fi +if ! ln -sf /usr/share/applications/bitsnet.desktop ~/.config/autostart/bitsnet.desktop +then + echo "failed" + exit 1 +fi +echo "done" + + +# Create manpage +echo -n "Creating man page... " +if ! sudo cp ./bitsnet.1.gz /usr/share/man/man1/ +then + echo "failed" + exit 1 +else + echo "done" fi -ln -sf ~/.local/share/applications/bitsnet.desktop ~/.config/autostart/bitsnet.desktop -# TODO: Create a man page +echo +echo "BITSnet Login installed" +echo "run man bitsnet for usage" +exit 0 diff --git a/util.sh b/util.sh index 2857ef5..576b949 100644 --- a/util.sh +++ b/util.sh @@ -1,8 +1,15 @@ +# +# util.sh +# Defines a set of utility function for bitsnet +# -# Formatting output on terminal +# Used to format output on terminal GREEN="$(tput setaf 2)" RESET="$(tput sgr0)" +# +# Display help msg on screen +# function show_help { echo "Usage: " echo "bitsnet [OPTIONS]" @@ -26,18 +33,25 @@ function show_help { Quiet mode. Don't send a notification -h display help" - exit } +# +# Display debug messages if enabled +# function debug_msg { if [[ debug -eq 1 ]]; then echo "${GREEN}DEBUG:${RESET} $1" fi } +# +# Extract reply from the recieved response +# function extract_msg { reply="${reply##*\<\![CDATA[}" reply=$(echo "$reply" | cut -d']' -f1) + + # if there was no readable reply if [[ "$reply" == "" ]]; then reply="Cannot connect to BITSnet" @@ -45,42 +59,28 @@ function extract_msg { echo $reply } +# +# Send notification if enabled +# function send_msg { - if [[ $debug == 0 || $quiet != 1 ]]; then - notify-send 'BITSnet' "$1" --icon=network-transmit + if [[ $debug == 0 && $quiet != 1 ]]; then + notify-send 'BITSnet' "$1" --icon=network-transmit --hint=int:transient:1 else debug_msg "reply: $1" fi } -function update { - debug_msg "Updating" - cd /tmp - debug_msg "Cloning repo" - git clone https://github.com/OSDLabs/BitsnetLogin - cd BitsnetLogin - debug_msg "Installing" - ./install - echo "Updated" - cd /tmp - rm -rf BitsnetLogin - debug_msg "Exiting" - exit -} - -function log_out { - reply=$(wget -qO- --no-check-certificate --post-data="mode=193&username=garbage" $login_url) - reply=$(extract_msg $reply) - send_msg "$reply" - exit -} - +# +# Get device info +# function get_device { devInfo="$(nmcli dev | grep " connected" | cut -d " " -f1)" + + # If the connection is through a virtual bridge, select the next device if [[ $devInfo == "virbr"* ]]; then - # If the connection is through a virtual bridge, select the next device devInfo=$(echo "$devInfo" | sed 1,1d) fi; + if [[ $devInfo == "en"* ]]; then # ethernet dev=1 @@ -98,11 +98,46 @@ function get_device { echo "$dev" } +# +# Login to the router +# function router_login() { wget --post-data="username=${username[1]}&password=${password[1]}&loginAccept=1&buttonClicked=4&err_flag=0&info_flag=0&Submit=Submit&err_msg=&info_msg=&redirect_url=" https://20.20.2.11/login.html --no-check-certificate --quiet -O /dev/null } +# +# Login to the LDAP +# returns reply +# function ldap_login() { reply=$(wget -qO- --no-check-certificate --post-data="mode=191&username=$1&password=$2" $login_url) echo $reply } + +# +# Send a logout request +# +function log_out { + reply=$(wget -qO- --no-check-certificate --post-data="mode=193&username=garbage" $login_url -O /dev/null) + reply=$(extract_msg $reply) + send_msg "$reply" + exit 0 +} + +# +# Update the program +# +function update { + debug_msg "Updating" + cd /tmp + debug_msg "Cloning repo" + git clone https://github.com/OSDLabs/BitsnetLogin + cd BitsnetLogin + debug_msg "Installing" + ./install + echo "Updated" + cd /tmp + rm -rf BitsnetLogin + debug_msg "Exiting" + exit 0 +}