verify.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. //-
  3. // Copyright (c) 2011-2016 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. require_once '../functions.php';
  27. require_once 'Log.php';
  28. $dsn = "$db_driver:host=$db_host;dbname=$db_name";
  29. $options = [
  30. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  31. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  32. PDO::ATTR_EMULATE_PREPARES => false,
  33. ];
  34. try {
  35. $dbh = new PDO($dsn, $db_user, $db_pass, $options);
  36. } catch (PDOException $e) {
  37. die($e->getMessage());
  38. }
  39. $logger = Log::singleton('file', LOGFILE, TOOL_NAME . ' : Verify Config');
  40. if ($logger === false) {
  41. die("Failed to open logfile.\n");
  42. }
  43. $mask = Log::MAX(LOG_LEVEL);
  44. $logger->setMask($mask);
  45. $config = $_GET['config'] ?: '';
  46. $sn = $_GET['sn'];
  47. $md5 = $_GET['md5'] ?: '';
  48. $image = $_GET['image'] ?: '';
  49. header('Content-type: text/plain');
  50. if (!isset($sn)) {
  51. echo "Invalid request!\r\n";
  52. $logger->emerg('Invalid request from ' . $_SERVER['REMOTE_ADDR'] . " config = $config, sn = $sn, md5 = $md5, image = $image");
  53. exit(1);
  54. }
  55. $logger->debug("Received request from {$_SERVER['REMOTE_ADDR']} with config = $config, sn = $sn, md5 = $md5, and image = $image");
  56. $status = PROVISION_FAIL;
  57. $good_config = true;
  58. $good_image = true;
  59. if (isset($config) && $config != '') {
  60. $cmd = escapeshellcmd('/usr/bin/diff ' . DEVICE_CONFIG_DIR . '/' . $config . ' ' . DEVICE_TMP_DIR . '/' . $config);
  61. exec($cmd, $output, $return_var);
  62. if (count($output) > 0 || $return_var != 0) {
  63. echo "ERROR: Config validation failed\r\n";
  64. $out_str = implode("\n", $output);
  65. $logger->emerg("Failed to verify config $config for $sn ({$_SERVER['REMOTE_ADDR']}) '$out_str' ($return_var)");
  66. call_hook('VERIFY:FAIL', array('config', $_SERVER['REMOTE_ADDR'], $config, $sn, $md5, $image));
  67. $good_config = false;
  68. } else {
  69. $logger->debug("Verified config $config for $sn ({$_SERVER['REMOTE_ADDR']})");
  70. @unlink(DEVICE_TMP_DIR . '/' . $config);
  71. }
  72. }
  73. if (isset($md5) && $md5 != '' && isset($image) && $image != '') {
  74. $good_md5 = strtolower(md5_file(TFTPBOOT . '/' . $image));
  75. $md5 = strtolower($md5);
  76. if ($md5 != $good_md5) {
  77. echo "ERROR: Image verification failed\r\n";
  78. $logger->emerg("Failed to verify image $image with MD5 $md5 (good MD5: $good_md5) for $sn ({$_SERVER['REMOTE_ADDR']})");
  79. call_hook('VERIFY:FAIL', array('image', $_SERVER['REMOTE_ADDR'], $config, $sn, $md5, $good_md5, $image));
  80. $good_image = false;
  81. } else {
  82. $logger->debug("Verified image with MD5 $md5 for $sn ({$_SERVER['REMOTE_ADDR']})");
  83. }
  84. }
  85. if ($good_config && $good_image) {
  86. $status = PROVISION_SUCCESS;
  87. echo "SUCCESS\r\n";
  88. call_hook('VERIFY:SUCCESS', array($_SERVER['REMOTE_ADDR'], $config, $sn, $md5, $image));
  89. }
  90. $sql = 'UPDATE DEVICE_MAP SET provisioned_status = ?, should_re_ztp = ? WHERE serial_number = ?';
  91. try {
  92. $sth = $dbh->prepare($sql);
  93. $sth->execute(array($status, 0, $sn));
  94. $sth->closeCursor();
  95. } catch (PDOException $e) {
  96. echo 'ERROR: Failed to update switch status: ' . $e->getMessage() . "\r\n";
  97. $logger->crit('Failed to update switch status: ' . $e->getMessage());
  98. exit(1);
  99. }
  100. $logger->close();