From 10445367515af3b0905912ae12d974d5b594f9a2 Mon Sep 17 00:00:00 2001 From: Luke Wahlmeier Date: Wed, 9 Apr 2014 14:37:21 -0600 Subject: [PATCH 1/3] refactored how the static ddns call worked since most everything wants the same process, also updated some methods to depend more on local config instead of passed arguments. Added ability to run on the commandline --- godaddy-ddns.php | 136 +++++++++++++++++++++++++++++------------------ 1 file changed, 85 insertions(+), 51 deletions(-) mode change 100644 => 100755 godaddy-ddns.php diff --git a/godaddy-ddns.php b/godaddy-ddns.php old mode 100644 new mode 100755 index 8863d11..0e34445 --- a/godaddy-ddns.php +++ b/godaddy-ddns.php @@ -50,10 +50,6 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ -// Launch the method for accepting DDNS API requests -GoDaddyDNS::ddns(array( - 'detect_external_ip' => false, -)); /** * The main class for sending and parsing server requests to the @@ -69,6 +65,8 @@ class GoDaddyDNS private $_config; private $_curlHandle; private $_lastResponse; + public $_lastError; + public $_lastSuccess; /** * Initialize the configuration array with configuration defaults. @@ -78,11 +76,13 @@ public function __construct($config = array()) { $this->_config = array_merge(array( 'cookie_file' => tempnam(sys_get_temp_dir(), 'Curl'), 'auto_remove_cookie_file' => true, - 'auto_logout' => false, + 'auto_logout' => true, 'godaddy_dns_default_url' => 'https://dns.godaddy.com/default.aspx', 'godaddy_dns_zonefile_url' => 'https://dns.godaddy.com/ZoneFile.aspx?zoneType=0&sa=&zone=', 'godaddy_dns_zonefile_ws_url' => 'https://dns.godaddy.com/ZoneFile_WS.asmx', 'hostip_api_url' => 'http://api.hostip.info/', + 'offline' => false, + 'detect_external_ip' => false, ), $config); } @@ -122,55 +122,63 @@ public static function ddns($defaults = array()) { ), $defaults); // Get request values from HTTP data, falling back to the defaults above - $username = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : + $defaults['username'] = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : (isset($_REQUEST['user']) ? $_REQUEST['user'] : $defaults['username']); - $password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : + $defaults['password'] = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : (isset($_REQUEST['pw']) ? $_REQUEST['pw'] : $defaults['password']); - $hostname = isset($_REQUEST['hostname']) ? $_REQUEST['hostname'] : $defaults['hostname']; - $myip = isset($_REQUEST['myip']) ? $_REQUEST['myip'] : + $defaults['hostname'] = isset($_REQUEST['hostname']) ? $_REQUEST['hostname'] : $defaults['hostname']; + $defaults['myip'] = isset($_REQUEST['myip']) ? $_REQUEST['myip'] : (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : $defaults['myip']); - $offline = isset($_REQUEST['offline']) ? (strtoupper($_REQUEST['offline']) == 'YES') : $defaults['offline']; + $defaults['offline'] = isset($_REQUEST['offline']) ? (strtoupper($_REQUEST['offline']) == 'YES') : $defaults['offline']; + $dns = new self($defaults); + if(!$dns->updateRecord()) { + if($dns->_lastError == "noauth") { + header('WWW-Authenticate: Basic realm="Dynamic DNS"'); + header('HTTP/1.1 401 Unauthorized'); + } elseif($dns->_lastError == "badauth") { + header('WWW-Authenticate: Basic realm="Bad Auth"'); + } else {//($dns->_lastError == "911" || $dns->_lastError == "nohost") { + header('HTTP/1.1 500 Internal Server Error'); + } + echo $dns->_lastError; + } else { + echo (($dns->_lastSuccess['last_ip'] == $dns->_lastSuccess['new_ip']) ? 'nochg' : 'good') . ' ' . $dns->_lastSuccess['new_ip']; + } + } - if ($hostname) { - if ($username && $password) { - // Instantiate the GoDaddyDNS class - $dns = new self($defaults); - if ($dns->authenticate($username, $password, $hostname)) { - if ($offline) { - // Use offline IP - $myip = $defaults['offline_ip']; - } elseif ($defaults['detect_external_ip'] && $dns->isPrivateIp($myip)) { - if (($externalip = $dns->getPublicIp($myip))) { - // Use detected external IP - $myip = $externalip; - } + public function updateRecord() { + $this->_lastError = null; + if ($this->_config['hostname']) { + if ($this->_config['username'] && $this->_config['password']) { + if ($this->authenticate()) { + if ($this->_config['offline']) { + $this->_config['myip'] = $this->_config['offline_ip']; + } elseif ($this->_config['detect_external_ip']) { + $this->_config['myip'] = $this->getPublicIp(); } - // Attempt to update the hostname's A record - if (($result = $dns->setRecord($hostname, $myip, 'A'))) { - echo (($result['last_ip'] == $result['new_ip']) ? 'nochg' : 'good') . ' ' . $result['new_ip']; + if (($result = $this->setRecord())) { + $this->_lastSuccess = $result; + return true; } else { - header('HTTP/1.1 500 Internal Server Error'); - echo '911'; + $this->_lastError = '911'; } } else { - header('HTTP/1.1 403 Forbidden'); - echo 'badauth'; + $this->_lastError = 'badauth'; } } else { - header('WWW-Authenticate: Basic realm="Dynamic DNS"'); - header('HTTP/1.1 401 Unauthorized'); - echo 'noauth'; + $this->_lastError = 'noauth'; } } else { - header('HTTP/1.1 500 Internal Server Error'); - echo 'nohost'; + $this->_lastError = 'nohost'; } + return false; } + /** * Login to the user's account, returning an error if the credentials are * invalid or the login fails. @@ -178,31 +186,31 @@ public static function ddns($defaults = array()) { * A optional FQDN or TLD can be specified to log the user directly into * a specific zone record (eliminates an additional page request). */ - public function authenticate($username, $password, $hostname = null) { - if ($hostname) { - list($host, $domain) = $this->_splitHostname($hostname); + public function authenticate() { + if ($this->_config['hostname']) { + list($host, $domain) = $this->_splitHostname($this->_config['hostname']); $loginUrl = $this->_config['godaddy_dns_zonefile_url'] . $domain; } else { $loginUrl = $this->_config['godaddy_dns_default_url']; } $this->_lastResponse = $this->_fetchURL($loginUrl); - if (!$this->isLoggedIn($username)) { + if (!$this->isLoggedIn($this->_config['username'])) { // User is not already logged in, build and submit a login request $postUrl = curl_getinfo($this->_curlHandle, CURLINFO_EFFECTIVE_URL); $post = array( 'Login$userEntryPanel2$LoginImageButton.x' => 0, 'Login$userEntryPanel2$LoginImageButton.y' => 0, - 'Login$userEntryPanel2$UsernameTextBox' => $username, - 'Login$userEntryPanel2$PasswordTextBox' => $password, + 'Login$userEntryPanel2$UsernameTextBox' => $this->_config['username'], + 'Login$userEntryPanel2$PasswordTextBox' => $this->_config['password'], '__EVENTARGUMENT' => $this->_getField('__EVENTARGUMENT'), '__EVENTTARGET' => $this->_getField('__EVENTTARGET'), '__VIEWSTATE' => $this->_getField('__VIEWSTATE'), ); $this->_lastResponse = $this->_fetchURL($postUrl, $post); - if (!$this->isLoggedIn($username, $this->_lastResponse)) { + if (!$this->isLoggedIn()) { // Invalid username/password or unknown response received return false; } @@ -213,9 +221,9 @@ public function authenticate($username, $password, $hostname = null) { /** * Check to see if the expected user is logged in. */ - public function isLoggedIn($username) { + public function isLoggedIn() { if (preg_match('#Welcome: (.*)#', $this->_lastResponse, $match)) { - if (substr(strtolower($match[1]),0,7) == substr(strtolower($username),0,7) || $match[2] == $username) { + if (substr(strtolower($match[1]),0,7) == substr(strtolower($this->_config['username']),0,7) || $match[2] == $this->_config['username']) { return true; } else { // An unexpected user was logged in @@ -248,17 +256,17 @@ public function logout() { * (such as the TTL), or submitting batch edit requests (instead * of handling them one at a time) could be added in the future. */ - public function setRecord($hostname, $data, $type = 'A') { - list($host, $domain) = $this->_splitHostname($hostname); + public function setRecord($type = 'A') { + list($host, $domain) = $this->_splitHostname($this->_config['hostname']); switch (strtoupper($type)) { case 'A': if (!($record = $this->_findRecord($host, $domain, $type))) { // Host record not found return false; - } elseif ($record['data'] != $data) { + } elseif ($record['data'] != $this->_config['myip']) { // A record is out of date, build the query for updating it $post = array( - 'sInput' => '', + 'sInput' => '', ); $calloutResponse = $this->_fetchURL($this->_config['godaddy_dns_zonefile_ws_url'] . '/EditRecordField', http_build_query($post, '', '&')); if (strpos($calloutResponse, 'SUCCESS') === false) { @@ -278,7 +286,7 @@ public function setRecord($hostname, $data, $type = 'A') { // The update succeeded or no changes were needed return array( 'last_ip' => $record['data'], - 'new_ip' => $data, + 'new_ip' => $this->_config['myip'], ); case 'CNAME': case 'MX': @@ -327,7 +335,7 @@ public function getPublicIp() { /** * Checks to see whether an IP address is private (non-routable) or public. */ - public function isPrivateIp($ip) { + public static function isPrivateIp($ip) { return (strpos($ip, '10.') === 0 || strpos($ip, '127.') === 0 || strpos($ip, '169.254.') === 0 || @@ -338,7 +346,7 @@ public function isPrivateIp($ip) { /** * Determine the host and domain name components given a hostname - could add some sanity testing. */ - private function _splitHostname($hostname) { + private static function _splitHostname($hostname) { $len = strlen($hostname); $tldPos = strrpos($hostname, '.', 0); $sldPos = strrpos($hostname, '.', ($tldPos-$len-1)); @@ -425,3 +433,29 @@ private function _getFieldAttribute($attribute, $fieldHtml) { return false; } } + +if(php_sapi_name() == "cli") { + if (count($argv) < 4) { + echo "Usage:\n ".$argv[0]." \n"; + exit(1); + } + print_r($argv); + + $ddns = new GoDaddyDNS(array( + 'username' => $argv[1], + 'password' => $argv[2], + 'hostname' => $argv[3], + 'detect_external_ip' => true,)); + + if($ddns->updateRecord()) { + echo "Updated Record\n"; + exit(0); + } else { + echo "Problem updating Record \"".$ddns->_lastError."\"\n"; + exit(1); + } +} else { + GoDaddyDNS::ddns(); +} + +?> From ac35d86ffce9572553923d2ed24032220a06eef7 Mon Sep 17 00:00:00 2001 From: Luke Wahlmeier Date: Wed, 9 Apr 2014 14:57:01 -0600 Subject: [PATCH 2/3] Added comment around new function call --- godaddy-ddns.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/godaddy-ddns.php b/godaddy-ddns.php index 0e34445..d41068b 100755 --- a/godaddy-ddns.php +++ b/godaddy-ddns.php @@ -149,8 +149,13 @@ public static function ddns($defaults = array()) { } + /** + * This is the normal process we run through to update a DNS entry, check/parse config, get any + * IP information then update the record. + * True is returned if we can update, false if we cant. _lastError and _lastSuccess where added to get + * any needed info from after we try. + */ public function updateRecord() { - $this->_lastError = null; if ($this->_config['hostname']) { if ($this->_config['username'] && $this->_config['password']) { if ($this->authenticate()) { From 06600b37f7f8e16d84a305cba4c78419b8b9d9ff Mon Sep 17 00:00:00 2001 From: Luke Wahlmeier Date: Mon, 14 Apr 2014 09:52:19 -0600 Subject: [PATCH 3/3] Removed an unneeded debug log message --- godaddy-ddns.php | 1 - 1 file changed, 1 deletion(-) diff --git a/godaddy-ddns.php b/godaddy-ddns.php index d41068b..1159133 100755 --- a/godaddy-ddns.php +++ b/godaddy-ddns.php @@ -444,7 +444,6 @@ private function _getFieldAttribute($attribute, $fieldHtml) { echo "Usage:\n ".$argv[0]." \n"; exit(1); } - print_r($argv); $ddns = new GoDaddyDNS(array( 'username' => $argv[1],