Project

General

Profile

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

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

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

    
144
			$dyndnslck = lock("DDNS".$dnsID, LOCK_EX);
145

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

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

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

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

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

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

    
549
					/* Include Route 53 Library Class */
550
					require_once('/etc/inc/r53.class');
551

    
552
					/* Set Amazon AWS Credentials for this record */
553
					$r53 = new Route53($AccessKeyId, $SecretAccessKey);
554

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

    
568
					$records = $r53->listResourceRecordSets("/hostedzone/$ZoneID");
569

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

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

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

    
1072
					// loop over the lines
1073
					for ($pos=0; ($successful_update || $pos == 0) && $pos < $lines; $pos++){
1074
						$resp = $data[$pos];
1075
						if (preg_match('/UAUTH/i', $resp)) {
1076
							$status = "DynDNS: The username specified is not authorized to update this hostname and domain.";
1077
						} else if (preg_match('/NOHOST/i', $resp)) {
1078
							$status = "DynDNS: No valid FQDN (fully qualified domain name) was specified";
1079
						} else if (preg_match('/INVLDHST/i', $resp)) {
1080
							$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.";
1081
						} else if (preg_match('/INVLDIP/i', $resp)) {
1082
							$status = "DynDNS: The IP address given is not valid.";
1083
						} else if (preg_match('/DUPHST/i', $resp)) {
1084
							$status = "DynDNS: Duplicate values exist for a record. Only single values for records are supported currently.";
1085
						} else if (preg_match('/NOUPDATE/i', $resp)) {
1086
							$status = "DynDNS: No changes made to the hostname (".strtok($resp,' ')."). Continual updates with no changes lead to blocked clients.";
1087
							$successful_update = true; //success if it is the same so that it saves
1088
						} else if (preg_match('/OK/i', $resp)) {
1089
							$status = "DynDNS: (Success) (".strtok($resp,' ').") IP Address for Changed Successfully!";
1090
							$successful_update = true;
1091
						} else {
1092
							$status = "DynDNS: (Unknown Response)";
1093
							log_error("DynDNS: PAYLOAD: {$resp}");
1094
							$this->_debug($resp);
1095
						}
1096
						log_error($status);
1097
					}
1098
					break;
1099
				case 'eurodns':
1100
					if (preg_match('/notfqdn/i', $data)) {
1101
						$status = "phpDynDNS: (Error) Not A FQDN!";
1102
					} else if (preg_match('/nochg/i', $data)) {
1103
						$status = "phpDynDNS: (Success) No Change In IP Address";
1104
						$successful_update = true;
1105
					} else if (preg_match('/good/i', $data)) {
1106
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
1107
						$successful_update = true;
1108
					} else if (preg_match('/badauth/i', $data)) {
1109
						$status = "phpDynDNS: (Error) User Authorization Failed";
1110
					} else {
1111
						$status = "phpDynDNS: (Unknown Response)";
1112
						log_error("phpDynDNS: PAYLOAD: {$data}");
1113
						$this->_debug($data);
1114
					}
1115
					break;
1116
				case 'gratisdns':
1117
					if (preg_match('/Forkerte værdier/i', $data)) {
1118
					        $status = "phpDynDNS: (Error) Wrong values - Update could not be completed.";
1119
					} else if (preg_match('/Bruger login: Bruger eksistere ikke/i', $data)) {
1120
					        $status = "phpDynDNS: (Error) Unknown username - User does not exist.";
1121
					} else if (preg_match('/Bruger login: 1Fejl i kodeord/i', $data)) {
1122
					        $status = "phpDynDNS: (Error) Wrong password - Remember password is case sensitive.";
1123
					} else if (preg_match('/Domæne kan IKKE administreres af bruger/i', $data)) {
1124
					        $status = "phpDynDNS: (Error) User unable to administer the selected domain.";
1125
					} else if (preg_match('/OK/i', $data)) {
1126
					        $status = "phpDynDNS: (Success) IP Address Updated Successfully!";
1127
					        $successful_update = true;
1128
					} else {
1129
					        $status = "phpDynDNS: (Unknown Response)";
1130
					        log_error("phpDynDNS: PAYLOAD: {$data}");
1131
					        $this->_debug($data);
1132
					}
1133
					break;
1134
			}
1135
			
1136
			if($successful_update == true) {
1137
				/* Write WAN IP to cache file */
1138
				$wan_ip = $this->_checkIP();
1139
				conf_mount_rw();
1140
				if ($this->_useIPv6 == false && $wan_ip > 0) {
1141
					$currentTime = time();
1142
					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));
1143
					log_error("phpDynDNS: updating cache file {$this->_cacheFile}: {$wan_ip}");
1144
					@file_put_contents($this->_cacheFile, "{$wan_ip}:{$currentTime}");
1145
				} else
1146
					@unlink($this->_cacheFile);
1147
				if ($this->_useIPv6 == true && $wan_ip > 0) {
1148
					$currentTime = time();
1149
					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));
1150
					log_error("phpDynDNS: updating cache file {$this->_cacheFile_v6}: {$wan_ip}");
1151
					@file_put_contents($this->_cacheFile_v6, "{$wan_ip}|{$currentTime}");
1152
				} else
1153
					@unlink($this->_cacheFile_v6);
1154
				conf_mount_ro();
1155
			}
1156
			$this->status = $status;
1157
			log_error($status);
1158
		}
1159

    
1160
		/*
1161
		 * Private Function (added 12 July 05) [beta]
1162
		 *   Return Error, Set Last Error, and Die.
1163
		 */
1164
		function _error($errorNumber = '1') {
1165
			switch ($errorNumber) {
1166
				case 0:
1167
					break;
1168
				case 2:
1169
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
1170
					break;
1171
				case 3:
1172
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
1173
					break;
1174
				case 4:
1175
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
1176
					break;
1177
				case 5:
1178
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
1179
					break;
1180
				case 6:
1181
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
1182
					break;
1183
				case 7:
1184
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
1185
					break;
1186
				case 8:
1187
					$status = "Route 53: (Error) Invalid ZoneID";
1188
					break;
1189
				case 9:
1190
					$status = "Route 53: (Error) Invalid TTL";
1191
					break;  
1192
				case 10:
1193
					$error = "phpDynDNS ({$this->_dnsHost}): No change in my IP address and/or " . $this->_dnsMaxCacheAgeDays . " days has not passed. Not updating dynamic DNS entry.";
1194
					break;
1195
				default:
1196
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
1197
					/* FIXME: $data isn't in scope here */
1198
					/* $this->_debug($data); */
1199
					break;
1200
			}
1201
			$this->lastError = $error;
1202
			log_error($error);
1203
		}
1204

    
1205
		/*
1206
		 * Private Function (added 12 July 05) [beta]
1207
		 *   - Detect whether or not IP needs to be updated.
1208
		 *      | Written Specifically for pfSense (pfsense.com) may
1209
		 *      | work with other systems. pfSense base is FreeBSD.
1210
		 */
1211
		function _detectChange() {
1212
			global $debug;
1213

    
1214
			if ($debug)
1215
				log_error("DynDns ({$this->_dnsHost}): _detectChange() starting.");
1216
		
1217
			$currentTime = time();
1218

    
1219
			$wan_ip = $this->_checkIP();
1220
			if ($wan_ip == 0) {
1221
				log_error("DynDns ({$this->_dnsHost}): Current WAN IP could not be determined, skipping update process.");
1222
				return false;
1223
			}
1224
			$log_error = "DynDns ({$this->_dnsHost}): Current WAN IP: {$wan_ip} ";
1225

    
1226
			if ($this->_useIPv6 == true) {
1227
				if (file_exists($this->_cacheFile_v6)) {
1228
					$contents = file_get_contents($this->_cacheFile_v6);
1229
					list($cacheIP,$cacheTime) = explode('|', $contents);
1230
					$this->_debug($cacheIP.'/'.$cacheTime);
1231
					$initial = false;
1232
					$log_error .= "Cached IPv6: {$cacheIP} ";
1233
				} else {
1234
					conf_mount_rw();
1235
					$cacheIP = '::';
1236
					@file_put_contents($this->_cacheFile, "::|{$currentTime}");
1237
					conf_mount_ro();
1238
					$cacheTime = $currentTime;
1239
					$initial = true;
1240
					$log_error .= "No Cached IPv6 found.";
1241
				}
1242
			} else {
1243
				if (file_exists($this->_cacheFile)) {
1244
					$contents = file_get_contents($this->_cacheFile);
1245
					list($cacheIP,$cacheTime) = explode(':', $contents);
1246
					$this->_debug($cacheIP.'/'.$cacheTime);
1247
					$initial = false;
1248
					$log_error .= "Cached IP: {$cacheIP} ";
1249
				} else {
1250
					conf_mount_rw();
1251
					$cacheIP = '0.0.0.0';
1252
					@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
1253
					conf_mount_ro();
1254
					$cacheTime = $currentTime;
1255
					$initial = true;
1256
					$log_error .= "No Cached IP found.";
1257
				}
1258
			}
1259
			if ($this->_dnsVerboseLog)
1260
				log_error($log_error);
1261

    
1262
			// Convert seconds = days * hr/day * min/hr * sec/min
1263
			$maxCacheAgeSecs = $this->_dnsMaxCacheAgeDays * 24 * 60 * 60;
1264

    
1265
			$needs_updating = FALSE;
1266
			/* lets determine if the item needs updating */
1267
			if ($cacheIP != $wan_ip) {
1268
				$needs_updating = true;
1269
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
1270
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
1271
			}
1272
			if (($currentTime - $cacheTime) > $maxCacheAgeSecs) {
1273
				$needs_updating = true;
1274
				$this->_forceUpdateNeeded = true;
1275
				$update_reason = "DynDns: More than " . $this->_dnsMaxCacheAgeDays . " days.  Updating. ";
1276
				$update_reason .= "{$currentTime} - {$cacheTime} > {$maxCacheAgeSecs} ";
1277
			}
1278
			if ($initial == true) {
1279
				$needs_updating = true;
1280
				$update_reason .= "Initial update. ";
1281
			}
1282

    
1283
			/*   finally if we need updating then store the
1284
			 *   new cache value and return true
1285
			 */
1286
			if ($needs_updating == true) {
1287
				if ($this->_dnsVerboseLog)
1288
					log_error("DynDns ({$this->_dnsHost}): {$update_reason}");
1289
				return true;
1290
			}
1291

    
1292
			return false;			
1293
		}
1294

    
1295
		/*
1296
		 * Private Function (added 16 July 05) [beta]
1297
		 *   - Writes debug information to a file.
1298
		 *   - This function is only called when a unknown response
1299
		 *   - status is returned from a DynDNS service provider.
1300
		 */
1301
		function _debug($data) {
1302
			global $g;
1303

    
1304
			if (!$g['debug'])
1305
				return;
1306
			$string = date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data."\n";
1307
			conf_mount_rw();
1308
			$file = fopen($this->_debugFile, 'a');
1309
			fwrite($file, $string);
1310
			fclose($file);
1311
			conf_mount_ro();
1312
		}
1313
		function _checkIP() {
1314
			global $debug;
1315

    
1316
			if ($debug)
1317
				log_error("DynDns ({$this->_dnsHost}): _checkIP() starting.");
1318

    
1319
			if ($this->_useIPv6 == true) {
1320
				$ip_address = find_interface_ipv6($this->_if);
1321
				if (!is_ipaddrv6($ip_address))
1322
					return 0;
1323
			} else {
1324
				$ip_address = find_interface_ip($this->_if);
1325
				if (!is_ipaddr($ip_address))
1326
					return 0;
1327
			}
1328
			if ($this->_useIPv6 == false && is_private_ip($ip_address)) {
1329
				$hosttocheck = "checkip.dyndns.org";
1330
				$try = 0;
1331
				while ($try < 3) {
1332
					$checkip = gethostbyname($hosttocheck);
1333
					if (is_ipaddr($checkip))
1334
						break;
1335
					$try++;
1336
				}
1337
				if ($try >= 3) {
1338
					log_error("Dyndns debug information ({$this->_dnsHost}): Could not resolve {$hosttocheck} to ip using interface ip {$ip_address}.");
1339
					return 0;
1340
				}
1341
				$ip_ch = curl_init("http://{$checkip}");
1342
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
1343
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
1344
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
1345
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
1346
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
1347
				$ip_result_page = curl_exec($ip_ch);
1348
				curl_close($ip_ch);
1349
				$ip_result_decoded = urldecode($ip_result_page);
1350
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
1351
				$ip_address = trim($matches[1]);
1352
				if (is_ipaddr($ip_address)) {
1353
					if ($this->_dnsVerboseLog)
1354
						log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from {$hosttocheck}");
1355
				} else {
1356
					log_error("DynDns ({$this->_dnsHost}): IP address could not be extracted from {$hosttocheck}");
1357
					return 0;
1358
				}
1359
			} else {
1360
				if ($this->_dnsVerboseLog)
1361
					log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from local system.");
1362
			}
1363
			$this->_dnsIP = $ip_address;
1364

    
1365
			return $ip_address;
1366
		}
1367

    
1368
	}
1369

    
1370
?>
(17-17/66)