verify.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. #-
  3. # Copyright (c) 2011-2015 Joe Clarke <jclarke@cisco.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. # SUCH DAMAGE.
  26. #
  27. #
  28. include_once 'db.inc.php';
  29. include_once 'swreg.inc.php';
  30. require_once 'MDB2.php';
  31. require_once 'Log.php';
  32. $dsn = "mysql://$db_user:$db_pass@$db_host/$db_name";
  33. $options = array('result_buffering', false);
  34. $dbh = MDB2::factory($dsn, $options);
  35. if (PEAR::isError($dbh)) {
  36. die($dbh->getMessage());
  37. }
  38. $dbh->setFetchMode(MDB2_FETCHMODE_ASSOC);
  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::UPTO(LOG_LEVEL);
  44. $logger->setMask($mask);
  45. $config = $_GET['config'];
  46. $sn = $_GET['sn'];
  47. header('Content-type: text/plain');
  48. if (!isset($config) || !isset($sn)) {
  49. echo "Invalid request!\r\n";
  50. $logger->emerg("Invalid request from " . $_SERVER['REMOTE_ADDR'] . " config = $config, sn = $sn");
  51. exit(1);
  52. }
  53. $logger->debug("Received request from {$_SERVER['REMOTE_ADDR']} with config = $config and sn = $sn");
  54. $cmd = escapeshellcmd("/usr/bin/diff " . DEVICE_CONFIG_DIR . '/' . $config . " " . DEVICE_TMP_DIR . '/' . $config);
  55. exec($cmd, $output, $return_var);
  56. $status = 0;
  57. if (count($output) > 0 || $return_var != 0) {
  58. echo "ERROR\r\n";
  59. $logger->emerg("Failed to verify config $config for $sn ({$_SERVER['REMOTE_ADDR']})");
  60. } else {
  61. echo "SUCCESS\r\n";
  62. $status = 1;
  63. $logger->debug("Verified config $config for $sn ({$_SERVER['REMOTE_ADDR']})");
  64. @unlink(DEVICE_TMP_DIR . '/' . $config);
  65. }
  66. $sql = 'UPDATE DEVICE_MAP SET provisioned_status = ? WHERE serial_number = ?';
  67. $sth = $dbh->prepare($sql);
  68. if (PEAR::isError($sth)) {
  69. echo "ERROR: Failed to prepare query '$sql': " . $sth->getUserInfo() . "\r\n";
  70. $logger->crit("Failed to prepare query '$sql': " . $sth->getUserInfo());
  71. exit(1);
  72. }
  73. $res = $sth->execute(array($status, $sn));
  74. $sth->free();
  75. if (PEAR::isError($res)) {
  76. echo "ERROR: Failed to execute query '$sql' with parameters '$status, $sn': " . $res->getUserInfo() . "\r\n";
  77. $logger->crit("Failed to execute query '$sql' with parameters '$status, $sn': " . $res->getUserInfo());
  78. exit(1);
  79. }
  80. $dbh->disconnect();
  81. $logger->close();
  82. ?>