| 3202 | rexy | 1 | #!/bin/bash
 | 
        
           |  |  | 2 | #
 | 
        
           |  |  | 3 | # $Id: alcasar-migration-3.3.0_dbRadiusAttrs.sh 2664 2018-11-19 17:35:12Z tom.houdayer $
 | 
        
           |  |  | 4 | #
 | 
        
           |  |  | 5 | # alcasar-migration_db-3.3.0.sh
 | 
        
           |  |  | 6 | # by Tom HOUDAYER
 | 
        
           |  |  | 7 | #
 | 
        
           |  |  | 8 | # This script is distributed under the Gnu General Public License (GPL)
 | 
        
           |  |  | 9 | #
 | 
        
           |  |  | 10 | # Migrate user database to ALCASAR 3.3.0
 | 
        
           |  |  | 11 | # Changes:
 | 
        
           |  |  | 12 | # - Explode "Filter-Id" RADIUS attribute into "Alcasar-Filter", "Alcasar-Protocols-Filter", "Alcasar-Status-Page-Must-Stay-Open" and "Alcasar-Imputability-Warning"
 | 
        
           |  |  | 13 | # - Rename "ChilliSpot-*" RADIUS attribute to "CoovaChilli-*"
 | 
        
           |  |  | 14 | # - Rename "Max-All-Session" RADIUS attribute to "Alcasar-Expire-After"
 | 
        
           |  |  | 15 |   | 
        
           |  |  | 16 | PASSWD_FILE="/root/ALCASAR-passwords.txt"
 | 
        
           |  |  | 17 | DB_PASS=$(cat $PASSWD_FILE | grep ^db_root= | cut -d'=' -f2-)
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | DRY_RUN=false
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | if [ $# -eq 1 ] && [ "$1" == "--simulation" ]; then
 | 
        
           |  |  | 22 | 	DRY_RUN=true
 | 
        
           |  |  | 23 | fi
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | db_query () {
 | 
        
           |  |  | 26 | 	if $DRY_RUN && [[ ! "$1" =~ ^'SELECT ' ]]; then
 | 
        
           |  |  | 27 | 		echo "[SQL] request: \"$1\""
 | 
        
           |  |  | 28 | 	else
 | 
        
           | 3272 | rexy | 29 | 		mariadb -u root -p"$DB_PASS" -D radius -e "$1" -Ns
 | 
        
           | 3202 | rexy | 30 | 		[ $? -ne 0 ] && echo "[SQL] ERROR (\"$1\")"
 | 
        
           |  |  | 31 | 	fi
 | 
        
           |  |  | 32 | }
 | 
        
           |  |  | 33 |   | 
        
           |  |  | 34 | for step in $(seq 1 2); do
 | 
        
           |  |  | 35 | 	if [ $step -eq 1 ]; then
 | 
        
           |  |  | 36 | 		tableNameCheck='radcheck'
 | 
        
           |  |  | 37 | 		tableNameReply='radreply'
 | 
        
           |  |  | 38 | 		loginName='username'
 | 
        
           |  |  | 39 | 	else
 | 
        
           |  |  | 40 | 		tableNameCheck='radgroupcheck'
 | 
        
           |  |  | 41 | 		tableNameReply='radgroupreply'
 | 
        
           |  |  | 42 | 		loginName='groupname'
 | 
        
           |  |  | 43 | 	fi
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 | 	# Explode "Filter-Id" RADIUS attribute into "Alcasar-Filter", "Alcasar-Protocols-Filter", "Alcasar-Status-Page-Must-Stay-Open" and "Alcasar-Imputability-Warning"
 | 
        
           |  |  | 46 | 	db_res=$(db_query "SELECT $loginName, value FROM $tableNameReply WHERE attribute = 'Filter-Id';")
 | 
        
           |  |  | 47 | 	if [ -n "$db_res" ]; then
 | 
        
           |  |  | 48 | 		echo "$(echo "$db_res" | wc -l) \"Filter-Id\" found in table \"$tableNameReply\"."
 | 
        
           |  |  | 49 | 		while read -r line; do
 | 
        
           |  |  | 50 | 			login=$(echo "$line" | cut -f1)
 | 
        
           |  |  | 51 | 			filterId=$(echo "$line" | cut -f2)
 | 
        
           |  |  | 52 | 			echo " $login ($filterId)..."
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 | 			if [ ${filterId:5:1} == '1' ]; then   # Filter: HAVP_WL
 | 
        
           |  |  | 55 | 				filter='4'
 | 
        
           |  |  | 56 | 			elif [ ${filterId:6:1} == '1' ]; then # Filter: HAVP_BL
 | 
        
           |  |  | 57 | 				filter='3'
 | 
        
           |  |  | 58 | 			elif [ ${filterId:7:1} == '1' ]; then # Filter: HAVP
 | 
        
           |  |  | 59 | 				filter='2'
 | 
        
           |  |  | 60 | 			else                                  # Filter: NOT_FILTERED
 | 
        
           |  |  | 61 | 				filter=''
 | 
        
           |  |  | 62 | 			fi
 | 
        
           |  |  | 63 | 			[ ! -z "$filter" ] && db_query "INSERT INTO $tableNameReply ($loginName, attribute, value, op) VALUES ('$login','Alcasar-Filter','$filter', '=');"
 | 
        
           |  |  | 64 |   | 
        
           |  |  | 65 | 			if [ ${filterId:2:1} == '1' ]; then   # FilterProto: PROFILE 3 (Custom)
 | 
        
           |  |  | 66 | 				filterProto='4';
 | 
        
           |  |  | 67 | 			elif [ ${filterId:1:1} == '1' ]; then # FilterProto: PROFILE 2 (WEB + Mail + Remote access)
 | 
        
           |  |  | 68 | 				filterProto='3';
 | 
        
           |  |  | 69 | 			elif [ ${filterId:0:1} == '1' ]; then # FilterProto: PROFILE 1 (WEB)
 | 
        
           |  |  | 70 | 				filterProto='2';
 | 
        
           |  |  | 71 | 			else                                  # FilterProto: PROFILE 0 (Not filtered)
 | 
        
           |  |  | 72 | 				filterProto='';
 | 
        
           |  |  | 73 | 			fi
 | 
        
           |  |  | 74 | 			[ ! -z "$filterProto" ] && db_query "INSERT INTO $tableNameReply ($loginName, attribute, value, op) VALUES ('$login','Alcasar-Protocols-Filter','$filterProto', '=');"
 | 
        
           |  |  | 75 |   | 
        
           |  |  | 76 | 			if [ ${filterId:4:1} == '1' ]; then   # status_open_required
 | 
        
           |  |  | 77 | 				statusOpenRequired='2';
 | 
        
           |  |  | 78 | 			else
 | 
        
           |  |  | 79 | 				statusOpenRequired='';
 | 
        
           |  |  | 80 | 			fi
 | 
        
           |  |  | 81 | 			[ ! -z "$statusOpenRequired" ] && db_query "INSERT INTO $tableNameReply ($loginName, attribute, value, op) VALUES ('$login','Alcasar-Status-Page-Must-Stay-Open','$statusOpenRequired', '=');"
 | 
        
           |  |  | 82 |   | 
        
           |  |  | 83 | 			if [ ${filterId:3:1} == '1' ]; then   # imputability warning
 | 
        
           |  |  | 84 | 				imputabilityWarning='1';
 | 
        
           |  |  | 85 | 			else
 | 
        
           |  |  | 86 | 				imputabilityWarning='';
 | 
        
           |  |  | 87 | 			fi
 | 
        
           |  |  | 88 | 			[ ! -z "$imputabilityWarning" ] && db_query "INSERT INTO $tableNameReply ($loginName, attribute, value, op) VALUES ('$login','Alcasar-Imputability-Warning','$imputabilityWarning', '=');"
 | 
        
           |  |  | 89 |   | 
        
           |  |  | 90 | 			db_query "DELETE FROM $tableNameReply WHERE attribute = 'Filter-Id' AND $loginName = '$login';"
 | 
        
           |  |  | 91 | 		done <<< "$db_res"
 | 
        
           |  |  | 92 | 	fi
 | 
        
           |  |  | 93 |   | 
        
           |  |  | 94 | 	# Rename "ChilliSpot-*" RADIUS attribute to "CoovaChilli-*"
 | 
        
           |  |  | 95 | 	db_query "UPDATE $tableNameReply SET attribute = 'CoovaChilli-Max-Input-Octets'   WHERE attribute = 'ChilliSpot-Max-Input-Octets';"
 | 
        
           |  |  | 96 | 	db_query "UPDATE $tableNameReply SET attribute = 'CoovaChilli-Max-Output-Octets'  WHERE attribute = 'ChilliSpot-Max-Output-Octets';"
 | 
        
           |  |  | 97 | 	db_query "UPDATE $tableNameReply SET attribute = 'CoovaChilli-Max-Total-Octets'   WHERE attribute = 'ChilliSpot-Max-Total-Octets';"
 | 
        
           |  |  | 98 | 	db_query "UPDATE $tableNameReply SET attribute = 'CoovaChilli-Bandwidth-Max-Up'   WHERE attribute = 'ChilliSpot-Bandwidth-Max-Up';"
 | 
        
           |  |  | 99 | 	db_query "UPDATE $tableNameReply SET attribute = 'CoovaChilli-Bandwidth-Max-Down' WHERE attribute = 'ChilliSpot-Bandwidth-Max-Down';"
 | 
        
           |  |  | 100 |   | 
        
           |  |  | 101 | 	# Rename "Max-All-Session" RADIUS attribute to "Alcasar-Expire-After"
 | 
        
           |  |  | 102 | 	db_query "UPDATE $tableNameCheck SET attribute = 'Alcasar-Expire-After' WHERE attribute = 'Max-All-Session';"
 | 
        
           |  |  | 103 | done
 |