-
Notifications
You must be signed in to change notification settings - Fork 1
/
cr-client
137 lines (121 loc) · 3.26 KB
/
cr-client
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
#!/bin/bash
CONF_FILE="`dirname $0`/config"
PID_FILE="$HOME/cr-client.pid"
LOG_FILE="$HOME/cr-client.log"
E_BADARGS=132
E_LOGOUT=133
E_LOGIN=134
E_MISSING_CONF=135
E_MISSING_ADDR=136
if [[ ! -f "$CONF_FILE" ]]; then
echo "$CONF_FILE not found"
exit $E_MISSING_CONF
fi
# Get Cyberoam Server from config
ADDR=`awk '$1=="server"&&$0=$2' "$CONF_FILE"`
[[ "$ADDR" =~ ^\ *$ ]] && echo "No server specified in $CONF_FILE" && exit $E_MISSING_ADDR
LOGIN_ADDR=$ADDR"/httpclient.html"
RELOGIN_ADDR=$ADDR"/live"
usage ()
{
cat << EOF
Usage:
`basename $0` login <profile>
`basename $0` login <username> <password>
`basename $0` logout
`basename $0` -h
`basename $0` --help
EOF
}
bad_args ()
{
echo "Invalid arguments"
usage
exit $E_BADARGS
}
if [[ $1 == "-h" || $1 == "--help" ]]
then
usage
exit
fi
if [[ $# < 1 || $# > 3 ]]
then
bad_args
fi
login ()
{
# Get the response XML
RET_VAL=`curl -m 5 -k -s --data "username=$1&password=$2&mode=191" "$LOGIN_ADDR"`
# Extract content of message tag
RET_VAL=`echo $RET_VAL | sed -n -e 's/.*<message>\(.*\)<\/message>.*/\1/p'`
if [[ "$RET_VAL" == "" ]]; then echo "No comprehendable response from the server"; fi
echo $RET_VAL
[[ ! $RET_VAL =~ "successfully logged in" ]] && echo "Login Error: $RET_VAL" && return 1 || return 0
}
logout ()
{
if [[ -f "$PID_FILE" ]]; then
# Kill relogin process of the previous login
kill -9 `awk '$0=$1' "$PID_FILE"`
# Get username of previous login
USER=`awk '$0=$2' "$PID_FILE"`
RET_VAL=`curl -m 5 -k -s --data "username=$USER&mode=193" "$LOGIN_ADDR"`
RET_VAL=`echo $RET_VAL | sed -n -e 's/.*<message>\(.*\)<\/message>.*/\1/p'`
rm "$PID_FILE"
[[ ! "$RET_VAL" =~ "You have successfully logged off" ]] && echo "Coudn't comprehend the response: $RET_VAL" && return 1 || return 0
fi
echo "$PID_FILE not found, failed to perform logout operation"
return 1
}
perform_login ()
{
logout > /dev/null
USER=$1
PASS=$2
login $USER $PASS
if [[ $? != 0 ]]; then exit $E_LOGIN; fi
(
read USER RELOGIN_ADDR
while true
do
# As per observing cyberoam activity, it sends a relogin (kind of) every 3 minutes, to be safe, taking val 90
sleep 90
TRIES=30
RET=`curl -m 5 -k -s "$RELOGIN_ADDR?mode=192&username=$USER"`
RET=`echo $RET | sed -n -e 's/.*<ack>\(.*\)<\/ack>.*/\1/p'`
while [[ ! "$RET" =~ "ack" && $TRIES > 0 ]]; do
sleep 2
echo "Error! $TRIES tries remaining"
RET=`curl -m 5 -k -s "$RELOGIN_ADDR?mode=192&username=$USER"`
RET=`echo $RET | sed -n -e 's/.*<ack>\(.*\)<\/ack>.*/\1/p'`
let 'TRIES--'
done
if [[ ! "$RET" =~ "ack" ]]; then echo "`date`: "$RET; break; fi
done
) < <(echo $USER $RELOGIN_ADDR) >> "$LOG_FILE" & # Fork it in background
pid=$!
# Write the pidfile
echo $pid $USER > "$PID_FILE"
echo "User $USER Logged in..."
}
if [[ $# == 1 ]]
then
if [[ "$1" != "logout" ]]; then bad_args; fi
logout
if [[ $? == 0 ]]; then echo "Logged off..."; exit 0; fi
exit $E_LOGOUT
fi
if [[ $# == 2 ]]
then
[[ "$1" != "login" ]] && bad_args
read USER PASS <<< `awk 'NF==3 && $1=="'"$2"'"{print $2, $3;exit}' "$CONF_FILE"`
if [[ $USER == "" || $PASS == "" ]]; then echo "Profile $2 not found in $CONF_FILE"; exit $E_LOGIN; fi
if [[ $? == 0 ]]; then
perform_login $USER $PASS
fi
fi
if [[ $# == 3 ]]
then
[[ "$1" != "login" ]] && bad_args
perform_login $2 $3
fi