872 |
richard |
1 |
#!/bin/sh
|
2234 |
richard |
2 |
#
|
|
|
3 |
# $Id: alcasar-conup.sh 2956 2021-05-24 19:57:17Z rexy $
|
|
|
4 |
#
|
872 |
richard |
5 |
# alcasar-conup.sh
|
2956 |
rexy |
6 |
# by Rexy & Pierre RIVAULT
|
872 |
richard |
7 |
# This script is distributed under the Gnu General Public License (GPL)
|
|
|
8 |
|
2886 |
rexy |
9 |
# This script is started by coova after each successfull login
|
|
|
10 |
# Ce script est démarré par coova à chaque connexion d'usager (authentification réussi)
|
872 |
richard |
11 |
|
2956 |
rexy |
12 |
CONF_FILE="/usr/local/etc/alcasar.conf"
|
2501 |
tom.houday |
13 |
PASSWD_FILE="/root/ALCASAR-passwords.txt"
|
2886 |
rexy |
14 |
DB_USER=`cat $PASSWD_FILE|grep ^db_user=|cut -d'=' -f2`
|
|
|
15 |
DB_PASSWORD=`cat $PASSWD_FILE|grep ^db_password=|cut -d'=' -f2`
|
2501 |
tom.houday |
16 |
|
2234 |
richard |
17 |
if [ -z $FRAMED_IP_ADDRESS ]; then
|
|
|
18 |
exit 1
|
2184 |
richard |
19 |
fi
|
2069 |
richard |
20 |
|
2886 |
rexy |
21 |
# Retrieve 3 alcasar special radius attributes (search order : default group, then user's group, then user)
|
2712 |
tom.houday |
22 |
db_query="SELECT attribute, value FROM ( \
|
2886 |
rexy |
23 |
( SELECT attribute, value FROM radreply WHERE username = '$USER_NAME' AND (attribute IN ('Alcasar-Filter', 'Alcasar-Protocols-Filter', 'Alcasar-Status-Page-Must-Stay-Open')) ) UNION \
|
2712 |
tom.houday |
24 |
( SELECT attribute, value FROM radgroupreply gr LEFT JOIN radusergroup ug ON gr.groupname = ug.groupname WHERE username = '$USER_NAME' AND (attribute IN ('Alcasar-Filter', 'Alcasar-Protocols-Filter', 'Alcasar-Status-Page-Must-Stay-Open')) ORDER BY ug.priority ) UNION \
|
2883 |
rexy |
25 |
( SELECT attribute, value FROM radgroupreply WHERE groupname = 'default' AND (attribute IN ('Alcasar-Filter', 'Alcasar-Protocols-Filter', 'Alcasar-Status-Page-Must-Stay-Open')) ) \
|
2712 |
tom.houday |
26 |
) attrs GROUP BY attribute;"
|
2886 |
rexy |
27 |
db_res=$(mysql -u$DB_USER -p$DB_PASSWORD -D radius -e "$db_query" -Ns)
|
2006 |
raphael.pi |
28 |
|
2692 |
tom.houday |
29 |
filter=$(echo "$db_res" | awk '$1 == "Alcasar-Filter" { print $2 }')
|
|
|
30 |
filterProto=$(echo "$db_res" | awk '$1 == "Alcasar-Protocols-Filter" { print $2 }')
|
2883 |
rexy |
31 |
statusPageRequired=$(echo "$db_res" | awk '$1 == "Alcasar-Status-Page-Must-Stay-Open" { print $2 }')
|
2501 |
tom.houday |
32 |
|
2883 |
rexy |
33 |
|
|
|
34 |
# Add user to his IPSET
|
2841 |
rexy |
35 |
if [ "$filter" == '4' ]; then # AV_WL
|
|
|
36 |
set_filter="av_wl"
|
|
|
37 |
elif [ "$filter" == '3' ]; then # AV_BL
|
|
|
38 |
set_filter="av_bl"
|
|
|
39 |
elif [ "$filter" == '2' ]; then # AV
|
|
|
40 |
set_filter="av"
|
2501 |
tom.houday |
41 |
else # NOT_FILTERED
|
2234 |
richard |
42 |
set_filter="not_filtered"
|
2006 |
raphael.pi |
43 |
fi
|
|
|
44 |
|
2501 |
tom.houday |
45 |
if [ "$filterProto" == '4' ]; then # PROFILE 3 (Custom)
|
|
|
46 |
set_filterProto="proto_3";
|
|
|
47 |
elif [ "$filterProto" == '3' ]; then # PROFILE 2 (WEB + Mail + Remote access)
|
|
|
48 |
set_filterProto="proto_2";
|
|
|
49 |
elif [ "$filterProto" == '2' ]; then # PROFILE 1 (WEB)
|
|
|
50 |
set_filterProto="proto_1";
|
2234 |
richard |
51 |
else # PROFILE 0 (Not filtered)
|
2501 |
tom.houday |
52 |
set_filterProto="proto_0";
|
2006 |
raphael.pi |
53 |
fi
|
|
|
54 |
|
2886 |
rexy |
55 |
# Add user to his IPSET
|
2501 |
tom.houday |
56 |
ipset add $set_filter $FRAMED_IP_ADDRESS
|
|
|
57 |
ipset add $set_filterProto $FRAMED_IP_ADDRESS
|
2184 |
richard |
58 |
|
2886 |
rexy |
59 |
# If status page isn't required :
|
|
|
60 |
# -add user_IP with flag PERM in /tmp/current_users.txt (watchdog remove these @IP at midnight)
|
|
|
61 |
# if the user has the "Expiration" attribute, add its @MAC as an authenticated user (with the same user's attributes)
|
2883 |
rexy |
62 |
if [ "$statusPageRequired" == '2' ]; then # Status page is not required
|
2884 |
rexy |
63 |
current_users_file="/tmp/current_users.txt"
|
|
|
64 |
if [ ! -e $current_users_file ]; then
|
|
|
65 |
touch $current_users_file && chown root:apache $current_users_file && chmod 660 $current_users_file
|
|
|
66 |
fi
|
|
|
67 |
echo "$FRAMED_IP_ADDRESS:PERM" >> $current_users_file
|
2394 |
tom.houday |
68 |
fi
|
|
|
69 |
|
2956 |
rexy |
70 |
# set the user_ip to an gw_ipset for load-balancing
|
|
|
71 |
gw_min="gw0"
|
|
|
72 |
weight=`grep ^PUBLIC_WEIGHT= $CONF_FILE | cut -d"=" -f2`
|
|
|
73 |
already=`ipset list $gw_min | grep Number\ of\ entries: | cut -d":" -f2`
|
|
|
74 |
#The *1000 is here to avoid working on floats in bash
|
|
|
75 |
gw_min_value=$((1000 * $already / $weight))
|
|
|
76 |
|
|
|
77 |
nb_gw=`grep ^WAN $CONF_FILE | wc -l`
|
|
|
78 |
for (( i = 1 ; i <= $nb_gw ; i++ ));do
|
|
|
79 |
gw="gw${i}"
|
|
|
80 |
weight=`grep ^WAN$i= $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F ',' '{ print $2 }'`
|
|
|
81 |
already=`ipset list $gw | grep Number\ of\ entries: | cut -d":" -f2`
|
|
|
82 |
value=$((1000 * $already / $weight))
|
|
|
83 |
if [ $value -lt $gw_min_value ]
|
|
|
84 |
then
|
|
|
85 |
gw_min_value=$value
|
|
|
86 |
gw_min=$gw
|
|
|
87 |
fi
|
|
|
88 |
done
|
|
|
89 |
ipset add $gw_min $FRAMED_IP_ADDRESS
|
|
|
90 |
|
2883 |
rexy |
91 |
#############################
|
2886 |
rexy |
92 |
## Debug : show all the coova parse variables (+ ALCASAR-Filter + ALCASAR-Protocols-Filter + Alcasar-Status-Page-Must-Stay-Open).
|
2883 |
rexy |
93 |
## see "/src/chilli.c" for the complete list of parse variables
|
|
|
94 |
#debug_file="/tmp/debug-conup.txt"
|
|
|
95 |
#echo "-----------------------------------------------" >> $debug_file
|
|
|
96 |
#echo `date` >> $debug_file
|
|
|
97 |
#for i in DEV NET MASK ADDR USER_NAME NAS_IP_ADDRESS SERVICE_TYPE FRAMED_IP_ADDRESS FILTER_ID STATE CLASS CUI SESSION_TIMEOUT IDLE_TIMEOUT CALLING_STATION_ID CALLED_STATION_ID NAS_ID NAS_PORT_TYPE ACCT_SESSION_ID ACCT_INTERIM_INTERVAL WISPR_LOCATION_ID WISPR_LOCATION_NAME WISPR_BANDWIDTH_MAX_UP WISPR_BANDWIDTH_MAX_DOWN COOVACHILLI_MAX_INPUT_OCTETS COOVACHILLI_MAX_OUTPUT_OCTETS COOVACHILLI_MAX_TOTAL_OCTETS INPUT_OCTETS OUTPUT_OCTETS INPUT_PACKETS OUTPUT_PACKETS SESSION_TIME IDLE_TIME LOCATION OLD_LOCATION TERMINATE_CAUSE
|
2184 |
richard |
98 |
#do
|
2883 |
rexy |
99 |
# echo -n "$i=" >> $debug_file
|
|
|
100 |
# if [[ -v $i ]];
|
|
|
101 |
# then
|
|
|
102 |
# echo -n "${!i}; " >> $debug_file
|
|
|
103 |
# else
|
|
|
104 |
# echo -n "not defined; " >> $debug_file
|
|
|
105 |
# fi
|
2184 |
richard |
106 |
#done
|
2883 |
rexy |
107 |
#echo >> $debug_file
|
2886 |
rexy |
108 |
#echo "ALCASAR-Filter : $set_filter" >> $debug_file
|
|
|
109 |
#echo "ALCASAR-Protocols-Filter : $set_filterProto" >> $debug_file
|
|
|
110 |
#echo "Alcasar-Status-Page-Must-Stay-Open : $statusPageRequired" >> $debug_file
|
2883 |
rexy |
111 |
## END DEBUG
|
|
|
112 |
#################################
|