Project

General

Profile

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

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

    
126
			$this->_dnsVerboseLog = $dnsVerboseLog;
127
			if ($this->_dnsVerboseLog)
128
				log_error("DynDns: updatedns() starting");
129

    
130
			$dyndnslck = lock("DDNS".$dnsID, LOCK_EX);
131

    
132
			if (!$dnsService) $this->_error(2);
133
			switch ($dnsService) {
134
			case 'freedns':
135
				if (!$dnsHost) $this->_error(5);
136
				break;
137
			case 'namecheap':
138
				if (!$dnsPass) $this->_error(4);
139
				if (!$dnsHost) $this->_error(5);
140
				break;
141
			case 'route53':
142
				if (!$dnsZoneID) $this->_error(8);
143
				if (!$dnsTTL) $this->_error(9);
144
				break;
145
			case 'custom':
146
				if (!$dnsUpdateURL) $this->_error(7);
147
				break;
148
			default:
149
				if (!$dnsUser) $this->_error(3);
150
				if (!$dnsPass) $this->_error(4);
151
				if (!$dnsHost) $this->_error(5);
152
			}
153
			
154
			$this->_dnsService = strtolower($dnsService);
155
			$this->_dnsUser = $dnsUser;
156
			$this->_dnsPass = $dnsPass;
157
			$this->_dnsHost = $dnsHost;
158
			$this->_dnsServer = $dnsServer;
159
			$this->_dnsPort = $dnsPort;
160
			$this->_dnsWildcard = $dnsWildcard;
161
			$this->_dnsMX = $dnsMX;
162
			$this->_dnsZoneID = $dnsZoneID;
163
			$this->_dnsTTL = $dnsTTL;
164
			$this->_if = get_failover_interface($dnsIf);
165
			$this->_checkIP();
166
			$this->_dnsUpdateURL = $dnsUpdateURL;
167
			$this->_dnsResultMatch = $dnsResultMatch;
168
			$this->_dnsRequestIf = get_failover_interface($dnsRequestIf);
169
			if ($this->_dnsVerboseLog)
170
				log_error("DynDNS ({$this->_dnsHost}): running get_failover_interface for {$dnsRequestIf}. found {$this->_dnsRequestIf}");
171
			$this->_dnsRequestIfIP = get_interface_ip($dnsRequestIf);
172
			$this->_dnsMaxCacheAgeDays = 25;
173
			$this->_dnsDummyUpdateDone = false;
174
			$this->_forceUpdateNeeded = $forceUpdate;
175
			
176
			// Ensure that we were able to lookup the IP
177
			if(!is_ipaddr($this->_dnsIP)) {
178
				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.");
179
				unlock($dyndnslck);
180
				return;
181
			}
182

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

    
229
			unlock($dyndnslck);
230
		}
231
			
232
		/*
233
		 * Private Function (added 12 July 05) [beta]
234
		 *   Send Update To Selected Service.
235
		 */
236
		function _update() {
237
		
238
			if ($this->_dnsVerboseLog)
239
				log_error("DynDNS ({$this->_dnsHost}): DynDns _update() starting.");
240
		
241
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53 ') {
242
				$ch = curl_init();
243
				curl_setopt($ch, CURLOPT_HEADER, 0);
244
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
245
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
246
				curl_setopt($ch, CURLOPT_INTERFACE, $this->_dnsRequestIfIP);
247
				curl_setopt($ch, CURLOPT_TIMEOUT, 120); // Completely empirical
248
			}
249

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

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

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

    
522
					/* Include Route 53 Library Class */
523
					require_once('/etc/inc/r53.class');
524

    
525
					/* Set Amazon AWS Credentials for this record */
526
					$r53 = new Route53($AccessKeyId, $SecretAccessKey);
527

    
528
					/* Function to find old values of records in Route 53 */
529
					if(!function_exists('Searchrecords')) {
530
						function SearchRecords($records, $name) {
531
							$result = array();
532
							foreach($records as $record) {
533
								if(strtolower($record['Name']) == strtolower($name)) {
534
									$result [] = $record;
535
								}
536
							}
537
							return ($result) ? $result : false;
538
						}
539
					}
540

    
541
					$records = $r53->listResourceRecordSets("/hostedzone/$ZoneID");
542

    
543
					/* Get IP for your hostname in Route 53 */
544
					if(false !== ($a_result = SearchRecords($records['ResourceRecordSets'], "$hostname"))) {
545
						$OldTTL=$a_result[0][TTL];
546
						$OldIP=$a_result[0][ResourceRecords][0];
547
					} else {
548
						$OldIP="";
549
					}
550

    
551
					/* Check if we need to update DNS Record */
552
					if ($OldIP !== $NewIP) {
553
						if(!empty($OldIP)) {
554
							/* Your Hostname already exists, deleting and creating it again */
555
							$changes = array();
556
							$changes[] = $r53->prepareChange(DELETE, $hostname, A, $OldTTL, $OldIP);
557
							$changes[] = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
558
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
559
						} else {
560
							/* Your Hostname does not exist yet, creating it */
561
							$changes = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
562
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
563
						}
564
					}
565
					$this->_checkStatus(0, $result);
566
					break;
567
				case 'custom':
568
					if ($this->_dnsVerboseLog)
569
						log_error("Custom DDNS ({$this->_dnsHost}): DNS update() starting.");
570
					if (strstr($this->dnsUpdateURL, "%IP%")) {$needsIP = TRUE;} else {$needsIP = FALSE;}
571
					if ($this->_dnsUser != '') {
572
						curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
573
						curl_setopt($ch, CURLOPT_USERPWD, "{$this->_dnsUser}:{$this->_dnsPass}");
574
					}
575
					$server = str_replace("%IP%", $this->_dnsIP, $this->_dnsUpdateURL);
576
					if ($this->_dnsVerboseLog)
577
						log_error("Sending request to: ".$server);
578
					curl_setopt($ch, CURLOPT_URL, $server);
579
					break;
580
				case 'cloudflare':
581
					$needsIP = TRUE;
582
					$dnsServer ='www.cloudflare.com';
583
					$dnsHost = str_replace(' ','', $this->_dnsHost);
584
					$URL = "https://{$dnsServer}/api.html?a=DIUP&email={$this->_dnsUser}&tkn={$this->_dnsPass}&ip={$this->dnsIP}&hosts={$dnsHost}";
585
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
586
					curl_setopt($ch, CURLOPT_URL, $URL);
587
					break;
588
				default:
589
					break;
590
			}
591
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53') {
592
				$data = curl_exec($ch);
593
				$this->_checkStatus($ch, $data);
594
				@curl_close($ch);
595
			}
596
		}
597

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

    
1015
					// loop over the lines
1016
					for ($pos=0; ($successful_update || $pos == 0) && $pos < $lines; $pos++){
1017
						$resp = $data[$pos];
1018
						if (preg_match('/UAUTH/i', $resp)) {
1019
							$status = "DynDNS: The username specified is not authorized to update this hostname and domain.";
1020
						} else if (preg_match('/NOHOST/i', $resp)) {
1021
							$status = "DynDNS: No valid FQDN (fully qualified domain name) was specified";
1022
						} else if (preg_match('/INVLDHST/i', $resp)) {
1023
							$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.";
1024
						} else if (preg_match('/INVLDIP/i', $resp)) {
1025
              $status = "DynDNS: The IP address given is not valid.";
1026
            } else if (preg_match('/DUPHST/i', $resp)) {
1027
              $status = "DynDNS: Duplicate values exist for a record. Only single values for records are supported currently.";
1028
            } else if (preg_match('/NOUPDATE/i', $resp)) {
1029
              $status = "DynDNS: No changes made to the hostname (".strtok($resp,' ')."). Continual updates with no changes lead to blocked clients.";
1030
              $successful_update = true; //success if it is the same so that it saves
1031
            } else if (preg_match('/OK/i', $resp)) {
1032
              $status = "DynDNS: (Success) (".strtok($resp,' ').") IP Address for Changed Successfully! ($IP)";
1033
              $successful_update = true;
1034
            } else {
1035
              $status = "DynDNS: (Unknown Response)";
1036
              log_error("DynDNS: PAYLOAD: {$resp}");
1037
              $this->_debug($resp);
1038
           	}
1039
            log_error($status);
1040
          }
1041
          break;
1042
			}
1043
			
1044
			if($successful_update == true) {
1045
				/* Write WAN IP to cache file */
1046
				$wan_ip = $this->_checkIP();
1047
				conf_mount_rw();
1048
				if ($wan_ip > 0) {
1049
					$currentTime = time();
1050
					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));
1051
					log_error("phpDynDNS: updating cache file {$this->_cacheFile}: {$wan_ip}");
1052
					@file_put_contents($this->_cacheFile, "{$wan_ip}:{$currentTime}");
1053
				} else
1054
					@unlink($this->_cacheFile);
1055
				conf_mount_ro();
1056
			}
1057
			$this->status = $status;
1058
			log_error($status);
1059
		}
1060

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

    
1106
		/*
1107
		 * Private Function (added 12 July 05) [beta]
1108
		 *   - Detect whether or not IP needs to be updated.
1109
		 *      | Written Specifically for pfSense (pfsense.com) may
1110
		 *      | work with other systems. pfSense base is FreeBSD.
1111
		 */
1112
		function _detectChange() {
1113
			global $debug;
1114

    
1115
			if ($debug)
1116
				log_error("DynDns ({$this->_dnsHost}): _detectChange() starting.");
1117
		
1118
			$currentTime = time();
1119

    
1120
			$wan_ip = $this->_checkIP();
1121
			if ($wan_ip == 0) {
1122
				log_error("DynDns ({$this->_dnsHost}): Current WAN IP could not be determined, skipping update process.");
1123
				return false;
1124
			}
1125
			$log_error = "DynDns ({$this->_dnsHost}): Current WAN IP: {$wan_ip} ";
1126

    
1127
			if (file_exists($this->_cacheFile)) {
1128
				$contents = file_get_contents($this->_cacheFile);
1129
				list($cacheIP,$cacheTime) = explode(':', $contents);
1130
				$this->_debug($cacheIP.'/'.$cacheTime);
1131
				$initial = false;
1132
				$log_error .= "Cached IP: {$cacheIP} ";
1133
			} else {
1134
				conf_mount_rw();
1135
				$cacheIP = '0.0.0.0';
1136
				@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
1137
				conf_mount_ro();
1138
				$cacheTime = $currentTime;
1139
				$initial = true;
1140
				$log_error .= "No Cached IP found.";
1141
			}
1142
			if ($this->_dnsVerboseLog)
1143
				log_error($log_error);
1144

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

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

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

    
1175
			return false;			
1176
		}
1177

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

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

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

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

    
1242
			return $ip_address;
1243
		}
1244

    
1245
	}
1246

    
1247
?>
(17-17/66)