Project

General

Profile

Download (46.8 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 $_dnsMaxCacheAgeDays;
100
		var $_dnsDummyUpdateDone;
101
		var $_forceUpdateNeeded;
102
		
103
		/* 
104
		 * Public Constructor Function (added 12 July 05) [beta]
105
		 *   - Gets the dice rolling for the update. 
106
		 *   - $dnsResultMatch should only be used with $dnsService = 'custom'
107
		 *   -  $dnsResultMatch is parsed for '%IP%', which is the IP the provider was updated to, 
108
		 *   -  it is otherwise expected to be exactly identical to what is returned by the Provider.
109
		 *   - $dnsUser, and $dnsPass indicate HTTP Auth for custom DNS, if they are needed in the URL (GET Variables), include them in $dnsUpdateURL.
110
		 *   - $For custom requests, $dnsUpdateURL is parsed for '%IP%', which is replaced with the new IP.
111
		 */
112
		function updatedns ($dnsService = '', $dnsHost = '', $dnsUser = '', $dnsPass = '',
113
				    $dnsWildcard = 'OFF', $dnsMX = '', $dnsIf = '', $dnsBackMX = '',
114
				    $dnsServer = '', $dnsPort = '', $dnsUpdateURL = '', $forceUpdate = false,
115
				    $dnsZoneID ='', $dnsTTL='', $dnsResultMatch = '', $dnsRequestIf = '',
116
				    $dnsID = '') {
117
			
118
			global $config, $g;
119
			
120
			$this->_cacheFile = "{$g['conf_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}.cache";
121
			$this->_debugFile = "{$g['varetc_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}.debug";
122

    
123
			log_error("DynDns: updatedns() starting");
124

    
125
			$dyndnslck = lock("DDNS".$dnsID, LOCK_EX);
126

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

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

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

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

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

    
422
				case 'staticcling':
423
					$needsIP = FALSE;
424
					curl_setopt($ch, CURLOPT_URL, 'http://www.staticcling.org/update.html?login='.$this->_dnsUser.'&pass='.$this->_dnsPass);
425
					break;	                    
426
				case 'dnsomatic':
427
					/* Example syntax 
428
						https://username:password@updates.dnsomatic.com/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG
429
					*/
430
					$needsIP = FALSE;
431
					log_error("DNS-O-Matic: DNS update() starting.");
432
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
433
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
434
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
435
					$server = "https://" . $this->_dnsUser . ":" . $this->_dnsPass . "@updates.dnsomatic.com/nic/update?hostname=";
436
					if($this->_dnsServer)
437
						$server = $this->_dnsServer;
438
					if($this->_dnsPort)
439
						$port = ":" . $this->_dnsPort;
440
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NOCHG');
441
					break;
442
				case 'namecheap':
443
					/* Example:
444
						https://dynamicdns.park-your-domain.com/update?host=[host_name]&domain=[domain.com]&password=[domain_password]&ip=[your_ip]
445
					*/
446
					$needsIP = FALSE;
447
					log_error("Namecheap ({$this->_dnsHost}): DNS update() starting.");
448
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
449
					$dparts = explode(".", trim($this->_dnsHost));
450
					$domain_part_count = ($dparts[count($dparts)-1] == "uk") ? 3 : 2;
451
					$domain_offset = count($dparts) - $domain_part_count;
452
					$hostname = implode(".", array_slice($dparts, 0, $domain_offset));
453
					$domain = implode(".", array_slice($dparts, $domain_offset));
454
					$dnspass = trim($this->_dnsPass);
455
					$server = "https://dynamicdns.park-your-domain.com/update?host={$hostname}&domain={$domain}&password={$dnspass}&ip={$this->_dnsIP}";
456
					curl_setopt($ch, CURLOPT_URL, $server);
457
					break;
458
				case 'he-net':
459
					$needsIP = FALSE;
460
					log_error("HE.net ({$this->_dnsHost}): DNS update() starting.");
461
					$server = "https://dyn.dns.he.net/nic/update?";
462
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
463
					curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
464
					curl_setopt($ch, CURLOPT_URL, $server . 'hostname=' . $this->_dnsHost . '&password=' . $this->_dnsPass . '&myip=' . $this->_dnsIP);
465
					break;
466
				case 'he-net-tunnelbroker':
467
					$needsIP = FALSE;
468
					log_error("HE.net Tunnelbroker: DNS update() starting.");
469
					$server = "https://ipv4.tunnelbroker.net/ipv4_end.php?";
470
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
471
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser . ':' . $this->_dnsPass);
472
					curl_setopt($ch, CURLOPT_URL, $server . 'tid=' . $this->_dnsHost);
473
					break;
474
				case 'selfhost':
475
					$needsIP = FALSE;
476
					//log_error("SelfHost: DNS update() starting.");
477
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
478
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
479
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
480
					$server = "https://carol.selfhost.de/nic/update";
481
					$port = "";
482
					if($this->_dnsServer)
483
						$server = $this->_dnsServer;
484
					if($this->_dnsPort)
485
						$port = ":" . $this->_dnsPort;
486
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
487
					break;
488
				case 'route53':
489
					log_error("Route53 ({$this->_dnsHost}): DNS update() starting.");
490
					
491
					/* Setting Variables */
492
					$hostname = "{$this->_dnsHost}.";
493
					$ZoneID = $this->_dnsZoneID;
494
					$AccessKeyId=$this->_dnsUser;
495
					$SecretAccessKey=$this->_dnsPass;
496
					$NewIP=$this->_dnsIP;
497
					$NewTTL=$this->_dnsTTL;
498

    
499
					/* Include Route 53 Library Class */
500
					require_once('/etc/inc/r53.class');
501

    
502
					/* Set Amazon AWS Credentials for this record */
503
					$r53 = new Route53($AccessKeyId, $SecretAccessKey);
504

    
505
					/* Function to find old values of records in Route 53 */
506
					if(!function_exists('Searchrecords')) {
507
						function SearchRecords($records, $name) {
508
							$result = array();
509
							foreach($records as $record) {
510
								if(strtolower($record['Name']) == strtolower($name)) {
511
									$result [] = $record;
512
								}
513
							}
514
							return ($result) ? $result : false;
515
						}
516
					}
517

    
518
					$records = $r53->listResourceRecordSets("/hostedzone/$ZoneID");
519

    
520
					/* Get IP for your hostname in Route 53 */
521
					if(false !== ($a_result = SearchRecords($records['ResourceRecordSets'], "$hostname"))) {
522
						$OldTTL=$a_result[0][TTL];
523
						$OldIP=$a_result[0][ResourceRecords][0];
524
					} else {
525
						$OldIP="";
526
					}
527

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

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

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

    
1039
		/*
1040
		 * Private Function (added 12 July 05) [beta]
1041
		 *   - Detect whether or not IP needs to be updated.
1042
		 *      | Written Specifically for pfSense (pfsense.com) may
1043
		 *      | work with other systems. pfSense base is FreeBSD.
1044
		 */
1045
		function _detectChange() {
1046
			global $debug;
1047

    
1048
			if ($debug)
1049
				log_error("DynDns ({$this->_dnsHost}): _detectChange() starting.");
1050
		
1051
			$currentTime = time();
1052

    
1053
			$wan_ip = $this->_checkIP();
1054
			if ($wan_ip == 0) {
1055
				log_error("DynDns ({$this->_dnsHost}): Current WAN IP could not be determined, skipping update process.");
1056
				return false;
1057
			}
1058
			$log_error = "DynDns ({$this->_dnsHost}): Current WAN IP: {$wan_ip} ";
1059

    
1060
			if (file_exists($this->_cacheFile)) {
1061
				$contents = file_get_contents($this->_cacheFile);
1062
				list($cacheIP,$cacheTime) = explode(':', $contents);
1063
				$this->_debug($cacheIP.'/'.$cacheTime);
1064
				$initial = false;
1065
				$log_error .= "Cached IP: {$cacheIP} ";
1066
			} else {
1067
				conf_mount_rw();
1068
				$cacheIP = '0.0.0.0';
1069
				@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
1070
				conf_mount_ro();
1071
				$cacheTime = $currentTime;
1072
				$initial = true;
1073
				$log_error .= "No Cached IP found.";
1074
			}
1075
			log_error($log_error);
1076

    
1077
			// Convert seconds = days * hr/day * min/hr * sec/min
1078
			$maxCacheAgeSecs = $this->_dnsMaxCacheAgeDays * 24 * 60 * 60;
1079

    
1080
			$needs_updating = FALSE;
1081
			/* lets determine if the item needs updating */
1082
			if ($cacheIP != $wan_ip) {
1083
				$needs_updating = true;
1084
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
1085
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
1086
			}
1087
			if (($currentTime - $cacheTime) > $maxCacheAgeSecs) {
1088
				$needs_updating = true;
1089
				$this->_forceUpdateNeeded = true;
1090
				$update_reason = "DynDns: More than " . $this->_dnsMaxCacheAgeDays . " days.  Updating. ";
1091
				$update_reason .= "{$currentTime} - {$cacheTime} > {$maxCacheAgeSecs} ";
1092
			}
1093
			if ($initial == true) {
1094
				$needs_updating = true;
1095
				$update_reason .= "Initial update. ";
1096
			}
1097

    
1098
			/*   finally if we need updating then store the
1099
			 *   new cache value and return true
1100
			 */
1101
			if ($needs_updating == true) {
1102
				log_error("DynDns debug information ({$this->_dnsHost}): {$update_reason}");
1103
				return true;
1104
			}
1105

    
1106
			return false;			
1107
		}
1108

    
1109
		/*
1110
		 * Private Function (added 16 July 05) [beta]
1111
		 *   - Writes debug information to a file.
1112
		 *   - This function is only called when a unknown response
1113
		 *   - status is returned from a DynDNS service provider.
1114
		 */
1115
		function _debug($data) {
1116
			global $g;
1117

    
1118
			if (!$g['debug'])
1119
				return;
1120
			$string = date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data."\n";
1121
			conf_mount_rw();
1122
			$file = fopen($this->_debugFile, 'a');
1123
			fwrite($file, $string);
1124
			fclose($file);
1125
			conf_mount_ro();
1126
		}
1127
		function _checkIP() {
1128
			global $debug;
1129

    
1130
			if ($debug)
1131
				log_error("DynDns ({$this->_dnsHost}): _checkIP() starting.");
1132

    
1133
			$ip_address = find_interface_ip($this->_if);
1134
			if (!is_ipaddr($ip_address))
1135
				return 0;
1136
			if (is_private_ip($ip_address)) {
1137
				$hosttocheck = "checkip.dyndns.org";
1138
				$try = 0;
1139
				while ($try < 3) {
1140
					$checkip = gethostbyname($hosttocheck);
1141
					if (is_ipaddr($checkip))
1142
						break;
1143
					$try++;
1144
				}
1145
				if ($try >= 3) {
1146
					log_error("Dyndns debug information ({$this->_dnsHost}): Could not resolve {$hosttocheck} to ip using interface ip {$ip_address}.");
1147
					return 0;
1148
				}
1149
				$ip_ch = curl_init("http://{$checkip}");
1150
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
1151
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
1152
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
1153
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
1154
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
1155
				$ip_result_page = curl_exec($ip_ch);
1156
				curl_close($ip_ch);
1157
				$ip_result_decoded = urldecode($ip_result_page);
1158
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
1159
				$ip_address = trim($matches[1]);
1160
				if (is_ipaddr($ip_address))
1161
					log_error("DynDns debug information ({$this->_dnsHost}): {$ip_address} extracted from {$hosttocheck}");
1162
				else {
1163
					log_error("DynDns debug information ({$this->_dnsHost}): IP address could not be extracted from {$hosttocheck}");
1164
					return 0;
1165
				}
1166
			} else
1167
				log_error("DynDns debug information ({$this->_dnsHost}): {$ip_address} extracted from local system.");
1168

    
1169
			$this->_dnsIP = $ip_address;
1170

    
1171
			return $ip_address;
1172
		}
1173

    
1174
	}
1175

    
1176
?>
(17-17/66)