daily-psk.local 2.96 KB
Newer Older
1
#!rsc
2
# RouterOS script: daily-psk.local
3
# Copyright (c) 2013-2019 Christian Hesse <mail@eworm.de>
4
#                         Michael Gisbers <michael@gisbers.de>
Christian Hesse's avatar
Christian Hesse committed
5 6
#
# update daily PSK (pre shared key)
7 8
#
# !! Do not edit this file, it is generated from template!
Christian Hesse's avatar
Christian Hesse committed
9

10 11
:global Identity;
:global DailyPskMatchComment;
Christian Hesse's avatar
Christian Hesse committed
12

13
:global SendNotification;
14 15
:global UrlEncode;
:global WaitForFile;
16

17
:local Seen [ :toarray "" ];
18

19 20
# return pseudo-random string for PSK
:local GeneratePSK do={
21
  :local Date [ :tostr $1 ];
22

23
  :global DailyPskSecrets;
24

25 26
  :local Months { "jan"; "feb"; "mar"; "apr"; "may"; "jun";
                  "jul"; "aug"; "sep"; "oct"; "nov"; "dec" };
27

28 29 30
  :local Month [ :pick $Date 0 3 ];
  :local Day [ :tonum [ :pick $Date 4 6 ] ];
  :local Year [ :pick $Date 7 11 ];
31

32
  :for MIndex from=0 to=[ :len $Months ] do={
33 34
    :if ($Months->$MIndex = $Month) do={
      :set Month ($MIndex + 1);
35 36 37
    }
  }

38 39 40 41 42
  :local A ((14 - $Month) / 12);
  :local B ($Year - $A);
  :local C ($Month + 12 * $A - 2);
  :local WeekDay (7000 + $Day + $B + ($B / 4) - ($B / 100) + ($B / 400) + ((31 * $C) / 12));
  :set WeekDay ($WeekDay - (($WeekDay / 7) * 7));
43

44 45 46
  :return (($DailyPskSecrets->0->($Day - 1)) . \
    ($DailyPskSecrets->1->($Month - 1)) . \
    ($DailyPskSecrets->2->$WeekDay));
47
}
48

49 50 51 52 53 54 55 56 57 58
:local Date [ / system clock get date ];
:local NewPsk [ $GeneratePSK $Date ];

:foreach AccList in=[ / interface wireless access-list find where comment~$DailyPskMatchComment ] do={
  :local IntName [ / interface wireless access-list get $AccList interface ];
  :local Ssid [ / interface wireless get $IntName ssid ];
  :local OldPsk [ / interface wireless access-list get $AccList private-pre-shared-key ];
  :local Skip 0;

  :if ($NewPsk != $OldPsk) do={
59
    :log info ("Updating daily PSK for " . $Ssid . " to " . $NewPsk . " (was " . $OldPsk . ")");
60 61
    / interface wireless access-list set $AccList private-pre-shared-key=$NewPsk;

62
    :if ([ / interface wireless print count-only where name=$IntName disabled=no ] = 1) do={
63 64 65 66
      :foreach SeenSsid in=$Seen do={
        :if ($SeenSsid = $Ssid) do={
          :log debug ("Already sent a mail for SSID " . $Ssid . ", skipping.");
          :set Skip 1;
67 68 69
        }
      }

70 71
      :if ($Skip = 0) do={
        :set Seen ($Seen, $Ssid);
72

73
        :local Url ("https://www.eworm.de/cgi-bin/cqrlogo-wifi.cgi" . \
74
            "?scale=8&level=1&ssid=" . [ $UrlEncode $Ssid ] . "&pass=" . [ $UrlEncode $NewPsk ]);
75
        :local Attach "qrcode-daily.png";
76

77
        :do {
78
          / tool fetch check-certificate=yes-without-crl \
79
              $Url dst-path=$Attach;
80
          $WaitForFile $Attach;
81
        } on-error={
82
          :set Attach "";
83
        }
84

85 86 87 88 89
        $SendNotification ("daily PSK " . $Ssid) \
          ("This is the daily PSK on " . $Identity . ":\n\n" . \
            "SSID: " . $Ssid . "\n" . \
            "PSK:  " . $NewPsk . "\n" . \
            "Date: " . $Date . "\n\n" . \
90
            "A client device specific rule must not exist!\n\n" . \
91
            $Url) $Attach;
92 93
      }
    }
Christian Hesse's avatar
Christian Hesse committed
94 95
  }
}