Subversion Repositories ALCASAR

Rev

Rev 2594 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2594 tom.houday 1
#!/bin/sh
2
#
3
# $Id: alcasar-macup.sh 2692 2019-01-25 23:18:50Z tom.houdayer $
4
#
5
# alcasar-macup.sh
6
#
7
# This script is distributed under the Gnu General Public License (GPL)
8
 
9
PASSWD_FILE="/root/ALCASAR-passwords.txt"
10
 
11
if [ -z "$CALLING_STATION_ID" ]; then
12
	exit 1
13
fi
14
 
15
dbRootPass=$(grep ^db_root= $PASSWD_FILE | cut -d'=' -f2-)
16
 
17
chilli_query_res=$(chilli_query list mac $CALLING_STATION_ID)
18
[ -z "$chilli_query_res" ] && exit
19
 
20
is_connected=$(echo "$chilli_query_res" | awk '{ print $5 }')
21
current_mac=$CALLING_STATION_ID
22
 
23
if [ $is_connected == '0' ]; then
24
	db_query="SELECT username, IFNULL((UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(acctstoptime)), 0) AS timeout, acctterminatecause FROM radacct WHERE callingstationid='$current_mac' ORDER BY acctstarttime DESC LIMIT 1;"
25
	db_res=$(mysql -u root -p"$dbRootPass" -D radius -e "$db_query" -Bs)
26
 
27
	if [ -n "$db_res" ]; then
28
		username=$(echo "$db_res"           | cut -f1)
29
		timeout=$(echo "$db_res"            | cut -f2)
30
		acctterminatecause=$(echo "$db_res" | cut -f3)
31
 
32
		if [ "$acctterminatecause" != "User-Request" ]; then
2692 tom.houday 33
			db_query="SELECT attribute, value FROM ((SELECT attribute, value FROM radreply WHERE (attribute='Alcasar-Reconnect-Timeout') AND username='$username') UNION (SELECT attribute, value FROM radgroupreply gr LEFT JOIN radusergroup ug ON gr.groupname = ug.groupname WHERE (attribute='Alcasar-Reconnect-Timeout') AND username = '$username' ORDER BY ug.priority) UNION (SELECT attribute, value FROM radgroupreply WHERE groupname = 'ldap' AND (attribute='Alcasar-Reconnect-Timeout'))) attrs GROUP BY attribute;"
2594 tom.houday 34
			db_res=$(mysql -u root -p"$dbRootPass" -D radius -e "$db_query" -Bs)
35
 
36
			reconnectTimeout=$(echo "$db_res" | awk '$1 == "Alcasar-Reconnect-Timeout" { print $2 }')
37
 
38
			if [ -n "$reconnectTimeout" ] && [ $timeout -le $reconnectTimeout ]; then
39
				for i in {1..10}; do
40
					sleep 1
41
					chilli_query authorize mac $current_mac username "$username"
42
					chilli_query_res=$(chilli_query list mac $current_mac)
43
					isPassing=$(echo "$chilli_query_res"    | awk '{ print $3 }')
44
					is_connected=$(echo "$chilli_query_res" | awk '{ print $5 }')
45
					if [ "$is_connected" == '1' ] && [ "$isPassing" == 'pass' ]; then
46
						logger -t alcasar-macup "The MAC address \"$current_mac\" is reconnected with user \"$username\"."
47
						break
48
					fi
49
				done
50
			fi
51
		fi
52
	fi
53
fi