callhome.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. //-
  3. // Copyright (c) 2011-2019 Joe Clarke <jclarke@cisco.com>
  4. // All rights reserved.
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions
  7. // are met:
  8. // 1. Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // 2. Redistributions in binary form must reproduce the above copyright
  11. // notice, this list of conditions and the following disclaimer in the
  12. // documentation and/or other materials provided with the distribution.
  13. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  14. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  17. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19. // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. // SUCH DAMAGE.
  24. include_once '../db.inc.php';
  25. include_once 'swreg.inc.php';
  26. include_once '../swreg_creds.inc.php';
  27. require_once '../functions.php';
  28. require_once 'Log.php';
  29. $dsn = "$db_driver:host=$db_host;dbname=$db_name";
  30. $options = [
  31. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  32. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  33. PDO::ATTR_EMULATE_PREPARES => false,
  34. ];
  35. try {
  36. $dbh = new PDO($dsn, $db_user, $db_pass, $options);
  37. } catch (PDOException $e) {
  38. die($e->getMessage());
  39. }
  40. $logger = Log::singleton('file', LOGFILE, TOOL_NAME . ' : Switch Callhome Endpoint');
  41. if ($logger === false) {
  42. die("Failed to open logfile.\n");
  43. }
  44. $mask = Log::MAX(LOG_LEVEL);
  45. $logger->setMask($mask);
  46. list($pid, $sn) = explode('/', trim($_SERVER['PATH_INFO'], '/'));
  47. // Right now, we will just check if the switch should be re-ZTP'd
  48. $sql = 'SELECT should_re_ztp FROM DEVICE_MAP WHERE serial_number=?';
  49. try {
  50. $sth = $dbh->prepare($sql);
  51. $res = $sth->execute(array($sn));
  52. } catch (PDOException $e) {
  53. header('Content-type: text/plain');
  54. echo "! ERROR: Failed to get switch for SN {$sn}: " . $e->getMessage() . "\r\n";
  55. $logger->crit("Failed to get switch for SN {$sn}: " . $e->getMessage());
  56. exit(1);
  57. }
  58. $row = $sth->fetch();
  59. $sth->closeCursor();
  60. // Only handle re-ZTP for now.
  61. if ($row['should_re_ztp'] == 2) {
  62. // This hack is needed to support IOS trying to copy the file twice.
  63. $sql = 'UPDATE DEVICE_MAP SET should_re_ztp=? WHERE serial_number=?';
  64. $logger->info("Saw 'taste' copy from switch ${sn}");
  65. try {
  66. $sth = $dbh->prepare($sql);
  67. $res = $sth->execute(array(1, $sn));
  68. } catch (PDOException $e) {
  69. $logger->crit("Failed to set the post-taste re-ZTP bit for {$sn}: {$e->getMessage()}");
  70. }
  71. exit(0);
  72. } elseif ($row['should_re_ztp'] == 1) {
  73. header('Content-type: text/plain');
  74. echo "! RE-ZTP\r\n";
  75. $logger->info("Sending RE-ZTP message for switch {$sn}");
  76. $sql = 'UPDATE DEVICE_MAP SET should_re_ztp=?, provisioned_status=?, device_status=? WHERE serial_number=?';
  77. try {
  78. $sth = $dbh->prepare($sql);
  79. $res = $sth->execute(array(0, PROVISION_UNKNOWN, REACHABILITY_NEVER_REACHABLE, $sn));
  80. } catch (PDOException $e) {
  81. $logger->crit("Failed to reset re-ZTP bit for {$sn}: {$e->getMessage()}");
  82. }
  83. exit(0);
  84. }
  85. // Send a 404 to prevent unnecessarily writing to flash
  86. http_response_code(404);
  87. exit(0);