2023-08-09 00:55:30 +02:00
|
|
|
#!rsc by RouterOS
|
|
|
|
# RouterOS script: hotspot-to-wpa.capsman
|
2025-01-02 00:04:06 +01:00
|
|
|
# Copyright (c) 2019-2025 Christian Hesse <mail@eworm.de>
|
2025-01-24 20:46:11 +01:00
|
|
|
# https://rsc.eworm.de/COPYING.md
|
2023-08-09 00:55:30 +02:00
|
|
|
#
|
2025-02-07 17:39:48 +01:00
|
|
|
# requires RouterOS, version=7.15
|
2025-01-29 10:35:31 +01:00
|
|
|
# requires device-mode, hotspot
|
2023-11-15 21:31:18 +01:00
|
|
|
#
|
2023-08-09 00:55:30 +02:00
|
|
|
# add private WPA passphrase after hotspot login
|
2025-01-24 20:46:11 +01:00
|
|
|
# https://rsc.eworm.de/doc/hotspot-to-wpa.md
|
2023-08-09 00:55:30 +02:00
|
|
|
#
|
|
|
|
# !! Do not edit this file, it is generated from template!
|
|
|
|
|
2024-12-06 10:31:52 +01:00
|
|
|
:local ExitOK false;
|
2025-05-06 09:44:23 +02:00
|
|
|
:onerror Err {
|
2025-05-06 14:08:37 +02:00
|
|
|
:global GlobalConfigReady; :global GlobalFunctionsReady;
|
|
|
|
:retry { :if ($GlobalConfigReady != true || $GlobalFunctionsReady != true) \
|
|
|
|
do={ :error ("Global config and/or functions not ready."); }; } delay=500ms max=50;
|
2024-03-06 15:28:55 +01:00
|
|
|
:local ScriptName [ :jobname ];
|
2023-08-09 00:55:30 +02:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:global EitherOr;
|
2024-03-08 12:45:38 +01:00
|
|
|
:global LogPrint;
|
2024-03-04 13:48:01 +01:00
|
|
|
:global ParseKeyValueStore;
|
|
|
|
:global ScriptLock;
|
2023-08-09 00:55:30 +02:00
|
|
|
|
2024-03-06 15:28:55 +01:00
|
|
|
:local MacAddress $"mac-address";
|
|
|
|
:local UserName $username;
|
|
|
|
|
2024-03-05 16:12:36 +01:00
|
|
|
:if ([ $ScriptLock $ScriptName ] = false) do={
|
2024-12-06 10:31:52 +01:00
|
|
|
:set ExitOK true;
|
2024-03-06 15:28:55 +01:00
|
|
|
:error false;
|
2024-03-05 16:12:36 +01:00
|
|
|
}
|
2023-08-09 00:55:30 +02:00
|
|
|
|
2024-03-06 15:28:55 +01:00
|
|
|
:if ([ :typeof $MacAddress ] = "nothing" || [ :typeof $UserName ] = "nothing") do={
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint error $ScriptName ("This script is supposed to run from hotspot on login.");
|
2024-12-06 10:31:52 +01:00
|
|
|
:set ExitOK true;
|
2024-03-08 12:45:38 +01:00
|
|
|
:error false;
|
2024-03-04 13:48:01 +01:00
|
|
|
}
|
2023-08-09 00:55:30 +02:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:local Date [ /system/clock/get date ];
|
|
|
|
:local UserVal ({});
|
|
|
|
:if ([ :len [ /ip/hotspot/user/find where name=$UserName ] ] > 0) do={
|
|
|
|
:set UserVal [ /ip/hotspot/user/get [ find where name=$UserName ] ];
|
|
|
|
}
|
|
|
|
:local UserInfo [ $ParseKeyValueStore ($UserVal->"comment") ];
|
|
|
|
:local Hotspot [ /ip/hotspot/host/get [ find where mac-address=$MacAddress authorized ] server ];
|
2023-08-09 00:55:30 +02:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:if ([ :len [ /caps-man/access-list/find where comment="--- hotspot-to-wpa above ---" disabled ] ] = 0) do={
|
|
|
|
/caps-man/access-list/add comment="--- hotspot-to-wpa above ---" disabled=yes;
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint warning $ScriptName ("Added disabled access-list entry with comment '--- hotspot-to-wpa above ---'.");
|
2024-03-04 13:48:01 +01:00
|
|
|
}
|
|
|
|
:local PlaceBefore ([ /caps-man/access-list/find where comment="--- hotspot-to-wpa above ---" disabled ]->0);
|
2023-08-09 00:55:30 +02:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:if ([ :len [ /caps-man/access-list/find where \
|
|
|
|
comment=("hotspot-to-wpa template " . $Hotspot) disabled ] ] = 0) do={
|
|
|
|
/caps-man/access-list/add comment=("hotspot-to-wpa template " . $Hotspot) disabled=yes place-before=$PlaceBefore;
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint warning $ScriptName ("Added template in access-list for hotspot '" . $Hotspot . "'.");
|
2024-03-04 13:48:01 +01:00
|
|
|
}
|
|
|
|
:local Template [ /caps-man/access-list/get ([ find where \
|
|
|
|
comment=("hotspot-to-wpa template " . $Hotspot) disabled ]->0) ];
|
2023-08-09 00:55:30 +02:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:if ($Template->"action" = "reject") do={
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint info $ScriptName ("Ignoring login for hotspot '" . $Hotspot . "'.");
|
2024-12-06 10:31:52 +01:00
|
|
|
:set ExitOK true;
|
2024-03-06 15:28:55 +01:00
|
|
|
:error true;
|
2024-03-04 13:48:01 +01:00
|
|
|
}
|
2023-08-09 00:55:30 +02:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
# allow login page to load
|
|
|
|
:delay 1s;
|
2023-08-09 00:55:30 +02:00
|
|
|
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint info $ScriptName ("Adding/updating access-list entry for mac address " . $MacAddress . \
|
|
|
|
" (user " . $UserName . ").");
|
2024-03-04 13:48:01 +01:00
|
|
|
/caps-man/access-list/remove [ find where mac-address=$MacAddress comment~"^hotspot-to-wpa: " ];
|
|
|
|
/caps-man/access-list/add private-passphrase=($UserVal->"password") ssid-regexp="-wpa\$" \
|
|
|
|
mac-address=$MacAddress comment=("hotspot-to-wpa: " . $UserName . ", " . $MacAddress . ", " . $Date) \
|
|
|
|
action=reject place-before=$PlaceBefore;
|
2023-08-09 00:55:30 +02:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:local Entry [ /caps-man/access-list/find where mac-address=$MacAddress \
|
|
|
|
comment=("hotspot-to-wpa: " . $UserName . ", " . $MacAddress . ", " . $Date) ];
|
|
|
|
:local PrivatePassphrase [ $EitherOr ($UserInfo->"private-passphrase") ($Template->"private-passphrase") ];
|
|
|
|
:if ([ :len $PrivatePassphrase ] > 0) do={
|
|
|
|
:if ($PrivatePassphrase = "ignore") do={
|
|
|
|
/caps-man/access-list/set $Entry !private-passphrase;
|
|
|
|
} else={
|
|
|
|
/caps-man/access-list/set $Entry private-passphrase=$PrivatePassphrase;
|
|
|
|
}
|
2023-08-09 00:55:30 +02:00
|
|
|
}
|
2024-03-04 13:48:01 +01:00
|
|
|
:local SsidRegexp [ $EitherOr ($UserInfo->"ssid-regexp") ($Template->"ssid-regexp") ];
|
|
|
|
:if ([ :len $SsidRegexp ] > 0) do={
|
|
|
|
/caps-man/access-list/set $Entry ssid-regexp=$SsidRegexp;
|
|
|
|
}
|
|
|
|
:local VlanId [ $EitherOr ($UserInfo->"vlan-id") ($Template->"vlan-id") ];
|
|
|
|
:if ([ :len $VlanId ] > 0) do={
|
|
|
|
/caps-man/access-list/set $Entry vlan-id=$VlanId;
|
|
|
|
}
|
|
|
|
:local VlanMode [ $EitherOr ($UserInfo->"vlan-mode") ($Template->"vlan-mode") ];
|
|
|
|
:if ([ :len $VlanMode] > 0) do={
|
|
|
|
/caps-man/access-list/set $Entry vlan-mode=$VlanMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
:delay 2s;
|
|
|
|
/caps-man/access-list/set $Entry action=accept;
|
2025-05-06 09:44:23 +02:00
|
|
|
} do={
|
|
|
|
:global ExitError; $ExitError $ExitOK [ :jobname ] $Err;
|
2024-12-06 10:31:52 +01:00
|
|
|
}
|