Project

General

Profile

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

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

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

    
128
			$dyndnslck = lock("DDNS".$dnsID, LOCK_EX);
129

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

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

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

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

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

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

    
519
					/* Include Route 53 Library Class */
520
					require_once('/etc/inc/r53.class');
521

    
522
					/* Set Amazon AWS Credentials for this record */
523
					$r53 = new Route53($AccessKeyId, $SecretAccessKey);
524

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

    
538
					$records = $r53->listResourceRecordSets("/hostedzone/$ZoneID");
539

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

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

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

    
1018
		/*
1019
		 * Private Function (added 12 July 05) [beta]
1020
		 *   Return Error, Set Last Error, and Die.
1021
		 */
1022
		function _error($errorNumber = '1') {
1023
			switch ($errorNumber) {
1024
				case 0:
1025
					break;
1026
				case 2:
1027
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
1028
					break;
1029
				case 3:
1030
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
1031
					break;
1032
				case 4:
1033
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
1034
					break;
1035
				case 5:
1036
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
1037
					break;
1038
				case 6:
1039
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
1040
					break;
1041
				case 7:
1042
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
1043
					break;
1044
				case 8:
1045
					$status = "Route 53: (Error) Invalid ZoneID";
1046
					break;
1047
				case 9:
1048
					$status = "Route 53: (Error) Invalid TTL";
1049
					break;  
1050
				case 10:
1051
					$error = "phpDynDNS ({$this->_dnsHost}): No change in my IP address and/or " . $this->_dnsMaxCacheAgeDays . " days has not passed. Not updating dynamic DNS entry.";
1052
					break;
1053
				default:
1054
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
1055
					/* FIXME: $data isn't in scope here */
1056
					/* $this->_debug($data); */
1057
					break;
1058
			}
1059
			$this->lastError = $error;
1060
			log_error($error);
1061
		}
1062

    
1063
		/*
1064
		 * Private Function (added 12 July 05) [beta]
1065
		 *   - Detect whether or not IP needs to be updated.
1066
		 *      | Written Specifically for pfSense (pfsense.com) may
1067
		 *      | work with other systems. pfSense base is FreeBSD.
1068
		 */
1069
		function _detectChange() {
1070
			global $debug;
1071

    
1072
			if ($debug)
1073
				log_error("DynDns ({$this->_dnsHost}): _detectChange() starting.");
1074
		
1075
			$currentTime = time();
1076

    
1077
			$wan_ip = $this->_checkIP();
1078
			if ($wan_ip == 0) {
1079
				log_error("DynDns ({$this->_dnsHost}): Current WAN IP could not be determined, skipping update process.");
1080
				return false;
1081
			}
1082
			$log_error = "DynDns ({$this->_dnsHost}): Current WAN IP: {$wan_ip} ";
1083

    
1084
			if (file_exists($this->_cacheFile)) {
1085
				$contents = file_get_contents($this->_cacheFile);
1086
				list($cacheIP,$cacheTime) = explode(':', $contents);
1087
				$this->_debug($cacheIP.'/'.$cacheTime);
1088
				$initial = false;
1089
				$log_error .= "Cached IP: {$cacheIP} ";
1090
			} else {
1091
				conf_mount_rw();
1092
				$cacheIP = '0.0.0.0';
1093
				@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
1094
				conf_mount_ro();
1095
				$cacheTime = $currentTime;
1096
				$initial = true;
1097
				$log_error .= "No Cached IP found.";
1098
			}
1099
			if ($this->_dnsVerboseLog)
1100
				log_error($log_error);
1101

    
1102
			// Convert seconds = days * hr/day * min/hr * sec/min
1103
			$maxCacheAgeSecs = $this->_dnsMaxCacheAgeDays * 24 * 60 * 60;
1104

    
1105
			$needs_updating = FALSE;
1106
			/* lets determine if the item needs updating */
1107
			if ($cacheIP != $wan_ip) {
1108
				$needs_updating = true;
1109
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
1110
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
1111
			}
1112
			if (($currentTime - $cacheTime) > $maxCacheAgeSecs) {
1113
				$needs_updating = true;
1114
				$this->_forceUpdateNeeded = true;
1115
				$update_reason = "DynDns: More than " . $this->_dnsMaxCacheAgeDays . " days.  Updating. ";
1116
				$update_reason .= "{$currentTime} - {$cacheTime} > {$maxCacheAgeSecs} ";
1117
			}
1118
			if ($initial == true) {
1119
				$needs_updating = true;
1120
				$update_reason .= "Initial update. ";
1121
			}
1122

    
1123
			/*   finally if we need updating then store the
1124
			 *   new cache value and return true
1125
			 */
1126
			if ($needs_updating == true) {
1127
				if ($this->_dnsVerboseLog)
1128
					log_error("DynDns ({$this->_dnsHost}): {$update_reason}");
1129
				return true;
1130
			}
1131

    
1132
			return false;			
1133
		}
1134

    
1135
		/*
1136
		 * Private Function (added 16 July 05) [beta]
1137
		 *   - Writes debug information to a file.
1138
		 *   - This function is only called when a unknown response
1139
		 *   - status is returned from a DynDNS service provider.
1140
		 */
1141
		function _debug($data) {
1142
			global $g;
1143

    
1144
			if (!$g['debug'])
1145
				return;
1146
			$string = date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data."\n";
1147
			conf_mount_rw();
1148
			$file = fopen($this->_debugFile, 'a');
1149
			fwrite($file, $string);
1150
			fclose($file);
1151
			conf_mount_ro();
1152
		}
1153
		function _checkIP() {
1154
			global $debug;
1155

    
1156
			if ($debug)
1157
				log_error("DynDns ({$this->_dnsHost}): _checkIP() starting.");
1158

    
1159
			$ip_address = find_interface_ip($this->_if);
1160
			if (!is_ipaddr($ip_address))
1161
				return 0;
1162
			if (is_private_ip($ip_address)) {
1163
				$hosttocheck = "checkip.dyndns.org";
1164
				$try = 0;
1165
				while ($try < 3) {
1166
					$checkip = gethostbyname($hosttocheck);
1167
					if (is_ipaddr($checkip))
1168
						break;
1169
					$try++;
1170
				}
1171
				if ($try >= 3) {
1172
					log_error("Dyndns debug information ({$this->_dnsHost}): Could not resolve {$hosttocheck} to ip using interface ip {$ip_address}.");
1173
					return 0;
1174
				}
1175
				$ip_ch = curl_init("http://{$checkip}");
1176
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
1177
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
1178
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
1179
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
1180
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
1181
				$ip_result_page = curl_exec($ip_ch);
1182
				curl_close($ip_ch);
1183
				$ip_result_decoded = urldecode($ip_result_page);
1184
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
1185
				$ip_address = trim($matches[1]);
1186
				if (is_ipaddr($ip_address)) {
1187
					if ($this->_dnsVerboseLog)
1188
						log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from {$hosttocheck}");
1189
				} else {
1190
					log_error("DynDns ({$this->_dnsHost}): IP address could not be extracted from {$hosttocheck}");
1191
					return 0;
1192
				}
1193
			} else {
1194
				if ($this->_dnsVerboseLog)
1195
					log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from local system.");
1196
			}
1197
			$this->_dnsIP = $ip_address;
1198

    
1199
			return $ip_address;
1200
		}
1201

    
1202
	}
1203

    
1204
?>
(17-17/66)