-
Notifications
You must be signed in to change notification settings - Fork 2
/
bitsnet
executable file
·218 lines (182 loc) · 3.92 KB
/
bitsnet
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/bash
#
# bitsnet
# Script for logging into the bitsnet
#
#
# Load util functions
#
# shellcheck source=/dev/null
if ! source /usr/local/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 || \
echo "Please reinstall program" || exit 1
exit 1
fi
#
# Add a configuration file
#
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 /usr/local/etc/bitsnet/bitsnetrc ~/.bitsnetrc
then
echo "failed"
exit 1
else
echo "done"
fi
else
source "$HOME/.bitsnetrc"
fi
#
# if library ip is not loaded, load it
#
if [[ -z $library_ip ]]; then
library_ip="10.20.0.1"
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[1]=${OPTARG}
;;
p)
password[1]=${OPTARG}
;;
d)
debug=1
debug_msg "Going into debug mode"
;;
U)
update
;;
o)
log_out
;;
f)
force=1
;;
w)
hostel=1
;;
q)
quiet=1
;;
h | * | [?])
show_help
exit 1
;;
esac
done
#
# check if login is forced
#
if [[ $force -eq 1 ]]; then
debug_msg "Forcing login attempt"
fi
#
# display login url for debugging
#
debug_msg "LoginURL: $login_url"
#
# Get device info
#
debug_msg "Checking device info"
dev=$(get_device)
debug_msg "dev = $dev"
#
# Exit if dev is unknown and login attempt is not forced
#
if [[ "$dev" == "" && $force -ne 1 ]]; then
debug_msg "Exiting because dev is empty."
debug_msg "Make sure you're connected to a network interface"
debug_msg "Use -f option if you think this is a mistake"
send_msg "Network interface unknown."
exit 1
fi
#
# Check and exit if already logged in.
# Skip check if forced
#
if [[ $force -ne 1 ]]; then
debug_msg "Checking if already connected"
if wget --quiet --timeout=5 --tries=1 --spider --no-check-certificate \
--output-file=/dev/null https://google.com
then
send_msg "Already logged into the BITSnet"
exit 0
else
debug_msg "Not logged in"
fi
fi
#
# Check if it's the hostel wifi
# ip is hostel's and we're on wireless
#
#debug_msg "ip = $(get_ip)"
#debug_msg "library_ip = $library_ip"
#if [[ "$hostel" -eq 0 && $dev =~ ^wl && "$(get_ip)" != "$library_ip" ]]; then
#hostel=1;
#fi
#
# Login to the hostel wifi router
#
#debug_msg "Logging in"
#if [[ $hostel -eq 1 ]]; then
#debug_msg "Connected to hostel wifi"
#debug_msg "Sending request to hostel router"
#router_login
#else
#debug_msg "Not connected to the hostel wifi"
#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]} ]]
do
debug_msg "Trying with ID: ${username[$i]}"
reply=$(ldap_login "${username[$i]}" "${password[$i]}")
# Extract reply
reply=$(extract_msg "$reply")
# if logged in, get out
if [[ "$reply" == "You have successfully logged in" ]]; then
break
else
debug_msg "$reply"
fi
((i++))
done
#
# Display reply or send a notification
#
debug_msg "Reply sent"
send_msg "$reply"
#
# Exit
#
debug_msg "Bye"
exit 0