Project

General

Profile

Download (53.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
	 *    - CloudFlare (www.cloudflare.com)
30
 	 *    - Eurodns (eurodns.com)
31
	 * +----------------------------------------------------+
32
	 *  Requirements:
33
	 *    - PHP version 4.0.2 or higher with the CURL Library and the PCRE Library
34
	 * +----------------------------------------------------+
35
	 *  Public Functions
36
	 *    - updatedns()
37
	 *
38
	 *  Private Functions
39
	 *    - _update()
40
	 *    - _checkStatus()
41
	 *    - _error()
42
	 *    - _detectChange()
43
	 *    - _debug()
44
	 *    - _checkIP()
45
	 * +----------------------------------------------------+
46
	 *  DynDNS Dynamic - Last Tested: 12 July 2005
47
	 *  DynDNS Static  - Last Tested: NEVER
48
	 *  DynDNS Custom  - Last Tested: NEVER
49
	 *  No-IP          - Last Tested: 20 July 2008
50
	 *  HN.org         - Last Tested: 12 July 2005
51
	 *  EasyDNS        - Last Tested: 20 July 2008
52
	 *  DHS            - Last Tested: 12 July 2005
53
	 *  ZoneEdit       - Last Tested: NEVER
54
	 *  Dyns           - Last Tested: NEVER
55
	 *  ODS            - Last Tested: 02 August 2005
56
	 *  FreeDNS        - Last Tested: 23 Feb 2011
57
	 *  Loopia         - Last Tested: NEVER
58
	 *  StaticCling    - Last Tested: 27 April 2006
59
	 *  DNSexit	   - Last Tested: 20 July 2008
60
	 *  OpenDNS	   - Last Tested: 4 August 2008
61
	 *  Namecheap	   - Last Tested: 31 August 2010
62
	 *  HE.net         - Last Tested: 7 July 2013
63
	 *  HE.net IPv6    - Last Tested: 7 July 2013
64
	 *  HE.net Tunnel  - Last Tested: 28 June 2011
65
	 *  SelfHost       - Last Tested: 26 December 2011
66
	 *  Amazon Route 53 - Last tested: 01 April 2012
67
	 *  DNS-O-Matic	   - Last Tested: 9 September 2010
68
	 *  CloudFlare     - Last Tested: 30 May 2013
69
  	 *  Eurodns	   		- Last Tested : 27 June 2013
70
	 * +====================================================+
71
	 *
72
	 * @author 	E.Kristensen
73
	 * @link    	http://www.idylldesigns.com/projects/phpdns/
74
	 * @version 	0.8
75
	 * @updated	13 October 05 at 21:02:42 GMT
76
	 *
77
	 * DNSexit/OpenDNS support and multiwan extension for pfSense by Ermal Luci
78
	 * Custom DNS support by Matt Corallo
79
	 *
80
	 */
81

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

    
136
			$this->_curlIpresolveV4 = $curlIpresolveV4;
137
			$this->_curlSslVerifypeer = $curlSslVerifypeer;
138
			$this->_dnsVerboseLog = $dnsVerboseLog;
139
			if ($this->_dnsVerboseLog)
140
				log_error("DynDns: updatedns() starting");
141

    
142
			$dyndnslck = lock("DDNS".$dnsID, LOCK_EX);
143

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

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

    
252
			unlock($dyndnslck);
253
		}
254
			
255
		/*
256
		 * Private Function (added 12 July 05) [beta]
257
		 *   Send Update To Selected Service.
258
		 */
259
		function _update() {
260
		
261
			if ($this->_dnsVerboseLog)
262
				log_error("DynDNS ({$this->_dnsHost}): DynDns _update() starting.");
263
		
264
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53 ') {
265
				$ch = curl_init();
266
				curl_setopt($ch, CURLOPT_HEADER, 0);
267
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
268
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
269
				curl_setopt($ch, CURLOPT_INTERFACE, $this->_dnsRequestIfIP);
270
				curl_setopt($ch, CURLOPT_TIMEOUT, 120); // Completely empirical
271
			}
272

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

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

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

    
546
					/* Include Route 53 Library Class */
547
					require_once('/etc/inc/r53.class');
548

    
549
					/* Set Amazon AWS Credentials for this record */
550
					$r53 = new Route53($AccessKeyId, $SecretAccessKey);
551

    
552
					/* Function to find old values of records in Route 53 */
553
					if(!function_exists('Searchrecords')) {
554
						function SearchRecords($records, $name) {
555
							$result = array();
556
							foreach($records as $record) {
557
								if(strtolower($record['Name']) == strtolower($name)) {
558
									$result [] = $record;
559
								}
560
							}
561
							return ($result) ? $result : false;
562
						}
563
					}
564

    
565
					$records = $r53->listResourceRecordSets("/hostedzone/$ZoneID");
566

    
567
					/* Get IP for your hostname in Route 53 */
568
					if(false !== ($a_result = SearchRecords($records['ResourceRecordSets'], "$hostname"))) {
569
						$OldTTL=$a_result[0][TTL];
570
						$OldIP=$a_result[0][ResourceRecords][0];
571
					} else {
572
						$OldIP="";
573
					}
574

    
575
					/* Check if we need to update DNS Record */
576
					if ($OldIP !== $NewIP) {
577
						if(!empty($OldIP)) {
578
							/* Your Hostname already exists, deleting and creating it again */
579
							$changes = array();
580
							$changes[] = $r53->prepareChange(DELETE, $hostname, A, $OldTTL, $OldIP);
581
							$changes[] = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
582
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
583
						} else {
584
							/* Your Hostname does not exist yet, creating it */
585
							$changes = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
586
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
587
						}
588
					}
589
					$this->_checkStatus(0, $result);
590
					break;
591
				case 'custom':
592
				case 'custom-v6':
593
					if ($this->_dnsVerboseLog)
594
						log_error("Custom DDNS ({$this->_dnsHost}): DNS update() starting.");
595
					if (strstr($this->dnsUpdateURL, "%IP%")) {$needsIP = TRUE;} else {$needsIP = FALSE;}
596
					if ($this->_dnsUser != '') {
597
						if ($this->_curlIpresolveV4)
598
							curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
599
						if ($this->_curlSslVerifypeer)
600
							curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
601
						else
602
							curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
603
						curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
604
						curl_setopt($ch, CURLOPT_USERPWD, "{$this->_dnsUser}:{$this->_dnsPass}");
605
					}
606
					$server = str_replace("%IP%", $this->_dnsIP, $this->_dnsUpdateURL);
607
					if ($this->_dnsVerboseLog)
608
						log_error("Sending request to: ".$server);
609
					curl_setopt($ch, CURLOPT_URL, $server);
610
					break;
611
				case 'cloudflare':
612
					$needsIP = TRUE;
613
					$dnsServer ='www.cloudflare.com';
614
					$dnsHost = str_replace(' ','', $this->_dnsHost);
615
					$URL = "https://{$dnsServer}/api.html?a=DIUP&email={$this->_dnsUser}&tkn={$this->_dnsPass}&ip={$this->dnsIP}&hosts={$dnsHost}";
616
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
617
					curl_setopt($ch, CURLOPT_URL, $URL);
618
					break;
619
				case 'eurodns':		
620
					$needsIP = TRUE;
621
					if ($this->_dnsVerboseLog)
622
						log_error("EuroDynDns ({$this->_dnsHost}) DNS update() starting.");
623
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
624
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
625
					$server = "https://eurodyndns.org/update/";
626
					$port = "";
627
					if($this->_dnsPort)
628
						$port = ":" . $this->_dnsPort;
629
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP);
630
					break;	
631
				default:
632
					break;
633
			}
634
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53') {
635
				$data = curl_exec($ch);
636
				$this->_checkStatus($ch, $data);
637
				@curl_close($ch);
638
			}
639
		}
640

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

    
1060
					// loop over the lines
1061
					for ($pos=0; ($successful_update || $pos == 0) && $pos < $lines; $pos++){
1062
						$resp = $data[$pos];
1063
						if (preg_match('/UAUTH/i', $resp)) {
1064
							$status = "DynDNS: The username specified is not authorized to update this hostname and domain.";
1065
						} else if (preg_match('/NOHOST/i', $resp)) {
1066
							$status = "DynDNS: No valid FQDN (fully qualified domain name) was specified";
1067
						} else if (preg_match('/INVLDHST/i', $resp)) {
1068
							$status = "DynDNS: An invalid hostname was specified. This may be due to the fact the hostname has not been created in the system. Creating new host names via clients is not supported.";
1069
						} else if (preg_match('/INVLDIP/i', $resp)) {
1070
							$status = "DynDNS: The IP address given is not valid.";
1071
						} else if (preg_match('/DUPHST/i', $resp)) {
1072
							$status = "DynDNS: Duplicate values exist for a record. Only single values for records are supported currently.";
1073
						} else if (preg_match('/NOUPDATE/i', $resp)) {
1074
							$status = "DynDNS: No changes made to the hostname (".strtok($resp,' ')."). Continual updates with no changes lead to blocked clients.";
1075
							$successful_update = true; //success if it is the same so that it saves
1076
						} else if (preg_match('/OK/i', $resp)) {
1077
							$status = "DynDNS: (Success) (".strtok($resp,' ').") IP Address for Changed Successfully!";
1078
							$successful_update = true;
1079
						} else {
1080
							$status = "DynDNS: (Unknown Response)";
1081
							log_error("DynDNS: PAYLOAD: {$resp}");
1082
							$this->_debug($resp);
1083
						}
1084
						log_error($status);
1085
					}
1086
					break;
1087
				case 'eurodns':
1088
					if (preg_match('/notfqdn/i', $data)) {
1089
						$status = "phpDynDNS: (Error) Not A FQDN!";
1090
					} else if (preg_match('/nochg/i', $data)) {
1091
						$status = "phpDynDNS: (Success) No Change In IP Address";
1092
						$successful_update = true;
1093
					} else if (preg_match('/good/i', $data)) {
1094
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
1095
						$successful_update = true;
1096
					} else if (preg_match('/badauth/i', $data)) {
1097
						$status = "phpDynDNS: (Error) User Authorization Failed";
1098
					} else {
1099
						$status = "phpDynDNS: (Unknown Response)";
1100
						log_error("phpDynDNS: PAYLOAD: {$data}");
1101
						$this->_debug($data);
1102
					}
1103
					break;
1104
			}
1105
			
1106
			if($successful_update == true) {
1107
				/* Write WAN IP to cache file */
1108
				$wan_ip = $this->_checkIP();
1109
				conf_mount_rw();
1110
				if ($this->_useIPv6 == false && $wan_ip > 0) {
1111
					$currentTime = time();
1112
					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));
1113
					log_error("phpDynDNS: updating cache file {$this->_cacheFile}: {$wan_ip}");
1114
					@file_put_contents($this->_cacheFile, "{$wan_ip}:{$currentTime}");
1115
				} else
1116
					@unlink($this->_cacheFile);
1117
				if ($this->_useIPv6 == true && $wan_ip > 0) {
1118
					$currentTime = time();
1119
					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));
1120
					log_error("phpDynDNS: updating cache file {$this->_cacheFile_v6}: {$wan_ip}");
1121
					@file_put_contents($this->_cacheFile_v6, "{$wan_ip}|{$currentTime}");
1122
				} else
1123
					@unlink($this->_cacheFile_v6);
1124
				conf_mount_ro();
1125
			}
1126
			$this->status = $status;
1127
			log_error($status);
1128
		}
1129

    
1130
		/*
1131
		 * Private Function (added 12 July 05) [beta]
1132
		 *   Return Error, Set Last Error, and Die.
1133
		 */
1134
		function _error($errorNumber = '1') {
1135
			switch ($errorNumber) {
1136
				case 0:
1137
					break;
1138
				case 2:
1139
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
1140
					break;
1141
				case 3:
1142
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
1143
					break;
1144
				case 4:
1145
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
1146
					break;
1147
				case 5:
1148
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
1149
					break;
1150
				case 6:
1151
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
1152
					break;
1153
				case 7:
1154
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
1155
					break;
1156
				case 8:
1157
					$status = "Route 53: (Error) Invalid ZoneID";
1158
					break;
1159
				case 9:
1160
					$status = "Route 53: (Error) Invalid TTL";
1161
					break;  
1162
				case 10:
1163
					$error = "phpDynDNS ({$this->_dnsHost}): No change in my IP address and/or " . $this->_dnsMaxCacheAgeDays . " days has not passed. Not updating dynamic DNS entry.";
1164
					break;
1165
				default:
1166
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
1167
					/* FIXME: $data isn't in scope here */
1168
					/* $this->_debug($data); */
1169
					break;
1170
			}
1171
			$this->lastError = $error;
1172
			log_error($error);
1173
		}
1174

    
1175
		/*
1176
		 * Private Function (added 12 July 05) [beta]
1177
		 *   - Detect whether or not IP needs to be updated.
1178
		 *      | Written Specifically for pfSense (pfsense.com) may
1179
		 *      | work with other systems. pfSense base is FreeBSD.
1180
		 */
1181
		function _detectChange() {
1182
			global $debug;
1183

    
1184
			if ($debug)
1185
				log_error("DynDns ({$this->_dnsHost}): _detectChange() starting.");
1186
		
1187
			$currentTime = time();
1188

    
1189
			$wan_ip = $this->_checkIP();
1190
			if ($wan_ip == 0) {
1191
				log_error("DynDns ({$this->_dnsHost}): Current WAN IP could not be determined, skipping update process.");
1192
				return false;
1193
			}
1194
			$log_error = "DynDns ({$this->_dnsHost}): Current WAN IP: {$wan_ip} ";
1195

    
1196
			if ($this->_useIPv6 == true) {
1197
				if (file_exists($this->_cacheFile_v6)) {
1198
					$contents = file_get_contents($this->_cacheFile_v6);
1199
					list($cacheIP,$cacheTime) = explode('|', $contents);
1200
					$this->_debug($cacheIP.'/'.$cacheTime);
1201
					$initial = false;
1202
					$log_error .= "Cached IPv6: {$cacheIP} ";
1203
				} else {
1204
					conf_mount_rw();
1205
					$cacheIP = '::';
1206
					@file_put_contents($this->_cacheFile, "::|{$currentTime}");
1207
					conf_mount_ro();
1208
					$cacheTime = $currentTime;
1209
					$initial = true;
1210
					$log_error .= "No Cached IPv6 found.";
1211
				}
1212
			} else {
1213
				if (file_exists($this->_cacheFile)) {
1214
					$contents = file_get_contents($this->_cacheFile);
1215
					list($cacheIP,$cacheTime) = explode(':', $contents);
1216
					$this->_debug($cacheIP.'/'.$cacheTime);
1217
					$initial = false;
1218
					$log_error .= "Cached IP: {$cacheIP} ";
1219
				} else {
1220
					conf_mount_rw();
1221
					$cacheIP = '0.0.0.0';
1222
					@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
1223
					conf_mount_ro();
1224
					$cacheTime = $currentTime;
1225
					$initial = true;
1226
					$log_error .= "No Cached IP found.";
1227
				}
1228
			}
1229
			if ($this->_dnsVerboseLog)
1230
				log_error($log_error);
1231

    
1232
			// Convert seconds = days * hr/day * min/hr * sec/min
1233
			$maxCacheAgeSecs = $this->_dnsMaxCacheAgeDays * 24 * 60 * 60;
1234

    
1235
			$needs_updating = FALSE;
1236
			/* lets determine if the item needs updating */
1237
			if ($cacheIP != $wan_ip) {
1238
				$needs_updating = true;
1239
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
1240
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
1241
			}
1242
			if (($currentTime - $cacheTime) > $maxCacheAgeSecs) {
1243
				$needs_updating = true;
1244
				$this->_forceUpdateNeeded = true;
1245
				$update_reason = "DynDns: More than " . $this->_dnsMaxCacheAgeDays . " days.  Updating. ";
1246
				$update_reason .= "{$currentTime} - {$cacheTime} > {$maxCacheAgeSecs} ";
1247
			}
1248
			if ($initial == true) {
1249
				$needs_updating = true;
1250
				$update_reason .= "Initial update. ";
1251
			}
1252

    
1253
			/*   finally if we need updating then store the
1254
			 *   new cache value and return true
1255
			 */
1256
			if ($needs_updating == true) {
1257
				if ($this->_dnsVerboseLog)
1258
					log_error("DynDns ({$this->_dnsHost}): {$update_reason}");
1259
				return true;
1260
			}
1261

    
1262
			return false;			
1263
		}
1264

    
1265
		/*
1266
		 * Private Function (added 16 July 05) [beta]
1267
		 *   - Writes debug information to a file.
1268
		 *   - This function is only called when a unknown response
1269
		 *   - status is returned from a DynDNS service provider.
1270
		 */
1271
		function _debug($data) {
1272
			global $g;
1273

    
1274
			if (!$g['debug'])
1275
				return;
1276
			$string = date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data."\n";
1277
			conf_mount_rw();
1278
			$file = fopen($this->_debugFile, 'a');
1279
			fwrite($file, $string);
1280
			fclose($file);
1281
			conf_mount_ro();
1282
		}
1283
		function _checkIP() {
1284
			global $debug;
1285

    
1286
			if ($debug)
1287
				log_error("DynDns ({$this->_dnsHost}): _checkIP() starting.");
1288

    
1289
			if ($this->_useIPv6 == true) {
1290
				$ip_address = find_interface_ipv6($this->_if);
1291
				if (!is_ipaddrv6($ip_address))
1292
					return 0;
1293
			} else {
1294
				$ip_address = find_interface_ip($this->_if);
1295
				if (!is_ipaddr($ip_address))
1296
					return 0;
1297
			}
1298
			if ($this->_useIPv6 == false && is_private_ip($ip_address)) {
1299
				$hosttocheck = "checkip.dyndns.org";
1300
				$try = 0;
1301
				while ($try < 3) {
1302
					$checkip = gethostbyname($hosttocheck);
1303
					if (is_ipaddr($checkip))
1304
						break;
1305
					$try++;
1306
				}
1307
				if ($try >= 3) {
1308
					log_error("Dyndns debug information ({$this->_dnsHost}): Could not resolve {$hosttocheck} to ip using interface ip {$ip_address}.");
1309
					return 0;
1310
				}
1311
				$ip_ch = curl_init("http://{$checkip}");
1312
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
1313
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
1314
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
1315
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
1316
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
1317
				$ip_result_page = curl_exec($ip_ch);
1318
				curl_close($ip_ch);
1319
				$ip_result_decoded = urldecode($ip_result_page);
1320
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
1321
				$ip_address = trim($matches[1]);
1322
				if (is_ipaddr($ip_address)) {
1323
					if ($this->_dnsVerboseLog)
1324
						log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from {$hosttocheck}");
1325
				} else {
1326
					log_error("DynDns ({$this->_dnsHost}): IP address could not be extracted from {$hosttocheck}");
1327
					return 0;
1328
				}
1329
			} else {
1330
				if ($this->_dnsVerboseLog)
1331
					log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from local system.");
1332
			}
1333
			$this->_dnsIP = $ip_address;
1334

    
1335
			return $ip_address;
1336
		}
1337

    
1338
	}
1339

    
1340
?>
(17-17/66)