Subversion Repositories ALCASAR

Rev

Rev 2692 | 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 2712 2019-03-10 23:28:36Z 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
2712 tom.houday 33
			db_query_additionalGroups=''
34
			[ -n "$FILTER_ID" ] && db_query_additionalGroups="( SELECT attribute, value FROM radgroupreply WHERE groupname = '$FILTER_ID' AND (attribute='Alcasar-Reconnect-Timeout') ) UNION "
35
			db_query="SELECT attribute, value FROM ( \
36
				( SELECT attribute, value FROM radreply      WHERE username  = '$USER_NAME' AND (attribute='Alcasar-Reconnect-Timeout') ) UNION \
37
				( SELECT attribute, value FROM radgroupreply gr LEFT JOIN radusergroup ug ON gr.groupname = ug.groupname WHERE username = '$USER_NAME' AND (attribute='Alcasar-Reconnect-Timeout') ORDER BY ug.priority ) UNION \
38
				$db_query_additionalGroups \
39
				( SELECT attribute, value FROM radgroupreply WHERE groupname = 'default'    AND (attribute='Alcasar-Reconnect-Timeout') ) \
40
			) attrs GROUP BY attribute;"
2594 tom.houday 41
			db_res=$(mysql -u root -p"$dbRootPass" -D radius -e "$db_query" -Bs)
42
 
43
			reconnectTimeout=$(echo "$db_res" | awk '$1 == "Alcasar-Reconnect-Timeout" { print $2 }')
44
 
45
			if [ -n "$reconnectTimeout" ] && [ $timeout -le $reconnectTimeout ]; then
46
				for i in {1..10}; do
47
					sleep 1
48
					chilli_query authorize mac $current_mac username "$username"
49
					chilli_query_res=$(chilli_query list mac $current_mac)
50
					isPassing=$(echo "$chilli_query_res"    | awk '{ print $3 }')
51
					is_connected=$(echo "$chilli_query_res" | awk '{ print $5 }')
52
					if [ "$is_connected" == '1' ] && [ "$isPassing" == 'pass' ]; then
53
						logger -t alcasar-macup "The MAC address \"$current_mac\" is reconnected with user \"$username\"."
54
						break
55
					fi
56
				done
57
			fi
58
		fi
59
	fi
60
fi