Project

General

Profile

Download (47.3 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
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
442
					$server = "https://" . $this->_dnsUser . ":" . $this->_dnsPass . "@updates.dnsomatic.com/nic/update?hostname=";
443
					if($this->_dnsServer)
444
						$server = $this->_dnsServer;
445
					if($this->_dnsPort)
446
						$port = ":" . $this->_dnsPort;
447
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NOCHG');
448
					break;
449
				case 'namecheap':
450
					/* Example:
451
						https://dynamicdns.park-your-domain.com/update?host=[host_name]&domain=[domain.com]&password=[domain_password]&ip=[your_ip]
452
					*/
453
					$needsIP = FALSE;
454
					if ($this->_dnsVerboseLog)
455
						log_error("Namecheap ({$this->_dnsHost}): DNS update() starting.");
456
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
457
					$dparts = explode(".", trim($this->_dnsHost));
458
					$domain_part_count = ($dparts[count($dparts)-1] == "uk") ? 3 : 2;
459
					$domain_offset = count($dparts) - $domain_part_count;
460
					$hostname = implode(".", array_slice($dparts, 0, $domain_offset));
461
					$domain = implode(".", array_slice($dparts, $domain_offset));
462
					$dnspass = trim($this->_dnsPass);
463
					$server = "https://dynamicdns.park-your-domain.com/update?host={$hostname}&domain={$domain}&password={$dnspass}&ip={$this->_dnsIP}";
464
					curl_setopt($ch, CURLOPT_URL, $server);
465
					break;
466
				case 'he-net':
467
					$needsIP = FALSE;
468
					if ($this->_dnsVerboseLog)
469
						log_error("HE.net ({$this->_dnsHost}): DNS update() starting.");
470
					$server = "https://dyn.dns.he.net/nic/update?";
471
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
472
					curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
473
					curl_setopt($ch, CURLOPT_URL, $server . 'hostname=' . $this->_dnsHost . '&password=' . $this->_dnsPass . '&myip=' . $this->_dnsIP);
474
					break;
475
				case 'he-net-tunnelbroker':
476
					$needsIP = FALSE;
477
					if ($this->_dnsVerboseLog)
478
						log_error("HE.net Tunnelbroker: DNS update() starting.");
479
					$server = "https://ipv4.tunnelbroker.net/ipv4_end.php?";
480
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
481
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser . ':' . $this->_dnsPass);
482
					curl_setopt($ch, CURLOPT_URL, $server . 'tid=' . $this->_dnsHost);
483
					break;
484
				case 'selfhost':
485
					$needsIP = FALSE;
486
					if ($this->_dnsVerboseLog)
487
						log_error("SelfHost: DNS update() starting.");
488
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
489
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
490
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
491
					$server = "https://carol.selfhost.de/nic/update";
492
					$port = "";
493
					if($this->_dnsServer)
494
						$server = $this->_dnsServer;
495
					if($this->_dnsPort)
496
						$port = ":" . $this->_dnsPort;
497
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
498
					break;
499
				case 'route53':
500
					if ($this->_dnsVerboseLog)
501
						log_error("Route53 ({$this->_dnsHost}): DNS update() starting.");
502
					
503
					/* Setting Variables */
504
					$hostname = "{$this->_dnsHost}.";
505
					$ZoneID = $this->_dnsZoneID;
506
					$AccessKeyId=$this->_dnsUser;
507
					$SecretAccessKey=$this->_dnsPass;
508
					$NewIP=$this->_dnsIP;
509
					$NewTTL=$this->_dnsTTL;
510

    
511
					/* Include Route 53 Library Class */
512
					require_once('/etc/inc/r53.class');
513

    
514
					/* Set Amazon AWS Credentials for this record */
515
					$r53 = new Route53($AccessKeyId, $SecretAccessKey);
516

    
517
					/* Function to find old values of records in Route 53 */
518
					if(!function_exists('Searchrecords')) {
519
						function SearchRecords($records, $name) {
520
							$result = array();
521
							foreach($records as $record) {
522
								if(strtolower($record['Name']) == strtolower($name)) {
523
									$result [] = $record;
524
								}
525
							}
526
							return ($result) ? $result : false;
527
						}
528
					}
529

    
530
					$records = $r53->listResourceRecordSets("/hostedzone/$ZoneID");
531

    
532
					/* Get IP for your hostname in Route 53 */
533
					if(false !== ($a_result = SearchRecords($records['ResourceRecordSets'], "$hostname"))) {
534
						$OldTTL=$a_result[0][TTL];
535
						$OldIP=$a_result[0][ResourceRecords][0];
536
					} else {
537
						$OldIP="";
538
					}
539

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

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

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

    
1055
		/*
1056
		 * Private Function (added 12 July 05) [beta]
1057
		 *   - Detect whether or not IP needs to be updated.
1058
		 *      | Written Specifically for pfSense (pfsense.com) may
1059
		 *      | work with other systems. pfSense base is FreeBSD.
1060
		 */
1061
		function _detectChange() {
1062
			global $debug;
1063

    
1064
			if ($debug)
1065
				log_error("DynDns ({$this->_dnsHost}): _detectChange() starting.");
1066
		
1067
			$currentTime = time();
1068

    
1069
			$wan_ip = $this->_checkIP();
1070
			if ($wan_ip == 0) {
1071
				log_error("DynDns ({$this->_dnsHost}): Current WAN IP could not be determined, skipping update process.");
1072
				return false;
1073
			}
1074
			$log_error = "DynDns ({$this->_dnsHost}): Current WAN IP: {$wan_ip} ";
1075

    
1076
			if (file_exists($this->_cacheFile)) {
1077
				$contents = file_get_contents($this->_cacheFile);
1078
				list($cacheIP,$cacheTime) = explode(':', $contents);
1079
				$this->_debug($cacheIP.'/'.$cacheTime);
1080
				$initial = false;
1081
				$log_error .= "Cached IP: {$cacheIP} ";
1082
			} else {
1083
				conf_mount_rw();
1084
				$cacheIP = '0.0.0.0';
1085
				@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
1086
				conf_mount_ro();
1087
				$cacheTime = $currentTime;
1088
				$initial = true;
1089
				$log_error .= "No Cached IP found.";
1090
			}
1091
			if ($this->_dnsVerboseLog)
1092
				log_error($log_error);
1093

    
1094
			// Convert seconds = days * hr/day * min/hr * sec/min
1095
			$maxCacheAgeSecs = $this->_dnsMaxCacheAgeDays * 24 * 60 * 60;
1096

    
1097
			$needs_updating = FALSE;
1098
			/* lets determine if the item needs updating */
1099
			if ($cacheIP != $wan_ip) {
1100
				$needs_updating = true;
1101
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
1102
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
1103
			}
1104
			if (($currentTime - $cacheTime) > $maxCacheAgeSecs) {
1105
				$needs_updating = true;
1106
				$this->_forceUpdateNeeded = true;
1107
				$update_reason = "DynDns: More than " . $this->_dnsMaxCacheAgeDays . " days.  Updating. ";
1108
				$update_reason .= "{$currentTime} - {$cacheTime} > {$maxCacheAgeSecs} ";
1109
			}
1110
			if ($initial == true) {
1111
				$needs_updating = true;
1112
				$update_reason .= "Initial update. ";
1113
			}
1114

    
1115
			/*   finally if we need updating then store the
1116
			 *   new cache value and return true
1117
			 */
1118
			if ($needs_updating == true) {
1119
				if ($this->_dnsVerboseLog)
1120
					log_error("DynDns ({$this->_dnsHost}): {$update_reason}");
1121
				return true;
1122
			}
1123

    
1124
			return false;			
1125
		}
1126

    
1127
		/*
1128
		 * Private Function (added 16 July 05) [beta]
1129
		 *   - Writes debug information to a file.
1130
		 *   - This function is only called when a unknown response
1131
		 *   - status is returned from a DynDNS service provider.
1132
		 */
1133
		function _debug($data) {
1134
			global $g;
1135

    
1136
			if (!$g['debug'])
1137
				return;
1138
			$string = date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data."\n";
1139
			conf_mount_rw();
1140
			$file = fopen($this->_debugFile, 'a');
1141
			fwrite($file, $string);
1142
			fclose($file);
1143
			conf_mount_ro();
1144
		}
1145
		function _checkIP() {
1146
			global $debug;
1147

    
1148
			if ($debug)
1149
				log_error("DynDns ({$this->_dnsHost}): _checkIP() starting.");
1150

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

    
1191
			return $ip_address;
1192
		}
1193

    
1194
	}
1195

    
1196
?>
(17-17/66)