Project

General

Profile

Download (49.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
	/*
3
	 * PHP.updateDNS (pfSense version)
4
	 *
5
	 * +====================================================+
6
	 *  Services Supported:
7
	 *    - DynDns (dyndns.org) [dynamic, static, custom]
8
	 *    - DHSDns (dhs.org)
9
	 *    - No-IP (no-ip.com)
10
	 *    - EasyDNS (easydns.com)
11
	 *    - DHS (www.dhs.org)
12
	 *    - HN (hn.org) -- incomplete checking!
13
	 *    - DynS (dyns.org)
14
	 *    - ZoneEdit (zoneedit.com)
15
	 *    - FreeDNS (freedns.afraid.org)
16
	 *    - Loopia (loopia.se)
17
	 *    - StaticCling (staticcling.org)
18
	 *    - DNSexit (dnsexit.com)
19
	 *    - OpenDNS (opendns.com)
20
	 *    - Namecheap (namecheap.com)
21
	 *    - HE.net (dns.he.net)
22
	 *    - HE.net IPv6 (dns.he.net)
23
	 *    - HE.net Tunnelbroker IP update (ipv4.tunnelbroker.net)
24
	 *    - SelfHost (selfhost.de)
25
	 *    - Amazon Route 53 (aws.amazon.com)
26
	 *    - DNS-O-Matic (dnsomatic.com)
27
	 *    - Custom DDNS (any URL)
28
	 *    - Custom DDNS IPv6 (any URL)
29
	 * +----------------------------------------------------+
30
	 *  Requirements:
31
	 *    - PHP version 4.0.2 or higher with the CURL Library and the PCRE Library
32
	 * +----------------------------------------------------+
33
	 *  Public Functions
34
	 *    - updatedns()
35
	 *
36
	 *  Private Functions
37
	 *    - _update()
38
	 *    - _checkStatus()
39
	 *    - _error()
40
	 *    - _detectChange()
41
	 *    - _debug()
42
	 *    - _checkIP()
43
	 * +----------------------------------------------------+
44
	 *  DynDNS Dynamic - Last Tested: 12 July 2005
45
	 *  DynDNS Static  - Last Tested: NEVER
46
	 *  DynDNS Custom  - Last Tested: NEVER
47
	 *  No-IP          - Last Tested: 20 July 2008
48
	 *  HN.org         - Last Tested: 12 July 2005
49
	 *  EasyDNS        - Last Tested: 20 July 2008
50
	 *  DHS            - Last Tested: 12 July 2005
51
	 *  ZoneEdit       - Last Tested: NEVER
52
	 *  Dyns           - Last Tested: NEVER
53
	 *  ODS            - Last Tested: 02 August 2005
54
	 *  FreeDNS        - Last Tested: 23 Feb 2011
55
	 *  Loopia         - Last Tested: NEVER
56
	 *  StaticCling    - Last Tested: 27 April 2006
57
	 *  DNSexit	   - Last Tested: 20 July 2008
58
	 *  OpenDNS	   - Last Tested: 4 August 2008
59
	 *  Namecheap	   - Last Tested: 31 August 2010
60
	 *  HE.net         - Last Tested: 7 July 2013
61
	 *  HE.net IPv6    - Last Tested: 7 July 2013
62
	 *  HE.net Tunnel  - Last Tested: 28 June 2011
63
	 *  SelfHost       - Last Tested: 26 December 2011
64
	 *  Amazon Route 53 - Last tested: 01 April 2012
65
	 *  DNS-O-Matic	   - Last Tested: 9 September 2010
66
	 * +====================================================+
67
	 *
68
	 * @author 	E.Kristensen
69
	 * @link    	http://www.idylldesigns.com/projects/phpdns/
70
	 * @version 	0.8
71
	 * @updated	13 October 05 at 21:02:42 GMT
72
	 *
73
	 * DNSexit/OpenDNS support and multiwan extension for pfSense by Ermal Luci
74
	 * Custom DNS support by Matt Corallo
75
	 *
76
	 */
77

    
78
	class updatedns {
79
		var $_cacheFile;
80
		var $_cacheFile_v6;
81
		var $_debugFile;
82
		var $_UserAgent = 'User-Agent: phpDynDNS/0.7';
83
		var $_errorVerbosity = 0;
84
		var $_dnsService;
85
		var $_dnsUser;
86
		var $_dnsPass;
87
		var $_dnsHost;
88
		var $_dnsIP;
89
		var $_dnsWildcard;
90
		var $_dnsMX;
91
		var $_dnsBackMX;
92
		var $_dnsServer;
93
		var $_dnsPort;
94
		var $_dnsUpdateURL;
95
		var $_dnsZoneID;
96
		var $_dnsTTL;
97
		var $status;
98
		var $_debugID;
99
		var $_if;
100
		var $_dnsResultMatch;
101
		var $_dnsRequestIf;
102
		var $_dnsRequestIfIP;
103
		var $_dnsVerboseLog;
104
		var $_dnsMaxCacheAgeDays;
105
		var $_dnsDummyUpdateDone;
106
		var $_forceUpdateNeeded;
107
		var $_useIPv6;
108
		
109
		/* 
110
		 * Public Constructor Function (added 12 July 05) [beta]
111
		 *   - Gets the dice rolling for the update. 
112
		 *   - $dnsResultMatch should only be used with $dnsService = 'custom'
113
		 *   -  $dnsResultMatch is parsed for '%IP%', which is the IP the provider was updated to, 
114
		 *   -  it is otherwise expected to be exactly identical to what is returned by the Provider.
115
		 *   - $dnsUser, and $dnsPass indicate HTTP Auth for custom DNS, if they are needed in the URL (GET Variables), include them in $dnsUpdateURL.
116
		 *   - $For custom requests, $dnsUpdateURL is parsed for '%IP%', which is replaced with the new IP.
117
		 */
118
		function updatedns ($dnsService = '', $dnsHost = '', $dnsUser = '', $dnsPass = '',
119
				    $dnsWildcard = 'OFF', $dnsMX = '', $dnsIf = '', $dnsBackMX = '',
120
				    $dnsServer = '', $dnsPort = '', $dnsUpdateURL = '', $forceUpdate = false,
121
				    $dnsZoneID ='', $dnsTTL='', $dnsResultMatch = '', $dnsRequestIf = '',
122
				    $dnsID = '', $dnsVerboseLog = false) {
123
			
124
			global $config, $g;
125
			
126
			$this->_cacheFile = "{$g['conf_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}.cache";
127
			$this->_cacheFile_v6 = "{$g['conf_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}_v6.cache";
128
			$this->_debugFile = "{$g['varetc_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}.debug";
129

    
130
			$this->_dnsVerboseLog = $dnsVerboseLog;
131
			if ($this->_dnsVerboseLog)
132
				log_error("DynDns: updatedns() starting");
133

    
134
			$dyndnslck = lock("DDNS".$dnsID, LOCK_EX);
135

    
136
			if (!$dnsService) $this->_error(2);
137
			switch ($dnsService) {
138
			case 'freedns':
139
				if (!$dnsHost) $this->_error(5);
140
				break;
141
			case 'namecheap':
142
				if (!$dnsPass) $this->_error(4);
143
				if (!$dnsHost) $this->_error(5);
144
				break;
145
			case 'route53':
146
				if (!$dnsZoneID) $this->_error(8);
147
				if (!$dnsTTL) $this->_error(9);
148
				break;
149
			case 'custom':
150
				if (!$dnsUpdateURL) $this->_error(7);
151
				break;
152
			default:
153
				if (!$dnsUser) $this->_error(3);
154
				if (!$dnsPass) $this->_error(4);
155
				if (!$dnsHost) $this->_error(5);
156
			}
157
			
158
			switch ($dnsService) {
159
			case 'he-net-v6':
160
			case 'custom-v6':
161
				$this->_useIPv6 = true;
162
				break;
163
			default:
164
				$this->_useIPv6 = false;
165
			}
166
			$this->_dnsService = strtolower($dnsService);
167
			$this->_dnsUser = $dnsUser;
168
			$this->_dnsPass = $dnsPass;
169
			$this->_dnsHost = $dnsHost;
170
			$this->_dnsServer = $dnsServer;
171
			$this->_dnsPort = $dnsPort;
172
			$this->_dnsWildcard = $dnsWildcard;
173
			$this->_dnsMX = $dnsMX;
174
			$this->_dnsZoneID = $dnsZoneID;
175
			$this->_dnsTTL = $dnsTTL;
176
			$this->_if = get_failover_interface($dnsIf);
177
			$this->_checkIP();
178
			$this->_dnsUpdateURL = $dnsUpdateURL;
179
			$this->_dnsResultMatch = $dnsResultMatch;
180
			$this->_dnsRequestIf = get_failover_interface($dnsRequestIf);
181
			if ($this->_dnsVerboseLog)
182
				log_error("DynDNS ({$this->_dnsHost}): running get_failover_interface for {$dnsRequestIf}. found {$this->_dnsRequestIf}");
183
			$this->_dnsRequestIfIP = get_interface_ip($dnsRequestIf);
184
			$this->_dnsMaxCacheAgeDays = 25;
185
			$this->_dnsDummyUpdateDone = false;
186
			$this->_forceUpdateNeeded = $forceUpdate;
187
			
188
			// Ensure that we were able to lookup the IP
189
			if(!is_ipaddr($this->_dnsIP)) {
190
				log_error("DynDNS ({$this->_dnsHost}) There was an error trying to determine the public IP for interface - {$dnsIf}({$this->_if}). Probably interface is not a WAN interface.");
191
				unlock($dyndnslck);
192
				return;
193
			}
194

    
195
			$this->_debugID = rand(1000000, 9999999);
196
			
197
			if ($forceUpdate == false && $this->_detectChange() == false) {
198
				$this->_error(10);
199
			} else {
200
				switch ($this->_dnsService) {
201
				case 'dnsomatic':
202
				case 'dyndns':
203
				case 'dyndns-static':
204
				case 'dyndns-custom':
205
				case 'dhs':
206
				case 'noip':
207
				case 'noip-free':
208
				case 'easydns':
209
				case 'hn':
210
				case 'zoneedit':
211
				case 'dyns':
212
				case 'ods':
213
				case 'freedns':
214
				case 'loopia':
215
				case 'staticcling':
216
				case 'dnsexit':
217
				case 'custom':
218
				case 'custom-v6':
219
				case 'opendns':
220
				case 'namecheap':
221
				case 'he-net':
222
				case 'he-net-v6':
223
				case 'selfhost':
224
				case 'he-net-tunnelbroker':
225
				case 'route53':
226
					$this->_update();
227
					if($this->_dnsDummyUpdateDone == true) {
228
						// If a dummy update was needed, then sleep a while and do the update again to put the proper address back.
229
						// Some providers (e.g. No-IP free accounts) need to have at least 1 address change every month.
230
						// If the address has not changed recently, or the user did "Force Update", then the code does
231
						// a dummy address change for providers like this.
232
						sleep(10);
233
						$this->_update();
234
					}
235
					break;
236
				default:
237
					$this->_error(6);
238
					break;
239
				}
240
			}
241

    
242
			unlock($dyndnslck);
243
		}
244
			
245
		/*
246
		 * Private Function (added 12 July 05) [beta]
247
		 *   Send Update To Selected Service.
248
		 */
249
		function _update() {
250
		
251
			if ($this->_dnsVerboseLog)
252
				log_error("DynDNS ({$this->_dnsHost}): DynDns _update() starting.");
253
		
254
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53 ') {
255
				$ch = curl_init();
256
				curl_setopt($ch, CURLOPT_HEADER, 0);
257
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
258
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
259
				curl_setopt($ch, CURLOPT_INTERFACE, $this->_dnsRequestIfIP);
260
				curl_setopt($ch, CURLOPT_TIMEOUT, 120); // Completely empirical
261
			}
262

    
263
			switch ($this->_dnsService) {
264
				case 'dyndns':
265
				case 'dyndns-static':
266
				case 'dyndns-custom':
267
					$needsIP = FALSE;
268
					if ($this->_dnsVerboseLog)
269
						log_error("DynDNS: ({$this->_dnsHost}) DNS update() starting.");
270
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
271
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
272
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
273
					$server = "https://members.dyndns.org/nic/update";
274
					$port = "";
275
					if($this->_dnsServer)
276
						$server = $this->_dnsServer;
277
					if($this->_dnsPort)
278
						$port = ":" . $this->_dnsPort;
279
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
280
					break;
281
				case 'dhs':
282
					$needsIP = TRUE;
283
					$post_data['hostscmd'] = 'edit';
284
					$post_data['hostscmdstage'] = '2';
285
					$post_data['type'] = '4';
286
					$post_data['updatetype'] = 'Online';
287
					$post_data['mx'] = $this->_dnsMX;
288
					$post_data['mx2'] = '';
289
					$post_data['txt'] = '';
290
					$post_data['offline_url'] = '';
291
					$post_data['cloak'] = 'Y';
292
					$post_data['cloak_title'] = '';
293
					$post_data['ip'] = $this->_dnsIP;
294
					$post_data['domain'] = 'dyn.dhs.org';
295
					$post_data['hostname'] = $this->_dnsHost;
296
					$post_data['submit'] = 'Update';
297
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
298
					$server = "https://members.dhs.org/nic/hosts";
299
					$port = "";
300
					if($this->_dnsServer)
301
						$server = $this->_dnsServer;
302
					if($this->_dnsPort)
303
						$port = ":" . $this->_dnsPort;					
304
					curl_setopt($ch, CURLOPT_URL, '{$server}{$port}');
305
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
306
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
307
					break;
308
				case 'noip':
309
				case 'noip-free':
310
					$needsIP = TRUE;
311
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
312
					$server = "http://dynupdate.no-ip.com/ducupdate.php";
313
					$port = "";
314
					if($this->_dnsServer)
315
						$server = $this->_dnsServer;
316
					if($this->_dnsPort)
317
						$port = ":" . $this->_dnsPort;
318
					if(($this->_dnsService == "noip-free") && 
319
					   ($this->_forceUpdateNeeded == true) && 
320
					   ($this->_dnsDummyUpdateDone == false)) {
321
						// Update the IP to a dummy value to force No-IP free accounts to see a change.
322
						$iptoset = "192.168.1.1";
323
						$this->_dnsDummyUpdateDone = true;
324
						log_error("DynDNS ({$this->_dnsHost}): Processing dummy update on No-IP free account. IP temporarily set to " . $iptoset);
325
					} else {
326
						$iptoset = $this->_dnsIP;
327
					}
328
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?username=' . urlencode($this->_dnsUser) . '&pass=' . urlencode($this->_dnsPass) . '&hostname=' . $this->_dnsHost.'&ip=' . $iptoset);
329
					break;
330
				case 'easydns':
331
					$needsIP = TRUE;
332
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
333
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
334
					$server = "http://members.easydns.com/dyn/dyndns.php";
335
					$port = "";
336
					if($this->_dnsServer)
337
						$server = $this->_dnsServer;
338
					if($this->_dnsPort)
339
						$port = ":" . $this->_dnsPort;
340
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard=' . $this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=' . $this->_dnsBackMX);
341
					break;
342
				case 'hn':
343
					$needsIP = TRUE;
344
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
345
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
346
					$server = "http://dup.hn.org/vanity/update";
347
					$port = "";
348
					if($this->_dnsServer)
349
						$server = $this->_dnsServer;
350
					if($this->_dnsPort)
351
						$port = ":" . $this->_dnsPort;
352
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?ver=1&IP=' . $this->_dnsIP);
353
					break;
354
				case 'zoneedit':
355
					$needsIP = FALSE;
356
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
357
					curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
358
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
359

    
360
					$server = "https://dynamic.zoneedit.com/auth/dynamic.html";
361
					$port = "";
362
					if($this->_dnsServer)
363
						$server = $this->_dnsServer;
364
					if($this->_dnsPort)
365
						$port = ":" . $this->_dnsPort;
366
					curl_setopt($ch, CURLOPT_URL, "{$server}{$port}?host=" .$this->_dnsHost);
367
					break;
368
				case 'dyns':
369
					$needsIP = FALSE;
370
					$server = "http://www.dyns.cx/postscript011.php";
371
					$port = "";
372
					if($this->_dnsServer)
373
						$server = $this->_dnsServer;
374
					if($this->_dnsPort)
375
						$port = ":" . $this->_dnsPort;					
376
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?username=' . urlencode($this->_dnsUser) . '&password=' . $this->_dnsPass . '&host=' . $this->_dnsHost);
377
					break;
378
				case 'ods':
379
					$needsIP = FALSE;
380
					$misc_errno = 0;
381
					$misc_error = "";
382
					$server = "ods.org";
383
					$port = "";
384
					if($this->_dnsServer)
385
						$server = $this->_dnsServer;
386
					if($this->_dnsPort)
387
						$port = ":" . $this->_dnsPort;						
388
					$this->con['socket'] = fsockopen("{$server}{$port}", "7070", $misc_errno, $misc_error, 30);
389
					/* Check that we have connected */
390
					if (!$this->con['socket']) {
391
						print "error! could not connect.";
392
						break;
393
					}
394
					/* Here is the loop. Read the incoming data (from the socket connection) */
395
					while (!feof($this->con['socket'])) {
396
						$this->con['buffer']['all'] = trim(fgets($this->con['socket'], 4096));
397
						$code = substr($this->con['buffer']['all'], 0, 3);
398
						sleep(1);
399
						switch($code) {
400
							case 100:
401
								fputs($this->con['socket'], "LOGIN ".$this->_dnsUser." ".$this->_dnsPass."\n");
402
								break;
403
							case 225:
404
								fputs($this->con['socket'], "DELRR ".$this->_dnsHost." A\n");
405
								break;
406
							case 901:
407
								fputs($this->con['socket'], "ADDRR ".$this->_dnsHost." A ".$this->_dnsIP."\n");
408
								break;
409
							case 795:
410
								fputs($this->con['socket'], "QUIT\n");
411
								break;
412
						}
413
					}
414
					$this->_checkStatus(0, $code);
415
					break;
416
				case 'freedns':
417
					$needIP = FALSE;
418
					curl_setopt($ch, CURLOPT_URL, 'http://freedns.afraid.org/dynamic/update.php?' . $this->_dnsPass);
419
					break;
420
				case 'dnsexit':
421
					$needsIP = TRUE;
422
					curl_setopt($ch, CURLOPT_URL, 'http://www.dnsexit.com/RemoteUpdate.sv?login='.$this->_dnsUser. '&password='.$this->_dnsPass.'&host='.$this->_dnsHost.'&myip='.$this->_dnsIP);
423
					break;
424
				case 'loopia':
425
					$needsIP = TRUE;
426
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
427
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
428
					curl_setopt($ch, CURLOPT_URL, 'https://dns.loopia.se/XDynDNSServer/XDynDNS.php?hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP);
429
					break;
430
				case 'opendns':
431
					$needsIP = FALSE;
432
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
433
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
434
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
435
					$server = "https://updates.opendns.com/nic/update?hostname=". $this->_dnsHost;
436
					$port = "";
437
					if($this->_dnsServer)
438
						$server = $this->_dnsServer;
439
					if($this->_dnsPort)
440
						$port = ":" . $this->_dnsPort;
441
					curl_setopt($ch, CURLOPT_URL, $server .$port);
442
					break;
443

    
444
				case 'staticcling':
445
					$needsIP = FALSE;
446
					curl_setopt($ch, CURLOPT_URL, 'http://www.staticcling.org/update.html?login='.$this->_dnsUser.'&pass='.$this->_dnsPass);
447
					break;	                    
448
				case 'dnsomatic':
449
					/* Example syntax 
450
						https://username:password@updates.dnsomatic.com/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG
451
					*/
452
					$needsIP = FALSE;
453
					if ($this->_dnsVerboseLog)
454
						log_error("DNS-O-Matic: DNS update() starting.");
455
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
456
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
457
					/*
458
					Reference: https://www.dnsomatic.com/wiki/api
459
						DNS-O-Matic usernames are 3-25 characters.
460
						DNS-O-Matic passwords are 6-20 characters.
461
						All ASCII letters and numbers accepted.
462
						Dots, dashes, and underscores allowed, but not at the beginning or end of the string.
463
					Required: "rawurlencode" http://www.php.net/manual/en/function.rawurlencode.php
464
						Encodes the given string according to RFC 3986.
465
					*/
466
					$server = "https://" . rawurlencode($this->_dnsUser) . ":" . rawurlencode($this->_dnsPass) . "@updates.dnsomatic.com/nic/update?hostname=";
467
					if($this->_dnsServer)
468
						$server = $this->_dnsServer;
469
					if($this->_dnsPort)
470
						$port = ":" . $this->_dnsPort;
471
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NOCHG');
472
					break;
473
				case 'namecheap':
474
					/* Example:
475
						https://dynamicdns.park-your-domain.com/update?host=[host_name]&domain=[domain.com]&password=[domain_password]&ip=[your_ip]
476
					*/
477
					$needsIP = FALSE;
478
					if ($this->_dnsVerboseLog)
479
						log_error("Namecheap ({$this->_dnsHost}): DNS update() starting.");
480
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
481
					$dparts = explode(".", trim($this->_dnsHost));
482
					$domain_part_count = ($dparts[count($dparts)-1] == "uk") ? 3 : 2;
483
					$domain_offset = count($dparts) - $domain_part_count;
484
					$hostname = implode(".", array_slice($dparts, 0, $domain_offset));
485
					$domain = implode(".", array_slice($dparts, $domain_offset));
486
					$dnspass = trim($this->_dnsPass);
487
					$server = "https://dynamicdns.park-your-domain.com/update?host={$hostname}&domain={$domain}&password={$dnspass}&ip={$this->_dnsIP}";
488
					curl_setopt($ch, CURLOPT_URL, $server);
489
					break;
490
				case 'he-net':
491
				case 'he-net-v6':
492
					$needsIP = FALSE;
493
					if ($this->_dnsVerboseLog)
494
						log_error("HE.net ({$this->_dnsHost}): DNS update() starting.");
495
					$server = "https://dyn.dns.he.net/nic/update?";
496
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
497
					curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
498
					curl_setopt($ch, CURLOPT_URL, $server . 'hostname=' . $this->_dnsHost . '&password=' . $this->_dnsPass . '&myip=' . $this->_dnsIP);
499
					break;
500
				case 'he-net-tunnelbroker':
501
					$needsIP = FALSE;
502
					if ($this->_dnsVerboseLog)
503
						log_error("HE.net Tunnelbroker: DNS update() starting.");
504
					$server = "https://ipv4.tunnelbroker.net/ipv4_end.php?";
505
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
506
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser . ':' . $this->_dnsPass);
507
					curl_setopt($ch, CURLOPT_URL, $server . 'tid=' . $this->_dnsHost);
508
					break;
509
				case 'selfhost':
510
					$needsIP = FALSE;
511
					if ($this->_dnsVerboseLog)
512
						log_error("SelfHost: DNS update() starting.");
513
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
514
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
515
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
516
					$server = "https://carol.selfhost.de/nic/update";
517
					$port = "";
518
					if($this->_dnsServer)
519
						$server = $this->_dnsServer;
520
					if($this->_dnsPort)
521
						$port = ":" . $this->_dnsPort;
522
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
523
					break;
524
				case 'route53':
525
					if ($this->_dnsVerboseLog)
526
						log_error("Route53 ({$this->_dnsHost}): DNS update() starting.");
527
					
528
					/* Setting Variables */
529
					$hostname = "{$this->_dnsHost}.";
530
					$ZoneID = $this->_dnsZoneID;
531
					$AccessKeyId=$this->_dnsUser;
532
					$SecretAccessKey=$this->_dnsPass;
533
					$NewIP=$this->_dnsIP;
534
					$NewTTL=$this->_dnsTTL;
535

    
536
					/* Include Route 53 Library Class */
537
					require_once('/etc/inc/r53.class');
538

    
539
					/* Set Amazon AWS Credentials for this record */
540
					$r53 = new Route53($AccessKeyId, $SecretAccessKey);
541

    
542
					/* Function to find old values of records in Route 53 */
543
					if(!function_exists('Searchrecords')) {
544
						function SearchRecords($records, $name) {
545
							$result = array();
546
							foreach($records as $record) {
547
								if(strtolower($record['Name']) == strtolower($name)) {
548
									$result [] = $record;
549
								}
550
							}
551
							return ($result) ? $result : false;
552
						}
553
					}
554

    
555
					$records = $r53->listResourceRecordSets("/hostedzone/$ZoneID");
556

    
557
					/* Get IP for your hostname in Route 53 */
558
					if(false !== ($a_result = SearchRecords($records['ResourceRecordSets'], "$hostname"))) {
559
						$OldTTL=$a_result[0][TTL];
560
						$OldIP=$a_result[0][ResourceRecords][0];
561
					} else {
562
						$OldIP="";
563
					}
564

    
565
					/* Check if we need to update DNS Record */
566
					if ($OldIP !== $NewIP) {
567
						if(!empty($OldIP)) {
568
							/* Your Hostname already exists, deleting and creating it again */
569
							$changes = array();
570
							$changes[] = $r53->prepareChange(DELETE, $hostname, A, $OldTTL, $OldIP);
571
							$changes[] = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
572
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
573
						} else {
574
							/* Your Hostname does not exist yet, creating it */
575
							$changes = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
576
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
577
						}
578
					}
579
					$this->_checkStatus(0, $result);
580
					break;
581
				case 'custom':
582
				case 'custom-v6':
583
					if ($this->_dnsVerboseLog)
584
						log_error("Custom DDNS ({$this->_dnsHost}): DNS update() starting.");
585
					if (strstr($this->dnsUpdateURL, "%IP%")) {$needsIP = TRUE;} else {$needsIP = FALSE;}
586
					if ($this->_dnsUser != '') {
587
						curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
588
						curl_setopt($ch, CURLOPT_USERPWD, "{$this->_dnsUser}:{$this->_dnsPass}");
589
					}
590
					$server = str_replace("%IP%", $this->_dnsIP, $this->_dnsUpdateURL);
591
					if ($this->_dnsVerboseLog)
592
						log_error("Sending request to: ".$server);
593
					curl_setopt($ch, CURLOPT_URL, $server);
594
					break;
595
				default:
596
					break;
597
			}
598
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53') {
599
				$data = curl_exec($ch);
600
				$this->_checkStatus($ch, $data);
601
				@curl_close($ch);
602
			}
603
		}
604

    
605
		/*
606
		 * Private Function (added 12 July 2005) [beta]
607
		 *   Retrieve Update Status
608
		 */
609
		function _checkStatus($ch, $data) {
610
			if ($this->_dnsVerboseLog) {
611
				log_error("DynDNS ({$this->_dnsHost}): DynDns _checkStatus() starting.");
612
				log_error("DynDNS ({$this->_dnsHost}): Current Service: {$this->_dnsService}");
613
			}
614
			$successful_update = false;
615
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53' && @curl_error($ch)) {
616
				$status = "Curl error occurred: " . curl_error($ch);
617
				log_error($status);
618
				$this->status = $status;
619
				return;
620
			}
621
			switch ($this->_dnsService) {
622
				case 'dnsomatic':
623
					if (preg_match('/badauth/i', $data)) {
624
						$status = "DNS-O-Matic ({$this->_dnsHost}): The DNS-O-Matic username or password specified are incorrect. No updates will be distributed to services until this is resolved.";
625
					} else if (preg_match('/notfqdn /i', $data)) {
626
						$status = "DNS-O-Matic ({$this->_dnsHost}): The hostname specified is not a fully-qualified domain name. If no hostnames included, notfqdn will be returned once.";
627
					} else if (preg_match('/nohost/i', $data)) {
628
						$status = "DNS-O-Matic ({$this->_dnsHost}): The hostname passed could not be matched to any services configured. The service field will be blank in the return code.";
629
					} else if (preg_match('/numhost/i', $data)) {
630
						$status = "DNS-O-Matic ({$this->_dnsHost}): You may update up to 20 hosts. numhost is returned if you try to update more than 20 or update a round-robin.";	
631
					} else if (preg_match('/abuse/i', $data)) {
632
						$status = "DNS-O-Matic ({$this->_dnsHost}): The hostname is blocked for update abuse.";
633
					} else if (preg_match('/good/i', $data)) {
634
						$status = "DNS-O-Matic ({$this->_dnsHost}): (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
635
						$successful_update = true;
636
					} else if (preg_match('/dnserr/i', $data)) {
637
						$status = "DNS-O-Matic ({$this->_dnsHost}): DNS error encountered. Stop updating for 30 minutes.";
638
					} else {
639
						$status = "DNS-O-Matic ({$this->_dnsHost}): (Unknown Response)";
640
						log_error("DNS-O-Matic ({$this->_dnsHost}): PAYLOAD: {$data}");
641
						$this->_debug($data);
642
					}
643
					break;
644
				case 'dyndns':
645
					if (preg_match('/notfqdn/i', $data)) {
646
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Not A FQDN!";
647
					} else if (preg_match('/nochg/i', $data)) {
648
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) No Change In IP Address";
649
						$successful_update = true;
650
					} else if (preg_match('/good/i', $data)) {
651
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
652
						$successful_update = true;
653
					} else if (preg_match('/noauth/i', $data)) {
654
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) User Authorization Failed";
655
					} else {
656
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
657
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
658
						$this->_debug($data);
659
					}
660
					break;
661
				case 'dyndns-static':
662
					if (preg_match('/notfqdn/i', $data)) {
663
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Not A FQDN!";
664
					} else if (preg_match('/nochg/i', $data)) {
665
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) No Change In IP Address";
666
						$successful_update = true;
667
					} else if (preg_match('/good/i', $data)) {
668
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
669
						$successful_update = true;
670
					} else if (preg_match('/noauth/i', $data)) {
671
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) User Authorization Failed";
672
					} else {
673
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
674
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
675
						$this->_debug($data);
676
					}
677
					break;
678
				case 'dyndns-custom':
679
					if (preg_match('/notfqdn/i', $data)) {
680
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Not A FQDN!";
681
					} else if (preg_match('/nochg/i', $data)) {
682
						$status = "phpDynDNS: (Success) No Change In IP Address";
683
						$successful_update = true;
684
					} else if (preg_match('/good/i', $data)) {
685
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
686
						$successful_update = true;
687
					} else if (preg_match('/noauth/i', $data)) {
688
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) User Authorization Failed";
689
					} else {
690
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
691
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
692
						$this->_debug($data);
693
					}
694
					break;
695
				case 'dhs':
696
					break;
697
				case 'noip':
698
				case 'noip-free':
699
					list($ip,$code) = explode(":",$data);
700
					switch ($code) {
701
						case 0:
702
							$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP address is current, no update performed.";
703
							$successful_update = true;
704
							break;
705
						case 1:
706
							$status = "phpDynDNS ({$this->_dnsHost}): (Success) DNS hostname update successful.";
707
							$successful_update = true;
708
							break;
709
						case 2:
710
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Hostname supplied does not exist.";
711
							break;
712
						case 3:
713
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Invalid Username.";
714
							break;
715
						case 4:
716
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Invalid Password.";
717
							break;
718
						case 5:
719
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) To many updates sent.";
720
							break;
721
						case 6:
722
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Account disabled due to violation of No-IP terms of service.";
723
							break;
724
						case 7:
725
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Invalid IP. IP Address submitted is improperly formatted or is a private IP address or is on a blacklist.";
726
							break;
727
						case 8:
728
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Disabled / Locked Hostname.";
729
							break;
730
						case 9:
731
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Host updated is configured as a web redirect and no update was performed.";
732
							break;
733
						case 10:
734
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Group supplied does not exist.";
735
							break;
736
						case 11:
737
							$status = "phpDynDNS ({$this->_dnsHost}): (Success) DNS group update is successful.";
738
							$successful_update = true;
739
							break;
740
						case 12:
741
							$status = "phpDynDNS ({$this->_dnsHost}): (Success) DNS group is current, no update performed.";
742
							$successful_update = true;
743
							break;
744
						case 13:
745
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Update client support not available for supplied hostname or group.";
746
							break;
747
						case 14:
748
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Hostname supplied does not have offline settings configured.";
749
							break;
750
						case 99:
751
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
752
							break;
753
						case 100:
754
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
755
							break;
756
						default:
757
							$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
758
							$this->_debug("Unknown Response: ".$data);
759
							break;
760
					}
761
					break;
762
				case 'easydns':
763
					if (preg_match('/NOACCESS/i', $data)) {
764
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Authentication Failed: Username and/or Password was Incorrect.";
765
					} else if (preg_match('/NOSERVICE/i', $data)) {
766
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) No Service: Dynamic DNS Service has been disabled for this domain.";
767
					} else if (preg_match('/ILLEGAL INPUT/i', $data)) {
768
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Illegal Input: Self-Explanatory";
769
					} else if (preg_match('/TOOSOON/i', $data)) {
770
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Too Soon: Not Enough Time Has Elapsed Since Last Update";
771
					} else if (preg_match('/NOERROR/i', $data)) {
772
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Updated Successfully!";
773
						$successful_update = true;
774
					} else {
775
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
776
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
777
						$this->_debug($data);
778
					}
779
					break;
780
				case 'hn':
781
					/* FIXME: add checks */
782
					break;
783
				case 'zoneedit':
784
					if (preg_match('/799/i', $data)) {
785
						$status = "phpDynDNS ({$this->_dnsHost}): (Error 799) Update Failed!";				
786
					} else if (preg_match('/700/i', $data)) {
787
						$status = "phpDynDNS ({$this->_dnsHost}): (Error 700) Update Failed!";
788
					} else if (preg_match('/200/i', $data)) {
789
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
790
						$successful_update = true;
791
					} else if (preg_match('/201/i', $data)) {
792
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
793
						$successful_update = true;						
794
					} else {
795
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
796
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
797
						$this->_debug($data);
798
					}
799
					break;
800
				case 'dyns':
801
					if (preg_match("/400/i", $data)) {
802
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Bad Request - The URL was malformed. Required parameters were not provided.";
803
					} else if (preg_match('/402/i', $data)) {
804
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Update Too Soon - You have tried updating to quickly since last change.";
805
					} else if (preg_match('/403/i', $data)) {
806
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Database Error - There was a server-sided database error.";
807
					} else if (preg_match('/405/i', $data)) {
808
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Hostname Error - The hostname (".$this->_dnsHost.") doesn't belong to you.";
809
					} else if (preg_match('/200/i', $data)) {
810
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
811
						$successful_update = true;
812
					} else {
813
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
814
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
815
						$this->_debug($data);
816
					}
817
					break;
818
				case 'ods':
819
					if (preg_match("/299/i", $data)) {
820
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
821
						$successful_update = true;
822
					} else {
823
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
824
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
825
						$this->_debug($data);
826
					}
827
					break;
828
				case 'freedns':
829
					if (preg_match("/has not changed./i", $data)) {
830
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) No Change In IP Address";
831
						$successful_update = true;
832
					} else if (preg_match("/Updated/i", $data)) {
833
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
834
						$successful_update = true;
835
					} else {
836
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
837
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
838
						$this->_debug($data);
839
					} 
840
					break;
841
				case 'dnsexit':
842
					if (preg_match("/is the same/i", $data)) {
843
						$status = "phpDynDns ({$this->_dnsHost}): (Success) No Change In IP Address";
844
						$successful_update = true;
845
					} else if (preg_match("/Success/i", $data)) {
846
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
847
						$successful_update = true;
848
					} else {
849
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
850
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
851
						$this->_debug($data);
852
					}
853
					break;
854
				case 'loopia':
855
					if (preg_match("/nochg/i", $data)) {
856
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) No Change In IP Address";
857
						$successful_update = true;
858
					} else if (preg_match("/good/i", $data)) {
859
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
860
						$successful_update = true;
861
					} else if (preg_match('/badauth/i', $data)) {
862
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) User Authorization Failed";
863
					} else {
864
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
865
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
866
						$this->_debug($data);
867
					}
868
					break;
869
				case 'opendns':
870
					if (preg_match('/badauth/i', $data)) {
871
						$status = "phpDynDNS({$this->_dnsHost}): (Error) Not a valid username or password!";
872
					} else if (preg_match('/nohost/i', $data)) {
873
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Hostname you are trying to update does not exist.";
874
						$successful_update = true;
875
					} else if (preg_match('/good/i', $data)) {
876
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
877
						$successful_update = true;
878
					} else if (preg_match('/yours/i', $data)) {
879
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) hostname specified exists, but not under the username specified.";
880
					} else if (preg_match('/abuse/i', $data)) {
881
						$status = "phpDynDns ({$this->_dnsHost}): (Error) Updating too frequently, considered abuse.";
882
					} else {
883
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
884
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
885
						$this->_debug($data);
886
					}
887
					break;
888
				case 'staticcling':
889
					if (preg_match("/invalid ip/i", $data)) {
890
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Bad Request - The IP provided was invalid.";
891
					} else if (preg_match('/required info missing/i', $data)) {
892
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Bad Request - Required parameters were not provided.";
893
					} else if (preg_match('/invalid characters/i', $data)) {
894
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Bad Request - Illegal characters in either the username or the password.";
895
					} else if (preg_match('/bad password/i', $data)) {
896
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Invalid password.";
897
					} else if (preg_match('/account locked/i', $data)) {
898
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) This account has been administratively locked.";
899
					} else if (preg_match('/update too frequent/i', $data)) {
900
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Updating too frequently.";
901
					} else if (preg_match('/DB error/i', $data)) {
902
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Server side error.";
903
					} else if (preg_match('/success/i', $data)) {
904
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
905
						$successful_update = true;
906
					} else {
907
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
908
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
909
						$this->_debug($data);
910
					}
911
					break;
912
				case 'namecheap':
913
					$tmp = str_replace("^M", "", $data);
914
					$ncresponse = @xml2array($tmp);
915
					if (preg_match("/internal server error/i", $data)) {
916
						$status = "phpDynDNS: (Error) Server side error.";
917
					} else if (preg_match("/request is badly formed/i", $data)) {
918
						$status = "phpDynDNS: (Error) Badly Formed Request (check your settings).";
919
					} else if ($ncresponse['interface-response']['ErrCount'] === "0") {
920
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
921
						$successful_update = true;
922
					} else if (is_numeric($ncresponse['interface-response']['ErrCount']) && ($ncresponse['interface-response']['ErrCount'] > 0)) {
923
						$status = "phpDynDNS: (Error) " . implode(", ", $ncresponse["interface-response"]["errors"]);
924
						$successful_update = true;
925
					} else {
926
						$status = "phpDynDNS: (Unknown Response)";
927
						log_error("phpDynDNS: PAYLOAD: {$data}");
928
						$this->_debug($data);
929
					}
930
					break;
931
					
932
				case 'he-net':
933
				case 'he-net-v6':
934
					if (preg_match("/badip/i", $data)) {
935
						$status = "phpDynDNS: (Error) Bad Request - The IP provided was invalid.";
936
					} else if (preg_match('/nohost/i', $data)) {
937
						$status = "phpDynDNS: (Error) Bad Request - A hostname was not provided.";
938
					} else if (preg_match('/badauth/i', $data)) {
939
						$status = "phpDynDNS: (Error) Invalid username or password.";
940
					} else if (preg_match('/good/i', $data)) {
941
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
942
						$successful_update = true;
943
					} else if (preg_match('/nochg/i', $data)) {
944
						$status = "phpDynDNS: (Success) No Change In IP Address.";
945
						$successful_update = true;
946
					} else {
947
						$status = "phpDynDNS: (Unknown Response)";
948
						log_error("phpDynDNS: PAYLOAD: {$data}");
949
						$this->_debug($data);
950
					}
951
					break;
952
				case 'he-net-tunnelbroker':
953
					/*
954
					-ERROR: Missing parameter(s).
955
					-ERROR: Invalid API key or password
956
					-ERROR: Tunnel not found
957
					-ERROR: Another tunnel exists for this IP.
958
					-ERROR: This tunnel is already associated with this IP address
959
					+OK: Tunnel endpoint updated to: x.x.x.x
960
					*/
961
					if (preg_match("/Missing parameter/i", $data)) {
962
						$status = "phpDynDNS: (Error) Bad Request - Missing/Invalid Parameters.";
963
					} else if (preg_match('/Tunnel not found/i', $data)) {
964
						$status = "phpDynDNS: (Error) Bad Request - Invalid Tunnel ID.";
965
					} else if (preg_match('/Invalid API key or password/i', $data)) {
966
						$status = "phpDynDNS: (Error) Invalid username or password.";
967
					} else if (preg_match('/OK:/i', $data)) {
968
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
969
						$successful_update = true;
970
					} else if (preg_match('/This tunnel is already associated with this IP address/i', $data)) {
971
						$status = "phpDynDNS: (Success) No Change In IP Address.";
972
						$successful_update = true;
973
					} else {
974
						$status = "phpDynDNS: (Unknown Response)";
975
						log_error("phpDynDNS: PAYLOAD: {$data}");
976
						$this->_debug($data);
977
					}
978
					break;
979
				case 'selfhost':
980
					if (preg_match('/notfqdn/i', $data)) {
981
						$status = "phpDynDNS: (Error) Not A FQDN!";
982
					} else if (preg_match('/nochg/i', $data)) {
983
						$status = "phpDynDNS: (Success) No Change In IP Address";
984
						$successful_update = true;
985
					} else if (preg_match('/good/i', $data)) {
986
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
987
						$successful_update = true;
988
					} else if (preg_match('/noauth/i', $data)) {
989
						$status = "phpDynDNS: (Error) User Authorization Failed";
990
					} else {
991
						$status = "phpDynDNS: (Unknown Response)";
992
						log_error("phpDynDNS: PAYLOAD: {$data}");
993
						$this->_debug($data);
994
					}
995
					break;
996
				case 'route53':
997
					$successful_update = true;
998
					break;
999
				case 'custom':
1000
				case 'custom-v6':
1001
					$successful_update = false;
1002
					if ($this->_dnsResultMatch == "") {
1003
						$successful_update = true;
1004
					}else {
1005
						$this->_dnsResultMatch = str_replace("%IP%", $this->_dnsIP, $this->_dnsResultMatch);
1006
						$matches = preg_split("/(?<!\\\\)\\|/", $this->_dnsResultMatch);
1007
						foreach($matches as $match) {
1008
							$match= str_replace("\\|", "|", $match);
1009
							if(strcmp($match, trim($data, "\t\n\r")) == 0)
1010
								$successful_update = true;
1011
						}
1012
						unset ($matches);
1013
					}
1014
					if ($successful_update == true)
1015
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
1016
					else
1017
						$status = "phpDynDNS: (Error) Result did not match.";
1018
					break;
1019
			}
1020
			
1021
			if($successful_update == true) {
1022
				/* Write WAN IP to cache file */
1023
				$wan_ip = $this->_checkIP();
1024
				conf_mount_rw();
1025
				if ($this->_useIPv6 == false && $wan_ip > 0) {
1026
					$currentTime = time();
1027
					notify_all_remote(sprintf(gettext("DynDNS updated IP Address on %s (%s) to %s"), convert_real_interface_to_friendly_descr($this->_if), $this->_if, $wan_ip));
1028
					log_error("phpDynDNS: updating cache file {$this->_cacheFile}: {$wan_ip}");
1029
					@file_put_contents($this->_cacheFile, "{$wan_ip}:{$currentTime}");
1030
				} else
1031
					@unlink($this->_cacheFile);
1032
				if ($this->_useIPv6 == true && $wan_ip > 0) {
1033
					$currentTime = time();
1034
					notify_all_remote(sprintf(gettext("DynDNS updated IPv6 Address on %s (%s) to %s"), convert_real_interface_to_friendly_descr($this->_if), $this->_if, $wan_ip));
1035
					log_error("phpDynDNS: updating cache file {$this->_cacheFile_v6}: {$wan_ip}");
1036
					@file_put_contents($this->_cacheFile_v6, "{$wan_ip}|{$currentTime}");
1037
				} else
1038
					@unlink($this->_cacheFile_v6);
1039
				conf_mount_ro();
1040
			}
1041
			$this->status = $status;
1042
			log_error($status);
1043
		}
1044

    
1045
		/*
1046
		 * Private Function (added 12 July 05) [beta]
1047
		 *   Return Error, Set Last Error, and Die.
1048
		 */
1049
		function _error($errorNumber = '1') {
1050
			switch ($errorNumber) {
1051
				case 0:
1052
					break;
1053
				case 2:
1054
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
1055
					break;
1056
				case 3:
1057
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
1058
					break;
1059
				case 4:
1060
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
1061
					break;
1062
				case 5:
1063
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
1064
					break;
1065
				case 6:
1066
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
1067
					break;
1068
				case 7:
1069
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
1070
					break;
1071
				case 8:
1072
					$status = "Route 53: (Error) Invalid ZoneID";
1073
					break;
1074
				case 9:
1075
					$status = "Route 53: (Error) Invalid TTL";
1076
					break;  
1077
				case 10:
1078
					$error = "phpDynDNS ({$this->_dnsHost}): No change in my IP address and/or " . $this->_dnsMaxCacheAgeDays . " days has not passed. Not updating dynamic DNS entry.";
1079
					break;
1080
				default:
1081
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
1082
					/* FIXME: $data isn't in scope here */
1083
					/* $this->_debug($data); */
1084
					break;
1085
			}
1086
			$this->lastError = $error;
1087
			log_error($error);
1088
		}
1089

    
1090
		/*
1091
		 * Private Function (added 12 July 05) [beta]
1092
		 *   - Detect whether or not IP needs to be updated.
1093
		 *      | Written Specifically for pfSense (pfsense.com) may
1094
		 *      | work with other systems. pfSense base is FreeBSD.
1095
		 */
1096
		function _detectChange() {
1097
			global $debug;
1098

    
1099
			if ($debug)
1100
				log_error("DynDns ({$this->_dnsHost}): _detectChange() starting.");
1101
		
1102
			$currentTime = time();
1103

    
1104
			$wan_ip = $this->_checkIP();
1105
			if ($wan_ip == 0) {
1106
				log_error("DynDns ({$this->_dnsHost}): Current WAN IP could not be determined, skipping update process.");
1107
				return false;
1108
			}
1109
			$log_error = "DynDns ({$this->_dnsHost}): Current WAN IP: {$wan_ip} ";
1110

    
1111
			if ($this->_useIPv6 == true) {
1112
				if (file_exists($this->_cacheFile_v6)) {
1113
					$contents = file_get_contents($this->_cacheFile_v6);
1114
					list($cacheIP,$cacheTime) = explode('|', $contents);
1115
					$this->_debug($cacheIP.'/'.$cacheTime);
1116
					$initial = false;
1117
					$log_error .= "Cached IPv6: {$cacheIP} ";
1118
				} else {
1119
					conf_mount_rw();
1120
					$cacheIP = '::';
1121
					@file_put_contents($this->_cacheFile, "::|{$currentTime}");
1122
					conf_mount_ro();
1123
					$cacheTime = $currentTime;
1124
					$initial = true;
1125
					$log_error .= "No Cached IPv6 found.";
1126
				}
1127
			} else {
1128
				if (file_exists($this->_cacheFile)) {
1129
					$contents = file_get_contents($this->_cacheFile);
1130
					list($cacheIP,$cacheTime) = explode(':', $contents);
1131
					$this->_debug($cacheIP.'/'.$cacheTime);
1132
					$initial = false;
1133
					$log_error .= "Cached IP: {$cacheIP} ";
1134
				} else {
1135
					conf_mount_rw();
1136
					$cacheIP = '0.0.0.0';
1137
					@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
1138
					conf_mount_ro();
1139
					$cacheTime = $currentTime;
1140
					$initial = true;
1141
					$log_error .= "No Cached IP found.";
1142
				}
1143
			}
1144
			if ($this->_dnsVerboseLog)
1145
				log_error($log_error);
1146

    
1147
			// Convert seconds = days * hr/day * min/hr * sec/min
1148
			$maxCacheAgeSecs = $this->_dnsMaxCacheAgeDays * 24 * 60 * 60;
1149

    
1150
			$needs_updating = FALSE;
1151
			/* lets determine if the item needs updating */
1152
			if ($cacheIP != $wan_ip) {
1153
				$needs_updating = true;
1154
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
1155
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
1156
			}
1157
			if (($currentTime - $cacheTime) > $maxCacheAgeSecs) {
1158
				$needs_updating = true;
1159
				$this->_forceUpdateNeeded = true;
1160
				$update_reason = "DynDns: More than " . $this->_dnsMaxCacheAgeDays . " days.  Updating. ";
1161
				$update_reason .= "{$currentTime} - {$cacheTime} > {$maxCacheAgeSecs} ";
1162
			}
1163
			if ($initial == true) {
1164
				$needs_updating = true;
1165
				$update_reason .= "Initial update. ";
1166
			}
1167

    
1168
			/*   finally if we need updating then store the
1169
			 *   new cache value and return true
1170
			 */
1171
			if ($needs_updating == true) {
1172
				if ($this->_dnsVerboseLog)
1173
					log_error("DynDns ({$this->_dnsHost}): {$update_reason}");
1174
				return true;
1175
			}
1176

    
1177
			return false;			
1178
		}
1179

    
1180
		/*
1181
		 * Private Function (added 16 July 05) [beta]
1182
		 *   - Writes debug information to a file.
1183
		 *   - This function is only called when a unknown response
1184
		 *   - status is returned from a DynDNS service provider.
1185
		 */
1186
		function _debug($data) {
1187
			global $g;
1188

    
1189
			if (!$g['debug'])
1190
				return;
1191
			$string = date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data."\n";
1192
			conf_mount_rw();
1193
			$file = fopen($this->_debugFile, 'a');
1194
			fwrite($file, $string);
1195
			fclose($file);
1196
			conf_mount_ro();
1197
		}
1198
		function _checkIP() {
1199
			global $debug;
1200

    
1201
			if ($debug)
1202
				log_error("DynDns ({$this->_dnsHost}): _checkIP() starting.");
1203

    
1204
			if ($this->_useIPv6 == true) {
1205
				$ip_address = find_interface_ipv6($this->_if);
1206
				if (!is_ipaddrv6($ip_address))
1207
					return 0;
1208
			} else {
1209
				$ip_address = find_interface_ip($this->_if);
1210
				if (!is_ipaddr($ip_address))
1211
					return 0;
1212
			}
1213
			if ($this->_useIPv6 == false && is_private_ip($ip_address)) {
1214
				$hosttocheck = "checkip.dyndns.org";
1215
				$try = 0;
1216
				while ($try < 3) {
1217
					$checkip = gethostbyname($hosttocheck);
1218
					if (is_ipaddr($checkip))
1219
						break;
1220
					$try++;
1221
				}
1222
				if ($try >= 3) {
1223
					log_error("Dyndns debug information ({$this->_dnsHost}): Could not resolve {$hosttocheck} to ip using interface ip {$ip_address}.");
1224
					return 0;
1225
				}
1226
				$ip_ch = curl_init("http://{$checkip}");
1227
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
1228
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
1229
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
1230
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
1231
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
1232
				$ip_result_page = curl_exec($ip_ch);
1233
				curl_close($ip_ch);
1234
				$ip_result_decoded = urldecode($ip_result_page);
1235
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
1236
				$ip_address = trim($matches[1]);
1237
				if (is_ipaddr($ip_address)) {
1238
					if ($this->_dnsVerboseLog)
1239
						log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from {$hosttocheck}");
1240
				} else {
1241
					log_error("DynDns ({$this->_dnsHost}): IP address could not be extracted from {$hosttocheck}");
1242
					return 0;
1243
				}
1244
			} else {
1245
				if ($this->_dnsVerboseLog)
1246
					log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from local system.");
1247
			}
1248
			$this->_dnsIP = $ip_address;
1249

    
1250
			return $ip_address;
1251
		}
1252

    
1253
	}
1254

    
1255
?>
(17-17/66)